From 3804cbda875d19d5ff2fc5b90243d9f4bd406735 Mon Sep 17 00:00:00 2001 From: SohaibAli Date: Thu, 12 Nov 2026 03:58:05 +0500 Subject: [PATCH 1/2] test(server): cover a Claude root refusal before the turn attaches The root-refusal message is lost when Claude Code exits before the turn attaches to its query. The turn fails with the generic "could not start this turn" message instead. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index ee1b475bd30d5..362521eb875ed 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -12099,6 +12099,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { const openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame = Effect.fnUntraced(function* ( name: string, + firstQueryExit: Stream.Stream< + SDKMessage, + ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError + > = Stream.empty, ) { const fileSystem = yield* FileSystem.FileSystem; const idAllocator = yield* IdAllocator.IdAllocatorV2; @@ -12137,7 +12141,9 @@ describe("ClaudeAdapterV2 background wake turns", () => { // The first CLI exits without writing a frame. messages: opened === 1 - ? Stream.empty.pipe(Stream.ensuring(Deferred.succeed(deadStreamEnded, undefined))) + ? firstQueryExit.pipe( + Stream.ensuring(Deferred.succeed(deadStreamEnded, undefined)), + ) : Stream.fromQueue(sdkMessages), offer: () => Effect.void, setModel: () => Effect.void, @@ -12184,6 +12190,43 @@ describe("ClaudeAdapterV2 background wake turns", () => { return { runtime, turnInput }; }); + it.effect("shows Claude's root refusal when its query exits before the turn starts", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame( + "claude-query-refuses-root-before-frame", + Stream.fail( + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "fromAsyncIterable", + cause: new ClaudeAdapterV2.ClaudeCodeRootRefusalError({ + cause: new Error("Claude Code process exited with code 1"), + }), + }), + ), + ); + const started = yield* harness.runtime + .startTurn(harness.turnInput("a", 1)) + .pipe(Effect.exit); + assert.isTrue(Exit.isFailure(started)); + if (Exit.isFailure(started)) { + assert.strictEqual( + makeProviderFailure({ cause: started.cause, class: "transport_error" }).message, + "Claude Code exited before it started: --dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons", + ); + } + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + it.effect("fails a turn whose Claude query ends before its first frame", () => Effect.scoped( Effect.gen(function* () { From adb39158e6e77668d0d8f6081fc4fe777fd9e3ce Mon Sep 17 00:00:00 2001 From: SohaibAli Date: Thu, 12 Nov 2026 03:59:15 +0500 Subject: [PATCH 2/2] fix(server): show Claude's root refusal when it exits before the turn starts Claude Code refuses Full access as root and exits at once. When the process exits before the turn attaches to its query, the stream's exit handler finds no turn to finalize and drops the failure. attachTurn then fails with no cause, so the root-refusal message from #23804 never reaches the user. The live query now keeps its stream failure, and attachTurn passes it as the cause of the "ended before the turn could start" error. Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Adapters/ClaudeAdapterV2.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index b8e6e3d90b760..089481c8869a0 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4529,6 +4529,8 @@ interface ClaudeLiveQueryContext { claudeCodeVersion: string | undefined; permissionMode: PermissionMode; stopping: boolean; + // A turn that had not attached when the CLI exited fails its start with this cause. + exitCause: Cause.Cause | undefined; readonly subagentsFromEarlierProcesses: ReadonlySet; // Subagents this process still ran when a replacement took over; only its exit can end them. handedOffSubagents: ReadonlySet; @@ -11380,6 +11382,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( claudeCodeVersion: undefined, permissionMode: queryOptions.permissionMode, stopping: false, + exitCause: undefined, handedOffSubagents: new Set(), subagentsFromEarlierProcesses: new Set( [...(yield* Ref.get(sessionSubagentsByTaskId)).values()].filter( @@ -11451,6 +11454,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( Effect.exit, Effect.flatMap( Effect.fnUntraced(function* (exit: ClaudeQueryStreamExit) { + if (exit._tag === "Failure") context.exitCause = exit.cause; const heldContext = yield* Ref.get(activeTurn); if ( (yield* Ref.get(queryContext))?.query === querySession && @@ -11630,6 +11634,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( return yield* new ProviderAdapter.ProviderAdapterProtocolError({ driver: CLAUDE_PROVIDER, detail: "Claude provider query ended before the turn could start.", + ...(querySession.exitCause === undefined + ? {} + : { cause: querySession.exitCause }), }); } if (