Architecture: How to fix AI panel chat persistence that's lost on every pa...
Documentation for Sessions
🏗️ Architecture: How to fix AI panel chat persistence that's lost on every pa...
Date: 2026-02-20
Time: 03:48:23 UTC
Session: gspace1-125.134
Agent: storage
Confidence: 92%
Status: ✅ Documented
📋 Decision Context
Question:
How to fix AI panel chat persistence that's lost on every page refresh?
Task: Fix AI panel chat persistence
🎯 Decision Made
Chosen:
Backend-first persistence: save messages to MongoDB during POST /ai-api/chat, load from backend on page init
Options Considered:
- Fix autoFlashState race conditions (attempted 3x, failed)
- Backend-first: save during POST /chat + load on init
- Replace autoFlashState with direct IndexedDB calls
- Use Redis for real-time cache + MongoDB for persistence
💭 Reasoning
autoFlashState (IndexedDB) has unresolvable race conditions: 3 interleaved async loading paths (pre-load check, loadStateFromStorage promise, hydration watcher polling at 50ms) that overwrite each other. Multiple attempts to fix via hydration gates and effects all failed because the race is inside autoFlashState itself, not in consumer services. The correct fix: make the backend save messages inline with the POST /chat call (zero race conditions, synchronous with response), and load from backend MongoDB on page init. IndexedDB becomes a secondary cache that we don't rely on. Backend changes: ChatController accepts sessionId, calls ChatSessionsService.appendMessages() after generating response. Frontend changes: component passes sessionId to chat request, loads user's active session from GET /sessions/user/:userId on init.
📁 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.service.tslibs/libs-ai/panel-intelligence/src/lib/services/panel-session-persistence.service.tslibs/libs-ai/ai-panel-core/src/lib/components/ai-panel/ai-panel.component.ts
🔗 Related
- Decision ID:
dev_1771559303535_2tgi2mq - Session: 125
- Workspace: gspace1
Auto-generated by MCP Smart Consolidation