Repository navigation
[PR] feat(storage): index.db backup and corruption recovery commands bili storage backup / repair (#7156) | feat(storage): index.db 备份与损坏恢复命令 bili storage backup / repair(#7156) - #7431
Conversation
…ed index.db snapshots and corruption recovery (#7156) The 2026-11-20 production incident (#7156) left the operator with a hand-rolled .recover-equivalent and manual file surgery as the only recovery path for a proven-corrupt 3.2 GB index.db. Those moves are now first-class, self-verifying commands: bili storage backup [--keep N] Online-safe consistent snapshot via VACUUM INTO (never rewrites the source, no exclusive lock — #4612 does not apply); the snapshot must pass its own quick_check before it counts; <sessions>/backups/ keeps the newest N (3). bili storage repair [--from <backup.db>] With --from: verify the backup (quick_check + readable sessions table), quarantine the corrupt index.db IN PLACE (index.db.corrupt-<ts>, -wal/-shm/ .chk moved alongside — evidence kept), COPY the backup in (the backup stays intact), re-verify, tell the operator to restart. Without --from: salvage — read-only row-by-row extraction into a fresh schema (FK dependency order), bisecting broken page ranges to single rows and re-reading row columns individually (a corrupt overflow cell nulls its own column, the rest of the row survives — the #7156 field case lost only its big columns this way). Self-verified before the same swap. Safety rails (all tested): * swap refuses while another live process holds index.db.lock (#4175 pid discipline — a quarantined file must not be stolen out from under a writer); * repair refuses to replace a HEALTHY index (passing quick_check) with a possibly-older backup — deliberate rollback stays a manual rename. New module src/storage-backup-repair.ts + cli.ts wiring (bare 'bili storage' stays a usage error — gc archives real sessions, so no action is the silent default). README 'Storage maintenance CLI' section + CHANGELOG. Regression: tests/storage-backup-repair.test.ts (7 cases: verdicts, live-writer pid discipline, backup+retention, --from swap, healthy-index refusal, no-backup salvage, --json).
📦 Built Package ArtifactBranch: Latest build — install this exact versionnpm install -g billion-context@0.0.11156-pr.7431.400d744This exact version is immutable and reproducible. The rolling Recent builds (commit → version)
Download artifact
tar xzf billion-context-pr7431.tgz
npm install -g packageInstall instructions are refreshed on each push. |
(ai: vllm-qwen/qwen3.8-27b) 查重: 不重复 —— 本 PR 是 #7156 事故家族的「运维防线」,与 #7424(运行时防线:拒启 + 信封导入保护)、#7425(大字段拆表,降低损坏代价)互补;无其他 open [PR] 针对本线程。 标签: scope/storage + p2(存储域运维工具,源自 p1 事故 #7156;命令本身 opt-in,不引入运行时行为)。 CI 红,两个根因(均为机械性登记遗漏;master 头 e691e6e 全绿 → PR 特有问题):
代码审核发现(独立于 CI):
计划(全部 additive commit 加在 PR 分支,不动你的提交):
本地定向验证后 push,CI 重绿后按八问体例回复审核结论。 |
The suite's freshDeadPid() fixture uses spawnSync("true") to mint a
guaranteed-dead pid, which trips the process-spawning tripwire in
run-ci-tests.mjs and fails every shard before any test runs. The only
child is that dead-pid holder: no ports, no servers, mkdtemp-isolated
per test — the entry is for the tripwire, like #5046/#4612.
…nt as loss (#7156) No-backup salvage walked rowids (SELECT max(rowid) + per-row re-read), but schema v11 has four WITHOUT ROWID tables (conversations, session_ext, kv_store, message_media) where max(rowid) throws 'no such column: rowid' — a structural property, not corruption. The old code took that as 'unreadable' and skipped the whole table silently, and 'lost' only counted per-row losses, so every salvage exited 0 with ok:true while those tables came out empty. Now: - isWithoutRowid() detects the table kind from the DDL in sqlite_master (not error-text matching); - WITHOUT ROWID tables are copied whole: the read runs on the read-only src handle (a corrupt page throws mid-scan) and the write is one transaction on the fresh db — all rows land or none do. (A cross-connection INSERT...SELECT is not an option: without ATTACH the SELECT side resolves to the fresh db's own empty table and 'succeeds' with 0 rows.) - any table the salvager cannot read (note set) now counts as loss: ok:false, exit 1, and the report names it in skippedTables (JSON) and a 'skipped tables:' line (text) — matching the stated contract 'exit 1 and report per table when anything is lost'. Behavior change: salvage that previously exited 0 with silently emptied WITHOUT ROWID tables now exits 1 and names them; intact WITHOUT ROWID tables are now preserved instead of dropped.
- buildSeedDb seeds real rows in conversations (1) and kv_store (2) so the salvage path has WITHOUT ROWID data to carry; - the existing salvage test now asserts the kv_store report is self-consistent with the new db (salvaged count == actual count, note present when rows are lost) and that skippedTables exists; - new deterministic test: corrupt EXACTLY the kv_store root page (rootpage from sqlite_master, whole page XOR 0xff — unlike the 1/3 window, the target is exact) and assert exit 1, ok:false, kv_store salvaged 0 with a 'WITHOUT ROWID' note and named in skippedTables, while the intact conversations table is copied whole (1 row) and rowid tables are unaffected.
(ai: opencode/vllm-qwen/qwen3.8-27b) CI fix run (attempt 1/6): architecture-docs fixed and pushed.
七问
收工判定:architecture-docs 已修复并随新 head |
…7156) The ③ salvage description predates the fix that makes WITHOUT ROWID tables copy whole and counts skipped tables as loss (exit 1); the entry now states both. The new tests live in the same file, under the existing no-backup salvage item of the regression list.
c85ddab to
92eb029
Compare
(ai: opencode/vllm-qwen/qwen3.8-27b) Re-wake verification: the reported blocker is already fixed; full CI is running on the current head — no further work needed this run.
七问
收工判定:当前 head |
(ai: vllm-qwen/qwen3.8-27b) 审核结论:PR #7431(bili storage backup / repair)—— 发现 3 处问题,已全部修掉,CI 全绿做了什么:在 PR 分支上追加 4 个 commit(未动原始 commit),推至 发现的 3 处问题:
修复(4 个追加 commit):
本地验证:目标套件 8/8 绿、相邻 7 套件 66/67(1 skip = 可选 better-sqlite3 未装,既有)、tsc 干净、build 成功、architecture-docs guard 绿、LIVE_FILES tripwire 本地模拟通过。 八问:
CI(92eb029 实测):48 个 check run —— 36 success + 12 skipped(e2e 车道,本 diff 不触发),0 failed。前一轮红的两个根因(LIVE_FILES + architecture-docs)均已修复转绿。 判定:可合并——功能 + 护栏 + 回归测试闭环,2 处 CI 根因与 1 处代码缺陷全部修复;mergeable_state=behind 只是 master 前进(mergeable=true,无冲突)。合并留给 owner。 |
…lict at 0.0.202 Master advanced 122 commits past the PR base (batch-seam double-claim fix #7428 -> kernel 0.0.201, storage backup/repair #7156/#7431, ingest-band default-off #7426, pf-tail-ids cap #7381, OpenAI reasoning alias #7432, dsh-mneme scan #7430, and more). Content hunks auto-merged (kernel/src/ compress.ts, all four storage-render goldens); the only conflicts were the three kernel version lines, resolved at 0.0.202 = master's 0.0.201 + this PR's kernel/src change, per the CI-enforced kernel version discipline. Targeted local verification: tests/golden-storage-render.test.ts + tests/storage-state-fields.test.ts 7/7, kernel issue6116/issue3769/ hide-consumed suites 27/27.
…te refusal test tests/issue7156-pure-sqlite-refusal.test.ts (added to master via #7431) carries the same hardcoded zeroPage(p, 2) recipe that 484a3d3 already fixed in tests/issue7156-storage-corruption.test.ts: page 3 of the file only lands on the sessions data page under the 12-table schema; this PR's schema (15 tables after the three-table retirement) shifts it onto an empty page, so the first post-open write no longer throws CORRUPT and the test fails on every CI shard. Look the sessions root page up from sqlite_master and zero THAT page — stable across schema sizes (same pattern as 484a3d3). Verified locally (node 22, linux): without the fix the test fails with 'Missing expected exception: a first-write corruption in the pure-sqlite stance refuses to start' (the exact ubuntu CI failure); with the fix 5/5 pass. PR #6785 CI fix.
背景
2026-11-20 #7156 事故(3.2GB index.db 被多个长驻 bili 进程共写损坏)里,恢复手段是一段手写的 .recover 等价脚本 + 手工文件手术。这个 PR 把这两步做成自带验证的一等命令。
bili storage backup [--keep N]VACUUM INTO在线一致性快照:不重写源库、不取独占锁,proxy 在线可跑(storage gc full VACUUM holds the write lock across the whole O(db) rewrite with no progress — online proxy shadow disabled mid-run | storage gc 全量 VACUUM 全程持写锁且无进度:在线代理影子层被降级 #4612 的--vacuum full离线约束不适用)<sessions>/backups/,按 retention 留最新 N 份(默认 3)bili storage repair--from <backup.db>(有备份)index.db.corrupt-<ts>隔离(-wal/-shm/.chk 一并移走,证据保留)不带
--from(无备份 salvage)安全护栏(全部有测试钉死)
index.db.lock(pid 存活判定,stale 自动放行)时拒绝换装(Storage Reliability Trio: open-time self-check (quick_check) + online backup (VACUUM INTO) + second-process probe on the | 存储可靠性三件套:开库自检 quick_check + 在线备份 VACUUM INTO + 同库第二进程探测 #4175 pid 纪律)——隔离文件不能从活写者脚下抽走回归
tests/storage-backup-repair.test.ts7 用例:verdict 分类、活写者 pid 纪律、备份快照+retention、--from换装(坏备份/不存在/健康库全拒绝)、无备份 salvage、活写者拒绝关联