Skip to content

fix(loader): run a plugin declared in more than one file once, from its first file in breadth-first include order - #3042

Merged
robcohen merged 1 commit into
mainfrom
fix/dedup-repeated-plugins
Nov 26, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/dedup-repeated-plugins

Conversation

@robcohen

@robcohen robcohen commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

What

When the same plugin is declared in more than one file of a ledger (the main file and its includes), rledger now runs each (name, config) pair once, from the first file that declares it in beancount's breadth-first include order (SourceMap::parse_ranks, #3037). The same name with a different config is a different plugin and still runs.

Why

bean-check 3.2.3 ignores plugin directives in included files. rledger runs them on purpose (so a sub-ledger that declares auto_accounts for itself still works when included), and that is kept. But a plugin declared in both main and an include ran once per declaration. That is harmless for idempotent plugins and wrong for plugins that add entries: implicit_prices added two identical prices, and check_commodity reported each error twice.

How

  • One canonical function: rustledger_loader::plugins_in_run_order(plugins, source_map). It sorts the declarations by breadth-first file rank (each file's own declarations stay in order) and runs each pair only from its first file. The deviations from Python are documented there and at the dispatch site, per the Python Compatibility Policy.
  • run_plugins applies it. So every surface runs the same list: rledger check and everything that calls process, the WASM Ledger, ffi-wasi and the FFI component, and the LSP (which calls run_plugins on a view's declarations). The FFI's reported plugins list and the split/merge equivalence fingerprint also use it.
  • Declaration lists are unchanged. LoadResult::plugins and Ledger::plugins still hold every declaration in file-id order. The LSP's view_plugins needs the full list: a buffer can delete the first of two declarations, and the second must then run.
  • Nested includes: plugins from nested includes now also run in breadth-first order. Example: main includes x then y, and x includes z. y's plugins now run before z's; they used to run after. This matters for chained rename_accounts.

Decision taken: a repeat within one file still runs as written

The rule dedupes across files, not inside a file. beancount runs every declaration in the top-level file, including a repeat, and plugin_conformance::python_plugins_back_to_back_after_a_timeout relies on that (it declares nap.py twice in one file). A stricter "once everywhere" rule would make single-file ledgers differ from bean-check. If the maintainer wants a repeat inside one file collapsed too, it is a one-line change in plugins_in_run_order, plus that test.

Cache

  • The loader cache restored every plugin with file_id: 0, so a cache hit would have treated a repeat from an include as a repeat within one file and run it twice. The CLI test caught this. CachedPlugin gains file_id: u16 (this is a breaking change to a public struct; see CHANGELOG). Loader CACHE_VERSION goes from 78 to 79, and the WASM pin moves to 79.
  • WASM CACHE_VERSION goes from 137 to 138, because the Ledger payload stores plugin output.

Tests

  • Loader unit test for plugins_in_run_order: breadth-first order, first file wins, repeat within one file kept, a different config or the python: prefix counts as a different plugin.
  • crates/rustledger-loader/tests/repeated_plugins_test.rs, using non-idempotent plugins (implicit_prices, check_commodity). Cases: main + include, nested and sibling includes, plugin declared only in an include, different config, repeat within one file, and breadth-first order shown with chained rename_accounts.
  • Drift guards across surfaces:
    • WASM: process, the multi-file Ledger and the restored Ledger, and ffi-wasi load_file_with_fs (directives, errors and the reported plugin list).
    • LSP: all_diagnostics with each file open, and ledger_diagnostics_multi with every file in the view.
    • CLI: rledger check cold and on a cache hit.
  • The new tests fail with the old dispatch (checked).

How to test locally: cargo test --all-features -p rustledger-loader -p rustledger-wasm -p rustledger-lsp -p rustledger -p rustledger-ffi-wasi

🤖 Generated with Claude Code

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

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 34.0ms ± 1.1ms 446.3ms ± 16.2ms 13.1x 43.3 MB / 51.0 MB
investment 50.9ms ± 1.2ms 1552.4ms ± 2.6ms 30.5x 47.3 MB / 59.0 MB
fifo 48.8ms ± 1.4ms 8061.2ms ± 161.8ms 165.1x 49.2 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 33.1ms ± 1.3ms 34.0ms ± 1.1ms +2.7% ✅ within noise 43.2 MB → 43.3 MB (+0.2%)
investment 50.2ms ± 0.8ms 50.9ms ± 1.2ms +1.4% ✅ within noise 47.3 MB → 47.3 MB (+0.1%)
fifo 49.5ms ± 0.6ms 48.8ms ± 1.4ms -1.3% ✅ within noise 49.6 MB → 49.2 MB (-0.7%)

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

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…ts first file in breadth-first include order

rledger runs the plugins of included files (bean-check 3.2.3 ignores them),
so a plugin declared in the main file and again in an include ran once per
declaration: a plugin that adds entries (implicit_prices) added them twice,
and check_commodity reported each error twice.

- New canonical `rustledger_loader::plugins_in_run_order`: the declarations
  in beancount's breadth-first file order (`SourceMap::parse_ranks`), each
  distinct (name as written, config) pair run from the first file that
  declares it only. A repeat within that one file still runs as written, as
  beancount runs a top-level file's repeat, so a single-file ledger runs
  what bean-check runs. Both deviations are documented there and at the
  dispatch site.
- `run_plugins` applies it, so `rledger check`/`process`, the WASM `Ledger`,
  ffi-wasi/component and the LSP (which runs a view's declarations) all
  share it; the FFI's reported plugin list and the split/merge equivalence
  fingerprint use it too. `LoadResult::plugins` / `Ledger::plugins` keep
  every declaration, as the LSP's view rebuilding needs them.
- The plugins of nested includes now run breadth first as well.
- The loader cache restored every plugin as file 0, so a hit would have run
  an included repeat; `CachedPlugin` gains `file_id` (loader CACHE_VERSION
  78 -> 79), WASM pin 79 and WASM CACHE_VERSION 137 -> 138 (the `Ledger`
  archives plugin output).
- Tests: loader unit + integration (non-idempotent implicit_prices and
  check_commodity, nested includes, different config, one-file repeat,
  breadth-first rename order), drift guards across process, WASM `Ledger`
  (and restored), ffi-wasi, the LSP (current-file and multi-file paths), and
  the CLI cold and on a cache hit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/dedup-repeated-plugins branch from 7de2417 to 98c150d Compare November 26, 2026 06:33
@robcohen
robcohen merged commit 5081337 into main Nov 26, 2026
69 of 70 checks passed
@robcohen
robcohen deleted the fix/dedup-repeated-plugins branch November 26, 2026 14:51
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