Skip to content

fix: persist derivedFromSessionId immediately at lineage establishment (#2986) - #2987

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-13_lineage-persist-window
Oct 15, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-13_lineage-persist-window

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2986

问题

kind 子会话的血缘链接 metadata.derivedFromSessionId 只在内存里建立后走去震颟保存(markDirty → scheduleSave,默认 500ms)落盘。去震颟窗口内非优雅退出(SIGKILL/崩溃/OOM/断电)会让链接在写盘前丢失 —— 重启后盘上无父 ID,两个继承门都打不开、#2887 的 boot reconcile 也无从起作,子会话永久孤儿(共享前缀整段重发、超窗请求 503)。

修复

新增 persistLineageLinkNow(session):在两个血缘建立点(resume-inheritance #1486 与显式 derived link #1333)紧跟 markDirty 之后触发一次 fire-and-forget 的立即 writeNow。把丢失窗口从去震颟间隔(默认 500ms)压到 sub-ms tick。

用异步而非 flushSync:该路径在模型请求热路径上,不能阻塞响应或冻结事件循环(flushSync 靠 Atomics.wait 重试最多 ~3s);写失败会记 warn 日志,去震颟保存在下次变更时重试兜底。

行为变更(披露)

  • 旧:血缘链接仅在去震颟节奏落盘(默认 500ms 窗口)。
  • 新:首次建立血缘时额外触发一次立即会话记录写入。
  • 频率极低(每个 kind 子会话一次)、非阻塞、成本可忽略。记录内容不变(同一 guardedBuild),仅提前时机。

残留(诚实说明)

测试

tests/issue1333-derived-inheritance.test.ts 新增回归测试:debounceMs=60_000 下,derivedFromSessionId 必须在 3s 轮询窗口内出现在盘上(仅去震颟会使其缺失)。已验证在未修复代码上失败(not ok)、修复后通过。typecheck 全绿;lineage 套件 9/9 通过。CI 跑全局回归。

config surface

不涉及 —— 未新增/改名/改语义任何配置字段、env 或 CLI flag。

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-13_lineage-persist-window (1d73941)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3645-pr.2987.00436db

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-pr2987.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 #2987 审查结论 —— 已对当前 master(7ad3c1f6e)完成 rebased-head 验证。

结构化八问

  1. 是什么问题:kind 子会话的血缘链接 metadata.derivedFromSessionId 在内存建立后只走去震颟保存(默认 500ms 窗口)落盘;窗口内非优雅退出(SIGKILL/崩溃/OOM/断电)链接永久丢失——重启后两个继承门都打不开、fix(sessions): reconcile forked-session fold lineage from disk at boot (#2807) #2887 boot reconcile 无从起作,子会话永久孤儿(共享前缀整段重发、超窗请求 503)。
  2. 什么原因导致的:根本原因是「丢失即永久孤儿」的关键字段沿用了普通可变状态的同一低优先级去震颟持久化路径——持久化机制不区分字段关键性,丢失窗口 = 去震颟间隔。「重启后缺链接」只是表面现象。
  3. 影响范围与严重程度:仅 kind 子会话(pi RLM child / omp fork / opencode subagent / claude --resume 继承),且需父链接建立后的 ~500ms 窗口内恰好非优雅退出才触发——触发概率低,但后果严重且不可自愈(孤儿化后只能人工介入)。
  4. 怎么修复的:src/server/handle.ts 新增 persistLineageLinkNow(session)(fire-and-forget getStore().writeNow + 失败 warn),在两个血缘建立点紧跟 markDirty 触发,把丢失窗口从去震颟间隔压到 sub-ms tick。选异步而非 flushSync 的理由经核实成立:sync flush 路径靠 Atomics.wait 退避重试(kernel/src/persist/store.ts:286-319),会冻结事件循环,而该路径在模型请求热路径上。完整性核查:全 src grep 证实这两处是 derivedFromSessionId 仅有的赋值点(第三处引用是 src/decompress-shared.ts:493 的只读父链遍历)——修复覆盖全部路径,无遗漏点。
  5. 是否有回归问题:无。逐项清点触碰路径的既有行为:Claude 上 resume 导致消息错乱 #1486 resume-inheritance 与 [pi plugin] Prime RLM inline 派生子会话在代理模式下零继承:搜不到也解压不了父会话压缩块(billion-context-pi#531 同场景) #1333 derived-link 两块的门控条件、adoption 流程、日志、miss-retry 语义全部不变;wire/消息形状不动;持久化格式不变(同一 guardedBuild/buildRecord,内容相同仅时机提前);写失败原本就是 best-effort(去震颟兜底重试),无 fail-fast 变吞错倒置;多实例同状态目录的多写者覆写仲裁缺口(无单实例守卫:多个 bili 代理进程并存共写 sessions 目录,伴随孤儿 ref、有注册表无 transcript、计数自相矛盾的污染簇 #394 场景③)为既有、不变,本 PR 仅收窄其窗口(PR 已诚实披露,属 无单实例守卫:多个 bili 代理进程并存共写 sessions 目录,伴随孤儿 ref、有注册表无 transcript、计数自相矛盾的污染簇 #394 跟踪面,不再另立项)。唯一行为变更=每个 kind 子会话建立时 +1 次立即写,PR body 已按旧值→新值披露。
  6. 是否破坏缓存:不涉及——未触碰任何缓存/重试/增量游标/去重键失效策略。
  7. 是否改了设计方向:不涉及——留在既有持久化层内,仅加一个立即写调用点,无架构/边界/数据流转向。
  8. 是否引入新配置:不涉及——无新 config 字段/env/CLI flag(PR body「config surface 不涉及」属实)。

验证证据(均在真实树上执行,非纸面)

  • rebase 验证:分支 base 为 c60c2d1,master 此后前进 217 commits,其中仅 2 个碰 handle.ts(ddf366b4b、6bb976ccf,均为 persona-guard 域,与本 PR hunk 不同区域);本地 rebase 到当前 master 零冲突,rebased head 上重跑验证通过。
  • 定向回归:lineage 套件在 rebased head 9/9 通过(新增测试 330ms 完成)。
  • 测试有效性 A/B:把 handle.ts 换回未修复的 master 版本 → 新测试 not ok 1(复现原 bug);恢复修复 → 通过。测试用 debounceMs=60_000,3s 轮询窗口内只有立即写能让字段上盘——断言有牙、非恒真,且确定性成立(helper server,无端口假设)。
  • CI(PR head adce1d1a7):32 项检查 31 项 success,仅剩 test (windows-latest, 22) 一个 job 仍在跑;e2e / hermetic registry / kernel-guard / static-gates 等全绿。

判定

可合。 分支落后 master 217 commits 但文本零冲突(clean rebase 已实证)、GitHub 显示 mergeable=True。按 [PR] 处理纪律我未推送任何东西;若你希望合并前把分支刷新到当前 master,说一声我就推 rebase;否则直接合(GitHub 生成 merge commit,无冲突风险)亦可。

… establishment (#2986)

The kind-child lineage link (metadata.derivedFromSessionId) was set in memory then persisted only on the debounced save cadence. A non-graceful exit inside that window dropped the link before it reached disk, permanently orphaning the child after restart (shared prefix re-sent whole, over-window requests 503).

Add persistLineageLinkNow(): a fire-and-forget immediate writeNow fired right after markDirty at both establishment sites (resume-inheritance #1486 and the explicit derived link #1333). Shrinks the loss window from the debounce interval (default 500ms) to a sub-ms tick. Async on purpose: this runs on the model-request hot path, so it must not stall the response or freeze the loop (unlike flushSync Atomics.wait retry); a failed write logs and the debounced save retries.

Behavior change: one extra immediate session-record write now fires once per child session at first lineage establishment (previously only the debounced cadence). Low frequency, non-blocking, negligible cost.

Regression test pins it: with a 60s debounce, derivedFromSessionId must appear on disk within a 3s poll window (debounce-only leaves it absent). Verified to fail on the unfixed code.
@ranxianglei
ranxianglei force-pushed the 2026-10-13_lineage-persist-window branch from adce1d1 to 1d73941 Compare October 14, 2026 13:12
@ranxianglei
ranxianglei merged commit a2a3b0e into master Oct 15, 2026
35 of 41 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.466-master.a2a3b0e (rolling master tag).

npm install -g billion-context@0.0.466-master.a2a3b0e  # 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

Development

Successfully merging this pull request may close these issues.

[Bug] 血缘元数据落盘窗口:derivedFromSessionId 可能未持久化(非优雅退出/多实例覆写),kind 子会话永久孤儿

1 participant