Skip to content

fix(parser): a * that begins any cost component is the merge marker - #2399

Merged
robcohen merged 2 commits into
mainfrom
fix/trailing-merge-marker-2329
Sep 23, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/trailing-merge-marker-2329

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

A * that begins any cost component is now the merge marker, not only a leading one. {100.00 USD, *}, {2020-01-01, *} and {*, 100.00 USD} are all merges, as in beancount's grammar. A * after a number or ) is still multiplication ({500 * 2 USD}).

Why

Beancount's cost is a comma-separated list of components (number, currency, date, label, *), and * may be any of them. Checked against beancount 3.2.3's parser:

spec beancount main this PR
{*}, {*, 100 USD} merge merge merge
{100.00 USD, *}, {2020-01-01, *}, {"lbl", *}, {{100 USD, *}} merge plain spec merge
{500 * 2 USD} multiplication (1000) multiplication multiplication
{500 * 2 USD, *} merge plain spec merge
{* 100.00 USD} (no comma) syntax error merge merge (unchanged leniency)

On main the trailing form was a silent change of operation: -5 X {110.00 USD, *} failed with No matching lot, and -5 X {100.00 USD, *} sold from the 100 lot instead of the merged pool. Both now book from the pool at 110, like {*}.

How

  • MergeFlag: the one state machine both the green and red CST walkers feed. It now sets the flag on a * whose previous non-whitespace token is the opener or a comma, instead of deciding on the first token after the opener. Tokens before the first opener, and later openers on malformed input, are still ignored.
  • Tests: the row test that pinned {2 USD, *} as not a merge now pins it as a merge, with rows for the other positions and a multiplication-then-comma case. The red-tree accessor test gains the trailing form, and two tests were renamed that named the old rule.
  • rledger format round-trips every form unchanged.

Caches

Parser output changes, so the loader cache goes to v36 and the wasm cache to v24 (with its loader pin).

