diff --git a/server/src/__tests__/computer-environment-selection.test.ts b/server/src/__tests__/computer-environment-selection.test.ts new file mode 100644 index 00000000000..31dacb9a31f --- /dev/null +++ b/server/src/__tests__/computer-environment-selection.test.ts @@ -0,0 +1,11 @@ +import { describe, expect, it } from "vitest"; +import { assertEnvironmentSelectionForCompany } from "../services/environment-selection.js"; + +describe("computer environment selection", () => { + const environment = { id: "environment", driver: "computer", status: "active", config: {}, metadata: { computerCompanyId: "owner" } }; + const service = { getById: async () => environment }; + it("permits the attached company and rejects another company", async () => { + await expect(assertEnvironmentSelectionForCompany(service, "owner", "environment")).resolves.toBeUndefined(); + await expect(assertEnvironmentSelectionForCompany(service, "other", "environment")).rejects.toThrow("not available in this company"); + }); +}); diff --git a/server/src/__tests__/environment-config.test.ts b/server/src/__tests__/environment-config.test.ts index faa56777305..0d51c27a35f 100644 --- a/server/src/__tests__/environment-config.test.ts +++ b/server/src/__tests__/environment-config.test.ts @@ -3,6 +3,15 @@ import { HttpError } from "../errors.js"; import { normalizeEnvironmentConfig, parseEnvironmentDriverConfig } from "../services/environment-config.ts"; describe("environment config helpers", () => { + it("stores only the Boat secret reference and rejects unmanaged credentials", () => { + const config = { provider: "boat", sandboxId: "bx_fixture", apiKeySecretRef: { + type: "secret_ref", secretId: "11111111-1111-4111-8111-111111111111", version: "latest", + }, runnerIdleTimeoutMs: 300000 }; + expect(normalizeEnvironmentConfig({ driver: "computer", config })).toEqual(config); + expect(() => normalizeEnvironmentConfig({ driver: "computer", config: { ...config, apiKey: "must-not-persist" } })).toThrow(); + expect(() => normalizeEnvironmentConfig({ driver: "computer", config: { ...config, sandboxId: "../../other" } })).toThrow(); + }); + it("normalizes SSH config into its canonical stored shape", () => { const config = normalizeEnvironmentConfig({ driver: "ssh", diff --git a/server/src/routes/computers.ts b/server/src/routes/computers.ts index a1e00466f57..719848f69a0 100644 --- a/server/src/routes/computers.ts +++ b/server/src/routes/computers.ts @@ -37,7 +37,7 @@ export function computerRoutes(db: Db, computers = computerService(db)) { .orderBy(desc(environmentLeases.updatedAt)).limit(1); const [agent] = issue.assigneeAgentId ? await db.select().from(agents) .where(and(eq(agents.companyId, issue.companyId), eq(agents.id, issue.assigneeAgentId))).limit(1) : []; - const environmentId = lease?.environmentId ?? agent?.defaultEnvironmentId ?? (await settings.getGeneral()).defaultEnvironmentId; + const environmentId = lease?.environmentId ?? agent?.defaultEnvironmentId ?? (await settings.get()).defaultEnvironmentId; const [environment] = environmentId ? await db.select().from(environments).where(eq(environments.id, environmentId)).limit(1) : []; if (!environment || environment.driver !== "computer" || environment.metadata?.computerCompanyId !== issue.companyId || environment.status !== "active") { if (req.method === "GET") { res.json(null); return; } @@ -100,7 +100,7 @@ export function computerRoutes(db: Db, computers = computerService(db)) { router.use((error: unknown, _req: import("express").Request, res: import("express").Response, next: import("express").NextFunction) => { if (!(error instanceof ComputerError)) return next(error); - res.status(error.code === "conflict" ? 409 : error.code === "invalid" ? 422 : error.code === "not_found" ? 404 : 502).json({ error: error.message, code: error.code }); + res.status(error.code === "conflict" ? 409 : error.code === "invalid" ? 422 : error.code === "not_found" ? 404 : error.code === "forbidden" ? 403 : 502).json({ error: error.message, code: error.code }); }); return router; } diff --git a/server/src/routes/environments.ts b/server/src/routes/environments.ts index b29668df5e7..f831c25c313 100644 --- a/server/src/routes/environments.ts +++ b/server/src/routes/environments.ts @@ -1032,7 +1032,7 @@ export function environmentRoutes( const input = { ...req.body, ...(req.body.driver === "computer" ? { - status: "inactive" as const, + status: "archived" as const, metadata: { ...req.body.metadata, computerCompanyId: companyId }, } : {}), envVars: await secrets.normalizeEnvBindingsForPersistence( @@ -1276,7 +1276,7 @@ export function environmentRoutes( if (parsed.driver !== "computer") throw unprocessable("Invalid Boat configuration."); await computerService(db).attach({ companyId: computerCompanyId, environmentId: environment.id, sandboxId: parsed.config.sandboxId, apiKeySecretRef: parsed.config.apiKeySecretRef }); - if (req.body.status !== "inactive") environment = (await svc.update(environment.id, { status: "active" }))!; + if (req.body.status !== "archived") environment = (await svc.update(environment.id, { status: "active" }))!; } let customImageReconciliation: Awaited< ReturnType @@ -1323,7 +1323,7 @@ export function environmentRoutes( if (typeof companyId !== "string") throw unprocessable("Computer attachment has no company."); assertCompanyAccess(req, companyId); if (impact.staticReferences.isInstanceDefault) throw conflict("Choose another default environment before disconnecting this computer."); - await svc.update(existing.id, { status: "inactive" }); + await svc.update(existing.id, { status: "archived" }); await computerService(db).detach({ companyId, environmentId: existing.id }); await Promise.all([ executionWorkspaces.clearEnvironmentSelection(companyId, existing.id), @@ -1332,7 +1332,7 @@ export function environmentRoutes( ]); await logInstanceEnvironmentActivity({ actor, action: "environment.disconnected", entityId: existing.id, details: { name: existing.name, driver: "computer", filesRetained: true } }); - res.json({ ...existing, status: "inactive", disconnected: true, destroyedReusableSandboxLeaseCount: 0 }); + res.json({ ...existing, status: "archived", disconnected: true, destroyedReusableSandboxLeaseCount: 0 }); return; } // With explicit consent, destroy the environment's reusable sandbox leases @@ -1541,7 +1541,7 @@ export function environmentRoutes( router.use((error: unknown, _req: import("express").Request, res: import("express").Response, next: import("express").NextFunction) => { if (!(error instanceof ComputerError)) return next(error); - res.status(error.code === "conflict" ? 409 : error.code === "invalid" ? 422 : error.code === "not_found" ? 404 : 502).json({ error: error.message, code: error.code }); + res.status(error.code === "conflict" ? 409 : error.code === "invalid" ? 422 : error.code === "not_found" ? 404 : error.code === "forbidden" ? 403 : 502).json({ error: error.message, code: error.code }); }); return router; } diff --git a/server/src/services/company-deletion.ts b/server/src/services/company-deletion.ts index 4ab40f03e69..bb41cfedf98 100644 --- a/server/src/services/company-deletion.ts +++ b/server/src/services/company-deletion.ts @@ -1,6 +1,8 @@ import type { Db } from "@paperclipai/db"; import { eq, inArray } from "drizzle-orm"; import { createAgentLifecycle, scheduleAgentLifecycle } from "./agent-lifecycle.js"; +import { computerService } from "../modules/computers/index.js"; +import { computerCompanyDeletion } from "../modules/computers/company-deletion.js"; import { agentLifecycleCompanyDeletion } from "../modules/agent-lifecycle/company-deletion.js"; import { companies, @@ -47,6 +49,10 @@ export async function deleteCompany(db: Db, id: string) { await lifecycle.terminateAgent(agent.id); await scheduleAgentLifecycle(db, agent.id); } + const computers = computerService(db); + for (const computer of await computers.list(id)) { + await computers.detach({ companyId: id, environmentId: computer.environmentId }); + } return db.transaction(async (tx) => { // Exclude accounting writers before taking child locks. KEY SHARE must // remain compatible: native writers can already hold a child row while @@ -83,6 +89,7 @@ export async function deleteCompany(db: Db, id: string) { await tx.delete(issueComments).where(eq(issueComments.companyId, id)); await tx.delete(approvalComments).where(eq(approvalComments.companyId, id)); await tx.delete(approvals).where(eq(approvals.companyId, id)); + await computerCompanyDeletion.deleteCompanyData(tx, id); await tx.delete(companySecrets).where(eq(companySecrets.companyId, id)); await tx.delete(joinRequests).where(eq(joinRequests.companyId, id)); await tx.delete(invites).where(eq(invites.companyId, id)); diff --git a/server/src/services/environment-selection.ts b/server/src/services/environment-selection.ts index eba3d8772c3..6c755e656bf 100644 --- a/server/src/services/environment-selection.ts +++ b/server/src/services/environment-selection.ts @@ -7,9 +7,10 @@ export async function assertEnvironmentSelectionForCompany( driver: string; status?: string | null; config: Record | null; + metadata?: Record | null; } | null>; }, - _companyId: string, + companyId: string, environmentId: string | null | undefined, options?: { allowedDrivers?: string[]; @@ -21,6 +22,9 @@ export async function assertEnvironmentSelectionForCompany( if (!environment) { throw unprocessable("Environment not found."); } + if (environment.driver === "computer" && environment.metadata?.computerCompanyId !== companyId) { + throw unprocessable("Computer environment is not available in this company."); + } if (environment.status === "archived") { throw unprocessable("Environment is archived."); } diff --git a/ui/src/api/environments.ts b/ui/src/api/environments.ts index 3e9c8d4da5e..c50b32e4960 100644 --- a/ui/src/api/environments.ts +++ b/ui/src/api/environments.ts @@ -127,14 +127,14 @@ export const environmentsApi = { create: (companyId: string, body: { name: string; description?: string | null; - driver: "local" | "ssh" | "sandbox" | "plugin"; + driver: Environment["driver"]; config?: Record; metadata?: Record | null; }) => api.post(`/companies/${companyId}/environments`, body), update: (environmentId: string, body: { name?: string; description?: string | null; - driver?: "local" | "ssh" | "sandbox" | "plugin"; + driver?: Environment["driver"]; status?: "active" | "archived"; config?: Record; // The only field accepted on platform-managed environments (the server @@ -161,7 +161,7 @@ export const environmentsApi = { ), probeConfig: (companyId: string, body: { name?: string; - driver: "local" | "ssh" | "sandbox" | "plugin"; + driver: Environment["driver"]; description?: string | null; config?: Record; metadata?: Record | null; diff --git a/ui/src/pages/Agents.test.tsx b/ui/src/pages/Agents.test.tsx index b0586dea5a1..c277f0885b8 100644 --- a/ui/src/pages/Agents.test.tsx +++ b/ui/src/pages/Agents.test.tsx @@ -181,6 +181,7 @@ const environmentCapabilities: EnvironmentCapabilities = { local: "supported", ssh: "supported", sandbox: "supported", + computer: "supported", plugin: "supported", }, sandboxProviders: { diff --git a/ui/src/pages/InstanceExperimentalSettings.test.tsx b/ui/src/pages/InstanceExperimentalSettings.test.tsx index 28ec402a2dc..b636df9431f 100644 --- a/ui/src/pages/InstanceExperimentalSettings.test.tsx +++ b/ui/src/pages/InstanceExperimentalSettings.test.tsx @@ -76,6 +76,7 @@ const PAPERCLIP_RUNNER_TOGGLE_SELECTOR = function defaultExperimentalSettings(): InstanceExperimentalSettingsPayload { return { enableEnvironments: false, + enableBoatEnvironments: false, enableNativeRunner: false, enableAiConnectionRouters: false, enableManagedSandboxOnly: false,