@@ -2585,6 +2585,10 @@ function makeFakeClaudeCli(answerSetServers = true) {
25852585 const spawns: Array<SpawnOptions> = [];
25862586 const controlRequests: Array<object> = [];
25872587 const stdins: Array<NodeStream.PassThrough> = [];
2588+ const processes: Array<{
2589+ readonly stdout: NodeStream.PassThrough;
2590+ readonly exit: (code: number, endStdout: boolean) => void;
2591+ }> = [];
25882592 const spawn = (options: SpawnOptions): SpawnedProcess => {
25892593 spawns.push(options);
25902594 const stdin = new NodeStream.PassThrough();
@@ -2601,6 +2605,14 @@ function makeFakeClaudeCli(answerSetServers = true) {
26012605 return true;
26022606 },
26032607 });
2608+ processes.push({
2609+ stdout,
2610+ exit: (code, endStdout) => {
2611+ child.exitCode = code;
2612+ child.emit("exit", code, null);
2613+ if (endStdout) stdout.end();
2614+ },
2615+ });
26042616 let pending = "";
26052617 stdin.on("data", (chunk: Buffer) => {
26062618 pending += chunk.toString("utf8");
@@ -2626,10 +2638,10 @@ function makeFakeClaudeCli(answerSetServers = true) {
26262638 return child;
26272639 };
26282640 const closed = () => stdins.length > 0 && stdins.every((stdin) => stdin.writableEnded);
2629- return { spawn, spawns, controlRequests, closed };
2641+ return { spawn, spawns, controlRequests, closed, processes };
26302642}
26312643
2632- describe("ClaudeAdapterV2 MCP credential channel ", () => {
2644+ describe("ClaudeAdapterV2 SDK query runner ", () => {
26332645 const t3McpServer = {
26342646 type: "http" as const,
26352647 url: "http://127.0.0.1:43123/mcp",
@@ -2667,6 +2679,55 @@ describe("ClaudeAdapterV2 MCP credential channel", () => {
26672679 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
26682680 );
26692681
2682+ it.effect.each([0, 1])(
2683+ "fails the message stream when the child exits %s but stdout stays open",
2684+ (code) =>
2685+ Effect.gen(function* () {
2686+ const cli = makeFakeClaudeCli();
2687+ const session = yield* openWith(cli);
2688+ const completed =
2689+ yield* Deferred.make<Exit.Exit<void, ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError>>();
2690+ yield* session.messages.pipe(
2691+ Stream.runDrain,
2692+ Effect.exit,
2693+ Effect.flatMap((exit) => Deferred.succeed(completed, exit)),
2694+ Effect.forkChild,
2695+ );
2696+ cli.processes[0]!.exit(code, false);
2697+ yield* TestClock.adjust("2 seconds");
2698+
2699+ assert.isTrue(yield* Deferred.isDone(completed));
2700+ const exit = yield* Deferred.await(completed);
2701+ assert.isTrue(Exit.isFailure(exit));
2702+ if (Exit.isFailure(exit)) {
2703+ assert.equal(
2704+ makeProviderFailure({ cause: exit.cause, class: "transport_error" }).message,
2705+ "Claude Code exited before its message stream closed. Retry the turn.",
2706+ );
2707+ }
2708+ yield* session.close;
2709+ }),
2710+ );
2711+
2712+ it.effect("drains buffered messages when the child exits before stream consumption", () =>
2713+ Effect.gen(function* () {
2714+ const cli = makeFakeClaudeCli();
2715+ const session = yield* openWith(cli);
2716+ const message: SDKMessage = {
2717+ type: "system",
2718+ subtype: "status",
2719+ status: null,
2720+ uuid: "00000000-0000-4000-8000-000000000001",
2721+ session_id: "native-thread-claude-mcp-channel",
2722+ };
2723+ cli.processes[0]!.stdout.write(`${JSON.stringify(message)}\n`);
2724+ cli.processes[0]!.exit(0, true);
2725+
2726+ assert.deepEqual(yield* session.messages.pipe(Stream.runCollect), [message]);
2727+ yield* session.close;
2728+ }),
2729+ );
2730+
26702731 it.effect("commits queued delivery before interruptible protocol logging", () =>
26712732 Effect.gen(function* () {
26722733 const logging = yield* Deferred.make<void>();
0 commit comments