Flag only deposits and withdrawals as missing from contributions

JA
7d ago
+324-29

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:

  1. 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.
  2. It fired for a foreign dividend or fee with no rate, because flow_rate_missing covers income and fee flows.
  3. A foreign position journalled in or out was valued off by a day's FX move. journal_fx, journal_prior_fx and carry_fx converted in one step at date - 1, while Balance::SyncCache takes two.
  4. 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):

  • bb8471e28 Flag only deposits and withdrawals as missing from contributions. DailyReturns::Row gains contribution_rate_missing and contribution_journal_unpriced: the same conditions as rate_missing and unvalued_journal, narrowed to external_inflow/external_outflow. On a carry day they also take the carry's missing rate, and the part of carry_incomplete that is the external journals' own shortfall (a journal with no quantity, which is always external, or a netted arrival). The shared conditions move into entry_rate_missing_sql, journal_rate_missing_sql and journal_unpriced_sql, so each flag and its twin narrow one expression. flow_rate_missing and journal_unpriced keep their meaning.
  • 356bc1060 Convert a journal through the account's currency, as the balance does. Each of the three laterals now uses entry_rate_lookup into the account's currency at the holding row's date, then the family currency at the previous day's rate. These are the two steps entry_fx → fx takes for an ordinary flow.
  • 0ee82517d Treat a pending transfer counterpart as absent. sql_joins joins the counterpart's transaction, transfer_resolution_sql requires Transaction.not_pending_sql on it, and the Ruby transfer_counterpart returns nil for a pending? counterpart.
  • 9f2ad793f Bump 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_missing and journal_unpriced produce the same values. Suppression and rate_missing? are unchanged, and the whole daily-returns file passes.
  • The FX laterals are read by journal_value_sql, carried_journals_by_date and the rate-missing and unpriced conditions. A rate missing at either step is NULL and still flags (tested). income_by_security does not read them.
  • FlowClassifier readers: DailyReturns, plus Drivers, Performance and income attribution through it. Income attribution keys on 'income' only. ReturnScope builds 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 raised NoMethodError (no such column); the carry ones read Expected false to be truthy. The FX tests read the one-step figures (550, -550). The rate-flag test read Expected false to be truthy. The pending test read Expected: :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/portfolio and test/models/balance: 445 runs, 0 failures. Full suite on beastpc: 17,316 runs, 0 failures, 8 errors (ReleaseNotesWorkflowTest: no node in 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 in bb8471e28.