Repository navigation
refactor(report): single source of truth for account balances (#1726) - #1727
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds regression coverage to prevent reintroducing the #1726 bug where held-commodity reductions (e.g., selling part of a position) fail to net against prior augmentations in rledger report balances and rledger report balsheet.
Changes:
- Add an end-to-end CLI regression test ensuring a buy/sell sequence nets to a single
5 AAPLholding in bothbalancesandbalsheet. - Add clarifying comments in
balancesandbalsheetreport implementations documenting the intentional “sum by commodity, cost-less” behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| crates/rustledger/tests/report_reduction_netting_test.rs | Adds regression tests that exercise rledger report balances/balsheet output for reduction-netting behavior. |
| crates/rustledger/src/cmd/report_cmd/balsheet.rs | Documents the deliberate cost-less inventory summation approach that enables reduction netting. |
| crates/rustledger/src/cmd/report_cmd/balances.rs | Documents the deliberate cost-less inventory summation approach that enables reduction netting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
📊 Benchmark Results
vs main branch (2026-07-08)
What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
robcohen
force-pushed
the
fix/report-reduction-tests
branch
from
July 9, 2026 04:02
0af4ed1 to
85acd0f
Compare
#1726 fixed the symptom — `report balances`/`balsheet` weren't netting a held-commodity reduction against its augmentation — but the root cause was structural: three reports (`balances`, `balsheet`, `income`) each re-derived per-account balances from raw postings with their own copy of the accumulation logic, and the copies had already drifted (`income` was cost-less; `balances`/`balsheet` used a cost lot-key that failed to net reductions). Same question, three implementations, two answers. This makes "what does each account hold" a single function, `report_cmd::account_balances`, that every balance-computing report is a *view* over. The three reports now just partition/filter its output by account prefix; none re-derives balances. The behavior is identical (verified) but the drift class is closed: the reports can no longer disagree, because there is one implementation. - New `account_balances(directives) -> BTreeMap<Account, Inventory>` in report_cmd/mod.rs, documenting the deliberate cost-less sum-by-commodity behavior + its trade-off (different-cost lots merge; cost is never rendered) in the one place it now lives. - `balances`/`balsheet`/`income` rewritten to consume it (−61 lines of duplicated accumulation). - Regression tests (report_reduction_netting_test.rs): buy(10 AAPL {150}) /sell(-5 AAPL {150} @ 180) must render a single netted 5 AAPL in both `balances` and `balsheet`. Verified they fail on pre-#1726 code and pass now; 5 AAPL matches beancount 3.2.3 sum(position). - Drift guard: a test asserts `balances` and `balsheet` report the same figure for the same account — the invariant the shared source enforces, so a future re-fork is caught. `holdings` (needs cost) and `networth` (per-period time series) are deliberately NOT folded in: they are different computations, not drift copies, and both are already correct. Follow-up to #1726 (@weslem). Cost-accurate lot tracking, if ever wanted, now has exactly one place to live. Co-Authored-By: Claude Opus 4.8 <[email protected]>
robcohen
force-pushed
the
fix/report-reduction-tests
branch
from
July 9, 2026 04:07
85acd0f to
9df7a04
Compare
Addresses Copilot review on #1727: assert_netted_to_5 verified every AAPL line reads 5 but not that balsheet actually carries the holding into Total Assets and Net Worth — a regression dropping the commodity from the totals could print a lone 5 AAPL account row and still pass. The balsheet test now asserts the 5 AAPL figure appears in the Total Assets line and the Net Worth section explicitly. Co-Authored-By: Claude Opus 4.8 <[email protected]>
robcohen
enabled auto-merge (squash)
July 9, 2026 04:38
robcohen
added a commit
that referenced
this pull request
Jul 9, 2026
Follow-up to #1727. That PR made `account_balances` the single source of truth but realized cost-less (a held commodity showed `5 AAPL`). This realizes through the booking engine instead, so reports match beancount: `5 AAPL {150.00 USD}`, with reductions matched against lots and different-cost lots kept separate. - account_balances now drives a `BookingEngine` over the (already-booked) directive stream and returns its realized per-account inventories — the same lot-matching the loader's book phase uses, not a re-implementation. Reductions net against their lots by the account's booking method (FIFO/LIFO/…); this supersedes #1727's documented "different-cost lots merge to 20 AAPL" trade-off — they now stay separate (`10 AAPL {150}` + `10 AAPL {200}`), like beancount. - rustledger-booking: `BookingEngine::into_inventories()` exposes the realized map (previously only single-account `inventory()` existed). - balances/balsheet render the lot cost in text/CSV/JSON (new `cost` column/field; empty for plain currency, so income and cash rows are unchanged). Verified against beancount 3.2.3: reduction nets to `5 AAPL {150.00 USD}`, two lots show separately. Report + CLI suites pass; the one failing integration test (test_account_lifecycle_consistency) is pre-existing on the parent branch and unrelated (a bean-check validation test). Co-Authored-By: Claude Opus 4.8 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Jul 9, 2026
) Follow-up to #1727. That PR made `account_balances` the single source of truth but realized cost-less (a held commodity showed `5 AAPL`). This realizes through the booking engine instead, so reports match beancount: `5 AAPL {150.00 USD}`, with reductions matched against lots and different-cost lots kept separate. - account_balances now drives a `BookingEngine` over the (already-booked) directive stream and returns its realized per-account inventories — the same lot-matching the loader's book phase uses, not a re-implementation. Reductions net against their lots by the account's booking method (FIFO/LIFO/…); this supersedes #1727's documented "different-cost lots merge to 20 AAPL" trade-off — they now stay separate (`10 AAPL {150}` + `10 AAPL {200}`), like beancount. - rustledger-booking: `BookingEngine::into_inventories()` exposes the realized map (previously only single-account `inventory()` existed). - balances/balsheet render the lot cost in text/CSV/JSON (new `cost` column/field; empty for plain currency, so income and cash rows are unchanged). Verified against beancount 3.2.3: reduction nets to `5 AAPL {150.00 USD}`, two lots show separately. Report + CLI suites pass; the one failing integration test (test_account_lifecycle_consistency) is pre-existing on the parent branch and unrelated (a bean-check validation test). 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.
Root-cause follow-up to #1726 (@weslem's fix, merged). #1726 closed the specific hole; this closes the class.
What went wrong
Three balance-computing reports —
balances,balsheet,income— each re-derived per-account balances from raw postings with their own copy of the accumulation logic. The copies had already drifted:incomesummed cost-less, whilebalances/balsheetused a cost lot-key that silently failed to net a reduction against its augmentation (the #1726 bug: buy 10 AAPL / sell 5 → showed10and-5instead of5). Same question, three implementations, two answers. #1726 converged them by makingbalances/balsheetcost-less too — correct, but it left three copies that can drift again.The fix
report_cmd::account_balances(directives)is now the single source of truth for "what does each account hold". Every balance-computing report is a view over it — the three reports just partition its output by account prefix; none re-derives balances. One implementation ⇒ they cannot disagree.account_balancesinreport_cmd/mod.rs, with the deliberate cost-less sum-by-commodity behavior + trade-off documented in the one place it lives.balances/balsheet/incomerewritten to consume it (−61 lines of duplicated accumulation, +44 for the shared fn).5 AAPLinbalancesandbalsheet. Verified they fail on pre-Correct balances and balsheet reports for reductions in holdings #1726 code and pass now; matches beancount 3.2.3sum(position).Verified
cargo test -p rustledgerreport/CLI suites: 69 pass (report_parse_cache, cli_commands, pad_expansion, reduction_netting)0 issues, fmt cleanCost-accurate lot tracking, if ever wanted, now has exactly one place to live — the deeper
pipeline-finalize-invariantsdirection (reports/BQL/FFI all reading a booked realization) can build on this.🤖 Generated with Claude Code