Say which return method an account's data can support
Part of #121
Part of we-promise/sure#4078 (returns engine, part 3; under we-promise/sure#3494)
Upstream draft PR: we-promise/sure#4212 (pull/4212), opened 2026-10-14 from this same branch; same branch, same commit (now b07e2bcee).
Not stacked: this branch sits directly on upstream main. Portfolio::ReturnScope needs only Portfolio::FlowClassifier, which upstream merged in we-promise/sure#3557 (33bc80295). It does not read DailyReturns, Drivers or anything else in the stack. Performance, #452 (up/portfolio-performance-v2), merges this branch in, because it reads the class.
Supersedes #375 as the upstream pre-flight for Portfolio::ReturnScope. #375 sits on up/pending-sql-parity, which upstream has since squash-merged as we-promise/sure#3557, and is over 120 commits behind. #375 stays open until the owner decides.
- Base:
mirror/upstream-mainata3bb5d991, the same commit as upstreammain. - Head:
up/portfolio-return-scope-v2atb07e2bcee.
Updated 2026-10-14: the head moved from 5170b4940 to 603257ae2. ReturnScope#external_transactions? no longer passes the classifier's SQL through sanitize_sql_array; it is now a relation that adds the CASE through Arel.sql (9cd1395af, 603257ae2), with a fail-first probe test. This is a sibling of the finding in full review 6168438106 on #443. The full suite on 603257ae2 had 11,846 runs, 0 failures and 0 errors; details are in the comment on this PR. The verification below is from 5170b4940.
Updated 2026-10-14 (2): the head moved from 603257ae2 to 20716ccd1, "Drop the valuations check that never changes the kind". This answers full review 6170555231: kind returned VALUATION_TRACKED both after elsif valuations? and in the else, so the check never changed the result. Removed: valuations?, the batch :valuations input, and the Valuation rows the batch entry query fetched only for it. That query now reads trades alone. A new test checks, on both the instance and the batch path, that a valuation-only account is valuation tracked and that a trade beside a valuation still makes the account trade tracked. Three mutations were all caught, and only the new test catches the batch query counting a valuation as a trade. On 20716ccd1: return_scope_test.rb had 25 runs, 94 assertions and 0 failures. The full suite had 11,846 runs, 54,647 assertions, 0 failures, 0 errors and 34 skips. Rubocop reported no offenses and Brakeman reported 0 warnings. The reply is comment 6171254772.
Updated 2026-10-14 (3): the head moved from 20716ccd1 to b07e2bcee, "Count only quantity trades as trade tracking". It answers Codex on we-promise/sure#4212. Trade::CreateForm records a dividend, interest or a fee as a zero-quantity trade, so a manually valued account with only such trades resolved as trade tracked. Both trades? and the batch trade_account_ids now join trades on qty <> 0 through one shared constant. Fail-first: "an account whose only trades are dividends, interest or fees is valuation tracked", through new and resolve_all. 3 mutations (instance path, batch path, the condition) were all caught. Full suite on b07e2bcee: 11,847 runs, 54,652 assertions, 0 failures, 0 errors, 34 skips. Rubocop is clean and Brakeman reports 0 warnings. The transfer fan-out raised on #443 does not reach this branch: both of its sql_joins uses sit inside EXISTS.
Commits
Cherry-picked from #375. Every patch is identical to its original (same git patch-id), with no conflicts. return_scope.rb, its test and the test helper were byte-identical to #375's head 9d2d58028 as rebuilt; later changes are in the updates above.
| Commit | #375 original | Subject |
|---|---|---|
2671d6847 | 15dce011a | Say which return method an account's data can support |
17a10ad4f | dcb296e5b | Pin the batch path's transfer history rules |
0d0ccf59e | 3d2392d16 | Refuse balance fixture amounts finer than the stored scale |
e646a2a5e | ab4a52295 | Count balance days only up to each account's cut-off |
5170b4940 | 9d2d58028 | Read tracking history only up to each account's cut-off |
9cd1395af | (new) | Quote tracking-check values instead of re-sanitizing |
603257ae2 | (new) | Ask the tracking check through a relation, not raw SQL |
20716ccd1 | (new) | Drop the valuations check that never changes the kind |
b07e2bcee | (new) | Count only quantity trades as trade tracking |
No commit message carries a fork reference.
Summary
Adds Portfolio::ReturnScope, which decides before any figure is computed what an account's records can support over a period. A manually valued account has no record of what was paid in, so a money-weighted return over its flows would be invented. An account with one day of history has no return at all.
What changed
app/models/portfolio/return_scope.rb (new; 280 lines at 20716ccd1, plus the shared quantity-trade join in b07e2bcee):
trade_tracked: the account records trades or external transfers, so both a time-weighted and a money-weighted return hold.valuation_tracked: balances that no trade or transfer explains, whether valuation entries assert them or nothing does. Its change is a value return, labelled as one (return_label_key).insufficient: fewer than two balance days in the account's own currency.- History before the period counts. Excluded entries count for nothing. Every check reads history only up to the earlier of the period end and the account's cut-off.
resolve_all(accounts:, period:, active_until_dates: {})answers for a set in four queries however many accounts it is given. Each account is its own flow-classifier scope there, so a transfer to a sibling in the same set is still external for it.
test/support/portfolio_returns_test_helper.rb (new): byte-identical to the file the daily returns pre-flight (#443, we-promise/sure#4197) adds, so either can merge first. git diff origin/up/portfolio-daily-returns-v2 origin/up/portfolio-return-scope-v2 -- test/support/portfolio_returns_test_helper.rb is empty.
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::ReturnScope" app lib finds only the new file. Portfolio::Performance (the performance slice) passes Account.where(id: account_ids) and the cut-offs DailyReturns resolved.
What else reads what I just changed the reading of? Nothing outside the new files changes. It reads Portfolio::FlowClassifier with a one-account scope, as the classifier's own tests do.
Verification (on 5170b4940)
test/models/portfolio/return_scope_test.rb: 24 runs, 87 assertions, 0 failures.- Full suite: 11,845 runs, 54,640 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
return_scope_test.rband restored after. All are caught:- one balance day treated as enough: 4 failures;
- instance
balance_daysignoring the cut-off: 2; history_end_dateignoring the cut-off: 3;- batch balance-day cut-off with
<for<=: 4; - the helper's scale check skipped: 1.
- Carried from #375. The code was unchanged until
9cd1395af, so its full mutation list, the equivalent mutants and the fix-round table (cubic VYFF, owner decision 6, the 2026-10-09 cut-off decision) still apply.
Review history carried from #375
Every review on #375 is answered there: the light review 6066273897/6066279327, the code-master review 6105957533 (all ten findings declined with measurements) and the nightly review 6120598852, in 6075797361, 6078207983 and 6140641882. Its cubic thread is resolved.