diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index c44993035afa2..90a0eeeb451de 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -13713,6 +13713,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; @@ -13751,7 +13755,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, @@ -13800,6 +13806,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.gen(function* () { const harness = yield* openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame( diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 8080c2a5fbc6c..a77547145a267 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -5007,6 +5007,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; @@ -12674,6 +12676,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( @@ -12753,6 +12756,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 && @@ -13006,6 +13010,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( return yield* new ProviderAdapter.ProviderAdapterProtocolError({ driver: CLAUDE_PROVIDER, detail: CLAUDE_QUERY_ENDED_BEFORE_TURN_DETAIL, + ...(querySession.exitCause === undefined + ? {} + : { cause: querySession.exitCause }), }); } if (