From 473e11d25fe050c8b79ba90427fe5979cd878ead Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Tue, 2 Dec 2025 17:17:54 +0000 Subject: [PATCH] Fix stale closure bug in agent-input-area message handlers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace direct `agentId` prop usage with `agentIdRef.current` to prevent messages from being sent to the wrong agent during navigation. Fixed 3 instances: - handleSendMessage: Line 383 - handleSendQueuedNow: Line 869 - handleCancelAgent: Line 834 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../app/src/components/agent-input-area.tsx | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/app/src/components/agent-input-area.tsx b/packages/app/src/components/agent-input-area.tsx index 9918bcf7c..d91941be5 100644 --- a/packages/app/src/components/agent-input-area.tsx +++ b/packages/app/src/components/agent-input-area.tsx @@ -101,12 +101,12 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) { }), [] ); - const noopSendAgentMessage = useCallback(async () => {}, []); - const noopSendAgentAudio = useCallback(async () => {}, []); - const noopCancelAgentRun = useCallback(() => {}, []); - const noopGetDraftInput = useCallback(() => undefined, []); - const noopSaveDraftInput = useCallback(() => {}, []); - const noopSetQueuedMessages = useCallback(() => {}, []); + const noopSendAgentMessage = useCallback(async () => {}, []); + const noopSendAgentAudio = useCallback(async () => {}, []); + const noopCancelAgentRun = useCallback(() => {}, []); + const noopGetDraftInput = useCallback(() => undefined, []); + const noopSaveDraftInput = useCallback(() => {}, []); + const noopSetQueuedMessages = useCallback(() => {}, []); const ws = session?.ws ?? inertWebSocket; const sendAgentMessage = session?.sendAgentMessage ?? noopSendAgentMessage; const sendAgentAudio = session?.sendAgentAudio ?? noopSendAgentAudio; @@ -380,7 +380,7 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) { setIsProcessing(true); try { - await sendAgentMessage(agentId, message, imageAttachments); + await sendAgentMessage(agentIdRef.current, message, imageAttachments); } catch (error) { console.error("[AgentInput] Failed to send message:", error); } finally { @@ -662,7 +662,7 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) { const updateQueue = useCallback( (updater: (current: QueuedMessage[]) => QueuedMessage[]) => { - setQueuedMessagesByAgent((prev) => { + setQueuedMessagesByAgent((prev: Map) => { const next = new Map(prev); next.set(agentId, updater(prev.get(agentId) ?? [])); return next; @@ -831,7 +831,7 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) { return; } setIsCancellingAgent(true); - cancelAgentRun(agentId); + cancelAgentRun(agentIdRef.current); } function handleQueueCurrentInput() { @@ -866,7 +866,7 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) { // Cancels current agent run before sending queued prompt handleCancelAgent(); - await sendAgentMessage(agentId, item.text, item.images); + await sendAgentMessage(agentIdRef.current, item.text, item.images); } const realtimeButton = (