From 931a1008d5f0d8dc0ec620d0c487575783e0901b Mon Sep 17 00:00:00 2001 From: Zhishu <963336185@qq.com> Date: Thu, 22 Oct 2026 12:47:22 +0800 Subject: [PATCH] fix(overflow): recognize the DashScope input-length-range 400 so the probe path can arm (#4262) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DashScope compatible-mode rejects an over-long input as "Range of input length should be [1, 1000000]" — no token/length subject phrase — so inspectContextOverflow did not treat it as an overflow: the #2313 probe forward took the 400, armOverflowShrink never armed, and every attempt re-forwarded the identical 16.9MiB payload. A session past the real 1M input cap could only livelock (timer, no reply). Add the marker to CONTEXT_OVERFLOW_PATTERNS and read the upper bound as the window in parseOverflowWindow; the stated range then arms the emergency shrink and the #1195 in-request refold recovers the turn. Tests: unit cases for the real body (window 1000000) and for a parameter-range 400 that must NOT match, plus e2e #2313 e on the probe path (fails before this change with the 400 passed through, passes after). --- src/util.ts | 8 ++++++ tests/context-overflow.test.ts | 22 ++++++++++++++++ tests/issue2313-probe-forward.test.ts | 38 +++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/src/util.ts b/src/util.ts index af516bc0e8..b6d54068f0 100644 --- a/src/util.ts +++ b/src/util.ts @@ -385,6 +385,10 @@ const CONTEXT_OVERFLOW_PATTERNS: RegExp[] = [ /输入[^。;;!!\n]{0,12}(?:超出|超过|超限)/, // (f) #3240/#3246 consolidation: mixed-language gateways write "prompt 过长". /prompt[^。;;!!\n]{0,6}(?:过长|太长|超长)/i, + // DashScope compatible-mode states the ceiling as an input-length RANGE + // ("Range of input length should be [1, 1000000]"), with no token or length + // subject phrase: unlisted here, the 400 is not an overflow and nothing arms. + /range of input length should be \[/i, ]; function toTokenNumber(s: string): number | undefined { @@ -450,6 +454,10 @@ function parseOverflowWindow(text: string): number | undefined { // first parenthesized number is the rejected payload and must not be used. m = text.match(/maximum number of tokens allowed\s*\((\d[\d,]*)\)/i); if (m) return toTokenNumber(m[1]); + // DashScope: "Range of input length should be [1, 1000000]" — the upper + // bound is the input ceiling; the lower bound is not a window. + m = text.match(/range of input length should be \[\s*\d[\d,]*\s*,\s*(\d[\d,]*)\s*\]/i); + if (m) return toTokenNumber(m[1]); m = text.match(/maximum context length is (\d[\d,]*)/i) ?? text.match(/maximum context length of (\d[\d,]*)/i) ?? diff --git a/tests/context-overflow.test.ts b/tests/context-overflow.test.ts index a6f0559bbb..1d2471520f 100644 --- a/tests/context-overflow.test.ts +++ b/tests/context-overflow.test.ts @@ -133,6 +133,28 @@ test("Gemini 'input token count (N) exceeds' states the input count (#3450)", () assert.equal(info.requestedCompletion, undefined); }); +test("DashScope 'Range of input length should be [1, N]' → overflow + input ceiling", () => { + // The real compatible-mode rejection (no token/length-subject phrase): while + // it went unrecognized, the 400 never armed the emergency shrink, so the + // #2313 probe kept re-forwarding the same over-long payload. + const info = inspectContextOverflow( + 400, + '{"error":{"message":"<400> InternalError.Algo.InvalidParameter: Range of input length should be [1, 1000000]","type":"invalid_request_error","param":null,"code":"invalid_parameter_error"},"id":"chatcmpl-55bbf071-1040-9261-8cd4-76088d25ee5c","request_id":"55bbf071-1040-9261-8cd4-76088d25ee5c"}', + ); + assert.equal(info.isOverflow, true); + assert.equal(info.window, 1000000); + assert.equal(info.requestedMessages, undefined); + assert.equal(info.requestedCompletion, undefined); +}); + +test("a parameter-range 400 without the input-length phrasing is NOT an overflow", () => { + const info = inspectContextOverflow( + 400, + '{"error":{"message":"InvalidParameter: temperature should be in range [0, 2]","code":"invalid_parameter_error"}}', + ); + assert.equal(info.isOverflow, false); +}); + test("overflow body WITHOUT stated counts → no requested* fields (#3450 regression guard)", () => { // The legacy phrasing must keep behaving exactly as before (selfheal T3 // relies on it): window only, no invented counts. diff --git a/tests/issue2313-probe-forward.test.ts b/tests/issue2313-probe-forward.test.ts index 3cdc538dd9..844b8e4c02 100644 --- a/tests/issue2313-probe-forward.test.ts +++ b/tests/issue2313-probe-forward.test.ts @@ -285,3 +285,41 @@ test("e2e #2313 d: probe 400 without a parseable window arms the fallback shrink await close(); } }); + +test("e2e #2313 e: a probe 400 stating only an input-length RANGE arms the shrink — the DashScope livelock recovers", async () => { + // DashScope compatible-mode rejects an over-long input as "Range of input + // length should be [1, 20000]" — no token/length-subject phrase, so while it + // was unrecognized as an overflow the arm never fired, the probe gate + // re-forwarded the same raw body on every attempt and the session could + // only 400 (each turn burning the client's ~300s stream idle). + const rejectBody = JSON.stringify({ + error: { + message: "<400> InternalError.Algo.InvalidParameter: Range of input length should be [1, 20000]", + type: "invalid_request_error", + param: null, + code: "invalid_parameter_error", + }, + id: "chatcmpl-55bbf071-1040-9261-8cd4-76088d25ee5c", + request_id: "55bbf071-1040-9261-8cd4-76088d25ee5c", + }); + const { ctx, calls, close } = await setup(rejectBody); + try { + await seedStuckMeter(ctx, "i2313-e"); + + const r = await post(ctx, bigMessages()); + assert.equal(r.status, 200, "the stated range is an overflow on the probe path, and the in-request refold recovers"); + await r.text(); + + const forwards = calls.filter((c) => !c.summary); + assert.equal(forwards.length, 3, "turn 1 + raw probe forward + one folded retry"); + assert.ok(calls.some((c) => c.summary), "the refold made summarization call(s)"); + assert.ok(forwards[2]!.contentChars < forwards[1]!.contentChars, "the retry carries the folded payload"); + + const s = getSession("i2313-e"); + assert.ok(s); + assert.equal(s.stats.overflowArmTokens, undefined, "the successful retry's usage report retires the one-shot arm (#1110)"); + assert.equal(s.stats.lastInputTokensSource, "usage", "the folded retry settles the final baseline"); + } finally { + await close(); + } +});