Read the open lots in a FIFO account's avg-cost fallback
Part of jaysbeekay/sure#551. This is PR 4b of the stack (PR 3 #582 ← PR 3b #583 ← PR 4 #584 ← PR 4b ← 5; owner decision 18). It is stacked on #584, so its base is feat/551-lot-basis-pr4-named-lot and the diff shows only this slice. It is retargeted down the stack as the lower slices merge.
Problem
Where a holding has no stored cost, Holding#avg_cost falls back to the trades. That is typically a linked account's current row, when the provider gives no cost. The fallback pools every purchase, which gives the average method's figure whatever the account's method. This dates from PR 2 and was found while testing #584.
On the #551 Example, the current row of a linked FIFO account reads:
- $15.20 unnamed, where FIFO's figure is $17.33;
- $15.20 again with the $20 lot named, where it is $12.
The rows the calculators date are right. Only the fallback is wrong.
What this changes
In a FIFO account, both fallback paths now answer the mean of the position's open derived lots, as Holding::LotBook#average_cost does. The two paths are Holding#calculate_avg_cost (Ruby) and Holding.preload_avg_costs (batched SQL).
- The opening lot stays out, as it does in the book.
- An open lot with no cost makes it unknown.
- The stored lots are the position as it stands, so they answer only for the position's latest row, and only while they add up to it (
HoldingLot.in_partition). Any other row is unknown rather than the pooled figure, which is not FIFO's. - A transfer-in that FIFO has sold off no longer blanks the figure ($12, where the pooled path said nil).
- The average path is unchanged.
Query cost. The preload learns each holding's method from the accounts join it already makes, so a page with no FIFO account reads nothing extra (the data_quality_issues budget of 4 holds). FIFO holdings add four queries for all of them together, and a test holds that constant from one holding to three.
Screenshots. None: no view or copy changes, only the figure the existing views read.
Blast radius
Who else hits these lines, and what do they pass?
- The batched path:
InvestmentStatement#holdings_with_avg_costs(holdings, unrealized gains, data quality) andPortfolio::RealizedGainscallHolding.preload_avg_costs. - The per-holding path: single holdings reach
Holding#calculate_avg_costthrough#avg_cost. - Both change together. Both now answer the same for a FIFO holding, and the new tests assert both paths side by side.
What else reads what changed (siblings):
- The calculators already write FIFO's figure on the rows they date, so they are unchanged.
- The parity test in
InvestmentStatementTeststill holds.
Tests
test/models/holding_lot_disposal_test.rb; every figure is asserted for both paths.
a linked fifo account's current row falls back to the open lots, in both paths[average $15.20 unchanged; FIFO $17.33, then $12 with the $20 lot named].a fifo row the lots cannot describe is unknown, not the pooled average[nil for an earlier row and for lots out of partition, where the pool gives $15.20].a sold-off transfer-in no longer blanks a fifo fallback[average nil, FIFO $12].an open transfer-in lot with no cost leaves the fifo fallback unknown(guard).units held before the first trade stay out of the fifo fallback, as they stay out of the book[$150, equal to the calculators' dated figure] (guard).a clamped fifo position stays unknown in both paths(guard).the fifo fallback preload reads every holding in the same number of queries.
Evidence
Fail first. On #584's head (f0535792c), the first three tests fail for the expected reasons:
- $15.20 where $17.33 is expected;
- $15.20 where nil is expected;
- nil where $12 is expected.
The average control already passes there. The guard tests pass on the old code too, so the mutations below are what prove them.
Mutations (8; 7 caught, 1 equivalent):
| # | Mutation | Caught by |
|---|---|---|
| F1 | the Ruby path keeps pooling | current-row, cannot-describe and sold-off tests |
| F2 | the preload keeps pooling | same three |
| F3 | any row reads the lots | cannot-describe test |
| F4 | no partition check | cannot-describe test |
| F5 | an uncosted lot not checked | uncosted-lot test |
| F6 | the opening lot counted | pre-history test |
| F7 | the FIFO flag never set in the SQL | current-row, cannot-describe and sold-off tests |
| F8 | the preload reads lots before the clamp | equivalent: a clamp means the trades' lots exceed the holding, so a clamped position is never in partition and the lot read already answers nil. The clamped-FIFO test pins that. |
Full suite at 72f74a4ea: 14417 runs, 66934 assertions, 0 failures, 0 errors, 38 skips. bin/rubocop and bin/brakeman (0 warnings) are clean.
AGENTS.md
I checked the change against the current we-promise/sure AGENTS.md:
- Code placement: models only.
- Performance: no N+1, with batching held by a test.
- Comments: intent only.
- Migrations, API, UI: none.
- Commit: the subject is ≤72 characters.