diff --git a/packages/paperclip-runner/scripts/stage-runner-binary.test.mjs b/packages/paperclip-runner/scripts/stage-runner-binary.test.mjs index 2ee6166fecb..93a0d01ee9c 100644 --- a/packages/paperclip-runner/scripts/stage-runner-binary.test.mjs +++ b/packages/paperclip-runner/scripts/stage-runner-binary.test.mjs @@ -84,10 +84,12 @@ test("release metadata rejects incompatible native, harness and protocol contrac } }); -async function standaloneSleep() { - if (process.platform !== "linux") return "/bin/sleep"; - const multicall = (await readFile("/bin/sleep")).equals(await readFile("/bin/echo")); - return multicall ? "/usr/bin/gnusleep" : "/bin/sleep"; +async function standaloneHostUtility(name) { + const standard = `/bin/${name}`; + if (process.platform !== "linux") return standard; + const other = name === "sleep" ? "/bin/echo" : "/bin/sleep"; + const multicall = (await readFile(standard)).equals(await readFile(other)); + return multicall ? `/usr/bin/gnu${name}` : standard; } async function withOwnedChildren(root, action) { @@ -118,7 +120,7 @@ async function withOwnedChildren(root, action) { test("atomically publishes while the previous executable is running", { skip: process.platform === "win32", timeout: 15_000 }, async () => { const root = await mkdtemp(path.join(tmpdir(), "runnerd-stage-test-")); const destination = path.join(root, "paperclip-runnerd"); - const sleep = await standaloneSleep(); + const sleep = await standaloneHostUtility("sleep"); await withOwnedChildren(root, async start => { await stageRunnerBinary(sleep, destination); const previous = await stat(destination); @@ -159,7 +161,7 @@ test("a source rebuild selects fresh dev bytes while preserving release binaries await mkdir(path.dirname(built), { recursive: true }); await copyHostFixture("/bin/sh", built); const previousBuild = path.join(root, "previous-build"); - await copyHostFixture(await standaloneSleep(), previousBuild); + await copyHostFixture(await standaloneHostUtility("sleep"), previousBuild); await stageRunnerBinary(previousBuild, hostTarget); await stageRunnerBinary(built, path.join(binaryRoot, "paperclip-runnerd")); const platforms = {}, releaseBytes = new Map(); diff --git a/server/src/__tests__/project-delete-references.test.ts b/server/src/__tests__/project-delete-references.test.ts new file mode 100644 index 00000000000..f98209ac608 --- /dev/null +++ b/server/src/__tests__/project-delete-references.test.ts @@ -0,0 +1,99 @@ +import { randomUUID } from "node:crypto"; +import { eq } from "drizzle-orm"; +import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; +import { + companies, + costEvents, + createDb, + financeEvents, + issues, + projects, +} from "@paperclipai/db"; +import { + getEmbeddedPostgresTestSupport, + startEmbeddedPostgresTestDatabase, +} from "./helpers/embedded-postgres.js"; +import { issueService } from "../services/issues.ts"; +import { projectService } from "../services/projects.ts"; + +const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport(); +const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip; + +describeEmbeddedPostgres("project deletion with retained references", () => { + let db!: ReturnType; + let tempDb: Awaited> | null = null; + + beforeAll(async () => { + tempDb = await startEmbeddedPostgresTestDatabase("paperclip-project-delete-"); + db = createDb(tempDb.connectionString); + }, 20_000); + + afterEach(async () => { + await db.delete(financeEvents); + await db.delete(costEvents); + await db.delete(issues); + await db.delete(projects); + await db.delete(companies); + }); + + afterAll(async () => { + await tempDb?.cleanup(); + }); + + async function fixture() { + const [company] = await db.insert(companies).values({ + name: randomUUID(), + issuePrefix: `T${randomUUID().slice(0, 6).toUpperCase()}`, + }).returning(); + const [project] = await db.insert(projects).values({ + companyId: company.id, + name: "Menu", + }).returning(); + return { company, project }; + } + + it("keeps spend history and clears its project attribution", async () => { + const { company, project } = await fixture(); + const [cost] = await db.insert(costEvents).values({ + companyId: company.id, + projectId: project.id, + usageKind: "decision", + provider: "fixture", + model: "fixture-model", + costCents: 12, + occurredAt: new Date(), + }).returning(); + const [finance] = await db.insert(financeEvents).values({ + companyId: company.id, + projectId: project.id, + eventKind: "charge", + biller: "fixture", + amountCents: 12, + occurredAt: new Date(), + }).returning(); + + const removed = await projectService(db).remove(project.id); + + expect(removed?.id).toBe(project.id); + const [retainedCost] = await db.select().from(costEvents).where(eq(costEvents.id, cost.id)); + const [retainedFinance] = await db.select().from(financeEvents).where(eq(financeEvents.id, finance.id)); + expect(retainedCost?.projectId).toBeNull(); + expect(retainedFinance?.projectId).toBeNull(); + expect(await db.select().from(projects).where(eq(projects.id, project.id))).toHaveLength(0); + }); + + it("refuses to delete a project that still owns tasks", async () => { + const { company, project } = await fixture(); + await issueService(db).create(company.id, { + title: "Retained task", + status: "todo", + projectId: project.id, + }); + + await expect(projectService(db).remove(project.id)).rejects.toMatchObject({ + status: 409, + message: "This project still has tasks. Move or delete its tasks before deleting the project.", + }); + expect(await db.select().from(projects).where(eq(projects.id, project.id))).toHaveLength(1); + }); +}); diff --git a/server/src/services/native-runtime/runner-bridge-tools.test.ts b/server/src/services/native-runtime/runner-bridge-tools.test.ts index 847eb728b1e..aafc7e0d0f5 100644 --- a/server/src/services/native-runtime/runner-bridge-tools.test.ts +++ b/server/src/services/native-runtime/runner-bridge-tools.test.ts @@ -176,7 +176,7 @@ if sys.argv[1] != "completion": expect(await readFile(join(directory, "output.txt"), "utf8")).toBe("hello"); const read = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/cat", args: [secret] }, authorize); expect(read).toMatchObject({ exitCode: 1 }); expect(String((read as any).output)).not.toContain("PRIVATE"); - const write = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/sh", args: ["-c", 'printf stolen > "$1"', "sh", secret] }, authorize); + const write = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/sh", args: ["-c", 'printf stolen > "$1" || exit 1', "sh", secret] }, authorize); expect(await readFile(secret, "utf8")).toBe("PRIVATE"); expect(write).toMatchObject({ stopped: null, exitCode: expect.any(Number) }); expect((write as { exitCode: number }).exitCode).not.toBe(0); }); diff --git a/server/src/services/projects.ts b/server/src/services/projects.ts index 5a6ffb68915..a5421b8f381 100644 --- a/server/src/services/projects.ts +++ b/server/src/services/projects.ts @@ -19,6 +19,8 @@ import { plugins, projectWorkspaces, workspaceRuntimeServices, + costEvents, + financeEvents, } from "@paperclipai/db"; import { deriveProjectUrlKey, @@ -1122,6 +1124,15 @@ export function projectService(db: Db, options: { captureWorkspaceUpdates?: bool code: "project_execution_admission_pending", }); } + const [retainedTask] = await tx.select({ id: issues.id }).from(issues) + .where(eq(issues.projectId, id)).limit(1); + if (retainedTask) throw conflict("This project still has tasks. Move or delete its tasks before deleting the project.", { + code: "project_tasks_pending", + }); + await tx.update(costEvents).set({ projectId: null }) + .where(and(eq(costEvents.companyId, project.companyId), eq(costEvents.projectId, id))); + await tx.update(financeEvents).set({ projectId: null }) + .where(and(eq(financeEvents.companyId, project.companyId), eq(financeEvents.projectId, id))); await tx.select({ id: projectWorkspaces.id }).from(projectWorkspaces).where(eq(projectWorkspaces.projectId, id)).for("update"); await recordProjectLifecycleEvent(tx as unknown as Db, project.companyId, id, "delete"); await notifyDeliveryWork(tx, DELIVERY_QUEUES.skillSourceSync);