Skip to content

[PR] fix: keep whole-message inserts out of semantic appends - #6738

Merged
ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-14_cache-diff-insertion-boundaries
Nov 14, 2026
Merged

ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-14_cache-diff-insertion-boundaries

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Closes #6736

Problem

The offline cache-diff tool treats any byte insertion surrounded by an unchanged prefix and suffix as semantic message growth. That shape also describes inserting a complete message into the middle of existing history, prepending a message, or lengthening a model name before the conversation array. These are not growth inside one existing message, but the report calls them pure-append and loses client/proxy rewrite attribution.

The special case introduced in #3479 explicitly describes an unchanged element count and growth inside one logical unit. Its actual condition only checks the byte prefix/suffix. This patch enforces the documented structural boundary while retaining the intended large single-message history container.

Change

Accept mid-insert only when both bodies select the same root message array, its element count stays unchanged, and one existing element span contains the insertion and grows by exactly the added text length. Whole-message insertions and changes before the array fall through to divergence. Field changes retain the existing preArray note in the public report.

Handle an initially empty array as an ordinary first tail append. Skip legal JSON whitespace before separators when collecting spans, so the tighter check continues to recognize growth in later elements of a whitespace-formatted array. Normal strict-prefix and tail append classifications remain intact.

This changes the offline bili acp-cache diff classifier and its reports. It does not change wire bytes, provider requests, billing, or the separate live-panel threshold policy.

Verification

  • 23 new tests: 14 failures / 9 passing controls on unchanged master.
  • All three conversation keys (messages, input, contents) reproduce whole-message prepend/middle insertion and pre-array model growth.
  • Controls preserve single-element growth with a fixed trailer, Unicode content, a following unchanged message, empty/nonempty tail appends, escaped keys, separator whitespace, strict prefixes and actual substitutions.
  • Four real dump-file tests exercise public runDiff: proxy insertion, client insertion, client rewriting normalized by the proxy, and a field-level model change. They assert classification, attribution, the correct message index, and the field-level note.
  • 71/71 related tests pass, including the original [Bug] Two stable false positives in BODY STABILITY / acp-cache diff: dump request-header preamble folded into the byte comparison, and the auto-review giant single user message makes a pure append look like a mid-history insert | [Bug] BODY STABILITY /… #3476 in-unit growth suite, root-array lookup, Unicode offsets and the existing cache-diff report/usage cases.
  • Full npm run typecheck, npm run build, and git diff --check pass.
  • Independent read-only review checked the final production logic and all cases, including separator whitespace and the field-level report boundary; no blocker. The reviewer did not rerun tests.
node --import ./tests/preload-test-env.mjs --import ./tests/preload-storage-baseline.mjs --import tsx --test --test-concurrency=1 tests/acp-cache-diff-insertion-boundaries.test.ts tests/acp-cache-diff.test.ts tests/acp-cache-diff-root-array.test.ts tests/acp-cache-diff-unicode.test.ts tests/issue3476-mid-insertion-append.test.ts

Validation used Node 24.22.1, TZ=UTC and an isolated temporary directory. No runtime dependency or configuration surface is added.

History check

Checked all Afloat16 issue descriptions and actual related PR patches. A fresh read of #6496 changes UTF-8 offsets only. #6691, already on this base, fixes discovery of the root array; the new regressions still fail on that corrected locator. The complete #3479 patch establishes the in-unit exception but lacks these count/span checks. #3027 concerns live-ledger producer attribution, not this offline append decision. Focused all-state insertion/semantic-append searches found no existing equivalent correction.

Base: master a8bbb8d23108ba9148d09512927041364625d910, bili 0.1.214. Full-suite status is the hosted CI on the exact pushed head.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复。 No other open PR or issue targets the cache-diff insertion-boundary fix. I re-verified the PR's own history check against current master: #6496 (UTF-8 offsets only), #6691 (root-array discovery, already on this base), #3479 (the in-unit exception this PR tightens), #3027 (live-ledger producer attribution, different surface). #6738 is the only open PR touching src/acp-cache-diff.ts insertion classification. Base a8bbb8d231 is current origin/master (merge-base confirmed, not stale).

