Skip to content

fix(parser): an invalid account starts bison's recovery, and three more recovery shapes count as bean-check does - #3043

Merged
robcohen merged 1 commit into
mainfrom
fix/recovery-invalid-account
Nov 26, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/recovery-invalid-account

Conversation

@robcohen

@robcohen robcohen commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

What changed

These are the shapes #3040 left in cst/recovery.rs, rledger's model of bison's error recovery.

An invalid account starts the recovery window. P0006 is rledger's line error for a line it cannot read that holds an account such as Assets:Café or Assets:<BOM>A. It started no window, so the lines bison keeps quiet after it kept their errors. For example, Assets:FundoInclusão:X 1 BRL at the top of a file followed by Assets:Eventos:Y gave two errors where bean-check gives one.

beancount's lexer reads that account into an ACCOUNT: its account rules (ACCOUNTTYPE / ACCOUNTNAME in lexer.l) admit any UTF-8 character, a BOM included. Glued to more text, it reads it into an invalid token. Either way the grammar fails on the line as it does on any other. So the error now starts the window, exactly as rledger's other line errors do. The existing token-by-token machine then decides where bison fails.

The one exception is a line holding a stray \r (#2980). No recovery is modeled there, and the lines after keep their errors, as before. Starting a window there would trade one unmodeled case for another, and the differential showed it moving ~180 BOM/\r inputs away.

An undated declaration that bison starts mid-line and that then fails. In popmeta pushmeta kk: @, ~ pushmeta kk: "v" @ or x option "title" "t" 5, bison's recovery shifts the declaration's first three tokens and resumes reporting. It then fails again at the next token. The declaration builds nothing, so it changes no state. Bison reports the second failure and starts its recovery over there.

Before, rledger reported one error on such a line and treated the lines after it as reported. Now that failure is reported. Where rledger has no error at that token, one is added: unexpected "@" in the pushmeta directive: expected the end of the line. The window then goes on from the failing token. The completion is modeled for option/plugin with two strings, popmeta KEY:, and pushmeta KEY: with an optional string, account, date, currency, tag or bool value. A numeric value or expression, and a string running over a line end, are left unmodeled.

An indented line after an option in a transaction's body (#3040 item 2). Bison builds the option whole and is back at a declarations, so the next indented line fails at its indent. rledger reads that line as part of the transaction. On $x bean-check reports two errors (unexpected INDENT and Invalid token: '$x') and rledger reported one. Since rledger's error already names $x, the indent's error is the one added: unexpected indent: no entry continues on this line.

Counting a grammar failure beside the invalid tokens. A line that fails and also has invalid tokens reports one error per token plus one for the failure. The old guard never let rledger add a missing token error past its own error count, so Assets:B Assets:a # 1.5 1,50 @@ lost the 1,50. The failure now counts as its own error only where it is known to be bison's own failure, with nothing before keeping bison recovering:

  • a column-0 statement failing among its first three tokens, which the recovery's grammar knows whole;
  • a body line failing at its first token. A dated directive's body takes a key there; a transaction's body also takes a posting, a tag or a link.

Restricting it this way matters: unrestricted, the change moved ~100 realistic g3 inputs away (for example Z 1,50, where Z is a posting flag beancount takes).

Separately, the read-back over lines before an orphan indent used to treat any indented line holding a date or keyword as ending the recovery. It now runs the recovery machine over that line (recovery_ends_on_line), so date: 2024-01-02 (a DATE shift, then an EOL that is not taken) no longer ends it.

The pass stays linear. Each new lexing reuses what the read-back already covered, and the dated header kind is cached per header. test_quiet_bison_recovery_scans_linear_bytes gains four shapes: invalid accounts, lines after options in a body, undated declarations failing mid-line, and date lines in a recovery.

Measurements (beancount 3.2.3)

Left as is, and why

WASM CACHE_VERSION 139 (137 is #3041, 138 is #3042). Loader cache unchanged (v78, after #3041).

How to test

cargo build --release -p rustledger-loader --no-default-features --example parse_error_lines
python3 -I scripts/compat-parse-error-parity.py --rledger target/release/examples/parse_error_lines \
  --fuzz-dir DIR --fuzz-commit 9bc79f4f35f52c50813164f43307e7d151d70d8c \
  --compat-dir tests/compatibility/files --check
STRICT_BASELINE=1 cargo test -p rustledger-parser
cargo test -p rustledger-loader -p rustledger-wasm -p rustledger-lsp -p rustledger --all-features

New tests in crates/rustledger-parser/tests/bison_recovery_model.rs pin each shape, with every count checked against beancount.parser.parser.parse_string 3.2.3:

  • test_parse_invalid_account_starts_the_recovery_window
  • test_parse_indented_line_after_an_option_in_a_body_reports_its_indent
  • test_parse_undated_declaration_failing_mid_line_is_reported
  • test_parse_date_on_an_indented_line_does_not_end_the_recovery
  • test_parse_grammar_failure_counts_beside_the_invalid_tokens

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 26, 2026 06:40
@robcohen
robcohen force-pushed the fix/recovery-invalid-account branch from 51d39f3 to a4c1ff4 Compare November 26, 2026 06:49
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.51741% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/recovery.rs 96.51% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 37.0ms ± 0.9ms 524.5ms ± 2.6ms 14.2x 43.1 MB / 51.2 MB
investment 58.1ms ± 0.7ms 1874.0ms ± 34.1ms 32.2x 47.2 MB / 59.0 MB
fifo 58.1ms ± 8.2ms 9358.6ms ± 394.1ms 161.0x 49.4 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 36.6ms ± 1.0ms 37.0ms ± 0.9ms +1.1% ✅ within noise 42.4 MB → 43.1 MB (+1.6%)
investment 58.3ms ± 1.0ms 58.1ms ± 0.7ms -0.3% ✅ within noise 47.4 MB → 47.2 MB (-0.2%)
fifo 56.2ms ± 2.9ms 58.1ms ± 8.2ms +3.4% ✅ within noise 49.1 MB → 49.4 MB (+0.7%)

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

github-actions Bot commented Nov 26, 2026 •

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

…re recovery shapes count as bean-check does

The shapes #3040 left in cst/recovery.rs's model of bison's error recovery.

- An invalid account (P0006, rledger's line error for an unreadable line
  that holds an account like `Assets:Café` or `Assets:<BOM>A`) started no
  recovery window, so the lines bison keeps quiet after it kept their
  errors. beancount's lexer reads such an account into an ACCOUNT (or,
  glued to more, an invalid token) and its grammar fails on the line as on
  any other: the error now starts the window like rledger's other line
  errors. Not on a line holding a stray `\r` (unmodeled, as before).
- An undated declaration bison's recovery starts mid-line and that fails
  past its first three tokens (`popmeta pushmeta kk: @`) builds nothing:
  bison reports that failure and restarts its recovery there. The failure
  is now reported (an error is added where rledger has none) and the window
  goes on from it.
- An indented line after an `option` bison builds inside a transaction's
  body fails at its indent; on `  $x` the indent's error is added beside
  the invalid token's, which rledger names already.
- A line's grammar failure counts as one error beside its invalid tokens
  where it is known to be bison's own (a column-0 statement failing among
  its first three tokens, a body line failing at its first token, nothing
  before keeping bison recovering), so a misgrouped number there is
  reported. An indented line whose date starts no statement
  (`    date: 2024-01-02`) no longer ends the read-back recovery.

Parity corpus: 107,435 -> 107,631 matching, 0 regressions. BOM matrix: 52
more rows match, none fewer. Generated differentials (1.26M inputs, main vs
branch): 8,105 gained, 473 closer, 48 moved away (44 with a stray `\r`, a
BOM or control characters). WASM cache 139.

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