Description
src/chat.py stores all escalation events in a single flat list:
self.escalations: List[EscalationEvent] = []
get_escalations(conversation_id) filters this list on every call:
def get_escalations(self, conversation_id=None) -> List[EscalationEvent]:
if conversation_id:
return [e for e in self.escalations if e.conversation_id == conversation_id]
return self.escalations.copy()
In a high-volume support environment with thousands of escalations, every call to GET /chat/{id}/escalations scans the entire list. With 10,000 escalations and 100 concurrent requests, this is 1,000,000 comparisons per second.
Requirements & context
- Replace the flat
self.escalations: List[EscalationEvent] with a per-conversation dict:
self.escalations: Dict[str, List[EscalationEvent]] = {}
- Update
escalate_conversation to append to self.escalations.setdefault(conversation_id, [])
- Update
get_escalations(conversation_id):
- With
conversation_id: return self.escalations.get(conversation_id, [])
- Without: return a flat list from all values (only used for admin queries)
- The change must be backward-compatible — existing callers of
get_escalations must work without modification
- Write a performance test confirming lookup time is O(1) per conversation, not O(n) global
Suggested execution
git checkout -b fix/chat-escalations-dict-index
- Update
src/chat.py
- Write tests confirming O(1) lookup
Guidelines
- PR must include:
Closes #[issue_id]
- Timeframe: 24 hours
Description
src/chat.pystores all escalation events in a single flat list:get_escalations(conversation_id)filters this list on every call:In a high-volume support environment with thousands of escalations, every call to
GET /chat/{id}/escalationsscans the entire list. With 10,000 escalations and 100 concurrent requests, this is 1,000,000 comparisons per second.Requirements & context
self.escalations: List[EscalationEvent]with a per-conversation dict:escalate_conversationto append toself.escalations.setdefault(conversation_id, [])get_escalations(conversation_id):conversation_id: returnself.escalations.get(conversation_id, [])get_escalationsmust work without modificationSuggested execution
src/chat.pyGuidelines
Closes #[issue_id]