import { randomUUID } from "node:crypto"; import express from "express"; import request from "supertest"; import { eq } from "drizzle-orm"; import { agents, companies, companyMemberships, principalPermissionGrants, environments, plugins, createDb, type Db } from "@paperclipai/db"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../../../__tests__/helpers/embedded-postgres.js"; import { createAgentLifecycle, configureAgentLifecycle, createAgentLifecycleEffects } from "../../../services/agent-lifecycle.js"; import { agentService as agentConfiguration } from "../../../services/agents.js"; import { createLifecycleDriver } from "../../../services/agent-lifecycle-driver.js"; import { agentRoutes } from "../../../routes/agents.js"; import { errorHandler } from "../../../middleware/error-handler.js"; import { createLifecycleStore as createStore } from "../adapters/postgres.js"; import type { LifecycleAgent, LifecycleDriver } from "../application/ports.js"; const createLifecycleStore = (db: Db) => createStore(db, createAgentLifecycleEffects()); const support = await getEmbeddedPostgresTestSupport(); (support.supported ? describe : describe.skip)("agent setup lifecycle", () => { let database: Awaited>; let db: Db; let companyId: string; const workers: ReturnType[] = []; const releases: (() => void)[] = []; beforeAll(async () => { database = await startEmbeddedPostgresTestDatabase("paperclip-agent-setup-"); db = createDb(database.connectionString); }, 90_000); afterEach(() => { for (const release of releases.splice(0)) release(); }); afterAll(async () => { for (const work of workers) await work.stop(); await database?.cleanup(); }); beforeEach(async () => { for (const work of workers) await work.stop(); companyId = randomUUID(); await db.insert(companies).values({ id: companyId, name: "Setup test", issuePrefix: `S${companyId.slice(0, 7)}` }); }); function worker(runHost: LifecycleDriver["runHost"] = async () => "complete") { const work = configureAgentLifecycle(db, { requiredPluginIds: async () => [], runPlugin: async () => "complete", runHost }); workers.push(work); return work; } async function current(id: string) { return (await db.select().from(agents).where(eq(agents.id, id)))[0]!; } it("keeps early setup inactive and completes the same record without renaming it", async () => { const runHost = vi.fn(async () => "complete" as const); const work = worker(runHost); const lifecycle = createAgentLifecycle(db); const id = randomUUID(); const input = { id, name: "Early agent", adapterType: "process" }; const first = await lifecycle.requestHire(companyId, input, { setupOnly: true }); const retry = await lifecycle.requestHire(companyId, input, { setupOnly: true }); expect(first.id).toBe(id); expect(retry.id).toBe(id); await work.process(id); expect(runHost).not.toHaveBeenCalled(); expect(await current(id)).toMatchObject({ status: "paused", lifecycleState: "paused", lifecycleHolds: ["setup"] }); expect(await db.select().from(principalPermissionGrants).where(eq(principalPermissionGrants.principalId, id))).toHaveLength(0); await expect(agentConfiguration(db).createApiKey(id, "Incomplete setup")).rejects.toThrow("Finish agent setup"); await expect(lifecycle.resumeAgent(id)).rejects.toThrow("Finish agent setup"); const finished = await lifecycle.requestHire(companyId, { id, name: "Early agent", adapterType: "process", status: "idle" }, { setupAgentId: id }); expect(finished).toMatchObject({ id, name: "Early agent" }); await work.process(id); expect(await current(id)).toMatchObject({ status: "idle", lifecycleState: "ready", lifecycleHolds: [] }); expect(await db.select().from(agents).where(eq(agents.companyId, companyId))).toHaveLength(1); await expect(lifecycle.requestHire(companyId, { id, name: "Early agent" }, { setupAgentId: id })).rejects.toThrow("no longer available"); }); async function agentEnvironment(hold?: () => Promise) { const pluginId = randomUUID(), environmentId = randomUUID(); await db.insert(plugins).values({ id: pluginId, pluginKey: pluginId, packageName: "agent-environment-test", version: "1.0.0", status: "ready", manifestJson: { agentLifecycle: true, capabilities: ["agents.lifecycle.manage", "environment.drivers.register"], environmentDrivers: ["tasks", "desktop"].map(driverKey => ({ driverKey, requiresAgent: true, supportsTasks: true })) } as never }); const environment = async (id: string, driverKey: string) => { await db.insert(environments).values({ id, name: `Agent-owned ${id}`, driver: "plugin", status: "active", config: { pluginKey: pluginId, driverKey, driverConfig: {} } }); return id; }; await environment(environmentId, "tasks"); const call = vi.fn(async (_id, _method, input) => { if (input.phase === "preparing") await hold?.(); return { operationId: input.operationId, version: input.version, status: "complete" }; }); const runHost = vi.fn(async () => "complete"); const driver = createLifecycleDriver(db, { call } as never); const work = configureAgentLifecycle(db, { ...driver, runHost }); workers.push(work); const phases = () => call.mock.calls.map(([, , request]) => request.phase); return { pluginId, environmentId, environment, call, runHost, work, phases }; } it("prepares agent-dependent drafts, stays idle until finish, and invalidates changed environments", async () => { const { environmentId, call, runHost, work } = await agentEnvironment(); const lifecycle = createAgentLifecycle(db); const id = randomUUID(); const input = { id, name: "Early computer", adapterType: "paperclip_runner", adapterConfig: { provider: "codex" }, defaultEnvironmentId: environmentId }; const draft = await lifecycle.requestHire(companyId, input, { setupOnly: true, runnerResolved: true }); expect(draft).toMatchObject({ lifecycleState: "preparing", status: "paused", pauseReason: "setup" }); await work.process(id); expect(await current(id)).toMatchObject({ lifecycleState: "verifying", status: "paused", lifecycleHolds: ["setup"] }); expect(call.mock.calls.map(([, , request]) => request.phase)).toEqual(["preparing"]); const store = createLifecycleStore(db); expect(await store.pending(100, new Date())).not.toContain(id); expect(await store.claim(id, "unexpected", new Date())).toBeNull(); await work.process(id); expect(runHost).toHaveBeenCalledTimes(1); await expect(agentConfiguration(db).createApiKey(id, "Incomplete computer")).rejects.toThrow("Finish agent setup"); await expect(lifecycle.resumeAgent(id)).rejects.toThrow("Finish agent setup"); await lifecycle.pauseAgent(id, "budget"); await work.process(id); expect(await current(id)).toMatchObject({ lifecycleState: "paused", lifecycleHolds: ["setup", "budget"] }); await lifecycle.updateConfiguration(id, { adapterConfig: { provider: "codex", model: "changed" } }); expect((await current(id)).lifecycleState).toBe("paused"); await store.change(id, "reconcile"); await work.process(id); expect(await current(id)).toMatchObject({ lifecycleState: "verifying", lifecycleHolds: ["setup"] }); await lifecycle.updateConfiguration(id, { adapterConfig: { provider: "codex", model: "prepared" } }); await work.process(id); expect((await current(id)).lifecycleState).toBe("verifying"); const [local] = await db.select().from(environments).where(eq(environments.driver, "local")); await lifecycle.updateConfiguration(id, { defaultEnvironmentId: local.id }); await work.process(id); expect((await current(id)).lifecycleState).toBe("paused"); await lifecycle.updateConfiguration(id, { defaultEnvironmentId: environmentId }); await work.process(id); expect((await current(id)).lifecycleState).toBe("verifying"); await lifecycle.updateConfiguration(id, { defaultEnvironmentId: local.id }); expect((await current(id)).lifecycleState).toBe("pausing"); await lifecycle.updateConfiguration(id, { defaultEnvironmentId: environmentId }); await work.process(id); expect((await current(id)).lifecycleState).toBe("verifying"); const finished = await lifecycle.requestHire(companyId, input, { setupAgentId: id, runnerResolved: true }); expect(finished.id).toBe(id); await work.process(id); expect(await current(id)).toMatchObject({ lifecycleState: "ready", lifecycleHolds: [] }); expect(call.mock.calls.map(([, , request]) => request.phase).at(-1)).toBe("verifying"); }); it("does not prepare agent-owned resources before required hire approval", async () => { const pluginId = randomUUID(), environmentId = randomUUID(); await db.update(companies).set({ requireBoardApprovalForNewAgents: true }).where(eq(companies.id, companyId)); await db.insert(plugins).values({ id: pluginId, pluginKey: pluginId, packageName: "approval-computer-test", version: "1.0.0", status: "ready", manifestJson: { agentLifecycle: true, capabilities: ["agents.lifecycle.manage", "environment.drivers.register"], environmentDrivers: [{ driverKey: "tasks", requiresAgent: true, supportsTasks: true }] } as never }); await db.insert(environments).values({ id: environmentId, name: "Approval computer", driver: "plugin", status: "active", config: { pluginKey: pluginId, driverKey: "tasks", driverConfig: {} } }); const runHost = vi.fn(async () => "complete" as const); const work = worker(runHost); const lifecycle = createAgentLifecycle(db); const draft = await lifecycle.requestHire(companyId, { name: "Unapproved computer", adapterType: "paperclip_runner", adapterConfig: { provider: "codex" }, defaultEnvironmentId: environmentId }, { setupOnly: true, runnerResolved: true }); await work.process(draft.id); await lifecycle.updateConfiguration(draft.id, { adapterConfig: { provider: "codex", model: "changed" } }); await work.process(draft.id); expect(await current(draft.id)).toMatchObject({ lifecycleState: "paused", lifecycleHolds: ["setup"] }); expect(runHost).not.toHaveBeenCalled(); }); it("keeps a prepared draft prepared while its harness, model, sign-in and instructions change", async () => { const { environmentId, environment, work, phases } = await agentEnvironment(); const lifecycle = createAgentLifecycle(db); const id = randomUUID(); await lifecycle.requestHire(companyId, { id, name: "Prepared", adapterType: "paperclip_runner", adapterConfig: { provider: "codex" }, defaultEnvironmentId: environmentId }, { setupOnly: true, runnerResolved: true }); await work.process(id); const prepared = await current(id); expect(prepared.lifecycleState).toBe("verifying"); await lifecycle.updateConfiguration(id, { adapterConfig: { provider: "claude", model: "changed", env: { TOKEN: { type: "plain", value: "saved" } } } }); await lifecycle.updateConfiguration(id, { runtimeConfig: { heartbeat: { enabled: false } } }); await lifecycle.updateConfiguration(id, { defaultEnvironmentId: await environment(randomUUID(), "tasks") }); await work.process(id); expect(await current(id)).toMatchObject({ lifecycleState: "verifying", lifecycleVersion: prepared.lifecycleVersion, lifecycleOperation: { id: prepared.lifecycleOperation!.id } }); expect(phases()).toEqual(["preparing"]); await lifecycle.updateConfiguration(id, { defaultEnvironmentId: await environment(randomUUID(), "desktop") }); expect((await current(id)).lifecycleState).toBe("preparing"); await work.process(id); expect(phases()).toEqual(["preparing", "preparing"]); expect((await current(id)).lifecycleState).toBe("verifying"); }); it("verifies a prepared draft at hire without preparing it again", async () => { const { environmentId, environment, work, phases } = await agentEnvironment(); const lifecycle = createAgentLifecycle(db); const hire = async (finalEnvironmentId: string) => { const id = randomUUID(); const input = { id, name: "Hired", adapterType: "paperclip_runner", adapterConfig: { provider: "codex" }, defaultEnvironmentId: environmentId }; await lifecycle.requestHire(companyId, input, { setupOnly: true, runnerResolved: true }); await work.process(id); const prepared = await current(id); await lifecycle.requestHire(companyId, { ...input, adapterConfig: { provider: "claude", model: "chosen" }, status: "idle", defaultEnvironmentId: finalEnvironmentId }, { setupAgentId: id, runnerResolved: true, responsibleUserId: "owner" }); return { prepared, hired: await current(id) }; }; const same = await hire(environmentId); expect(same.hired).toMatchObject({ lifecycleState: "verifying", lifecycleHolds: [], lifecycleOperation: { responsibleUserId: "owner" } }); expect(same.hired.lifecycleVersion).toBeGreaterThan(same.prepared.lifecycleVersion); expect(same.hired.lifecycleOperation!.id).not.toBe(same.prepared.lifecycleOperation!.id); await work.process(same.hired.id); expect((await current(same.hired.id)).lifecycleState).toBe("ready"); expect(phases()).toEqual(["preparing", "verifying"]); expect((await hire(await environment(randomUUID(), "desktop"))).hired.lifecycleState).toBe("preparing"); }); it("joins the preparation in progress when a draft is edited and hired", async () => { let entered!: () => void, release!: () => void; const started = new Promise(resolve => { entered = resolve; }); const held = new Promise(resolve => { release = resolve; }); const { environmentId, work, phases } = await agentEnvironment(() => { entered(); return held; }); releases.push(release); const lifecycle = createAgentLifecycle(db); const id = randomUUID(); const input = { id, name: "Joined", adapterType: "paperclip_runner", adapterConfig: { provider: "codex" }, defaultEnvironmentId: environmentId }; await lifecycle.requestHire(companyId, input, { setupOnly: true, runnerResolved: true }); const processing = work.process(id); await started; const preparing = await current(id); await lifecycle.updateConfiguration(id, { adapterConfig: { provider: "claude" } }); await lifecycle.requestHire(companyId, { ...input, adapterConfig: { provider: "claude" }, status: "idle" }, { setupAgentId: id, runnerResolved: true, responsibleUserId: "owner" }); expect(await current(id)).toMatchObject({ lifecycleState: "preparing", lifecycleHolds: [], lifecycleVersion: preparing.lifecycleVersion, lifecycleOperation: { id: preparing.lifecycleOperation!.id, leaseOwner: preparing.lifecycleOperation!.leaseOwner, responsibleUserId: "owner" } }); release(); await processing; expect(await current(id)).toMatchObject({ lifecycleState: "ready", lifecycleHolds: [], lifecycleOperation: { responsibleUserId: "owner" } }); expect(phases()).toEqual(["preparing", "verifying"]); }); it("keeps a running verification when only instructions change", async () => { const { environmentId, work } = await agentEnvironment(); const lifecycle = createAgentLifecycle(db); const id = randomUUID(); await lifecycle.requestHire(companyId, { id, name: "Verified", adapterType: "paperclip_runner", adapterConfig: { provider: "codex", promptTemplate: "Old" }, defaultEnvironmentId: environmentId, status: "idle" }, { runnerResolved: true }); await db.update(agents).set({ lifecycleState: "verifying" }).where(eq(agents.id, id)); const verifying = await current(id); await lifecycle.updateConfiguration(id, { adapterConfig: { provider: "codex", instructionsBundleMode: "managed", instructionsRootPath: "/managed", instructionsEntryFile: "AGENTS.md", instructionsFilePath: "/managed/AGENTS.md" } }); expect(await current(id)).toMatchObject({ lifecycleVersion: verifying.lifecycleVersion, adapterConfig: { instructionsFilePath: "/managed/AGENTS.md" }, lifecycleOperation: { id: verifying.lifecycleOperation!.id } }); await lifecycle.updateConfiguration(id, { adapterConfig: { provider: "codex", model: "changed" } }); expect((await current(id)).lifecycleVersion).toBe(verifying.lifecycleVersion + 1); }); it("lets only the setup creator configure and retry a stopping draft with creation permission", async () => { await db.insert(companyMemberships).values(["setup-owner", "setup-other"].map(principalId => ({ companyId, principalType: "user", principalId, membershipRole: "member" }))); await db.insert(principalPermissionGrants).values(["setup-owner", "setup-other"].map(principalId => ({ companyId, principalType: "user", principalId, permissionKey: "agents:create" }))); const lifecycle = createAgentLifecycle(db); const draft = await lifecycle.requestHire(companyId, { name: "Stopping setup", adapterType: "process" }, { setupOnly: true, createdByUserId: "setup-owner" }); await db.update(agents).set({ lifecycleState: "pausing" }).where(eq(agents.id, draft.id)); function app(userId: string) { const value = express(); value.use(express.json()); value.use((req, _res, next) => { req.actor = { type: "board", source: "session", userId, companyIds: [companyId], memberships: [{ companyId, membershipRole: "member", status: "active" }] }; next(); }); value.use("/api", agentRoutes(db)); value.use(errorHandler); return value; } const updated = await request(app("setup-owner")).patch(`/api/agents/${draft.id}`).send({ runtimeConfig: { heartbeat: { enabled: false } } }); expect(updated.status, JSON.stringify(updated.body)).toBe(200); await request(app("setup-owner")).post(`/api/agents/${draft.id}/lifecycle/retry`).send({}).expect(200); await request(app("setup-other")).patch(`/api/agents/${draft.id}`).send({ runtimeConfig: { heartbeat: { enabled: true } } }).expect(403); await request(app("setup-other")).post(`/api/agents/${draft.id}/lifecycle/retry`).send({}).expect(403); await expect(lifecycle.requestHire(companyId, { name: draft.name }, { setupAgentId: draft.id })).rejects.toThrow("no longer available"); expect((await current(draft.id)).lifecycleState).toBe("pausing"); }); });