diff --git a/reference/architecture.md b/reference/architecture.md index f02e4afefa..477af01d9e 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -304,6 +304,7 @@ billion-context/ │ ├── remote-notices.ts # Remote announcement feed watcher: fail-open polling, last-known-good snapshot (#5548) │ ├── request-membership.ts # Settle-side membership probe: is this usage report ours (#4805) │ ├── resolver-wedge.ts # Persistent local DNS resolver wedge detector + alarm (#5221) +│ ├── responses-system-sections.ts # Single source of truth for the Responses-wire developer-section join: steady prepare + round-2 rebuild share one separator (#6922) │ ├── responses-tool-output.ts # Responses tool-output image parts: projection + patch helpers │ ├── rollout.ts # Feature rollout: per-install uuid, basis-point buckets, nested cohorts (#3062) │ ├── runaway-guard.ts # Intrinsic stream terminator for runaway tag/codepoint enumeration (#2346) diff --git a/src/server.ts b/src/server.ts index 7255880683..30e1247f10 100644 --- a/src/server.ts +++ b/src/server.ts @@ -55,7 +55,7 @@ import { prepareResponses, prepareResponsesCompact } from "./server/prepare-resp import { FORCE_TEXT_PROTOCOL } from "./server/inject.js"; export { isChatGptCodexUpstream, isCodexResponsesLite, resolvePromptCacheKey, shouldInjectPromptCacheKey } from "./server/prepare-responses.js"; import { biliToolsDeclaredOnWire, countBiliToolUses, evaluateSelfHealRound, nudgeSuppressed, pluginLaneDegraded, pluginLaneRestore } from "./session-self-heal.js"; -import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, type NudgeStamps, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback } from "./session.js"; +import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, type NudgeStamps, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, applyPostRebuildBaselineAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback } from "./session.js"; import type { SeparatorManifest } from "./server/responses-ordering.js"; import { detectStaleInstall, isVersionNewer } from "./update.js"; import { decideEaddrinuse, eaddrinuseExitCode, isPortBusyError, postBindCanLadder, UPGRADE_REPLACEMENT_ENV, type FailFastReason } from "./listen-policy.js"; @@ -4374,10 +4374,7 @@ export async function preflightCompressIfNeeded( const rebuiltImageTokens = imageReserveFor(session, rebuilt.protocol, rebuilt.body, opts, upstreamUrl); remainingImageTokens = rebuiltImageTokens; reducedImageWire = result.compressedRanges > 0 && rebuiltImageTokens < imageTokens; - if (rebuiltTextSize > session.stats.lastInputTokens) { - session.stats.lastInputTokens = rebuiltTextSize; - session.stats.lastInputTokensSource = "estimate"; - } + applyPostRebuildBaselineAnchor(session, rebuiltTextSize); // Same calibrated caliber as the trigger above — gate, per-round exit // and this final fit must judge the payload on one scale (#1933 F1). // The baseline anchor above stays raw deliberately: it is a floor for diff --git a/src/server/budget.ts b/src/server/budget.ts index fe1fa22c22..5810dc9613 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -10,15 +10,16 @@ export { outputBudgetField, readOutputBudget, writeOutputBudget }; // #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). 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. +// total instead of clamping). Wired into ALL FOUR prepare lanes — openai, +// responses, google, and anthropic (#5880 added the anthropic site: wrapper +// providers on that wire bill completion against the SAME stated window as +// input — DeepSeek's 400 quotes "N in the messages, M in the completion" +// against one wall — which is exactly the shape this clamp exists for; for +// canonical Anthropic, whose input limit is enforced independently of +// max_tokens, the requested budget already rides under the window and the +// clamp is a no-op). 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; // #5278: safety inflation applied to the planning estimate BEFORE sizing both diff --git a/src/server/handle.ts b/src/server/handle.ts index 11dbc5d5b5..ee2271775a 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -3132,7 +3132,7 @@ async function handleRequest( return await prepareGoogle(work as GoogleRequestBody, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, nativeWindow, googleModel ?? v1Model, googlePathKind(urlPath) === "stream-generate", visibilityMarkers, upstreamOrigin, req, decide); } const preparedOut = protocol === "anthropic" - ? await prepareAnthropic(work as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, reasoningCfg, visibilityMarkers, requestModel, decide) + ? await prepareAnthropic(work as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, visibilityMarkers, requestModel, decide) : protocol === "openai" ? await prepareOpenai(work as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, reqSurface, log, session, pluginMode, upstreamOrigin, nativeWindow, reasoningCfg, visibilityMarkers, route?.rewrittenUrl, decide, bodyText) : responsesCompact diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 79193300d8..5f8a2c9dbe 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -8,7 +8,8 @@ import { captureThreadsTurn, estimateThreadsTokens, isThreadsBody, threadsReforg import type { CompressReasoningConfig } from "../reasoning-drop.js"; import type { ProxyOptions } from "../config.js"; import { pluginAgentHeader } from "../plugin.js"; -import { applyReasoningDrop, dedupeAdjacentOutputConfig, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js"; +import { applyReasoningDrop, dedupeAdjacentOutputConfig, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js"; +import { currentCalibrationFactor } from "../util.js"; import { recordNudgeObservation } from "../context-analysis.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { emitNudgeInjected, nudgeDebugMeta } from "../nudge-debug.js"; @@ -40,7 +41,7 @@ import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; import { isAutoModeClassifier } from "../auto-mode-forge.js"; -import { countSystemAndToolsTokens, emergencyNudge, emergencyNudgeArmed, projectThinkingMass } from "./budget.js"; +import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, projectThinkingMass } from "./budget.js"; import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { effectiveBlockOpsEnabled } from "./prepare-responses.js"; import { injectSystem, injectTool } from "./inject.js"; @@ -63,6 +64,7 @@ export async function prepareAnthropic( session: Session, pluginMode: boolean, upstreamOrigin: string, + nativeWindow: number, reasoning: CompressReasoningConfig | undefined, visibilityMarkers: boolean, /** The route's resolved model, when the caller already has one. On the @@ -693,6 +695,14 @@ export async function prepareAnthropic( // main-scale tiebreak sees this request's own upper bound. probeRequestMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0); if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin; + // #5880: egress output-budget clamp, same call-site family as the openai/ + // responses/google lanes (prepare-openai.ts). Wrapper providers on this + // wire bill completion against the SAME stated window as input (DeepSeek's + // 400: "N in the messages, M in the completion" against one wall), so an + // input-only fit judgment must be backed by an output clamp here too; for + // canonical Anthropic (separate input/output limits) the requested + // max_tokens already rides under the window and the clamp is a no-op. + clampOutgoingOutput(rebuilt as Record, "max_tokens", { systemText: extractSystem(systemOut), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserve, kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: upstreamOrigin, ledgerFloorTokens: dshLedgerFloorTokens(session.metadata), knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, upstreamOrigin, opts.sessionHeader, modelIdOf(parsed)) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; }); if (nudgeOutcome === "injected") recordNudgeInject(session, nudge); return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } diff --git a/src/session.ts b/src/session.ts index f518aa9ca0..c405868cc2 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1192,6 +1192,22 @@ export function setPostRebuildAnchor(session: Session, tokens: number): void { markDirty(session); } +// #1987 post-rebuild baseline anchor, hardened by #5880: an upstream-proven +// overflow-arm tag must NEVER be demoted to estimate-grade by a local payload +// estimate — demoting drops the evidence floor that keeps an armed session +// folding instead of trusting optimistic estimates (same doctrine as +// armFailureShrink, relay.ts #2484). In the #5880 incident the caliber delta +// between the netted arm value and a fresh estimate of the rebuilt view tipped +// the inequality on every fold turn, silently disarming the session so its +// estimate-only forwards 400'd turn after turn. +export function applyPostRebuildBaselineAnchor(session: Session, rebuiltTextSize: number): void { + if (session.stats.lastInputTokensSource === "overflow-arm") return; + if (rebuiltTextSize > session.stats.lastInputTokens) { + session.stats.lastInputTokens = rebuiltTextSize; + session.stats.lastInputTokensSource = "estimate"; + } +} + /** Metadata is persisted user-editable JSON — re-validate on read; a corrupt * stamp degrades to "no anchor" (legacy sizing) instead of poisoning the meter. */ export function postRebuildAnchorTokens(session: Session | undefined): number { diff --git a/tests/issue2929-codex-fold-double-miss.test.ts b/tests/issue2929-codex-fold-double-miss.test.ts index 81b61ca727..7419629dc1 100644 --- a/tests/issue2929-codex-fold-double-miss.test.ts +++ b/tests/issue2929-codex-fold-double-miss.test.ts @@ -34,16 +34,17 @@ type Item = Record; const SUMMARY_MARKER = "[Compressed conversation section]"; const NUDGE_MARKER = "Compressible ranges ("; -// Indices verified against the kernel's nudge gate with nudgeGrowthTokens=2000 -// and ~9KiB filler turns. Pre-#4521 (master) the injections landed on -// requests {7,8,9}; since #4521 the armed-but-silent status line rides an -// ephemeral carrier on the gated turns and its shown-marker feeds the nudge -// gate back, so the schedule shifts to {4,6,8,9} — the fold/proxy-tool-round -// mechanics under test are unchanged, only this probe's anchor re-calibrates -// to the next nudge-carrying request pair (6 → tool round, 7 → fold round). -const PROXY_TOOL_AT = 6; // upstream request index whose response = acp_status call (round 1) -const FOLD_AT = 7; // upstream request index whose response = compress call (round 2) -// FOLD_AT+1 = post-fold refreshed re-request (round 3); FOLD_AT+2 = next steady client turn. +// The fold turn is materialized SCHEDULE-INDEPENDENTLY: the fake upstream +// arms on the first request carrying the ephemeral nudge (with >=11 tagged +// refs to fold) and folds on the re-request that follows the acp_status +// round. A hardcoded request-index anchor broke every time the nudge gate's +// token schedule shifted — #4521's status-line feedback ({7,8,9} → {4,6,8,9}), +// #6928's status-line removal, and #4313's timestamp-rendering default flip +// (the suite preload pin BILI_MESSAGE_TIMESTAMPS="0" changes the wire shape +// and hence the schedule: OFF {4,13} vs ON {3,5,11,14} — direct local runs +// without the preload diverge from CI). The probe also pins +// compress.messageTimestamps:false so its wire shape is hermetic regardless +// of how the suite is launched. const TOTAL_TURNS = 13; const FILLER = (seed: number, kb: number): string => { @@ -120,6 +121,13 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl try { const captured: Array<{ url: string; body: string }> = []; + // Object (not bare lets) so the outer-scope reads aren't narrowed to + // the initial "idle" — the mutations happen inside the request callback. + const fold = { + state: "idle" as "idle" | "armed" | "folded", + armIdx: -1, // upstream request index that carried the nudge (acp_status round) + foldIdx: -1, // upstream request index that carried the compress call + }; upstream = http.createServer((req, res) => { const chunks: Buffer[] = []; req.on("data", (c: Buffer) => chunks.push(c)); @@ -127,15 +135,21 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl const idx = captured.length; const body = Buffer.concat(chunks).toString("utf8"); captured.push({ url: req.url ?? "", body }); - if (idx === PROXY_TOOL_AT) { - // Round 1 of the fold turn: model asks for proxy status — the - // loop executes it locally and re-requests (round 2). + const input = (JSON.parse(body) as { input?: Item[] }).input ?? []; + if (fold.state === "idle" && hasNudge(input) && refsOf(input).length >= 11) { + // Round 1 of the fold turn: the first nudge-carrying request + // with enough refs — model asks for proxy status, the loop + // executes it locally and re-requests (round 2). + fold.state = "armed"; + fold.armIdx = idx; functionCallSse(res, `resp_${idx}`, "acp_status", "{}", `call_st_${idx}`); return; } - if (idx === FOLD_AT) { - const prev = (JSON.parse(captured[idx - 1]!.body) as { input?: Item[] }).input ?? []; - const uniq = refsOf(prev); + if (fold.state === "armed") { + // Round 2: the re-request after the acp_status round — fold. + fold.state = "folded"; + fold.foldIdx = idx; + const uniq = refsOf(input); assert.ok(uniq.length >= 11, `fold round: expected >=11 tagged refs, saw ${uniq.length}`); const args = JSON.stringify({ content: [ { startId: uniq[3]!, endId: uniq[5]!, topic: "F1A", summary: `F1A summary: ${FILLER(1, 1)}` }, @@ -144,10 +158,9 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl functionCallSse(res, `resp_${idx}`, "compress", args, `call_cmp_${idx}`); return; } - const parsed = JSON.parse(body) as { input?: Item[] }; let label = "0"; - for (let i = (parsed.input ?? []).length - 1; i >= 0; i--) { - const it = parsed.input![i]!; + for (let i = input.length - 1; i >= 0; i--) { + const it = input[i]!; if (it.type === "message" && it.role === "user" && typeof it.content === "string") { const m = (it.content as string).match(/Turn (\d+):/); if (m) { label = m[1]!; break; } @@ -169,7 +182,7 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 200_000 } } } }, modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), - compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 2000 }, + compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 2000, messageTimestamps: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: true, @@ -223,17 +236,18 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl } const inputs = captured.map((c) => (JSON.parse(c.body) as { input?: Item[] }).input ?? []); - assert.ok(inputs.length >= FOLD_AT + 3, `expected >= ${FOLD_AT + 3} upstream requests, got ${inputs.length}`); + assert.ok(fold.state === "folded", `scenario miss: the nudge never fired on a request with >=11 refs — probe cannot materialize the #2929 trigger; adjust nudgeGrowthTokens/filler`); + assert.ok(inputs.length >= fold.foldIdx + 3, `expected >= ${fold.foldIdx + 3} upstream requests, got ${inputs.length}`); // (0) Scenario materialization: the nudge must have been injected into - // the PRE-FOLD original request (production shape: nudge armed -> model + // the arming (pre-fold) request (production shape: nudge armed -> model // calls proxy tool -> compress). Without it this probe does not cover // the #2929 trigger. - assert.ok(hasNudge(inputs[PROXY_TOOL_AT]!), `scenario miss: no ephemeral nudge in the pre-fold request (inputs[${PROXY_TOOL_AT}]) — probe does not cover the production trigger; adjust nudgeGrowthTokens/filler`); - console.log(`probe: nudge confirmed in pre-fold request; total upstream requests=${inputs.length}`); + assert.ok(hasNudge(inputs[fold.armIdx]!), `scenario miss: no ephemeral nudge in the arming request (inputs[${fold.armIdx}]) — probe does not cover the production trigger`); + console.log(`probe: nudge at inputs[${fold.armIdx}], fold at inputs[${fold.foldIdx}]; total upstream requests=${inputs.length}`); // (1) The post-fold re-request carries the in-place summary(s). - const rPost = inputs[FOLD_AT + 1]!; + const rPost = inputs[fold.foldIdx + 1]!; const postSums = summaryIndices(rPost); assert.ok(postSums.length >= 1, "post-fold re-request must carry the in-place summary"); @@ -241,7 +255,7 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl // the post-fold re-request up to and including the summary anchor. In // production this is where the chain breaks (next turn hits only the // instructions baseline). - const next = inputs[FOLD_AT + 2]!; + const next = inputs[fold.foldIdx + 2]!; const nextSums = summaryIndices(next); assert.ok(nextSums.length >= 1, `next steady turn lost the summary entirely (summaries: ${nextSums.length}) — fold did not survive to the client turn`); assert.equal(nextSums[0], postSums[0], `summary anchor drifted: re-request item[${postSums[0]}] vs next turn item[${nextSums[0]}]`); diff --git a/tests/issue5880-anthropic-clamp-arm.test.ts b/tests/issue5880-anthropic-clamp-arm.test.ts new file mode 100644 index 0000000000..d12ccb4aad --- /dev/null +++ b/tests/issue5880-anthropic-clamp-arm.test.ts @@ -0,0 +1,253 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +// #5880: two independent defects let a session bili believes fits sail past the +// upstream wall on the ANTHROPIC wire (wrapper providers like DeepSeek bill the +// completion against the SAME window — their 400 says so): +// A. the post-preflight-fold anchor write (server.ts) overwrote an ACTIVE +// overflow-arm evidence tag with an estimate-grade one whenever the fresh +// rebuilt-view estimate measured slightly above the netted arm value — +// every successful fold silently disarmed the evidence floor, so the next +// turn fit-forwarded on a stale optimistic estimate until the 400 re-armed +// it: a livelock (fold → forward → 400 → re-arm → repeat). +// B. the outgoing max_tokens clamp had NO call site on the anthropic lane +// (budget.ts wired openai/responses/google only) — even with an honest +// estimate, requested input+output could exceed the native window. +// These tests pin both: the arm tag survives the anchor write (and unarmed +// sessions keep the #1987 behavior), and the anthropic lane clamps max_tokens +// against the RAW native window exactly like the other lanes do (#2096 +// semantics — the headroom-adjusted window feeds nudge/preflight, the raw one +// guards the egress budget). + +import { createInitialState } from "acp-kernel"; +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 { setLogCapture } from "../src/logger.ts"; +import { applyPostRebuildBaselineAnchor } from "../src/session.ts"; +import type { Session } from "../src/session.ts"; + +// ---------- Part 1: unit — the post-rebuild anchor must not demote an arm ---------- + +function makeSession(lastInputTokens: number, source: Session["stats"]["lastInputTokensSource"]): Session { + const now = Date.now(); + return { + id: "s5880-unit", + meta: {}, + metadata: {}, + state: createInitialState(), + stats: { + requests: 1, + tokensSaved: 0, + inputTokens: 0, + cachedTokens: 0, + outputTokens: 0, + cacheSamples: 0, + lastInputTokens, + lastInputTokensSource: source, + compressCreditTokens: 0, + contextTokens: lastInputTokens, + retrieveCalls: 0, + retrieveHits: 0, + retrieveMisses: 0, + storedBytes: 0, + storeBytesSaved: 0, + rangeRestores: 0, + }, + pendingRetrievals: [], + createdAt: now, + lastSeen: now, + blockContents: new Map(), + inFlight: 0, + persisted: false, + } as unknown as Session; +} + +test("#5880 unit: an active overflow-arm tag survives the post-rebuild anchor write", () => { + // Incident shape: the arm holds the netted measured value; the fresh + // rebuilt-view estimate measures slightly HIGHER (caliber delta) — the old + // inequality-based write demoted the tag to "estimate" here. + const s = makeSession(783_000, "overflow-arm"); + applyPostRebuildBaselineAnchor(s, 783_353); + assert.equal(s.stats.lastInputTokensSource, "overflow-arm", "armed tag must not be demoted"); + assert.equal(s.stats.lastInputTokens, 783_000, "arm value must not be overwritten by the estimate"); +}); + +test("#5880 unit: unarmed sessions keep the #1987 anchor behavior (both directions)", () => { + const up = makeSession(100_000, "estimate"); + applyPostRebuildBaselineAnchor(up, 120_000); + assert.equal(up.stats.lastInputTokens, 120_000, "larger rebuilt view must still anchor (#1987)"); + assert.equal(up.stats.lastInputTokensSource, "estimate"); + + const down = makeSession(100_000, "estimate"); + applyPostRebuildBaselineAnchor(down, 80_000); + assert.equal(down.stats.lastInputTokens, 100_000, "smaller rebuilt view must not lower the floor"); +}); + +test("#5880 unit: usage-grade baselines keep the pre-existing #604 overwrite behavior", () => { + // Only the overflow-arm tag is protected; a newer larger local measurement + // still retires an older usage sample (the relay path does the same). + const u = makeSession(100_000, "usage"); + applyPostRebuildBaselineAnchor(u, 120_000); + assert.equal(u.stats.lastInputTokens, 120_000); + assert.equal(u.stats.lastInputTokensSource, "estimate", "usage→estimate overwrite stays #604 behavior"); +}); + +// ---------- Part 2: e2e — the anthropic lane clamps max_tokens near the wall ---------- + +const WINDOW = 200_000; +const MODEL = "ds-flash"; + +// #5278/#2096 derivation kept here so the cap math stays testable end-to-end. +const expectedCap = (input: number, window: number): number => { + const safeEst = Math.ceil(input * 1.12); + const margin = Math.max(2048, Math.ceil(safeEst * 0.05)); + return Math.floor(window - safeEst - margin); +}; + +function filler(chars: number): string { + const unit = "the quick brown fox jumps over the lazy dog. "; + let s = ""; + while (s.length < chars) s += unit; + return s.slice(0, chars); +} + +function anthropicSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +const anthropicSseReply = (): string => + anthropicSse("message_start", { type: "message_start", message: { id: "m1", role: "assistant" } }) + + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }) + + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } }) + + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }) + + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 2 } }) + + anthropicSse("message_stop", { type: "message_stop" }); + +interface Rig { + proxyPort: number; + upstreamPort: number; + bodies: Array<{ url: string; json: Record }>; + closeAll(): Promise; +} + +async function startHarness(): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const bodies: Rig["bodies"] = []; + const upstream = http.createServer((req, res) => { + let raw = ""; + req.on("data", (c) => { raw += c; }); + req.on("end", () => { + try { bodies.push({ url: req.url ?? "", json: JSON.parse(raw) }); } catch { /* ignore */ } + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(anthropicSseReply()); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + 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: { [MODEL]: { context: WINDOW } } } }, + modelContextLimit: WINDOW, + kernelConfig: defaultConfig(WINDOW), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return { + proxyPort: (proxy.address() as { port: number }).port, + upstreamPort, + bodies, + closeAll: async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + upstream.close(); + upstream.closeIdleConnections?.(); + await Promise.allSettled([once(proxy, "close"), once(upstream, "close")]); + }, + }; +} + +async function driveAnthropic(rig: Rig, session: string, body: Record): Promise { + const r = await 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": session }, + body: JSON.stringify(body), + }); + assert.equal(r.status, 200, `request must succeed, got ${r.status}: ${await r.text().catch(() => "")}`); + await r.body?.cancel().catch(() => { /* drained */ }); +} + +test("#5880 e2e: near-wall anthropic request gets max_tokens clamped against the RAW window", async () => { + const logs: string[] = []; + setLogCapture((level, msg) => { logs.push(`${level} ${msg}`); }); + const rig = await startHarness(); + try { + // ~560k ASCII chars ≈ 140k estimated tokens (non-CJK counts len/4) with + // max_tokens 60_000 (>25% of the window → headroom reservation shrinks + // the EFFECTIVE window to 150_000). The cap must be computed against + // the RAW 200_000, where it lands ~35k — below the request, so it fires; + // against the effective 150_000 it would go negative and surrender. + await driveAnthropic(rig, "s5880-clamp", { + model: MODEL, + max_tokens: 60_000, + stream: true, + system: "You are a helpful assistant.", + messages: [{ role: "user", content: filler(560_000) }], + }); + const sent = rig.bodies.at(-1)?.json; + assert.ok(sent, "upstream must have received the request"); + const sentMax: unknown = sent.max_tokens; + assert.ok(typeof sentMax === "number", `max_tokens must remain a number, got ${typeof sentMax}`); + assert.ok(sentMax < 60_000, `near-wall request must be clamped below 60000, got ${sentMax}`); + assert.ok(sentMax >= 1024, `clamp must stay above the surrender floor, got ${sentMax}`); + + const line = logs.find((l) => l.includes(`[s5880-clamp]`) && l.includes("output budget clamped")); + assert.ok(line, `a clamp log line must exist, got: ${JSON.stringify(logs.filter((l) => l.includes("clamped")))}`); + const m = /output budget clamped 60000 -> (\d+) \(input~(\d+), window=(\d+)\)/.exec(line!); + assert.ok(m, `clamp line must carry the cap math, got: ${line}`); + assert.equal(Number(m![3]), WINDOW, "cap must be computed against the RAW native window (#2096)"); + assert.equal(sentMax, expectedCap(Number(m![2]), WINDOW), "cap math must match the logged estimate"); + } finally { + setLogCapture(null); + await rig.closeAll(); + } +}); + +test("#5880 e2e: comfortably-fitting anthropic request keeps its max_tokens", async () => { + const logs: string[] = []; + setLogCapture((level, msg) => { logs.push(`${level} ${msg}`); }); + const rig = await startHarness(); + try { + await driveAnthropic(rig, "s5880-fit", { + model: MODEL, + max_tokens: 60_000, + stream: true, + system: "You are a helpful assistant.", + messages: [{ role: "user", content: "hello" }], + }); + const sent = rig.bodies.at(-1)?.json; + assert.ok(sent, "upstream must have received the request"); + assert.equal(sent?.max_tokens, 60_000, "fitting request must pass through unclamped"); + assert.ok(!logs.some((l) => l.includes("[s5880-fit]") && l.includes("output budget clamped")), "no clamp may fire for a fitting request"); + } finally { + setLogCapture(null); + await rig.closeAll(); + } +});