Skip to content

fix(parser): report the metadata shapes beancount rejects - #2888

Merged
robcohen merged 4 commits into
mainfrom
fix/metadata-shapes
Nov 7, 2026
Merged

robcohen merged 4 commits into
mainfrom
fix/metadata-shapes

Conversation

@robcohen

@robcohen robcohen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

What changed

Each row of #2813 was checked against bean-check 3.2.3 before coding:

Input Before bean-check 3.2.3 Now
kk: True / true / False / false Bool Invalid token: 'True' parse error (only TRUE / FALSE are booleans)
kk: Foo:Bar (invalid root) Account Invalid account name E1005
indented kk: 1 (or ; c, #tag) after option, include, plugin, pushtag, poptag, pushmeta, popmeta dropped silently syntax error, unexpected INDENT parse error at the first indented line
posting filename: / lineno: accepted Duplicate posting metadata field, at the transaction's line parse error at the transaction's line
kk: 1 / 0 already an error segfaults unchanged (already an error)
kk: 10/4 2.50 2.5 2.5

How each row is implemented:

  • Booleans: the lexer drops its True / true / False / false tokens, so those spellings now lex like any other word, as an error. The spec grammar (spec/core/grammar.peg) already allowed only TRUE / FALSE.
  • Invalid account roots: validators::helpers::push_invalid_meta_accounts checks every account-valued metadata entry with the canonical validate_account_name, against the ledger's configured account_types (so option "name_assets" is honoured, and no root prefixes are string-matched). It runs on directive and posting metadata in the Late phase. The error is anchored at the directive, like the other E1005s, so it is reported on the directive's line where bean-check names the metadata line.
  • Indented lines after no-metadata directives: report_no_metadata_continuations runs over the green tree only, so it costs no red nodes. It reports at most one error per indented block, because beancount's recovery swallows the rest of the block. Whitespace-only lines are skipped. It does not report when that line already has a syntax error of its own, or when the directive's header does, since beancount's recovery swallows the indented line in that case. A poptag of an absent tag is not a syntax error, so the indented-line error is still reported after it, as in beancount.
  • Posting filename: / lineno:: report_posting_filename_lineno runs only when the source contains filename: or lineno:. A directive's own filename: / lineno: is still accepted, as in beancount.
  • Division: the parser's arithmetic now divides with the canonical rustledger_core::py_div (Python's division) instead of checked_div. This applies to amounts as well as metadata values.
    • An exact quotient gets the ideal exponent: 10/4 is 2.5, 10.00/4 is 2.50.
    • An inexact quotient has 28 significant digits: 6 / 2.1 is 2.857142857142857142857142857, one digit shorter than before.
    • Every case is checked against beancount's values.

Decision notes

Why

Issue #2813. Each change matches beancount; no deviation is introduced.

How to test

cargo test -p rustledger-parser --all-features --test metadata_shapes --test parser_integration_test
cargo test -p rustledger-parser --all-features --lib test_arithmetic_division_has_pythons_scale test_report_no_metadata_continuations_is_linear_in_the_input
cargo test -p rustledger-validate --all-features --test validation_integration_test meta

Verification

  • Probes against bean-check 3.2.3: 41 probe files plus 21 for the no-metadata directives. They cover each row, mixed blocks (comment then metadata), tab indentation, a multi-line string header, a broken header, poptag of an absent tag, a blank line, a tag line, and posting / transaction / directive filename: and lineno:. The parse-error lines match for every probe, except that E1005 is placed on the directive's line.
  • Corpus: I compared all diagnostics (parse and validation) on all 1,113 in-tree and compat ledgers between current origin/main (built in a separate worktree and target dir) and this branch. There are no differences.
  • Baselines:
    • STRICT_BASELINE=1 corpus and CST baselines pass.
    • Two parser-corpus rows move, for parser-lima/test-cases_Expressions.ExplicitPrecision{,2}.beancount. Their (1 + 5) / 2.1 USD balance amounts now have Python's 28 significant digits. The source hashes are unchanged and the CST rows do not move. Only those rows were taken from a regenerated manifest.
  • Formatting paths:
    • Valid files are untouched. The only new errors are on shapes that occur in no corpus file, and parse results are identical on all 1,113 ledgers.
    • On the new error files, format_source and whole-file range formatting keep kk: True, the absorbed kk: 1, and the posting filename: byte for byte.
    • One shape is rewritten, exactly as main already does: an indented comment after option (which bean-check rejects) is dedented to column 0, which makes that line valid. The CLI refuses to format a file with errors, so only format_source and LSP range formatting do this.
  • Linear time: test_report_no_metadata_continuations_is_linear_in_the_input counts header tokens, checked line bytes and errors visited, over 20,000 pushtag / poptag blocks among 20,000 other errors. It can fail: with a per-line errors.iter().any(..) restored, it counted 2,000,740,000 units on a 900,000-byte input and failed.
  • Checks:
    • cargo test --workspace --all-features --no-fail-fast passes, apart from one failure of plugin_conformance::python_plugins_back_to_back_after_a_timeout, a timing test that ran while the machine was loaded. It passed when run alone, twice.
    • cargo clippy --all-features --all-targets -- -D warnings, RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps and cargo fmt --check pass.
  • Parse caches: loader CACHE_VERSION 60 (with FIXTURE_VERSION), WASM 77 (with LOADER_CACHE_VERSION_PIN 60).

