From fdfec4e4909a8801c3ad8e590c27a8409e3b82fc Mon Sep 17 00:00:00 2001 From: Vadym O Date: Sat, 7 Nov 2026 19:20:47 +0800 Subject: [PATCH] claude ingress: report provider context overflow as "prompt is too long" Claude Code recognizes an oversized prompt only by Anthropic's wording ("prompt is too long"). Only that wording triggers its reactive compaction and the head-truncating retry inside /compact. A routed provider's own overflow 400 ("Your input exceeds the context window of this model", code context_length_exceeded) reached it verbatim and read as a generic 400. The session then resent the same oversized history on every turn and every /compact, and never recovered (hosted agent stuck on the Codex 272k window, COMPACTION-400-REPRO). anthropicErrorBody now prefixes "prompt is too long: " to definite context-overflow errors and keeps the provider text. Every Claude-ingress error path goes through it. isDefiniteContextOverflowMessage moves from combos/failover.ts to lib/errors.ts so both callers share one phrase list. 429s and messages that already carry the wording are left alone. Co-Authored-By: Claude Code --- src/claude/outbound.ts | 18 ++++++++-- src/combos/failover.ts | 23 +----------- src/lib/errors.ts | 21 +++++++++++ .../claude-529-mapping.test.ts | 36 ++++++++++++++++++- 4 files changed, 73 insertions(+), 25 deletions(-) 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);