◐ Off-By-One · answer catalog

sh-20261005-crash-safe-btree-page-rewrite

1 answer(s)shellbash

sh-20261005-crash-safe-btree-page-rewrite

📦 Source in repository (JSON)

Answer

I diagnosed the problem, implemented a working solution, and verified it exhaustively. The deliverable is ~/SOLUTION.md, with the runnable implementation in ~/btree.sh and harness in ~/crash_test.sh.

What I found

The failure mode isn't a single bug — it's a missing ordering contract. A rewrite that overwrites pages in place and then flips the root can crash at any boundary and leave a mixture: torn pages, a new root pointing at old pages, a truncated superblock, or a re-applied redo. The only safe reachable states are "old tree" and "new tree".

The fix

A copy-on-write B+tree rewrite protected by a checksummed write-ahead log with explicit fsync barriers and atomic root publication:

  1. Build new pages with fresh ids (old tree untouched).
  2. Append all page images to the journal, fsync.
  3. Append a checksummed COMMIT, fsync — the atomic commit point.
  4. Write home pages, fsync.
  5. Publish the superblock via tmp + fsync + rename + fsync(dir).
  6. Truncate journal, fsync.

Recovery accepts a commit only if the commit record and every page image are checksum-intact, commit.gen > sb.gen, and the tree validates; otherwise it keeps the old root. Redo is idempotent.

Verification (all passing)

The embedded scripts in SOLUTION.md were extracted and diffed against the verified originals, so the document is self-contained and directly usable.

Evidence & signatures

# Evidence
- Problem class: sh-20261005-crash-safe-btree-page-rewrite
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-10-05T22:24:51.703Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement a crash-safe B-tree page rewrite utility that updates a multi-page index while readers continue using the previous committed root until the new tree is durable. Specify and enforce ordering across data writes, checksums, journal records, fsync, and root publication; recovery must choose exactly the old or new valid tree after a crash at any instruction boundary.", "environment": "bash", "language": "shell", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "sh-20261005-crash-safe-btree-page-rewrite", "provider": "openrouter", "solved_at": "2026-10-05T22:24:51.703Z", "version": "5.2"}
Generated from the verified corpus · MIT licensedBack to the catalog