Skip to content

fix(validate): no balance error for a transaction beancount discards at a parse error on its lines or an invalid-date directive after it - #2992

Merged
robcohen merged 3 commits into
mainfrom
fix/e3001-indented-invalid
Nov 15, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/e3001-indented-invalid

Conversation

@robcohen

@robcohen robcohen commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

What

An unbalanced transaction no longer gets a balance error (E3001) where beancount discards it:

  • a parse error on one of its own lines: an indented 2024-13-45, 1,50, 1,,000 USD, 5, "s", T, !;
  • a column-0 directive whose date is no calendar date right after it (2024-02-30 open Assets:C, 2024-13-45 * "y"), also after an indented comment line.

After a blank line the transaction is complete in beancount too and keeps its residual, as before.

Why

bean-check 3.2.3 fails a transaction's statement at any error on its lines, a grammar error as well as one of its lexer's, and at a lexer error right where the statement goes on, and its recovery discards the transaction with no residual. Measured with beancount.loader.load_string:

Next line(s) after an unbalanced transaction bean-check 3.2.3 main this PR
2024-13-45, 1,50, 1,,000 USD, 5 no E3001 E3001 no E3001
; c then 2024-13-45 no E3001 E3001 no E3001
2024-02-30 open Assets:B (directly, or after ; c) no E3001 E3001 no E3001
; c then 2024-13-45 * "y" no E3001 E3001 no E3001
blank line, then 2024-02-30 open Assets:B E3001 E3001 E3001
#t after the first posting, a repeated metadata key E3001 E3001 E3001

Not every error on a transaction's lines makes beancount discard it. Its builder (grammar.py) reports some while it still builds the transaction: a duplicate cost, date or label in a cost spec, cost merging, a duplicate merge-cost spec, a negative price, a per-unit cost inside {{ }}, cost and price currencies that differ, a total price without units, a tag or link line after the first posting, a repeated metadata key. And rledger reports some diagnostics beancount does not give at all: an amount past Decimal's precision, trailing tokens after 1,,000 (one number, 1000, in beancount), the deprecated pipe (P0024), a BOM read into an account (P0026). The transaction is kept there, so its residual is checked.

How

  • transaction_holds_invalid_token (crates/rustledger-parser/src/cst/convert.rs) also answers true when the parser reports, inside the transaction's node, an error at which beancount's grammar or lexer fails the statement (fails_inside, fails_the_statement). That is an allow-list: a date that is no calendar date (InvalidDate, InvalidDateValue), the lexer-level kinds (UnexpectedChar, UnexpectedEof, UnclosedString, InvalidEscape, IndentationError; listed for completeness, though the current parser constructs none of them, so in practice the invalid-token path below covers lexer failures), the parser's one generic grammar error (unexpected input, identified by its message as the other recovery passes already do) and an unclosed cost spec. Every other error keeps the residual: the builder checks and rledger-only diagnostics above, and any diagnostic added later, so a gap can only leave a residual reported, never drop one beancount reports. (Round 1 of this PR had the opposite design, a deny-list of builder errors, which the review showed missed several.)
  • kept_under_error_recovery (crates/rustledger-validate/src/validators/transaction.rs) passes the predicate the line right after the transaction's span too (line_at). A directive's span runs to the next directive's start, but a line with an invalid date builds no directive, so when it ends the file it was outside the span and compat(validate): an unbalanced transaction followed directly by a column-0 invalid token keeps its E3001, unlike beancount #2976's next-line rule never saw it. The line is read through the existing SourceLines::text API, in 64-byte chunks and a character at a time where a chunk would run past the file's end or split a character, so no API changes.
  • Re-measured on current main first: T@ and $x already matched after fix(parser): read a lone capital glued to a non-blank as one invalid token, as beancount does #2990, as the issue's T@ row now does.

Differential

Per-transaction E3001 lines compared between rledger check --format json (main and this branch) and beancount.loader.load_string.

Random: 48,000 generated ledgers (4 seeds of 12,000): 1 or 2 unbalanced or balanced transactions with 12 tail shapes (costs, prices, metadata, repeated keys, tag lines before and after the first posting), 5 header shapes (incl. the pipe), 10 separators (none, blank, column-0 comment, one or two indented comments, whitespace-only), then an indented lead (46 kinds, valid and invalid, incl. BOM-led accounts and different-currency costs and prices), a column-0 lead (31 kinds: an invalid-date directive of each kind, invalid tokens, valid directives, option, pushtag, include, BOM lines) or nothing; 15% CRLF, 15% no final newline.

Targeted: 891 ledgers, one per builder error in beancount's grammar.py and per rledger-only diagnostic (the review's shapes), each as the first, middle or last line of an unbalanced transaction, under three headers, followed by nothing, by a blank line and a directive, or in CRLF.

