Skip to content

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

Closed
robcohen wants to merge 1 commit into
mainfrom
test/booking-differential-fuzz
Closed

robcohen wants to merge 1 commit into
mainfrom
test/booking-differential-fuzz

Conversation

@robcohen

Copy link
Copy Markdown
Member

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. Filed separately 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.

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 as a permanent source of false alarms.

scripts/compat-booking-fuzz.py --runs 200
scripts/compat-booking-fuzz.py --seed 35     # reproduce one case
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:47
Copilot AI lite review requested due to automatic review settings August 23, 2026 20:47
Comment on lines +131 to +132
"SELECT account, units(position) AS u, cost_number AS cn, "
"cost_currency AS cc, cost_date AS cd",
@robcohen

Copy link
Copy Markdown
Member Author

Superseded by the same commit on an allowed branch prefix — test/ is rejected by the branch-name check.

Comment on lines +131 to +132
"SELECT account, units(position) AS u, cost_number AS cn, "
"cost_currency AS cc, cost_date AS cd",

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 differential “booking fuzzer” script that generates small Beancount ledgers and compares booked lot identities (and accept/reject behavior) between rledger and Python beancount, aiming to catch lot-selection/order divergences that example-based tests routinely miss.

Changes:

  • Introduces scripts/compat-booking-fuzz.py to generate randomized ledgers across supported booking methods (STRICT/FIFO/LIFO/HIFO) and compare booked lots per account.
  • Implements a --self-test mode that verifies the harness can detect planted divergences before running real engine comparisons.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

failures += 1
print("\n".join(report))
print("-" * 60)
total = len(list(seeds))
Comment on lines +141 to +143
lots: Booked = {}
for row in json.loads(out.stdout).get("rows", []):
units = row.get("u") or {}
@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!

@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

@robcohen
robcohen deleted the test/booking-differential-fuzz branch September 27, 2026 21:34
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.

3 participants