Skip to content

feat(plugin): let the host set the WASM plugin and importer time budget - #2485

Merged
robcohen merged 4 commits into
mainfrom
feat/plugin-time-budget
Oct 7, 2026
Merged

robcohen merged 4 commits into
mainfrom
feat/plugin-time-budget

Conversation

@robcohen

@robcohen robcohen commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Summary

Lets whoever runs rustledger set the CPU budget for WASM plugins and importers. Until now every call got a fixed 30 seconds, and no user setting reached it.

# ~/.config/rledger/config.toml (or .rledger.toml, /etc/rledger/config.toml)
[plugins]
max_time_secs = 120
rledger --plugin-max-time-secs 120 check ledger.beancount   # overrides the config for one run

It applies to:

  • plugin directives in check, query, report and lint;
  • check --plugin;
  • extract's WASM importers.

Defaults are unchanged.

Host-side only, deliberately

The budget is never set from the ledger. A ledger that could raise its own plugins' budget would let its author spend unbounded CPU on any service that loads it: hosted rustfava, a server, CI checking contributed ledgers. A ledger-level setting would also need syntax beancount rejects (#2484 has the reasoning).

Type of change

  • Feature, with a small breaking change for embedders (new LoadOptions field)

Changes

  • Config (config.rs): new [plugins] section, PluginsConfig { max_time_secs }.
    • It merges like [price], so project config overrides user, which overrides system.
    • It uses deny_unknown_fields, so a misspelled key is an error, not a silent 30 s.
    • Zero is refused in both the config (NonZeroU64) and the flag (range(1..)). The sandbox floors budgets at 1 s, so 0 would otherwise quietly mean one second.
  • CLI (rledger.rs, plugin_budget.rs):
    • The global --plugin-max-time-secs overrides the config.
    • rledger sets the value once at startup in rustledger::plugin_budget, and every command reads it from there.
    • ag-rledger takes it from the config file.
  • Loader: LoadOptions::plugin_max_time_secs: Option<u64>.
  • Dispatch: new ResolvedPlugin::run_with_max_time_secs; run delegates with None.
  • Importers:
  • Docs: configuration (new "Plugin Time Budget" section), check, importing, custom-plugins.
  • CHANGELOG: entry under Breaking.

Not covered: Python plugins keep PYTHON_FUEL, which still needs re-measuring, and the LSP never runs WASM plugins.

Testing

The tests use a plugin (and an importer) that burns 3–4G fuel, measured by bracketing the budget. That is within the default 30 s and trapped at 1 s, independent of host speed.

  • dispatch::tests::wasm_plugin_runs_under_the_host_time_budget: default, 1 s and 60 s.
  • wasm::tests::registry_loads_importers_under_the_host_time_budget: both registry methods.
  • tests/plugin_time_budget.rs, through the binary:
    • the default;
    • the flag;
    • the config file;
    • the flag overriding the config;
    • the setting reaching query.
  • Config parse/merge, unknown-key rejection, and zero rejection, in both the config and the flag.
  • Each test was shown to fail when its wiring is removed or the precedence reversed.
  • cargo test across plugin, importer, loader and CLI: 1758 passed, 0 failed.
  • cargo clippy --workspace --all-features --all-targets -D warnings is clean, as are the feature-gated builds of the plugin, loader, importer and CLI crates.
  • cargo doc -D warnings is clean.

Closes #2484

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Every WASM plugin and importer call got a fixed 30-second budget, and
no user setting reached it: the CLI, the loader's `plugin` directives
and `importers.toml` all used the defaults.

- rledger config: `[plugins] max_time_secs`, merged across the system,
  user and project files like every other section. A misspelled key is
  an error, not a silent fall back to 30 seconds.
- A global `--plugin-max-time-secs <SECS>` overrides it for one run.
  `rledger` sets it once at startup (`rustledger::plugin_budget`), so
  every command reads the same value: `plugin` directives in check,
  query, report and lint, `check --plugin`, and extract's importers.
  ag-rledger takes it from the config file.
- Library: `LoadOptions::plugin_max_time_secs` (None keeps the
  default) reaches dispatch through the new
  `ResolvedPlugin::run_with_max_time_secs`; `ImporterRegistry` gained
  `register_wasm_from_path_with_config` and
  `register_wasm_dir_with_config`. Existing methods are unchanged.
