Skip to content

fix(validate): a transaction beancount's grammar discards moves no inventory - #3066

Merged
robcohen merged 1 commit into
mainfrom
fix/casc3-parse-error-locations
Nov 27, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/casc3-parse-error-locations

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

A transaction beancount's grammar discards now moves no inventory, so a later balance sees its account without that transaction's postings (casc3, left open by #3064):

2024-01-01 open Assets:A
2024-01-01 open Assets:B
2024-01-02 * "x"
  Assets:A  1 $ USD
  Assets:B  -1 USD
2024-01-03 balance Assets:A 0 USD
  • bean-check 3.2.3: only Invalid token: '$'. beancount drops the transaction, so the balance holds.
  • rledger before this PR: kept Assets:A 1 under error recovery, booked it, and reported E2001 Balance failed beside the parse error.
  • Now: validate_transaction_late returns before the balance check and the inventory update when beancount discards the transaction. That is decided by rustledger_parser::beancount_drops_directive, from the error oracle's reduction trace.

Parse-error locations plumbed to the validator

The validator asks only about transactions holding a parse error, so a clean ledger never reads its source text.

  • rustledger_validate::SourceLines gains:
    • with_parse_errors(by_file), sorted per file;
    • with_file_parse_errors(file_id, starts), which replaces one file's list (for an edited buffer);
    • parse_error_in(file_id, range) -> Option<bool>, which returns None when the locations were not given, in which case behaviour is as before.
  • rustledger_loader::SourceMap gains set_parse_errors and parse_error_starts. The loader records each file's error starts where it records the file's parse errors, and source_lines / source_lines_overlaid hand them on. So CLI, WASM, FFI and the LSP's loaded path all get them.
  • The LSP gives the edited buffer's own fresh parse errors, on both its loaded and single-file paths.
  • The line after the transaction is checked too. A parse error starting on the line after the transaction counts, as in directive_text (compat(validate): an unbalanced transaction keeps its E3001 before an indented invalid line or an invalid-date directive, unlike beancount #2987).

Tests

  • Surface agreement: a new case, "a transaction beancount's grammar discards moves no inventory", run by every surface. I checked that it fails without the fix (E2001 comes back).
  • Unit tests: cover SourceLines::parse_error_in and with_file_parse_errors.
  • Local runs: workspace clippy with -D warnings is clean, and the parser, validate, loader, lsp, wasm and rustledger tests all pass.

Caches

WASM cache v154. The loader cache is unchanged, since it is never saved for a source with a parse error.

Merge order

This PR is independent of the others. The cache numbers assume this order:

  1. feat(parser): keep one BOM diagnostic per line where beancount reads a BOM into an account #3065 (v151)
  2. drop-line-error (v152)
  3. multi-line strings (v153)
  4. this PR (v154)
  5. quiet_after_invalid_tokens deletion (v155)

If this PR merges earlier, I will renumber.

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 27, 2026 14:25
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 39.5ms ± 0.6ms 491.8ms ± 5.7ms 12.5x 45.1 MB / 51.1 MB
investment 59.0ms ± 0.8ms 1970.2ms ± 13.1ms 33.4x 47.1 MB / 59.2 MB
fifo 57.6ms ± 0.7ms 11498.0ms ± 48.1ms 199.5x 49.0 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 40.0ms ± 0.5ms 39.5ms ± 0.6ms -1.4% ✅ within noise 45.0 MB → 45.1 MB (+0.2%)
investment 59.4ms ± 0.5ms 59.0ms ± 0.8ms -0.7% ✅ within noise 46.5 MB → 47.1 MB (+1.5%)
fifo 57.9ms ± 0.7ms 57.6ms ± 0.7ms -0.5% ✅ within noise 49.3 MB → 49.0 MB (-0.5%)

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

@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 828
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

…ventory

`Assets:A  1 $ USD` makes beancount drop the whole transaction, so a
later `balance Assets:A 0 USD` holds there. rledger kept the posting
under error recovery, booked it into the validator's inventory, and
reported the balance failing (E2001) beside the parse error (casc3).

The late transaction check now returns before the balance check and the
inventory update when beancount discards the transaction
(rustledger_parser::beancount_drops_directive). It asks that only for a
transaction a parse error starts in, or on the line after, so a clean
ledger never reads its source: rustledger_validate::SourceLines gains
with_parse_errors / with_file_parse_errors / parse_error_in, and
rustledger_loader::SourceMap records each file's parse-error starts
(set_parse_errors / parse_error_starts), which source_lines hands on to
every surface. The LSP gives the edited buffer's own.

Pinned on every surface by a surface-agreement case. The semantic
differential re-pins two parser-lima ledgers: beancount's `Balance
failed` after a dropped transaction now matches (ErrorInTransactionLine
matches outright; ErrorInPosting's next difference is its balances).
WASM cache v154;
the loader cache is never saved for a source with a parse error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/casc3-parse-error-locations branch from 955d5fb to 316fd62 Compare November 27, 2026 23:15
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-validate/src/lib.rs 96.42% 2 Missing ⚠️
crates/rustledger-loader/src/source_map.rs 88.88% 1 Missing ⚠️
crates/rustledger-lsp/src/handlers/diagnostics.rs 90.90% 1 Missing ⚠️
.../rustledger-validate/src/validators/transaction.rs 94.11% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 8b76b3a into main Nov 27, 2026
67 of 71 checks passed
@robcohen
robcohen deleted the fix/casc3-parse-error-locations branch November 27, 2026 23:36
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.

1 participant