diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index b231fbfca9d98..5c16c78729537 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -1741,10 +1741,13 @@ describe("ClaudeAdapterV2 native fork", () => { ); }); +// With `answerSetServers` off, the CLI holds its mcp_set_servers answer until +// `answerHeldSetServers`, as Claude Code does while SessionStart hooks run. function makeFakeClaudeCli(answerSetServers = true) { const spawns: Array = []; const controlRequests: Array = []; const stdins: Array = []; + const held = Promise.withResolvers<() => void>(); const spawn = (options: SpawnOptions): SpawnedProcess => { spawns.push(options); const stdin = new NodeStream.PassThrough(); @@ -1770,13 +1773,15 @@ function makeFakeClaudeCli(answerSetServers = true) { if (frame.type !== "control_request") continue; controlRequests.push(frame.request); const setServers = frame.request.subtype === "mcp_set_servers"; - if (setServers && !answerSetServers) continue; const response = { subtype: "success", request_id: frame.request_id, response: setServers ? { added: [], removed: [], errors: {} } : {}, }; - stdout.write(`${JSON.stringify({ type: "control_response", response })}\n`); + const answer = () => + stdout.write(`${JSON.stringify({ type: "control_response", response })}\n`); + if (setServers && !answerSetServers) held.resolve(answer); + else answer(); } }); stdin.on("end", () => { @@ -1787,7 +1792,7 @@ function makeFakeClaudeCli(answerSetServers = true) { }; // The SDK closes the CLI by ending its stdin. const closed = () => stdins.length > 0 && stdins.every((stdin) => stdin.writableEnded); - return { spawn, spawns, controlRequests, closed }; + return { spawn, spawns, controlRequests, closed, answerHeldSetServers: held.promise }; } describe("ClaudeAdapterV2 MCP credential channel", () => { @@ -1839,6 +1844,19 @@ describe("ClaudeAdapterV2 MCP credential channel", () => { assert.notInclude(spawned, "dummy-mcp-credential"); }), ); + + it.effect("waits for a CLI whose startup hooks outlast 90 seconds", () => + Effect.gen(function* () { + const cli = makeFakeClaudeCli(false); + const opening = yield* openWith(cli).pipe(Effect.forkChild); + const answer = yield* Effect.promise(() => cli.answerHeldSetServers); + yield* TestClock.adjust("10 minutes"); + answer(); + const session = yield* Fiber.join(opening); + assert.isFalse(cli.closed()); + yield* session.close; + }), + ); }); describe("ClaudeAdapterV2 native session identity", () => { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 97ebde00ac277..8b88cd6c81d5e 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -300,9 +300,6 @@ export const CLAUDE_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(CLAUDE_PROV const DEFAULT_CLAUDE_SETTINGS = Schema.decodeSync(ClaudeSettings)({}); // Entry extensions the Claude SDK runs through node instead of executing. const CLAUDE_SCRIPT_EXTENSIONS = [".js", ".mjs", ".tsx", ".ts", ".jsx"]; -// The SDK puts no deadline on control requests. The CLI answers -// mcp_set_servers once each server connected or hit its own 30 s timeout. -const CLAUDE_MCP_REGISTRATION_TIMEOUT = "90 seconds"; // Claude can consume a subagent result before the launching turn ends, leaving // its notification with no native wake turn to answer the continuation. const CLAUDE_WAKE_TURN_START_DEADLINE = "2 minutes"; @@ -947,6 +944,10 @@ export const layerQueryRunner: Layer.Layer< restore( Effect.gen(function* () { if (mcpServers !== undefined) { + // No deadline: the CLI answers only after startup, including SessionStart + // hooks that may run for minutes in a fresh worktree. Hook and server + // timeouts bound that wait, a CLI exit rejects the request, and Stop + // interrupts it. // Error texts and the CLI's mcp_status response can echo the headers, so // only server names are logged. const failed = yield* Effect.tryPromise(() => @@ -954,16 +955,6 @@ export const layerQueryRunner: Layer.Layer< ).pipe( Effect.map((result) => Object.keys(result.errors)), Effect.orElseSucceed(() => Object.keys(mcpServers)), - Effect.timeoutOrElse({ - duration: CLAUDE_MCP_REGISTRATION_TIMEOUT, - orElse: () => - Effect.fail( - queryRunnerError( - "Claude Code did not answer the MCP server registration.", - "setMcpServers", - ), - ), - }), ); if (failed.length > 0) { yield* Effect.logWarning("orchestration-v2.claude-mcp-servers-unavailable", {