Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpawnOptions> = [];
const controlRequests: Array<object> = [];
const stdins: Array<NodeStream.PassThrough> = [];
const held = Promise.withResolvers<() => void>();
const spawn = (options: SpawnOptions): SpawnedProcess => {
spawns.push(options);
const stdin = new NodeStream.PassThrough();
Expand All @@ -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", () => {
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down
17 changes: 4 additions & 13 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -947,23 +944,17 @@ 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(() =>
queryRuntime.setMcpServers(mcpServers),
).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", {
Expand Down
Loading