Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClaudeAgentSdkQueryRunnerError> | undefined;
readonly subagentsFromEarlierProcesses: ReadonlySet<ActiveClaudeSubagent>;
// Subagents this process still ran when a replacement took over; only its exit can end them.
handedOffSubagents: ReadonlySet<ActiveClaudeSubagent>;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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 (
Expand Down
Loading