diff --git a/src/features/agents/SessionEditor.tsx b/src/features/agents/SessionEditor.tsx index 5bfe26036..39757e3a6 100644 --- a/src/features/agents/SessionEditor.tsx +++ b/src/features/agents/SessionEditor.tsx @@ -174,15 +174,18 @@ function TaskSession({ id, docked }: { id: string; docked: boolean }) { }, [id, connected]); // A commit in the task's checkout moves its HEAD: pick it up without waiting for the agent. // The watcher reports every repository under the project, so the main checkout and other - // tasks' worktrees would otherwise re-read this task's commits for nothing. - const cwd = task?.cwd ?? null; + // tasks' worktrees would otherwise re-read this task's changes for nothing. A worktree's + // gitdir is `/.git/worktrees/`, outside the worktree itself: the watcher only + // reports the project there, so that is the root its events match. + const refreshRoot = + task?.isolation === "worktree" ? (task.projectRoot ?? project?.root ?? null) : (task?.cwd ?? null); useEffect(() => { if (!connected) return; return onIdeEvent("vcsChanged", (e) => { - if (!cwd || !isSameOrInside(cwd, e.root)) return; + if (!refreshRoot || !isSameOrInside(refreshRoot, e.root)) return; scheduleChanges(id); }); - }, [id, connected, cwd]); + }, [id, connected, refreshRoot]); // A kept-alive tab stays mounted behind the shown one: it isn't read there, nor searched. const shown = useEditorShown(); useEffect(() => { diff --git a/src/features/agents/sessionState.test.tsx b/src/features/agents/sessionState.test.tsx index d350d8174..b06e7fffe 100644 --- a/src/features/agents/sessionState.test.tsx +++ b/src/features/agents/sessionState.test.tsx @@ -22,6 +22,7 @@ const backend = vi.hoisted(() => ({ changes: [] as AgentChange[], changesCalls: 0, grouped: { uncommitted: [] as AgentChange[], commits: [] as AgentCommit[] }, + groupedCalls: 0, rolledBack: [] as Array<[string, string[] | null, string | null]>, answers: [] as Array<[string, number, string | null]>, fetched: [] as string[], @@ -59,7 +60,10 @@ vi.mock("@/platform/ipc", async (original) => ({ backend.changesCalls += 1; return { status: "ok", data: backend.changes }; }, - agentGroupedChanges: async () => ({ status: "ok", data: backend.grouped }), + agentGroupedChanges: async () => { + backend.groupedCalls += 1; + return { status: "ok", data: backend.grouped }; + }, agentRollbackChanges: async (task: string, paths: string[] | null, view: string | null) => { backend.rolledBack.push([task, paths, view]); return { status: "ok", data: null }; @@ -170,6 +174,7 @@ beforeEach(() => { backend.changes = []; backend.changesCalls = 0; backend.grouped = { uncommitted: [], commits: [] }; + backend.groupedCalls = 0; backend.rolledBack = []; backend.external = []; // The flat list unless a test asks for the grouped view, so counts and selections stay as before. @@ -1407,6 +1412,26 @@ describe("changes refresh", () => { await act(() => vi.advanceTimersByTimeAsync(600)); expect(backend.changesCalls).toBe(1); }); + + it("re-reads a worktree task's changes on the project's events, where its gitdir lives", async () => { + vi.useFakeTimers(); + useChangesOptions.setState({ groupByCommit: true }); + useAgents.setState({ + tasks: new Map([["A", { ...task("A"), isolation: "worktree", cwd: "/worktrees/a", projectRoot: "/project" }]]), + }); + render(); + await act(async () => {}); + backend.groupedCalls = 0; + + act(() => emitLocalIdeEvent({ type: "vcsChanged", root: "/elsewhere" })); + await act(() => vi.advanceTimersByTimeAsync(600)); + expect(backend.groupedCalls).toBe(0); + + // A commit in the worktree writes /.git/worktrees/, so the event is rooted at the project. + act(() => emitLocalIdeEvent({ type: "vcsChanged", root: "/project" })); + await act(() => vi.advanceTimersByTimeAsync(600)); + expect(backend.groupedCalls).toBe(1); + }); }); describe("accept changes", () => {