fix(server): never propagate archived untrustedSource onto fresh checkouts

Both archive metadata backfill and directory workspace creation previously consulted already-archived records matching a path, allowing an old archived unapproved record to copy `untrustedSource` onto a fresh approved worktree that reused the same slug path.

Ignore `untrustedSource` on already-archived records during archive target resolution and metadata backfill (only backfill when active workspaces are being archived), and share one `resolveCheckoutUntrustedSource` rule across `createWorkspaceForDirectory` and `ensureWorkspaceRecordUnarchived` that checks `paseo/worktree.json` first and falls back only to active workspaces.
This commit is contained in:
Mohamed Boudra
2026-11-24 09:54:22 +01:00
parent e3e001f27f
commit acbe2409c1
4 changed files with 139 additions and 23 deletions
@@ -1307,3 +1307,43 @@ test("unarchiving an approved workspace onto a retained unapproved checkout inhe
expect(restored.archivedAt).toBeNull();
expect(restored.untrustedSource).toEqual(untrustedSource);
});
test("creating a directory workspace on a fresh checkout ignores archived unapproved records on the same path", async () => {
const sourceCwd = path.join(tmpDir, "repo-dir-reused-path");
const worktreePath = path.join(tmpDir, "worktree-dir-reused-path");
mkdirSync(sourceCwd, { recursive: true });
mkdirSync(worktreePath, { recursive: true });
gitRoots.add(sourceCwd);
gitRoots.add(worktreePath);
const oldUntrusted = await provisioning.createWorkspaceForWorktree({
sourceCwd,
repoRoot: sourceCwd,
cwd: worktreePath,
worktreeRoot: worktreePath,
branch: "contributor/pr-58",
baseBranch: "main",
title: null,
untrustedSource: {
kind: "change_request",
forge: "github",
number: 58,
headRepository: "contributor/repo",
},
});
await workspaceRegistry.archive(oldUntrusted.workspaceId, ARCHIVED_AT);
await provisioning.createWorkspaceForWorktree({
sourceCwd,
repoRoot: sourceCwd,
cwd: worktreePath,
worktreeRoot: worktreePath,
branch: "feature/fresh-approved",
baseBranch: "main",
title: null,
});
const extra = await provisioning.createWorkspaceForDirectory(worktreePath);
expect(extra.untrustedSource).toBeUndefined();
});
@@ -275,7 +275,7 @@ export function createWorkspaceProvisioningService(deps: {
? await readPaseoWorktreeMetadata(checkout.worktreeRoot ?? normalizedCwd)
: null;
const checkoutRoot = checkout.worktreeRoot ?? normalizedCwd;
const inheritedUntrustedSource = await resolveDirectoryUntrustedSource(
const inheritedUntrustedSource = await resolveCheckoutUntrustedSource(
checkoutRoot,
baseMetadata?.untrustedSource,
);
@@ -427,15 +427,19 @@ export function createWorkspaceProvisioningService(deps: {
).workspaceId;
}
async function resolveDirectoryUntrustedSource(
async function resolveCheckoutUntrustedSource(
checkoutRoot: string,
metadataUntrustedSource: UntrustedWorkspaceSource | undefined,
knownMetadataUntrustedSource?: UntrustedWorkspaceSource,
): Promise<UntrustedWorkspaceSource | undefined> {
const metadataUntrustedSource =
knownMetadataUntrustedSource ??
(await readPaseoWorktreeMetadata(checkoutRoot).catch(() => null))?.untrustedSource;
if (metadataUntrustedSource) {
return metadataUntrustedSource;
}
const matchesCheckout = createRealpathAwarePathMatcher(checkoutRoot);
return (await workspaceRegistry.listAll()).find(
const active = await workspaceRegistry.listActive();
return active.find(
(candidate) =>
Boolean(candidate.untrustedSource) &&
matchesCheckout(candidate.worktreeRoot ?? candidate.cwd),
@@ -448,20 +452,9 @@ export function createWorkspaceProvisioningService(deps: {
if (workspace.untrustedSource) {
return workspace;
}
const checkoutRoot = workspace.worktreeRoot ?? workspace.cwd;
const metadataUntrustedSource = (
await readPaseoWorktreeMetadata(checkoutRoot).catch(() => null)
)?.untrustedSource;
if (metadataUntrustedSource) {
return { ...workspace, untrustedSource: metadataUntrustedSource };
}
const matchesCheckout = createRealpathAwarePathMatcher(checkoutRoot);
const active = await workspaceRegistry.listActive();
const inheritedUntrustedSource = active.find(
(candidate) =>
Boolean(candidate.untrustedSource) &&
matchesCheckout(candidate.worktreeRoot ?? candidate.cwd),
)?.untrustedSource;
const inheritedUntrustedSource = await resolveCheckoutUntrustedSource(
workspace.worktreeRoot ?? workspace.cwd,
);
return inheritedUntrustedSource
? { ...workspace, untrustedSource: inheritedUntrustedSource }
: workspace;
@@ -588,6 +588,85 @@ describe("archiveByScope", () => {
expect(readFileSync(marker, "utf8")).toBe("ran");
});
test("retrying archive on an already-archived unapproved workspace does not mark a fresh worktree at the same path", async () => {
const { tempDir, repoDir } = createGitRepo();
const markerName = "retry-archived-reused-path.log";
const marker = path.join(repoDir, markerName);
writeFileSync(
path.join(repoDir, "paseo.json"),
JSON.stringify({
worktree: {
teardown: [
`node -e "require('fs').writeFileSync(process.env.PASEO_SOURCE_CHECKOUT_PATH + '/${markerName}', 'ran')"`,
],
},
}),
);
execFileSync("git", ["add", "."], { cwd: repoDir, stdio: "pipe" });
execFileSync("git", ["-c", "commit.gpgsign=false", "commit", "-m", "retry archived"], {
cwd: repoDir,
stdio: "pipe",
});
const paseoHome = path.join(tempDir, ".paseo");
const worktree = await createPaseoOwnedWorktree(repoDir, paseoHome, "reused-after-retry");
const oldArchivedId = "ws-old-archived-unapproved";
const freshWorkspaceId = "ws-fresh-approved";
const deps = createArchiveDeps({
paseoHome,
activeWorkspaces: [
{
workspaceId: freshWorkspaceId,
cwd: worktree.worktreePath,
kind: "worktree",
worktreeRoot: worktree.worktreePath,
isPaseoOwnedWorktree: true,
mainRepoRoot: repoDir,
},
],
});
deps.getWorkspace = async (workspaceId) =>
workspaceId === oldArchivedId
? {
workspaceId: oldArchivedId,
projectId: "proj-1",
cwd: worktree.worktreePath,
kind: "worktree",
worktreeRoot: worktree.worktreePath,
isPaseoOwnedWorktree: true,
mainRepoRoot: repoDir,
untrustedSource: {
kind: "change_request",
forge: "github",
number: 42,
headRepository: "contributor/paseo",
},
archivedAt: "2026-11-24T00:00:00.000Z",
createdAt: "2026-11-24T00:00:00.000Z",
updatedAt: "2026-11-24T00:00:00.000Z",
}
: null;
deps.isCheckoutUntrusted = async () => true;
const retry = await archiveByScope(deps, {
scope: { kind: "workspace", workspaceId: oldArchivedId },
requestId: "req-retry-archived",
});
expect(retry.archivedWorkspaceIds).toEqual([]);
expect(retry.removedDirectory).toBe(false);
expect(
(await readPaseoWorktreeMetadata(worktree.worktreePath))?.untrustedSource,
).toBeUndefined();
const fresh = await archiveByScope(deps, {
scope: { kind: "workspace", workspaceId: freshWorkspaceId },
requestId: "req-archive-fresh",
});
expect(fresh.archivedWorkspaceIds).toEqual([freshWorkspaceId]);
expect(fresh.removedDirectory).toBe(true);
expect(existsSync(worktree.worktreePath)).toBe(false);
expect(readFileSync(marker, "utf8")).toBe("ran");
});
test("workspace scope skips teardown when retained checkout metadata carries untrustedSource", async () => {
const { tempDir, repoDir } = createGitRepo();
const markerName = "retained-metadata-teardown.log";
@@ -354,7 +354,10 @@ async function resolveArchiveTarget(
return {
backing: {
...backing,
untrustedSource: firstUntrustedSource(backing.untrustedSource, activeOnCheckout),
untrustedSource: firstUntrustedSource(
isArchived ? null : backing.untrustedSource,
activeOnCheckout,
),
},
teardownTargets: isArchived ? [] : [{ workspaceId, cwd: record.cwd }],
checkoutWorkspaceIds,
@@ -384,7 +387,7 @@ async function resolveArchiveTarget(
return {
backing: {
...resolvedBacking,
untrustedSource: firstUntrustedSource(resolvedBacking.untrustedSource, targetWorkspaces),
untrustedSource: firstUntrustedSource(null, targetWorkspaces),
},
teardownTargets: targetWorkspaces.map((workspace) => ({
workspaceId: workspace.workspaceId,
@@ -659,9 +662,10 @@ async function resolveCheckoutTeardownPlan(
const teardownGate = checkoutUntrusted
? { allowed: [] as Array<{ workspaceId: string; cwd: string }>, blockedSource: null }
: await filterAllowedTeardownTargets(dependencies, teardownTargets, checkoutWorkspaceIds);
const untrustedSourceToPersist = metadataUntrustedSource
? null
: (backing.untrustedSource ?? teardownGate.blockedSource);
const untrustedSourceToPersist =
teardownTargets.length === 0 || metadataUntrustedSource
? null
: (backing.untrustedSource ?? teardownGate.blockedSource);
if (untrustedSourceToPersist) {
// Backfill checkout metadata before removal so pre-upgrade unapproved
// checkouts keep their block if the directory is retained on disk.