Skip to content

fix(loader): normalize @@ prices in the shared finalize phase - #1654

Merged
robcohen merged 1 commit into
mainfrom
fix/normalize-in-finalize
Jun 30, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/normalize-in-finalize

Conversation

@robcohen

Copy link
Copy Markdown
Member

The bug

A v0.17.0 regression: 7 USD @@ 10 EUR surfaces through the FFI component (what rustfava reads) as the raw total price 10 instead of the Beancount-style per-unit price 10/7 ≈ 1.428. rledger check was unaffected.

Root cause

The @@→@ normalization (normalize_prices) lived in exactly one place — the CLI's check.rs — not in the loader's shared process() pipeline. PR #1462 (fix(ffi): route load_source through the canonical pipeline) routed the FFI/MCP surface through that shared pipeline to deduplicate the loader, and in doing so dropped the normalization the CLI still did separately. So process() returned a Finalized ledger that wasn't actually finalized.

This is the same drift category as #1648 (LSP-vs-check): a step that should be a pipeline invariant is instead a per-consumer responsibility, so the next consumer to route through the pipeline silently diverges.

Fix — put the invariant in the type, not the consumer

  • Call normalize_prices in the loader's finalize transition (LateValidated → Finalized). It runs after Late validation, so exact totals still survive for the precise balance-residual check (Replace rust_decimal with arbitrary-precision decimal to close last Python compat divergence #1240) — which is the entire reason normalization is deferred rather than done during booking. finalize is the unique correct home.
  • Finalized is the only publicly-exposed phase and process() is the only path to a Ledger, so "prices are normalized" is now an invariant every consumer gets by construction — CLI, FFI/MCP component, BQL. None can drift, and the compiler enforces it (you cannot obtain a non-finalized ledger).
  • Removed the now-redundant normalize loop (and the unused Directive import) from check.rs.

Tests

  • Loader invariant (process.rs): process()/finalize normalizes @@ to a per-unit @ price (runs without the wasm).
  • FFI component parity (parity.rs): the component surfaces 7 USD @@ 10 EUR as per-unit 1.42857…, matching check — the CLI↔FFI parity guard the regression slipped past.

Verification

No regressions: 16 FFI parity + 92 loader + 105 booking + 362 CLI + 163 query + 104 ops tests pass; clippy -D warnings clean; fmt clean. The only normalize_prices caller is now process::finalize.

🤖 Generated with Claude Code

v0.17.0 regression (#1462): `7 USD @@ 10 EUR` surfaced through the FFI component
(rustfava) as the raw total price (10) instead of the Beancount per-unit price
(10/7 ≈ 1.428). `rledger check` was unaffected.

Root cause: the @@→@ normalization (`normalize_prices`) lived ONLY in the CLI
`check.rs`, not in the loader's shared `process()` pipeline. When #1462 routed the
FFI/MCP surface through that shared pipeline to deduplicate the loader, it lost
the normalization the CLI still did separately — so `process()` returned a
`Finalized` ledger that wasn't actually finalized. The price invariant was a
per-consumer responsibility: the classic drift category (cf. #1648).

Fix — put the invariant in the type, not the consumer:
- Call `normalize_prices` in the `finalize` transition (LateValidated →
  Finalized). It runs AFTER Late validation, so exact totals still survive for
  the precise balance-residual check (#1240) — the reason normalization is
  deferred rather than done during booking.
- `Finalized` is the only publicly-exposed phase and `process()` is the only path
  to a `Ledger`, so 'prices are normalized' is now an invariant every consumer
  (CLI, FFI/MCP component, BQL) gets by construction — none can drift.
- Remove the now-redundant normalize loop (+ unused import) from `check.rs`.

Tests:
- Loader: `process()`/finalize normalizes `@@` to per-unit (invariant; no wasm).
- FFI component parity: the component surfaces `7 USD @@ 10 EUR` as per-unit
  1.42857…, matching `check` — the CLI-vs-FFI parity guard.

No regressions: 16 FFI parity + 92 loader + 105 booking + 362 CLI + 163 query +
104 ops tests pass; clippy -D warnings clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings June 30, 2026 18:50
@robcohen
robcohen requested a review from a team as a code owner June 30, 2026 18:50
@codecov

codecov Bot commented Jun 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

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 32.9ms ± 0.4ms 23.6 MB
beancount 908.5ms ± 7.8ms 50.6 MB
Comparison 27.6x faster 2.1x less

vs main branch (2026-06-30)

Metric main this PR Change
Time 42ms 32.9ms -21.7% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen merged commit f29ec15 into main Jun 30, 2026
44 checks passed
@robcohen
robcohen deleted the fix/normalize-in-finalize branch June 30, 2026 19:12
@robcohen robcohen mentioned this pull request Jun 30, 2026
robcohen added a commit that referenced this pull request Jun 30, 2026
Patch release to ship the @@ total-price normalization fix (#1654): the FFI
component (rustfava) was exposing the raw @@ total instead of the per-unit price.
Workspace 0.17.0 -> 0.17.1 (cargo set-version); packages/mcp-server bumped to match.

Co-authored-by: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen removed the request for review from Copilot June 30, 2026 19:41
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