From 45806ea907334d86c7f0729692d5d4826cf570f8 Mon Sep 17 00:00:00 2001
From: YurenHao0426 <blackhao0426@gmail.com>
Date: Sat, 14 Feb 2026 04:27:16 +0000
Subject: Fix stale closure bug in council/debate node title generation

The onComplete callbacks captured `nodes` from the closure at call time,
which was stale by the time streaming finished. Use useFlowStore.getState()
to read fresh state instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---
 frontend/src/components/Sidebar.tsx | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

(limited to 'frontend/src/components/Sidebar.tsx')

diff --git a/frontend/src/components/Sidebar.tsx b/frontend/src/components/Sidebar.tsx
index 672da12..08f0988 100644
--- a/frontend/src/components/Sidebar.tsx
+++ b/frontend/src/components/Sidebar.tsx
@@ -566,8 +566,9 @@ const Sidebar: React.FC<SidebarProps> = ({ isOpen, onToggle, onInteract }) => {
           onStreamBuffer: (text) => { setCouncilStreamBuffer(text); setStreamBuffer(text); },
           onComplete: () => {
             setCouncilStage('');
-            // Fetch final response for title generation
-            const currentNode = nodes.find(n => n.id === runningNodeId);
+            // Fetch final response for title generation (use fresh store state, not stale closure)
+            const freshNodes = useFlowStore.getState().nodes;
+            const currentNode = freshNodes.find(n => n.id === runningNodeId);
             const finalResponse = currentNode?.data.response || '';
             if (finalResponse) generateTitle(runningNodeId, runningPrompt, finalResponse);
           },
@@ -696,7 +697,9 @@ const Sidebar: React.FC<SidebarProps> = ({ isOpen, onToggle, onInteract }) => {
           onStreamBuffer: (text) => { setDebateStreamBuffer(text); setStreamBuffer(text); },
           onComplete: () => {
             setDebateStage('');
-            const currentNode = nodes.find(n => n.id === runningNodeId);
+            // Fetch final response for title generation (use fresh store state, not stale closure)
+            const freshNodes = useFlowStore.getState().nodes;
+            const currentNode = freshNodes.find(n => n.id === runningNodeId);
             const finalResponse = currentNode?.data.response || '';
             if (finalResponse) generateTitle(runningNodeId, runningPrompt, finalResponse);
           },
-- 
cgit v1.2.3

