Leave a clamped position's basis unknown, and read lot costs back
Part of #4449.
Slice 5 (U5) of the per-lot cost basis series that #4449 proposes. Depends on #4498 (slice 4, the lot cost editor), which depends on the slices before it. This PR targets up/551-u4 (the branch of #4498, pushed to this repo at the same commit as the fork's), so Files changed shows this slice alone. Merge bottom-up: once #4498 is in main, I retarget this PR to main and merge main down. Merged before #4498, it would land in up/551-u4 rather than main.
In short. A position with fewer units than its trades bought (clamped) has an unknown basis, and user-set lot costs are read back into the average-cost fallback. The batched cost (Holding.preload_avg_costs) applies both rules too. Look first: Holding#calculate_avg_cost, Holding.preload_avg_costs. Check: bin/rails test test/models/holding_test.rb ("the batch follows the holding's own lot rules").
Fork pre-flight: jaysbeekay/sure#603 (https://github.com/jaysbeekay/sure/pull/603), which holds this slice's CI and review history. Same branch, same commit. It ports the model part of the merged fork PR jaysbeekay/sure#583, whose review history is there too.
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
- New Features
- Choose average-cost or FIFO accounting for eligible accounts. New accounts default to average cost; changing the method recalculates cost basis and realized gains.
- View acquisition lots, quantities, and costs in holdings. Edit lot costs or restore a lot to its trade price.
- FIFO sales use the costs of the lots they dispose of when calculating realized gains.
- Bug Fixes
- Improved cost-basis handling for transfers, splits, and security remaps. Cost basis remains unknown when holdings cannot be reliably reconciled, avoiding misleading values.