From bcd039f57464d5a27d07ba54d99751e4a5a4b176 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 12:49:33 +0800 Subject: [PATCH] fix: recognize Chinese relay overflow wording in the context-overflow detector (#3240) --- src/util.ts | 12 +++++++++++ tests/context-overflow.test.ts | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/util.ts b/src/util.ts index cd307566fd..cf3500f5dc 100644 --- a/src/util.ts +++ b/src/util.ts @@ -261,6 +261,18 @@ const CONTEXT_OVERFLOW_PATTERNS: RegExp[] = [ // Gemini (generativelanguage.googleapis.com) 400 INVALID_ARGUMENT: // "The input token count (N) exceeds the maximum number of tokens allowed (W)." /exceeds the maximum number of tokens/i, + // #3240: domestic relay/gateway overflow wording — 中转站 localize provider + // rejections ("上下文长度超出限制", "输入长度超过模型支持的最大上下文长度", + // "请求的输入 token 数超出限制", …) and every pattern above is English, so a + // Chinese body never arms the overflow self-heal and a wrong declared window + // locks the session on repeated identical 400s. A directional verb + // (超出/超过/超限/过长/超长) is required: an unrelated 400 that merely mentions + // 上下文/token (auth expiry, rate limit, format error) must not match. + /上下文.{0,10}(?:超出|超过|超限|过长|超长)/, + /(?:超出|超过|超限).{0,10}上下文/, + /输入.{0,12}(?:超出|超过|超限|过长|超长)/, + /token\s*数.{0,10}(?:超出|超过|超限)/i, + /prompt.{0,6}(?:过长|太长|超长)/i, ]; function toTokenNumber(s: string): number | undefined { diff --git a/tests/context-overflow.test.ts b/tests/context-overflow.test.ts index 77ac2fe9ea..4264d590c4 100644 --- a/tests/context-overflow.test.ts +++ b/tests/context-overflow.test.ts @@ -151,6 +151,43 @@ test("a bare 5xx without an overflow marker is NOT an overflow", () => { assert.equal(inspectContextOverflow(503, '{"error":"upstream unavailable"}').isOverflow, false); }); +test("Chinese relays: localized overflow wording → overflow (#3240)", () => { + // Domestic relay/gateway (中转站) bodies localize the provider's rejection; + // phrasings quoted in billion-context-pi#639 plus common gateway variants. + // None carries a parseable window number → arm at payload size, never learn + // a wrong window. + const zhBodies = [ + JSON.stringify({ error: { message: "上下文长度超出限制" } }), + JSON.stringify({ message: "输入长度超过模型支持的最大上下文长度" }), + JSON.stringify({ error: { message: "请求的输入 token 数超出限制" } }), + JSON.stringify({ error: { message: "输入 tokens 超过模型最大上下文长度" } }), + JSON.stringify({ error: { message: "超出模型的上下文窗口限制" } }), + JSON.stringify({ error: { message: "prompt 过长,请缩短输入后重试" } }), + ]; + for (const body of zhBodies) { + const info = inspectContextOverflow(400, body); + assert.equal(info.isOverflow, true, `should match: ${body}`); + assert.equal(info.window, undefined, `no number to learn from: ${body}`); + } +}); + +test("a 5xx carrying a Chinese overflow marker IS an overflow (#2484 + #3240)", () => { + assert.equal( + inspectContextOverflow(502, '{"error":{"message":"上下文长度超出限制","type":"upstream_error"}}').isOverflow, + true, + ); +}); + +test("Chinese errors that merely mention 上下文/token are NOT overflows (#3240)", () => { + // Directional-verb gate: auth expiry, rate limit and format errors must not + // arm the emergency shrink. + assert.equal(inspectContextOverflow(400, '{"message":"无效的 API key"}').isOverflow, false); + assert.equal(inspectContextOverflow(400, '{"message":"请求过于频繁,请稍后再试"}').isOverflow, false); + assert.equal(inspectContextOverflow(400, '{"message":"token 已过期,请重新登录"}').isOverflow, false); + assert.equal(inspectContextOverflow(400, '{"message":"输入格式错误:content 不能为空"}').isOverflow, false); + assert.equal(inspectContextOverflow(400, '{"message":"当前上下文窗口配置不正确"}').isOverflow, false); +}); + test("empty body is NOT an overflow", () => { assert.equal(inspectContextOverflow(400, "").isOverflow, false); });