From 5d5a196525814a1914bcb3b26b20cfaefc9199be Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Sun, 21 Dec 2025 17:08:05 +0000 Subject: [PATCH] fix: prevent infinite loop in git-diff screen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added hasRequestedRef to track if a git diff request was already made for the current agentId. This prevents duplicate requests when the requestGitDiff function reference changes due to store updates. The issue was that requestGitDiff in the useEffect dependency array caused an infinite loop: request → store update → new function ref → effect re-runs → request again. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- packages/app/src/app/git-diff.tsx | 9 ++++++++- plan.md | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/app/src/app/git-diff.tsx b/packages/app/src/app/git-diff.tsx index dd1901252..fca720ca7 100644 --- a/packages/app/src/app/git-diff.tsx +++ b/packages/app/src/app/git-diff.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import { View, Text, ScrollView, ActivityIndicator } from "react-native"; import { useLocalSearchParams } from "expo-router"; import { StyleSheet } from "react-native-unistyles"; @@ -97,6 +97,7 @@ function GitDiffContent({ serverLabel: string; }) { const [isLoading, setIsLoading] = useState(true); + const hasRequestedRef = useRef(null); const agent = useSessionStore((state) => agentId && serverId ? state.sessions[serverId]?.agents?.get(agentId) : undefined @@ -116,6 +117,12 @@ function GitDiffContent({ return; } + // Prevent duplicate requests for the same agentId + if (hasRequestedRef.current === agentId) { + return; + } + hasRequestedRef.current = agentId; + setIsLoading(true); requestGitDiff(agentId); diff --git a/plan.md b/plan.md index 4f82c5d7d..0ee2f3a8e 100644 --- a/plan.md +++ b/plan.md @@ -222,13 +222,14 @@ Remove `requestGitDiff` from useEffect dependencies in `git-diff.tsx`. Use a ref --- -- [ ] **Fix**: Remove requestGitDiff from useEffect dependencies in git-diff.tsx. +- [x] **Fix**: Remove requestGitDiff from useEffect dependencies in git-diff.tsx. - The `requestGitDiff` function reference changes on every store update - Remove it from the dependency array to prevent infinite loop - Use a ref to track if we've already requested the diff - Keep `agentId` as the only dependency (or use empty deps with agentId check) - Run typecheck after fix. + - **Done (2025-12-21 18:10)**: Added `hasRequestedRef` to track if a request was already made for the current agentId. The ref prevents duplicate requests when `requestGitDiff` reference changes due to store updates. Typecheck passes. - [ ] **Test**: Verify git diff loads correctly after fix.