Skip to content

chore(compat): fuzz currency-less units numbers against beancount - #2564

Merged
robcohen merged 1 commit into
mainfrom
chore/booking-fuzz-number-only-postings
Oct 10, 2026
Merged

robcohen merged 1 commit into
mainfrom
chore/booking-fuzz-number-only-postings

Conversation

@robcohen

@robcohen robcohen commented Oct 9, 2026

Copy link
Copy Markdown
Member

What

scripts/compat-booking-fuzz.py now generates postings written as a number with no currency (Assets:CashA -100, Assets:StockA -5 {}, -3 @ 1.10 USD) and compares how rledger and Python beancount book them.

Every seed books its existing lot-selection ledger as before, plus a second "number-only" ledger. The second ledger comes from its own RNG (random.Random(f"number-only-{seed}")), so every seed's lot-selection ledger is byte-for-byte unchanged. Each number-only ledger:

  • sets up three cash accounts and two stock accounts. Each one holds a single currency (or commodity), several, none, or one that was later emptied. The holdings are weighted toward a single currency, because a refusal anywhere rejects the whole ledger.
  • adds 1–3 transactions drawn from seven shapes. Each shape targets one branch of beancount's categorize_by_currency, as ported in fix(booking): infer a currency-less units number from the account balance #2490:
    • plain-one-group: the other postings form one currency group (step 1). That currency wins even when the account holds a different one. Sometimes there is an auto-posting beside it.
    • plain-several-groups: the other postings span two groups, so the balance decides (step 2). Sometimes there is no auto-posting, so a wrong currency makes the transaction unbalanced.
    • plain-no-group: the posting sits beside an auto-posting only.
    • several-number-only: 2–3 currency-less postings in one transaction. The balancing leg is an auto-posting, number-only, or written with a currency.
    • cost-reduce: -N {}, -N {C CUR} or -N {C}, optionally priced. The cash leg is sometimes number-only too.
    • cost-augment: N {C CUR}.
    • price: -N @ R CUR, @@, or @ R with the price currency elided.
  • varies precision (0–3 places), so tolerance inference from the postings as written is exercised. It also turns on infer_tolerance_from_cost 20% of the time, and shuffles posting order 30% of the time.

The report keeps the existing lines and adds a per-shape and a per-holding tally. Each line shows agreed/total and how many ledgers both engines booked versus both refused, so it shows that the accepting and the refusing sides of the rule were both reached. Zeros are printed too. Any number-only divergence fails the run, like any other unexplained divergence. --self-test now also fails if any shape or holding goes ungenerated over 300 seeds, or if a ledger has no currency-less posting. The detector for currency-less postings is itself tested on cases where it must answer no.

Why

#2490 checked the rule with a 37-case matrix and a corpus sweep, but nothing compared it against beancount after the PR merged.

How tested

All runs used beancount 3.2.3. Each run pointed --rledger at a copied, named binary, never at target/.

Against main (rledger-main, built from 001cca9):

  • --self-test passes.
  • Seeds 0–1999: 2000/2000 lot-selection ledgers agree, and 2000/2000 number-only ledgers agree.
shape agreed both booked both rejected
plain-one-group 378/378 265 113
plain-several-groups 366/366 120 246
plain-no-group 390/390 166 224
several-number-only 373/373 83 290
cost-reduce 382/382 143 239
cost-augment 378/378 167 211
price 390/390 144 246

By holding: one 1339/1339, several 581/581, empty 282/282, emptied 285/285.

Self-check: the new shapes do report a broken rule. I made three temporary edits to resolve_elided_units_currencies, built each one and copied the binary, then ran seeds 0–199:

broken build number-only divergences (seeds 0–199)
1. an account holding several currencies resolves to the first one, so an ambiguous case is accepted 33, in all 7 shapes; "beancount rejected, rledger booked"
2. step 2 (the balance fallback) is skipped 77, in all 7 shapes; "rledger rejected, beancount booked"
3. the balance outranks the other postings' group (wrong currency) 7, in plain-one-group, plain-several-groups and price, including numeric ones such as Assets:CashA/EUR: rledger=None beancount=-328.9

Lot-selection ledgers stayed at 200/200 in all three runs. After restoring the code and rebuilding, the binary's sha256 matched the main build (14afbb34…).

Out of scope: two divergences unrelated to the currency-less rule

An earlier version of the generator priced reductions and paid them in USD whatever the lots' cost currency was. It found two divergences that reproduce identically with the commodity written. They come from cost booking, not from inferring a currency:

  • -2 HOOL {11.00 EUR} @ 14.50 USD: beancount refuses it ("Cost and price currencies must match: EUR != USD"), and rledger books it.
  • -1 HOOL {} beside Assets:Cash 13.00 USD, when the lot is held at EUR cost: beancount takes the cost currency from the other postings' group (USD) and finds no position. rledger books the EUR lot.

The generator now keeps a reduction's price and cash leg in the lots' cost currency. The comment and the docstring say why. Neither divergence is waived; they are left for a separate decision.

CHANGELOG: entry under "Testing & Engineering Quality".

Closes #2513

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

The booking fuzzer never generated a posting written as a number with
no currency, so the categorize_by_currency rule #2490 ported had no
differential coverage. Every seed now also books a second ledger, drawn
from its own RNG so the lot-selection ledgers are unchanged, with
number-only postings in seven shapes against accounts holding one
currency, several, none, or one since emptied. The report tallies
agreement per shape and per holding, with both-booked and both-refused
counts, and the self-test fails if a shape or holding stops appearing.

Seeds 0-1999 agree against main. Three deliberately broken builds of
the rule each produce divergences.

Closes #2513

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner October 9, 2026 20:32
@codecov

codecov Bot commented Oct 9, 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

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 819
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

Copy link
Copy Markdown
Member Author

Review rounds (#2513)

Round What was checked Findings
1 Full read of the generator, tallies and self-test diff (git diff origin/main...HEAD) None
2 Independent re-run: --self-test passes. On fresh seeds 5000–5099 against an rledger with the rule deliberately broken (balance fallback outranks the group), the fuzzer reports 4 number-only divergences, all in plain-one-group (10/14 agreed). Lot-selection ledgers stay 100/100. None

The loop converged after round 1 found nothing, and round 2 confirmed it. Campaign numbers are in the PR body: seeds 0–1999 give 2000/2000 on both ledger kinds. Three broken builds give 33, 77 and 7 divergences.

Two booking divergences turned up while building the generator. Neither comes from the currency-less rule, and both reproduce with the commodity written. They are listed in the PR body as follow-ups to decide:

  • a price currency that differs from the cost currency on a reduction;
  • {} beside cash in another currency.

@robcohen
robcohen merged commit 59d6a76 into main Oct 10, 2026
43 checks passed
@robcohen
robcohen deleted the chore/booking-fuzz-number-only-postings branch October 10, 2026 17:06
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.

Booking fuzzer never generates currency-less (number-only) postings

1 participant