import { type ProcessManager, validateConfigPatch } from '../manager'; import { safeErrorMessage } from '../errorMessage'; import { WorkflowRuntimeUnavailableError, type WorkflowRuntimePort, } from '../workflow/runtime'; import { readLimitedJsonBody } from './jsonBody'; import { MANAGED_CONTROL_TARGET, type ServerControlTarget, } from './controlTarget'; import { statusWithHealth } from './controlStatus'; import type { AgentLifecyclePort } from './lifecycle'; import type { RouteResponse } from './types'; const MAX_CONFIG_BODY_BYTES = 64 * 1024; export async function routeControlRequest( request: Request, pathname: string, method: string, manager: ProcessManager, runtime: WorkflowRuntimePort, lifecycle?: AgentLifecyclePort, controlTarget: ServerControlTarget = MANAGED_CONTROL_TARGET ): Promise { if (pathname === '/control/status') { return coordinatedLease(lifecycle, async () => ({ status: 200, body: await statusWithHealth(manager, controlTarget), })); } if (pathname === '/control/logs') { return { status: 200, body: { lines: manager.getLogs() } }; } if (pathname === '/control/start' && method === 'POST') { assertManagedControl(controlTarget); return coordinated(lifecycle, async () => { await manager.start(); return { status: 200, body: await statusWithHealth(manager, controlTarget) }; }); } if (pathname === '/control/stop' && method === 'POST') { assertManagedControl(controlTarget); return coordinated(lifecycle, async () => { await stopWithRuntimeCleanup(manager, runtime); return { status: 200, body: await statusWithHealth(manager, controlTarget) }; }); } if (pathname === '/control/restart' && method === 'POST') { assertManagedControl(controlTarget); return coordinated(lifecycle, async () => { const closeResult = await settle(() => runtime.close()); if (!closeResult.ok) { await stopAfterCloseFailure(manager, workflowCloseError(closeResult.error)); } await manager.restart(); return { status: 200, body: await statusWithHealth(manager, controlTarget) }; }); } if (pathname === '/control/config' && method === 'GET') { return { status: 200, body: configSnapshot(manager) }; } if (pathname === '/control/config' && method === 'PUT') { assertManagedControl(controlTarget); const update = configUpdate( await readLimitedJsonBody(request, { scope: 'Agent configuration', maxBytes: MAX_CONFIG_BODY_BYTES, limitLabel: '64 KiB', }) ); return coordinated(lifecycle, async () => { manager.setConfig(update.patch, update.expectedRevision); return { status: 200, body: configSnapshot(manager) }; }); } return null; } function configSnapshot(manager: ProcessManager) { return { ...manager.getConfig(), configRevision: manager.getConfigRevision?.() ?? 0 }; } function configUpdate(value: unknown): { patch: ReturnType; expectedRevision?: number; } { if (value === null || typeof value !== 'object' || Array.isArray(value)) { return { patch: validateConfigPatch(value) }; } const input = { ...(value as Record) }; const hasExpectedRevision = Object.hasOwn(input, 'expectedRevision'); const hasConfigRevision = Object.hasOwn(input, 'configRevision'); const expected = hasExpectedRevision ? input.expectedRevision : hasConfigRevision ? input.configRevision : undefined; if ( hasExpectedRevision && hasConfigRevision && input.expectedRevision !== input.configRevision ) { throw new Error('expectedRevision and configRevision must match'); } delete input.expectedRevision; delete input.configRevision; if (expected !== undefined && (!Number.isSafeInteger(expected) || (expected as number) < 0)) { throw new Error('expectedRevision must be a non-negative safe integer'); } return { patch: validateConfigPatch(input), ...(expected === undefined ? {} : { expectedRevision: expected as number }), }; } export function assertManagedControl(controlTarget: ServerControlTarget): void { if (controlTarget.mode === 'external') { throw new Error( 'Server lifecycle is disabled in external mode (BUNQUEUE_MANAGED=0); manage this broker with its external supervisor' ); } } /** * Release the Workflow Engine's read-write SQLite connection before the data * file is replaced. A broker that crashed (or was killed externally) leaves the * engine open; after a restore it would keep writing to the unlinked file. */ export async function closeWorkflowRuntime(runtime: WorkflowRuntimePort): Promise { const closeResult = await settle(() => runtime.close()); if (!closeResult.ok) throw workflowCloseError(closeResult.error); } async function stopWithRuntimeCleanup( manager: ProcessManager, runtime: WorkflowRuntimePort ): Promise { const closeResult = await settle(() => runtime.close()); try { await manager.stop(); } catch (stopFailure) { if (!closeResult.ok) throw combinedShutdownError(closeResult.error, stopFailure); throw stopFailure; } if (!closeResult.ok) throw workflowCloseError(closeResult.error); } async function stopAfterCloseFailure( manager: ProcessManager, closeFailure: unknown ): Promise { try { await manager.stop(); } catch (stopFailure) { throw combinedShutdownError(closeFailure, stopFailure); } throw closeFailure; } function combinedShutdownError(closeFailure: unknown, stopFailure: unknown): Error { return new WorkflowRuntimeUnavailableError( `Workflow Engine shutdown failed (${message(closeFailure)}); ` + `managed server stop also failed: ${message(stopFailure)}` ); } function workflowCloseError(error: unknown): WorkflowRuntimeUnavailableError { try { if (error instanceof WorkflowRuntimeUnavailableError) return error; } catch { // A hostile thrown Proxy must still become an unavailable-runtime error. } return new WorkflowRuntimeUnavailableError(`Workflow Engine shutdown failed: ${message(error)}`); } function message(error: unknown): string { return safeErrorMessage(error); } function coordinated( lifecycle: AgentLifecyclePort | undefined, operation: () => Promise ): Promise { return lifecycle ? lifecycle.run(operation) : operation(); } function coordinatedLease( lifecycle: AgentLifecyclePort | undefined, operation: () => Promise ): Promise { return lifecycle ? lifecycle.lease(operation) : operation(); } type SettledOperation = { ok: true } | { ok: false; error: unknown }; async function settle(operation: () => Promise): Promise { try { await operation(); return { ok: true }; } catch (error) { return { ok: false, error }; } }