Skip to content

refactor(report): single source of truth for account balances (#1726) - #1727

Merged
robcohen merged 2 commits into
mainfrom
fix/report-reduction-tests
Jul 9, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/report-reduction-tests

Conversation

@robcohen

@robcohen robcohen commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

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: income summed cost-less, while balances/balsheet used a cost lot-key that silently failed to net a reduction against its augmentation (the #1726 bug: buy 10 AAPL / sell 5 → showed 10 and -5 instead of 5). Same question, three implementations, two answers. #1726 converged them by making balances/balsheet cost-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.

  • New shared account_balances in report_cmd/mod.rs, with the deliberate cost-less sum-by-commodity behavior + trade-off documented in the one place it lives.
  • balances/balsheet/income rewritten to consume it (−61 lines of duplicated accumulation, +44 for the shared fn).
  • Regression tests: buy(10 AAPL {150})/sell(-5 AAPL {150} @ 180) must net to a single 5 AAPL in balances and balsheet. Verified they fail on pre-Correct balances and balsheet reports for reductions in holdings #1726 code and pass now; matches beancount 3.2.3 sum(position).

Verified

Cost-accurate lot tracking, if ever wanted, now has exactly one place to live — the deeper pipeline-finalize-invariants direction (reports/BQL/FFI all reading a booked realization) can build on this.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings July 9, 2026 03:54
@robcohen
robcohen requested a review from a team as a code owner July 9, 2026 03:54
@codecov

codecov Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/report_cmd/balsheet.rs 90.00% 1 Missing ⚠️
crates/rustledger/src/cmd/report_cmd/income.rs 87.50% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

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

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 AAPL holding in both balances and balsheet.
  • Add clarifying comments in balances and balsheet report 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.

@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 28.8ms ± 1.1ms 24 MB
beancount 609.7ms ± 2.3ms 50.7 MB
Comparison 21.2x faster 2.1x less

vs main branch (2026-07-08)

Metric main this PR Change
Time 35ms 28.8ms -17.7% 🟢
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

@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 810
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
robcohen force-pushed the fix/report-reduction-tests branch from 0af4ed1 to 85acd0f Compare July 9, 2026 04:02
@robcohen robcohen changed the title test(report): pin reduction-netting for balances/balsheet (#1726) refactor(report): single source of truth for account balances (#1726) Jul 9, 2026
#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
robcohen force-pushed the fix/report-reduction-tests branch from 85acd0f to 9df7a04 Compare July 9, 2026 04:07
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
robcohen enabled auto-merge (squash) July 9, 2026 04:38
@robcohen
robcohen merged commit a1308ed into main Jul 9, 2026
46 checks passed
@robcohen
robcohen deleted the fix/report-reduction-tests branch July 9, 2026 05:02
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]>
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