diff --git a/src/util.ts b/src/util.ts index 2fc9dd5935..1752f43955 100644 --- a/src/util.ts +++ b/src/util.ts @@ -286,6 +286,31 @@ 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, + // (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 { @@ -321,6 +346,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; } @@ -340,6 +377,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 new file mode 100644 index 0000000000..b43e006907 --- /dev/null +++ b/tests/cjk-context-overflow.test.ts @@ -0,0 +1,182 @@ +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); +}); + +// ── 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); +}); + +// ── 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); +});