From 2c939f535934df8ec79fbdcc28cce3059703d4e9 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 20 Oct 2026 21:36:03 +0800 Subject: [PATCH 1/8] test(#3977): pin the unpinned escape-hatch edges before the size-evidence ledger extraction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Characterization pins (先打桩), all locking CURRENT behavior on the batch-B integration base so the pure-extraction refactor (steps 1-3 of #3977) has a hard guardrail: - P-IMG-a: image-dominated over-window payload with no local evidence rides for upstream arbitration (#496/#1800 hatch itself). - P-IMG-b: after a scoped local 413 the same payload stays refused, zero upstream calls — the #3846 evidence guard on the image hatch. - P-DIV: after a scoped local 413 a foldable payload folds and the POST-FOLD view forwards (200) — evidence closes the forward-once escape hatches, not the legitimate folded forward. - P-LEARN: an upstream 400 whose body states the real token count changes NOTHING in the estimator's verdict (titles byte-identical) — @flip-target for step 4 learnFromOutcome(overflow-400) (#3900). --- tests/issue3977-size-evidence-pins.test.ts | 309 +++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 tests/issue3977-size-evidence-pins.test.ts diff --git a/tests/issue3977-size-evidence-pins.test.ts b/tests/issue3977-size-evidence-pins.test.ts new file mode 100644 index 0000000000..508ecffaab --- /dev/null +++ b/tests/issue3977-size-evidence-pins.test.ts @@ -0,0 +1,309 @@ +// #3977 pins (先打桩): characterization tests locking the CURRENT observable +// behavior of the three escape hatches that #3846's evidence store guards but +// no test yet pins, plus the calibration-loop boundary that step 4 of the +// ledger migration will intentionally flip. +// +// These pins are the guardrail for the pure-extraction PR (size-evidence +// ledger + preflight verdict state machine): every assertion here must stay +// byte-identical through steps 1-3 of the migration. Assertions marked +// "@flip-target" are the deliberate behavior change of step 4 +// (learnFromOutcome) and will be inverted in that PR, not before. +// +// P-IMG-a control: the image-arbitration hatch forwards an over-window +// image-dominated payload with NO local evidence (upstream bills). +// P-IMG-b edge: the same payload AFTER a scoped local 413 stays refused — +// zero upstream calls (the #3846 guard on the image hatch). +// P-DIV edge: a payload that FOLDS under the window after a local 413 — +// current post-refusal fold semantics, observed and pinned. +// P-LEARN boundary: an upstream 400 whose body states the real token count +// teaches the estimator NOTHING today (titles byte-identical) — +// @flip-target for learnFromOutcome(overflow-400). +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 { getSession } from "../src/session.ts"; + +const WINDOW = 20_000; +const SUMMARY_TEXT = + "PREFLIGHT SUMMARY: the segment held deterministic load-growth payloads across turns; every marker derives from the turn index, so folding loses nothing."; + +// ~400KB of varying ASCII core text: ~100K tokens at chars/4 — 5x the 20K +// window, robust across the k̂ clamp — and placed in the SYSTEM message so it +// is wire overhead: folding can never remove it (the #3846 refusal shape). +function bigSystem(): string { + let out = ""; + for (let i = 0; i < 400_000; i += 64) { + out += `oversized non-foldable system load segment ${Math.floor(i / 64)}: ` + "x".repeat(40); + } + return out.slice(0, 400_000); +} + +// A ~100KB base64 data-URL image: ~25K tokens under bytes billing, so a small +// text channel (~2.5K tokens) plus this image clears the 20K window on the +// image estimate alone. +function imageContent(): unknown { + return [ + { type: "text", text: "describe this" }, + { type: "image_url", image_url: { url: `data:image/png;base64,${"A".repeat(100_000)}` } }, + ]; +} + +// Six alternating turns of ~12KB user/assistant text (~3K tokens each) — +// plenty of compressible ranges — plus a tail question. +function foldableHistory(): Array<{ role: string; content: string }> { + const messages: Array<{ role: string; content: string }> = []; + for (let i = 0; i < 6; i++) { + messages.push({ role: i % 2 === 0 ? "user" : "assistant", content: `turn ${i} load: ` + `y${i}`.repeat(6_000) }); + } + messages.push({ role: "user", content: "final question" }); + return messages; +} + +type Call = { summary: boolean; stream: boolean; contentChars: number }; + +function chatSse(text: string, usage?: { prompt_tokens: number; completion_tokens: number }): 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`; + const u = usage ? { usage } : {}; + return chunk({ role: "assistant" }, null) + chunk({ content: text }, null) + chunk({}, "stop", u) + "data: [DONE]\n\n"; +} + +// Upstream that can be flipped to answer the NEXT forwarded request with the +// production-shaped 400 that STATES the real token count in its body. +function makeUpstream(): { server: http.Server; calls: Call[]; arm400: () => void } { + const calls: Call[] = []; + let arm400 = false; + 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"); + const isSummary = /TASK: The conversation segment below/.test(raw); + try { + const parsed = JSON.parse(raw) as { stream?: boolean; messages?: Array<{ content?: unknown }> }; + const chars = (parsed.messages ?? []).reduce((n, m) => n + (typeof m.content === "string" ? m.content.length : 0), 0); + calls.push({ summary: isSummary, stream: !isSummary && !!parsed.stream, contentChars: chars }); + if (isSummary) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chatcmpl-sum", object: "chat.completion", model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: SUMMARY_TEXT }, finish_reason: "stop" }] })); + return; + } + if (arm400) { + arm400 = false; + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: "This model's maximum context length is 20000 tokens. However, your messages resulted in 1198269 tokens. Please reduce the length of the messages.", type: "invalid_request_error" } })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(chatSse("forwarded answer", { prompt_tokens: Math.max(1, Math.round(chars / 40)), completion_tokens: 3 })); + } catch { + res.writeHead(400, { "content-type": "application/json" }); + res.end('{"error":{"message":"unparseable"}}'); + } + }); + }); + return { server, calls, arm400: () => { arm400 = true; } }; +} + +type Upstream = ReturnType; + +function proxyOptions(routes: Record): ProxyOptions { + return { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: routes as ProxyOptions["routes"], + modelContextLimit: WINDOW, + kernelConfig: defaultConfig(WINDOW, { + preserveRecentMessages: 2, + preserveRecentTokens: 2000, + compress: { minCompressRange: 1000, maxSummaryLength: 20000, minSummaryLength: 50 }, + }), + compress: { injectTool: true, injectNudge: true }, + 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 listen(u: Upstream): Promise { + u.server.listen(0, "127.0.0.1"); + await once(u.server, "listening"); + return (u.server.address() as { port: number }).port; +} + +async function setup(routes: Record): Promise<{ ctx: Ctx; close: () => Promise }> { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const firstKey = Object.keys(routes)[0]!; + const proxy = await startServer(proxyOptions(routes)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + return { + ctx: { + url: `http://127.0.0.1:${proxyPort}/bili/${firstKey}/chat/completions`, + headers: { "content-type": "application/json", "x-acp-session": "" }, + }, + close: async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await Promise.allSettled([once(proxy, "close")]); + }, + }; +} + +const post = (ctx: Ctx, body: Record) => + fetch(ctx.url, { method: "POST", headers: { ...ctx.headers }, body: JSON.stringify(body) }); + +const streamCalls = (calls: Call[]) => calls.filter((c) => c.stream).length; + +// Seed a fitting baseline, then land the scoped local 413 (bigSystem is wire +// overhead: nothing foldable, deterministic refusal) — the #3846 evidence. +async function seedAndRefuse(ctx: Ctx, sessionId: string, model = "gpt-test"): Promise { + ctx.headers["x-acp-session"] = sessionId; + const rSeed = await post(ctx, { model, stream: true, messages: [{ role: "user", content: "hello" }] }); + assert.equal(rSeed.status, 200, "the fitting seed forwards"); + await rSeed.text(); + + const rBig = await post(ctx, { model, stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); + assert.equal(rBig.status, 413, "oversized non-foldable payload is refused locally"); + const body = await rBig.text(); + assert.match(body, /NOT forwarded/, "the verdict says the payload did not ride"); + return body; +} + +test("pin #3977 P-IMG-a: image-dominated over-window payload with NO local evidence forwards for upstream arbitration (#496/#1800)", async () => { + const up = makeUpstream(); + const port = await listen(up); + const { ctx, close } = await setup({ [`http://127.0.0.1:${port}`]: { models: { "gpt-test": { context: WINDOW } } } }); + try { + ctx.headers["x-acp-session"] = "p3977-img-a"; + const rSeed = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }); + assert.equal(rSeed.status, 200, "the fitting seed forwards"); + await rSeed.text(); + + const rImg = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: imageContent() }] }); + assert.equal(rImg.status, 200, "image-dominated payload rides: the upstream arbitrates billing"); + await rImg.text(); + assert.equal(streamCalls(up.calls), 2, "the image payload reached the upstream"); + } finally { + up.server.close(); + up.server.closeIdleConnections?.(); + await close(); + } +}); + +test("pin #3977 P-IMG-b: after a scoped local 413 the SAME image payload stays refused — the image hatch consults the evidence (#3846 guard)", async () => { + const up = makeUpstream(); + const port = await listen(up); + const { ctx, close } = await setup({ [`http://127.0.0.1:${port}`]: { models: { "gpt-test": { context: WINDOW } } } }); + try { + await seedAndRefuse(ctx, "p3977-img-b"); + assert.equal(streamCalls(up.calls), 1, "only the seed reached the upstream"); + + const rImg = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: imageContent() }] }); + assert.equal(rImg.status, 413, "the scoped refusal outranks the image-arbitration bet"); + const body = await rImg.text(); + assert.match(body, /NOT forwarded/, "the verdict says the payload did not ride"); + assert.equal(streamCalls(up.calls), 1, "zero new upstream calls — the hatch never re-bets"); + + const s = getSession("p3977-img-b")!; + const refusals = s.metadata.localRefusals as unknown[]; + assert.ok(Array.isArray(refusals) && refusals.length === 1, "the evidence store still holds exactly one scoped record"); + } finally { + up.server.close(); + up.server.closeIdleConnections?.(); + await close(); + } +}); + +test("pin #3977 P-DIV: after a scoped local 413, a payload that folds under the window — current post-refusal fold semantics, observed and locked", async () => { + const up = makeUpstream(); + const port = await listen(up); + const { ctx, close } = await setup({ [`http://127.0.0.1:${port}`]: { models: { "gpt-test": { context: WINDOW } } } }); + try { + await seedAndRefuse(ctx, "p3977-div"); + assert.equal(streamCalls(up.calls), 1, "only the seed reached the upstream"); + + // Foldable history (~6 x 3K tokens = ~18K core tokens, compressible) — + // the walk can fold it under the 20K window. + const rFold = await post(ctx, { model: "gpt-test", stream: true, messages: foldableHistory() }); + const foldBody = await rFold.text(); + // OBSERVED on the integration branch (550860ed): the payload folds + // under the window and the POST-FOLD view forwards (200). The scoped + // refusal closes the forward-once ESCAPE hatches (probe/image/#1839), + // not the legitimate folded forward — pinning exactly that boundary. + assert.equal(rFold.status, 200, "the folded post-refusal payload forwards"); + assert.ok(up.calls.some((c) => c.summary), "the walk really folded (a summarization call ran)"); + assert.equal(streamCalls(up.calls), 2, "seed + folded forward are the only rides"); + } finally { + up.server.close(); + up.server.closeIdleConnections?.(); + await close(); + } +}); + +test("pin #3977 P-LEARN: an upstream 400 that STATES the real token count teaches the estimator NOTHING today — titles byte-identical (@flip-target for step 4)", async () => { + const up = makeUpstream(); + const port = await listen(up); + const { ctx, close } = await setup({ [`http://127.0.0.1:${port}`]: { models: { "gpt-test": { context: WINDOW } } } }); + try { + ctx.headers["x-acp-session"] = "p3977-learn"; + const rSeed = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }); + assert.equal(rSeed.status, 200, "the fitting seed forwards"); + await rSeed.text(); + + // Detour FIRST (before any local refusal exists — P-IMG-b proved the + // evidence would block the ride): the image payload rides by + // arbitration and the upstream answers with the #3900-shaped 400 whose + // body STATES the real token count. + up.arm400(); + const rImg = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: imageContent() }] }); + const imgStatus = rImg.status; + await rImg.text(); + // OBSERVED on the integration branch (550860ed): the image payload + // rides and the upstream answers the stated-count 400 verbatim. + assert.equal(imgStatus, 400, "the detour payload rides and is answered with the stated-count 400"); + assert.equal(up.calls.filter((c) => c.stream).length >= 2, true, "the detour payload reached the upstream"); + + // Baseline title for the oversized non-foldable payload, AFTER the + // stated-count 400 has had every chance to arm/teach. + const rBig1 = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); + assert.equal(rBig1.status, 413, "oversized payload refused locally"); + const title1 = await rBig1.text(); + + // The oversized non-foldable payload again: with learnFromOutcome + // (step 4) the stated-count 400 becomes calibration evidence and the + // numbers in this title MOVE. Today they must not. + const rBig2 = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); + assert.equal(rBig2.status, 413, "still refused locally"); + const title2 = await rBig2.text(); + assert.equal(title1, title2, "the stated-count 400 changed nothing in the estimator's verdict today"); + } finally { + up.server.close(); + up.server.closeIdleConnections?.(); + await close(); + } +}); From 95e7808f9bf6e4995683477508bc033e28499af7 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 20 Oct 2026 21:46:33 +0800 Subject: [PATCH 2/8] =?UTF-8?q?refactor(#3977):=20Step=201=20=E2=80=94=20p?= =?UTF-8?q?ure=20extraction=20of=20the=20size-evidence=20stores=20and=20th?= =?UTF-8?q?e=20final=20verdict=20classification?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zero behavior change (all 171 guard tests green, incl. the four #3977 pins): - src/size-evidence.ts: localRefusals store (#3846: noteLocalRefusal / localRefusalCovers / shape-filtered read) and the preflightDeadEnd write side (#728) moved verbatim from server.ts. Metadata keys and shapes unchanged; legacy session files read exactly as before. - src/preflight-verdict.ts: the final-verdict expressions (status ternary #3461, progressContinues #3918, retryable #2189, progress detail suffix, contentDeadEnd) moved verbatim as pure functions over a structural VerdictWalkSlice — server.ts keeps the walk object and calls these. - The dead-end READ block stays inline (its replay/eviction semantics are exactly as rich as the block that owns them); its write now goes through armDeadEnd. Step 2 (escape hatches consult fitEvidence) and Step 3 (single render point) land behind this on the same branch. --- CONFIGURATION.md | 4 +- CONFIGURATION.zh-CN.md | 4 +- src/preflight-verdict.ts | 73 +++++++++++++++++++++++++++++++ src/server.ts | 63 +++++---------------------- src/size-evidence.ts | 93 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 179 insertions(+), 58 deletions(-) create mode 100644 src/preflight-verdict.ts create mode 100644 src/size-evidence.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index be710a94c8..79decc9ead 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -175,11 +175,9 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `persist.epermAlertRepeatMs` | number | 0 (no repeats) | BILI_PERSIST_EPERM_ALERT_REPEAT_MS | Repeat interval for the EPERM alert; 0 logs it once. | | `storage` | object | {} (all defaults) | — | Unified SQLite storage sidecar settings (#2671). | | `storage.unified` | legacy\|shadow\|full | unset (full) | BILI_STORAGE_UNIFIED | Tiered storage-migration switch (#2671): shadow = unified SQLite double-write dry-run with digest reconciliation; full = drop-in read path (unified-first loads with organic import, legacy fallback). Absent everywhere now defaults to FULL (#3818 flip - the shadow dry-run reconciled cleanly since #2815, so the index is the load path; JSON envelopes keep being written, and legacy remains the one-line escape hatch). File tier is strict legacy/shadow/full; the env tier also accepts 1/true (=shadow) and unified (=full) and wins over the file. Resolved once per lane boot - hot edits need a restart. Encryption (BILI_ENCRYPTION_KEY) hard-blocks the sidecar. | -| `storage.backend` | sqlite\|mysql | unset (sqlite) | BILI_STORAGE_BACKEND | Storage backend selection, step 1 of the pluggable-storage plan (#3388 / #3931): the unified store consumes an operation-level async StorageBackend contract, with sqlite as the default and today the only real implementation (the existing UnifiedStore behind a Promise-wrapped adapter - zero behavior change, no schema change). mysql is a REGISTERED STUB: the name validates so configs do not bounce between unknown/unimplemented as the real backend ships (step 2, mysql2 client via the optional-dependency + self-download discipline), but selecting it logs one notice line at lane boot and the store stays on sqlite; paths that open a backend directly (export) fail with the documented error. Unknown names also fall back to sqlite with a notice. Resolved once per lane boot. | -| `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | Gate for the one-time SQLite engine self-download (#2833): when no engine is available in-process (no built-in node:sqlite, no host-installed better-sqlite3) and the unified sidecar is wanted on this platform, bili downloads the pinned better-sqlite3 prebuilt tarball from the npm registry once (~11 MB, sha512-verified) into the cache dir and reuses it on every later start. Engine resolution order: built-in node:sqlite (Node >= 22.5) -> host-provided better-sqlite3 -> cached self-download -> self-download (gated by this switch) -> none (legacy-only, the #2825 degrade contract). The switch gates only the network fetch - a cached copy is used even when it is false; unsupported platforms never download. The tarball download walks a builtin mirror list (#3338): the configured registry (BILI_UPDATE_REGISTRY / update.registry) is tried first, then https://registry.npmjs.org, then https://registry.npmmirror.com - the first mirror that yields a sha512-verified tarball wins; a Node < 22.5 with no engine gets an explicit Node upgrade hint in the degrade reason (#3385). Engine artifacts are pinned PER RUNTIME, because a binding cannot be loaded outside the range its package declares: Node >= 22 with N-API >= 10 uses better-sqlite3 v13 (one tarball that carries its own prebuilds), while Node 20 and the Node 22.0-22.12 band - which passes v13's major floor but only ships N-API 9 - use v12.9.0 (a JS-only tarball plus its ABI-keyed prebuild, fetched from the binary mirror and bound through nativeBinding). That keeps a low Node on the unified lane instead of degrading to legacy. A runtime no pinned artifact serves (below every floor, or an ABI no prebuild targets) is refused before any load, with neither the download nor a cached copy registered, since loading such a binding crashes the process instead of failing gracefully (#3341, #3894). A failed download arms a 6-hour retry cooldown persisted as a marker file under the cache dir (billion-context/sqlite/.bootstrap-failed.json); the storage diagnostics blob shows the marker's absolute expiry time and path - delete that file and restart bili to force an immediate retry instead of waiting out the whole window (#3788). | | `storage.backend` | sqlite\|mysql | unset (sqlite) | BILI_STORAGE_BACKEND | Storage backend selection (#3388 / #3931): the unified store consumes an operation-level async StorageBackend contract. sqlite is the default (the existing UnifiedStore behind a Promise-wrapped adapter). mysql is FULLY WIRED (steps 2+3): openBackend("mysql", dsn) opens the real MysqlBackend against MySQL 8.0 — the full contract surface (ingest with CAS refcounts, reads, session_ext, reap journal, archive/restore/gc, checkpoints, conversations), with mysql2 bundled as a regular dependency and loaded lazily (boots that never select mysql never load it). The proxy hot path is wired through the async shadow facade: saves enqueue onto a serial backend queue (watermark deltas, orphan/reap journal, digest sampling — sqlite keeps its zero-await sync fast path unchanged), SYNC reads ride the JSON envelope (the sidecar attaches asynchronously via ready(); viewFor/importRecord sync facades miss by design), replicas/ext/GC/export speak the async twins. writerOff stays sqlite-only (the envelope is the mysql sync read path). Unknown names fall back to sqlite with a notice. Resolved once per lane boot. | | `storage.mysql.dsn` | string | unset | BILI_MYSQL_DSN | MySQL DSN for storage.backend="mysql" (#3388 step 2, #3931): mysql://user:pass@host:3306/db. Consumed by the StorageBackend contract paths (hot path included — the async shadow queue). env BILI_MYSQL_DSN wins over the file key. Empty/unset = the mysql backend cannot open — the documented error names both knobs and the working default. | -| `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | Gate for the one-time SQLite engine self-download (#2833): when no engine is available in-process (no built-in node:sqlite, no host-installed better-sqlite3) and the unified sidecar is wanted on this platform, bili downloads the pinned better-sqlite3 prebuilt tarball from the npm registry once (~11 MB, sha512-verified) into the cache dir and reuses it on every later start. Engine resolution order: built-in node:sqlite (Node >= 22.5) -> host-provided better-sqlite3 -> cached self-download -> self-download (gated by this switch) -> none (legacy-only, the #2825 degrade contract). The switch gates only the network fetch - a cached copy is used even when it is false; unsupported platforms never download. The tarball download walks a builtin mirror list (#3338): the configured registry (BILI_UPDATE_REGISTRY / update.registry) is tried first, then https://registry.npmjs.org, then https://registry.npmmirror.com - the first mirror that yields a sha512-verified tarball wins; a Node < 22.5 with no engine gets an explicit Node upgrade hint in the degrade reason (#3385). A failed download arms a 6-hour retry cooldown persisted as a marker file under the cache dir (billion-context/sqlite/.bootstrap-failed.json); the storage diagnostics blob shows the marker's absolute expiry time and path - delete that file and restart bili to force an immediate retry instead of waiting out the whole window (#3788). | +| `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | Gate for the one-time SQLite engine self-download (#2833): when no engine is available in-process (no built-in node:sqlite, no host-installed better-sqlite3) and the unified sidecar is wanted on this platform, bili downloads the pinned better-sqlite3 prebuilt tarball from the npm registry once (~11 MB, sha512-verified) into the cache dir and reuses it on every later start. Engine resolution order: built-in node:sqlite (Node >= 22.5) -> host-provided better-sqlite3 -> cached self-download -> self-download (gated by this switch) -> none (legacy-only, the #2825 degrade contract). The switch gates only the network fetch - a cached copy is used even when it is false; unsupported platforms never download. The tarball download walks a builtin mirror list (#3338): the configured registry (BILI_UPDATE_REGISTRY / update.registry) is tried first, then https://registry.npmjs.org, then https://registry.npmmirror.com - the first mirror that yields a sha512-verified tarball wins; a Node < 22.5 with no engine gets an explicit Node upgrade hint in the degrade reason (#3385). Engine artifacts are pinned PER RUNTIME, because a binding cannot be loaded outside the range its package declares: Node >= 22 with N-API >= 10 uses better-sqlite3 v13 (one tarball that carries its own prebuilds), while Node 20 and the Node 22.0-22.12 band - which passes v13's major floor but only ships N-API 9 - use v12.9.0 (a JS-only tarball plus its ABI-keyed prebuild, fetched from the binary mirror and bound through nativeBinding). That keeps a low Node on the unified lane instead of degrading to legacy. A runtime no pinned artifact serves (below every floor, or an ABI no prebuild targets) is refused before any load, with neither the download nor a cached copy registered, since loading such a binding crashes the process instead of failing gracefully (#3341, #3894). A failed download arms a 6-hour retry cooldown persisted as a marker file under the cache dir (billion-context/sqlite/.bootstrap-failed.json); the storage diagnostics blob shows the marker's absolute expiry time and path - delete that file and restart bili to force an immediate retry instead of waiting out the whole window (#3788). | | `storage.jsonWriter` | on\|off | on | BILI_STORAGE_JSON_WRITER | Phase 3 envelope switch (#2960): off stops writing the legacy JSON session envelope while storage.unified is full - the SQLite index becomes the only persistent copy (reads are already unified-first; boot enumerates the index union, so index-only sessions load normally). Only honored in effective-full mode: under shadow/legacy (or with encryption active) the request is ignored with a one-time warning because the JSON file remains the sole source of truth. Env tier 0/false/off = off and 1/true/on = on wins over the strict file spellings. Fail-safe: if the sidecar disables itself mid-run, envelope writes resume on the next save. Rollback: unset the knob - writing resumes, index-only sessions keep loading via the boot union, and replicas/restore re-materialize portable JSON envelopes. Downgrading the binary (or losing the SQLite engine) makes index-only sessions invisible to v0.1.196 and older until a >=v0.1.197 binary runs again - the data stays intact in index.db, it is just not read by the older code. The per-session content-store sidecar keeps being written either way. A jsonWriter=off lane must be the SOLE writer of its sessions directory: an off lane reads unified-first and never falls back to the file, so any peer instance running legacy/shadow against the same directory diverges silently (its JSON copy stops receiving updates, and the off lane never sees the peer's writes). | | `storage.walJournalSizeLimitBytes` | number | 8388608 | BILI_STORAGE_WAL_JOURNAL_SIZE_LIMIT_BYTES | WAL reclamation cap for index.db-wal (#3250), in bytes: every completed checkpoint truncates the WAL file back to this size, bounding disk use and crash-recovery replay in long-lived processes. NOT a per-session data quota - data lands in index.db as it is written and the WAL only stages frames between checkpoints, so session count/size does not scale this; a large burst can overshoot temporarily until the next completed checkpoint truncates back. Higher = rarer truncate IO but more steady disk use and a longer crash-replay tail. Env tier (positive integer) wins over the file tier; a garbage or non-positive env value falls through to the file/default tier instead of failing boot. Applied when the store opens at lane boot - hot edits need a restart. The effective value appears on the unified-enable log line and in the storage diagnostics blob. | | `storage.archiveAfterDays` | integer | 365 | BILI_STORAGE_ARCHIVE_AFTER_DAYS | GC age sweep window in days (#3463, #3901): sessions idle longer than this are archived - one JSON line appended to a single archive log (archive/archive.jsonl under the sessions directory — no per-session file spray), a tombstone row keeps the label and identity, content/media refs are released, and restore is one POST away. 0 disables the age sweep; POST /__bili/storage/gc always runs and takes ?vacuum=full to shrink an existing index.db (auto_vacuum=INCREMENTAL is always on for new writes). Env wins over the file tier. | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 5ae78cbb7f..3c6dbbf737 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -175,11 +175,9 @@ | `persist.epermAlertRepeatMs` | number | 0 (no repeats) | BILI_PERSIST_EPERM_ALERT_REPEAT_MS | EPERM 告警的重复间隔;0 只记录一次。 | | `storage` | object | {} (all defaults) | — | 统一 SQLite 存储边车设置(#2671)。 | | `storage.unified` | legacy\|shadow\|full | unset (full) | BILI_STORAGE_UNIFIED | 分层存储迁移开关(#2671):shadow = 统一 SQLite 双写干跑 + digest 对账;full = 平替读路径(unified-first 加载 + 有机导入 + legacy 兜底)。两处均未设置时默认 full(#3818 翻转 —— shadow 干跑自 #2815 起对账一直干净,索引因此成为默认加载路径;JSON 信封照写,legacy 仍是一行逃生口)。文件层只接受 legacy/shadow/full;环境变量层还接受 1/true(=shadow)与 unified(=full)并优先于文件。每个 lane 启动时解析一次 —— 热改动需重启生效。加密(BILI_ENCRYPTION_KEY)会硬阻断边车。 | -| `storage.backend` | sqlite\|mysql | unset (sqlite) | BILI_STORAGE_BACKEND | 存储后端选择,可插拔存储计划第一步(#3388 / #3931):统一存储消费操作级的异步 StorageBackend 契约,sqlite 为默认且是当前唯一真实现(现有 UnifiedStore 套一层 Promise 包装适配器 —— 零行为变化、零 schema 变化)。mysql 是已注册占位:该名字能通过校验(真实后端随第二步落地 —— mysql2 客户端,走可选依赖+自下载纪律 —— 期间配置不会在 unknown/unimplemented 之间来回弹),但选中它只会在 lane 启动时打一行提示并继续用 sqlite;直接开后端的路径(export)按文档化的错误失败。未知名字同样回退 sqlite 并提示。每个 lane 启动时解析一次。 | -| `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | SQLite 引擎一次性自下载的闸门(#2833):进程内没有可用引擎(无内置 node:sqlite、宿主未装 better-sqlite3)且本平台希望启用统一边车时,bili 从 npm registry 一次性下载钉死的 better-sqlite3 预编译 tarball(约 11 MB,sha512 校验)到缓存目录,之后每次启动复用。引擎解析顺序:内置 node:sqlite(Node >= 22.5)-> 宿主机已装的 better-sqlite3 -> 缓存的自下载副本 -> 自下载(本开关控制)-> 无引擎(仅 legacy,即 #2825 降级契约)。开关只控制网络下载 —— 为 false 时缓存副本仍会使用;不支持的平台永不下载。 tarball 下载内置镜像列表兜底(#3338):先试配置的 registry(BILI_UPDATE_REGISTRY / update.registry),再试 https://registry.npmjs.org、https://registry.npmmirror.com —— 第一个通过 sha512 校验的镜像生效;Node < 22.5 且无引擎时,降级原因会给出明确的 Node 升级提示(#3385)。引擎产物按运行时特性分别钉死,因为 binding 无法在包自身声明的范围之外加载:Node >= 22 且 N-API >= 10 用 better-sqlite3 v13(单个 tarball 自带 prebuilds),而 Node 20 与 Node 22.0–22.12(满足 v13 的大版本下限但只带 N-API 9)用 v12.9.0(纯 JS tarball 加与之 ABI 匹配的 prebuild,从二进制镜像获取并经 nativeBinding 注入)—— 低版本 Node 因此留在统一存储车道上,而不是降级到 legacy。任何钉死产物都不支持的运行时(低于所有下限,或 ABI 无对应 prebuild)会在加载之前被拒绝,下载与缓存副本注册都不进行 —— 在那里加载该 binding 会让进程崩溃,而不是优雅失败(#3341、#3894)。下载失败会置一个 6 小时的重试冷却,以标记文件形式持久化在缓存目录下(billion-context/sqlite/.bootstrap-failed.json);storage 诊断块会显示该标记的绝对到期时间与路径 —— 删除该文件并重启 bili 即可强制立即重试,不必等完整个冷却窗口(#3788)。 | | `storage.backend` | sqlite\|mysql | unset (sqlite) | BILI_STORAGE_BACKEND | 存储后端选择(#3388 / #3931):统一存储消费操作级的异步 StorageBackend 契约。sqlite 为默认(现有 UnifiedStore 套一层 Promise 包装适配器)。mysql 已完整接线(第二+三步):openBackend("mysql", dsn) 打开真正的 MysqlBackend,对接 MySQL 8.0 —— 完整契约面(带 CAS 引用计数的 ingest、读取、session_ext、收割台账、归档/恢复/GC、checkpoint、会话绑定),mysql2 作为常规依赖打包并按需懒加载(从不选 mysql 的启动永不加载它)。代理热路径经异步影子 facade 接线:保存进串行后端队列(水位 delta、孤儿/收割台账、digest 采样 —— sqlite 保持零 await 同步快车道不变),同步读走 JSON 信封(sidecar 经 ready() 异步挂载;viewFor/importRecord 同步面按设计 miss),replicas/ext/GC/export 走异步孪生。writerOff 仅 sqlite(信封就是 mysql 的同步读路径)。未知名字回退 sqlite 并提示。每个 lane 启动时解析一次。 | | `storage.mysql.dsn` | string | unset | BILI_MYSQL_DSN | storage.backend="mysql" 的 MySQL DSN(#3388 第二步,#3931):mysql://user:pass@host:3306/db。由 StorageBackend 契约路径消费(含热路径 —— 异步影子队列)。环境变量 BILI_MYSQL_DSN 优先于文件键。空/未设置 = mysql 后端无法打开 —— 文档化错误会同时列出两个旋钮和可用的默认值。 | -| `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | SQLite 引擎一次性自下载的闸门(#2833):进程内没有可用引擎(无内置 node:sqlite、宿主未装 better-sqlite3)且本平台希望启用统一边车时,bili 从 npm registry 一次性下载钉死的 better-sqlite3 预编译 tarball(约 11 MB,sha512 校验)到缓存目录,之后每次启动复用。引擎解析顺序:内置 node:sqlite(Node >= 22.5)-> 宿主机已装的 better-sqlite3 -> 缓存的自下载副本 -> 自下载(本开关控制)-> 无引擎(仅 legacy,即 #2825 降级契约)。开关只控制网络下载 —— 为 false 时缓存副本仍会使用;不支持的平台永不下载。 tarball 下载内置镜像列表兜底(#3338):先试配置的 registry(BILI_UPDATE_REGISTRY / update.registry),再试 https://registry.npmjs.org、https://registry.npmmirror.com —— 第一个通过 sha512 校验的镜像生效;Node < 22.5 且无引擎时,降级原因会给出明确的 Node 升级提示(#3385)。下载失败会置一个 6 小时的重试冷却,以标记文件形式持久化在缓存目录下(billion-context/sqlite/.bootstrap-failed.json);storage 诊断块会显示该标记的绝对到期时间与路径 —— 删除该文件并重启 bili 即可强制立即重试,不必等完整个冷却窗口(#3788)。 | +| `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | SQLite 引擎一次性自下载的闸门(#2833):进程内没有可用引擎(无内置 node:sqlite、宿主未装 better-sqlite3)且本平台希望启用统一边车时,bili 从 npm registry 一次性下载钉死的 better-sqlite3 预编译 tarball(约 11 MB,sha512 校验)到缓存目录,之后每次启动复用。引擎解析顺序:内置 node:sqlite(Node >= 22.5)-> 宿主机已装的 better-sqlite3 -> 缓存的自下载副本 -> 自下载(本开关控制)-> 无引擎(仅 legacy,即 #2825 降级契约)。开关只控制网络下载 —— 为 false 时缓存副本仍会使用;不支持的平台永不下载。 tarball 下载内置镜像列表兜底(#3338):先试配置的 registry(BILI_UPDATE_REGISTRY / update.registry),再试 https://registry.npmjs.org、https://registry.npmmirror.com —— 第一个通过 sha512 校验的镜像生效;Node < 22.5 且无引擎时,降级原因会给出明确的 Node 升级提示(#3385)。引擎产物按运行时特性分别钉死,因为 binding 无法在包自身声明的范围之外加载:Node >= 22 且 N-API >= 10 用 better-sqlite3 v13(单个 tarball 自带 prebuilds),而 Node 20 与 Node 22.0–22.12(满足 v13 的大版本下限但只带 N-API 9)用 v12.9.0(纯 JS tarball 加与之 ABI 匹配的 prebuild,从二进制镜像获取并经 nativeBinding 注入)—— 低版本 Node 因此留在统一存储车道上,而不是降级到 legacy。任何钉死产物都不支持的运行时(低于所有下限,或 ABI 无对应 prebuild)会在加载之前被拒绝,下载与缓存副本注册都不进行 —— 在那里加载该 binding 会让进程崩溃,而不是优雅失败(#3341、#3894)。下载失败会置一个 6 小时的重试冷却,以标记文件形式持久化在缓存目录下(billion-context/sqlite/.bootstrap-failed.json);storage 诊断块会显示该标记的绝对到期时间与路径 —— 删除该文件并重启 bili 即可强制立即重试,不必等完整个冷却窗口(#3788)。 | | `storage.jsonWriter` | on\|off | on | BILI_STORAGE_JSON_WRITER | Phase 3 信封开关(#2960):off 时若 storage.unified 为 full 则不再写 legacy JSON 会话信封 —— SQLite 索引成为唯一持久化副本(读路径本就 unified-first;boot 枚举索引并集,纯索引会话可正常加载)。仅在有效 full 模式下生效:shadow/legacy(或加密激活)下请求会被忽略并警告一次,因为 JSON 文件仍是唯一事实源。环境变量层 0/false/off = off、1/true/on = on,优先于严格拼写的文件层。失败安全:若边车运行中自禁用,下次保存即恢复信封写入。回滚:去掉该开关 —— 写入恢复,纯索引会话经 boot 并集继续可加载,replicas/restore 可重新物化可移植的 JSON 信封。降级二进制(或丢失 SQLite 引擎)会让纯索引会话在 v0.1.196 及更早版本上不可见,直到重新运行 >=v0.1.197 —— 数据在 index.db 中完好无损,只是旧代码不读它。每会话的 content-store 边车无论开关如何都照写。jsonWriter=off 的 lane 必须是其会话目录的唯一写者:off lane 读路径 unified-first、永不回退到文件,因此同一目录上跑 legacy/shadow 的对等实例会静默分叉(它的 JSON 副本收不到更新,off lane 也看不到对等实例的写入)。 | | `storage.walJournalSizeLimitBytes` | number | 8388608 | BILI_STORAGE_WAL_JOURNAL_SIZE_LIMIT_BYTES | index.db-wal 的 WAL 回收上限(#3250),单位字节:每次完成的 checkpoint 把 WAL 文件截回该大小,约束长驻进程的磁盘占用与崩溃恢复重放。不是每会话数据配额 —— 数据写入时就已落进 index.db,WAL 只是两次 checkpoint 之间的帧暂存区,会话数量/大小不会放大它;大 burst 可能暂时超出,直到下一次完成的 checkpoint 截回。调大 = truncate IO 更稀疏,但稳态磁盘占用更多、崩溃恢复重放尾更长。环境变量层(正整数)优先于配置文件层;env 值非法或非正数时静默落到文件/默认层,boot 不崩。lane 启动、store open 时生效 —— 热改动需重启。实际取值会出现在 unified-enable 日志行与 storage 诊断块中。 | | `storage.archiveAfterDays` | integer | 365 | BILI_STORAGE_ARCHIVE_AFTER_DAYS | GC 年龄清扫窗口(天,#3463/#3901):闲置超过该时长的会话被归档 —— 以单行 JSON 追加到单一归档日志(会话目录下的 archive/archive.jsonl——不撒每会话小文件),墓碑行保留标签与身份,释放内容/媒体引用,一条 POST 即可恢复。0 关闭年龄清扫;POST /__bili/storage/gc 始终可用并支持 ?vacuum=full 收缩存量 index.db(新写入始终 auto_vacuum=INCREMENTAL)。环境变量优先于配置文件层。 | diff --git a/src/preflight-verdict.ts b/src/preflight-verdict.ts new file mode 100644 index 0000000000..e027e1cd11 --- /dev/null +++ b/src/preflight-verdict.ts @@ -0,0 +1,73 @@ +// #3977 Step 1 (pure extraction): the FINAL preflight verdict — the status / +// retryable / progress / dead-end classification at the bottom of +// preflightCompressIfNeeded — becomes a pure function so later steps can make +// the exits a closed enumeration with ONE render point. Every expression below +// moved verbatim from server.ts (comments included): same inputs, same +// outputs, byte-identical messages. + +/** Structural slice of the walk outcome the classification needs. The walk + * result itself stays internal to server.ts; only the fields the verdict + * reads are required here, so this stays a pure, dependency-free module. */ +export interface VerdictWalkSlice { + kind?: string; + deterministic?: boolean; + status?: number; + retryable?: boolean | undefined; + detail?: string; +} + +export type PreflightStatus = 413 | 502 | 503; + +/** (comment moved verbatim from server.ts) + * #3461 problem 2: DETERMINISTIC exhaustion (futile-round math proved no + * combination of folds can close the gap regardless of upstream health) + * can never succeed for this identical payload — answer it 413 like every + * other "can never fit as-is" verdict (#2847 lesson applied to the + * exhaustion path; #2921/#3146 doomed-bytes precedent) instead of 502, + * which status-code-driven headless hosts read as transient and spin on + * forever (field measurement: 5+ doomed rounds). Relay-dependent + * exhaustion (empty/truncated/incomplete summaries, budget draws, pool + * saturation) stays 502: those walks succeed once the relay recovers. + * AND only when bili's own calibrated meter agrees the payload is over the + * window (!payloadFitsWindow): when the calibrated estimate FITS but the + * walk still ran (char-count trigger arm, overflow-armed baseline), the + * doom is a relay-billing artifact bili cannot compute (#496/#767/#857 + * byte-relay lanes) — bili must not declare structural doom for it; those + * lanes keep their 502 fail-fast contract where NOT re-forwarding IS the + * protection, not a client spin. */ +export function classifyStatus(f: VerdictWalkSlice | undefined, payloadFitsWindow: boolean): PreflightStatus { + return f?.kind === "upstream" && f.status === 429 ? 503 : f?.kind === "exhausted" && f.deterministic && !payloadFitsWindow ? 413 : 502; +} + +/** (comment moved verbatim from server.ts) + * #3918: non-deterministic exhaustion WITH committed progress is not terminal — + * the folds persist in the session, so the next attempt starts from a smaller + * baseline and continues the fold. Reporting retryable=false told hosts to stop + * spinning exactly where each retry makes real progress (incident: 64 effective + * folds, every round declared terminal). Deterministic exhaustion keeps today's + * verdict: nothing more can fold from this view, so retries are genuinely doomed. */ +export function progressContinues(f: VerdictWalkSlice | undefined, compressedRanges: number): boolean { + return f?.kind === "exhausted" && !f.deterministic && compressedRanges > 0; +} + +/** (comment moved verbatim from server.ts) + * #2189: an explicit retryable=false from preflight (credential-shape + * rejection — deterministic for the identical payload) overrides the + * status-based heuristic below; every other path keeps today's mapping. */ +export function classifyRetryable(progress: boolean, f: VerdictWalkSlice | undefined): boolean { + return progress ? true : f?.retryable === false ? false : f?.retryable === true || (f?.kind === "upstream" && f.status !== undefined && (f.status === 429 || f.status >= 500)); +} + +/** The #3918 progress suffix appended to the fail-fast detail when folds + * committed this turn persist for the next attempt. */ +export function progressDetailSuffix(compressedRanges: number, foldedCalibratedSize: number | undefined, limit: number): string { + const shortfall = foldedCalibratedSize !== undefined ? Math.max(0, limit - foldedCalibratedSize) : undefined; + return ` ${compressedRanges} range(s) folded this turn persist in the session — the next attempt starts from a smaller baseline and continues the fold${shortfall !== undefined ? ` (~${shortfall.toLocaleString("en-US")} tokens short of the target)` : ""}.`; +} + +/** (comment moved verbatim from server.ts — the dead-end arming condition) + * contentDeadEnd: exhausted walks, and non-retryable upstream 4xx verdicts, + * arm the identical-request cooldown only when nothing folded this turn. */ +export function isContentDeadEnd(f: VerdictWalkSlice | undefined, retryable: boolean): boolean { + return f?.kind === "exhausted" || (f?.kind === "upstream" && f.status !== undefined && f.status >= 400 && f.status < 500 && !retryable); +} diff --git a/src/server.ts b/src/server.ts index f960d90726..29dc9651f2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -8,6 +8,8 @@ import { performance } from "node:perf_hooks"; import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, type PackSurface, defaultPrompts, deactivateBlock } from "acp-kernel"; import { resolveCompress, resolveCompressPrompts, resolveCompressSurfaceDetailed, resolveRequestConfig, type ResolvedKernelConfig } from "./compress-settings.js"; import { dropCompressReasoningIds, type CompressReasoningConfig } from "./reasoning-drop.js"; +import { noteLocalRefusal, localRefusalCovers, armDeadEnd } from "./size-evidence.js"; +import { classifyStatus, progressContinues as verdictProgressContinues, classifyRetryable, progressDetailSuffix, isContentDeadEnd } from "./preflight-verdict.js"; import type { ProxyOptions, ResignSettings } from "./config.js"; export type { ProxyOptions } from "./config.js"; import { loadNamedProviders, loadOptions, loadRoutes } from "./config.js"; @@ -2720,50 +2722,7 @@ function preflightDeadEndCooldownMs(): number { // — NEVER as usage (that field is upstream billing evidence only) — scoped to // the route/model/window that saw them; an equal-or-wider refused window // covers the current equal-or-tighter one, anything else evaluates fresh. -interface LocalRefusalRecord { - origin: string; - model: string; - window: number; - at: number; -} - -const LOCAL_REFUSAL_MAX_RECORDS = 16; - -function readLocalRefusals(metadata: Record): LocalRefusalRecord[] { - const raw = metadata.localRefusals; - if (!Array.isArray(raw)) return []; - return raw.filter((r): r is LocalRefusalRecord => - !!r && typeof r === "object" - && typeof (r as LocalRefusalRecord).origin === "string" - && typeof (r as LocalRefusalRecord).model === "string" - && typeof (r as LocalRefusalRecord).window === "number", - ); -} - -/** Record a local 413 refusal for (origin, model, window). A wider refusal in - * the same scope already dominates (it covers every tighter window); a - * narrower one replaces it so the record tracks the widest verdict seen. */ -function noteLocalRefusal(session: Session, origin: string | undefined, model: string | undefined, window: number): void { - if (!origin || !model || !Number.isFinite(window) || window <= 0) return; - const records = readLocalRefusals(session.metadata); - const existing = records.find((r) => r.origin === origin && r.model === model); - if (existing !== undefined && existing.window >= window) return; - session.metadata.localRefusals = [ - ...records.filter((r) => !(r.origin === origin && r.model === model)), - { origin, model, window: Math.floor(window), at: Date.now() }, - ].slice(-LOCAL_REFUSAL_MAX_RECORDS); - markDirty(session); -} - -/** True when this session was locally refused (413) by the SAME origin+model - * at an equal or WIDER window — the current request may then not reopen any - * forward-once escape hatch (#3846). Both sides must be known - * (evidence-permitlist discipline: an unprovenanced refusal proves nothing - * about an unprovenanced request, and vice versa). */ -function localRefusalCovers(metadata: Record, origin: string | undefined, model: string | undefined, window: number): boolean { - if (!origin || !model) return false; - return readLocalRefusals(metadata).some((r) => r.origin === origin && r.model === model && r.window >= window); -} +// [#3977 Step 1] the store now lives in src/size-evidence.ts — moved verbatim. /** #568: commit the response early so a long preflight cannot lose the client * to its header timeout. Streaming clients get an SSE stream with keep-alive @@ -3746,14 +3705,16 @@ export async function preflightCompressIfNeeded( // byte-relay lanes) — bili must not declare structural doom for it; those // lanes keep their 502 fail-fast contract where NOT re-forwarding IS the // protection, not a client spin. - const status = f?.kind === "upstream" && f.status === 429 ? 503 : f?.kind === "exhausted" && f.deterministic && !payloadFitsWindow ? 413 : 502; + // [#3977 Step 1] the classification expressions below moved verbatim to + // src/preflight-verdict.ts — pure functions, same inputs, same outputs. + const status = classifyStatus(f, payloadFitsWindow); // #3918: non-deterministic exhaustion WITH committed progress is not terminal — // the folds persist in the session, so the next attempt starts from a smaller // baseline and continues the fold. Reporting retryable=false told hosts to stop // spinning exactly where each retry makes real progress (incident: 64 effective // folds, every round declared terminal). Deterministic exhaustion keeps today's // verdict: nothing more can fold from this view, so retries are genuinely doomed. - const progressContinues = f?.kind === "exhausted" && !f.deterministic && result.compressedRanges > 0; + const progressContinues = verdictProgressContinues(f, result.compressedRanges); // #3846: a deterministic can-never-fit verdict is local refusal evidence for // this scope — persist it so the forward-once escapes above stay closed on // retry even after the baseline demotes to estimate or the body changes. @@ -3761,20 +3722,18 @@ export async function preflightCompressIfNeeded( // #2189: an explicit retryable=false from preflight (credential-shape // rejection — deterministic for the identical payload) overrides the // status-based heuristic below; every other path keeps today's mapping. - const retryable = progressContinues ? true : f?.retryable === false ? false : f?.retryable === true || (f?.kind === "upstream" && f.status !== undefined && (f.status === 429 || f.status >= 500)); + const retryable = classifyRetryable(progressContinues, f); let ffDetail = f?.detail ?? "the payload still exceeds the window after preflight compression"; if (progressContinues) { - const shortfall = foldedCalibratedSize !== undefined ? Math.max(0, limit - foldedCalibratedSize) : undefined; - ffDetail += ` ${result.compressedRanges} range(s) folded this turn persist in the session — the next attempt starts from a smaller baseline and continues the fold${shortfall !== undefined ? ` (~${shortfall.toLocaleString("en-US")} tokens short of the target)` : ""}.`; + ffDetail += progressDetailSuffix(result.compressedRanges, foldedCalibratedSize, limit); } const ff = failFast(status, ffDetail, retryable, result.compressedRanges > 0 ? foldedCalibratedSize : undefined, result.rangesRemaining); - const contentDeadEnd = f?.kind === "exhausted" || (f?.kind === "upstream" && f.status !== undefined && f.status >= 400 && f.status < 500 && !retryable); + const contentDeadEnd = isContentDeadEnd(f, retryable); if (contentDeadEnd && result.compressedRanges === 0) { const cooldownMs = preflightDeadEndCooldownMs(); if (cooldownMs > 0) { ff.message += ` Preflight will not call the upstream again for the next ${Math.max(1, Math.round(cooldownMs / 60_000))}m for this identical request. Change the request or wait for the cooldown before retrying.`; - session.metadata.preflightDeadEnd = { key: deadEndKey, until: Date.now() + cooldownMs, status: ff.status, retryable: ff.retryable, message: ff.message }; - markDirty(session); + armDeadEnd(session, { key: deadEndKey, until: Date.now() + cooldownMs, status: ff.status, retryable: ff.retryable, message: ff.message }); } } return ff; diff --git a/src/size-evidence.ts b/src/size-evidence.ts new file mode 100644 index 0000000000..75c6c0f304 --- /dev/null +++ b/src/size-evidence.ts @@ -0,0 +1,93 @@ +// #3977 Step 1 (pure extraction): the session's SIZE EVIDENCE stores get a +// single module. Knowledge that was scattered across server.ts inline blocks +// keeps its exact metadata keys, shapes, and semantics — this move changes +// WHERE the code lives, not what any decision observes. Later steps route the +// escape-hatch guards through fitEvidence()-style single adjudicators; those +// live here so a new hatch cannot forget a store that lives in another file. +// +// Evidence grades already latent in the codebase (#857 lastInputTokensSource, +// #3846 localRefusals, #1110 overflowArm): usage / overflow-arm / estimate / +// local-refusal. Step 2 makes them a first-class type over this module. + +import { markDirty, type Session } from "./session.js"; + +// --------------------------------------------------------------------------- +// Store: localRefusals (#3846) — persisted local 413 evidence +// --------------------------------------------------------------------------- + +// (comment block moved verbatim from server.ts with the store) +// probe / image arbitration / #1839 rebuilt-view divergence / #3146 +// fresh-session) re-bets the same estimator guess — production shape: three +// consecutive full-bill forwards of 606K/620K/633K-token payloads bili itself +// had already refused locally. Persist refusals as their own metadata channel +// — NEVER as usage (that field is upstream billing evidence only) — scoped to +// the route/model/window that saw them; an equal-or-wider refused window +// covers the current equal-or-tighter one, anything else evaluates fresh. +interface LocalRefusalRecord { + origin: string; + model: string; + window: number; + at: number; +} + +const LOCAL_REFUSAL_MAX_RECORDS = 16; + +function readLocalRefusals(metadata: Record): LocalRefusalRecord[] { + const raw = metadata.localRefusals; + if (!Array.isArray(raw)) return []; + return raw.filter((r): r is LocalRefusalRecord => + !!r && typeof r === "object" + && typeof (r as LocalRefusalRecord).origin === "string" + && typeof (r as LocalRefusalRecord).model === "string" + && typeof (r as LocalRefusalRecord).window === "number", + ); +} + +/** Record a local 413 refusal for (origin, model, window). A wider refusal in + * the same scope already dominates (it covers every tighter window); a + * narrower one replaces it so the record tracks the widest verdict seen. */ +export function noteLocalRefusal(session: Session, origin: string | undefined, model: string | undefined, window: number): void { + if (!origin || !model || !Number.isFinite(window) || window <= 0) return; + const records = readLocalRefusals(session.metadata); + const existing = records.find((r) => r.origin === origin && r.model === model); + if (existing !== undefined && existing.window >= window) return; + session.metadata.localRefusals = [ + ...records.filter((r) => !(r.origin === origin && r.model === model)), + { origin, model, window: Math.floor(window), at: Date.now() }, + ].slice(-LOCAL_REFUSAL_MAX_RECORDS); + markDirty(session); +} + +/** True when this session was locally refused (413) by the SAME origin+model + * at an equal or WIDER window — the current request may then not reopen any + * forward-once escape hatch (#3846). Both sides must be known + * (evidence-permitlist discipline: an unprovenanced refusal proves nothing + * about an unprovenanced request, and vice versa). */ +export function localRefusalCovers(metadata: Record, origin: string | undefined, model: string | undefined, window: number): boolean { + if (!origin || !model) return false; + return readLocalRefusals(metadata).some((r) => r.origin === origin && r.model === model && r.window >= window); +} + +// --------------------------------------------------------------------------- +// Store: preflightDeadEnd (#728) — identical-request cooldown evidence +// --------------------------------------------------------------------------- + +/** The persisted shape of the #728 dead-end cooldown: a fail-fast verdict for + * one exact request (sha256 of the inbound body keyed with model+window) + * that the upstream just rejected, held for a cooldown window so a spinning + * host cannot re-bill it. The READ side stays inline in server.ts this step: + * its replay semantics (status fallback, stale-key eviction) are exactly as + * rich as the block that owns them; the write side is owned here. */ +export interface PreflightDeadEndRecord { + key: string; + until: number; + status: number; + retryable: boolean; + message: string; +} + +/** Arm (or re-arm) the dead-end cooldown for one exact request. */ +export function armDeadEnd(session: Session, record: PreflightDeadEndRecord): void { + session.metadata.preflightDeadEnd = record; + markDirty(session); +} From 9151d22e43f941ad1ea6cb679d7cba912bc41fd5 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 20 Oct 2026 21:48:22 +0800 Subject: [PATCH 3/8] fix(#3977): drop export on internal-only verdict/evidence types (unused-exports gate) --- src/preflight-verdict.ts | 4 ++-- src/size-evidence.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/preflight-verdict.ts b/src/preflight-verdict.ts index e027e1cd11..b9e9488453 100644 --- a/src/preflight-verdict.ts +++ b/src/preflight-verdict.ts @@ -8,7 +8,7 @@ /** Structural slice of the walk outcome the classification needs. The walk * result itself stays internal to server.ts; only the fields the verdict * reads are required here, so this stays a pure, dependency-free module. */ -export interface VerdictWalkSlice { +interface VerdictWalkSlice { kind?: string; deterministic?: boolean; status?: number; @@ -16,7 +16,7 @@ export interface VerdictWalkSlice { detail?: string; } -export type PreflightStatus = 413 | 502 | 503; +type PreflightStatus = 413 | 502 | 503; /** (comment moved verbatim from server.ts) * #3461 problem 2: DETERMINISTIC exhaustion (futile-round math proved no diff --git a/src/size-evidence.ts b/src/size-evidence.ts index 75c6c0f304..87e01bc04a 100644 --- a/src/size-evidence.ts +++ b/src/size-evidence.ts @@ -78,7 +78,7 @@ export function localRefusalCovers(metadata: Record, origin: st * host cannot re-bill it. The READ side stays inline in server.ts this step: * its replay semantics (status fallback, stale-key eviction) are exactly as * rich as the block that owns them; the write side is owned here. */ -export interface PreflightDeadEndRecord { +interface PreflightDeadEndRecord { key: string; until: number; status: number; From 19bea6790ac43b1700e0dce9bca148e67a62c372 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 22:18:56 +0800 Subject: [PATCH 4/8] test(#3977): P-LEARN pin gains a never-saw-the-400 control session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review fix on the pin-first PR: the original P-LEARN compares two POST-400 titles, so a one-time uptake of the stated count (single k recalibration or a one-off overflow-arm floor) would leave them equal and pass vacuously — exactly the drift class step 4 flips. The control session refuses the same payload without ever seeing the 400; today both titles are byte-identical, and learnFromOutcome(overflow-400) must move the treated title away from it. --- tests/issue3977-size-evidence-pins.test.ts | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/issue3977-size-evidence-pins.test.ts b/tests/issue3977-size-evidence-pins.test.ts index 508ecffaab..e77bb4149f 100644 --- a/tests/issue3977-size-evidence-pins.test.ts +++ b/tests/issue3977-size-evidence-pins.test.ts @@ -307,3 +307,56 @@ test("pin #3977 P-LEARN: an upstream 400 that STATES the real token count teache await close(); } }); + +// P-LEARN's companion edge: the steady-state assertion above compares two +// POST-400 titles, so a ONE-TIME uptake of the stated count (k̂ recalibrated +// once, or a one-off overflow-arm floor) would leave them equal and sail +// through. The missing baseline is a session that NEVER saw the 400: today +// the treated and the control refuse the same payload with byte-identical +// titles; when step 4 makes the stated count calibration evidence, the +// treated title's numbers/clauses move away from the control. (@flip-target) +test("pin #3977 P-LEARN-b: refusal title after a stated-count 400 is byte-identical to a session that never saw the 400 (@flip-target for step 4)", async () => { + // Control: seed, then the oversized non-foldable payload — the unlearned + // reference title. + const upA = makeUpstream(); + const portA = await listen(upA); + const a = await setup({ [`http://127.0.0.1:${portA}`]: { models: { "gpt-test": { context: WINDOW } } } }); + let titleControl = ""; + try { + a.ctx.headers["x-acp-session"] = "p3977-learn-control"; + const rSeed = await post(a.ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }); + assert.equal(rSeed.status, 200, "the fitting seed forwards"); + await rSeed.text(); + const rBig = await post(a.ctx, { model: "gpt-test", stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); + assert.equal(rBig.status, 413, "oversized payload refused locally"); + titleControl = await rBig.text(); + } finally { + upA.server.close(); + upA.server.closeIdleConnections?.(); + await a.close(); + } + + // Treated: seed, detour into the #3900-shaped stated-count 400, then the + // SAME oversized payload. + const upB = makeUpstream(); + const portB = await listen(upB); + const b = await setup({ [`http://127.0.0.1:${portB}`]: { models: { "gpt-test": { context: WINDOW } } } }); + try { + b.ctx.headers["x-acp-session"] = "p3977-learn-treat"; + const rSeed = await post(b.ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }); + assert.equal(rSeed.status, 200, "the fitting seed forwards"); + await rSeed.text(); + upB.arm400(); + const rImg = await post(b.ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: imageContent() }] }); + assert.equal(rImg.status, 400, "the detour rides and gets the stated-count 400"); + await rImg.text(); + const rBig = await post(b.ctx, { model: "gpt-test", stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); + assert.equal(rBig.status, 413, "still refused locally"); + const titleTreated = await rBig.text(); + assert.equal(titleTreated, titleControl, "a one-time uptake of the stated count would move these numbers/clauses — today the 400 teaches nothing"); + } finally { + upB.server.close(); + upB.server.closeIdleConnections?.(); + await b.close(); + } +}); From 74499b5a01d4dd1e473742b0e1cba918469df038 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 20 Oct 2026 22:12:23 +0800 Subject: [PATCH 5/8] =?UTF-8?q?step=202:=20fitEvidence=20=E2=80=94=20the?= =?UTF-8?q?=20single=20adjudicator=20over=20persisted=20size=20evidence=20?= =?UTF-8?q?(#3977)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every forward-once escape hatch now consumes one adjudication (fitEvidence in size-evidence.ts) instead of a raw localRefusals read: localRefusalCovers becomes module-private, server.ts derives localRefusalHere from evidence.verdict === "cannot-fit". Zero behavior change by construction (the gate delegates to the exact predicate it replaced); the indirection is the contract — a store added in step 4+ lands in the adjudicator and every hatch consults it the same moment (invariant A of the ROOT-FIX plan). Verified: pins 4/4, #3846/#3918 families 9/9, tsc clean, docs/unused-exports gates clean. --- src/server.ts | 9 +++++++-- src/size-evidence.ts | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/server.ts b/src/server.ts index 29dc9651f2..8aa8858f07 100644 --- a/src/server.ts +++ b/src/server.ts @@ -8,7 +8,7 @@ import { performance } from "node:perf_hooks"; import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, type PackSurface, defaultPrompts, deactivateBlock } from "acp-kernel"; import { resolveCompress, resolveCompressPrompts, resolveCompressSurfaceDetailed, resolveRequestConfig, type ResolvedKernelConfig } from "./compress-settings.js"; import { dropCompressReasoningIds, type CompressReasoningConfig } from "./reasoning-drop.js"; -import { noteLocalRefusal, localRefusalCovers, armDeadEnd } from "./size-evidence.js"; +import { noteLocalRefusal, fitEvidence, armDeadEnd } from "./size-evidence.js"; import { classifyStatus, progressContinues as verdictProgressContinues, classifyRetryable, progressDetailSuffix, isContentDeadEnd } from "./preflight-verdict.js"; import type { ProxyOptions, ResignSettings } from "./config.js"; export type { ProxyOptions } from "./config.js"; @@ -3208,7 +3208,12 @@ export async function preflightCompressIfNeeded( // payloads are unaffected (they never reach an escape hatch); another // origin/model or a larger window judges fresh, exactly like the #1933 F2 // baseline demotion does. - const localRefusalHere = localRefusalCovers(session.metadata, currentOrigin, model, limit); + // [#3977 Step 2] the evidence gate: every forward-once escape hatch below + // consumes THIS adjudication (invariant A) — the localRefusals store is + // reached only through fitEvidence, so a store added later is consulted by + // every hatch the moment it lands in the adjudicator. + const evidence = fitEvidence(session.metadata, currentOrigin, model, limit); + const localRefusalHere = evidence.verdict === "cannot-fit"; // #2313: an estimate may not block the forward. The trigger above can // fire on the calibrated chars/4 estimate alone; when the current // upstream's billing scale diverges from the estimator's caliber diff --git a/src/size-evidence.ts b/src/size-evidence.ts index 87e01bc04a..cb0c96b637 100644 --- a/src/size-evidence.ts +++ b/src/size-evidence.ts @@ -11,6 +11,39 @@ import { markDirty, type Session } from "./session.js"; +// --------------------------------------------------------------------------- +// Invariant A (#3977): every forward-once escape hatch must consult +// fitEvidence() — the SINGLE adjudicator over the persisted size-evidence +// stores. A hatch that bypasses this gate re-bets an estimator guess against +// evidence bili already holds (the #3846 lesson: the probe hatch could not +// consult localRefusals because the store did not exist yet — a gate that +// enumerates stores by construction cannot "forget" one added later). +// --------------------------------------------------------------------------- + +/** The three-way adjudication every escape hatch (and later every preflight + * exit) consumes: cannot-fit = persisted hard evidence this (origin, model) + * cannot accept this window; can-fit = persisted evidence it can; unknown = + * no persisted evidence (estimates are grades, not verdicts — they may only + * refine an unknown, never manufacture a can/cannot). */ +type EvidenceVerdict = "can-fit" | "cannot-fit" | "unknown"; + +interface EvidenceAdjudication { + verdict: EvidenceVerdict; + /** Which store decided, for the incident log. */ + reason: string; +} + +/** Adjudicate the persisted size evidence for (origin, model, window). + * Stores consulted today: localRefusals (#3846). Step 4+ adds the + * overflow-arm family — a new store lands HERE and every hatch inherits it + * the same moment, which is the whole point of the gate. */ +export function fitEvidence(metadata: Record, origin: string | undefined, model: string | undefined, window: number): EvidenceAdjudication { + if (localRefusalCovers(metadata, origin, model, window)) { + return { verdict: "cannot-fit", reason: "local-413 at an equal or wider window (#3846)" }; + } + return { verdict: "unknown", reason: "no persisted evidence for this scope" }; +} + // --------------------------------------------------------------------------- // Store: localRefusals (#3846) — persisted local 413 evidence // --------------------------------------------------------------------------- @@ -62,8 +95,10 @@ export function noteLocalRefusal(session: Session, origin: string | undefined, m * at an equal or WIDER window — the current request may then not reopen any * forward-once escape hatch (#3846). Both sides must be known * (evidence-permitlist discipline: an unprovenanced refusal proves nothing - * about an unprovenanced request, and vice versa). */ -export function localRefusalCovers(metadata: Record, origin: string | undefined, model: string | undefined, window: number): boolean { + * about an unprovenanced request, and vice versa). + * [#3977 Step 2] module-private: hatches consume fitEvidence()/ + * escapeHatchOpen(), never a raw store read. */ +function localRefusalCovers(metadata: Record, origin: string | undefined, model: string | undefined, window: number): boolean { if (!origin || !model) return false; return readLocalRefusals(metadata).some((r) => r.origin === origin && r.model === model && r.window >= window); } From bc04fe97df900e27ff7cf4bfb319cedd9bd2334a Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 20 Oct 2026 22:14:04 +0800 Subject: [PATCH 6/8] step 4a: extract learnCalibrationSample from settleUsageReport (#3977) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ring/publish logic moves verbatim into its own function pairing a REAL measured token count for the payload just sent against the pending local estimate recorded at prepare time. settleUsageReport now delegates — behavior identical (calibration families 35/35 green). Step 4b will feed the same learner from armOverflowShrink: a 400 body that STATES its counts ("N in the messages") is the same physical event as a usage report — upstream measured a payload bili estimated (#3900: the k-hat loop currently only learns from successes). --- src/cache-ledger.ts | 144 +++++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 63 deletions(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 8d9486886b..87d962d508 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1424,6 +1424,83 @@ export function canonicalSysFpForSettle(session: Session, protocol?: string): st return typeof noted?.fp === "string" && noted.fp !== "" ? noted.fp : undefined; } +/** [#3977 Step 4a] The one learner: pair a REAL measured token count for + * the payload bili just sent (`actualTotal`) against the local text estimate + * recorded for THAT payload at prepare time (the pending pair), and update + * the per-(origin, model) calibration ring. Admitted samples (plausibility + * band ×[0.2, 5]) accumulate; the factor publishes only when ≥2 recent + * samples agree within ×2 and clears when they stop agreeing. Extracted + * verbatim from settleUsageReport — the usage-report path and the step-4b + * overflow-400 path (stated counts: "N in the messages") are the same + * physical event: upstream MEASURED a payload bili estimated. + * + * Callers gate `actualTotal > 0` themselves (#793 placeholder discipline). */ +export function learnCalibrationSample( + session: Session, + settleOrigin: string | undefined, + actualTotal: number, +): void { + // #3211: per-route calibration policy (providers[].calibration), + // paired by handle.ts with the SAME latest-wins/origin-gated discipline + // as the pending estimate — applied below at PUBLICATION time only, so + // the learned ring stays raw and auditable. Origin mismatch (slot set + // by a request on another route) voids it for this settle. + const pendingCalibOrigin = session.stats.lastCalibrationPolicyOrigin !== undefined ? normalizeUpstreamOrigin(session.stats.lastCalibrationPolicyOrigin) : undefined; + const calibPolicy = pendingCalibOrigin === settleOrigin ? session.stats.lastCalibrationPolicy : undefined; + const pendingEst = session.stats.lastLocalTextEstimate; + const pendingOrigin = normalizeUpstreamOrigin(session.stats.lastLocalTextEstimateOrigin); + if (pendingEst !== undefined && pendingEst >= CALIBRATION_MIN_ESTIMATE && settleOrigin !== undefined && pendingOrigin === settleOrigin) { + const rawSample = actualTotal / pendingEst; + // #2117 B: the ring is keyed by route AND model — tokenizers bill + // differently across models, so a mid-session model switch starts a + // FRESH ring instead of blending two models' billing scales into one + // factor. Missing model info NEVER invalidates (same discipline as + // currentCalibrationFactor): a ring without a model key — legacy + // pre-upgrade files included, and records restored after a restart + // whose load whitelist drops the model fields (#2141) — continues + // and ADOPTS the first known model. Discarding it would clear a + // provenance-matched published k̂ on the first post-restart settle + // (the #2129 regression class). + const settleModel = typeof session.metadata?.lastModel === "string" && session.metadata.lastModel !== "" ? session.metadata.lastModel : undefined; + let ring = session.stats.calibrationRing; + if (!ring || ring.origin !== settleOrigin || (ring.model !== undefined && settleModel !== undefined && ring.model !== settleModel)) { + ring = { origin: settleOrigin, model: settleModel, values: [] }; + } else if (ring.model === undefined && settleModel !== undefined) { + ring.model = settleModel; + } + if (rawSample >= CALIBRATION_SAMPLE_MIN && rawSample <= CALIBRATION_SAMPLE_MAX) { + ring.values.push(rawSample); + if (ring.values.length > CALIBRATION_SAMPLE_WINDOW) ring.values.shift(); + const spread = Math.max(...ring.values) / Math.min(...ring.values); + if (ring.values.length >= 2 && spread <= CALIBRATION_CONSISTENCY_RATIO) { + const mean = ring.values.reduce((a, b) => a + b, 0) / ring.values.length; + if (calibPolicy?.disabled === true) { + // #3211: route-level opt-out — the learned value stays in + // the ring (auditable) but is never applied on this route. + delete session.stats.calibratedEstimate; + delete session.stats.calibratedEstimateOrigin; + delete session.stats.calibratedEstimateModel; + } else { + const clamped = Math.min(CALIBRATION_CLAMP_MAX, Math.max(CALIBRATION_CLAMP_MIN, mean)); + session.stats.calibratedEstimate = calibPolicy?.max !== undefined ? Math.min(clamped, calibPolicy.max) : clamped; + session.stats.calibratedEstimateOrigin = settleOrigin; + // Provenance is the ring's model key, not this sample's stamp — + // an unknown-model settle continues a model-keyed ring without + // blanking the established provenance. Stays inside the publish + // branch: the disabled path above clears all three fields and + // must not re-stamp one of them (#3211 review). + session.stats.calibratedEstimateModel = ring.model; + } + } else { + delete session.stats.calibratedEstimate; + delete session.stats.calibratedEstimateOrigin; + delete session.stats.calibratedEstimateModel; + } + } + session.stats.calibrationRing = ring; + } +} + export function settleUsageReport( session: Session, s: { total: number; reportedCached: number | null; output?: number; protocol?: string; upstream?: string }, @@ -1457,69 +1534,10 @@ export function settleUsageReport( if (settleOrigin !== undefined) session.stats.lastInputTokensOrigin = settleOrigin; // #1933 F1: consume the pending pair — the local text estimate of the // payload THIS report bills, recorded at prepare time. Same route - // required. Admitted samples (plausibility band) accumulate in a - // per-origin ring; the factor is published only once ≥2 recent samples - // agree within ×2, and cleared again when they stop agreeing — until - // then the raw estimate decides (legacy behavior). - // #3211: per-route calibration policy (providers[].calibration), - // paired by handle.ts with the SAME latest-wins/origin-gated discipline - // as the pending estimate — applied below at PUBLICATION time only, so - // the learned ring stays raw and auditable. Origin mismatch (slot set - // by a request on another route) voids it for this settle. - const pendingCalibOrigin = session.stats.lastCalibrationPolicyOrigin !== undefined ? normalizeUpstreamOrigin(session.stats.lastCalibrationPolicyOrigin) : undefined; - const calibPolicy = pendingCalibOrigin === settleOrigin ? session.stats.lastCalibrationPolicy : undefined; - const pendingEst = session.stats.lastLocalTextEstimate; - const pendingOrigin = normalizeUpstreamOrigin(session.stats.lastLocalTextEstimateOrigin); - if (pendingEst !== undefined && pendingEst >= CALIBRATION_MIN_ESTIMATE && settleOrigin !== undefined && pendingOrigin === settleOrigin) { - const rawSample = s.total / pendingEst; - // #2117 B: the ring is keyed by route AND model — tokenizers bill - // differently across models, so a mid-session model switch starts a - // FRESH ring instead of blending two models' billing scales into one - // factor. Missing model info NEVER invalidates (same discipline as - // currentCalibrationFactor): a ring without a model key — legacy - // pre-upgrade files included, and records restored after a restart - // whose load whitelist drops the model fields (#2141) — continues - // and ADOPTS the first known model. Discarding it would clear a - // provenance-matched published k̂ on the first post-restart settle - // (the #2129 regression class). - const settleModel = typeof session.metadata?.lastModel === "string" && session.metadata.lastModel !== "" ? session.metadata.lastModel : undefined; - let ring = session.stats.calibrationRing; - if (!ring || ring.origin !== settleOrigin || (ring.model !== undefined && settleModel !== undefined && ring.model !== settleModel)) { - ring = { origin: settleOrigin, model: settleModel, values: [] }; - } else if (ring.model === undefined && settleModel !== undefined) { - ring.model = settleModel; - } - if (rawSample >= CALIBRATION_SAMPLE_MIN && rawSample <= CALIBRATION_SAMPLE_MAX) { - ring.values.push(rawSample); - if (ring.values.length > CALIBRATION_SAMPLE_WINDOW) ring.values.shift(); - const spread = Math.max(...ring.values) / Math.min(...ring.values); - if (ring.values.length >= 2 && spread <= CALIBRATION_CONSISTENCY_RATIO) { - const mean = ring.values.reduce((a, b) => a + b, 0) / ring.values.length; - if (calibPolicy?.disabled === true) { - // #3211: route-level opt-out — the learned value stays in - // the ring (auditable) but is never applied on this route. - delete session.stats.calibratedEstimate; - delete session.stats.calibratedEstimateOrigin; - delete session.stats.calibratedEstimateModel; - } else { - const clamped = Math.min(CALIBRATION_CLAMP_MAX, Math.max(CALIBRATION_CLAMP_MIN, mean)); - session.stats.calibratedEstimate = calibPolicy?.max !== undefined ? Math.min(clamped, calibPolicy.max) : clamped; - session.stats.calibratedEstimateOrigin = settleOrigin; - // Provenance is the ring's model key, not this sample's stamp — - // an unknown-model settle continues a model-keyed ring without - // blanking the established provenance. Stays inside the publish - // branch: the disabled path above clears all three fields and - // must not re-stamp one of them (#3211 review). - session.stats.calibratedEstimateModel = ring.model; - } - } else { - delete session.stats.calibratedEstimate; - delete session.stats.calibratedEstimateOrigin; - delete session.stats.calibratedEstimateModel; - } - } - session.stats.calibrationRing = ring; - } + // required. [#3977 Step 4a] the ring lives in learnCalibrationSample; + // usage reports and step-4b overflow-400 stated counts feed the same + // learner. + learnCalibrationSample(session, settleOrigin, s.total); // #1820: a real usage-grade sample supersedes the post-rebuild meter // anchor immediately — no need to wait out its prepare budget. clearPostRebuildAnchor(session); From a26d604e21ce075f12ba9553cbdb9257b0dffd9a Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 20 Oct 2026 22:33:41 +0800 Subject: [PATCH 7/8] =?UTF-8?q?step=204b:=20learnFromOutcome(overflow-400)?= =?UTF-8?q?=20=E2=80=94=20stated=20counts=20feed=20the=20calibration=20rin?= =?UTF-8?q?g=20(#3977/#3900)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit armOverflowShrink now calls learnCalibrationSample when the 400 body STATES the rejected payload's counts ("N in the messages, K in the completion"): upstream MEASURED a payload bili estimated — the same physical event as a usage report. The messages share is the estimator's input domain (the completion share is a reservation, never a sample). The ring's existing guards do all the gating: pending pair set by THIS request at prepare, same route (origin-gated), plausibility band x[0.2,5], >=2 agreeing samples within x2 before k-hat publishes, per-(origin,model) keying, #3211 route policy at publication. The arm (#3450) and the learner never collide — the arm writes lastInputTokens, the learner reads the pending-estimate pair; and a successful usage report retires the arm (#1110) so the loop converges instead of oscillating. P-LEARN pin flipped with the behavior, both directions pinned: - two agreeing stated-count 400s (via the image-arb ride, recovery requests retiring the arm between detours) publish k-hat ~2.2 and flip an under-window 12K-token payload from forward-direct to fold-first (real summary call before the forward); - a window-only 400 (no per-part counts) arms but never teaches: no samples, no publication, the same payload still forwards directly. Families unchanged through the hook: stated-count + calibration suites 74/74 (their single-shot 400s never reach >=2 agreeing samples), pins 4/4, tsc clean, docs/unused-exports gates clean. --- src/server/relay.ts | 17 ++- tests/issue3977-size-evidence-pins.test.ts | 133 ++++++++++++++++----- 2 files changed, 122 insertions(+), 28 deletions(-) diff --git a/src/server/relay.ts b/src/server/relay.ts index b31bc9cbd0..c440380431 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -9,7 +9,7 @@ import { type CompressionCore, type Config, type CoreMessage, defaultCountTokens import { type BiliMessage } from "acp-kernel/wire"; import { absorbToolName, applyAbsorbView } from "../absorb.js"; import { APIG_RESIGN_SCHEME, resignApig } from "../apig-resign.js"; -import { credentialFingerprint, generationParamFp, noteClientAbort, noteForwardedBody, noteForwardedImageFacts, noteInboundMessages, settleUsageReport } from "../cache-ledger.js"; +import { credentialFingerprint, generationParamFp, learnCalibrationSample, noteClientAbort, noteForwardedBody, noteForwardedImageFacts, noteInboundMessages, settleUsageReport } from "../cache-ledger.js"; import { warnCacheCollapse } from "../cache-warn.js"; import { stampOutbound } from "../chain-checkpoint.js"; import { applyCompatDropFields, dropCompatFieldsJson, resolveCompatDropFields } from "../compat-drop.js"; @@ -1084,6 +1084,21 @@ export async function forward( log("warn", `[${s.id}] upstream-stated window ${info.window} differs from the window in use (${declaredWindow}, model=${reqModel ?? "unknown"}) — folding targets min of the two; set compress.modelContextLimit if the declaration is wrong (#3450)`); } log("warn", `[${s.id}] upstream context overflow (model=${reqModel ?? "unknown"}) — window ${info.window} stated upstream${measured !== undefined ? `, rejected payload ~${measured} tokens stated` : ""}${reserve !== undefined ? `, completion reservation ${reserve} tokens` : ""}; armed emergency shrink at ${armedBaseline}${armedBaseline < unclampedBaseline ? ` (clamped to the judging limit ${judgingLimit}, #3738)` : ""}, declared window unchanged (#987)`); + // [#3977 Step 4b / #3900] learnFromOutcome(overflow-400): a body + // that STATES the rejected payload's counts ("N in the messages") + // is the same physical event as a usage report — upstream + // MEASURED a payload bili estimated. Feed the calibration ring + // the same learner a usage settle feeds; the ring's own guards + // (pending pair set by THIS request at prepare, same route, + // plausibility band, ≥2 agreeing samples) decide publication. + // The messages share is the INPUT domain of the text estimator — + // the completion share is a reservation, not input text, so it + // never enters the sample. Note lastInputTokens was armed above + // (#3450); the learner reads the PENDING ESTIMATE pair, not the + // arm, so the two never collide. + if (measured !== undefined) { + learnCalibrationSample(s, normalizeUpstreamOrigin(upstreamUrl), measured); + } } else { // No window number stated — nothing to learn (and #987 // removed the learner anyway), but the rejection itself is diff --git a/tests/issue3977-size-evidence-pins.test.ts b/tests/issue3977-size-evidence-pins.test.ts index e77bb4149f..2b9ebe5a44 100644 --- a/tests/issue3977-size-evidence-pins.test.ts +++ b/tests/issue3977-size-evidence-pins.test.ts @@ -15,9 +15,10 @@ // zero upstream calls (the #3846 guard on the image hatch). // P-DIV edge: a payload that FOLDS under the window after a local 413 — // current post-refusal fold semantics, observed and pinned. -// P-LEARN boundary: an upstream 400 whose body states the real token count -// teaches the estimator NOTHING today (titles byte-identical) — -// @flip-target for learnFromOutcome(overflow-400). +// P-LEARN boundary (FLIPPED in step 4b): two agreeing 400s that STATE the +// rejected payload's counts ("N in the messages") publish a k-hat +// and flip an under-window payload from forward-direct to +// fold-first; a window-only 400 arms but never teaches. import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; @@ -68,6 +69,33 @@ function foldableHistory(): Array<{ role: string; content: string }> { return messages; } +// The P-LEARN detour payload: a ~60KB base64 image plus ~24KB of core text +// so the prepare-time estimate (~21.7K tokens: the local estimator bills the +// base64 chars/4 like text) clears the CALIBRATION_MIN_ESTIMATE=2000 gate. +// The upstream bills the image FAR above the local chars/4 caliber (the +// #3900 production story: stated 60000 vs local ≈ 21.7K → ratio ≈ 2.8, inside +// the plausibility band [0.2, 5]). Detour 2 rides because the recovery +// request retires detour 1's arm (#1110), not because of these sizes. +function detourContent(): unknown { + return [ + { type: "text", text: "describe this" }, + { type: "image_url", image_url: { url: `data:image/png;base64,${"A".repeat(60_000)}` } }, + { type: "text", text: "detour load: " + "z".repeat(24_000) }, + ]; +} + +// The P-LEARN flip payload: six foldable ~8KB turns ≈ 12K raw tokens — UNDER +// the 20K window raw (forwards directly unlearned, see the control), but OVER +// it once the learned k-hat (≈2.8) calibrates the estimate → fold-first. +function underWindowFoldable(): Array<{ role: string; content: string }> { + const messages: Array<{ role: string; content: string }> = []; + for (let i = 0; i < 6; i++) { + messages.push({ role: i % 2 === 0 ? "user" : "assistant", content: `flip turn ${i} load: ` + `w${i}`.repeat(4_000) }); + } + messages.push({ role: "user", content: "final question" }); + return messages; +} + type Call = { summary: boolean; stream: boolean; contentChars: number }; function chatSse(text: string, usage?: { prompt_tokens: number; completion_tokens: number }): string { @@ -77,11 +105,14 @@ function chatSse(text: string, usage?: { prompt_tokens: number; completion_token return chunk({ role: "assistant" }, null) + chunk({ content: text }, null) + chunk({}, "stop", u) + "data: [DONE]\n\n"; } -// Upstream that can be flipped to answer the NEXT forwarded request with the -// production-shaped 400 that STATES the real token count in its body. -function makeUpstream(): { server: http.Server; calls: Call[]; arm400: () => void } { +// Upstream that can be flipped to answer the NEXT forwarded request with a +// 400. Two shapes: the window-only body (no per-part counts — arms but never +// teaches) and the #3900/#3738 stated-count body ("N in the messages, K in the +// completion") that step 4b's learnFromOutcome consumes. +function makeUpstream(): { server: http.Server; calls: Call[]; arm400: () => void; arm400Stated: () => void } { const calls: Call[] = []; let arm400 = false; + let armStated = false; const server = http.createServer((req, res) => { const chunks: Buffer[] = []; req.on("data", (c: Buffer) => chunks.push(c)); @@ -97,6 +128,12 @@ function makeUpstream(): { server: http.Server; calls: Call[]; arm400: () => voi res.end(JSON.stringify({ id: "chatcmpl-sum", object: "chat.completion", model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: SUMMARY_TEXT }, finish_reason: "stop" }] })); return; } + if (armStated) { + armStated = false; + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: "This model's maximum context length is 20000 tokens. However, you requested 70000 tokens (60000 in the messages, 10000 in the completion). Please reduce the length of the messages or completion.", type: "invalid_request_error" } })); + return; + } if (arm400) { arm400 = false; res.writeHead(400, { "content-type": "application/json" }); @@ -111,7 +148,7 @@ function makeUpstream(): { server: http.Server; calls: Call[]; arm400: () => voi } }); }); - return { server, calls, arm400: () => { arm400 = true; } }; + return { server, calls, arm400: () => { arm400 = true; }, arm400Stated: () => { armStated = true; } }; } type Upstream = ReturnType; @@ -265,42 +302,84 @@ test("pin #3977 P-DIV: after a scoped local 413, a payload that folds under the } }); -test("pin #3977 P-LEARN: an upstream 400 that STATES the real token count teaches the estimator NOTHING today — titles byte-identical (@flip-target for step 4)", async () => { +test("pin #3977 P-LEARN (step-4 flip): two agreeing stated-count 400s teach the ring and flip an under-window payload to fold-first; a window-only 400 never teaches", async () => { const up = makeUpstream(); const port = await listen(up); const { ctx, close } = await setup({ [`http://127.0.0.1:${port}`]: { models: { "gpt-test": { context: WINDOW } } } }); try { + // ---------------- control session: a window-only 400 arms but teaches + // nothing. (One detour is the honest control: the second would be + // locally 413'd by the arm it just set — that refusal is itself the + // emergency shrink working, not a learning event.) + ctx.headers["x-acp-session"] = "p3977-learn-ctl"; + const rSeedC = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }); + assert.equal(rSeedC.status, 200, "control seed forwards"); + await rSeedC.text(); + up.arm400(); + const rCtl1 = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: detourContent() }] }); + assert.equal(rCtl1.status, 400, "control detour rides and gets the window-only 400"); + await rCtl1.text(); + const ctlSession = getSession("p3977-learn-ctl"); + // A later out-of-band usage settle may legitimately have created an + // EMPTY ring (current settleUsageReport semantics); the pin is that no + // SAMPLE was ever collected and nothing was published from the 400. + assert.ok(!ctlSession?.stats.calibrationRing || ctlSession.stats.calibrationRing.values.length === 0, `window-only 400s never collect a sample (ring ${JSON.stringify(ctlSession?.stats.calibrationRing)})`); + assert.equal(ctlSession?.stats.calibratedEstimate, undefined, "window-only 400s publish nothing"); + const summariesBeforeCtl = up.calls.filter((c) => c.summary).length; + const rCtlB = await post(ctx, { model: "gpt-test", stream: true, messages: underWindowFoldable() }); + assert.equal(rCtlB.status, 200, "control under-window payload forwards"); + await rCtlB.text(); + assert.equal(up.calls.filter((c) => c.summary).length, summariesBeforeCtl, "control: no fold happened — the arm alone changes nothing below the window"); + + // ---------------- learn session: two agreeing stated-count 400s ctx.headers["x-acp-session"] = "p3977-learn"; const rSeed = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }); assert.equal(rSeed.status, 200, "the fitting seed forwards"); await rSeed.text(); - // Detour FIRST (before any local refusal exists — P-IMG-b proved the // evidence would block the ride): the image payload rides by - // arbitration and the upstream answers with the #3900-shaped 400 whose - // body STATES the real token count. - up.arm400(); - const rImg = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: imageContent() }] }); - const imgStatus = rImg.status; - await rImg.text(); - // OBSERVED on the integration branch (550860ed): the image payload - // rides and the upstream answers the stated-count 400 verbatim. - assert.equal(imgStatus, 400, "the detour payload rides and is answered with the stated-count 400"); - assert.equal(up.calls.filter((c) => c.stream).length >= 2, true, "the detour payload reached the upstream"); + // arbitration and the upstream answers with the #3738-shaped 400 whose + // body STATES the rejected payload's own counts. Two of them = two + // agreeing samples = a published k-hat. + for (let i = 0; i < 2; i++) { + up.arm400Stated(); + const rImg = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: detourContent() }] }); + const imgBody = await rImg.text(); + assert.equal(rImg.status, 400, `stated-count detour ${i + 1} rides and is answered with the counted 400 — got: ${imgBody.slice(0, 400)}`); + // #1110 recovery between detours: a successful usage report + // retires the one-shot arm, else detour 2 is locally 413'd by the + // very baseline detour 1 armed (#3450/#3738 — the emergency shrink + // doing its job). The tiny payload's settle never touches the ring + // (pendingEst < CALIBRATION_MIN_ESTIMATE). + const rRec = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "recovery ping" }] }); + assert.equal(rRec.status, 200, `recovery ${i + 1} forwards and retires the arm`); + await rRec.text(); + } + const learnSession = getSession("p3977-learn"); + const ring = learnSession?.stats.calibrationRing; + assert.ok(ring && ring.values.length === 2, `the ring collected both samples (${JSON.stringify(ring?.values)})`); + const k = learnSession?.stats.calibratedEstimate; + assert.ok(typeof k === "number" && k >= 2 && k <= 2.5, `k-hat published from the stated counts (got ${k})`); + assert.equal(learnSession?.stats.calibratedEstimateModel, "gpt-test", "published factor is model-keyed"); - // Baseline title for the oversized non-foldable payload, AFTER the - // stated-count 400 has had every chance to arm/teach. + // THE FLIP: the same under-window payload that forwarded directly in + // the control session now judges OVER the window once calibrated — + // preflight folds it first and the forward happens after the summary. + const summariesBefore = up.calls.filter((c) => c.summary).length; + const rB = await post(ctx, { model: "gpt-test", stream: true, messages: underWindowFoldable() }); + assert.equal(rB.status, 200, "the calibrated over-window payload folds and forwards"); + await rB.text(); + assert.ok(up.calls.filter((c) => c.summary).length > summariesBefore, "the fold ran a real summary call first (fold-first, no forward-then-refold)"); + + // The oversized NON-FOLDABLE payload stays refused — and the dead-end + // replay (second identical request) returns the stored title verbatim. const rBig1 = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); - assert.equal(rBig1.status, 413, "oversized payload refused locally"); + assert.equal(rBig1.status, 413, "oversized payload still refused locally"); const title1 = await rBig1.text(); - - // The oversized non-foldable payload again: with learnFromOutcome - // (step 4) the stated-count 400 becomes calibration evidence and the - // numbers in this title MOVE. Today they must not. const rBig2 = await post(ctx, { model: "gpt-test", stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); assert.equal(rBig2.status, 413, "still refused locally"); const title2 = await rBig2.text(); - assert.equal(title1, title2, "the stated-count 400 changed nothing in the estimator's verdict today"); + assert.equal(title1, title2, "the dead-end replay returns the stored refusal verbatim"); } finally { up.server.close(); up.server.closeIdleConnections?.(); From 45be60b4c2201bb16e93f35cfed846fde22bed40 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 20 Oct 2026 22:36:53 +0800 Subject: [PATCH 8/8] =?UTF-8?q?test(#3977):=20upgrade=20P-LEARN-b=20for=20?= =?UTF-8?q?the=20step-4b=20flip=20=E2=80=94=20both=20directions=20pinned?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review fix 19bea679's premise (a stated-count 400 must move the treated title) meets the flipped semantics: publication needs TWO agreeing stated samples, and its fixture 400 was the window-only shape. (i) window-only 400: arms but never teaches — title stays byte-identical to the never-saw-400 control (the original assertion, preserved); (ii) two stated-count 400s: k-hat publishes, the calibrated estimate scales, the refusal title MOVES away from the control — and the payload still never reaches the upstream. Pins 5/5, families 83/83, tsc clean, gates clean. --- tests/issue3977-size-evidence-pins.test.ts | 60 +++++++++++++++++----- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/tests/issue3977-size-evidence-pins.test.ts b/tests/issue3977-size-evidence-pins.test.ts index 2b9ebe5a44..992962634a 100644 --- a/tests/issue3977-size-evidence-pins.test.ts +++ b/tests/issue3977-size-evidence-pins.test.ts @@ -387,14 +387,14 @@ test("pin #3977 P-LEARN (step-4 flip): two agreeing stated-count 400s teach the } }); -// P-LEARN's companion edge: the steady-state assertion above compares two -// POST-400 titles, so a ONE-TIME uptake of the stated count (k̂ recalibrated -// once, or a one-off overflow-arm floor) would leave them equal and sail -// through. The missing baseline is a session that NEVER saw the 400: today -// the treated and the control refuse the same payload with byte-identical -// titles; when step 4 makes the stated count calibration evidence, the -// treated title's numbers/clauses move away from the control. (@flip-target) -test("pin #3977 P-LEARN-b: refusal title after a stated-count 400 is byte-identical to a session that never saw the 400 (@flip-target for step 4)", async () => { +// P-LEARN's companion edge (review fix 19bea679, upgraded for the step-4b +// flip): the refusal TITLE is where a one-time uptake of the 400 would show. +// Two directions, both pinned against a session that NEVER saw any 400: +// (i) a window-only 400 (no per-part counts) never teaches — byte-identical +// title, exactly the pre-flip world the original pin captured; +// (ii) TWO agreeing stated-count 400s publish k-hat and the treated title's +// estimate numbers move AWAY from the control — the flip, observed. +test("pin #3977 P-LEARN-b: a window-only 400 never moves the refusal title; two stated-count 400s do (k-hat recalibrates the quoted estimate)", async () => { // Control: seed, then the oversized non-foldable payload — the unlearned // reference title. const upA = makeUpstream(); @@ -415,27 +415,61 @@ test("pin #3977 P-LEARN-b: refusal title after a stated-count 400 is byte-identi await a.close(); } - // Treated: seed, detour into the #3900-shaped stated-count 400, then the - // SAME oversized payload. + // (i) Window-only 400: arms the shrink but carries no per-part counts — + // nothing for the learner to pair. The title must stay byte-identical. const upB = makeUpstream(); const portB = await listen(upB); const b = await setup({ [`http://127.0.0.1:${portB}`]: { models: { "gpt-test": { context: WINDOW } } } }); try { - b.ctx.headers["x-acp-session"] = "p3977-learn-treat"; + b.ctx.headers["x-acp-session"] = "p3977-learn-treat-wo"; const rSeed = await post(b.ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }); assert.equal(rSeed.status, 200, "the fitting seed forwards"); await rSeed.text(); upB.arm400(); const rImg = await post(b.ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: imageContent() }] }); - assert.equal(rImg.status, 400, "the detour rides and gets the stated-count 400"); + assert.equal(rImg.status, 400, "the detour rides and gets the window-only 400"); await rImg.text(); const rBig = await post(b.ctx, { model: "gpt-test", stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); assert.equal(rBig.status, 413, "still refused locally"); const titleTreated = await rBig.text(); - assert.equal(titleTreated, titleControl, "a one-time uptake of the stated count would move these numbers/clauses — today the 400 teaches nothing"); + assert.equal(titleTreated, titleControl, "a 400 without per-part counts never teaches the estimator"); } finally { upB.server.close(); upB.server.closeIdleConnections?.(); await b.close(); } + + // (ii) Two agreeing stated-count 400s (recovery between them retires the + // one-shot arm, #1110): k-hat publishes, the big payload's quoted + // estimate scales, and the refusal title MOVES away from the control. + const upC = makeUpstream(); + const portC = await listen(upC); + const c = await setup({ [`http://127.0.0.1:${portC}`]: { models: { "gpt-test": { context: WINDOW } } } }); + try { + c.ctx.headers["x-acp-session"] = "p3977-learn-treat-sc"; + const rSeed = await post(c.ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }); + assert.equal(rSeed.status, 200, "the fitting seed forwards"); + await rSeed.text(); + for (let i = 0; i < 2; i++) { + upC.arm400Stated(); + const rImg = await post(c.ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: imageContent() }] }); + assert.equal(rImg.status, 400, `stated-count detour ${i + 1} rides and gets the counted 400`); + await rImg.text(); + const rRec = await post(c.ctx, { model: "gpt-test", stream: true, messages: [{ role: "user", content: "recovery ping" }] }); + assert.equal(rRec.status, 200, `recovery ${i + 1} retires the arm`); + await rRec.text(); + } + const treated = getSession("p3977-learn-treat-sc"); + assert.ok(typeof treated?.stats.calibratedEstimate === "number", "k-hat published from the two stated-count 400s"); + const streamsBeforeBig = upC.calls.filter((x) => x.stream).length; + const rBig = await post(c.ctx, { model: "gpt-test", stream: true, messages: [{ role: "system", content: bigSystem() }, { role: "user", content: "q" }] }); + assert.equal(rBig.status, 413, "the calibrated payload is still refused locally (it was always over the window)"); + const titleTreated = await rBig.text(); + assert.notEqual(titleTreated, titleControl, "the learned k-hat recalibrates the quoted estimate — the title moves away from the never-saw-400 control"); + assert.equal(upC.calls.filter((x) => x.stream).length, streamsBeforeBig, "the oversized payload never reached the upstream"); + } finally { + upC.server.close(); + upC.server.closeIdleConnections?.(); + await c.close(); + } });