Skip to content

fix(loader)!: keep an option's previous value when its new value is rejected, and refuse queries over a rejected booking_method - #2923

Merged
robcohen merged 9 commits into
mainfrom
fix/invalid-option-keeps-default
Nov 10, 2026
Merged

robcohen merged 9 commits into
mainfrom
fix/invalid-option-keeps-default

Conversation

@robcohen

@robcohen robcohen commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

What changed

An option value that fails its check is now reported and not applied. The option keeps its default or an earlier valid setting, as in beancount 3.2.3, whose grammar.option returns on the converter's ValueError without assigning the option.

  • Options::set_inner applies each value through Options::apply, which returns whether the value was accepted. Every validating arm rejects through one helper, Options::reject, which reports the error and leaves the field alone. This covers:
    • all five name_* roots (E7008, via check_root);
    • all seven account_* leaves (E7002, via check_leaf): account_previous_balances, account_previous_earnings, account_previous_conversions, account_current_earnings, account_current_conversions, account_unrealized_gains and account_rounding;
    • booking_method;
    • plugin_processing_mode;
    • the numeric and map options that already kept their value (tolerance_multiplier, inferred_tolerance_default, display_precision, long_string_maxlines).
  • The account checks use fix(parser): lex an account component with any non-ASCII start, as beancount does #2908's strict validator (is_valid_account_name_strict), merged from main.
  • A rejected value is not recorded in set_options. As a result it raises no E7003 "last value wins" warning, which would be false because the rejected value did not win. It raises no E7010 either, and it cannot gate effective_booking_method.
  • An invalid account_rounding is only the E7002. It no longer also raises the E7007 no-effect notice for a value it did not take.
  • Breaking: booking_method is checked case-sensitively, as beancount's Booking[value] is and as an open directive's booking method already is (P0025). fifo is E7002 and keeps STRICT; rledger used to book it as FIFO silently. Documented in docs/reference/options.md.
  • Nothing reads a raw option value any more:
    • the WASM account_types_from_raw and extract_options now read through rustledger_loader::Options;
    • the FFI component's booking_method_from resolves through Options::effective_booking_method;
    • the FFI's ledger-options.booking-method is that effective method.

Invalid booking_method refuses query and report

When the last booking_method value is rejected, the option keeps its previous method. If that is not the method the ledger asks for, every lot reduction would be booked with a method the ledger did not ask for, and rledger query and rledger report now refuse instead of printing those figures. They exit 1, the existing refusal's code, with:

main.beancount: option "booking_method" at main.beancount:1: "fifo" is not a booking method (E7002); refusing to derive figures, which would be booked with STRICT. Run `rledger check main.beancount` to see it.

check is unchanged: it reports the E7002 and the STRICT booking's findings, as bean-check does.

This is a deliberate Python deviation, commented at Options::query_refusal. bean-query answers with the kept method's figures; we refuse because they would silently use a booking method the user did not ask for. Only booking_method triggers this refusal; any other rejected option keeps its default and answers.