- `WasmImporterError::Runtime` prints its cause chain, so an importer's
  fuel trap says `all fuel consumed by WebAssembly`.

The budget is the host's, never the ledger's: a ledger that could
raise it would let its author spend unbounded CPU on any service that
loads it. Python plugins keep their own fixed budget.

Tests run a plugin (and an importer) that burns 3-4G fuel: within the
default 30 s, trapped at 1 s. They cover dispatch, the importer
registry, and the binary (flag, config file, flag over config, and
`query`). Each was shown to fail with the wiring removed.

Closes #2484

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 7, 2026 17:54
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 30.8ms ± 4.0ms 449.1ms ± 7.4ms 14.6x 51.0 MB / 49.9 MB
investment 45.2ms ± 0.6ms 1664.4ms ± 65.6ms 36.8x 55.2 MB / 58.0 MB
fifo 44.9ms ± 3.5ms 8210.5ms ± 28.3ms 182.8x 55.3 MB / 58.2 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 30.0ms ± 0.9ms 30.8ms ± 4.0ms +2.8% ✅ within noise 51.1 MB → 51.0 MB (-0.0%)
investment 45.4ms ± 1.5ms 45.2ms ± 0.6ms -0.4% ✅ within noise 55.3 MB → 55.2 MB (-0.1%)
fifo 43.4ms ± 0.7ms 44.9ms ± 3.5ms +3.5% ✅ within noise 55.3 MB → 55.3 MB (-0.1%)

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

…econd

The sandbox floors a budget at one second, so `max_time_secs = 0` or
`--plugin-max-time-secs 0` quietly meant one second, where many tools
read zero as "no limit". The config field is now `NonZeroU64`, so the
file fails to parse and names itself; the flag takes `1..`.

Also drops an intra-doc link to `crate::sandbox` from
`run_with_max_time_secs`, which broke rustdoc without `wasm-runtime`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen

robcohen commented Oct 7, 2026 •

Copy link
Copy Markdown
Member Author

Deep review

Coverage of the setting. I checked every way a command can reach a WASM plugin or importer:

  • Each rustledger_loader::load/process call in the CLI is covered. check, query, report and lint set plugin_max_time_secs. price and extract's duplicate and ledger-profile loads run no plugins.
  • check --plugin uses the setting through PluginManager::with_config, and extract --wasm-importer/--wasm-importer-dir through the _with_config registry methods.
  • In ag-rledger, every plugin-running command calls load_config() first. ag-rledger lint offers only transfers, which loads no plugins.
  • The LSP never runs WASM plugins (E8006), and Python plugins keep PYTHON_FUEL. Both are documented.

Fixed in 9238302.

  • A zero budget was silently one second. The sandbox floors budgets with max(1), so --plugin-max-time-secs 0 trapped the test plugin at 1 s, while many tools read 0 as "no limit". The config field is now NonZeroU64, so the file fails to parse and names itself, and the flag uses range(1..). There's a test for each.
  • Broken doc link without the feature. run_with_max_time_secs's doc linked crate::sandbox, which breaks rustdoc without wasm-runtime. CI only builds docs with --all-features. The same file's crate::python link has the same problem on main; I left it alone.

Checked and fine:

  • Burn size. I bracketed the test plugin's real fuel by varying the budget. It traps at 3 s and passes at 4 s, so it burns 3–4G fuel: within the default 30 s and trapped at 1 s, with margin on both sides.
  • The tests can fail. Each was shown to fail when its wiring is removed or the precedence reversed.
  • Huge values. u64::MAX saturates; the existing saturation tests cover it.
  • Feature-gated builds. Clippy is clean for plugin (none, wasm-runtime, python-plugins), loader, importer and CLI with no default features. The only failures there are older integration tests that need features.
  • Totals. 1758 tests pass across plugin, importer, loader and CLI.

Second pass

Fixed in a747b94. The trust boundary is "not the beancount file", not "not the ledger's repository". A project .rledger.toml is found from the working directory, so running rledger check inside someone else's repository applies their max_time_secs. I confirmed it: with cwd in the ledger's directory, a .rledger.toml there set the budget, and from another directory the same ledger didn't. That's the same trust project config already gets for aliases and the default file, so I didn't gate it. The configuration docs now say where the boundary is and to run from a directory you control.

