Value a journalled security as the flow it is
Part of #118 (IP2, the performance engine; its tracking issue #121 is closed)
Part of we-promise/sure#4078 (returns engine, part 3: "a security transferred in from another broker is valued from the position"; under we-promise/sure#3494)
Upstream draft PR: we-promise/sure#4198 (pull/4198), opened 2026-10-14 from this same branch, so it moves with this head (now 260966d58; the upstream page was not re-read from this session).
Stacked on #443 (up/portfolio-daily-returns-v2, the daily returns pre-flight). The base here is mirror/upstream-main, so until #443 merges, GitHub shows its commits here as well.
Held for #463, in draft. The owner decided on 2026-10-16 that this stack does not merge until #463's trade-fee fix lands, and moved it back to draft on 2026-10-23.
Supersedes #373 as the upstream pre-flight for journal flows. #373 sits on the old up/portfolio-daily-returns, which in turn sits on up/pending-sql-parity. Upstream has since squash-merged that as we-promise/sure#3557, and the old branch is 123 commits behind. This branch is cut from #443, which is cut from current upstream main. #373 stays open until the owner decides; fork #374 to #378 are stacked on the old up/portfolio-journal-flows and are untouched.
- Base:
mirror/upstream-mainatbf8d6cc39. - Head:
up/portfolio-journal-flows-v2at260966d58.
Updated 2026-10-22, adversarial review fixes: the head moved from b0d52d41b to 260966d58. This answers adversarial review 6342817844, and the reply is comment 6349443277.
- A merge brings in #443 at
c285e4a49: the SyncCache FX window, the cut-off-aware classifier, and mirrorbf8d6cc39. 15f062a2a, "Value an unpriced journal out from the day before, and cap the carry":- Journal out. A journal that moves a whole position out on a day with no usable price is valued from the last holdings row before its date. It is booked as the negative flow and taken out of
market. If no earlier row exists, or if part of the position is still held, the day is suppressed. - Carry. The inbound carry is capped at the quantity still held on the first priced day, so a position that leaves before it is priced carries nothing.
- Journal out. A journal that moves a whole position out on a day with no usable price is valued from the last holdings row before its date. It is booked as the negative flow and taken out of
260966d58putsRow#unvalued_journalon each row, so a caller can tell a suppression caused by a journal from one caused by the denominator.b0d52d41b(2026-10-18, before this round) merged #443's150964275.- Evidence. The new tests failed first, and each mutation failed its test. Portfolio tests: 259 runs, 0 failures. Full suite at the top of the stack (
145e36d15): 13,138 runs, 1 failure, 0 errors, 36 skips. The one failure isProvider::OpenaiTest's Ruby 3.4Hash#inspectassertion; the local run used Ruby 3.3.6.
Updated 2026-10-15, overnight review fixes: the head moved from b2b6981e8 to 1c42bcef9.
9b88d2db8merges #443 at0f371528f(excluded security-transfer legs, transfer ordering, the pinned widening test, the liability refusal).1c42bcef9answers CodeRabbit PRRT_kwDOPRMXr86sdtDf (raised on we-promise/sure#4213):carried_journals_by_datenow selectsentries.date < :end_date AND first_priced.date >= :start_date, so a zero-price journal dated before the period and first priced inside it is carried.- Evidence. Two new tests failed on
b2b6981e8(first priced on day two, and on day one); a third covers a journal first priced before the period. 2 mutations, both caught. Full suite on1c42bcef9: 11,893 runs, 54,761 assertions, 0 failures, 0 errors, 34 skips. Brakeman 0 warnings.
Updated 2026-10-14: the head moved from d28a35d4a to 152bc767e: 8f22d853f merges #443's bind fix (0fe5578bb) and 152bc767e routes journal_predicate through the same post-binding splice, with its own probe test. Details are in the comment on this PR. The verification below is from d28a35d4a.
- Stacked above it: net contributions, #343 (we-promise/sure#4028) and #344 (we-promise/sure#4029).
Updated 2026-10-14 (2), stack fix pass: the head moved from b7bc1e505 to b2b6981e8.
18343b905merges #443's51c7d69c5: one transfer row per entry (the nightly 6172428284 blocker), two-step flow FX, and the excluded-entry and missing-rate changes. The conflict indaily_returns.rbwas resolved by giving the journalELSEbranch and the flow missing-rate check #443's conversion and itsamount <> 0guard.8cb179e86, "Book an unpriced journal on its first priced day", answers Codex on we-promise/sure#4198. An inbound journal with no price on its date is carried to the first later priced day, booked there as a flow and taken out ofmarket. The unpriced-journal check now covers internal legs too.b2b6981e8adds the control test "a journal priced on its own day is not carried to a later one".- Evidence. Fail-first: 2 tests on
b7bc1e505. 5 mutations were all caught. Portfolio model tests: 124 runs, 0 failures. Brakeman reports 0 warnings. The root cause (per-leg unique indexes ontransfers) is raised separately as jaysbeekay/sure#459 / we-promise/sure#4225.
Commits (this PR's own)
Cherry-picked from the rebased up/net-contributions-model (at 0ca4e5360). The original #373 commits are in brackets. Every patch is identical to its original (same git patch-id). The resulting daily_returns.rb and its test were byte-identical to #373's head 351d120b7 (as rebuilt; later changes are in the updates above).
| Commit | Rebased source | #373 original | Subject |
|---|---|---|---|
60d3c3a03 | bbfe35c70 | 08ce0491b | Value a journalled security as the flow it is |
0d90ae376 | 471ac0a88 | eac0ad5fa | Pin the journal's currency, rate check and label rule |
83b11de2b | fbe69657c | a9b41ff98 | Leave journals out of the entry-currency rate check |
d28a35d4a | 46bf5367d | 351d120b7 | Suppress a journal day when the trade has no quantity |
152bc767e | (new) | (new) | Splice the journal predicate after binding as well |
8cb179e86 | (new) | (new) | Book an unpriced journal on its first priced day |
b2b6981e8 | (new) | (new) | Test that a journal priced on its own day is not carried |
15f062a2a | (new) | (new) | Value an unpriced journal out from the day before, and cap the carry |
260966d58 | (new) | (new) | Say on a row when it was suppressed for an unvalued journal |
#373's no-ff merge of the daily-returns branch, 42d36d7be, is not needed here: this branch is cut from the finished daily-returns-v2.
Summary
A security moved in from a broker outside the scope is written as a Transfer-labelled trade with amount: 0. Valued at its cash amount it moved nothing, so the arriving position landed in the closing value with the denominator unchanged. A position worth half the account read as a 50% day. This values such a journal from the position instead, and takes the same value back out of the market component so the day still reconciles.
What changed
app/models/portfolio/daily_returns.rb:
- What a journal is. A Transfer-labelled trade with no cash amount.
- How it is valued. Its external flow is
qty xthat day's holding price, converted at the previous day's rate. The holding row is chosen withLIMIT 1, preferring the entry's currency. A journal out on a day with no usable price is valued from the previous holdings row, the value the balance carried. - Market. The same value is subtracted from
market, becauseBalance::BaseCalculatorbooks the arriving (or departing) position there. - Suppression. A journal with no usable price, no rate or no quantity suppresses the day and sets
Row#unvalued_journal. It also raisesrate_missing?when the rate is what is missing. - Rate check. A journal is left out of the entry-currency rate check, since it is valued from the holding's currency.
- Known limit: a multi-currency rate gap in
unexplained. When a journalled holding's currency differs from the account's and the rate moved that day,market(valued in the account's currency, converted at the previous day's account-currency rate) andjournal_flow(converted from the holding's currency at the previous day's rate) take different routes. The difference stays inunexplained, where it is visible, rather than being folded into another driver. - Unchanged. A Transfer-labelled trade carrying a cash amount, and Contribution or Withdrawal trades, still flow at their cash amount.
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? Nothing upstream calls Portfolio::DailyReturns yet. On the stack above, Balance::ChartSeriesBuilder (#343) reads its external_flow, rate_missing and suppressed. So a journalled position now counts as money put in, and an unpriced journal raises the net contributions "incomplete" note. Portfolio::Performance (#452) reads Row#unvalued_journal to withhold MWR.
What else reads what I just changed the reading of? holdings.price is also read by Balance::BaseCalculator#market_value_change_on_date, and this PR reads it on purpose so the flow and the market move are one number. The predicate reads Portfolio::FlowClassifier::TRANSFER_LABEL, the constant the classifier keys its journal rule on.
Verification (on d28a35d4a; later rounds are in the updates above)
test/models/portfolio/daily_returns_test.rb: 51 runs, 141 assertions, 0 failures.- Full suite: 11,834 runs, 54,532 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 restored after. All are caught:
- journal valued at its cash amount: 6 failures;
- journal not taken back out of market: 4;
- NULL-quantity clause dropped: 1;
BOOL_OR→BOOL_ANDon the unpriced check: 1.
- Carried from #373. The code was unchanged until
152bc767e,18343b905and8cb179e86. With the daily-returns slice'sdaily_returns.rbalone, 6 of the new tests fail. The full mutation list and the fix-round table (cubic Vczd, code-master review 6105957389) on #373 still apply.