@@ -123,6 +123,7 @@ import * as Context from "effect/Context";
123123import * as SecureRandom from "effect/crypto/SecureRandom" ;
124124import * as DateTime from "effect/DateTime" ;
125125import * as Deferred from "effect/Deferred" ;
126+ import * as Duration from "effect/Duration" ;
126127import * as Effect from "effect/Effect" ;
127128import * as Equal from "effect/Equal" ;
128129import * as Exit from "effect/Exit" ;
@@ -338,6 +339,13 @@ const WORKFLOW_MEMBER_TRANSCRIPT_FLUSH_MS = 500;
338339const BACKGROUND_MOVE_TIMEOUT_MS = 5_000 ;
339340// How long a Stop waits for the CLI to exit before the user is offered Force stop.
340341const CLAUDE_STOP_CONFIRM_TIMEOUT_MS = 10_000 ;
342+ // Matches the stderr tail the SDK keeps for a CLI it spawns itself.
343+ 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 ) ;
346+ // Claude Code's refusal of bypassPermissions as root, which it explains only on stderr.
347+ const CLAUDE_ROOT_REFUSAL =
348+ "--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons" ;
341349
342350const CLAUDE_GROUP_WATCH_SCRIPT = [
343351 "{ trap '' TERM; read -r _ <&3; kill -TERM 0; sleep 5; kill -KILL 0; } </dev/null >/dev/null 2>&1 &" ,
@@ -352,6 +360,8 @@ const CLAUDE_GROUP_WATCH_SCRIPT = [
352360export function spawnClaudeProcessGroup (
353361 options : ClaudeSpawnOptions ,
354362 onExit ?: ( ) => void ,
363+ onStderr : ( text : string ) => void = ( ) => { } ,
364+ onStderrClose : ( ) => void = ( ) => { } ,
355365) : ClaudeSpawnedProcess {
356366 const child = NodeChildProcess . spawn (
357367 "/bin/sh" ,
@@ -360,10 +370,12 @@ export function spawnClaudeProcessGroup(
360370 cwd : options . cwd ,
361371 env : options . env ,
362372 signal : options . signal ,
363- stdio : [ "pipe" , "pipe" , "ignore " , "pipe" ] ,
373+ stdio : [ "pipe" , "pipe" , "pipe " , "pipe" ] ,
364374 detached : true ,
365375 } ,
366376 ) ;
377+ // Always drained, so a chatty CLI never blocks on a full stderr pipe.
378+ child . stderr ?. setEncoding ( "utf8" ) . on ( "data" , onStderr ) . once ( "close" , onStderrClose ) ;
367379 child . once ( "exit" , ( ) => {
368380 try {
369381 signalProcessGroup ( child . pid ?? 0 , "SIGKILL" ) ;
@@ -558,6 +570,17 @@ export class ClaudeAgentSdkQueryRunnerError extends Schema.TaggedError<ClaudeAge
558570 }
559571}
560572
573+ export class ClaudeCodeRootRefusalError extends Schema . TaggedError < ClaudeCodeRootRefusalError > ( ) (
574+ "ClaudeCodeRootRefusalError" ,
575+ {
576+ cause : Schema . Defect ( ) ,
577+ } ,
578+ ) {
579+ override get message ( ) : string {
580+ return `Claude Code exited before it started: ${ CLAUDE_ROOT_REFUSAL } ` ;
581+ }
582+ }
583+
561584class ClaudeMcpRegistrationTimeoutError extends Schema . TaggedError < ClaudeMcpRegistrationTimeoutError > ( ) (
562585 "ClaudeMcpRegistrationTimeoutError" ,
563586 { } ,
@@ -1009,10 +1032,25 @@ export const layerQueryRunner: Layer.Layer<
10091032 const unit = launch ?. unit ;
10101033 const stopScope =
10111034 unit === undefined ? undefined : ( ) => void runFork ( agentScope . stop ( unit ) ) ;
1035+ // Only a CLI that fails before its first message explains itself on
1036+ // stderr. The known root refusal becomes a curated failure; any other
1037+ // stderr can hold private text, so it stays out of turn failures.
1038+ let stderrTail = "" ;
1039+ let stderrClosed : Promise < void > = Promise . resolve ( ) ;
1040+ let answered = false ;
1041+ const appendStderr = ( text : string ) => {
1042+ if ( answered ) return ;
1043+ stderrTail = ( stderrTail + text ) . slice ( - CLAUDE_STDERR_TAIL_LENGTH ) ;
1044+ } ;
10121045 const spawnProcess =
10131046 options . spawnClaudeCodeProcess ??
10141047 ( ( spawnOptions : ClaudeSpawnOptions ) => {
1015- if ( platform !== "win32" ) return spawnClaudeProcessGroup ( spawnOptions , stopScope ) ;
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+ }
10161054 const child = NodeChildProcess . spawn ( spawnOptions . command , spawnOptions . args , {
10171055 cwd : spawnOptions . cwd ,
10181056 env : spawnOptions . env ,
@@ -1132,9 +1170,31 @@ export const layerQueryRunner: Layer.Layer<
11321170 ) ;
11331171
11341172 return {
1135- messages : Stream . fromAsyncIterable ( claudeQueryMessages ( queryRuntime ) , ( cause ) =>
1136- queryRunnerError ( cause , "fromAsyncIterable" ) ,
1173+ messages : Stream . fromAsyncIterable (
1174+ claudeQueryMessages ( queryRuntime ) ,
1175+ ( cause ) => cause ,
11371176 ) . 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+ ) ,
1193+ Stream . tap ( ( ) =>
1194+ Effect . sync ( ( ) => {
1195+ answered = true ;
1196+ } ) ,
1197+ ) ,
11381198 Stream . tap ( ( message ) =>
11391199 logProtocolEvent ( {
11401200 direction : "incoming" ,
0 commit comments