Repository navigation
fix: compound costs mispriced in doctor region and ops clamp - #1732
Merged
Merged
Conversation
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]>
Contributor
📊 Benchmark Results
vs main branch (2026-07-10)
What do these numbers mean?
|
Contributor
There was a problem hiding this comment.
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 allCostNumbervariants (includingCompound). - Update
rledger doctor region --conversion costto usetotal_forinstead of atotal()/per_unit()accessor chain. - Update
ops::clampto build positions viaPosition::from_posting(delegating toCostSpec::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.
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 returnNoneforCostNumber::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 inposting_positionhad 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), butops::clampis a public API and the WIT builder input type explicitly allows Compound from hosts.The fixes
CostNumber::total_for(units)— exhaustive over all four variants; stored totals stay precision-exact (the post-Type-driven: make CostSpec invalid states unrepresentable #1164 preference), Compound =N·a + b(the fix(parser,booking)!: compound cost {a # b} carries both components (N*a + b) #1704 rule). Doc directs total-cost consumers here instead of accessor chains.total_for. Shows 60.00 USD; balances.posting_positiondelegates to the canonicalPosition::from_posting(→CostSpec::resolve, the only 4-variant-complete path), with the transaction date for lot-date fill — matching booking/query/reports.Tests
total_forunit test (all four variants)None)ops+core suites green, clippy 0, fmt clean.
🤖 Generated with Claude Code