Skip to content

Commit a480e86

Browse files
fix(server): wait for Claude's stderr to close before classifying a startup failure
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 144d2fe commit a480e86

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import * as Context from "effect/Context";
123123
import * as SecureRandom from "effect/crypto/SecureRandom";
124124
import * as DateTime from "effect/DateTime";
125125
import * as Deferred from "effect/Deferred";
126+
import * as Duration from "effect/Duration";
126127
import * as Effect from "effect/Effect";
127128
import * as Equal from "effect/Equal";
128129
import * as Exit from "effect/Exit";
@@ -340,6 +341,8 @@ const BACKGROUND_MOVE_TIMEOUT_MS = 5_000;
340341
const CLAUDE_STOP_CONFIRM_TIMEOUT_MS = 10_000;
341342
// Matches the stderr tail the SDK keeps for a CLI it spawns itself.
342343
const CLAUDE_STDERR_TAIL_LENGTH = 2_048;
344+
// The process group is killed on exit, so its stderr closes promptly.
345+
const CLAUDE_STDERR_DRAIN_TIMEOUT = Duration.seconds(1);
343346
// Claude Code's refusal of bypassPermissions as root, which it explains only on stderr.
344347
const CLAUDE_ROOT_REFUSAL =
345348
"--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons";
@@ -358,6 +361,7 @@ export function spawnClaudeProcessGroup(
358361
options: ClaudeSpawnOptions,
359362
onExit?: () => void,
360363
onStderr: (text: string) => void = () => {},
364+
onStderrClose: () => void = () => {},
361365
): ClaudeSpawnedProcess {
362366
const child = NodeChildProcess.spawn(
363367
"/bin/sh",
@@ -371,7 +375,7 @@ export function spawnClaudeProcessGroup(
371375
},
372376
);
373377
// Always drained, so a chatty CLI never blocks on a full stderr pipe.
374-
child.stderr?.setEncoding("utf8").on("data", onStderr);
378+
child.stderr?.setEncoding("utf8").on("data", onStderr).once("close", onStderrClose);
375379
child.once("exit", () => {
376380
try {
377381
signalProcessGroup(child.pid ?? 0, "SIGKILL");
@@ -1032,6 +1036,7 @@ export const layerQueryRunner: Layer.Layer<
10321036
// stderr. The known root refusal becomes a curated failure; any other
10331037
// stderr can hold private text, so it stays out of turn failures.
10341038
let stderrTail = "";
1039+
let stderrClosed: Promise<void> = Promise.resolve();
10351040
let answered = false;
10361041
const appendStderr = (text: string) => {
10371042
if (answered) return;
@@ -1040,8 +1045,12 @@ export const layerQueryRunner: Layer.Layer<
10401045
const spawnProcess =
10411046
options.spawnClaudeCodeProcess ??
10421047
((spawnOptions: ClaudeSpawnOptions) => {
1043-
if (platform !== "win32")
1044-
return spawnClaudeProcessGroup(spawnOptions, stopScope, appendStderr);
1048+
if (platform !== "win32") {
1049+
// Node can report "exit" before it has read the stderr the CLI wrote.
1050+
let stderrDone = () => {};
1051+
stderrClosed = new Promise((resolve) => (stderrDone = resolve));
1052+
return spawnClaudeProcessGroup(spawnOptions, stopScope, appendStderr, stderrDone);
1053+
}
10451054
const child = NodeChildProcess.spawn(spawnOptions.command, spawnOptions.args, {
10461055
cwd: spawnOptions.cwd,
10471056
env: spawnOptions.env,
@@ -1161,13 +1170,26 @@ export const layerQueryRunner: Layer.Layer<
11611170
);
11621171

11631172
return {
1164-
messages: Stream.fromAsyncIterable(claudeQueryMessages(queryRuntime), (cause) => {
1165-
const refusedRoot = !answered && stderrTail.includes(CLAUDE_ROOT_REFUSAL);
1166-
return queryRunnerError(
1167-
refusedRoot ? new ClaudeCodeRootRefusalError({ cause }) : cause,
1168-
"fromAsyncIterable",
1169-
);
1170-
}).pipe(
1173+
messages: Stream.fromAsyncIterable(
1174+
claudeQueryMessages(queryRuntime),
1175+
(cause) => cause,
1176+
).pipe(
1177+
Stream.catch((cause) =>
1178+
Stream.fromEffect(
1179+
Effect.gen(function* () {
1180+
if (!answered) {
1181+
yield* Effect.promise(() => stderrClosed).pipe(
1182+
Effect.timeoutOption(CLAUDE_STDERR_DRAIN_TIMEOUT),
1183+
);
1184+
}
1185+
const refusedRoot = !answered && stderrTail.includes(CLAUDE_ROOT_REFUSAL);
1186+
return yield* queryRunnerError(
1187+
refusedRoot ? new ClaudeCodeRootRefusalError({ cause }) : cause,
1188+
"fromAsyncIterable",
1189+
);
1190+
}),
1191+
),
1192+
),
11711193
Stream.tap(() =>
11721194
Effect.sync(() => {
11731195
answered = true;

0 commit comments

Comments
 (0)