diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index ba11826d09..a3810e6603 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -175,12 +175,17 @@ interface LedgerLine { /** #2131: first diverging byte offset when be=0 and both full bodies were * retained under BODY_FULL_CAP. Sparse. */ bl?: number; + /** #2866: longest common SUFFIX of the two bodies, stored only on the + * slow path where prefix+suffix coverage decided the class. Sparse. */ + bls?: number; /** #2131: divergence class of a be=0 pair — "head": change within the first * 1 KiB (model/tools/sampling-params/system region → host-side parameter * change, not a history rewrite); "append": the whole shorter payload - * survives ±64 closing-bracket bytes (pure tail growth/trim); "mid": a - * mid-history rewrite; "unknown": bodies above BODY_FULL_CAP (inequality - * proven by digest, offset unavailable). Sparse. */ + * survives ±64 bytes — proven by prefix reach OR by prefix+suffix + * coverage (#2866: wire bodies carry fields after the message array, so + * pure tail-append diverges at the array's close); "mid": a mid-history + * rewrite; "unknown": bodies above BODY_FULL_CAP (inequality proven by + * digest, offset unavailable). Sparse. */ bs?: "head" | "append" | "mid" | "unknown"; } @@ -564,6 +569,23 @@ function byteLcp(a: string, b: string): number { return lcp; } +// #2866: the suffix twin of byteLcp — how many trailing bytes the two bodies +// still share. Real wire bodies serialize fields AFTER the message array +// (tools, stream_options), so a pure tail-append diverges at the array's close +// while everything from there to the payload end survives verbatim. +function byteLcs(a: string, b: string): number { + let lcs = 0; + const n = Math.min(a.length, b.length); + let ia = a.length - 1; + let ib = b.length - 1; + while (lcs < n && ia >= 0 && ib >= 0 && a.charCodeAt(ia) === b.charCodeAt(ib)) { + lcs++; + ia--; + ib--; + } + return lcs; +} + function detectSeam(session: Session, led: CacheLedger): void { const line = led.lines[led.lines.length - 1]; if (!line || line.unk === 1 || line.missed <= 0) return; @@ -1253,10 +1275,28 @@ export function settleUsageReport( if (curSlot.full !== null && prevSlot.full !== null) { const lcp = byteLcp(prevSlot.full, curSlot.full); curLine.bl = lcp; + const minLen = Math.min(prevSlot.full.length, curSlot.full.length); // Append/trim first: "the whole shorter payload survives" // is anchored to the END of the payload and stays true on // small bodies where the head threshold alone would lie. - curLine.bs = lcp >= Math.min(prevSlot.full.length, curSlot.full.length) - 64 ? "append" : lcp < 1024 ? "head" : "mid"; + if (lcp >= minLen - 64) { + curLine.bs = "append"; + } else if (lcp < 1024) { + // #2866: head pairs never get suffix-rescued — a model + // switch on a small body must stay head even when the + // trailing fields happen to match. + curLine.bs = "head"; + } else { + // #2866: wire bodies serialize fields AFTER the message + // array (tools, stream_options), so a pure tail-append + // diverges at the array's close, far short of the payload + // end. Prefix+suffix coverage under the same ±64B + // tolerance proves the shorter payload still survives; + // only genuine mid-history rewrites fall through to mid. + const lcs = byteLcs(prevSlot.full, curSlot.full); + if (lcs > 0) curLine.bls = lcs; + curLine.bs = lcp + lcs >= minLen - 64 ? "append" : "mid"; + } } else { curLine.bs = "unknown"; } @@ -1544,6 +1584,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport { sysFp: l.sp, bodyEqual: l.be, bodyLcp: l.bl, + bodyLcs: l.bls, bodyClass: l.bs, })), linesOmitted: led.sampleSeq - led.lines.length, diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 4e2a78c32f..a40caa14d1 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "1088bae87675b4b44d38c604cd1f7456b665e53dd4ba27649048ebda00ec31f8", + "stateDigest": "54e2bf9113918ddcd6537829e2c4739789e2707506833ed2bbe5c380976de1d1", "sectionDigests": { "state": "b8d5d5c3a1d6423b", "blockContents": "b3a9c59b22ec1a3a", "stats": "5dedce65e3559c6c", - "metadata": "e912f03b1cd341f4" + "metadata": "4456c4109b8f568e" }, "stateKeyDigests": { "blocks": "8a3021f39f1f2eb8", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 2190701b7d..3aef96c3b6 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "384f7fd038ac18c7517c7a7d452d68a862616063d1d191133fc2e124eb5957b9", + "stateDigest": "b50a8f2fb47ad92ff4142134af9ac2d2fd39b00273976e54ca80894b4a8b8684", "sectionDigests": { "state": "aa312c3572a95958", "blockContents": "dd2de296a5bc6160", "stats": "88eb19a9daefcbc3", - "metadata": "0d8b8f419b38f159" + "metadata": "f860a5a1b7c8844d" }, "stateKeyDigests": { "blocks": "bdde93b0e100e13e", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index 9f4049d79e..7c9d02f95d 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "7fd4909d7178239b13237590d0ccd03103dd8e46e7642ea620dcc82155187a78", + "stateDigest": "acb2872cd54bbbfb207b6727c1d874e587864853608f5e3ac3f68c2e6008f845", "sectionDigests": { "state": "3d8afb23f4d63b31", "blockContents": "4cc83a808a659f28", "stats": "c95acb7dd4c8d0b4", - "metadata": "db5726ac9598a0e2" + "metadata": "2ddcdf09630b5780" }, "stateKeyDigests": { "blocks": "9adb53303cd4b786", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index cb2790f43b..ca3a19afea 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "e6e2a18fc7c9b1f44a0572b54008817e21db85fb0e98829e3bde7d1506c21820", + "stateDigest": "a3f1b0dc9745a71635378759f70e2f4f8be385d16bfda24171a8713c02d978f1", "sectionDigests": { "state": "1361703652576fc5", "blockContents": "1727e2e1faf79b55", "stats": "07d32933c52cb7f8", - "metadata": "61c8172889bbdc91" + "metadata": "c679e7df1b9b3bc9" }, "stateKeyDigests": { "blocks": "0458e9c8a7bb1a99", diff --git a/tests/issue2131-body-stability-visibility.test.ts b/tests/issue2131-body-stability-visibility.test.ts index a752a3c478..f4ef042f66 100644 --- a/tests/issue2131-body-stability-visibility.test.ts +++ b/tests/issue2131-body-stability-visibility.test.ts @@ -5,6 +5,9 @@ // aggregate (plus size-bucket hit stratification and the low/high-hit gap // split) so "was the prefix unchanged between calls" is stored data visible in // /acp-cache, __bili/cache-report and the dashboard — not an interpretation. +// #2866: real wire bodies serialize fields AFTER the message array (tools, +// stream_options), so a pure tail-append diverges at the array's close — the +// append class is decided by prefix+suffix coverage, not prefix reach alone. import { test } from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; @@ -101,6 +104,79 @@ test("mid-history change classifies as a real rewrite (bs=mid)", () => { assert.equal(rep.stability.mid, 1); }); +// Realistic outbound shape: fields serialized AFTER the message array (the +// actual wire order on every bili lane) make pure tail-append diverge at the +// array's close — the #2866 regression surface. +function wireBody(msgs: string[], model = "m"): string { + const tools = Array.from({ length: 8 }, (_, i) => ({ + type: "function", + function: { + name: `tool_${i}`, + description: `Tool ${i} does something useful for the agent.` + "p".repeat(120), + parameters: { type: "object", properties: { arg: { type: "string", description: "q".repeat(80) }, n: { type: "number" } }, required: ["arg"] }, + }, + })); + return JSON.stringify({ model, stream: true, messages: msgs.map((c) => ({ role: "user", content: c })), tools, stream_options: { include_usage: true } }); +} + +test("#2866 realistic wire body: pure tail-append with trailing fields classifies bs=append", () => { + const s = makeSession(); + const many = Array.from({ length: 6 }, (_, i) => `msg${i}-${"x".repeat(400)}`); + const b1 = wireBody(many); + const b2 = wireBody([...many, `extra-${"y".repeat(200)}`, `more-${"z".repeat(200)}`]); + noteForwardedBody(s, b1); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, b2); + settle(s, T0 + 1000, 100_000, 90_000); + const line = getCacheLedger(s).lines[getCacheLedger(s).lines.length - 1]!; + assert.equal(line.be, 0); + // Divergence lands at the messages array's close — far short of the payload + // end because tools/stream_options follow the array. Prefix reach alone + // (the pre-#2866 rule) would have misclassified this pair as mid. + assert.ok(line.bl !== undefined && line.bl < b1.length - 64, "LCP stops at the array close, not the payload end"); + assert.equal(line.bs, "append", "prefix+suffix coverage proves the shorter payload survives"); + assert.ok(line.bls !== undefined && line.bls > 0, "slow path stores the suffix reach"); + const rep = buildSessionCacheReport(s); + assert.equal(rep.stability.append, 1); + assert.equal(rep.stability.mid, 0); +}); + +test("#2866 realistic wire body: genuine mid-history rewrite stays bs=mid (no over-rescue)", () => { + const s = makeSession(); + const many = Array.from({ length: 8 }, (_, i) => `msg${i}-${"x".repeat(500)}`); + const b1 = wireBody(many); + const changed = [...many]; + changed[3] = `msg3-${"y".repeat(700)}`; + const b2 = wireBody(changed); + noteForwardedBody(s, b1); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, b2); + settle(s, T0 + 1000, 100_000, 90_000); + const line = getCacheLedger(s).lines[getCacheLedger(s).lines.length - 1]!; + assert.equal(line.be, 0); + assert.equal(line.bs, "mid", "length-changing mid rewrite must not be suffix-rescued"); + assert.ok(line.bl !== undefined && line.bl > 1024 && line.bl < b1.length - 64); + const rep = buildSessionCacheReport(s); + assert.equal(rep.stability.mid, 1); +}); + +test("#2866 realistic wire body: model switch stays bs=head despite an identical tail", () => { + const s = makeSession(); + const many = Array.from({ length: 4 }, (_, i) => `msg${i}-${"x".repeat(400)}`); + const b1 = wireBody(many); + const b2 = wireBody(many, "OTHER-MODEL"); + noteForwardedBody(s, b1); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, b2); + settle(s, T0 + 1000, 100_000, 90_000); + const line = getCacheLedger(s).lines[getCacheLedger(s).lines.length - 1]!; + assert.equal(line.be, 0); + assert.ok(line.bl !== undefined && line.bl < 1024); + assert.equal(line.bs, "head", "head pairs are never suffix-rescued — a model switch stays host-side"); + const rep = buildSessionCacheReport(s); + assert.equal(rep.stability.head, 1); +}); + test("bodies above the retention cap stay provable via digest only (bs=unknown)", () => { const s = makeSession(); const big1 = JSON.stringify({ model: "m", messages: [{ role: "user", content: "x".repeat(8 * 1024 * 1024 + 1024) }] });