From 62e2dcddb6e4bf19c4e20408302e5a84b1a5efef Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 12:10:17 +0800 Subject: [PATCH] fix: classify in-unit growth as a semantic append in body stability (#3476) The panel bs classifier and the acp-cache diff tool both defined 'append' by the first diverging byte offset. A layout that grows inside a single logical unit -- auto-review's giant user message whose FILTERED_HISTORY gains entries while a fixed PENDING_ACTION block stays pinned at the end -- is semantically an append (no prior byte moved or changed) yet its first divergence lands mid-body, so both stably reported 'mid' / mid-stream-rewrite (false positive). Recognize a semantic append: when the older payload is fully covered by common prefix+suffix (empty middle residual) and the newer one carries extra bytes in that middle. A genuine in-place rewrite leaves a non-empty residual on the shorter side, so it is never matched -- this cannot mask a real mid-history break. --- src/acp-cache-diff.ts | 33 +++++- src/cache-ledger.ts | 20 +++- tests/issue3476-mid-insertion-append.test.ts | 117 +++++++++++++++++++ 3 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 tests/issue3476-mid-insertion-append.test.ts diff --git a/src/acp-cache-diff.ts b/src/acp-cache-diff.ts index efb2874c77..ca141e4ad9 100644 --- a/src/acp-cache-diff.ts +++ b/src/acp-cache-diff.ts @@ -33,7 +33,12 @@ interface AppendInfo { // appended elements) — the healthy shape for array-appending clients. // tail-fields: same element stability, but the divergence is trailing // fields after the message array. - kind: "strict" | "json-tail" | "tail-fields"; + // mid-insert: same element COUNT, but one element's INTERIOR grew (a single + // logical unit whose content extended in place, e.g. auto-review's giant + // user message with a growing FILTERED_HISTORY + fixed PENDING_ACTION + // trailer) — byte-wise a mid-body insertion, semantically an append + // (#3476). + kind: "strict" | "json-tail" | "tail-fields" | "mid-insert"; countA: number; countB: number; } @@ -179,6 +184,22 @@ function prefixCompare(a: Buffer, b: Buffer): { rel: "append" | "diverge"; offse return { rel: "diverge", offset: n, extra: b.length - a.length }; } +// #3476: true iff `prev` is fully covered by the common prefix (length `lcp`) +// plus some common suffix — i.e. its middle residual is EMPTY — while `cur` +// carries extra bytes in that middle. That is a SEMANTIC append: no prior byte +// moved or changed, only new bytes landed between a stable head and a stable +// trailer (e.g. a single giant message whose interior container grows, with a +// fixed trailer shifting right). A genuine rewrite leaves a non-empty residual +// on the shorter side, so it is never matched here — this cannot mask a real +// mid-history break. +function isMidInsertionAppend(prev: Buffer, cur: Buffer, lcp: number): boolean { + if (cur.length <= prev.length || lcp >= prev.length) return false; + const room = prev.length - lcp; + let lcs = 0; + while (lcs < room && prev[prev.length - 1 - lcs] === cur[cur.length - 1 - lcs]) lcs++; + return lcp + lcs === prev.length; +} + const MSG_KEYS = ["messages", "input", "contents"]; interface MsgArrayInfo { @@ -352,6 +373,16 @@ export function compareWires(aBody: Buffer, bBody: Buffer): WireRel { } } } + // #3476: same element count but one element's interior grew — a single + // logical unit extending in place (auto-review FILTERED_HISTORY + fixed + // PENDING_ACTION trailer). Byte-wise a mid-body insertion; semantically an + // append. Recognize it instead of flagging a rewrite. + if (isMidInsertionAppend(aBody, bBody, O)) { + return { + rel: "append", byteOffset: O, extra: byteRel.extra, preArray: false, + append: { kind: "mid-insert", countA: ia?.items.length ?? 0, countB: ib?.items.length ?? 0 }, + }; + } return { rel: "diverge", byteOffset: O, extra: byteRel.extra, append: null, preArray: false }; } diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 38fc7d93ea..a21ee159dd 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -760,6 +760,20 @@ function byteLcp(a: Buffer, b: Buffer): number { return lcp; } +// #3476: true iff `prev` is fully covered by the common prefix (length `lcp`) +// plus some common suffix (its middle residual is EMPTY) while `cur` carries +// extra bytes in that middle — a SEMANTIC append where one logical unit's +// interior grows in place (auto-review FILTERED_HISTORY + fixed PENDING_ACTION +// trailer). A genuine rewrite leaves a non-empty residual on the shorter side, +// so it is never matched here and stays "mid". Mirrors src/acp-cache-diff.ts. +function isMidInsertionAppend(prev: Buffer, cur: Buffer, lcp: number): boolean { + if (cur.length <= prev.length || lcp >= prev.length) return false; + const room = prev.length - lcp; + let lcs = 0; + while (lcs < room && prev[prev.length - 1 - lcs] === cur[cur.length - 1 - lcs]) lcs++; + return lcp + lcs === prev.length; +} + // #2982: byte offset of the CLOSING bracket terminating the top-level // messages/input/contents array (same key set as seamLcp's msgsOf), or null // when the body carries no such array (or is not valid JSON). String-aware @@ -1684,7 +1698,11 @@ export function settleUsageReport( // (unparseable body) keeps the conservative pre-fix class. const shorterFull = prevSlot.full.length <= curSlot.full.length ? prevSlot.full : curSlot.full; const arrClose = messagesArrayCloseOffset(shorterFull); - curLine.bs = lcp >= Math.min(prevSlot.full.length, curSlot.full.length) - 64 ? "append" : lcp < 1024 ? "head" : arrClose !== null && lcp >= arrClose ? "tail" : "mid"; + // #3476: a growing logical unit (single giant message whose interior + // container extends in place) is byte-wise a mid-body insertion but + // semantically an append — recognize it in the would-be-mid slot. + const midInsert = isMidInsertionAppend(prevSlot.full, curSlot.full, lcp); + curLine.bs = lcp >= Math.min(prevSlot.full.length, curSlot.full.length) - 64 ? "append" : lcp < 1024 ? "head" : arrClose !== null && lcp >= arrClose ? "tail" : midInsert ? "append" : "mid"; } else { curLine.bs = "unknown"; } diff --git a/tests/issue3476-mid-insertion-append.test.ts b/tests/issue3476-mid-insertion-append.test.ts new file mode 100644 index 0000000000..597d7404df --- /dev/null +++ b/tests/issue3476-mid-insertion-append.test.ts @@ -0,0 +1,117 @@ +// #3476: the per-call body-stability classifier (panel `bs`, src/cache-ledger.ts) +// and the offline `bili acp-cache diff` tool (src/acp-cache-diff.ts::compareWires) +// both defined "append" purely by the FIRST diverging byte offset. A layout that +// grows INSIDE a single logical unit — auto-review's giant user message, whose +// FILTERED_HISTORY gains entries while a fixed PENDING_ACTION block stays at the +// very end — is semantically an append (no prior byte moved or changed) yet its +// first divergence lands mid-body, so both classifiers stably reported "mid" +// (false positive). Fix: recognize a SEMANTIC append — the older payload fully +// covered by common prefix+suffix (empty middle residual) while the newer one +// carries extra bytes in that middle. These tests pin that recognition AND guard +// that a genuine in-place rewrite of the same unit still reads as a rewrite. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { compareWires, runDiff } from "../src/acp-cache-diff.ts"; +import { noteForwardedBody, settleUsageReport, getCacheLedger, buildSessionCacheReport } from "../src/cache-ledger.ts"; +import type { Session } from "../src/session.ts"; +import { rmrf } from "./tmp-rm.ts"; + +// auto-review sidecar shape: fixed REVIEW_POLICY system + one giant user message +// whose FILTERED_HISTORY grows in place while PENDING_ACTION stays pinned last. +function reviewBody(history: string[]): string { + const sys = JSON.stringify({ role: "system", content: "REVIEW_POLICY\nYou are the final authorization reviewer for exactly one pending tool call." + "policy-body-fill ".repeat(40) }); + const env = "ENVIRONMENT\nPROJECT_INSTRUCTIONS\n" + "instruction-line ".repeat(60); + const hist = history.map((h) => JSON.stringify(h)).join(","); + const user = JSON.stringify({ role: "user", content: `${env}FILTERED_HISTORY [${hist}] PENDING_ACTION ${JSON.stringify({ tool: "bash", cmd: "rm -rf build" })}` }); + return `{"model":"gpt-x","messages":[${sys},${user}]}`; +} + +let seq = 0; +function makeSession(): Session { + seq += 1; + return { + id: `midins-${seq}`, + metadata: {}, + stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} +function settle(s: Session, input: number, cached: number): void { + settleUsageReport(s, { total: input, reportedCached: cached, output: 0, protocol: "openai", upstream: "http://u" }); +} + +test("compareWires: growing-container insert (auto-review shape) is append(mid-insert), not a rewrite", () => { + const a = Buffer.from(reviewBody(["turn-1 transcript chunk"])); + const b = Buffer.from(reviewBody(["turn-1 transcript chunk", "turn-2 transcript chunk"])); + const r = compareWires(a, b); + assert.equal(r.rel, "append", "semantic append must not read as a mid-stream rewrite"); + assert.equal(r.append?.kind, "mid-insert"); +}); + +test("compareWires: in-place REWRITE of the same unit still diverges (no masking a real break)", () => { + const a = Buffer.from(reviewBody(["alpha-entry"])); + const b = Buffer.from(reviewBody(["omega-entry"])); + const r = compareWires(a, b); + assert.equal(r.rel, "diverge", "a changed byte inside the unit is a real rewrite — mid-insert must not swallow it"); +}); + +test("compareWires: healthy array-append is still append(json-tail), unchanged by the fix", () => { + const m1 = { role: "user", content: "one" }; + const m2 = { role: "assistant", content: "two" }; + const a = Buffer.from(JSON.stringify({ model: "m", messages: [m1] })); + const b = Buffer.from(JSON.stringify({ model: "m", messages: [m1, m2] })); + const r = compareWires(a, b); + assert.equal(r.rel, "append"); + assert.equal(r.append?.kind, "json-tail"); +}); + +test("runDiff: auto-review sidecar pair classifies pure-append (was mid-stream-rewrite)", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "acp3476-")); + try { + const tA = Date.parse("2026-10-16T11:19:00.000Z"); + const tB = tA + 6_000; + const hdrs = "content-type: application/json"; + const write = (ts: number, kind: "REQ", body: string): void => { + fs.writeFileSync(path.join(dir, `${ts}-subrev-${kind}.txt`), `POST /v1/chat/completions\n${hdrs}\n\n${body}`); + }; + write(tA, "REQ", reviewBody(["turn-1 transcript chunk"])); + write(tB, "REQ", reviewBody(["turn-1 transcript chunk", "turn-2 transcript chunk"])); + const report = runDiff(dir, { noLog: true }); + assert.equal(report.totalPairs, 1); + const p = report.sessions[0]!.pairs[0]!; + assert.equal(p.category, "pure-append", "the sidecar's semantic append must not be charged as a rewrite"); + assert.equal(p.outgoing, "append"); + assert.equal(p.outgoingAppend?.kind, "mid-insert"); + } finally { rmrf(dir); } +}); + +test("panel bs: growing-container insert is append (was mid), stability.mid stays 0", () => { + const s = makeSession(); + noteForwardedBody(s, reviewBody(["turn-1 transcript chunk"])); + settle(s, 100_000, 99_000); + noteForwardedBody(s, reviewBody(["turn-1 transcript chunk", "turn-2 transcript chunk"])); + settle(s, 100_000, 90_000); + const line = getCacheLedger(s).lines.at(-1)!; + assert.equal(line.be, 0); + assert.equal(line.bs, "append", "byte-wise mid-body insertion of a stable-trailer unit is a semantic append"); + const rep = buildSessionCacheReport(s); + assert.equal(rep.stability.append, 1); + assert.equal(rep.stability.mid, 0, "no mid-history count may be charged for the auto-review layout"); +}); + +test("panel bs: in-place REWRITE of the same unit still classifies mid (guard)", () => { + const s = makeSession(); + noteForwardedBody(s, reviewBody(["alpha-entry"])); + settle(s, 100_000, 99_000); + noteForwardedBody(s, reviewBody(["omega-entry"])); + settle(s, 100_000, 90_000); + const line = getCacheLedger(s).lines.at(-1)!; + assert.equal(line.be, 0); + assert.equal(line.bs, "mid", "a changed byte inside the unit is a real mid-history break"); + const rep = buildSessionCacheReport(s); + assert.equal(rep.stability.mid, 1); + assert.equal(rep.stability.append, 0); +});