diff --git a/server/src/services/online-workspace-source.ts b/server/src/services/online-workspace-source.ts index c1892888434..527588c8ec0 100644 --- a/server/src/services/online-workspace-source.ts +++ b/server/src/services/online-workspace-source.ts @@ -112,7 +112,13 @@ export async function explainOnlineWorkspaceSource(db: ReadDb, workspace: Worksp } } catch { return unproven("git_identity_unreadable"); } const captureRoot = await physicalPath(cwd), gitSources = selected.git ? [selected.git] : nestedGit; - if (!gitSources.length) return unproven(ordinaryUnproven ?? "not_git_source"); + if (!gitSources.length) { + if (ordinaryUnproven) return unproven(ordinaryUnproven); + const marker = await fs.lstat(path.join(captureRoot, ".git")).catch((error: NodeJS.ErrnoException) => { + if (error.code !== "ENOENT") throw error; return null; + }); + return unproven(marker ? "git_marker_not_repository" : "git_checkout_missing"); + } if (selected.git && selected.git.root !== captureRoot) return unproven("git_root_mismatch"); if (selected.unownedPaths.length) return unproven("unowned_path"); if (!workspace.sourceIssueId) return unproven("task_binding_missing"); diff --git a/server/src/services/remote-workspace-migration-tree.ts b/server/src/services/remote-workspace-migration-tree.ts index 8531a70b9ee..b64f77f47d8 100644 --- a/server/src/services/remote-workspace-migration-tree.ts +++ b/server/src/services/remote-workspace-migration-tree.ts @@ -246,8 +246,13 @@ export async function onlineOrdinarySourceBlockers(db: Pick, comp /** Only the maintenance entry point can acknowledge that every old deployment * has stopped. A task-level migration hold cannot fence other path aliases. */ -export async function workspaceMigrationLayoutBlockers(db: ReadDb, workspace: Workspace, writersStopped = false, onlineSource?: WorkspaceSourceScope) { - if (writersStopped) return offlineWorkspaceMigrationBlockers(db); +export type WorkspaceLayoutBlocker = { reason: string; state?: string }; +const overlapState = (root: string, physical: string) => + physical === root ? "same_directory" : physical.startsWith(`${root}/`) ? "nested_workspace" : "enclosing_workspace"; + +export async function workspaceMigrationLayoutBlockers(db: ReadDb, workspace: Workspace, writersStopped = false, + onlineSource?: WorkspaceSourceScope): Promise { + if (writersStopped) return (await offlineWorkspaceMigrationBlockers(db)).map(reason => ({ reason })); const cwd = rootOf(workspace); if (!cwd) return []; const local = await db.select({ id: executionWorkspaces.id, cwd: executionWorkspaces.cwd, @@ -261,9 +266,10 @@ export async function workspaceMigrationLayoutBlockers(db: ReadDb, workspace: Wo const physical = await physicalPath(cwd); if (onlineSource?.gitSourceRoots && physical.startsWith(`${root}/.paperclip/worktrees/`)) { const entry = await fs.lstat(physical).catch((error: NodeJS.ErrnoException) => { if (error.code !== "ENOENT") throw error; return null; }); - if (entry) return ["nested_workspace_import_required"]; + if (entry) return [{ reason: "nested_workspace_import_required" }]; } - if (!onlineSource?.workspaceIds.includes(row.id) && overlaps(root, physical)) return ["overlapping_workspace_requires_maintenance"]; + if (!onlineSource?.workspaceIds.includes(row.id) && overlaps(root, physical)) + return [{ reason: "overlapping_workspace_requires_maintenance", state: overlapState(root, physical) }]; } } const migrated = await db.select({ id: workspaceOperations.id, cwd: workspaceOperations.cwd }).from(workspaceOperations).where(and( @@ -275,7 +281,7 @@ export async function workspaceMigrationLayoutBlockers(db: ReadDb, workspace: Wo const sourcePath = await physicalPath(row.cwd); if (sourcePath.startsWith(`${root}/.paperclip/worktrees/`) && !(onlineSource?.gitSourceRoots && onlineSource.importedChildren?.some(child => child.operationId === row.id && child.sourcePath === sourcePath))) - return ["overlapping_workspace_requires_maintenance"]; + return [{ reason: "overlapping_workspace_requires_maintenance", state: "imported_child_unrecorded" }]; } return []; } diff --git a/server/src/services/remote-workspace-migration.test.ts b/server/src/services/remote-workspace-migration.test.ts index 743a99ff4dd..a1200adc41f 100644 --- a/server/src/services/remote-workspace-migration.test.ts +++ b/server/src/services/remote-workspace-migration.test.ts @@ -404,6 +404,12 @@ describe.skipIf(!support.supported)("remote workspace migration", () => { } it("requires offline maintenance for overlapping physical workspace paths", async () => { const { parent, child } = await nestedFixture(); + expect(await inventoryRemoteWorkspaceMigration(db, child)).toMatchObject({ eligible: false, + reasons: ["overlapping_workspace_requires_maintenance"], sourceScopeUnproven: "ordinary_root_unmanaged", + details: ["overlapping_workspace_requires_maintenance:enclosing_workspace"] }); + expect(await inventoryRemoteWorkspaceMigration(db, parent)).toMatchObject({ eligible: false, + reasons: ["overlapping_workspace_requires_maintenance"], sourceScopeUnproven: "ordinary_root_unmanaged", + details: ["overlapping_workspace_requires_maintenance:nested_workspace"] }); await expect(migrateRemoteWorkspace({ db, storage, ...child, apply: true })).rejects.toThrow("overlapping_workspace_requires_maintenance"); await expect(migrateRemoteWorkspace({ db, storage, ...parent, apply: true })).rejects.toThrow("overlapping_workspace_requires_maintenance"); expect(await fs.readFile(path.join(child.cwd, "child.txt"), "utf8")).toBe("saved child edits\n"); diff --git a/server/src/services/remote-workspace-migration.ts b/server/src/services/remote-workspace-migration.ts index 02c74897fb4..e3c71f80eb4 100644 --- a/server/src/services/remote-workspace-migration.ts +++ b/server/src/services/remote-workspace-migration.ts @@ -210,7 +210,11 @@ async function inspect(db: ReadDb, input: { companyId: string; workspaceId: stri filesGone = !entry && !retainedDirectory && !archivedGitSource && workspace.status === "archived" && !reasons.includes("restore_point_expired") && !reasons.includes("restore_point_invalid") && await workspaceFilesProvenGone(db, workspace); - if (entry?.isDirectory() && !entry.isSymbolicLink()) reasons.push(...await workspaceMigrationLayoutBlockers(db, workspace, input.writersStopped, input.onlineSource)); + if (entry?.isDirectory() && !entry.isSymbolicLink()) { + for (const blocker of await workspaceMigrationLayoutBlockers(db, workspace, input.writersStopped, input.onlineSource)) { + if (blocker.state) detail(blocker.reason, blocker.state); else reasons.push(blocker.reason); + } + } } const [issue] = workspace.sourceIssueId ? await db.select().from(issues).where(and(eq(issues.companyId, workspace.companyId), eq(issues.id, workspace.sourceIssueId))) : []; // Historical records still own their saved files after the task selects a @@ -412,8 +416,9 @@ export async function inventoryRemoteWorkspaceMigration(db: Db, input: { company const onlineSource = explained && !("unproven" in explained) ? explained : undefined; const onlineSourceUnproven = Boolean(workspace && explained && !onlineSource && await requiresOnlineWorkspaceSource(workspace)); const result = await inspect(db, { ...input, onlineSource, onlineSourceUnproven }); + const scopeNamed = onlineSourceUnproven || result.reasons.includes("overlapping_workspace_requires_maintenance"); return { companyId: input.companyId, workspaceId: input.workspaceId, eligible: result.reasons.length === 0, - ...(onlineSourceUnproven && explained && "unproven" in explained ? { sourceScopeUnproven: explained.unproven } : {}), + ...(scopeNamed && explained && "unproven" in explained ? { sourceScopeUnproven: explained.unproven } : {}), ...(result.details.length ? { details: result.details } : {}), alreadyMigrated: Boolean(result.authority), ...(result.filesGone ? { filesUnavailable: true } : {}), reasons: result.reasons, localPath: result.workspace.cwd ?? result.workspace.providerRef, source: result.source, operationId: typeof result.marker.operationId === "string" ? result.marker.operationId : null, diff --git a/server/src/services/remote-workspace-online-source.test.ts b/server/src/services/remote-workspace-online-source.test.ts index 64c77b5f858..807f6e466f4 100644 --- a/server/src/services/remote-workspace-online-source.test.ts +++ b/server/src/services/remote-workspace-online-source.test.ts @@ -162,11 +162,23 @@ describe.skipIf(!support.supported)("online ordinary physical source fence", () await fs.writeFile(path.join(info, "exclude"), "/.paperclip-repositories/\n"); if (objects) await fs.mkdir(path.join(f.cwd, ".git", "objects")); expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: false, - reasons: expect.arrayContaining(["online_workspace_source_scope_unproven"]) }); + reasons: expect.arrayContaining(["online_workspace_source_scope_unproven"]), sourceScopeUnproven: objects ? "git_head_missing" : "git_marker_not_repository" }); await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })).rejects.toThrow("online_workspace_source_scope_unproven"); expect(await db.select().from(workspaceOperations)).toEqual([]); expect(await fs.readFile(path.join(info, "exclude"), "utf8")).toBe("/.paperclip-repositories/\n"); }); + it("names a Git record whose unmanaged directory holds no repository", async () => { + const f = await fixture(), cwd = path.join(home, "declared-git-without-checkout"); + await fs.mkdir(cwd); await fs.writeFile(path.join(cwd, "saved.txt"), "kept\n"); + for (const alias of f.aliases.slice(1)) await db.update(executionWorkspaces).set({ providerType: "plugin" }) + .where(eq(executionWorkspaces.id, alias.workspace.id)); + await db.update(executionWorkspaces).set({ cwd, providerRef: cwd, repoUrl: "https://example.invalid/declared.git", branchName: "main" }) + .where(eq(executionWorkspaces.id, f.workspaceId)); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: false, + reasons: ["online_workspace_source_scope_unproven"], sourceScopeUnproven: "git_checkout_missing" }); + await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })).rejects.toThrow("online_workspace_source_scope_unproven"); + expect(await fs.readFile(path.join(cwd, "saved.txt"), "utf8")).toBe("kept\n"); + }); it("preserves unmanaged Git import in genuine offline mode while retaining online and drain guards", async () => { const f = await fixture(), cwd = path.join(home, "self-hosted-repository"), repoUrl = "https://example.invalid/self-hosted.git"; await fs.mkdir(cwd); await git(cwd, "init", "-q", "-b", "main");