Break a period's change in value into its drivers
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#4213 (pull/4213), opened 2026-10-14 from this same branch, so it moves with this head (now de2be1a7f; the upstream page was not re-read from this session).
Stacked on #444 (up/portfolio-journal-flows-v2, the journal flows pre-flight, upstream we-promise/sure#4198), which is stacked on #443 (up/portfolio-daily-returns-v2, upstream we-promise/sure#4197). The base here is mirror/upstream-main, so until those merge, GitHub shows their commits here as well.
Held for #463, in draft. The owner decided on 2026-10-16 (comment 6207727507) that this stack does not merge until #463's trade-fee fix lands, and moved it back to draft on 2026-10-23.
Supersedes #374 as the upstream pre-flight for Portfolio::Drivers. #374 sits on the old up/portfolio-journal-flows, which sits on up/pending-sql-parity. Upstream has since squash-merged that as we-promise/sure#3557 (33bc80295), and the old branch is over 120 commits behind. #374 stays open until the owner decides.
- Base:
mirror/upstream-mainatbf8d6cc39. - Head:
up/portfolio-drivers-v2atde2be1a7f.
Updated 2026-10-22: the head moved from f283f3587 to de2be1a7f, a merge of #444 at 260966d58. That brings #443's SyncCache FX window and cut-off-aware classifier, #444's journal-out valuation and capped carry, and mirror bf8d6cc39. This PR's own files are unchanged.
- The answer to adversarial review of 2026-10-22 and nightly 6328555243 is in comment 6349444711. Two nits are declined there with evidence: the zero-decimal tolerance and the constant's read order.
f283f3587(2026-10-18, before this round) merged #444'sb0d52d41b.- Evidence. Portfolio tests: 259 runs, 0 failures. Full suite at the top of the stack (
145e36d15): 13,138 runs, 1 failure, 0 errors, 36 skips. The one failure isProvider::OpenaiTest's Ruby 3.4Hash#inspectassertion; the local run used Ruby 3.3.6. CI onde2be1a7fis green.
Updated 2026-10-15: the head moved from cc334ffab to f9b99fd45, a clean merge of #444 at 1c42bcef9. It brings the fixes for the three CodeRabbit threads on we-promise/sure#4213 (PRRT_kwDOPRMXr86sdtDf fixed on #444; PRRT_kwDOPRMXr86sdtD0 and PRRT_kwDOPRMXr86sdtD9 on #443) and jjmata's two points on we-promise/sure#4197. No change to this PR's own files. Portfolio model and investment statement tests on f9b99fd45: 176 runs, 0 failures. The full suite ran on #454 at 4d6909c07, which contains this head: 12,001 runs, 0 failures, 0 errors.
Updated 2026-10-14: the head moved from 6c247bb81 to 6525686e0, a merge of up/portfolio-journal-flows-v2 at 152bc767e. It brings in the post-binding splice of the classifier SQL and journal_predicate in DailyReturns (#443 0fe5578bb, #444 152bc767e; full review 6168438106 on #443). This slice's own files are unchanged, and the portfolio model tests pass (122 runs, 0 failures). The verification below is from 6c247bb81.
- Stacked above it: performance, #452 (
up/portfolio-performance-v2), then income, #453, and income by security, #454.
Updated 2026-10-14 (2), stack fix pass: the head moved from b687ae7cd to cc334ffab.
7fdbad42cmerges journal-flows-v2 atb2b6981e8. It brings #443's one-row-per-entry transfer join (the nightly 6172427317 blocker), the flow FX and excluded-entry changes, and #444's journal carry.cc334ffabdocuments onDrivers#feesthat a commission folded into a trade's entry amount lands inmarket, notfees. This is a known limit and an owner decision on Codex we-promise/sure#4213; there is no behaviour change.- Evidence. Portfolio model and investment statement tests: 169 runs, 0 failures. Rubocop is clean and 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 #374. Every patch is identical to its original (same git patch-id), with no conflicts. drivers.rb and its test were byte-identical to #374's head 8c11ccc39 (as rebuilt; later changes are in the updates above). #374's two --no-ff merges of the journal-flows branch are not needed: this branch is cut from the finished journal-flows-v2.
| Commit | #374 original | Subject |
|---|---|---|
b18a7f2b4 | 9461180ac | Break a period's change in value into its drivers |
a6cb0091c | c3adfda9e | Pin the reconcile tolerance at one minor unit |
6c247bb81 | 7598762ac | Say when the drivers cover a suppressed day |
cc334ffab | (new) | Document that an embedded trade commission reads as market |
No commit message carries a fork reference.
Summary
Adds Portfolio::Drivers, which says where a period's change in value came from: net external flows, composition (accounts entering or leaving), income, fees, market movement, revaluations and the currency effect, with the residual reported as unexplained.
What changed
app/models/portfolio/drivers.rb (new, 189 lines): sums the daily rows' components.
external_net + composition + income - fees + market + revaluations + fx_effect == change.fx_effectis measured per day byDailyReturns, not taken as the residual, soreconciles?can return false andunexplainedis a real figure.revaluationsis reported apart frommarket, so a manually valued account does not show a flat zero market move.RECONCILE_TOLERANCEis one minor unit. A known limitation for zero-decimal currencies is noted in the code.complete?,suppressed_datesandto_h[:suppressed_days]say when a day in the period was suppressed.reconciles?speaks only to the rows as recorded and is read together withcomplete?.
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? No caller on this branch: git grep -n "Portfolio::Drivers" app lib finds the new file and a comment in daily_returns.rb. Portfolio::Performance, the next slice, reads to_h.
What else reads what I just changed the reading of? Nothing outside the new file changes. It reads only daily_returns.rows.
Verification (on 6c247bb81; later rounds are in the updates above)
test/models/portfolio/drivers_test.rb: 10 runs, 45 assertions, 0 failures. All portfolio model tests: 120 runs, 527 assertions, 0 failures.- Full suite: 11,844 runs, 54,579 assertions, 0 failures, 0 errors, 34 skips. Run locally on PostgreSQL 16.
- Lint and security:
bin/rubocop -f githubon the changed files reports no offenses, andbin/brakeman --no-pager -qreports 0 warnings. No ERB or JS changed. - Mutations re-run on this base, each against
drivers_test.rband restored after. All are caught:- fees added instead of subtracted in
unexplained: 2 failures; <in place of<=inreconciles?: 1;complete?always true: 1;suppressed_daysreported as 0: 1.
- fees added instead of subtracted in
- Carried from #374. The code is unchanged apart from
cc334ffab's comment, so its full mutation list and the fix-round table (light review 6065791803, the suppressed-day contract) still apply.
Review history carried from #374
All of #374's review items are answered there. The nightly review 6157134591 (2026-10-13, MEDIUM: a presenter must show complete? / suppressed_days, and merge in stack order) repeats 6120597962, which was answered in 6140345615: the flag is exposed by this model and the presenter obligation belongs to the UI drop.