Skip to content

fix(parser): report a second leading BOM on line 1 as on any later line - #3168

Merged
robcohen merged 1 commit into
mainfrom
fix/line-1-double-bom
Nov 30, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/line-1-double-bom

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

<BOM><BOM>*x on line 1 now reports P0026 (the BOM), as the same line does anywhere later in a file, and as <BOM><BOM>$x on line 1 already did. The error count is unchanged.

Before / after (bean-check 3.2.3: one Invalid token: '<BOM><BOM>*x' on the line in every row)

Input rledger before rledger after
line 1 <BOM><BOM>*x (review repros i14a, i14d) P0012 unexpected input: invalid token 'x', col 8 P0026 at the BOM
line 2 <BOM><BOM>*x (i14b) P0026 P0026 (unchanged)
line 1 <BOM><BOM>$x (i14c) P0026 P0026 (unchanged)

Why it happened

The tree keeps line 1's second BOM as a token of its own (#3101) and lexes the rest of the word apart (*, then x), so line 1 carried a BOM diagnostic, a line error and an x diagnostic, all on the one token the error oracle's reference reports there. The reconciler names a slot with the tightest diagnostic on its token, so x won. On later lines the BOM-led word is one error token and P0026 is the only diagnostic.

How

  • reconcile (the format-generic reconciler in cst/oracle/mod.rs) takes a names(e, slot) hook, consulted before width when it pairs a slot with a diagnostic on its token.
  • beancount's names_line_1_bom_led_token: the P0026 of a second BOM at byte 3 names the lexical slot starting there. Restricted to that one place on purpose: a version that preferred any BOM diagnostic at a BOM-led slot's start also replaced a pinned mid-file InvalidAccount message (bom_mid_line_run::test_parse_partly_invalid_account_word_keeps_mains_message).
  • transactions_beancount_drops now reads a directive from the start of its header line (past a byte-0 BOM, which P1 strips). Without it this change regressed the loaded ledger: line 1's old header-defect error sat inside the transaction, so the transaction was left out; the BOM's P0026 starts before its span, so it was kept (fuzz 22c82db5d04b). The same rule now also leaves out an indented dated line beancount reads as unexpected INDENT (3 fuzz inputs), and 11 line-1 double-BOM transactions that main kept: in every case beancount builds nothing there.
  • P1 in compatibility.md says it.

Proof

  • New overlays::tests::test_byte_zero_bom_then_a_second_reports_line_1_as_a_later_line (7 line shapes: line 1 has the same error count as the same line on line 2, and its first error is P0026; bean-check gives the same count on both, checked for each), and transactions_beancount_drops::test_transactions_beancount_drops_one_a_second_leading_bom_starts (a double-BOM line-1 transaction is left out; a byte-0 BOM alone keeps a transaction whose error is a builder check).
  • Corpus (parse_error_lines --jsonl + semantic_snapshot, 45,182 inputs, release builds of origin/main 1f4b9dc and this branch): 46 fuzz inputs differ in parse errors, all the same change: a line-1 P0012 becomes P0026, same line, same count. semantic_snapshot additionally differs on 14 fuzz inputs, each one transaction fewer (11 line-1 double-BOM, 3 indented dated lines), as above. No compat or tracked ledger changes.
  • Parity (local, beancount 3.2.3 + ptadiff pinned): compat-parse-error-parity --check no regressions, 122,533 / 123,394 (unchanged); compat-semantic-parity --check no regressions, unchanged. No re-pin.
  • cargo test -p rustledger-parser -p rustledger-loader -p rustledger-lsp -p rustledger-wasm --all-features: 2,430 passed, 0 failed. Clippy -D warnings: clean.
  • Cache: loader 85→86, WASM 169→170. CHANGELOG: Fixed.

Merge note

Independent branch off main; the other parser PRs of this batch bump the caches to the same numbers (rebase cache.rs). The oracle line-index PR also edits reconcile_errors_with's call to reconcile, and the broken-directive PR renames transactions_beancount_drops: trivial conflicts either way.

🤖 Generated with Claude Code

@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

@codecov

codecov Bot commented Nov 30, 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 Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Instruction counts and wasm fuel vs base (deterministic)

