Skip to content

Commit 2483f40

Browse files
committed
fix(errors): narrow the server-class override to the 400 verdict only
Exact-head CI caught what my focused suites missed: the first version of this fix returned a blanket 502 for any structured server class, which flattened genuine 504 and 503 verdicts and broke tests/web-search-timeout-contract.ts (a routed-body stall is really a gateway timeout, and the log surface asserts 504). Reproduced locally: 2 fail in that suite. The classification is authoritative about blame, not about which server status fits. So message inference still picks the specific status, and only the 400 verdict is overridden — the one case that both blames the caller and stops the retry. 429, 499, 401 and 403 are left alone because each is a signal the caller routes on, and overriding them would trade one misreport for another. Differential probe vs origin/dev: exactly two cases change, both 400 -> 502 (upstream 5xx carrying 'malformed' or 'invalid request' text). 504, 503, 429, 499, cyber-policy 400, auth 401, permission 403, real invalid_request 400, proxy_error 500 and the no-message 502 default are all unchanged. 210 pass / 0 fail across the nine suites that touch this function. Mutation oracle still holds: 45/1 without the fix, 46/0 with it.
1 parent 93975ab commit 2483f40

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

‎src/lib/errors.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,20 +404,30 @@ 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;
415407
// Client-closed messages often arrive as invalid_request_error after classifyError; check message
416408
// before treating every invalid_request_error as HTTP 400.
417409
const message = error.message ?? "";
418410
if (message && isClientClosedMessage(message)) return 499;
419411
if (error.type === "invalid_request_error") return 400;
420412
if (error.type === "proxy_error") return 500;
421-
if (message) return inferHttpStatusFromAdapterMessage(message);
413+
// A structured server class must not be downgraded to a CLIENT error by message wording.
414+
// classifyError assigns `server_error` + `upstream_server_error` to every 5xx it sees, so
415+
// the class is authoritative about blame: the upstream failed, the caller did not send a
416+
// bad request. What it is NOT authoritative about is which server status fits — a stall is
417+
// genuinely 504 and an overload genuinely 503, and flattening those to 502 discards
418+
// information both the log surface and the retry policy read. So message inference still
419+
// chooses the specific status, and only a client-error verdict is overridden.
420+
//
421+
// The override is deliberately narrowed to 400 alone. 429, 499, 401 and 403 are all
422+
// actionable signals the caller routes on — retry-after, client cancellation, re-auth,
423+
// entitlement — and overriding them would trade one kind of misreport for another. 400 is
424+
// the single verdict that both blames the caller and stops the retry, which is the failure
425+
// being fixed: an upstream 500 whose text happens to contain "malformed" or "invalid
426+
// request" used to return 400, so Claude Code stopped retrying a retryable failure.
427+
const structuredServerClass = error.type === "server_error" || error.code === "upstream_server_error";
428+
if (message) {
429+
const inferred = inferHttpStatusFromAdapterMessage(message);
430+
return structuredServerClass && inferred === 400 ? 502 : inferred;
431+
}
422432
return 502;
423433
}

0 commit comments

Comments
 (0)