Skip to content

fix(parser): no error for a BOM-led account after a line's first error, and report a header defect before a tag (#2968) - #2983

Merged
robcohen merged 3 commits into
mainfrom
fix/bom-recovery-rows
Nov 14, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/bom-recovery-rows

Conversation

@robcohen

@robcohen robcohen commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

What changed

Two error counts after a line's grammar has already failed, both found in the #2931 reviews and both pre-existing on main (#2968's update section).

  1. A BOM-led account after an earlier error on its line is no error. beancount 3.2.3 reads a BOM into an account (<BOM>Assets:A, <BOM>xAssets:A), so it is no invalid token there; the ACCOUNT is an error only when the grammar fails right at it. After an earlier error on the line (an invalid token, Assets:A $@ <BOM>Assets:A, or a defect the grammar fails at first, commodity HOOL TRUE <BOM>Assets:A), its grammar is recovering and reports nothing for it. rledger reported a BomInDirectiveBody there. quiet_after_invalid_tokens now drops that P0026 when a kept error before it on the same line counts as such a failure point: one past the line's content start, or an invalid token's own. A whole-line error (which starts at the indentation, <BOM>Assets:A 1 USD) does not count, so that pair stays as the BOM combination matrix's rules model it. Linear: each line's end and content start are found once.

  2. A header defect before a tag or link is reported in the tag's place. reject_tags_and_links reports a tag on a directive that does not take one, and the header check (directive_header_check) then never ran, because the converter had reported an error. With an invalid token between the two (close Assets:A TRUE $x #t), the tag's error is dropped after the token, so the line lost the unexpected BOOL beancount reports first. The header check now also runs when the converter's only new errors are tag/link rejections (is_tag_or_link_rejection, matched by a shared message constant), and its defect replaces them when it comes before the first tag. A tag before the defect (close Assets:A #t TRUE) keeps its own message.

Issue rows (bean-check 3.2.3 / main / this PR, the line shown)

After 2024-01-01 open Assets:A and 2024-01-01 open Assets:B:

