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 存活快照

DavidShene07d852dbd76
diff --git a/apps/desktop/src/renderer/__tests__/worktreeContextRecycleRefresh.test.tsx b/apps/desktop/src/renderer/__tests__/worktreeContextRecycleRefresh.test.tsx
index 6d73898c92..a1148f98a7 100644
--- a/apps/desktop/src/renderer/__tests__/worktreeContextRecycleRefresh.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/worktreeContextRecycleRefresh.test.tsx
@@ -154,6 +154,51 @@ describe('WorktreeContext recycle refresh', () => {
expect(view.getByTestId('ids').textContent).toBe('open:/tmp/wt/open');
});
+ it.each(['/tmp/wt/open', '/tmp/wt/restored'])(
+ 'uses replacement metadata at %s while its probe is pending or fails, ignoring old results',
+ async (restoredPath) => {
+ const meta = { sessionId: 'open', path: '/tmp/wt/open' };
+ mocks.worktreeListAll.mockResolvedValue([meta]);
+ mocks.worktreeDetectCwd.mockResolvedValue({ isInsideWorktree: false });
+ let refresh!: (sessionId: string) => Promise<void>;
+ function Actions() {
+ refresh = useRefreshWorktreeForSession();
+ return <><Probe /><ActiveProbe /></>;
+ }
+ const view = render(<WorktreeProvider><Actions /></WorktreeProvider>);
+ await act(async () => {});
+ expect(view.getByTestId('active').textContent).toBe('');
+ expect(view.getByTestId('ids').textContent).toBe('');
+
+ let finishOld!: (value: { isInsideWorktree: boolean }) => void;
+ mocks.worktreeDetectCwd.mockImplementationOnce(() => new Promise((resolve) => { finishOld = resolve; }));
+ await act(async () => { window.dispatchEvent(new Event('focus')); });
+
+ let rejectNew!: (reason: Error) => void;
+ mocks.worktreeGetForSession.mockResolvedValue({ ...meta, path: restoredPath });
+ mocks.worktreeDetectCwd.mockImplementationOnce(() => new Promise((_resolve, reject) => { rejectNew = reject; }));
+ await act(async () => { await refresh('open'); });
+ expect(view.getByTestId('ids').textContent).toBe(`open:${restoredPath}`);
+ expect(view.getByTestId('active').textContent).toBe(restoredPath);
+ expect(mocks.worktreeDetectCwd).toHaveBeenLastCalledWith({ cwd: restoredPath });
+
+ await act(async () => { finishOld({ isInsideWorktree: false }); });
+ expect(view.getByTestId('active').textContent).toBe(restoredPath);
+ expect(view.getByTestId('ids').textContent).toBe(`open:${restoredPath}`);
+ await act(async () => { rejectNew(new Error('[INTERNAL] Worktree directory probe failed')); });
+ expect(view.getByTestId('active').textContent).toBe(restoredPath);
+ expect(view.getByTestId('ids').textContent).toBe(`open:${restoredPath}`);
+
+ // A conclusive result for the new metadata must still update both views.
+ await act(async () => { window.dispatchEvent(new Event('focus')); });
+ expect(view.getByTestId('active').textContent).toBe('');
+ expect(view.getByTestId('ids').textContent).toBe('');
+ expect(mocks.worktreeDetectCwd).toHaveBeenCalledTimes(4);
+ expect(mocks.worktreeListAll).toHaveBeenCalledOnce();
+ expect(mocks.worktreeGetForSession).toHaveBeenCalledExactlyOnceWith('open');
+ },
+ );
+
it('ignores liveness reports captured before same-path restoration or recycling', async () => {
const meta = { sessionId: 'open', path: '/tmp/wt/open' };
mocks.worktreeListAll.mockResolvedValue([meta]);
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 15d31ba571..8cc488058a 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/sessionWorktreeInfo.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/sessionWorktreeInfo.ts
@@ -110,14 +110,15 @@ export function useTaskInfoWorktree(
const managed = resolveManagedWorktree(official);
const [observed, setObserved] = useState<SessionWorktreeInfo | null>(null);
const initialOfficialStillLive = !observeTelemetry || liveOfficial !== null;
- const [liveness, setLiveness] = useState({ sessionId: session.id, live: initialOfficialStillLive });
- if (liveness.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.
- setLiveness({ sessionId: session.id, live: initialOfficialStillLive });
+ const [liveness, setLiveness] = useState({ sessionId: session.id, official, live: initialOfficialStillLive });
+ const matchesSnapshot = liveness.sessionId === session.id && liveness.official === official;
+ if (!matchesSnapshot) {
+ // Route changes and same-path restoration both replace the snapshot that
+ // owns this result. Match Context's metadata identity guard so an old
+ // invalid result cannot override a new authoritative row while probing.
+ setLiveness({ sessionId: session.id, official, live: initialOfficialStillLive });
}
- const displayedOfficialStillLive = liveness.sessionId === session.id
+ const displayedOfficialStillLive = matchesSnapshot
? liveness.live
: initialOfficialStillLive;
const officialPath = official?.path ?? null;
@@ -141,14 +142,14 @@ export function useTaskInfoWorktree(
const live = await probeIsInsideWorktree(officialPath, deviceId);
if (cancelled || gen !== generation) return;
if (live === null) return; // IPC 失败不代表目录已被删除,保留上次状态。
- setLiveness({ sessionId: session.id, live });
+ setLiveness({ sessionId: session.id, official, live });
if (official) reportLiveness(official, live);
if (live) {
setObserved(null);
return;
}
} else if (gen === generation) {
- setLiveness({ sessionId: session.id, live: false });
+ setLiveness({ sessionId: session.id, official, live: false });
}
if (isRemote) {
if (!cancelled && gen === generation) setObserved(null);