Break a period's change in value into its drivers

JA
7d ago
+455-0

Part of #121 (IP2, the performance engine, under epic #118). Part of we-promise/sure#4078 and we-promise/sure#3494.

Fork pre-flight for we-promise/sure#5091 (same commits), which replaces we-promise/sure#4213.

Replaces jaysbeekay/sure#450 (same changes, rebased and native-stacked).

Stacked on jaysbeekay/sure#709 in a fork native stack.

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.

Light-review fixes (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 as unexplained.
  • Currency effect. fx_effect is measured per day by DailyReturns, not taken as the residual. So reconciles? can return false, and unexplained is 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_TOLERANCE is 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_dates and to_h[:suppressed_days] say when a day in the period was suppressed. A day DailyReturns suppresses (an unvaluable journal, a non-positive denominator) still reconciles, because its value stays in market. So reconciles? speaks only to the rows as recorded, and is documented to be read together with complete?. 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 we-promise/sure#4198 at b2b6981e8, which carries we-promise/sure#4197's fixes (one row per entry, flow FX, excluded entries) and we-promise/sure#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 we-promise/sure#4229. There is no behaviour change.

Updated 2026-10-15. f9b99fd45 merges we-promise/sure#4198 at 1c42bcef9, which closes the three CodeRabbit threads raised here: the journal carry (fixed on we-promise/sure#4198), and the excluded counterpart and transfer ordering (fixed on we-promise/sure#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 6525686e0 only 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 tip 3f622bb8f, 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 (we-promise/sure#4218 at 4d6909c07, which contains this head) gave 12,001 runs, 0 failures, 0 errors.
  • On db414ee31 (2026-10-23): merged the latest journal-flows fixes from we-promise/sure#4198 (77e3ea386), then current main (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 github on the changed files reports no offenses, and bin/brakeman --no-pager -q reports 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:

MutationTests that catch it
fees added instead of subtracted2
composition left out of unexplained1
fx_effect taken as the residual1
revaluations folded into market2
reconciles? always true1
value_open from the last row2
an empty scope raising1
tolerance 0, 0.02, or < for <=1 each
complete? always true1
suppressed_days reported as 01

Re-run on this branch, cut from current main:

  • fees added instead of subtracted: 2 failures;
  • < for <= in reconciles?: 1;
  • complete? always true: 1;
  • suppressed_days as 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.