Skip to content

feat(compat): semantic differential against beancount 3.2.3, gated in CI - #3071

Merged
robcohen merged 3 commits into
mainfrom
feat/semantic-differential
Nov 27, 2026
Merged

robcohen merged 3 commits into
mainfrom
feat/semantic-differential

Conversation

@robcohen

@robcohen robcohen commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

What

A full-pipeline semantic differential against beancount 3.2.3, the counterpart of the parse-error parity harness for everything after the parser: booking under every method, interpolation, prices, tolerances and rounding, pads and balance assertions, options and includes, plugins.

Both sides describe each ledger with a snapshot in pta-standards' format-generic ptadiff-snapshot/1 schema (errors; booked entries with interpolated, padding and plugin-made entries, each posting's units, resolved cost, price and flag, each transaction's inferred tolerances; final balances lot by lot; effective options). ptadiff (rustledger/pta-standards#77) canonicalizes, normalizes, compares, keys (field|path|kind|booking|features), minimizes and clusters.

  • crates/rustledger-loader/examples/semantic_snapshot.rs: rledger's side, ptadiff's JSON-lines protocol in semantic mode (multi-file inputs). Loads through Loader::load + process with check's options, and reads every field from its owner: parse_errors_to_ledger_errors, option_warnings_to_errors, merge_with_padding_spanned, CostSpec::resolve, transaction_tolerances (on pre_booking_directives), DisplayContext, and the realization below. No re-derivation.
  • rustledger_booking::account_balances (new, with RealizeError): the realization balances/balsheet/income print, moved out of the CLI (report_cmd::account_balances now calls it), so the snapshot cannot drift from the reports.
  • scripts/compat-semantic-parity.py + tests/baselines/semantic-parity.manifest: a pinned 10,000-ledger sample of ptadiff's seeded generators (lots and booking, pads/balances/tolerances, multi-file with options and plugins, built-in plugins) plus the compat corpus. Pins every category's and deviation's count and every mismatching ledger; --check fails on a regression or a moved pin, --update ratchets, --self-test runs first in CI.
  • scripts/semantic_parity_hooks.py: rledger's deliberate differences as data, each pointing at docs/reference/compatibility.md, counted apart from the mismatches: AVERAGE booking (feat(loader): add path traversal protection #20), plugins in included files (new chore(ci): increase Claude code review max turns #21), currency_accounts posting order (new, Plugin Differences), cross-file booking order (fix: replace unwrap calls with defensive pattern matching #7), custom amounts and display precision (test: add comprehensive integration tests for loader crate #5, extended); representation-only ones normalized (one E3001 per currency, ci: add merge_group trigger for GitHub Merge Queue #1; unpopped push, missing include and document root located nowhere, refactor: extract evaluate_function into category-based helpers #3; effective_date link suffix).
  • compat.yml: a Semantic parity job beside the parse-error one.

Results

10,730 ledgers (10,000 generated, 730 compat): 97.7% of the judged ledgers match (9,363 match, 217 mismatch, 1,106 deliberate deviations, 44 unjudged: third-party plugins beancount cannot import where CI runs it). 15 categories; the largest, minimized:

