Skip to content

fix(parser): report a number beyond 28 digits wherever it stands - #3158

Merged
robcohen merged 2 commits into
mainfrom
fix/decimal-precision-typed
Nov 30, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/decimal-precision-typed

Conversation

@robcohen

@robcohen robcohen commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

What

A number with more significant digits than rust_decimal holds (P10) is now a parse error at the number in every context, and the error oracle tells P9/P10 errors apart by a typed discriminator instead of their message prefix.

Correctness bug fixed first: a 31-digit number in a cost spec was dropped with no parse error, the cost became unknown, and the only diagnostic was a misleading BOOK interpolation failed.

Before / after (N = 1234567890123456789012345678901, 31 digits)

Input bean-check 3.2.3 rledger before rledger after
Assets:A 1 HOOL {N USD} (and {{N USD}}, {N # 1 USD}, {1 # N USD}, {N USD, 2024-01-01}) clean no parse error; BOOK interpolation failed P0012 invalid number in cost (likely exceeds 28-digit Decimal precision) at N (the BOOK follow-on remains, as for an amount)
{1+N USD} clean clean, cost silently read as 1 USD P10 at N
price HOOL N USD, price HOOL -N USD clean P0012 unexpected input over the whole line P10 (in amount) at N
price HOOL 1+N USD clean malformed amount: expected one number… P10 at N
balance Assets:A N USD Balance failed (load-time) P0012 unexpected input over the line P10 at N
balance Assets:A 1 ~ N USD clean clean, tolerance dropped P10 at N (balance kept, no tolerance)
kk: N, kk: N USD, posting metadata clean clean, value None / the currency invalid number in metadata value … at N
custom "b" N USD clean clean, number dropped invalid number in custom value …
pushmeta kk: N clean clean invalid number in pushmeta value …
Assets:A N USD, @ N USD, @@ N USD clean P10 (in amount) unchanged, byte-identical message
Assets:A 1+N USD, kk: 1+N clean P9 invalid arithmetic expression … P10 at N (the number is the reason)
Assets:A N USD $ Invalid token: '$' P10 unexpected input: invalid token '$' (beancount's error on that line)

Every one of these was checked with bean-check (beancount 3.2.3); none is a beancount syntax error, so each P10 error is rledger's own (documented deviation P10).

How

  • ParseError::number_error: Option<NumberError> (new, NumberError::{UnevaluableExpression, BeyondPrecision}), a sibling of refactor(parser): classify builder checks by EntryCheck, drop RLEDGER_ERROR_ORACLE #3093's check field (check is classified ErrorClass::Other by the reconciler, which these must not be). Set at every emitting site: the amount path's two errors, unevaluable_value_error (all its callers), and the new beyond_precision_error.
  • The divide-by-zero and decimal-precision overlays match on number_error instead of starts_with("invalid arithmetic expression") / starts_with("invalid number in amount (likely exceeds").
  • decimal-precision now claims an error only on a line beancount reads and reports nothing on (as divide-by-zero already did), so on a line beancount reports, the reconciler pairs beancount's error with rledger's diagnostic on its token (the $ row above). Without this, 4 fuzz inputs lost the name of an invalid token to the P10 message.
  • Cost spec: one rule (number_beyond_precision) in the green walk's cost_spec_errors, the red extract_unclosed_cost_brace_errors, and the tree builder's recorder (LineChecks::cost_spec, so the walk-free shortcut cannot skip it; debug builds assert agreement). Metadata/pushmeta: meta_number_beyond_precision in MetaCheck::line, directive_header_check and the recorder's meta. custom: values_beyond_precision, shared by the green and red value walks. price/balance: directive_value_beyond_precision before the malformed-value fallback, plus the tolerance check.
  • number_beyond_precision only parses a token longer than 28 bytes (shorter ones always fit), so metadata lines pay one length compare.

Proof

  • New crates/rustledger-parser/tests/number_beyond_precision.rs: every context above (one error, at the number, exact message and discriminator), the cost staying unknown, 28 digits clean everywhere, P9 discriminator on 1/0 in amount/metadata/custom/pushmeta, and beancount's error winning on a line it reports.
  • Corpus (parse_error_lines --jsonl + semantic_snapshot, 45,182 inputs: pinned fuzz_parse + fuzz_green_eq_red corpus, compat corpus, every tracked ledger, profiling workloads; release builds of origin/main 1f4b9dc and this branch). 18 inputs differ, all fuzz inputs, all the same change: an expression holding a >28-digit number was reported as P9 (invalid arithmetic expression in metadata value … ×15, … in amount … ×3) and is now P10 at that number, on the same line, same code, same count. No compat or tracked ledger changes. semantic_snapshot differs on exactly those 18 (the error message only).
  • Parity scripts (local, beancount 3.2.3 + ptadiff at the pinned revisions): compat-parse-error-parity --check: no regressions, 122,533 / 123,394 matching, identical to origin/main. compat-semantic-parity --check: no regressions, 9,563 match / 7 mismatch, identical to origin/main. No manifest re-pin needed.
  • cargo test -p rustledger-parser -p rustledger-loader -p rustledger-lsp -p rustledger-wasm --all-features (debug, so the recorder/walk agreement asserts run): 2,415 passed, 0 failed. cargo test -p rustledger-surface-parity-tests: passed. cargo clippy --all-features --all-targets -- -D warnings on parser/loader/lsp/wasm/rledger: clean.
  • Cache: loader CACHE_VERSION 85→86 (with both FIXTURE_VERSIONs), WASM 169→170 (and its loader pin): a file that loaded clean (a cost or metadata number beyond precision) now has a parse error.
  • Docs: compatibility.md P9 and P10 updated. CHANGELOG: Fixed.

Merge note

This and the other parser PRs of this batch (#3162 line-start \r, #3168 BOM on line 1, #3169 leading-gap indented comment, #3170 oracle line index) are independent branches off main; each bumps the cache versions to 86/170, so whichever lands second needs a trivial rebase of cache.rs (bump again).

🤖 Generated with Claude Code

A 31-digit number in a cost spec (`{N USD}`, `{{N USD}}`, either side of
`#`) was dropped with no parse error: the cost became unknown and the
only diagnostic was booking's `interpolation failed`. On a `price` or
`balance` line the directive got a generic `unexpected input` over the
line, and in a balance tolerance, a metadata, `custom` or `pushmeta`
value the number was dropped with no error at all. Each is now reported
at the number (`invalid number in cost (likely exceeds 28-digit Decimal
precision)`, `in amount`, `in metadata value`, ...), as an amount's and
an `@` price's always were (that wording is unchanged). An expression
holding such a number is reported at the number rather than as an
unevaluable expression. beancount 3.2.3 accepts these numbers: P10.

The error oracle's `divide-by-zero` (P9) and `decimal-precision` (P10)
overlays matched these errors by the start of their message. They now
match a typed discriminator, `ParseError::number_error`
(`NumberError::{UnevaluableExpression, BeyondPrecision}`, new), set at
every emitting site, following #3093's `check`. P10 is now claimed, like
P9, only on a line beancount reads and reports nothing on, so an invalid
token after such a number keeps its name.

Cost-spec numbers are checked in the shared cost-spec rule of both tree
walks and the tree builder's recorder; metadata and `pushmeta` values in
the metadata line check; `custom` values in the shared value walk.

Loader cache v86, WASM cache v170: a file that loaded clean (a cost or
metadata number beyond precision) now has a parse error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 30, 2026 15:01
@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

❌ Patch coverage is 95.16129% with 9 lines in your changes missing coverage. Please review.

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

📢 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.8M 289.7M -0.01% ✅ -17.1%
check/investment 448.9M 449.6M +0.17% ✅ -12.9%
check/tagged 419.0M 419.5M +0.10% ✅ -18.6%
check/multicurrency 434.6M 438.7M +0.93% ✅ -24.9%
check/fifo 438.2M 438.7M +0.13% ✅ -17.0%
check/metadata 609.2M 610.1M +0.15% ✅ -23.6%
check-warm/simple 128.5M 128.5M -0.00% ✅ -18.4%
format/simple 608.4M 608.2M -0.03% ✅ +12.8%
format/metadata 1.50G 1.50G +0.15% ✅ +26.1%
format/errors 92.0M 92.1M +0.13% ✅ +103.4%
check/errors 269.0M 269.1M +0.02% ✅ —
check-json/errors 149.1M 149.2M +0.02% ✅ —
check/junkline 30.5M 30.5M -0.05% ✅ +299.9%
check-json/junkline 43.9M 43.8M -0.17% ✅ +614.4%
query/journal/simple 442.6M 442.5M -0.01% ✅ -28.9%
query/groupby/simple 339.0M 339.1M +0.04% ✅ -26.3%
query/groupby/investment 554.8M 556.1M +0.23% ✅ -25.4%
lsp/open-diagnostics/simple 336.6M 336.8M +0.06% ✅ —
lsp/open-diagnostics/metadata 897.0M 898.9M +0.22% ✅ —
lsp/format/simple 1.02G 1.02G +0.05% ✅ —
lsp/format/metadata 2.52G 2.52G +0.11% ✅ —
lsp/edit/simple 670.3M 670.7M +0.06% ✅ —
Workload (wasmtime fuel) base this PR Change vs v0.24.0
wasm/validate/simple 1.14G 1.14G +0.01% ✅ —
wasm/validate/metadata 2.77G 2.78G +0.20% ✅ —
wasm/format/simple 1.39G 1.39G +0.01% ✅ —
wasm/format/metadata 3.53G 3.54G +0.15% ✅ —
wasm/query/simple 1.27G 1.27G +0.01% ✅ —
ffi/load/simple 1.74G 1.74G +0.02% ✅ —
ffi/load/metadata 4.02G 4.03G +0.24% ✅ —
ffi/query/simple 1.37G 1.38G +0.07% ✅ —
ffi/validate/simple 1.21G 1.21G +0.02% ✅ —
ffi/format/simple 1.46G 1.46G +0.01% ✅ —

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 39.4ms ± 0.4ms 829.5ms ± 10.6ms 21.1x 34.9 MB / 51.2 MB
investment 62.3ms ± 0.8ms 2945.7ms ± 15.6ms 47.3x 44.8 MB / 59.1 MB
fifo 61.3ms ± 0.7ms 14752.5ms ± 140.8ms 240.6x 43.2 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 39.4ms ± 1.8ms 39.4ms ± 0.4ms -0.1% ✅ within noise 34.7 MB → 34.9 MB (+0.7%)
investment 61.9ms ± 0.7ms 62.3ms ± 0.8ms +0.5% ✅ within noise 44.8 MB → 44.8 MB (+0.0%)
fifo 61.0ms ± 1.0ms 61.3ms ± 0.7ms +0.5% ✅ within noise 42.8 MB → 43.2 MB (+1.0%)

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

@robcohen
robcohen merged commit 784e8d0 into main Nov 30, 2026
67 of 74 checks passed
@robcohen
robcohen deleted the fix/decimal-precision-typed branch November 30, 2026 19:18
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