Skip to content

Commit 2143f6e

Browse files
committed
fix(server): preserve Claude final-result API error text
1 parent 380bba3 commit 2143f6e

2 files changed

Lines changed: 117 additions & 3 deletions

File tree

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

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3146,6 +3146,114 @@ describe("ClaudeAdapterV2 background wake turns", () => {
31463146
),
31473147
);
31483148

3149+
it.effect.each([
3150+
{
3151+
name: "an error result",
3152+
isError: true,
3153+
result: "API Error: 400 Request rejected by a safeguard.",
3154+
expected: "API Error: 400 Request rejected by a safeguard.",
3155+
},
3156+
{
3157+
name: "an unflagged API error result",
3158+
isError: false,
3159+
result: "API Error: 400 Request rejected by a safeguard.",
3160+
expected: "API Error: 400 Request rejected by a safeguard.",
3161+
},
3162+
{
3163+
name: "surrounding whitespace",
3164+
isError: true,
3165+
result: " API Error: 400 Request rejected.\n",
3166+
expected: "API Error: 400 Request rejected.",
3167+
},
3168+
{
3169+
name: "an overlong error",
3170+
isError: true,
3171+
result: `API Error: 400 ${"x".repeat(1_000)}`,
3172+
expected: `API Error: 400 ${"x".repeat(485)}`,
3173+
},
3174+
{
3175+
name: "an empty error",
3176+
isError: true,
3177+
result: "",
3178+
expected: "Claude gave up after repeated API errors.",
3179+
},
3180+
{
3181+
name: "ordinary assistant text",
3182+
isError: true,
3183+
result: "I found three files.",
3184+
expected: "Claude gave up after repeated API errors.",
3185+
},
3186+
{
3187+
name: "a listed non-diagnostic error",
3188+
isError: true,
3189+
result: "API Error: 400 Request rejected.",
3190+
errors: ["[ede_diagnostic] internal detail", "Tool execution failed: EACCES"],
3191+
expected: "Tool execution failed: EACCES",
3192+
},
3193+
{
3194+
name: "an HTTP 529 overload",
3195+
isError: true,
3196+
result: "API Error: 529 Specific overload detail.",
3197+
apiErrorStatus: 529,
3198+
expected: "Claude API is overloaded (529). Try again shortly.",
3199+
},
3200+
{
3201+
name: "an HTTP 429 rate limit",
3202+
isError: true,
3203+
result: "API Error: 429 Specific rate limit detail.",
3204+
apiErrorStatus: 429,
3205+
expected: "Claude API rate limit reached. Try again later.",
3206+
},
3207+
{
3208+
name: "a different terminal reason",
3209+
isError: true,
3210+
result: "API Error: 400 Request rejected.",
3211+
terminalReason: "prompt_too_long" as const,
3212+
expected: "Claude stopped: the prompt exceeds the model's context window.",
3213+
},
3214+
])("reports terminal API error text for $name", (input) =>
3215+
Effect.gen(function* () {
3216+
const harness = yield* makeWakeHarness;
3217+
const now = yield* DateTime.now;
3218+
yield* harness.runtime.startTurn(
3219+
makeClaudeTestTurnInput({
3220+
threadId: harness.threadId,
3221+
providerThread: harness.providerThread,
3222+
now,
3223+
attemptId: RunAttemptId.make("attempt-claude-specific-api-error"),
3224+
text: "Continue.",
3225+
attachments: [],
3226+
}),
3227+
);
3228+
const apiErrorStatus = input.apiErrorStatus ?? 400;
3229+
yield* Queue.offer(
3230+
harness.sdkMessages,
3231+
makeResultFrame({
3232+
uuid: "00000000-0000-4000-8000-000000000699",
3233+
result: input.result,
3234+
terminalReason: input.terminalReason ?? "api_error",
3235+
apiErrorStatus,
3236+
isError: input.isError,
3237+
...(input.errors === undefined ? {} : { errors: input.errors }),
3238+
}),
3239+
);
3240+
3241+
const terminal = yield* Queue.take(harness.terminalReceipts);
3242+
assert.equal(terminal.status, "failed");
3243+
if (terminal.status !== "failed") return;
3244+
assert.equal(terminal.failure.message, input.expected);
3245+
assert.equal(terminal.failure.code, `api_error_${apiErrorStatus}`);
3246+
assert.equal(
3247+
terminal.failure.class,
3248+
apiErrorStatus === 429 ? "usage_limit" : "provider_error",
3249+
);
3250+
}).pipe(
3251+
Effect.provide(
3252+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3253+
),
3254+
),
3255+
);
3256+
31493257
it.effect("names an expired Claude login instead of the terminal API error", () =>
31503258
Effect.gen(function* () {
31513259
const configDir = "/synthetic/Claude config";
@@ -3177,7 +3285,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
31773285
}),
31783286
makeResultFrame({
31793287
uuid: "00000000-0000-4000-8000-000000000607",
3180-
result: "API Error",
3288+
result: "API Error: 401 Credentials expired.",
31813289
terminalReason: "api_error",
31823290
}),
31833291
]);
@@ -3238,7 +3346,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
32383346
harness.sdkMessages,
32393347
makeResultFrame({
32403348
uuid: "00000000-0000-4000-8000-000000000610",
3241-
result: "API Error",
3349+
result: recovered ? "API Error" : "API Error: 400 Specific provider detail.",
32423350
terminalReason: "api_error",
32433351
}),
32443352
);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2702,11 +2702,17 @@ function providerFailureFromResult(
27022702
? "usage_limit"
27032703
: "provider_error";
27042704
const listedError = resultUserFacingError(message);
2705+
const apiErrorMessage =
2706+
message.subtype === "success" &&
2707+
message.terminal_reason === "api_error" &&
2708+
message.result.trim().startsWith("API Error:")
2709+
? message.result.trim().slice(0, 500)
2710+
: undefined;
27052711
const structuredError = isOverloadedResult(message)
27062712
? "Claude API is overloaded (529). Try again shortly."
27072713
: message.subtype === "success" && message.api_error_status === 429
27082714
? "Claude API rate limit reached. Try again later."
2709-
: terminalResultError(message.terminal_reason, failureHint);
2715+
: terminalResultError(message.terminal_reason, failureHint ?? apiErrorMessage);
27102716
if (message.subtype !== "success") {
27112717
return makeProviderFailure({
27122718
message: listedError ?? structuredError ?? message.errors.join("\n"),

0 commit comments

Comments
 (0)