mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-28 12:04:41 +01:00
<!-- entire-trail-link-start --> https://entire.com/gh/paperclipai/-/trails/1/changes/paperclip/1 <!-- entire-trail-link-end --> ## Thinking Path > - Paperclip manages agents and their execution environments. > - New agent setup saves an inactive agent before model connection tests. > - Some plugins need that agent to prepare its execution resource. > - The wizard tested without the agent ID, while the setup hold prevented preparation. > - Environment changes also discarded the selected model account. > - This change prepares the saved draft during setup and keeps normal execution held. > - Users can test the prepared resource before they finish hiring. ## Linked Issues or Issue Description **What happened?** An agent-owned plugin environment could not acquire a test lease during hiring. Changing environments also returned the user to Connect and cleared the selected account. **Expected behavior** Keep the selected account, invalidate its test, and prepare the saved agent resource in the background. Test that same agent before finishing setup. **Steps to reproduce** 1. Start a new Claude or Codex agent and select a saved account. 2. Change to an agent-owned plugin environment. 3. Try to finish setup or reconnect the account. Related: #17750 defers probes until after hire submission. This change implements preparation during the wizard instead. The related PR search and ROADMAP review found no other implementation of this setup preparation path. ## What Changed - Add the optional `requiresAgent` driver declaration. Require agent lifecycle participation when it is set. - Prepare saved drafts on creation or environment changes. Keep the setup hold and wait in `verifying` without background verification scans. - Bind tests to the saved agent. Return a warning without allocating a lease when the required agent is absent or still preparing. - Preserve the account and configuration step when the environment changes. Save the selection and invalidate the old test. - Show active preparation and support authorized retries. Keep approval, company, budget, and normal task admission controls. - Document the contract and cover lifecycle, lease, manifest, and wizard behavior. ## Verification - Server and UI TypeScript checks passed. - 112 wizard and setup helper tests passed. The shared manifest suite passed. - 123 lifecycle, policy, lease, and environment-test API cases passed after the approval-gate fix. The diagnostic and driver suites and all 80 agent adapter route tests also passed. - Token gates, module boundaries, map validation, and whitespace checks passed. - All 66 lifecycle/policy cases passed after the stopping-draft permission fix. The new real-DB route case verifies creator success, other-creator denial, and refusal to complete a hire while stopping. - Full workspace typecheck and build were attempted. Both stop at the unchanged Rust Runner step because Cargo is not installed on this host. - CI on43be3c93dpassed all automated checks, including full test shards, typecheck, build, Rust checks, browser suites, and canary checks: https://github.com/paperclipai/paperclip/actions/runs/40368174530. - The full local repository test command was started. Two unchanged workspace tests assume Git's initial branch is master; this host defaults to main. Both fail in isolation under the host default and pass with process-scoped init.defaultBranch=master. The broad run was stopped after those confirmed fixture failures; CI supplies the full clean check. - Early new-test failures were fixture collisions and an unscoped sweep assertion; the fixtures and assertion were corrected. One route-suite load raced with SDK output regeneration; rebuilding first and running the suite afterward passed. - Initial CI exposed a route mock missing the new prerequisite lookup. The fixture was updated and a real HTTP no-lease case was added; all 54 cases in that route suite pass. Security review identified allocation before required approval; the new approval gate and its tests address it. - CI on50b5a08afreported 32 of 33 expected calls in the unchanged cleanup-history scheduling test. Its isolated case passed locally without cleanup-code changes; the original failure remains recorded and the final CI run passes on the current head. - Standard Greptile reviewed43be3c93dat 5/5. A subsequent deeper review identified runner selection, stale detail reads, error retention, and test file size issues; 70343387b addresses all four with resolved threads. Standard review returned 5/5 on that commit and again on rebased headfda42c5ab. The rebase preserves master's new managed environment skill declarations alongside requiresAgent. Full CI onfda42c5abpasses: 55 successful checks, two intentional skips, no failures. Run: https://github.com/paperclipai/paperclip/actions/runs/40405121641. - After those fixes, 133 UI tests and 61 lifecycle/policy cases pass; all eight focused UI regressions fail against the previous implementation and pass with the fix. Token, map, and whitespace gates pass. The stale-read fixture initially omitted the setup URL because navigation is mocked; following the URL activates the real detail query and reproduces the race. - UI TypeScript passes. This host's server dependency links now point to a separate scratch checkout lacking requiresAgent, so the ordinary server check reports that missing property. A no-emit check pinned to this branch's shared declarations passes; it disables declaration generation and supplies explicit local type roots. The first temporary configuration lacked those type roots; the corrected configuration passes. CI remains the full workspace verification. - After rebasing onto8373d7867, all 133 UI, 61 lifecycle/policy, and 44 manifest cases pass again, as do UI TypeScript, token, map, and whitespace checks. The server no-emit check now hits three mismatched SDK/Runner declarations from the scratch dependency links; these are outside this change. Full CI builds the complete dependency set. - Review cycle: the first Apex review scored 3/5 with four findings. All were fixed together, the branch was rebased to resolve a schema conflict, full CI and standard Greptile passed, and a second Apex review returned 5/5 onfda42c5abwith zero new comments. All six review threads are resolved, and the PR is conflict-free. Review: https://github.com/paperclipai/paperclip/pull/17761#issuecomment-6422635718. - No live provider resource was created and no deployment was performed. ## Risks Plugins must opt in and ship a compatible declaration. Companies requiring hire approval keep drafts paused and defer allocation and verification until approval. Other companies prepare drafts during the wizard under their existing agent-creation authority; the setup hold still blocks ordinary work. Preparation uses existing lifecycle ownership, retry, and cleanup paths. The open wizard polls only while preparation or stopping is active. No database migration is required. ## Model Used OpenAI Codex, based on GPT-6, with reasoning, tool use, and code execution. The exact serving model ID and context window size are not exposed. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have described the issue in-PR using the bug report fields - [x] I have not referenced internal or instance-local issues or links - [x] My branch name describes the change and contains no internal ticket id - [x] I have run the targeted tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation - [x] I have considered and documented the risks - [x] All Paperclip gates are green — 55 successful checks and two intentional skips onfda42c5ab- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
157 lines
12 KiB
TypeScript
157 lines
12 KiB
TypeScript
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, 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 } 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<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
|
let db: Db;
|
|
let companyId: string;
|
|
const workers: ReturnType<typeof configureAgentLifecycle>[] = [];
|
|
beforeAll(async () => { database = await startEmbeddedPostgresTestDatabase("paperclip-agent-setup-"); db = createDb(database.connectionString); }, 90_000);
|
|
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: (agent: LifecycleAgent) => Promise<"complete" | "pending"> = 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");
|
|
});
|
|
|
|
it("prepares agent-dependent drafts, stays idle until finish, and invalidates changed environments", async () => {
|
|
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: [{ driverKey: "tasks", requiresAgent: true, supportsTasks: true }] } as never });
|
|
await db.insert(environments).values({ id: environmentId, name: "Agent-owned test computer", driver: "plugin", status: "active",
|
|
config: { pluginKey: pluginId, driverKey: "tasks", driverConfig: {} } });
|
|
const call = vi.fn(async (_id, _method, input) => ({ operationId: input.operationId, version: input.version, status: "complete" }));
|
|
const driver = createLifecycleDriver(db, { call } as never);
|
|
const runHost = vi.fn(async () => "complete" as const);
|
|
const work = configureAgentLifecycle(db, { ...driver, requiredPluginIds: async () => [pluginId], runHost });
|
|
workers.push(work);
|
|
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("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");
|
|
});
|
|
});
|