Skip to content
58 changes: 52 additions & 6 deletions src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -1913,16 +1947,22 @@ export function handleAcpCache(session: Session, args?: Record<string, unknown>)
// #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(
{ ...report, lines: report.lines.slice(-FULL_DETAIL_LINES), linesOmitted: report.linesOmitted + dropped },
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)}]`);
Expand Down Expand Up @@ -1968,11 +2008,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)");
}
Expand Down
34 changes: 27 additions & 7 deletions src/stream.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -964,9 +964,23 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, 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: 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 };
Expand All @@ -991,11 +1005,17 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, 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} Vraw=${rawViewTokens} 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
Expand Down
30 changes: 30 additions & 0 deletions tests/cache-ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>).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";
Expand Down
102 changes: 102 additions & 0 deletions tests/issue3307-fold-attribution-wire.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, number>; messageRefs: { byRef: Record<string, string> } };
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<Record<string, unknown>> };
// 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);
});
Loading
Loading