Skip to content

fix(booking): prorate never rounds a nonzero half-floor share to zero - #3039

Merged
robcohen merged 1 commit into
mainfrom
fix/prorate-tiny-values
Nov 26, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/prorate-tiny-values

Conversation

@robcohen

Copy link
Copy Markdown
Member

What changed

rustledger_booking::prorate no longer returns 0 for a nonzero share of exactly half of Decimal's 1e-28 floor.

The differential proptest (tests/prorate_differential.rs) found this on a random draw. Shrunk case: prorate(1e-25, 0.002, 4). The product 2e-28 is exact, so the fast path divides it. rust_decimal's checked_div breaks ties to even, so 5e-29 becomes 0. The exact reference rounds it half away from zero, to 1e-28. The test's one-ulp tolerance allows tie-to-even everywhere else, but a nonzero share flushed to zero is the #2344 / #2346 failure class.

Root cause

The cause is a tie-break at the scale limit, not the order of operations: the fast path's quotient is a tie whose even neighbor is zero. (The older saved seed, 1e-20 * 1e-9 / 0.1, was the product-underflow case, and the existing scale guard already sends it to the exact path.)

Fix

The fast path now also falls through to the existing exact BigDecimal path when it gives a zero share of a nonzero product. Every other zero quotient is below half the floor, and the exact path rounds it to zero too, so the extra cost applies only to zero shares. Nothing changes on the hot path for nonzero results.

Callers affected

  • book.rs: the sale value of a @@ total price split across lots, which the gain is booked from.
  • book.rs: the per-lot @@ price annotation.
  • rustledger-plugin valuation plugin.
  • rustledger-budget accrual split.

In each one, a share of exactly ±5e-29 now comes out as ±1e-28 instead of 0. Booked directives can change, so the WASM CACHE_VERSION is bumped to 135 (133 and 134 are taken by other PRs). The loader cache archives only parsed directives, so it is not bumped.

Tests

  • Named regressions in crates/rustledger-booking/tests/prorate.rs: the shrunk tie case with both signs, the older seed, and below-half-floor shares that stay zero.
  • Both proptest seeds are added to prorate_differential.proptest-regressions. Without the fix, the new seed fails deterministically with "fabricated zero".
  • cargo clippy --all-features --all-targets -D warnings, and tests for booking, loader, validate, rustledger, plugin, budget and wasm.

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 26, 2026 03:49
@codecov

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

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 41.9ms ± 0.9ms 592.7ms ± 6.0ms 14.2x 43.0 MB / 50.9 MB
investment 64.3ms ± 0.4ms 2195.6ms ± 27.5ms 34.1x 47.3 MB / 59.0 MB
fifo 63.2ms ± 1.0ms 12052.7ms ± 401.1ms 190.8x 48.4 MB / 58.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 41.3ms ± 0.3ms 41.9ms ± 0.9ms +1.5% ✅ within noise 43.4 MB → 43.0 MB (-1.0%)
investment 64.7ms ± 1.1ms 64.3ms ± 0.4ms -0.5% ✅ within noise 47.4 MB → 47.3 MB (-0.4%)
fifo 62.5ms ± 1.2ms 63.2ms ± 1.0ms +1.0% ✅ within noise 49.3 MB → 48.4 MB (-1.8%)

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

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 180 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

rust_decimal division breaks ties to even, so a share of exactly half of
the 1e-28 floor (1e-25 * 0.002 / 4 = 5e-29) came back as 0 from the fast
path, a nonzero share flushed to nothing, where the exact reference
rounds it to 1e-28. A zero share of a nonzero product now falls through
to the exact BigDecimal path; nonzero results are unaffected.

