shell-20261008-raft-snapshot-install-fsync-recovery
Done. The verified solution is at /workspace/SOLUTION.md (31 KB, self-contained, with the full implementation and harness embedded). Supporting files: /workspace/raft_snapshot.sh and /workspace/verify.sh.
The classic temp → rename → manifest → truncate log sequence has seven reachable crash states (F1–F7). The fatal ones are:
snap-<idx>) or a stale .tmp/directory entry, so a torn or leftover file is loaded as a valid snapshot and commitIndex is set from its name.commitIndex downward. That silently loses committed entries — the actual safety violation.Make the snapshot one self-describing, checksummed file (512-byte header with index/term/length/sha256/commit, then payload, then DATAEND trailer), so data and its commit metadata are durable or torn together. The single commit point is one atomic rename inside one directory:
fsync(temp) # bytes durable BEFORE the name (Rule 1)
rename -> final # atomic, same FS
fsync(dir) # name durable (Rule 2)
write_stable(commit = max(commit, idx)) # only after Rule 2
truncate log > idx # idempotent, last
Recovery wipes staging, validates every candidate, keeps the highest valid snapshot, quarantines the rest, discards the torn log tail, and computes:
commit = max(stable.commit, snapshot.index)
coverage = max(snapshot.index, lastIntactLogIndex)
if commit > coverage: NEEDS_RESYNC = 1 # I6 — never clamp, never roll back
bash /workspace/verify.sh)==== 22 passed, 0 failed ====
Covers: baseline restart; six torn-write shapes (truncate, payload flip, trailer flip, bad magic, trailing junk, zeroed length) — each rejected with commit preserved and NEEDS_RESYNC set; stale staging + invalid final entries; crash injected at each of the 5 durable install steps; torn stable record; and the ordering adversary where a rename is durable but bytes are not (commit kept, NEEDS_RESYNC flagged). I also extracted the embedded code blocks from SOLUTION.md into a clean directory and re-ran the whole suite against them, confirming the document is truly self-contained (embedded script == tested workspace script).
# Evidence - Problem class: shell-20261008-raft-snapshot-install-fsync-recovery - Model: openrouter/deepseek/deepseek-v4.1-flash - Solved: 2026-10-08T23:21:35.306Z - Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Analyze and implement a crash-safe snapshot installation protocol for a Raft node whose log truncation, snapshot rename, and metadata updates can be interrupted at any instruction boundary. Recover deterministically from torn writes and stale directory entries while preserving the committed-index safety invariant, even when fsync ordering differs across supported filesystems.", "environment": "bash", "language": "shell", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "shell-20261008-raft-snapshot-install-fsync-recovery", "provider": "openrouter", "solved_at": "2026-10-08T23:21:35.307Z", "version": "5"}