Skip to content

fix(parser): keep the lines after a column-0 line beancount cannot read as quiet as beancount does - #3000

Merged
robcohen merged 4 commits into
mainfrom
fix/quiet-after-unreadable-col0
Nov 25, 2026
Merged

robcohen merged 4 commits into
mainfrom
fix/quiet-after-unreadable-col0

Conversation

@robcohen

@robcohen robcohen commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

What

After a column-0 line whose statement fails, bean-check 3.2.3 reports only the lexer's own errors on the lines after it until its grammar has shifted three tokens again. rledger reported each of those lines. This models that window after three more kinds of column-0 line:

  • a line whose first token beancount's lexer rejects ($, $x, 2<BOM>?02, 'Z);
  • a marker alone on its line (!, *, :, #);
  • an undated declaration holding such a token (pushmeta k: "v").

This is part A of #2993: column-0 unreadable lines. Part B, an indented line after a failed header with a blank or comment line between and the option rows, is not changed here, so #2993 stays open.

Why it works, and where it stops

beancount's grammar has one error rule, declarations: declarations error (grammar.y). During recovery bison discards every token except one a declarations can take next: an end of line, a DATE, or an undated declaration's keyword (option, include, plugin, pushtag, poptag, pushmeta, popmeta). It reports no syntax error until it has shifted three tokens. So an indented posting, or another unreadable line, is discarded to its end. Its lexer errors are still reported, because beancount's lexer reports those itself.

quiet_after_failed_line_starts (crates/rustledger-parser/src/cst/convert.rs) already models this window after a line a BOM starts (#2956) and after a dated line that opens no directive (#2979). The new lines walk with the dated line's rules, the ones calibrated over #2985's review rounds. Where a correct model would need bison's exact token accounting, the window stops and keeps main's errors:

  • A line holding a date or an undated keyword (\t1poptag #t, key:2024/01/03 P | 1, and the unreadable line itself past its first word): bison shifts these and may end its recovery inside the line, and then builds or reports what follows. The check is a byte scan for the date shape and the keyword substrings. That over-detects (inside strings and invalid runs), which only makes more lines keep their errors.
  • A line right after another line with an error (past up to two blank or skipped lines): beancount may still be in its recovery from that line, which is not modeled.
  • A line holding a BOM, and any line a BOM starts: these are left to the BOM handling of compat(parser): a BOM at the start of a line ends its block, and hides the rest of a comment line, unlike beancount #2956. BOM and dated windows are unchanged.
  • A node that runs past its line (an unclosed string).
  • A line holding a stray \r (one not right before a \n, anywhere on the line, or at the end of the input): this parser reads it differently from beancount depending on context (\r;c is an error here and a blank line there; fix(parser): read a stray \r before an invalid token as a blank, as beancount does #2980). Such a line starts no window, ends one before it, and counts as a failed line when deciding whether the next line starts one, so the lines around it keep main's errors. Two review rounds found stray-\r shapes the window got wrong (19, then 109 of over a million inputs), so this blanket exclusion replaces shape-by-shape handling and gives up those gains. A plain CRLF line ending is no stray \r. The unreadable line's own date check skips only its first character, so a date glued to it (@2024-1-2) counts as one bison shifts.
  • An indented ; comment line inside an unreadable window counts as a line with tokens, as on main: the round-1 blank_or_comment_line helper that looked past it was removed with the blanket stray-\r rule. Against the intermediate head f407d26 this moved 38 of 50,000 no-\r control inputs (27 closer to bean-check, 11 back to main's count, e.g. x / ;pushmeta / ~: bean-check {1}, main {1,3}, this PR {1,3}); against main nothing is worse. Looking past such lines again is left for part B of compat(parser): stay quiet after any line beancount cannot read, until its grammar has read three tokens #2993.

Results

Issue rows in scope: all match bean-check 3.2.3:

  • the marker rows (!\n!\n, *\n:\n, #\n#\n, !\n!\n!\n, !\n!\n<directive>);
  • comment 1 ($\n2<BOM>?02, with and without a final newline, 1-2-3#x\n2<BOM>?02);
  • comment 2 (pushmeta k: "v" then 2<BOM>024-01-02 close Assets:A or 2$024-01-02 close Assets:A).

Parse-error parity corpus (pinned fuzz 9bc79f4f, matrix, tracked, compat): 82,846 → 92,500 inputs match, with 0 regressions against main's committed manifest. 10,055 pins are stale: 9,654 inputs now match, and 402 others changed category, so their pins are rewritten. The four ledger2beancount rows that local fetching flags as unpinned upstream drift are left out, as in #2988 and #2990.

Source Before After
fuzz 27,820 29,465
matrix 54,138 62,147
compat 506 506
tracked 382 382

Generated differentials: three designs, three seeds each, comparing per-line counts of bean-check 3.2.3 (the harness's run_beancount), main 91f2b8c6273 and this branch.

Generator Inputs Gained Closer Regressed Further Sideways Controls changed
G1, line-type sequences: unreadable lines, failed headers, BOM lines, failed dated lines, lone dates, indented lines, blank and comment lines, and stray \rs at line start and mid-line (\r;c, option \r;c, poptag\t\r#c, 2024-01-02\r;c, x \r;c); CRLF, no final newline, file BOM 450,000 10,830 10,384 0 0 0 102, all gained or closer
G2, random token lines from beancount's lexer.l classes, glued, blank-separated or separated by stray \rs (\r, \r, \r\t, \r;c ) 450,000 4,359 8,109 0 0 0 0
G3, mutations of real ledgers (compat corpus, examples/, parser test data) and the fuzz corpus, with unreadable and stray-\r lines inserted 360,000 29,916 32,744 0 0 0 8,694, all gained or closer

G1's changed controls hold a file-start BOM before a BOM line, which leaves an unreadable $ line. G3's hold the unreadable lines already in the fuzz-corpus files. Neither is a true control change.

Caches: WASM 123 → 124, since it archives parse errors. The loader stays at 75: every changed source has a parse error on its trigger line before and after, and the pass only drops errors, so parsed directives do not move.

Timing: linear. Seconds at 20k / 40k / 80k / 160k repetitions, main then branch:

Shape main branch
$ lines 0.03 / 0.07 / 0.17 / 0.34 0.06 / 0.08 / 0.17 / 0.34
! lines 0.04 / 0.07 / 0.10 / 0.22 0.02 / 0.04 / 0.06 / 0.13
$ and a posting 0.07 / 0.14 / 0.29 / 0.56 0.10 / 0.16 / 0.34 / 0.75
pushmeta k: lines 0.06 / 0.09 / 0.18 / 0.36 0.05 / 0.09 / 0.25 / 0.34
one long $ $ $ line 0.02 / 0.04 / 0.09 / 0.18 0.03 / 0.05 / 0.10 / 0.24
$ then postings 0.04 / 0.10 / 0.17 / 0.33 0.04 / 0.09 / 0.21 / 0.36
2<BOM>?02 lines 0.06 / 0.13 / 0.30 / 0.58 0.07 / 0.15 / 0.28 / 0.64
$ then x 1poptag 0.07 / 0.16 / 0.32 / 0.64 0.09 / 0.17 / 0.37 / 0.64
$ x and a blank line 0.05 / 0.12 / 0.24 / 0.54 0.09 / 0.18 / 0.40 / 0.72

A new counter, RECOVERY_SHIFT_SCANNED_BYTES, guards it: test_parse_unreadable_line_windows_scan_linear_bytes requires at most four times the input over seven shapes. The answer to whether an invalid token is a BOM-led account is now cached per token, so a long BOM run is asked about once. That keeps test_tokenize_long_bom_runs_are_linear within its bound.

Tests

  • New file: crates/rustledger-parser/tests/unreadable_line_recovery.rs. It pins the issue rows, the postings and metadata under an unreadable line (LF and CRLF), the window's end at blank, skipped and directive lines, and every unmodeled shape above at main's count. All counts were checked against bean-check 3.2.3.
  • Updated, toward bean-check:
    • indented_star_line.rs: @@ junk / * heading / * ind now gives line 4 one error, as bean-check does.
    • line_start_bom_recovery.rs: the plain-account line after 5 now gives one error, as bean-check does. The BOM-led account line keeps its count, since the window hands over at a BOM line.
  • BOM combination matrix: regenerated with BOM_MATRIX_UPDATE=1 BOM_MATRIX_RELABEL=1. Two rows moved from the known list to matching (11,164 → 11,166). One listed exception is now explained by the existing inner-bom-under-line-error rule, so it is relabelled with the same counts and removed from the exceptions file.
  • Checks: fmt, clippy with -D warnings (parser, wasm) and cargo doc -Dwarnings pass. All parser tests pass (1,108, including corpus_baseline against the fetched compat corpus), as do the wasm cache tests. The loader suite shows only the two known local discover failures (a stray /tmp/rec_before.beancount).

How to test

cargo test -p rustledger-parser --test unreadable_line_recovery
cargo test -p rustledger-parser
cargo build --release -p rustledger-loader --no-default-features --example parse_error_lines
python -I scripts/compat-parse-error-parity.py --rledger target/release/examples/parse_error_lines \
  --fuzz-dir <fuzz_parse + fuzz_green_eq_red at 9bc79f4f> --fuzz-commit 9bc79f4f35f52c50813164f43307e7d151d70d8c \
  --compat-dir tests/compatibility/files --check

Not changed (documented)

  • compat(parser): stay quiet after any line beancount cannot read, until its grammar has read three tokens #2993's part B: an indented line after a failed header with a blank line between (pushtag #a $ / blank / 2024-02-30 5: bean-check {2, 4}, main and branch {2, 4x2}), the unterminated-string and comment-line rows, and the option rows.
  • Lines that keep main's count where bean-check differs, each pinned in the new test file:
    • AB-C / key:2024/01/03 P | 1: bean-check reports the pipe too;
    • $ 2024-01-02 * "y" with a posting;
    • $ / Assets:A 1 USD 2024-01-02;
    • $ / 2024-01-02 123 / posting.

Part of #2993

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 15, 2026 15:46
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 51.7ms ± 0.7ms 794.3ms ± 6.0ms 15.4x 44.9 MB / 51.1 MB
investment 77.2ms ± 0.8ms 2892.0ms ± 13.8ms 37.5x 47.0 MB / 59.1 MB
fifo 74.7ms ± 0.4ms 14559.6ms ± 89.1ms 195.0x 48.8 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 51.5ms ± 0.4ms 51.7ms ± 0.7ms +0.3% ✅ within noise 44.9 MB → 44.9 MB (+0.2%)
investment 76.8ms ± 0.6ms 77.2ms ± 0.8ms +0.4% ✅ within noise 46.9 MB → 47.0 MB (+0.3%)
fifo 74.8ms ± 0.6ms 74.7ms ± 0.4ms -0.2% ✅ within noise 49.2 MB → 48.8 MB (-0.8%)

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

@github-actions

github-actions Bot commented Nov 15, 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

robcohen and others added 4 commits November 24, 2026 22:14
…ad as quiet as beancount does

After a column-0 line whose statement fails (a first token beancount's
lexer rejects, a marker alone on its line, an undated declaration holding
such a token), bean-check 3.2.3 reports only the lexer's own errors until
its grammar has shifted three tokens again. quiet_after_failed_line_starts
now models that window for these lines with the dated line's rules, and
quiets a line only where beancount's recovery surely discards it: a line
holding a date or an undated keyword (which bison shifts in its recovery,
per its one error rule declarations: declarations error), a line holding a
BOM, and a line right after another failed line keep their errors.

Part of #2993 (part A: column-0 unreadable lines).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
beancount reads a stray CR then a comment as a blank line; this parser reports an error there. That line no longer starts an unreadable-line window, counts as a blank line inside one, and is looked past when deciding whether a line follows another failed line. A date glued to the first token of an unreadable line now counts as one bison shifts. Tests for each reported shape; parity manifest updated.

Part of #2993.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A line holding a stray CR (one not right before a newline, anywhere on the line) is read here differently from beancount by context. Such a line now starts no unreadable-line window, ends one before it, and counts as a failed line in the look-back, so every line around it keeps its errors as on main. This replaces the line-start-only handling of the previous commit, which missed the mid-line forms (option \r;c, poptag\t\r#c, 2024-01-02\r;c). Tests for each reported shape; parity manifest updated.

Part of #2993.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- LSP surface_cleanup test: open / # / @@@ junk is now 2 errors, as
  bean-check 3.2.3 reports (the marker line and the invalid token).
- Doc comments: a string unclosed to the end of the input is cut at its
  line and does start a window, matching beancount; pin it with a test.
- unreadable_lines: skip nodes not at a line start before scanning for
  the line end.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/quiet-after-unreadable-col0 branch from 3d8009c to c165a0b Compare November 25, 2026 04:19
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.31034% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/convert.rs 99.20% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 540944c into main Nov 25, 2026
62 of 63 checks passed
@robcohen
robcohen deleted the fix/quiet-after-unreadable-col0 branch November 25, 2026 16:25
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