Skip to content

[PR] fix: enforce turn-aligned fold coverage after re-anchor (#4348) - #4353

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-23_4348-turn-aligned-coverage
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-23_4348-turn-aligned-coverage

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

After an identity-churn incident re-anchors fold coverage (re-anchored N/M), a block can end up covering only one half of a tool-call / tool-result pair while the other half is stranded as uncovered debris. The kernel turn-integrity INV2 guard then correctly refuses to fold that orphan half, so the debris is permanently unrecoverable and resent verbatim on every turn (~170K chars across 11 clusters in session 01a11bc7 after the #4323 incident; manual fold attempts all hit Range would split N turns).

Root cause

rewriteBlocks splices claimed ids in place and leaves unmatched old ids as dead refs. It does not re-check the turn-pair invariant that the original fold's turn-split guard guaranteed at creation time — so re-anchoring can produce a mid-turn coverage island that no subsequent fold can repair. The guard is correct and must not be loosened (strict providers reject rebuilt requests missing a call or result); the defect is on the bookkeeping side that feeds it.

Fix (host-side)

New enforceTurnAlignedCoverage in src/fold-reconcile.ts, run after rewriteBlocks for proxy mode / repair mode on every pass: for any tool pair whose exactly one half is covered by an active block, drop the covered half so the pair becomes fully uncovered and foldable again. Conservative — it only removes coverage, never claims churned bytes (honors the exact-match doctrine). Idempotent. Makes already-stranded debris foldable again and prevents future mid-turn islands.

Behavior change (flagged): previously a mid-turn covered half stayed covered and its partner was permanently un-foldable; now the covered half is dropped from coverage so the whole pair folds together. Net effect: transiently re-exposes that half verbatim until the next fold absorbs the complete pair — strictly shrinks the permanent-debris class and adds no new bytes to any request.

Scope / gates

Single module (src/fold-reconcile.ts) + one new test file. No kernel change, no config surface, no wire/message-shape change, no cache-key change, no persistence change. Proxy-mode only — plugin-mode blocks mirror the agent's private compression state and are left untouched (matches the existing zombie-reap gate).

Tests

tests/issue4348-turn-aligned-coverage.test.ts: 8 pure-function pins (drop covered call / drop covered result / aligned pair same-block untouched / both-halves-in-different-blocks untouched / lone-half untouched / keeps unrelated coverage / cleans directMessageIds / non-tool no-op) + 3 wiring tests (proxy heals an existing island on a later pass with no fresh churn / plugin leaves it untouched / warn observes without rewriting). Targeted local on the rebased head: typecheck clean, build ok, 105/105 across the full fold-reconcile family. Full-suite verdict = CI.

Closes #4348

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-23_4348-turn-aligned-coverage (b405f62)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.5166-pr.4353.34b7848

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-pr4353.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)

查重: 不重复 — 本 PR 是 #4348 的修复 PR(正文 Closes #4348),检索 open+closed issue/PR 无同机制的并行工作(re-anchor 后 mid-turn 覆盖孤岛 / 孤儿半对永久不可折叠)。

标签: bug + scope/kernel + p1(缺陷类;src 侧压缩决策面 fold-reconcile;p1 = 核心承诺失灵——被弃半对永久不可折叠、每轮逐字重发,属缓存/上下文失控类,按 owner 修订 2026-10-19 不降 p2。宿主客户端从 issue 无法判定,不打客户端标签。)

开始 review:拉取 PR head、核对代码与 CI 状态,稍后按八问给结论。

Re-anchoring coverage after an identity-churn incident splices claimed
ids in place and leaves unmatched old ids as dead refs, so a block can
end up covering only one half of a tool-call / tool-result pair while
the other half is stranded as debris. The kernel turn-integrity INV2
guard then correctly refuses to fold that orphan half, leaving the debris
permanently unrecoverable and resent verbatim on every turn.

Add enforceTurnAlignedCoverage: after rewriteBlocks, for any tool pair
whose exactly one half is covered by an active block, drop the covered
half so the pair becomes fully uncovered and foldable again. Runs in
proxy mode, repair mode, on every pass — heals already-stranded sessions
without fresh churn and prevents future mid-turn islands. Conservative:
it only removes coverage, never claims churned bytes.

Regression pins: tests/issue4348-turn-aligned-coverage.test.ts (pure
function + proxy/plugin/warn wiring).

Closes #4348
@ranxianglei
ranxianglei force-pushed the 2026-10-23_4348-turn-aligned-coverage branch from 2caca60 to b405f62 Compare October 23, 2026 02:22
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

Review 结论(结构化八问)

1. 是什么问题:身份抖动(churn)触发的 re-anchor 之后,一个 fold 块可能只盖住工具调用/结果对的一半,另一半被甩在覆盖之外;kernel 的 turn-integrity INV2 守卫正确地拒绝任何会拆散该对的折叠(Range would split N turns),于是被甩的那一半永久不可折叠,每轮逐字重发(#4323 事故会话里 ~170K 字符、11 个簇)。

2. 什么原因导致的:rewriteBlocks 按声称结果原地拼接 id、把失配的旧 id 留成死引用,但没有复检建块时由 turn-split 守卫保证的「一对同块」不变量——记账侧能造出 mid-turn 覆盖孤岛。守卫侧(INV2)是对的、不能松(严格 provider 会拒收缺半边重建请求),缺陷在喂给守卫的记账侧。层次判断成立:修的是生产者(reconcile 记账),不是症状拦截。

3. 影响范围与严重程度:proxy 模式 + 经历过身份抖动 re-anchor 的会话(p1):受影响半对永久不可折叠、每轮逐字重发,属上下文只增不减的失控类(owner 修订 2026-10-19 不降 p2)。触发条件明确(需先有 churn re-anchor),非全量用户。

4. 怎么修复的:新增 enforceTurnAlignedCoverage(src/fold-reconcile.ts),在每次 repair pass 的 rewriteBlocks 之后跑:凡「恰好一半被活跃块覆盖」的工具对,丢掉被覆盖的那一半,让整对回到完全未覆盖=可再折叠状态。保守(只删覆盖、不认领 churn 字节,守 exact-match 教义)、幂等;孤岛清空后把该块置 inactive,挡住 syncBlocks/nudge/report 面。仅 proxy 车道(plugin 块镜像 agent 私压状态,不动,与既有 zombie-reap 门一致);warn 模式只观察不改写。

5. 是否有回归问题(逐项审过既有行为):

  • 稳态无孤岛 pass:纯扫描 no-op——对齐对同块不动/两半在不同块不动/孤半不动/非工具 no-op,均有测试钉住 ✅
  • zombie-reap 交互:核对了 kernel/src/sync.ts 复活路径——proxy 模式输入视图是客户端原始重发史,既无覆盖 id 也无已渲染摘要 id,stillPresent 两个析取都不成立,清空的孤岛不会被复活;prune 的 knownSummaryIds(192版本缓存命中率低codex #2733 机制)会把残留摘要副本丢掉,无自持 stale-summary 泄漏 ✅
  • plugin 门 / warn 门:各有一条负向测试钉住不动 ✅
  • anchor/order 元数据:被丢 id 的陈旧 anchor 条目下一 pass 自然剪掉(anchor 循环迭代新鲜 covered 集)✅
  • kernel INV2 守卫:零改动(diff 只有 1 个 src 文件 + 1 个测试文件)✅
  • 高危模式清点:fail-fast 未变吞错、无阈值/时序/输出格式漂移;唯一语义变化即第 6 条披露的行为变更本身。
  • 一处描述瑕疵(非代码缺陷):PR 正文「adds no new bytes to any request」不严格——被丢的那半在下次折叠前会以逐字形式回到线上(这正是披露的临时重新暴露),准确说法是「不引入客户端自身逐字历史之外的新字节」。留档,不改代码。

6. 是否破坏缓存:不破坏缓存键/ref/去重策略(零改动)。临时重新暴露会改变一次出站字节(到整对被下一次折叠吸收为止)——这是修复意图本身,已在 PR 正文按旧值→新值+原因披露,且只缩小永久残骸类。

7. 是否改了设计方向:否。host 侧记账修复,kernel 守卫保持严格,架构/数据流未转向。

8. 是否引入了新的配置:无。

验证证据(本地定向,非全量)

判定

可合(等 rebase 后的新矩阵跑绿)。按 §7.4 属人工合并档:压缩决策面的覆盖语义变化 + 已披露行为变更 = 产品决策,不走 auto-merge。测试有效性合格(正/负双向断言齐备,符合 #3617 门规);唯一缺口是无真实 #4348 会话级 e2e A/B,但机制级单测复现充分,可接受。

@ranxianglei

Copy link
Copy Markdown
Owner Author

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

Closing this — superseded by #4354.

My root-cause read here was off. I attributed the incident debris to a re-anchor bookkeeping edge (a block covering exactly one half of a call/result pair, so the turn-split guard pins the orphan forever). @ranxianglei measured the actual session and the real cause is different: successful compress receipts were permanently exempt from folding (ALWAYS_PROTECTED_TOOLS, no age limit), so long sessions accumulate them — the lace is simply what that design leaves behind, and the Range would split N turns fatal was the symptom, not the cause.

This PR fixes the mid-turn-island edge, not the cause, so it does not resolve #4348. On re-examination that edge also looks far less reachable than I assumed: protection/exemption changes are applied atomically per toolCallId (both halves together), which is exactly what prevents a split-pair island from forming naturally. Shipping a fix for an unproven bookkeeping edge in the hot src/fold-reconcile.ts path would be a mask, not a root-cause fix (§7.3).

If a genuine mid-turn coverage island is ever observed with evidence, it merits its own issue with a repro — not a blind defensive fix. Closing per one-issue-one-PR; #4354 is the fix for #4348. Reopen if you want the turn-alignment invariant enforced separately.

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

Labels

bug Something isn't working p1 scope/kernel

Projects

None yet

1 participant