Flag only deposits and withdrawals as missing from contributions
Split on 2026-11-20. we-promise/sure#5285 now carries only the contribution flags, plus two review fixes (a netted carry its zero-priced row shows to be short; an account arriving or leaving at no rate). The journal FX conversion moved to we-promise/sure#5395 (pre-flight #875) and the pending transfer counterpart to we-promise/sure#5396 (pre-flight #876), both at the top of the stack above we-promise/sure#5297. Same branch, same commits: up/portfolio-returns-deferrals-v3 is at 58717c4ca in both repositories. Older notes below describe the four-finding version.
Commits now on this branch (one file, app/models/portfolio/daily_returns.rb, plus tests): 4489183e5 flag only deposits and withdrawals as missing from contributions; a9e645a26 flag a netted carry its zero-priced row shows to be short; 9d8a68fa8 flag an account arriving or leaving at no rate; f642a7a62 leave contributions complete when a partial outflow is valued; ef1d97987 merge of up/portfolio-income-by-security-v3; 58717c4ca (2026-11-21, tests only, from the Gatekeeper review) covers the withdrawal side of contribution_journal_unpriced (no earlier row; an earlier price with no rate) and an empty foreign account arriving or leaving at no rate. Narrowing journal_flow_unvalued_sql to inflows, dropping its prior-rate clause, or dropping either zero-balance guard now fails a test. Merged up into up/net-contributions-model (5e57ee048, #848) and up/net-contributions-chart (e61815450, #344). Full suite on each: 17,943 / 17,989 / 18,004 runs, 0 failures (8 ReleaseNotesWorkflowTest errors, no node).
Part of jaysbeekay/sure#840
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#5285 (https://github.com/we-promise/sure/pull/5285), 9 of 11 in the returns stack (bottom: we-promise/sure#5129). Same branch, same commits: up/portfolio-returns-deferrals-v3 is pushed to both repositories at the same head.
Stacked on jaysbeekay/sure#713 (up/portfolio-income-by-security-v3, the top of the returns stack), so this PR shows only this slice's diff.
Child pre-flight: jaysbeekay/sure#848, the fork pre-flight for we-promise/sure#5296 (net contributions model), is stacked on this branch.
Problem
Four review findings on the returns engine were deferred to we-promise/sure#4198, which closed unmerged. Its work moved to the returns stack (we-promise/sure#5129 up to we-promise/sure#5094), and all four were still open at its top:
- The net-contributions note fired for an internal journal leg with no price, because the only signal was
unvalued_journal, which covers every journal leg. - It fired for a foreign dividend or fee with no rate, because
flow_rate_missingcovers income and fee flows. - A foreign position journalled in or out was valued off by a day's FX move.
journal_fx,journal_prior_fxandcarry_fxconverted in one step atdate - 1, whileBalance::SyncCachetakes two. - A posted transfer leg linked to a pending one read as internal, although the pending side has moved no balance yet.
Change
One commit per finding, plus the cache version (non-test diff +93 / -39):
bb8471e28Flag only deposits and withdrawals as missing from contributions.DailyReturns::Rowgainscontribution_rate_missingandcontribution_journal_unpriced: the same conditions asrate_missingandunvalued_journal, narrowed toexternal_inflow/external_outflow. On a carry day they also take the carry's missing rate, and the part ofcarry_incompletethat is the external journals' own shortfall (a journal with no quantity, which is always external, or a netted arrival). The shared conditions move intoentry_rate_missing_sql,journal_rate_missing_sqlandjournal_unpriced_sql, so each flag and its twin narrow one expression.flow_rate_missingandjournal_unpricedkeep their meaning.356bc1060Convert a journal through the account's currency, as the balance does. Each of the three laterals now usesentry_rate_lookupinto the account's currency at the holding row's date, then the family currency at the previous day's rate. These are the two stepsentry_fx→fxtakes for an ordinary flow.0ee82517dTreat a pending transfer counterpart as absent.sql_joinsjoins the counterpart's transaction,transfer_resolution_sqlrequiresTransaction.not_pending_sqlon it, and the Rubytransfer_counterpartreturns nil for apending?counterpart.9f2ad793fBump the performance cache version to v12, because cached returns and drivers from v11 carry the old journal FX and transfer resolution.
Not in this PR: the net-contributions reader (Balance::ChartSeriesBuilder#net_contributions_incomplete?) lives in we-promise/sure#5296 (pre-flight jaysbeekay/sure#848), which is stacked on this slice, with we-promise/sure#5297 (the chart) above it. we-promise/sure#5296 will switch that reader to the new flags as a follow-up commit. Nothing in app/ reads the new flags yet.
Behaviour change to note: "a journal priced in a foreign currency converts at the previous day's rate" pinned the one-step figure (550). It is replaced by "...converts at the holding's own date" (600), which is the amount the balance moved.
Blast radius
flow_rate_missingandjournal_unpricedproduce the same values. Suppression andrate_missing?are unchanged, and the whole daily-returns file passes.- The FX laterals are read by
journal_value_sql,carried_journals_by_dateand the rate-missing and unpriced conditions. A rate missing at either step is NULL and still flags (tested).income_by_securitydoes not read them. FlowClassifierreaders:DailyReturns, plusDrivers,Performanceand income attribution through it. Income attribution keys on'income'only.ReturnScopebuilds single-account classifiers whose counterparts are never in scope, so it is unaffected.
Evidence
- Fail-first. Each new test fails on
a9f9832ef. The contribution tests raisedNoMethodError(no such column); the carry ones readExpected false to be truthy. The FX tests read the one-step figures (550, -550). The rate-flag test readExpected false to be truthy. The pending test readExpected: :external_outflow, Actual: :internal. - Mutation. 15 mutations, each one condition broken on its own, and all 15 killed: each contribution restriction, each carry branch, each FX step and date, the SQL and Ruby pending checks, and the cache version.
- Tests.
test/models/portfolioandtest/models/balance: 445 runs, 0 failures. Full suite on beastpc: 17,316 runs, 0 failures, 8 errors (ReleaseNotesWorkflowTest: nonodein the test image). - Checks. Rubocop: no offenses. Brakeman: 0 warnings. Checked against upstream
AGENTS.md: no UI, API or migration changes. - Gatekeeper. Issue review 6971232784 had one gap, a missing positive test for
contribution_journal_unpriced. It was answered in 6971243278 and the test is inbb8471e28.