Repository navigation
fix(ffi): a balance diff belongs to its own assertion - #2242
Conversation
`attach_balance_diffs` keyed the validator's results by `(date, account, currency)`. Those three do not identify a balance assertion: a ledger may assert the same account and currency twice on one date with different amounts, and so different differences. Building a map collapsed them, and one directive received the other's diff. Closes #2239. 2024-03-01 balance Assets:Cash 90.00 USD 2024-03-01 balance Assets:Cash 120.00 USD before both carry -20.00 after 90.00 carries 10.00, 120.00 carries -20.00 bean-query reports 10.00 and -20.00 here. The query layer had the same collision, fixed in #2238, which is also what added `BalanceActual::asserted` -- so the fix here is the key, not the semantics. This surface deliberately attaches a diff for EVERY assertion including passing ones, where it is zero; that is its contract (#1663) and is unchanged. The asserted number is compared as the STRING the WIT directive carries. Both sides come from `Decimal::to_string()` on the same value -- `amount_from_core` for the directive, the validator's record for the map -- so they agree by construction rather than by luck. Tested at the WIT level, because the collision is in the map the component builds and a validator unit test cannot see it. The component wasm was REBUILT for both runs: the checked-in one was three days stale, and testing against it would have measured old code either way. The test fails against a freshly built unfixed component (both diffs -20.00) and passes against a freshly built fixed one. No WIT change; the wire contract is untouched.
There was a problem hiding this comment.
🟡 Changes recommended
The updated doc comment in attach_balance_diffs states passing assertions have a zero diff, but the validator records and exposes the raw signed computed - asserted even when within tolerance, making the contract documentation inaccurate.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Fixes an FFI Component bug where attach_balance_diffs could attach the wrong balance diff to a balance directive when multiple assertions share the same (date, account, currency), by keying the validator results with the asserted amount as well (closing #2239). Adds a WIT-level parity test to prevent regressions at the component boundary.
Changes:
- Update
attach_balance_diffsto key balance actuals by(date, account, currency, asserted)instead of(date, account, currency). - Add a parity test that loads a ledger with two same-day balance assertions and verifies each directive receives its own
diff.
File summaries
| File | Description |
|---|---|
| crates/rustledger-ffi-component/src/convert.rs | Fixes the balance-actual lookup key used to attach diff values to WIT balance directives (prevents same-day assertion collisions). |
| crates/rustledger-ffi-component-tests/tests/parity.rs | Adds a component-level regression test covering the same-day, same-account, same-currency double-assertion case. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
The comment claimed a passing assertion carries a diff of zero. It does not: `BalanceActual::diff` is the raw signed `computed - asserted`, recorded before the tolerance is consulted, so an assertion that passes WITHIN tolerance carries its real difference. Measured through the component: `100.01` asserted against an accumulated `100.00` under a `0.05` tolerance carries `-0.01`, and only an exact match carries `0.00`. That distinction is the reason `#balances.discrepancy` could not reuse this value directly -- it reports a difference only for failures -- so the comment was wrong about the very thing it existed to explain. Caught in review of #2242. No behavior change.
|
Right, and the comment was wrong about the thing it existed to explain — corrected in the latest commit.
The comment now says the value is the raw signed difference, zero only on an exact match — and notes that this is precisely why |
attach_balance_diffskeyed the validator's results by(date, account, currency). Those three do not identify a balance assertion — a ledger may assert the same account and currency twice on one date with different amounts, and so different differences. Building a map collapsed them, and one directive received the other's diff. Closes #2239.bean-query reports
10.00and-20.00here. The query layer had the same collision, fixed in #2238 — which is also what addedBalanceActual::asserted, so the fix here is the key, not the semantics.This surface deliberately attaches a diff for every assertion including passing ones, where it is zero. That is its contract (#1663) and is unchanged, which is why this wasn't folded into #2238.
The asserted number is compared as the string the WIT directive carries. Both sides come from
Decimal::to_string()on the same value —amount_from_corefor the directive, the validator's record for the map — so they agree by construction rather than by luck. I checked that rather than assuming, since a string key that silently never matched would drop the diff entirely, which is worse than attaching the wrong one.Tested at the WIT level
The collision is in the map the component builds, so a validator unit test cannot see it. When I filed #2239 I noted I'd read the code but not built a component-level test; this is that test.
The component wasm was rebuilt for both runs. The checked-in one was three days stale, and testing against it would have measured old code whichever way it came out. Against a freshly built unfixed component the test fails with both diffs at
-20.00; against a freshly built fixed one it passes.Full
paritysuite: 27 passed, including the existingload_reports_balance_assertion_failurethat pins the #1663 contract.No WIT change — the wire contract is untouched, only which record attaches to which directive.
4725 workspace tests green, clippy clean on 1.97.0, typos clean.