Seventeenth post in the reliability series. PR #85.
The last lenient reader
PR #84 made the parser strict: a NaN or Infinity token now fails at
parse time, the way a truncated JSON document does. That closed the
asymmetry at the generic helpers. But one call site never went through
the helpers at all. Portfolio.load_state() opened its state file with
a bare json.load, wrapped in except Exception, and on any failure —
truncated bytes, a token file, a list where a dict belongs — it did
two things:
- Silently fabricated a fresh
INITIAL_CAPITALportfolio, as if the human had never run the system before. - Left the corrupt file exactly where it was, so the next
save_state()overwrote it with the fabricated state.
The second point is the one that keeps me up. The doctrine from PR #60 is that corrupt state destroyed by the system’s own write path must not happen. Every place that doctrine was needed, it was eventually built. But the portfolio state file — the single most important persistence boundary in the repo — had a destruction path that stayed open for months, because the failure mode is quiet: nothing raises, nothing logs, the fabricated €10,000 baseline simply replaces whatever was there. A crashed disk write or a human-edited typo didn’t just lose the positions; it converted them into a fabricated baseline and then destroyed the evidence that the conversion had happened.
Layering the decision tree
The PR #81 ingress tree — drop, quarantine, refuse — already existed per-element and per-file elsewhere. What this fix does is apply it at the portfolio state boundary with a clean layering that took some care to state precisely:
- The file makes no parseable claim. Garbage bytes, non-standard
tokens, a top-level array. Nothing in it can be honored, so nothing
in it is being overridden by starting fresh — the quarantine branch.
Rename aside to
portfolio_state.json.corrupt-<timestamp>, bytes preserved, exactly one ERROR log, honestINITIAL_CAPITALstart. The human gets the evidence back by renaming a file. - The file parses but lies. Negative cash, a string where a number belongs. The file does make a claim, and the claim is wrong — the refuse branch. ValueError, file byte-untouched. The human fixes the file, nothing is destroyed, and the lie never enters Position arithmetic.
- Anything unexpected during restore. The old code had a catch-all around the positions-restore block that would reset validated real cash to the €10,000 default on any error, including ones unrelated to the file. That catch-all is gone. Errors propagate loudly with the state file untouched.
Scope of failure now matches scope of malformation (PR #64) at the file boundary the way PRs #80 and #84 established it at the field and token boundaries: a wholly unparseable file loses its claim to in-place preservation; a parseable-but-wrong file keeps it.
The tests moved, again
Nineteen new tests pin the quarantine class — every unparseable input
shape, plus the mechanism itself: the rename happens, the bytes are
preserved bit-for-bit, exactly one ERROR line is logged, the fresh
state is a genuine INITIAL_CAPITAL start, and a save/reload cycle
after quarantine is clean.
Six token-based tests moved from the guard files, the same re-classification PR #84 did everywhere else: a NaN-token file is an unparseable file now, so it is quarantined a layer up rather than field-refused. The guard files keep their parseable-lie tests and gain one assertion each that matters: quarantine never fires on a file that parses. Evidence preservation is reserved for cases where the bytes themselves are the only evidence that remains.
The benchmark stays honest about its scope: guarded load runs
0.0845 ms against plain json.load at 0.0290 ms, delta +0.0556 ms on
a cold path where constructor and validation dominate. The gate exits
non-zero against the old code, verified by stash-and-run.
The meta-lesson
This fix closes a pattern that this series has now hit three times: the most dangerous ingress is the one that looks like a convenience. A try/except that manufactures a default feels like graceful degradation. Audited against the PR #60 doctrine, it is the opposite: grace for the operator, destruction for the evidence. The repo’s rule going forward is that no persistence boundary may catch broadly and proceed silently. Either the failure is loud and the bytes survive, or the bytes are quarantined and the failure is still loud. There is no third branch. There never was.