diff --git a/src/claude/outbound.ts b/src/claude/outbound.ts index bb09821a8d3..e0f2785d75d 100644 --- a/src/claude/outbound.ts +++ b/src/claude/outbound.ts @@ -11,7 +11,7 @@ * - errors: {type:"error", error:{type,message}}; may arrive mid-stream after HTTP 200. */ import { createHash } from "node:crypto"; -import { httpStatusFromTerminalError } from "../lib/errors"; +import { httpStatusFromTerminalError, isDefiniteContextOverflowMessage } from "../lib/errors"; import { isTransientUpstreamStatus } from "../lib/upstream-retry"; import { isTranslatorBudgetExceededError, @@ -70,8 +70,22 @@ export function anthropicErrorType(status: number): string { } } +/** + * Claude Code recognizes an overflowing prompt only by Anthropic's wording ("prompt is too long"), + * and only then runs its reactive compaction and the head-truncating retry inside /compact. A routed + * provider's own overflow text ("Your input exceeds the context window of this model") reads to it as + * a generic 400, so every later turn and every /compact resends the same oversized history and the + * session never recovers. Prefixed, never replaced: the provider text stays the evidence. + */ +function claudeCodeOverflowMessage(status: number, message: string, code?: string): string { + if (status === 429 || /prompt is too long/i.test(message)) return message; + return code === "context_length_exceeded" || isDefiniteContextOverflowMessage(message) + ? `prompt is too long: ${message}` + : message; +} + export function anthropicErrorBody(status: number, message: string, type?: string, code?: string): Rec { - return { type: "error", error: { type: type ?? anthropicErrorType(status), message, ...(code ? { code } : {}) } }; + return { type: "error", error: { type: type ?? anthropicErrorType(status), message: claudeCodeOverflowMessage(status, message, code), ...(code ? { code } : {}) } }; } export function anthropicErrorResponse(status: number, message: string, type?: string, code?: string): Response { diff --git a/src/combos/failover.ts b/src/combos/failover.ts index 0fb852770a1..3082417fce1 100644 --- a/src/combos/failover.ts +++ b/src/combos/failover.ts @@ -1,5 +1,5 @@ import { parseResetCooldownMs } from "../codex/routing"; -import { classifyError, isCyberPolicyCode } from "../lib/errors"; +import { classifyError, isCyberPolicyCode, isDefiniteContextOverflowMessage } from "../lib/errors"; import { isNonReplayableUpstreamCode } from "../lib/upstream-retry"; import type { OcxComboTarget } from "../types"; import { targetKey } from "./types"; @@ -616,30 +616,9 @@ function isProviderTargetContextOverflow( /** A status can carry a verdict about the REQUEST; 401/403/429 speak about the credential. */ const CONTEXT_VERDICT_STATUSES: ReadonlySet = new Set([400, 413, 422]); -/** - * Phrases a provider emits when the INPUT does not fit this model's context window. Matched - * against the innermost provider message only, so an unrelated refusal that merely quotes one - * of these tokens in a code field cannot authorize a replay. - */ -const DEFINITE_CONTEXT_OVERFLOW_PHRASES = [ - "exceeds the context window", - "exceed the context window", - "context window exceeded", - "context length exceeded", - "maximum context length", - "maximum context window", - "too many tokens", -]; - /** Wrapper envelopes unwrapped before the leaf message is read. */ const MAX_CONTEXT_OVERFLOW_ENVELOPES = 4; -function isDefiniteContextOverflowMessage(text: string): boolean { - const normalized = text.toLowerCase(); - return normalized === "context_length_exceeded" - || DEFINITE_CONTEXT_OVERFLOW_PHRASES.some(phrase => normalized.includes(phrase)); -} - /** * Confirm a context overflow from the provider MESSAGE rather than from a code token that * merely appears somewhere in the envelope. An upstream controls both fields and can emit a diff --git a/src/lib/errors.ts b/src/lib/errors.ts index 4a3f44ff868..c630cd625c9 100644 --- a/src/lib/errors.ts +++ b/src/lib/errors.ts @@ -684,3 +684,24 @@ export function httpStatusFromTerminalError(error: { } return 502; } + +/** + * Phrases a provider emits when the INPUT does not fit this model's context window. Matched + * against the innermost provider message only, so an unrelated refusal that merely quotes one + * of these tokens in a code field cannot authorize a replay. + */ +const DEFINITE_CONTEXT_OVERFLOW_PHRASES = [ + "exceeds the context window", + "exceed the context window", + "context window exceeded", + "context length exceeded", + "maximum context length", + "maximum context window", + "too many tokens", +]; + +export function isDefiniteContextOverflowMessage(text: string): boolean { + const normalized = text.toLowerCase(); + return normalized === "context_length_exceeded" + || DEFINITE_CONTEXT_OVERFLOW_PHRASES.some(phrase => normalized.includes(phrase)); +} diff --git a/tests/claude-integration/claude-529-mapping.test.ts b/tests/claude-integration/claude-529-mapping.test.ts index 480ba7bd220..1b35ed584b4 100644 --- a/tests/claude-integration/claude-529-mapping.test.ts +++ b/tests/claude-integration/claude-529-mapping.test.ts @@ -126,8 +126,42 @@ test("non-transient upstream 400 stays 400 invalid_request_error; no retry", asy }); expect(calls).toBe(1); expect(response.status).toBe(400); - const json = await response.json() as { error?: { type?: string } }; + const json = await response.json() as { error?: { type?: string; message?: string } }; expect(json.error?.type).toBe("invalid_request_error"); + expect(json.error?.message).toBe("bad request shape"); + } finally { + await server.stop(true); + await upstream.stop(true); + } +}); + +// Claude Code runs its reactive compaction and the head-truncating /compact retry only for Anthropic's +// "prompt is too long" wording. OpenAI's overflow text alone left a hosted session refusing every turn +// and every /compact (COMPACTION-400-REPRO, 2026-11-07). +test("upstream context-window 400 reaches Claude Code as prompt is too long, provider text kept", async () => { + const upstream = Bun.serve({ + port: 0, + fetch() { + return Response.json({ error: { + message: "Your input exceeds the context window of this model. Please adjust your input and try again.", + type: "invalid_request_error", code: "context_length_exceeded", + } }, { status: 400 }); + }, + }); + saveConfig(nativeConfig(`${upstream.url.toString().replace(/\/$/, "")}/v1`)); + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/messages", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: messagesBody(), + }); + expect(response.status).toBe(400); + const json = await response.json() as { error?: { type?: string; message?: string } }; + expect(json.error?.type).toBe("invalid_request_error"); + expect(json.error?.message).toBe( + "prompt is too long: Your input exceeds the context window of this model. Please adjust your input and try again.", + ); } finally { await server.stop(true); await upstream.stop(true);