Break a period's change in value into its drivers
Updated 2026-11-20 (head e273b85e2). New test "a second journal in is an external flow, not market, when one journal out cannot explain it": with the old journal matching the second arrival landed in the market driver (Expected external_net 500, Actual 0) while the drivers still reconciled.
Part of #4078 (part 3, returns engine)
Part of #3494
Replaces #4213 (same changes, rebased and native-stacked; its review history stays there).
What: adds Portfolio::Drivers, which sums DailyReturns rows into a period's change in value: external_net + composition + income - fees + market + revaluations + fx_effect == change, with any residual reported as unexplained. The currency effect is measured per day, not taken as the residual, so reconciles? can be false. complete? and suppressed_dates say when a day was suppressed.
Why: without it a return figure has no explanation beside it, and a gap in the data reads as market movement.
Start reading: Portfolio::Drivers#to_h, #reconciles? and #unexplained in app/models/portfolio/drivers.rb.
Key tests (test/models/portfolio/drivers_test.rb):
- "drivers reconcile to the period change for every account shape"
- "reconciles is falsifiable and reports an unexplained move"
- "a valuation tracked account reports its move as revaluations not market"
- "fx effect carries a rate only move and market stays zero"
- "a suppressed journal day still reconciles, so the drivers say a day was suppressed"
Behaviour change: none; the performance PR (#5092) is the first reader. Known limit, documented on Drivers#fees: a commission folded into a trade's amount lands in market (#4229). No migration.
Stack: 5 of 11 in the portfolio returns stack, on #5090. GitHub shows only this slice's diff. Next: #5092 (performance). Merge order: #5129 → #5088 → #5089 → #5090 → #5091 → #5092 → #5093 → #5094 → #5285 → #5296 → #5297.
Size: +204 outside tests, in one new file.
Fork pre-flight: jaysbeekay/sure#710 (same commits, against a mirror of upstream main). Earlier evidence: jaysbeekay/sure#450.
Full description and evidence
Rebased 2026-11-15 onto main at dbeebf14c. Its 4 own commits replayed onto the slice below unchanged. test/models/portfolio and test/models/balance at this slice: 311 runs, 0 failures. Full suite at the top of the stack: 16,441 runs, 0 failures (8 ReleaseNotesWorkflowTest errors: no node in the test image). Rubocop and Brakeman clean.
Review fix (2026-11-15, 831ed8e94). A comment on value_open says why the first and last rows open and close the period: DailyReturns builds each row's opening from the row before it, so the rows are oldest first by construction. Reversing their order fails 8 drivers and 68 daily-returns tests, so no further assertion was added.
Problem
Portfolio::DailyReturns gives each day's components (flows, income, fees, market, revaluations, currency effect), but nothing says where a whole period's change in value came from, or whether those components account for all of it. Without that, a return figure has no explanation beside it, and a gap in the data reads as market movement.
Change
app/models/portfolio/drivers.rb (new, 201 lines at cc334ffab; 189 before that commit's 12 comment lines). It sums the daily rows' components:
- Identity.
external_net + composition + income - fees + market + revaluations + fx_effect == change, with any residual reported asunexplained. - Currency effect.
fx_effectis measured per day byDailyReturns, not taken as the residual. Soreconciles?can return false, andunexplainedis a real figure. - Revaluations. These are reported apart from
market. On a valuation day the whole move lands in the balance adjustments, so a manually valued account would otherwise show a flat zero market move. - Tolerance.
RECONCILE_TOLERANCEis one minor unit, because a multi-currency family accumulates sub-cent dust from converting entries and balance rows by different paths. A known limitation for zero-decimal currencies is noted in the code; no follow-up issue is filed yet. - Suppressed days.
complete?,suppressed_datesandto_h[:suppressed_days]say when a day in the period was suppressed. A dayDailyReturnssuppresses (an unvaluable journal, a non-positive denominator) still reconciles, because its value stays inmarket. Soreconciles?speaks only to the rows as recorded, and is documented to be read together withcomplete?. Anything presenting the drivers should show that flag beside them.
Nothing calls this class yet; the performance PR reads to_h.
Updated 2026-10-14. 7fdbad42c merges #4198 at b2b6981e8, which carries #4197's fixes (one row per entry, flow FX, excluded entries) and #4198's journal carry. cc334ffab documents a known limit on Drivers#fees: a commission folded into a buy's or sell's entry amount lands in market, not fees. Writers disagree on where the fee goes, so moving it needs a common contract first; that follow-up is #4229. There is no behaviour change.
Updated 2026-10-15. f9b99fd45 merges #4198 at 1c42bcef9, which closes the three CodeRabbit threads raised here: the journal carry (fixed on #4198), and the excluded counterpart and transfer ordering (fixed on #4197). No change to this PR's own files.
Validation
On 6c247bb81, stacked on d28a35d4a and cut from main at 3e35aaa07:
- Full suite (
bin/rails test): 11,844 runs, 54,579 assertions, 0 failures, 0 errors, 34 skips. Run locally on PostgreSQL 16. - 2026-10-14: the head moved to
6525686e0only by merging the bind fix down the stack, and this PR's own files are unchanged. The portfolio model tests pass (122 runs, 0 failures) and CI is green; the stack tip3f622bb8f, which contains this head, ran the full suite: 11,978 runs, 0 failures, 0 errors, 34 skips. - After the review fixes, on
cc334ffab(2026-10-14): portfolio model tests 169 runs, 0 failures. Brakeman reports 0 warnings. - On
f9b99fd45(2026-10-15): portfolio model and investment statement tests 176 runs, 0 failures. The full suite on the stack's top (#4218 at4d6909c07, which contains this head) gave 12,001 runs, 0 failures, 0 errors. - On
db414ee31(2026-10-23): merged the latest journal-flows fixes from #4198 (77e3ea386), then currentmain(db414ee31), with no conflicts and none of this PR's files changed by either. Full suite: 13,271 runs, 0 failures, 0 errors, 36 skips. - Lint and security:
bin/rubocop -f githubon the changed files reports no offenses, andbin/brakeman --no-pager -qreports 0 warnings. No ERB, JS or system-test surface.
Tests
test/models/portfolio/drivers_test.rb, on 6c247bb81: 10 runs, 45 assertions, 0 failures.
Fail-first. The suppressed-day test failed on the missing complete? while every assertion above it passed.
Mutations. Each was run against this file's tests on the fork pre-flight, then restored. All were caught:
| Mutation | Tests that catch it |
|---|---|
| fees added instead of subtracted | 2 |
composition left out of unexplained | 1 |
fx_effect taken as the residual | 1 |
| revaluations folded into market | 2 |
reconciles? always true | 1 |
value_open from the last row | 2 |
| an empty scope raising | 1 |
tolerance 0, 0.02, or < for <= | 1 each |
complete? always true | 1 |
suppressed_days reported as 0 | 1 |
Re-run on this branch, cut from current main:
- fees added instead of subtracted: 2 failures;
<for<=inreconciles?: 1;complete?always true: 1;suppressed_daysas 0: 1.
The code was byte-identical to the fork pre-flight it supersedes when rebuilt. cc334ffab (12 comment lines in drivers.rb, no behaviour change) was added afterwards.
Migration notes
None. No schema change.
Summary by CodeRabbit
- New Features
- Added portfolio performance breakdowns showing opening and closing values, overall change, and contributions from external flows, income, fees, market movements, revaluations, portfolio composition, and currency effects.
- Added an unexplained-change amount to show any residual not accounted for by the listed components.
- Added indicators showing whether the breakdown reconciles and whether the period is complete, along with excluded dates and their count.
- Reconciliation checks accept differences of up to one cent.