From 5abc96318c78239fd2ea9d4a208df8e9fd9eec97 Mon Sep 17 00:00:00 2001 From: chief-archivist Date: Tue, 3 Nov 2026 14:12:25 +0100 Subject: [PATCH 1/3] fix(server): Claude turns on busy hosts no longer time out registering MCP tools The 90-second MCP registration deadline started when the Claude CLI was spawned, so it also covered CLI boot. On a host with its CPUs heavily oversubscribed, boot alone takes about a minute, and turns failed with "Claude timed out while registering its MCP tools". The deadline now starts once the CLI has booted, so it covers only the mcp_set_servers request it was added for. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.ts | 5 +++ .../ClaudeAgentSdkQueryRunner.test.ts | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index deacdfd617373..1b8c3a0ad8982 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -1286,6 +1286,11 @@ export const layerQueryRunner: Layer.Layer< restore( Effect.gen(function* () { if (mcpServers !== undefined) { + // CLI boot takes about a minute on a host with CPUs 12x oversubscribed, so the + // deadline covers only registration. A CLI that exits rejects this wait. + yield* Effect.promise(() => + queryRuntime.initializationResult().catch(() => undefined), + ); // Errors and mcp_status can echo headers; log only server names. const failed = yield* Effect.tryPromise({ try: () => queryRuntime.setMcpServers(mcpServers), diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index 1ea0a62e168ce..692b25bde9b7a 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -254,6 +254,7 @@ it.effect("reopens a stalled MCP registration before delivering the resumed prom if (typeof prompt === "string") throw new Error("Expected streaming input"); prompts.push(prompt); return { + initializationResult: async () => ({}), setMcpServers: prompts.length === 1 ? firstRegistration : secondRegistration, close: prompts.length === 1 ? firstClose : secondClose, [Symbol.asyncDispose]: async () => { @@ -360,6 +361,7 @@ it.effect.each([true, false])("only retries after confirmed process exit: %s", ( cleanupProgress.resolve("retry"); } return { + initializationResult: async () => ({}), setMcpServers: () => { registrationStarted.resolve(); return first @@ -410,6 +412,7 @@ it.effect("stops retrying MCP registration after two timeouts and reports the ca const querySpy = vi.spyOn(ClaudeSdk, "query").mockImplementation(() => { const started = querySpy.mock.calls.length === 1 ? firstRegistration : secondRegistration; return { + initializationResult: async () => ({}), setMcpServers: () => { started.resolve(); return new Promise(() => {}); @@ -450,6 +453,7 @@ it.effect("cancels a stalled registration without retrying or retaining its prom const querySpy = vi.spyOn(ClaudeSdk, "query").mockImplementation( () => ({ + initializationResult: async () => ({}), setMcpServers: () => { registrationStarted.resolve(); return new Promise(() => {}); @@ -480,6 +484,7 @@ it.effect("does not reuse a fresh session ID after MCP registration times out", const querySpy = vi.spyOn(ClaudeSdk, "query").mockImplementation( () => ({ + initializationResult: async () => ({}), setMcpServers: () => { registrationStarted.resolve(); return new Promise(() => {}); @@ -507,6 +512,45 @@ it.effect("does not reuse a fresh session ID after MCP registration times out", ), ); +it.effect("starts the MCP registration deadline after a slow CLI boot", () => + Effect.scoped( + Effect.gen(function* () { + const waiting = Promise.withResolvers(); + const booted = Promise.withResolvers(); + const querySpy = vi.spyOn(ClaudeSdk, "query").mockImplementation( + () => + ({ + initializationResult: () => { + waiting.resolve(); + return booted.promise; + }, + // The CLI answers control requests only after it has booted. + setMcpServers: async () => { + waiting.resolve(); + await booted.promise; + return { added: ["t3-code"], removed: [], errors: {} }; + }, + close: vi.fn(), + [Symbol.asyncDispose]: async () => {}, + }) as unknown as ClaudeSdk.Query, + ); + yield* Effect.addFinalizer(() => Effect.sync(() => querySpy.mockRestore())); + const runner = yield* ClaudeAgentSdkQueryRunner; + const { resume, ...options } = resumedMcpQueryInput.options; + const opening = yield* runner + .open({ ...resumedMcpQueryInput, options: { ...options, sessionId: resume } }) + .pipe(Effect.result, Effect.forkChild); + yield* Effect.promise(() => waiting.promise); + yield* TestClock.adjust("120 seconds"); + booted.resolve(); + const result = yield* Fiber.join(opening); + + assert.equal(result._tag, "Success"); + if (result._tag === "Success") yield* result.success.close; + }).pipe(Effect.provide(realQueryRunner)), + ), +); + function fakeClaudeCli(script: string): string { const dir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-claude-cli-")); const cli = NodePath.join(dir, "claude"); From 2611c2c70f6d6dcb989c146df998aebb26cf82eb Mon Sep 17 00:00:00 2001 From: chief-archivist Date: Tue, 3 Nov 2026 14:53:33 +0100 Subject: [PATCH 2/3] fix(server): a Claude CLI that never boots still times out The SDK puts no deadline on query() initialization, so waiting for boot before the registration deadline left a hung CLI waiting until Stop. Boot now gets its own five-minute deadline and fails through the same timeout error and cleanup as registration. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.ts | 37 +++++++++++-------- .../ClaudeAgentSdkQueryRunner.test.ts | 11 ++++-- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 1b8c3a0ad8982..0021f99d03070 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -338,6 +338,7 @@ export const CLAUDE_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(CLAUDE_PROV const CLAUDE_SCRIPT_EXTENSIONS = [".js", ".mjs", ".tsx", ".ts", ".jsx"]; // The SDK has no control-request deadline; the CLI waits up to 30 s per MCP server. const CLAUDE_MCP_REGISTRATION_TIMEOUT = "90 seconds"; +const CLAUDE_BOOT_TIMEOUT = "5 minutes"; // 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"; @@ -1286,10 +1287,27 @@ export const layerQueryRunner: Layer.Layer< restore( Effect.gen(function* () { if (mcpServers !== undefined) { - // CLI boot takes about a minute on a host with CPUs 12x oversubscribed, so the - // deadline covers only registration. A CLI that exits rejects this wait. + const timedOut = (method: string) => () => + Effect.logWarning("orchestration-v2.claude-mcp-registration-timeout", { + threadId: input.threadId, + providerSessionId: input.providerSessionId, + method, + }).pipe( + Effect.andThen( + Effect.fail( + queryRunnerError(new ClaudeMcpRegistrationTimeoutError(), method), + ), + ), + ); + // The SDK leaves query() boot unbounded, and it takes about a minute on a host + // with CPUs 12x oversubscribed, so it gets its own, longer deadline. yield* Effect.promise(() => queryRuntime.initializationResult().catch(() => undefined), + ).pipe( + Effect.timeoutOrElse({ + duration: CLAUDE_BOOT_TIMEOUT, + orElse: timedOut("initialize"), + }), ); // Errors and mcp_status can echo headers; log only server names. const failed = yield* Effect.tryPromise({ @@ -1300,20 +1318,7 @@ export const layerQueryRunner: Layer.Layer< Effect.orElseSucceed(() => Object.keys(mcpServers)), Effect.timeoutOrElse({ duration: CLAUDE_MCP_REGISTRATION_TIMEOUT, - orElse: () => - Effect.logWarning("orchestration-v2.claude-mcp-registration-timeout", { - threadId: input.threadId, - providerSessionId: input.providerSessionId, - }).pipe( - Effect.andThen( - Effect.fail( - queryRunnerError( - new ClaudeMcpRegistrationTimeoutError(), - "setMcpServers", - ), - ), - ), - ), + orElse: timedOut("setMcpServers"), }), ); if (failed.length > 0) { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index 692b25bde9b7a..3a73636ea47ea 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -512,7 +512,10 @@ it.effect("does not reuse a fresh session ID after MCP registration times out", ), ); -it.effect("starts the MCP registration deadline after a slow CLI boot", () => +it.effect.each([ + ["opens after a slow CLI boot", "120 seconds", true], + ["times out a CLI that never boots", "5 minutes", false], +] as const)("%s", ([, bootWait, boots]) => Effect.scoped( Effect.gen(function* () { const waiting = Promise.withResolvers(); @@ -541,11 +544,11 @@ it.effect("starts the MCP registration deadline after a slow CLI boot", () => .open({ ...resumedMcpQueryInput, options: { ...options, sessionId: resume } }) .pipe(Effect.result, Effect.forkChild); yield* Effect.promise(() => waiting.promise); - yield* TestClock.adjust("120 seconds"); - booted.resolve(); + yield* TestClock.adjust(bootWait); + if (boots) booted.resolve(); const result = yield* Fiber.join(opening); - assert.equal(result._tag, "Success"); + assert.equal(result._tag, boots ? "Success" : "Failure"); if (result._tag === "Success") yield* result.success.close; }).pipe(Effect.provide(realQueryRunner)), ), From 8afe10b45be7340e1551d7e5150a6948310f9ff0 Mon Sep 17 00:00:00 2001 From: chief-archivist Date: Tue, 3 Nov 2026 15:05:26 +0100 Subject: [PATCH 3/3] fix(server): a Claude boot timeout says Claude timed out starting up Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/Adapters/ClaudeAdapterV2.ts | 15 ++++++++++----- .../Adapters/ClaudeAgentSdkQueryRunner.test.ts | 7 +++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 0021f99d03070..82a9e98291a54 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -615,10 +615,12 @@ export class ClaudeCodeRootRefusalError extends Schema.TaggedError()( "ClaudeMcpRegistrationTimeoutError", - {}, + { phase: Schema.Literals(["initialize", "setMcpServers"]) }, ) { override get message(): string { - return "Claude timed out while registering its MCP tools. Retry the turn; if it keeps failing, check Claude and the server logs."; + return this.phase === "initialize" + ? "Claude timed out while starting up. Retry the turn; if it keeps failing, check Claude and the server logs." + : "Claude timed out while registering its MCP tools. Retry the turn; if it keeps failing, check Claude and the server logs."; } } @@ -1287,15 +1289,18 @@ export const layerQueryRunner: Layer.Layer< restore( Effect.gen(function* () { if (mcpServers !== undefined) { - const timedOut = (method: string) => () => + const timedOut = (phase: ClaudeMcpRegistrationTimeoutError["phase"]) => () => Effect.logWarning("orchestration-v2.claude-mcp-registration-timeout", { threadId: input.threadId, providerSessionId: input.providerSessionId, - method, + phase, }).pipe( Effect.andThen( Effect.fail( - queryRunnerError(new ClaudeMcpRegistrationTimeoutError(), method), + queryRunnerError( + new ClaudeMcpRegistrationTimeoutError({ phase }), + phase, + ), ), ), ); diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index 3a73636ea47ea..25ff05f0e2aa4 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -550,6 +550,13 @@ it.effect.each([ assert.equal(result._tag, boots ? "Success" : "Failure"); if (result._tag === "Success") yield* result.success.close; + else + assert.equal( + makeProviderFailure({ + cause: { _tag: "ProviderAdapterTurnStartError", cause: result.failure }, + }).message, + "Claude timed out while starting up. Retry the turn; if it keeps failing, check Claude and the server logs.", + ); }).pipe(Effect.provide(realQueryRunner)), ), );