#2872 rows

This PR fixes one #2872 row: indented metadata ( mm: "x") after pushmeta / pushtag, or an indented comment after option, is now an error, as bean-check's unexpected INDENT.

Follow-ups (not fixed here): custom "a" True gives 2 errors where bean-check gives 1. A capitalized word lexes as a one-letter currency plus an error token, as Truex already did on main.

Review round 1

  1. Misspelled booleans are named. true, false, True and False in a value now report invalid token 'true' (booleans are written TRUE or FALSE) instead of a bare unexpected input. This follows the maintainer's decision: match beancount, with a kind error.
    • name_misspelled_booleans reads the word from the source around the error, at most five letters each way. The word matters because True lexes as the currency T plus an error token rue. Reading the whole word also keeps Truex from being taken for a boolean.
    • A CHANGELOG Breaking entry says rledger's own render_commas: true (Is it intentional that format strips thousands separators, even with option "render_commas" "TRUE"? #1892) must now be written TRUE, as bean-check already requires.
    • New test: test_parse_names_a_misspelled_boolean covers each spelling in metadata, a custom value and pushmeta, plus the Truex negative case.
  2. One rule, shared. previous_line_is_a_gap and report_no_metadata_continuations now share one keyword list (NO_METADATA_DIRECTIVES) and one predicate (swallows_the_next_line). The predicate decides whether a header error swallows the next line:
    • A syntax or lexer error does.
    • A poptag / popmeta of nothing pushed, or a push never popped, does not, because that is beancount's builder error and the parser carries on.
    • So poptag #zz then 2024-02-30 open Assets:A is now 3 errors, as in bean-check (it was 2).
    • An invalid date on a pushmeta header now swallows the indented line, as there. The indented-line report runs after report_invalid_dates, so header date errors count.
    • Drift guard: test_parse_no_metadata_header_errors_swallow_the_next_line_as_beancount pins six shapes, each checked against bean-check 3.2.3.
  3. CHANGELOG, tolerance note. It now says the py_div change can flip a balance check in beancount's direction: 10/4 USD against -2.46 USD is now accepted, and 10/3*3 USD against -10 USD now reports its residual. Both cases were checked against bean-check and against main.
  4. Nits.
    • The unevaluable-expression messages, for amounts and for values, now name the underflow case (an inexact quotient below 1e-28). The one parser-lima row whose error text moved, test-cases_SyntaxErrors.ErrorInPosting.beancount, is updated: same source hash, and its CST row does not move.
    • report_posting_filename_lineno pushes key directly.
    • The redundant keyword_name is gone, replaced by the shared table.

Verification for this round:

Review round 2

The boolean hint now fires only on a word that stands alone as a value. The word is read over ASCII letters. It needs a line start or whitespace before it, and whitespace, a line end, a comment's ; or the end of the source after it. Assets:true, true., True', trueé and 😀true keep the generic message, because bean-check names the whole token there. The letter cap is fixed, so the word spans at most one letter more than the five of false. The negatives (also xfalse, falsee, and open Assets:true) and the positives (at the end of the source, before ;) are in test_parse_names_a_misspelled_boolean. Parser tests, STRICT_BASELINE=1 corpus and CST baselines, clippy and cargo fmt --check pass.

Closes #2813

🤖 Generated with Claude Code

Each row of #2813, checked against bean-check 3.2.3:

- `True`, `true`, `False`, `false` were booleans. Beancount's lexer has only
  `TRUE` and `FALSE`; the others are `Invalid token`, now a parse error.
