Skip to content
TypeScript

fix(server): preserve Claude final-result API error text (#19136)

Ibrahim Elkamali55a8fe9a938c
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
index 19311662d75..58460f92d80 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
@@ -3642,6 +3642,97 @@ describe("ClaudeAdapterV2 background wake turns", () => {
),
);
+ it.effect.each([
+ {
+ name: "an unflagged result with surrounding whitespace",
+ isError: false,
+ result: " API Error: 400 Request rejected by a safeguard.\n",
+ expected: "API Error: 400 Request rejected by a safeguard.",
+ },
+ {
+ name: "a secret crossing the truncation boundary",
+ isError: true,
+ result: `API Error: 400 ${"x".repeat(440)} {"token":"${"synthetic-secret".repeat(100)}"} ${"y".repeat(100)}`,
+ expected: `API Error: 400 ${"x".repeat(440)} {"token":"[REDACTED]"} `.padEnd(500, "y"),
+ },
+ {
+ name: "ordinary assistant text",
+ isError: true,
+ result: "I found three files.",
+ expected: "Claude gave up after repeated API errors.",
+ },
+ {
+ name: "a listed non-diagnostic error",
+ isError: true,
+ result: "API Error: 400 Request rejected.",
+ errors: ["[ede_diagnostic] internal detail", "Tool execution failed: EACCES"],
+ expected: "Tool execution failed: EACCES",
+ },
+ {
+ name: "an HTTP 529 overload",
+ isError: true,
+ result: "API Error: 529 Specific overload detail.",
+ apiErrorStatus: 529,
+ expected: "Claude API is overloaded (529). Try again shortly.",
+ },
+ {
+ name: "an HTTP 429 rate limit",
+ isError: true,
+ result: "API Error: 429 Specific rate limit detail.",
+ apiErrorStatus: 429,
+ expected: "Claude API rate limit reached. Try again later.",
+ },
+ {
+ name: "a different terminal reason",
+ isError: true,
+ result: "API Error: 400 Request rejected.",
+ terminalReason: "prompt_too_long" as const,
+ expected:
+ "Claude stopped: the prompt exceeds the model's context window. Run /compact or start a new thread.",
+ },
+ ])("reports terminal API error text for $name", (input) =>
+ Effect.gen(function* () {
+ const harness = yield* makeWakeHarness;
+ const now = yield* DateTime.now;
+ yield* harness.runtime.startTurn(
+ makeClaudeTestTurnInput({
+ threadId: harness.threadId,
+ providerThread: harness.providerThread,
+ now,
+ attemptId: RunAttemptId.make("attempt-claude-specific-api-error"),
+ text: "Continue.",
+ attachments: [],
+ }),
+ );
+ const apiErrorStatus = input.apiErrorStatus ?? 400;
+ yield* Queue.offer(
+ harness.sdkMessages,
+ makeResultFrame({
+ uuid: "00000000-0000-4000-8000-000000000699",
+ result: input.result,
+ terminalReason: input.terminalReason ?? "api_error",
+ apiErrorStatus,
+ isError: input.isError,
+ ...(input.errors === undefined ? {} : { errors: input.errors }),
+ }),
+ );
+
+ const terminal = yield* Queue.take(harness.terminalReceipts);
+ assert.equal(terminal.status, "failed");
+ if (terminal.status !== "failed") return;
+ assert.equal(terminal.failure.message, input.expected);
+ assert.equal(terminal.failure.code, `api_error_${apiErrorStatus}`);
+ assert.equal(
+ terminal.failure.class,
+ apiErrorStatus === 429 ? "usage_limit" : "provider_error",
+ );
+ }).pipe(
+ Effect.provide(
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
+ ),
+ ),
+ );
+
it.effect("names an expired Claude login instead of the terminal API error", () =>
Effect.gen(function* () {
const configDir = "/synthetic/Claude config";
@@ -3673,7 +3764,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
}),
makeResultFrame({
uuid: "00000000-0000-4000-8000-000000000607",
- result: "API Error",
+ result: "API Error: 401 Credentials expired.",
terminalReason: "api_error",
}),
]);
@@ -3932,7 +4023,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
harness.sdkMessages,
makeResultFrame({
uuid: "00000000-0000-4000-8000-000000000610",
- result: "API Error",
+ result: recovered ? "API Error" : "API Error: 400 Specific provider detail.",
terminalReason: "api_error",
}),
);
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
index d6aada46a90..b1762e89710 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
@@ -2958,11 +2958,17 @@ function providerFailureFromResult(
? "usage_limit"
: "provider_error";
const listedError = resultUserFacingError(message);
+ const apiErrorMessage =
+ message.subtype === "success" &&
+ message.terminal_reason === "api_error" &&
+ message.result.trim().startsWith("API Error:")
+ ? makeProviderFailure({ message: message.result }).message.slice(0, 500)
+ : undefined;
const structuredError = isOverloadedResult(message)
? "Claude API is overloaded (529). Try again shortly."
: message.subtype === "success" && message.api_error_status === 429
? "Claude API rate limit reached. Try again later."
- : terminalResultError(message.terminal_reason, failureHint);
+ : terminalResultError(message.terminal_reason, failureHint ?? apiErrorMessage);
if (message.subtype !== "success") {
return makeProviderFailure({
message: listedError ?? structuredError ?? message.errors.join("\n"),