Skip to content

[PR] fix: fork delayed completion — child session where the first request races ahead and lands is no longer permanently latched by CHILD_CONFLICT (#3025) | [PR] fix: fork 迟补全——首条请求抢跑落地的子会话不再被 CHILD_CONFLICT 永久闩死 (#3025) - #3039

Open
ranxianglei wants to merge 3 commits into
masterfrom
2026-10-13_fork-late-completion

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #3025

是什么

服务端迟补全(owner 已拍板的方向一):子会话的首条 stamped 请求若在 adoption 之前落地,该请求处理中 recordPluginSession 就把 child 注册进 conversations 表——此后任何 fork POST 都撞 409 CHILD_CONFLICT,继承永久丢失(零折叠起步)。本 PR 让服务端在「child 存在但从未分叉且仍是纯前缀」时把继承就地补全到已存在的会话上,而不是硬冲突。

基于 #3024 分支当前 tip 构建(480c62cf2+9d44414ac);#3024 合并后 rebase 即自动收敛。复用 #3024 的 forkSourceMessages() 源视图选择。

机制

  • replay() 对「child 存在但无匹配 receipt」不再就地结算 409,改为下放到锁内裁决;锁前用廉价 lateCandidate() 探测(可解析 + 无 receipt + 从未物化 + 零自折叠 block)放行注册门。资格在窗口内只会退化不会改善,锁前探测是 sound 的;权威判定在两把锁内重跑。
  • 父锁内、snapshot/revision/prefix 校验通过后:严格资格 = child 现有 raw view 与请求前缀全长逐条 identity-hash 相等(多一个自己的 turn 或自己折过一次都失格)+ 逐 ref 一致性断言(byRaw[rawId] === ref ?? BLOCKED_REF——编号分歧 = 拒绝而非写坏,防 kernel 版本编号漂移)。
  • 合格 → 共享 completeForkTarget(..., late=true) 把 blocks/refs/计数器/raw baseline/content store/receipt 就地物化到活会话;不合格 → 与现状完全相同的 409 CHILD_CONFLICT。
  • late 子会话保留自己的 rules/nudge/stats/metadata/transient 记账(它已经跑过真实请求,覆写会回退实测值);fresh-fork 的冷启动 seeding(!late 分支)与原代码字节级一致。
  • 锁序:parent → child 双锁是全代理唯一的两锁路径,无反向获取序,无死锁。
  • late 路径用 eager flushSync(child) 替代 publishForkSession(其 fresh-session 守卫会拒绝已在池中的会话),receipt 与继承一次写入落地([plugin] fork snapshot maintenance is O(full history) every request + snapshot persisted into every session file #2077 连续性:崩溃窗口不留「有 block 无 receipt」的再闩死残态)。

行为变更披露(owner policy 2026-09-22)

为什么不是另两个方向(issue §建议方向)

测试

tests/plugin-fork.test.ts 新增 5 个回归测试,驱动真实竞态形态(child 先把继承历史作为真实模型请求发出去,fork POST 随后到达):

  1. repro:latched child → 201 lateAdopted,继承父折叠(下一条请求转发 summary 而非原文)、幂等重放 200 replayed、child snapshot 服务前缀;
  2. 负例:child 长出了自己的 turn → 409,无 receipt 写入;
  3. 负例:child 自行折叠过历史 → 409,无 receipt;
  4. 负例:ref 编号漂移 → 409(拒绝而非写坏);
  5. 并发:fork POST 与 in-flight child 请求竞争 child 锁 → 串行等待后 201(无死锁)。

已验证 pre-fix 代码上 red(repro + 并发两个正例失败于 409/503)、post-fix green;plugin-fork / dsh-fork-adopt / pi-omp-fork-adopt 三套件 103 全绿。全量回归以远程 CI 本 head 为准。

§7.4 说明:向活会话 seed message refs/blocks 且新增响应字段 → 人审域,不走 auto-merge。

ework-agent added 3 commits October 13, 2026 19:40
…VAILABLE (#3011)

Client (src/agent/fork-adopt.ts): classify degraded outcomes by structured
verdict instead of a reason-string regex. The regex only matched 'error' /
'http 5xx', so the incident's 'snapshot http 409 SNAPSHOT_UNAVAILABLE' was
treated as terminal after ONE attempt and the child's inheritance was
permanently abandoned in-process. Each result now carries an explicit
retryable flag: snapshot GET retries on network errors, HTTP >=500, and
409 code SNAPSHOT_UNAVAILABLE (a residency gap that self-heals on the
parent's next request); 404 PARENT_NOT_FOUND and other 4xx stay terminal.
Fork POST classification unchanged (network/5xx retryable). Give-up now
logs an explicit 'permanently disabled' line distinct from the transient
'will retry' line.

Server (src/plugin.ts): new forkSourceMessages() selects the raw source
view. The retained copy (pluginSnapshot) stays primary; when it is missing
(the residency gap: LRU eviction or host restart losing an unpersisted
copy) the recent raw view session.lastMessages is served instead — it is
persisted unconditionally and re-stamped by every parent request. A
head-divergent tail wins over a frozen pre-compaction retained copy
(zero-overlap guard case), so the served identities survive native
compaction rebases. Folded tails never serve. The retention-cap refusal
(#2433) stays first. Fork materialization uses the same selection, so a
zero-branchpoint child keeps its legitimate empty snapshot.

Server (src/session.ts): resetSessionCompression clears the stale
publicSnapshot* metadata so a rebased parent cannot fail the list-level
stored-refs check against the old view.
…s gate)

Review fix for #3011/#3024: forkSourceMessages is only referenced inside
src/plugin.ts (forkSnapshot + handlePluginFork); no test or module imports
it, so the leading export tripped scripts/check-unused-exports.mjs
(INTERNAL_ONLY). Drop the keyword per the gate's own remediation — no
behavior change.
)

A child whose first stamped request landed before adoption gets registered
server-side during that request (recordPluginSession runs before forwarding),
after which every fork POST met 409 CHILD_CONFLICT forever — permanent loss
of inheritance (zero-fold start).

Server-side late completion: when POST /__bili/plugin/fork finds an existing
child without a receipt, strict eligibility under both locks (no receipt, no
materialization, zero self-blocks, full-length identity-hash equality with
the requested prefix, per-ref numbering consistency) now completes the
inheritance IN PLACE on the live session instead of hard-conflicting:
- shared completeForkTarget() for fresh and late paths; late children keep
  their own rules/nudge/stats/metadata (they already ran a real request);
- parent -> child lock order is the only two-lock path (no deadlock);
- late path flushes eagerly (publishForkSession's fresh-session guard
  rejects resident sessions) so receipt + inheritance land in one write;
- ineligible children (own turns, self-fold, ref drift) still 409 as before.

Behavior change: a strict subset of former 409 CHILD_CONFLICT outcomes now
returns 201 with additive field lateAdopted: true (race-lost victims only);
all other 409s unchanged (same codes/text). PLUGIN.md §8 updated.

Tests: 5 new regression tests driving the real race shape (verified red on
pre-fix code): repro+idempotency+live-inheritance, own-turns/self-fold/ref-
drift negatives, child-lock concurrency.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-13_fork-late-completion (5e35bde)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3434-pr.3039.11c7144

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-pr3039.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 #3039 审查完毕。结论先行:代码成立,CI 本 head 全绿(34/34),可合;建议先合 #3024。按八问体例如下。

审查过程与证据

  • 拉取分支通读新增提交(5e35bde04:src/plugin.ts +287/-112、tests/plugin-fork.test.ts +102、PLUGIN.md 一段)的完整 diff;窗口内另两个提交是 [PR] fix: fork adoption no longer gives up permanently on 409 SNAPSHOT_UNAVAILABLE (#3011) #3024 自身的(480c62cf2+9d44414ac),已在该 PR 审过,不在此重复。
  • 方向关:[Bug] Fork adoption server-side completion window: After the child session's first degraded request is committed, POST fork is permanently latched by 409 CHILD_CONFLICT, preventing retroactive adoption | [Bug] fork adoption 服务端完成窗口:子会话首条降级请求落地后,POST fork… #3025 方向一(服务端迟补全)是你 inline 拍板的;实现与批准设计逐项一致——严格资格三条件 + 全长逐条 identity + 逐 ref 断言、共享物化函数、parent→child 锁序、201+lateAdopted、客户端零改动、叠在 [PR] fix: fork adoption no longer gives up permanently on 409 SNAPSHOT_UNAVAILABLE (#3011) #3024 上复用 forkSourceMessages()。
  • 机制抽查(不止读 diff,逐点回到定义处验证):
    • raw id 是内容寻址(kernel/src/wire/message-id.ts:32-43:sha256(role|contentType|toolCallId|toolName|text) + 会话内出现次序后缀)→ 位置级 identity-hash 相等数学上蕴含 child 现有消息的 rawId 与请求逐条相同,逐 ref 断言因此没有歧义空间;编号漂移 = 拒绝而非写坏。成立。
    • 锁前探测单调性:资格条件(无 receipt / 无 parentConversationId / 零自折叠 block / 纯前缀历史)在窗口内只会退化不会改善 → 廉价探测不会误放,权威判定在两把锁内重跑;JS 单线程使同步谓词评估原子。成立。
    • 锁序:grep 全仓 withSessionLock 调用点,其余路径全部单会话锁,parent→child 是全代理唯一两锁路径、无反向获取序 → 无死锁;并发测试驱动真实 child 锁竞争佐证。
    • 崩溃窗口:late 路径用 eager flushSync(child) 让 receipt 与继承一次写入落地([plugin] fork snapshot maintenance is O(full history) every request + snapshot persisted into every session file #2077 连续性),不留「有 block 无 receipt」的再闩死残态。
    • !late 分支与原 fresh-fork 代码逐行等价(仅独立键赋值顺序差异,无语义差别);late 子会话保留自己实测的 rules/nudge/stats/metadata/transient,只覆写继承本体。
  • 既有行为回归清单(逐项判定):
    1. 正常 fork(child 未注册)流程 → 保持(重构等价);
    2. 幂等重放(相同 payload → 200 replayed)→ 保持;
    3. 真幂等冲突(child 有 receipt 但 payload 不同)→ 409 CHILD_CONFLICT 保持;一处措辞漂移:该角落的 error text 从「…idempotency payload differs」变为「child conversation already registered」(注册门先命中)。同 code、同 409,客户端只读 code 不读 text(src/agent/fork-adopt.ts:467-486 逐一核过)→ 零影响;PR 正文「其余 409 全部不变(同 code、同 error text)」在此角落略强于事实,记录在案,不拦;
    4. 边缘组合(child 存在但未注册 + 父状态已漂移)→ 笼统 CHILD_CONFLICT 变为更精确的 PARENT_REVISION_CONFLICT/BRANCH_POINT_CONFLICT,同为 409、客户端同样终态(前者多触发一次重快照重试,终态不变)——PR 已披露,复核属实;
    5. registeredIds 同父保留逻辑(dsh 扩展接入 fork 协议(stage 1/#2394 L0):fork 子会话首请求前 snapshot→match→fork 收养 #2399 stage 2)→ 保持;
    6. 父侧 publicSnapshotRetained + eager flush → 保持;
    7. 高危模式逐项过:无模糊匹配语义漂移(资格是严格相等)、无 fail-fast 变吞错(断言失败仍 409/fail-closed)、无阈值/时序漂移、响应字段纯加法式。
  • 测试有效性:5 个新测试驱动真实竞态形态(child 先把继承历史作为真实模型请求发出去、fork POST 随后到达),断言有牙——正例不止看状态码:断言下一条请求的转发体含 summary 且不再含原文(证明块在真实管线里可用,不是账面记账);每个负例额外断言「未写 receipt」(无半成品采纳);并发例对真实 child 锁竞争。本地定向跑 plugin-fork / dsh-fork-adopt / pi-omp-fork-adopt / fork-adoption 四套件 105/105 绿;远程 CI 本 head 34/34 绿(含 ubuntu/windows × node 22/24 全量 test lane + 全部 e2e + static gates)。
  • diff 整洁度:新增提交只碰 3 个文件,每行都与本 PR 目的相关;注释带 issue 编号,符合仓库惯例。

八问

  1. 是什么问题:fork 子会话的首条请求若在继承完成前落地,服务端就把该子会话永久注册,此后所有继承请求撞 409——这个子会话永久失去继承(零折叠起步)。
  2. 什么原因导致的:注册时序与继承完成时序解耦——recordPluginSession 在转发前执行且持久化,服务端对「child 存在但未分叉」没有二次补全路径;客户端又把 CHILD_CONFLICT 分类为终态,两侧叠加成永久闩死。
  3. 影响范围与严重程度:低-中。触发面窄(首条 adoption 瞬败 / 宿主重启重放),后果仅该子会话永久零折叠起步,最坏尾部是大历史克隆撞 context_length_exceeded 循环。
  4. 怎么修复的:服务端迟补全——fork 端点命中「child 存在但无继承凭据」时,锁内做严格资格判定(未分叉三条件 + 全长逐条 identity-hash 相等 + 逐 ref 编号一致性),合格则用共享物化函数把继承就地补全到活会话并落 receipt;不合格维持原 409。客户端零改动。
  5. 是否有回归问题:无——既有行为清单逐项保持或已披露(见上第 1–7 项);唯一措辞漂移(第 3 项)客户端透明,已记录。
  6. 是否破坏缓存:否——上游 prompt cache 不受影响;幂等去重键(requestHash)语义不变,迟补全落的 receipt 用同一 hash,重放路径一致。
  7. 是否改了设计方向:否——仍在现有 fork 协议边界内,给冲突分支加一条「可迟补全」出口;数据流不变(客户端驱动 adoption、服务端物化)。
  8. 是否引入新配置:无。

收工判定

可合——§7.4 人审域(向活会话 seed refs/blocks + 响应形状扩展),不走 auto-merge,等你合并。建议顺序:先合 #3024 再合本 PR(本分支基于 #3024 的 tip 构建,先合 #3024 后本 PR rebase 自动收敛为单个新提交;直接合也可以,只是 #3024 的内容会随本 PR 一起进 master)。

@ranxianglei ranxianglei changed the title fix: fork 迟补全——首条请求抢跑落地的子会话不再被 CHILD_CONFLICT 永久闩死 (#3025) [PR] fix: fork delayed completion — child session where the first request races ahead and lands is no longer permanently latched by CHILD_CONFLICT (#3025) | [PR] fix: fork 迟补全——首条请求抢跑落地的子会话不再被 CHILD_CONFLICT 永久闩死 (#3025) Oct 16, 2026

This branch has not been deployed

No deployments
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