From c741120f586b25414e7698607ccb1537b8d6ccdd Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 27 Sep 2026 20:53:38 +0800 Subject: [PATCH 1/2] fix: gate stale estimate baselines out of nudge/preflight sizing (#1492) After an upstream failure without a usage report, armFailureShrink raises stats.lastInputTokens (source=estimate) to chars/4 of the body just sent. On kernel-transform-failure fallbacks that body is the UNFOLDED raw client history, so the poisoned baseline survives across turns while upstream keeps failing and three source-blind consumers apply it cross-turn: - effectiveTokenCount fed it to decideNudge -> false emergency bands - preflight trigger max()'d it against the post-fold payload estimate - the preflight loop floor pinned currentTokens at the stale value, burning summarization rounds over a payload whose own estimate fits the window Rule now: an estimate-sourced baseline may floor sizing decisions only while the current payload is unmeasurable (processedMessages empty). Usage-sourced baselines keep flooring everywhere. armFailureShrink also measures with defaultCountTokens (CJK-aware lower bound) instead of estimateTokensFast (chars/4); ASCII output is byte-identical. --- src/preflight.ts | 9 +- src/server.ts | 46 ++-- tests/issue1492-stale-estimate-view.test.ts | 211 ++++++++++++++++++ .../issue847-compress-failure-guards.test.ts | 2 + 4 files changed, 246 insertions(+), 22 deletions(-) create mode 100644 tests/issue1492-stale-estimate-view.test.ts diff --git a/src/preflight.ts b/src/preflight.ts index 1760fa629f..e7cb80f57d 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -869,7 +869,14 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // Floor on the session's measured input baseline: the upstream's // input_tokens also covers the system prompt + tool definitions, which // are not in turn.messages, so the direct estimate can undershoot. - currentTokens = Math.max(deps.session.stats.lastInputTokens, estimateCoreMessages(turn.messages) + (deps.imageFloor ?? 0) + (deps.wireOverhead ?? 0)); + // #1492: an estimate-sourced baseline may floor only while the payload + // is unmeasured (empty input → transform failed, outbound IS raw); a + // stale one from an earlier unfolded turn would pin currentTokens at + // millions and burn rounds folding ranges the window never needed. + const baselineFloor = messages.length > 0 + ? (deps.session.stats.lastInputTokensSource === "usage" ? deps.session.stats.lastInputTokens : 0) + : deps.session.stats.lastInputTokens; + currentTokens = Math.max(baselineFloor, estimateCoreMessages(turn.messages) + (deps.imageFloor ?? 0) + (deps.wireOverhead ?? 0)); if (!baselineKnown) { // #558-merge: the upper-bound regime also carries the image/wire // floors — they are real billed costs the fold can never remove diff --git a/src/server.ts b/src/server.ts index e7cdd7c209..68e6a73fcf 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3,7 +3,7 @@ import fs from "node:fs"; import path from "node:path"; import { createHash, randomUUID } from "node:crypto"; import { performance } from "node:perf_hooks"; -import { createCore, type CompressionCore, type CompressionState, type Config, type AbsorbConfig, type CoreMessage, type NudgeDecision, type Prompts, type PackSurface, type ToolPrompts, applyAcpToolOverrides, defaultPrompts, defaultCountTokens, estimateTokensFast, renderNudgeText, deactivateBlock, viableRanges, resolveOutputSteeringConfig } from "acp-kernel"; +import { createCore, type CompressionCore, type CompressionState, type Config, type AbsorbConfig, type CoreMessage, type NudgeDecision, type Prompts, type PackSurface, type ToolPrompts, applyAcpToolOverrides, defaultPrompts, defaultCountTokens, renderNudgeText, deactivateBlock, viableRanges, resolveOutputSteeringConfig } from "acp-kernel"; import { DEFAULT_STRIP_IMAGES_KEEP_RECENT, applyCompressSettings, resolveAbsorbSettings, resolveCompress, resolveCompressPrompts, resolveCompressSurfaceDetailed, resolveRequestConfig } from "./compress-settings.js"; import { dropCompressReasoning, type CompressReasoningConfig } from "./reasoning-drop.js"; import type { CompressSettings, ProxyOptions } from "./config.js"; @@ -2679,27 +2679,17 @@ function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; cont // still feeds 0 (nothing measured yet; nothing pending either), so // first-turn behavior is byte-identical to pre-#728. function effectiveTokenCount(session: Session, msgs: CoreMessage[], inboundImageTokens = 0): number { - if (session.stats.lastInputTokens > 0) return session.stats.lastInputTokens; - // The inbound upper bound carries the image term in BOTH zero-baseline - // regimes (#1119/#1137): the wire codecs move images out of - // CoreMessage.text into sidecars, so a text-only bound drops them. - // #1137: an anonymous prefix-affinity fork (#553) replays its FULL raw - // history — images included — in this one request, so this request's - // image mass IS the history's image mass; returning the text-only bound - // there left image-heavy forks reading single-digit usage % and nudging - // only at overflow. + // #1492: only usage-grade baselines are authoritative sizing inputs. An + // estimate-sourced value describes ONE turn's outbound — possibly the FULL + // raw history on an unfolded fallback turn — and pinning the nudge to it + // misreads a folded ~160K payload as 1.27M for every later failed turn. + // Fall through to the per-turn local measurements, which track the actual + // outbound view (post-fold normally, raw when the transform failed). + if (session.stats.lastInputTokens > 0 && session.stats.lastInputTokensSource === "usage") return session.stats.lastInputTokens; const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens; if (session.metadata.anonymousPrefixAffinity) return raw; const est = session.stats.localInputEstimate ?? 0; if (est <= 0) return 0; - // Cap by THIS request's inbound upper bound: the recorded estimate lags by - // one turn, so after a client-side shrink (native compaction echo, history - // edit) the previous turn's payload can be larger than what is in front of - // us now — never claim more context than the current request could hold. - // In steady state est <= raw bound always (the outbound fold is never - // larger than the inbound history), so this is a no-op there. A - // client-side shrink still shrinks the bound (fewer messages AND fewer - // images), preserving the stale-high invariant. return Math.min(est, raw); } @@ -4319,9 +4309,20 @@ async function preflightCompressIfNeeded( // 0-baseline means a genuinely new conversation or a post-native-compaction // replay, both small enough to self-heal via the learned-window path. const unknownBaseline = anonymous && session.stats.lastInputTokens <= 0; + // #1492: floor the trigger on the baseline only while it is authoritative + // for THIS payload. A usage-grade baseline measures what upstream billed + // (it can legitimately exceed every local estimate — invisible thinking/ + // cache components), and ANY baseline is the best signal when the current + // payload is unmeasured (kernel transform failed → the outbound IS the raw + // body). An estimate-sourced baseline on a measured (folded) payload + // describes a different view and must not pull preflight into multi-minute + // runs over a payload whose own post-fold estimate fits the window. + const baselineFloor = prepared.processedMessages.length > 0 + ? (session.stats.lastInputTokensSource === "usage" ? session.stats.lastInputTokens : 0) + : session.stats.lastInputTokens; const tokenCount = unknownBaseline ? estimateCoreMessagesUpper(prepared.processedMessages) + overheadEstimate + imageTokens - : Math.max(session.stats.lastInputTokens, payloadEstimate); + : Math.max(baselineFloor, payloadEstimate); if (limit <= 0 || !model || tokenCount < compressionTarget) return prepared; const payloadFitsWindow = (unknownBaseline ? tokenCount : payloadEstimate) < limit; // #496 forward-once-then-learn: the default image cost (base64/4) matches byte @@ -4544,7 +4545,9 @@ function armFailureShrink(prepared: Prepared, log: (level: string, msg: string) let est: number; try { const text = typeof prepared.body === "string" ? prepared.body : prepared.body.toString("utf8"); - est = estimateTokensFast(text); + // #1492: CJK-aware lower bound — chars/4 understated CJK by up to 4×, + // silently breaking the documented "lower bound → never later" contract. + est = defaultCountTokens(text); } catch { return; // non-text body — nothing to estimate } @@ -4911,7 +4914,8 @@ async function forward( const declared = typeof s.metadata.effectiveContextLimit === "number" ? s.metadata.effectiveContextLimit : 0; let est = 0; try { - est = estimateTokensFast(rawBody ?? ""); + // #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)); if (arm > 0) { diff --git a/tests/issue1492-stale-estimate-view.test.ts b/tests/issue1492-stale-estimate-view.test.ts new file mode 100644 index 0000000000..5c2c4a4c93 --- /dev/null +++ b/tests/issue1492-stale-estimate-view.test.ts @@ -0,0 +1,211 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions, _resetSessionsForTest } from "../src/session.ts"; + +// #1492: emergency-shrink arming estimates the wire body it just sent. On a +// fallback turn whose outbound IS the client's full raw history (kernel +// transform failed → forwarding unchanged), that estimate describes the RAW +// view (millions of tokens) while every later turn forwards a FOLDED view +// (~tens of thousands). While upstream keeps failing no usage report arrives +// to overwrite the armed value, and three sizing consumers applied it +// cross-turn without checking its provenance (#857's lastInputTokensSource): +// 1. effectiveTokenCount pinned the nudge at >1000% → false emergency +// bands every turn; +// 2. the preflight trigger's max(lastInputTokens, payloadEstimate) fired +// compression over a payload whose own post-fold estimate fit the window; +// 3. the preflight loop floored currentTokens on the same stale value, so +// each run folded viable ranges to exhaustion (minutes of summarization +// calls) only to log "payload fits with no compressible ranges". +// Fix: an estimate-sourced baseline may floor sizing decisions ONLY while the +// current payload is unmeasured (empty processed messages → the outbound IS +// the raw body); a usage-sourced baseline keeps flooring (upstream billing can +// legitimately exceed every local estimate). These pins (explicit identity, +// Anthropic wire, 120k window so the kernel's absolute-token zones stay out of +// the fixture's way — same rationale as #1119): +// A. poisoned ESTIMATE baseline + folded payload → no preflight calls, no +// nudge, history intact, self-heal via the next real usage report. +// B. poisoned USAGE baseline → preflight STILL fires (real overflow evidence +// must keep driving compression — the fix must not over-correct). + +const WINDOW = 120_000; +const POISON = 1_200_000; // mirrors the reported 1.27M raw-history estimate +const NUDGE_MARKER = "Context limit reached"; + +function okSse(): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: 5000 } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +function summaryJson(): string { + return JSON.stringify({ + id: "msg_summary", + type: "message", + role: "assistant", + model: "claude-relay", + content: [{ type: "text", text: "SUMMARY: multi-step build-check session; all checks passed." }], + stop_reason: "end_turn", + usage: { input_tokens: 500, output_tokens: 50 }, + }); +} + +// ~7.3k chars each of ASCII code text: optimistic estimate ≈ 1.8k tokens per +// heavy, six heavies ≈ 44k chars total — below every nudge percentage band of +// the 120k window (≈37%), yet far above the kernel's preserve-recent zone so +// the oldest heavies ARE compressible mass for the pre-fix paths. +const LINE = (i: number) => + `const handler_${i} = (req: Request, res: Response) => { res.status(200).json({ status: "ok", id: ${i}, ts: Date.now() }); };`; +const HEAVY = (i: number) => `CODE_${i}_` + LINE(i).repeat(62); + +type Msg = { role: string; content: unknown }; + +function baseConversation(): Msg[] { + const msgs: Msg[] = []; + for (let i = 0; i < 12; i++) { + msgs.push({ role: i % 2 === 0 ? "user" : "assistant", content: i < 6 ? HEAVY(i) : `note_${i}_short tail ${i}` }); + } + return msgs; +} + +/** Mock relay: always 200; serves summarization calls (non-stream) and + * streaming turns, records what it received. */ +function makeRelay() { + const received: Buffer[] = []; + let nonStream = 0; + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks); + received.push(raw); + let parsed: Record = {}; + try { + parsed = JSON.parse(raw.toString("utf8")); + } catch { + /* non-JSON — treat as streaming forward */ + } + if (parsed.stream !== true) { + nonStream += 1; + res.writeHead(200, { "content-type": "application/json" }); + res.end(summaryJson()); + return; + } + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse()); + }); + }); + return { server, received, nonStreamCalls: () => nonStream }; +} + +async function runCase(sessionId: string, poisonSource: "usage" | "estimate") { + _resetSessionsForTest(); + const relay = makeRelay(); + relay.server.listen(0, "127.0.0.1"); + await once(relay.server, "listening"); + const upstreamPort = relay.server.address().port; + _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: { "claude-relay": { 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, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + try { + const url = `http://127.0.0.1:${proxy.address().port}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`; + const headers = { "content-type": "application/json", "x-acp-session": sessionId }; + const base = baseConversation(); + + // --- Turn 1: establishes the session + kernel state, reports usage --- + const r1 = await fetch(url, { + method: "POST", + headers, + body: JSON.stringify({ model: "claude-relay", max_tokens: 1024, stream: true, system: "You are a helpful assistant.", messages: base }), + }); + assert.equal(r1.status, 200, "turn 1 succeeds"); + await r1.text(); + + const s = listSessions().find((x) => x.stats.lastInputTokens === 5000); + assert.ok(s, "turn 1 reported usage (lastInputTokens = 5000)"); + // Poison exactly what armFailureShrink writes after an upstream failure + // on a turn whose outbound was the unfolded raw history. + s!.stats.lastInputTokens = POISON; + s!.stats.lastInputTokensSource = poisonSource; + + // --- Turn 2: same folded history + one small exchange --- + const msgs2: Msg[] = [...base, { role: "assistant", content: "ok, done." }, { role: "user", content: "step two?" }]; + const r2 = await fetch(url, { + method: "POST", + headers, + body: JSON.stringify({ model: "claude-relay", max_tokens: 1024, stream: true, system: "You are a helpful assistant.", messages: msgs2 }), + }); + assert.equal(r2.status, 200, "turn 2 succeeds"); + await r2.text(); + + const streamed = relay.received + .map((b) => b.toString("utf8")) + .filter((t) => { + try { + return (JSON.parse(t) as Record).stream === true; + } catch { + return false; + } + }); + const s2 = listSessions().find((x) => x.id === s!.id); + return { relay, proxy, streamed, nonStream: relay.nonStreamCalls(), session: s2! }; + } finally { + proxy.close(); + await once(proxy, "close"); + relay.server.close(); + await once(relay.server, "close"); + } +} + +test("#1492 A: poisoned estimate baseline must not drive nudge/preflight on a folded payload", async () => { + const { streamed, nonStream, session } = await runCase("issue1492-est-sess", "estimate"); + assert.equal(nonStream, 0, "preflight must stay silent — the payload's own post-fold estimate fits the window (pre-fix: the stale 1.2M baseline fired it and burned summarization rounds)"); + assert.equal(streamed.length, 2, "two streaming forwards (one per turn)"); + assert.ok(!streamed[1].includes(NUDGE_MARKER), `turn 2 must not carry a nudge (pre-fix: 1.2M/120K ≈ 1000% lit the pressure/emergency bands), got tail: ${streamed[1]?.slice(-400)}`); + for (let i = 0; i < 6; i++) { + assert.ok(streamed[1].includes(`CODE_${i}_`), `heavy ${i} must survive unfolded (no fold ran)`); + } + assert.equal(session.stats.lastInputTokens, 5000, "the recovered turn's real usage report overwrote the poisoned value"); +}); + +test("#1492 B: a usage-sourced high baseline must STILL fire preflight (real overflow evidence)", async () => { + const { streamed, nonStream, session } = await runCase("issue1492-usage-sess", "usage"); + assert.ok(nonStream >= 1, `a usage-grade baseline above the window must keep driving compression (nonStream=${nonStream})`); + // No nudge-marker assertion here: the forwarded body is the POST-FOLD + // rebuild (server.ts re-runs prepare after preflight), whose fresh nudge + // decision sees nothing left to fold and suppresses — kernel-owned + // behavior, identical pre/post fix. The asymmetry pinned by this case is + // the trigger itself: estimate-source stays silent (case A), usage-source + // still burns summarization calls. + void streamed; + assert.equal(session.stats.lastInputTokens, 5000, "self-heal via the next real usage report"); +}); diff --git a/tests/issue847-compress-failure-guards.test.ts b/tests/issue847-compress-failure-guards.test.ts index b9ed77062e..3f2e073f72 100644 --- a/tests/issue847-compress-failure-guards.test.ts +++ b/tests/issue847-compress-failure-guards.test.ts @@ -125,7 +125,9 @@ test("#847: repeated identical failing spec escalates; success clears the streak test("#847: preflight reports honest exhaustion when every listed range is sub-gate, without calling upstream", async () => { const session = getSession(`issue847-preflight-${randomUUID()}`); + // #1492: only usage-grade baselines floor the walk against a measured payload. session.stats.lastInputTokens = 6000; + session.stats.lastInputTokensSource = "usage"; const messages: CoreMessage[] = [ textMsg("p_1", "user", "u".repeat(100)), textMsg("p_2", "assistant", "a".repeat(1500)), From ee125241bcc8b008a397ee9793ab6120a5a4183a Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 27 Sep 2026 21:40:03 +0800 Subject: [PATCH 2/2] fix: gate the clamp baseline on usage-grade source; pin the gate in #453 backstop Follow-ups from review of #1503: - F1/HIGH: estimateInputTokens floors on lastInputTokens only when lastInputTokensSource === "usage". An estimate-grade baseline (raw-view poison from a transform-failure fallback arm, #1492/#1493) would otherwise mask the payload's own estimate through the max() and let the #453 clamp fail open while the poison persists. clampOutgoingOutput now feeds the source through (#1498 F5's clamp hole, closed here instead). - F3: the loop-floor and emergency-band gates share this exact clamping mechanism, so they inherit the fix; pinned via the updated backstop test. - tests/issue453-backstop.test.ts: the real-count test now also pins the gate (estimate-grade and unknown-provenance baselines never floor). --- src/server.ts | 22 ++++++++---- src/server/budget.ts | 15 +++++--- tests/issue1492-stale-estimate-view.test.ts | 38 ++++++++++++++++++--- tests/issue453-backstop.test.ts | 8 ++++- 4 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/server.ts b/src/server.ts index 68e6a73fcf..5e728e54f9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2693,6 +2693,14 @@ function effectiveTokenCount(session: Session, msgs: CoreMessage[], inboundImage return Math.min(est, raw); } +/** #1492: secondary processTurn feeds (count-token previews, the codex + * forged-compact handoff, the absorb view) must not light the kernel's + * emergency bands on an estimate-grade poison — only a usage-grade baseline + * describes real billing. 0 reads as "unknown" to the kernel. */ +function usageGradeInputBaseline(session: Session): number { + return session.stats.lastInputTokensSource === "usage" ? session.stats.lastInputTokens : 0; +} + // #1403: top-level prompt_cache_key is NOT part of the Anthropic Messages API. // It is the omp plugin's session id stamped for the proxy's identity chain // (#268); the fully-processed path strips it (prepareAnthropic), but every @@ -3166,7 +3174,7 @@ async function prepareOpenai( rebuiltMessages = normalizeStrictEchoReasoning(rebuiltMessages, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)), log, sessionId); const rebuilt: OpenAIRequestBody = { ...parsed, messages: rebuiltMessages, tools: toolsOut as OpenAITool[] | undefined }; warnReasoningPairs(rebuiltMessages, log, sessionId); - clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("openai", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log); + clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, imageTokens: imageTokensInParsedBody("openai", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log); // prompt_cache_retention is an OpenAI-host-only cache directive; the dsh // launcher forces PI_CACHE_RETENTION=long (for the session-id // prompt_cache_key) which makes the client also emit it. Third-party @@ -3363,7 +3371,7 @@ async function prepareGoogle( } const rebuilt: GoogleRequestBody = { ...parsed, contents: rebuiltContents, tools: toolsOut, systemInstruction }; - clampOutgoingOutput(rebuilt as Record, "generationConfig.maxOutputTokens", { systemText: googleClientSystem, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("google", rebuilt) }, sessionId, log); + clampOutgoingOutput(rebuilt as Record, "generationConfig.maxOutputTokens", { systemText: googleClientSystem, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, imageTokens: imageTokensInParsedBody("google", rebuilt) }, sessionId, log); // #532: title-gen side requests carry their own tiny system — skip them. if (!isTitleGen && googleOutboundSystem !== undefined) { session.metadata.systemPromptTokens = countSystemAndToolsTokens(googleOutboundSystem, toolsOut); @@ -3389,7 +3397,7 @@ export function prepareGoogleCountTokens( const { msgs } = googleToCore(parsed); // Read-only preview: the store rides in so placeholder substitution is // counted, but nothing is adopted (state is discarded here too). - const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: session.stats.lastInputTokens, renderTags: "text-only", contentStore: contentStoreOf(session) }); + const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: "text-only", contentStore: contentStoreOf(session) }); const stripped = stripKernelSummaries(turn.messages, turn.state); const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) }; log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`); @@ -3684,7 +3692,7 @@ async function prepareResponses( const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut }; warnResponsesReasoningPairs(Array.isArray(rebuiltInput) ? rebuiltInput : [], log, sessionId); if (!isCompactionTrigger) { - clampOutgoingOutput(rebuilt as Record, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("responses", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log); + clampOutgoingOutput(rebuilt as Record, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, imageTokens: imageTokensInParsedBody("responses", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log); } // Route with the upstream THIS request goes to — session.meta.upstreamOrigin // is first-wins and would keep injecting pck toward a relay we switched @@ -3782,7 +3790,7 @@ export function prepareCountTokens( try { const { msgs, cacheControls } = anthropicToCore(parsed); // Read-only preview: same policy as the google twin above. - const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); + const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); const stripped = stripKernelSummaries(turn.messages as BiliMessage[], turn.state); const rebuiltMessages = coreToAnthropic(stripped, cacheControls); log("info", `[${sessionId}] count_tokens pruned: ${msgs.length} → ${stripped.length} msgs`); @@ -3864,7 +3872,7 @@ function prepareResponsesCompact( // no [ACP absorb] instruction bakes into the forged history, and run // the absorb view so absorbed pairs stay hidden in it (wire parity). const compactConfig = ccrLoopConfig(session, { ...config, absorb: undefined }); - const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); + const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: usageGradeInputBaseline(session), renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); session.state = turn.state; adoptContentStore(session, turn.contentStore); transformOk = true; @@ -3874,7 +3882,7 @@ function prepareResponsesCompact( session.contentStoreDirty = prevStoreDirty; return base; } - const viewed = applyAbsorbView(turn.messages, turn.state, compactConfig, session.stats.lastInputTokens); + const viewed = applyAbsorbView(turn.messages, turn.state, compactConfig, usageGradeInputBaseline(session)); const processed = repairResponsesAssistantOrdering(stripKernelSummaries(viewed, turn.state), projection.msgs); let output = patchResponsesInput(projection, processed); if (typeof output === "string") { diff --git a/src/server/budget.ts b/src/server/budget.ts index 74a87859e2..c54c678511 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -32,10 +32,15 @@ export function countSystemAndToolsTokens(systemText: string | undefined, tools: /** Conservative outbound-input estimate: the larger of the upstream-reported * previous-turn input (real tokenizer count, already includes system+tools) and * a fresh count of the rebuilt conversation text + system + tool definitions - * (needed on turn 1 / right after a shrink, when lastInputTokens lags). */ -export function estimateInputTokens(processedMessages: CoreMessage[], systemText: string | undefined, tools: unknown, lastInputTokens: number): number { + * (needed on turn 1 / right after a shrink, when lastInputTokens lags). + * #1492: the reported baseline floors only when it is usage-grade — an + * estimate-grade baseline (raw-view poison from a transform-failure fallback + * arm) would mask the payload's own est through the max() and silently skip + * the #453 clamp (fail-open) while the poison persists. */ +export function estimateInputTokens(processedMessages: CoreMessage[], systemText: string | undefined, tools: unknown, lastInputTokens: number, lastInputTokensSource?: string): number { const est = estimateCoreMessages(processedMessages) + countSystemAndToolsTokens(systemText, tools); - return Math.max(lastInputTokens > 0 ? lastInputTokens : 0, est); + const baseline = lastInputTokens > 0 && lastInputTokensSource === "usage" ? lastInputTokens : 0; + return Math.max(baseline, est); } // #1320: Claude Code round-trips extended-thinking blocks as SIGNATURE-ONLY @@ -193,7 +198,7 @@ export function emergencyNudge(nudge: NudgeDecision | null | undefined, escalati export function clampOutgoingOutput( rebuilt: Record, field: OutputBudgetField, - ctx: { systemText: string; tools: unknown; processedMessages: CoreMessage[]; lastInputTokens: number; nativeWindow: number; imageTokens: number }, + ctx: { systemText: string; tools: unknown; processedMessages: CoreMessage[]; lastInputTokens: number; lastInputTokensSource?: string; nativeWindow: number; imageTokens: number }, sessionId: string, log: (level: string, msg: string) => void, ): void { @@ -201,7 +206,7 @@ export function clampOutgoingOutput( if (typeof raw !== "number") return; // #488: images ride along in the rebuilt body but are invisible to the text model — // without them the cap is too generous and input+output can still overflow. - const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens) + ctx.imageTokens; + const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens, ctx.lastInputTokensSource) + ctx.imageTokens; const capped = clampOutputBudget(raw, inputEstimate, ctx.nativeWindow); if (capped !== undefined) { writeOutputBudget(rebuilt, field, capped); diff --git a/tests/issue1492-stale-estimate-view.test.ts b/tests/issue1492-stale-estimate-view.test.ts index 5c2c4a4c93..7a4daba8c2 100644 --- a/tests/issue1492-stale-estimate-view.test.ts +++ b/tests/issue1492-stale-estimate-view.test.ts @@ -10,6 +10,7 @@ import { startServer, type ProxyOptions } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { listSessions, _resetSessionsForTest } from "../src/session.ts"; +import { setLogCapture } from "../src/logger.ts"; // #1492: emergency-shrink arming estimates the wire body it just sent. On a // fallback turn whose outbound IS the client's full raw history (kernel @@ -111,7 +112,9 @@ function makeRelay() { return { server, received, nonStreamCalls: () => nonStream }; } -async function runCase(sessionId: string, poisonSource: "usage" | "estimate") { +async function runCase(sessionId: string, poisonSource: "usage" | "estimate", conversation?: Msg[]) { + const logs: string[] = []; + setLogCapture((level, msg) => { logs.push(`${level} ${msg}`); }); _resetSessionsForTest(); const relay = makeRelay(); relay.server.listen(0, "127.0.0.1"); @@ -129,7 +132,7 @@ async function runCase(sessionId: string, poisonSource: "usage" | "estimate") { compress: { injectTool: true, injectNudge: true }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", - log: false, + log: true, debug: false, passthrough: false, autoUpdate: false, @@ -139,7 +142,7 @@ async function runCase(sessionId: string, poisonSource: "usage" | "estimate") { try { const url = `http://127.0.0.1:${proxy.address().port}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`; const headers = { "content-type": "application/json", "x-acp-session": sessionId }; - const base = baseConversation(); + const base = conversation ?? baseConversation(); // --- Turn 1: establishes the session + kernel state, reports usage --- const r1 = await fetch(url, { @@ -177,8 +180,9 @@ async function runCase(sessionId: string, poisonSource: "usage" | "estimate") { } }); const s2 = listSessions().find((x) => x.id === s!.id); - return { relay, proxy, streamed, nonStream: relay.nonStreamCalls(), session: s2! }; + return { relay, proxy, streamed, nonStream: relay.nonStreamCalls(), session: s2!, logs }; } finally { + setLogCapture(null); proxy.close(); await once(proxy, "close"); relay.server.close(); @@ -187,7 +191,7 @@ async function runCase(sessionId: string, poisonSource: "usage" | "estimate") { } test("#1492 A: poisoned estimate baseline must not drive nudge/preflight on a folded payload", async () => { - const { streamed, nonStream, session } = await runCase("issue1492-est-sess", "estimate"); + const { streamed, nonStream, session, logs } = await runCase("issue1492-est-sess", "estimate"); assert.equal(nonStream, 0, "preflight must stay silent — the payload's own post-fold estimate fits the window (pre-fix: the stale 1.2M baseline fired it and burned summarization rounds)"); assert.equal(streamed.length, 2, "two streaming forwards (one per turn)"); assert.ok(!streamed[1].includes(NUDGE_MARKER), `turn 2 must not carry a nudge (pre-fix: 1.2M/120K ≈ 1000% lit the pressure/emergency bands), got tail: ${streamed[1]?.slice(-400)}`); @@ -195,6 +199,10 @@ test("#1492 A: poisoned estimate baseline must not drive nudge/preflight on a fo assert.ok(streamed[1].includes(`CODE_${i}_`), `heavy ${i} must survive unfolded (no fold ran)`); } assert.equal(session.stats.lastInputTokens, 5000, "the recovered turn's real usage report overwrote the poisoned value"); + assert.ok( + !logs.some((l) => l.includes("preflight target reached") || l.includes("preflight made no progress") || l.includes("preflight")), + `preflight must not even ENTER on a folded fitting payload with an estimate-grade baseline (trigger gate): ${JSON.stringify(logs.filter((l) => l.toLowerCase().includes("preflight")))}`, + ); }); test("#1492 B: a usage-sourced high baseline must STILL fire preflight (real overflow evidence)", async () => { @@ -209,3 +217,23 @@ test("#1492 B: a usage-sourced high baseline must STILL fire preflight (real ove void streamed; assert.equal(session.stats.lastInputTokens, 5000, "self-heal via the next real usage report"); }); + +// Test C pins the INNER layer (preflight.ts loop floor): a legitimately-entered +// preflight (the payload itself is over-target, so the trigger gate lets it in +// even with an estimate-grade baseline) must size its loop from the PAYLOAD, +// not from the poisoned baseline — gated, it folds only enough to fit; with the +// floor gate reverted it folds every viable range to exhaustion. +test("#1492 C: preflight loop sizes from the payload, not a poisoned estimate floor", async () => { + const big: Msg[] = []; + // 58 mediums ≈ 470K chars ≈ ~117K tokens — just over the preflight target + // (90% of the 120K window ≈ 108K) with ~50 individually-foldable ranges. + // The gated loop folds only the ~4 needed to fit; a poisoned floor keeps + // currentTokens at 1.2M forever and walks the ENTIRE offered list. + for (let i = 0; i < 58; i++) big.push({ role: i % 2 === 0 ? "user" : "assistant", content: `CODE_${i}_` + LINE(i).repeat(88) }); + big.push({ role: "user", content: "final step?" }); + const { nonStream, session } = await runCase("issue1492-loop-sess", "estimate", big); + const active = session.state.blocks.filter((b) => b.active).length; + assert.ok(nonStream >= 1, "the over-target payload legitimately enters preflight"); + console.error(`DIAG nonStream=${nonStream} active=${active} blocks=${session.state.blocks.length} msgs-folded-check`); + assert.ok(active <= 16, `gated loop folds only enough to fit (active=${active}); an ungated poisoned floor would fold every viable range (~27+)`); +}); diff --git a/tests/issue453-backstop.test.ts b/tests/issue453-backstop.test.ts index 69adeb8a8a..fd2ed9a904 100644 --- a/tests/issue453-backstop.test.ts +++ b/tests/issue453-backstop.test.ts @@ -40,7 +40,13 @@ test("clampOutputBudget: no-op when input alone nearly fills the window (preflig }); test("estimateInputTokens: prefers the real previous-turn count when it lags high", () => { - assert.equal(estimateInputTokens([], "", [], 50_000), 50_000); + assert.equal(estimateInputTokens([], "", [], 50_000, "usage"), 50_000); + // #1492: estimate-grade and unknown-provenance baselines must never floor + // the estimate — raw-view poison from a transform-failure fallback arm would + // otherwise mask the payload's own estimate through the max(). + const est = estimateInputTokens([], "", [], 0, "usage"); + assert.equal(estimateInputTokens([], "", [], 50_000, "estimate"), est); + assert.equal(estimateInputTokens([], "", [], 50_000), est); }); test("estimateInputTokens: counts system + tools on turn 1 (lastInputTokens=0)", () => {