Repository navigation
feat(returns): rledger report returns (money-weighted / XIRR) - #1818
Conversation
Wires the rustledger-returns engine (#1815 xirr, #1816 extraction) into a first-class CLI report, the consumer phase-1 piece of #1814. rledger report returns --investments <prefix> [--income <prefix>] [--currency USD] [--end YYYY-MM-DD] - Report::Returns clap variant + report_cmd/returns.rs. --investments (repeatable, required) and --income (repeatable) define the portfolio boundary; --currency defaults to the ledger's first operating_currency; --end defaults to today. Added Report::Returns to the needs_balance_view gate so extraction receives the booked, pad-expanded stream its contract requires. - impl PriceOracle for PriceDatabase via a PriceDbOracle newtype in the CLI (the composition root). The convert signatures are identical, so it is a pass-through; this keeps rustledger-returns a leaf and rustledger-query free of a returns dependency, rather than putting the impl in query. - Reports the money-weighted return plus supporting figures (capital invested, distributions received, current market value, flow count) in text / json / csv. An undefined series (no sign change) reports n/a, not a fabricated rate. - Drift-guard unit test (owed from #1816 round 2, CLAUDE.md Canonical- Function Discipline): terminal_value's realization must equal the market valuation of report_cmd::account_balances' inventories for the same scope and date. Plus 5 integration tests: the boundary model (a dividend-to-bank is a flow only when --income scopes it: 32.36% vs 30.00%), currency default, json fields, and the missing-currency error. Scope: XIRR (money-weighted) only, whole-scope. Time-weighted return and per-commodity grouping remain follow-ups per #1814.
📊 Benchmark Results
vs main branch (2026-07-20)
What do these numbers mean?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
Adds a new CLI report (rledger report returns) that wires the existing rustledger-returns XIRR engine into the rustledger binary, including text/json/csv output and end-to-end tests. This sits in the rustledger (CLI) crate as the consumer/composition root, keeping rustledger-returns as a leaf by adapting the query crate’s PriceDatabase behind the PriceOracle trait.
Changes:
- Introduces
Report::Returnsclap variant and dispatcher plumbing to ensure the report receives the booked, pad-expanded directive stream (balance_input). - Implements the new report in
report_cmd/returns.rs, includingPriceDbOracleadapter, currency defaulting, flow extraction, terminal valuation, and output rendering (text/json/csv). - Adds integration tests covering the boundary model (
--income), JSON fields, default reporting currency behavior, and missing-currency error path; updates Cargo dependencies to includerustledger-returns.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| crates/rustledger/tests/report_returns_test.rs | New end-to-end tests for rledger report returns behavior and error paths. |
| crates/rustledger/src/cmd/report_cmd/returns.rs | New returns report implementation + PriceDbOracle adapter + drift-guard unit test. |
| crates/rustledger/src/cmd/report_cmd/mod.rs | Registers the report module/variant, routes balance_input, and plumbs operating_currency into rendering. |
| crates/rustledger/Cargo.toml | Adds rustledger-returns dependency to the CLI crate. |
| Cargo.toml | Adds workspace dependency entry for rustledger-returns. |
| Cargo.lock | Records the new dependency in the lockfile. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…% (review) Deep review of the returns CLI found two defects plus surfaced an output bug and a coverage gap. - Canonical-Function Discipline: report_returns hand-combines extract_flows + terminal_value into the xirr series (it needs the parts for the summary breakdown), duplicating the engine's canonical extract_cash_flows. Pinned the two as equivalent with a drift-guard test (series_matches_extract_cash_flows) and a cross-reference comment, so an engine change to how the series is assembled can't silently drift the CLI's rate. - Per-format precision: the money-weighted return was 2 decimals for text/csv but 4 for JSON, so the same ledger reported different rates across formats. Unified to 2 decimals everywhere. - Negative-zero display: a 0% return (capital returned unchanged) converges to a tiny signed epsilon that rendered as "-0.00%". Normalized so a rate rounding to zero shows "0.00%" in every format. - Coverage: added an integration test valuing a pad+balance-seeded holding, proving the needs_balance_view wiring (Report::Returns must be in that gate or the pad-expanded stream never reaches extraction and the position vanishes). The prior fixtures used only explicit buys. 8 returns tests (2 unit drift-guards + 6 integration).
Deep review — round 1Workflow-backed review (4 finder angles + adversarial verify) on the CLI consumer. Two confirmed findings, one refuted; it also cleared the JSON- Canonical-Function Discipline (CONFIRMED)
Per-format rate precision (CONFIRMED)The money-weighted return was formatted at 2 decimals for text/csv but 4 for JSON — the same ledger reported different rates across formats. Fixed: unified to 2 decimals everywhere (JSON emits the bare 2dp number). Surfaced while adding coverage
Refuted (agreed)Money fields not 8 returns tests (2 unit drift-guards + 6 integration); clippy/doc/fmt clean. |
Copilot review: the currency-default test used out.contains("USD"), which
passes on the fixtures other USD literals even if the default were wrong.
Assert the Reporting currency lines value is USD instead.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
|
Good catch on the vacuous |
What
The consumer that makes investment returns reachable from the command line — phase 1's user-facing piece of #1814, built on the merged XIRR engine (#1815) and cash-flow extraction (#1816):
How it wires together
Report::Returnsclap variant +report_cmd/returns.rs.--investments(repeatable, required) and--income(repeatable) define the portfolio boundary;--currencydefaults to the ledger's firstoperating_currencyoption;--enddefaults to today.Report::Returnsis added to theneeds_balance_viewgate so extraction receives the booked, pad-expanded stream its input contract requires.impl PriceOracle for PriceDatabasevia aPriceDbOraclenewtype in the CLI (the composition root). Theconvertsignatures are byte-identical, so it's a pass-through. This keepsrustledger-returnsa leaf andrustledger-queryfree of a returns dependency, rather than putting the impl in query and pulling returns into every BQL build.n/a, never a fabricated rate.The boundary model, visible in the tests
The
--incomescope decides whether a dividend-to-bank counts. On the same ledger (buy 10 AAPL for 1000, a 20 dividend to the bank, hold to a 130 close):--investments Assets:Broker --income Income:Dividends--investments Assets:Broker(no--income)Without
--income, the dividend transaction touches no investment account, so it isn't a flow — the boundary model, working as designed.Tests
terminal_value's realization must equal the market valuation ofreport_cmd::account_balances' inventories for the same scope/date. The two deliberately re-derive the same realization loop across a crate boundary; this pins their agreement.Scope
XIRR (money-weighted) only, whole-scope. Time-weighted return and per-commodity grouping remain follow-ups per #1814 (TWR isn't in the engine yet). No new publishable crate (returns landed in #1815).
Part of #1814.
🤖 Generated with Claude Code