Checked and fine: config show prints the [plugins] section, config init writes the commented template, and profiles have no plugin settings, as intended.

Third pass (independent reviewer + me)

Fixed in c272351:

  1. The flag broke alias expansion when its value was a separate argument. rledger --plugin-max-time-secs 120 ck … failed with unrecognized subcommand 'ck': expand_aliases knew only -P/--profile take a value, so it read 120 as the command. Value-taking global flags are now one list, GLOBAL_VALUE_FLAGS. I reproduced it, added a unit test that fails without the fix, and checked it end to end.
  2. "Seconds of CPU" overstated the budget 5–20×. Fuel is converted at a deliberately low 1G/s, so a call never outlasts its budget and usually runs out much sooner: the test loop runs at about 20G fuel/s. The flag help, config template and docs now describe it as a time budget and an upper bound.
  3. Two "no trap" tests could pass vacuously. They now also assert the plugin ran to completion, via its empty output's reading marker decode error.
  4. Semver. Config gained a pub plugins field and isn't #[non_exhaustive]; the CHANGELOG now names it with LoadOptions. The new PluginsConfig is #[non_exhaustive].
  5. ag-rledger scope. It has no --plugin-max-time-secs (unknown flags are accepted silently), and load_config ignores a config that fails to load, so a typo or 0 there falls back to 30 s. The CHANGELOG now scopes the flag and the misspelled-key/zero errors to rledger. Release builds of ag-rledger have no WASM, so this only affects builds with default features.

Moved to #2486: the LSP point from this pass was confirmed in pass 4. Details below.

1759 tests pass across plugin, importer, loader and CLI, and workspace clippy is clean.

Fourth pass

Confirmed and filed as #2486 (older than this PR, out of scope). When the LSP is built together with the CLI, its loader gets wasm-plugins and python-plugins (cargo tree -p rustledger-lsp -p rustledger -i rustledger-loader -e features). That's a plain cargo build/cargo install, since both are default-members, and the flake's rustledger package. Those builds of the LSP run WASM and Python plugins that its E8006 diagnostic says it skips, and #2485's budget can't reach them. Release builds the LSP alone. The issue proposes a "native plugins only" mode for the LSP.

Checked and fine: rledger --help and check --help show the flag with the bound wording; doc links resolve to #plugin-time-budget; no {{ (VitePress) in the touched docs. CI had no failures on c272351 at the time of writing.

No open findings in this PR.

The project config is found from the working directory, so running
rledger inside someone else's repository applies their
`max_time_secs` (as it already applies their aliases and default
file). Nothing in a beancount file can raise it; the docs now say
where the boundary actually is.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.21531% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-plugin/src/dispatch.rs 89.13% 5 Missing ⚠️
crates/rustledger/src/cmd/extract_cmd/mod.rs 70.00% 3 Missing ⚠️
crates/rustledger-importer/src/wasm.rs 97.43% 1 Missing ⚠️
crates/rustledger/src/cmd/check.rs 83.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

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

… is a bound

- Alias expansion took the first non-flag argument as the command and
  knew only `-P`/`--profile` take a value, so
  `rledger --plugin-max-time-secs 120 <alias>` read `120` as the
  command and clap rejected the alias. Value-taking global flags are
  now one list (`GLOBAL_VALUE_FLAGS`), with a test.
- "Seconds of CPU" overstated the budget 5-20x: fuel is converted at a
  deliberately low 1G/s, so a call never outlasts its budget and most
  code runs out much sooner. The flag help, config template and docs
  now call it a time budget and an upper bound.
- The no-trap tests assert the plugin ran to completion (its empty
  output's decode error), not only that it did not trap.
- `PluginsConfig` is `#[non_exhaustive]`; the CHANGELOG names
  `Config`'s new `plugins` field as breaking for struct literals, and
  scopes the flag and the misspelled-key/zero errors to `rledger`
  (`ag-rledger` has no flag and ignores a config that fails to load).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
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.

Plugins and importers: let the host configure the sandbox time budget

1 participant