From 720a6e331b7726cd8699e8320648040568aa5f83 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 07:42:52 +0800 Subject: [PATCH 1/4] fix: reserve output headroom by route, not wire (#4205) --- src/plugin.ts | 2 +- src/server.ts | 7 +++--- src/server/budget.ts | 13 +++++++--- src/server/handle.ts | 37 ++++++++++++++++----------- src/server/relay.ts | 39 +++++++++++++++++++---------- src/server/side-request.ts | 18 ++++++++++---- src/util.ts | 51 ++++++++++++++++++++++++++++++-------- 7 files changed, 117 insertions(+), 50 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index fa02d130dd..6ee5d90614 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1434,7 +1434,7 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr child.contentStore = cloneStoreForRefs(parentStore, new Set(prefix.filter((m) => m.ref !== null).map((m) => m.ref!))) ?? undefined; child.metadata.publicSnapshotTextComparable = true; child.metadata.publicSnapshotStoredRefs = Object.keys(child.contentStore?.byRef ?? {}); - for (const key of ["pluginAgent", "lastModel", "effectiveConfig", "effectiveConfigEpoch", "effectiveCcr", "effectiveContextLimit", "lastWindowSource", "systemPromptTokens"]) if (parent.metadata[key] !== undefined) child.metadata[key] = structuredClone(parent.metadata[key]); + for (const key of ["pluginAgent", "lastModel", "effectiveConfig", "effectiveConfigEpoch", "effectiveCcr", "effectiveContextLimit", "reservedContextLimit", "lastWindowSource", "systemPromptTokens"]) if (parent.metadata[key] !== undefined) child.metadata[key] = structuredClone(parent.metadata[key]); child.metadata.parentConversationId = request.parentConversationId; child.metadata.parentRevision = request.parentRevision; child.stats.contextTokens = prune(child.pluginSnapshot, child.state).reduce((sum, m) => sum + countMessageTokens(m), 0) + (typeof child.metadata.systemPromptTokens === "number" ? child.metadata.systemPromptTokens : 0); diff --git a/src/server.ts b/src/server.ts index df98297b21..51fbcca966 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3421,9 +3421,10 @@ export async function preflightCompressIfNeeded( // windowShrinkReason (set ONLY by the operator-override and codex-align // paths) — NOT just on limit < resolvedNativeWindow: the per-request // output-headroom reservation (reserveOutputHeadroom) also lowers - // reqConfig.modelContextLimit below native for every non-Anthropic turn - // with a max_tokens, so comparing alone would emit this note for a - // setting the operator never touched (#737 review). + // reqConfig.modelContextLimit below native for every turn whose route + // reserves headroom (non-anthropic wires plus anthropic routes lacking + // separate-output-budget evidence, #4205), so comparing alone would emit + // this note for a setting the operator never touched (#737 review). const ffEmbeddedUrl = route?.rewrittenUrl; const ffProviderKeys = Object.keys(opts.routes); const shrinkNote = buildWindowShrinkNote({ diff --git a/src/server/budget.ts b/src/server/budget.ts index 3f3c52c523..9defd4ee77 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -10,10 +10,15 @@ export { readOutputBudget }; // #453 hard backstop: cap the forwarded output budget so input+output can never // exceed the window on request-rebuilding upstreams (vLLM rejects an oversized -// total instead of clamping). Non-Anthropic only — Anthropic enforces its input -// limit independently of max_tokens (see shouldReserveOutputHeadroom). The proxy -// owns both sides of the sum, so capping output to (window - input - margin) -// makes the overflow impossible even when the agent ignores the compress nudge. +// total instead of clamping). Wired into the openai/responses/google prepare +// lanes only — canonical Anthropic routes enforce their input limit +// independently of max_tokens, so they get no clamp call site. #4205 boundary: +// wrapper providers on the anthropic wire (completion billed against the same +// window) DO reserve headroom at the band layer but keep no egress clamp here; +// their residual over-requests ride the overflow self-heal (#4093) instead. +// The proxy owns both sides of the sum, so capping output to (window - input - +// margin) makes the overflow impossible even when the agent ignores the +// compress nudge. const OUTPUT_CLAMP_MARGIN_PCT = 0.05; const OUTPUT_CLAMP_MIN_MARGIN = 2048; const OUTPUT_CLAMP_FLOOR = 1024; diff --git a/src/server/handle.ts b/src/server/handle.ts index 60229b7698..a1d5f4bcb6 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -43,7 +43,7 @@ import { resolveClaudeTranscriptLineage } from "../claude-transcript-lineage.js" import { consumePluginRegisterFor, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; import { scheduleAffinityPersist } from "../affinity-persist.js"; import { evaluateChain, extractChainCarriers } from "../chain-checkpoint.js"; -import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; +import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, normalizeUpstreamOrigin, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; import { checkTunnelDestination, tunnelAllowlistFromEnv } from "../tunnel-guard.js"; import { resolveCompatDropFields } from "../compat-drop.js"; import { DecompressedTooLargeError, decodeRequestBody } from "../content-encoding.js"; @@ -1996,7 +1996,7 @@ async function handleRequest( // model's real window. overflowArmTokens is set ONLY by an upstream // context-overflow 400 and cleared by the next real usage report. const armedForGuard = typeof session.stats.overflowArmTokens === "number" && session.stats.overflowArmTokens > 0 ? session.stats.overflowArmTokens : 0; - const guard = sideRequestGuard(parsed, protocol, reqConfig.modelContextLimit, imageBillingFor(opts, route?.rewrittenUrl ?? upstreamOrigin), imageTokenCapFor(opts, route?.rewrittenUrl ?? upstreamOrigin), headroomCap, armedForGuard, imageReserveFor(session, protocol, parsed, opts, route?.rewrittenUrl ?? upstreamOrigin)); + const guard = sideRequestGuard(parsed, protocol, reqConfig.modelContextLimit, imageBillingFor(opts, route?.rewrittenUrl ?? upstreamOrigin), imageTokenCapFor(opts, route?.rewrittenUrl ?? upstreamOrigin), headroomCap, armedForGuard, imageReserveFor(session, protocol, parsed, opts, route?.rewrittenUrl ?? upstreamOrigin), shouldReserveOutputHeadroom(protocol, normalizeUpstreamOrigin(route?.rewrittenUrl ?? upstreamOrigin), requestModel)); if (guard.blocked) { log("warn", `[${session.id}] side request (~${guard.estimate} tokens) ≥ effective window ${guard.limit} (model=${reqModel ?? "?"}) — NOT forwarded: guaranteed upstream 400 (side requests bypass preflight by design, #388)`); if (!res.headersSent && !res.writableEnded && !res.destroyed) { @@ -2062,11 +2062,14 @@ async function handleRequest( // kernel's nudge/truncate bands sit below (window - reserved) and a // context+output overflow can't happen on a small window (e.g. 100k with a // large max_tokens — the most common "context blew up" cause; none of the - // three layers reserved room for the output before this). Anthropic is - // exempt: its input limit is enforced independently of max_tokens - // (separate output budget), so reserving would shift every band down by - // maxOutput on every session for no safety gain — see - // shouldReserveOutputHeadroom. The request's own budget field is the exact + // three layers reserved room for the output before this). #4205: EVERY + // wire reserves unless the route carries positive evidence of a separate + // output budget (real-Claude routing — api.anthropic.com or a claude-* + // model id; see shouldReserveOutputHeadroom / hasSeparateOutputBudgetEvidence). + // The old blanket anthropic exemption is what let wrapper providers on + // that wire (DeepSeek /anthropic bills completion against the same + // window) 400 at the wall (#3900); proven-share learning (#3973) composes + // here later. The request's own budget field is the exact // output budget requested for THIS turn, so it is precise and per-request; // when the harness omits every budget field (#924 fallback below) the // model's declared max output stands in for it. @@ -2079,7 +2082,8 @@ async function handleRequest( // self-heal above handles the resulting overflow. Feeds reqConfig (→ // processTurn `config`), so diagNudge shows the reserved window (no extra log). const nativeWindow = reqConfig.modelContextLimit; - if (shouldReserveOutputHeadroom(protocol)) { + const headroomOrigin = normalizeUpstreamOrigin(route?.rewrittenUrl ?? upstreamOrigin); + if (shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel)) { const p = parsed as Record; const rawMax = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; let maxOutput = typeof rawMax === "number" ? rawMax : 0; @@ -2188,12 +2192,17 @@ async function handleRequest( } } } - // Record the FINAL effective window (post self-heal + output-headroom) - // so the status panel / acp_status show the window the kernel is actually - // using, in every mode (plugin AND wire). #393: previously this was set - // pre-self-heal and only for plugin sessions, so wire-mode panels fell - // back to a hardcoded 200K. - session.metadata.effectiveContextLimit = reqConfig.modelContextLimit; + // #4205 caliber: status surfaces record the UNRESERVED window (post + // self-heal, pre output-headroom) — the wall the client/upstream + // declares, stable across turns regardless of per-request max_tokens. + // The reserved judging window (what nudge/truncate/preflight actually + // judge against) rides reqConfig into the kernel AND the per-request + // twin below: request-scoped consumers read the twin instead (#3738's + // arm clamp must clamp to the limit the kernel judges, not display N). + // #393: both were previously one value set post-self-heal only for + // plugin sessions, so wire-mode panels fell back to a hardcoded 200K. + session.metadata.effectiveContextLimit = nativeWindow; + session.metadata.reservedContextLimit = reqConfig.modelContextLimit; // #955 runtime-info: record the model id + window source for this // session so /__bili/plugin/status can show them pre-first-request and // post-hoc forensics can tell which source sized the window. diff --git a/src/server/relay.ts b/src/server/relay.ts index 594cd98736..4d87f72e11 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -593,6 +593,10 @@ export async function forward( // No LocalRefusalRecord here: the doomed body itself re-trips this fuse on // every retry, so a record would only outlive the turn and lock the route // the way #3974 suffered. + // #4205: the window below is the UNRESERVED wall (effectiveContextLimit + // records N) — the bar sizes against the actual upstream limit; bodies + // between the reserved band and the wall ride forward→400→refold rescue + // instead of an instant 413 (slightly looser than the pre-#4205 R-sized bar). if (prepared && !prepared.sidePassthrough && req.method !== "GET" && req.method !== "HEAD") { const declaredWindow = typeof prepared.session.metadata.effectiveContextLimit === "number" && prepared.session.metadata.effectiveContextLimit > 0 ? prepared.session.metadata.effectiveContextLimit @@ -1069,11 +1073,12 @@ export async function forward( // // #3738: the armed value must be ≤ the limit the kernel actually // JUDGES against, or the arm can never trigger the fold it - // exists to cause. The kernel compares the payload to - // `session.metadata.effectiveContextLimit` — the - // output-headroom-RESERVED window (handle.ts:2137 - // `reserveOutputHeadroom`), e.g. 1,048,576 - 256,000 = 792,576 — - // while `info.window` is the RAW stated window (1,048,576). A + // exists to cause. The kernel judges against reqConfig's + // output-headroom-RESERVED window, recorded per-request as + // `session.metadata.reservedContextLimit` (#4205 split the display + // off into the unreserved `effectiveContextLimit`), e.g. 1,048,576 + // - 256,000 = 792,576 — while `info.window` is the RAW stated + // window (1,048,576). A // live limb hit exactly this: upstream 400 "maximum context // length is 1048576 ... you requested 1097304 (841304 in the // messages, 256000 in the completion)", the arm sat at 1,048,576 @@ -1092,9 +1097,9 @@ export async function forward( // (a stated count exceeds the reserved limit exactly when the // completion share pushed the total over the window). const measured = typeof info.requestedMessages === "number" && info.requestedMessages > 0 ? info.requestedMessages : undefined; - const judgingLimit = typeof s.metadata.effectiveContextLimit === "number" && s.metadata.effectiveContextLimit > 0 - ? s.metadata.effectiveContextLimit - : 0; + const judgingLimit = typeof s.metadata.reservedContextLimit === "number" && s.metadata.reservedContextLimit > 0 + ? s.metadata.reservedContextLimit + : typeof s.metadata.effectiveContextLimit === "number" && s.metadata.effectiveContextLimit > 0 ? s.metadata.effectiveContextLimit : 0; const unclampedBaseline = measured ?? info.window; const armedBaseline = judgingLimit > 0 && judgingLimit < unclampedBaseline ? judgingLimit : unclampedBaseline; s.stats.lastInputTokens = armedBaseline; @@ -1118,11 +1123,13 @@ export async function forward( const reserve = typeof info.requestedCompletion === "number" && info.requestedCompletion > 0 ? info.requestedCompletion : undefined; if (reserve !== undefined) s.stats.overflowArmCompletionReserve = reserve; // #3450: one-time mismatch warning — the upstream-stated window - // differs from the window bili actually judges on. + // differs from the window bili actually judges on. #4205: compare + // against the UNRESERVED declaration, so routes that reserve output + // headroom no longer warn on every arm just because R < N. const declaredWindow = typeof s.metadata.effectiveContextLimit === "number" ? s.metadata.effectiveContextLimit : 0; if (declaredWindow > 0 && declaredWindow !== info.window && !s.stats.overflowArmMismatchWarned) { s.stats.overflowArmMismatchWarned = true; - 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-stated window ${info.window} differs from the declared window in use (${declaredWindow}, model=${reqModel ?? "unknown"}) — folding targets the smallest of the stated window, the declared window and the per-route output reservation; 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)`); // #3900/#4079: the stated count is a usage-grade measurement of @@ -1146,13 +1153,19 @@ export async function forward( // declared window arms at the declaration — which is what // the #496 image-relay forward-once gate needs to break the // #488 400 loop after exactly one rejected forward. - const declared = typeof s.metadata.effectiveContextLimit === "number" ? s.metadata.effectiveContextLimit : 0; + // #4205: arm against the JUDGING (reserved) window so the #3738 + // invariant holds here too; pre-#4205 persisted sessions carry that + // value in effectiveContextLimit (the field then held R), hence the + // fallback. + const judging = typeof s.metadata.reservedContextLimit === "number" && s.metadata.reservedContextLimit > 0 + ? s.metadata.reservedContextLimit + : typeof s.metadata.effectiveContextLimit === "number" ? s.metadata.effectiveContextLimit : 0; let est = 0; try { // #1492: same CJK-aware lower bound as armFailureShrink. est = defaultCountTokens(rawBody ?? ""); } catch { est = 0; } - const arm = Math.max(0, Math.min(declared, Number.isFinite(est) ? est : declared)); + const arm = Math.max(0, Math.min(judging, Number.isFinite(est) ? est : judging)); if (arm > 0) { s.stats.lastInputTokens = arm; // #1839: an estimate promoted to "usage" would enter the nudge @@ -1162,7 +1175,7 @@ export async function forward( s.stats.overflowArmTokens = arm; // #1110: guard reads this, not the baseline s.stats.lastInputTokensOrigin = normalizeUpstreamOrigin(upstreamUrl); // #2313: same-route evidence, see the window branch above } - log("warn", `[${s.id}] upstream context overflow (window not parseable, model=${reqModel ?? "unknown"}) — armed emergency shrink at ~${arm} tokens (min of declared ${declared} and payload estimate), nothing learned (#987): ${info.message}`); + log("warn", `[${s.id}] upstream context overflow (window not parseable, model=${reqModel ?? "unknown"}) — armed emergency shrink at ~${arm} tokens (min of judging window ${judging} and payload estimate), nothing learned (#987): ${info.message}`); } // The armed emergency (lastInputTokens) lives in memory only // until scheduled — the error path returns before forward()'s diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 511daf8bdc..c1b80004dd 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -1,7 +1,7 @@ import type { WireProtocol } from "../util.js"; import { estimateRawBodyTokens } from "../preflight.js"; import { imageTokensInParsedBody } from "../image-tokens.js"; -import { reserveOutputHeadroom, shouldReserveOutputHeadroom, systemCarrierTexts } from "../util.js"; +import { reserveOutputHeadroom, systemCarrierTexts } from "../util.js"; import { type ResolvedImageBilling } from "../image-tokens.js"; import { ACP_TOOL_NAMES, ABSORB_TOOL_NAME, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "acp-kernel"; import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../compress-tool.js"; @@ -540,9 +540,12 @@ const SIDE_REQUEST_GUARD_TOLERANCE = 1.15; * from this path. Block here instead of forwarding: estimate the RAW body * (CJK-aware text + image tokens) against the effective window — the declared * modelContextLimit (#987: no learned window exists anymore) minus the output - * reservation on wires where output counts against the window. blocked=false - * with limit<=0 means "window unknown — forward as before". blocked requires - * estimate >= limit x SIDE_REQUEST_GUARD_TOLERANCE (estimator bias). */ + * reservation on routes that carry one (#4205: decided by the caller via + * shouldReserveOutputHeadroom with route+model evidence — the guard stays + * pure mechanics and self-reserves once from the UNRESERVED limit it is + * handed). blocked=false with limit<=0 means "window unknown — forward as + * before". blocked requires estimate >= limit x SIDE_REQUEST_GUARD_TOLERANCE + * (estimator bias). */ export function sideRequestGuard( parsed: unknown, protocol: WireProtocol, @@ -555,6 +558,11 @@ export function sideRequestGuard( * prior) — replaces the internal billing-based estimate when provided so * the guard sees the same image channel every other gate consumes. */ imageReserve?: number, + /** #4205: whether THIS route reserves the output budget from the window — + * policy lives at the call site (shouldReserveOutputHeadroom sees route + + * model evidence the guard cannot); default false keeps non-reserving + * callers byte-identical. */ + reserveHeadroom: boolean = false, ): { blocked: boolean; estimate: number; limit: number } { let limit = modelContextLimit; // #987: no learned window exists, but a usage-grounded arm left by an @@ -565,7 +573,7 @@ export function sideRequestGuard( if (armedLimit > 0 && (limit <= 0 || armedLimit < limit)) limit = armedLimit; const field = outputBudgetField(parsed); const maxOut = (field ? readOutputBudget(parsed as Record, field) : undefined) ?? 0; - if (limit > 0 && shouldReserveOutputHeadroom(protocol)) limit = reserveOutputHeadroom(limit, maxOut, headroomCap); + if (reserveHeadroom && limit > 0) limit = reserveOutputHeadroom(limit, maxOut, headroomCap); const imageMass = imageReserve ?? imageTokensInParsedBody(protocol, parsed, imageBilling, configuredCap); const estimate = estimateRawBodyTokens(parsed) + imageMass; return { blocked: limit > 0 && estimate >= limit * SIDE_REQUEST_GUARD_TOLERANCE, estimate, limit }; diff --git a/src/util.ts b/src/util.ts index c7f2c27718..95f8aba135 100644 --- a/src/util.ts +++ b/src/util.ts @@ -565,18 +565,49 @@ export function appendSystemText( return base ? `${base}\n\n---\n\n${added}` : added; } +/** #4205/#3900: positive evidence that an anthropic-wire route enforces its + * input limit INDEPENDENTLY of max_tokens (separate output budget — reserving + * headroom there would just shift every band down for no safety gain). Two + * shapes carry that evidence: the canonical Anthropic API host (direct Claude + * Code / SDK traffic), or a claude-* model id on the wire (gateway / Bedrock / + * Vertex fronts of real Claude — their billing keeps the separate output + * budget too). Wrapper providers on the anthropic wire (DeepSeek /anthropic) + * bill completion against the SAME window and 400'd at the wall (#3900: + * 1,070,269 + 128,000 vs 1,048,576) — they carry no evidence here, so they + * reserve. A claude-named model on a mis-billing wrapper keeps the documented + * first-shot residual: one 400 per fresh baseline, then the self-heal (#4093) + * and the proven-share learning (#3973) correct it. Evidence-permitlist + * discipline (KDD#9): extend with traffic evidence, never wholesale. */ +const ANTHROPIC_API_HOST = "api.anthropic.com"; +const CLAUDE_MODEL_ID_PREFIX = "claude-"; + +export function hasSeparateOutputBudgetEvidence(origin: string | undefined, modelId: string | undefined): boolean { + if (origin !== undefined) { + try { + if (new URL(origin).hostname.toLowerCase() === ANTHROPIC_API_HOST) return true; + } catch { /* unparseable origin — no evidence */ } + } + if (typeof modelId === "string" && modelId.length > CLAUDE_MODEL_ID_PREFIX.length && modelId.toLowerCase().startsWith(CLAUDE_MODEL_ID_PREFIX)) return true; + return false; +} + /** - * Whether the OUTPUT budget should be reserved from the context window at all. - * Anthropic's Messages API enforces the input limit INDEPENDENTLY of - * max_tokens (the output budget is separate — input up to the window works - * with any max_tokens), so reserving it would shift the nudge/truncate bands - * down by maxOutput on every session with no safety gain. The OpenAI-family - * APIs count output against the window, so the reservation is only needed - * there. Unknown/other protocols reserve (conservative — a missed reservation - * at worst overflows once and the self-heal corrects it). + * Whether the OUTPUT budget should be reserved from the context window for + * THIS request. Every wire reserves by default — unknown/other protocols + * included (conservative: a missed reservation at worst overflows once and + * the self-heal corrects it). The Anthropic Messages API family is the one + * whose canonical routes enforce the input limit INDEPENDENTLY of max_tokens, + * so only routes carrying positive separate-wall evidence + * (hasSeparateOutputBudgetEvidence) are exempt; everything else on the wire + * reserves (#4205: the blanket protocol exemption was what let wrapper + * providers on that wire 400 at the wall, #3900). Absent origin/model + * evidence the call degrades to "reserve", never to "exempt". Proven-share + * overrides (#3973) compose at the call site: live overflow evidence can + * force a reservation even on an exempt route. */ -export function shouldReserveOutputHeadroom(protocol: string | undefined): boolean { - return protocol !== "anthropic"; +export function shouldReserveOutputHeadroom(protocol: string | undefined, origin?: string, modelId?: string): boolean { + if (protocol === "anthropic") return !hasSeparateOutputBudgetEvidence(origin, modelId); + return true; } /** System-carrier texts of a parsed request body, covering all four wire shapes: From e2aa53da68a73ce463fb198568cf3e816aecca97 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 07:42:52 +0800 Subject: [PATCH 2/4] test: update affected suites + regression pin for route-scoped headroom (#4205) --- tests/context-overflow.test.ts | 19 +- tests/fix-arm-headroom-clamp.test.ts | 11 +- tests/fork-nudge-deadzone.test.ts | 5 +- tests/golden/storage-render/anthropic.json | 4 +- tests/golden/storage-render/chat.json | 4 +- tests/golden/storage-render/google.json | 4 +- tests/golden/storage-render/responses.json | 4 +- .../issue4205-output-headroom-caliber.test.ts | 184 ++++++++++++++++++ tests/side-request-isolation.test.ts | 16 +- 9 files changed, 228 insertions(+), 23 deletions(-) create mode 100644 tests/issue4205-output-headroom-caliber.test.ts diff --git a/tests/context-overflow.test.ts b/tests/context-overflow.test.ts index a0f32fbe6f..727a072013 100644 --- a/tests/context-overflow.test.ts +++ b/tests/context-overflow.test.ts @@ -329,13 +329,20 @@ test("reserveOutputHeadroom: degenerate maxOutput >= window stays a no-op even w assert.equal(reserveOutputHeadroom(100_000, 150_000, 0.25), 100_000); }); -// Protocol gate: Anthropic's Messages API enforces the input limit -// independently of max_tokens (separate output budget), so reserving there -// would shift every nudge/truncate band down by maxOutput for no safety gain. -// OpenAI-family APIs count output against the window — reserve there. -test("shouldReserveOutputHeadroom: anthropic exempt, other protocols reserve", () => { - assert.equal(shouldReserveOutputHeadroom("anthropic"), false, "Anthropic: output budget is separate from the context window"); +// #4205: every wire reserves by default. The anthropic family is exempt ONLY +// with positive separate-output-budget evidence (real-Claude routing — +// api.anthropic.com or a claude-* model id): the blanket protocol exemption +// was what let wrapper providers on that wire bill completion against the +// same window and 400 at the wall (#3900). Both directions pinned (#3617). +test("shouldReserveOutputHeadroom: reserve-by-default, real-Claude routing exempt (#4205)", () => { assert.equal(shouldReserveOutputHeadroom("openai"), true); assert.equal(shouldReserveOutputHeadroom("responses"), true); + assert.equal(shouldReserveOutputHeadroom("google"), true); assert.equal(shouldReserveOutputHeadroom(undefined), true, "unknown protocol → conservative (reserve)"); + assert.equal(shouldReserveOutputHeadroom("anthropic"), true, "no route evidence → conservative (reserve)"); + assert.equal(shouldReserveOutputHeadroom("anthropic", "https://api.deepseek.com"), true, "wrapper origin → reserve"); + assert.equal(shouldReserveOutputHeadroom("anthropic", "https://api.anthropic.com", "claude-sonnet-4-5"), false, "canonical Anthropic host → exempt"); + assert.equal(shouldReserveOutputHeadroom("anthropic", "http://127.0.0.1:8787", "claude-test"), false, "claude-* model id → exempt (gateway front)"); + assert.equal(shouldReserveOutputHeadroom("anthropic", "http://127.0.0.1:8787", "DeepSeek-V3.2-Exp"), true, "#3900 shape: deepseek model on the anthropic wire → reserve"); + assert.equal(shouldReserveOutputHeadroom("anthropic", "https://api.anthropic.com.evil.example"), true, "host match is exact — no suffix tricks"); }); diff --git a/tests/fix-arm-headroom-clamp.test.ts b/tests/fix-arm-headroom-clamp.test.ts index b6ca5c10aa..882acb4b99 100644 --- a/tests/fix-arm-headroom-clamp.test.ts +++ b/tests/fix-arm-headroom-clamp.test.ts @@ -201,12 +201,13 @@ test("the stated-window arm is clamped to the headroom-reserved judging limit", s.stats.lastInputTokens < WINDOW, `arm ${s.stats.lastInputTokens} must sit BELOW the stated window ${WINDOW}`, ); - // The reserved judging limit, read exactly the way relay.ts does (a - // `metadata` value is typed loosely, so narrow it before comparing). + // The reserved judging limit, read exactly the way relay.ts does (#4205: + // per-request twin of the unreserved display window; `metadata` values are + // typed loosely, so narrow before comparing). const judgingLimit = - typeof s.metadata.effectiveContextLimit === "number" && s.metadata.effectiveContextLimit > 0 - ? s.metadata.effectiveContextLimit - : 0; + typeof s.metadata.reservedContextLimit === "number" && s.metadata.reservedContextLimit > 0 + ? s.metadata.reservedContextLimit + : typeof s.metadata.effectiveContextLimit === "number" && s.metadata.effectiveContextLimit > 0 ? s.metadata.effectiveContextLimit : 0; assert.ok(judgingLimit > 0, "handle must have recorded the reserved judging limit"); assert.ok( s.stats.lastInputTokens <= judgingLimit, diff --git a/tests/fork-nudge-deadzone.test.ts b/tests/fork-nudge-deadzone.test.ts index 6cb9e7018f..6868f5e306 100644 --- a/tests/fork-nudge-deadzone.test.ts +++ b/tests/fork-nudge-deadzone.test.ts @@ -216,7 +216,10 @@ test("the child's first wire turn sizes on the seeded baseline, not 0 (lane proo const deadline = Date.now() + 5_000; let nudgeLine: string | undefined; for (;;) { - nudgeLine = readLog().split("\n").filter((l) => l.includes(child.id)).find((l) => l.includes("nudge")); + // #4205: the [headroom] line also says "nudge" (it names the + // judge layer) and precedes the decision line on reserving routes — + // exclude it or the filter grabs the wrong line. + nudgeLine = readLog().split("\n").filter((l) => l.includes(child.id)).find((l) => l.includes("nudge") && !l.includes("[headroom]")); if (nudgeLine || Date.now() >= deadline) break; await new Promise((resolve) => setTimeout(resolve, 50)); } diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 9922bf5c88..627cad7a26 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "cf0f44631a155d96c30f5f4c5aed5c9a811627b73fd6ff150bc33bd803b1eb69", + "stateDigest": "2afbea1dbbc9381f8ec7b49a4fa6b5d55b4084ff20304f20a1f51cb120b9a49f", "sectionDigests": { "state": "f93dc1c876c672d4", "blockContents": "dc7d13d250c94bb4", "stats": "c6f1d434975d047d", - "metadata": "385b2c8a1a78693b" + "metadata": "9c05fef8fb761941" }, "stateKeyDigests": { "blocks": "3a454b9a16e4f0b9", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 088fca8f81..c3686433ba 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "fc56136834423d14c5ba7d067e5f6199644a457be99d2b4e67099ed7834a5414", + "stateDigest": "a4abd4cb1cc9ade667c277fb1095641d6135cae0ac2d6ee2905920365f4f56b4", "sectionDigests": { "state": "8390f21336885863", "blockContents": "94a58f96d7c5cfc3", "stats": "24d705b0e39bd13c", - "metadata": "e2e52ff012db9ec9" + "metadata": "68d1e5ca212cef7b" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index dc85099f53..145d5c3f6e 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "78dcd31a4bec1b25e44ffbe71c7d4b73aaddbe0d189c21af6dc5cb2c9a41f14b", + "stateDigest": "9ad5cff9d94aa21024b20aa870e94cd0a62c9dedc452ad7e76d8821a3635f30a", "sectionDigests": { "state": "2e1def3a3234afdc", "blockContents": "473264d9df8ba338", "stats": "900481685c848462", - "metadata": "b7d88f55cd40b838" + "metadata": "2f0093fdaa6fb5a7" }, "stateKeyDigests": { "blocks": "0cfd0b9c0fc53073", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 5170e2a244..2070742004 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "f1f4810c773dbc73a492fc1875dd10d8dcd1a32513b13d74b0fc79791b75a840", + "stateDigest": "f1c5b00ba772868d48c8addcb150a54908eb308f9bd3a4fd46d7a79084effdbc", "sectionDigests": { "state": "db313e8eba2d65fc", "blockContents": "b6701268a0bdcd8a", "stats": "327d3967b8371aa8", - "metadata": "23bf7ae84bfda524" + "metadata": "e4cac45276f335cc" }, "stateKeyDigests": { "blocks": "34b0dc17f8c24595", diff --git a/tests/issue4205-output-headroom-caliber.test.ts b/tests/issue4205-output-headroom-caliber.test.ts new file mode 100644 index 0000000000..fb26f48ce0 --- /dev/null +++ b/tests/issue4205-output-headroom-caliber.test.ts @@ -0,0 +1,184 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; + +// #4205: output-headroom caliber split + route-scoped reservation (redo of the +// reverted #4184, whose global anthropic flip red-lined ~40 tests that pin the +// UNRESERVED window). Two things must hold together: +// 1. CALIBER — `metadata.effectiveContextLimit` stays the unreserved window +// N for every display surface (status panel / acp_status / web); the +// reserved window R the kernel actually judges against is recorded per +// request in `metadata.reservedContextLimit`. Logs carry both numbers. +// 2. ROUTE SCOPE — the anthropic exemption is evidence-based, not a global +// boolean: real-Claude routes (api.anthropic.com host or claude-* model +// id) keep reserving zero; wrapper fronts that bill completion inside the +// input wall (#3900: DeepSeek /anthropic, 1,070,269 + 128,000 vs +// 1,048,576) reserve like every other wire. The #3900 regression shape: +// a payload that fits the wall but not the reserved band must fail fast +// LOCALLY on a reserving route — the doomed body never reaches upstream — +// while the identical payload rides through on an exempt route. + +const WINDOW = 100_000; +const MAX_TOKENS = 20_000; +// Default cap 0.25 x 100_000 = 25_000 > 20_000, so the full max_tokens is +// reserved: R = 80_000. +const RESERVED_WINDOW = WINDOW - MAX_TOKENS; +// 368_000 ASCII chars = exactly 92_000 tokens at the kernel's chars/4 meter — +// inside (R=80_000, N=100_000): over the reserved band, under the wall. +const FILLER = "x".repeat(368_000); + +interface Rig { + proxyPort: number; + upstreamPort: number; + proxy: http.Server; + upstream: http.Server; + /** model id of every MAIN-LANE request that reached the mock upstream + * (preflight's own summary probes carry a different max_tokens and do + * not count — the regression is about the client's body leaving) */ + mainModels: string[]; +} + +async function startRig(): Promise { + const rig: Rig = { proxyPort: 0, upstreamPort: 0, proxy: undefined as unknown as http.Server, upstream: undefined as unknown as http.Server, mainModels: [] }; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + let parsed: { model?: string; max_tokens?: number } = {}; + try { parsed = JSON.parse(raw) as { model?: string; max_tokens?: number }; } catch { /* keep default */ } + const model = parsed.model ?? "unknown"; + if (parsed.max_tokens === MAX_TOKENS) rig.mainModels.push(model); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_s", type: "message", role: "assistant", model, content: [{ type: "text", text: "s" }], stop_reason: "end_turn", usage: { input_tokens: 500, output_tokens: 5 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + rig.upstream = upstream; + const upstreamPort = (upstream.address() as { port: number }).port; + rig.upstreamPort = upstreamPort; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "deepseek-flash": { context: WINDOW }, "claude-x": { context: WINDOW } } } }, + modelContextLimit: WINDOW, + kernelConfig: defaultConfig(WINDOW), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + compat: { roles: {} }, streamErrorShape: "protocol", passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + rig.proxy = proxy; + rig.proxyPort = (proxy.address() as { port: number }).port; + return rig; +} + +async function post(rig: Rig, sid: string, model: string, messages: Array<{ role: string; content: string }>): Promise { + return fetch(`http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sid }, + body: JSON.stringify({ model, max_tokens: MAX_TOKENS, stream: false, messages }), + }); +} + +function meta(sid: string): { effectiveContextLimit?: number; reservedContextLimit?: number } { + const sess = listSessions().find((s) => s.id === sid); + return (sess?.metadata ?? {}) as { effectiveContextLimit?: number; reservedContextLimit?: number }; +} + +async function closeRig(rig: Rig): Promise { + rig.proxy.close(); + rig.proxy.closeIdleConnections?.(); + await once(rig.proxy, "close"); + rig.upstream.close(); + rig.upstream.closeIdleConnections?.(); + await once(rig.upstream, "close"); +} + +test("#4205 caliber: a reserving route records N in effectiveContextLimit and R in reservedContextLimit", async () => { + const rig = await startRig(); + try { + const r = await post(rig, "caliber-wrap", "deepseek-flash", [{ role: "user", content: "hi" }]); + assert.equal(r.status, 200); + await r.text(); + const m = meta("caliber-wrap"); + assert.equal(m.effectiveContextLimit, WINDOW, "display field keeps the unreserved window N"); + assert.equal(m.reservedContextLimit, RESERVED_WINDOW, "decision field records the reserved window R"); + } finally { + await closeRig(rig); + } +}); + +test("#4205 exemption: a real-Claude route reserves nothing, both fields record N", async () => { + const rig = await startRig(); + try { + const r = await post(rig, "caliber-claude", "claude-x", [{ role: "user", content: "hi" }]); + assert.equal(r.status, 200); + await r.text(); + const m = meta("caliber-claude"); + assert.equal(m.effectiveContextLimit, WINDOW); + assert.equal(m.reservedContextLimit, WINDOW, "claude-* model id carries separate-output-budget evidence"); + } finally { + await closeRig(rig); + } +}); + +test("#4205 #3900-class: doomed body fails fast locally on the reserving route, rides through on the exempt route", async () => { + const rig = await startRig(); + try { + // Wrapper front (no separate-output-budget evidence): R = 80_000. + const w1 = await post(rig, "wrap-sess", "deepseek-flash", [{ role: "user", content: "hi" }]); + assert.equal(w1.status, 200); + await w1.text(); + // Usage report from turn 1 settles a usage-grade baseline, so an + // over-band turn 2 is judged locally instead of forwarded for + // evidence (#3146 forward-once only covers unknown baselines). + const w2 = await post(rig, "wrap-sess", "deepseek-flash", [ + { role: "user", content: "hi" }, + { role: "assistant", content: "s" }, + { role: "user", content: FILLER }, + ]); + await w2.text(); + // Local refusal: deterministic exhaustion (nothing foldable) with the + // calibrated meter agreeing the payload is over R. 413 is the + // deterministic verdict; 502 is the relay-dependent variant. Either + // way the body stayed local — the #3900 regression was the body + // reaching upstream and burning a guaranteed 400. + assert.ok(w2.status === 413 || w2.status === 502, `expected local fail-fast, got ${w2.status}`); + assert.deepEqual(rig.mainModels.filter((m) => m === "deepseek-flash"), ["deepseek-flash"], "doomed turn-2 body never left the proxy as a main-lane forward"); + + // Control: identical payload on an exempt route (limit N = 100_000) + // fits and rides through untouched. + const c1 = await post(rig, "ctrl-sess", "claude-x", [{ role: "user", content: "hi" }]); + assert.equal(c1.status, 200); + await c1.text(); + const c2 = await post(rig, "ctrl-sess", "claude-x", [ + { role: "user", content: "hi" }, + { role: "assistant", content: "s" }, + { role: "user", content: FILLER }, + ]); + assert.equal(c2.status, 200, "identical payload fits the unreserved wall on the exempt route"); + await c2.text(); + assert.deepEqual(rig.mainModels.filter((m) => m === "claude-x"), ["claude-x", "claude-x"], "both turns reached upstream on the exempt route"); + } finally { + await closeRig(rig); + } +}); diff --git a/tests/side-request-isolation.test.ts b/tests/side-request-isolation.test.ts index d27a087328..234ada5e07 100644 --- a/tests/side-request-isolation.test.ts +++ b/tests/side-request-isolation.test.ts @@ -550,14 +550,24 @@ test("sideRequestGuard: raw-body fit against declared ∩ armed window minus out assert.equal(sideRequestGuard(body, "anthropic", Math.floor(est / 1.10), undefined, undefined, 1).blocked, false, "within the 15% estimator tolerance → forward"); assert.equal(sideRequestGuard(body, "anthropic", 1_000_000, undefined, undefined, 1, Math.floor(est / 1.15)).blocked, true, "armed smaller (beyond tolerance) → blocks"); assert.equal(sideRequestGuard(body, "anthropic", est + 1, undefined, undefined, 1, 1_000_000).blocked, false, "armed larger than declared is ignored"); - // OpenAI wire: the output budget counts against the window → headroom reserved. + // OpenAI wire: the output budget counts against the window → headroom + // reserved. #4205: the reservation policy is passed from the call site — + // these rows pin the reserving direction explicitly. const oa = { model: MODEL, max_completion_tokens: 2_000, stream: true, messages: [{ role: "user", content: txt }] }; const oaEst = estimateRawBodyTokens(oa); const oaLimit = Math.floor(oaEst / 1.15); - const g = sideRequestGuard(oa, "openai", oaLimit + 2_000, undefined, undefined, 1); + const g = sideRequestGuard(oa, "openai", oaLimit + 2_000, undefined, undefined, 1, 0, undefined, true); assert.equal(g.limit, oaLimit, "limit reduced by max_completion_tokens"); assert.equal(g.blocked, true, "boundary after reservation (with tolerance) blocks"); - assert.equal(sideRequestGuard(oa, "openai", oaEst + 2_001, undefined, undefined, 1).blocked, false); + assert.equal(sideRequestGuard(oa, "openai", oaEst + 2_001, undefined, undefined, 1, 0, undefined, true).blocked, false); + // #4205: wrapper providers on the anthropic wire reserve too — same + // mechanics, policy decided by the caller (route + model evidence). + // The rows above with default (false) pin the non-reserving real-Claude + // route; these pin the reserving wrapper route (#3617 both directions). + const gw = sideRequestGuard(body, "anthropic", Math.floor(est / 1.15) + 100, undefined, undefined, 1, 0, undefined, true); + assert.equal(gw.limit, Math.floor(est / 1.15), "anthropic wrapper route: limit reduced by max_tokens"); + assert.equal(gw.blocked, true, "anthropic wrapper route: boundary after reservation blocks"); + assert.equal(sideRequestGuard(body, "anthropic", est + 101, undefined, undefined, 1, 0, undefined, true).blocked, false, "anthropic wrapper route: within tolerance after reservation forwards"); // Image tokens count toward the estimate. const imgBody = { model: MODEL, max_tokens: 100, messages: [{ role: "user", content: [ { type: "text", text: "z".repeat(4000) }, From b370b965a0acf86b607191073af75b88d696d77a Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 07:57:43 +0800 Subject: [PATCH 3/4] test: align #1531 status-surface assertions with the #4205 caliber split The caliber split moves metadata.effectiveContextLimit to the UNRESERVED window N on every display surface; the reserved band now lives in metadata.reservedContextLimit. The three #1531 subtests in tests/runtime-info.test.ts still pinned the reserved values (229376 / 983040 / 90000) on the plugin status contextLimit field, red-lining CI shard test (ubuntu-latest, 22, 3) with '262144 !== 229376'. Update the expectations to N (262144 / 1048576 / 100000); the entry-wins discriminating power of each subtest is kept (agent-slot 100000 vs conv 262144). --- tests/runtime-info.test.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/runtime-info.test.ts b/tests/runtime-info.test.ts index 65cdb1cf5c..ea4ab336e4 100644 --- a/tests/runtime-info.test.ts +++ b/tests/runtime-info.test.ts @@ -402,8 +402,10 @@ describe("runtime-info in the native-window chain (#955, e2e)", () => { assert.equal(status.pluginAgent, "omp"); assert.equal(status.model, "test-model"); assert.equal(status.windowSource, "runtime-info"); - // 262144 − min(32768, headroom cap × 262144) = 262144 − 32768 - assert.equal(status.contextLimit, 229376); + // #4205: the status surface reports the UNRESERVED window N; the + // reserved band (N − min(maxOut, cap×N) = 229376 here) is recorded in + // metadata.reservedContextLimit for decision consumers. + assert.equal(status.contextLimit, 262144); assert.equal(status.runtimeInfo?.contextWindow, 262144); }); @@ -424,21 +426,23 @@ describe("runtime-info in the native-window chain (#955, e2e)", () => { return (await fetch(`http://127.0.0.1:${h!.proxyPort}/__bili/plugin/status?conversationId=${sid}`)).json() as Promise<{ windowSource: string | null; contextLimit: number | null }>; }; + // #4205: status reports the unreserved window N per entry (the + // reserved bands 229376 / 983040 live in reservedContextLimit). await postReport("astra", 262144, 32768, "sid-a"); const a1 = await ask("astra", "sid-a"); assert.equal(a1.windowSource, "runtime-info"); - assert.equal(a1.contextLimit, 229376); + assert.equal(a1.contextLimit, 262144); await postReport("gemini", 1048576, 65536, "sid-b"); const b1 = await ask("gemini", "sid-b"); assert.equal(b1.windowSource, "runtime-info"); - assert.equal(b1.contextLimit, 983040); + assert.equal(b1.contextLimit, 1048576); // old code: B's report clobbered the single omp slot → A regressed to // registry-peek here. Conversation-scoped storage keeps A intact. const a2 = await ask("astra", "sid-a"); assert.equal(a2.windowSource, "runtime-info"); - assert.equal(a2.contextLimit, 229376); + assert.equal(a2.contextLimit, 262144); }); it("#1531: a per-agent header still wins exclusively over a conversation entry", async () => { @@ -460,8 +464,10 @@ describe("runtime-info in the native-window chain (#955, e2e)", () => { assert.equal(resp.status, 200); const status = await (await fetch(`http://127.0.0.1:${h!.proxyPort}/__bili/plugin/status?conversationId=sid-h`)).json() as { windowSource: string | null; contextLimit: number | null }; assert.equal(status.windowSource, "runtime-info"); - // 100000 − min(10000, cap × 100000) = 90000 — the AGENT-SLOT entry, not the conv entry's 229376 - assert.equal(status.contextLimit, 90000); + // #4205: status reports unreserved N — the AGENT-SLOT entry's 100000, + // not the conv entry's 262144 (reserved bands 90000 / 229376 live in + // reservedContextLimit). + assert.equal(status.contextLimit, 100000); }); it("#1531: no conversation signal + no header still misses (plain clients unchanged)", async () => { From 17508661200662bbbbd67bb232dd12a8dec50b4f Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 08:18:10 +0800 Subject: [PATCH 4/4] test: route external-summary-autofold fixture through the /bili/ prefix (#4205) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bare-path /v1/messages requests missed the routes table, fell back to the option-level window, and read as fallback-derived — where the #282 floor lifts any reserved sub-100K window to 100K. Once anthropic wrapper routes reserve output headroom (#4205), that dilution pushed the control-arm usage from ~84% down to 42%, out of the >=75% pressure band these tests exercise. Routing through the zero-config prefix makes the declared routes-table window authoritative (source=configured, no floor). Complements 6ca565bab (runtime-info status-surface alignment). --- tests/external-summary-autofold.test.ts | 44 +++++++++++++++---------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/tests/external-summary-autofold.test.ts b/tests/external-summary-autofold.test.ts index e88a3fea17..99d0b28708 100644 --- a/tests/external-summary-autofold.test.ts +++ b/tests/external-summary-autofold.test.ts @@ -68,8 +68,14 @@ function bigConversation(pairs = 12): unknown[] { return messages; } -async function postTurn(port: number, session: string, pairs = 12): Promise<{ status: number; body: string }> { - const res = await fetch(`http://127.0.0.1:${port}/v1/messages`, { +// Route through the zero-config /bili// prefix so the routes table +// entry declared in startServer matches (window source=configured): a bare +// /v1/messages request misses the table, falls back to the option-level +// window, and reads as fallback-derived — where the #282 floor lifts any +// reserved sub-100K window to 100K and dilutes the pressure bands these tests +// exercise (#4205). +async function postTurn(port: number, upstreamUrl: string, session: string, pairs = 12): Promise<{ status: number; body: string }> { + const res = await fetch(`http://127.0.0.1:${port}/bili/${upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", @@ -132,13 +138,14 @@ test("auto-fold fires below the window via the external chain, and the model see const port = (server.address() as AddressInfo).port; process.env.E2E_SUM_KEY = "k"; - // ~40K tokens = 80% of the 50K window. On a first turn the growth gate + // ~40K tokens ≈ 82% of the reserved 48_976 judging window (#4205: + // declared 50K − max_tokens 1024). On a first turn the growth gate // is idle by construction (growthSinceReference = 0), so the ONLY classic // nudge path at this size is the pressure band (usage >= 75%), which // bypasses the growth gate — the control arm in the "auto-fold off" test // proves a nudge WOULD fire here without auto-fold. The 8192 growth floor // owns the cadence instead. - const turn = await postTurn(port, "autofold-1", 36); + const turn = await postTurn(port, main.url, "autofold-1", 36); assert.equal(turn.status, 200); // The external chain did the folding (zero model involvement). @@ -198,7 +205,7 @@ test("auto-fold off: no growth fold below the window (growthArmed only when enga const port = (server.address() as AddressInfo).port; process.env.E2E_SUM_KEY = "k"; - const turn = await postTurn(port, "autofold-off"); + const turn = await postTurn(port, main.url, "autofold-off"); assert.equal(turn.status, 200); assert.equal(sum.bodies.length, 0, "external chain must not fold when autoFold is off and the payload fits the window"); const forwarded = main.bodies.at(-1) as { messages?: unknown[] }; @@ -208,17 +215,18 @@ test("auto-fold off: no growth fold below the window (growthArmed only when enga // size with auto-fold off. The usage meter is cold on turn 1 (usage=0%, // growth ref = self — a first-turn nudge is structurally impossible), so // turn 1 only warms the baseline via the stub's honest 42K bill; turn 2 - // sits at 84% usage = pressure band, where the classic cadence MUST + // sits at ≈86% usage (42_000 billed / 48_976 reserved) = pressure band, + // where the classic cadence MUST // inject "Context breakdown:". That proves the first test's absence // assertion targets a live, firing cadence — not an always-idle one. - const warm = await postTurn(port, "autofold-off-pressure", 36); + const warm = await postTurn(port, main.url, "autofold-off-pressure", 36); assert.equal(warm.status, 200); assert.equal(sum.bodies.length, 0, "external chain stays idle below the window even in the pressure band"); - const pressure = await postTurn(port, "autofold-off-pressure", 36); + const pressure = await postTurn(port, main.url, "autofold-off-pressure", 36); assert.equal(pressure.status, 200); assert.equal(sum.bodies.length, 0, "classic nudges never call the external chain"); const pressured = JSON.stringify(main.bodies.at(-1)); - assert.ok(pressured.includes("Context breakdown:"), "control arm: classic cadence nudges at 84% usage without auto-fold"); + assert.ok(pressured.includes("Context breakdown:"), "control arm: classic cadence nudges at ≈86% usage without auto-fold"); }); test("auto-fold fail-open: broken chain forwards the request instead of failing it", async () => { @@ -257,7 +265,7 @@ test("auto-fold fail-open: broken chain forwards the request instead of failing const port = (server.address() as AddressInfo).port; process.env.E2E_SUM_KEY = "k"; - const turn = await postTurn(port, "autofold-fail"); + const turn = await postTurn(port, main.url, "autofold-fail"); assert.equal(turn.status, 200, "growth-fold failure must forward (fail-open), not fail the request"); assert.ok(main.bodies.length >= 1, "payload reached the main upstream"); const text = JSON.stringify(main.bodies.at(-1)); @@ -302,7 +310,7 @@ test("auto-fold backoff: a failed growth fold arms a cooldown — the next turn // Turn 1: the chain is down — the growth fold fails, the request still // forwards (fail-open), and the failure arms the 10-minute backoff. - const turn1 = await postTurn(port, "autofold-backoff"); + const turn1 = await postTurn(port, main.url, "autofold-backoff"); assert.equal(turn1.status, 200, "turn 1 must forward despite the failing chain"); assert.ok(sum.bodies.length >= 1, `turn 1 attempted external summaries, got ${sum.bodies.length}`); @@ -310,7 +318,7 @@ test("auto-fold backoff: a failed growth fold arms a cooldown — the next turn // gate disengages auto-fold BEFORE any attempt, so the external chain is // never called again and the payload forwards raw. const attemptsAfterTurn1 = sum.bodies.length; - const turn2 = await postTurn(port, "autofold-backoff"); + const turn2 = await postTurn(port, main.url, "autofold-backoff"); assert.equal(turn2.status, 200, "turn 2 must forward (backoff = skip, not fail)"); assert.equal(sum.bodies.length, attemptsAfterTurn1, "backoff must skip external calls on the very next turn"); const forwarded2 = main.bodies.at(-1) as { messages?: unknown[] }; @@ -379,7 +387,7 @@ test("auto-fold backoff: a pure queue-drop (zero dispatched attempts) does NOT a // Turn 1: every batch is dropped in the queue. The fold makes zero // progress, yet NOTHING was wrong with the chain — under the pre-#2662 // policy this still armed the 10-minute cooldown. - const turn1 = await postTurn(port, "autofold-qdrop", 36); + const turn1 = await postTurn(port, main.url, "autofold-qdrop", 36); assert.equal(turn1.status, 200, "queue-dropped fold must still forward (fail-open)"); assert.equal(sum.bodies.length, 0, "pure queue-drop: no summary call may reach the endpoint at all"); const forwarded1 = main.bodies.at(-1) as { messages?: unknown[] }; @@ -389,7 +397,7 @@ test("auto-fold backoff: a pure queue-drop (zero dispatched attempts) does NOT a // retries the chain and the fold lands. releaseHolders(); await Promise.all(holders); - const turn2 = await postTurn(port, "autofold-qdrop", 36); + const turn2 = await postTurn(port, main.url, "autofold-qdrop", 36); assert.equal(turn2.status, 200, "turn 2 must forward"); assert.ok(sum.bodies.length >= 1, `backoff must NOT have armed on pure queue-drops; expected external calls on turn 2, got ${sum.bodies.length}`); const forwarded2 = main.bodies.at(-1) as { messages?: unknown[] }; @@ -499,9 +507,9 @@ test("degenerate sub-MIN growth window: classic nudges still fire (no silent sup const port = (server.address() as AddressInfo).port; process.env.E2E_SUM_KEY = "k"; - const warm = await postTurn(port, "deg-win", 2); + const warm = await postTurn(port, main.url, "deg-win", 2); assert.equal(warm.status, 200, "warm-up turn forwards cleanly"); - const turn = await postTurn(port, "deg-win", 2); + const turn = await postTurn(port, main.url, "deg-win", 2); assert.equal(turn.status, 200, "turn 2 must forward cleanly"); assert.equal(sum.bodies.length, 0, "growth folding never engaged (degenerate window) — no external calls"); const text = JSON.stringify(main.bodies.at(-1)); @@ -546,9 +554,9 @@ test("explicit autoFoldTargetTokens clamped to the window: classic nudges still const port = (server.address() as AddressInfo).port; process.env.E2E_SUM_KEY = "k"; - const warm = await postTurn(port, "deg-target", 36); + const warm = await postTurn(port, main.url, "deg-target", 36); assert.equal(warm.status, 200, "warm-up turn forwards cleanly"); - const turn = await postTurn(port, "deg-target", 36); + const turn = await postTurn(port, main.url, "deg-target", 36); assert.equal(turn.status, 200, "turn 2 must forward cleanly"); assert.equal(sum.bodies.length, 0, "clamped target is a no-op growth config — no external calls"); const text = JSON.stringify(main.bodies.at(-1));