Skip to content

fix(lint): load every lint's ledger through one shared helper, and refuse failed processing in closed-nonempty - #2922

Merged
robcohen merged 5 commits into
mainfrom
refactor/lint-load-helper
Nov 10, 2026
Merged

robcohen merged 5 commits into
mainfrom
refactor/lint-load-helper

Conversation

@robcohen

@robcohen robcohen commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

What changed

Every rledger lint subcommand now loads its ledger through one shared helper, crates/rustledger/src/cmd/lint/load.rs, which decides which load and processing errors a lint may answer over. It has three levels:

Helper Does Used by
load_booked Refuses load errors, processes as check does (plugins within the budget, validation), refuses booking and plugin failures, passes every other error through in Ledger::errors linked-transit, closed-nonempty
load_clean Refuses load errors transfers --apply
load_source + warn_load_errors Answers over what parsed, with a LOAD_ERRORS warning control-chars, read-only transfers

The refusal messages are the ones linked-transit already printed (<file>: cannot inspect source with load errors: ..., <file>: cannot inspect a ledger whose processing failed: ...), and describe_error moved with them.

Per lint:

  • linked-transit: unchanged in behavior. It calls load_booked instead of its own copy. Messages, exit codes, JSON and the ag-rledger envelope are byte-identical, and its tests pass unchanged.
  • closed-nonempty (the fix): it refused load errors but answered over failed processing. It now refuses a ledger whose booking or plugins failed, and exits 2 for every "could not answer" (it exited 1), matching linked-transit and check. Its load-error message is now linked-transit's.
  • transfers --apply: refuses a file with load errors before writing anything. All inputs load before any edit, so a clean input is not written either. Exit 2 (see Review round 1).
  • transfers (read-only): still answers over what parsed. It now warns that the file had load errors.
  • control-chars: still answers over what parsed. A control character is often what broke the parse, and the parser recovers enough to find it: 2024-01-02 * "bell\u{7}" junk (P0012) still reports control character in narration: "bell\u{7}". But recovery can also drop a whole transaction ("bell\u{7}" "x" "y" junk yields no finding), so it now warns. It never prints a clean "No control characters" silently.

transfers and control-chars now take the same warnings / err arguments linked-transit does, so in ag-rledger the warning is an entry in the envelope's warnings, with code LOAD_ERRORS.

On canonical-function discipline: ag-rledger only dispatches to lint::run_with_writer and has no loading of its own. check --lint transfers runs on the ledger check has already loaded and reported, so it is not a lint loader. Nothing else runs lints.

Why

Issue #2899 proposes a cross-group lint, and we said there that loading would first move into a shared helper. Each lint loaded the ledger its own way, and only linked-transit refused failed booking or plugins. This ledger:

plugin "no.such.plugin"
2024-01-01 open Assets:Cash
2024-01-01 open Expenses:Food
2024-01-02 * "lunch"
  Assets:Cash
  Expenses:Food
2024-12-31 close Assets:Cash
  • Before: rledger lint closed-nonempty printed No accounts closed with a non-zero balance. and exited 0.
  • After: it prints error: <file>: cannot inspect a ledger whose processing failed: <file>:5: BOOK: interpolation failed: ...; E8004: Python plugin "no.such.plugin" ... and exits 2.

How to test

cargo test -p rustledger --all-features

New tests:

  • cmd::lint::load::tests::*: booking-only, plugin-only and combined refusals, each asserted by its location; other errors kept; load-error refusal at both refusing levels, including a duplicate include; each parse error named with its line, and the list capped; the warning to a sink or to stderr, and none without errors; ErrorIndex and TransactionSites, plus the two tests moved from linked_transit.
  • closed_nonempty::tests: test_run_with_writer_refuses_a_booking_failure, ..._refuses_a_plugin_failure, and ..._refuses_failed_booking_and_plugins (the exact ledger above).
  • control_chars::tests::test_run_with_writer_answers_over_load_errors_with_a_warning, ..._never_reports_clean_silently_over_load_errors, ..._writes_the_load_warning_to_err_without_a_sink.
  • transfers::tests::test_run_with_writer_apply_refuses_load_errors_before_writing, test_run_with_writer_preview_warns_on_load_errors.
  • cli_commands_test: test_lint_closed_nonempty_refuses_failed_processing_with_exit_2, test_lint_transfers_apply_refuses_load_errors_and_writes_nothing.
  • ag_rledger_test: test_lint_control_chars_warns_on_load_errors, and test_lint_every_lint_exits_two_when_it_cannot_answer (see Review round 1).

