fix: tolerate unreadable prior snapshot in export - #12
Merged
Merged
Conversation
Spec for making export tolerate an existing-but-unreadable prior snapshot (wrong passphrase / corrupt / unsupported schema): warn and regenerate instead of crashing, by catching ValueError alongside FileNotFoundError at the prior-read call site. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Task-by-task plan: TDD the except ValueError branch in cli.export, a regression test for the all-fail overwrite consequence, a CLAUDE.md gotcha, and full verification (pytest/ruff/mypy/100% coverage). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
read() raises ValueError when an existing snapshot can't be decrypted (wrong passphrase, corrupt, or unsupported schema). export only caught FileNotFoundError, so a passphrase change bricked every subsequent run. Catch ValueError too: warn, treat the prior as absent, and regenerate. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
When the prior snapshot can't be decoded and every account also fails, the all-error snapshot replaces the unreadable file rather than the keep-previous branch firing. Characterisation test for that consequence. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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
exportreads the existing snapshot before fetching (to carry account data forward and to decide whether to keep the previous file when all accounts fail). It only caughtFileNotFoundError, so an existing-but-undecodable prior — most commonly after the encryption passphrase was rotated viaset-encryption-passphrase— raised an uncaughtValueErrorand crashed every subsequent run until the file was manually deleted.cli.py): catchValueErroralongsideFileNotFoundErrorat the prior-read call site. Log a warning naming the file and cause, treat the prior as absent, and regenerate.snapshot.read()stays strict — resilience lives at the call site, which knows the prior is only an optimisation.prior_by_loginempty, so failed accounts get empty carry-forward and the all-fail "keep previous" guard is bypassed (the undecodable file is overwritten with a readable all-error snapshot).CLAUDE.mdgotcha added. NoREADMEor schema change.Test Plan
uv run pytest— 86 passed, 100% coverage maintaineduv run ruff check src/ tests/— cleanuv run mypy src/mt5_pnl_exporter— cleantest_export_regenerates_when_prior_unreadable— garbage prior + succeeding account → exit 0, file regenerated and decryptable, warning emittedtest_export_overwrites_unreadable_prior_when_all_fail— garbage prior + failing account → exit 1, readable all-error snapshot replaces the garbageset-encryption-passphraseto change the passphrase, thenexport— now warns and writes a fresh snapshot instead of crashingFollow-ups (out of scope, noted from review)
ValidationError ⊆ ValueError). A test variant for those would lock the contract explicitly.snapshot.read()doespath.read_bytes()outside itstry, so anOSError/PermissionErrorrace (e.g. a locked file on Windows) still escapes uncaught — separate from this fix.🤖 Generated with Claude Code