Skip to content

fix(parser): drop a sign check that ends at its line's first invalid token, as beancount does - #2981

Merged
robcohen merged 3 commits into
mainfrom
fix/sign-check-invalid-token
Nov 14, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/sign-check-invalid-token

Conversation

@robcohen

@robcohen robcohen commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

What

A posting's sign check that ends right where its line's first invalid token starts is no longer reported beside the token's own error. The same goes for a balance or price amount check that ends there.

Line (after two opens and a * header) bean-check 3.2.3 main this PR
Assets:A -$x 1 2 1
Assets:A +$ 1 2 1
Assets:A -$~ 2 USD, -$~@@, -$~ "s", -$~ 1,000 1 2 1
Assets:A -$ { 1 2 1
Assets:A -<BOM> { 1 2 1
Assets:A +<BOM>x EUR \x11 2 3 2
2024-01-02 balance Assets:A 1 +2x EUR (header) 1 2 1
2615-2-3# / \tG:Ψ+t+{ (fuzz input), line 2 1 2 1

Why

quiet_after_invalid_tokens (cst/convert.rs) drops a construct check that starts before a line's first invalid token, when the line's grammar reads up to that token, because beancount's grammar fails at the token and reports nothing else for the line. It required the check to run past the token's start (e.span.end > at). A sign check covers the sign and the blanks after it, so in -$x it ends exactly at the token and was kept. The condition is now >=. The existing guards are unchanged: only construct checks (not line errors), and only when the line's grammar reads up to the token (indented_grammar_reaches / header_grammar_reaches).

Generated differential

Two generators, each input run through bean-check 3.2.3 (the harness's run_beancount), main (9e834a94ac2) and this branch, comparing per-line counts:

  1. Targeted (426,522 inputs + 64,982 controls): posting, metadata and header lines (balance, price, open, custom, note, pad, txn headers…) built from 55 posting, 13 metadata and 33 header fragments that end right before one of 22 invalid tokens ($, ~, `, \x11, BOM, ?, %, |, ...). Each fragment is followed by 0 to 2 blanks and one of 15 tails, with LF, CRLF or EOF endings, 2-space, 4-space or tab indents, an erroring line before (or not), postings after (or not), and a BOM at file start (or not). Controls are the same shapes with a valid token in place of the invalid one and no invalid token anywhere.
    • 6,901 gained, 0 regressed; 27 that mismatch on both moved closer to beancount, none further.
    • 0 of 64,982 controls changed (full error lists, messages included).
  2. Offset insertion (24,710 inputs): each of 9 invalid pieces inserted at, or replacing, every byte of 9 base ledgers (every directive kind, cost and price specs, arithmetic, metadata), LF and CRLF.
    • 1,603 gained, 16 regressed, 31 closer, none further.

Only two messages are ever dropped: the posting sign check (6,915 + 1,650 inputs) and the balance/price header's "malformed amount" (13).

Known mismatch: a word our lexer does not read as invalid (#2982)

The round-1 review ran its own differential: 394,277 inputs, 3,513 gained, 205 regressed, 292 closer. Every regression there, and the 16 from the offset generator above, has one mechanism. A sign, or a balance/price amount, is followed by any invalid token, and later on the same line comes a capital-led word with a character our lexer reads as a token of its own (U~SD, U?SD, U%SD, U$ D). beancount reads that word as one more invalid token; our lexer reads valid tokens. On main, the dropped sign or amount check stood in for the missing error. Without it, the line counts what main counts with no check there.

  • 198 postings: the sign before any first invalid token: $ (46), -$ (15), ' (13), <BOM>x (12), x (11), $~, a backtick, \x7f, $x, =, <, \, €, \x11, a BOM, .5 (7).
  • 7 headers, where "malformed amount" is dropped.
Line bean-check 3.2.3 main this PR main, without the sign / with a plain amount
Assets:A -$ U~SD 2 2 1 1 ($ U~SD)
Assets:A -' 1 U%SD 2 2 1 1
Assets:A -x U?SD 2 2 1 1
Assets:A -.5 U~SD 2 2 1 1
2024-01-02 balance Assets:A 1 + 2x U?SD 2 2 1 1 (balance Assets:A 2x U?SD)
2024-01-02 price HOOL -1 + 2< U?SD 2 2 1 1 (price HOOL 2x U?SD)

Posting rows are in a transaction after two opens and Assets:B 1 USD. Every row was checked with bean-check 3.2.3. test_parse_check_dropped_before_a_word_we_do_not_read_as_invalid pins posting and header rows with each analog. The fix belongs in the lexer and is tracked in #2982.

Parity corpus

main this PR
Matching (of 111,323) 79,511 79,549

38 inputs newly match and none are lost. 4 still-mismatching fuzz inputs change category (lexer-token-split to rl-indented-line); on each, every changed line moved to beancount's count, and what remains is the existing orphan-indented-line gap. The manifest is updated with --update, and --check passes.

Caches

  • WASM cache 115 → 116: it archives parse errors, and these change.
  • Loader cache unchanged at 75: only errors on a line that keeps its invalid token's error are dropped, so every affected file has parse errors before and after, and the loader caches only error-free loads. Parsed directives are unchanged (the pass only filters errors).

How to test

cargo test -p rustledger-parser --test construct_check_at_invalid_token
python3 -I scripts/compat-parse-error-parity.py --rledger target/release/examples/parse_error_lines \
  --fuzz-dir <fuzz-corpus at 9bc79f4f> --fuzz-commit 9bc79f4f35f52c50813164f43307e7d151d70d8c \
  --compat-dir tests/compatibility/files --check

Parser tests (all 42 suites), rustledger-wasm cache tests, clippy -D warnings, cargo fmt --check and cargo doc with -Dwarnings pass. Two rustledger-loader discover tests fail locally on main as well (they walk up from a temp dir); they are not touched here.

Closes #2967

🤖 Generated with Claude Code

…token, as beancount does

A posting's sign check covers the sign and the blanks after it, so in
`-$x` it ends exactly where the invalid token starts.
quiet_after_invalid_tokens dropped a construct check only when it ran
past the token's start, so this one was kept beside the token's own
error, one more per line than bean-check 3.2.3, whose grammar reads the
sign and fails at the token. The same held for a balance or price
amount check. A check that ends at the token is now dropped like one
that runs past it, still only when the line's grammar reads up to the
token.

Parity corpus: 79,511 -> 79,549 matching, none lost. WASM cache 115 ->
116; loader cache unchanged (every affected file errors before and
after).

Closes #2967

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

codecov Bot commented Nov 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 39.9ms ± 0.8ms 492.0ms ± 5.8ms 12.3x 45.2 MB / 51.0 MB
investment 58.8ms ± 0.5ms 1964.8ms ± 18.4ms 33.4x 47.2 MB / 59.1 MB
fifo 57.5ms ± 0.5ms 11651.5ms ± 26.4ms 202.5x 49.2 MB / 59.1 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 39.4ms ± 0.7ms 39.9ms ± 0.8ms +1.2% ✅ within noise 45.1 MB → 45.2 MB (+0.1%)
investment 58.9ms ± 0.6ms 58.8ms ± 0.5ms -0.1% ✅ within noise 47.0 MB → 47.2 MB (+0.4%)
fifo 57.7ms ± 0.6ms 57.5ms ± 0.5ms -0.3% ✅ within noise 49.3 MB → 49.2 MB (-0.2%)

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

robcohen and others added 2 commits November 14, 2026 14:12
… in for

The round-1 review differential found the known mismatch is wider than
`-.5 <word>`: a sign before any first invalid token, and a balance or
price amount check, both followed by a word our lexer reads as valid
tokens (U~SD, U?SD, U%SD). Pin posting and header rows, each checked
against bean-check 3.2.3, and reword the CHANGELOG. The lexer gap is
#2982.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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
robcohen merged commit 48ddb34 into main Nov 14, 2026
62 of 63 checks passed
@robcohen
robcohen deleted the fix/sign-check-invalid-token branch November 14, 2026 20:53
robcohen added a commit that referenced this pull request Nov 14, 2026
…r, and report a header defect before a tag (#2968) (#2983)

* fix(parser): no error for a BOM-led account after a line's first error, 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]>

* test(parser): pin and document the tag-side counts change 2 moves (#2968)

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]>

---------

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.

compat(parser): a posting's sign check that ends at an invalid token is one error more than beancount

1 participant