Found while doing this: the wasm cache archives booked directives and errors, and the booking fixes merged since its v23 (#2379, #2385, #2391, #2395, #2397) each changed those without a bump, so a v23 blob could still serve their pre-fix answers. v24 covers them too, and its doc comment lists them.

The CostNumber/MetaValue byte-layout tripwires still compare bytes; only their FIXTURE_VERSION moves, since neither encoding changed.

Evidence

  • a_trailing_star_component_merges_like_a_leading_one (loader): {*}, {110.00 USD, *} and {100.00 USD, *} all book as a merge at 110. It fails on main's parser ("No matching lot").
  • Workspace suite green (169 binaries); clippy and rustdoc clean.
  • Output identity: check byte-identical to main on four benchmark ledgers.
  • Callgrind on check: −0.03% to +0.004%.

Not in this PR

A number in a merge spec is ignored: {*, 100 USD} has always booked at the pool's 110, and the trailing form now does too. What it should mean is its own question: #2398.

Closes #2329
Refs #2398

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Beancount's grammar takes a cost as a comma-separated list of components
(number, currency, date, label, `*`), and `*` may be any of them:
`{100.00 USD, *}`, `{2020-01-01, *}` and `{*, 100.00 USD}` all set merge
(checked against beancount 3.2.3's parser, which reaches its "Cost merging is
not supported yet" action for each). `MergeFlag` decided the flag from the
first token after the opener only, so a trailing `*` parsed as a plain
per-unit spec, with no diagnostic: `-5 X {110.00 USD, *}` failed with
`No matching lot`, and `-5 X {100.00 USD, *}` silently sold from the lot at
100 instead of the merged pool.

The rule is now: a `*` is the merge marker when the previous non-whitespace
token is the opener or a comma, i.e. when it begins a component. A `*` after a
number or `)` is still multiplication (`{500 * 2 USD}`). `MergeFlag` is the one
machine both the green and red walkers feed, so both change together; the
test that pinned `{2 USD, *}` as not-merge now pins it as a merge, with rows
for the other positions, and the red-tree accessor test gains the trailing
form. `rledger format` round-trips every form unchanged.

Caches: parser output changes, so loader CACHE_VERSION goes to 36 and the
wasm cache to 24 (with its loader pin). The wasm cache archives BOOKED
directives and errors, and the booking fixes merged since v23 (#2379, #2385,
#2391, #2395, #2397) each changed those without a bump, so a v23 blob could
serve their pre-fix answers; v24 covers them too. The CostNumber and
MetaValue byte-layout tripwires still compare their bytes; only their
FIXTURE_VERSION moves.

Not changed: a number in a merge spec is ignored, for the leading form as it
always was and now for the trailing one (#2398).

Tests: the MergeFlag row test and the ast accessor test; a loader test that
`{*}`, `{110.00 USD, *}` and `{100.00 USD, *}` all book from the merged pool
at 110, which fails on main's parser. Workspace suite green (169 binaries);
`check` byte-identical to main on four benchmarks; callgrind -0.03% to
+0.004%.

Closes #2329
Refs #2398

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner September 23, 2026 03:03
@codecov

codecov Bot commented Sep 23, 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 Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 45.3ms ± 0.8ms 835.0ms ± 5.7ms 18.4x 51.0 MB / 50.2 MB
investment 71.4ms ± 0.7ms 3162.3ms ± 22.9ms 44.3x 55.1 MB / 58.1 MB
fifo 67.4ms ± 0.9ms 15808.4ms ± 127.8ms 234.5x 57.0 MB / 57.7 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.7ms ± 0.3ms 45.3ms ± 0.8ms +1.4% ✅ within noise 51.3 MB → 51.0 MB (-0.5%)
investment 71.9ms ± 1.6ms 71.4ms ± 0.7ms -0.7% ✅ within noise 57.1 MB → 55.1 MB (-3.6%)
fifo 70.2ms ± 1.2ms 67.4ms ± 0.9ms -4.0% ✅ within noise 56.9 MB → 57.0 MB (+0.1%)

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

…fter, the opener

Review of #2399. `cargo mutants --in-diff` (the CI mutation gate covers
rustledger-core only, not the parser) left the two `past_opener` guards
alive: flipping either let a comma before the opener, or a second `{` on
malformed input, arm a component start, so `, * {...` or `{2 USD {*` would
read as a merge. Both are malformed-input paths the doc comment promises to
ignore; two direct-feed assertions now pin them, and all 11 in-diff mutants
are caught.

Also checked in review:
- Only `merge` changes. Parsing 18 shapes with main's and this branch's
  parser gives identical number, currency, date and label for every one
  (compound `#` and total `{{}}` included); a missing comma (`{100 USD *}`)
  is still a parse error, as in beancount.
- No other code re-derives the merge flag; `Amount::is_arithmetic` sees the
  posting's amount node, never a cost spec's tokens.
- A trailing `*` on a buy books the same lot as before, as the leading form
  always has.

Refs #2329

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 178 masked)
Full AST Match 100%
Total Files 813
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

Copy link
Copy Markdown
Member Author

Deep review (head 0282650)

Verdict: ready once CI is green. No defects. One test gap closed.

Scope: only merge changes

Parsing 18 cost shapes with main's parser and this branch's gives identical number, currency, date and label for every one, including compound {100 # 5 USD, *} and total {{100 USD, *}}. Only merge differs, and only where a * begins a component:

spec main merge PR merge
{*}, { * }, {*, 100 USD}, {* 100 USD}, {*, *} true true
{100.00 USD, *}, {100 USD,*}, {2020-01-01, *}, {"lbl", *}, {{100 USD, *}}, {100 # 5 USD, *}, {100 USD, *, 2020-01-01} false true
{500 * 2 USD, *}, {(1 + 2) * 3 USD, *} false true (value still 1000 / 9)
{500 * 2 USD}, {(1 + 2) * 3 USD}, {*, 500 * 2 USD} as before as before
{100 USD *} (no comma) parse error parse error (beancount: syntax error)

No second copy of the rule

MergeFlag is the only merge detector. The green walker (cost_spec_from_tokens) and the red one (ast::CostSpec::is_merge) both feed it, and ast_is_merge_agrees_with_the_converted_cost_spec ties them. The other STAR match in ast.rs is Amount::is_arithmetic, which sees a posting's amount node, never a cost spec's tokens. The LSP, completion and wasm read the parsed CostSpec.

Behavior

  • Sales: all three forms ({*}, {110 USD, *}, {100 USD, *}) now book from the merged pool; on main the trailing forms failed or sold from one lot.
  • Buys: a trailing * on an augmentation books the same lot as before (5 X {100 USD, 2020-01-02}), as the leading form always has.
  • Format: rledger format round-trips every form unchanged.
  • A number inside a merge spec is ignored, as it always was for the leading form (A number in a {*} merge spec is silently ignored: {*, 100 USD} books at the pool's 110 #2398).

Mutation testing (cargo mutants --in-diff, parser)

The CI mutation gate covers rustledger-core only. Locally, 11 mutants on the changed lines: the first run missed both past_opener guards. A comma before the opener, or a second { on malformed input, could arm a component start, reading , * {… or {2 USD {* as a merge. 0282650 pins both with direct-feed tests, and all 11 are now caught.

Caches

Evidence

  • Tests: workspace suite green (169 binaries), clippy and rustdoc clean.
  • Loader test: the pipeline test fails on main's parser ("No matching lot").
  • Output and perf: check byte-identical to main on four benchmarks; callgrind −0.03% to +0.004%.

@robcohen
robcohen merged commit 40ba754 into main Sep 23, 2026
67 of 68 checks passed
@robcohen
robcohen deleted the fix/trailing-merge-marker-2329 branch September 23, 2026 10:14
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]: a trailing * cost component does not set the merge flag

1 participant