# Ledgers Category Minimized
1 173 options|display_precision {150 # 5 USD} sampled as one 0; a balance ~ tolerance sampled (fixed in a separate PR)
2 15 entries|extra-in-impl|close plugin close_tree + plugin auto_accounts + close Expenses:Rent (never opened)
3 8 entries|extra-in-impl|open plugin auto + a transaction booking refuses: rledger auto-opens its accounts
4 3 entries|postings.order|...|reduce STRICT total sale of two lots: postings by date, beancount by purchase (fixed in a separate PR)
5 8 entries|postings.order|...multi-currency (compat) beancount regroups postings by currency group when it interpolates

Fixes, one PR per root cause: #3072 (display precision sampling), #3073 (close_tree and a never-opened parent), #3074 (STRICT total-match posting order). With all three applied, the same inputs match at 99.5% (46 mismatches, 13 categories, no regression); each ratchets this manifest once it lands after this PR.

Open questions (not decided here)

  1. Synth-pass plugins see what booking later refuses, and run before regular plugins whatever the declared order. plugin "beancount.plugins.auto" + a transaction booking refuses: rledger auto-opens its accounts (and nounused then reports them), beancount's auto_accounts runs after booking over the surviving entries. plugin close_tree declared before plugin auto_accounts: beancount drops the never-opened close before anything opens it, rledger opens it first. Options: declare a deviation (the split pass is documented architecture); let auto_accounts skip transactions booking refuses; honour declaration order when a regular plugin precedes a synth one.
  2. Exact cost weights against beancount's 28-digit per-unit. A whole sale of 17.125 ACME {286.04 # 2.75 USD} with gains elided: beancount books 3527.25 (its rounded per-unit makes the tie .245000...001), rledger 3527.24 (exact .245, half-even). And a STRICT total sale of 4 VTI {125.51 USD, "lot1"} + 19.25 VTI {94.94 # 3.83 USD} @ 163.92 with cash 3811.14: rledger reports E3001 (residual -0.00500000000000000000000002, from the rounded per-unit times the units), beancount none (its product rounds back to the exact total; the exact residual is -0.005, within tolerance). Options: extend feat(core): implement string interning for performance #18 (weigh totals as written) to costs and carry the lot's exact total through a whole-lot reduction; round each weight to 28 significant digits in the precise residual, as Python's context does; or document.
  3. A cost spec reducing an uncosted holding. Holding 10 BTC without cost, -1 BTC {98.52 USD}: beancount No position matches; rledger books a short lot at cost beside the long (ReductionScope::CostBearingOnly, pinned as deliberate in core). Options: follow beancount (a reduction against every position when the posting has a cost spec), or document as a deviation.
  4. beancount regroups postings by currency group when it interpolates (compat: 28600.65 USD, 50 RHT, elided Equity:Opening-Balances books as USD, -USD, RHT, -RHT); rledger keeps the written order. Cosmetic. Options: mimic, or document.
  5. A misspelled beancount plugin path (beancount.plugin.unrealized) runs rledger's native plugin by its last segment, where beancount fails to import it. Options: require beancount.plugins. for that namespace, or document.
  6. Third-party zerosum: beancount's opens the matched account (Assets:ZeroSum-Matched:...), rledger's does not.
  7. Parse-recovery effects on meaning (a transaction with a syntax error in its header moves no inventory in beancount; open include "x" still includes x): the open parser PRs (fix(validate): a transaction beancount's grammar discards moves no inventory #3066) cover the first; the second belongs to the parse-error harness.

How to test

pip install beancount==3.2.3 "ptadiff @ git+https://github.com/rustledger/pta-standards@$(sed -n 's/^#@ptadiff-rev //p' tests/baselines/semantic-parity.manifest)#subdirectory=tooling/differential"
cargo build --release -p rustledger-loader --example semantic_snapshot
python -I scripts/compat-semantic-parity.py --self-test
python -I scripts/compat-semantic-parity.py --rledger target/release/examples/semantic_snapshot \
    --compat-dir tests/compatibility/files --check --minimize 10 --report report.md

#@ptadiff-rev pins rustledger/pta-standards#77's head; merge that first (the pinned commit is fetchable from its branch meanwhile).

🤖 Generated with Claude Code

The parse-error harness compares syntax errors; this compares what rledger
and beancount.loader.load_file make of a well-formed ledger after a FULL
load: booking under every method, interpolation, prices, tolerances and
rounding, pads and balance assertions, options and includes, plugins.

- crates/rustledger-loader/examples/semantic_snapshot.rs: rledger side of
  pta-standards ptadiff semantic protocol (ptadiff-snapshot/1). Loads
  through Loader::load and process as rledger check does, every field read
  from the function that owns it.
- rustledger_booking::account_balances (+ RealizeError): the realization
  the balance reports print, moved out of the CLI so the snapshot reads
  the same one; the CLI wrapper calls it.
- scripts/compat-semantic-parity.py + tests/baselines/semantic-parity.manifest:
  pinned 10k generated ledgers + the compat corpus; per-category and
  per-deviation counts and every mismatching ledger pinned; --check fails
  on regressions, --update ratchets.
- scripts/semantic_parity_hooks.py: rledger deliberate differences as
  data, each pointing at docs/reference/compatibility.md (two new
  sections: plugins in included files, currency_accounts posting order;
  #5 extended for custom amounts).
- compat.yml: Semantic parity job.

97.7% of judged ledgers match; the rest is clustered into 16 categories.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e, as CI runs it

A third-party plugin the oracle cannot import leaves a ledger unjudged; the
first pin came from a venv that had some installed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

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 828
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

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 52.9ms ± 1.0ms 820.1ms ± 8.9ms 15.5x 43.3 MB / 50.9 MB
investment 80.9ms ± 4.6ms 2956.9ms ± 35.4ms 36.5x 47.3 MB / 59.0 MB
fifo 77.0ms ± 1.0ms 14774.8ms ± 51.7ms 191.9x 49.3 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 52.4ms ± 0.3ms 52.9ms ± 1.0ms +1.0% ✅ within noise 43.4 MB → 43.3 MB (-0.2%)
investment 80.1ms ± 0.7ms 80.9ms ± 4.6ms +1.0% ✅ within noise 47.4 MB → 47.3 MB (-0.2%)
fifo 77.7ms ± 1.6ms 77.0ms ± 1.0ms -0.9% ✅ within noise 48.7 MB → 49.3 MB (+1.3%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.5ms, investment 2.1ms, fifo 0.9ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

…dards#77)

The tooling/differential tree is identical to the feature-branch commit
it replaces, so no count changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Nov 27, 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 7a96d8f into main Nov 27, 2026
73 of 74 checks passed
@robcohen
robcohen deleted the feat/semantic-differential branch November 27, 2026 16:52
robcohen added a commit that referenced this pull request Nov 27, 2026
… from beancount

Beancount booking regroups a transaction postings by the currency each
weighs in. rledger keeps the written order: compatibility.md #22, and the
semantic gate counts such a ledger as the currency-group-posting-order
deviation (only when regrouping reproduces beancount order exactly).

Maintainer decision on question 4 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
…s beancount does

auto_accounts (and the auto plugin) ran in the pre-booking synth pass,
ahead of every other plugin whatever the declared order, and over
transactions booking then refused. It opened the accounts of a refused
transaction and of an elided posting booking drops, where bean-check opens
neither, and a plugin declared before it saw its opens.

It is now a regular plugin, run at its declared place. Early validation
still judges posting accounts against the opens it will insert, through
rustledger_loader::early_validation_view: a copy of the stream with them,
computed by the plugin itself, that nothing downstream sees. The LSP reads
the same view. check --auto runs it after the file plugins, as
bean-check --auto does. Document discovery stays in the synth pass (beancount
runs it first; its documents carry no amounts).

WASM cache v157. Maintainer decision on question 1 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
…uct rounds

booked_cost_number decided whether a sale takes the lot exact total with
Decimal checked_mul, which rounds a product past 28 digits and returns it
anyway. A whole sale of 19.25 VTI {94.94 # 3.83 USD} looked exact, was
weighed at the exact product of the rounded per-unit cost, and a residual
of exactly -0.005 against a 0.005 tolerance became a spurious E3001. The
comparison is exact now.

Exact total-cost weights are kept (compatibility #18 now covers costs);
the one-cent difference an elided gain on an exact half-cent tie can show
is documented and registered as the total-cost-half-cent deviation in the
semantic gate, whose manifest is ratcheted.

WASM cache v158. Maintainer decision on question 2 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… from beancount

Beancount booking regroups a transaction postings by the currency each
weighs in. rledger keeps the written order: compatibility.md #22, and the
semantic gate counts such a ledger as the currency-group-posting-order
deviation (only when regrouping reproduces beancount order exactly).

Maintainer decision on question 4 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… date

The Python plugin dates the opens of the accounts it moves matched postings
to at entries[0].date (usually an open or commodity); rledger dated them at
the first transaction, later. Verified black-box against
beancount_reds_plugins under bean-check 3.2.3.

WASM cache v159. Maintainer decision on question 6 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… lot matches, as beancount does

Inventory::is_booking_reduction counted only lots held at cost (#875), so
holding 10 BTC bought without a cost, -1 BTC {98.52 USD} opened a short
lot beside the long, and a purchase at a cost after a sale without a cost
spec was an augmentation. bean-check 3.2.3 refuses both (No position
matches); the gate now considers every opposite-sign position.

BREAKING CHANGE: such postings are booking failures now.

WASM cache v160. Maintainer decision on question 3 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
…s beancount does (#3075)

* fix(loader): run auto_accounts where it is declared, after booking, as beancount does

auto_accounts (and the auto plugin) ran in the pre-booking synth pass,
ahead of every other plugin whatever the declared order, and over
transactions booking then refused. It opened the accounts of a refused
transaction and of an elided posting booking drops, where bean-check opens
neither, and a plugin declared before it saw its opens.

It is now a regular plugin, run at its declared place. Early validation
still judges posting accounts against the opens it will insert, through
rustledger_loader::early_validation_view: a copy of the stream with them,
computed by the plugin itself, that nothing downstream sees. The LSP reads
the same view. check --auto runs it after the file plugins, as
bean-check --auto does. Document discovery stays in the synth pass (beancount
runs it first; its documents carry no amounts).

WASM cache v157. Maintainer decision on question 1 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(compat): ratchet the semantic parity manifest

With close_tree (#3073) on main, auto_accounts at its declared place clears
both entries|extra-in-impl|open (8) and entries|extra-in-impl|close (12):
99.50% -> 99.74% of judged ledgers match, no regression.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
A plugin reference was matched to a native plugin by its last dotted
segment, so beancount.plugin.unrealized (which beancount fails to import)
or anything.zerosum silently ran the native plugin. A native plugin is now
named by its bare name, by beancount.plugins.<name>, or by the exact path
of the third-party module it replaces (THIRD_PARTY_MODULES); anything else
resolves as any other module reference. native_plugin_name is the rule.

BREAKING CHANGE: a misspelled or otherwise inexact plugin path no longer
runs a native plugin.

Surface parity: the LSP E8006 note for the two lima fixtures that use the
misspelled path is allowlisted with the existing plugin entries. Semantic
gate ratcheted (two compat files now match). WASM cache v161.
Maintainer decision on question 5 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… from beancount

Beancount booking regroups a transaction postings by the currency each
weighs in. rledger keeps the written order: compatibility.md #22, and the
semantic gate counts such a ledger as the currency-group-posting-order
deviation (only when regrouping reproduces beancount order exactly).

Maintainer decision on question 4 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… date

The Python plugin dates the opens of the accounts it moves matched postings
to at entries[0].date (usually an open or commodity); rledger dated them at
the first transaction, later. Verified black-box against
beancount_reds_plugins under bean-check 3.2.3.

WASM cache v159. Maintainer decision on question 6 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… lot matches, as beancount does

Inventory::is_booking_reduction counted only lots held at cost (#875), so
holding 10 BTC bought without a cost, -1 BTC {98.52 USD} opened a short
lot beside the long, and a purchase at a cost after a sale without a cost
spec was an augmentation. bean-check 3.2.3 refuses both (No position
matches); the gate now considers every opposite-sign position.

BREAKING CHANGE: such postings are booking failures now.

WASM cache v160. Maintainer decision on question 3 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
A plugin reference was matched to a native plugin by its last dotted
segment, so beancount.plugin.unrealized (which beancount fails to import)
or anything.zerosum silently ran the native plugin. A native plugin is now
named by its bare name, by beancount.plugins.<name>, or by the exact path
of the third-party module it replaces (THIRD_PARTY_MODULES); anything else
resolves as any other module reference. native_plugin_name is the rule.

BREAKING CHANGE: a misspelled or otherwise inexact plugin path no longer
runs a native plugin.

Surface parity: the LSP E8006 note for the two lima fixtures that use the
misspelled path is allowlisted with the existing plugin entries. Semantic
gate ratcheted (two compat files now match). WASM cache v161.
Maintainer decision on question 5 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
…uct rounds

booked_cost_number decided whether a sale takes the lot exact total with
Decimal checked_mul, which rounds a product past 28 digits and returns it
anyway. A whole sale of 19.25 VTI {94.94 # 3.83 USD} looked exact, was
weighed at the exact product of the rounded per-unit cost, and a residual
of exactly -0.005 against a 0.005 tolerance became a spurious E3001. The
comparison is exact now.

Exact total-cost weights are kept (compatibility #18 now covers costs);
the one-cent difference an elided gain on an exact half-cent tie can show
is documented and registered as the total-cost-half-cent deviation in the
semantic gate, whose manifest is ratcheted.

WASM cache v158. Maintainer decision on question 2 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… from beancount

Beancount booking regroups a transaction postings by the currency each
weighs in. rledger keeps the written order: compatibility.md #22, and the
semantic gate counts such a ledger as the currency-group-posting-order
deviation (only when regrouping reproduces beancount order exactly).

Maintainer decision on question 4 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… date

The Python plugin dates the opens of the accounts it moves matched postings
to at entries[0].date (usually an open or commodity); rledger dated them at
the first transaction, later. Verified black-box against
beancount_reds_plugins under bean-check 3.2.3.

WASM cache v159. Maintainer decision on question 6 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
… lot matches, as beancount does

Inventory::is_booking_reduction counted only lots held at cost (#875), so
holding 10 BTC bought without a cost, -1 BTC {98.52 USD} opened a short
lot beside the long, and a purchase at a cost after a sale without a cost
spec was an augmentation. bean-check 3.2.3 refuses both (No position
matches); the gate now considers every opposite-sign position.

BREAKING CHANGE: such postings are booking failures now.

WASM cache v160. Maintainer decision on question 3 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
A plugin reference was matched to a native plugin by its last dotted
segment, so beancount.plugin.unrealized (which beancount fails to import)
or anything.zerosum silently ran the native plugin. A native plugin is now
named by its bare name, by beancount.plugins.<name>, or by the exact path
of the third-party module it replaces (THIRD_PARTY_MODULES); anything else
resolves as any other module reference. native_plugin_name is the rule.

BREAKING CHANGE: a misspelled or otherwise inexact plugin path no longer
runs a native plugin.

Surface parity: the LSP E8006 note for the two lima fixtures that use the
misspelled path is allowlisted with the existing plugin entries. Semantic
gate ratcheted (two compat files now match). WASM cache v161.
Maintainer decision on question 5 of #3071.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant