Count the anchor day's flows in a linked account's contributions
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::ChartSeriesBuildernet_contributions_by_datestarts from the first row'svalue_openon both paths. The explicit-anchor path used to open atfirst.value_closeand add onlyrest's flows.- New keyword
anchor_before_activity:, which is false by default and does nothing without ananchor_date. When true, the anchor date's point is measured before that day's flows (itsvalue_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_rowsno 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 forvalue_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.
| Test | Old code gave | Expected |
|---|---|---|
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 understated | false | true |
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 understated | false | true (and false for a flow before the anchor) |
builder an anchor point before the day's activity opens at that day's opening value | ArgumentError (keyword absent) | [10000, 15000, 13000] |
builder the before-activity opening needs an anchor date | ArgumentError (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, withanchor_datenil.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 --)
| # | Mutation | Caught by |
|---|---|---|
| M1 | anchor day opens at its close (value_close minus the day's flows) | 8 tests: all daily, coarse and builder anchor tests |
| M2 | understated check drops the anchor row again (.drop(anchor_date ? 1 : 0)) | both anchor-day understated tests |
| M3 | value_point_before_activity? always false | both coarse tests |
| M4 | value_point_before_activity? always true | the four daily tests |
| M5 | before-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. |
| M6 | drop the linked? && balance_type == :investment guard on the anchor | an unlinked account with provider entries keeps the inception-anchored line |
| M7 | builder ignores anchor_before_activity | both coarse tests and the builder before-activity test |
| M8 | value_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 |
| M9 | value_point_before_activity? inverts != to == | the same unit test, plus the four daily tests |
| M10 | value_point_before_activity? drops return false unless point | the same unit test only |
| M11 | value_point_before_activity? true whenever a point exists | the 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 andtest/components/UI/account/chart_test.rball 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):
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], andcontributions.values[1].trend.previous1_500 becomes 1_000.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].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 teston046b3e26d:12337 runs, 53075 assertions, 0 failures, 0 errors, 35 skipsbin/rails teston57a29b03a:12338 runs, 53080 assertions, 0 failures, 0 errors, 35 skipsbin/rubocopon the four changed files: no offensesbin/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: onlychart_series_builder.rb. Both readers ofnet_contribution_rowsare changed together, so the line and the understated note read the same days.counted_contribution_rowshas no remaining uses.grep -rn "net_contributions_series\|net_contributions_understated?" app:account/chartable.rbandUI/account/chart.rbonly, as above.value_point_before_activity?readsbuilder.balance_series, the builder's memoized query, so it adds no query. The value line's anchor point comes fromLinkedInvestmentSeriesNormalizer. That method's other readers areAccount::Chartable#balance_series(every view),AccountableSparklinesController(aggregate_account_ids),Account::Anchorable#history_start_dateandInvestmentStatement(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.