Add cash-anchor accounting for manual accounts
Part 1 of #5620. Starts at main. The public-key layer is already on main via #4615.
main ← up/bitcoin-cash · Previous discussion #4613 · Fork diff.
A manual investment or Crypto account needs a cash baseline when a provider starts tracking one of its holdings. This adds a dated cash anchor. A later edit to an earlier cash movement adjusts the baseline once. Ordinary valuations, imported holdings and unrelated securities keep their accounting history.
Holding materialization carries forward an observed position, its per-share cost and any explicit cost lock. Recording or correcting a split after the position was carried forward recalculates its quantity, price and inherited cost. Deleting the last trade removes the calculated rows and preserves the observations they came from.
Migrations
Two nullable columns on valuations, superseded_at and cash_entry_total, record which same-day valuation was superseded and the cash ledger total at the anchor. Two boolean columns on holdings, materialized and cost_basis_inherited, default to false so existing observations and explicit cost overrides are preserved.
Earlier review
The same-day valuation race is covered by an account row lock. Regression tests cover a late split and the deletion of a trade after an imported holding is recorded. Maintainer review · Imported holding follow-up.
Validation
The full Rails suite passed on 5878948f1 (18,543 tests, 9 skips). The Chromium suite passed (460 tests). RuboCop, ERB lint, Biome and Brakeman passed.
Summary by cubic
Adds a cash anchor for manual investment and crypto accounts so a provider that starts tracking a holding gets a cash baseline from the ledger. The anchor records the account's cash and ledger baseline on its date; later edits to earlier cash movements adjust the baseline once. Ordinary valuations, imported holdings, and unrelated securities keep their existing accounting history.
Behavior
- A cash-anchored account rebuilds from its first anchor date and keeps history before it as recorded; non-anchored accounts still rebuild on late price or rate changes.
- Untraded and provider snapshots carry forward as calculated rows, repriced daily, preserving manual basis and cost locks.
- One position per security and date feeds balances and reports; provider snapshots outrank observed rows, observed outrank calculated, and the account currency wins within a source.
- Tax lot quantities follow the preferred position for cash-anchored accounts.
- Recording or correcting a split or snapshot after carry-forward recalculates quantity, price, and inherited cost.
- Deleting an account's trades clears their calculated rows and preserves independently imported holdings.
- Future-dated holdings and scheduled trades affect balances only from their own dates; stale future-dated calculated rows are purged, and the current-holdings currency cache rotates at the date boundary.
- The holdings API lists only active calculations, but a superseded row stays accessible by ID.
- Cash-anchored account syncs run in the family's time zone, falling back to the app zone, and are not coalesced with unrelated pending syncs.
- Cash anchors are excluded from account activity and the valuations API; data export and import preserve provenance columns, trade settlement currency, and custom exchange rates.
Migration
valuationsgainssuperseded_atandcash_entry_total;holdingsgainsmaterialized,cost_basis_inherited, andsuperseded, all defaulting to preserve existing rows.- A batch backfill marks legacy calculated holdings as materialized, counting only rows that match the migration criteria; protected observations and rows without a matching trade cannot establish provenance.
Summary by CodeRabbit
- New Features
- Added cash balance reconciliations that preserve a ledger baseline and account for later cash movements, trades, and currency conversion.
- Improved historical balance and holdings calculations for accounts with an opening balance, including imported holdings and updated historical prices.
- Data exports and restores now retain cash reconciliation and holding details.
- Bug Fixes
- Future-dated or superseded holdings no longer affect current balances, currency lists, or holdings lists; recalculation cleans up stale future holdings.
- Cash reconciliations no longer appear as regular account activity or interfere with valuation creation.
- Eligible account syncs use the family’s configured time zone, falling back to the application time zone when unavailable.
Feedback carried over from earlier PRs
The original comments are linked below, including follow-ups addressed by a later layer.
- Reviewer suggestion: Preserve snapshot-backed quantities before the first trade, including future-dated trades. #5621 review · #5621 review · #5622 review · #5622 review
- Reviewer suggestion: Keep snapshot-only holdings visible when FX conversion is unavailable. #5621 review
- Reviewer suggestion: Keep unparented manual edits out of cancellable wallet sync trees. #5621 review · #5622 review
- Reviewer suggestion: Read anchor cash and ledger totals consistently during concurrent entry changes. #5621 review · #5622 review
- Reviewer suggestion: Remove obsolete calculated holdings inside the rebuild window. #5621 review · #5622 review
- Reviewer suggestion: Index synthesized trade-day prices for carry-forward lookups. #5621 review · #5622 review
- Reviewer suggestion: Protect a pre-existing cash-entry-total column on rollback. #5621 review · #5622 review
- Reviewer suggestion: Distinguish legacy calculated holdings from genuine snapshots. #5621 review
- Reviewer suggestion: Exclude cash-only anchors from historical total-balance reconciliation. #5621 review
- Reviewer suggestion: Test the effective time zone inside the real synchronization path. #5621 review
- Reviewer suggestion: Preserve imported pre-window balances when an incremental rebuild yields no rows. #5622 review