Repository navigation
test(compat): add a differential booking fuzzer - #2116
Conversation
Booking decides WHICH lot a reduction consumes. Get it wrong and the postings still balance, the count is still right and the error list is still empty — the ledger is simply wrong about what you own and what you gained. Example-based tests are worst at exactly that, and the fix log shows it: five booking fixes in twenty commits (#2058, #2069, #2081, #2093, #2099), each an ordering or lot-selection case nobody had written a test for. So this generates ledgers and compares BOOKED LOTS against Python beancount — an independent implementation, not a restatement of our own logic. For every account it compares full lot identity, (currency, per-unit cost, cost currency, lot date) -> units, so consuming the wrong lot at the same total value is still a failure. It also compares ACCEPTANCE: a ledger one engine books and the other rejects is a divergence even when no numbers differ, which is the shape of #2099. It found a real divergence on its first campaign — 58/60 agreed, both failures LIFO with same-date lots. Filed separately with a 12-line reproduction. Two things learned building it, both encoded in the script: `cost(position)` is the wrong column to read. For a costless posting it returns the units themselves, so a cash leg looks like it holds a cost of 1. `cost_number` is per-unit and null when there is no cost, which is what makes it the right discriminator. The first draft compared cash legs against nothing and reported four false divergences per run. AVERAGE and NONE booking are deliberately not generated: beancount rejects both outright, so every such run would be a known-unsupported skip rather than a test. `--self-test` plants six known outcomes (wrong lot, wrong units, either side rejecting, and two agreements) and requires each be classified correctly, then requires the real engines to agree on a hand-checked ledger. A harness that has only ever printed "no divergences" cannot be distinguished from one that is incapable of printing anything else; this one is checked in both directions before it is believed.
There was a problem hiding this comment.
Pull request overview
Adds a new compatibility-focused booking fuzzer script to generate randomized ledgers and differential-check rustledger’s booked lots (and acceptance/rejection) against Python beancount, targeting subtle lot-selection regressions that example-based tests tend to miss.
Changes:
- Introduces
scripts/compat-booking-fuzz.pyto generate ledgers across booking methods (STRICT/FIFO/LIFO/HIFO) and compare per-lot identity → units between rustledger and beancount. - Adds a
--self-testmode that plants known divergences to ensure the harness can actually detect mismatches. - Provides seed-based reproduction (
--seed) and multi-run execution (--runs) for fuzz campaigns.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Two of the four were real defects, not style. The `--python` default was `/home/dev/.compat-venv/bin/python` — my own machine's path, committed. It now defaults to `sys.executable`, which matches how the sibling compat scripts are invoked (`python3 scripts/compat-values.py`, under an interpreter that already has beancount) and still takes an override for a dedicated venv. `json.loads` was unguarded on the rledger side while the beancount side already caught decode failures. A banner or stray log line on stdout would have aborted the whole campaign rather than counting as one booking failure — and the asymmetry between the two paths is what made it wrong, since the comparison treats them as equivalent. Verified: a non-JSON stdout now returns ERROR instead of raising. Also extracted the query out of adjacent string literals so the intent is explicit, and stopped materializing the seed sequence a second time after iterating it. Self-test still passes and seed 35 still reproduces the LIFO divergence (#2115).
|
All four addressed in The
Also extracted the query out of adjacent string literals, and stopped materializing the seed sequence a second time after iterating it. Self-test still passes, and seed 35 still reproduces the LIFO divergence (#2115). |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Supersedes #2114, which used a
test/branch prefix — rejected by the branch-name check (onlyfeat|fix|chore|docs|refactor|perf|release|hotfix). Same commit, allowed prefix.Booking decides which lot a reduction consumes. Get it wrong and the postings still balance, the count is still right, and the error list is still empty — the ledger is simply wrong about what you own and what you gained.
Example-based tests are worst at exactly that, and the fix log agrees: five booking fixes in twenty commits (#2058, #2069, #2081, #2093, #2099), each an ordering or lot-selection case nobody had written a test for.
What it does
Generates ledgers and compares booked lots against Python beancount — an independent implementation rather than a restatement of our own logic. For every account it compares full lot identity,
(currency, per-unit cost, cost currency, lot date) -> units, so consuming the wrong lot at the same total value is still a failure.It also compares acceptance: a ledger one engine books and the other rejects is a divergence even when no numbers differ. That's the shape of #2099 — the bug there was answering at all.
It found a real divergence immediately
First campaign: 58/60 agreed. Both failures were LIFO with same-date lots, now filed as #2115 with a 12-line reproduction — beancount and rustledger disagree about which same-date lot LIFO treats as "last in", giving different cost bases and different realised gains (18.00 USD apart on the generated case).
Two things learned building it
cost(position)is the wrong column. For a costless posting it returns the units themselves, so a cash leg looks like it holds a cost of 1.cost_numberis per-unit and null when there is no cost, which makes it the right discriminator. The first draft compared cash legs against nothing and reported four false divergences per run — caught by the self-test, not by reading.AVERAGE and NONE are deliberately not generated. beancount rejects both outright, so every such run would be a known-unsupported skip rather than a test.
Why you can believe a green run
--self-testplants six known outcomes — wrong lot, wrong units, either side rejecting, and two agreements — and requires each to be classified correctly. Then it requires the real engines to agree on a hand-checked ledger.A harness that has only ever printed "no divergences" cannot be distinguished from one incapable of printing anything else. This one is checked in both directions before it's believed, which is also how the
cost(position)mistake surfaced within minutes rather than living on as a permanent source of false alarms.