Skip to content

fix: seam detector no longer false-flags no-baseline first bills (#1891) - #1893

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-02_fix-seam-nobaseline-false-positive
Oct 2, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-02_fix-seam-nobaseline-false-positive

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #1891

What & why

The [cache-seam] warnings in #1891's log are FALSE POSITIVES of the #1606 seam detector, not real prefix breaks: all five field events fire on the session's FIRST measurable sample (no ledger baseline), where acp-kernel's decomposeSample(prev=null) forces growth=0 and books the ENTIRE uncached initial bill as ttlRepay — which detectSeam then misreads as an "unexplained mid-history break" (field signature: unexplained == input − cached, exactly). Every event was followed by 85–99% hits.

Two sub-defects in the same seam-diagnostics failure domain:

  1. Detector false-positive on no-baseline openers (src/cache-ledger.ts) — rebook the no-baseline residual ttlRepay → newContent (closure invariant missed === nc+cr+tr preserved), mark the line nb, skip it in detectSeam (also keeps it out of the rewind/provider-side body-pair classifications), and surface initialBills in the CACHE INVALIDATION section so users can verify opener bills are not counted as re-pay.
  2. Missing outbound-body capture on the main send path (src/server.ts) — noteForwardedBody ran only at the non-streaming settle and loop re-fetch; streaming/plugin-pipe lanes (the reporter's /bili/ lane) got "aggregate flag only" with no byte forensics possible. It now runs at the single main chokepoint (fetchWithTransportRetry site; skips GET/HEAD and side requests that never settle usage), and the non-streaming note records the exact final wire bytes (wireBody after compat-role/steering/chain-stamp rewrites) instead of the pre-rewrite prepared body.

Behavior changes (old → new)

  • Ledger accounting: a session's first measurable sample — its uncached input was booked in the ttlRepay bucket; now booked in newContent. In /acp-cache: ttlRepay and invalidation.remaining no longer inflate by every new session's opener bill; newContent grows by exactly that amount. Closure invariant unchanged (residual stays 0). WHY: with no prior bill nothing could have expired — booking it as re-pay was the category error behind 缓存泄露问题 #1891's false warnings.
  • [cache-seam] warn + CACHE SEAM report section: no longer fires on no-baseline openers; genuine mid-history breaks still fire (regression-tested, mutation-verified).
  • Report output: additive — new initialBills block + one extra CACHE INVALIDATION line when present. Ledger persistence stays v:1 with additive optional fields (nb, nbSamples, nbInput), normalized on load for pre-upgrade ledgers.
  • Forensics: streaming lanes now yield SeamEvents with LCP/message-index instead of aggregate-only flags; memory bounded by the existing per-session 512 KB SEAM_BODY_CAP WeakMap. No wire change, no config-surface change.
  • Known divergence: bili's incremental ledger now intentionally diverges from acp-kernel's batch buildCacheReport on no-baseline samples (kernel still books them as ttlRepay); bili never renders the kernel batch path — the parity test documents the exact delta. Kernel-side alignment is a cross-repo follow-up if wanted.

Tests

  • New tests/issue1891-seam-nobaseline.test.ts: unit tests (rebooking, the 25074/1024 field signature, over-suppression guard for real breaks, unknown-sample quarantine, pre-upgrade ledger normalization) + an e2e streaming /bili/ lane test proving the chokepoint capture yields byte-level forensics. Both mutations verified red (rebooking disabled → 3 fails; chokepoint note removed → e2e fail).
  • Updated two tests in tests/cache-ledger.test.ts that pinned the old buggy semantics (cold-start-in-ttl assertion; kernel-batch parity expectations).
  • Targeted suites green: issue1891 + cache-seam-detector + cache-ledger + issue1547 + all seam-family suites (78 tests pass); typecheck clean; build OK. Full regression + codex E2E left to CI (hot-file trigger covers this diff).

The #1606 seam detector flagged the FIRST measurable sample of a session as a suspected mid-history prefix break: with no previous ledger baseline, decomposeSample(prev=null) forces growth=0 and books the entire uncached initial bill as ttlRepay, which detectSeam misreads as an unexplained residual (all five field events in #1891 had unexplained == input - cached exactly). Rebook the no-baseline residual as new content (closure invariant missed === nc+cr+tr preserved), mark the line nb and skip it in detectSeam, surface initialBills in the /acp-cache invalidation section. Second half of the same failure domain: the main send path never captured outbound bodies, so streaming /bili/ lanes got 'aggregate flag only' with no byte forensics - noteForwardedBody now runs at the single main chokepoint (fetchWithTransportRetry site), and the non-streaming note records the exact final wire bytes.
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-02_fix-seam-nobaseline-false-positive (6c6eec7)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1888-pr.1893.e780e46

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-pr1893.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

[bot] 🏷 PR #1893 review 结论(已核对代码、独立复现 mutation、验证合并到最新 master、CI 全绿)

结构化七问:

  1. 是什么问题:缓存泄露问题 #1891 的 [cache-seam] 警告是误报——检测器把「会话第一笔可计量账单」(无 ledger 基线)错认成 mid-history prefix break;同时 streaming /bili/ 等 lane 没有任何 outbound body capture,真告警时也无字节级取证。
  2. 什么原因导致的:无基线时 acp-kernel decomposeSample(prev=null) 强制 growth=0,把整笔 uncached input 记入 ttlRepay 桶;detectSeam 看到「大 ttlRepay + 无归因」即判 seam。现场签名完全吻合(unexplained == input − cached;五起事件后全部 85–99% 命中)。后半:body capture 原本只挂在 non-streaming settle + loop re-fetch,主发送路径从未记录。
  3. 怎么修复的:同一失败域两处——① 无基线残差 ttlRepay→newContent 重记账(闭包不变式 missed===nc+cr+tr 保持),行标 nb,detectSeam 跳过,并在 /acp-cache CACHE INVALIDATION 段新增 initialBills 让用户可自证 opener 未被计为 re-pay;② noteForwardedBody 上移到单一主 chokepoint(fetchWithTransportRetry 前,跳 GET/HEAD 与 side 请求),non-streaming settle 改记最终 wire 字节 wireBody(含 compat-role/steering/chain-stamp 改写)而非改写前的 prepared.body。
  4. 是否有回归问题:逐项审过既有行为——
    • 闭包不变式 / residual:保持(纯桶内移动,residual=0 有测试钉住);
    • 真实 mid-history break:照常触发(over-suppression guard 单测 + e2e 真 break,msgIndex 定位精确)——防「过度掩盖」成立;
    • unk 隔离、fold/switch/restart 归因、rewind/provider-side 分类:路径未变(nb 行结构上不可能带 sw/pw/uw/rs——均要求 prevLine 或既有历史,与 nb 互斥);
    • 已披露的小语义漂移:nb 行不再计入 abortCorrelated(旧代码计)。此类 miss 全属 initial content,归因 abort 本身误导;abortAt 保留,30s 窗口内下一样本仍可捕获——可接受;
    • kernel batch parity:有意分歧(kernel 仍记 ttlRepay),parity 测试钉住精确 delta(±1000),bili 从不渲染 kernel batch 路径——PR body 已披露;
    • 重启场景:ledger 随 metadata 持久化(persist.ts:682/760),正常重启走既有 rs/restartDrops 路径,不受影响;仅 ledger 丢失时 opener 落入 initialBills,方向与修复一致(无基线 ⇒ 无法区分初始内容与重启重付)。
      结论:无意外回归。
  5. 是否破坏缓存:不涉及(prefix cache、prefix affinity、增量游标、去重键均未触碰)。持久化保持 v:1,仅加可选字段 nb/nbSamples/nbInput,load 时归一化(有专门 no-NaN 测试)。
  6. 是否改了设计方向:无。无 wire 变更、无 config surface 变更;内存受既有 SEAM_BODY_CAP=512KB/session WeakMap 约束(未变)。
  7. 是否引入了新的配置:无。

测试有效性:断言有牙,我独立复现了两个 mutation——禁用 rebooking → 恰好 3 红(opener / 字段签名 / e2e);删 chokepoint note → 恰好 1 红(streaming e2e)。字段签名测试逐字编码 #1891 现场数据(25074/1024);over-suppression guard 防过度掩盖。本地定向套件 57/57 绿;typecheck + build 绿;对最新 master(6cab41b)做验证性 merge:无冲突、typecheck 绿、定向 45/45 绿。全量回归由 CI 把关,已 14/14 全绿(含 ubuntu/windows 22/24 矩阵、e2e、hermetic registry、real-client lanes)。

diff 整洁度:4 文件全在主题内(2 src + 2 tests),单一线性 commit,无 lockfile/生成物/version bump。通过。

⚠️ 合并顺序风险(需 owner 拍板):open PR #1856(cache-ledger-attribution)重写 src/cache-ledger.ts 同一 invalidation-bucket 区域(+116/−44)+ tests/cache-ledger.test.ts(+105)+ src/server.ts(+16)。GitHub 报 mergeable=true,但两者无论谁先合,另一个都需要语义调和(#1856 的新断言可能钉的是旧桶数字)。建议先合本 PR(更小、已绿、修活跃字段 bug),再 rebase #1856。另:#1449/#1446(server.ts 拆分重构)长期 open 且同文件,区域与本 PR 插入点不同,顺序敏感但风险低。

已知限制(已单独跟踪,非阻塞):in-request 代理自发的重发 hop(compat-role ladder、overflow refold、fake-completion retry)成功后不重记 body,该样本的取证配对用首次尝试字节。仅诊断层面(字节指针仍指向真实分歧点;严格优于修复前的 prepared.body)。已立项:#1894。

判定:可合并(human merge)。方向正确、根因修复(非止血)、闭包不变式保持、mutation 双向独立复现、CI 全绿;唯一需要 owner 做的是确认与 #1856 的合并顺序。

@ranxianglei
ranxianglei merged commit f6b2798 into master Oct 2, 2026
14 checks passed
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.

缓存泄露问题

1 participant