The refusal is part of the one query rule (#2861, #2881):

  • Options records the rejected booking_method with its site. The loader now passes the site through Options::set_scoped_at.
  • Options::query_refusal(booked_with) exposes it, only when the ledger is booked with a method other than the one the last value asks for (see Review round 2).
  • Ledger::blocks_queries is the whole rule: a blocking LedgerError, or that refusal.
  • Ledger::query_refusal returns the refusal as a LedgerError for which LedgerError::blocks_queries holds, for surfaces that list only ledger errors.

Detection reads the rejected option itself, never message text.

Surfaces that go through it:

  • CLI query and report: via bail_on_booking_errors, the gate both share.
  • WASM query, queryMultiFile, Ledger, ParsedLedger: via load_and_book / Ledger::blocks_queries. queryMultiFile and queryWithHost append the refusal reason; query(source), Ledger and ParsedLedger list the plain option E7002.
  • MCP server: through WASM query and ParsedLedger.
  • FFI component (query, batch, query-file, batch-file, sessions): ffi-wasi's loads carry the refusal in a separate query_refusal field, which LoadResult::blocks_queries and the session's query_blocked read. It is listed on query results only, never among the load's errors, so load, validate and info() are unchanged and the session plugins still run. The batch refusal text names it, and a refused batch lists the reason beside it.

The shared query_answer_rule cases gain seven cases, so every surface's drift guard pins them (loader, CLI, FFI and WASM):

  • an invalid booking_method (refuses);
  • a valid FIFO (answers);
  • a bad account_rounding (answers);
  • fifo then FIFO (answers);
  • FIFO then fifo (answers);
  • FIFO then bogus (refuses);
  • bogus then FIFO (answers; only the clearing by the later accepted value lets it).

Mutation checks:

  • Initial: making Options::query_refusal return None fails the loader rule test, the CLI rule test and both new CLI refusal tests.
  • Round 2: the shared cases make the same mutation fail the FFI and WASM rule tests too.
  • Round 3: deleting the clearing self.rejected_booking_method = None; in the booking_method arm passed every test before round 3. It now fails the loader unit test and the shared rule tests on every surface (see Review round 3).

No BQL compat corpus file has an invalid booking_method: the fetched corpus holds only FIFO, NONE and STRICT. So nothing is registered under KNOWN_RUST_DIVERGENCES.

Review round 2

  • The refusal fires only when the booking differs from the request. Before, the first rejection was recorded and never cleared, so "fifo" then "FIFO" booked FIFO yet refused "with FIFO rather than the method the ledger asks for". Now:
    • a later accepted booking_method clears an earlier rejection (last wins);
    • the latest rejected value whose case-insensitive reading names the method actually booked does not refuse (FIFO then fifo books FIFO);
    • anything else refuses (fifo alone, bogus, FIFO then bogus).
  • Options::query_refusal takes the method booked (Ledger::booking_method), so the comparison sees the effective method, including an embedder's default.
  • The message can no longer name the requested method as the wrong one. It reads option "booking_method" at <file>:<line>: "<bad value>" is not a booking method (E7002); refusing to derive figures, which would be booked with <effective>.
  • The FFI refusal belongs to queries only. It moved out of errors into query_refusal (above):
    • SessionState::from_file no longer gates the host's session plugins on it, pinned by test_from_file_invalid_booking_method_refuses_queries_only, which runs check_commodity as a session plugin;
    • load(), validate() and info() no longer show the refusal text.
  • The FFI lists no option warnings at all, for any option (a pre-existing gap, not touched here), so those surfaces show no E7002 for it, as on main.
  • A host-supplied booking-method the loader would reject (from-entries-with-options, for example lowercase fifo) is reported as a session error and replaced by STRICT, which info() echoes, the way fix(validate)!: pad parents, sub-accounts, tolerances and cost lots as beancount does #2920 reports an unparsable tolerance multiplier. An empty value is the default, with no error. Pinned by test_from_entries_with_options_reports_an_invalid_booking_method.
  • In ffi-wasi, the misplaced push_query_refusal is replaced by query_refusal_error, which sits above the ledger_error_to_ffi doc block, so each function keeps its own doc.
  • The WASM surface sentence is corrected (above).
  • origin/main is merged again (fix(lint): load every lint's ledger through one shared helper, and refuse failed processing in closed-nonempty #2922).
  • docs/commands/query.md, docs/commands/report.md, docs/reference/options.md and the CHANGELOG Breaking entry describe the last-wins rule.

Review round 3

  • The clearing is now pinned. bogus then FIFO (must answer) joins QUERY_RULE_CASES and test_query_refusal_only_when_booking_differs_from_the_request. bogus names no method, so only the clearing by the later accepted value lets it answer. Deleting self.rejected_booking_method = None; now fails tests on every surface: the loader unit test and test_blocks_queries_gives_each_case_its_answer, the CLI test_query_answers_or_refuses_by_the_shared_rule, both FFI shared-rule tests (source and file), and both WASM shared-rule tests (wasm-pack test --node). Before this round every test passed.
  • FFI held_booking_method no longer trims. Only the empty string is the default. The value is validated exactly as stored, through the loader's Options, which rejects "FIFO " as beancount does. A rejected value is reported quoted as the host sent it and replaced by STRICT, so info() and the booking agree. Tests cover lowercase, a trailing space and a leading space.
  • Doc splices fixed. refused_errors and held_booking_method now sit above the doc blocks of with_load_errors and sanitize_options. A scan of the whole diff for an insertion between a doc comment and its item finds no other.
  • Stale docs. Options::rejected_booking_method is documented as the latest rejection, cleared by a later accepted value. bail_on_booking_errors (doc and # Errors) states the round-2 rule, and says bean-query answers with the kept method.
  • Semver. The CHANGELOG Breaking entry lists the new pub fields on structs that are not #[non_exhaustive]: Options::rejected_booking_method, cache::CachedOptions::rejected_booking_method, and rustledger_ffi_wasi::helpers::{LoadResult, FileLoad}::query_refusal.
  • Caches. Unchanged at loader 68 / WASM 90, above fix(parser): keep a BOM inside a string literal as string content, as beancount does #2926's 67 / 89.

Caches

  • Loader cache v68. The cached options held the rejected value, and the archived CachedOptions gains rejected_booking_method, so a cache hit still refuses. No CostNumber or MetaValue encoding moved, so those byte and layout fixtures stand, and only their FIXTURE_VERSION advances.
  • WASM cache v90 (pin 68). The archived account-type names and error list held the rejected rename's effects, and query_refused is now set for a rejected booking_method.
  • After merging main (fix(parser): lex an account component with any non-ASCII start, as beancount does #2908: loader v66 / WASM v87), each version has exactly one note.

Before / after against bean-check 3.2.3

Error counts. E7003 is a warning and is not counted.

Shape bean-check 3.2.3 rledger before rledger after
name_equity "A😀" + open Equity:A (issue example) 1 (option) 1 (E1005; root accepted before #2908) 1 (E7008)
name_equity "capital" + open Equity:A 1 (option) 2 (E7008, E1005) 1 (E7008)
name_assets "" 1 2 (E7008, E1005) 1
name_income "revenue" (lowercase) 1 2 1
name_expenses "Exp:Sub" 1 2 1
name_income "Assets" (collides with a root) 1 (Invalid account name: Income:A) 1 (E1005) 1 (E1005), rename applied in all three
name_equity "Capital" then "bad", opens Capital:A, Equity:B 2 (option, Equity:B); keeps Capital 3 (E7008, E1005 x2) 2 (E7008, E1005 on Equity:B)
name_equity "bad" then "Capital" 2; Capital 2 (+ E7003 warning) 2
each of the 7 account_* options = "bad:leaf" 1 each 1 each, account_rounding 2 (E7002, E7007) 1 each
account_current_conversions "" 1 1 1
account_previous_balances "Anfang" then "x" 1; keeps Anfang 1, held x (+ E7003) 1, keeps Anfang
account_previous_balances "x" then "Anfang" 1; Anfang 1 (+ E7003) 1; Anfang
booking_method "FIFO" then "XYZ" 1; keeps FIFO 1, held XYZ (so booked STRICT) 1, keeps FIFO
booking_method "fifo" + an ambiguous sale 2 (option, Ambiguous matches); STRICT 0, booked FIFO 2 (E7002, E4003); STRICT; query/report refuse
plugin_processing_mode "default" then "bogus" 1; default 1, held bogus 1; default
tolerance_multiplier "0.6" then "abc" 1; 0.6 1 (+ E7003); 0.6 1; 0.6
inferred_tolerance_default "USD:abc" / display_precision "USD" 1 / 1 1 / 1 1 / 1

Out of scope, tracked in #2925: beancount's boolean options have no rejecting converter (any other word reads as false, with no error). rledger still reports E7002 for an unrecognized render_commas / infer_tolerance_from_cost word while setting the same false. #2925 also covers the tolerance and included-file option differences.

How to test

cargo test -p rustledger-loader --all-features options::tests
cargo test -p rustledger-loader --all-features --test invalid_option_keeps_default_test --test query_answer_rule_test
cargo test -p rustledger --all-features --test invalid_booking_method_refusal_test --test query_answer_rule_test
cargo test -p rustledger-ffi-component --all-features query_answer_rule
cd crates/rustledger-wasm && wasm-pack test --node -- --test wasm_invalid_option --test wasm_query_sources

New tests:

  • options.rs:
    • test_set_invalid_name_option_keeps_default (includes A😀)
    • test_set_invalid_name_option_keeps_earlier_valid_value
    • test_set_name_option_colliding_root_is_applied
    • test_set_invalid_account_option_keeps_default
    • test_set_invalid_account_option_keeps_earlier_valid_value
    • test_set_two_valid_values_still_warn_duplicate
    • test_set_invalid_booking_method_keeps_earlier_value
    • test_set_invalid_plugin_processing_mode_keeps_earlier_value
    • test_set_invalid_numeric_option_keeps_earlier_value
    • test_query_refusal_only_when_booking_differs_from_the_request
  • rustledger-loader/tests/invalid_option_keeps_default_test.rs (full pipeline):
    • test_load_invalid_name_option_keeps_default_root (issue's A😀 and capital)
    • test_load_invalid_name_option_keeps_earlier_valid_root
    • test_load_invalid_account_option_keeps_earlier_valid_leaf
    • test_load_lowercase_booking_method_books_strict
  • rustledger/tests/invalid_booking_method_refusal_test.rs:
    • test_query_invalid_booking_method_refuses
    • test_report_invalid_booking_method_refuses
    • test_query_and_report_valid_booking_method_answer
    • test_query_and_report_other_rejected_option_answers
  • rustledger-wasm/tests/wasm_invalid_option.rs (node):
    • Ledger / ParsedLedger error lists for the issue's case (E7008 alone)
    • query refusal on both
  • WASM test_account_types_from_raw_rejected_root_keeps_earlier_value.
  • Seven new shared query_answer_rule cases.
  • FFI: test_from_entries_with_options_reports_an_invalid_booking_method (lowercase, trailing and leading space), test_from_file_invalid_booking_method_refuses_queries_only.

Gates run:

  • fmt, clippy -D warnings, cargo doc -Dwarnings;
  • tests for rustledger-loader, rustledger, -ffi-wasi, -ffi-component, -wasm, -lsp, -validate, -parser, -plugin;
  • the wasm node tests above.

The parser corpus baselines are unaffected because the changes are loader-side.

Closes #2911

🤖 Generated with Claude Code

…jected

beancount 3.2.3's grammar.option returns on the converter's ValueError
without assigning the option, so an invalid value is one error and has no
other effect. rledger reported the error (E7008, E7002) and applied the
value anyway: `option "name_equity" "capital"` renamed the equity root, and
`open Equity:A` then failed with E1005.

Every validating setter now rejects through one path (`Options::reject`)
that reports the error and leaves the option at its default or an
earlier valid setting: all five name_* roots, all seven account_* leaves
(account_rounding included, without the E7007 no-effect notice for a
value it did not take), booking_method (now case-sensitive, as
beancount's Booking[...] lookup is), plugin_processing_mode, and the
numeric and map options that already kept their value. A rejected value
is not recorded in set_options, so it raises no E7003 or E7010 and
cannot gate effective_booking_method.

The WASM account_types_from_raw copied the raw name_* tuples itself; it
now reads them through rustledger_loader::Options, so it cannot apply a
root the loader rejected.

Loader cache v68 and WASM cache v90: cached options and the WASM
account-type names held the rejected value.

Closes #2911

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 10, 2026 05:15
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 39.4ms ± 0.4ms 590.4ms ± 11.1ms 15.0x 44.9 MB / 51.1 MB
investment 62.0ms ± 0.9ms 2135.4ms ± 27.1ms 34.4x 47.2 MB / 58.9 MB
fifo 59.7ms ± 0.9ms 11736.9ms ± 65.4ms 196.5x 49.1 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 38.7ms ± 0.4ms 39.4ms ± 0.4ms +1.9% ✅ within noise 42.8 MB → 44.9 MB (+4.8%)
investment 61.5ms ± 0.7ms 62.0ms ± 0.9ms +0.8% ✅ within noise 57.0 MB → 47.2 MB (-17.3%)
fifo 59.0ms ± 0.6ms 59.7ms ± 0.9ms +1.3% ✅ within noise 45.0 MB → 49.1 MB (+9.2%)

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

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.79208% with 17 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/cache.rs 11.11% 16 Missing ⚠️
crates/rustledger-loader/src/options.rs 99.71% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

An invalid `option "booking_method"` (E7002, for example the lowercase
"fifo") keeps the previous method, as in beancount, so every lot
reduction would be booked with a method the ledger did not ask for.
`rledger query` and `rledger report` now refuse instead of printing those
figures, with the existing refusal's exit code and a message naming the
option, its value and its file:line. `check` is unchanged. This is a
deliberate deviation: bean-query answers with the STRICT figures.

The refusal is part of the one query rule. `Options` records the
rejected booking_method (with its site, which the loader now passes via
`set_scoped_at`) and exposes `query_refusal`; `Ledger::blocks_queries`
is the whole rule (a blocking error, or that refusal), and
`Ledger::query_refusal` gives it as a blocking `LedgerError` for
surfaces that list only ledger errors. The CLI gate shared by query and
report, the WASM `query`, `queryMultiFile`, `Ledger` and `ParsedLedger`
(and the MCP server through WASM), and the FFI component's loads all
apply it. The shared query_answer_rule cases gain an invalid, a valid
booking_method and another rejected option, so every surface's drift
guard pins it. Another rejected option keeps its default and answers.

Refs #2911

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen and others added 2 commits November 10, 2026 01:38
Brings in #2908 (strict account validator; loader v66, WASM v87),
#2919 and #2924. Keeps this branch at loader v68 / WASM v90 with one
note per version, drops the merged warn_if_invalid_root (rejection now
goes through check_root), and adds the issue's `A😀` root to the
rejected-name tests.

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

Round-1 review of #2923:

- The FFI component's booking_method_from resolves through
  Options::effective_booking_method instead of parsing the wire string;
  WASM extract_options reads title and operating currencies through
  Options, so no surface reads a raw option value.
- docs/reference/options.md: booking_method values are case-sensitive,
  as in beancount and as the open directive's booking method (P0025).
- An end-to-end load test that `booking_method "fifo"` books STRICT
  with E7002 (and the ambiguous sale STRICT reports), and node tests
  that the WASM Ledger and ParsedLedger report the issue's `A😀` root
  as E7008 alone and refuse a query over an invalid booking_method.

Refs #2911

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen robcohen changed the title fix(loader): keep an option's previous value when its new value is rejected fix(loader)!: keep an option's previous value when its new value is rejected, and refuse queries over a rejected booking_method Nov 10, 2026
robcohen and others added 2 commits November 10, 2026 03:00
…r asks

Round-2 review of #2923.

The refusal recorded the first rejected booking_method and never cleared
it, so `"fifo"` then `"FIFO"` booked FIFO yet refused. Now the last value
wins: an accepted booking_method clears an earlier rejection, and a
rejected value whose case-insensitive reading names the method actually
booked (`FIFO` then `fifo`) does not refuse. `fifo` alone, `bogus`, and
`FIFO` then `bogus` still refuse. `Options::query_refusal` takes the
method booked, and the message names only the rejected value as wrong:
`"<value>" is not a booking method (E7002); ... booked with <method>`.
The three orderings join the shared query_answer_rule cases.

In the FFI the refusal moves out of the load's `errors` into a separate
`query_refusal`, listed on query results only, so `load`, `validate` and
`info()` do not show it and a session still runs the host's plugins. A
host-supplied `booking-method` the loader would reject is reported as a
session error and replaced by STRICT. `query_refusal_error` sits above
the `ledger_error_to_ffi` doc block.

Refs #2911

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen and others added 3 commits November 10, 2026 05:00
…ng method as sent

Round-3 review of #2923.

- `bogus` then `FIFO` joins the shared query_answer_rule cases and the
  refusal unit test: `bogus` names no method, so only the clearing by a
  later accepted value lets it answer. Deleting that clearing now fails
  the loader, CLI, FFI and WASM tests; it passed them all before.
- FFI `held_booking_method` no longer trims. Only the empty string is the
  default; the stored value is validated exactly as sent, so `"FIFO "` is
  rejected as the loader and beancount reject it, reported quoted as the
  host sent it, and replaced by STRICT.
- `refused_errors` and `held_booking_method` move above the doc blocks
  they had split.
- `Options::rejected_booking_method` and `bail_on_booking_errors` describe
  the round-2 rule; the CHANGELOG Breaking entry lists the new pub fields
  on exhaustive structs.

Refs #2911

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

Round-4 review of #2923: BATCH_REFUSED and its doc hold for a refusal
by a rejected booking_method (whose reason follows it in each query, not
in the load); errors.md scopes "the value is not applied" to the options
beancount validates, naming the boolean exception (#2925), and gains an
E7008 entry; the LedgerError::blocks_queries doc is rewrapped.

Refs #2911

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Brings in #2926 (loader v67, WASM v89). This branch stays at loader v68
and WASM v90 (pin 68), with one note per version.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 6afd960 into main Nov 10, 2026
67 of 68 checks passed
@robcohen
robcohen deleted the fix/invalid-option-keeps-default branch November 10, 2026 14:05
robcohen added a commit that referenced this pull request Nov 10, 2026
Brings in #2926. WASM cache v91 (main holds v89, #2923 claims v90),
loader pin 67.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 10, 2026
Brings in #2923. WASM cache v91 (main holds v90), loader pin 68. The
held-options session keeps both checks: the tolerance multiplier and the
booking method.

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.

fix(loader): an invalid name_* or account_* option value is applied; beancount keeps the default

1 participant