From 62289ef1cf32e9b159cc70f6a48f907c4774a6d0 Mon Sep 17 00:00:00 2001 From: Zhishu <963336185@qq.com> Date: Tue, 20 Oct 2026 20:03:27 +0800 Subject: [PATCH 1/4] fix(headroom): remember the proven completion share so the anthropic lane stops re-overflowing (#3900) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An anthropic-wire upstream can still bill max_tokens inside the context wall: DeepSeek's /anthropic endpoint answers a 1,048,576-token request with "requested 1370540 (1114540 in the messages, 256000 in the completion)", while shouldReserveOutputHeadroom() exempts the anthropic protocol, so the preflight judged those messages against the full window. #3450 already learns the stated completion share from that rejection, but it books it on the one-shot overflow arm, and the next real usage report retires the arm and the share together (#1110). The recovered session then judges on the declared window again, over-fills, and re-earns the same 400 — each recovery paying a fresh multi-range emergency fold during which the client sees only a timer. Keep the proof on its own lifetime: a session-level provider fact scoped to the upstream origin and model that stated it. armOverflowShrink() stamps it; the output-headroom step reserves it on wires that do not reserve output budget themselves, so the nudge/preflight denominator becomes window - proven share. A route or model change, or an absent/zero proof, falls back to the untouched protocol rule; the one-shot arm and its #1110 retirement are unchanged. Tests: tests/e2e-proven-completion-wall.test.ts proves the share survives the usage report, that the next turn judges on window - completion, and that a foreign origin cannot claim it; context-overflow.test.ts covers scoping and corrupt walls. --- src/persist.ts | 8 + src/server/handle.ts | 34 ++- src/server/relay.ts | 13 +- src/session.ts | 11 + src/util.ts | 28 +++ tests/context-overflow.test.ts | 31 ++- tests/e2e-proven-completion-wall.test.ts | 254 +++++++++++++++++++++++ 7 files changed, 375 insertions(+), 4 deletions(-) create mode 100644 tests/e2e-proven-completion-wall.test.ts diff --git a/src/persist.ts b/src/persist.ts index 943debceea..ad251ca685 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -153,6 +153,10 @@ interface PersistedSession { overflowArmTokens?: number; overflowArmCompletionReserve?: number; overflowArmMismatchWarned?: boolean; + // #3450 follow-up: provider-lifetime completion proof + its scope. + provenCompletionWall?: number; + provenCompletionWallOrigin?: string; + provenCompletionWallModel?: string; contextTokens?: number; contextTokensSource?: string; // #2129: measurement-state fields buildRecord persists via spread — @@ -2787,6 +2791,10 @@ function buildSession(parsed: PersistedSession): Session { // #3450: proven completion reservation + one-time warn flag — legacy files lack both. overflowArmCompletionReserve: typeof stats.overflowArmCompletionReserve === "number" && Number.isFinite(stats.overflowArmCompletionReserve) && stats.overflowArmCompletionReserve > 0 ? stats.overflowArmCompletionReserve : undefined, overflowArmMismatchWarned: stats.overflowArmMismatchWarned === true ? true : undefined, + // #3450 follow-up: the proof outlives the arm, so a restart must not forget it; absent = no proof. + provenCompletionWall: typeof stats.provenCompletionWall === "number" && Number.isFinite(stats.provenCompletionWall) && stats.provenCompletionWall > 0 ? stats.provenCompletionWall : undefined, + provenCompletionWallOrigin: typeof stats.provenCompletionWallOrigin === "string" && stats.provenCompletionWallOrigin.length > 0 ? stats.provenCompletionWallOrigin : undefined, + provenCompletionWallModel: typeof stats.provenCompletionWallModel === "string" && stats.provenCompletionWallModel.length > 0 ? stats.provenCompletionWallModel : undefined, // In-memory only — a fresh process has no pending compress fold. compressCreditTokens: 0, contextTokens: Math.max(0, stats.contextTokens ?? parsed.contextTokens ?? 0), diff --git a/src/server/handle.ts b/src/server/handle.ts index a1d5f4bcb6..5eedefb902 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -43,7 +43,7 @@ import { resolveClaudeTranscriptLineage } from "../claude-transcript-lineage.js" import { consumePluginRegisterFor, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; import { scheduleAffinityPersist } from "../affinity-persist.js"; import { evaluateChain, extractChainCarriers } from "../chain-checkpoint.js"; -import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, normalizeUpstreamOrigin, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; +import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, normalizeUpstreamOrigin, provenCompletionReserve, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; import { checkTunnelDestination, tunnelAllowlistFromEnv } from "../tunnel-guard.js"; import { resolveCompatDropFields } from "../compat-drop.js"; import { DecompressedTooLargeError, decodeRequestBody } from "../content-encoding.js"; @@ -2083,7 +2083,19 @@ async function handleRequest( // processTurn `config`), so diagNudge shows the reserved window (no extra log). const nativeWindow = reqConfig.modelContextLimit; const headroomOrigin = normalizeUpstreamOrigin(route?.rewrittenUrl ?? upstreamOrigin); - if (shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel)) { + // #3450 follow-up (#3973): a 400 that stated a completion share disproves + // any separate-wall evidence for this origin+model — judging input against + // the full window would re-earn the same 400 every turn (#3738: nudge and + // preflight must judge on a number the upstream will accept). #4205 made + // the reservation the default for every wire without separate-budget + // evidence, so the proven share now forces the reservation where that + // evidence exists and clamps the resulting window below (see provenWindow). + const provenReserve = provenCompletionReserve( + session.stats, + headroomOrigin, + (parsed as { model?: string }).model, + ); + if (shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel) || provenReserve > 0) { const p = parsed as Record; const rawMax = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; let maxOutput = typeof rawMax === "number" ? rawMax : 0; @@ -2157,6 +2169,17 @@ async function handleRequest( } } } + if (provenReserve > 0 && !shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel)) { + // Proven share outranks the client-side fallbacks: it is what + // the upstream already counted into a rejection on this exact + // origin+model, so window − proven share is the input it accepts. + if (provenReserve > maxOutput) maxOutput = provenReserve; + const proofModel = (parsed as { model?: string }).model ?? "?"; + if (!headroomFallbackLogged.has(`${proofModel}|proven-completion`)) { + headroomFallbackLogged.add(`${proofModel}|proven-completion`); + log("info", `[headroom] model=${proofModel}: reserving the PROVEN completion share ${provenReserve} although protocol ${protocol ?? "unknown"} is exempt (#3450) — this origin+model billed completion inside the wall in an earlier rejection; judging input against ${nativeWindow} - ${provenReserve}`); + } + } // #2283 sub-defect 1: emit [window-change] when the per-session resolution // triple (model/source/base) moves mid-session. Placed pre output-headroom so // #2096's per-request max_tokens reservation can't read as a base change. @@ -2178,6 +2201,13 @@ async function handleRequest( log("info", `[${session.id}] fallback context window floored: ${reserved} → ${FALLBACK_EFFECTIVE_WINDOW_FLOOR} (model=${String(requestModel ?? "?")} not authoritatively identified; self-heal corrects it if the real window is smaller)`); reserved = FALLBACK_EFFECTIVE_WINDOW_FLOOR; } + // The #896 cap bounds GUESSED budgets; a proven share is measured — + // the upstream already counted it in a rejection on this origin+model — + // so the window may never stay above (declared window − proven share). + if (provenReserve > 0 && nativeWindow > provenReserve) { + const provenWindow = nativeWindow - provenReserve; + if (provenWindow < reserved) reserved = provenWindow; + } if (reserved !== reqConfig.modelContextLimit) { reqConfig = { ...reqConfig, modelContextLimit: reserved }; // #2096: the [window] line above already went out with the diff --git a/src/server/relay.ts b/src/server/relay.ts index b7ecbdfea3..001ba4a6c9 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -1121,7 +1121,18 @@ export async function forward( // against the same wall — remember it so preflight folds against // window − completion (the foldable quantity is MESSAGES). const reserve = typeof info.requestedCompletion === "number" && info.requestedCompletion > 0 ? info.requestedCompletion : undefined; - if (reserve !== undefined) s.stats.overflowArmCompletionReserve = reserve; + if (reserve !== undefined) { + s.stats.overflowArmCompletionReserve = reserve; + // #3450 follow-up: same evidence on the provider's lifetime — + // the arm retires on the next usage report (#1110), this must + // not (see provenCompletionReserve). Scoped to the origin+model. + const provenOrigin = normalizeUpstreamOrigin(upstreamUrl); + if (provenOrigin !== undefined && reqModel !== undefined) { + s.stats.provenCompletionWall = reserve; + s.stats.provenCompletionWallOrigin = provenOrigin; + s.stats.provenCompletionWallModel = reqModel; + } + } // #3450: one-time mismatch warning — the upstream-stated window // differs from the window bili actually judges on. #4205: compare // against the UNRESERVED declaration, so routes that reserve output diff --git a/src/session.ts b/src/session.ts index 13ee9cf544..1d9ea92294 100644 --- a/src/session.ts +++ b/src/session.ts @@ -346,6 +346,17 @@ export type Session = { /** #3450: one-time mismatch warning already emitted for this session * (upstream-stated window ≠ window in use). Provenance only. */ overflowArmMismatchWarned?: boolean; + /** #3450 follow-up: same completion-share proof as + * overflowArmCompletionReserve, on a PROVIDER lifetime — the arm retires + * on the next usage report (#1110), "this endpoint bills max_tokens + * inside the window" does not stop being true when one turn recovers. + * Scoped to the origin+model that stated it; read via + * provenCompletionReserve(). Absent/zero → no proof. */ + provenCompletionWall?: number; + /** normalizeUpstreamOrigin() of the upstream that stated the proof. */ + provenCompletionWallOrigin?: string; + /** Model on the rejected request that stated the proof. */ + provenCompletionWallModel?: string; /** Tokens compressed THIS turn whose fold has not yet materialized in * an upstream usage report (the post-compress re-request re-sends the * UNFOLDED history for prefix-cache reasons, so its usage report diff --git a/src/util.ts b/src/util.ts index 95f8aba135..cd91009124 100644 --- a/src/util.ts +++ b/src/util.ts @@ -610,6 +610,34 @@ export function shouldReserveOutputHeadroom(protocol: string | undefined, origin return true; } +/** + * The completion share an upstream PROVED it bills inside the context wall for + * one (origin, model) pair, or 0 when this request cannot claim that proof. + * + * #3450 learns the share from a rejected request but books it on the one-shot + * overflow arm, which retires on the next usage report (#1110) — the wrong + * lifetime for "this endpoint counts max_tokens inside the window", a PROVIDER + * fact that survives the fold which recovered the turn. Dropping it re-judges + * every later turn on the declared window, so an anthropic-wire host that bills + * completion inside the wall (DeepSeek's /anthropic: 1048576 stated, + * "requested 1370540 (1114540 in the messages, 256000 in the completion)") + * over-fills, 400s, and pays a new emergency fold each time. + * + * Scoped: a route or model change returns 0 and falls back to the plain + * protocol rule, never silently reserving on an unrelated upstream. + */ +export function provenCompletionReserve( + stats: { provenCompletionWall?: number; provenCompletionWallOrigin?: string; provenCompletionWallModel?: string } | undefined, + origin: string | undefined, + model: string | undefined, +): number { + const wall = stats?.provenCompletionWall; + if (typeof wall !== "number" || !Number.isFinite(wall) || wall <= 0) return 0; + if (origin === undefined || origin === "" || stats?.provenCompletionWallOrigin !== origin) return 0; + if (model === undefined || model === "" || stats?.provenCompletionWallModel !== model) return 0; + return wall; +} + /** System-carrier texts of a parsed request body, covering all four wire shapes: * anthropic/openai-hoisted top-level `system` (string or text-block array), * responses `instructions`, google `systemInstruction.parts`, and system/developer diff --git a/tests/context-overflow.test.ts b/tests/context-overflow.test.ts index 727a072013..a6f0559bbb 100644 --- a/tests/context-overflow.test.ts +++ b/tests/context-overflow.test.ts @@ -1,6 +1,6 @@ import test from "node:test"; import assert from "node:assert/strict"; -import { inspectContextOverflow, reserveOutputHeadroom, shouldReserveOutputHeadroom } from "../src/util.ts"; +import { inspectContextOverflow, provenCompletionReserve, reserveOutputHeadroom, shouldReserveOutputHeadroom } from "../src/util.ts"; // A context-overflow error is the only reliable signal that the configured // window is wrong (e.g. the 200k fallback for an unknown model on a relay). @@ -346,3 +346,32 @@ test("shouldReserveOutputHeadroom: reserve-by-default, real-Claude routing exemp assert.equal(shouldReserveOutputHeadroom("anthropic", "http://127.0.0.1:8787", "DeepSeek-V3.2-Exp"), true, "#3900 shape: deepseek model on the anthropic wire → reserve"); assert.equal(shouldReserveOutputHeadroom("anthropic", "https://api.anthropic.com.evil.example"), true, "host match is exact — no suffix tricks"); }); + +// #3450 follow-up: a stated completion share is a PROVIDER fact, so it outlives +// the one-shot arm — but only for the origin+model that stated it. +const PROVEN = { provenCompletionWall: 7_000, provenCompletionWallOrigin: "http://127.0.0.1:9", provenCompletionWallModel: "deepseek-flash" }; + +test("provenCompletionReserve: matching origin+model returns the proven share", () => { + assert.equal(provenCompletionReserve(PROVEN, "http://127.0.0.1:9", "deepseek-flash"), 7_000); +}); + +test("provenCompletionReserve: no proof / incomplete proof → 0 (protocol rule unchanged)", () => { + assert.equal(provenCompletionReserve(undefined, "http://127.0.0.1:9", "deepseek-flash"), 0); + assert.equal(provenCompletionReserve({}, "http://127.0.0.1:9", "deepseek-flash"), 0); + assert.equal(provenCompletionReserve({ provenCompletionWallOrigin: PROVEN.provenCompletionWallOrigin, provenCompletionWallModel: PROVEN.provenCompletionWallModel }, "http://127.0.0.1:9", "deepseek-flash"), 0); +}); + +test("provenCompletionReserve: a different origin or model cannot claim the proof", () => { + assert.equal(provenCompletionReserve(PROVEN, "http://127.0.0.1:10", "deepseek-flash"), 0, "different upstream"); + assert.equal(provenCompletionReserve(PROVEN, "http://127.0.0.1:9", "deepseek-chat"), 0, "different model"); + assert.equal(provenCompletionReserve(PROVEN, undefined, "deepseek-flash"), 0, "unknown origin"); + assert.equal(provenCompletionReserve(PROVEN, "http://127.0.0.1:9", undefined), 0, "unknown model"); + assert.equal(provenCompletionReserve(PROVEN, "", "deepseek-flash"), 0, "empty origin"); + assert.equal(provenCompletionReserve(PROVEN, "http://127.0.0.1:9", ""), 0, "empty model"); +}); + +test("provenCompletionReserve: corrupt walls are rejected outright", () => { + for (const wall of [0, -1, Number.NaN, Number.POSITIVE_INFINITY, "7000"]) { + assert.equal(provenCompletionReserve({ ...PROVEN, provenCompletionWall: wall as number }, "http://127.0.0.1:9", "deepseek-flash"), 0, `wall=${String(wall)}`); + } +}); diff --git a/tests/e2e-proven-completion-wall.test.ts b/tests/e2e-proven-completion-wall.test.ts new file mode 100644 index 0000000000..f16cc33b37 --- /dev/null +++ b/tests/e2e-proven-completion-wall.test.ts @@ -0,0 +1,254 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; + +// #3450 follow-up: "the upstream counts the completion share inside the +// context wall" is a PROVIDER fact, not turn evidence. #3450 learns the share +// but books it on the one-shot overflow arm, which retires on the next usage +// report (#1110) — deliberately, for "this session cannot exceed N". The +// provider fact, however, stays true after the turn recovered: dropping it +// re-judges every later turn on the declared window, which on an anthropic-WIRE +// endpoint that bills completion inside the wall (DeepSeek /anthropic: 1048576 +// stated, "requested 1370540 (1114540 in the messages, 256000 in the +// completion)") means the session over-fills again, 400s again, and pays +// another multi-range emergency fold each time — the live 5m28s incident. +// +// Contract pinned here: +// 1. recovery still retires the ARM and its reserve (#1110 unchanged); +// 2. the proven share survives on its own lifetime, scoped to origin+model; +// 3. the very next turn judges input against (declared window − proven share) +// even though the wire protocol is anthropic (shouldReserveOutputHeadroom +// says false) — so the 400 cannot recur; +// 4. a DIFFERENT origin can never claim the proof (its own declaration rules). + +const SMALL_WINDOW_OVERFLOW_BODY = JSON.stringify({ + error: { + code: "context_length_exceeded", + message: "This model's maximum context length is 30000 tokens. However, you requested 35000 tokens (28000 in the messages, 7000 in the completion). Please reduce the length of the inputs or output.", + }, +}); + +// 12000 of a 40000 wall is 30% — above the 25% #896 cap on GUESSED output +// budgets. A measured share must not be clipped by that cap. +const COMPLETION_HEAVY_OVERFLOW_BODY = JSON.stringify({ + error: { + code: "context_length_exceeded", + message: "This model's maximum context length is 40000 tokens. However, you requested 42000 tokens (30000 in the messages, 12000 in the completion). Please reduce the length of the inputs or output.", + }, +}); + +function okSse(): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: 5000 } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +const SUMMARY_TEXT = + "PREFLIGHT SUMMARY: the segment covered a multi-step debugging session. Key decisions: folded the oldest ranges because the payload had to fit the proven message budget. Files touched: src/a.ts:10. Outcome: fixed and verified by tests."; + +function summaryJson(): string { + return JSON.stringify({ + id: "msg_summary", + type: "message", + role: "assistant", + model: "claude-test", + content: [{ type: "text", text: SUMMARY_TEXT }], + stop_reason: "end_turn", + usage: { input_tokens: 500, output_tokens: 50 }, + }); +} + +function bigConversation(): Array<{ role: string; content: string }> { + const msgs: Array<{ role: string; content: string }> = []; + for (let i = 0; i < 12; i++) { + const role = i % 2 === 0 ? "user" : "assistant"; + msgs.push({ role, content: `Message ${i} of the long conversation. ` + `MARKER_${i}_content_`.repeat(250) }); + } + return msgs; +} + +type Upstream = { server: http.Server; port: number; streamingCalls: number; summaryCalls: number }; + +async function makeUpstream(overflowFirst: boolean, overflowBody: string = SMALL_WINDOW_OVERFLOW_BODY): Promise { + const state = { server: undefined as unknown as http.Server, port: 0, streamingCalls: 0, summaryCalls: 0 }; + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + let parsed: { stream?: boolean } = {}; + try { + parsed = JSON.parse(raw); + } catch { + parsed = {}; + } + if (parsed.stream === false) { + state.summaryCalls += 1; + res.writeHead(200, { "content-type": "application/json" }); + res.end(summaryJson()); + return; + } + if (overflowFirst && state.streamingCalls === 0) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(overflowBody); + } else { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse()); + } + state.streamingCalls += 1; + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + state.server = server; + state.port = (server.address() as { port: number }).port; + return state as Upstream; +} + +function baseOptions(ports: number[], window: number): ProxyOptions { + const routes: NonNullable = {}; + for (const port of ports) { + routes[`http://127.0.0.1:${port}`] = { models: { "claude-test": { context: window } } }; + } + return { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes, + modelContextLimit: window, + kernelConfig: defaultConfig(window), + compress: { injectTool: true, injectNudge: true }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions; +} + +test("e2e #3450 follow-up: a proven completion wall outlives the arm — the next turn judges on window − completion and a foreign origin cannot claim it", async () => { + const proving = await makeUpstream(true); + const foreign = await makeUpstream(false); + const proxy = await startServer(baseOptions([proving.port, foreign.port], 40_000)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + const url = (port: number) => `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${port}/v1/messages`; + // The wire is anthropic (shouldReserveOutputHeadroom === false) — the whole + // point of the proof is that this exemption must give way to measured truth. + const body = JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, messages: bigConversation() }); + const headers = { "content-type": "application/json", "x-acp-session": "t3450-proof-sess" }; + + try { + // 1. First request: upstream rejects with the stated counts; the + // in-request refold recovers it (T2's contract, unchanged). + const r1 = await fetch(url(proving.port), { method: "POST", headers, body }); + assert.equal(r1.status, 200, "the stated completion share still rescues the turn in-request"); + await r1.text(); + assert.ok(proving.summaryCalls >= 1, "preflight folded against window − completion"); + const foldsAfterFirst = proving.summaryCalls; + const s1 = listSessions().find((x) => x.id === "t3450-proof-sess"); + assert.ok(s1, "session exists"); + // 2. The arm retired with its reserve (#1110 lifecycle untouched) ... + assert.equal(s1!.stats.overflowArmTokens, undefined, "the one-shot arm retires on the usage report"); + assert.equal(s1!.stats.overflowArmCompletionReserve, undefined, "the arm's own reservation retires with it (#3450)"); + // ... but the PROVIDER fact it proved survives, scoped to this route. + assert.equal(s1!.stats.provenCompletionWall, 7_000, "the completion share is remembered past the arm"); + assert.equal(s1!.stats.provenCompletionWallOrigin, `http://127.0.0.1:${proving.port}`); + assert.equal(s1!.stats.provenCompletionWallModel, "claude-test"); + + // 3. Second request on an anthropic wire: the exemption must yield to + // the proof, or this turn re-earns the 400 (the live incident loop). + const r2 = await fetch(url(proving.port), { method: "POST", headers, body }); + assert.equal(r2.status, 200, "the follow-up turn must not walk into the same wall again"); + await r2.text(); + const s2 = listSessions().find((x) => x.id === "t3450-proof-sess"); + assert.ok(s2, "same session"); + assert.equal(s2!.metadata.effectiveContextLimit, 33_000, "judged on 40000 − 7000 proven completion, not the raw declared window"); + assert.equal(proving.summaryCalls, foldsAfterFirst, "the second turn folds nothing: the proven denominator already fits"); + + // 4. A different upstream has its own declaration — the proof never + // travels with the session id to a route that never stated it. + const foreignHeaders = { "content-type": "application/json", "x-acp-session": "t3450-proof-foreign" }; + const r3 = await fetch(url(foreign.port), { method: "POST", headers: foreignHeaders, body }); + assert.equal(r3.status, 200); + await r3.text(); + const s3 = listSessions().find((x) => x.id === "t3450-proof-foreign"); + assert.ok(s3, "foreign session exists"); + assert.equal(s3!.stats.provenCompletionWall, undefined, "a new session starts with no proof"); + assert.equal(s3!.metadata.effectiveContextLimit, 40_000, "anthropic wire on an unproven origin keeps the plain protocol rule"); + } finally { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + for (const up of [proving, foreign]) { + up.server.close(); + up.server.closeIdleConnections?.(); + await once(up.server, "close"); + } + } +}); + +test("e2e #3450 follow-up: a proven share above the #896 cap is reserved in full", async () => { + const proving = await makeUpstream(true, COMPLETION_HEAVY_OVERFLOW_BODY); + const proxy = await startServer(baseOptions([proving.port], 40_000)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${proving.port}/v1/messages`; + const body = JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, messages: bigConversation() }); + const headers = { "content-type": "application/json", "x-acp-session": "t3450-proof-cap" }; + + try { + const r1 = await fetch(url, { method: "POST", headers, body }); + assert.equal(r1.status, 200, "the stated completion share still rescues the turn in-request"); + await r1.text(); + const s1 = listSessions().find((x) => x.id === "t3450-proof-cap"); + assert.equal(s1!.stats.provenCompletionWall, 12_000, "the stated completion share is booked in full"); + + const r2 = await fetch(url, { method: "POST", headers, body }); + assert.equal(r2.status, 200, "the follow-up turn must not walk into the same wall again"); + await r2.text(); + const s2 = listSessions().find((x) => x.id === "t3450-proof-cap"); + // 12000 is 30% of the 40000 window: the 25% cap would leave 30000, + // which the upstream's own arithmetic says still overflows. + assert.equal(s2!.metadata.effectiveContextLimit, 28_000, "measured proof outranks the guess-only #896 cap"); + } finally { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + proving.server.close(); + proving.server.closeIdleConnections?.(); + await once(proving.server, "close"); + } +}); From e6bf8c0937466403d85b973b1c6e153c3db7e93c Mon Sep 17 00:00:00 2001 From: Zhishu <963336185@qq.com> Date: Wed, 21 Oct 2026 14:48:40 +0800 Subject: [PATCH 2/4] test: clamp the overflow arm against the proven window (#3900) After an overflow proves a completion share against the stated wall, handle.ts rewrites effectiveContextLimit to window - share, so the #3738 arm clamp now lands at min(stated messages, proven window). Pin the new number and keep the stated ceiling assertion so the rule, not the width, is what the case checks. --- tests/e2e-issue3900-overflow-teaches.test.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/e2e-issue3900-overflow-teaches.test.ts b/tests/e2e-issue3900-overflow-teaches.test.ts index c48070e4cd..0987796460 100644 --- a/tests/e2e-issue3900-overflow-teaches.test.ts +++ b/tests/e2e-issue3900-overflow-teaches.test.ts @@ -113,9 +113,16 @@ test("e2e #3900: two overflow 400s with stated counts publish k̂ — the estima // pinned directly in the unit test; here the refold hook's re-prepare // legitimately notes a fresh pair afterwards, so lastLocalTextEstimate // may hold a new value by the time we look.) - // The arm keeps its #1110/#3738/#3450 semantics untouched. + // The arm keeps its #1110/#3738/#3450 semantics untouched: the + // baseline is still min(stated message tokens, the judging limit). + // Only the judging limit moves: this 400 proved the upstream bills a + // 5_000-token completion share against the 30_000 wall (#3900), so + // handle.ts rewrites effectiveContextLimit to 25_000 and the clamp + // lands there - the window the session can actually use. The clamp + // RULE is what this assertion pins; the stated ceiling is unchanged + // by it (asserted right below). assert.equal(st.lastInputTokensSource, "overflow-arm"); - assert.equal(st.lastInputTokens, 30_000, "armed at min(stated messages 40k, judging limit 30k) — #3738 clamp"); + assert.equal(st.lastInputTokens, 25_000, "armed at min(stated messages 40k, proven judging limit 25k = 30k - 5k completion share) - #3738 clamp"); assert.equal(st.overflowArmTokens, 30_000, "guard keeps the stated ceiling"); assert.equal(st.overflowArmCompletionReserve, 5_000, "the completion share stays reserved (#3450)"); } finally { From db7c3894835367ea994a0c4e19eb4c0aeca5da4c Mon Sep 17 00:00:00 2001 From: Zhishu <963336185@qq.com> Date: Wed, 21 Oct 2026 19:23:50 +0800 Subject: [PATCH 3/4] chore(ci): draw a fresh runner for the #3665 drain flake (no content change) From da1c3ac10dbab97e61d598db832013cf76085b5a Mon Sep 17 00:00:00 2001 From: Zhishu <963336185@qq.com> Date: Sat, 24 Oct 2026 00:59:03 +0800 Subject: [PATCH 4/4] test: assert the proven wall through reservedContextLimit (#4205 split) #4205 split the display window (effectiveContextLimit, the declared wall) from the judged one (reservedContextLimit). The proven-share clamp acts on the judged window, so the e2e assertions must read reservedContextLimit; effectiveContextLimit stays the declared 40000 and is asserted as such. --- tests/e2e-proven-completion-wall.test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/e2e-proven-completion-wall.test.ts b/tests/e2e-proven-completion-wall.test.ts index f16cc33b37..09a0c3a819 100644 --- a/tests/e2e-proven-completion-wall.test.ts +++ b/tests/e2e-proven-completion-wall.test.ts @@ -191,7 +191,11 @@ test("e2e #3450 follow-up: a proven completion wall outlives the arm — the nex await r2.text(); const s2 = listSessions().find((x) => x.id === "t3450-proof-sess"); assert.ok(s2, "same session"); - assert.equal(s2!.metadata.effectiveContextLimit, 33_000, "judged on 40000 − 7000 proven completion, not the raw declared window"); + // #4205 split the display window (effectiveContextLimit, the declared + // wall) from the judged one (reservedContextLimit); the proof acts on + // the latter — the number nudge/preflight actually judge input against. + assert.equal(s2!.metadata.effectiveContextLimit, 40_000, "the declared wall stays visible as declared"); + assert.equal(s2!.metadata.reservedContextLimit, 33_000, "judged on 40000 − 7000 proven completion, not the raw declared window"); assert.equal(proving.summaryCalls, foldsAfterFirst, "the second turn folds nothing: the proven denominator already fits"); // 4. A different upstream has its own declaration — the proof never @@ -203,7 +207,7 @@ test("e2e #3450 follow-up: a proven completion wall outlives the arm — the nex const s3 = listSessions().find((x) => x.id === "t3450-proof-foreign"); assert.ok(s3, "foreign session exists"); assert.equal(s3!.stats.provenCompletionWall, undefined, "a new session starts with no proof"); - assert.equal(s3!.metadata.effectiveContextLimit, 40_000, "anthropic wire on an unproven origin keeps the plain protocol rule"); + assert.equal(s3!.metadata.reservedContextLimit, 40_000, "anthropic wire on an unproven origin keeps the plain protocol rule"); } finally { proxy.close(); proxy.closeIdleConnections?.(); @@ -242,7 +246,7 @@ test("e2e #3450 follow-up: a proven share above the #896 cap is reserved in full const s2 = listSessions().find((x) => x.id === "t3450-proof-cap"); // 12000 is 30% of the 40000 window: the 25% cap would leave 30000, // which the upstream's own arithmetic says still overflows. - assert.equal(s2!.metadata.effectiveContextLimit, 28_000, "measured proof outranks the guess-only #896 cap"); + assert.equal(s2!.metadata.reservedContextLimit, 28_000, "measured proof outranks the guess-only #896 cap"); } finally { proxy.close(); proxy.closeIdleConnections?.();