From 2e85fc1acc22343c53ddbd38b6d488c87c08c953 Mon Sep 17 00:00:00 2001 From: Vadym Samoilenko Date: Mon, 23 Mar 2026 19:30:04 +0000 Subject: [PATCH] Fix root cause: naive vs aware datetime crash + stuck AI mode indicator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The autonomous loop was crashing on every decision with: TypeError: can't subtract offset-naive and offset-aware datetimes because MongoDB stores created_at without timezone info but the code compared it against datetime.now(timezone.utc). - conversation_context_service: make created_at timezone-aware before subtraction (replace tzinfo=utc when naive) - DiscussionPanel: fix sync effect — when server reports AI mode is inactive, always clear localAiModeActive regardless of its value, so the "AI is generating..." spinner doesn't get stuck when the backend fails/stops before the frontend has confirmed AI mode started Co-Authored-By: Claude Sonnet 4.6 --- backend/app/services/conversation_context_service.py | 3 +++ src/components/focus-group-session/DiscussionPanel.tsx | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/app/services/conversation_context_service.py b/backend/app/services/conversation_context_service.py index 33722672..e09bf659 100755 --- a/backend/app/services/conversation_context_service.py +++ b/backend/app/services/conversation_context_service.py @@ -50,6 +50,9 @@ class ConversationContextService: # Calculate elapsed time created_at = focus_group.get('created_at') if created_at: + # MongoDB may return naive datetimes — make timezone-aware before subtracting + if created_at.tzinfo is None: + created_at = created_at.replace(tzinfo=timezone.utc) elapsed_minutes = (datetime.now(timezone.utc) - created_at).total_seconds() / 60 else: elapsed_minutes = 0 diff --git a/src/components/focus-group-session/DiscussionPanel.tsx b/src/components/focus-group-session/DiscussionPanel.tsx index dffb6101..7bad6693 100755 --- a/src/components/focus-group-session/DiscussionPanel.tsx +++ b/src/components/focus-group-session/DiscussionPanel.tsx @@ -77,12 +77,13 @@ const DiscussionPanel = ({ // Calculate reasoning panel visibility - only show when user explicitly expands it const reasoningPanelVisible = reasoningPanelExpanded; - // Sync localAiModeActive back to null once parent prop has caught up + // When server reports AI is NOT active, clear any optimistic local override immediately. + // This handles the case where AI mode fails/stops before the frontend confirms it started. useEffect(() => { - if (localAiModeActive !== null && localAiModeActive === isAiModeActive) { + if (!isAiModeActive && localAiModeActive !== null) { setLocalAiModeActive(null); } - }, [isAiModeActive, localAiModeActive]); + }, [isAiModeActive]); // Fetch reasoning history when in AI mode useEffect(() => {