From 8fdee8835b45e736a9c4c25e2f33a82e1ce3d063 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 00:52:53 +0000 Subject: [PATCH 1/3] test(claude): an API-error turn is not also shown as a reply Reproduces #24171: the CLI's API-error frame becomes an assistant message while the result fails the turn with the same text. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index c09aca71dfc2f..15401e3eb844f 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -12719,6 +12719,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, ) => From c8fa1c46a73bcf712b078f85b54fc2a6d5fab98a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 01:20:40 +0000 Subject: [PATCH 2/3] fix(claude): an API-error turn shows its error once, not also as a reply The CLI reports an API error (such as a safeguards refusal) as a assistant frame with error set, then fails the turn with the same text. The adapter published that frame as an assistant message, so the error appeared three times. Skip it; the turn's failure carries it. Fixes pingdotgg/t3code#24171 Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 29 +++++++++++++++---- .../Adapters/ClaudeAdapterV2.ts | 11 +++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 15401e3eb844f..47e17a7cf9a1b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -12514,15 +12514,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"); diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 3b3e2b273977f..77b2139607e87 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 { @@ -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) { From fda3cf1ec963a56556320b95468c3718cfeb3c9d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 02:15:10 +0000 Subject: [PATCH 3/3] fix(claude): fail a flagged result even when its terminal reason is not a failure With the synthetic API-error frame no longer published, a result with is_error set but no failing terminal_reason settled as an empty completed turn. It now fails with the result's text. The replay fixture no longer expects the repeated error as a reply. Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts | 7 +++++++ .../src/orchestration-v2/Adapters/ClaudeAdapterV2.ts | 2 +- .../testkit/fixtures/claude_result_is_error/output.ts | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 47e17a7cf9a1b..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", diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 77b2139607e87..319c37fdad0e2 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4725,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"; } 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);