Group a portfolio's income by month, with its fees and fee ratio
This is the upstream pre-flight for slice 6 of 7 of the portfolio return engine (drop 2 of #118). The upstream PR on we-promise/sure is NOT YET OPENED (awaiting owner approval). When it is opened it will be from this branch: same branch, same commit.
Base is the parent slice's branch, so this diff shows only this slice's commits. Fork stack: #372 daily returns → #373 journal flows → #374 drivers → #376 performance (carrying #375 return scope, which is also based on up/pending-sql-parity) → #377 income → #378 income by security.
Part of we-promise/sure#4078
Part of we-promise/sure#3494
Slice 6 of 7 of the portfolio returns engine; we-promise/sure#4078 is the issue (part 4, income).
Fork pre-flight PR: jaysbeekay/sure#377 (https://github.com/jaysbeekay/sure/pull/377), same branch, same commit.
Stacked on the performance PR. Until that merges, GitHub shows its commits here as well; this PR's own commits are the ones after up/portfolio-performance. I will rebase onto main as each parent merges.
Stack, in merge order: we-promise/sure#3557 (flow classifier) → daily returns → journal flows → drivers → return scope → performance → income → income by security. Return scope also stands alone on we-promise/sure#3557.
Summary
Adds Portfolio::Income: the period's dividend and interest income by calendar month, the fees charged against the same accounts, and the fee ratio. Served through Portfolio::Performance#income.
What changed
app/models/portfolio/income.rb (new): regroups the daily rows' income and fees by month and re-derives nothing. What counts as income (a Trade-shaped dividend since we-promise/sure#1311, or a Transaction-shaped one) is the classifier's answer, already converted at the previous day's rate, so the months add up to the drivers' income.
- A month that paid nothing is absent; a month whose payouts and reversals net to zero or below is kept, so the buckets always add up to the total.
- Fees are reported apart, as a positive magnitude, not netted out of income.
fee_ratiois fees over the period's average daily value, so it does not shrink as the portfolio grows, and nil when there was no value.
app/models/portfolio/performance.rb: #income, a cache entry of its own computed only when asked, so a caller that reads only the returns never pays for it. The fee ratio is withheld when a rate is missing; the money figures are not.
Demo data
None needed: model only, no UI. Exercised by the tests below.
Migration notes
None. No schema change; reads existing balances, entries, trades, transactions, holdings and exchange_rates columns.
Verification
income_test.rb+performance_test.rb: 42 runs, 107 assertions, 0 failures.- Mutations, all caught:
- empty months kept: 6 tests;
- a net-zero month dropped: 1;
- a negative month dropped: 2;
- fees netted out of income: 1;
- fee ratio over the closing value: 2;
- fee ratio 0 instead of nil with no value: 1;
- buckets unsorted: 2;
- fee ratio not withheld on a missing rate: 1;
- the rate-missing flag not passed to income: 1.
- Full suite after the cut-off follow-up, at the top of the stack (
up/portfolio-income-by-securityat 1dc444d54): 10,782 runs, 45,992 assertions, 0 failures, 0 errors, 33 skips (local, PostgreSQL 16, 4 workers). bin/rubocopon the changed files: no offenses.bin/brakeman --no-pager -q: 0 warnings.
Blast radius
Who else hits this line, and what do they pass? Portfolio::Performance#income is the only caller of Portfolio::Income. The #metrics blob and its cache key are unchanged, and income is a separate _income entry.
What else reads what I just changed the reading of? Row#income and Row#fees are also summed by Portfolio::Drivers. A test asserts the two agree.
Engine fix round (2026-10-09)
Cut-off follow-up. up/portfolio-performance is merged down again (fa668c673, --no-ff, clean, no conflicts) to bring in ReturnScope reading its trade, valuation and transfer history only up to each account's cut-off (#375 9d2d58028, cherry-picked onto #376 as 164e38dcd). Nothing in this slice reads ReturnScope directly.
up/portfolio-performance is merged down with a --no-ff merge, clean, no force push. That brings CACHE_VERSION v8 and the fixture move.
| Commit | Finding | Test | Fail-first / mutation |
|---|---|---|---|
| f5acb4cd2 | cubic VcEb (on #376): eur_account_with_fee belongs here | re-added beside the two fee ratio tests | n/a |
| 2533892f6 | cubic VbHz and the Gatekeeper observation: #income ran the whole compute | "reading the income alone computes none of the returns" (mocha: compute, ReturnScope.resolve_all and Xirr.new never) | failed with "unexpected invocation: Portfolio::Performance#compute()"; reverting to rate_missing? caught; income_metrics(false) caught (:227, and the existing :191) |
| 342c323c2 | Gatekeeper MODERATE: Date in the cached hash | "the cached payload's months survive a JSON round trip unchanged" | failed at :193 (Expected ["2026-03-01"], Actual [Sun, 01 Mar 2026]); a Date month caught (:193) |
| 78686785d | cubic VbIF (owner decision 5: accept the behaviour, fix the comment) and VbIL (comment arithmetic) | comment-only | n/a |
- cubic VbIA (no FX version in the income cache key) is declined, with evidence in the thread. The key follows the app-wide
Family#build_cache_keyconvention that the returns beside it, the net worth series, account totals and sparklines all share. monthis now an ISO 8601 string (Date.iso8601parses it). Nothing in this stack reads it from the hash. The income cache entry first appears in this slice, so no warm entry holds the old shape and the version stays v8.income_test.rb+performance_test.rb: all pass. All portfolio model tests on this head: 186 runs, 0 failures.
Precision follow-up (2026-10-13). up/portfolio-performance is merged down again, as 0827b17f6 (--no-ff). It brings in #376's fix that rounds each chained return level to 20 significant digits (dba9fcb3b, 274f314f9). The only conflict was two private methods added next to each other in performance.rb; both are kept. Full suite at 0827b17f6: 10,766 runs, 45,953 assertions, 0 failures, 0 errors, 33 skips.