Input bean-check main PR
2024-01-02 * / Assets:A $@ <BOM>xAssets:A.00 / Assets:B 2 3 2
2024-01-02 close Assets:A TRUE <BOM>x (k: #t Assets:A<BOM>~ 3 2 3
Assets:A $@ <BOM>Assets:A (in a transaction) 1 2 1
Assets:A $@ <BOM>Assets:A.00 2 3 2
close Assets:A TRUE $x #t 2 1 2
close Assets:A TRUE $x (k: #t Assets:A$~ 4 3 4
close Assets:A TRUE <BOM>x 2 2 2

Also fixed, from the differential: Assets:A <BOM>Assets:B <BOM>Assets:A 1/2/1; commodity HOOL TRUE <BOM>Assets:A 1/2/1; Assets:A 1 USD ! <BOM>Assets:A 1/2/1; close Assets:A TRUE #t ^l 1/2/1; open Assets:C USD TRUE $x #t, event "a" "b" TRUE $x #t, pad Assets:A Assets:B 1 $x ^l, balance Assets:A 1 USD TRUE $x #t 2/1/2.

Differential

My own generator (posting, metadata and 17 header shapes, each followed by 1 to 4 pieces from pools of invalid tokens, BOM-led accounts, tags/links and valid tokens, glued or after blanks or a tab; 15% after an erroring line; 15% CRLF, 10% no final newline; postings or a metadata line after), two seeds, run through bean-check 3.2.3 (the harness's run_beancount), main (48ddb343451, with #2981) and this branch, per-line counts. Controls hold no invalid token, no BOM and no tag or link.

Seed Targeted inputs Gained Regressed Closer Further Controls changed
1 337,709 99,943 171 10,940 1 0 of 62,291
2 337,611 99,838 148 10,950 2 0 of 62,389

No input changes on more than one line, and no directive is dropped or added (both changes only remove or replace errors on a line that already has one).

Documented family: the BOM error stood in for one rledger already misses (319 regressions, 3 further)

On every regressed line, rledger already reads the line with one error fewer than beancount, and main's P0026 for the BOM-led account made the count match by chance. 316 of the 319 count exactly what main counts with each BOM-led account written as a plain account (Assets:Z). The other 3 are lines where that rewrite itself changes how a neighbouring token lexes (a BOM glued to a link, ^l<BOM>Assets:A,), so the whole-line analog cannot be built; the round-1 review's own generator found more such partial-rewrite and gluing cases, all the same mechanism. Each differs by one on one line. Examples (bean-check / main / PR, and main on the plain-account version):

Line (in a transaction) bean-check main PR main, plain account
Assets:A ~$ <BOM><BOM>Assets:B -$ 3 3 2 2
Assets:A % $x <BOM>Assets:A, ^a-b 2 2 1 1
2024-01-02 price HOOL 1 USD -1$x <BOM>Assets:A ~$ (header) 3 3 2 2

What rledger misses there: ~$, which beancount reads as ~ (a grammar error) and $; % $x, beancount's FLAG error and $x; -1$x, its MINUS error and $x; a mid-line T#! (below). test_parse_bom_account_no_longer_stands_in_for_a_missed_error pins the first two with their plain-account versions.

Documented family: the tag errors stood in for one rledger already misses (change 2)

Where the header defect now replaces a directive's tag and link errors, a line whose extra tag errors made up for something rledger already misses counts what main counts for the same line with its tags and links removed (the tag-side counterpart of the family above). The round-1 review measured it at about 345 regressions per seed (348 of 1,901 on seed 7, 344 of 1,740 on seed 8), plus about 400 per seed moving further; on its generator change 2 overall gained about 28.7k and regressed about 490 per seed, and change 1 gained about 73k and regressed about 1.3k. Examples (bean-check / main / PR, and main with the tags and links removed), after the two open lines:

Line 3 bean-check main PR main, no tags or links
2024-01-02 event "a" "b" "s" T#! #t #u 2 2 1 1
2024-01-02 balance Assets:A 1 USD { U?SD #t #u 2 2 1 1
2024-01-02 price HOOL 1 USD USD U?SD #t^l 2 2 1 1
2024-01-02 open Assets:C USD "FIFO" 2024-01-01 * ^l #t NULL 2 2 1 1
2024-01-02 close Assets:A {#t #u 1 1 2 2
2024-01-02 query "q" "select 1" {#t #u 1 1 2 2

What rledger misses there: a mid-line T#! and U?SD, each one invalid token in beancount; after NULL, beancount's second grammar error (expecting ... TAG or LINK). The {#t #u rows go the other way: rledger lexes {# and an invalid run where beancount reads { and a tag (the matrix's brace-hash-before-tag), so with the header check now running the line shows main's existing over-count for {#t alone. Avoiding it means changing how {# lexes, which is left out of this PR. test_parse_tag_errors_no_longer_stand_in_for_a_missed_error pins all six with their untagged versions (counts checked with bean-check).

Parity corpus

79,549 of 111,323 matching on main (48ddb343451) and on this branch: none gained, none lost. One fuzz input that mismatches on both moved one closer on one line. --check passes and the manifest is unchanged (the row-1 shapes the issue counted in the corpus mismatch for other reasons too).

BOM combination matrix

tests/bom_combination_matrix.rs (13,438 rows) took the moved counts with BOM_MATRIX_UPDATE=1 BOM_MATRIX_RELABEL=1:

  • 616 rows newly match bean-check and 7 no longer do: 6,998 to 7,607 in the matching list, net +609.
  • The 7 that no longer match all have a mid-line T#! (cause mid-line-hash-bang): the lone capital stays a token here, one error fewer than beancount, and main matched only by the BOM error. They, and 2 rows already in the known list that moved one further for the same reason, are in the known list with that reason (9 accepted lines).
  • 119 accepted further-than-main lines are no longer further and were removed; 33 more now take a recovery cause (indented-line-after-error and others, with the line's standalone count) instead of like-main-without-bom; one new further-than-main line (2024-01-02 ! #t ^l open \tT#!\t(1 + 2) \t<BOM>Ü:A Assets:A, the same T#! case) is accepted as mid-line-hash-bang.
  • One line no rule explains is a listed exception: Heading<BOM>a-b:C~<BOM>1:A<BOM>a-b:C\t{ counts 2 (bean-check 1, main 3): the P0026 after the ~ error is dropped, and the unclosed-cost error is left; the construct rule needs main to count as rledger does, and main still counts the dropped P0026.

Caches

How to test

cargo test -p rustledger-parser --test bom_account_and_tag_recovery
cargo test -p rustledger-parser --test bom_combination_matrix
cargo test -p rustledger-parser
cargo test -p rustledger-wasm --lib cache

Each count in bom_account_and_tag_recovery.rs was checked against bean-check 3.2.3; the tests also assert the green and red paths agree.

Local runs: parser, loader, LSP and WASM cache tests pass, clippy -D warnings, cargo fmt --check and cargo doc with -Dwarnings are clean. Three tests fail locally on main too, from a stray /tmp/rec_before.beancount the discovery walk finds above the temp dir: loader discover::tests::a_non_utf8_file_is_not_a_candidate and skips_files_that_declare_no_include, and LSP main_loop::tests::an_unrelated_root_is_probed_once_not_once_per_file.

Not changed

Closes #2968

🤖 Generated with Claude Code

robcohen and others added 2 commits November 14, 2026 14:53
…r, and report a header defect before a tag

A BOM that beancount reads into an account (`<BOM>Assets:A`) after an
earlier error on its line got its own BomInDirectiveBody, one error more
than bean-check 3.2.3, whose grammar is already recovering there. It is
now dropped after an invalid token or a defect the grammar fails at
first; an error that stands for the whole line does not count as one.

A directive that does not take tags or links was reported only through
its tag's error, so the header check never looked for an earlier defect
(`close Assets:A TRUE $x #t`), and the tag's error is dropped after an
invalid token. The header check now runs there too, and a defect before
the tag is reported in its place.

The BOM combination matrix takes in the moved counts: 618 more rows
match bean-check, and the 9 that no longer do are a mid-line `T#!` that
already counts one fewer here, which the dropped BOM error used to make
up for; each is accepted with its reason.

WASM cache v117 (v116 is held by #2981); the loader cache is unchanged.

Closes #2968

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

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 53.6ms ± 2.8ms 811.8ms ± 16.6ms 15.1x 45.2 MB / 51.0 MB
investment 78.9ms ± 1.5ms 2932.8ms ± 15.5ms 37.2x 46.4 MB / 59.1 MB
fifo 76.0ms ± 1.0ms 14691.7ms ± 67.2ms 193.3x 49.3 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 52.0ms ± 0.4ms 53.6ms ± 2.8ms +3.2% ✅ within noise 45.1 MB → 45.2 MB (+0.1%)
investment 78.5ms ± 1.2ms 78.9ms ± 1.5ms +0.5% ✅ within noise 47.2 MB → 46.4 MB (-1.6%)
fifo 76.0ms ± 1.4ms 76.0ms ± 1.0ms +0.0% ✅ within noise 49.2 MB → 49.3 MB (+0.3%)

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

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/convert.rs 98.14% 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

)

Where the header defect now replaces a directive's tag and link errors,
a line whose extra tag errors made up for a token rledger misses (`T#!`,
`U?SD`, a second grammar error after `NULL`) counts one fewer than
bean-check, and `{#t #u` one more, as `{#t` alone does. Each counts
what main counts with its tags and links removed; pinned with those
versions, and named in the CHANGELOG entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 7eb5a53 into main Nov 14, 2026
62 of 63 checks passed
@robcohen
robcohen deleted the fix/bom-recovery-rows branch November 14, 2026 22:18
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.

compat(parser): two BOM inputs count one error away from beancount on #2931 where main matches

1 participant