Skip to content

[PR] fix(storage): DB open self-check no longer blocks startup — large-DB background check + verdict cache (#4609) | fix(storage): 开库自检不再阻塞启动——大库后台查 + 判定缓存(#4609) - #4852

Merged
ranxianglei merged 3 commits into
masterfrom
2026-10-26_quickcheck-cache
Oct 26, 2026

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

这是啥问题

0.1.205 起(#4175),UnifiedStore.open() 在listen 之前无条件跑 PRAGMA quick_check,耗时 O(库大小):14 GB 库要 260 秒。native 车道 spawn 代理的预算是硬常量 60 秒 → 子进程还在扫库就被杀 → 插件判定 bootstrap 失败 → 宿主整个生命周期直连(不压缩),大会话带原始历史出网撞上游 400。每次重启重复同样的扫描(判定从不缓存),260 秒里零日志,和"卡死"无法区分。

根本原因

自检本身没错,错在位置:O(库大小)的工作被放在了启动关键路径上,且判定结果从不缓存。60 秒预算是宿主侧常量,扫描时间无上界,调哪边都不可能收敛——唯一正确的修法是别让 listen 等自检。

改动(两半,#4175 的"坏文件不得迁移"前提在便宜场景原样保留)

1. 判定缓存 index.db.chk.json

2. 尺寸门限 512 MiB

隔离联动:quarantine 时 .chk.json 随 db/-wal/-shm 一起改名走,新库从干净缓存开始。
逃生门:BILI_STORAGE_QUICK_CHECK=0 语义不变(不查、不碰缓存)。

诚实交代的取舍

大库升级后的首次开库,迁移会跑在"未验证"的文件上(#4175 前提的让步)——V8 迁移本身流式可续、JSON envelopes 永远是恢复源,窗口有界且可见(日志+runtime-status)。

验证

  • 新增 tests/storage-integrity-cache.test.ts 6 用例:同步查写缓存+重开跳过、stat 变化强制重查、缓存失败抛类型错误(不重扫)、超阈值走后台不阻塞 open、env=0 全跳过、坏小库照旧同步抛错 → 6/6
  • 既有 storage-durability 5/5、launcher-site-overlay 31/31 绿;storage-engine-proofs 8/12(4 个失败为 master 既有红,stash 复核确认与本改动无关)
  • tsc --noEmit 干净

关联:#4609(P1)、#4175(自检来源)、#4677(大库迁移教训:关键路径上的 O(库) 工作)

PRAGMA quick_check walks every B-tree page of every table, so a 14 GB
index.db costs ~260 s of single-core scan on EVERY open — and because
open() runs it before listen, the native lane's 60 s spawn budget kills
the child mid-scan, the host falls back to direct (no compression) for
its whole lifetime, and every restart repeats the scan (#4609).

Two halves, #4175's premise (a corrupt file must never be migrated
against) preserved wherever it is cheap:

- verdict cache: index.db.chk.json records the last outcome keyed by
  (schema version, index.db stat, -wal stat). An unchanged file set
  skips the rescan; any write or a schema upgrade invalidates the key; a
  hot -wal left by a killed process changes the key by construction —
  exactly the risky boots that deserve a rescan. A clean close of a
  verified store refreshes the key onto the post-checkpoint stats, so
  the normal restart path is rescan-free.

- size gate: stores at or below 512 MiB keep the synchronous
  pre-migration check (a couple of seconds at most). Larger stores check
  post-open on a second read-only connection in a worker thread (WAL
  allows concurrent readers; the main handle has already run recovery),
  so open() returns in milliseconds. A background failure is recorded
  loudly, cached, and makes the NEXT open throw StorageIntegrityError so
  the existing #4175 quarantine-and-reopen-fresh machinery runs.

BILI_STORAGE_QUICK_CHECK=0 keeps its meaning: no check, no cache IO.
Quarantine now moves the .chk.json sidecar along with the db set.
@ranxianglei ranxianglei added the ci:full Run the FULL CI matrix (all e2e lanes + 12-leg test matrix) on this PR label Oct 26, 2026
@ranxianglei ranxianglei changed the title fix(storage): 开库自检不再阻塞启动——大库后台查 + 判定缓存(#4609) [PR] fix(storage): DB open self-check no longer blocks startup — large-DB background check + verdict cache (#4609) | fix(storage): 开库自检不再阻塞启动——大库后台查 + 判定缓存(#4609) Oct 26, 2026
…the dist-external gate (#4852)

The WORKER_SOURCE template literal is bundled VERBATIM into dist/, where
scripts/check-dist-external.mjs text-scans for quoted require("…") forms.
The literal require("node:sqlite") inside the worker source tripped the
gate in dist/index.js, dist/agent/pi-native.js and dist/agent/opencode-native.js
— node:sqlite is NOT listed in module.builtinModules (still experimental),
so the checker's builtin exemption never applied. Route the specifier
through a named const (SQLITE_SPEC) — the checker's documented sanctioned
"opaque specifier" path — mirroring 8b70e91 for
src/storage/quick-check-worker.ts. Runtime behavior unchanged: same
worker_threads Worker, read-only DatabaseSync, PRAGMA quick_check,
postMessage verdict.
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-26_quickcheck-cache (f49caca)

Latest build — install this exact version

npm install -g billion-context@0.0.6275-pr.4852.dda5292

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
f49caca billion-context@0.0.6275-pr.4852.dda5292 build
bec0cda billion-context@0.0.6266-pr.4852.9d24ce5 build

Download artifact

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

Install instructions are refreshed on each push.

…scans (#4852)

The quick_check verdict cache was named index.db.chk.json — the sessions
directory's first non-envelope .json. The dsh persona e2e walker treats
every .json there as a session envelope and RETURNED from the whole scan
when one had no payload.id, so the raw-conversation-key assertions went
blind ('the raw conversation key persisted a session' failed on both #2241
and #2791). Rename the sidecar to index.db.chk (non-.json; the sessions
tree stays envelope-only) and harden the walker to skip stray files
instead of aborting. Also demote four integrity-check exports the
unused-exports gate flagged (INTEGRITY_SYNC_THRESHOLD_BYTES, IntegrityCache,
IntegrityMode, effectiveSyncThreshold) to module-internal.

Verified locally: tsc, unused-exports gate, dist-external build,
storage-integrity-cache 6/6, storage-durability 5/5, and the two dsh
persona lanes now pass (#2241, #2791).
@ranxianglei
ranxianglei merged commit 96b7e43 into master Oct 26, 2026
56 checks passed
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

先给结论:这 11 项 CI 失败根因只有两个,都在本 PR 新增的 src/storage/integrity-check.ts,且都是构建门禁问题(非运行时)。我独立定位后核对了分支最新状态——分支已在我分析的失败头 8c17da952 之后前进到 f49cacab2,其中已包含对这两个门禁的修复。因此本车道无需再 push、也无需开新 issue;请让 CI 在 f49cacab2 上重跑,预期转绿。

八问:

  1. 是什么问题:PR 新增的存储完整性自检文件同时踩中两条构建门禁——unused-exports 和 check-dist-external。后者卡在每条 e2e 车道的 build 步骤,所以 10 条真实客户端车道(opencode / codex / dsh / pi 等)还没跑到测试就全红,加上 unused-exports 共 11 项。

  2. 什么原因导致的(两处独立):

    • check-dist-external:后台 quick_check 的 Worker 源码是一段 eval 字符串,里面写了字面量 require("node:sqlite")。这段字符串会原样进 dist,而门禁用正则扫 dist 里的裸 require(...) 文本;CI 所用 Node(v22.x)里 sqlite 不在 module.builtinModules(worker_threads 在,所以只报 sqlite)→ 被判成未白名单的外部依赖。属自伤、非继承自 master——driver.ts 里真正那处 require("node:sqlite") 会被 esbuild 改写为 __require(...),门禁的词边界正则不匹配,所以 master 一直是绿的。
    • unused-exports:该文件把 4 个只在模块内部使用的符号(INTEGRITY_SYNC_THRESHOLD_BYTES / IntegrityCache / IntegrityMode / effectiveSyncThreshold)都加了 export,被判为 INTERNAL_ONLY。
  3. 影响范围与严重程度:仅影响本 PR 的 CI 门禁(build 阶段即挂),不影响运行行为,用户无感。11 项红但根因收敛为两处。

  4. 怎么修复的(已由分支上的提交完成,我核实而非重做):

    • bec0cdafb:把 Worker 里的 sqlite 加载改为不透明变量 const SQLITE_SPEC="node:sqlite"; … require(SQLITE_SPEC)——即门禁文档认可的“不透明说明符”惰性路径,规避文本扫描。
    • f49cacab2:去掉上述 4 个符号的 export(另含一处 sidecar 命名改动,见第 7 问备注)。
    • 我的独立验证:unused-exports 门禁在当前头实跑 clean;check-dist-external 我用真实门禁代码 + 同 tsup 配置(esm / node22 / minify / createRequire banner)打包当前头的 Worker 源码实测,node:sqlite 引用数从 1 → 0。
  5. 是否有回归问题:无。去 export 不改运行时(符号仍在模块内被使用);Worker 改成 require(SQLITE_SPEC) 运行时仍解析同一个内置 node:sqlite,行为逐字节等价。均未触碰语义 / 阈值 / 时序 / 输出格式。

  6. 是否破坏缓存:不涉及(未触碰压缩 / 缓存 / 增量游标 / 去重键)。

  7. 是否改了设计方向:不涉及。

    备注:f49cacab2 顺带把完整性自检的 sidecar 从 index.db.chk.json 改名为 index.db.chk(避免被会话目录扫描当成 JSON 载荷)。这是一处功能性改动,属 [PR] fix(storage): DB open self-check no longer blocks startup — large-DB background check + verdict cache (#4609) | fix(storage): 开库自检不再阻塞启动——大库后台查 + 判定缓存(#4609) #4852 自身范畴、非本次 CI 门禁范围,我未对其做端到端验证,建议随本次 CI 一并确认。

  8. 是否引入新的配置:不涉及。

收工判定:本车道不再 push、不开新 issue(无需要 owner 拍板的设计决策项)。请等待 / 触发 f49cacab2 上的 CI 重跑确认转绿;若仍红,把具体 job 日志发我,我再定向排查。

@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.858-master.96b7e43 (rolling master tag).

npm install -g billion-context@0.0.858-master.96b7e43  # 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.

@github-actions github-actions Bot mentioned this pull request Oct 27, 2026
ranxianglei pushed a commit to elight/billion-context that referenced this pull request Oct 30, 2026
Conflict resolution notes:
- store.ts constructor: keep master's signature (dbPath last param, ranxianglei#4852
  integrity work); ranxianglei#4628's extra path param drops in favour of this.dbPath.
- gc() zero-ref sweep: keep ranxianglei#4800's index-driven batched collection
  (collectZeroRefContent, fixed 2000-row batches) instead of ranxianglei#4628's
  rowid-IN-subquery adaptive batches — with idx_content_refcount present,
  candidate selection is an index range scan and per-batch TRUNCATE
  checkpoints bound WAL growth. ranxianglei#4612's test asserts the actual log format.
- live-files.mjs: union of both sides' entries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:full Run the FULL CI matrix (all e2e lanes + 12-leg test matrix) on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant