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.
This commit is contained in:
Mohamed Boudra
2026-11-24 10:05:52 +01:00
parent f5e58a19c4
commit 4304f94306
2 changed files with 45 additions and 6 deletions
@@ -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);
});
@@ -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(