Review

Verdict: mergeable (可合) — pending the full CI test shards, which are still running (see below).

What I verified (live, on head 9b950cd675)

  • Ran the PR's exact test command: 71/71 pass across the 5 touched suites (acp-cache-diff-insertion-boundaries + the 4 existing cache-diff suites).
  • Ran the new test file on base a8bbb8d231: exactly 14 fail / 9 pass — matches the PR's claim, confirming the 14 are real regression tests, not tautologies.
  • npm run typecheck: clean. npm run build: clean (commit=9b950cd675).
  • CI on the exact pushed head: 5 static gates green (lock-sync, unused-exports, config-surface-guard, test-import-graph, pi-subagents-guard), 0 failed so far, but the full test shards + mysql-contract are still queued/in-progress (14 pending). I have not seen the full suite green — the mergeable verdict rests on my local runs of the touched suites + typecheck/build, not the full CI. The full-suite verdict will come from CI.

Behavior-change audit (existing behaviors the diff touches)

The diff changes four things in src/acp-cache-diff.ts:

  1. isMidInsertionAppend comment corrected — the old comment falsely claimed "this cannot mask a real mid-history break"; the new one states the caller must verify the insertion is inside one existing message. Comment-only, no behavior change.
  2. elementSpans: skip legal JSON whitespace before separators — fixes span collection for pretty-printed arrays (a later element's span was previously mis-measured when separators carried whitespace). Narrow, correct; covered by a separator-whitespace control.
  3. compareWires mid-insert block: enforce the documented structural boundary — accept mid-insert only when (a) both bodies select the same root message array key, (b) element count is unchanged, and (c) one existing element span contains the insertion and grows by exactly the added length. Whole-message insertions and pre-array field growth now fall through to diverge.
  4. Final return: preArray computed from the actual array start — was always false; now true when the divergence is before the message array, restoring the "field-level change" note for pre-array growth (e.g. model-name lengthening). Adds a note only; does not change attribution or category.

Empty-array special case: an initially-empty array growing to one element is now json-tail (countA:0) instead of mid-insert (countA:0). Label change only — rel stays append; disclosed in the PR.

Attribution logic (classifyPair): I traced all three producer dump tests — proxy insertion → inRel append → proxy ✓; client insertion → passthrough pair → client ✓; normalized → outRel append + inRel diverge → "proxy normalized" note ✓. Consistent with the new classifications.

Untouched paths (verified unchanged): strict byte-prefix, json-tail, tail-fields, glue-only gaps, and the #3476 in-unit growth case (still mid-insert). The issue3476-mid-insertion-append.test.ts suite — which pins BOTH the offline compareWires and the live-panel bs classification — still passes in full.

Finding (follow-up, NOT a blocker)

The live ledger (src/cache-ledger.ts:959) carries its OWN copy of isMidInsertionAppend (a binary-search variant, "Mirrors src/acp-cache-diff.ts") used in the panel's bs classification (src/cache-ledger.ts:2015). It has the same byte-shape predicate without the structural check — a whole-message mid-history insertion is classified append instead of mid in the live panel. The PR deliberately scopes this out ("does not change … the separate live-panel threshold policy") and source issue #6736 is about the offline tool, so this is not a blocker. But it is the same mechanism on a separate hot-path, diagnostic-only surface: a live fix would need a cheap structural check (the offline JSON.parse+spans approach is too slow for the per-request hot path). I found no existing issue for it. Recommendation: owner decides whether to extend the structural check to the live ledger (separate work, different cost profile). Per the no-new-issues-for-follow-ups rule I did not open one — parking it here.

Test quality (质量关)

The 23 new tests have teeth: they assert rel, kind, countA/countB, preArray, category, attribution, messageIndex, roleType, and report notes — not just "doesn't throw." The 14-fail/9-pass split on base confirms the regressions are real. The 4 real-dump-file runDiff tests exercise the public API end-to-end. The escaped-key (mess\u0061ges) and separator-whitespace cases cover the non-obvious edges. No tautologies, no mock-only tests.

Diff cleanliness + ⑤b code-cleanliness (external contributor, strict bar)

2 files: src/acp-cache-diff.ts (+30/−12) and the new test file (+116). Every line relates to the fix; no unrelated changes, whitespace churn, or lock-file churn. Naming descriptive, no dead code, no duplication, no magic numbers (the " \t\n\r" set is JSON-legal whitespace), comments explain why, style consistent, no new dependencies, functions single-responsibility, tests row-driven. All pass. High-quality external contribution — no rework needed.

结构化八问

  1. 是什么问题: 离线 bili acp-cache diff 把"前后字节不变、中间插入"一律判成语义追加(pure-append/mid-insert),但整条消息插入历史中间、头部插入、以及消息数组前的字段(如模型名)增长也符合这个字节形状——这些不是"一条已有消息内部增长",报告却丢失了 client/proxy 改写归属。
  2. 什么原因导致的: [PR] fix: classify in-unit growth as a semantic append in body stability (#3476) #3479 引入的 mid-insert 特例,文档描述是"元素数不变 + 单个逻辑单元内增长",但实际条件只检查字节前缀/后缀,没有强制结构边界。根因 = 字节形状谓词缺少结构校验(同根数组、元素数不变、插入落在单个已有元素 span 内)。
  3. 影响范围与严重程度: 仅影响离线诊断工具 bili acp-cache diff 的分类与报告,不碰线上字节、provider 请求、计费。触发条件:整条消息插入/头部插入/数组前字段增长。严重度:诊断误报(把 mid-stream-rewrite 报成 pure-append、丢失归属)——体验瑕疵级,不影响核心功能。
  4. 怎么修复的: 把 [PR] fix: classify in-unit growth as a semantic append in body stability (#3476) #3479 文档承诺的结构边界真正落地——仅当两 body 选中同一根消息数组、元素数不变、且某个已有元素 span 包含插入点并恰好增长 added 长度时才接受 mid-insert;整条消息插入与数组前变更落入 diverge。空数组首条消息按普通 tail append 处理。收集 span 时跳过分隔符前的合法 JSON 空白(修复 pretty 数组)。
  5. 是否有回归问题: 审过的既有行为——strict 前缀、json-tail、tail-fields、glue 容差、[Bug] Two stable false positives in BODY STABILITY / acp-cache diff: dump request-header preamble folded into the byte comparison, and the auto-review giant single user message makes a pure append look like a mid-history insert | [Bug] BODY STABILITY /… #3476 单元内增长(仍 mid-insert)——全部保持不变。唯一对外可见的标签变化:空数组→非空首条消息从 mid-insert(countA:0)变为 json-tail(countA:0),rel 仍为 append(PR 已披露)。无意外回归。
  6. 是否破坏缓存: 不涉及——离线分类器,不碰缓存/重试/游标/去重键。
  7. 是否改了设计方向: 不涉及——无架构/边界/数据流转向,只是把已有文档承诺落地。
  8. 是否引入了新的配置: 不涉及——无新 env/config/默认值。

判定: 可合(mergeable)。 外部贡献质量高,回归测试有牙,行为变更已披露,无意外回归。唯一跟进项(非阻塞):live ledger(src/cache-ledger.ts)有同样的字节形状谓词缺结构校验,面板 bs 会把整条消息插入报成 append 而非 mid——建议 owner 决定是否把结构校验扩展到热路径(独立工作,成本剖面不同)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache-diff misclassifies whole-message history insertions as semantic appends

2 participants