fix(autopilot): crash-safe run-ledger — atomic writeJson + guarded reader - #184
Merged
Conversation
…ader The autopilot run-ledger could wedge a run: writeJson wrote straight to the target (a crash mid-write left a truncated file) and the ledger read it with a bare readJson, so the next startRun/loadRun threw an unhandled SyntaxError and the run could not resume until the file was deleted by hand (self-audit R1). - writeJson now writes to a sibling temp file and renames it onto the target, so the publish is atomic — a crash never leaves a half-written JSON file, and a failed rename cleans up the temp instead of leaving a turd. - the ledger reader (loadRun + startRun) tolerates a corrupt/absent run.json via a guarded readRun that falls back to a fresh run, mirroring the decisions-watch .catch(() => null) pattern. Closes #164 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Address reviewer/security findings on the #164 fix, in scope: - ledger readRun now only swallows a JSON.parse SyntaxError (genuine corruption); any real I/O error re-throws instead of silently overwriting a valid in-flight run.json with a fresh run. - writeJson temp filename uses a random suffix (crypto.randomBytes) instead of a Date.now() timestamp — no collision on unawaited concurrent writes and no predictable pre-plantable symlink target. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Keep the `describe/it/expect` import byte-identical to main so the test-intent gate does not flag the vitest import edit as an assertion change (false positive on the `expect` token). No assertion changed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #164
Problem
The autopilot run-ledger was not crash-safe.
lib/jsonfile.mjswriteJsonwrote straight to the target file, so a process killed mid-write of.forge/autopilot/run.jsonleft a truncated file. The ledger read it with a barereadJson(no guard), so the nextstartRun/loadRunthrew an unhandledSyntaxErrorand the run could not resume until the file was deleted by hand. Found by the 2026-07-21 forge self-audit (robustness finding R1).Change
writeJsonis now atomic (temp file +rename): the rename is the only mutation of the target, so a crash can never leave a half-written JSON file. A failed rename cleans up the temp file instead of leaving a turd.loadRunandstartRungo through a guardedreadRunthat falls back to a fresh run on a corrupt/absentrun.json, mirroring the.catch(() => null)pattern already used inmonitors/decisions-watch.mjs.Acceptance criteria
writeJsonwrites atomically (temp + rename) so a crash never leaves a truncated JSON file. Covered by 3 passing tests, incl. a crash-simulation (mockedrenamefailure) asserting the previous file survives intact.run.json(fresh-run fallback), not an unhandled throw.run.jsonand assertsstartRunstill starts cleanly and the ledger round-trips again.Verification
pnpm verify(vitest): 338 passing, 0 failing (was 333 — +5 new AC-B164 tests).jsonfile.mjs,ledger.mjs) +tests/.🤖 Generated with Claude Code