Skip to content

fix(validate): no balance error for a transaction a column-0 invalid token breaks into, as in beancount - #2986

Merged
robcohen merged 1 commit into
mainfrom
fix/col0-invalid-after-txn
Nov 15, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/col0-invalid-after-txn

Conversation

@robcohen

@robcohen robcohen commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

What

An unbalanced transaction followed directly (no blank or column-0 comment line between) by a line that starts with a token beancount's lexer rejects no longer gets an E3001 beside the token's parse error. bean-check 3.2.3 fails the statement at that token and its recovery discards the transaction, so the token is the one error there.

2024-01-01 open Assets:A
2024-01-01 open Equity:E
2024-01-02 * "x"
  Assets:A  1 USD
  Equity:E  -2 USD
$x
errors
bean-check 3.2.3 line 6: the invalid token
main line 6 P0012, line 3 E3001
this PR line 6 P0012

Why and how

#2984 (#2956) already made transaction_holds_invalid_token answer true for a line a mid-file BOM starts right after a transaction's block. Its next-sibling branch now calls a new helper, line_leads_with_an_invalid_token, which reuses the parser's existing rules for what beancount's lexer rejects instead of adding a copy:

First token of the line Rule (existing canonical) beancount
an invalid token here ($x, x, k:, 'Z, =, \, €, a control character) ERROR_TOKEN discards the transaction
a date that is no calendar date (2024-13-45, 2024-02-30) parse_date_token discards
a number beancount refuses (1,50, 1,0) holds_a_misgrouped_number discards
a lone capital glued to what follows it (T@, X(, P") holds_a_glued_lone_capital discards
BOMs not read into an account (<BOM>$x) bom_led_account (unchanged) discards
a token beancount lexes (an account, a number, ~, *, &, ` , a tag, a link, TRUE, {, a string, a valid date, T x`) none

An indented comment line between the block and the line does not end the block, in beancount or here; a blank or column-0 comment line does, and the residual is reported.

1,,000: checking 1,,000 showed beancount's number validator accepts one doubled comma after a single digit (1,,000, 0,,111, 1,,000,000, 1,,000.00), though its documented rule does not. I enumerated every run of digits and commas up to nine long through beancount's lexer, and that is the only shape that differs from the strict rule (11,,111, 1,,111,,111, 1,,,111 are errors). holds_a_misgrouped_number now accepts it. That predicate is shared with #2985 (#2979), so a quiet line under a failed dated line holding 1,,000 is now quiet as in beancount (2024-01-02 123 * "n" / Assets:A 1,,000 USD: bean-check {4}, main {4, 5}, this PR {4}).

Measurements

Full-check differential: per ledger, whether a balance residual is reported, rledger check --format json (main 8894a1b and this branch) against beancount's loader.load_string (3.2.3) in the compat container. Ledgers: 9 transaction shapes (plain, metadata last, transaction metadata, a cost or a price on the last posting, a comment last, one posting, an unknown account, balanced) × 106 column-0 line leads × 4 separators (none, blank line, column-0 comment, indented comment) × LF / CRLF / no final newline, plus two transactions in a row and the leads after balance, open and note.

ledgers main matches beancount this PR matches beancount gained regressed
first lead set 7,056 6,316 7,056 740 0
extended lead set (capitals, number groupings, date forms, BOM variants) 12,320 10,681 12,320 1,639 0

Parse-error parity (pinned fuzz corpus 9bc79f4, compat files, tracked ledgers): --check passes with no regressions and no manifest change. corpus_baseline passes against the fetched compat corpus.

Not changed (documented)

When beancount discards such a transaction it reports none of its other findings either. These stay as on main, as #2956 left them:

Finding on the discarded transaction ledgers in the differential (sep none, LF) beancount rledger
E3004 one-posting warning 63 none (beancount has no such warning) reported
E1001 unknown account 21 none reported
E2001 on a balance directive followed by such a line 21 none (the directive is discarded too) reported

E3004 is an rledger addition (#2872 asks whether it is deliberate). The other two would need the validator to treat the whole directive as discarded, not only its residual; that is a larger change than this issue.

Caches

  • WASM CACHE_VERSION 119 → 120: both payloads archive validation findings and parse errors, and both move.
  • Loader cache stays at 75: every affected source has a parse error before and after, and the loader caches only error-free loads; parsed directives do not change.

How to test

cargo test -p rustledger-parser --test col0_invalid_after_txn
cargo test -p rustledger-parser --test dated_line_no_directive
cargo test -p rustledger-loader --test surface_agreement_test
cargo test -p rustledger-wasm --lib cache

The new surface-agreement case puts $x right after one unbalanced transaction and after a blank line following another; bean-check reports lines 7 and 13 for the tokens and the residual only on line 9, as every surface now does.

Closes #2976

🤖 Generated with Claude Code

…token breaks into, as in beancount

An unbalanced transaction followed directly (no blank or column-0 comment
line between) by a line led by a token beancount's lexer rejects got an
E3001 beside the token's parse error. bean-check 3.2.3 fails the statement
at that token and its recovery discards the transaction, so the token is
the one error there.

transaction_holds_invalid_token already answered true for a line a
mid-file BOM starts there (#2956). Its next-sibling branch now asks
line_leads_with_an_invalid_token, which reuses the parser's own rules for
what beancount's lexer rejects: an invalid token, a date that is no
calendar date (parse_date_token), a number beancount refuses
(holds_a_misgrouped_number), a lone capital glued to what follows it
(holds_a_glued_lone_capital), and BOMs not read into an account
(bom_led_account). A line led by a token beancount lexes keeps the
transaction and its residual.

holds_a_misgrouped_number now accepts beancount's one doubled comma after
a single digit (1,,000), which its validator takes as a number; checked
over every run of digits and commas up to nine long.

Measured against beancount's full loader on 19,376 generated ledgers:
every residual decision matches (2,379 gained, 0 regressed). WASM cache
v120; the loader cache is unchanged (every affected source has a parse
error).

Closes #2976

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

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 53.7ms ± 3.8ms 827.8ms ± 5.4ms 15.4x 45.1 MB / 51.1 MB
investment 79.4ms ± 1.0ms 2967.0ms ± 44.7ms 37.4x 47.3 MB / 59.1 MB
fifo 77.5ms ± 0.9ms 14812.5ms ± 205.0ms 191.0x 49.2 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 51.8ms ± 0.5ms 53.7ms ± 3.8ms +3.7% ✅ within noise 45.1 MB → 45.1 MB (+0.0%)
investment 79.9ms ± 1.2ms 79.4ms ± 1.0ms -0.6% ✅ within noise 47.0 MB → 47.3 MB (+0.7%)
fifo 77.9ms ± 0.9ms 77.5ms ± 0.9ms -0.5% ✅ 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 1.7ms, investment 0.7ms, 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 15, 2026

Copy link
Copy Markdown

Codecov Report

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

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

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 followed directly by a column-0 invalid token keeps its E3001, unlike beancount

1 participant