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 dfb3dbbefd..326bf87db4 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 @@ -1239,7 +1239,7 @@ test("unarchiving an approved workspace ignores archived unapproved records on t headRepository: "contributor/repo", }, }); - await workspaceRegistry.archive(oldUntrusted.workspaceId, ARCHIVED_AT); + await workspaceRegistry.archive(oldUntrusted.workspaceId, "2026-04-01T00:00:00.000Z"); const approved = await provisioning.createWorkspaceForWorktree({ sourceCwd, @@ -1250,7 +1250,7 @@ test("unarchiving an approved workspace ignores archived unapproved records on t baseBranch: "main", title: null, }); - await workspaceRegistry.archive(approved.workspaceId, ARCHIVED_AT); + await workspaceRegistry.archive(approved.workspaceId, "2026-04-02T00:00:00.000Z"); const archivedApproved = (await workspaceRegistry.get(approved.workspaceId))!; const restored = await provisioning.ensureWorkspaceRecordUnarchived(archivedApproved); @@ -1421,3 +1421,47 @@ test("creating a directory workspace on a pre-upgrade retained unapproved checko expect(reopened.untrustedSource).toEqual(untrustedSource); }); + +test("unarchiving an older approved workspace onto a pre-upgrade retained unapproved checkout inherits untrustedSource from the latest archived record", async () => { + const sourceCwd = path.join(tmpDir, "repo-pre-upgrade-unarchive-retained"); + const worktreePath = path.join(tmpDir, "worktree-pre-upgrade-unarchive-retained"); + mkdirSync(sourceCwd, { recursive: true }); + mkdirSync(worktreePath, { recursive: true }); + gitRoots.add(sourceCwd); + gitRoots.add(worktreePath); + + const olderApproved = await provisioning.createWorkspaceForWorktree({ + sourceCwd, + repoRoot: sourceCwd, + cwd: worktreePath, + worktreeRoot: worktreePath, + branch: "feature/older-approved", + baseBranch: "main", + title: null, + }); + await workspaceRegistry.archive(olderApproved.workspaceId, "2026-04-01T00:00:00.000Z"); + const archivedOlderApproved = (await workspaceRegistry.get(olderApproved.workspaceId))!; + + const untrustedSource = { + kind: "change_request" as const, + forge: "github", + number: 61, + headRepository: "contributor/repo", + }; + const retainedUntrusted = await provisioning.createWorkspaceForWorktree({ + sourceCwd, + repoRoot: sourceCwd, + cwd: worktreePath, + worktreeRoot: worktreePath, + branch: "contributor/pr-61", + baseBranch: "main", + title: null, + untrustedSource, + }); + await workspaceRegistry.archive(retainedUntrusted.workspaceId, "2026-04-02T00:00:00.000Z"); + + const restored = await provisioning.ensureWorkspaceRecordUnarchived(archivedOlderApproved); + + expect(restored.archivedAt).toBeNull(); + expect(restored.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 465ebe79ad..2450aab353 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 @@ -275,7 +275,7 @@ export function createWorkspaceProvisioningService(deps: { ? await readPaseoWorktreeMetadata(checkout.worktreeRoot ?? normalizedCwd) : null; const checkoutRoot = checkout.worktreeRoot ?? normalizedCwd; - const inheritedUntrustedSource = await resolveCheckoutUntrustedSource( + const { untrustedSource: inheritedUntrustedSource } = await resolveCheckoutUntrustedSource( checkoutRoot, baseMetadata?.untrustedSource, ); @@ -430,12 +430,12 @@ export function createWorkspaceProvisioningService(deps: { async function resolveCheckoutUntrustedSource( checkoutRoot: string, knownMetadataUntrustedSource?: UntrustedWorkspaceSource, - ): Promise { + ): Promise<{ hasEvidence: boolean; untrustedSource: UntrustedWorkspaceSource | undefined }> { const metadataUntrustedSource = knownMetadataUntrustedSource ?? (await readPaseoWorktreeMetadata(checkoutRoot).catch(() => null))?.untrustedSource; if (metadataUntrustedSource) { - return metadataUntrustedSource; + return { hasEvidence: true, untrustedSource: metadataUntrustedSource }; } const matchesCheckout = createRealpathAwarePathMatcher(checkoutRoot); const matching = (await workspaceRegistry.listAll()).filter((candidate) => @@ -443,49 +443,43 @@ export function createWorkspaceProvisioningService(deps: { ); const active = matching.filter((candidate) => !candidate.archivedAt); if (active.length > 0) { - return active.find((candidate) => Boolean(candidate.untrustedSource))?.untrustedSource; + return { + hasEvidence: true, + untrustedSource: 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; + .sort( + (a, b) => + (b.archivedAt ?? "").localeCompare(a.archivedAt ?? "") || + b.createdAt.localeCompare(a.createdAt), + )[0]; + return { + hasEvidence: Boolean(latestArchived), + untrustedSource: latestArchived?.untrustedSource, + }; } async function syncUnarchivedCheckoutUntrustedSource( workspace: PersistedWorkspaceRecord, ): Promise { - const checkoutRoot = workspace.worktreeRoot ?? workspace.cwd; - const metadataUntrustedSource = ( - await readPaseoWorktreeMetadata(checkoutRoot).catch(() => null) - )?.untrustedSource; - if (metadataUntrustedSource) { - return workspace.untrustedSource - ? workspace - : { ...workspace, untrustedSource: metadataUntrustedSource }; - } - const matchesCheckout = createRealpathAwarePathMatcher(checkoutRoot); - const activeOnCheckout = (await workspaceRegistry.listActive()).filter((candidate) => - matchesCheckout(candidate.worktreeRoot ?? candidate.cwd), - ); - if (activeOnCheckout.length === 0) { + const resolved = await resolveCheckoutUntrustedSource(workspace.worktreeRoot ?? workspace.cwd); + if (!resolved.hasEvidence) { return workspace; } - const activeUntrustedSource = activeOnCheckout.find((candidate) => - Boolean(candidate.untrustedSource), - )?.untrustedSource; - if (activeUntrustedSource) { + if (resolved.untrustedSource) { return workspace.untrustedSource ? workspace - : { ...workspace, untrustedSource: activeUntrustedSource }; + : { ...workspace, untrustedSource: resolved.untrustedSource }; } if (!workspace.untrustedSource) { return workspace; } - // Live active workspaces already own this checkout and it is approved: - // drop stale untrustedSource from a previous checkout that used this path. const { untrustedSource: _stale, ...approved } = workspace; return approved; }