From 5bdc878a6cc22ad10ed00ead4b3c9a2e771fd80a Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 16 Oct 2026 10:30:28 +0800 Subject: [PATCH 1/7] fix: acp-cache renders per-line WIRE/UPSTREAM/RESTART sections (#3307) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit wire/upstream/restart already carried per-event data in the cache report (switchEvents / restartEvents) but handleAcpCache only ever rendered their aggregate counts in CACHE INVALIDATION — no per-line timestamps, so seam-vs-restart cross-checks (e.g. #3416 273->28 vs the 10 restart/refork events) were impossible to run off the report. Surface them exactly like the existing MODEL/KEY/PROMPT/PROJECTION sections. Refs #3307. --- src/cache-ledger.ts | 10 ++++++++-- tests/cache-ledger.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 20dbe753fb..337d1863d1 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1913,6 +1913,12 @@ export function handleAcpCache(session: Session, args?: Record) // #2867: proxy-side mode/projection switches get their own section only // when observed — a session that never switches modes stays byte-identical. const projText = report.projSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.projSwitches, detail, "PROJECTION SWITCHES (proxy mode/projection change — bili's own doing, e.g. self-heal degrade-to-proxy)") : ""; + // #3307: wire/upstream/restart carry per-event data in the report (switchEvents / + // restartEvents) but only their aggregate counts reached CACHE INVALIDATION — no + // per-line timestamps, so cross-checks (e.g. seam-vs-restart) were impossible. + const wireText = report.wireSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.wireSwitches, detail, "WIRE SWITCHES (client switched protocol wire between requests)") : ""; + const upstreamText = report.upstreamSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.upstreamSwitches, detail, "UPSTREAM SWITCHES (relay/upstream origin changed between requests)") : ""; + const restartText = report.restartDrops.count > 0 ? "\n\n" + formatModelSwitches(report.restartDrops, detail, "RESTART/REFORK (first bill under a fresh daemon boot — prefix re-billed once)") : ""; if (detail === "full" && report.lines.length > FULL_DETAIL_LINES) { const dropped = report.lines.length - FULL_DETAIL_LINES; const capped = formatCacheReport( @@ -1920,9 +1926,9 @@ export function handleAcpCache(session: Session, args?: Record) session.id, { detail }, ); - return toolOk(tail(capped + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + keyText + promptText + projText + "\n\n" + formatInvalidation(report))); + return toolOk(tail(capped + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + keyText + promptText + projText + wireText + upstreamText + restartText + "\n\n" + formatInvalidation(report))); } - return toolOk(tail(formatCacheReport(report, session.id, { detail }) + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + keyText + promptText + projText + "\n\n" + formatInvalidation(report))); + return toolOk(tail(formatCacheReport(report, session.id, { detail }) + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + keyText + promptText + projText + wireText + upstreamText + restartText + "\n\n" + formatInvalidation(report))); } catch (err) { loggerLog("warn", `[${session.id}] [acp_cache] report failed: ${String(err)}`); return toolFail(`[acp_cache FAILED: ${String(err)}]`); diff --git a/tests/cache-ledger.test.ts b/tests/cache-ledger.test.ts index a85ca9722d..feb47e922c 100644 --- a/tests/cache-ledger.test.ts +++ b/tests/cache-ledger.test.ts @@ -683,6 +683,36 @@ test("handleAcpCache renders the CACHE INVALIDATION breakdown incl. unmeasured ( assert.match(text, /unmeasured .*excluded from hit rate/); }); +test("handleAcpCache renders per-line WIRE/UPSTREAM SWITCHES with timestamps like MODEL/KEY (#3307)", () => { + // wire-only flip (mirrors the #1536 wire fixture) — wire wins its own partition + const wireSession = makeSession(); + wireSession.metadata.lastModel = "gpt-5"; + recordCacheSample(wireSession, { at: T0 + 1000, input: 10000, cached: 9000, protocol: "anthropic" }); + recordCacheSample(wireSession, { at: T0 + 2000, input: 11000, cached: 0, protocol: "openai" }); + const wireText = handleAcpCache(wireSession).text; + assert.match(wireText, /WIRE SWITCHES/); + assert.match(wireText, /anthropic → openai/); + // upstream-only flip (mirrors the #1536 upstream fixture) — upstream wins its own partition + const upSession = makeSession(); + upSession.metadata.lastModel = "gpt-5"; + recordCacheSample(upSession, { at: T0 + 1000, input: 10000, cached: 9000, protocol: "openai", upstream: "https://api.openai.com" }); + recordCacheSample(upSession, { at: T0 + 2000, input: 11000, cached: 0, protocol: "openai", upstream: "https://relay.example.com" }); + const upText = handleAcpCache(upSession).text; + assert.match(upText, /UPSTREAM SWITCHES/); + assert.match(upText, /https:\/\/api\.openai\.com → https:\/\/relay\.example\.com/); +}); + +test("handleAcpCache renders a per-line RESTART/REFORK section with its event (#3307)", () => { + const session = makeSession(); + session.metadata.lastModel = "gpt-5"; + recordCacheSample(session, { at: T0 + 1000, input: 10000, cached: 9000 }); + (session.metadata["cacheLedger"] as Record).lastBoot = "previous-boot"; + recordCacheSample(session, { at: T0 + 2000, input: 11000, cached: 0 }); + const text = handleAcpCache(session).text; + assert.match(text, /RESTART\/REFORK/); + assert.match(text, /→ \(restart\)/); +}); + test("per-cause buckets partition a multi-flag sample's residual (#1847)", () => { const session = makeSession(); session.metadata.lastModel = "gpt-5"; From 4db43b05bf7e2fc6e3194f538b63bb086bd63a51 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 16 Oct 2026 13:00:05 +0800 Subject: [PATCH 2/7] fix: acp-cache seam report keeps suspect/client sections disjoint (#3307) detectSeam pushes both the #3023 client-side rewrite and the legacy suspected-rebuild fall-through into one shared capped ring (led.seamEvents); formatSeam's CACHE SEAM section iterated every event without filtering, so a host-owned client rewrite was also listed under the 'suspected mid-history prefix breaks' header. List only non-client events there so the two sections stay disjoint. --- src/cache-ledger.ts | 12 ++- .../issue3307-seam-suspect-vs-client.test.ts | 74 +++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 tests/issue3307-seam-suspect-vs-client.test.ts diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 337d1863d1..79598e16d3 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1974,11 +1974,17 @@ function formatSeam(r: BiliCacheReport): string { if (r.seam.suspects > 0) { out.push("⚠ CACHE SEAM (suspected mid-history prefix breaks)"); out.push(` ${r.seam.suspects} sample(s) · ${fmtTok(r.seam.missed)} tok re-billed with no fold/switch/restart attribution`); - for (const e of r.seam.events) { + // #3307: seamEvents holds BOTH suspect and #3023 client-side events + // (one shared capped ring), so listing every event here echoed host-owned + // rewrites under the "suspected bili rebuild" header — the ✎ CLIENT + // section below already owns those. List only suspects here so the two + // sections stay disjoint and a client insert is never misread as a seam. + const suspectEvents = r.seam.events.filter((e) => !e.client); + for (const e of suspectEvents) { out.push(` #${e.seq} ${fmtTime(e.at)} hit ${e.hitPct.toFixed(1)}% · input ${fmtTok(e.input)} · divergence ≥${fmtTok(e.lcpBytes)}B at message[${e.msgIndex ?? "?"}] of ${e.prevMsgs}→${e.curMsgs}`); } - if (r.seam.events.length === 0) { - out.push(" (no body-pair forensics on this lane — aggregate flag only; report the session + log if this persists)"); + if (suspectEvents.length === 0) { + out.push(" (no body-pair forensics retained for the suspect samples on this lane — aggregate flag only; client-side rewrites are listed separately below)"); } out.push(" if reproducible: /acp-cache detail:\"full\" + bili.log around the timestamps above (likely a #1548-family round-2/steady render seam)"); } diff --git a/tests/issue3307-seam-suspect-vs-client.test.ts b/tests/issue3307-seam-suspect-vs-client.test.ts new file mode 100644 index 0000000000..cce83f8531 --- /dev/null +++ b/tests/issue3307-seam-suspect-vs-client.test.ts @@ -0,0 +1,74 @@ +// #3307: the text report's two seam sections must stay disjoint. detectSeam +// pushes BOTH the #3023 client-side rewrite AND the legacy "suspected rebuild" +// fall-through into ONE shared capped ring (led.seamEvents); formatSeam's +// ⚠ CACHE SEAM section used to iterate every event in that ring without +// filtering, so a host-owned client rewrite was ALSO listed under the +// "suspected mid-history prefix breaks" header (double-listed), contaminating +// the suspect triage. The ✎ CLIENT section already filters to e.client. This +// pins that a session carrying both arms renders each seq under exactly one +// header — the client seq only under ✎, the suspect seq only under ⚠. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { noteForwardedBody, noteInboundMessages, settleUsageReport, getCacheLedger, handleAcpCache } from "../src/cache-ledger.ts"; +import type { CoreMessage } from "acp-kernel"; +import type { Session } from "../src/session.ts"; + +let seq = 0; +function makeSession(): Session { + seq += 1; + return { + id: `seam-disjoint-${seq}`, + metadata: {}, + stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +const body = (msgs: string[]): string => JSON.stringify({ model: "m", messages: msgs.map((c) => ({ role: "user", content: c })) }); +const inbound = (msgs: string[]): CoreMessage[] => msgs.map((c) => ({ role: "user", content: c })) as unknown as CoreMessage[]; +const T0 = Date.parse("2026-10-16T08:00:00Z"); + +function settle(session: Session, at: number, input: number, cached: number): void { + settleUsageReport(session, { total: input, reportedCached: cached, output: 0, protocol: "openai", upstream: "http://u" }); +} + +test("#3307: client rewrite and genuine seam render under disjoint sections", () => { + const s = makeSession(); + + noteInboundMessages(s, inbound(["m0", "m1", "m2"])); + noteForwardedBody(s, body(["m0", "m1", "m2"])); + settle(s, T0, 100_000, 99_000); + + // Arm A — CLIENT mid-list insert at index 1 (inbound breaks mid-list). + noteInboundMessages(s, inbound(["m0", "INS", "m1", "m2"])); + noteForwardedBody(s, body(["m0", "INS", "m1", "m2"])); + settle(s, T0 + 1000, 100_000, 20_000); + const clientSeq = getCacheLedger(s).lines.at(-1)!.seq; + + // Arm B — GENUINE seam: inbound prefix-stable (pure tail append) but the + // outbound rebuild rewrote a MIDDLE message → bili-owned divergence. + noteInboundMessages(s, inbound(["m0", "INS", "m1", "m2", "m3"])); + noteForwardedBody(s, body(["m0", "INS", "CHG", "m2", "m3"])); + settle(s, T0 + 2000, 100_000, 20_000); + const suspectSeq = getCacheLedger(s).lines.at(-1)!.seq; + + const led = getCacheLedger(s); + assert.equal(led.agg.clientInserts, 1, "client arm booked"); + assert.equal(led.agg.seamSuspects, 1, "suspect arm booked"); + assert.notEqual(clientSeq, suspectSeq, "two distinct samples"); + assert.ok(led.seamEvents?.some((e) => e.client && e.seq === clientSeq), "client event carries .client"); + assert.ok(led.seamEvents?.some((e) => !e.client && e.seq === suspectSeq), "suspect event has no .client"); + + const text = handleAcpCache(s).text; + const warnStart = text.indexOf("⚠ CACHE SEAM"); + const cliStart = text.indexOf("✎ CLIENT MID-HISTORY REWRITE"); + assert.ok(warnStart >= 0, "⚠ suspected section present"); + assert.ok(cliStart > warnStart, "✎ client section present after ⚠"); + const warnSection = text.slice(warnStart, cliStart); + const clientSection = text.slice(cliStart); + + assert.ok(warnSection.includes(`#${suspectSeq}`), "suspect seq listed under ⚠"); + assert.ok(!warnSection.includes(`#${clientSeq}`), "client seq NOT echoed under ⚠ (the #3307 bug)"); + assert.ok(clientSection.includes(`#${clientSeq}`), "client seq listed under ✎"); + assert.ok(!clientSection.includes(`#${suspectSeq}`), "suspect seq NOT listed under ✎"); +}); From cfb2d31a0d598fe1f7e6e691b710a0c5ad73e838 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 02:22:43 +0800 Subject: [PATCH 3/7] fix: log per-fold firstFoldStartTokens (X) + V/Vp for #3307 attribution forensics #3307's unattributed re-pay survives even when every seam fold has a real start_ref, so the V' fallback is not the trigger: decomposeSample drops the fold when bestX = prefixTokensBeforeRef(start) >= billed input, zeroing structuralExcess. That value is in-memory (tokenSnapshot) and never persisted, so it cannot be reconstructed from the blocks table after the fact. Emit one [acp-compress-obs] fold-attribution line per fold carrying the actual X per block plus the same-space view tokens V/Vp. Cross-referenced against the ledger's billed input it separates the two candidate inflation sources: unit scale (kernel chars/4 estimate vs provider-billed tokens) vs view identity (snapshot tracked on a larger view than the shrunken outbound). Pure logging; no behavior change. --- src/cache-ledger.ts | 2 +- src/stream.ts | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 79598e16d3..a48e222dbd 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -320,7 +320,7 @@ function round1(n: number): number { * sum of first-render token counts of all refs chronologically before it. * Refs are assigned in message order and never reused (kernel contract), * so numeric order IS view order. */ -function prefixTokensBeforeRef(session: Session, ref: string): number { +export function prefixTokensBeforeRef(session: Session, ref: string): number { const byRef = session.state?.messageRefs?.byRef; const snap = session.state?.tokenSnapshot; if (!byRef || !snap || refNum(ref) === 0) return 0; diff --git a/src/stream.ts b/src/stream.ts index 8325a875b0..d46502c013 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,7 +1,7 @@ import { createHash, randomBytes } from "node:crypto"; import { activeBlockSpans, collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, hideConsumedCompressCalls, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics } from "acp-kernel"; import { excludeDeadRefRanges, handleAcpStatus, liveCompressionRanges } from "./acp-status.js"; -import { handleAcpCache, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; +import { handleAcpCache, prefixTokensBeforeRef, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; import { type Session, cacheBlockContent, markDirty, scrubTurnSeparatorIds, statusInputBaseline } from "./session.js"; import { COMPRESS_TOOL_NAME, parseCompressInput, type ParsedRange } from "./compress-tool.js"; import { isProxyToolFor } from "./proxy-tool.js"; @@ -991,11 +991,17 @@ export function applyRanges(parsed: ReturnType, ctx: } // #800: feed the cache ledger — the next request's usage report will // attribute its re-pay cliff to these folds via decomposeSample. - recordCacheFoldsFromBlocks( - ctx.session, - res.state.blocks.filter((b) => !beforeIds.has(b.blockId)), - { V: viewTokens, Vp: postCtx }, - ); + const newFoldBlocks = res.state.blocks.filter((b) => !beforeIds.has(b.blockId)); + // #3307: the ledger credits a fold only when firstFoldStartTokens=X < billed + // input, but X is a kernel chars/4 estimate vs provider-billed input — in long + // sessions X can land >= input and the re-pay drops unattributed. Log X plus the + // same-space view tokens (V/Vp) so one capture separates unit-scale from view + // identity as the inflation source. + if (newFoldBlocks.length > 0) { + const xs = newFoldBlocks.map((b) => (b.startRef ? `${b.startRef}=${prefixTokensBeforeRef(ctx.session, b.startRef)}` : `${b.blockId}=none`)).join(" "); + ctx.log(`[acp-compress-obs] fold-attribution X(firstFoldStartTokens)=${xs} · V=${viewTokens} Vp=${postCtx} (#3307)`); + } + recordCacheFoldsFromBlocks(ctx.session, newFoldBlocks, { V: viewTokens, Vp: postCtx }); const warn = r.warnings.length > 0 ? ` ${r.warnings.join("; ")}` : ""; // #1495: apply-layer per-range errors (unknown refs, …) were invisible From 3a1541c32083c0c21e18be519e0812140b8217ba Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 12:16:33 +0800 Subject: [PATCH 4/7] fix: fold-attribution log adds compressed-view total (Vproc) for #3307 view-factor forensics --- src/stream.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/stream.ts b/src/stream.ts index d46502c013..a4b6e1ff79 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -967,6 +967,11 @@ export function applyRanges(parsed: ReturnType, ctx: const viewMessages = ctx.compressMessages ?? ctx.messages; let viewTokens = 0; for (const m of viewMessages) viewTokens += countMessageTokens(m); + // #3307: also measure the compressed-view total (ctx.messages = processedMessages). + // Logged only, never used — so one capture separates the ~50x view factor (V vs + // Vproc) from the residual chars/4-vs-BPE estimator factor without a second trip. + let processedViewTokens = 0; + for (const m of ctx.messages ?? []) processedViewTokens += countMessageTokens(m); const shrinkRatio = viewTokens > 0 ? Math.min(1, r.tokensCompressed / viewTokens) : 0; const foldPoint = [...ranges].sort((a, b) => refNum(a.startRef) - refNum(b.startRef))[0]?.startRef ?? "unknown"; ctx.session.lastCompress = { at: Date.now(), shrinkRatio, foldPoint, blocks: r.blocksCreated, tokensCompressed: r.tokensCompressed }; @@ -999,7 +1004,7 @@ export function applyRanges(parsed: ReturnType, ctx: // identity as the inflation source. if (newFoldBlocks.length > 0) { const xs = newFoldBlocks.map((b) => (b.startRef ? `${b.startRef}=${prefixTokensBeforeRef(ctx.session, b.startRef)}` : `${b.blockId}=none`)).join(" "); - ctx.log(`[acp-compress-obs] fold-attribution X(firstFoldStartTokens)=${xs} · V=${viewTokens} Vp=${postCtx} (#3307)`); + ctx.log(`[acp-compress-obs] fold-attribution X(firstFoldStartTokens)=${xs} · V=${viewTokens} Vproc=${processedViewTokens} Vp=${postCtx} (#3307)`); } recordCacheFoldsFromBlocks(ctx.session, newFoldBlocks, { V: viewTokens, Vp: postCtx }); From e31661f74f92507f1dbc3f693a374a300d0e622c Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 10:51:17 +0800 Subject: [PATCH 5/7] fix(cache): measure fold-shrink V over the live view, not the raw resend (#3307 P1) #3307 P1: the observability denominator V used (compressMessages ?? messages), which in proxy mode is the client un-shrunken full-history resend -- ~50x the billed input for a stateless client (codex_webui). Every real fold read as a ~1% shrink and postCtx/ceiling/the degenerate-fold gate all ran off the wrong basis. V now measures the LIVE context being managed (ctx.messages / processedMessages, what gets billed next); applyCompression still runs on compressMessages (raw boundaries, #2627) so fold geometry is unchanged. The fold-attribution log now reports V(live) vs Vraw(inbound) so one capture shows the collapse. Behavior change (intended, disclosed): in stateless-client proxy sessions shrink% rises from the degenerate ~1% to its true value; the degenerate-fold gate (DEGENERATE_FOLD_COVERAGE=0.8) and the maxShrink advisory go from effectively-never-firing to firing correctly; postCtx and the next-request cache ceiling are computed from the live view. Unchanged when there is no separate raw resend (messages == compressMessages), which is what every existing unit test exercises. Regression: tests/issue3307-fold-view-basis.test.ts pins V = live view across single/multi-level folds, verifies Vp === V - S (same-view net change), and keeps the messages==compressMessages baseline unchanged. --- src/stream.ts | 23 ++-- tests/issue3307-fold-view-basis.test.ts | 140 ++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 tests/issue3307-fold-view-basis.test.ts diff --git a/src/stream.ts b/src/stream.ts index a4b6e1ff79..38a476d217 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -964,14 +964,23 @@ export function applyRanges(parsed: ReturnType, ctx: // with the kernel's own per-message counter instead — a same-space // denominator makes the ratio structurally ≤1, so #1839's // "trustworthy baseline" gate is subsumed and no longer needed. - const viewMessages = ctx.compressMessages ?? ctx.messages; + // #3307: "the current view" is the LIVE context being managed — the + // compressed outbound (ctx.messages / processedMessages), i.e. what gets + // billed next — NOT the client's un-shrunken resend. applyCompression + // still runs on ctx.compressMessages (raw message boundaries, #2627) so + // the fold geometry maps correctly; only the OBSERVABILITY denominator + // moves. A stateless client (codex_webui) re-sends its FULL history every + // turn, so preferring compressMessages made V ~50x the billed input and + // read every real fold as a ~1% shrink — postCtx/ceiling and the + // degenerate-fold gate below all distorted off the wrong basis. + const viewMessages = ctx.messages; let viewTokens = 0; for (const m of viewMessages) viewTokens += countMessageTokens(m); - // #3307: also measure the compressed-view total (ctx.messages = processedMessages). - // Logged only, never used — so one capture separates the ~50x view factor (V vs - // Vproc) from the residual chars/4-vs-BPE estimator factor without a second trip. - let processedViewTokens = 0; - for (const m of ctx.messages ?? []) processedViewTokens += countMessageTokens(m); + // #3307: keep the raw-inbound total alongside (log-only) so one capture + // shows the view-factor collapse — V=live vs Vraw=inbound — separating it + // from the residual chars/4-vs-BPE estimator factor without a second trip. + let rawViewTokens = 0; + for (const m of ctx.compressMessages ?? ctx.messages) rawViewTokens += countMessageTokens(m); const shrinkRatio = viewTokens > 0 ? Math.min(1, r.tokensCompressed / viewTokens) : 0; const foldPoint = [...ranges].sort((a, b) => refNum(a.startRef) - refNum(b.startRef))[0]?.startRef ?? "unknown"; ctx.session.lastCompress = { at: Date.now(), shrinkRatio, foldPoint, blocks: r.blocksCreated, tokensCompressed: r.tokensCompressed }; @@ -1004,7 +1013,7 @@ export function applyRanges(parsed: ReturnType, ctx: // identity as the inflation source. if (newFoldBlocks.length > 0) { const xs = newFoldBlocks.map((b) => (b.startRef ? `${b.startRef}=${prefixTokensBeforeRef(ctx.session, b.startRef)}` : `${b.blockId}=none`)).join(" "); - ctx.log(`[acp-compress-obs] fold-attribution X(firstFoldStartTokens)=${xs} · V=${viewTokens} Vproc=${processedViewTokens} Vp=${postCtx} (#3307)`); + ctx.log(`[acp-compress-obs] fold-attribution X(firstFoldStartTokens)=${xs} · V=${viewTokens} Vraw=${rawViewTokens} Vp=${postCtx} (#3307)`); } recordCacheFoldsFromBlocks(ctx.session, newFoldBlocks, { V: viewTokens, Vp: postCtx }); diff --git a/tests/issue3307-fold-view-basis.test.ts b/tests/issue3307-fold-view-basis.test.ts new file mode 100644 index 0000000000..45664df3e9 --- /dev/null +++ b/tests/issue3307-fold-view-basis.test.ts @@ -0,0 +1,140 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig, countMessageTokens } from "acp-kernel"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { getCacheLedger } from "../src/cache-ledger.ts"; + +// #3307 P1 regression: the observability denominator V must measure the LIVE +// context being managed (ctx.messages / processedMessages — what gets billed +// next), NOT the client's un-shrunken resend (ctx.compressMessages). A stateless +// client (codex_webui) re-sends its FULL history every turn, so the old +// `compressMessages ?? messages` basis made V ~50x the billed input and read every +// real fold as a ~1% shrink. These tests pin the corrected basis and verify the +// numerator/denominator share one view's net change (Vp === V - S). + +type Ctx = Omit & { log: (m: string) => void; logs: string[] }; + +function makeSession() { + return { + id: "fold-view-basis-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 100000, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +// The raw resend: refs are assigned over THIS array (fold geometry maps onto +// compressMessages), mirroring production where processTurn ran over the client's +// un-shrunken history. Big enough per message to clear the kernel's min range. +function makeRaw(n: number): CoreMessage[] { + const out: CoreMessage[] = []; + for (let i = 1; i <= n; i++) out.push({ id: `raw_${i}`, role: i % 2 === 1 ? "user" : "assistant", contentType: "text", text: `RAW-${i}-` + "x".repeat(20000) }); + return out; +} + +// The compressed live view (ctx.messages / processedMessages) BEFORE this fold: +// an early span already summarized, plus the recent turns still unfolded — so it is +// far smaller than the raw resend yet still LARGER than any single fold target (the +// pre-fold live view contains the about-to-be-folded content unfolded, hence V >= S). +function makeLiveView(): CoreMessage[] { + return [ + { id: "sum_early", role: "user", contentType: "text", text: "[summary] ancient turns folded away" }, + { id: "live_a", role: "assistant", contentType: "text", text: "LIVE-" + "y".repeat(20000) }, + { id: "live_b", role: "user", contentType: "text", text: "LIVE-" + "z".repeat(20000) }, + { id: "live_c", role: "assistant", contentType: "text", text: "LIVE-" + "w".repeat(20000) }, + ]; +} + +function totalTokens(msgs: CoreMessage[]): number { + return msgs.reduce((n, m) => n + countMessageTokens(m), 0); +} + +// refs come from the RAW array (geometry space); messages is the smaller live view. +function makeCtx(raw: CoreMessage[], live: CoreMessage[]): Ctx { + const logs: string[] = []; + const session = makeSession(); + const res = assignRefs(raw, { existing: emptyRefMap(), nextIndex: 0 }); + session.state.messageRefs = res.map; + return { + core: createCore(), + config: defaultConfig(200000), + messages: live, + compressMessages: raw, + session, + log: (m: string) => { logs.push(m); }, + logs, + }; +} + +const SUMMARY = "OBS-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK"; +const ARGS_1_2 = { content: [{ startId: "m00001", endId: "m00002", summary: SUMMARY }] }; +const ARGS_3_4 = { content: [{ startId: "m00003", endId: "m00004", summary: SUMMARY }] }; + +test("#3307 P1: V tracks the live view (ctx.messages), never the raw resend (compressMessages)", () => { + const raw = makeRaw(6); + const live = makeLiveView(); + const rawTotal = totalTokens(raw); + const liveTotal = totalTokens(live); + assert.ok(rawTotal > liveTotal, `precondition: raw (${rawTotal}) must exceed live (${liveTotal}) so the assertion means something`); + + const ctx = makeCtx(raw, live); + getCacheLedger(ctx.session); // bootstrap the ledger before its first fold (#1911 pattern) + const out = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out}`); + + const folds = getCacheLedger(ctx.session).folds; + assert.equal(folds.length, 1, "one fold recorded"); + assert.equal(folds[0].V, liveTotal, `V is the pre-fold LIVE view size (${liveTotal}), not the raw resend`); + assert.notEqual(folds[0].V, rawTotal, "V must NOT be the bloated raw-inbound total"); +}); + +test("#3307 P1: numerator/denominator share one view's net change (Vp === V - S)", () => { + const ctx = makeCtx(makeRaw(6), makeLiveView()); + getCacheLedger(ctx.session); + const out = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out}`); + const folds = getCacheLedger(ctx.session).folds; + const S = folds[0].S, V = folds[0].V, Vp = folds[0].Vp; + assert.ok(S !== undefined, "S recorded"); + assert.ok(V !== undefined, "V recorded"); + assert.ok(Vp !== undefined, "Vp recorded"); + assert.ok(S > 0, "S = tokens removed from the view"); + assert.ok(V > 0, "V = pre-fold live view size"); + assert.equal(Vp, V - S, "post-fold live view === pre-fold live view - removed (same-view net change)"); + assert.ok(Vp >= 0, "post-fold live view is non-negative"); +}); + +test("#3307 P1: multi-level folding keeps tracking the live view on the second fold", () => { + const raw = makeRaw(8); + const live = makeLiveView(); + const liveTotal = totalTokens(live); + const ctx = makeCtx(raw, live); + getCacheLedger(ctx.session); + const o1 = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(o1.startsWith("[Compressed"), `first fold failed: ${o1}`); + const o2 = applyRanges(parseCompressInput(ARGS_3_4), ctx as never).text; + assert.ok(o2.startsWith("[Compressed"), `second fold failed: ${o2}`); + const folds = getCacheLedger(ctx.session).folds; + assert.equal(folds.length, 2, "two folds recorded"); + assert.equal(folds[0].V, liveTotal, "first fold V = live view"); + assert.equal(folds[1].V, liveTotal, "second fold STILL tracks the live view, not the raw resend"); +}); + +test("#3307 P1 baseline: when messages == compressMessages (no raw/live split) V is unchanged", () => { + const raw = makeRaw(6); + const total = totalTokens(raw); + const ctx = makeCtx(raw, raw); // single-level: the live view IS the raw array + getCacheLedger(ctx.session); + const out = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out}`); + assert.equal(getCacheLedger(ctx.session).folds[0].V, total, "V equals the (single) view size — old behavior preserved when no separate raw resend exists"); +}); From da6834ee9f8c012d02a3fc9dfbe54d32676b8b4f Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 11:10:22 +0800 Subject: [PATCH 6/7] fix(cache): attribute fold re-pay via wire-measured prefix, not the inflated snapshot (#3307 P2-A) #3307 P2-A: the snapshot-based fold prefix X (prefixTokensBeforeRef over the client un-shrunken resent history) exceeds the billed input for a stateless proxy client, so decomposeSample zeroes structuralExcess and the fold re-pay falls into the unattributed seam bucket. When a genuine bili-side mid-history break is present, measure the stable prefix off the wire (byte-LCP of this outbound vs the previous, scaled to billed input) and min() it into X before decomposeSample, so the fold is credited again instead of crying a false seam. Gated to mid-list breaks only (a tail append lands the LCP near the end -> xMeasured ~= input -> no effect) and never on a client-side inbound rewrite (#3023), whose earlier divergence would understate and mis-credit the fold. Host-side only -- no kernel change; decomposeSample is called as-is with the corrected firstFoldStartTokens. Regression: tests/issue3307-fold-attribution-wire.test.ts pins attribution for the inflated-X mid-history fold plus both guards (tail-append not credited; client rewrite stays client-side, not fold). --- src/cache-ledger.ts | 34 ++++++ tests/issue3307-fold-attribution-wire.test.ts | 102 ++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 tests/issue3307-fold-attribution-wire.test.ts diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index a48e222dbd..ab3f61e306 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1014,6 +1014,40 @@ export function recordCacheSample( viewBefore: f.V, viewAfter: f.Vp, })); + // #3307 P2-A: f.X (firstFoldStartTokens) is prefixTokensBeforeRef over the + // client's UN-SHRUNKEN resent history — for a stateless proxy client that far + // exceeds the billed input, so decomposeSample zeroes structuralExcess and the + // fold's re-pay falls into the unattributed seam bucket. When a genuine + // bili-side mid-history break is present, measure the stable prefix straight + // off the wire instead: the byte-LCP between this outbound and the previous + // marks where a summary replaced folded content, so (lcpBytes / cur.bytes) * + // billed input is a same-space lower bound on the pre-fold prefix. min() with + // the snapshot X leaves already-correct folds untouched. Gated to mid-list + // breaks only (a tail append lands lcpBytes near the end -> xMeasured ~= input + // -> no effect) and never on a client-side inbound rewrite (#3023), whose + // earlier divergence would understate the fold and mis-credit it. + if (pending.length > 0 && known && s.input > 0) { + const rawMissed = s.input - effCached; + if (rawMissed > 8192 && rawMissed > 0.3 * s.input) { + const curB = session.transient?.sentBody; + const prevB = session.transient?.settledBody; + if (curB !== undefined && prevB !== undefined && curB.bytes <= SEAM_BODY_CAP && prevB.bytes <= SEAM_BODY_CAP) { + const curIn = session.transient?.inBody; + const prevIn = session.transient?.settledInBody; + const clientBreak = curIn !== undefined && prevIn !== undefined && curIn.msgs <= INBOUND_MSG_CAP && prevIn.msgs <= INBOUND_MSG_CAP && (() => { + const k = firstDivergentInboundMsg(prevIn, curIn); + return k !== null && k < Math.min(prevIn.msgs, curIn.msgs); + })(); + if (!clientBreak) { + const lcp = seamLcp(prevB.head, curB.head); + if (lcp.msgIndex !== null && lcp.prevMsgs > 0 && lcp.msgIndex < lcp.prevMsgs && lcp.lcpBytes > 0 && lcp.lcpBytes < curB.head.byteLength) { + const xMeasured = Math.round(s.input * (lcp.lcpBytes / curB.bytes)); + for (const p of pending) p.firstFoldStartTokens = Math.min(p.firstFoldStartTokens ?? Number.POSITIVE_INFINITY, xMeasured); + } + } + } + } + } const rawDec = decomposeSample( prevLine ? { at: prevLine.at, input: prevLine.input, cached: prevLine.cached } : null, { at: s.at, input: s.input, cached: effCached }, diff --git a/tests/issue3307-fold-attribution-wire.test.ts b/tests/issue3307-fold-attribution-wire.test.ts new file mode 100644 index 0000000000..0d7b0d0ce1 --- /dev/null +++ b/tests/issue3307-fold-attribution-wire.test.ts @@ -0,0 +1,102 @@ +// #3307 P2-A regression: a stateless proxy client resends its full un-shrunken +// history every turn, so the snapshot-based fold prefix X (prefixTokensBeforeRef) +// far exceeds the billed input and decomposeSample zeroes structuralExcess — the +// fold's re-pay then falls into the unattributed seam bucket instead of being +// credited to the fold. P2-A measures the stable prefix off the wire (byte-LCP of +// this outbound vs the previous, scaled to billed input) and min()s it into X, so +// a genuine bili-side mid-history break attributes to its fold again. These pin the +// fix AND its two guards: a pure tail-append must NOT be credited to a pending fold +// (no false credit on normal growth), and a client-side inbound rewrite (#3023) must +// NOT be mis-credited either. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { noteForwardedBody, noteInboundMessages, settleUsageReport, getCacheLedger } from "../src/cache-ledger.ts"; +import type { Session } from "../src/session.ts"; + +let seq = 0; +function makeSession(): Session { + seq += 1; + return { + id: `foldwire-${seq}`, + metadata: {}, + stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +const body = (msgs: string[]): string => JSON.stringify({ model: "m", messages: msgs.map((c) => ({ role: "user", content: c })) }); +// Anchor in the past relative to the live clock: settleUsageReport stamps each +// sample with Date.now(), so a pushed fold's createdAt must precede it to become +// pending (a fixed future literal would silently drop the fold from the window). +const T0 = Date.now() - 3_600_000; + +function settle(session: Session, at: number, input: number, cached: number): void { + settleUsageReport(session, { total: input, reportedCached: cached, output: 0, protocol: "openai", upstream: "http://u" }); +} + +// Snapshot whose pre-fold prefix sums to 49*6000 = 294000 tokens — larger than any +// billed input below, so the UNcorrected firstFoldStartTokens zeroes structuralExcess +// exactly like the production stateless-resend case. +function makeSessionWithInflatedPrefix(): Session { + const s = makeSession(); + const st = s.state as unknown as { tokenSnapshot: Record; messageRefs: { byRef: Record } }; + for (let i = 1; i <= 49; i++) { + const r = `m${String(i).padStart(5, "0")}`; + st.tokenSnapshot[r] = 6000; + st.messageRefs.byRef[r] = `raw_${i}`; + } + return s; +} + +function pushFold(s: Session, at: number): void { + const st = s.state as unknown as { blocks: Array> }; + // startRef m00050 -> prefixTokensBeforeRef sums m00001..m00049 = 294000. + st.blocks.push({ blockId: "b1", createdAt: at, compressedTokens: 30_000, summary: "x".repeat(200), startRef: "m00050" }); +} + +test("P2-A: inflated snapshot X + wire mid-history break attributes the fold (not a seam)", () => { + const s = makeSessionWithInflatedPrefix(); + noteForwardedBody(s, body(["a", "b", "c", "d"])); + settle(s, T0, 100_000, 99_000); + pushFold(s, T0 + 500); + // Next turn: a summary replaces message[1] (mid-history break), big residual. + noteForwardedBody(s, body(["a", "SUMMARY", "c", "d"])); + settle(s, T0 + 1000, 100_000, 30_000); + const led = getCacheLedger(s); + const line = led.lines[led.lines.length - 1]!; + assert.ok(line.foldSeq !== null, "the fold's re-pay must be attributed via the wire-measured prefix (uncorrected X=294000 would zero structuralExcess)"); + assert.notEqual(line.seam, 1, "an attributed fold must not flag as an unexplained seam"); +}); + +test("P2-A: a pure tail-append with a pending inflated-X fold is NOT credited to the fold", () => { + const s = makeSessionWithInflatedPrefix(); + noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + pushFold(s, T0 + 500); + // Pure tail append — no mid-history break, so the wire correction must not fire. + noteForwardedBody(s, body(["a", "b", "c"])); + settle(s, T0 + 1000, 100_000, 30_000); + const led = getCacheLedger(s); + const line = led.lines[led.lines.length - 1]!; + assert.equal(line.foldSeq, null, "tail-append growth must not be credited to the fold"); + assert.equal(led.agg.providerSideMisses, 1, "the tail-append miss stays provider-side"); + assert.notEqual(line.seam, 1); +}); + +test("P2-A: a client-side inbound rewrite is NOT credited to a pending fold (#3023)", () => { + const s = makeSessionWithInflatedPrefix(); + noteForwardedBody(s, body(["a", "b", "c"])); + noteInboundMessages(s, [{ role: "user", content: "a" }, { role: "user", content: "b" }, { role: "user", content: "c" }] as unknown as CoreMessage[]); + settle(s, T0, 100_000, 99_000); + pushFold(s, T0 + 500); + // Outbound mid-break AND inbound mid-rewrite (index 1 changed) — host churn, not bili. + noteForwardedBody(s, body(["a", "X2", "c"])); + noteInboundMessages(s, [{ role: "user", content: "a" }, { role: "user", content: "CHANGED" }, { role: "user", content: "c" }] as unknown as CoreMessage[]); + settle(s, T0 + 1000, 100_000, 30_000); + const led = getCacheLedger(s); + const line = led.lines[led.lines.length - 1]!; + assert.equal(line.foldSeq, null, "client rewrite must not be credited to the fold"); + assert.equal(line.prov, "client", "classified client-side (#3023)"); + assert.equal(led.agg.clientInserts, 1); +}); From fad2033510c565c8d01f5b076adc883d3f760e24 Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 11:25:22 +0800 Subject: [PATCH 7/7] test(cache): pin P1 re-folding V basis (live view after a prior fold shrank it) (#3307) The multi-level P1 test held the live view constant across folds, so it did not cover the re-fold topology the #3307 GO called out (a fold whose live view already embeds an earlier summary and is smaller than the raw resend). Add a case that shrinks ctx.messages after fold-1 and asserts fold-2 V tracks that current view, never the raw resend or a stale snapshot. --- tests/issue3307-fold-view-basis.test.ts | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/issue3307-fold-view-basis.test.ts b/tests/issue3307-fold-view-basis.test.ts index 45664df3e9..aba0976eff 100644 --- a/tests/issue3307-fold-view-basis.test.ts +++ b/tests/issue3307-fold-view-basis.test.ts @@ -129,6 +129,33 @@ test("#3307 P1: multi-level folding keeps tracking the live view on the second f assert.equal(folds[1].V, liveTotal, "second fold STILL tracks the live view, not the raw resend"); }); +test("#3307 P1: re-folding still measures V off the already-shrunk live view", () => { + const raw = makeRaw(8); + const live0 = makeLiveView(); + const ctx = makeCtx(raw, live0); + getCacheLedger(ctx.session); + const o1 = applyRanges(parseCompressInput(ARGS_1_2), ctx as never).text; + assert.ok(o1.startsWith("[Compressed"), `first fold failed: ${o1}`); + assert.equal(getCacheLedger(ctx.session).folds[0].V, totalTokens(live0), "fold-1 V = pre-fold live view"); + // Re-folding: after fold-1 the live view has SHRUNK — the folded region is now a + // summary. V must track THIS current (smaller, summary-bearing) view, not the raw + // resend nor a stale pre-fold snapshot. This is the topology the existing multi-level + // test did not pin (it held the live view constant across folds). + const live1: CoreMessage[] = [ + { id: "sum_early", role: "user", contentType: "text", text: "[summary] ancient turns folded away" }, + { id: "sum_fold1", role: "user", contentType: "text", text: "[summary] fold-1 region collapsed" }, + { id: "live_b", role: "user", contentType: "text", text: "LIVE-" + "z".repeat(20000) }, + { id: "live_c", role: "assistant", contentType: "text", text: "LIVE-" + "w".repeat(20000) }, + ]; + assert.ok(totalTokens(live1) < totalTokens(live0), "precondition: the live view shrank after fold-1"); + ctx.messages = live1; + const o2 = applyRanges(parseCompressInput(ARGS_3_4), ctx as never).text; + assert.ok(o2.startsWith("[Compressed"), `re-fold failed: ${o2}`); + const folds = getCacheLedger(ctx.session).folds; + assert.equal(folds.length, 2, "two folds recorded"); + assert.equal(folds[1].V, totalTokens(live1), "re-fold V tracks the CURRENT (shrunken) live view, never the raw resend or a stale snapshot"); +}); + test("#3307 P1 baseline: when messages == compressMessages (no raw/live split) V is unchanged", () => { const raw = makeRaw(6); const total = totalTokens(raw);