Skip to content

Commit 93975ab

Browse files
committed
fix(errors): let a structured server class outrank message inference
Closes the reviewer blocker on #2729. Deriving response.failed status from the classified payload is only correct if httpStatusFromTerminalError respects the classification, and it did not: only server_error + server_is_overloaded was recognized, so a generic upstream_server_error fell through to message inference. An upstream 5xx whose text contained 'malformed' or 'invalid request' returned 400, and Claude Code stopped retrying a genuinely retryable failure — the same inversion #2729 set out to fix, one layer down. classifyError assigns upstream_server_error to every 5xx it observes, so the class is authoritative over the words in the message. Probed before and after against origin/dev: 429/401/403/quota/invalid-request/ client-cancel/proxy_error/undefined all unchanged, and cyber-policy refusals still return 400 because that check runs earlier. Only the generic server class moves, 400 -> 502. Regression is behavioral and mutation-verified: revert the fix and keep the test -> 45 pass / 1 fail; with the fix -> 46 pass / 0 fail.
1 parent 19c328e commit 93975ab

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎src/lib/errors.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ export function httpStatusFromTerminalError(error: {
404404
) return 403;
405405
if (error.type === "insufficient_quota" || error.code === "insufficient_quota") return 429;
406406
if (error.type === "server_error" && error.code === "server_is_overloaded") return 503;
407+
// A structured generic upstream/server classification outranks message inference.
408+
// classifyError assigns `server_error` + `upstream_server_error` to every 5xx it sees,
409+
// so the class is authoritative: the provider already told us this was a server failure.
410+
// Falling through to the message heuristics below inverts that, because an upstream 500
411+
// whose text happens to contain "malformed" or "invalid request" is read as a client
412+
// error and returns 400. Claude Code then stops retrying a failure that was retryable,
413+
// and the caller sees invalid_request_error for an upstream outage.
414+
if (error.type === "server_error" || error.code === "upstream_server_error") return 502;
407415
// Client-closed messages often arrive as invalid_request_error after classifyError; check message
408416
// before treating every invalid_request_error as HTTP 400.
409417
const message = error.message ?? "";

‎tests/claude-outbound.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,38 @@ describe("claude outbound SSE", () => {
607607
});
608608
});
609609

610+
// Deriving the status from the classified payload is only safe if a STRUCTURED server
611+
// class outranks the message heuristics. `httpStatusFromTerminalError` recognized only
612+
// `server_error` + `server_is_overloaded`, so a generic `upstream_server_error` fell
613+
// through to `inferHttpStatusFromAdapterMessage`. An upstream 5xx whose text happens to
614+
// contain "malformed" or "invalid request" then returned 400, and Claude Code stopped
615+
// retrying a genuinely retryable upstream failure — the exact inversion this PR set out
616+
// to fix, reintroduced one layer down. classifyError assigns `upstream_server_error` to
617+
// every 5xx it sees, so the class is authoritative over the words in the message.
618+
test("generic upstream_server_error with client-sounding text stays a transient server failure", async () => {
619+
const upstream = [
620+
sse("response.created", { response: {} }),
621+
sse("response.failed", {
622+
response: {
623+
status: "failed",
624+
error: {
625+
type: "server_error",
626+
code: "upstream_server_error",
627+
message: "upstream stream produced malformed tool call arguments",
628+
},
629+
},
630+
}),
631+
].join("");
632+
const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m"));
633+
expect(events.at(-1)!.data).toEqual({
634+
type: "error",
635+
error: {
636+
type: "overloaded_error",
637+
message: "upstream stream produced malformed tool call arguments",
638+
},
639+
});
640+
});
641+
610642
test("internal reader exception stays api_error (not promoted to overloaded)", async () => {
611643
const boom = new ReadableStream<Uint8Array>({
612644
start(controller) {

0 commit comments

Comments
 (0)