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
8 changes: 8 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) ??
Expand Down
22 changes: 22 additions & 0 deletions tests/context-overflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions tests/issue2313-probe-forward.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Loading