diff --git a/server/src/services/remote-workspace-migration-tree.ts b/server/src/services/remote-workspace-migration-tree.ts index 78e43820233..8531a70b9ee 100644 --- a/server/src/services/remote-workspace-migration-tree.ts +++ b/server/src/services/remote-workspace-migration-tree.ts @@ -94,9 +94,10 @@ export async function inspectOnlineOrdinarySource(db: ReadDb, workspace: Workspa function ordinaryMemberUnproven(row: { cwd: string | null; providerRef: string | null; providerType: string; repoUrl: string | null; branchName: string | null; sourceIssueId: string | null; strategyType: string; mode: string; status: string; - metadata: Record | null }, root: string, isolatedIssueId: string | null) { - if (row.cwd?.startsWith(`${root}/`)) return "ordinary_member_nested"; - if (row.cwd !== root || row.providerRef && row.providerRef !== root) return "ordinary_member_locator_mismatch"; + metadata: Record | null }, physical: string[], root: string, isolatedIssueId: string | null) { + if (physical.some(cwd => cwd.startsWith(`${root}/`))) return "ordinary_member_nested"; + if (physical.some(cwd => root.startsWith(`${cwd}/`))) return "ordinary_member_encloses_root"; + if (!row.cwd || physical.some(cwd => cwd !== root)) return "ordinary_member_locator_mismatch"; if (row.providerType !== "local_fs" || row.repoUrl || row.branchName) return "ordinary_member_git"; if (!row.sourceIssueId || isolatedIssueId && row.sourceIssueId !== isolatedIssueId) return "ordinary_member_task_binding"; if (row.strategyType !== "project_primary" || !["isolated_workspace", "shared_workspace"].includes(row.mode)) return "ordinary_member_strategy_unsupported"; @@ -142,7 +143,7 @@ export async function explainOnlineOrdinarySource(db: ReadDb, workspace: Workspa let physical: string[]; try { physical = await Promise.all(paths.map(physicalPath)); } catch { return unproven("member_unreadable"); } if (!physical.some(cwd => overlaps(root, cwd))) continue; - const member = ordinaryMemberUnproven(row, root, isolated ? workspace.sourceIssueId : null); + const member = ordinaryMemberUnproven(row, physical, root, isolated ? workspace.sourceIssueId : null); if (member) return unproven(member); aliases.push(row); } diff --git a/server/src/services/remote-workspace-online-source.test.ts b/server/src/services/remote-workspace-online-source.test.ts index d0196e3e4f3..64c77b5f858 100644 --- a/server/src/services/remote-workspace-online-source.test.ts +++ b/server/src/services/remote-workspace-online-source.test.ts @@ -134,6 +134,28 @@ describe.skipIf(!support.supported)("online ordinary physical source fence", () expect(await fs.lstat(path.join(targetDir, "host-only.txt")).catch(() => null)).toBeNull(); expect(await git(home, "rev-parse", "HEAD")).toBe(head); }); + it.each(["trailing", "symlink"] as const)("imports a managed directory whose alias record spells the same folder differently (%s)", async spelling => { + const f = await fixture(), alias = f.aliases[1].workspace, link = path.join(home, "managed-alias"); + if (spelling === "symlink") await fs.symlink(f.cwd, link); + const cwd = spelling === "trailing" ? `${f.cwd}/` : link; + await db.update(executionWorkspaces).set({ cwd, providerRef: cwd }).where(eq(executionWorkspaces.id, alias.id)); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: true, reasons: [] }); + expect((await inspectOnlineWorkspaceSource(db, await stored(f.workspaceId)))?.workspaceIds).toContain(alias.id); + await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })).resolves.toMatchObject({ applied: true }); + expect(readRemoteWorkspaceAuthority((await stored(alias.id)).metadata)).toBeNull(); + expect(await fs.readFile(path.join(f.cwd, "saved.txt"), "utf8")).toBe("saved legacy edits\n"); + }); + it.each([["enclosing", "ordinary_member_encloses_root"], ["elsewhere", "ordinary_member_locator_mismatch"]] as const)( + "keeps a managed directory with an %s alias locator blocked and names it", async (shape, code) => { + const f = await fixture(), alias = f.aliases[1].workspace, other = path.join(home, "elsewhere"); + await fs.mkdir(other); + await db.update(executionWorkspaces).set(shape === "enclosing" ? { cwd: path.dirname(f.cwd), providerRef: path.dirname(f.cwd) } + : { cwd: f.cwd, providerRef: other }).where(eq(executionWorkspaces.id, alias.id)); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: false, + reasons: expect.arrayContaining(["online_workspace_source_scope_unproven"]), sourceScopeUnproven: code }); + await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })).rejects.toThrow("online_workspace_source_scope_unproven"); + expect(await db.select().from(workspaceOperations)).toEqual([]); + }); it.each([false, true])("does not admit a Git fragment as an ordinary migration source (objects: %s)", async objects => { const f = await fixture(), info = path.join(f.cwd, ".git", "info"); await fs.mkdir(info, { recursive: true });