Version the gains series key on holdings, and pin two roll-up decisions (#133 review round 2)
Merged
JA
3mo ago+211+211+012345678901234567890123456789-11-11-01234567890123456789
Summary
Second review round on #133 (issue #119), stacked on feat/portfolio-foundations. Every open finding on #133 was checked against the code; the verdict table is posted as a comment there. This PR carries the fixes for the findings that held up:
- Stale
gains_seriescache after a manual cost-basis edit (valid).HoldingsController#updateāHolding#set_manual_cost_basis!writes the holding alone;Family#build_cache_keyreads onlylatest_sync_completed_atandaccounts.maximum(:updated_at), neither of which moves, so the cached gains survived the edit. The gains key now carries a holdings version (count-max(updated_at)over the historical-scope accounts), which also covers unlock, remap and deletion. The value series read balances only, which a sync rewrites, so they keep the cheaper key. - Holding collision handler skipped
updated_at(cubic, on this PR; valid).Account::ProviderImportAdapter#import_holding's composite-key rescue rewrites qty, price, amount and cost basis withupdate_columns, which leaves the timestamp alone; if that sync then failed, neither part of the gains key moved. The rescue now stampsupdated_atin the same write. The other direct holding writes were checked:Holding::Materializersetsupdated_atin itsupsert_allpayload, and the unlink paths only nullaccount_provider_id. - Two roll-up decisions outside the contract (valid): a security whose holdings carry no value is omitted rather than listed at weight 0 (new row P27), and a rolled-up return covers only the holdings with a known cost basis while the amount counts every holding (new row P28). Both now have tests bound by the manifest; the gate verifies 28 rows.
- Memoization (Codacy):
shares_versionandholdings_rolled_up_by_securityare memoized per instance. allocationN+1 (Codacy / "Jedi" M4): no preload applies (Holding#tradesis a method, not an association); the stored-cost-basis path issues no trade queries, now pinned by a query-count test.
Scope and dependencies
- Issue: #119, via #133
- Parent PR/branch, if stacked: #133 (
feat/portfolio-foundations) - Intentionally out of scope: Plaid zero-amount dividends (#123);
Balance::ChartSeriesBuilder'sb.currency = accounts.currencyfilter andcurrent_holdings' per-(account, security) selection, both upstream code shared with the net worth chart; taxonomy parent/child mapping (#122); a migrate-from-base CI job; renamingHoldingAllocation#weight(would change the three views kept byte-identical to upstream).
Evidence checklist
- Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
- Contract rows touched by this change name existing tests (P27, P28 in
docs/portfolio/methodology.mdandconfig/portfolio_contract_tests.yml). - Any gate test has an observed failing mutation transcript attached (below).
- Fixtures used as evidence assert internal consistency (the P28 test asserts the row's amount over both holdings before asserting the trend over one).
- Financial calculations use
BigDecimaland documented rounding points (unchanged). - De-identification uses token-shape allowlists; no personal data is included.
- Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.
Mutation transcripts
| Mutation | Tests that went red |
|---|---|
(holdings_version if kind == :gains) ā nil | "gains series cache key changes when a holding's cost basis is edited by hand", "⦠when a holding is deleted" (2 failures / 2 runs) |
next if value.zero? removed | "a security whose holdings carry no value is omitted from top_holdings and allocation" |
next unless trend replaced by a flat trend for holdings without a cost basis | "a rolled-up return is measured over the holdings whose cost basis is known" |
updates[:updated_at] = Time.current removed from the collision handler | "a composite-key collision stamps updated_at on the row it adopts" (1 failure / 1 run) |
Validation
Commands and results (run on Ruby 3.3.6 in the session container, since the 3.4.9 tarball host is denied by the egress policy; CI runs 3.4.9):
| Command | Result |
|---|---|
bin/rails test test/models/investment_statement_test.rb test/models/investment_statement/historical_scope_test.rb test/lib/portfolio/contract_coverage_test.rb test/tasks/portfolio_task_test.rb | 69 runs, 203 assertions, 0 failures |
bin/rails test test/models/account/provider_import_adapter_test.rb test/models/investment_statement_test.rb test/tasks/portfolio_task_test.rb (second commit) | 110 runs, 385 assertions, 0 failures |
bin/rails test test/controllers/pages_controller_test.rb test/controllers/reports_controller_test.rb | 63 runs, 224 assertions, 0 failures |
bin/rails test test/models/portfolio/flow_classifier_test.rb test/models/security_test.rb | 0 failures |
bin/rails portfolio:verify_contract_coverage | Verified 28 portfolio contract rows against existing tests |
bin/rubocop -f github on changed files | no offenses |
bin/brakeman --no-pager | No warnings found |
Fork CI on bf75f99 | all seven checks green |
Risks, blockers, and follow-up
- The gains key adds two
holdingsqueries perInvestmentStatementinstance that asks for a gains series (none today; #120 is the first reader). - The verdict comment on #133 lists the findings left unchanged and why; the owner's call on Blocker 2 (
allocationkept until #120 wires it) stands as documented in P3.
š¤ Generated with Claude Code
https://claude.ai/code/session_01JqGVXNrELAK5ah6VWymaNK
- Invalidate gains-series caches after holding changes, including edits, deletions, remaps, unlocks, and collision updates.
- Preserve value-series cache keys for sync-related data.
- Memoize portfolio calculations.
- Add contract coverage for zero-value securities and cost-basis-aware returns.
- Verify that
allocationdoes not issue per-holding trade queries when cost basis is stored. - Extend contract verification to 28 rows.
- Add a regression test for collision-adopted holdings and
updated_atchanges.
| Author | Lines added | Lines removed |
|---|---|---|
| Not available | Not available | Not available |