diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index c09aca71dfc2f..44eaa3889b1a9 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -8784,6 +8784,13 @@ describe("ClaudeAdapterV2 background wake turns", () => { expected: "Claude stopped: the prompt exceeds the model's context window. Run /compact or start a new thread.", }, + { + name: "a flagged result whose terminal reason is not a failure", + isError: true, + result: "API Error: 400 Request rejected by a safeguard.", + terminalReason: "completed" as const, + expected: "API Error: 400 Request rejected by a safeguard.", + }, ...[ { name: "a model version error rewritten by a custom endpoint", @@ -12514,15 +12521,34 @@ describe("ClaudeAdapterV2 background wake turns", () => { }, }, parent_tool_use_id: null, + error: "server_error", uuid: "retry-notice", session_id: WAKE_NATIVE_SESSION, }), ); - yield* harness.until(() => - harness.events.some( - (event) => - event.type === "turn_item.updated" && event.turnItem.type === "assistant_message", - ), + // The frame emits nothing, so a later retry frame marks that it was processed. + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "system", + subtype: "api_retry", + attempt: 1, + max_retries: 10, + retry_delay_ms: 600, + error_status: 503, + error: "server_error", + uuid: "00000000-0000-4000-8000-000000000212", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* harness.until( + () => + harness.events.filter( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "error" && + event.turnItem.status === "running", + ).length >= 2, ); } yield* TestClock.adjust("9 minutes"); @@ -12719,6 +12745,76 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("fails an API-error turn without also showing the error as a reply", () => + Effect.gen(function* () { + const refusal = + "API Error: Opus 5.5's safeguards flagged this session (https://www.anthropic.com/legal/aup)."; + const harness = yield* makeWakeHarness; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-synthetic-api-error"), + text: "Hello.", + attachments: [], + }), + ); + yield* Queue.offerAll(harness.sdkMessages, [ + claudeSdkFrame({ + type: "assistant", + message: { + model: "", + id: "msg_synthetic_api_error", + type: "message", + role: "assistant", + content: [{ type: "text", text: refusal }], + stop_reason: "stop_sequence", + stop_sequence: "", + usage: { + input_tokens: 0, + output_tokens: 0, + cache_creation_input_tokens: 0, + cache_read_input_tokens: 0, + }, + }, + parent_tool_use_id: null, + error: "invalid_request", + isApiErrorMessage: true, + uuid: "00000000-0000-4000-8000-000000000671", + session_id: WAKE_NATIVE_SESSION, + }), + makeResultFrame({ + uuid: "00000000-0000-4000-8000-000000000672", + result: refusal, + isError: true, + terminalReason: "api_error", + }), + ]); + const terminal = yield* Queue.take(harness.terminalReceipts); + assert.equal(terminal.status, "failed"); + if (terminal.status !== "failed") return; + assert.include(terminal.failure.message, "safeguards flagged this session"); + assert.isFalse( + harness.events.some( + (event) => + (event.type === "message.updated" && event.message.role === "assistant") || + (event.type === "turn_item.updated" && event.turnItem.type === "assistant_message"), + ), + "the API error is the turn's failure, not an assistant reply", + ); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + const providerThreadRosterEvents = ( events: ReadonlyArray, ) => diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 3b3e2b273977f..319c37fdad0e2 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -2738,6 +2738,16 @@ function isClaudeGoalAcknowledgement(message: SDKMessage): boolean { return text.startsWith(CLAUDE_GOAL_SET_PREFIX) || text.startsWith("Goal cleared: "); } +/** The CLI repeats an API error as a synthetic reply; the turn's failure already carries it. */ +function isClaudeApiErrorNotice(message: SDKMessage): boolean { + return ( + message.type === "assistant" && + message.parent_tool_use_id === null && + message.message.model === "" && + message.error !== undefined + ); +} + const providerGoalsEqual = Schema.toEquivalence(Schema.NullOr(OrchestrationV2ProviderGoal)); function normalizeClaudeResultText(text: string): string { @@ -4715,7 +4725,7 @@ function terminalStatusFromResult( return isOverloadedResult(message) || message.api_error_status === 429 || terminalResultError(message.terminal_reason, failureHint) !== undefined || - (message.is_error && failureHint !== undefined) + message.is_error ? "failed" : "completed"; } @@ -12040,6 +12050,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }); return; } + if (isClaudeApiErrorNotice(message)) return; if (isClaudeGoalAcknowledgement(message)) { // The CLI repeats a local command's output as the turn's result text. if (assistantText !== null) { diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/claude_result_is_error/output.ts b/apps/server/src/orchestration-v2/testkit/fixtures/claude_result_is_error/output.ts index a8cf34999da34..af96166eafd7a 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/claude_result_is_error/output.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/claude_result_is_error/output.ts @@ -11,7 +11,6 @@ import { projectionFor, } from "../shared.ts"; -const AUTH_ERROR_TEXT = "Failed to authenticate. API Error: 401 Invalid authentication credentials"; const AUTH_FAILURE_TEXT = "Claude could not authenticate. For subscription login, run `claude auth login` on this environment's machine, then start a new thread. For API-key authentication, check this instance's configured credentials."; @@ -55,7 +54,8 @@ export function assertClaudeResultIsErrorOutput( const run1AssistantTexts = projection.turnItems.flatMap((item) => item.runId === failedRun?.id && item.type === "assistant_message" ? [item.text] : [], ); - assert.deepEqual(run1AssistantTexts, [AUTH_ERROR_TEXT]); + // The turn's error item carries the API error; the CLI's synthetic repeat is not a reply. + assert.deepEqual(run1AssistantTexts, []); const recoveredRun = projection.runs.find((run) => run.ordinal === 2); assert.isDefined(recoveredRun);