docs/commands/lint.md has a new table of what each lint reads and when it refuses, and CHANGELOG.md has two Breaking entries (closed-nonempty, transfers) and a note on the entry that introduces control-chars.

Refs #2899

Review round 1

  • One exit rule for every lint. A lint exits 0 when it answered, 1 for a finding under an opt-in fail flag, and 2 when it could not answer. lint::ERROR_EXIT_CODE replaces the per-lint error_exit_code. transfers (including the --apply refusal) and control-chars on a missing file now exit 2, where they exited 1. ag-rledger does not map lint exit codes: a lint that cannot answer is an error envelope there ("ok": false, COMMAND_FAILED or FILE_NOT_FOUND), unchanged.
  • Tests that could not fail now can. The old check err.contains("; ") was satisfied by the E8004 message alone. The booking refusal is now asserted by its location (<file>:4: BOOK: interpolation failed, :5: in the [Feature]: Opt-in audit for transactions spanning account groups #2899 ledger, :6: behind a two-line plugin fixture). There are booking-only, plugin-only and combined fixtures in both load::tests and closed_nonempty::tests. I checked that the tests now catch a regression: with the booking refusal dropped from load_booked, five tests fail (load_booked_refuses_a_booking_failure, load_booked_names_every_failure, closed_nonempty's refuses_a_booking_failure and refuses_failed_booking_and_plugins, linked_transit's refuses_booking_errors).
  • The rest of what cross-group needs is in lint/load.rs.
    • ErrorIndex places each remaining processing error on the transaction it sits on: a validator's error by its span, a plugin's by its file and line anywhere in the transaction's lines.
    • TransactionSites / TransactionSite give each transaction its identity and printed location. They flag a plugin-inserted transaction with no source location, or one at a location another transaction already has.
    • span_lines, errors_within and their two tests moved with them, and there is a new test_error_index_places_errors_on_their_transaction.
    • linked-transit uses these helpers. Its unit tests pass. I ran 576 linked-transit invocations against the round-0 binary: 12 inputs (matched, mismatch, missing, complex, errors on legs, check_commodity, forecast, effective_date, include and duplicate input, parse error, missing plugin, unbookable, missing file), 4 account sets, 3 prefixes, text and JSON, with and without --fail-on. 540 are byte-identical in stdout, stderr and exit code. The other 36 are the parse-error input, and only their stderr differs, by the located load-error detail below.
  • Load errors name a location. A refusal or warning lists each parse error as file:line: Pnnnn: message, up to ten, then ; and N more, where it said parse errors in <file>. The change is in the lint loader (describe_load_errors, using the load's source map). LoadError's Display is unchanged.
  • Docs. docs/commands/lint.md opens with the exit-rule table. It also says that load_clean / load_booked refuse even the load errors the loader counts as harmless (a duplicate include, an include cycle, an include glob matching nothing), as bean-check reports them. There is a test for the duplicate include.
  • ag-rledger tests. The closed-nonempty, transfers and control-chars assertions moved out of test_lint_linked_transit_exits_two_when_it_cannot_answer into test_lint_every_lint_exits_two_when_it_cannot_answer. It asserts exit 2 for a missing file for each lint, and for closed-nonempty on load errors, a failed plugin and a failed booking: exit 2, empty stdout, and an "ok": false / COMMAND_FAILED envelope whose message says why.
  • CHANGELOG. The closed-nonempty and transfers entries are under Breaking, since both lints shipped in v0.24.0. They say "as rledger check does on a file it cannot read". The control-chars note is folded into the entry that introduces the lint.

Review round 2

  • TransactionSites::for_ledger(&ledger) returns a per-ledger view (LedgerSites) whose site reads that ledger's location counts, so a lint cannot read sites against stale counts.
  • OutputFormat moved from closed_nonempty into the lint module.
  • transfers::run's docs describe the read-only warning and the exit 2.
  • The ag-rledger test asserts FILE_NOT_FOUND for each missing-file envelope, and the COMMAND_FAILED envelope of a refused transfers --apply.
  • Merged origin/main (only a CHANGELOG conflict: both Breaking entries kept).

🤖 Generated with Claude Code

…fuse failed processing in closed-nonempty

Each `rledger lint` subcommand loaded its ledger its own way. Only
`linked-transit` refused a ledger whose booking or plugins failed;
`closed-nonempty` refused load errors but answered over failed processing,
so a ledger with `plugin "no.such.plugin"`, an unbookable transaction and
a `close` printed "No accounts closed with a non-zero balance." and exited
0. `transfers` and `control-chars` never looked at load errors.

`lint/load.rs` is now the one place lints load from, at three levels:

- `load_booked`: refuses load errors, processes as `check` does, refuses
  booking and plugin failures, and passes every other error through.
  Used by `linked-transit` (behavior byte-identical) and `closed-nonempty`,
  which now refuses failed processing and exits 2 when it cannot answer.
- `load_clean`: refuses load errors. Used by `transfers --apply`, which
  refuses before any file is written.
- `load_source` + `warn_load_errors`: answers over what parsed, with a
  `LOAD_ERRORS` warning. Used by `control-chars` (a control character is
  often what broke the parse) and read-only `transfers`.

Refs #2899

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

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 52.3ms ± 1.2ms 827.1ms ± 9.7ms 15.8x 42.8 MB / 51.1 MB
investment 78.5ms ± 0.6ms 2892.3ms ± 6.1ms 36.8x 59.0 MB / 59.1 MB
fifo 74.2ms ± 0.6ms 14568.7ms ± 60.5ms 196.4x 45.1 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 52.1ms ± 1.1ms 52.3ms ± 1.2ms +0.4% ✅ within noise 42.8 MB → 42.8 MB (-0.0%)
investment 78.4ms ± 0.5ms 78.5ms ± 0.6ms +0.1% ✅ within noise 59.0 MB → 59.0 MB (+0.0%)
fifo 74.0ms ± 0.3ms 74.2ms ± 0.6ms +0.3% ✅ within noise 45.0 MB → 45.1 MB (+0.1%)

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

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.33174% with 23 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/lint/load.rs 95.11% 20 Missing ⚠️
crates/rustledger/src/cmd/lint/transfers.rs 97.56% 2 Missing ⚠️
crates/rustledger/src/bin/ag-rledger.rs 83.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@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

robcohen and others added 4 commits November 9, 2026 23:13
… transaction-site helpers in the shared loader

Review round 1 of the shared lint loader.

- Every lint exits 2 when it cannot answer (`lint::ERROR_EXIT_CODE`
  replaces the per-lint `error_exit_code`): `transfers`, including the
  `--apply` refusal, and `control-chars` on a missing file, as
  `linked-transit` and `closed-nonempty` already did. 0 means it answered,
  1 a finding under an opt-in fail flag.
- A load-error refusal or warning lists each parse error as
  `file:line: Pnnnn: message`, up to ten, then how many more, where it said
  only `parse errors in <file>`. Done in the lint loader; `LoadError`'s
  Display is unchanged.
- `ErrorIndex` (each remaining processing error on the transaction it sits
  on) and `TransactionSites` (plugin-inserted transactions with no or a
  shared location) move from `linked-transit` into `lint/load.rs`, for the
  cross-group lint to build on. `linked-transit`'s output is unchanged
  except for the located load-error detail.
- Tests that could not fail now can: the booking refusal is asserted by its
  `BOOK: interpolation failed` location, with booking-only and plugin-only
  fixtures; dropping the booking refusal fails five tests.
- The ag-rledger assertions for the other lints move to their own test,
  which checks the `ok: false` envelope.
- CHANGELOG: the `closed-nonempty` and `transfers` entries move to
  Breaking, and the `control-chars` note folds into the entry that
  introduces the lint. Docs: the exit rule, and that a refusal covers the
  load errors the loader counts as harmless.

Refs #2899

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…and review nits

Review round 2 of the shared lint loader.

- `TransactionSites::for_ledger(&ledger)` returns a per-ledger view whose
  `site` reads that ledger's location counts, replacing `count` + `site`:
  a lint can no longer read sites against another ledger's counts.
- `OutputFormat` moves from `closed_nonempty` into the lint module.
- `transfers::run`'s docs say what it does now: a read-only run over load
  errors answers with a warning, and "cannot answer" exits 2.
- The ag-rledger test asserts `error.code` for each missing-file envelope,
  and the envelope of a refused `transfers --apply`.

Refs #2899

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

A missing Python plugin is E8004 with the Python plugin runtime and E8005
in the release configuration, which ships without it. The refusal tests
asserted E8004 and failed in the Slim Build job; they now assert the
plugin named in either message.

Refs #2899

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit aa15f7b into main Nov 10, 2026
49 of 50 checks passed
@robcohen
robcohen deleted the refactor/lint-load-helper branch November 10, 2026 07:53
robcohen added a commit that referenced this pull request Nov 10, 2026
robcohen added a commit that referenced this pull request Nov 10, 2026
Brings in #2922. WASM cache v88 (main holds v87), loader pin 66.

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

The control-chars load-error tests from #2922 use the header
`* "bell\u{7}" junk`. With this branch the invalid token after the
lone string keeps that string in the payee, so the control character is
reported in the payee rather than the narration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 12, 2026
…d token (#2944)

* fix(parser): keep each header string in its own field after an invalid token

A transaction whose header holds an invalid token is kept under error
recovery, but its payee and narration were read by counting only the
strings that survived, so `* "p" <bad>` loaded with no payee and the
narration `p`. The LSP, WASM and FFI, which load despite parse errors,
showed the shifted fields.

Both walks (red `convert_transaction`, green `convert_transaction_header`)
now read the fields through one shared rule,
`txn_header::header_payee_narration`: an invalid token before the first
tag or link holds the string slot it sits in. A lone string followed by
one is the payee, with an empty narration; a lone string after one is the
narration; two strings are the payee and narration whatever surrounds
them. Error counts and spans are unchanged.

Loader cache v73, WASM cache v98.

Closes #2932

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

* fix(parser): any unexpected header token holds its slot, read by one rule

Review round 1 of the header recovery fix (#2932):

- A lone string is the payee only when an unexpected token follows it and
  none comes before it, so `* <bad> "n" <bad>` keeps the narration `n`: a
  token written after a string no longer changes its field.
- Every token the header grammar has no place for holds its slot, not only
  an invalid token: a number, a currency, an account, `~`, `@`, a brace, a
  comma, a keyword, a second flag, a mid-line `#!`. `* "p" 123` keeps
  payee `p`, as `* "p" <bad>` does.
- The rule is `txn_header::header_strings`, which returns string indexes,
  so the typed `ast::Transaction::payee()` / `narration()` accessors use it
  too and cannot drift from the directive. Formatter output is unchanged.
- The loader cache v73 note says the bump only follows the pin convention:
  that cache is never saved for a load with errors.
- The parser corpus baseline entry for lima's ErrorInTransactionLine
  (`* "Dinner" A:*:B`) moves: `Dinner` is now the payee.

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

* fix(parser): only an invalid token after a lone header string keeps it the payee

Review round 2 of the header recovery fix (#2932):

- Narrow the rule to the one token that can have swallowed a string: an
  invalid token (a mid-line BOM lexes as one). A lone string right after
  the flag, followed by an invalid token, is the payee with an empty
  narration. Every other case keeps beancount's count rule, so
  `* "Coffee" 5.00 USD` (a forgotten newline) keeps the narration
  `Coffee`, and anything before the string (`* 123 "n" <bad>`,
  `* 2024-01-03 "n" <bad>`, `* <bad> "n" <bad>`) leaves it the narration.
- `header_strings` now reads token kinds only; the date and flag are told
  apart by position, so the green walk no longer tracks offsets and the
  dead `k != DATE` guard is gone.
- The `tests/ast.rs` flip and the lima parser-corpus manifest change are
  reverted: neither header has an invalid token.
- Cache notes name the actual holders of the skipped versions.

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

* test(parser): pin that an invalid token is never the header flag

Reword the header_strings doc: a transaction node opens only on a flag
token, a string, a tag or a link, not on an invalid token. Add the
[date, bad, s, bad] case, so dropping the ERROR_TOKEN guard in the flag
arm (which would read the invalid token as the flag and move the string
to the payee) fails a test.

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

* chore: re-trigger CI (runs for 9a1d7d7 never left the queue)

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

* test(lint): expect the payee for a lone header string before an invalid token

The control-chars load-error tests from #2922 use the header
`* "bell\u{7}" junk`. With this branch the invalid token after the
lone string keeps that string in the payee, so the control character is
reported in the payee rather than the narration.

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

---------

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