Repository navigation
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
Conversation
…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]>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
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]>
This was referenced Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
An unbalanced transaction no longer gets a balance error (
E3001) where beancount discards it:2024-13-45,1,50,1,,000 USD,5,"s",T,!;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:2024-13-45,1,50,1,,000 USD,5; cthen2024-13-452024-02-30 open Assets:B(directly, or after; c); cthen2024-13-45 * "y"2024-02-30 open Assets:B#tafter the first posting, a repeated metadata keyNot 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 pastDecimal's precision, trailing tokens after1,,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 existingSourceLines::textAPI, 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.T@and$xalready matched after fix(parser): read a lone capital glued to a non-blank as one invalid token, as beancount does #2990, as the issue'sT@row now does.Differential
Per-transaction
E3001lines compared betweenrledger check --format json(main and this branch) andbeancount.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.pyand 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."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 amountonAssets:A 1 USD 5), also left on the safe side.Parse errors are unchanged:
parse_error_linesgives byte-identical output on main and the branch for all 48,000 random inputs.Caches
CACHE_VERSION122 to 123: both payloads archive validation findings, and residuals 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 andgrammar.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 againstbeancount.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
Closes #2987
🤖 Generated with Claude Code