Architecture: How to persist AI panel chat sessions reliably across page r...

Documentation for Sessions

🏗️ Architecture: How to persist AI panel chat sessions reliably across page r...

Date: 2026-02-20
Time: 21:39:41 UTC
Session: gspace1-129.7
Agent: storage
Confidence: 95%
Status: ✅ Documented


📋 Decision Context

Question:

How to persist AI panel chat sessions reliably across page refresh and devices?

Task: Fix AI panel chat persistence across refresh and multiple sessions


🎯 Decision Made

Chosen:

MongoDB as source of truth with separate _backendSessions signal immune to autoFlashState race conditions

Options Considered:

  1. Fix autoFlashState race conditions directly
  2. Use localStorage for session tracking
  3. MongoDB backend-first with autoFlashState as cache
  4. Replace autoFlashState entirely with plain signals
  5. MongoDB + separate _backendSessions signal immune to autoFlashState

💭 Reasoning

autoFlashState (IndexedDB) has 3 interleaved async loading paths that create unresolvable race conditions — stale data overwrites fresh data unpredictably. After 6+ failed attempts to fix via hydration gates, effects, localStorage, and auth timing workarounds, the only reliable approach was: (1) Backend saves messages inline during POST /ai-api/chat via appendMessages(), (2) JWT-authenticated endpoints GET /sessions/my/active and /sessions/my/history load data on page init without any frontend userId resolution, (3) Backend sessions stored in a plain Angular signal (_backendSessions) that autoFlashState cannot touch — this was the critical insight that made multi-session persistence work. The sessionHistory computed merges both sources with backend taking priority.


📁 Files Involved


🔗 Related


Auto-generated by MCP Smart Consolidation