Skip to content

fix(parser): report a repeated metadata key on a transaction or posting, keeping the first value - #2897

Merged
robcohen merged 5 commits into
mainfrom
fix/duplicate-meta-keys
Nov 9, 2026
Merged

robcohen merged 5 commits into
mainfrom
fix/duplicate-meta-keys

Conversation

@robcohen

@robcohen robcohen commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

What changed

A metadata key written twice on a transaction or a posting is now reported as beancount 3.2.3 reports it, and the first value stands:

Input bean-check 3.2.3 Before Now
kk: "first" / kk: "second" on a transaction Duplicate metadata field on entry, at the transaction's line; value "first" no error; value "second" parse error at the transaction's line; value "first"
pk: 1 / pk: 2 on a posting Duplicate posting metadata field, at the transaction's line; value 1 no error; value 2 parse error; value 1
pushmeta kk: "p", then a transaction writing kk twice one duplicate error; value "a" (first written) no error; value "b" one error; value "a"
kk on the transaction and on one of its postings no error no error no error
kk written twice on an open (or any other directive) no error, last value no error, last value unchanged

Stricter than Python, deliberately (maintainer's decision): beancount checks a repeat with if value is not posting_or_kv.value (grammar.py, the transaction and posting builders). That is an object-identity test, so it stays silent when the repeated value is the identical interned object: TRUE/TRUE, ""/"", one-character strings, accounts, #t/#t. rledger reports every repeat. This follows the AGENTS.md checklist: a "Stricter than Python" comment at report_duplicate_metadata, the regression test test_parse_reports_a_repeat_with_an_identical_value_unlike_python, and a CHANGELOG note.

rledger format: these are parse errors now, so rledger format refuses a file with a repeated transaction or posting key, as it refuses any file with errors. bean-format still formats such a file. This follows the existing policy for beancount's builder errors.

The issue asked for the error "on the repeated line". bean-check reports it at the transaction's line, so this PR does the same. Beancount checks repeats only on transactions and postings; every other directive's metadata takes the last value with no error, which is what rledger already does, so that is left alone.

How it works:

Fixtures:

  • tests/fixtures/validation-errors.beancount had a "Duplicate metadata key (E6001)" section, but no E6001 validator exists, and the shape is now a parse error. The file must parse clean (test_validation_errors_parses), so the section is removed.
  • The two lima MetaData.MetadataTransactionRepeated* fixtures expect exactly these errors (their .txtpb lists three duplicate key test), so they join the expected-parse-error list.
  • metadata_many_keys now expects the first value.

Why

Issue #2893 (found in the #2891 review). This matches beancount.

How to test

cargo test -p rustledger-parser --all-features --test metadata_shapes
cargo test -p rustledger-parser --all-features --lib test_report_duplicate_metadata_is_linear_in_the_keys
cargo test -p rustledger-core --all-features --lib build_keeping_first

Verification

  • Probes: the issue's shapes plus an open, pushmeta, and transaction-vs-posting keys, run against full bean-check output. The error lines, counts and kept values all match.
  • Corpus: all diagnostics on all 1,113 ledgers, current origin/main vs this branch. 4 files differ, and all now match bean-check exactly:
    • the two lima MetadataTransactionRepeated fixtures (each in-tree, and one also in the compat corpus): 3 errors at line 1, as their .txtpb and bean-check give;
    • parser-lima/examples_data_error-duplicates.beancount: m1 at line 5.
  • Baselines: only the examples_data_error-duplicates row in parser-corpus.manifest is updated. Its source hash is unchanged and its CST row does not move. STRICT_BASELINE=1 passes.
  • Linear time: test_report_duplicate_metadata_is_linear_in_the_keys puts 20,000 keys, each written twice, on a transaction and 20,000 on a posting. It asserts at most 1,000 pairwise key comparisons, and a quadratic version would make about 2×10^8. The core test bounds build_keeping_first at under 10 comparisons per entry for 50,000 entries.
  • Checks:
    • cargo test --workspace --all-features --no-fail-fast passes (private TMPDIR).
    • cargo clippy --all-features --all-targets -- -D warnings, RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps and cargo fmt --check pass.

Review round 1

  1. Identical-value repeats: kept as errors, per the maintainer's decision. This is documented as a deliberate "Stricter than Python" deviation (see above) and pinned by a test with TRUE, "", "x", Assets:A, #t and "ab" repeats. bean-check reports only the "ab" one.
  2. A broken metadata line is no repeat: kk: "a" then kk: (bad (and kk: 2024-02-30, kk: 1 2, kk: Assets:a) now gives only the line's own error, as bean-check does.
    • report_duplicate_metadata skips a META_ENTRY that holds an already-reported error. It now runs after report_invalid_dates, so date errors count.
    • Green/red parity is asserted in the new test.
    • On the kept value: when a transaction's metadata line holds an error, beancount 3.2.3 drops the whole transaction (bean-query PRINT shows only the open), so it has no kept value to match. rledger keeps the transaction under its existing error-recovery policy. Its metadata is visible only through LSP/WASM, because check, query and format refuse the file.
  3. rledger format refusal: noted in the CHANGELOG and above.
  4. Nits:
    • A posting's written filename: / lineno: is now dropped from its metadata by the red and green converters (drop_posting_location_keys), so the real location stands, as bean-query shows. The docs now say so, and there is a test.
    • In spec/impl/test-vectors.md, "Duplicate metadata key" moves from the validation checklist to the parser one, described as a parse error.
  5. Cache: merged origin/main (fix(validate): report a pad on a closed account, and pad whatever the accounts' state #2895).

Closes #2893

🤖 Generated with Claude Code

…ng, keeping the first value

Beancount builds a transaction's and a posting's metadata checking each
key against the ones before: a repeat is `Duplicate metadata field on
entry` / `Duplicate posting metadata field`, reported at the
transaction's line, and the first value stands. rledger kept the last
value silently. Every other directive takes the last value with no error
in beancount too, and is unchanged.

- core: `MetadataBuilder::build_keeping_first` (near-linear, like
  `build`) keeps each key's first entry.
- parser: transaction and posting metadata, red and green, build with
  it; `report_duplicate_metadata` (one green pass over the transactions)
  reports each repeat. It replaces `report_posting_filename_lineno`: a
  posting starts with `filename` and `lineno` set, as in beancount. A
  counter test pins it linear.
- fixtures: the lima MetadataTransactionRepeated cases expect these errors
  (their txtpb lists them); validation-errors.beancount loses its
  unimplemented "E6001 duplicate metadata key" section, which is now a
  parse error; metadata_many_keys expects the first value.
- One parser-lima baseline row (examples_data_error-duplicates) moves.
- Loader CACHE_VERSION 62, WASM 82.

Closes #2893

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

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 39.6ms ± 1.1ms 580.5ms ± 10.3ms 14.7x 42.9 MB / 51.1 MB
investment 61.5ms ± 3.6ms 2126.5ms ± 12.7ms 34.6x 56.9 MB / 59.0 MB
fifo 59.3ms ± 1.5ms 11838.4ms ± 250.2ms 199.6x 55.1 MB / 58.7 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 39.7ms ± 1.0ms 39.6ms ± 1.1ms -0.3% ✅ within noise 42.8 MB → 42.9 MB (+0.2%)
investment 60.0ms ± 0.7ms 61.5ms ± 3.6ms +2.5% ✅ within noise 58.9 MB → 56.9 MB (-3.3%)
fifo 57.8ms ± 0.6ms 59.3ms ± 1.5ms +2.6% ✅ within noise 55.0 MB → 55.1 MB (+0.1%)

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

@codecov

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

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

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

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 8, 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 4 commits November 8, 2026 07:48
# Conflicts:
#	CHANGELOG.md
#	crates/rustledger-wasm/src/cache.rs
…ing locations, and document the stricter repeat check

- A META_ENTRY that holds its own error (a bad value, an invalid date)
  is no repeat, as in beancount. The duplicate check now runs after
  report_invalid_dates.
- A posting's written `filename:` / `lineno:` is dropped from its
  metadata (red and green), so the real location stands as in beancount.
- Document reporting a repeat with an identical value as a deliberate
  stricter-than-Python deviation (beancount compares by identity), with
  a regression test and a CHANGELOG note.
- CHANGELOG: `rledger format` now refuses files with such repeats.
- spec/impl/test-vectors.md: the duplicate key is a parser test now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a line, and share POSTING_LOCATION_KEYS

A transaction whose metadata line holds an error is dropped whole by
beancount, so the doc comments now say that and that rledger keeps the
entry under its error-recovery policy. The posting location keys are
one const, shared by the duplicate check's preset and
drop_posting_location_keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The compat-corpus copy of lima's MetaData.MetadataTransactionRepeated
now gets the duplicate-metadata parse error (#2893). Its source hash is
unchanged, so only that row's output hash moves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit d2a0ee5 into main Nov 9, 2026
67 of 68 checks passed
@robcohen
robcohen deleted the fix/duplicate-meta-keys branch November 9, 2026 06:03
robcohen added a commit that referenced this pull request Nov 9, 2026
Resolves the WASM cache and CHANGELOG conflicts with #2897: v82 is
#2897's (loader v62), and this branch's note moves to v83 over loader
v62.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 9, 2026
Merges origin/main (#2897: loader cache v62, WASM v82). This branch keeps
loader v63 and WASM v84; their notes now name v62 and v83 as predecessors.

Review round 1 on #2903:
- quiet_after_invalid_tokens drops a construct check that contains the
  invalid token on an indented line only when the line's posting or
  metadata grammar reaches that token (indented_grammar_reaches). Where the
  grammar fails first (`1 VV {}{$`: `unexpected LCURL`, then the token),
  both errors stand, as in beancount.
- An `open` with an invalid token on its header gets no booking-method
  error (P0025), and a transaction with an invalid token anywhere in it
  gets no repeated-metadata-key error: beancount never builds either.
- cr_outside_strings_present streams the lexer and stops at the first
  `\r` outside a string.
- Parse-error parity manifest re-pinned with --update; --check is clean.
  76,882 of 111,323 inputs match beancount 3.2.3 (main: 57,518).

Refs #2872

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 29, 2026
…rror difference P12

beancount 3.2.3 stays silent on a transaction or posting metadata key
repeated with the same value as its first one when that value is TRUE /
FALSE, an empty or one-character string, an account, a tag or empty, and
compares each repeat with the first value. rledger reports every repeat
(#2897), on purpose. Document it as P12 in compatibility.md, restate the
inline comment at report_duplicate_metadata from black-box observation,
and extend the pinning test to postings, empty values and a repeat back
to the first value. No behavior change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 29, 2026
…rror difference P12

beancount 3.2.3 stays silent on a transaction or posting metadata key
repeated with the same value as its first one when that value is TRUE /
FALSE, an empty or one-character string, an account, a tag or empty, and
compares each repeat with the first value. rledger reports every repeat
(#2897), on purpose. Document it as P12 in compatibility.md, restate the
inline comment at report_duplicate_metadata from black-box observation,
and extend the pinning test to postings, empty values and a repeat back
to the first value. No behavior change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 29, 2026
…rror difference P12 (#3091)

beancount 3.2.3 stays silent on a transaction or posting metadata key
repeated with the same value as its first one when that value is TRUE /
FALSE, an empty or one-character string, an account, a tag or empty, and
compares each repeat with the first value. rledger reports every repeat
(#2897), on purpose. Document it as P12 in compatibility.md, restate the
inline comment at report_duplicate_metadata from black-box observation,
and extend the pinning test to postings, empty values and a repeat back
to the first value. No behavior change.

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

Development

Successfully merging this pull request may close these issues.

fix(parser): a repeated metadata key is accepted silently; beancount reports it and keeps the first value

1 participant