Group a portfolio's income by month, with its fees and fee ratio

Closed
JA
21d ago
+6312-36

Part of #118 (IP2, the performance engine; its tracking issue #121 is closed)
Part of we-promise/sure#4078 (returns engine, part 3; under we-promise/sure#3494)

Upstream draft PR: we-promise/sure#4217 (pull/4217), opened 2026-10-14 from this same branch, so it moves with this head (now 8cbb31662; the upstream page was not re-read from this session).

Stacked on #452 (up/portfolio-performance-v2, which stacks on #450 drivers and #451 return scope). The base here is mirror/upstream-main, so until those merge, GitHub shows their commits here as well.

Held for #463. The owner decided on 2026-10-16 (comment 6207728450) that this stack does not merge until #463's trade-fee fix lands.

Supersedes #377 as the upstream pre-flight for Portfolio::Income. #377 sits on the old up/portfolio-performance and is over 120 commits behind. #377 stays open until the owner decides.

  • Base: mirror/upstream-main at bf8d6cc39.
  • Head: up/portfolio-income-v2 at 8cbb31662.

Updated 2026-10-23, merge-down: the head moved from 5ec913732 to 8cbb31662, a clean merge of #452 at c80ba2311, which carries #450 at db414ee31 (#444's fc400f58f, #443's c285e4a49, upstream main at 2250ba801). No change to this PR's own files. Portfolio tests on 8cbb31662: 249 runs, 0 failures. Full suite on the stack top (#454 at 58d469e77): 13,375 runs, 61,229 assertions, 0 failures, 0 errors, 36 skips, on Ruby 3.4.9.

Updated 2026-10-22, adversarial review round: the head moved from c9704aa6a to 5ec913732. This answers the adversarial review of 2026-10-22 and nightly 6328555509, in comment 6349448531.

  • Merges bring in #452 at dc325b25a: family scoping, the one-day TWR change, MWR withheld for an unvalued journal, and cache v9. Through #452 it also carries the #443/#444 fixes and mirror bf8d6cc39.
  • 21798a362 pins that a fee rebate makes fees and fee_ratio negative.
  • c9704aa6a (2026-10-18, before this round) merged #452's ab71cf6e3.
  • Evidence. Portfolio tests on 5ec913732: 259 runs, 0 failures. Full suite at 145e36d15: 13,138 runs, 1 failure, 0 errors, 36 skips. The one failure is Provider::OpenaiTest's Ruby 3.4 Hash#inspect assertion; the local run used Ruby 3.3.6.

Updated 2026-10-15: the head moved from b786df777 to 52031186f, a clean merge of #452 at fce8b6868 (the overnight review fixes on #443 and #444). No change to this PR's own files. Portfolio model and investment statement tests on 52031186f: 253 runs, 0 failures. Full suite on #454 at 4d6909c07: 12,001 runs, 0 failures, 0 errors.

Updated 2026-10-14: the head moved from 62281704a to d835d5899, with merges only (1638bac03 and d835d5899). They merge up/portfolio-performance-v2 and bring in the classifier-SQL bind fixes from #443, #444 and #451 (full review 6168438106 on #443). This slice's own files are unchanged, and the portfolio model tests pass (198 runs, 0 failures). The verification below is from 62281704a.

  • Stacked above it: income by security, #454 (up/portfolio-income-by-security-v2).

Updated 2026-10-14 (2), stack fix pass: the head moved from 733ed9cd3 to b786df777, a merge of performance-v2 at 88fdbd232.

  • It brings #443's one-row-per-entry transfer join (the nightly 6172425551 blocker), the flow FX and excluded-entry changes, #444's journal carry, and #450's Drivers#fees comment.
  • By merge it also carries #451's 20716ccd1, e7925cfbb (on #452) and b07e2bcee, so ReturnScope differs from #377's copy.
  • Portfolio::Income reads the rows' income, so its total now includes excluded income entries and converts foreign ones as the balance did.
  • This slice's own files are unchanged.
  • Evidence. Portfolio model and investment statement tests: 246 runs, 0 failures. Brakeman reports 0 warnings. The root cause (per-leg unique indexes on transfers) is raised separately as jaysbeekay/sure#459 / we-promise/sure#4225.

Commits (this PR's own)

Cherry-picked from #377.

Commit#377 originalSubject
85e58e9b637a44f9c4Group a portfolio's income by month, with its fees and fee ratio
63474c0f5f5acb4cd2Add the fee fixture back beside the fee ratio tests
c0f8a61a82533892f6Read the income's missing-rate flag from the daily returns
63f73b518342c323c2Emit each income month as an ISO 8601 string
62281704a78686785dCorrect two comments on the income buckets and fee ratio
21798a362(new)Pin that a fee rebate makes fees and the fee ratio negative
  • One conflict, resolved as #377 resolved it. 85e58e9b6 conflicted in performance.rb, because the precision fix's compound and this slice's income_metrics are new private methods added at the same place. Both are kept, income_metrics first, which is the order in #377's merge 0827b17f6. The other four patches are identical to their originals (same git patch-id).
  • Not carried: #377's three --no-ff merges of the performance branch.
  • Files against #377's head e693a2d9a: income.rb, performance.rb, income_test.rb and performance_test.rb were byte-identical (as rebuilt; later changes are in the updates above). The only difference is the 18-line helper test in return_scope_test.rb, which arrives with #451 (see #452).

No commit message carries a fork reference.

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, 126 lines): it regroups the daily rows' income and fees by month and re-derives nothing, 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. A rebate larger than the fees makes them, and the ratio, negative (pinned by a test).
  • fee_ratio is fees over the period's average daily value, and nil when there was no value.
  • Each bucket's month is an ISO 8601 string, so it reads the same from every cache store.
  • Known limit, held for #463. A commission folded into a trade's entry amount is not counted in fees.

app/models/portfolio/performance.rb (+23): #income, a cache entry of its own, computed only when asked. It reads the missing-rate flag from the daily returns, so it never runs the returns' compute. The fee ratio is withheld when a rate is missing; the money figures are not.

Demo data

None needed: model only, no UI.

Migration notes

None. No schema change.

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; 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, and a test asserts that the two agree.

Verification (on 62281704a; later rounds are in the updates above)

  • income_test.rb + performance_test.rb: 51 runs, 160 assertions, 0 failures.
  • Full suite: 11,957 runs, 54,994 assertions, 0 failures, 0 errors, 34 skips. Run locally on PostgreSQL 16.
  • Lint and security: bin/rubocop -f github on the changed files reports no offenses, and bin/brakeman --no-pager -q reports 0 warnings. No ERB or JS changed.
  • Mutations re-run on this base, each restored after. All are caught:
    • fee ratio not withheld on a missing rate: 2 failures;
    • a Date month in the cached hash: 1;
    • fee ratio 0 instead of nil with no value: 1;
    • income reading the #metrics missing-rate flag: 1.
  • Carried from #377. The code is unchanged, so its full mutation list and the fix-round table (cubic VcEb, VbHz, VbIF, VbIL; the Gatekeeper MODERATE on Date) still apply. cubic VbIA stays declined there with evidence.

Review history carried from #377

Every review on #377 is answered there (6075797713, 6140345951) and its four cubic threads are resolved. The nightly review 6157135734 (2026-10-13, INSUFFICIENT EVIDENCE: a target-base conflict and no current workflow run, so resolve the conflict and run the full suite on the resulting head) is what this rebuild does. The conflict is the one described above, and the full suite above ran on the resulting head.