From 4304f9430615142ee767325caebdc06f0289b5c6 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Tue, 24 Nov 2026 10:05:52 +0100 Subject: [PATCH] fix(server): preserve block when opening a pre-upgrade retained checkout Opening a pre-upgrade retained checkout directory whose last unapproved workspace was already archived before this change has no active workspace on the path and no `untrustedSource` in `paseo/worktree.json`. In `resolveCheckoutUntrustedSource`, prefer `paseo/worktree.json`, then active workspaces on the checkout when any exist (so a fresh approved worktree at a reused path stays approved), and only when zero active workspaces exist on the directory fall back to the most recently archived record for that path. --- .../workspace-provisioning-service.test.ts | 31 +++++++++++++++++++ .../workspace-provisioning-service.ts | 20 ++++++++---- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/server/src/server/session/workspace-provisioning/workspace-provisioning-service.test.ts b/packages/server/src/server/session/workspace-provisioning/workspace-provisioning-service.test.ts index 3e1a788cff..dfb3dbbefd 100644 --- a/packages/server/src/server/session/workspace-provisioning/workspace-provisioning-service.test.ts +++ b/packages/server/src/server/session/workspace-provisioning/workspace-provisioning-service.test.ts @@ -1390,3 +1390,34 @@ test("unarchiving an old unapproved workspace onto a live approved checkout drop expect(unarchived.untrustedSource).toBeUndefined(); expect((await workspaceRegistry.get(oldUntrusted.workspaceId))?.untrustedSource).toBeUndefined(); }); + +test("creating a directory workspace on a pre-upgrade retained unapproved checkout inherits untrustedSource when no active workspace owns the path", async () => { + const sourceCwd = path.join(tmpDir, "repo-pre-upgrade-retained-dir"); + const worktreePath = path.join(tmpDir, "worktree-pre-upgrade-retained-dir"); + mkdirSync(sourceCwd, { recursive: true }); + mkdirSync(worktreePath, { recursive: true }); + gitRoots.add(sourceCwd); + gitRoots.add(worktreePath); + + const untrustedSource = { + kind: "change_request" as const, + forge: "github", + number: 60, + headRepository: "contributor/repo", + }; + const retainedUntrusted = await provisioning.createWorkspaceForWorktree({ + sourceCwd, + repoRoot: sourceCwd, + cwd: worktreePath, + worktreeRoot: worktreePath, + branch: "contributor/pr-60", + baseBranch: "main", + title: null, + untrustedSource, + }); + await workspaceRegistry.archive(retainedUntrusted.workspaceId, ARCHIVED_AT); + + const reopened = await provisioning.createWorkspaceForDirectory(worktreePath); + + expect(reopened.untrustedSource).toEqual(untrustedSource); +}); diff --git a/packages/server/src/server/session/workspace-provisioning/workspace-provisioning-service.ts b/packages/server/src/server/session/workspace-provisioning/workspace-provisioning-service.ts index e89856ef4f..465ebe79ad 100644 --- a/packages/server/src/server/session/workspace-provisioning/workspace-provisioning-service.ts +++ b/packages/server/src/server/session/workspace-provisioning/workspace-provisioning-service.ts @@ -438,12 +438,20 @@ export function createWorkspaceProvisioningService(deps: { return metadataUntrustedSource; } const matchesCheckout = createRealpathAwarePathMatcher(checkoutRoot); - const active = await workspaceRegistry.listActive(); - return active.find( - (candidate) => - Boolean(candidate.untrustedSource) && - matchesCheckout(candidate.worktreeRoot ?? candidate.cwd), - )?.untrustedSource; + const matching = (await workspaceRegistry.listAll()).filter((candidate) => + matchesCheckout(candidate.worktreeRoot ?? candidate.cwd), + ); + const active = matching.filter((candidate) => !candidate.archivedAt); + if (active.length > 0) { + return active.find((candidate) => Boolean(candidate.untrustedSource))?.untrustedSource; + } + // No active workspace owns this existing directory yet (for example, a + // checkout retained on disk from before metadata persisted untrustedSource): + // fall back to the most recently archived record for this path. + const latestArchived = matching + .filter((candidate) => Boolean(candidate.archivedAt)) + .sort((a, b) => (b.archivedAt ?? "").localeCompare(a.archivedAt ?? ""))[0]; + return latestArchived?.untrustedSource; } async function syncUnarchivedCheckoutUntrustedSource(