fix: make session history save atomic and surface corrupt files - #83
Merged
shauryagangrade merged 1 commit intoAug 14, 2026
Merged
Conversation
save() wrote the session file in place and swallowed every exception, so an interrupted or failed save could leave a partial file that load() then silently treated as 'no history' — silent data loss. Save now writes to a temp file in the same directory and os.replace()s it into place (no partial file on crash), cleans up the temp file on failure, and prints a warning to stderr instead of passing. load() now reports the path and suggests removing the file when a session file exists but cannot be parsed; a genuinely missing file stays silent. Fixes shauryagangrade#53.
shauryagangrade
marked this pull request as ready for review
August 14, 2026 17:46
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 #53.
Problem
gcode/history.pywrote session history in place and swallowed every exception insave(bareexcept Exception: pass). An interrupted or failed save could leave a partial session file — andloadthen returnedNone, so the whole conversation looked like it never existed. Silent data loss.Change
save()now writes to a temp file in the same directory (tempfile.mkstemp) andos.replace()s it into place, so a crash or interrupt can never leave a partial file. On failure the temp file is removed and a warning naming the path is printed to stderr —savestill never crashes the REPL.load()now prints a warning naming the file and suggesting the user inspect/remove it when a session file exists but cannot be parsed, instead of silently returningNone. A genuinely missing file still returnsNonesilently (no history yet is not an error).saveis called once per turn (not per keystroke), so a per-failure warning is not spammy.Tests
test_save_write_failure_resilience: patchedos.replaceto fail — asserts no exception, no partial file at the real path, no stray.tmpfile, and a warning naming the path.test_load_corrupt_json_warns_with_path: invalid JSON — assertsNoneplus a warning containing the path and "corrupt".test_load_missing_file_no_warning: missing file — assertsNonewith no stderr output.Validation
ruff check .clean,ruff format --check .clean,mypy gcodeclean,banditclean,compileallclean.git diff --checkclean; rebased onto currentmain.