diff --git a/src/preflight-verdict.ts b/src/preflight-verdict.ts new file mode 100644 index 0000000000..f7354d0649 --- /dev/null +++ b/src/preflight-verdict.ts @@ -0,0 +1,51 @@ +// #3977 goal B: the preflight exit state machine. Every exit of +// preflightCompressIfNeeded is exactly one of these verdicts, decided by the +// triple 【evidence adjudication (src/size-evidence.ts), walk result +// (PreflightResult), fit measurement (fits)】 — not by if-chain position. +// Failure titles render from the FAIL_FAST verdict at a single site. +// +// Migration plan for this enum (issue #3977): it LANDS here in step 1 as the +// typed contract; step 2 routes the eight escape hatches through the +// transition table that consumes these kinds; step 3 collapses the exit sites +// onto one render. Until step 2 the preflight function still returns its +// Prepared | PreflightFailFast shapes directly — behavior is unchanged. + +/** Why a payload is forwarded ONCE for upstream arbitration instead of being + * folded or refused. Each reason is one escape hatch family: + * - probe-evidence: #2313 estimate-only probe (baseline carries no current- + * route upstream evidence; forward once to acquire usage/overflow evidence) + * - image-billing: #496/#1800 image arbitration — images alone clear the + * window on estimate with no overflow evidence (immediate + post-fold gates) + * - divergence: #1839 the walk's own post-fold view fits but the rebuilt + * normal-config payload measures over + * - fresh-size: #3146 fresh (unknown-baseline) session still over-window by + * local estimate after maximum folding */ +export type ForwardOnceReason = "probe-evidence" | "image-billing" | "divergence" | "fresh-size"; + +/** What a fail-fast verdict is FOR. The class drives retry semantics and the + * rendered title; status/retryable stay explicit fields because the same + * class can carry different statuses across paths (deterministic-exhaustion + * is always 413, transient is 502 today but is defined by cause, not code). */ +export type FailFastClass = + | "aborted" // client disconnected mid-preflight (status 0, no response) + | "doomed-bytes" // #2490/#3146 doom math: no tokenizer could fit it (413, retryable=false) + | "deterministic-exhaustion" // #3461: math proved no combination of folds closes the gap (413) + | "upstream-throttle" // upstream 429 surfaced through preflight (503) + | "transient" // relay-dependent exhaustion: empty/truncated summaries, budget draws, pool saturation (502) + | "dead-end-cooldown"; // #726 cached diagnosis replayed (mechanism removed in #4079 — no exit site on current master) + +export type PreflightVerdict = + /** Payload fits the window (or never triggered); forward as-is. */ + | { kind: "forward-fits" } + /** Folding committed and the rebuilt payload fits; forward the rebuild. */ + | { kind: "fold" } + /** Forward exactly once; the upstream arbitrates size/billing (#496 house pattern). */ + | { kind: "forward-once-arbitrate"; reason: ForwardOnceReason } + /** Refuse locally with a diagnostic; nothing was forwarded. */ + | { kind: "fail-fast"; status: number; retryable: boolean; cls: FailFastClass } + /** Dead-end cooldown active: identical request already proved un-foldable (#726; mechanism removed in #4079 — no exit site on current master, kind retained for ledger mapping until step 2). */ + | { kind: "cooldown" } + /** Growth-armed zero progress with no foldable candidates: lean steady state, no backoff (#2638). */ + | { kind: "steady-state" } + /** Growth-armed zero progress caused purely by queue drops: pool congestion, retry next turn (#2662). */ + | { kind: "congestion" }; diff --git a/src/server.ts b/src/server.ts index df98297b21..529497ca7f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -57,6 +57,7 @@ export { isChatGptCodexUpstream, isCodexResponsesLite, resolvePromptCacheKey, sh import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "./fold-reconcile.js"; import { biliToolsDeclaredOnWire, countBiliToolUses, evaluateSelfHealRound, nudgeSuppressed, pluginLaneDegraded, pluginLaneRestore } from "./session-self-heal.js"; import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, splitSessionWarnings, RESPONSES_TURN_SEPARATOR_ID_PREFIX } from "./session.js"; +import { baselineFloor as projectBaselineFloor, hasOverflowEvidence, localRefusalCovers, noteLocalRefusal, readBaselineEvidence } from "./size-evidence.js"; import { detectStaleInstall } from "./update.js"; import { decideEaddrinuse, eaddrinuseExitCode, postBindCanLadder, UPGRADE_REPLACEMENT_ENV, type FailFastReason } from "./listen-policy.js"; import { getAdvisoryState, cannotResolveTarget } from "./advisory.js"; @@ -2729,79 +2730,6 @@ function preflightHoldGraceMs(): number { // (#4079) the #726 dead-end COOLDOWN was removed entirely — its knob and // config key with it. -// #3846: a local 413 ("can never fit, NOT forwarded") is independent evidence -// that this origin/model/window cannot accept the payload — but it only ever -// lived in the #726 dead-end marker's body-hash dimension: the moment the -// baseline source demotes to estimate (the #2313 probe re-arms at -// baselineFloor === 0) or the body changes between turns (the hash misses), -// the refusal evaporates and every forward-once escape hatch (estimate-only -// 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; - -/** (#3974/#4079) a local refusal is route-scoped CIRCUMSTANTIAL evidence, not - * a permanent property of the model: windows get re-declared (config fix, - * catalog refresh, #3993-style fallback recovery), payloads shrink, and an - * unexpiring record locked new sessions out of every forward-once escape - * hatch forever. Ten minutes covers the aggressive host-retry window the - * record exists for (#3846's consecutive full-bill re-sends) while - * guaranteeing the lock self-heals without any user action. */ -const LOCAL_REFUSAL_TTL_MS = 10 * 60_000; - -function readLocalRefusals(metadata: Record): LocalRefusalRecord[] { - const raw = metadata.localRefusals; - if (!Array.isArray(raw)) return []; - const now = Date.now(); - 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" - // (#4079/#3974) a record without a readable timestamp is treated as - // expired too — sessions persisted before the TTL carried no `at`, and - // those are exactly the ones stuck in the never-expiring lock. - && typeof (r as LocalRefusalRecord).at === "number" - && (r as LocalRefusalRecord).at > now - LOCAL_REFUSAL_TTL_MS, - ); -} - -/** 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); -} - /** #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 * comment lines (`: bili-preflight` — a spec-mandated no-op for every SSE @@ -3224,9 +3152,8 @@ export async function preflightCompressIfNeeded( // (#604) on a measured (folded) payload describes a different view and // must not pull preflight into multi-minute runs over a payload whose own // post-fold estimate fits the window. - const baselineFloorRaw = prepared.processedMessages.length > 0 - ? ((session.stats.lastInputTokensSource === "usage" || session.stats.lastInputTokensSource === "overflow-arm") ? session.stats.lastInputTokens : 0) - : session.stats.lastInputTokens; + const baselineEvidence = readBaselineEvidence(session.stats); + const currentOrigin = normalizeUpstreamOrigin(route?.upstream); // #1933 F2: a usage baseline is only authoritative for the route that // measured it — provider billing scales differ per upstream (the incident: // ~257K local estimate vs 59-63% real usage on one route; after a mid- @@ -3235,11 +3162,11 @@ export async function preflightCompressIfNeeded( // untrusted when the request now routes elsewhere; the payload's own // (calibrated) estimate then judges it. Unprovenanced baselines (sessions // started before this field existed) keep the legacy behavior. - let baselineFloor = baselineFloorRaw; - const currentOrigin = normalizeUpstreamOrigin(route?.upstream); - const baselineOrigin = normalizeUpstreamOrigin(session.stats.lastInputTokensOrigin); - if (baselineFloor > 0 && currentOrigin !== undefined && baselineOrigin !== undefined && baselineOrigin !== currentOrigin) { - log("info", `[${session.id}] preflight usage-baseline ~${baselineFloor} tok was measured on ${maskUrlForLog(baselineOrigin)}, request now routes to ${maskUrlForLog(currentOrigin)} — demoting to untrusted, judging by this payload's own estimate (#1933)`); + const baselineProjection = projectBaselineFloor(baselineEvidence, currentOrigin, prepared.processedMessages.length > 0); + let baselineFloor = baselineProjection.floor; + if (baselineProjection.demoted && currentOrigin !== undefined) { + // demoted ⇒ both origins are known (size-evidence.projectBaselineFloor contract) + log("info", `[${session.id}] preflight usage-baseline ~${baselineProjection.from} tok was measured on ${maskUrlForLog(baselineProjection.origin ?? "")}, request now routes to ${maskUrlForLog(currentOrigin)} — demoting to untrusted, judging by this payload's own estimate (#1933)`); baselineFloor = 0; } // #1933 F1: scale the local text estimate by the per-route calibration @@ -3313,7 +3240,7 @@ export async function preflightCompressIfNeeded( // [#autoFold] a growth trigger is operator intent, not a billing guess — // it never needs upstream evidence and must fold on the FIRST armed // request, so the #2313 estimate-only forward-once never skips it. - const probeForEvidence = !growthArmed && !unknownBaseline && session.stats.lastInputTokens > 0 && baselineFloor <= 0 && prepared.processedMessages.length > 0; + const probeForEvidence = !growthArmed && !unknownBaseline && baselineEvidence.tokens > 0 && baselineFloor <= 0 && prepared.processedMessages.length > 0; if (probeForEvidence) { // #2490: mathematically-doomed probe guard. The forward-once below bets // one request on "the estimator might be wrong about size"; that bet has @@ -3379,8 +3306,7 @@ export async function preflightCompressIfNeeded( // estimates back into lastInputTokens, which permanently closed this // hatch on pixel-billing upstreams). With evidence present we trust the // estimate and fall through to fold / fail-fast below. - const noOverflowEvidence = session.stats.lastInputTokens < limit - || (session.stats.lastInputTokensSource !== "usage" && session.stats.lastInputTokensSource !== "overflow-arm"); + const noOverflowEvidence = !hasOverflowEvidence(baselineEvidence, limit); // #1800/#1843: the TEXT channel fits the window but `text + imageReserve` // does not, and we hold no overflow evidence → the image reserve clears the // window on ESTIMATE alone while the real bill may be far smaller (pixel-tile @@ -3440,7 +3366,7 @@ export async function preflightCompressIfNeeded( // estimator diverges from upstream billing (#3146 read chars-as-tokens // ~2x high). Rejections backed by real evidence keep today's wording. const estimateOnly = baselineFloor === 0 && kFactor === undefined - && session.stats.lastInputTokensSource !== "usage" && session.stats.lastInputTokensSource !== "overflow-arm"; + && !(baselineEvidence.grade === "usage" || baselineEvidence.grade === "overflow"); const estimateNote = estimateOnly ? " (size judged by bili's local estimate, which can diverge from what the upstream actually bills)" : ""; const message = `${sizeClause} (model=${model})${rangesClause}${estimateNote} ` + diff --git a/src/size-evidence.ts b/src/size-evidence.ts new file mode 100644 index 0000000000..b965a713c0 --- /dev/null +++ b/src/size-evidence.ts @@ -0,0 +1,171 @@ +// #3977 goal A: the single authority over SIZE EVIDENCE — what bili knows +// about how big a payload is, WHERE each number came from, and how much it can +// be trusted. The kernel owns WHAT is foldable (#3236 unit-projection); this +// module owns the evidence plane WHETHER-folding-suffices is judged against. +// +// Phase 1 (this file): the graded view over the existing stores + the local- +// refusal store, moved here from server.ts byte-for-byte. No persistence +// format changes: metadata keys stay exactly as they are; reads/writes merely +// converge on this facade. fitEvidence() — the single adjudication every +// forward-once / arbitration / backoff decision must route through — lands +// with its callers in migration step 2; learnFromOutcome() in step 4. + +import { markDirty, type Session } from "./session.js"; +import { normalizeUpstreamOrigin } from "./util.js"; + +/** Provenance grade of a size measurement. Estimation and hard evidence were + * one untyped number in the if-chain (#1839 estimate-as-fact, #3146 unknown- + * as-estimate, #3663 mixed rulers — three symptoms, one disease); every + * judgment now declares the grade it consumed. */ +export type EvidenceGrade = "usage" | "overflow" | "local-refusal" | "estimate"; + +/** One size-evidence record. `at` is epoch ms; 0 while the underlying store + * carries no timestamp (the baseline store has none today — adding one is a + * persistence change deferred past phase 1). */ +export interface SizeEvidence { + kind: "baseline" | "refusal"; + origin: string | null; // normalized upstream origin; null = unprovenanced + model: string | null; + window: number | null; // the window the verdict was made against + tokens: number; + at: number; + grade: EvidenceGrade; +} + +/** Map the baseline store's source marker onto the grade ladder. Unknown or + * absent sources project to estimate — the exact legacy treatment (#1933: + * sessions started before the field existed keep legacy behavior). */ +export function gradeOfSource(source: string | undefined): EvidenceGrade | null { + if (source === "usage") return "usage"; + if (source === "overflow-arm") return "overflow"; + if (source === "estimate") return "estimate"; + return null; +} + +/** The session's size baseline as one graded record. usage-grade measures what + * upstream billed; overflow-grade is upstream REJECTION evidence at that size + * (armOverflowShrink stamps the origin so the arm survives #1933 F2 on its + * own route); everything else is bili's own meter. */ +export function readBaselineEvidence(stats: Session["stats"]): SizeEvidence { + const tokens = typeof stats.lastInputTokens === "number" ? stats.lastInputTokens : 0; + const origin = stats.lastInputTokensOrigin !== undefined ? normalizeUpstreamOrigin(stats.lastInputTokensOrigin) : undefined; + return { + kind: "baseline", + origin: origin ?? null, + model: null, + window: null, + tokens, + at: 0, + grade: gradeOfSource(stats.lastInputTokensSource) ?? "estimate", + }; +} + +export interface BaselineProjection { + floor: number; // the floor after cross-origin demotion + from: number; // the raw floor before demotion (for the log line) + demoted: boolean; + origin: string | null; // the baseline's provenance origin (for the log line) +} + +/** #1492 floor rule + #1933 F2 cross-origin demotion, pure. A usage-grade + * baseline floors the trigger while authoritative for THIS payload (it can + * legitimately exceed every local estimate — invisible thinking/cache + * components); an overflow-grade one is upstream rejection evidence at that + * size, floored too (#1195/#987 lose their trigger otherwise). An estimate- + * grade baseline floors only when the current payload is unmeasured + * (hasProcessedMessages=false ⇒ kernel transform failed ⇒ the outbound IS the + * raw body). A baseline measured on another origin is demoted to untrusted + * (provider billing scales differ per upstream, #1933 F2). Unprovenanced + * origins never demote anything — an unprovenanced baseline proves nothing + * about any specific route (evidence-permitlist discipline). */ +export function baselineFloor(evidence: SizeEvidence, currentOrigin: string | undefined, hasProcessedMessages: boolean): BaselineProjection { + const raw = hasProcessedMessages + ? (evidence.grade === "usage" || evidence.grade === "overflow" ? evidence.tokens : 0) + : evidence.tokens; + if (raw > 0 && currentOrigin !== undefined && evidence.origin !== null && evidence.origin !== currentOrigin) { + return { floor: 0, from: raw, demoted: true, origin: evidence.origin }; + } + return { floor: raw, from: raw, demoted: false, origin: evidence.origin }; +} + +/** #496/#1839/#857: a baseline ≥ window counts as upstream overflow evidence + * ONLY at usage or overflow grade. An estimate-derived or legacy-unmarked + * baseline is NOT one (#857: preflight used to write image estimates back + * into lastInputTokens, which permanently closed the image-arbitration hatch + * on pixel-billing upstreams). */ +export function hasOverflowEvidence(evidence: SizeEvidence, limit: number): boolean { + return evidence.tokens >= limit && (evidence.grade === "usage" || evidence.grade === "overflow"); +} + +// #3846: a local 413 ("can never fit, NOT forwarded") is independent evidence +// that this origin/model/window cannot accept the payload — but it only ever +// lived in the #726 dead-end marker's body-hash dimension: the moment the +// baseline source demotes to estimate (the #2313 probe re-arms at +// baselineFloor === 0) or the body changes between turns (the hash misses), +// the refusal evaporates and every forward-once escape hatch (estimate-only +// 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. +export interface LocalRefusalRecord { + origin: string; + model: string; + window: number; + at: number; +} + +const LOCAL_REFUSAL_MAX_RECORDS = 16; + +/** (#3974/#4079) a local refusal is route-scoped CIRCUMSTANTIAL evidence, not + * a permanent property of the model: windows get re-declared (config fix, + * catalog refresh, #3993-style fallback recovery), payloads shrink, and an + * unexpiring record locked new sessions out of every forward-once escape + * hatch forever. Ten minutes covers the aggressive host-retry window the + * record exists for (#3846's consecutive full-bill re-sends) while + * guaranteeing the lock self-heals without any user action. */ +const LOCAL_REFUSAL_TTL_MS = 10 * 60_000; + +export function readLocalRefusals(metadata: Record): LocalRefusalRecord[] { + const raw = metadata.localRefusals; + if (!Array.isArray(raw)) return []; + const now = Date.now(); + 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" + // (#4079/#3974) a record without a readable timestamp is treated as + // expired too — sessions persisted before the TTL carried no `at`, and + // those are exactly the ones stuck in the never-expiring lock. + && typeof (r as LocalRefusalRecord).at === "number" + && (r as LocalRefusalRecord).at > now - LOCAL_REFUSAL_TTL_MS, + ); +} + +/** 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); +} diff --git a/tests/issue3977-preflight-escape-pins.test.ts b/tests/issue3977-preflight-escape-pins.test.ts new file mode 100644 index 0000000000..1197e0186a --- /dev/null +++ b/tests/issue3977-preflight-escape-pins.test.ts @@ -0,0 +1,604 @@ +// #3977 step 0 (先打桩): pins the forward-once escape hatches that the #3846 +// local-refusal evidence did NOT yet cover, BEFORE the evidence-ledger / +// verdict-state-machine migration touches them. Every gate below is pinned in +// BOTH directions (#3617 gate discipline): "fires when it should" AND "does +// NOT fire when a covering local refusal exists": +// P-DIV the #1839 rebuilt-view divergence forward-once (src/server.ts:3658) +// × local refusal evidence — pinned by driving the two-view +// divergence directly (walk view fits, normal-config rebuild over); +// P-FRESH the #3146 fresh-session forward-once (src/server.ts:3712-3730) +// × local refusal evidence — anonymous request, zero baseline, +// deterministic exhaustion; +// P-IMG the #496/#1800 image-arbitration forward-once (src/server.ts:3360 +// immediate + src/server.ts:3691 post-fold) × local refusal +// evidence — unit pair on the immediate gate plus an end-to-end +// flow proving a REAL 413 seed closes the post-fold gate on a later +// image turn (the write→read edge no existing test covers); +// P-LEARN calibration-loop boundary: an upstream overflow-400 carrying +// stated token counts arms the emergency shrink (#1839/#3450) but +// does NOT move k̂ (calibratedEstimate/calibrationRing) today — +// k̂ is written exclusively by settleUsageReport on successful +// usage reports. This pin defines the flip target for migration +// step 4 (learnFromOutcome(overflow-400)). +// Zero production code changes in this file — it records today's behavior. +import assert from "node:assert/strict"; +import http from "node:http"; +import net from "node:net"; +import { once } from "node:events"; +import { randomUUID } from "node:crypto"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { createCore, defaultConfig, type CoreMessage } from "acp-kernel"; +import { preflightCompressIfNeeded, startServer, type Prepared } 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, listSessions } from "../src/session.ts"; +import { normalizeUpstreamOrigin } from "../src/util.ts"; +import { _resetFetchUtilForTest, _liveUpstreamTimersForTest } from "../src/fetch-util.ts"; + +_setStoreForTest(new SessionStore({ enabled: false })); +setRegistryForTest({}); + +type LogLine = [string, string]; + +// --------------------------------------------------------------------------- +// Unit fixtures: drive preflightCompressIfNeeded directly (the proven +// tests/preflight-client-abort.test.ts pattern) so each gate's inputs — +// walk result, rebuild measurement, baseline regime, refusal evidence — are +// controlled exactly instead of reconstructed through the full HTTP stack. +// --------------------------------------------------------------------------- + +type UnitFixture = { + session: ReturnType; + core: ReturnType; + config: ReturnType; + prepared: Prepared; + req: http.IncomingMessage; + res: http.ServerResponse; + opts: ProxyOptions; + log: LogLine[]; + server: http.Server; + url: string; + origin: string; + window: number; + model: string; + route: { upstream: string; rewrittenUrl: string }; + summaryCalls: number[]; +}; + +async function listenPlain(handler: (req: http.IncomingMessage, res: http.ServerResponse) => void): Promise<{ server: http.Server; port: number }> { + const server = http.createServer(handler); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + return { server, port: (server.address() as { port: number }).port }; +} + +type PlainMessage = { role: CoreMessage["role"]; text: string }; + +async function makeUnitFixture(t: test.TestContext, spec: { + window: number; + core: PlainMessage[]; + extraItems?: Array>; + preserveRecentMessages?: number; + imageRoute?: boolean; + summaryText?: string; +}): Promise { + const core = createCore(); + const config = defaultConfig(spec.window, { + preserveRecentMessages: spec.preserveRecentMessages ?? 0, + preserveRecentTokens: 0, + }); + const session = getSession(`pin-${randomUUID()}`); + const messages: CoreMessage[] = spec.core.map((m, i) => ({ id: `m${i}`, role: m.role, contentType: "text", text: m.text })); + const items: Array> = [ + ...messages.map((m) => ({ type: "message", role: m.role, content: m.text })), + ...(spec.extraItems ?? []), + ]; + const prepared: Prepared = { + body: JSON.stringify({ model: "test-model", stream: true, input: items }), + session, + processedMessages: messages, + originalMessages: messages, + protocol: "responses", + stream: true, + compressInjected: false, + }; + const req = new http.IncomingMessage(new net.Socket()); + req.method = "POST"; + req.headers = { "content-type": "application/json" }; + const res = new http.ServerResponse(req); + const log: LogLine[] = []; + const fixture: UnitFixture = { + session, core, config, prepared, req, res, + opts: { + host: "127.0.0.1", + port: 0, + upstream: "", + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: spec.window, + kernelConfig: config, + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + compat: { roles: {} }, + streamErrorShape: "protocol", + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + passthroughSource: null, + autoUpdate: false, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }, + log, + server: null as unknown as http.Server, + url: "", + origin: "", + route: { upstream: "", rewrittenUrl: "" }, + window: spec.window, + model: "test-model", + summaryCalls: [], + }; + // Summary endpoint: every summarization draw returns one usable answer. + const { server, port } = await listenPlain((_r, sres) => { + fixture.summaryCalls.push(1); + sres.writeHead(200, { "content-type": "application/json" }); + sres.end(JSON.stringify({ output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: spec.summaryText ?? "PIN SUMMARY: earlier turns held deterministic load text." }] }] })); + }); + fixture.server = server; + fixture.url = `http://127.0.0.1:${port}/responses`; + fixture.origin = normalizeUpstreamOrigin(fixture.url) ?? ""; + fixture.route = { upstream: fixture.url, rewrittenUrl: fixture.url }; + fixture.opts.upstream = fixture.url; + req.url = fixture.url; + if (spec.imageRoute) { + fixture.opts.routes = { [fixture.url]: { models: { [fixture.model]: { context: spec.window } }, imageBilling: "bytes" } } as ProxyOptions["routes"]; + } + t.after(() => { void server.close(); }); + return fixture; +} + +function callGate(f: UnitFixture, runPrepare: () => Promise, anonymous: boolean) { + return preflightCompressIfNeeded( + f.prepared, + runPrepare, + f.req, + Buffer.from(f.prepared.body), + f.res, + f.opts, + f.core, + f.config, + f.config.modelContextLimit, + f.model, + undefined, + undefined, + f.route, + undefined, + anonymous, + (level, msg) => f.log.push([level, msg]), + `pin-${randomUUID()}`, + ); +} + +/** Seed the #3846 metadata channel exactly as noteLocalRefusal writes it + * (the helpers are module-private; the record shape is the contract). */ +function seedLocalRefusal(f: UnitFixture, window?: number): void { + f.session.metadata.localRefusals = [ + { origin: f.origin, model: f.model, window: window ?? f.window, at: Date.now() }, + ]; +} + +function cleanup(t: test.TestContext): void { + assert.equal(_liveUpstreamTimersForTest(), 0, "no live upstream timers"); + _resetFetchUtilForTest(); + void t; +} + +const MODEL = "test-model"; + +// --- P-DIV: #1839 divergence forward-once × local refusal ------------------ + +// Divergence construction: the REAL walk folds the large middle message and +// its own relaxed-zone view fits under the window (result.payloadEstimate < +// limit); the supplied rebuild (normal-config prepare stand-in) measures the +// ORIGINAL payload again, over the window, so fits=false and the flow reaches +// the #1839 gate with (compressedRanges>0, walkViewFits, rebuiltOver). +async function divFixture(t: test.TestContext): Promise { + const window = 6_000; + const core: PlainMessage[] = [ + { role: "user", text: "small lead-in question" }, + { role: "assistant", text: "FILLER_".repeat(4_000) }, + { role: "user", text: "small trailing question" }, + ]; + const f = await makeUnitFixture(t, { window, core }); + // usage-grade baseline above the floor: the #2313 estimate-only probe + // stays disarmed (baselineFloor > 0), isolating the divergence gate. + f.session.stats.lastInputTokens = 1_000; + f.session.stats.lastInputTokensSource = "usage"; + return f; +} + +test("P-DIV: #1839 divergence forward-once fires WITHOUT a covering local refusal", async (t) => { + const f = await divFixture(t); + const out = await callGate(f, async () => f.prepared, false); + assert.ok(!("failFast" in out), "no refusal evidence: the divergence gate must forward once"); + assert.ok(f.log.some(([, m]) => m.includes("forwarding once for upstream arbitration (#1839)")), + `expected the #1839 forward-once log line, got:\n${f.log.map(([l, m]) => `[${l}] ${m}`).join("\n")}`); + cleanup(t); +}); + +test("P-DIV: a covering local refusal CLOSES the #1839 divergence gate (fail-fast instead of re-forward)", async (t) => { + const f = await divFixture(t); + seedLocalRefusal(f); + const out = await callGate(f, async () => f.prepared, false); + assert.ok("failFast" in out, "a covering refusal must close the divergence escape hatch"); + // Walk succeeded (no failure kind), so the fall-through keeps the generic + // verdict contract: not a deterministic exhaustion, hence 502 — the point + // of the pin is NO forward, whatever the fall-through status is. + assert.equal(out.status, 502); + assert.equal(out.retryable, false); + assert.ok(!f.log.some(([, m]) => m.includes("forwarding once for upstream arbitration (#1839)")), + "the #1839 forward-once must stay silent under a covering refusal"); + cleanup(t); +}); + +// --- P-FRESH: #3146 fresh-session forward-once × local refusal ------------- + +// Anonymous request + zero baseline ⇒ unknownBaseline=true (the #553 regime). +// The oversized mass sits in a SYSTEM head, which IS foldable in the preflight +// walk — so the fixture forces the exhaustion another way (see below) instead +// of letting the walk succeed and take the fits-forward path. 36KB ≪ the 2MB +// doom bar, so the doomed-bytes guard does not pre-empt the gate under test. +async function freshFixture(t: test.TestContext): Promise { + const window = 6_000; + const core: PlainMessage[] = [ + { role: "system", text: "BLOB_".repeat(6_000) }, // ~7.5K tok est > 6K window + { role: "user", text: "current turn question" }, + ]; + // The system head IS a foldable candidate in the preflight walk (verified + // against the walk log), so a USABLE summary would fold it away and take + // the fits-forward path — never reaching the #3146 branch. Deliberately + // answer every draw with an 11-char summary (kernel minSummaryLength is + // 50): the exhaustion is forced, deterministic per construction, and the + // flow lands exactly on the #3146 fresh-session gate. + return makeUnitFixture(t, { window, core, preserveRecentMessages: 1, summaryText: "PIN SUMMARY" }); +} + +test("P-FRESH: #3146 fresh-session forward-once fires WITHOUT a covering local refusal", async (t) => { + const f = await freshFixture(t); + const out = await callGate(f, async () => f.prepared, true); + assert.ok(!("failFast" in out), "no refusal evidence: the fresh-session gate must forward once"); + assert.ok(f.log.some(([, m]) => m.includes("forwarding once for the upstream to arbitrate size before failing fast (#3146/#4185)")), + `expected the #3146 forward-once log line, got:\n${f.log.map(([l, m]) => `[${l}] ${m}`).join("\n")}`); + cleanup(t); +}); + +test("P-FRESH: a covering local refusal CLOSES the #3146 fresh-session gate (fail-fast instead of re-forward)", async (t) => { + const f = await freshFixture(t); + seedLocalRefusal(f); + const out = await callGate(f, async () => f.prepared, true); + assert.ok("failFast" in out, "a covering refusal must close the fresh-session escape hatch"); + assert.equal(out.status, 502, + // The exhaustion is NON-deterministic on purpose: the deliberately + // unusable summary leaves a lastUnusableDetail, so the verdict contract + // (src/server.ts:3749-3764) yields the generic 502, not the + // deterministic 413. The pin's point is the GATE CLOSURE (no forward), + // with the status following the existing verdict rules. + "refused fresh-session payload fails fast (no forward)"); + assert.equal(out.retryable, false); + assert.ok(f.log.some(([, m]) => m.includes("NOT re-forwarding for size arbitration (#3846/#4185)")), + `expected the #3846 refusal log line, got:\n${f.log.map(([l, m]) => `[${l}] ${m}`).join("\n")}`); + // the fall-through fail-fast re-notes the refusal — idempotent per scope: + // the widest-window-dominates rule must keep exactly one record. + const records = f.session.metadata.localRefusals as Array>; + assert.equal(records.length, 1); + assert.equal(records[0]?.window, f.window); + cleanup(t); +}); + +// --- P-IMG: #496/#1800 image-arbitration forward-once × local refusal ------ + +// Image-dominated payload: the bytes-billed image reserve (22K) alone clears +// the window (20K) on ESTIMATE; the text channel is small AND every text +// message sits below minCompressRange (5000 chars) ⇒ the walk has ZERO viable +// ranges ⇒ deterministic exhaustion with no summary draws. Usage-grade +// baseline below the window ⇒ noOverflowEvidence ⇒ imageArbitration=true. +// Negative direction: the immediate gate (src/server.ts:3360) forwards once +// (bare Prepared ⇒ no nudge ⇒ zero advertised ranges). Positive direction: +// the covering refusal skips the immediate gate, the walk exhausts +// deterministically, the post-fold gate (src/server.ts:3691) stays shut, and +// the final verdict is the deterministic 413. +async function imgFixture(t: test.TestContext): Promise { + const window = 20_000; + const filler = (i: number) => `MARKER_${i}: ` + "lorem ipsum dolor sit amet ".repeat(25); // ~500 chars < minCompressRange 5000 + const roles: Array<"user" | "assistant"> = ["user", "assistant", "user", "assistant", "user", "assistant"]; + const core: PlainMessage[] = roles.map((role, i) => ({ role, text: filler(i) })); + const imageB64 = "iVBORw0KGgo" + "A".repeat(22_000 * 4 - 32); // ~22K tokens at bytes billing (b64/4) + const extraItems: Array> = [ + { type: "message", role: "user", content: [{ type: "input_image", image_url: `data:image/png;base64,${imageB64}` }] }, + ]; + const f = await makeUnitFixture(t, { window, core, extraItems, imageRoute: true }); + f.session.stats.lastInputTokens = 1_000; + f.session.stats.lastInputTokensSource = "usage"; + return f; +} + +test("P-IMG: image-arbitration forward-once fires WITHOUT a covering local refusal (immediate gate, zero folds)", async (t) => { + const f = await imgFixture(t); + const out = await callGate(f, async () => f.prepared, false); + assert.ok(!("failFast" in out), "no refusal evidence: the image-arbitration gate must forward once"); + assert.ok(f.log.some(([, m]) => m.includes("forwarding for the upstream to arbitrate billing (#496/#1800)")), + `expected the #496/#1800 forward-once log line, got:\n${f.log.map(([l, m]) => `[${l}] ${m}`).join("\n")}`); + assert.equal(f.summaryCalls.length, 0, "the immediate gate forwards before any fold"); + cleanup(t); +}); + +test("P-IMG: a covering local refusal CLOSES the image-arbitration gate (413 instead of re-forward)", async (t) => { + const f = await imgFixture(t); + seedLocalRefusal(f); + const out = await callGate(f, async () => f.prepared, false); + assert.ok("failFast" in out, "a covering refusal must close the image-arbitration escape hatch"); + // Zero viable ranges (sub-minCompressRange text) ⇒ deterministic + // exhaustion, no summary draws spent; the calibrated meter + // (text+image ≫ window) agrees ⇒ the #3461 413 path. + assert.equal(out.status, 413); + assert.equal(out.retryable, false); + assert.equal(f.summaryCalls.length, 0, "the text portion still folds before the verdict"); + assert.ok(!f.log.some(([, m]) => m.includes("arbitrate billing (#496/#1800)")), + "the #496/#1800 forward-once must stay silent under a covering refusal"); + cleanup(t); +}); + +// --------------------------------------------------------------------------- +// End-to-end pins (full HTTP stack through startServer). +// --------------------------------------------------------------------------- + +// Under bytes billing the cost is base64 chars / 4 (base64ImageCost) — size +// the data URL so the reserve lands exactly on the target token count. +function imageB64Of(targetTokens: number): string { + return "iVBORw0KGgo" + "A".repeat(targetTokens * 4 - 11); +} + +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: MODEL, 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"; +} + +// Counting fake upstream for the e2e lanes: bills received message chars at +// ~40 B/token as prompt usage; answers summarization draws deterministically; +// replies to OVERLOAD400-marked traffic with a provider-shaped overflow 400 +// carrying stated token counts. +function makeCountingUpstream(): { server: http.Server; calls: { summary: boolean; forwarded: boolean; contentChars: number }[] } { + const calls: { summary: boolean; forwarded: boolean; contentChars: number }[] = []; + 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 { 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, forwarded: !isSummary, contentChars: chars }); + if (isSummary) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chatcmpl-sum", object: "chat.completion", model: MODEL, choices: [{ index: 0, message: { role: "assistant", content: "PIN SUMMARY: earlier turns held deterministic load text." }, finish_reason: "stop" }] })); + return; + } + if (raw.includes("OVERLOAD400")) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: `This model's maximum context length is ${E2E_WINDOW} tokens. However, you requested 250000 tokens (240000 in the messages, 10000 in the completion). Please reduce the length of the messages.`, type: "invalid_request_error", code: "context_length_exceeded" } })); + 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 }; +} + +const E2E_WINDOW = 200_000; +const E2E_MODEL = "gpt-pin"; + +function e2eProxyOptions(routes: Record): ProxyOptions { + return { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: routes as ProxyOptions["routes"], + modelContextLimit: E2E_WINDOW, + kernelConfig: defaultConfig(E2E_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; +} + +// Zero-config routing: the /bili//... prefix carries the full +// upstream URL (resolveUpstream strips the prefix and takes the rest verbatim), +// so the route key is the upstream ORIGIN and the client URL appends the path. +async function startE2E(t: test.TestContext) { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const upstream = makeCountingUpstream(); + upstream.server.listen(0, "127.0.0.1"); + await once(upstream.server, "listening"); + const upstreamPort = (upstream.server.address() as { port: number }).port; + const origin = `http://127.0.0.1:${upstreamPort}`; + // imageBilling "bytes" is load-bearing: the default "auto" resolves to + // pixels pricing, where an unparseable synthetic PNG costs ~0 tokens and + // the whole image-arbitration regime switches off. + const proxy = await startServer(e2eProxyOptions({ [origin]: { models: { [E2E_MODEL]: { context: E2E_WINDOW } }, imageBilling: "bytes" } })); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + t.after(async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await Promise.allSettled([once(proxy, "close")]); + await new Promise((resolve) => { upstream.server.close(() => resolve()); }); + _resetFetchUtilForTest(); + }); + const sid = `e2e-${randomUUID()}`; + return { + url: `http://127.0.0.1:${proxyPort}/bili/${origin}/chat/completions`, + headers: { "content-type": "application/json", "x-acp-session": sid }, + sid, + origin, + calls: upstream.calls, + }; +} + +// P-IMG (end-to-end): the write→read edge no existing test covers — a REAL +// local 413 (seeded through the full stack) must keep the LATER image +// arbitration gate closed, while an identical image turn on a session that +// was never refused still forwards for billing arbitration. +test("P-IMG e2e: a real 413 seed closes the image-arbitration gate on a later image turn (and only there)", async (t) => { + const refused = await startE2E(t); + const clean = await startE2E(t); + const body = (messages: unknown) => JSON.stringify({ model: E2E_MODEL, stream: true, messages }); + + // Turn 1 (both sessions): small fitting turn settles a usage baseline. + const r1a = await fetch(refused.url, { method: "POST", headers: refused.headers, body: body([{ role: "user", content: "hello" }]) }); + assert.equal(r1a.status, 200); + await r1a.arrayBuffer(); + const r1b = await fetch(clean.url, { method: "POST", headers: clean.headers, body: body([{ role: "user", content: "hello" }]) }); + assert.equal(r1b.status, 200); + await r1b.arrayBuffer(); + + // Turn 2 (refused session only): oversized NON-FOLDABLE system text — + // the #3846-proven deterministic 413 seed (system head is protected, so + // the walk folds nothing and the verdict is a local refusal). ~1.05M + // chars ≈ 260K-token estimate: over the 200K window, under the 2MB doom bar. + const blob = Array.from({ length: 15_000 }, (_, i) => `oversized seed segment ${i}: ` + "x".repeat(40)).join(""); + const seed = [{ role: "system", content: blob }, { role: "user", content: "q" }]; + const r2 = await fetch(refused.url, { method: "POST", headers: refused.headers, body: body(seed) }); + assert.equal(r2.status, 413, "the seed must be a local 413, not forwarded"); + await r2.arrayBuffer(); + const refusedSession = listSessions().find((s) => s.id === refused.sid); + const refusals = refusedSession?.metadata.localRefusals as Array> | undefined; + assert.ok(Array.isArray(refusals) && refusals.length === 1, "the 413 must persist exactly one local-refusal record"); + assert.equal(refusals?.[0]?.window, E2E_WINDOW); + + // Turn 3 (both sessions): old foldable history + a current turn whose + // TEXT channel (~17K tok) fits but whose bytes-billed image reserve + // (210K) clears the window on estimate ⇒ imageArbitration. The foldable + // text mass (~17K × slack 1.2) cannot close the ~27K deficit — the image + // is unfoldable — so the walk declares deterministic futility with zero + // summary calls in BOTH sessions; the only difference left is the + // post-fold arbitration gate, which the persisted refusal closes. + const imageB64 = imageB64Of(210_000); + const filler = "lorem ipsum dolor sit amet ".repeat(400); + const imageTurn = [ + { role: "user", content: "old load segment A " + "a".repeat(30_000) }, + { role: "assistant", content: "old load segment B " + "b".repeat(30_000) }, + { role: "user", content: [ + { type: "text", text: "current turn text " + filler }, + { type: "image_url", image_url: { url: `data:image/png;base64,${imageB64}` } }, + ] }, + ]; + const forwardsBefore = refused.calls.filter((c) => c.forwarded).length; + const r3r = await fetch(refused.url, { method: "POST", headers: refused.headers, body: body(imageTurn) }); + assert.equal(r3r.status, 413, "the persisted refusal must keep the image-arbitration gate closed"); + await r3r.arrayBuffer(); + const forwardsAfter = refused.calls.filter((c) => c.forwarded).length; + assert.equal(forwardsAfter, forwardsBefore, "NO additional forward may leave the proxy for a locally-refused scope"); + assert.equal(refused.calls.filter((c) => c.summary).length, 0, "futility spent no summarization call in the refused session either"); + + const r3c = await fetch(clean.url, { method: "POST", headers: clean.headers, body: body(imageTurn) }); + assert.equal(r3c.status, 200, "an identical image turn on a never-refused session must still forward for billing arbitration"); + await r3c.arrayBuffer(); + assert.equal(clean.calls.filter((c) => c.forwarded).length, 2, "seed + exactly one arbitration forward left the clean session"); + assert.equal(clean.calls.filter((c) => c.summary).length, 0, "and it too spent no summarization call"); +}); + +// P-LEARN: calibration-loop boundary. An upstream overflow-400 carrying +// stated token counts is the richest size evidence bili currently THROWS +// AWAY: armOverflowShrink consumes the window/count to arm the emergency +// shrink, but k̂ (calibratedEstimate + calibrationRing) moves ONLY on +// successful usage reports (settleUsageReport). Pinning today's boundary — +// the flip target of migration step 4 (learnFromOutcome(overflow-400)). +test("P-LEARN: an overflow-400 with stated token counts arms the shrink but does NOT move k̂", async (t) => { + const ctx = await startE2E(t); + const body = (messages: unknown) => JSON.stringify({ model: E2E_MODEL, stream: true, messages }); + + const r1 = await fetch(ctx.url, { method: "POST", headers: ctx.headers, body: body([{ role: "user", content: "hello" }]) }); + assert.equal(r1.status, 200); + await r1.arrayBuffer(); + + const s = listSessions().find((x) => x.id === ctx.sid); + assert.ok(s, "session must exist"); + // Pre-seed a concrete k̂ so the pin is "this exact state survives", not + // "absence stays absence". + const origin = normalizeUpstreamOrigin(ctx.origin) ?? ""; + s.stats.calibratedEstimate = 1.5; + s.stats.calibratedEstimateOrigin = origin; + s.stats.calibratedEstimateModel = E2E_MODEL; + s.stats.calibrationRing = { origin, model: E2E_MODEL, values: [1.4, 1.6] }; + const before = { + calibratedEstimate: s.stats.calibratedEstimate, + calibratedEstimateOrigin: s.stats.calibratedEstimateOrigin, + calibratedEstimateModel: s.stats.calibratedEstimateModel, + calibrationRing: s.stats.calibrationRing, + lastUsageGradeTokens: s.stats.lastUsageGradeTokens, + }; + + // Demote the baseline to estimate-only so the #2313 probe fires and the + // payload actually reaches the upstream (which answers with the 400). + s.stats.lastInputTokens = 60_000; + s.stats.lastInputTokensSource = "estimate"; + + const r2 = await fetch(ctx.url, { method: "POST", headers: ctx.headers, body: body([{ role: "user", content: "OVERLOAD400 please answer" }]) }); + assert.equal(r2.status, 400, "the overflow 400 passes through to the client"); + const errText = await r2.text(); + assert.match(errText, /maximum context length/i); + + const s2 = listSessions().find((x) => x.id === ctx.sid); + assert.ok(s2, "session must survive"); + // Positive control: the 400 WAS consumed as overflow evidence — the + // emergency shrink armed at the stated wall with the stated messages count. + assert.equal(s2.stats.lastInputTokensSource, "overflow-arm", "armOverflowShrink must have fired on the 400"); + assert.equal(s2.stats.overflowArmTokens, E2E_WINDOW, "the stated window must anchor the overflow arm"); + // The pin: none of the k̂ state moved. + assert.deepEqual( + { + calibratedEstimate: s2.stats.calibratedEstimate, + calibratedEstimateOrigin: s2.stats.calibratedEstimateOrigin, + calibratedEstimateModel: s2.stats.calibratedEstimateModel, + calibrationRing: s2.stats.calibrationRing, + lastUsageGradeTokens: s2.stats.lastUsageGradeTokens, + }, + before, + "an overflow-400 must not touch the calibration loop today (step-4 flip target)", + ); +});