- An account value with an invalid root (`kk: Foo:Bar`) was kept as an
  account. It is now E1005, checked with `validate_account_name` against
  the ledger's configured roots, on directive and posting metadata.
- An indented line after `option`, `include`, `plugin`, `pushtag`,
  `poptag`, `pushmeta` or `popmeta` was absorbed and dropped. It is now an
  error at the first indented line of the block, as beancount's
  `unexpected INDENT` (an indented comment too). Not reported where the
  line or the directive's header already has a syntax error, which
  beancount's recovery swallows. One green-tree pass, with a counter test.
- A posting's `filename:` / `lineno:` is an error at the transaction's
  line, as beancount's `Duplicate posting metadata field`. A directive's
  own are still accepted, as there.
- `kk: 1 / 0` was already an error (beancount segfaults).
- A division in an amount or value is `rustledger_core::py_div`, Python's:
  `10/4` is `2.5` (was `2.50`), an inexact quotient has 28 significant
  digits. Two parser-lima baseline rows (`(1 + 5) / 2.1` balances) move.

The `render_commas: true` spelling in the format test is dropped: it is a
parse error now, as in beancount. Loader CACHE_VERSION 60, WASM 77.

Closes #2813

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

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/convert.rs 97.78% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 52.4ms ± 2.2ms 838.3ms ± 11.5ms 16.0x 42.9 MB / 51.1 MB
investment 79.6ms ± 1.4ms 2932.6ms ± 11.9ms 36.8x 58.9 MB / 59.1 MB
fifo 74.3ms ± 0.8ms 14638.7ms ± 54.6ms 196.9x 54.8 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 51.9ms ± 1.2ms 52.4ms ± 2.2ms +0.9% ✅ within noise 42.8 MB → 42.9 MB (+0.2%)
investment 78.0ms ± 1.0ms 79.6ms ± 1.4ms +2.1% ✅ within noise 58.2 MB → 58.9 MB (+1.1%)
fifo 75.5ms ± 1.0ms 74.3ms ± 0.8ms -1.6% ✅ within noise 55.0 MB → 54.8 MB (-0.5%)

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

@github-actions

github-actions Bot commented Nov 7, 2026

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 and others added 2 commits November 7, 2026 07:30
…low rule

Review round 1 on #2813:

- `true`, `false`, `True` and `False` in a value now read `invalid token
  'true' (booleans are written TRUE or FALSE)` instead of a bare
  `unexpected input` (maintainer's decision: match beancount, with a kind
  error). CHANGELOG Breaking: `render_commas: true` (#1892) must now be
  written `TRUE`.
- `previous_line_is_a_gap` and `report_no_metadata_continuations` share
  one keyword list (`NO_METADATA_DIRECTIVES`) and one predicate for a
  header error that swallows the next line (`swallows_the_next_line`): a
  syntax or lexer error does, a `poptag`/`popmeta` of nothing pushed does
  not. `poptag #zz` then `  2024-02-30 open Assets:A` is three errors, as
  in bean-check (it was two); an invalid date on a `pushmeta` header now
  swallows the indented line, as there. A drift-guard test pins six shapes
  against bean-check 3.2.3. The indented-line report now runs after
  `report_invalid_dates`, so header date errors count.
- CHANGELOG: the py_div change can flip balance tolerance in beancount's
  direction.
- The unevaluable-expression messages name the underflow case; one
  parser-lima baseline row (ErrorInPosting) moves with the text.
- `report_posting_filename_lineno` pushes the key directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review round 2 on #2813: the boolean hint fired on account components and
on words glued to punctuation or non-ASCII (`Assets:true`, `true.`,
`True'`, `trueé`), where bean-check names the whole token. It now renames
only a word bounded by a line start or whitespace before it and by
whitespace, a line end, a comment's `;` or the end of the source after
it, read over ASCII letters only. The letter cap is fixed so the word
really spans at most one more than five letters. Negatives for each case
are added to `test_parse_names_a_misspelled_boolean`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit b71c6fa into main Nov 7, 2026
66 of 67 checks passed
@robcohen
robcohen deleted the fix/metadata-shapes branch November 7, 2026 15:47
robcohen added a commit that referenced this pull request Nov 7, 2026
#2888 took WASM cache v77 (loader v60). This branch's ParsedLedger payload
change moves to v78, its note after #2888's; LOADER_CACHE_VERSION_PIN is 60,
as main's loader cache is.

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

1 participant