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): atomically replace pooled worktree records

DavidShen01c7d12c3517
diff --git a/apps/desktop/src/main/__tests__/worktreePoolSafety.test.ts b/apps/desktop/src/main/__tests__/worktreePoolSafety.test.ts
index 6226b9ed69..9dbdda6000 100644
--- a/apps/desktop/src/main/__tests__/worktreePoolSafety.test.ts
+++ b/apps/desktop/src/main/__tests__/worktreePoolSafety.test.ts
@@ -45,6 +45,10 @@ vi.mock('../worktree/worktreeStore', () => ({
getAll: () => [...storeMap.values()],
getAllPaths: () => [...storeMap.values()].map((m) => m.path),
set: async (id: string, meta: WorktreeMeta) => { storeMap.set(id, meta); },
+ replace: async (previousId: string, id: string, meta: WorktreeMeta) => {
+ if (previousId !== id) storeMap.delete(previousId);
+ storeMap.set(id, meta);
+ },
del: (sessionId: string) => storeMap.delete(sessionId),
}));
diff --git a/apps/desktop/src/main/__tests__/worktreeStore.test.ts b/apps/desktop/src/main/__tests__/worktreeStore.test.ts
index e146040568..683b2cf55e 100644
--- a/apps/desktop/src/main/__tests__/worktreeStore.test.ts
+++ b/apps/desktop/src/main/__tests__/worktreeStore.test.ts
@@ -106,4 +106,25 @@ describe('worktreeStore', () => {
expect(storesByName.get('worktrees')?.worktrees).toEqual({ [meta.sessionId]: meta });
expect(storesByName.get('worktree-safe-directory-cleanup')).toEqual({ pending: ['/deferred'] });
});
+
+ it('replaces pooled generations atomically in the registry', async () => {
+ const store = await import('../worktree/worktreeStore');
+ const previous: WorktreeMeta = {
+ sessionId: 'previous', name: 'old', path: '/repo/.xdt-worktrees/old',
+ baseRepo: '/repo', branch: 'xdt/old', sourceBranch: 'main',
+ createdAt: '2026-05-26T00:00:00.000Z', ephemeral: true,
+ };
+ const next: WorktreeMeta = {
+ ...previous, sessionId: 'next', name: 'new', path: '/repo/.xdt-worktrees/new',
+ branch: 'xdt/new', createdAt: '2026-05-26T00:01:00.000Z',
+ };
+
+ await store.set(previous.sessionId, previous);
+ setWorktreePathInDbMock.mockClear();
+ await store.replace(previous.sessionId, next.sessionId, next);
+
+ expect(store.get(previous.sessionId)).toBeNull();
+ expect(store.get(next.sessionId)).toEqual(next);
+ expect(setWorktreePathInDbMock).toHaveBeenCalledWith(next.sessionId, next.path);
+ });
});
diff --git a/apps/desktop/src/main/worktree/WorktreePool.ts b/apps/desktop/src/main/worktree/WorktreePool.ts
index 47d52539df..97bba130f9 100644
--- a/apps/desktop/src/main/worktree/WorktreePool.ts
+++ b/apps/desktop/src/main/worktree/WorktreePool.ts
@@ -106,8 +106,7 @@ export async function acquireWorktree(
createdAt: new Date().toISOString(),
generation: randomUUID(),
};
- await store.set(req.sessionId, meta);
- if (entry.meta.sessionId !== req.sessionId) await store.del(entry.meta.sessionId);
+ await store.replace(entry.meta.sessionId, req.sessionId, meta);
log.info(
`[WorktreePool] reusing pooled worktree at ${meta.path} for session ${req.sessionId}`,
diff --git a/apps/desktop/src/main/worktree/worktreeStore.ts b/apps/desktop/src/main/worktree/worktreeStore.ts
index 1fc0ab4797..9561f0eee6 100644
--- a/apps/desktop/src/main/worktree/worktreeStore.ts
+++ b/apps/desktop/src/main/worktree/worktreeStore.ts
@@ -208,6 +208,27 @@ export async function del(sessionId: string): Promise<void> {
await mutateRegistry((map) => { delete map[sessionId]; });
}
+/** Atomically replace a pooled registration under the registry lock. */
+export async function replace(
+ previousSessionId: string,
+ sessionId: string,
+ meta: WorktreeMeta,
+): Promise<void> {
+ if (!sessionId) throw new Error('worktreeStore.replace: sessionId is required');
+ await mutateRegistry((map) => {
+ if (previousSessionId && previousSessionId !== sessionId) delete map[previousSessionId];
+ map[sessionId] = meta;
+ });
+ try {
+ await setWorktreePathInDb(sessionId, meta.path);
+ } catch (err) {
+ log.warn(
+ `[worktreeStore] DB sync failed for session ${sessionId}:`,
+ err instanceof Error ? err.message : String(err),
+ );
+ }
+}
+
async function mutateRegistry(mutate: (map: Record<string, WorktreeMeta>) => void): Promise<void> {
const uid = typeof process.getuid === 'function' ? process.getuid() : 0;
await withCrossProcessLock(path.join(os.tmpdir(), `cindy-worktree-registry-${uid}.lock`),