Skip to content

test(compat): add a differential booking fuzzer - #2116

Merged
robcohen merged 2 commits into
mainfrom
chore/booking-differential-fuzz
Aug 23, 2026
Merged

robcohen merged 2 commits into
mainfrom
chore/booking-differential-fuzz

Conversation

@robcohen

Copy link
Copy Markdown
Member

Supersedes #2114, which used a test/ branch prefix — rejected by the branch-name check (only feat|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_number is 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-test plants 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.

scripts/compat-booking-fuzz.py --runs 200
scripts/compat-booking-fuzz.py --seed 35     # reproduce the LIFO divergence
scripts/compat-booking-fuzz.py --self-test

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.
@robcohen
robcohen requested a review from a team as a code owner August 23, 2026 20:48
Copilot AI lite review requested due to automatic review settings August 23, 2026 20:48
Comment thread scripts/compat-booking-fuzz.py Fixed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py to generate ledgers across booking methods (STRICT/FIFO/LIFO/HIFO) and compare per-lot identity → units between rustledger and beancount.
  • Adds a --self-test mode 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.

Comment thread scripts/compat-booking-fuzz.py Resolved
Comment thread scripts/compat-booking-fuzz.py Resolved
Comment thread scripts/compat-booking-fuzz.py Resolved
@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 170 masked)
Full AST Match 100%
Total Files 811
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

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).
@robcohen

Copy link
Copy Markdown
Member Author

All four addressed in 823379c577. Two were real defects rather than style, and worth naming:

The --python default was /home/dev/.compat-venv/bin/python — my own machine's path, committed. Now sys.executable, which also matches how the sibling compat scripts are invoked (python3 scripts/compat-values.py, under an interpreter that already has beancount), with the flag still available 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 instead of counting as one booking failure. The asymmetry is what made it wrong — the comparison treats the two paths as equivalent, so they have to fail the same way. Verified: non-JSON stdout now returns ERROR rather than raising.

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

codecov Bot commented Aug 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit ba74755 into main Aug 23, 2026
44 checks passed
@robcohen
robcohen deleted the chore/booking-differential-fuzz branch August 23, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants