From 8528e5a93d3bd36b66fec19c52cd280951ed0677 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 21 Oct 2026 23:42:53 +0000 Subject: [PATCH 1/4] test(server): reproduce issue 23151, slow SessionStart hook fails Claude open Tests reproduce issue 23151: a Claude Code SessionStart hook that runs past 90 s makes ClaudeAgentSdkQueryRunner.open fail with "Claude Code did not answer the MCP server registration." Co-Authored-By: Claude Opus 5.5 --- .../ClaudeAgentSdkQueryRunner.test.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index d5c9686bb8d77..75132b5555858 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -7,10 +7,14 @@ import * as NodePath from "node:path"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, describe, it } from "@effect/vitest"; import { ProviderSessionId, ThreadId } from "@t3tools/contracts"; +import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; +import * as TestClock from "effect/testing/TestClock"; import { vi } from "vite-plus/test"; import * as HostProcess from "@t3tools/shared/HostProcess"; @@ -95,6 +99,53 @@ it.effect("forks a session stored under the instance's own CLAUDE_CONFIG_DIR", ( }).pipe(Effect.provide(forkRunnerLayer)), ).pipe(Effect.provide(NodeServices.layer)), ); +// The CLI answers mcp_set_servers only after its SessionStart hooks finish, and +// a project hook can legitimately run past 90 s, e.g. a cold worktree's setup +// script (#23151). +it.effect("opens a session whose SessionStart hook outlasts 90 seconds", () => + Effect.gen(function* () { + const registrationStarted = yield* Deferred.make(); + let answerRegistration: () => void = () => {}; + const close = vi.fn(); + const fakeQuery = vi.spyOn(ClaudeSdk, "query").mockImplementation( + () => + ({ + setMcpServers: () => { + Deferred.doneUnsafe(registrationStarted, Exit.void); + return new Promise((resolve) => { + answerRegistration = () => resolve({ added: ["t3-code"], removed: [], errors: {} }); + }); + }, + close, + }) as unknown as ClaudeSdk.Query, + ); + yield* Effect.addFinalizer(() => Effect.sync(() => fakeQuery.mockRestore())); + + const runner = yield* ClaudeAgentSdkQueryRunner; + const opening = yield* runner + .open({ + threadId: ThreadId.make("thread-slow-session-start-hook"), + providerSessionId: ProviderSessionId.make("session-slow-session-start-hook"), + options: { + sessionId: "native-session-slow-session-start-hook", + model: "claude-sonnet-4-6", + tools: [], + permissionMode: "default", + mcpServers: { "t3-code": { type: "http", url: "http://127.0.0.1:1/mcp" } }, + }, + }) + .pipe(Effect.forkScoped); + yield* Deferred.await(registrationStarted); + + yield* TestClock.adjust("105 seconds"); + answerRegistration(); + const exit = yield* Fiber.await(opening); + + assert.isTrue(Exit.isSuccess(exit), `open failed: ${String(exit)}`); + assert.equal(close.mock.calls.length, 0); + }).pipe(Effect.provide(forkRunnerLayer), Effect.provide(NodeServices.layer), Effect.scoped), +); + const realQueryRunner = layerQueryRunner.pipe( Layer.provide( Layer.succeed(ProviderEventLoggers, { From 7b6753972c700b3e4145716a88f134889a696f08 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 22 Oct 2026 00:03:34 +0000 Subject: [PATCH 2/4] fix(server): Claude sessions survive slow SessionStart hooks Claude Code reads mcp_set_servers only after its SessionStart hooks finish. A hook can run up to the CLI's default 10 minute hook timeout, for example a cold worktree setup script, so the 90 s registration deadline failed the open while the CLI was healthy. The deadline now covers that hook budget plus the per-server connect time. The SDK still rejects the request if the CLI exits, and interrupting the open still closes the query. Fixes #23151 Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Adapters/ClaudeAdapterV2.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 97ebde00ac277..cc6fca760dbdf 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -300,9 +300,11 @@ 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"; +// The SDK puts no deadline on control requests, though it rejects them if the +// CLI exits. The CLI reads mcp_set_servers only after its SessionStart hooks, +// which run up to Claude Code's default 10 minute hook timeout, then answers +// once each server connected or hit its own 30 s timeout (#23151). +const CLAUDE_MCP_REGISTRATION_TIMEOUT = "690 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"; From 4ef439b868ac267a33396f978b45607273ddd737 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 22 Oct 2026 02:05:44 +0000 Subject: [PATCH 3/4] test(server): provide the slow SessionStart hook test's layers in nested pipes tsc-rs fails the server typecheck on effect(multipleEffectProvide) when one pipe chains two Effect.provide calls. Match the neighbouring fork test's shape. Co-Authored-By: Claude Opus 5.5 --- .../ClaudeAgentSdkQueryRunner.test.ts | 80 ++++++++++--------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index 75132b5555858..c1d62cb4c0520 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -103,47 +103,49 @@ it.effect("forks a session stored under the instance's own CLAUDE_CONFIG_DIR", ( // a project hook can legitimately run past 90 s, e.g. a cold worktree's setup // script (#23151). it.effect("opens a session whose SessionStart hook outlasts 90 seconds", () => - Effect.gen(function* () { - const registrationStarted = yield* Deferred.make(); - let answerRegistration: () => void = () => {}; - const close = vi.fn(); - const fakeQuery = vi.spyOn(ClaudeSdk, "query").mockImplementation( - () => - ({ - setMcpServers: () => { - Deferred.doneUnsafe(registrationStarted, Exit.void); - return new Promise((resolve) => { - answerRegistration = () => resolve({ added: ["t3-code"], removed: [], errors: {} }); - }); + Effect.scoped( + Effect.gen(function* () { + const registrationStarted = yield* Deferred.make(); + let answerRegistration: () => void = () => {}; + const close = vi.fn(); + const fakeQuery = vi.spyOn(ClaudeSdk, "query").mockImplementation( + () => + ({ + setMcpServers: () => { + Deferred.doneUnsafe(registrationStarted, Exit.void); + return new Promise((resolve) => { + answerRegistration = () => resolve({ added: ["t3-code"], removed: [], errors: {} }); + }); + }, + close, + }) as unknown as ClaudeSdk.Query, + ); + yield* Effect.addFinalizer(() => Effect.sync(() => fakeQuery.mockRestore())); + + const runner = yield* ClaudeAgentSdkQueryRunner; + const opening = yield* runner + .open({ + threadId: ThreadId.make("thread-slow-session-start-hook"), + providerSessionId: ProviderSessionId.make("session-slow-session-start-hook"), + options: { + sessionId: "native-session-slow-session-start-hook", + model: "claude-sonnet-4-6", + tools: [], + permissionMode: "default", + mcpServers: { "t3-code": { type: "http", url: "http://127.0.0.1:1/mcp" } }, }, - close, - }) as unknown as ClaudeSdk.Query, - ); - yield* Effect.addFinalizer(() => Effect.sync(() => fakeQuery.mockRestore())); + }) + .pipe(Effect.forkScoped); + yield* Deferred.await(registrationStarted); - const runner = yield* ClaudeAgentSdkQueryRunner; - const opening = yield* runner - .open({ - threadId: ThreadId.make("thread-slow-session-start-hook"), - providerSessionId: ProviderSessionId.make("session-slow-session-start-hook"), - options: { - sessionId: "native-session-slow-session-start-hook", - model: "claude-sonnet-4-6", - tools: [], - permissionMode: "default", - mcpServers: { "t3-code": { type: "http", url: "http://127.0.0.1:1/mcp" } }, - }, - }) - .pipe(Effect.forkScoped); - yield* Deferred.await(registrationStarted); - - yield* TestClock.adjust("105 seconds"); - answerRegistration(); - const exit = yield* Fiber.await(opening); - - assert.isTrue(Exit.isSuccess(exit), `open failed: ${String(exit)}`); - assert.equal(close.mock.calls.length, 0); - }).pipe(Effect.provide(forkRunnerLayer), Effect.provide(NodeServices.layer), Effect.scoped), + yield* TestClock.adjust("105 seconds"); + answerRegistration(); + const exit = yield* Fiber.await(opening); + + assert.isTrue(Exit.isSuccess(exit), `open failed: ${String(exit)}`); + assert.equal(close.mock.calls.length, 0); + }).pipe(Effect.provide(forkRunnerLayer)), + ).pipe(Effect.provide(NodeServices.layer)), ); const realQueryRunner = layerQueryRunner.pipe( From 913dfa8e3dd4e13f1a9aef5d1c7f1edd588016a3 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 16:27:07 +0000 Subject: [PATCH 4/4] fix(server): give Claude boot room for SessionStart hooks up to their timeout SessionStart hooks run before the CLI answers initialize, so they count against the boot deadline, not MCP registration. Measured with SDK 0.3.321: a 75 s hook delayed initializationResult() by 75 s and mcp_set_servers was answered 0.1 s later. Keep registration at 90 s and raise boot from 5 to 11 minutes to cover Claude Code's 10 minute hook timeout. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts | 4 +++- .../Adapters/ClaudeAgentSdkQueryRunner.test.ts | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 64537c7e5fa8f..4e7fe41052834 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -350,7 +350,9 @@ 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"; +// Boot includes SessionStart hooks, which Claude Code runs to its 10 minute hook +// timeout before it answers initialize. +const CLAUDE_BOOT_TIMEOUT = "11 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"; diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index 3aca32bf41f90..2b891015e0e6f 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -500,7 +500,8 @@ it.effect("does not reuse a fresh session ID after MCP registration times out", it.effect.each([ ["opens after a slow CLI boot", "120 seconds", true], - ["times out a CLI that never boots", "5 minutes", false], + ["opens after SessionStart hooks run near their 10 minute timeout", "10 minutes", true], + ["times out a CLI that never boots", "11 minutes", false], ] as const)("%s", ([, bootWait, boots]) => Effect.gen(function* () { const waiting = Promise.withResolvers();