random targeted
match beancount on both 38,940 486
gained (main differed, branch matches) 8,208 108
regressed (main matched, branch differs) 0 0
differ on both, branch closer 55 0
differ on both, unchanged 797 297
crashes 0 0

"0 regressed" is a measurement over these sets, not a proof. The unchanged mismatches are pre-existing and outside this issue: postings with a different-currency cost or price, where rledger reports a residual per currency; a bare @; a BOM-led account posting; negative prices, whose weight beancount computes differently; a header with too many strings, which beancount does not build and rledger keeps (the safe side of the allow-list, as on main); and grammar failures rledger reports with a specific message (unexpected trailing tokens after posting amount on Assets:A 1 USD 5), also left on the safe side.

Parse errors are unchanged: parse_error_lines gives byte-identical output on main and the branch for all 48,000 random inputs.

Caches

  • WASM CACHE_VERSION 122 to 123: both payloads archive validation findings, and residuals move.
  • Loader stays at 75: every affected source has a parse error, before and after, which the loader's cache is never saved for; parse errors and parsed directives do not move.

Tests

  • crates/rustledger-parser/tests/txn_failed_statement.rs (new): indented grammar and lexer errors (11 lines, each also after an indented comment and with CRLF), invalid-date directives after the block (5, also after an indented comment, and kept after a blank line), and the transactions beancount builds: every builder-error shape from the review and grammar.py (24 lines, LF and CRLF), the pipe, and rledger-only diagnostics (1,,000, an over-long amount, a BOM-led account). Expectations were checked against beancount.loader.load_string: a discarded transaction reports no residual, and a kept one is built.
  • crates/rustledger-loader/tests/surface_agreement/cases.rs: a case with an indented invalid date, a tag line after a posting (kept), and an invalid-date directive ending the file, on every surface.

How to test

cargo test -p rustledger-parser --test txn_failed_statement
cargo test -p rustledger-loader --test surface_agreement_test
cargo test -p rustledger-wasm --lib cache

Closes #2987

🤖 Generated with Claude Code

…at a parse error on its lines or an invalid-date directive after it

bean-check 3.2.3 fails a transaction's statement at any error on its
lines, a grammar error as well as one of its lexer's, and at a column-0
directive whose date is no calendar date right after it, and discards the
transaction with no residual. rledger reported its E3001 too.

transaction_holds_invalid_token now also asks whether the parser reports
an error inside the transaction's node, excluding the errors beancount's
builder reports while it still builds the transaction (a tag line after
the first posting, a repeated metadata key) and two notices beancount does
not give (the deprecated pipe, a BOM read into an account). The balance
check passes it the line after the transaction's span too, since an
invalid-date line builds no directive and was outside the span at the end
of a file.

48,000 generated ledgers: 8,307 gained, 0 regressed, 50 closer; parse
errors unchanged.

Closes #2987

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 15, 2026 07:56
@codecov

codecov Bot commented Nov 15, 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 15, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 41.0ms ± 1.3ms 494.3ms ± 20.8ms 12.0x 45.2 MB / 51.0 MB
investment 59.5ms ± 1.8ms 1960.7ms ± 78.8ms 32.9x 47.0 MB / 59.0 MB
fifo 57.6ms ± 1.0ms 11400.8ms ± 44.4ms 198.1x 49.2 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 39.3ms ± 0.7ms 41.0ms ± 1.3ms +4.5% ✅ within noise 45.1 MB → 45.2 MB (+0.2%)
investment 63.1ms ± 5.3ms 59.5ms ± 1.8ms -5.7% 🟢 faster 46.3 MB → 47.0 MB (+1.6%)
fifo 58.8ms ± 3.5ms 57.6ms ± 1.0ms -2.1% ✅ within noise 49.0 MB → 49.2 MB (+0.5%)

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

@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

… lexer fails

Round-1 review of #2992: the deny-list of beancount's builder errors missed
several (a duplicate cost, date or label in a cost spec, a negative price,
a per-unit cost in {{ }}, cost and price currencies that differ), and
rledger-only diagnostics (an over-precise amount, trailing tokens after
1,,000) also hid an E3001 that beancount reports.

fails_the_statement is now an allow-list: an invalid date, the lexer-level
kinds, the parser's generic "unexpected input" and an unclosed cost spec.
Every other error, and any diagnostic added later, keeps the residual.
The message constants of round 1 are gone.

Random 48,000 ledgers: 8,208 gained, 0 regressed, 55 closer. Targeted
891 ledgers (every builder error in grammar.py): 108 gained, 0 regressed.

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(validate): an unbalanced transaction keeps its E3001 before an indented invalid line or an invalid-date directive, unlike beancount

1 participant