Skip to content

Instantly share code, notes, and snippets.

@adamdilek
Created March 25, 2026 22:47
Show Gist options
  • Select an option

  • Save adamdilek/4023fdc10b4b2fecd3310d44af849db0 to your computer and use it in GitHub Desktop.

Select an option

Save adamdilek/4023fdc10b4b2fecd3310d44af849db0 to your computer and use it in GitHub Desktop.
Reconstructed Balance — TODO & Next Steps

Reconstructed Balance — TODO List

PRs to Merge

  • PR #1410 — Reconstructed Balance for Cash Flow (CI fixing)
    • BalanceAnchor model, bi-directional reconstruction, CashForecastService integration
    • 10 commits, 434 specs pass
  • PR #1411 — Investment Transaction Sign Fix
    • buy/sell amount signs corrected in InvestmentsTransactionsSyncService
    • Rake task: bundle exec rake data:fix_investment_transaction_signs (153 txns to fix in prod)

Post-Merge: Run in Production

  • bundle exec rake data:fix_investment_transaction_signs — fix 153 existing buy/sell transactions
  • Set balance anchors for April company accounts (admin will contact clients for statement dates/balances):
    • Account 9 (May Advisory Checking): Dec 31, $178,245.96
    • Account 10 (April Tax Checking): Dec 31, $639,927.67
    • Account 11: Dec 31, $308,636.78
    • Account 142 (MMF): Dec 31, $516,967.46 — also reimport transactions from broker CSV (sign fix)

Next Session: Investment Accounts in Cash Flow

  • Investment accounts (139, 140, 141) should be excluded from cash flow calculations
    • T-bill buy/sell transactions missing from Plaid → fake inflow/outflow in cash flow
    • These are NOT cash accounts — deposits/withdrawals are T-bill trades, not real cash movement
    • MMF (142) is cash-equivalent → should STAY in cash flow
    • Gemini brainstorm prompt ready (see below)

Next Session: EnableTransactionTracking → SetBalanceAnchor Refactor

  • EnableTransactionTracking mutation should delegate to SetBalanceAnchor
    • Both do the same thing, two parallel systems is confusing
    • opening_balance_cents/date on BankAccount → deprecated, read from BalanceAnchor
    • balance_tracked_by_transactions flag → keep for now, auto-set when anchor exists

Next Session: DailyPositionRebuildJob for Integrated Accounts

  • When integrated account gets an anchor, DailyPositionRebuildJob overwrites current_value_cents with reconstructed value
    • This is WRONG for integrated accounts — current_value_cents should be bank-reported (Plaid snapshot)
    • Fix: skip rebuild job for integrated accounts, only run DailyBalanceCalculatorJob (writes to reconstructed_value_cents only)
    • Not a problem yet — no integrated accounts have anchors in production

Known Issues (Lower Priority)

  • Investment account holdings reliability (aggregate vs itemize) — separate project
  • Plaid sends bad balances for investment accounts — statement-based override needed (separate from reconstruction)
  • Combined statement for accounts 139+140+141 — can't set individual anchors without per-account breakdown
  • BalanceAnchor model vs opening_balance_cents field duplication — eventual cleanup

Gemini Brainstorm Prompt (Investment Cash Flow Exclusion)

/gemini-brainstorm Investment/brokerage account'lar (scheme: INVESTMENT) cash flow hesaplamalarını bozuyor. T-bill/bond account'larda Plaid transaction olarak sadece deposit/withdrawal/interest/fee gönderiyor — asıl T-bill buy/sell transaction'ları yok. Bu yüzden cash flow'da devasa inflow/outflow ($74M gibi) ve büyük adjustment'lar çıkıyor. Gerçek cash movement değil bunlar.

Yapılması gereken:
1. INVESTMENT scheme hesapları cash flow inflow/outflow hesaplamalarından çıkar
2. Ama MMF (money market fund) hesaplar cash-equivalent — bunlar KALMALI
3. CashForecastService'de hangi account'ların cash flow'a dahil olacağını belirleyen mantık nerede, nasıl değişmeli?
4. DailyPosition/HistoricalPosition'da investment account'lar opening/closing balance'a dahil mi, sadece transaction'ları mı çıkarıyoruz yoksa balance'ı da mı?
5. UI'da investment account'lar "Non-Cash Investments" gibi ayrı bir section'da mı gösterilmeli?
6. Mevcut liquidity_included flag ile ilişki ne? Bu flag yeterli mi yoksa yeni bir flag mı lazım (cash_flow_included gibi)?

Gerçek veri:
- Company 4 (April) — 4 investment account: 139 (Liquid Treasuries, $5.1M), 140 (U.S. Treasuries, $10.1M), 141 (Commercial Paper, $5.1M), 142 (MMF, $5.7K)
- 139, 140, 141: T-bill buy/sell transaction'ları Plaid'de YOK, balance reconstruction imkansız, cash flow'dan ÇIKMALI
- 142 (MMF): transaction'lar doğru (deposit/withdrawal/dividend/fee), reconstruction ÇALIŞIYOR, cash flow'da KALMALI
- Cash flow şu anda May'25'te +$74M inflow / -$74M outflow gösteriyor — bunlar T-bill deposit'leri, gerçek cash değil

Referans dosyalar:
- app/services/forecast/cash_forecast_service.rb — lines 779-797 zaten investment holding exclusion var, genişletilecek
- app/models/account_type.rb — investment_scheme? metodu
- app/models/bank_account.rb — account_type association
- CashForecastService'de liquid_positions, fetch_current_positions, actuals hesaplaması

Technical perspective.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment