Skip to content

[PR] 0.1.214 Flush to disk twice before each forwarding round: skip the second no-op ingest when state is unchanged (#6780) | 0.1.214 每轮转发前落盘两次:状态没变时跳过第二次的空转 ingest(#6780) - #6791

Merged
ranxianglei merged 2 commits into
masterfrom
fix/6780-skip-duplicate-pre-forward-ingest
Nov 15, 2026

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

一句话

修 #6780:0.1.214 起每轮请求在转发模型前会同步落盘两次,第二次是空转(状态一个字没变)却照付全部成本(三表全量读 + 31MB 记录重建 + 一次事务提交,慢盘上 ~1.3s)。本 PR 让第二次在"确认状态没变"时直接跳过重活,只重放落盘协议。

背景(为什么 0.1.214 变慢)

两个 0.1.214 改动叠加,把"本来在响应发出后才落盘"的重事务搬进了转发前的关键路径,且一轮跑两次:

  1. 8849b66b(durability-before-render,修 Compression fails in long sessions while new sessions work fine | 压缩失败.新会话反而没事 #3397 崩溃丢行):prepare 阶段在 forward 前同步调 ingestRowsNow() —— 这个保留,崩溃安全是真的;
  2. 5a80e2b8(pure-SQLite default):scheduleSave() 的 writer-off 分支改为 queueMicrotask 立即执行(不再 500ms debounce 合并)→ 同一 prepare 里 ingestRowsNow 之后紧跟的 markDirty() 触发第二次完整 buildRecordForSave + delta-ingest。第二次 watermark 已被第一次推进,是纯空 delta,却付全部固定成本。0.1.214 moved the two slow-ingest calls per round ahead of forward POST, increasing TTFT from 2.3s to 7.7s | 0.1.214 把每轮两次 slow-ingest 挪进了 forward POST 之前,首字从 2.3s 涨到 7.7s #6780 场景 pre-forward 两次共 ~3.2s,local= 2315ms → 7708ms。

改动(仅 src/persist.ts,逻辑 ~40 行)

  • ingestRowsNow() 成功落盘当前状态后,记一份"臂"(arm):revision epoch + lastMessages 数组引用 + metadata 指纹;
  • writer-off 合并保存在微任务里运行期对照活状态校验:epoch 恰好只被 prepare 自己那次 markDirty +1、还是同一个 lastMessages 数组(snapshotMessages 换引用但不 bump epoch)、无新的 decomposition payload、无 CCR re-key、无 pending ledger align、metadata 字节未动 → 全部满足才跳过 guardedBuild + shadow.record(重活),后面的 settle 协议(noteMetaFp / noteDurable / lastSaveLanded / drainFlushWaiters 等)原样执行;
  • 任何一项失配 → 走原全量路径。臂只是提示,永不承诺——失败方向永远是"多付一次旧成本",不是"少写一次数据"。

顺手记录的坑(只加注释,不改行为)

探针实测(256KB 未变 blob + 行内其他列正常变化):WAL 321KB、主库 +262144——值没变的溢出列在整行重建时照样被拷到新页,CASE WHEN 值相同 skip 对溢出列三种情形全部失效,只对 ≤~700B 内联值有效。#6325 的 skip 因此救不了大 blob;真正的字节修复是把 blob 搬出行(表化),已单独立 #6787(写侧)/ #6782(读侧三表全量读)记录,本 PR 不碰。

测试

  • 新增 tests/issue6780-pre-forward-dedup.test.ts:writer-off 模式下复刻 prepare 的两步(ingestRowsNow → markDirty 触发的合并保存),断言第二次不再调用 sidecar record 且 settle 协议照常落地;反例(arm 之后状态变化)断言仍走全量构建。
  • 回归:issue3397-durability-before-render 2/2、storage-unified 18/18、issue3157/multi-instance-persist 在干净 master 上本就失败(与本 PR 无关,已双 worktree 对照验证)。
  • tsc --noEmit 无新增错误(仅 2 个既有无关错误:pi-subagents 模块缺失)。

影响面

关联

…ng changed since the sync durability save (#6780)

0.1.214 runs two full unified-index ingests before the model call on every
turn: the synchronous durability ingest (8849b66/#6326) and the writer-off
coalesced build armed by prepare's own markDirty (5a80e2b removed the
debounce on this lane). The second one is a no-op delta that still pays the
three-table full reads, the whole-record re-serialization and a second
commit round-trip (~1.3s of #6780's pre-forward cost on a 23k-message
session; local=2315ms->7708ms).

ingestRowsNow now arms a per-session proof (revision epoch + lastMessages
array ref + metadata fingerprint) when it has made the current state
durable; the writer-off coalesced build validates it against live state at
run time and, on a full match, skips its heavy rebuild + full delta-ingest
while still running the settle protocol unchanged. Any mismatch (extra
markDirty, re-snapshot, decomposition payload, CCR re-key, pending ledger
align, metadata mutation) falls through to the full build — the arm is a
hint, never a commitment.

Also document in the sessions upsert why a same-value skip does not help
the big JSON columns: an unchanged OVERFLOW blob is copied onto fresh pages
whenever ANY column of the row changes, so moving the blobs out of the row
is the real byte-cost fix (write-side issue #6787).

Tests: new tests/issue6780-pre-forward-dedup.test.ts pins the skip and its
counterfactual (a changed state still takes the full build);
issue3397-durability-before-render and storage-unified suites stay green.
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: fix/6780-skip-duplicate-pre-forward-ingest (274ead0)

Latest build — install this exact version

npm install -g billion-context@0.0.10084-pr.6791.9f92029

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

Recent builds (commit → version)

commit version build
274ead0 billion-context@0.0.10084-pr.6791.9f92029 build
f87c45b billion-context@0.0.10075-pr.6791.3c4c81a build

Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title 0.1.214 每轮转发前落盘两次:状态没变时跳过第二次的空转 ingest(#6780) [PR] 0.1.214 Flush to disk twice before each forwarding round: skip the second no-op ingest when state is unchanged (#6780) | 0.1.214 每轮转发前落盘两次:状态没变时跳过第二次的空转 ingest(#6780) Nov 15, 2026
…d-strict (#6780 review)

Agent review of f87c45b found three latent skip-path holes in the new
arm, all on the 'skip when state unchanged' side (worst case: a change
lands a round late, never a lost record):

- F1 (merge-blocking): a fold-turn ledger align is consumed by
  scheduleSave's entry (consumePendingLedgerAlign) and restamps refs
  AFTER the arm was set — the restamped state then skipped the build
  (gate condition (e) read an already-consumed flag), so the
  alignment/re-keyed CCR entries would miss the coalesced save.
  consumePendingLedgerAlign now deletes the arm.
- F2: durabilityArms had no .delete at all — a stale arm could match a
  later save whose epoch happened to equal armed.epoch + 1. The arm is
  now single-shot: every run() consumes it after making its decision.
- F5: removeSession leaks the arm entry; cleared on removal.

Tests: the dedup suite now pins the full gate matrix — isolated
lastMessages ref-swap, fold/pendingLedgerAlign forcing the full build,
and the stale-arm negative — in addition to the original skip/counter-
factual pair. tsc clean; issue6780 4/4, issue3397 2/2, storage-unified
18/1 skip.

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(agent reviewer 完整跑了一遍 + 本地复跑测试)

结论:无数据丢失路径,可以合并。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 的 proof revision 语义,逐一核过无问题。

review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"这一侧,最坏后果是变更晚一轮落盘,不是丢):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 ref 会被跳过保存,本轮 fold 的账本对齐漏一次,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来没有任何失效机制(只有 set/get,没有 delete),一个陈旧 arm 可以匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目,内存泄漏。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,现在钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(独立 agent 完整跑了一遍,本地复跑测试)

结论:无数据丢失路径,可以合并。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 的 proof revision 语义,逐一核过无问题。

review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"这一侧,最坏后果是变更晚一轮落盘,不是丢数据):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 ref 会被跳过保存,本轮 fold 的账本对齐漏一次,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来没有任何失效机制(只有 set/get,没有 delete),一个陈旧 arm 可以匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不值得扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,现在钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review(agent reviewer 完整跑过 + 本地复跑)

结论:无数据丢失路径。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径;失败方向全部落回原行为,跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常。并发边界(writer-off 微任务在下一个宏任务前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 proof revision 语义,逐一核过无问题。

Review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"一侧,最坏后果是变更晚一轮落盘,不丢数据):

  1. F1(合前必修):fold 回合的 ledger 重编号(restamp)发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 refs 会被跳过本次保存,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来只有 set/get 没有 delete,陈旧 arm 可匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不值得扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(独立 agent 完整跑过 + 本地复跑)

结论:无数据丢失路径,可合并。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 proof revision 语义,逐一核过无问题。

Review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"一侧,最坏后果是变更晚一轮落盘,不是丢):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 refs 会被跳过本次保存,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来只有 set/get 没有 delete,陈旧 arm 可匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不值得扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,现在钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(独立 agent 完整跑过一遍 + 本地复跑)

结论:无数据丢失路径。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 的 proof revision 语义,逐一核过无问题。

Review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"这一侧,最坏后果是变更晚一轮落盘,不是丢数据):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 refs 会被跳过本次保存,本轮 fold 的账本对齐漏一次,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来没有任何失效机制(只有 set/get,没有 delete),一个陈旧 arm 可以匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目,内存泄漏。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,现在钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(独立 agent 完整跑过一遍 + 本地复跑)

结论:无数据丢失路径,可合并。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 的 proof revision 语义,逐一核过无问题。

Review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"这一侧,最坏后果是变更晚一轮落盘,不是丢):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 refs 会被跳过本次保存,本轮 fold 的账本对齐漏一次,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来没有任何失效机制(只有 set/get,没有 delete),一个陈旧 arm 可以匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不值得扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,现在钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(独立 agent 完整跑过一遍 + 本地复跑测试)

结论:无数据丢失路径,可合并。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 的 proof revision 语义,逐一核过无问题。

Review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"这一侧,最坏后果是变更晚一轮落盘,不是丢数据):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 refs 会被跳过本次保存,本轮 fold 的账本对齐漏一次,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来没有任何失效机制(只有 set/get,没有 delete),一个陈旧 arm 可以匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不值得扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,现在钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(独立 agent 完整跑过一遍 + 本地复跑)

结论:无数据丢失路径,可合并。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 proof revision 语义,逐一核过无问题。

Review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"一侧,最坏后果是变更晚一轮落盘,不是丢数据):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 refs 会被跳过本次保存,本轮 fold 的账本对齐漏一次,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来没有任何失效机制(只有 set/get,没有 delete),一个陈旧 arm 可以匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不值得扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(独立 agent 完整跑过一遍 + 本地复跑)

结论:无数据丢失路径,可合并。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 proof revision 语义,逐一核过无问题。

Review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"这一侧,最坏后果是变更晚一轮落盘,不是丢数据):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 refs 会被跳过本次保存,本轮 fold 的账本对齐漏一次,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来没有任何失效机制(只有 set/get,没有 delete),一个陈旧 arm 可以匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不值得扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,现在钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回归 review 结果(独立 agent 完整跑过一遍 + 本地复跑)

结论:无数据丢失路径,可合并。 核心保险:arm 只是"提示",六个门控条件里任何一个失配都走原来的全量路径,失败方向全部落回原行为;跳过只省掉"第二次全量 rebuild + 第二次 delta-ingest",settle 协议(noteDurable / lastSaveLanded / drainFlushWaiters)照常执行。并发边界(writer-off 微任务在下一个宏任务之前排空)、writerOffPending 合并、envelopeFrozen 共用 run()、#4788 proof revision 语义,逐一核过无问题。

Review 抓到 3 个真洞,全部已在 274ead0 修掉(都在"该全量时误跳过"这一侧,最坏后果是变更晚一轮落盘,不是丢数据):

  1. F1(最重要):fold(压缩)回合的 ledger 重编号发生在 scheduleSave 入口、arm 设置之后——门控条件 (e) 读到的是已被消耗的标志,六条全绿 → 重编号后的 refs 会被跳过本次保存,本轮 fold 的账本对齐漏一次,窗口内崩溃则会话余生未对齐(旧数据自洽,degrade-not-corrupt,但确实是洞)。修复:consumePendingLedgerAlign 无条件删 arm。
  2. F2:arm 原来没有任何失效机制(只有 set/get,没有 delete),一个陈旧 arm 可以匹配后来恰好 epoch 等于 armed.epoch+1 的保存(增量 lane 收尾的 scheduleSave 不 bump epoch,就是这个形状)。修复:arm 改单次有效,每次 run() 做完决定就删。
  3. F5(装饰):删除会话不清 arm 表条目,内存泄漏。修复:removeSession 删 arm。

另记录两个不修的(低危、下轮自愈,不扩大门控面):F3 prepare 的 markDirty 之后才写的 3 个统计估算字段不在门控信号面,最坏晚一轮,cache-ledger 下轮自愈;F4 stale-disk converge 路径的 metadata 不同源,只在磁盘更旧的窗口内。

测试:dedup 套件从 1 个扩到 4 个,现在钉住门控全矩阵——原 skip+反例、隔离的 lastMessages 引用替换、fold/pendingLedgerAlign 强制全量、陈旧 arm 负例。本地:tsc 干净,issue6780 4/4,issue3397 2/2,storage-unified 18/1 skip。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复。本 PR 即 #6791,修 #6780(症状);读侧 #6782 / 写侧 #6787(表化瘦身)另行跟进,不在本 PR,不重复。

标签判定(本环境 label API 未实现,未能落标,仅报判定): bug / scope/storage / p2 —— 持久层性能回归,能用但慢盘每轮多付 ~1.3s,非不可用、非缓存泄露。

审核独立性: 我独立读了 PR 两个 commit 的 diff(f87c45b 原始 perf 修复 + 274ead0 review 修复),独立跑了定向测试,没有照抄 issue 或作者结论。

审核过程:

关于 review 修复 commit(274ead0): 我最初审 f87c45b 时独立发现两个问题,274ead0f 把两者都覆盖了,且其中一处读得比我深:

  • 我发现 durabilityArms 没有任何清理(LRU 驱逐/删除后 arm 持有 lastMessages 引用,长驻 daemon 慢泄漏)→ 对应 F5(removeSession 删 arm)+ F2(单发臂,run 后消费,arm 同 tick 消费、活不到驱逐,驱逐泄漏一并覆盖)。
  • 我发现 gate 的 pendingLedgerAlign 检查疑似死信号(consumePendingLedgerAlign 总在 gate 前跑)→ 274ead0 的 F1 把它读成了真 bug:fold-turn 的 ledger align 在 scheduleSave 入口 consume 并 restamp(ref 重编号 + CCR 重 key),发生在 arm 之后;gate 读到的是已消费的 flag,restamp 后的状态会错误跳过 build,导致对齐/重 key 的 CCR 条目漏掉合并保存(违反 Ledger re-stamp (restamp) never re-keys the CCR content store — forked CCR sessions 409 forever, decompress can serve ANOTHER message’s original #4733 同记录不变量)。修复:consumePendingLedgerAlign 删 arm,强制 restamp 轮走全量 build。这个 merge-blocking 的洞我最初只判成「死信号」,274ead0f 的定性更准。

八问:

  1. 是什么问题: 0.1.214 起每轮请求在转发模型前同步落盘两次,第二次状态一字未变却照付全部固定成本(三表全量读 + 31MB 记录重建 + 事务提交,慢盘 ~1.3s,把 local= 从 2315ms 拉到 7708ms)。
  2. 什么原因导致的: 两个 0.1.214 改动叠加——[PR] fix: persist ledger rows before the model call (durability-before-render) #6326 durability-before-render 在 forward 前同步 ingestRowsNow(保留,崩溃安全是真的);[PR] storage: pure-SQLite default — JSON envelopes stop being written unless dual-write is requested (#6479) #6480 pure-SQLite 默认把 writer-off 合并保存从 500ms debounce 改成 queueMicrotask 立即执行,同一 prepare 里 ingestRowsNow 后紧跟的 markDirty 触发第二次完整 buildRecord + delta-ingest,而 watermark 已被第一次推进,是纯空 delta。
  3. 影响范围与严重程度: 只影响 writer-off(envelope 冻结)合并保存路径;debounce 路径(envelope-on)、incremental-lane safety-net、ingestRowsNow 本身都不变。严重程度:性能回归(慢盘每轮多付 ~1.3s),非数据/可用性问题。
  4. 怎么修复的: ingestRowsNow 成功落盘后记一份「臂」(epoch + lastMessages 引用 + metadata 指纹);writer-off 合并保存在微任务里运行期对照活状态校验——epoch 恰好只被 prepare 自己那次 markDirty +1、同一 lastMessages 数组、无新 decomposition payload、无 CCR re-key、无 pending ledger align、metadata 字节未动——全满足才跳过重活,否则走原全量路径。review 修复补三个洞:F1(align restamp 删 arm 强制全量)、F2(单发臂防 stale 误配 + 防驱逐泄漏)、F5(removeSession 删 arm)。
  5. 是否有回归问题: 审过的既有行为——ingestRowsNow 行为不变(仍同步落盘当前状态);崩溃窗口语义与 [PR] fix: persist ledger rows before the model call (durability-before-render) #6326 后一致(跳过的保存其数据已在同 tick 同步落盘);settle 协议(noteMetaFp/noteDurable/lastSaveLanded/drainFlushWaiters)在 skip 与 full 两条路径都原样执行。未发现回归。唯一已知残留:anthropic 车道在 markDirty 之后还改几个 stats 遥测字段(localInputEstimate/lastLocalTextEstimate 等),跳过路径下这些字段在盘上滞后一个 tick,下一轮 ingestRowsNow 自愈——遥测/估算字段非数据,可忽略。
  6. 是否破坏缓存: 不涉及——不改消息身份、不改 prefix cache 字节、不改 cache ref。跳过的是「第二次空转 ingest」,不触碰出站历史渲染。
  7. 是否改了设计方向: 不涉及——不改架构/边界/数据流,只在既有 writer-off 合并保存路径上加一个运行期校验的跳过提示。
  8. 是否引入了新的配置: 不涉及——无新 env/config/默认值。

判定: 可合。两个 commit 合起来是一个闭环(原始 perf 修复 + 三个洞的 review 修复)。定向测试本地全绿,CI 0 failed(12 pending 待跑完,full-suite 以 CI 为准)。读侧 #6782 / 写侧 #6787 另行跟进。

@ranxianglei
ranxianglei merged commit 1ee510c into master Nov 15, 2026
50 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1202-master.1ee510c (rolling master tag).

npm install -g billion-context@0.0.1202-master.1ee510c  # 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.

ranxianglei pushed a commit that referenced this pull request Nov 15, 2026
The mechanism this PR implemented (skip the redundant writer-off
guardedBuild after ingestRowsNow already covered the tick's state)
landed on master via #6791 (the #6780 fix) with a stricter gate
(durabilityArms: epoch+1, tail ref, no decompositionLog, no
contentStoreDirty, no pendingLedgerAlign, metadata bytes unchanged,
single-shot arm). The conflict rebase resolved src/persist.ts to the
master version; the PR's weaker ingestedRevision/ingestedTailRef gate
would have skipped builds master's gate deliberately runs, so it was
dropped. Only the behavioral regression tests remain — they assert
shadow.stats.ingests and pass against the master implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant