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:
- Fix autoFlashState race conditions directly
- Use localStorage for session tracking
- MongoDB backend-first with autoFlashState as cache
- Replace autoFlashState entirely with plain signals
- 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
apis/backend-ai-api/src/app/chat/chat.controller.tsapis/backend-ai-api/src/app/chat/chat.module.tsapis/backend-ai-api/src/app/sessions/sessions.controller.tsapis/backend-ai-api/src/app/sessions/sessions.service.tslibs/libs-ai/panel-intelligence/src/lib/services/panel-session-persistence.service.tslibs/libs-ai/panel-intelligence/src/lib/services/panel-intelligence.service.tslibs/libs-ai/ai-panel-core/src/lib/components/ai-panel/ai-panel.component.ts
🔗 Related
- Decision ID:
dev_1771623581026_4t8ghwd - Session: 129
- Workspace: gspace1
Auto-generated by MCP Smart Consolidation