import { randomUUID } from "node:crypto"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { eq } from "drizzle-orm"; import { agents, companies, companyMemberships, createDb, executionWorkspaces, heartbeatRuns, instanceSettings, issues, plugins, projects, projectWorkspaces, type Db, } from "@paperclipai/db"; import { startEmbeddedPostgresTestDatabase, type EmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; import { createPluginWorkspaceFixture } from "./helpers/plugin-workspaces.js"; import { issueService } from "../services/issues.js"; import { executionWorkspaceService } from "../services/execution-workspaces.js"; import { assertTaskWorkspaceExecutionAccess } from "../services/task-workspace-access.js"; describe("project move workspace reconciliation", () => { let pg: EmbeddedPostgresTestDatabase; let db: Db; beforeAll(async () => { pg = await startEmbeddedPostgresTestDatabase("paperclip-issue-project-move-"); db = createDb(pg.connectionString); }, 60_000); afterAll(async () => { await pg?.cleanup(); }, 30_000); async function seedTwoProjects() { const companyId = randomUUID(); const agentId = randomUUID(); await db.insert(companies).values({ id: companyId, name: "Move Co", issuePrefix: `M${companyId.slice(0, 5).toUpperCase()}`, status: "active", }); await db.insert(instanceSettings).values({ id: randomUUID(), singletonKey: "default", experimental: { enableIsolatedWorkspaces: true }, }).onConflictDoUpdate({ target: instanceSettings.singletonKey, set: { experimental: { enableIsolatedWorkspaces: true }, updatedAt: new Date() }, }); await db.insert(agents).values({ id: agentId, companyId, name: "Winston", adapterType: "paperclip_runner", status: "idle", }); const storage = await createPluginWorkspaceFixture(db, companyId); const [storagePlugin] = await db.select().from(plugins).where(eq(plugins.id, storage.pluginId)); await db.update(plugins).set({ manifestJson: { ...storagePlugin!.manifestJson, workspaceProvider: { ...storagePlugin!.manifestJson.workspaceProvider!, defaultForProjects: { mode: "isolated_workspace", strategy: "git_worktree" }, }, }, }).where(eq(plugins.id, storage.pluginId)); const [coreProject] = await db.insert(projects).values({ companyId, name: "Paperclip (Deprecated)", executionWorkspacePolicy: { enabled: true, defaultMode: "isolated_workspace", workspaceStrategy: { type: "git_worktree" }, }, }).returning(); const [coreWorkspace] = await db.insert(projectWorkspaces).values({ companyId, projectId: coreProject!.id, name: "paperclipai/paperclip (core)", sourceType: "git_repo", repoUrl: "https://github.com/paperclipai/paperclip", isPrimary: true, }).returning(); const [pluginProject] = await db.insert(projects).values({ companyId, name: "Paperclip", executionWorkspacePolicy: { enabled: true, storageProviderPluginId: storage.pluginId, }, }).returning(); const [pluginWorkspace] = await db.insert(projectWorkspaces).values({ companyId, projectId: pluginProject!.id, name: "paperclipai/paperclip (plugin)", sourceType: "git_repo", repoUrl: "https://github.com/paperclipai/paperclip", isPrimary: true, }).returning(); const [emptyProject] = await db.insert(projects).values({ companyId, name: "Notes Only", }).returning(); return { companyId, agentId, coreProject: coreProject!, coreWorkspace: coreWorkspace!, pluginProject: pluginProject!, pluginWorkspace: pluginWorkspace!, emptyProject: emptyProject!, }; } it("re-derives a project-derived workspace when moving an unbound task to another project", async () => { const f = await seedTwoProjects(); const svc = issueService(db); const created = await svc.create(f.companyId, { title: "Investigate duplicate routines", projectId: f.coreProject.id, assigneeAgentId: f.agentId, status: "todo", }); expect(created.projectWorkspaceId).toBe(f.coreWorkspace.id); expect(created.workspaceSelection).toMatchObject({ version: 1, source: "project", selection: { kind: "configured_source", projectWorkspaceId: f.coreWorkspace.id, mode: "managed_isolated", }, }); const moved = await svc.update(created.id, { projectId: f.pluginProject.id }); expect(moved).toMatchObject({ projectId: f.pluginProject.id, projectWorkspaceId: f.pluginWorkspace.id, workspaceBindingRevision: created.workspaceBindingRevision + 1, workspaceSelection: { version: 1, source: "project", selection: { kind: "configured_source", projectWorkspaceId: f.pluginWorkspace.id, mode: "managed_isolated", }, }, }); }); it("clears a project-derived workspace when moving to no project or a sourceless project", async () => { const f = await seedTwoProjects(); const svc = issueService(db); const created = await svc.create(f.companyId, { title: "Move to sourceless", projectId: f.coreProject.id, assigneeAgentId: f.agentId, status: "todo", }); const movedToEmpty = await svc.update(created.id, { projectId: f.emptyProject.id }); expect(movedToEmpty).toMatchObject({ projectId: f.emptyProject.id, projectWorkspaceId: null, workspaceSelection: null, }); const created2 = await svc.create(f.companyId, { title: "Move to projectless", projectId: f.coreProject.id, assigneeAgentId: f.agentId, status: "todo", }); const movedToNull = await svc.update(created2.id, { projectId: null }); expect(movedToNull).toMatchObject({ projectId: null, projectWorkspaceId: null, workspaceSelection: null, executionWorkspaceSettings: null, }); }); it("preserves an explicit workspace selection when moving a task to another project", async () => { const f = await seedTwoProjects(); const svc = issueService(db); const created = await svc.create(f.companyId, { title: "Explicit workspace task", projectId: f.coreProject.id, assigneeAgentId: f.agentId, status: "todo", workspaceSelection: { kind: "configured_source", projectWorkspaceId: f.coreWorkspace.id, mode: "managed_isolated", }, workspaceSelectionSource: "explicit", }); const moved = await svc.update(created.id, { projectId: f.pluginProject.id }); expect(moved).toMatchObject({ projectId: f.pluginProject.id, projectWorkspaceId: f.coreWorkspace.id, workspaceSelection: { version: 1, source: "explicit", selection: { kind: "configured_source", projectWorkspaceId: f.coreWorkspace.id, mode: "managed_isolated", }, }, }); }); it("preserves a bound execution workspace when moving a task to another project", async () => { const f = await seedTwoProjects(); const svc = issueService(db); const created = await svc.create(f.companyId, { title: "Task with bound files", projectId: f.coreProject.id, assigneeAgentId: f.agentId, status: "todo", }); const [boundWs] = await db.insert(executionWorkspaces).values({ companyId: f.companyId, projectId: f.coreProject.id, projectWorkspaceId: f.coreWorkspace.id, sourceIssueId: created.id, name: "Bound worktree", cwd: "/tmp/bound-worktree", mode: "isolated_workspace", strategyType: "git_worktree", }).returning(); await db.update(issues).set({ executionWorkspaceId: boundWs!.id }).where(eq(issues.id, created.id)); const moved = await svc.update(created.id, { projectId: f.pluginProject.id }); expect(moved).toMatchObject({ projectId: f.pluginProject.id, executionWorkspaceId: boundWs!.id, projectWorkspaceId: f.coreWorkspace.id, }); }); it("does not mutate workspace binding mid-run when moving a task with an active running run linked only by issueId", async () => { const f = await seedTwoProjects(); const svc = issueService(db); const created = await svc.create(f.companyId, { title: "Task with active run", projectId: f.coreProject.id, assigneeAgentId: f.agentId, status: "in_progress", }); const runId = randomUUID(); await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, issueId: created.id, status: "running", contextSnapshot: {}, }); const moved = await svc.update(created.id, { projectId: f.pluginProject.id }); expect(moved).toMatchObject({ projectId: f.pluginProject.id, projectWorkspaceId: f.coreWorkspace.id, }); }); it("re-derives a legacy projectWorkspaceId with no workspaceSelection when moving to another project", async () => { const f = await seedTwoProjects(); const svc = issueService(db); const issueId = randomUUID(); await db.insert(issues).values({ id: issueId, companyId: f.companyId, title: "Legacy unbound task", status: "todo", projectId: f.coreProject.id, projectWorkspaceId: f.coreWorkspace.id, workspaceSelection: null, assigneeAgentId: f.agentId, }); const moved = await svc.update(issueId, { projectId: f.pluginProject.id }); expect(moved).toMatchObject({ projectId: f.pluginProject.id, projectWorkspaceId: f.pluginWorkspace.id, workspaceBindingRevision: 1, workspaceSelection: { version: 1, source: "project", selection: { kind: "configured_source", projectWorkspaceId: f.pluginWorkspace.id, mode: "managed_isolated", }, }, }); }); it("honors an explicit projectWorkspaceId passed alongside a project move", async () => { const f = await seedTwoProjects(); const svc = issueService(db); const created = await svc.create(f.companyId, { title: "Move with explicit source", projectId: f.coreProject.id, assigneeAgentId: f.agentId, status: "todo", }); const moved = await svc.update(created.id, { projectId: f.pluginProject.id, projectWorkspaceId: f.coreWorkspace.id, }); expect(moved).toMatchObject({ projectId: f.pluginProject.id, projectWorkspaceId: f.coreWorkspace.id, }); }); it("allows moving an unbound task to a trust-denied project with workspaceSelectionActor while keeping execution access denied", async () => { const f = await seedTwoProjects(); const restrictedAgentId = randomUUID(); await db.insert(agents).values({ id: restrictedAgentId, companyId: f.companyId, name: "Restricted Reviewer", adapterType: "process", status: "idle", permissions: { trustPreset: "low_trust_review", authorizationPolicy: { trustBoundary: { mode: "low_trust_review", companyId: f.companyId, projectIds: [f.coreProject.id], }, }, }, }); await db.insert(companyMemberships).values({ companyId: f.companyId, principalType: "agent", principalId: restrictedAgentId, membershipRole: "member", status: "active", }); const svc = issueService(db); const board = { type: "board" as const, source: "local_implicit" as const }; await db.update(projects).set({ executionWorkspacePolicy: { enabled: true, storageProviderPluginId: (f.pluginProject.executionWorkspacePolicy as Record).storageProviderPluginId, trustPreset: "low_trust_review", authorizationPolicy: { trustBoundary: { mode: "low_trust_review", companyId: f.companyId, projectIds: [f.pluginProject.id], }, }, } as Record, }).where(eq(projects.id, f.pluginProject.id)); const created = await svc.create(f.companyId, { title: "Task moving to denied project", projectId: f.coreProject.id, assigneeAgentId: restrictedAgentId, status: "todo", workspaceSelectionActor: board, }); const moved = await svc.update(created.id, { projectId: f.pluginProject.id, workspaceSelectionActor: board, }); expect(moved).toMatchObject({ projectId: f.pluginProject.id, projectWorkspaceId: f.pluginWorkspace.id, workspaceSelection: null, workspaceBindingRevision: created.workspaceBindingRevision + 1, }); const agentActor = { type: "agent" as const, source: "agent_key" as const, agentId: restrictedAgentId, companyId: f.companyId, }; await expect( assertTaskWorkspaceExecutionAccess(db, agentActor, f.companyId, { issueId: created.id }), ).rejects.toMatchObject({ status: 403 }); }); it("reconciles an already-moved task with a stale project-derived workspace at admission", async () => { const f = await seedTwoProjects(); const issueId = randomUUID(); const runId = randomUUID(); await db.insert(issues).values({ id: issueId, companyId: f.companyId, title: "Already moved before fix", status: "todo", projectId: f.pluginProject.id, projectWorkspaceId: f.coreWorkspace.id, assigneeAgentId: f.agentId, executionWorkspaceSettings: { mode: "isolated_workspace" }, workspaceSelection: { version: 1, source: "project", selection: { kind: "configured_source", projectWorkspaceId: f.coreWorkspace.id, mode: "managed_isolated", }, }, }); await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, issueId, status: "running", contextSnapshot: { issueId }, }); const wsSvc = executionWorkspaceService(db); const reconciled = await wsSvc.reconcileStaleProjectWorkspaceAtAdmission({ companyId: f.companyId, issueId, runId, }); expect(reconciled).toBe(true); const [after] = await db.select().from(issues).where(eq(issues.id, issueId)); expect(after).toMatchObject({ projectId: f.pluginProject.id, projectWorkspaceId: f.pluginWorkspace.id, workspaceBindingRevision: 1, workspaceSelection: { version: 1, source: "project", selection: { kind: "configured_source", projectWorkspaceId: f.pluginWorkspace.id, mode: "managed_isolated", }, }, }); // Re-running admission reconciliation once consistent is a no-op. expect(await wsSvc.reconcileStaleProjectWorkspaceAtAdmission({ companyId: f.companyId, issueId, runId, })).toBe(false); }); it("does not reconcile an explicit cross-project workspace selection at admission", async () => { const f = await seedTwoProjects(); const issueId = randomUUID(); const runId = randomUUID(); await db.insert(issues).values({ id: issueId, companyId: f.companyId, title: "Explicit cross-project selection", status: "todo", projectId: f.pluginProject.id, projectWorkspaceId: f.coreWorkspace.id, assigneeAgentId: f.agentId, workspaceSelection: { version: 1, source: "explicit", selection: { kind: "configured_source", projectWorkspaceId: f.coreWorkspace.id, mode: "managed_isolated", }, }, }); const wsSvc = executionWorkspaceService(db); expect(await wsSvc.reconcileStaleProjectWorkspaceAtAdmission({ companyId: f.companyId, issueId, runId, })).toBe(false); const [after] = await db.select().from(issues).where(eq(issues.id, issueId)); expect(after?.projectWorkspaceId).toBe(f.coreWorkspace.id); }); });