diff --git a/packages/server/src/server/workspace-reconciliation-service.test.ts b/packages/server/src/server/workspace-reconciliation-service.test.ts index 3d0057bca..6f34b6985 100644 --- a/packages/server/src/server/workspace-reconciliation-service.test.ts +++ b/packages/server/src/server/workspace-reconciliation-service.test.ts @@ -641,7 +641,83 @@ describe("WorkspaceReconciliationService", () => { expect(workspaces.get("w1")).toMatchObject({ archivedAt, - branch: "new-branch", + branch: "old-branch", + }); + }); + + test("keeps a worktree restorable when it is archived while its checkout read is in flight", async () => { + const projectRoot = "/reconcile/archive-worktree/root"; + const worktreeCwd = "/reconcile/archive-worktree/worktrees/feature"; + const { projects, workspaces, projectRegistry, workspaceRegistry } = createTestRegistries(); + projects.set( + "p1", + createPersistedProjectRecord({ + projectId: "p1", + rootPath: projectRoot, + projectKey: deriveProjectKey({ + rootPath: projectRoot, + remoteUrl: null, + worktreeRoot: projectRoot, + mainRepoRoot: null, + }), + kind: "git", + displayName: "archive-worktree", + createdAt: timestamp, + updatedAt: timestamp, + }), + ); + workspaces.set( + "w1", + createPersistedWorkspaceRecord({ + workspaceId: "w1", + projectId: "p1", + cwd: worktreeCwd, + kind: "worktree", + displayName: "feature", + branch: "feature", + worktreeRoot: worktreeCwd, + isPaseoOwnedWorktree: true, + mainRepoRoot: projectRoot, + createdAt: timestamp, + updatedAt: timestamp, + }), + ); + const worktreeReadStarted = deferred(); + const allowWorktreeRead = deferred(); + const service = new WorkspaceReconciliationService({ + projectRegistry, + workspaceRegistry, + logger: createTestLogger(), + inspectDirectory: async () => "directory", + workspaceGitService: { + getCheckout: async (cwd) => { + if (cwd === projectRoot) { + return createCheckout(cwd, { isGit: true, currentBranch: "main", worktreeRoot: cwd }); + } + worktreeReadStarted.resolve(); + await allowWorktreeRead.promise; + // Archive deletes the worktree after archiving its record, so a read that + // lands mid-deletion no longer sees a Git checkout there. + return createCheckout(cwd); + }, + }, + }); + + const reconciliation = service.runOnce(); + await worktreeReadStarted.promise; + const archivedAt = "2025-01-02T00:00:00.000Z"; + await workspaceRegistry.archive("w1", archivedAt); + allowWorktreeRead.resolve(); + const result = await reconciliation; + + expect(result.changesApplied).toEqual([]); + expect(workspaces.get("w1")).toMatchObject({ + archivedAt, + kind: "worktree", + branch: "feature", + worktreeRoot: worktreeCwd, + isPaseoOwnedWorktree: true, + mainRepoRoot: projectRoot, }); }); diff --git a/packages/server/src/server/workspace-reconciliation-service.ts b/packages/server/src/server/workspace-reconciliation-service.ts index f1fe5c944..84a0221a7 100644 --- a/packages/server/src/server/workspace-reconciliation-service.ts +++ b/packages/server/src/server/workspace-reconciliation-service.ts @@ -396,8 +396,7 @@ export class WorkspaceReconciliationService { const workspaceCheckouts = await Promise.all( siblings.map(async (workspace) => { const current = await this.workspaceRegistry.get(workspace.workspaceId); - if (!current || current.cwd !== workspace.cwd || current.projectId !== workspace.projectId) - return null; + if (!current || !isStillReconcilable(current, workspace)) return null; return { workspace: current, checkout: await readCheckout(current.cwd) }; }), ); @@ -446,8 +445,9 @@ export class WorkspaceReconciliationService { return; let fields: Partial | null = null; await this.workspaceRegistry.update(workspace.workspaceId, (current) => { - if (current.cwd !== workspace.cwd || current.projectId !== workspace.projectId) - return current; + // Archive removes a worktree only after archiving its record, so a read + // that raced the archive may have seen the directory mid-deletion. + if (!isStillReconcilable(current, workspace)) return current; const update = reconcileWorkspacePlacement({ workspace: current, checkout: wsGit, @@ -685,3 +685,13 @@ async function mapBounded(items: T[], visit: (item: T) => Promise): Pro ); return results; } + +/** An archived record keeps the placement its restore depends on. */ +function isStillReconcilable( + current: PersistedWorkspaceRecord, + scanned: PersistedWorkspaceRecord, +): boolean { + return ( + !current.archivedAt && current.cwd === scanned.cwd && current.projectId === scanned.projectId + ); +}