Skip to content

refactor(parser): delete the hand-modeled bison recovery the error oracle replaced - #3063

Merged
robcohen merged 3 commits into
mainfrom
feat/lalr-delete-recovery
Nov 27, 2026
Merged

robcohen merged 3 commits into
mainfrom
feat/lalr-delete-recovery

Conversation

@robcohen

@robcohen robcohen commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Based on main (#3061 and #3062 merged).

What

Phase 5: delete the hand-modeled bison recovery, one piece per commit, now that the error oracle (#3061, on by default in #3062) decides which lines beancount reports. Every commit passes these gates:

  • scripts/compat-parse-error-parity.py --check (each commit re-pinned with --update on its own, on top of main);
  • the BOM combination matrix;
  • the STRICT_BASELINE corpus tests;
  • the parser suite;
  • workspace clippy, and the parser, loader, LSP, WASM and CLI suites.

1. refactor(parser): retire quiet_bison_recovery for the error oracle

This removes quiet_bison_recovery, the phantom re-parses (phantom_errors, MAX_PHANTOM_DEPTH), accepts(), and their helpers. The reconciler takes over what they did, from the oracle's trace of beancount's own parse:

  • Swallowed lines. The driver now records the tokens it discarded separately from those it shifted while recovering. A line where beancount reads no token keeps none of rledger's errors, builder checks included, because beancount checks nothing it doesn't build. A line end shifted while recovering doesn't count as reading the line. The tokenless tail of the input, passed while still recovering, counts as discarded.
  • Entries recovery starts mid-line. Lines of an entry built or dropped there carry beancount's verdict. Entries it builds are parsed once on their own, and their builder errors are added (an invalid booking method). Entries it drops contribute only the pipe warnings its grammar emitted before the drop.
  • Own errors spend the slot budget first. On a line beancount reports, rledger's own errors (a stray \r) count against that line's slots before anything else, and are capped at the slot count. The stray-\r overlay now claims those errors, not whole lines, and nothing on a swallowed line. Arithmetic errors aren't claimed inside recovery. docs/reference/compatibility.md P3 is updated to match.
  • Messages. A misgrouped number that rledger had no error for is synthesized as InvalidNumber. Synthesized messages quote the token with " as rledger's own do. They are placed on the line their slot is reported on (multi-line strings), and show only the token's first line.
  • Tests. The linear-time test of the deleted pass (test_quiet_bison_recovery_scans_linear_bytes) is ported to the oracle path as test_parse_with_the_oracle_is_linear_on_recovery_shapes. It uses the same shapes and adds the new ones (pipes in dropped entries, rebuilt entries with builder errors).
  • Re-pins. WASM cache v149. Corpus baseline: one file's error text moves (oklink, same counts). BOM matrix texts are relabeled (same counts). Two tests that pinned the hand model's added messages now pin the oracle's. format_roundtrip_properties::test_format_source_keeps_a_leading_indented_comment_indented (fix(format): never change what a file means; round-trip properties for the formatter #3060) expected \t; then \t+ to parse clean: it did only because this pass emptied the errors before the oracle ran. It now gets one error, as main already does for ; c then ! (a parse rledger finds an error in never comes out clean). The formatting and round-trip it pins are unchanged.

2. refactor(parser): delete cst/recovery.rs

model_stray_cr_lines re-parsed the input with each stray \r as a blank. It was the last user of BcLexer, the tainted port of beancount's lexer. The real lexer difference it hid, a comment ending at a stray \r, is already fixed on main by #3060. This PR first carried its own regex fix for it; that is dropped in favour of #3060's, so there is one implementation. The whole file is deleted: 2,640 lines, including BcLexer.

3. refactor(parser): delete quiet_after_failed_line_starts

This pass kept convert.rs's windows quiet after a line a BOM starts, or a dated line that opens no directive. Removing it changes no error count on any parity input. Its counter-based linear test measured only that pass, so it goes too; its shapes are added to the oracle's parse-level linear test.

Measurements

step parity: matching / 123,394 regressions pinned mismatches now matching
main (#3062, oracle on) 120,099 — —
1. quiet_bison_recovery gone 122,378 0 2,694
2. recovery.rs gone 122,378 0 0
3. quiet_after_failed_line_starts gone 122,378 0 0

The BOM combination matrix stays at 13,349 matching rows throughout. The STRICT_BASELINE corpus tests pass at every step.

What remains

Each of these was tried and kept, with the reason measured:

  • quiet_after_invalid_tokens (convert.rs). Parity doesn't move without it. But the BOM matrix loses 7 rows: on a line whose BOM-led accounts beancount reads as valid tokens (beancount reports nothing there), rledger would report each mid-file BOM instead of one per line. That one-per-line rule is a deliberate difference the oracle doesn't express yet, so it needs an overlay first.
  • drop_errors_inside_a_line_error. Parity is unchanged, but 8 tests break. That includes transaction_holds_invalid_token, the validator's predicate for transactions beancount discards, which reads the error kinds this pass leaves. The dropped-statement work below replaces that predicate with the oracle trace's "dropped" bit, after which this pass can go.
  • report_at_multi_line_string_ends. Parity is unchanged, but 4 tests break. It places rledger's own error on a multi-line string's last line for every surface (LSP, check). The reconciler only places the errors it synthesizes there, so deleting this pass needs the reconciler to move an assigned error's start the same way.
  • The BcLexer-dependent logos patches. BcLexer itself is gone. The logos lexer's own patches (lone-capital runs, BOM runs, the comment splits of perf(parser): a long run of mid-line #! or #+ with no blank lexes in quadratic time #2989) are what rledger's CST is built from, so they're not part of the error model. The one that hid a real difference, comments ending at a stray \r, is fixed on main (fix(format): never change what a file means; round-trip properties for the formatter #3060).
  • Dropped statements (casc2–4). Back transaction_holds_invalid_token (and a balance-directive equivalent) with the oracle's raw trace: is the directive's own statement dropped? Then the validator skips the residual (casc2, 1,23), the inventory update (casc3, $ mid-posting, so the later balance doesn't fail) and the balance check (casc4, tolerance after the currency), with regression tests.

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 27, 2026 08:39
@robcohen
robcohen force-pushed the feat/lalr-oracle-on branch from a2acccc to 02039dd Compare November 27, 2026 11:35
Base automatically changed from feat/lalr-oracle-on to main November 27, 2026 12:00
robcohen and others added 3 commits November 27, 2026 06:09
The hand-modeled copy of bison's recovery (quiet_bison_recovery, its
phantom re-parses and accepts()) no longer runs; the oracle's trace of
beancount's own parse decides which lines its recovery keeps quiet:

- a line beancount discards whole (it reads no token there) keeps none of
  rledger's errors, builder checks included; so does a tokenless tail read
  while recovering;
- entries beancount's recovery starts mid-line carry its verdict on their
  lines, built or dropped, with the builder errors of the built ones (parsed
  once on their own) and the pipe warnings of the dropped ones;
- rledger's own errors on a line beancount reports (a stray \r) count
  against that line's slots first;
- a number beancount's lexer rejects is synthesized as InvalidNumber, and
  synthesized messages quote tokens as rledger's own do.

Parity: 122,378 of 123,394 inputs match beancount 3.2.3 where 120,099 did,
none lost; BOM matrix unchanged at 13,349 rows. The linear-time test of the
deleted pass is ported to the oracle path. WASM cache v149.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
model_stray_cr_lines parsed the input a second time with each stray \r read
as a blank, and was the last user of BcLexer, the recovery model's port of
beancount's lexer. The error oracle already reads beancount's own tokens, a
stray \r among them as a blank, and a comment already runs through a stray
\r as beancount's does (#3060), so the second parse hid nothing more: the
rest of cst/recovery.rs goes.

Parity unchanged: 122,378 of 123,394 inputs match beancount 3.2.3, none
lost; BOM matrix and corpus baselines unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The windows convert.rs kept quiet after a line a BOM starts, or a column-0
dated line that opens no directive, are beancount's recovery windows, which
the error oracle now replays from its own grammar. Removing the pass changes
no error count on any parity input (122,376 of 123,394 still match, none
lost); the BOM matrix and corpus baselines are unchanged. Its linear-time
counter test goes with it, its shapes ported to the oracle's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the feat/lalr-delete-recovery branch from a28c9b4 to cb6ff42 Compare November 27, 2026 12:25
@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 36.8ms ± 1.5ms 560.6ms ± 8.6ms 15.2x 42.6 MB / 51.0 MB
investment 60.8ms ± 0.7ms 1888.5ms ± 20.9ms 31.1x 47.3 MB / 59.0 MB
fifo 57.6ms ± 0.8ms 11400.8ms ± 1027.1ms 197.9x 49.3 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 41.2ms ± 10.6ms 36.8ms ± 1.5ms -10.7% ✅ within noise 43.1 MB → 42.6 MB (-1.2%)
investment 62.7ms ± 7.1ms 60.8ms ± 0.7ms -3.1% ✅ within noise 46.5 MB → 47.3 MB (+1.7%)
fifo 58.4ms ± 1.2ms 57.6ms ± 0.8ms -1.4% ✅ within noise 49.3 MB → 49.3 MB (-0.0%)

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

@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.32203% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../rustledger-parser/src/cst/oracle/beancount/mod.rs 99.21% 1 Missing ⚠️
crates/rustledger-parser/src/cst/oracle/mod.rs 99.13% 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 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

@robcohen
robcohen merged commit 40803ce into main Nov 27, 2026
65 of 66 checks passed
@robcohen
robcohen deleted the feat/lalr-delete-recovery branch November 27, 2026 12:47
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