fix: complete Boat environment contracts and company cleanup

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-10 08:31:36 -05:00
1 parent d454d67414
commit ebb60670c1
9 files changed
+44 -11

No files matched your search

@@ -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");
});
});
@@ -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",
+2 -2
View File
@@ -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;
}
+5 -5
View File
@@ -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<typeof customImages.reconcileActiveTemplateForConfigChange>
@@ -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;
}
+7
View File
@@ -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));
+5 -1
View File
@@ -7,9 +7,10 @@ export async function assertEnvironmentSelectionForCompany(
driver: string;
status?: string | null;
config: Record<string, unknown> | null;
metadata?: Record<string, unknown> | 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.");
}
+3 -3
View File
@@ -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<string, unknown>;
metadata?: Record<string, unknown> | null;
}) => api.post<Environment>(`/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<string, unknown>;
// 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<string, unknown>;
metadata?: Record<string, unknown> | null;
+1
View File
@@ -181,6 +181,7 @@ const environmentCapabilities: EnvironmentCapabilities = {
local: "supported",
ssh: "supported",
sandbox: "supported",
computer: "supported",
plugin: "supported",
},
sandboxProviders: {
@@ -76,6 +76,7 @@ const PAPERCLIP_RUNNER_TOGGLE_SELECTOR =
function defaultExperimentalSettings(): InstanceExperimentalSettingsPayload {
return {
enableEnvironments: false,
enableBoatEnvironments: false,
enableNativeRunner: false,
enableAiConnectionRouters: false,
enableManagedSandboxOnly: false,