Skip to content

fix: compound costs mispriced in doctor region and ops clamp - #1732

Merged
robcohen merged 4 commits into
mainfrom
fix/compound-cost-consumers
Jul 10, 2026
Merged

robcohen merged 4 commits into
mainfrom
fix/compound-cost-consumers

Conversation

@robcohen

@robcohen robcohen commented Jul 9, 2026

Copy link
Copy Markdown
Member

Found by the Phase-0 duplication sweep (cost-resolution family, L2/L3) and confirmed empirically. Two consumers computed cost via the total()/per_unit() accessor chain — both return None for CostNumber::Compound, so both silently miscosted {a # b} lots.

The bugs

  • doctor region --conversion cost (L3, trivially reachable): reads the RAW load stream where Compound survives as-written. The accessor fallthrough displayed the bare unit count mislabeled with the cost currency — 10 WIDGET {5.00 # 10.00 USD} showed "10 USD" instead of 60.00 USD, and the net-changes view didn't balance (+10/−60).
  • ops::clamp (L2, contract-gated): the hand-rolled per-unit ladder in posting_position had no Compound arm — a pre-window compound lot was summarized cost-less, destroying its basis in the clamped opening balance. Masked on the standard load path (booking rewrites Compound → PerUnitFromTotal first), but ops::clamp is a public API and the WIT builder input type explicitly allows Compound from hosts.

The fixes

Tests

  • core total_for unit test (all four variants)
  • clamp regression: compound basis survives clamping (summarized per-unit 6.00; was None)
  • doctor integration: 60.00 USD asserted; the unit-count-mislabel shape pinned against

ops+core suites green, clippy 0, fmt clean.

🤖 Generated with Claude Code

Two consumers computed cost via the total()/per_unit() accessor chain,
which returns None for CostNumber::Compound — both silently miscost
compound (`{a # b}`) lots. Found by the Phase-0 duplication sweep
(cost-resolution family) and confirmed empirically:

- `doctor region --conversion cost` (L3, trivially reachable): reads
  the RAW load stream where Compound survives as-written; the accessor
  fallthrough displayed the bare unit count mislabeled with the cost
  currency — `10 WIDGET {5.00 # 10.00 USD}` showed "10 USD" instead of
  60.00 USD, and the net-changes view didn't balance (+10/-60).
- `ops::clamp` (L2, contract-gated): the hand-rolled per-unit ladder in
  posting_position had no Compound arm, so a pre-window compound lot
  was summarized COST-LESS — basis destroyed in the clamped opening
  balance. Masked on the standard load path (booking rewrites Compound
  first) but ops::clamp is a public API and the WIT builder input type
  explicitly allows Compound from hosts.

Fixes:
- core: new `CostNumber::total_for(units)` — exhaustive over all four
  variants (stored totals stay precision-exact; Compound = N·a + b per
  the #1704 rule). Doc directs total-cost consumers here instead of
  accessor chains.
- doctor region: accessor chain → total_for. Now shows 60.00 USD and
  balances.
- ops clamp: posting_position delegates to the canonical
  Position::from_posting (→ CostSpec::resolve, 4-variant-complete),
  with the transaction date for lot-date fill — matching how booking,
  query, and reports build lots.

Regression tests: core total_for (all variants), clamp compound-basis
preservation (summarized per-unit 6.00), doctor region integration
(60.00 USD asserted, unit-count-mislabel pinned against).

Co-Authored-By: Claude Fable 5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner July 9, 2026 16:06
Copilot AI review requested due to automatic review settings July 9, 2026 16:06
@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 33.9ms ± 4.3ms 23.6 MB
beancount 875.5ms ± 6.3ms 50.7 MB
Comparison 25.8x faster 2.1x less

vs main branch (2026-07-10)

Metric main this PR Change
Time 33.7ms 33.9ms +0.6% ✅
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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect handling of compound costs ({a # b}) in two downstream consumers that operate on (or can ingest) pre-booking cost specs, by introducing a canonical “total cost for N units” helper and routing affected code paths through complete cost-resolution logic.

Changes:

  • Add CostNumber::total_for(units) to compute a total cost across all CostNumber variants (including Compound).
  • Update rledger doctor region --conversion cost to use total_for instead of a total()/per_unit() accessor chain.
  • Update ops::clamp to build positions via Position::from_posting (delegating to CostSpec::resolve) and add regressions tests for doctor + clamp.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
crates/rustledger-core/src/cost.rs Adds CostNumber::total_for and unit tests for all variants.
crates/rustledger/src/cmd/doctor/region.rs Uses total_for for cost conversion display in doctor region.
crates/rustledger-ops/src/clamp.rs Replaces local cost/per-unit ladder with Position::from_posting; adds regression test for compound basis preservation.
crates/rustledger/tests/doctor_region_compound_cost_test.rs New integration test asserting correct compound total display and guarding against the prior “units mislabeled as cost” output shape.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger-core/src/cost.rs Resolved
@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
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 and others added 3 commits July 10, 2026 02:23
…nits (review)

Two review catches on the original fix:

- total_for returned the unsigned stored total for Total /
  PerUnitFromTotal (and an unsigned lump in Compound) while PerUnit
  was signed through multiplication — a reduction costed with
  opposite signs depending on spec shape. All variants now take the
  sign from units, matching the balance-weight rule; new test pins
  negative- and zero-units behavior for every variant.

- doctor region's cost branch handled a numberless cost spec (`{}`,
  bare `{USD}` lot matching) by displaying the unit count labeled
  with the cost currency — the original mislabel bug in a different
  coat. Numberless specs now fall through to the units display.

Co-Authored-By: Claude Fable 5 <[email protected]>
A bare "60.00 USD" match was satisfied by the cash leg and passed
even under the pre-fix bug; only the negative assertion actually
pinned the fix.

Co-Authored-By: Claude Fable 5 <[email protected]>
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 47c0978 into main Jul 10, 2026
45 of 46 checks passed
@robcohen
robcohen deleted the fix/compound-cost-consumers branch July 10, 2026 09:13
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.

2 participants