Count the anchor day's flows in a linked account's contributions

Merged
JA
2mo ago
+316-83

Fixes #382

Summary

For a linked investment account, the Net contributions line counted the anchor day's whole change in value as money put in. The anchor day is the first broker activity, where the value line is trimmed. That change includes the day's market gain, so the gap between Total value and Net contributions read 0 where the account had gained. This happens on coarse intervals today, and on the daily path once upstream we-promise/sure#4009 (dd8f7326f) reaches fork main at the next sync.

This PR follows the owner's decision of 2026-10-08, reading (b). The line starts from DailyReturns' value_open for the anchor day, which is the balance actually held before that day's activity. It then counts the anchor day's flows as well as later ones. From the anchor date on, the gap is the market's.

What changed

  • Balance::ChartSeriesBuilder
    • net_contributions_by_date starts from the first row's value_open on both paths. The explicit-anchor path used to open at first.value_close and add only rest's flows.
    • New keyword anchor_before_activity:, which is false by default and does nothing without an anchor_date. When true, the anchor date's point is measured before that day's flows (its value_open), and the day's flows arrive on the next point.
    • net_contributions_understated? reads every row, including the anchor day's. counted_contribution_rows (explicit_anchor ? rows.drop(1) : rows) is removed. net_contribution_rows no longer returns the explicit flag.
  • Account::Chartable#net_contributions_series
    • The value line's anchor point is no longer substituted into the contributions line.
    • New private value_point_before_activity?: true when the value line's point on the anchor date is not the close the balance query gave for that date. That happens when the normalizer prepended the point (coarse interval) or reset it (we-promise/sure#4009). The line is then measured at the same moment as the value point: the close when the point is the close, the opening otherwise.
    • The comment that becomes false after we-promise/sure#4009 ("They are the same figure unless a coarse interval skipped that date") is rewritten.
  • Review commit 57a29b03a "Test how the chart reads the value line's anchor point": a unit test for value_point_before_activity?, and a comment at the method stating its known limit after we-promise/sure#4009 (see below). No app code changed.
  • Not touched: Balance::LinkedInvestmentSeriesNormalizer (upstream's code), manual accounts, InvestmentStatement#net_contributions (KPI row), and the wording of the understated note.

Accepted cost, per the decision: where the account held a balance before the trim date, and the value line's anchor point is the normalizer's opening (0), the two lines do not open together. In the coarse test the value line opens at 0 and contributions at 1,000. From the next point the gap is the market's (100).

Verification

Fail-first (on fork main code, tests only)

Run on 1c1928ae5 with the new tests and the old app code: 49 runs, 8 failures, 2 errors.

TestOld code gaveExpected
chartable a linked account's net contributions count the anchor day's flows, not its market gain (pre-trim balance, daily)[1600, 1900][1500, 1800], gaps [100, 100]
chartable a linked account with no history before its first activity opens at that day's flows[600, 800][500, 800]
chartable a linked account opened with an anchor on its first activity date counts the anchor as held[1600, 1800][1500, 1800]
chartable a linked account whose history starts at its holdings counts the opening day's flows once[1600, 1900][1500, 1800]
chartable on a coarse interval a linked account's net contributions open before the anchor day's activity (re-expressed from the test that asserted [0, 1_600, 1_900], not deleted)[0, 1600, 1900][1000, 1500, 1800], gaps after the opening [100, 100]
chartable a linked account's unvalued flow on its anchor day makes net contributions understatedfalsetrue
builder an anchor date opens at that day's opening value and counts its flows (renamed from ...closing value and adds only later flows)[15400, 15400, 13400][15000, 15000, 13000]
builder an unvalued flow on the anchor day makes net contributions understatedfalsetrue (and false for a flow before the anchor)
builder an anchor point before the day's activity opens at that day's opening valueArgumentError (keyword absent)[10000, 15000, 13000]
builder the before-activity opening needs an anchor dateArgumentError (keyword absent)unchanged inception-anchored line

Two guard tests pass both before and after the fix:

  • an unlinked account with provider entries keeps the inception-anchored line: manual accounts are unchanged, with anchor_date nil.
  • on a coarse interval a linked account with no prior history opens both lines at zero: the non-explicit coarse path was already right.

The existing manual-account tests pass unchanged: balance_series accepts the net contributions view on the value line's dates and the builder's inception-anchored tests.

Mutations (each applied to the committed file, run, then restored with git checkout --)

#MutationCaught by
M1anchor day opens at its close (value_close minus the day's flows)8 tests: all daily, coarse and builder anchor tests
M2understated check drops the anchor row again (.drop(anchor_date ? 1 : 0))both anchor-day understated tests
M3value_point_before_activity? always falseboth coarse tests
M4value_point_before_activity? always truethe four daily tests
M5before-activity opening ignores anchor_date (applies to the first row)the before-activity opening needs an anchor date. It survived at first, because the fixture's first day had no flow. The test was tightened so that day carries a deposit, and M5 now fails it.
M6drop the linked? && balance_type == :investment guard on the anchoran unlinked account with provider entries keeps the inception-anchored line
M7builder ignores anchor_before_activityboth coarse tests and the builder before-activity test
M8value_point_before_activity? drops close.nil? || (57a29b03a)the value line's anchor point is read as before activity only when the normalizer supplied it (NoMethodError), plus both coarse tests
M9value_point_before_activity? inverts != to ==the same unit test, plus the four daily tests
M10value_point_before_activity? drops return false unless pointthe same unit test only
M11value_point_before_activity? true whenever a point existsthe same unit test, plus the four daily tests

The unit test was added for the light Gatekeeper review (6069483452) after the fix, so its evidence is M8 to M11 rather than a fail-first run. It covers: a flat day's close and an active day's close read false; a point reset below the close, or on a date the query has no row for, reads true; no point reads false.

With upstream we-promise/sure#4009 (dd8f7326f)

Tested in a throwaway detached worktree of this branch's head with dd8f7326f cherry-picked uncommitted. It applied cleanly. The worktree has since been removed.

  • Run as-is: 85 runs, 3 failures. Only the three daily value-line openings fail, because #4009 resets the value point to the pre-activity balance. Contributions, gaps after the opening, the coarse tests, the holdings-inception boundary, the understated tests, the normalizer's own tests and test/components/UI/account/chart_test.rb all pass.
  • With those three expectations restated (listed below): 85 runs, 249 assertions, 0 failures. The #382 reproduction now reads value [0, 1900] and contributions [1000, 1800], a day-4 gap of 100. That is the case the decision pinned.
  • The same restated tests against the unfixed app code with #4009 give 4 failures. These include the #382 reproduction ([0, 1900] instead of [1000, 1800]), the coarse test, the holdings boundary and the anchor-day understated test. So the fix is still needed after the sync.

Expectations the sync PR must restate (all in test/models/account/chartable_test.rb; nothing in the builder tests changes):

  1. a linked account's net contributions count the anchor day's flows, not its market gain: value [1_600, 1_900] becomes [0, 1_900], contributions [1_500, 1_800] become [1_000, 1_800], gaps [100, 100] become [-1_000, 100], and contributions.values[1].trend.previous 1_500 becomes 1_000.
  2. a linked account with no history before its first activity opens at that day's flows: value [600, 900] becomes [0, 900], contributions [500, 800] become [0, 800], gaps [100, 100] become [0, 100].
  3. a linked account opened with an anchor on its first activity date counts the anchor as held: value [1_600, 1_900] becomes [1_000, 1_900], contributions [1_500, 1_800] become [1_000, 1_800], gaps [100, 100] become [0, 100].

The day-4 gap stays 100 in all three. Only the anchor-date point moves, because the value line's anchor point moves from the day's close to before its activity, and the contributions line follows it.

Known limit after #4009 (cubic thread on value_point_before_activity?, now resolved; the restatements and the fix are tracked in jaysbeekay/sure#448). On fork main the method is exact, because close.nil? decides every case. After #4009, a reset first point whose value equals the day's close is read as the close. That happens when an anchor-day flow is offset by the market. Reproduced with dd8f7326f cherry-picked: an opening anchor of 1,000, a 500 deposit and a 500 fall give contributions [1500, 1800] instead of [1000, 1800]. Only the anchor date is off, by that day's flow. A provenance flag is not clean before #4009: #4009's unchanged? returns the original series in exactly this case, and the flag would change the Series::Value contract. The sync PR that brings in we-promise/sure#4009 fixes this, alongside the three restatements above. A comment at the method records the limit.

Suite and checks

  • bin/rails test on 046b3e26d: 12337 runs, 53075 assertions, 0 failures, 0 errors, 35 skips
  • bin/rails test on 57a29b03a: 12338 runs, 53080 assertions, 0 failures, 0 errors, 35 skips
  • bin/rubocop on the four changed files: no offenses
  • bin/brakeman --no-pager -q: 0 warnings
  • No ERB, JS, API or migration changes. Checked against upstream AGENTS.md: model-only, Minitest with fixtures, subject of 64 characters.

Blast radius

Who else hits this line, and what do they pass? net_contributions_by_date and the understated check are reached only through net_contribution_rows. Its two public callers are Balance::ChartSeriesBuilder#net_contributions_series and #net_contributions_understated?. Each has exactly one caller in app/, in Account::Chartable, which passes anchor_date: net_contributions_anchor_date (nil unless the account is a linked investment) and dates: from the trimmed value line. UI::Account::Chart#net_contributions_comparison and #net_contributions_understated? call those. With anchor_date: nil (every manual account), the line is unchanged: it always started from the first row's value_open plus its flows. anchor_before_activity defaults to false and is a no-op without an anchor.

What else reads what I changed the reading of? Sibling grep:

  • grep -rn "net_contribution_rows\|counted_contribution_rows\|net_contributions_by_date" app: only chart_series_builder.rb. Both readers of net_contribution_rows are changed together, so the line and the understated note read the same days. counted_contribution_rows has no remaining uses.
  • grep -rn "net_contributions_series\|net_contributions_understated?" app: account/chartable.rb and UI/account/chart.rb only, as above.
  • value_point_before_activity? reads builder.balance_series, the builder's memoized query, so it adds no query. The value line's anchor point comes from LinkedInvestmentSeriesNormalizer. That method's other readers are Account::Chartable#balance_series (every view), AccountableSparklinesController (aggregate_account_ids), Account::Anchorable#history_start_date and InvestmentStatement (trim_to_supported_history). None of them read net contributions, and none of them change. The normalizer's tests and the chart component tests pass unchanged.

Carry-over

The same commit goes onto up/net-contributions-model (we-promise/sure#4028, fork pre-flight jaysbeekay/sure#343) when that branch is rebased onto an upstream main containing we-promise/sure#4009. The chart branch up/net-contributions-chart (we-promise/sure#4029, jaysbeekay/sure#344) carries the same net_contributions_by_date logic and takes it at the same point. On those branches the three expectations above are written in their #4009 form directly.

Ordering (QA Verification Focus 5): this PR can merge before the weekly sync, because it fixes the coarse path and the daily anchor point on fork main as it is now. The sync PR that brings in we-promise/sure#4009 must then restate the three expectations listed above in the same PR, so fork main never ships the daily-path defect.