Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 切换任务时重置 worktree 存活状态

DavidShena5d332eb7a9d
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/sessionWorktreeInfoRefresh.test.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/sessionWorktreeInfoRefresh.test.tsx
index 96bd30bfe2..047e64002b 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/sessionWorktreeInfoRefresh.test.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/sessionWorktreeInfoRefresh.test.tsx
@@ -78,6 +78,26 @@ describe('opened worktree refresh', () => {
expect(result.current?.source).toBe('managed');
});
+ it('resets liveness from the new snapshot when switching tasks in place', async () => {
+ const metaA = { path: '/tmp/wt/a', name: 'a', branch: 'feature-a' };
+ const metaB = { path: '/tmp/wt/b', name: 'b', branch: 'feature-b' };
+ mocks.official.mockImplementation((task: { id: string }, options?: { includeInvalid?: boolean }) => {
+ if (options?.includeInvalid) return task.id === 'a' ? metaA : metaB;
+ return task.id === 'a' ? null : metaB;
+ });
+ mocks.detect.mockResolvedValue({ isInsideWorktree: false });
+ const view = renderHook(
+ ({ current }) => useTaskInfoWorktree(current, true, { observeTelemetry: true }),
+ { initialProps: { current: { ...session, id: 'a' } } },
+ );
+ await act(async () => {});
+ expect(view.result.current).toBeNull();
+
+ mocks.detect.mockResolvedValue({ isInsideWorktree: true });
+ await act(async () => view.rerender({ current: { ...session, id: 'b' } }));
+ expect(view.result.current?.source).toBe('managed');
+ });
+
it('applies recycle and restore events immediately', async () => {
const { result } = renderHook(() => useTaskInfoWorktree(session, true, { observeTelemetry: true }));
await act(async () => {});
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/sessionWorktreeInfo.ts b/apps/desktop/src/renderer/features/cc-agent/sidebar/sessionWorktreeInfo.ts
index 109fba0a1b..7f71d6c55a 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/sessionWorktreeInfo.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/sessionWorktreeInfo.ts
@@ -9,7 +9,7 @@
* 不写 store、不改变回收。目录没了摘掉徽标。外部 observed 不可 reveal。
*/
-import { useEffect, useState } from 'react';
+import { useEffect, useRef, useState } from 'react';
import { groupingWorktreeBaseRepo } from '@cindy/maker-shared/worktree-paths';
@@ -109,9 +109,16 @@ export function useTaskInfoWorktree(
const reportLiveness = useReportWorktreeLiveness();
const managed = resolveManagedWorktree(official);
const [observed, setObserved] = useState<SessionWorktreeInfo | null>(null);
- const [officialStillLive, setOfficialStillLive] = useState(
- () => !observeTelemetry || liveOfficial !== null,
- );
+ const initialOfficialStillLive = !observeTelemetry || liveOfficial !== null;
+ const [officialStillLive, setOfficialStillLive] = useState(initialOfficialStillLive);
+ const livenessRef = useRef({ sessionId: session.id, live: initialOfficialStillLive });
+ if (livenessRef.current.sessionId !== session.id) {
+ // The hook instance is reused when the route changes. Reset synchronously
+ // from the new snapshot so the previous task's state cannot leak for a
+ // render while the effect starts its probe.
+ livenessRef.current = { sessionId: session.id, live: initialOfficialStillLive };
+ }
+ const displayedOfficialStillLive = livenessRef.current.live;
const officialPath = official?.path ?? null;
const deviceId = session.deviceLinkDeviceId ?? null;
const isRemote = Boolean(deviceId || session.remoteHostId);
@@ -119,6 +126,7 @@ export function useTaskInfoWorktree(
useEffect(() => {
setObserved(null);
if (!enabled || isRemote || !observeTelemetry) {
+ livenessRef.current = { sessionId: session.id, live: !isRemote };
setOfficialStillLive(!isRemote);
return;
}
@@ -132,6 +140,7 @@ export function useTaskInfoWorktree(
const live = await probeIsInsideWorktree(officialPath, deviceId);
if (cancelled || gen !== generation) return;
if (live === null) return; // IPC 失败不代表目录已被删除,保留上次状态。
+ livenessRef.current = { sessionId: session.id, live };
setOfficialStillLive(live);
if (official) reportLiveness(official, live);
if (live) {
@@ -139,6 +148,7 @@ export function useTaskInfoWorktree(
return;
}
} else if (gen === generation) {
+ livenessRef.current = { sessionId: session.id, live: false };
setOfficialStillLive(false);
}
if (isRemote) {
@@ -190,7 +200,7 @@ export function useTaskInfoWorktree(
return selectDisplayedWorktree({
enabled,
managed,
- officialStillLive,
+ officialStillLive: displayedOfficialStillLive,
observed,
});
}