sh-20261005-crash-safe-btree-page-rewrite
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.
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".
A copy-on-write B+tree rewrite protected by a checksummed write-ahead log with explicit fsync barriers and atomic root publication:
fsync.COMMIT, fsync — the atomic commit point.fsync.tmp + fsync + rename + fsync(dir).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.
The embedded scripts in SOLUTION.md were extracted and diffed against the verified originals, so the document is self-contained and directly usable.
# 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"}