diff --git a/src/server/handle.ts b/src/server/handle.ts index 11dbc5d5b5..af6ffb3376 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -337,6 +337,7 @@ async function handleRequest( admission: DecodedRequestAdmission, decodingSignal: AbortSignal, ): Promise { + // #1073/#3245: an absolute-form request addressed to THIS instance's own // endpoint is a fetch of us, not a tunnel — strip the authority so every // downstream stage (admin gate, /bili/ tunnel admission, protocol detection) @@ -378,6 +379,7 @@ async function handleRequest( * reason: that wire's body carries `anthropic_version` and never `model`. * Undefined for every other protocol. */ let vertexModel: string | undefined; + // #903: cost clock starts BEFORE the body read — local= covers body // reception + parse + processTurn + rebuild/serialize, i.e. everything bili // does before handing off. inboundBytes stays the raw wire size (pre-decode). @@ -560,6 +562,7 @@ async function handleRequest( res.end(JSON.stringify({ error: { type: "invalid_request", message: String(err) } })); return; } + // #1757: resolve compat.dropFields once, ahead of the verbatim branches // that forward before reaching the final boundary (they can't use // forward()'s own resolution). Same destination derivation as @@ -605,6 +608,7 @@ async function handleRequest( await forward(req, res, opts, scrubCompatDrop(scrubAnthropicPck(protocol, bodyBuffer, log), compatDropPaths, log), null, core, config, log, route, instanceId, undefined, undefined, undefined, googleModel ?? vertexModel); return; } + // #5795: pre-ingest 429-hold fast path — while the egress governor holds // this origin+credential, refuse BEFORE prepare pays the O(history) // parse/convert/pin cost of a send that will not egress anyway (~6s per @@ -623,6 +627,7 @@ async function handleRequest( streamErrorShape: opts.streamErrorShape, log, })) return; + // #300: bili→bili chain detection. If the inbound request already carries // the x-bili-hop marker, an upstream bili instance already ran the // compression pipeline on it. Processing it again would double-compress @@ -637,6 +642,7 @@ async function handleRequest( ? `[chain] inbound request carries THIS instance's ${BILI_HOP_HEADER} marker (${hopMarker}) — self-loop detected. Passing through without processing; check your upstream config (it may point back to this instance).` : `[chain] inbound request carries ${BILI_HOP_HEADER} from another bili instance (${hopMarker}) — bili→bili chain detected. Passing through without processing to avoid double compression; keep only one bili instance in the chain.`); } + // #1086: byte pre-filter for the ACP-artifact content fallback — the only // remaining signal when a middlebox strips x-bili-hop. The DECISION is // deferred until session identity is resolved below: artifacts in a @@ -650,6 +656,7 @@ async function handleRequest( // entirely when the store is disabled; the hop marker above still catches chains. const artifactSeed = hopMarker === undefined && bodyBuffer.length > 0 && opts.chainContentDetection !== false && getStore().enabled && artifactSeedHit(bodyBuffer); + // #920: legacy opencode-acp sessions bypass the whole pipeline. The thin // plugin stamps this header per request for sessions with acp state on // disk; acp owns their context in-process, so binding/injecting/compressing @@ -676,6 +683,7 @@ async function handleRequest( return; } const countTokens = isCountTokensRequest(req.method ?? "GET", urlPath, bodyBuffer.length > 0); + // Per-request context limit: look up body.model against the per-route model // declaration in providers.json first (same model can have different // windows behind different relays), then the built-in table. Falls back to @@ -694,6 +702,7 @@ async function handleRequest( // Capture the raw-wire verdict before unwrapping or preparing can mutate // parsed. Only these scalars are needed for the post-forward bookkeeping. const rawWireMetadata = protocol ? preparePluginWireMetadata(bodyBuffer, parsed) : undefined; + // #3129: CloudCode/Antigravity v1internal dialect — unwrap the outer envelope // so the pipeline sees a standard GoogleRequest, and decide identity. The // inner request.sessionId is a constant FNV-1a hash, so identity comes from @@ -743,6 +752,7 @@ async function handleRequest( parsed = unwrapped.body; ccMeta = unwrapped.meta; } + // #2824: one cheap raw-text pass decides whether the incoming-image index // (#1995/#2607) can find anything — a provably image-free body skips BOTH // index builds (each one re-converts the full history wire→core). The @@ -763,6 +773,7 @@ async function handleRequest( if (Array.isArray(p.input)) return p.input.length; return null; })(); + // #1395 step 3 (#1421): chain-checkpoint ENFORCEMENT — first-processor- // wins. A verifiable checkpoint means an upstream bili already ran the // pipeline on this exact body: forward it verbatim (pipeline skipped) @@ -807,6 +818,7 @@ async function handleRequest( log("debug", `[chain] evaluation failed (${String(err)}); ignoring`); } } + // #806: a parseable body missing the conversation field used to crash the // kernel's conversation-signal fingerprint (body.messages.find on undefined — // top-level arrays included) and surface as an opaque 502; #806 answered that @@ -860,6 +872,7 @@ async function handleRequest( const inLen = Array.isArray(p.input) ? p.input.length : 0; log("info", `[debug] INCOMING previous_response_id=${hasPrev ? String(p.previous_response_id).slice(0, 16) : "absent"} input_items=${inLen} instructions=${p.instructions !== undefined ? "present" : "absent"}`); } + // Per-request context limit + compression tuning: look up body.model against // the per-route model declaration first, then the built-in table / registry. // Compress settings (global → provider → model) merge deepest-field-wins and @@ -1072,6 +1085,7 @@ async function handleRequest( } } let prepared: Prepared | null = null; + // Whether this request has already been handed to forward(). `prepared` // cannot answer that: the codex compaction_trigger path deliberately skips // the kernel and forwards its own normalized body with `prepared === null`, @@ -1084,6 +1098,7 @@ async function handleRequest( if (routePassthrough && hopMarker === undefined && protocol && parsed) { log("info", `[route-passthrough] ${maskUrlsInText(providerDestUrl)} matches a passthrough route — forwarding verbatim, kernel bypassed`); } + // #300: `hopMarker !== undefined` means an upstream bili already processed // this request — skip the whole pipeline (prepared stays null) so the // passthrough path below forwards it verbatim. #1421: `chainSkip` is the @@ -1205,6 +1220,7 @@ async function handleRequest( clientProvided: !!v1ConvId || !!convHeader, } : undefined; + // #2156: a request that will ride the #388 side passthrough further // down must NEVER participate in persona namespace resolution. // subagentNamespace's first-seen-system anchor is mutable process @@ -1283,6 +1299,7 @@ async function handleRequest( && req.headers["x-bili-ws-lane"] === undefined && hasLeakedBiliToolsOnly(parsed) && detectAcpArtifacts(bodyBuffer, parsed) === null)); + // #1916/#1307/#1314: the dsh persona fingerprint — dsh stamps ONE // conversation id on every model request of a session, INCLUDING the // auto-review classifyRisk() calls (fixed REVIEW_POLICY system + a @@ -2402,6 +2419,7 @@ async function handleRequest( // whichever mode served this session and the re-rank is pure output- // side policy on its result — both proxy and plugin lanes apply it. storeEffectiveSearchPlanAware(session, resolvedSearchPlanAware); + // #1897: omp-style hosts register bili's ACP tools as first-class extension // tools and include them in EVERY model request — including side requests // (title-gen), which carry no host action tools of their own. omp titles with @@ -2693,6 +2711,7 @@ async function handleRequest( // header) can false-positive it — only intent-certain side // identification (declared side agent, a tool-less tiny budget, or the // #2500 server-tool utility shape) diverts under a receipt. + // #2170 measure 1: the decision itself is resolveSideLane() (pure, // truth-table-tested); demotedSide ⊆ lane==="side" by construction. const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: declaredPurpose !== undefined ? `declared purpose=${declaredPurpose} (#6880)` : serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : audioOutputUtility ? "audio-output utility call (#6636)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); @@ -2763,6 +2782,7 @@ async function handleRequest( await forward(req, res, opts, sideBody, sidePrepared, core, reqConfig, log, route, instanceId, affinity); return; } + // #2170 measure 4 (runtime canary): every legitimately side-shaped // LANE-ELIGIBLE request returned inside the lane above. count_tokens, // /responses/compact and session-less (protocol-less) requests are @@ -2777,194 +2797,34 @@ async function handleRequest( session.metadata.sideEffectLeaks = (typeof session.metadata.sideEffectLeaks === "number" ? session.metadata.sideEffectLeaks : 0) + 1; log("warn", `[${session.id}] SIDE-EFFECT LEAK (#2170 canary): ${sideLane.reason} request entered the full pipeline — expected the #388 side passthrough; kernel state pollution likely (cf. #2156/#2164)`); } - // #987: the window is NEVER learned from traffic — no self-heal read - // here. Only the one-shot emergency shrink (armed on the overflow - // itself) reacts to a wrong declared window. - // Reserve the model's OUTPUT budget for this turn from the window so the - // 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). #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. - // #896: headroomCap (compress.outputHeadroomMaxPct, default 0.25, aligned - // with billion-context-pi #207) caps the reservation at headroomCap × window - // — reserved = min(maxOutput, headroomCap × window). Replies longer than the - // reservation overflow once; the self-heal above recovers it next turn. - // Only reserve when it leaves a usable window (maxOutput < window); - // otherwise the request is degenerate (output >= whole window) and the - // self-heal above handles the resulting overflow. Feeds reqConfig (→ - // processTurn `config`), so diagNudge shows the reserved window (no extra log). + + // Output-headroom reservation: the DECLARED window becomes the window + // this turn actually judges against — reserved = min(maxOutput, cap × + // window), floored for fallback-derived windows (#282) and clamped by a + // proven completion share (#3450). Algorithm and rationale live in + // reserveJudgingWindow(). const nativeWindow = reqConfig.modelContextLimit; - const headroomOrigin = normalizeUpstreamOrigin(route?.rewrittenUrl ?? upstreamOrigin); - // #3450 follow-up (#3973): a 400 that stated a completion share disproves - // any separate-wall evidence for this origin+model — judging input against - // the full window would re-earn the same 400 every turn (#3738: nudge and - // preflight must judge on a number the upstream will accept). #4205 made - // the reservation the default for every wire without separate-budget - // evidence, so the proven share now forces the reservation where that - // evidence exists and clamps the resulting window below (see provenWindow). - const provenReserve = provenCompletionReserve( - session.stats, - headroomOrigin, - (parsed as { model?: string }).model, - ); - // A codex live-table window already nets out codex's own output - // headroom; reserving bili's default share on top double-counts it and - // walls the session below codex's own auto-compact point. An explicit - // operator outputHeadroomMaxPct and a proven completion share still apply. - const operatorHeadroomCap = resolvedHeadroomCompress.outputHeadroomMaxPct !== undefined; - const skipForCodexWindow = codexWindowNetsOutput && !operatorHeadroomCap; - const reserveByProtocol = shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel) && !skipForCodexWindow; - if (skipForCodexWindow && provenReserve === 0) { - const skipModel = String(requestModel ?? "?"); - if (!headroomFallbackLogged.has(`${skipModel}|codex-netted`)) { - headroomFallbackLogged.add(`${skipModel}|codex-netted`); - log("info", `[headroom] model=${skipModel}: no output reservation — the codex-aligned window ${reqConfig.modelContextLimit} already nets out codex's own output headroom (effective_context_window_percent); set compress.outputHeadroomMaxPct to reserve anyway`); - } - } - if (reserveByProtocol || provenReserve > 0) { - const p = parsed as Record; - const rawMax = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; - let maxOutput = typeof rawMax === "number" ? rawMax : 0; - // #924: harnesses that omit every output-budget field (Codex native - // Responses sends no max_output_tokens — openai/codex#36180) still get - // the upstream's own default output cap applied, so reserving nothing - // leaves the nudge/truncate bands able to overflow with one long - // reply. Fall back to the model's declared max output — per-route - // config first (operator-declared, outranks auto-fetched data, same - // order as the window resolution #344), then the models.dev registry - // ceiling (cache-only + bundled-snapshot floor, never fetches — the - // source preflight's summary cap uses, #853) — through the SAME capped - // reservation below. Unknown model → 0 → today's behavior. - if (!(maxOutput > 0)) { - // Runtime-info protocol (#955): the plugin reported the client's - // CONFIGURED max output (or the model's declared default, e.g. - // dsh's defaultMaxTokens) for exactly this model — outranks - // configured/registry because it is what the client will - // actually ask the upstream for. Still only a fallback: a - // max_tokens on the wire beat it above. - const fbModel0 = (parsed as { model?: string }).model; - // #1531: same dual lookup as the window chain above — header - // agent wins exclusively, header-less agents resolve by - // conversation signal. - const fbAgent = pluginAgentHeader(req.headers); - const runtimeMax = (pluginHeadersMatchModel(req.headers, fbModel0) ? pluginReportedMaxOutput(req.headers) : undefined) - ?? (fbAgent !== undefined - ? pluginRuntimeInfoFor(fbAgent, fbModel0)?.maxOutput - : pluginRuntimeInfoForConversation(runtimeConversationId(req.headers, parsed, opts.sessionHeader), fbModel0)?.maxOutput); - if (typeof runtimeMax === "number" && runtimeMax > 0) { - maxOutput = runtimeMax; - if (!headroomFallbackLogged.has(`${fbModel0 ?? "?"}|runtime-info`)) { - headroomFallbackLogged.add(`${fbModel0 ?? "?"}|runtime-info`); - log("info", `[headroom] model=${fbModel0 ?? "?"}: request carries no output budget; reserving against runtime-info max output ${runtimeMax} (#955)`); - } - } - } - if (!(maxOutput > 0)) { - // Launcher env channel (#971): the client's OWN config declares - // this model's output ceiling (codex model_max_output_tokens, - // pi/omp maxTokens, opencode limit.output, codebuddy - // maxOutputTokens) — handed over at launch time. Below - // runtime-info (per-request plugin truth) but above the - // generic configured/registry sources, same rank order as the - // window chain's launcher tier (1b). - const fbLauncher = (parsed as { model?: string }).model; - const launcherMax = fbLauncher !== undefined ? launcherMaxOutput(fbLauncher) : undefined; - if (typeof launcherMax === "number" && launcherMax > 0) { - maxOutput = launcherMax; - if (!headroomFallbackLogged.has(`${fbLauncher}|launcher`)) { - headroomFallbackLogged.add(`${fbLauncher}|launcher`); - log("info", `[headroom] model=${fbLauncher}: request carries no output budget; reserving against launcher max output ${launcherMax} (#971)`); - } - } - } - if (!(maxOutput > 0)) { - const fbModel = (parsed as { model?: string }).model; - if (fbModel) { - let host: string | undefined; - try { host = providerDestUrl ? new URL(providerDestUrl).host : undefined; } catch { host = undefined; } - const cfgOut = resolveConfiguredOutputLimit(opts.routes, providerDestUrl, fbModel); - const regOut = peekRegistryOutputLimit(fbModel, host); - const budget = cfgOut !== undefined ? cfgOut : regOut; - if (typeof budget === "number" && budget > 0) { - maxOutput = budget; - const source = cfgOut !== undefined ? "configured" : "registry"; - if (!headroomFallbackLogged.has(`${fbModel}|${source}`)) { - headroomFallbackLogged.add(`${fbModel}|${source}`); - log("info", `[headroom] model=${fbModel}: request carries no output budget; reserving against ${source} max output ${budget} (#924)`); - } - } - } - } - if (provenReserve > 0 && !reserveByProtocol) { - // Proven share outranks the client-side fallbacks: it is what - // the upstream already counted into a rejection on this exact - // origin+model, so window − proven share is the input it accepts. - if (provenReserve > maxOutput) maxOutput = provenReserve; - const proofModel = (parsed as { model?: string }).model ?? "?"; - if (!headroomFallbackLogged.has(`${proofModel}|proven-completion`)) { - headroomFallbackLogged.add(`${proofModel}|proven-completion`); - log("info", `[headroom] model=${proofModel}: reserving the PROVEN completion share ${provenReserve} although ${skipForCodexWindow ? "the codex-aligned window already nets out codex's own output headroom" : `protocol ${protocol ?? "unknown"} is exempt`} (#3450) — this origin+model billed completion inside the wall in an earlier rejection; judging input against ${nativeWindow} - ${provenReserve}`); - } - } - // #2283 sub-defect 1: emit [window-change] when the per-session resolution - // triple (model/source/base) moves mid-session. Placed pre output-headroom so - // #2096's per-request max_tokens reservation can't read as a base change. - { - const curRes = { m: reqModelId ?? "?", s: wsSourceForLog ?? "?", b: reqConfig.modelContextLimit }; - const prevRes = session.metadata?.lastWindowResolution as { m: string; s: string; b: number } | undefined; - if (prevRes !== undefined && (prevRes.m !== curRes.m || prevRes.s !== curRes.s || prevRes.b !== curRes.b)) { - // #5490: landing mid-session on the ANONYMOUS DEFAULT (the new - // model resolved to nothing) is the dangerous shape — a long - // conversation suddenly judged against a guessed base, which - // is how a 1M-resident session reads 1567% and EMERGENCY-folds - // on one picker click. Operator-tuned "default" keeps info: - // that value is owned by the operator, not a guess. - const landsOnAnonymousDefault = curRes.s === "default" && nativeFromFallback; - log(landsOnAnonymousDefault ? "warn" : "info", `[${session.id}] [window-change] ${prevRes.m}/${prevRes.s}/${prevRes.b} -> ${curRes.m}/${curRes.s}/${curRes.b} (mid-session window resolution changed; nudge/preflight now judge against base ${curRes.b} pre-output-headroom, source=${curRes.s})${landsOnAnonymousDefault ? ` LANDING ON THE ANONYMOUS DEFAULT: model=${curRes.m} has no window evidence — the session now judges against the guessed base until it is declared (providers..models.${curRes.m}.context) (#5490)` : ""} (#2283)`); - } - if (session.metadata) session.metadata.lastWindowResolution = curRes; - } - let reserved = reserveOutputHeadroom(reqConfig.modelContextLimit, maxOutput, headroomCap); - // Fallback-derived windows are optimistic guesses: never let the - // output-headroom reservation push the effective window below the - // floor (issue #282: 128k table − 64k max_tokens → 64k effective - // for a 1M-window model). If the real window is smaller, the first - // upstream overflow self-heals it. - if (nativeFromFallback && reserved < FALLBACK_EFFECTIVE_WINDOW_FLOOR) { - log("info", `[${session.id}] fallback context window floored: ${reserved} → ${FALLBACK_EFFECTIVE_WINDOW_FLOOR} (model=${String(requestModel ?? "?")} not authoritatively identified; self-heal corrects it if the real window is smaller)`); - reserved = FALLBACK_EFFECTIVE_WINDOW_FLOOR; - } - // The #896 cap bounds GUESSED budgets; a proven share is measured — - // the upstream already counted it in a rejection on this origin+model — - // so the window may never stay above (declared window − proven share). - if (provenReserve > 0 && nativeWindow > provenReserve) { - const provenWindow = nativeWindow - provenReserve; - if (provenWindow < reserved) reserved = provenWindow; - } - if (reserved !== reqConfig.modelContextLimit) { - reqConfig = { ...reqConfig, modelContextLimit: reserved }; - // #2096: the [window] line above already went out with the - // pre-reservation base — surface the value nudge/preflight - // actually judge against, once per model|value. The clamp's - // native-window guarantee is stated here so the two windows - // can't be read as one in an incident log. - const headroomModel = String(requestModel ?? "?"); - if (!headroomEffectiveLogged.has(`${headroomModel}|${reserved}`)) { - headroomEffectiveLogged.add(`${headroomModel}|${reserved}`); - log("info", `[headroom] model=${headroomModel}: effective window ${nativeWindow} -> ${reserved} (reserved ${nativeWindow - reserved} for max output ${maxOutput}, cap ${headroomCap} via compress.outputHeadroomMaxPct) — nudge/preflight judge input against ${reserved}; the outgoing max_tokens clamp still guards the full ${nativeWindow}; a session overflow arm can judge even lower ([arm-judge]) (#2096, #3837, #5191)`); - } - } - } + reqConfig = reserveJudgingWindow({ + reqConfig, + nativeWindow, + headroomCap, + nativeFromFallback, + codexWindowNetsOutput, + operatorHeadroomCap: resolvedHeadroomCompress.outputHeadroomMaxPct !== undefined, + parsed, + req, + opts, + protocol, + requestModel, + providerDestUrl, + headroomOrigin: normalizeUpstreamOrigin(route?.rewrittenUrl ?? upstreamOrigin), + reqModelId, + windowSource: wsSourceForLog, + session, + log, + }); + + // #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. @@ -2996,6 +2856,7 @@ async function handleRequest( storeEffectiveConfig(session, reqConfig); const durableClassifier = pluginAgent ? durableMessageToolPaths[pluginAgent] : undefined; if (durableClassifier !== undefined) reqConfig = withDurableProtection(reqConfig, durableClassifier); + // acquireInFlight must precede the lock so evictOldest() cannot flush // this session between getSession and lock acquisition (inFlight===0 // window). Released in the outer finally after forward completes. @@ -3154,6 +3015,7 @@ async function handleRequest( if (ccMeta !== undefined) p.commandcode = { meta: ccMeta }; return p; }; + // #332: codex's native remote-compaction request (trigger form) // is dispatched BEFORE prepare/preflight. When it is not // intercepted, preserve what codex sent except for local bili @@ -3186,6 +3048,7 @@ async function handleRequest( log("info", `[${session.id}] codex compaction_trigger request not intercepted (${why}) — forwarding ${normalized ? `with bili summaries normalized (replaced=${replaced}, dropped=${dropped})` : "verbatim"} (no preflight, no rebuild, no window clamp)`); return { body: forwardBody, prepared: null }; } + // #3450: ONE wall per request — on the refold path the // prepare/nudge stage must judge against the same window the // preflight stage folds on. min(declared, stated): the @@ -3236,6 +3099,7 @@ async function handleRequest( } prepared.threadsReforge = undefined; } + // #2607: prepare just assigned numeric refs to this turn's new // messages (archivable media folds by default) — archive their // pixels before preflight or the model can fold them away. @@ -3245,6 +3109,7 @@ async function handleRequest( if (!countTokens && !responsesCompact && protocol && prepared && bodyMayCarryImages) { session.incomingImageIndex = refreshIncomingImageIndex(parsed, protocol, session.state, session.id, session.incomingImageIndex); } + // #2155 self-heal round: one evaluation per request, on the // nudge-carrying prepared result (bypass lanes have nudge // undefined and the hook no-ops inside). Side requests @@ -3295,6 +3160,7 @@ async function handleRequest( requireRecommendation: preparedCs?.requireRecommendation === true, modelRound: !countTokens && !responsesCompact && !prepared.codexForge && !prepared.threadsLane, }); + // A locally-served codex compaction forge never reaches an // upstream: preflight would burn summarization rounds for a // reply that is already forged (#3008 prompt form). @@ -3368,6 +3234,7 @@ async function handleRequest( } prepared = outcome; } + // #1266: capture the CLIENT's raw incoming wire AFTER session // binding so the filename carries the session id — INCOMING↔REQ // dumps pair by id for `bili acp-cache diff`. Moved from the @@ -3396,6 +3263,7 @@ async function handleRequest( await yieldToEventLoop(); return { body: prepared!.body, prepared: prepared! }; }; + // #1884/#2090 (un-armed signed traffic): a request that already // carries a body-covering signature (SDK-HMAC-SHA256 family — // CodeArts APIG, or any gateway-invented scheme the shape-based @@ -3534,3 +3402,223 @@ async function handleRequest( await forward(req, res, opts, scrubCompatDrop(scrubAnthropicPck(protocol, bodyBuffer, log), compatDropPaths, log), null, core, reqConfig, log, route, instanceId, undefined, undefined, undefined, throttleModel); } } + + // #987: the window is NEVER learned from traffic — no self-heal read + // here. Only the one-shot emergency shrink (armed on the overflow + // itself) reacts to a wrong declared window. + // Reserve the model's OUTPUT budget for this turn from the window so the + // 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). #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. + // #896: headroomCap (compress.outputHeadroomMaxPct, default 0.25, aligned + // with billion-context-pi #207) caps the reservation at headroomCap × window + // — reserved = min(maxOutput, headroomCap × window). Replies longer than the + // reservation overflow once; the self-heal above recovers it next turn. + // Only reserve when it leaves a usable window (maxOutput < window); + // otherwise the request is degenerate (output >= whole window) and the + // self-heal above handles the resulting overflow. Feeds reqConfig (→ + // processTurn `config`), so diagNudge shows the reserved window (no extra log). +// The 15 parameters below ARE the real coupling surface of this block: it takes +// no decision, forwards nothing and never touches kernel state — it only reads +// what the pipeline already resolved and returns a tightened Config. Extracting +// it does not decouple anything; it makes that surface visible and testable. +export function reserveJudgingWindow(input: { + reqConfig: Config; + /** The window BEFORE reservation (= the incoming Config's modelContextLimit). + * Later logs and metadata must tell "the wall the client declared" apart + * from "the wall this turn judges against". */ + nativeWindow: number; + headroomCap: number; + nativeFromFallback: boolean; + codexWindowNetsOutput: boolean; + operatorHeadroomCap: boolean; + parsed: unknown; + req: http.IncomingMessage; + opts: ProxyOptions; + protocol: WireProtocol; + requestModel: string | undefined; + providerDestUrl: string; + headroomOrigin: string | undefined; + reqModelId: string | undefined; + windowSource: string | undefined; + session: Session; + log: (level: string, msg: string) => void; +}): Config { + const { + reqConfig: incoming, nativeWindow, headroomCap, nativeFromFallback, codexWindowNetsOutput, operatorHeadroomCap, parsed, req, + opts, protocol, requestModel, providerDestUrl, headroomOrigin, reqModelId, + session, log, windowSource: wsSourceForLog, + } = input; + let reqConfig = incoming; + // #3450 follow-up (#3973): a 400 that stated a completion share disproves + // any separate-wall evidence for this origin+model — judging input against + // the full window would re-earn the same 400 every turn (#3738: nudge and + // preflight must judge on a number the upstream will accept). #4205 made + // the reservation the default for every wire without separate-budget + // evidence, so the proven share now forces the reservation where that + // evidence exists and clamps the resulting window below (see provenWindow). + const provenReserve = provenCompletionReserve( + session.stats, + headroomOrigin, + (parsed as { model?: string }).model, + ); + // A codex live-table window already nets out codex's own output + // headroom; reserving bili's default share on top double-counts it and + // walls the session below codex's own auto-compact point. An explicit + // operator outputHeadroomMaxPct and a proven completion share still apply. + const skipForCodexWindow = codexWindowNetsOutput && !operatorHeadroomCap; + const reserveByProtocol = shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel) && !skipForCodexWindow; + if (skipForCodexWindow && provenReserve === 0) { + const skipModel = String(requestModel ?? "?"); + if (!headroomFallbackLogged.has(`${skipModel}|codex-netted`)) { + headroomFallbackLogged.add(`${skipModel}|codex-netted`); + log("info", `[headroom] model=${skipModel}: no output reservation — the codex-aligned window ${reqConfig.modelContextLimit} already nets out codex's own output headroom (effective_context_window_percent); set compress.outputHeadroomMaxPct to reserve anyway`); + } + } + if (reserveByProtocol || provenReserve > 0) { + const p = parsed as Record; + const rawMax = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; + let maxOutput = typeof rawMax === "number" ? rawMax : 0; + // #924: harnesses that omit every output-budget field (Codex native + // Responses sends no max_output_tokens — openai/codex#36180) still get + // the upstream's own default output cap applied, so reserving nothing + // leaves the nudge/truncate bands able to overflow with one long + // reply. Fall back to the model's declared max output — per-route + // config first (operator-declared, outranks auto-fetched data, same + // order as the window resolution #344), then the models.dev registry + // ceiling (cache-only + bundled-snapshot floor, never fetches — the + // source preflight's summary cap uses, #853) — through the SAME capped + // reservation below. Unknown model → 0 → today's behavior. + if (!(maxOutput > 0)) { + // Runtime-info protocol (#955): the plugin reported the client's + // CONFIGURED max output (or the model's declared default, e.g. + // dsh's defaultMaxTokens) for exactly this model — outranks + // configured/registry because it is what the client will + // actually ask the upstream for. Still only a fallback: a + // max_tokens on the wire beat it above. + const fbModel0 = (parsed as { model?: string }).model; + // #1531: same dual lookup as the window chain above — header + // agent wins exclusively, header-less agents resolve by + // conversation signal. + const fbAgent = pluginAgentHeader(req.headers); + const runtimeMax = (pluginHeadersMatchModel(req.headers, fbModel0) ? pluginReportedMaxOutput(req.headers) : undefined) + ?? (fbAgent !== undefined + ? pluginRuntimeInfoFor(fbAgent, fbModel0)?.maxOutput + : pluginRuntimeInfoForConversation(runtimeConversationId(req.headers, parsed, opts.sessionHeader), fbModel0)?.maxOutput); + if (typeof runtimeMax === "number" && runtimeMax > 0) { + maxOutput = runtimeMax; + if (!headroomFallbackLogged.has(`${fbModel0 ?? "?"}|runtime-info`)) { + headroomFallbackLogged.add(`${fbModel0 ?? "?"}|runtime-info`); + log("info", `[headroom] model=${fbModel0 ?? "?"}: request carries no output budget; reserving against runtime-info max output ${runtimeMax} (#955)`); + } + } + } + if (!(maxOutput > 0)) { + // Launcher env channel (#971): the client's OWN config declares + // this model's output ceiling (codex model_max_output_tokens, + // pi/omp maxTokens, opencode limit.output, codebuddy + // maxOutputTokens) — handed over at launch time. Below + // runtime-info (per-request plugin truth) but above the + // generic configured/registry sources, same rank order as the + // window chain's launcher tier (1b). + const fbLauncher = (parsed as { model?: string }).model; + const launcherMax = fbLauncher !== undefined ? launcherMaxOutput(fbLauncher) : undefined; + if (typeof launcherMax === "number" && launcherMax > 0) { + maxOutput = launcherMax; + if (!headroomFallbackLogged.has(`${fbLauncher}|launcher`)) { + headroomFallbackLogged.add(`${fbLauncher}|launcher`); + log("info", `[headroom] model=${fbLauncher}: request carries no output budget; reserving against launcher max output ${launcherMax} (#971)`); + } + } + } + if (!(maxOutput > 0)) { + const fbModel = (parsed as { model?: string }).model; + if (fbModel) { + let host: string | undefined; + try { host = providerDestUrl ? new URL(providerDestUrl).host : undefined; } catch { host = undefined; } + const cfgOut = resolveConfiguredOutputLimit(opts.routes, providerDestUrl, fbModel); + const regOut = peekRegistryOutputLimit(fbModel, host); + const budget = cfgOut !== undefined ? cfgOut : regOut; + if (typeof budget === "number" && budget > 0) { + maxOutput = budget; + const source = cfgOut !== undefined ? "configured" : "registry"; + if (!headroomFallbackLogged.has(`${fbModel}|${source}`)) { + headroomFallbackLogged.add(`${fbModel}|${source}`); + log("info", `[headroom] model=${fbModel}: request carries no output budget; reserving against ${source} max output ${budget} (#924)`); + } + } + } + } + if (provenReserve > 0 && !reserveByProtocol) { + // Proven share outranks the client-side fallbacks: it is what + // the upstream already counted into a rejection on this exact + // origin+model, so window − proven share is the input it accepts. + if (provenReserve > maxOutput) maxOutput = provenReserve; + const proofModel = (parsed as { model?: string }).model ?? "?"; + if (!headroomFallbackLogged.has(`${proofModel}|proven-completion`)) { + headroomFallbackLogged.add(`${proofModel}|proven-completion`); + log("info", `[headroom] model=${proofModel}: reserving the PROVEN completion share ${provenReserve} although ${skipForCodexWindow ? "the codex-aligned window already nets out codex's own output headroom" : `protocol ${protocol ?? "unknown"} is exempt`} (#3450) — this origin+model billed completion inside the wall in an earlier rejection; judging input against ${nativeWindow} - ${provenReserve}`); + } + } + // #2283 sub-defect 1: emit [window-change] when the per-session resolution + // triple (model/source/base) moves mid-session. Placed pre output-headroom so + // #2096's per-request max_tokens reservation can't read as a base change. + { + const curRes = { m: reqModelId ?? "?", s: wsSourceForLog ?? "?", b: reqConfig.modelContextLimit }; + const prevRes = session.metadata?.lastWindowResolution as { m: string; s: string; b: number } | undefined; + if (prevRes !== undefined && (prevRes.m !== curRes.m || prevRes.s !== curRes.s || prevRes.b !== curRes.b)) { + // #5490: landing mid-session on the ANONYMOUS DEFAULT (the new + // model resolved to nothing) is the dangerous shape — a long + // conversation suddenly judged against a guessed base, which + // is how a 1M-resident session reads 1567% and EMERGENCY-folds + // on one picker click. Operator-tuned "default" keeps info: + // that value is owned by the operator, not a guess. + const landsOnAnonymousDefault = curRes.s === "default" && nativeFromFallback; + log(landsOnAnonymousDefault ? "warn" : "info", `[${session.id}] [window-change] ${prevRes.m}/${prevRes.s}/${prevRes.b} -> ${curRes.m}/${curRes.s}/${curRes.b} (mid-session window resolution changed; nudge/preflight now judge against base ${curRes.b} pre-output-headroom, source=${curRes.s})${landsOnAnonymousDefault ? ` LANDING ON THE ANONYMOUS DEFAULT: model=${curRes.m} has no window evidence — the session now judges against the guessed base until it is declared (providers..models.${curRes.m}.context) (#5490)` : ""} (#2283)`); + } + if (session.metadata) session.metadata.lastWindowResolution = curRes; + } + let reserved = reserveOutputHeadroom(reqConfig.modelContextLimit, maxOutput, headroomCap); + // Fallback-derived windows are optimistic guesses: never let the + // output-headroom reservation push the effective window below the + // floor (issue #282: 128k table − 64k max_tokens → 64k effective + // for a 1M-window model). If the real window is smaller, the first + // upstream overflow self-heals it. + if (nativeFromFallback && reserved < FALLBACK_EFFECTIVE_WINDOW_FLOOR) { + log("info", `[${session.id}] fallback context window floored: ${reserved} → ${FALLBACK_EFFECTIVE_WINDOW_FLOOR} (model=${String(requestModel ?? "?")} not authoritatively identified; self-heal corrects it if the real window is smaller)`); + reserved = FALLBACK_EFFECTIVE_WINDOW_FLOOR; + } + // The #896 cap bounds GUESSED budgets; a proven share is measured — + // the upstream already counted it in a rejection on this origin+model — + // so the window may never stay above (declared window − proven share). + if (provenReserve > 0 && nativeWindow > provenReserve) { + const provenWindow = nativeWindow - provenReserve; + if (provenWindow < reserved) reserved = provenWindow; + } + if (reserved !== reqConfig.modelContextLimit) { + reqConfig = { ...reqConfig, modelContextLimit: reserved }; + // #2096: the [window] line above already went out with the + // pre-reservation base — surface the value nudge/preflight + // actually judge against, once per model|value. The clamp's + // native-window guarantee is stated here so the two windows + // can't be read as one in an incident log. + const headroomModel = String(requestModel ?? "?"); + if (!headroomEffectiveLogged.has(`${headroomModel}|${reserved}`)) { + headroomEffectiveLogged.add(`${headroomModel}|${reserved}`); + log("info", `[headroom] model=${headroomModel}: effective window ${nativeWindow} -> ${reserved} (reserved ${nativeWindow - reserved} for max output ${maxOutput}, cap ${headroomCap} via compress.outputHeadroomMaxPct) — nudge/preflight judge input against ${reserved}; the outgoing max_tokens clamp still guards the full ${nativeWindow}; a session overflow arm can judge even lower ([arm-judge]) (#2096, #3837, #5191)`); + } + } + } + return reqConfig; +} diff --git a/tests/reserve-judging-window.test.ts b/tests/reserve-judging-window.test.ts new file mode 100644 index 0000000000..c7b83b9031 --- /dev/null +++ b/tests/reserve-judging-window.test.ts @@ -0,0 +1,188 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig, type Config } from "acp-kernel"; +import { reserveJudgingWindow } from "../src/server/handle.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import type { Session } from "../src/session.ts"; + +// The output-headroom reservation inside handleRequest(): the DECLARED window +// becomes the window this turn actually judges against. Extracted verbatim +// (readability refactor, no behavior change) so the budget matrix can be pinned +// directly instead of through six server fixtures. +// +// The four rules this file pins, each in both directions: +// #896 reserved = min(maxOutput, cap × window) — the cap bounds GUESSED budgets +// #282 a fallback-derived window never drops below FALLBACK_EFFECTIVE_WINDOW_FLOOR +// #4205 an anthropic route with separate-output-wall evidence reserves NOTHING +// #3450/#3973 a PROVEN completion share outranks that exemption and clamps the +// window to (declared − proven), scoped to the exact origin+model + +const BASE_WINDOW = 200_000; +const UNKNOWN_MODEL = "relay/test-unknown-model"; +const RELAY = "https://relay.example.com"; +const ANTHROPIC = "https://api.anthropic.com"; +const CLAUDE = "claude-sonnet-4-5"; + +type Input = Parameters[0]; + +function reserveWindow( + overrides: Partial = {}, + proven?: { wall: number; origin: string; model: string }, +): { config: Config; session: Session } { + const session = { id: "reserve-window", metadata: {}, stats: {} } as unknown as Session; + if (proven !== undefined) { + Object.assign(session.stats, { + provenCompletionWall: proven.wall, + provenCompletionWallOrigin: proven.origin, + provenCompletionWallModel: proven.model, + }); + } + const window = overrides.nativeWindow ?? BASE_WINDOW; + const model = overrides.requestModel ?? UNKNOWN_MODEL; + const origin = overrides.headroomOrigin ?? RELAY; + const input: Input = { + reqConfig: defaultConfig(window), + nativeWindow: window, + headroomCap: 0.25, + nativeFromFallback: false, + codexWindowNetsOutput: false, + operatorHeadroomCap: false, + parsed: { model, max_tokens: 64_000 }, + req: { headers: {} } as unknown as http.IncomingMessage, + opts: { routes: [], sessionHeader: "x-session-id" } as unknown as ProxyOptions, + protocol: "openai", + requestModel: model, + providerDestUrl: `${origin}/v1/chat/completions`, + headroomOrigin: origin, + reqModelId: model, + windowSource: "test", + session, + log: () => {}, + ...overrides, + }; + return { config: reserveJudgingWindow(input), session }; +} + +test("#896 the headroom cap bounds the reservation, not max_tokens", () => { + // 64k requested, cap = 25% of a 200k window = 50k → the CAP is what binds. + const { config } = reserveWindow(); + assert.equal(config.modelContextLimit, 150_000); +}); + +test("#896 max_tokens below the cap is what binds", () => { + const { config } = reserveWindow({ parsed: { model: UNKNOWN_MODEL, max_tokens: 8_000 } }); + assert.equal(config.modelContextLimit, 192_000); +}); + +test("#896 no output budget anywhere → no reservation and no crash", () => { + // Runtime-info, launcher, configured and registry tiers all miss for an + // unknown model on an unconfigured route: the window must survive intact + // rather than being shrunk by a guessed budget. + const { config } = reserveWindow({ parsed: { model: UNKNOWN_MODEL } }); + assert.equal(config.modelContextLimit, BASE_WINDOW); +}); + +test("#282 a fallback-derived window never drops below the floor", () => { + // 128k table window − min(64k, 25% = 32k) = 96k, which the floor lifts to 100k. + const { config } = reserveWindow({ nativeWindow: 128_000, nativeFromFallback: true }); + assert.equal(config.modelContextLimit, 100_000); +}); + +test("#282 the floor does NOT apply to an authoritative window", () => { + // Same arithmetic without the low-confidence flag: 96k stands. + const { config } = reserveWindow({ nativeWindow: 128_000 }); + assert.equal(config.modelContextLimit, 96_000); +}); + +test("#282 the floor only LIFTS, it never caps a wide fallback window", () => { + // A fallback-derived window whose reservation still leaves it above the + // floor must keep its own value — an unconditional floor would collapse a + // 400k guess to 100k and over-compress a session that fit perfectly. + const { config } = reserveWindow({ nativeWindow: 400_000, nativeFromFallback: true }); + assert.equal(config.modelContextLimit, 336_000); +}); + +test("#4205 an exempt anthropic route reserves nothing", () => { + const { config } = reserveWindow({ + protocol: "anthropic", + headroomOrigin: ANTHROPIC, + requestModel: CLAUDE, + reqModelId: CLAUDE, + providerDestUrl: `${ANTHROPIC}/v1/messages`, + }); + assert.equal(config.modelContextLimit, BASE_WINDOW); +}); + +test("#4205 an anthropic-shaped route WITHOUT separate-wall evidence still reserves", () => { + // Wrapper providers on the anthropic wire bill completion inside the wall + // (#3900): absent evidence the call degrades to "reserve", never "exempt". + const { config } = reserveWindow({ + protocol: "anthropic", + headroomOrigin: RELAY, + requestModel: UNKNOWN_MODEL, + reqModelId: UNKNOWN_MODEL, + }); + assert.equal(config.modelContextLimit, 150_000); +}); + +test("#3450/#3973 a proven completion share outranks the exemption and clamps the window", () => { + // Exempt route, but this origin+model was already rejected while billing a + // 150k completion inside the wall: the accepted input ceiling is + // window − proven = 50k, which must beat the cap path's 150k. + const { config } = reserveWindow( + { + protocol: "anthropic", + headroomOrigin: ANTHROPIC, + requestModel: CLAUDE, + reqModelId: CLAUDE, + providerDestUrl: `${ANTHROPIC}/v1/messages`, + }, + { wall: 150_000, origin: ANTHROPIC, model: CLAUDE }, + ); + assert.equal(config.modelContextLimit, 50_000); +}); + +test("#3450 a proven share is scoped to its own origin+model", () => { + // Same session proof, different model: the exemption stands, nothing is + // reserved, and no unrelated upstream inherits the clamp. + const { config } = reserveWindow( + { + protocol: "anthropic", + headroomOrigin: ANTHROPIC, + requestModel: CLAUDE, + reqModelId: CLAUDE, + providerDestUrl: `${ANTHROPIC}/v1/messages`, + }, + { wall: 150_000, origin: ANTHROPIC, model: "claude-opus-4" }, + ); + assert.equal(config.modelContextLimit, BASE_WINDOW); +}); + +test("#2283 the resolution triple is stamped with the PRE-reservation base", () => { + // The [window-change] diagnostic compares model/source/base across turns; + // recording the reserved value here would report a change on every turn + // whose max_tokens differs. + const { session } = reserveWindow(); + assert.deepEqual(session.metadata.lastWindowResolution, { + m: UNKNOWN_MODEL, + s: "test", + b: BASE_WINDOW, + }); +}); + +test("#2283 no stamp when the reservation is skipped entirely", () => { + // Exempt route takes the early skip: nothing was judged, so nothing is + // recorded and the next turn cannot read a phantom base change. + const { session } = reserveWindow({ + protocol: "anthropic", + headroomOrigin: ANTHROPIC, + requestModel: CLAUDE, + reqModelId: CLAUDE, + providerDestUrl: `${ANTHROPIC}/v1/messages`, + }); + assert.equal(session.metadata.lastWindowResolution, undefined); +});