From 2699e198d6a731898fcf0e7fc4dc15c84a4b1cd7 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 3 Dec 2025 19:14:37 +0700 Subject: [PATCH] fix: always fetch agent timeline on screen open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, opening an agent screen would skip fetching timeline history if stream state already existed in the cache. This caused issues when: - Opening a running agent after reconnection - Returning to an agent with stale/incomplete cache Now we always fetch fresh timeline on open, ensuring: - Complete history is shown regardless of client state - Reconnection scenarios work correctly - Agent state is always up-to-date Uses a ref to track initialization per mount to prevent infinite loops. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../src/app/agent/[serverId]/[agentId].tsx | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/packages/app/src/app/agent/[serverId]/[agentId].tsx b/packages/app/src/app/agent/[serverId]/[agentId].tsx index 46e1e6179..cb6534c1d 100644 --- a/packages/app/src/app/agent/[serverId]/[agentId].tsx +++ b/packages/app/src/app/agent/[serverId]/[agentId].tsx @@ -315,37 +315,38 @@ function AgentScreenContent({ serverId, agentId, onBack }: AgentScreenContentPro }; }, [resolvedAgentId, setFocusedAgentId]); - const hasStreamState = useSessionStore((state) => - resolvedAgentId ? state.sessions[serverId]?.agentStreamState?.has(resolvedAgentId) ?? false : false - ); + const isInitializing = resolvedAgentId ? isInitializingFromMap !== false : false; - const isInitializing = resolvedAgentId - ? isInitializingFromMap !== false - ? isInitializingFromMap - : !hasStreamState - : false; + // Track which agent we've already triggered initialization for + const initializedAgentRef = useRef(null); useEffect(() => { if (!resolvedAgentId || !initializeAgent) { return; } - console.log("[AgentScreen] initializeAgent effect", { + // Skip if already initializing + if (isInitializingFromMap === true) { + return; + } + + // Skip if we already triggered initialization for this agent + if (initializedAgentRef.current === resolvedAgentId) { + return; + } + + // Mark this agent as initialized + initializedAgentRef.current = resolvedAgentId; + + // Always fetch timeline when opening agent screen + // Server returns full snapshot, client replaces cache + // This ensures we always have complete history, even after reconnection + console.log("[AgentScreen] Fetching agent timeline", { agentId: resolvedAgentId, - hasStreamState, - isInitializingFromMap, }); - if (isInitializingFromMap !== false) { - return; - } - - if (hasStreamState) { - return; - } - initializeAgent({ agentId: resolvedAgentId }); - }, [resolvedAgentId, initializeAgent, isInitializingFromMap, hasStreamState]); + }, [resolvedAgentId, initializeAgent, isInitializingFromMap]); const agentControls = useMemo(() => { if (!resolvedAgentId) return null;