Description
src/chat.py holds all conversation history, escalation events, and active connections entirely in instance variables:
self.message_history: Dict[str, List[ChatMessage]] = {}
self.escalations: List[EscalationEvent] = []
A pod restart, dyno cycle, or deployment wipes every conversation. Users lose their support history and any pending escalations silently disappear — a critical reliability gap for a support chat system.
Requirements & context
- Create a
src/chat_store.py module with a ChatStore class that persists messages and escalations to Postgres using the existing SQLAlchemy setup
- Add two new SQLAlchemy models (or Alembic migrations):
chat_messages and chat_escalations tables mirroring the ChatMessage and EscalationEvent Pydantic models
- Update
ChatManager.send_message to persist each message to the DB (async-safe — use asyncio.to_thread if using sync SQLAlchemy)
- Update
ChatManager.get_message_history to fall back to the DB when the in-memory cache is empty (i.e. after a restart)
- Update
ChatManager.escalate_conversation to persist the escalation event
- Active WebSocket connections remain in-memory (they cannot be persisted) — document this clearly
Suggested execution
git checkout -b feat/chat-persistent-storage
- Create
src/chat_store.py with ChatStore
- Add SQLAlchemy models and Alembic migration
- Update
ChatManager to call ChatStore for read/write operations
- Write tests confirming messages survive a simulated manager restart (new instance reads from DB)
Guidelines
- Do not change the WebSocket or HTTP API contracts
- PR must include:
Closes #[issue_id]
- Timeframe: 96 hours
Description
src/chat.pyholds all conversation history, escalation events, and active connections entirely in instance variables:A pod restart, dyno cycle, or deployment wipes every conversation. Users lose their support history and any pending escalations silently disappear — a critical reliability gap for a support chat system.
Requirements & context
src/chat_store.pymodule with aChatStoreclass that persists messages and escalations to Postgres using the existing SQLAlchemy setupchat_messagesandchat_escalationstables mirroring theChatMessageandEscalationEventPydantic modelsChatManager.send_messageto persist each message to the DB (async-safe — useasyncio.to_threadif using sync SQLAlchemy)ChatManager.get_message_historyto fall back to the DB when the in-memory cache is empty (i.e. after a restart)ChatManager.escalate_conversationto persist the escalation eventSuggested execution
src/chat_store.pywithChatStoreChatManagerto callChatStorefor read/write operationsGuidelines
Closes #[issue_id]