Booked @@ splits can change, so the WASM cache moves to v135.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/prorate-tiny-values branch from f90a562 to cc9e3cf Compare November 26, 2026 04:28
@robcohen
robcohen merged commit ab5d4ac into main Nov 26, 2026
69 of 70 checks passed
@robcohen
robcohen deleted the fix/prorate-tiny-values branch November 26, 2026 05:13
robcohen added a commit that referenced this pull request Nov 26, 2026
…breadth-first order, each currency once (#3038)

beancount 3.2.3 parses its files breadth first and `aggregate_options_map`
merges `operating_currency` in that order, `uniquify`d (first occurrence
wins, the top-level file's own repeats included). rledger merged it depth
first and kept repeats: `main` (MMM) including `x` (XXX, including `z`
ZZZ) then `y` (YYY) gave [MMM, XXX, ZZZ, YYY] against [MMM, XXX, YYY, ZZZ],
and USD in main and an include gave [USD, USD, EUR] against [USD, EUR].

Auditing the other options beancount aggregates across files:
`display_precision` is merged into the top-level file's display context
with `DisplayContext::update_from`, which keeps a fixed precision, so the
top-level file's value wins; rledger let the last file read win (an
include's USD:0.001 rendered 1.23456 USD as 1.235 where bean-query gives
1.23). `insert_pythonpath` becomes a sorted set there (no order) and has
no effect in rledger. Included plugins are ignored by beancount and run by
rledger deliberately; their order is unchanged.

- `Options::set_in_file_at` records each merged option's values with its
  file; `Options::merge_across_files(ranks)` is the one merge, called by
  `Loader::load` over `SourceMap::parse_ranks` before the display context
  and the cache see the options. Every surface (CLI, LSP, WASM, FFI) reads
  the loader's options or the cache's merged copy.
- `apply` lists a repeated operating currency once, for options set
  without the loader.
- Loader CACHE_VERSION 77 -> 78 (cached values change), WASM pin 78, WASM
  CACHE_VERSION 137 (135, 136 taken by #3039, #3040).
- Tests: unit tests for the merge; loader tests for the issue's trees, a
  duplicate include, a disk glob and a cache round trip, all checked
  against bean-check 3.2.3; drift guards across the loader, WASM `Ledger`
  (and restored), ffi-wasi, the LSP, and the CLI cold and on a cache hit
  (`report returns` currency, `query` precision).

Closes #3038

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 26, 2026
…breadth-first order, each currency once (#3041)

* fix(loader): merge operating_currency across includes in beancount's breadth-first order, each currency once (#3038)

beancount 3.2.3 parses its files breadth first and `aggregate_options_map`
merges `operating_currency` in that order, `uniquify`d (first occurrence
wins, the top-level file's own repeats included). rledger merged it depth
first and kept repeats: `main` (MMM) including `x` (XXX, including `z`
ZZZ) then `y` (YYY) gave [MMM, XXX, ZZZ, YYY] against [MMM, XXX, YYY, ZZZ],
and USD in main and an include gave [USD, USD, EUR] against [USD, EUR].

Auditing the other options beancount aggregates across files:
`display_precision` is merged into the top-level file's display context
with `DisplayContext::update_from`, which keeps a fixed precision, so the
top-level file's value wins; rledger let the last file read win (an
include's USD:0.001 rendered 1.23456 USD as 1.235 where bean-query gives
1.23). `insert_pythonpath` becomes a sorted set there (no order) and has
no effect in rledger. Included plugins are ignored by beancount and run by
rledger deliberately; their order is unchanged.

- `Options::set_in_file_at` records each merged option's values with its
  file; `Options::merge_across_files(ranks)` is the one merge, called by
  `Loader::load` over `SourceMap::parse_ranks` before the display context
  and the cache see the options. Every surface (CLI, LSP, WASM, FFI) reads
  the loader's options or the cache's merged copy.
- `apply` lists a repeated operating currency once, for options set
  without the loader.
- Loader CACHE_VERSION 77 -> 78 (cached values change), WASM pin 78, WASM
  CACHE_VERSION 137 (135, 136 taken by #3039, #3040).
- Tests: unit tests for the merge; loader tests for the issue's trees, a
  duplicate include, a disk glob and a cache round trip, all checked
  against bean-check 3.2.3; drift guards across the loader, WASM `Ledger`
  (and restored), ffi-wasi, the LSP, and the CLI cold and on a cache hit
  (`report returns` currency, `query` precision).

Closes #3038

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(loader): don't link the private MERGED_ACROSS_FILES from public docs

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

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