diff --git a/packages/cli/src/serve/fast-path.test.ts b/packages/cli/src/serve/fast-path.test.ts index 74945437d43..259380d9fc6 100644 --- a/packages/cli/src/serve/fast-path.test.ts +++ b/packages/cli/src/serve/fast-path.test.ts @@ -514,6 +514,7 @@ describe('CLI entry import boundary', () => { '@qwen-code/acp-bridge/spawnChannel', '@qwen-code/acp-bridge/bridgeClient', '@qwen-code/acp-bridge/bridgeErrors', + '@qwen-code/qwen-code-core', ]; const forbiddenImports = [...graph.externalValueImports].filter( (specifier) => forbiddenExternalImports.includes(specifier), diff --git a/packages/cli/src/serve/run-qwen-serve.test.ts b/packages/cli/src/serve/run-qwen-serve.test.ts index 870d15c919b..a8d7828e50d 100644 --- a/packages/cli/src/serve/run-qwen-serve.test.ts +++ b/packages/cli/src/serve/run-qwen-serve.test.ts @@ -85,6 +85,12 @@ import { type WorkspaceRegistrationStore, } from './workspace-registration-store.js'; import type { WorkspaceRegistry } from './workspace-registry.js'; +import { + ENSURE_KEEP_ALIVE_MS, + WorkspaceRuntimeCoordinator, + WorkspaceRuntimeInitializationError, + WorkspaceRuntimeStillStartingError, +} from './workspace-runtime-coordinator.js'; import { getDeferredRuntimeRequestTiming } from './server/request-helpers.js'; import type { WorkspaceFileSystemFactory } from './fs/workspace-file-system.js'; import { ConversationWorkspace } from './conversations/conversation-workspace.js'; @@ -16790,6 +16796,51 @@ describe('runQwenServe startup observability', () => { throw new Error(`preheat status did not become ${status}`); } + async function waitForDaemonLog( + logBaseDir: string, + needle: string, + ): Promise { + const daemonDir = path.join(logBaseDir, 'daemon'); + for (let i = 0; i < 50; i++) { + if (fs.existsSync(daemonDir)) { + const logFile = fs + .readdirSync(daemonDir) + .find((file) => file.endsWith('.log')); + if (logFile) { + const content = fs.readFileSync( + path.join(daemonDir, logFile), + 'utf8', + ); + if (content.includes(needle)) return content; + } + } + await new Promise((resolve) => setTimeout(resolve, 10)); + } + throw new Error(`daemon log did not contain: ${needle}`); + } + + function lifecycleBridgeExtras() { + return { + getWorkspaceRuntimeLifecycleSnapshot: vi.fn().mockReturnValue({ + state: 'idle', + runtimeLive: true, + runtimeEpoch: 1, + activeWork: false, + }), + initializeWorkspaceMcp: vi.fn().mockResolvedValue({ accepted: true }), + queryWorkspaceStatus: vi.fn(async () => ({ + v: 1, + workspaceCwd: tmpDir, + initialized: true, + runtimeEpoch: 1, + source: 'live', + discoveryState: 'completed', + servers: [], + skills: [], + })), + }; + } + function installInternalBridge(preheat: () => Promise): HttpAcpBridge { const bridge = makeFakeBridge(); vi.mocked(bridge.preheat).mockImplementation(preheat); @@ -16993,6 +17044,575 @@ describe('runQwenServe startup observability', () => { } }); + it('starts workspace MCP discovery after ACP preheat succeeds', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-config-')), + ); + let resolvePreheat!: () => void; + const preheatPromise = new Promise((resolve) => { + resolvePreheat = resolve; + }); + const bridge = installInternalBridge(() => preheatPromise); + Object.assign(bridge, lifecycleBridgeExtras()); + const ensureSpy = vi.spyOn(WorkspaceRuntimeCoordinator.prototype, 'ensure'); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true }, + ); + + try { + await waitForPreheatStatus(handle, 'running'); + expect(ensureSpy).not.toHaveBeenCalled(); + expect(bridge.preheat).toHaveBeenCalledTimes(1); + expect(bridge.preheat).toHaveBeenCalledWith(); + + resolvePreheat(); + expect(await waitForPreheatStatus(handle, 'succeeded')).toMatchObject({ + status: 'succeeded', + }); + await vi.waitFor(() => expect(ensureSpy).toHaveBeenCalledOnce()); + expect(ensureSpy).toHaveBeenCalledWith({ + keepAliveMs: ENSURE_KEEP_ALIVE_MS, + }); + expect(ensureSpy.mock.instances[0]).toMatchObject({ + runtime: expect.objectContaining({ workspaceCwd: tmpDir }), + }); + expect(bridge.preheat).toHaveBeenCalledTimes(2); + expect(bridge.preheat).toHaveBeenNthCalledWith(1); + expect(bridge.preheat).toHaveBeenNthCalledWith(2, { + keepAliveMs: ENSURE_KEEP_ALIVE_MS, + }); + } finally { + await handle.close(); + } + }); + + it('does not leak an unhandled rejection when boot MCP discovery fails', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-reject-')), + ); + const logBaseDir = path.join(tmpDir, 'debug'); + const bridge = installInternalBridge(() => Promise.resolve()); + Object.assign(bridge, lifecycleBridgeExtras()); + const ensureSpy = vi + .spyOn(WorkspaceRuntimeCoordinator.prototype, 'ensure') + .mockImplementation(async () => { + throw new WorkspaceRuntimeInitializationError( + new Error('cause detail'), + ); + }); + const rejections: unknown[] = []; + const recordRejection = (reason: unknown) => rejections.push(reason); + process.on('unhandledRejection', recordRejection); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true, daemonLogBaseDir: logBaseDir }, + ); + + try { + expect(await waitForPreheatStatus(handle, 'succeeded')).toMatchObject({ + status: 'succeeded', + }); + await vi.waitFor(() => expect(ensureSpy).toHaveBeenCalledOnce()); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(rejections).toEqual([]); + await waitForDaemonLog( + logBaseDir, + 'workspace runtime ensure after preheat failed: cause detail', + ); + } finally { + process.off('unhandledRejection', recordRejection); + await handle.close(); + } + }); + + it('logs the deepest cause from a multi-hop boot runtime ensure failure', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-deep-cause-')), + ); + const logBaseDir = path.join(tmpDir, 'debug'); + const bridge = installInternalBridge(() => Promise.resolve()); + Object.assign(bridge, lifecycleBridgeExtras()); + vi.spyOn( + WorkspaceRuntimeCoordinator.prototype, + 'ensure', + ).mockImplementation(async () => { + throw new Error('wrapper', { + cause: new Error('middle', { cause: new Error('deepest leaf') }), + }); + }); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true, daemonLogBaseDir: logBaseDir }, + ); + + try { + expect(await waitForPreheatStatus(handle, 'succeeded')).toMatchObject({ + status: 'succeeded', + }); + const logContent = await waitForDaemonLog( + logBaseDir, + 'workspace runtime ensure after preheat failed: deepest leaf', + ); + expect(logContent).not.toContain('wrapper'); + expect(logContent).not.toContain('middle'); + } finally { + await handle.close(); + } + }); + + it('logs non-Error boot runtime ensure failures via String(err)', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-string-err-')), + ); + const logBaseDir = path.join(tmpDir, 'debug'); + const bridge = installInternalBridge(() => Promise.resolve()); + Object.assign(bridge, lifecycleBridgeExtras()); + vi.spyOn( + WorkspaceRuntimeCoordinator.prototype, + 'ensure', + ).mockImplementation(async () => { + // eslint-disable-next-line no-restricted-syntax -- pins String(err) fallback + throw 'plain string failure'; + }); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true, daemonLogBaseDir: logBaseDir }, + ); + + try { + expect(await waitForPreheatStatus(handle, 'succeeded')).toMatchObject({ + status: 'succeeded', + }); + await waitForDaemonLog( + logBaseDir, + 'workspace runtime ensure after preheat failed: plain string failure', + ); + } finally { + await handle.close(); + } + }); + + it('logs cause-less boot runtime ensure failures without an undefined suffix', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-no-cause-')), + ); + const logBaseDir = path.join(tmpDir, 'debug'); + const bridge = installInternalBridge(() => Promise.resolve()); + Object.assign(bridge, lifecycleBridgeExtras()); + vi.spyOn( + WorkspaceRuntimeCoordinator.prototype, + 'ensure', + ).mockImplementation(async () => { + throw new WorkspaceRuntimeStillStartingError(); + }); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true, daemonLogBaseDir: logBaseDir }, + ); + + try { + expect(await waitForPreheatStatus(handle, 'succeeded')).toMatchObject({ + status: 'succeeded', + }); + const logContent = await waitForDaemonLog( + logBaseDir, + 'workspace runtime ensure after preheat failed: Workspace runtime is still starting', + ); + expect(logContent).not.toContain('(undefined)'); + } finally { + await handle.close(); + } + }); + + it('logs when boot MCP discovery skips because lifecycle is unsupported', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-no-lifecycle-')), + ); + const logBaseDir = path.join(tmpDir, 'debug'); + const bridge = installInternalBridge(() => Promise.resolve()); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true, daemonLogBaseDir: logBaseDir }, + ); + + try { + expect(await waitForPreheatStatus(handle, 'succeeded')).toMatchObject({ + status: 'succeeded', + }); + await waitForDaemonLog( + logBaseDir, + 'workspace runtime ensure after preheat skipped: ' + + 'workspace runtime lifecycle is not supported', + ); + expect(bridge.preheat).toHaveBeenCalledOnce(); + } finally { + await handle.close(); + } + }); + + it('logs when boot ensure skips because primary workspace is not active', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync( + path.join(os.tmpdir(), 'qws-startup-mcp-inactive-primary-'), + ), + ); + const logBaseDir = path.join(tmpDir, 'debug'); + let resolvePreheat!: () => void; + const preheatPromise = new Promise((resolve) => { + resolvePreheat = resolve; + }); + const bridge = installInternalBridge(() => preheatPromise); + Object.assign(bridge, lifecycleBridgeExtras()); + const ensureSpy = vi.spyOn(WorkspaceRuntimeCoordinator.prototype, 'ensure'); + let workspaceRegistry: WorkspaceRegistry | undefined; + const originalCreateServeApp = serverModule.createServeApp; + vi.spyOn(serverModule, 'createServeApp').mockImplementation((...args) => { + workspaceRegistry = args[2]?.workspaceRegistry; + return originalCreateServeApp(...args); + }); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true, daemonLogBaseDir: logBaseDir }, + ); + + try { + await waitForPreheatStatus(handle, 'running'); + const previousState = workspaceRegistry!.primaryEntry.state; + workspaceRegistry!.primaryEntry.state = 'draining'; + resolvePreheat(); + try { + await waitForDaemonLog( + logBaseDir, + 'workspace runtime ensure after preheat skipped: no primary runtime', + ); + expect(ensureSpy).not.toHaveBeenCalled(); + } finally { + workspaceRegistry!.primaryEntry.state = previousState; + } + } finally { + await handle.close(); + } + }); + + it('logs when boot MCP discovery skips because primary runtime is gone', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-no-runtime-')), + ); + const logBaseDir = path.join(tmpDir, 'debug'); + let resolvePreheat!: () => void; + const preheatPromise = new Promise((resolve) => { + resolvePreheat = resolve; + }); + installInternalBridge(() => preheatPromise); + let workspaceRegistry: WorkspaceRegistry | undefined; + const originalCreateServeApp = serverModule.createServeApp; + vi.spyOn(serverModule, 'createServeApp').mockImplementation((...args) => { + workspaceRegistry = args[2]?.workspaceRegistry; + return originalCreateServeApp(...args); + }); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true, daemonLogBaseDir: logBaseDir }, + ); + + try { + await waitForPreheatStatus(handle, 'running'); + const current = workspaceRegistry!.primaryEntry.current; + delete workspaceRegistry!.primaryEntry.current; + resolvePreheat(); + try { + await waitForDaemonLog( + logBaseDir, + 'workspace runtime ensure after preheat skipped: no primary runtime', + ); + } finally { + workspaceRegistry!.primaryEntry.current = current; + } + } finally { + await handle.close(); + } + }); + + it('does not schedule workspace MCP discovery when ACP preheat fails', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-preheat-fail-')), + ); + const bridge = installInternalBridge(() => + Promise.reject(new Error('preheat boom')), + ); + Object.assign(bridge, lifecycleBridgeExtras()); + const ensureSpy = vi.spyOn(WorkspaceRuntimeCoordinator.prototype, 'ensure'); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true }, + ); + + try { + expect(await waitForPreheatStatus(handle, 'failed')).toMatchObject({ + status: 'failed', + error: 'preheat boom', + }); + await new Promise((resolve) => setTimeout(resolve, 50)); + expect(ensureSpy).not.toHaveBeenCalled(); + } finally { + await handle.close(); + } + }); + + it('starts workspace MCP discovery only for the primary workspace', async () => { + const root = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-primary-only-')), + ); + const primary = path.join(root, 'primary'); + const secondary = path.join(root, 'secondary'); + fs.mkdirSync(primary, { recursive: true }); + fs.mkdirSync(secondary, { recursive: true }); + tmpDir = root; + const bridge = installInternalBridge(() => Promise.resolve()); + Object.assign(bridge, { + ...lifecycleBridgeExtras(), + queryWorkspaceStatus: vi.fn(async () => ({ + v: 1, + workspaceCwd: primary, + initialized: true, + runtimeEpoch: 1, + source: 'live', + discoveryState: 'completed', + servers: [], + skills: [], + })), + }); + const ensureSpy = vi.spyOn(WorkspaceRuntimeCoordinator.prototype, 'ensure'); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: [primary, secondary], + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true }, + ); + + try { + expect(await waitForPreheatStatus(handle, 'succeeded')).toMatchObject({ + status: 'succeeded', + }); + await vi.waitFor(() => expect(ensureSpy).toHaveBeenCalledOnce()); + expect(ensureSpy.mock.instances[0]).toMatchObject({ + runtime: expect.objectContaining({ + workspaceCwd: canonicalizeWorkspace(primary), + }), + }); + } finally { + await handle.close(); + } + }); + + it('does not schedule workspace MCP discovery when shutting down during preheat', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-shutdown-')), + ); + let resolvePreheat!: () => void; + const preheatPromise = new Promise((resolve) => { + resolvePreheat = resolve; + }); + const bridge = installInternalBridge(() => preheatPromise); + Object.assign(bridge, lifecycleBridgeExtras()); + const ensureSpy = vi.spyOn(WorkspaceRuntimeCoordinator.prototype, 'ensure'); + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + }, + { preheatBridge: true }, + ); + + try { + await waitForPreheatStatus(handle, 'running'); + await handle.close(); + resolvePreheat(); + await new Promise((resolve) => setTimeout(resolve, 50)); + expect(ensureSpy).not.toHaveBeenCalled(); + } finally { + await handle.close(); + } + }); + + it('does not schedule workspace MCP discovery after runtime startup fails', async () => { + tmpDir = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-mcp-runtime-fail-')), + ); + const logBaseDir = path.join(tmpDir, 'debug'); + let resolvePreheat!: () => void; + const preheatPromise = new Promise((resolve) => { + resolvePreheat = resolve; + }); + const bridge = installInternalBridge(() => preheatPromise); + Object.assign(bridge, lifecycleBridgeExtras()); + const ensureSpy = vi.spyOn(WorkspaceRuntimeCoordinator.prototype, 'ensure'); + const stderrWrites: string[] = []; + vi.spyOn(process.stderr, 'write').mockImplementation((chunk) => { + stderrWrites.push(String(chunk)); + return true; + }); + let releaseWorkerStart!: () => void; + const workerStartGate = new Promise((resolve) => { + releaseWorkerStart = resolve; + }); + const worker = { + start: vi.fn(async () => { + await workerStartGate; + throw new Error('worker failed before ready'); + }), + stop: vi.fn().mockResolvedValue(undefined), + restart: vi.fn(), + killAllSync: vi.fn(), + snapshot: vi.fn( + (): ChannelWorkerSnapshot => ({ + enabled: true, + state: 'failed', + channels: ['telegram'], + exitCode: 1, + }), + ), + enqueueWebhookTask: vi.fn(), + deliverChannelMessage: vi.fn(), + }; + + const handle = await runQwenServe( + { + port: 0, + hostname: '127.0.0.1', + mode: 'http-bridge', + workspace: tmpDir, + maxSessions: 1, + serveWebShell: false, + channelSelection: { mode: 'names', names: ['telegram'] }, + }, + { + preheatBridge: true, + resolveOnListen: true, + daemonLogBaseDir: logBaseDir, + channelWorkerSupervisorFactory: vi.fn(() => worker), + }, + ); + + try { + await vi.waitFor(() => expect(bridge.preheat).toHaveBeenCalledOnce()); + releaseWorkerStart(); + await vi.waitFor(() => { + if ( + !stderrWrites.join('').includes('qwen serve: runtime startup failed') + ) { + throw new Error('waiting for runtime startup failure'); + } + }); + resolvePreheat(); + await vi.waitFor(() => { + if (ensureSpy.mock.calls.length > 0) { + throw new Error( + 'ensure was scheduled despite runtime startup failure', + ); + } + const stderr = stderrWrites.join(''); + if ( + !stderr.includes( + 'workspace runtime ensure after preheat skipped: runtime startup failed', + ) + ) { + throw new Error('waiting for runtime startup skip reason'); + } + }); + expect(ensureSpy).not.toHaveBeenCalled(); + await expect(handle.runtimeReady).rejects.toThrow( + 'worker failed before ready', + ); + } finally { + await handle.close(); + } + }); + it('tracks preheat running and succeeded states for an internally-created bridge', async () => { tmpDir = fs.realpathSync( fs.mkdtempSync(path.join(os.tmpdir(), 'qws-startup-preheat-')), diff --git a/packages/cli/src/serve/run-qwen-serve.ts b/packages/cli/src/serve/run-qwen-serve.ts index 3a8a937c6f1..18da4dce655 100644 --- a/packages/cli/src/serve/run-qwen-serve.ts +++ b/packages/cli/src/serve/run-qwen-serve.ts @@ -80,6 +80,8 @@ import type { TelemetryRuntimeConfig, TelemetrySettings, } from '@qwen-code/qwen-code-core'; +// Named subpath: the core barrel pulls shell/glob/chokidar into the serve +// pre-listen static closure. import { PRIVATE_CONVERSATIONS_RUNTIME_ENABLE, PRIVATE_CONVERSATIONS_RUNTIME_ENV, @@ -267,6 +269,18 @@ const QWEN_SERVE_WRITER_IDLE_TIMEOUT_MS_ENV = const SHUTDOWN_FORCE_CLOSE_MS = 5_000; const DAEMON_LOG_FORCED_FLUSH_BUDGET_MS = 250; const DEFAULT_LIVE_DISCOVERY_RETRY_MS = 5_000; +// Must match workspace-runtime-coordinator ENSURE_KEEP_ALIVE_MS. Defined +// here so the serve pre-listen graph does not statically import that module. +const ENSURE_KEEP_ALIVE_MS = 10 * 60_000; + +function deepestErrorMessage(err: unknown): string { + if (!(err instanceof Error)) return String(err); + let deepest: Error = err; + while (deepest.cause instanceof Error) { + deepest = deepest.cause; + } + return deepest.message; +} function channelDeliveryPublicError( code: Extract['code'], @@ -8970,7 +8984,59 @@ async function runQwenServeImpl( clearRuntimeStartupTimer(); markRuntimeReady(); }; - const startBridgePreheat = (bridge: AcpSessionBridge): void => { + const scheduleWorkspaceMcpDiscoveryAfterPreheat = ( + app: Application, + ): void => { + if (runtimeStartupError !== undefined) { + daemonLog.info( + 'workspace runtime ensure after preheat skipped: runtime startup failed', + ); + return; + } + if (shuttingDown) { + daemonLog.info( + 'workspace runtime ensure after preheat skipped: shutting down', + ); + return; + } + const registry = app.locals?.['workspaceRegistry'] as + | WorkspaceRegistry + | undefined; + const primaryEntry = registry?.primaryEntry; + const runtime = + primaryEntry?.state === 'active' + ? primaryEntry.current?.runtime + : undefined; + if (!runtime) { + daemonLog.info( + 'workspace runtime ensure after preheat skipped: no primary runtime', + ); + return; + } + const coordinator = + getWorkspaceRuntimeCoordinatorIfSupported?.(runtime); + if (!coordinator) { + daemonLog.info( + 'workspace runtime ensure after preheat skipped: ' + + 'workspace runtime lifecycle is not supported', + ); + return; + } + void coordinator + .ensure({ keepAliveMs: ENSURE_KEEP_ALIVE_MS }) + .catch((err) => { + if (shuttingDown || runtimeStartupError !== undefined) { + return; + } + daemonLog.warn( + `workspace runtime ensure after preheat failed: ${deepestErrorMessage(err)}`, + ); + }); + }; + const startBridgePreheat = ( + bridge: AcpSessionBridge, + app: Application, + ): void => { startup.preheat.status = 'running'; const preheatStartedAt = performance.now(); bridge @@ -8980,6 +9046,7 @@ async function runQwenServeImpl( startup.preheat.durationMs = Math.round( performance.now() - preheatStartedAt, ); + scheduleWorkspaceMcpDiscoveryAfterPreheat(app); }) .catch((err) => { const message = err instanceof Error ? err.message : String(err); @@ -9015,7 +9082,7 @@ async function runQwenServeImpl( return; } if (shouldPreheat && runtime.bridge) { - startBridgePreheat(runtime.bridge); + startBridgePreheat(runtime.bridge, runtime.app); } await completeRuntimeStartup(runtime.app); }) @@ -9570,7 +9637,7 @@ async function runQwenServeImpl( if (preparedRuntimeApp && bridgeRef && deps.bridge) { attachLiveDiscoveryControl(preparedRuntimeApp); if (shouldPreheat) { - startBridgePreheat(bridgeRef); + startBridgePreheat(bridgeRef, preparedRuntimeApp); } if (opts.channelSelection && !runtimeStartupSettled) { armRuntimeStartupTimer(); diff --git a/packages/cli/src/serve/workspace-runtime-coordinator.test.ts b/packages/cli/src/serve/workspace-runtime-coordinator.test.ts index 271613d32b3..bd47069666c 100644 --- a/packages/cli/src/serve/workspace-runtime-coordinator.test.ts +++ b/packages/cli/src/serve/workspace-runtime-coordinator.test.ts @@ -13,6 +13,7 @@ import { } from './acp-session-bridge.js'; import type { WorkspaceRuntime } from './workspace-registry.js'; import { + ENSURE_KEEP_ALIVE_MS, getWorkspaceRuntimeCoordinator, getWorkspaceRuntimeCoordinatorIfSupported, WorkspaceRuntimeCoordinator, @@ -816,6 +817,177 @@ describe('WorkspaceRuntimeCoordinator', () => { expect(harness.getWorkspaceSkillsRuntimeStatus).toHaveBeenCalledOnce(); }); + it('skips keep-alive preheat when preparing an already-live runtime', async () => { + const harness = makeRuntime(); + harness.setSnapshot({ + state: 'idle', + runtimeLive: true, + runtimeEpoch: 1, + }); + harness.getWorkspaceMcpStatus + .mockResolvedValueOnce({ + v: 1, + workspaceCwd: '/workspace', + initialized: true, + runtimeEpoch: 1, + source: 'live', + discoveryState: 'not_started', + servers: [], + }) + .mockResolvedValue({ + v: 1, + workspaceCwd: '/workspace', + initialized: true, + runtimeEpoch: 1, + source: 'live', + discoveryState: 'completed', + servers: [], + }); + const coordinator = getWorkspaceRuntimeCoordinator(harness.runtime); + + await coordinator.ensure({}); + + expect(harness.preheat).not.toHaveBeenCalled(); + expect(harness.initializeWorkspaceMcp).toHaveBeenCalledOnce(); + }); + + it('skips keep-alive preheat when skipKeepAlivePreheat is true', async () => { + const harness = makeRuntime(); + harness.setSnapshot({ + state: 'idle', + runtimeLive: true, + runtimeEpoch: 1, + }); + const coordinator = getWorkspaceRuntimeCoordinator(harness.runtime); + + await coordinator.ensure({ skipKeepAlivePreheat: true }); + + expect(harness.preheat).not.toHaveBeenCalled(); + }); + + it('keep-alive preheats a live runtime when skipKeepAlivePreheat is false', async () => { + const harness = makeRuntime(); + harness.setSnapshot({ + state: 'idle', + runtimeLive: true, + runtimeEpoch: 1, + }); + const coordinator = getWorkspaceRuntimeCoordinator(harness.runtime); + + await coordinator.ensure({ skipKeepAlivePreheat: false }); + + expect(harness.preheat).toHaveBeenCalledOnce(); + expect(harness.preheat).toHaveBeenCalledWith({ + keepAliveMs: ENSURE_KEEP_ALIVE_MS, + }); + }); + + it('keep-alive preheats a cold runtime for object-form ensure({})', async () => { + const harness = makeRuntime(); + harness.setSnapshot({ + state: 'cold', + runtimeLive: false, + runtimeEpoch: 0, + }); + const coordinator = getWorkspaceRuntimeCoordinator(harness.runtime); + + await coordinator.ensure({}); + + expect(harness.preheat).toHaveBeenCalledOnce(); + expect(harness.preheat).toHaveBeenCalledWith({ + keepAliveMs: ENSURE_KEEP_ALIVE_MS, + }); + }); + + it('reaps a live child before MCP init unless ensure arms a keep-alive hold', async () => { + const makeReapingHarness = () => { + const harness = makeRuntime(); + harness.setSnapshot({ + state: 'idle', + runtimeLive: true, + runtimeEpoch: 1, + }); + let keepAliveHold = false; + harness.preheat.mockImplementation( + async (options?: { keepAliveMs?: number }) => { + if ((options?.keepAliveMs ?? 0) > 0) { + keepAliveHold = true; + } + }, + ); + harness.getWorkspaceMcpStatus.mockImplementation(async () => { + // Default channelIdleTimeoutMs=0 reaps on the first workspace-control + // status read unless a keep-alive hold is armed. + if (!keepAliveHold) { + harness.setSnapshot({ + state: 'cold', + runtimeLive: false, + runtimeEpoch: 1, + }); + } + return { + v: 1, + workspaceCwd: '/workspace', + initialized: true, + runtimeEpoch: 1, + source: 'live' as const, + discoveryState: + keepAliveHold && + harness.initializeWorkspaceMcp.mock.calls.length > 0 + ? ('completed' as const) + : ('not_started' as const), + servers: [], + }; + }); + return harness; + }; + + const skipped = makeReapingHarness(); + await expect( + getWorkspaceRuntimeCoordinator(skipped.runtime).ensure({}), + ).rejects.toMatchObject({ + name: 'WorkspaceRuntimeInitializationError', + }); + expect(skipped.preheat).not.toHaveBeenCalled(); + expect(skipped.initializeWorkspaceMcp).not.toHaveBeenCalled(); + + const held = makeReapingHarness(); + await getWorkspaceRuntimeCoordinator(held.runtime).ensure({ + keepAliveMs: ENSURE_KEEP_ALIVE_MS, + }); + expect(held.preheat).toHaveBeenCalledWith({ + keepAliveMs: ENSURE_KEEP_ALIVE_MS, + }); + expect(held.initializeWorkspaceMcp).toHaveBeenCalledOnce(); + }); + + it('does not claim ACP preheat completed when skip-path runtime dies', async () => { + const harness = makeRuntime(); + let snapshotCalls = 0; + Object.assign(harness.bridge, { + getWorkspaceRuntimeLifecycleSnapshot: () => { + snapshotCalls += 1; + return { + state: 'idle' as const, + runtimeLive: snapshotCalls === 1, + runtimeEpoch: 1, + activeWork: false, + }; + }, + }); + const coordinator = getWorkspaceRuntimeCoordinator(harness.runtime); + + await expect( + coordinator.ensure({ skipKeepAlivePreheat: true }), + ).rejects.toMatchObject({ + name: 'WorkspaceRuntimeInitializationError', + cause: { + message: 'Runtime is not live after skipping keep-alive preheat', + }, + }); + expect(harness.preheat).not.toHaveBeenCalled(); + }); + it('reports the bridge lifecycle snapshot without synthesizing state', () => { const harness = makeRuntime(); harness.setSnapshot({ diff --git a/packages/cli/src/serve/workspace-runtime-coordinator.ts b/packages/cli/src/serve/workspace-runtime-coordinator.ts index 5f45b88665e..4918538db83 100644 --- a/packages/cli/src/serve/workspace-runtime-coordinator.ts +++ b/packages/cli/src/serve/workspace-runtime-coordinator.ts @@ -19,10 +19,24 @@ import { WorkspaceDrainingError } from './acp-session-bridge.js'; import type { WorkspaceRuntime } from './workspace-registry.js'; const DEFAULT_ENSURE_TIMEOUT_MS = 60_000; -const ENSURE_KEEP_ALIVE_MS = 10 * 60_000; +export const ENSURE_KEEP_ALIVE_MS = 10 * 60_000; const MCP_PREPARE_TIMEOUT_MS = 2 * 60_000; const MCP_POLL_INTERVAL_MS = 250; +export type EnsureOptions = { + timeoutMs?: number; + keepAliveMs?: number; + /** + * Skip keep-alive preheat when the runtime is already live. + * Object-form `ensure({})` defaults this to true unless `keepAliveMs` is + * set. Numeric `ensure()` / `ensure(timeoutMs)` never skip. Daemon boot + * after ACP warm-up must pass `keepAliveMs` so the default + * `channelIdleTimeoutMs=0` policy does not reap the child before MCP + * prepare finishes. + */ + skipKeepAlivePreheat?: boolean; +}; + type LifecycleAcpSessionBridge = AcpSessionBridge & { getWorkspaceRuntimeLifecycleSnapshot(): BridgeWorkspaceRuntimeLifecycleSnapshot; }; @@ -170,26 +184,50 @@ export class WorkspaceRuntimeCoordinator { }; } + /** + * `ensure(timeoutMs)` always keep-alive preheats. `ensure({ ... })` skips + * that preheat on a live runtime unless `skipKeepAlivePreheat` is false or + * `keepAliveMs` is set. Boot discovery must pass `keepAliveMs` so the + * already-warm child stays held through `initializeWorkspaceMcp`. + */ async ensure( - timeoutMs = DEFAULT_ENSURE_TIMEOUT_MS, + timeoutMsOrOptions: number | EnsureOptions = DEFAULT_ENSURE_TIMEOUT_MS, ): Promise { + const options = + typeof timeoutMsOrOptions === 'number' + ? { timeoutMs: timeoutMsOrOptions } + : timeoutMsOrOptions; + const timeoutMs = options.timeoutMs ?? DEFAULT_ENSURE_TIMEOUT_MS; this.assertAcceptingWork(); const deadline = Date.now() + timeoutMs; - try { - await withTimeout( - this.bridge.preheat({ keepAliveMs: ENSURE_KEEP_ALIVE_MS }), - timeoutMs, - ); - } catch (error) { - this.assertAcceptingWork(error); - if (error instanceof WorkspaceRuntimeStillStartingError) throw error; - throw new WorkspaceRuntimeInitializationError(error); + const snapshot = this.bridge.getWorkspaceRuntimeLifecycleSnapshot(); + const skipKeepAlivePreheat = + snapshot.runtimeLive && + options.keepAliveMs === undefined && + (options.skipKeepAlivePreheat ?? typeof timeoutMsOrOptions !== 'number'); + if (!skipKeepAlivePreheat) { + try { + await withTimeout( + this.bridge.preheat({ + keepAliveMs: options.keepAliveMs ?? ENSURE_KEEP_ALIVE_MS, + }), + timeoutMs, + ); + } catch (error) { + this.assertAcceptingWork(error); + if (error instanceof WorkspaceRuntimeStillStartingError) throw error; + throw new WorkspaceRuntimeInitializationError(error); + } } this.assertAcceptingWork(); const status = this.status(); if (!status.runtimeLive) { throw new WorkspaceRuntimeInitializationError( - new Error('ACP preheat completed without a live runtime'), + new Error( + skipKeepAlivePreheat + ? 'Runtime is not live after skipping keep-alive preheat' + : 'ACP preheat completed without a live runtime', + ), ); } const skillsReady =