Skip to content

Commit 55a8fe9

Browse files
Marve10sjuliusmarmingeclaude
authored
fix(server): preserve Claude final-result API error text (pingdotgg#19136)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3c63b50 commit 55a8fe9

2 files changed

Lines changed: 100 additions & 3 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,6 +3642,97 @@ describe("ClaudeAdapterV2 background wake turns", () => {
36423642
),
36433643
);
36443644

3645+
it.effect.each([
3646+
{
3647+
name: "an unflagged result with surrounding whitespace",
3648+
isError: false,
3649+
result: " API Error: 400 Request rejected by a safeguard.\n",
3650+
expected: "API Error: 400 Request rejected by a safeguard.",
3651+
},
3652+
{
3653+
name: "a secret crossing the truncation boundary",
3654+
isError: true,
3655+
result: `API Error: 400 ${"x".repeat(440)} {"token":"${"synthetic-secret".repeat(100)}"} ${"y".repeat(100)}`,
3656+
expected: `API Error: 400 ${"x".repeat(440)} {"token":"[REDACTED]"} `.padEnd(500, "y"),
3657+
},
3658+
{
3659+
name: "ordinary assistant text",
3660+
isError: true,
3661+
result: "I found three files.",
3662+
expected: "Claude gave up after repeated API errors.",
3663+
},
3664+
{
3665+
name: "a listed non-diagnostic error",
3666+
isError: true,
3667+
result: "API Error: 400 Request rejected.",
3668+
errors: ["[ede_diagnostic] internal detail", "Tool execution failed: EACCES"],
3669+
expected: "Tool execution failed: EACCES",
3670+
},
3671+
{
3672+
name: "an HTTP 529 overload",
3673+
isError: true,
3674+
result: "API Error: 529 Specific overload detail.",
3675+
apiErrorStatus: 529,
3676+
expected: "Claude API is overloaded (529). Try again shortly.",
3677+
},
3678+
{
3679+
name: "an HTTP 429 rate limit",
3680+
isError: true,
3681+
result: "API Error: 429 Specific rate limit detail.",
3682+
apiErrorStatus: 429,
3683+
expected: "Claude API rate limit reached. Try again later.",
3684+
},
3685+
{
3686+
name: "a different terminal reason",
3687+
isError: true,
3688+
result: "API Error: 400 Request rejected.",
3689+
terminalReason: "prompt_too_long" as const,
3690+
expected:
3691+
"Claude stopped: the prompt exceeds the model's context window. Run /compact or start a new thread.",
3692+
},
3693+
])("reports terminal API error text for $name", (input) =>
3694+
Effect.gen(function* () {
3695+
const harness = yield* makeWakeHarness;
3696+
const now = yield* DateTime.now;
3697+
yield* harness.runtime.startTurn(
3698+
makeClaudeTestTurnInput({
3699+
threadId: harness.threadId,
3700+
providerThread: harness.providerThread,
3701+
now,
3702+
attemptId: RunAttemptId.make("attempt-claude-specific-api-error"),
3703+
text: "Continue.",
3704+
attachments: [],
3705+
}),
3706+
);
3707+
const apiErrorStatus = input.apiErrorStatus ?? 400;
3708+
yield* Queue.offer(
3709+
harness.sdkMessages,
3710+
makeResultFrame({
3711+
uuid: "00000000-0000-4000-8000-000000000699",
3712+
result: input.result,
3713+
terminalReason: input.terminalReason ?? "api_error",
3714+
apiErrorStatus,
3715+
isError: input.isError,
3716+
...(input.errors === undefined ? {} : { errors: input.errors }),
3717+
}),
3718+
);
3719+
3720+
const terminal = yield* Queue.take(harness.terminalReceipts);
3721+
assert.equal(terminal.status, "failed");
3722+
if (terminal.status !== "failed") return;
3723+
assert.equal(terminal.failure.message, input.expected);
3724+
assert.equal(terminal.failure.code, `api_error_${apiErrorStatus}`);
3725+
assert.equal(
3726+
terminal.failure.class,
3727+
apiErrorStatus === 429 ? "usage_limit" : "provider_error",
3728+
);
3729+
}).pipe(
3730+
Effect.provide(
3731+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3732+
),
3733+
),
3734+
);
3735+
36453736
it.effect("names an expired Claude login instead of the terminal API error", () =>
36463737
Effect.gen(function* () {
36473738
const configDir = "/synthetic/Claude config";
@@ -3673,7 +3764,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
36733764
}),
36743765
makeResultFrame({
36753766
uuid: "00000000-0000-4000-8000-000000000607",
3676-
result: "API Error",
3767+
result: "API Error: 401 Credentials expired.",
36773768
terminalReason: "api_error",
36783769
}),
36793770
]);
@@ -3932,7 +4023,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
39324023
harness.sdkMessages,
39334024
makeResultFrame({
39344025
uuid: "00000000-0000-4000-8000-000000000610",
3935-
result: "API Error",
4026+
result: recovered ? "API Error" : "API Error: 400 Specific provider detail.",
39364027
terminalReason: "api_error",
39374028
}),
39384029
);

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2958,11 +2958,17 @@ function providerFailureFromResult(
29582958
? "usage_limit"
29592959
: "provider_error";
29602960
const listedError = resultUserFacingError(message);
2961+
const apiErrorMessage =
2962+
message.subtype === "success" &&
2963+
message.terminal_reason === "api_error" &&
2964+
message.result.trim().startsWith("API Error:")
2965+
? makeProviderFailure({ message: message.result }).message.slice(0, 500)
2966+
: undefined;
29612967
const structuredError = isOverloadedResult(message)
29622968
? "Claude API is overloaded (529). Try again shortly."
29632969
: message.subtype === "success" && message.api_error_status === 429
29642970
? "Claude API rate limit reached. Try again later."
2965-
: terminalResultError(message.terminal_reason, failureHint);
2971+
: terminalResultError(message.terminal_reason, failureHint ?? apiErrorMessage);
29662972
if (message.subtype !== "success") {
29672973
return makeProviderFailure({
29682974
message: listedError ?? structuredError ?? message.errors.join("\n"),

0 commit comments

Comments
 (0)