Skip to content

[PR] fix: route the fork lane through the network storage backend (#4911) - #4924

Merged
ranxianglei merged 3 commits into
masterfrom
2026-10-27_mysql-fork-clone-wiring
Oct 27, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-10-27_mysql-fork-clone-wiring

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #4911

What this fixes

In MySQL full-mode deployments the fork endpoint's unified-store row clone (and its inclusive throughSeq bound) never reached the backend: UnifiedShadow's sync facades return miss values when sync === null (network backends), so MysqlBackend.cloneSessionRows / .lastLiveSeq had no callers. Fork children on mysql silently ran kernel-only with an empty unified transcript.

Approach

The fork endpoint (handlePluginFork) is async, so it now awaits new async twins following the established viaAsync pattern:

  • shadow.ts: lastLiveSeqAsync / cloneSessionRowsAsync — sqlite delegates to the identical sync bodies (zero behavior change); mysql routes through the backend on the serial shadow queue; a failed/disabled sidecar degrades to the kernel-only fork (the fork itself never fails for a storage nicety — existing contract).
  • persist.ts: unifiedLastLiveSeqAsync / unifiedCloneSessionRowsAsync replace the sync wrappers (now orphaned; removed rather than left as dead code).
  • plugin.ts: fork block awaits them; the withSessionLock callback becomes async (it accepts () => T | Promise<T>), lock scope and ordering unchanged.
  • store.ts + backend.ts: UnifiedStore.cloneSessionRows reports false for an absent parent; SqliteBackend resolves that value instead of an unconditional true (previously inconsistent with MysqlBackend, which returns false).

Sync window reads (messagesForIds / uncoveredRows / messageIdRows) stay miss-by-design on network backends: their callers are the four prepare-* lanes' sync hot paths and snapshot render; routing them would be a pipeline rearchitecture decision, out of scope here (degradation to JSON envelope / legacy render is safe and documented). Same mechanism family as this issue — kept in one issue, not split.

Fixed in passing (labeled): the stale mysql selection notice in backendSelectionNotice still claimed the hot path "stays on sqlite until then" (step 3 landed in #3931).

Behavior changes (old → new)

  • mysql full-mode forks: child unified transcript rows were never cloned → now cloned through the backend (awaited before the 201 reply; adds at most two round-trips to fork latency on mysql only). No data loss either way (kernel-state fallback stays intact).
  • sqlite: zero change (twins delegate to the identical sync bodies).
  • absent-parent clone via the interface: SqliteBackend.cloneSessionRows resolved true → now false (matches MysqlBackend; the fork debug log line now fires honestly in that case).
  • startup log line for storage.backend="mysql": reworded to describe the current wiring (log text only).

Config surface

No fields added/renamed/re-semantics'd. Description-text updates only for storage.backend / BILI_STORAGE_BACKEND (fork added to the async-twin path list) — seeds updated and md regenerated (tools/gen-config-docs.mjs, en+zh).

Tests

  • tests/storage-backend-contract.test.ts: new fork-clone contract block — sqlite lane always runs; mirrored into the mysql lane (CI runs the mysql:8.0 service): inclusive throughSeq, per-row seq/msgId/contentHash fidelity, lineage columns, per-row refcount bump, idempotent replay, absent parent → false.
  • tests/storage-backend-mysql-e2e.test.ts: awaited clone lands real rows on real MySQL (read back through another async twin); regression guards pin that the sync facades still miss by design.
  • Local targeted runs green: contract suite, plugin-fork (49), issue4730-fork-live-seq, seq-ledger-pr3, fork-clone-media-refcount, storage-unified, storage-backend-knob; typecheck clean. Full-suite verdict = CI on this head.

The MySQL full-mode fork lane never reached the unified backend:
UnifiedShadow's sync facades (lastLiveSeq/cloneSessionRows) return
miss values when sync === null, so MysqlBackend's ready-made twins
had no callers. The fork endpoint is async, so wire it through new
async twins following the established viaAsync pattern:

- shadow: lastLiveSeqAsync / cloneSessionRowsAsync (sqlite delegates
  to the identical sync bodies; mysql routes through the backend on
  the serial queue; a failed/disabled sidecar degrades to the
  kernel-only fork and never fails the fork)
- persist: unifiedLastLiveSeqAsync / unifiedCloneSessionRowsAsync
  replace the now-orphaned sync wrappers
- plugin: handlePluginFork awaits them (the withSessionLock callback
  becomes async; lock scope and ordering are unchanged)
- store/backend: UnifiedStore.cloneSessionRows reports false for an
  absent parent (SqliteBackend no longer resolves an unconditional
  true — matches MysqlBackend semantics)

Tests: the contract suite gains a fork-clone block in both lanes
(inclusive throughSeq, seq/msgId/hash fidelity, lineage columns,
per-row refcount bump, idempotent replay, absent parent -> false);
the mysql e2e proves the awaited clone lands real rows and that the
sync facades still miss by design. Docs: the storage.backend rows
list fork among the async-twin paths (seeds + generated md en/zh).
@ranxianglei ranxianglei changed the title fix: route the fork lane through the network storage backend (#4911) [PR] fix: route the fork lane through the network storage backend (#4911) Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-27_mysql-fork-clone-wiring (5a958c4)

Latest build — install this exact version

npm install -g billion-context@0.0.6406-pr.4924.b394ab2

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
5a958c4 billion-context@0.0.6406-pr.4924.b394ab2 build
edc2185 billion-context@0.0.6404-pr.4924.0137fcb build
1e6cb3a billion-context@0.0.6399-pr.4924.5c609e9 build

Download artifact

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

Install instructions are refreshed on each push.

…qlite twin relies on (#4911)

mysql-contract CI caught ER_NO_DEFAULT_FOR_FIELD at MysqlBackend.cloneSessionRows:
the fork row clone's 5-column sessions INSERT leans on column defaults exactly
like the sqlite twin (schema.ts: meta_json TEXT NOT NULL DEFAULT '{}'), but the
mysql DDL never gave those five json columns defaults — tables created by the
pre-wiring DDL reject the clone in strict mode. Align the DDL and heal existing
tables idempotently (information_schema probe + MODIFY, the #4390 pattern).
@ranxianglei ranxianglei added bug Something isn't working p2 scope/storage labels Oct 27, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

深审通过 + 已推一个修 CI 的增量提交(edc2185)。

本 PR 的接线方案我逐项核过:withSessionLock 是 promise 链锁(session.ts:1240,持锁跨 await fn()),async 化不破坏互斥 ✅;viaAsync/enqueue 与既有 twins(viewForAsync/checkpointAsync 等)同款,克隆写进影子队列序列化、不与父会话 pending ingest 竞争 ✅;同步包装无孤立调用方 ✅;SqliteBackend「parent 缺失返回 false」的语义对齐正确 ✅;契约测试双向车道补齐(absent-parent→false 两车道都钉了)✅。SQLite 车道行为零变化(委托同一同步体)。

CI 红的根因(mysql-contract:ER_NO_DEFAULT_FOR_FIELD):孪生分歧——克隆的 5 列 sessions INSERT 依赖列默认值,SQLite DDL 给了(schema.ts:118-122 meta_json TEXT NOT NULL DEFAULT '{}'),MySQL DDL 没给(mysql.ts:86-90),严格模式直接拒。这是原死代码里的潜伏缺陷,被本次接线第一次真正执行而暴露——正好说明这趟接线的价值。

增量提交 edc2185:DDL 五个 json 列对齐 DEFAULT ('{}') + 存量表幂等迁移(information_schema probe + MODIFY,#4390 既有模式)。修的是根因(两孪生 DDL 应一致),不是在单一调用点硬编码列值;新表走新 DDL,老表启动自愈。本地 typecheck 干净、SQLite 车道契约测试全绿;MySQL 车道以 CI 为准。

遗留(不阻塞,已在 #4911 记录):MySQL 克隆的幂等 COUNT 检查仍在事务外——并发重复 fork 撞 PK 回滚后走 kernel-only 降级,数据正确只是降级,与 SQLite 侧(事务内检查)不一致,后续可顺手对齐。

…efaults (#4924 review)

edc2185 gave the five sessions json columns expression defaults, so the
header claim "every INSERT provides every column" no longer holds for the
fork row clone's 5-column INSERT — state the exception explicitly.
@ranxianglei
ranxianglei merged commit c499b14 into master Oct 27, 2026
51 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.878-master.c499b14 (rolling master tag).

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

bug Something isn't working p2 scope/storage

Projects

None yet

1 participant