diff --git a/server/src/services/online-durable-data-migration.test.ts b/server/src/services/online-durable-data-migration.test.ts index 2b30b918d77..ac7dc188ba8 100644 --- a/server/src/services/online-durable-data-migration.test.ts +++ b/server/src/services/online-durable-data-migration.test.ts @@ -407,6 +407,15 @@ describe("online durable-data migration pass", () => { expect(migrateWorkspace.mock.calls[0]).not.toEqual([expect.objectContaining({ writersStopped: expect.anything() })]); }); + it("counts a workspace moved without files as moved and names it separately", async () => { + const migrateWorkspace = vi.fn(async (input: { workspaceId: string }) => ({ alreadyMigrated: true, reasons: [], + ...(input.workspaceId === "w2" ? { filesUnavailable: true } : {}) })); + const fixture = owners({ localWorkspaces: async () => [workspace(1), workspace(2)], migrateWorkspace }); + const result = await runOnlineDurableDataMigration(db, { budgetMs: 60_000 }, fixture.owners); + expect(result.counts).toMatchObject({ workspaces: 2, workspacesFilesUnavailable: 1 }); + expect(Object.keys(result.deferred).filter(key => key.startsWith("workspaces:"))).toEqual([]); + }); + it("defers busy and unsupported workspaces with their owner reasons and does not force them", async () => { const reasons: Record = { w1: ["active_execution"], w2: ["task_binding_required", "workspace_provider_unsupported"], w3: [] }; const migrateWorkspace = vi.fn(async () => ({ alreadyMigrated: true, reasons: [] })); diff --git a/server/src/services/online-durable-data-migration.ts b/server/src/services/online-durable-data-migration.ts index 69e1f83949e..a56bbc1d568 100644 --- a/server/src/services/online-durable-data-migration.ts +++ b/server/src/services/online-durable-data-migration.ts @@ -45,7 +45,7 @@ type Owners = { localWorkspaces: typeof listLocalWorkspaces; inventoryWorkspace: (db: Db, input: { companyId: string; workspaceId: string }) => Promise<{ reasons: string[]; sourceScopeUnproven?: string; details?: string[] }>; migrateWorkspace: (input: { db: Db; storage: StorageService; companyId: string; workspaceId: string; apply: true; - signal: AbortSignal; sourceLockWaitMs: number }) => Promise<{ alreadyMigrated: boolean; reasons: string[] }>; + signal: AbortSignal; sourceLockWaitMs: number }) => Promise<{ alreadyMigrated: boolean; reasons: string[]; filesUnavailable?: boolean }>; nativeHarnessBackups: typeof prestageNativeHarnessBackups; nativeControllers: typeof prestageNativeControllers; runEvidencePage: typeof migrateRunEvidencePage; @@ -267,6 +267,7 @@ async function migrate(db: Db, input: { budgetMs: number; now?: () => number }, workspaceOwner = tracked; const result = await untilAborted(work, signal); reasons = result.alreadyMigrated ? [] : result.reasons.length ? result.reasons : ["not_migrated"]; + if (result.filesUnavailable) add(counts, "workspacesFilesUnavailable"); } } catch (error) { if (signal.aborted) throw error; diff --git a/server/src/services/remote-workspace-lifecycle.ts b/server/src/services/remote-workspace-lifecycle.ts index 6ae4bbe562b..8303a23a084 100644 --- a/server/src/services/remote-workspace-lifecycle.ts +++ b/server/src/services/remote-workspace-lifecycle.ts @@ -2,6 +2,7 @@ import { and, eq, sql } from "drizzle-orm"; import { environmentLeases, heartbeatRuns, executionWorkspaces, executionWorkspaceSnapshots, type Db } from "@paperclipai/db"; import { readRemoteWorkspaceAuthority, remoteWorkspaceRunReferenceSchema, type EnvironmentLease } from "@paperclipai/shared"; import { hasRemoteTerminationReceipt } from "./remote-execution-termination.js"; +import { readWorkspaceFilesUnavailable, WORKSPACE_FILES_UNAVAILABLE_MESSAGE } from "./workspace-files-unavailable.js"; type Workspace = Pick; type Transaction = Parameters[0]>[0]; @@ -65,6 +66,9 @@ export function matchPreparedRemoteWorkspaceSource (await execute("git", ["-C", cwd, ...args], { env: { ...process.env, @@ -81,6 +83,99 @@ describe.skipIf(!support.supported)("remote workspace migration", () => { return { companyId, workspaceId, issueId: issue!.id, cwd }; } async function workspace(id: string) { return (await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, id)))[0]!; } + async function archivedGitWorkspaceWithoutFiles() { + const f = await seed(), repository = path.join(root, "origin-repository"); await fs.mkdir(repository); + await db.update(executionWorkspaces).set({ status: "archived", closedAt: new Date(), providerType: "git_worktree", + strategyType: "git_worktree", repoUrl: pathToFileURL(repository).href, branchName: "task-branch", + metadata: { retainedNote: "keep", gitBranchOwnershipVersion: 1 } }).where(eq(executionWorkspaces.id, f.workspaceId)); + await fs.rm(f.cwd, { recursive: true }); + return { ...f, repository }; + } + async function expectFilesUnavailable(f: { companyId: string; workspaceId: string; cwd: string }) { + const row = await workspace(f.workspaceId); + expect(row).toMatchObject({ status: "archived", providerType: "cloud_sandbox", providerRef: null, cwd: `/workspaces/${f.workspaceId}` }); + expect(row.metadata).toMatchObject({ retainedNote: "keep", workspaceFilesUnavailable: { reason: "files_missing_at_migration", localPath: f.cwd } }); + expect(readRemoteWorkspaceAuthority(row.metadata)).toMatchObject({ snapshotId: null, location: null, writerRunId: null, dirty: false }); + expect(await db.select().from(executionWorkspaceSnapshots).where(eq(executionWorkspaceSnapshots.executionWorkspaceId, f.workspaceId))).toHaveLength(0); + expect(await db.select().from(activityLog).where(and(eq(activityLog.companyId, f.companyId), + eq(activityLog.action, "workspace.files_unavailable_at_migration")))).toEqual([expect.objectContaining({ + entityType: "execution_workspace", entityId: f.workspaceId, details: expect.objectContaining({ localPath: f.cwd }) })]); + expect(await inspectRemoteWorkspacePersistence(db, row)).toMatchObject({ ready: false, reason: WORKSPACE_FILES_UNAVAILABLE_MESSAGE }); + expect(await cleanupRemoteWorkspaceAllocation({ db, workspace: row, destroyLease: async () => { throw new Error("no allocation"); } })) + .toEqual({ cleaned: true, warnings: [] }); + } + it("moves an archived workspace whose files are already gone online without a snapshot", async () => { + const f = await archivedGitWorkspaceWithoutFiles(); + expect((await inventoryRemoteWorkspaceMigration(db, f)).reasons) + .toEqual(expect.arrayContaining(["archived_git_source_unavailable", "local_source_unsafe_or_missing"])); + await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })) + .rejects.toThrow(/archived_git_source_unavailable.*local_source_unsafe_or_missing|local_source_unsafe_or_missing.*archived_git_source_unavailable/); + await fs.rm(f.repository, { recursive: true }); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: true, filesUnavailable: true, reasons: [] }); + expect(await migrateRemoteWorkspace({ db, storage, ...f, apply: true })).toMatchObject({ applied: true, filesUnavailable: true }); + await expectFilesUnavailable(f); + expect(await migrateRemoteWorkspace({ db, storage, ...f, apply: true })).toMatchObject({ applied: true, replayed: true }); + expect(await db.select().from(activityLog).where(eq(activityLog.action, "workspace.files_unavailable_at_migration"))).toHaveLength(1); + }); + it("moves an archived workspace whose files are already gone during offline migration", async () => { + vi.stubEnv("PAPERCLIP_EPHEMERAL_MIGRATION", "true"); + const f = await seed(); + await db.update(executionWorkspaces).set({ status: "archived", closedAt: new Date() }).where(eq(executionWorkspaces.id, f.workspaceId)); + await fs.rm(f.cwd, { recursive: true }); + expect(await migrateWorkspaceTrees(db, { storage, signal: new AbortController().signal, phase: () => {} })).toBe(1); + await expectFilesUnavailable(f); + }); + it("moves an archived project task workspace whose repository cache is gone too", async () => { + vi.stubEnv("PAPERCLIP_HOME", await fs.realpath(root)); + const f = await seed(), [project] = await db.insert(projects).values({ companyId: f.companyId, name: "Gone project" }).returning(); + const managed = resolveManagedProjectWorkspaceDir({ companyId: f.companyId, projectId: project!.id, repoName: "repo" }); + const cwd = path.join(managed, ".paperclip", "worktrees", "task-branch"); + await db.update(executionWorkspaces).set({ status: "archived", closedAt: new Date(), providerType: "git_worktree", strategyType: "git_worktree", + projectId: project!.id, repoUrl: "https://example.test/acme/repo.git", branchName: "task-branch", cwd, providerRef: cwd, + metadata: { retainedNote: "keep", gitBranchOwnershipVersion: 1, createdByRuntime: true } }).where(eq(executionWorkspaces.id, f.workspaceId)); + await fs.rm(f.cwd, { recursive: true }); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: true, filesUnavailable: true, reasons: [] }); + expect(await migrateRemoteWorkspace({ db, storage, ...f, apply: true })).toMatchObject({ applied: true, filesUnavailable: true }); + await expectFilesUnavailable({ ...f, cwd }); + }); + it("keeps an archived workspace blocked while its repository at a plain local path still exists", async () => { + const f = await archivedGitWorkspaceWithoutFiles(); + await db.update(executionWorkspaces).set({ repoUrl: f.repository }).where(eq(executionWorkspaces.id, f.workspaceId)); + const inventory = await inventoryRemoteWorkspaceMigration(db, f); + expect(inventory).toMatchObject({ eligible: false }); + expect(inventory).not.toHaveProperty("filesUnavailable"); + await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })).rejects.toThrow("local_source_unsafe_or_missing"); + expect(readRemoteWorkspaceAuthority((await workspace(f.workspaceId)).metadata)).toBeNull(); + await fs.rm(f.repository, { recursive: true }); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: true, filesUnavailable: true, reasons: [] }); + }); + it("keeps an archived workspace blocked when it still has a saved file reference", async () => { + const f = await archivedGitWorkspaceWithoutFiles(); await fs.rm(f.repository, { recursive: true }); + for (const metadata of [{ gitCacheRestoration: { schema: "paperclip.git-cache-restoration.v2", broken: true } }, + { isolatedTaskDirectoryRetention: { schema: "unknown" } }]) { + await db.update(executionWorkspaces).set({ metadata: { retainedNote: "keep", gitBranchOwnershipVersion: 1, ...metadata } }) + .where(eq(executionWorkspaces.id, f.workspaceId)); + const inventory = await inventoryRemoteWorkspaceMigration(db, f); + expect(inventory).toMatchObject({ eligible: false }); + expect(inventory).not.toHaveProperty("filesUnavailable"); + expect(inventory.reasons).toContain("local_source_unsafe_or_missing"); + await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })).rejects.toThrow("local_source_unsafe_or_missing"); + } + expect(readRemoteWorkspaceAuthority((await workspace(f.workspaceId)).metadata)).toBeNull(); + }); + it("keeps workspaces blocked when their files may still exist", async () => { + const f = await seed(), elsewhere = path.join(root, "elsewhere"); + await db.update(executionWorkspaces).set({ status: "archived", closedAt: new Date() }).where(eq(executionWorkspaces.id, f.workspaceId)); + await fs.rename(f.cwd, elsewhere); await fs.symlink(elsewhere, f.cwd); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: false, reasons: ["local_source_unsafe_or_missing"] }); + await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })).rejects.toThrow("local_source_unsafe_or_missing"); + await fs.rm(f.cwd); + await db.update(executionWorkspaces).set({ status: "idle", closedAt: null }).where(eq(executionWorkspaces.id, f.workspaceId)); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: false, reasons: ["local_source_unsafe_or_missing"] }); + expect(readRemoteWorkspaceAuthority((await workspace(f.workspaceId)).metadata)).toBeNull(); + expect(await db.select().from(activityLog).where(eq(activityLog.action, "workspace.files_unavailable_at_migration"))).toHaveLength(0); + expect(await fs.readFile(path.join(elsewhere, "saved.txt"), "utf8")).toBe("original saved work\n"); + }); it("keeps provider-owned Runner task paths out of offline workspace migration", async () => { vi.stubEnv("PAPERCLIP_EPHEMERAL_MIGRATION", "true"); const companyId = randomUUID(), agentId = randomUUID(), environmentId = randomUUID(); diff --git a/server/src/services/remote-workspace-migration.ts b/server/src/services/remote-workspace-migration.ts index 0a5ab2e2165..02c74897fb4 100644 --- a/server/src/services/remote-workspace-migration.ts +++ b/server/src/services/remote-workspace-migration.ts @@ -6,7 +6,7 @@ import path from "node:path"; import { and, eq, inArray, ne, or, sql } from "drizzle-orm"; import { companies, environmentLeases, executionWorkspaceRepositories, executionWorkspaceRuntimeLeases, executionWorkspaceSnapshots, executionWorkspaces, - heartbeatRuns, issueRecoveryActions, issues, nativeRunFinalizations, projects, workspaceOperations, workspaceRuntimeServices, type Db, + heartbeatRuns, issueRecoveryActions, issues, nativeRunFinalizations, projects, projectWorkspaces, workspaceOperations, workspaceRuntimeServices, type Db, } from "@paperclipai/db"; import { readRemoteWorkspaceAuthority, remoteWorkspaceSourceSchema, type RemoteWorkspaceAuthority, type RemoteWorkspaceSource } from "@paperclipai/shared"; import { offlineMigrationExecutionWhere } from "./offline-migration-execution.js"; @@ -29,7 +29,11 @@ import { explainOnlineWorkspaceSource, inspectOnlineWorkspaceSource, requiresOnl import { withWorkspaceSourceLocks, withIndependentWorkspaceSourceLocks, IsolatedRepositorySourceBusy } from "./workspace-source-lock.js"; import { inspectOnlineLegacyWorkspaceRecoverySource, inspectOnlineLegacyWorkspaceRecoveryHold, isTaskDispositionRecovery, lockOnlineLegacyWorkspaceRecoverySources, ONLINE_LEGACY_WORKSPACE_PRESERVATION, type OnlineLegacyWorkspaceRecoveryHold } from "./online-legacy-workspace-recovery.js"; import { inspectNativeWorkspaceMigration, nativeWorkspaceMigrationMarker } from "./native-runtime/native-workspace-migration.js"; -import { readGitCacheExpiration, GIT_CACHE_EXPIRATION_REASON } from "./git-workspace-restoration.js"; +import { readGitCacheExpiration, GIT_CACHE_EXPIRATION_REASON, GIT_CACHE_RESTORATION_KEY, managedSourcePaths } from "./git-workspace-restoration.js"; +import { TASK_DIRECTORY_RETENTION_KEY } from "./isolated-task-directory-snapshot.js"; +import { recordedIsolatedRemoteSource } from "./isolated-remote-workspace-source.js"; +import { localRepositoryPath } from "./local-repository-path.js"; +import { workspaceFilesUnavailableRecord, WORKSPACE_FILES_UNAVAILABLE_KEY } from "./workspace-files-unavailable.js"; const SCHEMA = "paperclip.remote-workspace-migration.v1"; const SUPERSESSION = "paperclip.remote-workspace-migration-supersession.v1"; @@ -115,6 +119,37 @@ function recoveryState(recovery: { kind: string; cause: string; evidence: Record return workspaceExportCauses.includes(recovery.cause) ? `${recovery.kind}_export_cause` : `${recovery.kind}_file_evidence`; } +const FILES_GONE_REASONS = new Set(["archived_git_source_unavailable", "local_source_unsafe_or_missing"]); + +async function pathAbsent(candidate: string) { + if (!path.isAbsolute(candidate)) return false; + return !await fs.lstat(candidate).catch((error: NodeJS.ErrnoException) => { + if (error.code === "ENOENT") return null; + throw error; + }); +} + +const SAVED_FILE_REFERENCE_KEYS = [GIT_CACHE_RESTORATION_KEY, TASK_DIRECTORY_RETENTION_KEY]; + +async function workspaceFilesProvenGone(db: ReadDb, workspace: Workspace) { + if (SAVED_FILE_REFERENCE_KEYS.some(key => workspace.metadata?.[key] != null)) return false; + const candidates = [workspace.cwd, workspace.providerRef].filter((value): value is string => typeof value === "string"); + if (workspace.repoUrl) { + try { + const isolated = recordedIsolatedRemoteSource(workspace); + if (isolated) candidates.push(isolated); + const local = localRepositoryPath(workspace.repoUrl); + if (local) candidates.push(local); + } catch { return false; } + candidates.push(...managedSourcePaths(workspace)); + const [projectWorkspace] = workspace.projectWorkspaceId ? await db.select({ cwd: projectWorkspaces.cwd }).from(projectWorkspaces) + .where(and(eq(projectWorkspaces.companyId, workspace.companyId), eq(projectWorkspaces.id, workspace.projectWorkspaceId))) : []; + if (projectWorkspace?.cwd) candidates.push(projectWorkspace.cwd); + } + for (const candidate of new Set(candidates)) if (!await pathAbsent(candidate)) return false; + return candidates.length > 0; +} + async function inspect(db: ReadDb, input: { companyId: string; workspaceId: string; operationId?: string; writersStopped?: boolean; onlineSource?: OnlineWorkspaceSource; onlineSourceUnproven?: boolean }, cohort?: WorkspaceMigrationCohort) { const [workspace] = await db.select().from(executionWorkspaces).where(and(eq(executionWorkspaces.companyId, input.companyId), eq(executionWorkspaces.id, input.workspaceId))); if (!workspace) throw new Error("remote_workspace_migration_workspace_missing"); @@ -126,7 +161,7 @@ async function inspect(db: ReadDb, input: { companyId: string; workspaceId: stri reasons.push(reason); details.push(`${reason}:${/^[a-z_]{1,60}$/.test(state) ? state : "unrecognized"}`); }; - if (authority) return { workspace, reasons, details, settledFinalizationRunIds: [] as string[], authority, marker, source: authority.source, issueBinding: null, retainedDirectory: null, archivedGitSource: null, legacyRecoverySources: [] as LegacyWorkspaceRecoverySource[], finalizedRecoveryHolds: [] as FinalizedWorkspaceRecoveryHold[], onlineRecoveryHolds: [] as OnlineRecoveryHold[], archivedRepositoryIntents: [] as RepositoryRequest[] }; + if (authority) return { workspace, reasons, details, filesGone: false, settledFinalizationRunIds: [] as string[], authority, marker, source: authority.source, issueBinding: null, retainedDirectory: null, archivedGitSource: null, legacyRecoverySources: [] as LegacyWorkspaceRecoverySource[], finalizedRecoveryHolds: [] as FinalizedWorkspaceRecoveryHold[], onlineRecoveryHolds: [] as OnlineRecoveryHold[], archivedRepositoryIntents: [] as RepositoryRequest[] }; if (input.writersStopped) reasons.push(...await offlineWorkspaceMigrationBlockers(db)); if (input.onlineSourceUnproven) reasons.push("online_workspace_source_scope_unproven"); if (input.onlineSource) { @@ -147,6 +182,7 @@ async function inspect(db: ReadDb, input: { companyId: string; workspaceId: stri const cwd = workspace.cwd ?? workspace.providerRef; let retainedDirectory = null as ReturnType; let archivedGitSource: ArchivedGitMigrationSource | null = null; + let filesGone = false; if (!cwd || !path.isAbsolute(cwd)) reasons.push("local_source_missing"); else { const entry = await fs.lstat(cwd).catch((error: NodeJS.ErrnoException) => { @@ -165,10 +201,15 @@ async function inspect(db: ReadDb, input: { companyId: string; workspaceId: stri } if (!entry && !retainedDirectory && (workspace.status === "archived" || input.writersStopped)) { try { archivedGitSource = await inspectArchivedGitMigrationSource(db, workspace, { allowActiveRetainedBranch: input.writersStopped, operationId: allowedOperation }); } - catch (error) { reasons.push(error instanceof Error && error.message === "remote_workspace_migration_restore_point_expired" - ? "restore_point_expired" : "archived_git_source_unavailable"); } + catch (error) { + reasons.push(error instanceof Error && error.message === "remote_workspace_migration_restore_point_expired" + ? "restore_point_expired" : "archived_git_source_unavailable"); + } } if (!retainedDirectory && !archivedGitSource && (!entry?.isDirectory() || entry.isSymbolicLink())) reasons.push("local_source_unsafe_or_missing"); + 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)); } const [issue] = workspace.sourceIssueId ? await db.select().from(issues).where(and(eq(issues.companyId, workspace.companyId), eq(issues.id, workspace.sourceIssueId))) : []; @@ -361,7 +402,7 @@ async function inspect(db: ReadDb, input: { companyId: string; workspaceId: stri const materialized = repositories.filter(repository => repository.state === "ready"); if (materialized.length) source.repositories = materialized.map(repository => ({ id: repository.id, relativePath: repository.relativePath, repoUrl: repository.repoUrl, ref: repository.requestedRef, ...(repository.pinnedCommit ? { pinnedCommit: repository.pinnedCommit } : {}) })); - return { workspace, reasons: [...new Set(reasons)], details: [...new Set(details)].sort(), settledFinalizationRunIds: settledFinalizationRunIds.sort(), authority, marker, source, issueBinding: issue?.executionWorkspaceId ?? null, retainedDirectory, archivedGitSource, legacyRecoverySources, finalizedRecoveryHolds, onlineRecoveryHolds, archivedRepositoryIntents }; + return { workspace, reasons: [...new Set(reasons)].filter(reason => !filesGone || !FILES_GONE_REASONS.has(reason)), details: [...new Set(details)].filter(entry => !filesGone || !FILES_GONE_REASONS.has(entry.slice(0, entry.indexOf(":")))).sort(), filesGone, settledFinalizationRunIds: settledFinalizationRunIds.sort(), authority, marker, source, issueBinding: issue?.executionWorkspaceId ?? null, retainedDirectory, archivedGitSource, legacyRecoverySources, finalizedRecoveryHolds, onlineRecoveryHolds, archivedRepositoryIntents }; } export async function inventoryRemoteWorkspaceMigration(db: Db, input: { companyId: string; workspaceId: string; writersStopped?: boolean }) { @@ -374,7 +415,7 @@ export async function inventoryRemoteWorkspaceMigration(db: Db, input: { company return { companyId: input.companyId, workspaceId: input.workspaceId, eligible: result.reasons.length === 0, ...(onlineSourceUnproven && explained && "unproven" in explained ? { sourceScopeUnproven: explained.unproven } : {}), ...(result.details.length ? { details: result.details } : {}), - alreadyMigrated: Boolean(result.authority), reasons: result.reasons, localPath: result.workspace.cwd ?? result.workspace.providerRef, + 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, snapshotId: result.authority?.snapshotId ?? null }; } @@ -408,6 +449,22 @@ function recoverySourcesResumeIdentity(value: unknown): unknown { }); } +async function recordMissingWorkspaceFiles(tx: MigrationTransaction, workspace: Workspace, source: RemoteWorkspaceSource) { + const at = new Date(), localPath = (workspace.cwd ?? workspace.providerRef)!; + const authority: RemoteWorkspaceAuthority = { schema: "paperclip.remote-workspace.v1", epoch: 1, snapshotId: null, + source: remoteWorkspaceSourceSchema.parse(source), location: null, writerRunId: null, dirty: false }; + const metadata: Record = { ...workspace.metadata, remoteWorkspace: authority, + [WORKSPACE_FILES_UNAVAILABLE_KEY]: workspaceFilesUnavailableRecord(localPath, at) }; + delete metadata.remoteWorkspaceMigration; + const [saved] = await tx.update(executionWorkspaces).set({ providerType: "cloud_sandbox", providerRef: null, cwd: `/workspaces/${workspace.id}`, + metadata, updatedAt: at }).where(and(eq(executionWorkspaces.companyId, workspace.companyId), eq(executionWorkspaces.id, workspace.id))).returning(); + if (!saved) throw new Error("remote_workspace_migration_source_changed"); + const { publication } = await persistActivity(tx as unknown as Db, { companyId: workspace.companyId, actorType: "system", actorId: "workspace-migration", + action: "workspace.files_unavailable_at_migration", entityType: "execution_workspace", entityId: workspace.id, + details: { reason: "files_missing_at_migration", localPath, status: workspace.status } }); + return { workspace: saved, authority, source: authority.source, filesUnavailablePublication: publication }; +} + async function reserveMigration(input: MigrationInput, onlineSource?: OnlineWorkspaceSource, cohort?: WorkspaceMigrationCohort, expectedSourceFingerprint?: string) { return input.db.transaction(async tx => { @@ -419,9 +476,17 @@ async function reserveMigration(input: MigrationInput, onlineSource?: OnlineWork if (!workspace) throw new Error("remote_workspace_migration_workspace_missing"); if (workspace.sourceIssueId !== selected.sourceIssueId) throw new Error("remote_workspace_migration_source_changed"); const current = await inspect(tx as unknown as Db, { ...input, onlineSource }, cohort); - if (current.authority) return { workspace, authority: current.authority, operation: null, source: current.source, sourceFingerprint: "", issueBinding: current.issueBinding, retainedDirectory: null, archivedGitSource: null, legacyRecoverySources: [] as LegacyWorkspaceRecoverySource[], finalizedRecoveryHolds: [] as FinalizedWorkspaceRecoveryHold[], onlineRecoveryHolds: [] as OnlineRecoveryHold[], archivedRepositoryIntents: [] as RepositoryRequest[], settledFinalizationRunIds: [] as string[] }; + if (current.authority) return { workspace, authority: current.authority, filesUnavailablePublication: undefined, operation: null, source: current.source, sourceFingerprint: "", issueBinding: current.issueBinding, retainedDirectory: null, archivedGitSource: null, legacyRecoverySources: [] as LegacyWorkspaceRecoverySource[], finalizedRecoveryHolds: [] as FinalizedWorkspaceRecoveryHold[], onlineRecoveryHolds: [] as OnlineRecoveryHold[], archivedRepositoryIntents: [] as RepositoryRequest[], settledFinalizationRunIds: [] as string[] }; if (current.reasons.length) throw new Error(`remote_workspace_migration_blocked:${current.reasons.join(",")}`); const nativeSessionContinuity = await inspectNativeWorkspaceMigration(tx as unknown as Db, workspace); + if (current.filesGone) { + if (cohort || current.marker.schema === SCHEMA || nativeSessionContinuity || current.legacyRecoverySources.length + || current.finalizedRecoveryHolds.length || current.onlineRecoveryHolds.length || current.archivedRepositoryIntents.length) + throw new Error("remote_workspace_migration_blocked:local_source_unsafe_or_missing"); + return { ...await recordMissingWorkspaceFiles(tx, workspace, current.source), operation: null, sourceFingerprint: "", + issueBinding: current.issueBinding, retainedDirectory: null, archivedGitSource: null, legacyRecoverySources: [] as LegacyWorkspaceRecoverySource[], + finalizedRecoveryHolds: [] as FinalizedWorkspaceRecoveryHold[], onlineRecoveryHolds: [] as OnlineRecoveryHold[], archivedRepositoryIntents: [] as RepositoryRequest[], settledFinalizationRunIds: [] as string[] }; + } const source = remoteWorkspaceSourceSchema.parse(input.source ?? current.source); const sourceFingerprint = fingerprint(workspace); if (cohort && sourceFingerprint !== expectedSourceFingerprint) throw new Error("workspace_cohort_source_changed"); @@ -446,7 +511,7 @@ async function reserveMigration(input: MigrationInput, onlineSource?: OnlineWork if (canonical(operation.metadata?.onlineRecoveryHolds ?? []) !== canonical(current.onlineRecoveryHolds)) throw new Error("remote_workspace_migration_source_changed"); if (canonical(operation.metadata?.archivedRepositoryIntents ?? []) !== canonical(repositoryIntentBindings(current.archivedRepositoryIntents))) throw new Error("remote_workspace_migration_source_changed"); - return { workspace, authority: null, operation, source, sourceFingerprint, issueBinding, retainedDirectory: current.retainedDirectory, archivedGitSource: current.archivedGitSource, legacyRecoverySources: current.legacyRecoverySources, finalizedRecoveryHolds: current.finalizedRecoveryHolds, onlineRecoveryHolds: current.onlineRecoveryHolds, archivedRepositoryIntents: current.archivedRepositoryIntents, settledFinalizationRunIds: current.settledFinalizationRunIds }; + return { workspace, authority: null, filesUnavailablePublication: undefined, operation, source, sourceFingerprint, issueBinding, retainedDirectory: current.retainedDirectory, archivedGitSource: current.archivedGitSource, legacyRecoverySources: current.legacyRecoverySources, finalizedRecoveryHolds: current.finalizedRecoveryHolds, onlineRecoveryHolds: current.onlineRecoveryHolds, archivedRepositoryIntents: current.archivedRepositoryIntents, settledFinalizationRunIds: current.settledFinalizationRunIds }; } let migrationSupersession; if (workspace.metadata?.remoteWorkspaceMigrationSupersession != null) { @@ -474,7 +539,7 @@ async function reserveMigration(input: MigrationInput, onlineSource?: OnlineWork await tx.update(executionWorkspaces).set({ metadata: { ...workspace.metadata, remoteWorkspaceMigration: { schema: SCHEMA, operationId: operation!.id, state: "running", sourceFingerprint, source, } }, updatedAt: new Date() }).where(eq(executionWorkspaces.id, workspace.id)); - return { workspace, authority: null, operation: operation!, source, sourceFingerprint, issueBinding: current.issueBinding, retainedDirectory: current.retainedDirectory, archivedGitSource: current.archivedGitSource, legacyRecoverySources: current.legacyRecoverySources, finalizedRecoveryHolds: current.finalizedRecoveryHolds, onlineRecoveryHolds: current.onlineRecoveryHolds, archivedRepositoryIntents: current.archivedRepositoryIntents, settledFinalizationRunIds: current.settledFinalizationRunIds }; + return { workspace, authority: null, filesUnavailablePublication: undefined, operation: operation!, source, sourceFingerprint, issueBinding: current.issueBinding, retainedDirectory: current.retainedDirectory, archivedGitSource: current.archivedGitSource, legacyRecoverySources: current.legacyRecoverySources, finalizedRecoveryHolds: current.finalizedRecoveryHolds, onlineRecoveryHolds: current.onlineRecoveryHolds, archivedRepositoryIntents: current.archivedRepositoryIntents, settledFinalizationRunIds: current.settledFinalizationRunIds }; }); } type ReservedMigration = Awaited>; @@ -760,6 +825,10 @@ export async function migrateRemoteWorkspace(input: MigrationInput) { let committedPublication: Parameters[0] | undefined; const migrate = async (lock: MigrationTransaction) => { const reserved = await reserveMigration(input, onlineSource); + if (reserved.filesUnavailablePublication) { + publishActivity(reserved.filesUnavailablePublication); + return { ...await inventoryRemoteWorkspaceMigration(input.db, input), applied: true, replayed: false, filesUnavailable: true }; + } if (reserved.authority) return { ...await inventoryRemoteWorkspaceMigration(input.db, input), applied: true, replayed: true }; await lockMigrationRecovery(lock, [{ input, onlineSource, reserved }]); const childVerifier = createMigrationChildVerifier(input, onlineSource, lock); diff --git a/server/src/services/workspace-files-unavailable.ts b/server/src/services/workspace-files-unavailable.ts new file mode 100644 index 00000000000..13597c357f5 --- /dev/null +++ b/server/src/services/workspace-files-unavailable.ts @@ -0,0 +1,23 @@ +export const WORKSPACE_FILES_UNAVAILABLE_KEY = "workspaceFilesUnavailable"; +export const WORKSPACE_FILES_UNAVAILABLE_SCHEMA = "paperclip.workspace-files-unavailable.v1"; +export const WORKSPACE_FILES_UNAVAILABLE_MESSAGE = + "This workspace's files were not available when it moved to new storage, so they cannot be restored. Continue the task in a new workspace."; + +export type WorkspaceFilesUnavailable = { + schema: typeof WORKSPACE_FILES_UNAVAILABLE_SCHEMA; + reason: "files_missing_at_migration"; + recordedAt: string; + localPath: string; +}; + +export function workspaceFilesUnavailableRecord(localPath: string, at: Date): WorkspaceFilesUnavailable { + return { schema: WORKSPACE_FILES_UNAVAILABLE_SCHEMA, reason: "files_missing_at_migration", recordedAt: at.toISOString(), localPath }; +} + +export function readWorkspaceFilesUnavailable(metadata: Record | null | undefined): WorkspaceFilesUnavailable | null { + const value = metadata?.[WORKSPACE_FILES_UNAVAILABLE_KEY]; + if (!value || typeof value !== "object" || Array.isArray(value)) return null; + const record = value as Record; + return record.schema === WORKSPACE_FILES_UNAVAILABLE_SCHEMA && record.reason === "files_missing_at_migration" + && typeof record.recordedAt === "string" && typeof record.localPath === "string" ? record as WorkspaceFilesUnavailable : null; +}