From 290384db5a2a4cfa6686af4f3eeef2fc268797b2 Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 22:20:09 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20preflight=20=E2=80=94=20refuse=20doo?= =?UTF-8?q?med=20bodies=20before=20folding=20when=20tools[]/system/other?= =?UTF-8?q?=20alone=20exceed=20the=20window=20(#7167)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The preflight trigger's text channel includes tools[] (estimateWireOverhead counts them), so a body whose tools[] alone clear the window triggers the fold — irreversibly rewriting session history and billing real summary tokens — only for the post-fold measured dooms (#1839/#3146) to answer with the 413 anyway. Measure the UNMODIFIED prepared body with the #554 guard meter right after the trigger guard: if tools+system+other (dimensions folding cannot touch) >= the doom bar (native window when bili knows it, headroom limit as fallback — the SAME bar the post-fold dooms use), fail fast with the 413 before probeForEvidence / preflightCompress. The gate can only refuse a body the post-fold flow would have 413'd with the same meter — no new failure mode. Growth-armed payloads keep the autoFold fail-open rule; unparseable bodies fall through as before. Tests: tests/issue7167-preflight-irreducible-gate.test.ts (e2e through the proxy: doomed body -> 413 with zero summary calls and history intact on the next request; foldable control body still folds). --- src/server.ts | 31 +++ ...sue7167-preflight-irreducible-gate.test.ts | 261 ++++++++++++++++++ 2 files changed, 292 insertions(+) create mode 100644 tests/issue7167-preflight-irreducible-gate.test.ts diff --git a/src/server.ts b/src/server.ts index 3fb954b5ff..44c4bf2191 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4030,6 +4030,37 @@ export async function preflightCompressIfNeeded( } } if (limit <= 0 || !model || !triggerFires) return prepared; + // #7167: the irreducible-dimension gate — tools[], system and other are + // dimensions folding CANNOT touch: the moment they alone clear the doom + // bar, no amount of message compaction can fit the body, so the fold + // below is guaranteed-doomed from its first summary call — it would + // irreversibly rewrite the session's history and bill real tokens for a + // 413 the post-fold measured dooms (#1839/#3146 arms) would answer + // anyway. Measure the UNMODIFIED prepared body with the #554 guard meter + // and fail fast before probeForEvidence / preflightCompress. Same bar as + // those post-fold dooms (the native window when bili knows it, the + // headroom limit as fallback): the gate can only refuse a body the + // post-fold flow would have 413'd with the same measured meter — no new + // failure mode. Growth-armed payloads keep the autoFold fail-open rule + // (a growth trigger is operator intent and the payload fits the real + // window by definition — the #3444 floor mechanism owns that regime's + // unreachable-target case). Unparseable bodies fall through + // (measuredOutbound undefined) — current behavior holds. + if (!growthArmed) { + const preFoldOutbound = measuredOutbound(prepared); + if (preFoldOutbound !== undefined) { + const pf = preFoldOutbound.breakdown; + const irreducible = pf.tools + pf.system + pf.other; + const doomBar = typeof resolvedNativeWindow === "number" && resolvedNativeWindow > 0 ? resolvedNativeWindow : limit; + if (irreducible >= doomBar) { + const nativeKnown = typeof resolvedNativeWindow === "number" && resolvedNativeWindow > 0; + const irreducibleMessage = `tools[] + system + other alone measure ~${irreducible.toLocaleString("en-US")} tokens (${measuredBreakdownClause(pf)}; CJK-aware count, the #554 guard meter; images billed separately and never doom here — #496/#1800/#857) vs ${nativeKnown ? `the model's window ${doomBar.toLocaleString("en-US")}` : `bili's effective window ${doomBar.toLocaleString("en-US")}`} (model=${model}) — dimensions compression cannot touch, so no amount of message folding can fit this body. Compaction was NOT attempted and the body was NOT forwarded. Trim the tool surface (fewer or smaller tools, lazy tool discovery), shorten the system prompt${nativeKnown ? "" : ", or raise the effective window (compress.modelContextLimit)"} and retry.`; + log("error", `[${session.id}] preflight fail-fast 413 (retryable=false, irreducible-dimension gate): ${irreducibleMessage}`); + noteLocalRefusal(session, currentOrigin, model, limit); + return { failFast: true, status: 413, message: irreducibleMessage, retryable: false, respond: !res.writableEnded }; + } + } + } // #3846: an earlier local 413 for THIS origin+model at an equal or wider // window is independent evidence the payload cannot fit here — the // forward-once escapes below must not re-bet the estimator guess. Fitting diff --git a/tests/issue7167-preflight-irreducible-gate.test.ts b/tests/issue7167-preflight-irreducible-gate.test.ts new file mode 100644 index 0000000000..033e099466 --- /dev/null +++ b/tests/issue7167-preflight-irreducible-gate.test.ts @@ -0,0 +1,261 @@ +// #7167: preflight folded message ranges (irreversible history rewrite + real +// summary billing) BEFORE discovering that tools[] alone already exceeds the +// window — a fold that was guaranteed-doomed before it started. The +// irreducible-dimension gate measures the UNMODIFIED prepared body with the +// #554 guard meter right after the trigger guard: when tools[] + system + +// other alone clear the doom bar (the native window when known, the headroom +// limit as fallback — the same bar the post-fold measured dooms use), no +// amount of message compaction can fit the body, so preflight answers the +// actionable 413 (trim the tool surface / shorten the system prompt) with +// ZERO upstream calls and the session history untouched. Foldable bodies +// (irreducible part under the bar) keep the existing fold-first behavior. +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +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 { rmrf } from "./tmp-rm.ts"; + +const WINDOW = 20_000; + +type Call = { bytes: number; body: string }; + +function chatSse(text: string): string { + const chunk = (delta: Record, finish: string | null, extra?: Record): string => + `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta, finish_reason: finish }], ...extra })}\n\n`; + return chunk({ role: "assistant" }, null) + chunk({ content: text }, null) + chunk({}, "stop", { usage: { prompt_tokens: 100, completion_tokens: 3 } }) + "data: [DONE]\n\n"; +} + +/** ~800 chars ≈ 200 tokens of message content, with a stable per-message + * marker so a later request can prove the ORIGINAL history reached the + * upstream unfolded (no fold block was created by an earlier request). */ +function markedMessages(count: number): Array<{ role: string; content: string }> { + const filler = "0123456789abcdef".repeat(50); + const messages: Array<{ role: string; content: string }> = []; + for (let i = 0; i < count; i++) { + messages.push({ role: i % 2 === 0 ? "user" : "assistant", content: `MARKER-${i} ${filler}` }); + } + return messages; +} + +/** `count` tools whose descriptions carry ~`descChars` chars each — the + * tools[] dimension folding cannot touch. 250 × 400 chars ≈ 31K measured + * tokens (the #7167 shape: tools[] alone clears the 20K window); 20 × 1600 + * ≈ 8.5K leaves the messages dimension foldable (the control shape). */ +function sizedTools(count: number, descChars: number): object[] { + const pad = "0123456789abcdef".repeat(Math.ceil(descChars / 16)); + const tools: object[] = []; + for (let i = 0; i < count; i++) { + tools.push({ + type: "function", + function: { + name: `tool_${i}`, + description: `tool number ${i} ${pad.slice(0, descChars)}`, + parameters: { type: "object", properties: { arg: { type: "string" } } }, + }, + }); + } + return tools; +} + +function makeUpstream(): { server: http.Server; calls: Call[] } { + const calls: Call[] = []; + 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"); + calls.push({ bytes: raw.length, body: raw }); + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(chatSse("forwarded answer")); + }); + }); + return { server, calls }; +} + +/** External-summary upstream: records every call (the "billed summary + * tokens" the issue complains about) and answers a valid openai summary. */ +function makeSummaryUpstream(): { server: http.Server; bodies: unknown[] } { + const bodies: unknown[] = []; + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + try { bodies.push(JSON.parse(Buffer.concat(chunks).toString("utf8"))); } catch { bodies.push(null); } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ choices: [{ message: { content: "SUMMARY: preflight folded the early conversation into this summary." }, finish_reason: "stop" }] })); + }); + }); + return { server, bodies }; +} + +function proxyOptions(routes: Record, namedProviders: Record): ProxyOptions { + return { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: routes as ProxyOptions["routes"], + namedProviders: namedProviders as ProxyOptions["namedProviders"], + modelContextLimit: WINDOW, + kernelConfig: defaultConfig(WINDOW, { + preserveRecentMessages: 2, + preserveRecentTokens: 2000, + compress: { minCompressRange: 1000, maxSummaryLength: 20000, minSummaryLength: 50 }, + }), + compress: { + injectTool: true, + injectNudge: true, + // The control test needs a working fold: the external chain does + // the summarization (zero model involvement), pointed at the + // recording summary upstream. + externalSummary: { enabled: true, targets: ["sum/sm"] }, + }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + promptCache: { routing: "auto" }, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions; +} + +type Ctx = { url: string; headers: Record }; + +async function setup(sessionId: string): Promise<{ ctx: Ctx; calls: Call[]; summaryBodies: unknown[]; close: () => void }> { + // Isolate ALL on-disk state (unified storage, prefix-affinity, session-order): + // a second server instance hydrating the first's session would inherit its + // calibration factor and local-refusal records — the trigger math is + // session-state-sensitive and the tests must not see each other. + const root = mkdtempSync(path.join(tmpdir(), "bili-7167-")); + const prevXdg = { state: process.env.XDG_STATE_HOME, data: process.env.XDG_DATA_HOME, cache: process.env.XDG_CACHE_HOME }; + process.env.XDG_STATE_HOME = path.join(root, "state"); + process.env.XDG_DATA_HOME = path.join(root, "data"); + process.env.XDG_CACHE_HOME = path.join(root, "cache"); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const { server: upstream, calls } = makeUpstream(); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const summary = makeSummaryUpstream(); + summary.server.listen(0, "127.0.0.1"); + await once(summary.server, "listening"); + const summaryPort = (summary.server.address() as { port: number }).port; + process.env.E2E_7167_SUM_KEY = "k"; + const proxy = await startServer(proxyOptions( + { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: WINDOW } } } }, + { sum: { baseUrl: `http://127.0.0.1:${summaryPort}`, api: "openai", apiKeyEnv: "E2E_7167_SUM_KEY", models: { sm: {} } } }, + )); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + return { + ctx: { + url: `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/chat/completions`, + headers: { "content-type": "application/json", "x-acp-session": sessionId }, + }, + calls, + summaryBodies: summary.bodies, + close: () => { + proxy.close(); + proxy.closeAllConnections?.(); + upstream.close(); + upstream.closeAllConnections?.(); + summary.server.close(); + summary.server.closeAllConnections?.(); + for (const [key, value] of Object.entries(prevXdg)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + rmrf(root); + }, + }; +} + +const post = (ctx: Ctx, messages: Array<{ role: string; content: string }>, tools: object[]) => + fetch(ctx.url, { + method: "POST", + headers: { ...ctx.headers }, + body: JSON.stringify({ model: "gpt-test", stream: true, messages, tools }), + }); + +test("e2e #7167: tools[] alone over the window → fail-fast 413 BEFORE any fold — zero upstream calls, zero summary billing, history untouched", async () => { + const { ctx, calls, summaryBodies, close } = await setup("s-7167-doomed"); + try { + // The #7167 shape: tools[] ~31K measured tokens alone clear the 20K + // window; messages ~10K would have been foldable. Before the gate this + // exact body ran preflightCompress (irreversible fold + a billed + // summary call) and only THEN 413'd on the post-fold measured doom. + const r = await post(ctx, markedMessages(50), sizedTools(250, 400)); + const body = await r.text(); + const parsed = JSON.parse(body) as { error?: { type?: string; code?: string; retryable?: boolean; message?: string } }; + + assert.equal(r.status, 413, `doomed body must fail fast (got ${r.status}): ${body.slice(0, 300)}`); + assert.equal(parsed.error?.code, "preflight_compress_failed", "the preflight fail-fast house shape"); + assert.equal(parsed.error?.retryable, false, "retrying re-sends the same tool surface (#2847 semantics)"); + assert.match(parsed.error?.message ?? "", /tools\[\] \+ system \+ other alone measure/, "the irreducible dimensions name themselves"); + assert.match(parsed.error?.message ?? "", /Compaction was NOT attempted/, "the operator is told no doomed fold was run"); + assert.match(parsed.error?.message ?? "", /NOT forwarded/, "the body did not reach the upstream"); + assert.match(parsed.error?.message ?? "", /Trim the tool surface/, "the actionable remedy is present"); + // THE behavior under test: nothing was folded and nothing was billed — + // zero calls to the main upstream AND zero summary-chain calls. + assert.equal(calls.length, 0, `main upstream must see zero calls (got ${JSON.stringify(calls)})`); + assert.equal(summaryBodies.length, 0, `the summary chain must not be billed for a doomed fold (got ${summaryBodies.length} calls)`); + + // History intact: the SAME session resends the same 50 messages with a + // small tool surface (textChannel ~11K < 20K → no trigger). If the + // first request had folded, the forwarded body would carry the fold + // summary instead of the original messages. + const r2 = await post(ctx, markedMessages(50), sizedTools(2, 200)); + const body2 = await r2.text(); + assert.equal(r2.status, 200, `the same history with a fitting tool surface must forward (got ${r2.status}): ${body2.slice(0, 300)}`); + assert.equal(calls.length, 1, "exactly one forward reached the upstream"); + assert.equal(summaryBodies.length, 0, "no summary call on the fitting resend either"); + // Assert on the RECORDED REQUEST body (the response is the SSE stream): + // the original messages must have reached the upstream unfolded. + const sent = calls[0].body; + assert.ok(sent.includes("MARKER-0"), "the ORIGINAL first message reached the upstream unfolded"); + assert.ok(sent.includes("MARKER-49"), "the ORIGINAL last message reached the upstream unfolded"); + assert.ok(!sent.includes("SUMMARY: preflight folded"), "no fold block exists in the session history"); + } finally { + await close(); + } +}); + +test("e2e #7167 control: foldable bodies (irreducible part under the bar) keep the fold-first behavior — the gate must not false-block", async () => { + const { ctx, calls, summaryBodies, close } = await setup("s-7167-foldable"); + try { + // tools ~8.5K (< 20K bar) + messages ~16K: the trigger fires + // (textChannel ~26K ≥ 20K) but the irreducible part fits — folding the + // messages CAN make the body fit, so preflight must fold and forward, + // exactly as before the gate. + const r = await post(ctx, markedMessages(100), sizedTools(20, 1600)); + const body = await r.text(); + assert.equal(r.status, 200, `foldable body must fold and forward (got ${r.status}): ${body.slice(0, 300)}`); + assert.ok(summaryBodies.length >= 1, `the summary chain did the folding (got ${summaryBodies.length} calls)`); + assert.ok(calls.length >= 1, "the folded body reached the upstream"); + // The outbound must carry the fold summary — folding really happened, + // the gate did not false-block a foldable body. + assert.ok(calls[calls.length - 1].body.includes("SUMMARY: preflight folded"), "the forwarded body carries the fold block"); + assert.ok(!body.includes("Compaction was NOT attempted"), "the gate did not fire on a foldable body"); + } finally { + await close(); + } +}); From bd3d0a272695abadd451540f0afa27b0aab7198a Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 23:08:57 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20preflight=20meter=20=E2=80=94=20buck?= =?UTF-8?q?et=20the=20foldable=20message=20array=20by=20wire=20key=20(#716?= =?UTF-8?q?7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #7175 irreducible gate reads tools+system+other from estimateRawBodyBreakdown, which bucketed the outbound body by top-level key and only recognized 'messages'/'system'. On the Responses wire the foldable message array lives under 'input' (Google: 'contents'), so it landed in the 'other' bucket and the gate 413'd an entirely-foldable body before any fold ran — the #3686 / #488-B Responses e2e regressions on the PR head. Make the bucketing wire-aware: 'input'/'contents' -> messages, 'instructions'/'systemInstruction' -> system. 'total' is the sum of the buckets either way, so the doom verdicts that price 'total' (post-fold #1839/#3146 arms, the egress fuse) are untouched; only the gate's bucket split and the measuredBreakdownClause display shift. --- src/preflight.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/preflight.ts b/src/preflight.ts index 5f98e14d3f..60f9dff430 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -500,12 +500,24 @@ export interface RawBodyBreakdown { other: number; } +// #7175: the foldable message array and the system prompt sit under WIRE-SPECIFIC +// top-level keys — `messages` (OpenAI chat / Anthropic), `input` (Responses), +// `contents` (Google) for messages; `system` (Anthropic), `instructions` +// (Responses), `systemInstruction` (Google) for the system prompt. Bucketing on +// `messages`/`system` alone dropped the Responses/Google message array into +// "other", which the #7167 irreducible gate then read as "dimensions folding +// cannot touch" and 413'd an entirely-foldable body (the #3686/#488-B +// Responses regressions). `total` is the sum of the buckets either way, so the +// doom verdicts that price `total` are untouched by this re-split. +const MESSAGE_ARRAY_KEYS = new Set(["messages", "input", "contents"]); +const SYSTEM_PROMPT_KEYS = new Set(["system", "instructions", "systemInstruction"]); + export function estimateRawBodyBreakdown(parsed: unknown): RawBodyBreakdown { const buckets: [number, number, number, number] = [0, 0, 0, 0]; // messages, tools, system, other const pending: [unknown, string | undefined, number][] = []; if (parsed && typeof parsed === "object") { for (const [k, v] of Object.entries(parsed as Record)) { - const bucket = k === "messages" ? 0 : k === "tools" ? 1 : k === "system" ? 2 : 3; + const bucket = MESSAGE_ARRAY_KEYS.has(k) ? 0 : k === "tools" ? 1 : SYSTEM_PROMPT_KEYS.has(k) ? 2 : 3; pending.push([v, k, bucket]); } } else { From 8e64e120e91fa36076dd3e8436653e9ed9ae319d Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 23:33:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?test:=20egress-fuse=20#5000=20=E2=80=94=20a?= =?UTF-8?q?ssert=20the=20#7167=20irreducible=20gate,=20not=20the=20post-fo?= =?UTF-8?q?ld=20measured=20doom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new pre-fold irreducible-dimension gate (src/server.ts) now answers a tools-heavy body before the fold runs, so the e2e #5000 case (120 tools × 40KB, core text clearing the byte floor) 413s with the irreducible message instead of the post-fold 'outbound body measures ~N TEXT tokens' doom. The behavior is the PR's intended change (no history rewrite, no summary billing); the test's assertion and comment were stale. Update them to pin the new verdict: code preflight_compress_failed, retryable false, the irreducible-dimension message, 'Compaction was NOT attempted', zero upstream calls. --- tests/issue4079-egress-window-fuse.test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/issue4079-egress-window-fuse.test.ts b/tests/issue4079-egress-window-fuse.test.ts index 6a4d423bb0..8bc9c7f554 100644 --- a/tests/issue4079-egress-window-fuse.test.ts +++ b/tests/issue4079-egress-window-fuse.test.ts @@ -175,7 +175,7 @@ test("e2e #4079: a body that provably cannot fit any window is stopped at the eg } }); -test("e2e #5000: a body whose mass rides tools[] — core text clearing the byte floor — is still stopped by the measured tier, not forwarded", async () => { +test("e2e #5000: a body whose mass rides tools[] — core text clearing the byte floor — is stopped by the #7167 irreducible gate before the fold, not forwarded", async () => { const root = mkdtempSync(path.join(tmpdir(), "bili-4079t-")); const { ctx, calls, close } = await setup(); try { @@ -183,8 +183,11 @@ test("e2e #5000: a body whose mass rides tools[] — core text clearing the byte // which the core-text byte floor never sees (it prices m.text only). // 120 tools × 40KB descriptions ≈ 4.8MB of tool text ≈ ~1.2M measured // tokens — over the floored 1M bar — while the core text is ~1.5KB, - // ~5000× under the 7.5MB byte floor. With `model` present the preflight - // measured gate fires before the relay fuse; same verdict either way. + // ~5000× under the 7.5MB byte floor. With `model` present the #7167 + // irreducible-dimension gate fires BEFORE the fold (tools[] alone clear + // the doom bar, so no amount of message folding can fit the body) — + // no history rewrite, no summary billing — and answers the same 413 + // the post-fold measured doom would have, with the irreducible message. const tools = Array.from({ length: 120 }, (_, i) => ({ type: "function", function: { name: `tool_${i}`, description: "d".repeat(40_000) }, @@ -199,9 +202,10 @@ test("e2e #5000: a body whose mass rides tools[] — core text clearing the byte const parsed = JSON.parse(body) as { error?: { type?: string; code?: string; retryable?: boolean; message?: string } }; assert.equal(r.status, 413, `tools-mass body must fail fast (got ${r.status}): ${body.slice(0, 300)}`); - assert.equal(parsed.error?.code, "preflight_compress_failed", "the preflight fail-fast house shape (the measured gate answers before the relay)"); + assert.equal(parsed.error?.code, "preflight_compress_failed", "the preflight fail-fast house shape (the irreducible gate answers before the fold)"); assert.equal(parsed.error?.retryable, false, "4xx non-retryable class — retrying re-sends the same oversized history (#2847 semantics)"); - assert.match(parsed.error?.message ?? "", /outbound body measures ~[\d,]+ TEXT tokens/, "the measured tier names itself (text-only; image payloads are #496 arbitration, never doom)"); + assert.match(parsed.error?.message ?? "", /tools\[\] \+ system \+ other alone measure ~[\d,]+ tokens/, "the #7167 irreducible gate names the dimensions folding cannot touch (text-only; image payloads are #496 arbitration, never doom)"); + assert.match(parsed.error?.message ?? "", /Compaction was NOT attempted/, "no history rewrite, no summary billing — the fold never ran"); assert.match(parsed.error?.message ?? "", /NOT forwarded/, "the remedy wording is present"); // THE behavior under test: the doomed payload NEVER reaches the upstream // (before the measured tier it cleared the byte floor and rode forward From e836df8a146ca03c87c56180f196e694d385c1b2 Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 18 Nov 2026 23:42:15 +0800 Subject: [PATCH 4/4] test: pin the shape-aware breakdown (messages/input/contents -> messages bucket) The #7167 irreducible-dimension gate prices total - messages, so the foldable message channel must be bucketed as messages for every wire shape (chat messages[], Responses input, Google contents). Pin that with a unit test so the gate cannot doom an over-window Responses/Google body before a fold could run (the CI break where input/contents mass landed in other and the gate 413'd perfectly-foldable bodies). --- tests/raw-body-walk-depth.test.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/raw-body-walk-depth.test.ts b/tests/raw-body-walk-depth.test.ts index 3357d5277d..31c3a9dacb 100644 --- a/tests/raw-body-walk-depth.test.ts +++ b/tests/raw-body-walk-depth.test.ts @@ -84,3 +84,29 @@ test("non-object top-level bodies land whole in the other bucket", () => { assert.deepEqual(bd, { total: t("plain string body"), messages: 0, tools: 0, system: 0, other: t("plain string body") }); assert.equal(estimateRawBodyTokens("plain string body"), t("plain string body")); }); + +// #7167: the foldable message channel is bucketed as `messages` for EVERY wire +// shape — chat `messages[]`, Responses `input`, Google `contents` — so the +// irreducible-dimension gate (which prices `total − messages`) cannot doom an +// over-window Responses/Google body before a fold could run. Regression for +// the CI break where `input`/`contents` mass landed in `other` and the gate +// 413'd bodies that were perfectly foldable. +test("foldable channel is the messages bucket on every wire shape (#7167)", () => { + const filler = "m".repeat(1_000); + // Responses API: the conversation rides `input`, not `messages`. + const responses = { model: "gpt", stream: true, instructions: "sys", input: [{ role: "user", content: filler }] }; + const br = estimateRawBodyBreakdown(responses); + assert.ok(br.messages >= t(filler), "Responses `input` mass lands in the messages bucket"); + assert.equal(br.messages + br.tools + br.system + br.other, br.total, "buckets stay exhaustive"); + assert.equal(br.total, estimateRawBodyTokens(responses), "total invariant holds for Responses"); + // Google wire: the conversation rides `contents`, not `messages`. + const google = { model: "gemini", contents: [{ role: "user", parts: [{ text: filler }] }] }; + const bg = estimateRawBodyBreakdown(google); + assert.ok(bg.messages >= t(filler), "Google `contents` mass lands in the messages bucket"); + assert.equal(bg.total, estimateRawBodyTokens(google), "total invariant holds for Google"); + // The irreducible price the gate pays (total − messages) excludes the + // foldable channel on all three shapes. + for (const [shape, bd] of [["responses", br], ["google", bg]] as const) { + assert.ok(bd.total - bd.messages < t(filler), `${shape}: irreducible price excludes the foldable channel`); + } +});