feat: add state backup and auto-recovery#218
Open
christopherblaisdell wants to merge 2 commits intodanielcopper:mainfrom
Open
feat: add state backup and auto-recovery#218christopherblaisdell wants to merge 2 commits intodanielcopper:mainfrom
christopherblaisdell wants to merge 2 commits intodanielcopper:mainfrom
Conversation
added 2 commits
April 5, 2026 16:38
Rolling .prev backup for state.json prevents total state loss from mid-sync crashes or corruption. save_state(): - Rotates current state.json to state.json.prev before each write - Uses os.replace() for atomic rotation under the existing file lock load_state(): - Detects empty shortcut_registry in state.json - If state.json.prev has a non-empty registry, auto-recovers from it - Logs a warning when recovery occurs so the user knows it happened - Handles missing/corrupt .prev gracefully (no crash, no recovery) Tests: 8 new test cases covering rotation, recovery, and edge cases (empty .prev, corrupt .prev, no .prev file, normal operation)
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.
Summary
Adds a rolling \state.json.prev\ backup to prevent total state loss from mid-sync crashes or corruption. If state.json loses its shortcut registry (e.g., due to a crash during write), the previous state is automatically recovered on next load.
Motivation
The plugin's state.json holds the shortcut registry — the mapping of RomM ROM IDs to Steam shortcut app IDs. If this file is corrupted or emptied during a crash (e.g., CEF crash during sync, power loss, filesystem error), all shortcut mappings are lost and the user has to re-sync from scratch. With hundreds of shortcuts, this is a significant data loss.
What changed
persistence.py — save_state()
persistence.py — load_state()
test_persistence.py — 8 new test cases
Risk
Very low — filesystem operations with defensive error handling. Normal save/load path unchanged except for the additional .prev rotation step.
PR size
121 insertions, 1 deletion across 2 files.