Skip to content

[PR] fix: classify in-unit growth as a semantic append in body stability (#3476) - #3479

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-17_acp-cache-mid-insertion-append
Oct 21, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-17_acp-cache-mid-insertion-append

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #3476

What & why

The BODY STABILITY panel (/acp-cache, src/cache-ledger.ts) and the offline bili acp-cache diff tool (src/acp-cache-diff.ts) both defined an "append" purely by the FIRST diverging byte offset / LCP. A layout that grows inside a single logical unit — auto-review's giant single user message whose FILTERED_HISTORY gains entries while a fixed PENDING_ACTION block stays pinned 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 / mid-stream-rewrite. That is the 机制2 false positive from the issue: the panel's 51/51-mid sidecar rows and the diff tool's 6 main-session mid-stream-rewrite are the same blind spot at two entry points.

机制1 (the dump HTTP request-header preamble folded into the byte comparison) was investigated and found not to be the cause. Headers are stripped before comparison everywhere: the dump writers emit METHOD url\nhdrs\n\nbody and parseDumpFile splits at the first blank line comparing only the body; the panel's noteForwardedBody callers pass the JSON wire-body string only. A repro confirmed a header-only-differing pair still classifies pure-append / tail. The diff tool's six mid-stream-rewrite are all 机制2, none header-related.

Fix

Recognize a semantic append instead of flagging a rewrite when the older payload is fully covered by common prefix + common suffix (its middle residual is empty) while 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. One small mirrored helper isMidInsertionAppend(prev, cur, lcp) is used by both entry points:

  • compareWires returns rel: "append" with a new AppendInfo.kind === "mid-insert" for these pairs.
  • the panel bs ternary maps these to the existing "append" class rather than "mid".

Behavior change (old → new + why)

Pairs where a single logical unit grows in place (stable head, stable trailer, new bytes inserted between them) were previously classified:

  • diff tool: mid-stream-rewrite → now pure-append (outgoing = append, kind = mid-insert)
  • panel: bs = mid → now bs = append (and no longer increments stability.mid)

This is the intended fix — those pairs were always semantically appends; the old classification was the false positive being removed. No byte-exactness, fail-fast, or dedup semantics changed; bs remains display-only (no behavior branches on it). Healthy appends (strict / json-tail / tail-fields) and genuine rewrites are unchanged (existing suites + new guards).

Tests

New tests/issue3476-mid-insertion-append.test.ts (6 tests): pins the growing-container insert → append / mid-insert at both entry points, plus guards that (a) an in-place REWRITE of the same unit still diverges / mid, and (b) healthy array-appends still classify json-tail. Existing tests/acp-cache-diff.test.ts (18) and tests/issue2982-body-stability-tail-class.test.ts (8) pass unchanged on the rebased head; typecheck clean.

Config surface

None — no new/renamed/re-semantics config field. The fix is internal to the classifier.


Out of scope (per issue §8): the session-0b4de63c real host-side mid-insert case is a separate phenomenon and may be split out by the reporter.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-17_acp-cache-mid-insertion-append (62e2dcd)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4963-pr.3479.a77323d

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr3479.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复。 open+closed 全搜:涉及「单元内增长被误判 mid」机制的只有本源 #3476 与本 PR;最近的相邻项 #3372(CRLF 帧容差,保留字节精确比较)、#2982/#2983(tail 类,方向相反)均不重叠。热文件提示:PR #3333 也改 src/cache-ledger.ts,但其 hunk 集中在 handleAcpCache/formatSeam(约 L1913+),与本 PR 两处(helper ~L680、三元 ~L1516)无交集,无文本冲突;后合并者 rebase 即可。PR base 落后 master tip 2 个提交,GitHub mergeable=true,无冲突。

审核结论(先行):方向正确、实现干净、本地验证全过、CI 已全绿;但发现一处超出 PR 披露范围的行为变化(谓词匹配面比「单一单元原地增长」更宽),证据与选项见下,需 owner 拍板后收工。

八问(对本 PR 现状)

  1. 是什么问题:BODY STABILITY 面板与 acp-cache diff 都用「首个分歧字节位置」定义 append;单个逻辑单元内部原地增长(auto-review 巨型 user message:FILTERED_HISTORY 变长、尾部固定块不动)时首分歧落在中段 → 稳定误判 mid / mid-stream-rewrite(机制2 假阳性)。机制1(header 前导混入比较)作者已排除,我独立复核成立:写端/读端都在比较前剥 header,仅 header 不同的 pair 实测仍判 pure-append/tail。
  2. 什么原因导致的:分类器判据表达力缺口——append 只看前缀 LCP,没有「后缀覆盖」维度;任何首分歧不在尾部的增长形态都被归为 rewrite。修法是补上该维度:旧体被公共前缀+公共后缀完全覆盖(中间残差为空)且新体中间多字节 = 语义 append。
  3. 影响范围与严重程度:纯诊断/展示层,无可用性影响。我逐点核过:bs 在全仓无任何行为分支(只被 stability 聚合、报告 bodyClass 字段、文本渲染消费);[Enhancement][cache-ledger] attribute client-side mid-history insertions to a client bucket — seam forensics currently blame bili for host churn (found in #2997) #3023/feat: attribute client-side mid-history rewrites in the cache ledger (#3023) #3027 的 CLIENT MID-HISTORY REWRITE 计费桶走独立的入站消息指纹路径(noteInboundMessages/firstDivergentInboundMsg),不经 compareWires/bs,不受本 PR 影响——issue §6.2 担心的「桶被假阳性淹没」在本修法下不会发生。
  4. 怎么修复的:两入口共用一个 ~8 行 helper isMidInsertionAppend(prev,cur,lcp)(前缀+后缀覆盖判定;数学上等价于「cur = prev 在某处插入一段字节」,真 rewrite 必在短侧留非空残差 → 不可能误吞真断裂,这点我推演+实测确认)。compareWires 新增 kind="mid-insert";面板在 would-be-mid 槽映射到既有 "append" 类。插入位置正确:在 strict/json-tail/tail-fields 之后、最终 diverge 之前,只接住落空 pair。
  5. 是否有回归问题:审过的既有行为:strict/json-tail/tail-fields 健康 append、中段删除、等长原地改写、head 区改写——全部保持原判(本地探针逐项验证 + 既有 26 个测试原样绿)。唯一例外即下述发现:整元素插入与头部前置两种形态也被新分支吸收,超出披露的行为变更清单。
  6. 是否破坏缓存:不涉及——纯离线分类逻辑,无缓存/重试/游标/去重键。
  7. 是否改了设计方向:不涉及,分类器内部细化,无架构转向。
  8. 是否引入新配置:无(PR 声明与实际 diff 一致)。

发现:谓词匹配面超出披露范围(需拍板)

isMidInsertionAppend 判的是「prev 被 prefix+suffix 全覆盖」,这匹配任意单点 gap 插入,不止「单单元内部增长」。PR head 本地探针(两入口各测):

形态 diff 工具(旧→新) 面板 bs(旧→新)
单元内增长,count 不变(auto-review 形状) mid-stream-rewrite → append(mid-insert) ✓ 预期 mid → append ✓ 预期
整元素中段插入,count 2→3 mid-stream-rewrite → append(mid-insert, msgs 2→3) ⚠️ 超披露 mid → append ⚠️ 超披露
头部前置,count 2→3 mid-stream-rewrite → append(mid-insert) ⚠️ 超披露 head → head(lcp<1024 兜住)——两入口对同一 pair 结论不一致
中段删除 diverge → diverge ✓ mid → mid ✓
等长原地改写 diverge → diverge ✓ mid → mid ✓

后果:真实 host 侧 mid-history 插入(正是 #3023/#3027 与 §8 session-0b4de63c 那类现象,dsh host 插入有生产案例)在离线 diff 工具里失去生产者归因——原来 mid-stream-rewrite(client) 能指向 host,现在变成 pure-append(无 usage 数据时)或 prefix-stable-miss(有 usage 数据时,miss 会被记到 provider/TTL 名下,归因方向反了)。面板侧丢一行 stability.mid。计费桶本身不受影响(独立机制,已核)。缓解因素:(a) issue 作者提的判据①(尾部固定块整体平移)本身就没有 count 条件,PR 是照单实现;(b) count 变化时输出带 msgs A→B 标签,人眼可辨;(c) 该形态频率低(§8 一例)。

选项(均为语义取舍,请 owner 拍板):

  • A(我推荐):给新分支加元素数相等门限——diff 工具加 ia && ib && ia.key===ib.key && ia.items.length===ib.items.length;面板复用既有 messagesArrayCloseOffset 解析点顺带取出 count(它本来就 JSON.parse 短侧)。效果:新分支严格收窄到 issue 描述的范围,其余形态全部回到 PR 前判定;对修复本体零风险(新 6 个测试全是 count 不变形状,探针 D 同形状仍命中)。代价:一行条件 + 2 个 guard 测试。影响面:仅上述两种形态的分类回退;回归风险:无(严格收窄)。
  • B:维持现状,在 PR Behavior change 节补一句「整元素插入与头部前置形态同样翻转为 append(输出带 msgs 标签)」,由 §8 拆出的条目跟踪该形状。影响面:离线工具对 host 插入形状的归因能力下降(低频);回归风险:诊断精度回退,非功能回归。

拍板 A 的话我直接在 PR 分支补 additive commit(含 guard 测试)并跑定向验证。

本地验证(PR head f4a6fb6)

  • typecheck 干净;
  • 定向测试 32/32 绿:tests/issue3476-mid-insertion-append.test.ts(6)+ tests/acp-cache-diff.test.ts(18)+ tests/issue2982-body-stability-tail-class.test.ts(8);
  • 上述五形态探针逐一实测(脚本留存工作区可复现)。

CI(f4a6fb6)

全绿:29 success(含 kernel-guard / pi-subagents-guard / config-surface-guard / version-guard / lane-guard / docs-sync 等全部 guard 道)+ 13 skipped(zero-token e2e 道),0 failure。

判定

代码质量达标(命名达意、注释讲为什么、无死代码、风格与既有 tail-class 注释体例一致)、方向成立、回归清点除发现项外干净、CI 全绿。待办一项:谓词收窄 A/B 拍板;落实后即可合并。

…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.
@ranxianglei
ranxianglei force-pushed the 2026-10-17_acp-cache-mid-insertion-append branch from 64f28ad to 62e2dcd Compare October 21, 2026 13:49
@ranxianglei
ranxianglei merged commit c080a62 into master Oct 21, 2026
44 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.727-master.c080a62 (rolling master tag).

npm install -g billion-context@0.0.727-master.c080a62  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

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

Labels

None yet

Projects

None yet

1 participant