Workload base this PR Change vs v0.24.0
check/simple 289.7M 289.7M -0.01% ✅ -17.1%
check/investment 450.0M 450.1M +0.01% ✅ -12.8%
check/tagged 420.2M 420.1M -0.01% ✅ -18.5%
check/multicurrency 439.3M 439.2M -0.00% ✅ -24.8%
check/fifo 439.4M 439.4M +0.01% ✅ -16.8%
check/metadata 614.3M 614.1M -0.02% ✅ -23.1%
check-warm/simple 128.5M 128.5M +0.00% ✅ -18.4%
format/simple 613.3M 613.2M -0.02% ✅ +13.8%
format/metadata 1.52G 1.52G +0.02% ✅ +27.4%
format/errors 92.3M 92.3M +0.03% ✅ +103.8%
check/errors 269.3M 269.3M -0.00% ✅ —
check-json/errors 149.4M 149.4M -0.03% ✅ —
check/junkline 30.6M 30.6M -0.01% ✅ +300.4%
check-json/junkline 43.9M 43.8M -0.13% ✅ +614.3%
query/journal/simple 442.5M 442.4M -0.01% ✅ -29.0%
query/groupby/simple 338.8M 339.1M +0.07% ✅ -26.3%
query/groupby/investment 556.0M 556.5M +0.10% ✅ -25.3%
lsp/open-diagnostics/simple 337.0M 336.9M -0.01% ✅ —
lsp/open-diagnostics/metadata 901.0M 901.0M +0.00% ✅ —
lsp/format/simple 1.03G 1.03G +0.02% ✅ —
lsp/format/metadata 2.53G 2.53G +0.00% ✅ —
lsp/edit/simple 670.9M 670.9M -0.00% ✅ —
Workload (wasmtime fuel) base this PR Change vs v0.24.0
wasm/validate/simple 1.14G 1.14G +0.00% ✅ —
wasm/validate/metadata 2.79G 2.79G +0.00% ✅ —
wasm/format/simple 1.40G 1.40G +0.00% ✅ —
wasm/format/metadata 3.55G 3.55G +0.00% ✅ —
wasm/query/simple 1.28G 1.28G +0.00% ✅ —
ffi/load/simple 1.75G 1.75G +0.00% ✅ —
ffi/load/metadata 4.04G 4.04G +0.00% ✅ —
ffi/query/simple 1.38G 1.38G +0.00% ✅ —
ffi/validate/simple 1.22G 1.22G +0.00% ✅ —
ffi/format/simple 1.46G 1.46G +0.00% ✅ —

Instruction counts (cachegrind I refs) are deterministic: the same binary on the same input gives the same count to within ~0.02%, so unlike wall clock a 1% change is real. 🟡 ≥ 1%, 🔴 ≥ 5%. The wasm/ and ffi/ rows are wasmtime fuel (WebAssembly operators the guest executed), which is exact: the same .wasm on the same input gives the same fuel on any machine. Static data moves the component's heap, so a change to embedded strings alone can move ffi/ by a few hundredths of a percent. The last column is cumulative drift from the pinned v0.24.0 baseline (scripts/perf-baseline.json).

Wall clock (hyperfine)

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 29.5ms ± 0.9ms 502.3ms ± 6.5ms 17.0x 34.7 MB / 51.2 MB
investment 47.8ms ± 0.9ms 2001.1ms ± 27.6ms 41.9x 44.8 MB / 59.3 MB
fifo 46.8ms ± 1.0ms 11859.5ms ± 270.9ms 253.6x 42.8 MB / 59.1 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 29.6ms ± 0.9ms 29.5ms ± 0.9ms -0.4% ✅ within noise 34.9 MB → 34.7 MB (-0.6%)
investment 47.7ms ± 0.8ms 47.8ms ± 0.9ms +0.1% ✅ within noise 44.8 MB → 44.8 MB (-0.1%)
fifo 46.7ms ± 0.6ms 46.8ms ± 1.0ms +0.1% ✅ within noise 42.8 MB → 42.8 MB (-0.0%)

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

`<BOM><BOM>*x` on line 1 reported `P0012 unexpected input: invalid token
'x'` at column 8, where the same line anywhere else, and `<BOM><BOM>$x` on
line 1, report the BOM (`P0026`). bean-check 3.2.3 reports one
`Invalid token: '<BOM><BOM>*x'` on each.

The tree keeps line 1's second BOM as a token of its own (#3101) and
lexes the rest of the word apart, so the line held a diagnostic on the
`x` as well as the BOM's, both on the token the reference reports, and
the reconciler named the slot with the tightest. `reconcile` gains a
`names(e, slot)` hook, taken before width when it pairs a slot with a
diagnostic on its token; beancount's names that second BOM's `P0026` for
the token beancount reads from it, on line 1 only (P1). Later lines are
unchanged (a wider rule took a mid-file `InvalidAccount` message over a
BOM-led account word). The line keeps its error count.

The header-defect error that line 1 had sat inside the transaction after
the BOM, so `transactions_beancount_drops` asked it and left it out, as
beancount builds nothing there; the BOM's `P0026` starts before the
transaction's span. The rule now reads a directive from the start of its
header line (past a byte-0 BOM, as P1 strips it), so such a transaction
stays out of the ledger.

Loader cache v86, WASM cache v170.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/line-1-double-bom branch from e4457d5 to f76b3e9 Compare November 30, 2026 19:45
@robcohen
robcohen merged commit 3060ac2 into main Nov 30, 2026
67 of 74 checks passed
@robcohen
robcohen deleted the fix/line-1-double-bom branch November 30, 2026 20:09
robcohen added a commit that referenced this pull request Nov 30, 2026
…3162 and #3168 (#3175)

Parser Baselines has been red on main since #3158: the parser-output hash
moved on 54 corpus files whose source is unchanged (49 parser-lima, 2
double-entry-generator, 2 beancount-lazy-plugins, 1 jcornaz-parser). These
are the intended output changes of #3158 (a typed number error on every
ParseError), #3162 (a line-start stray \r read at column 0) and #3168 (a
second leading BOM on line 1). Regenerated with
scripts/regen-corpus-baselines.sh on a freshly fetched corpus (751 files);
every changed entry keeps its path and source hash, and the CST manifest
does not move.

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.

1 participant