Skip to content

fix(loader): merge operating_currency across includes in beancount's breadth-first order, each currency once - #3041

Merged
robcohen merged 2 commits into
mainfrom
fix/operating-currency-order
Nov 26, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/operating-currency-order

Conversation

@robcohen

Copy link
Copy Markdown
Member

What changed

beancount 3.2.3 parses included files breadth first and merges option "operating_currency" in that order, dropping repeats (loader.aggregate_options_map → misc_utils.uniquify, first occurrence wins, the top-level file's own repeats included). rledger merged depth first and kept repeats.

Tree bean-check 3.2.3 rledger before rledger now
main MMM → x XXX (→ z ZZZ), y YYY [MMM, XXX, YYY, ZZZ] [MMM, XXX, ZZZ, YYY] same as beancount
main USD, USD, include a (USD, GBP, EUR), EUR [USD, EUR, GBP] [USD, USD, EUR, USD, GBP, EUR] same as beancount
duplicate include (c includes b, which main also includes) [AAA, BBB, CCC] [AAA, CCC, BBB] same as beancount

Audit of the other options merged across files

  • display_precision also differed. beancount merges each file's display context into the top-level file's with DisplayContext::update_from, and that keeps a fixed precision, so the top-level file's value wins. rledger applied values last-wins in depth-first order, so an include's USD:0.001 overrode the main file's USD:0.01: 1.23456 USD rendered as 1.235 USD, where bean-query gives 1.23 USD. This is fixed. For a currency that only included files fix, beancount 3.2.3 crashes inside that merge (_FixedPrecisionContext has no fractional_dist). rledger takes the value from the first file in parse order.
  • insert_pythonpath: beancount turns it into a sorted set of directories, so order cannot matter, and the option has no effect in rledger. Unchanged.
  • inferred_tolerance_default, documents, and every other option are taken from the top-level file only, in both beancount and rledger. Unchanged.
  • Plugins in included files: beancount ignores them (options_map["plugin"] comes from the top file only; verified with bean-check). rledger runs them on purpose, so beancount gives no reference order. Their order (file id, depth first) is unchanged and still matches the LSP's view_plugins.

How

How to test

cargo test -p rustledger-loader --test merged_options_test
cargo test -p rustledger-loader --lib merge_across_files
cargo test -p rustledger-wasm --all-features options_merged_across_files
cargo test -p rustledger-lsp --all-features merges_options_across_files
cargo test -p rustledger --test check_parse_cache

The drift guards cover the loader, the WASM Ledger (fresh and restored from cache), ffi-wasi load_file_with_fs (the component's options), the LSP LedgerState, and the CLI both cold and on a cache hit (the report returns default currency and query precision). I checked every expected value against beancount.loader.load_file / bean-check 3.2.3.

Closes #3038

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 26, 2026 04:48
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 53.9ms ± 1.5ms 827.6ms ± 21.6ms 15.3x 43.4 MB / 51.0 MB
investment 80.4ms ± 1.0ms 2927.1ms ± 31.6ms 36.4x 47.3 MB / 59.2 MB
fifo 76.8ms ± 0.6ms 14777.4ms ± 262.0ms 192.3x 49.5 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 53.2ms ± 0.9ms 53.9ms ± 1.5ms +1.4% ✅ within noise 43.1 MB → 43.4 MB (+0.7%)
investment 79.4ms ± 0.5ms 80.4ms ± 1.0ms +1.3% ✅ within noise 46.4 MB → 47.3 MB (+2.0%)
fifo 75.8ms ± 0.5ms 76.8ms ± 0.6ms +1.4% ✅ within noise 49.3 MB → 49.5 MB (+0.3%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.8ms, investment 0.5ms, fifo 0.4ms), 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

@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

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.63014% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/options.rs 98.21% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

robcohen and others added 2 commits November 25, 2026 23:50
…breadth-first order, each currency once (#3038)

beancount 3.2.3 parses its files breadth first and `aggregate_options_map`
merges `operating_currency` in that order, `uniquify`d (first occurrence
wins, the top-level file's own repeats included). rledger merged it depth
first and kept repeats: `main` (MMM) including `x` (XXX, including `z`
ZZZ) then `y` (YYY) gave [MMM, XXX, ZZZ, YYY] against [MMM, XXX, YYY, ZZZ],
and USD in main and an include gave [USD, USD, EUR] against [USD, EUR].

Auditing the other options beancount aggregates across files:
`display_precision` is merged into the top-level file's display context
with `DisplayContext::update_from`, which keeps a fixed precision, so the
top-level file's value wins; rledger let the last file read win (an
include's USD:0.001 rendered 1.23456 USD as 1.235 where bean-query gives
1.23). `insert_pythonpath` becomes a sorted set there (no order) and has
no effect in rledger. Included plugins are ignored by beancount and run by
rledger deliberately; their order is unchanged.

- `Options::set_in_file_at` records each merged option's values with its
  file; `Options::merge_across_files(ranks)` is the one merge, called by
  `Loader::load` over `SourceMap::parse_ranks` before the display context
  and the cache see the options. Every surface (CLI, LSP, WASM, FFI) reads
  the loader's options or the cache's merged copy.
- `apply` lists a repeated operating currency once, for options set
  without the loader.
- Loader CACHE_VERSION 77 -> 78 (cached values change), WASM pin 78, WASM
  CACHE_VERSION 137 (135, 136 taken by #3039, #3040).
- Tests: unit tests for the merge; loader tests for the issue's trees, a
  duplicate include, a disk glob and a cache round trip, all checked
  against bean-check 3.2.3; drift guards across the loader, WASM `Ledger`
  (and restored), ffi-wasi, the LSP, and the CLI cold and on a cache hit
  (`report returns` currency, `query` precision).

Closes #3038

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/operating-currency-order branch from 49c6bd2 to adcf5ad Compare November 26, 2026 05:52
@robcohen
robcohen merged commit 3b2e811 into main Nov 26, 2026
67 of 68 checks passed
@robcohen
robcohen deleted the fix/operating-currency-order branch November 26, 2026 06:28
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.

compat(loader): merge operating_currency across includes in beancount's breadth-first order, without duplicates

1 participant