mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-26 18:31:34 +01:00
## Thinking Path > - Paperclip is the control plane for AI-agent companies, so execution environment selection has to stay inspectable and predictable across companies, agents, and runs. > - The environment subsystem decides where an agent heartbeat actually runs and how remote sandbox state is realized and restored. > - That subsystem previously mixed company-scoped environment catalogs with issue-level environment stamping, so a reassigned issue could keep executing in the previous assignee's sandbox. > - That behavior breaks the control-plane contract: changing the assignee should change the executing agent/environment path unless there is an explicit current override. > - Fixing it cleanly required more than a narrow patch; the environment model had to move to instance scope with a single inherited default and per-agent override semantics. > - This pull request rewires the schema, server/API surface, runtime resolution, and UI around that model, then adds regression coverage for cross-company inheritance and per-agent isolation. > - The benefit is that environment choice now follows the approved instance/agent configuration path instead of stale issue state, while shared environments only need to be configured once per instance. ## Linked Issues or Issue Description - No directly matching public GitHub issue or PR was found while searching for this refactor. ### What happened? Reassigning work between agents with different execution environments could keep running in the previous sandbox because environment choice was stamped onto the issue and outranked the current assignee. The same subsystem also forced environment catalogs to be duplicated per company even though the underlying execution environments were instance-wide resources. ### Expected behavior Execution should resolve through the current instance and agent configuration path, with one instance-scoped environment catalog, one instance default, optional per-agent override, and no stale issue-level environment authority surviving reassignment. ### Steps to reproduce 1. Configure two agents to use different execution environments. 2. Assign an issue to the first agent so the issue records execution state in that environment. 3. Reassign the same issue to the second agent and run another heartbeat. 4. Observe that the pre-fix runtime can still sync or execute in the original sandbox instead of the second agent's environment. ### Paperclip version or commit Current `master` before this PR. ### Deployment mode Self-hosted server. ### Installation method Built from source (`pnpm dev` / `pnpm build`). ### Agent adapter(s) involved - Claude Code - Not adapter-specific (core bug in environment authority / resolution) ### Database mode External Postgres. ### Access context Both board reassignment and agent heartbeats were involved. ## What Changed - Moved environments and their default selection contract to instance scope in DB/shared types, including the migration that dedupes legacy per-company environments and seeds the instance local default. - Reworked environment CRUD/auth flows to use instance-scoped APIs and added route/service coverage for instance-level environment management. - Changed runtime resolution to prefer `agent default -> instance default -> built-in local`, removed issue-level environment stamping from the active execution path, and isolated sandbox/plugin leases by `(executionWorkspaceId, agentId)`. - Added environment env-var runtime precedence so environment-provided values act as the baseline for agent execution. - Moved the environment UI into instance settings and updated agent configuration surfaces to reflect inherit/override behavior. - Added regression coverage for instance-default inheritance across companies and for the new runtime resolution behavior. - Fixed a rebase-only duplicate `enableTaskWatchdogs` flag regression in instance settings types/validators/services so the branch typechecks cleanly on current `master`. - Updated stale server tests so CI matches the shipped instance-scoped environment contract. ## Verification - `git diff --check` - `pnpm --filter @paperclipai/shared typecheck` - `pnpm --filter @paperclipai/db typecheck` - `pnpm exec vitest run server/src/__tests__/environment-runtime-driver-contract.test.ts server/src/__tests__/agent-permissions-routes.test.ts server/src/__tests__/environment-routes.test.ts server/src/__tests__/environment-instance-routes.test.ts server/src/__tests__/execution-workspace-policy.test.ts server/src/__tests__/heartbeat-plugin-environment.test.ts server/src/__tests__/instance-settings-routes.test.ts` ## Risks - The migration changes environment scope and dedupes existing rows, so installs with unusual legacy environment combinations should be reviewed carefully during upgrade. - Remote execution behavior now depends on instance-default inheritance semantics instead of issue-level stamping, so any remaining code paths that still assume issue-scoped environment authority would surface as follow-up bugs. - This PR includes both server/runtime behavior and UI relocation, so reviewers should watch for authorization edge cases around instance settings and environment management. > I checked [`ROADMAP.md`](ROADMAP.md). This work fits the existing Cloud / Sandbox agents direction as a bug-fix/refactor to current behavior, not a new parallel product surface. ## Model Used - OpenAI Codex coding agent in this Paperclip/Codex session; GPT-5-class tool-using model with code execution and shell access. The exact backend model ID is not exposed to the session runtime. ## 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 either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge
508 lines
16 KiB
TypeScript
508 lines
16 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { agents, companies, createDb, environmentLeases, environments, heartbeatRuns } from "@paperclipai/db";
|
|
import {
|
|
getEmbeddedPostgresTestSupport,
|
|
startEmbeddedPostgresTestDatabase,
|
|
} from "./helpers/embedded-postgres.js";
|
|
import { environmentService } from "../services/environments.ts";
|
|
|
|
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
|
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
|
|
|
if (!embeddedPostgresSupport.supported) {
|
|
console.warn(
|
|
`Skipping embedded Postgres environment service tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
|
|
);
|
|
}
|
|
|
|
describeEmbeddedPostgres("environmentService leases", () => {
|
|
let stopDb: (() => Promise<void>) | null = null;
|
|
let db!: ReturnType<typeof createDb>;
|
|
let svc!: ReturnType<typeof environmentService>;
|
|
|
|
beforeAll(async () => {
|
|
const started = await startEmbeddedPostgresTestDatabase("environment-service");
|
|
stopDb = started.stop;
|
|
db = createDb(started.connectionString);
|
|
svc = environmentService(db);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await db.delete(environmentLeases);
|
|
await db.delete(heartbeatRuns);
|
|
await db.delete(agents);
|
|
await db.delete(environments);
|
|
await db.delete(companies);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await stopDb?.();
|
|
});
|
|
|
|
async function seedEnvironment() {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const environmentId = randomUUID();
|
|
const runId = randomUUID();
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "CodexCoder",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {},
|
|
permissions: {},
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
await db.insert(environments).values({
|
|
id: environmentId,
|
|
name: "Lease Fixture",
|
|
driver: "ssh",
|
|
status: "active",
|
|
config: {
|
|
host: "fixture.example.test",
|
|
port: 22,
|
|
username: "fixture",
|
|
remoteWorkspacePath: "/srv/paperclip",
|
|
},
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
await db.insert(heartbeatRuns).values({
|
|
id: runId,
|
|
companyId,
|
|
agentId,
|
|
invocationSource: "manual",
|
|
status: "running",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
return { companyId, agentId, environmentId, runId };
|
|
}
|
|
|
|
it("acquires and releases a lease for a run", async () => {
|
|
const { companyId, environmentId, runId } = await seedEnvironment();
|
|
|
|
const lease = await svc.acquireLease({
|
|
companyId,
|
|
environmentId,
|
|
heartbeatRunId: runId,
|
|
metadata: { driver: "local" },
|
|
});
|
|
|
|
expect(lease.status).toBe("active");
|
|
expect(lease.heartbeatRunId).toBe(runId);
|
|
|
|
const released = await svc.releaseLease(lease.id);
|
|
|
|
expect(released?.status).toBe("released");
|
|
expect(released?.releasedAt).not.toBeNull();
|
|
});
|
|
|
|
it("releases all active leases for a run without touching unrelated rows", async () => {
|
|
const { companyId, agentId, environmentId, runId } = await seedEnvironment();
|
|
const otherRunId = randomUUID();
|
|
|
|
await db.insert(heartbeatRuns).values({
|
|
id: otherRunId,
|
|
companyId,
|
|
agentId,
|
|
invocationSource: "manual",
|
|
status: "running",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
const targetLease = await svc.acquireLease({
|
|
companyId,
|
|
environmentId,
|
|
heartbeatRunId: runId,
|
|
});
|
|
const otherLease = await svc.acquireLease({
|
|
companyId,
|
|
environmentId,
|
|
heartbeatRunId: otherRunId,
|
|
});
|
|
|
|
const released = await svc.releaseLeasesForRun(runId);
|
|
|
|
expect(released.map((lease) => lease.id)).toEqual([targetLease.id]);
|
|
|
|
const stillActive = await svc.listLeases(environmentId, { status: "active" });
|
|
expect(stillActive.map((lease) => lease.id)).toEqual([otherLease.id]);
|
|
});
|
|
|
|
it("creates and then reuses the default local environment for a company", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
const created = await svc.ensureLocalEnvironment(companyId);
|
|
const reused = await svc.ensureLocalEnvironment(companyId);
|
|
|
|
expect(created.driver).toBe("local");
|
|
expect(reused.id).toBe(created.id);
|
|
|
|
const rows = await db.select().from(environments).where(eq(environments.driver, "local"));
|
|
expect(rows).toHaveLength(1);
|
|
expect(rows[0]?.name).toBe("Local");
|
|
});
|
|
|
|
it("leaves an existing default local environment untouched", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
const archivedAt = new Date("2025-01-01T00:00:00.000Z");
|
|
const [existing] = await db
|
|
.insert(environments)
|
|
.values({
|
|
name: "Archived Local",
|
|
description: "Operator-managed local environment",
|
|
driver: "local",
|
|
status: "archived",
|
|
config: { shell: "zsh" },
|
|
metadata: { owner: "operator" },
|
|
createdAt: archivedAt,
|
|
updatedAt: archivedAt,
|
|
})
|
|
.returning();
|
|
|
|
const ensured = await svc.ensureLocalEnvironment(companyId);
|
|
|
|
expect(ensured.id).toBe(existing?.id);
|
|
expect(ensured.name).toBe("Archived Local");
|
|
expect(ensured.status).toBe("archived");
|
|
expect(ensured.metadata).toEqual({ owner: "operator" });
|
|
|
|
const rows = await db.select().from(environments).where(eq(environments.driver, "local"));
|
|
expect(rows).toHaveLength(1);
|
|
expect(rows[0]?.updatedAt.toISOString()).toBe(archivedAt.toISOString());
|
|
});
|
|
|
|
it("deduplicates concurrent default local environment creation", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
const results = await Promise.all(
|
|
Array.from({ length: 8 }, () => svc.ensureLocalEnvironment(companyId)),
|
|
);
|
|
|
|
expect(new Set(results.map((environment) => environment.id)).size).toBe(1);
|
|
|
|
const rows = await db.select().from(environments).where(eq(environments.driver, "local"));
|
|
expect(rows).toHaveLength(1);
|
|
expect(rows[0]?.driver).toBe("local");
|
|
expect(rows[0]?.status).toBe("active");
|
|
});
|
|
|
|
it("ensures, refreshes, and finds a managed Kubernetes sandbox environment", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
// No managed k8s env yet.
|
|
expect(await svc.findKubernetesEnvironment(companyId)).toBeNull();
|
|
|
|
const created = await svc.ensureKubernetesEnvironment(companyId, {
|
|
backend: "job",
|
|
inCluster: true,
|
|
runtimeClassName: "gvisor",
|
|
egressMode: "cilium",
|
|
egressAllowFqdns: ["api.anthropic.com"],
|
|
});
|
|
|
|
expect(created.driver).toBe("sandbox");
|
|
expect(created.config.provider).toBe("kubernetes");
|
|
expect(created.config.backend).toBe("job");
|
|
expect(created.config.runtimeClassName).toBe("gvisor");
|
|
expect(created.metadata?.managedKubernetesSandbox).toBe(true);
|
|
|
|
// Idempotent: second call refreshes config in place, no new row.
|
|
const refreshed = await svc.ensureKubernetesEnvironment(companyId, {
|
|
backend: "job",
|
|
inCluster: true,
|
|
egressMode: "cilium",
|
|
egressAllowFqdns: ["api.anthropic.com", "api.openai.com"],
|
|
});
|
|
expect(refreshed.id).toBe(created.id);
|
|
expect(refreshed.config.egressAllowFqdns).toEqual([
|
|
"api.anthropic.com",
|
|
"api.openai.com",
|
|
]);
|
|
|
|
const found = await svc.findKubernetesEnvironment(companyId);
|
|
expect(found?.id).toBe(created.id);
|
|
|
|
const rows = await db
|
|
.select()
|
|
.from(environments)
|
|
.where(eq(environments.driver, "sandbox"));
|
|
expect(rows.filter((row) => row.driver === "sandbox")).toHaveLength(1);
|
|
});
|
|
|
|
it("deduplicates concurrent managed Kubernetes environment creation", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
// No partial unique index covers sandbox drivers yet, so dedup is
|
|
// post-insert convergence (prefer the oldest row, delete the loser).
|
|
const results = await Promise.all(
|
|
Array.from({ length: 8 }, () =>
|
|
svc.ensureKubernetesEnvironment(companyId, { inCluster: true, backend: "job" }),
|
|
),
|
|
);
|
|
|
|
expect(new Set(results.map((environment) => environment.id)).size).toBe(1);
|
|
|
|
const rows = await db
|
|
.select()
|
|
.from(environments)
|
|
.where(eq(environments.driver, "sandbox"));
|
|
expect(rows).toHaveLength(1);
|
|
expect((rows[0]?.metadata as Record<string, unknown>)?.managedKubernetesSandbox).toBe(true);
|
|
});
|
|
|
|
it("returns a conflict when creating a second environment with the same name", async () => {
|
|
await seedEnvironment();
|
|
|
|
await svc.create({
|
|
name: "Shared Fixture",
|
|
driver: "ssh",
|
|
status: "active",
|
|
config: {
|
|
host: "fixture.example.test",
|
|
port: 22,
|
|
username: "fixture",
|
|
remoteWorkspacePath: "/srv/paperclip",
|
|
},
|
|
});
|
|
|
|
await expect(svc.create({
|
|
name: "Shared Fixture",
|
|
driver: "sandbox",
|
|
status: "active",
|
|
config: {
|
|
provider: "fake-plugin",
|
|
image: "fake:test",
|
|
reuseLease: false,
|
|
},
|
|
})).rejects.toMatchObject({
|
|
status: 409,
|
|
message: 'An environment named "Shared Fixture" already exists for this instance.',
|
|
});
|
|
});
|
|
|
|
it("returns a conflict when renaming an environment to an existing name", async () => {
|
|
const { environmentId } = await seedEnvironment();
|
|
const otherEnvironmentId = randomUUID();
|
|
const now = new Date();
|
|
|
|
await db.insert(environments).values({
|
|
id: otherEnvironmentId,
|
|
name: "Other Fixture",
|
|
driver: "sandbox",
|
|
status: "active",
|
|
config: {
|
|
provider: "fake-plugin",
|
|
image: "fake:test",
|
|
reuseLease: false,
|
|
},
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
|
|
await expect(svc.update(otherEnvironmentId, {
|
|
name: "Lease Fixture",
|
|
})).rejects.toMatchObject({
|
|
status: 409,
|
|
message: 'An environment named "Lease Fixture" already exists for this instance.',
|
|
});
|
|
|
|
const original = await svc.getById(environmentId);
|
|
expect(original?.name).toBe("Lease Fixture");
|
|
});
|
|
|
|
it("rejects a second managed-sandbox row for the same company at the DB level", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
const now = new Date();
|
|
await db.insert(environments).values({
|
|
name: "First",
|
|
driver: "sandbox",
|
|
status: "active",
|
|
config: { provider: "kubernetes" },
|
|
metadata: { managedByPaperclip: true, managedKubernetesSandbox: true },
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
|
|
// Partial unique index environments_company_managed_sandbox_idx rejects a
|
|
// second row matching driver='sandbox' AND managedByPaperclip=true for the
|
|
// same company. This is the DB-level invariant that replaced the previous
|
|
// application-side post-insert convergence loop.
|
|
const secondInsert = db.insert(environments).values({
|
|
name: "Second",
|
|
driver: "sandbox",
|
|
status: "active",
|
|
config: { provider: "kubernetes" },
|
|
metadata: { managedByPaperclip: true, managedKubernetesSandbox: true },
|
|
createdAt: new Date(now.getTime() + 1),
|
|
updatedAt: new Date(now.getTime() + 1),
|
|
});
|
|
let raisedConstraint: string | null = null;
|
|
try {
|
|
await secondInsert;
|
|
} catch (error) {
|
|
raisedConstraint =
|
|
(error as { constraint_name?: string; cause?: { constraint_name?: string } })
|
|
?.constraint_name ??
|
|
(error as { cause?: { constraint_name?: string } })?.cause?.constraint_name ??
|
|
"unknown";
|
|
}
|
|
expect(raisedConstraint).toBe("environments_managed_sandbox_idx");
|
|
|
|
// Index does NOT cover tenant-created sandbox rows (no managedByPaperclip
|
|
// marker) — operators must be able to keep multiple tenant sandbox envs.
|
|
await db.insert(environments).values({
|
|
name: "Tenant Sandbox",
|
|
driver: "sandbox",
|
|
status: "active",
|
|
config: { provider: "fake" },
|
|
metadata: { tenant: true },
|
|
createdAt: new Date(now.getTime() + 2),
|
|
updatedAt: new Date(now.getTime() + 2),
|
|
});
|
|
|
|
const rows = await db
|
|
.select()
|
|
.from(environments)
|
|
.where(eq(environments.driver, "sandbox"));
|
|
expect(rows).toHaveLength(2);
|
|
});
|
|
|
|
it("does not treat a non-kubernetes sandbox environment as the managed k8s env", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
await svc.create(companyId, {
|
|
name: "Fake Sandbox",
|
|
driver: "sandbox",
|
|
config: { provider: "fake", image: "busybox", reuseLease: false },
|
|
});
|
|
|
|
expect(await svc.findKubernetesEnvironment(companyId)).toBeNull();
|
|
});
|
|
|
|
it("ignores a config.provider=kubernetes sandbox env without the managed marker", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
// A tenant-created sandbox env with config.provider "kubernetes" but WITHOUT
|
|
// the managed metadata marker must NOT be treated as the managed k8s env,
|
|
// otherwise it would bypass the operator gVisor runtimeClass / Cilium egress.
|
|
await svc.create(companyId, {
|
|
name: "Tenant K8s Sandbox",
|
|
driver: "sandbox",
|
|
config: { provider: "kubernetes", reuseLease: false },
|
|
});
|
|
|
|
expect(await svc.findKubernetesEnvironment(companyId)).toBeNull();
|
|
|
|
// The managed env (created via ensureKubernetesEnvironment) carries the
|
|
// marker and is the only one found.
|
|
const managed = await svc.ensureKubernetesEnvironment(companyId, {
|
|
backend: "job",
|
|
inCluster: true,
|
|
runtimeClassName: "gvisor",
|
|
});
|
|
const found = await svc.findKubernetesEnvironment(companyId);
|
|
expect(found?.id).toBe(managed.id);
|
|
});
|
|
|
|
it("allows multiple SSH environments for the same company", async () => {
|
|
const companyId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Acme",
|
|
status: "active",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
const first = await svc.create(companyId, {
|
|
name: "Production SSH",
|
|
driver: "ssh",
|
|
config: { host: "prod.example.com", username: "deploy" },
|
|
});
|
|
const second = await svc.create(companyId, {
|
|
name: "Staging SSH",
|
|
driver: "ssh",
|
|
config: { host: "staging.example.com", username: "deploy" },
|
|
});
|
|
|
|
expect(first.id).not.toBe(second.id);
|
|
|
|
const rows = await db.select().from(environments);
|
|
expect(rows.filter((row) => row.driver === "ssh")).toHaveLength(2);
|
|
});
|
|
});
|