Keep an archived worktree restorable when reconciliation races its removal (#9753)

Archiving a worktree archives its record, then deletes the directory. A
reconciliation pass that listed the workspace before the archive went on
to read its checkout while the directory was being deleted, saw no Git
checkout, and wrote kind "directory" onto the archived record. Undo then
refused to recreate the worktree: "The archived workspace directory no
longer exists and cannot be recreated."

Reconciliation now skips a record that is archived when it re-reads it
and when it writes the update, as it already does when listing.

Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
This commit is contained in:
paseo-bot[bot] authored and GitHub committed 2026-11-15 09:34:41 +00:00
1 parent a5015dba80
commit fe59a6c1a2
2 files changed
+91 -5

No files matched your search

@@ -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,
});
});
@@ -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<MutableWorkspacePlacement> | 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<T, R>(items: T[], visit: (item: T) => Promise<R>): 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
);
}