Leave a clamped position's basis unknown, and read lot costs back
Part of #4449.
Replaces #4499, closed so the stack can be a native GitHub stack (heads in this repo). Its review history, including jjmata's threads and their answers, stays there.
What: a clamped position (the provider holds fewer units than the imported trades bought) now has an unknown basis, in the reverse calculator and in Holding#calculate_avg_cost. The average-cost fallback also reads a user-set lot cost (slice 4's editor), so a linked position with a costed transfer-in resolves. Holding.preload_avg_costs follows both rules.
Why: a clamped position applied the trades' average to units the provider says aren't there (10 bought at $100 and $150 against 3 held read $125), and a linked account's provider row refused any position with a transfer-in, even one the user had costed.
Start reading: Holding#calculate_avg_cost and Holding.preload_avg_costs in app/models/holding.rb.
Key tests (test/models/holding_test.rb, test/models/holding_lot_test.rb):
- "the avg-cost fallback answers nil for a clamped position"
- "the avg-cost fallback counts a user-set lot cost, so a linked position resolves"
- "the avg-cost fallback leaves a costed transfer unknown across a later split"
- "the batch follows the holding's own lot rules"
Behaviour change: a clamped position's average cost shows as unknown rather than the trades' average; a linked position whose transfer-in lot the user costed now shows a figure. No UI or schema change, so no screenshots.
Stack: 3 of 5 in the lot stack (slice 5 of #4449), on #4901. GitHub shows only this slice's diff. Next: #4903. Merge order: #4900 → #4901 → #4902 → #4903 → #4904 (slice 2, #4899, is merged).
Size: +66 / −13 outside tests.
Fork pre-flight: jaysbeekay/sure#603 (same commits, on the fork's copy of this branch). It ports the model part of the merged fork PR jaysbeekay/sure#583.
Full description and evidence
Problem
- A clamped position still reports a figure. When the provider holds fewer units than the imported trades bought, slice 2 records a clamp, but the figure is unchanged: the trades' average is applied to units the provider says aren't there. On the test fixture, 10 bought at $100 and $150 against 3 held still reads $125.
- A linked account's provider row ignores lot costs. Where the provider gives no cost,
Holding#avg_costfalls back tocalculate_avg_cost, which refuses any position with a transfer-in, even after the user has costed that transfer-in's lot in slice 4's editor.
What this changes
- The reverse calculator reports a clamped position's basis as unknown on every date, and marks it unknown, so the materializer clears a figure stored on an earlier run.
Holding#calculate_avg_cost:- it answers nil for a clamped position;
- it counts a trade whose lot the user has costed at that cost, a transfer-in included (the cost is already in the account's currency);
- it stays within its existing two trades queries.
Not in this slice: unclamping. The next reverse sync already unclamps a position the provider brings back above its trades (slice 2), and the figure returns with it.
No UI change, so no screenshots.
Blast radius
Who else hits these lines, and what do they pass?
- The fallback is reached from
Holding#avg_cost, for rows with no stored basis. - A clamped position's sales now read an unknown basis, from the snapshot and from fifo disposals alike. A clamped position is out of partition, so
HoldingLotDisposal.readable_by_salealready falls back to the snapshot.
Siblings:
clampedis written by the reverse calculator and stored by the materializer. The fallback is its only other reader.- The partition guard is deliberately not applied to the clamp: a clamped position is out of partition by definition, and the read only ever makes a figure unknown.
Tests
test/models/holding_lot_test.rb:
provider quantity below trade-derived quantity makes the position unknown: nil, where the trades give $125; the control keeps $102.50.an unclamped position that the provider drops below its trades is clamped, extended: $102.50 stored, then cleared once the provider drops below.the avg-cost fallback answers nil for a clamped position: the control is unchanged.the avg-cost fallback counts a user-set lot cost, so a linked position resolves: the provider row is nil, then $15.20 with the transfer-in costed, then $13.20 with the buy costed.
Changed tests:
- Slice 2's clamp test no longer pins the $125 figure, which this slice changes by design.
- Two
ReverseCalculatorTestcases bought 15 against a provider holding fixed at 10, so they were clamped positions without meaning to be. They now passheld: 15.
Fail first: on slice 4's head, the new tests fail with $125 where nil is expected, and nil where $15.20 is expected. Mutations: removing the calculator's unknown report, the unknown mark, the fallback's clamp check, or its lot-cost join each fail one of them.
Validation
- Full suite at
ec8a1c063:13524 runs, 62238 assertions, 0 failures, 0 errors, 36 skips. bin/rubocopandbin/brakeman(0 warnings) are clean.- No migration, ERB or API change.
AGENTS.md
- Code placement: model logic only.
- Comments: they describe current behaviour.
- Commits: subjects are ≤72 characters, with no fork references.
Summary by CodeRabbit
- Bug Fixes
- Average-cost calculations now include manually entered costs for eligible lots, including lots associated with internal movements.
- Costs are not applied to lots when a later security split makes them inapplicable.
- Holdings with clamped quantities no longer display a calculated cost basis or average-cost fallback.
- Cost basis is unavailable when provider-reported quantities are below quantities derived from trades.
- Batched average-cost results now match individual holding calculations.