Repository navigation
fix(core): try_reduce is now reduce-on-a-clone — equivalent by construction - #1690
Merged
Merged
Conversation
…uction
The read-only preview duplicated every booking method's selection logic in a
parallel try_* tree (~330 lines) and had drifted from the mutating path in
three places, found by a new equivalence property test used as the oracle:
- STRICT: try_ fell back to FIFO on ANY multi-lot match; reduce returns
AmbiguousMatch unless the lots are financially interchangeable.
- NONE: try_ routed to try_reduce_ordered (NoMatchingLot on an empty
inventory); reduce_none appends/shorts.
- {*} merge: try_ dispatched to try_reduce_average; the mutating path uses
the separate reduce_merge.
This is the recurring one-logic-two-paths drift class (#1648, #1663, #1686).
Structural fix: try_reduce = reduce on a clone — equivalence holds by
construction and cannot re-drift. The clone is cheap (imbl::Vector positions
clone via O(1) structural sharing; reduce's copy-on-write rebuild touches only
the clone). The whole try_* tree is deleted.
New tests/try_reduce_equivalence.rs pins the obligation: 500 randomized cases
across all 7 methods x satisfiable/unsatisfiable specs (labels, unmatched
costs, {*} merge, wrong currency, oversells) — preview == commit exactly.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
📊 Benchmark Results
vs main branch (2026-07-02)
What do these numbers mean?
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes Inventory::try_reduce predict Inventory::reduce exactly by implementing try_reduce as reduce executed on a cloned inventory, eliminating duplicated booking-method selection logic that previously drifted from the mutating path.
Changes:
- Re-implement
Inventory::try_reduceasself.clone().reduce(...), deleting the paralleltry_*dispatch tree. - Add a proptest property test to pin
try_reduce(...) == reduce(&mut inv.clone(), ...)across booking methods/specs and error cases. - Check in a proptest regression seed for a previously failing minimal counterexample.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| crates/rustledger-core/src/inventory/booking.rs | Replaces try_reduce’s duplicated logic with reduce-on-a-clone to guarantee equivalence. |
| crates/rustledger-core/tests/try_reduce_equivalence.rs | Adds randomized equivalence property testing for try_reduce vs reduce. |
| crates/rustledger-core/tests/try_reduce_equivalence.proptest-regressions | Adds a saved regression seed to re-run a previously found failing case. |
💡 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 |
Merged
robcohen
added a commit
that referenced
this pull request
Jul 2, 2026
Patch release. User-facing fixes since v0.19.0: - NONE booking: over-reduction now shorts past zero from ANY balance (consumes available units, appends the remainder as a negative position), fixing the inconsistency where shorting from a positive balance was rejected while shorting from zero was allowed (#1686, via #1688). - try_reduce: the read-only preview is now reduce-on-a-clone — equivalent to the mutating path by construction. Previously it diverged under STRICT (FIFO fallback instead of AmbiguousMatch), NONE (error instead of short), and {*} merge (wrong dispatch) (#1690). No WIT change (rustledger:ledger stays 3.2.0); embedders are unaffected. Also carries the formal-verification hardening arc (#1683-#1697): behavior replay across 11 corpora, dual-direction trace validation, exact-rational AVERAGE model, coverage lint, weekly escalated replay, pad-directive spec, and Kani proofs for the new NONE semantics. Workspace 0.19.0 -> 0.19.1; packages/mcp-server bumped to match. Co-authored-by: Claude Opus 4.8 <[email protected]>
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.
FV follow-up #1 (of the improvement menu): the
try_reducepreview must predictreduceexactly — and it didn't.The divergences (equivalence property written first, as the oracle)
try_reducere-implemented every booking method's selection logic in a paralleltry_*tree (~330 lines). The property test caught it drifting in three places:try_fell back to FIFO on any multi-lot match;reducereturnsAmbiguousMatchunless the lots are financially interchangeable.try_routed to ordered reduction (NoMatchingLoton an empty inventory);reduce_noneappends/shorts (oracle's minimal case).{*}merge:try_dispatched totry_reduce_average; the mutating path uses the separatereduce_merge.Same one-logic-two-paths class as #1648/#1663/#1686 — a preview surface silently disagreeing with the commit.
The structural fix
try_reduce=reduceon a clone — equivalent by construction, cannot re-drift, and deletes the whole duplicate tree (−326 lines). Cheap:positionsis animbl::Vector(O(1) structural-sharing clone);reduce's copy-on-write rebuild touches only the clone. (Note:try_reducecurrently has no in-repo callers — deleting the API entirely is also an option, but keeping it correct + guarded seemed the conservative call; happy to drop it instead.)Guard
tests/try_reduce_equivalence.rs: 500 randomized cases × 7 methods × satisfiable/unsatisfiable specs (labels, unmatched costs,{*}, wrong currency, oversells) — preview == commit exactly (sameOkmatched+basis or sameErr).Verification
Equivalence property green; core lib 422/422 (no existing test had pinned the divergent preview); replay + TLA proptests green; clippy
-D warnings+ fmt clean.🤖 Generated with Claude Code