From 0012480223833ee8106b73951735dae853d51dd7 Mon Sep 17 00:00:00 2001 From: dadaya <1179002658@qq.com> Date: Mon, 19 Oct 2026 21:45:44 +0800 Subject: [PATCH 1/3] fix(util): recognise CJK context-overflow rejections so the shrink arm can fire MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Chinese-fronted relays (DeepSeek / Qwen / GLM / Moonshot and the [OI]-compatible resellers between them) translate the provider's context-overflow rejection into Chinese. CONTEXT_OVERFLOW_PATTERNS held 18 English markers only, so those 400s fell through as generic bad requests: armOverflowShrink never ran, no shrink was armed, and the identical oversized payload was re-forwarded on every retry while bili's meter stayed frozen at its last (stale) reading — the client saw the same error forever. Add five CJK markers that pair a CONTEXT subject with an exceed verb inside one clause, so throttle phrasing (频率超限 / 请求过于频繁 / 触发限流) can never match. parseOverflowWindow gains the CJK twin: the window is the number attached to the limit word (上限 / 最大 / 限制), never the payload size quoted earlier in the same sentence, and a trailing token unit is required so a bare 上限 3 次 quota line cannot masquerade as a window. Field repro (v0.1.202, DeepSeek-compatible relay, 5.0MiB outbound): {"message":"请求上下文超限:本次请求约 1748713 tokens,超过单次上限 1500000 tokens。请缩短对话历史或开启上下文压缩后重试"} before: isOverflow=false, window=undefined -> 400 loop, no arm after: isOverflow=true, window=1500000 -> arm fires, fold happens --- src/util.ts | 30 +++++++ tests/cjk-context-overflow.test.ts | 130 +++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 tests/cjk-context-overflow.test.ts diff --git a/src/util.ts b/src/util.ts index 2fc9dd5935..3f57dd6b7a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -286,6 +286,24 @@ const CONTEXT_OVERFLOW_PATTERNS: RegExp[] = [ /context_limit/i, /上下文.{0,8}(?:超出|超过|超限)/, /(?:超出|超过|超限).{0,8}上下文/, + // The #3787 pair above covers only 上下文 as the SUBJECT and 超出/超过/超限 + // as the predicate. Chinese relays phrase the same rejection several other + // ways, and every one of them falls through to "generic bad request" today + // — no shrink armed, the identical oversized payload re-forwarded on every + // retry (#3240's field loop). Each entry below still pairs a subject with a + // limit predicate INSIDE one clause ([^。;;!!\n] never crosses a sentence), + // so throttle phrasing (请求频率超限 / 请求过于频繁 / 触发限流) and the + // output-length notice (输出内容过长) stay dark: neither names an input-side + // subject that has run past a limit. + // (a) 上下文 described as full/over-long rather than exceeded. + /上下文.{0,8}(?:已满|过长|超长|太大)/, + // (b) the ceiling named 模型上限 / 上下文窗口 instead of 上下文. + /(?:超出|超过|超限).{0,12}(?:模型上限|上下文窗口)/, + // (c) the input side named instead of 上下文: "输入过长,请减少对话轮次", + // "提示词太长,请精简后重试", "消息超长". + /(?:输入|请求|提示词|对话|消息)[^。;;!!\n]{0,10}(?:过长|太长|超长)/, + // (d) the token counter itself as the subject: "输入 token 数量超出允许范围". + /tokens?[^。;;!!\n]{0,10}(?:超出|超过|超限)/i, ]; function toTokenNumber(s: string): number | undefined { @@ -321,6 +339,18 @@ function parseOverflowWindow(text: string): number | undefined { text.match(/limit of (\d[\d,]*)\s*token/i) ?? text.match(/(\d[\d,]*)\s*maximum\b/i); if (m) return toTokenNumber(m[1]); + // CJK: the window is the number ATTACHED TO the limit word (上限 / 最大 / + // 限制), never the payload size quoted elsewhere in the same sentence — + // "本次请求约 1748713 tokens,超过单次上限 1500000 tokens" must read 1500000. + // The trailing form ("200000 tokens 的上限") is tried FIRST: when a relay + // states both numbers that way, the leading form would otherwise reach past + // the limit word and grab the payload quoted after it. A trailing unit + // (tokens / token / 个token) is required either way so a bare "上限 3 次" + // throttle line cannot masquerade as a window. + m = + text.match(/(\d[\d,]*)\s*(?:个\s*)?tokens?\s*(?:的)?(?:上限|最大|限制)/i) ?? + text.match(/(?:上限|最大(?:值|限制)?|限制)[^。;;!!\n]{0,20}?(\d[\d,]*)\s*(?:个\s*)?tokens?/i); + if (m) return toTokenNumber(m[1]); return undefined; } diff --git a/tests/cjk-context-overflow.test.ts b/tests/cjk-context-overflow.test.ts new file mode 100644 index 0000000000..00e5addf39 --- /dev/null +++ b/tests/cjk-context-overflow.test.ts @@ -0,0 +1,130 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { inspectContextOverflow } from "../src/util.ts"; + +// #3787 taught the overflow detector to read the 上下文 family of Chinese +// rejections. Two gaps survive it, and both leave the same failure mode in +// place: `armOverflowShrink` never runs, so no emergency shrink is armed, and +// the identical oversized payload is re-forwarded on every retry while the +// local meter stays frozen at its last reading — the client sees the same error +// forever (field repro: #3240's relay loop, three 400s over four minutes on a +// 5.0 MiB body with zero `armed emergency shrink` lines in the log). +// +// 1. A rejection that names the INPUT side rather than 上下文 +// ("输入过长,请减少对话轮次") matches no marker at all. +// 2. `parseOverflowWindow` is English-only, so even a recognized CJK body +// yields no window: the arm falls into the "window not parseable" branch +// and sizes at the rejected payload's own estimate instead of the limit +// the relay just stated. +// +// Each gate ships both directions (#3617): a false positive arms a shrink +// against a payload that fits, so the shared vocabulary (throttle, balance, +// output-length) must stay dark. + +test("CJK: field repro '请求上下文超限:本次请求约 N tokens,超过单次上限 W tokens' → overflow + window W", () => { + // Verbatim body from a live 400 loop. The payload number precedes the limit + // number, so the window parse must take the number attached to 上限, never + // the first number in the sentence. + const body = + "请求上下文超限:本次请求约 1748713 tokens,超过单次上限 1500000 tokens。请缩短对话历史或开启上下文压缩后重试(请求未发送到上游,未计费)。"; + const info = inspectContextOverflow(400, body); + assert.equal(info.isOverflow, true); + assert.equal(info.window, 1500000); +}); + +test("CJK: input-side phrasing '输入过长' / '提示词太长' → overflow (#3787 leaves these dark)", () => { + assert.equal(inspectContextOverflow(400, "输入过长:请减少对话轮次").isOverflow, true); + assert.equal(inspectContextOverflow(400, "提示词太长,请精简后重试").isOverflow, true); +}); + +test("CJK: '请求/对话/消息' + length predicate → overflow", () => { + assert.equal(inspectContextOverflow(400, "本次请求过长,已被拒绝").isOverflow, true); + assert.equal(inspectContextOverflow(400, "对话太长,请开启新会话").isOverflow, true); + assert.equal(inspectContextOverflow(400, "消息超长,无法提交").isOverflow, true); +}); + +test("CJK: token-side phrasing '输入 token 数量超出允许范围' → overflow", () => { + const info = inspectContextOverflow(400, "输入 token 数量超出允许范围"); + assert.equal(info.isOverflow, true); + assert.equal(info.window, undefined, "no window number stated → no window (a wrong guess is worse than none)"); +}); + +test("CJK: '本次请求超过模型上限 N tokens' → overflow + window N", () => { + const info = inspectContextOverflow(400, "本次请求超过模型上限 128000 tokens"); + assert.equal(info.isOverflow, true); + assert.equal(info.window, 128000); +}); + +test("CJK: 'N tokens 的上限' (limit word trails the number) → window N", () => { + // The reversed word order: relays that quote the limit first and the + // comparison after it. Only the number bound to the limit word counts — + // the payload size quoted elsewhere in the body must not be taken. + const info = inspectContextOverflow(400, "上下文超限:已突破 200000 tokens 的上限,本次请求约 240000 tokens"); + assert.equal(info.isOverflow, true); + assert.equal(info.window, 200000); +}); + +test("CJK: '上下文窗口已满' inside a JSON error envelope → overflow, no window", () => { + const body = JSON.stringify({ error: { message: "上下文窗口已满,无法继续对话", type: "invalid_request_error" } }); + const info = inspectContextOverflow(400, body); + assert.equal(info.isOverflow, true); + assert.equal(info.window, undefined); +}); + +test("CJK: 5xx gateway relay of a CJK overflow body → overflow (#2484 lane)", () => { + // Some gateways relay the provider's rejection under 502/503 instead of 400. + const info = inspectContextOverflow(502, "上游返回:上下文超限"); + assert.equal(info.isOverflow, true); +}); + +test("CJK: 413 with a CJK overflow body → overflow", () => { + const info = inspectContextOverflow(413, "请求体过大,超出上下文限制"); + assert.equal(info.isOverflow, true); +}); + +// ── negative direction: shared vocabulary, NOT a context overflow ──────────── + +test("CJK negative: '请求频率超限' (throttle) → not an overflow", () => { + const info = inspectContextOverflow(429, "请求频率超限,请稍后重试"); + assert.equal(info.isOverflow, false); +}); + +test("CJK negative: '触发限流' → not an overflow", () => { + assert.equal(inspectContextOverflow(400, "触发限流,请降低并发").isOverflow, false); +}); + +test("CJK negative: '请求过于频繁' → not an overflow", () => { + // 请求 + a frequency predicate is throttle phrasing; only a LENGTH predicate + // on an input subject means the window. + assert.equal(inspectContextOverflow(429, "请求过于频繁,请稍后重试").isOverflow, false); +}); + +test("CJK negative: '账户余额不足' → not an overflow", () => { + assert.equal(inspectContextOverflow(400, "账户余额不足,请充值").isOverflow, false); +}); + +test("CJK negative: '参数错误:messages 不能为空' → not an overflow", () => { + assert.equal(inspectContextOverflow(400, "参数错误:messages 不能为空").isOverflow, false); +}); + +test("CJK negative: '服务暂时不可用' (5xx, no input subject) → not an overflow", () => { + assert.equal(inspectContextOverflow(503, "服务暂时不可用,请稍后重试").isOverflow, false); +}); + +test("CJK negative: '输出内容过长,已被截断' (output cap, not input window) → not an overflow", () => { + // 内容过长 without an input/request/context subject is the OUTPUT-length + // notice; shrinking the input would not fix it. + assert.equal(inspectContextOverflow(400, "输出内容过长,已被截断").isOverflow, false); +}); + +test("CJK negative: status 200 never counts, whatever the body says", () => { + assert.equal(inspectContextOverflow(200, "上下文超限").isOverflow, false); +}); + +test("CJK negative: '上限 3 次' (rate cap, no token unit) yields no window", () => { + // The window parser requires a token unit; a bare numeric cap is a quota, + // not a context window. + const info = inspectContextOverflow(400, "本分钟调用上限 3 次,上下文超限请重试"); + assert.equal(info.isOverflow, true); + assert.equal(info.window, undefined); +}); From 8b811c5f0788e39187291366ea579d847ffa9cda Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 23:24:55 +0800 Subject: [PATCH 2/3] fix(util): teach parseOverflowCounts the CJK payload-count phrasing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review supplement for #3947 cause-5 (agreed in the #3947 thread): the CJK branch of parseOverflowWindow landed in this PR, but parseOverflowCounts stayed English-only — '本次请求约 1748713 tokens,超过单次上限 1500000 tokens' classifies as overflow yet yields no requestedTotal, while the limit number must never be read as the payload size. The branch reads the number adjacent to the overflow predicate (subject-anchored on 请求/输入, limit vocabulary between subject and number vetoes the guess) and books it as total+messages (a rejection bills input only, #3450). Both directions pinned: field repro → requestedTotal 1748713 with window still 1500000; ceiling phrasing ('请求上限为 N tokens,超出部分将被截断') → no requestedTotal, window N. --- src/util.ts | 12 ++++++++++++ tests/cjk-context-overflow.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/util.ts b/src/util.ts index 3f57dd6b7a..5987c543a8 100644 --- a/src/util.ts +++ b/src/util.ts @@ -370,6 +370,18 @@ function parseOverflowCounts(text: string): { total?: number; messages?: number; // tokens allowed (1048576)." m = text.match(/input token count\s*\((\d[\d,]*)\)\s*exceeds/i); if (m) return { total: toTokenNumber(m[1]), messages: toTokenNumber(m[1]) }; + // CJK relay (#3947 field body): "本次请求约 1748713 tokens,超过单次上限 + // 1500000 tokens" — the rejected payload's count is the number ADJACENT TO + // THE OVERFLOW PREDICATE (number-tokens-超过), never the limit attached to + // 上限/最大/限制 (that half belongs to parseOverflowWindow). Subject-anchored + // (请求/输入), and the span between subject and number must be free of limit + // vocabulary: "您的请求最多支持 1500000 tokens,超出部分将被截断" states a + // CEILING, not a measurement. A rejection bills input only — no completion + // has run yet — so total and messages carry the same number (#3450). + for (const mm of text.matchAll(/(?:请求|输入)([^。;;!!\n]{0,12}?)(\d[\d,]*)\s*(?:个\s*)?tokens?\s*[,,]?\s*(?:超过|超出|超限)/gi)) { + const n = toTokenNumber(mm[2]); + if (n !== undefined && !/(?:支持|最大|上限|限制|允许)/.test(mm[1])) return { total: n, messages: n }; + } return {}; } diff --git a/tests/cjk-context-overflow.test.ts b/tests/cjk-context-overflow.test.ts index 00e5addf39..1488789a65 100644 --- a/tests/cjk-context-overflow.test.ts +++ b/tests/cjk-context-overflow.test.ts @@ -128,3 +128,29 @@ test("CJK negative: '上限 3 次' (rate cap, no token unit) yields no window", assert.equal(info.isOverflow, true); assert.equal(info.window, undefined); }); + +// ── requestedTotal (parseOverflowCounts) — the rejected payload's own size ─── + +test("CJK counts: field repro → requestedTotal is the payload size N, never the limit W", () => { + // Both parsers run on the same body and must each take their half: the + // window reader the number bound to 上限, the counts reader the one bound + // to the overflow predicate. A rejection bills input only (the request was + // never forwarded), so total and messages carry the same number (#3450). + const body = + "请求上下文超限:本次请求约 1748713 tokens,超过单次上限 1500000 tokens。请缩短对话历史或开启上下文压缩后重试(请求未发送到上游,未计费)。"; + const info = inspectContextOverflow(400, body); + assert.equal(info.isOverflow, true); + assert.equal(info.window, 1500000); + assert.equal(info.requestedTotal, 1748713); + assert.equal(info.requestedMessages, 1748713); +}); + +test("CJK counts: '…请求上限为 N tokens,超出部分将被截断' states a CEILING → no requestedTotal", () => { + // Limit vocabulary between the subject and the number vetoes the guess: + // this body names the supported maximum, not the measured payload. The + // window parser still reads it correctly — the ceiling IS the window here. + const info = inspectContextOverflow(400, "上下文超限:您的请求上限为 1500000 tokens,超出部分将被截断"); + assert.equal(info.isOverflow, true); + assert.equal(info.window, 1500000); + assert.equal(info.requestedTotal, undefined); +}); From 85a8166c6222ee1346de661eb70b471bd4281bcf Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 23:25:08 +0800 Subject: [PATCH 3/3] fix(util): consolidate #3246's residual CJK phrasings into this PR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #3246 (Closes #3240) predates this PR and is now mergeable:false against master because #3787 landed the 上下文 direction it carried. After #3787 + this PR's (a)-(d), two of its patterns keep unique coverage: the input side named with an EXCEED predicate ('输入长度超出限制' — (c)'s length predicates do not reach it, and extending (c)'s subjects to 超出/超过/超限 would light up throttle phrasings like '请求超出频率限制') and the mixed-language 'prompt 过长'. Both are folded in here so the CJK detector surface lands in one PR; #3246's field-phrasing regression cases (b-c-pi#639) move into tests/cjk-context-overflow.test.ts alongside them. --- src/util.ts | 7 +++++++ tests/cjk-context-overflow.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/util.ts b/src/util.ts index 5987c543a8..1752f43955 100644 --- a/src/util.ts +++ b/src/util.ts @@ -304,6 +304,13 @@ const CONTEXT_OVERFLOW_PATTERNS: RegExp[] = [ /(?:输入|请求|提示词|对话|消息)[^。;;!!\n]{0,10}(?:过长|太长|超长)/, // (d) the token counter itself as the subject: "输入 token 数量超出允许范围". /tokens?[^。;;!!\n]{0,10}(?:超出|超过|超限)/i, + // (e) #3240/#3246 consolidation: the INPUT side named with an EXCEED + // predicate rather than a length predicate ("输入长度超出限制"). The + // exceed set stays input-only: extending (c)'s subjects to 超出/超过/ + // 超限 would light up throttle phrasings like "请求超出频率限制". + /输入[^。;;!!\n]{0,12}(?:超出|超过|超限)/, + // (f) #3240/#3246 consolidation: mixed-language gateways write "prompt 过长". + /prompt[^。;;!!\n]{0,6}(?:过长|太长|超长)/i, ]; function toTokenNumber(s: string): number | undefined { diff --git a/tests/cjk-context-overflow.test.ts b/tests/cjk-context-overflow.test.ts index 1488789a65..b43e006907 100644 --- a/tests/cjk-context-overflow.test.ts +++ b/tests/cjk-context-overflow.test.ts @@ -154,3 +154,29 @@ test("CJK counts: '…请求上限为 N tokens,超出部分将被截断' state assert.equal(info.window, 1500000); assert.equal(info.requestedTotal, undefined); }); + +// ── consolidated from #3246 (b-c-pi#639 field phrasings) ───────────────────── + +test("CJK: #3240 field phrasings (consolidated via #3246) → overflow, no window learned", () => { + 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("CJK: errors merely mentioning 上下文/token stay dark (consolidated via #3246)", () => { + 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); +});