Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions tests/context-overflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
Loading