mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-04 11:01:16 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Environments give each agent run an execution target: the local host, SSH, or a sandbox provider > - A managed deployment can provision one platform-managed sandbox environment through the `PAPERCLIP_MANAGED_CONFIG` `environments` section > - That row is fully locked today. A tenant cannot add environment variables for their agents. There is also no way to hide local execution — run selection falls back to the local row > - A platform that manages the sandbox for its tenants needs both: the tenant adds env vars (and nothing else), and local execution is neither visible nor reachable > - This pull request opens exactly one tenant edit (env vars) on the managed sandbox row, and adds an `enableManagedSandboxOnly` mode that hides local and makes run selection fail closed > - The benefit is a complete managed-sandbox experience with no change for self-hosted instances ## Linked Issues or Issue Description **Subsystem affected** Environments (managed sandbox provisioning, environment routes, run environment selection) and the environments UI. **Problem or motivation** Platform-provisioned sandbox environments (`metadata.managedByPaperclip`) reject every write on cloud-managed instances. Agents often need environment variables inside their sandbox. The tenant has no way to set them on the managed row. Separately, an operator cannot remove local execution: the environment list always shows the local row, and run selection falls back to it when no default is set. **Proposed solution** Allow an envVars-only PATCH on the managed sandbox row, and echo those env vars back for editing. Add a managed-tier feature (`enableManagedSandboxOnly`) that hides the local environment from all read surfaces and redirects local-landing run selection to the managed sandbox environment, failing closed when it is unavailable. **Alternatives considered** UI-only hiding of the local row. This was rejected: it does not stop a run from resolving to local, so it is presentation without enforcement. Full unlock of the managed row was also rejected: name, driver, and config stay platform-owned so boot reconciliation cannot fight tenant edits. ## What Changed - `server/src/routes/environments.ts`: the platform-provisioned write floor admits an envVars-only PATCH on the generalized managed sandbox row (sandbox driver, `managedByPaperclip`, not legacy kubernetes-marker rows). Name, driver, config, status, metadata, and DELETE stay rejected. The read floor stops blanking env vars on that row; credential-shaped config keys stay redacted for every actor. Legacy kubernetes-marker rows keep the full floor. - Same file: under `enableManagedSandboxOnly`, the environments list and the by-id read omit the local row for every actor, including instance admins. - `server/src/services/execution-workspace-policy.ts`: `resolveExecutionWorkspaceEnvironmentId` gains the managed-sandbox-only inputs. A selection that lands on the local environment is redirected to the managed sandbox environment. With no active managed row it throws `ManagedSandboxUnavailableError` — never local. Non-local selections (ssh, user-created sandboxes) are untouched. - `server/src/services/heartbeat.ts`: the run path reads the flag, looks up the managed row (`findManagedSandboxEnvironment`, new read-only finder in `environments.ts`), and passes both to the resolver. Mirrors the forced-kubernetes precedent, which keeps precedence when both regimes are on. - `server/src/services/managed-environments.ts`: after a successful reconcile, the instance default environment moves to the managed sandbox row when the current default is unset, local, or dangling. A tenant-chosen custom environment is never overridden. - `packages/shared`: new `enableManagedSandboxOnly` key (schema default false, catalog tier `managed`, cloudDefault false, selfHostedDefault false) and the matching interface field. - UI: managed rows show a "Managed by Paperclip" lock badge; editing one opens a dedicated env-vars-only editor that sends the one PATCH shape the server admits (the old full form failed with a 403 on save). New `ui/src/lib/managed-sandbox-environment.ts` mirrors the local filter for cached lists (applied in the project picker; the agent picker already excluded local). The experimental settings page gains the toggle at its alphabetical card position. `environmentsApi.update` now declares the `envVars` field it already sent. - Tests: environment route floor coverage (envVars-only accepted, mixed bodies rejected, legacy rows still blanked and locked, local hidden and 404 under the flag, self-hosted unchanged), an embedded-postgres service test pinning that boot reconciliation never touches tenant env vars, resolver redirect/fail-closed cases, managed-environments default-stamping cases, and UI lib/settings tests. ## Verification - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/environment-routes.test.ts src/__tests__/environment-service.test.ts src/__tests__/execution-workspace-policy.test.ts src/services/managed-environments.test.ts` — all pass. - `pnpm --filter @paperclipai/shared exec vitest run` — 425 pass (catalog/schema default parity is pinned by an existing test). - `pnpm --filter @paperclipai/ui exec tsc --noEmit` and the affected UI suites (CompanyEnvironments, InstanceExperimentalSettings incl. card-order test, new lib test) — all pass. - Full workspace `pnpm test`: 3,414 passed. 17 files report failures on this machine; the identical 17 fail on a clean `origin/master` worktree in the same environment (git-worktree/skills/embedded-postgres environment dependencies and plugin-SDK zero-test collections). One additional file (`issue-monitor-scheduler.test.ts`) failed one timing-sensitive test in one of two full-suite runs and passes 7/7 in isolation on this branch — a flake in a domain this diff does not touch. The branch introduces no new failures. - Self-hosted zero-delta: every new behavior is gated on the cloud-managed instance check or the new flag, which defaults to false in schema and catalog; pinned by the "does not floor platform-marked rows on self-hosted instances" and flag-off tests. ## Risks - Behavior is opt-in twice over: the write-floor exception applies only to rows the managed-config provisioner stamps, and the hiding/forcing applies only when `enableManagedSandboxOnly` is on (default false everywhere). Self-hosted instances see no change. - The env-vars echo is scoped to the generalized managed sandbox row; legacy kubernetes-marker rows keep the blanket floor because pre-generalization builds may have written platform values there. - Fail-closed run selection means a managed instance with the flag on and an archived managed row (provider plugin down) refuses runs with a precise error instead of running locally. That is the intended posture. ## Model Used Claude Fable 5 (`claude-fable-5`), extended thinking, agentic tool use via Claude Code CLI. ## 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 - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [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
337 lines
11 KiB
TypeScript
337 lines
11 KiB
TypeScript
import express from "express";
|
|
import request from "supertest";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { environmentRoutes } from "../routes/environments.js";
|
|
import { errorHandler } from "../middleware/index.js";
|
|
|
|
const mockIssueService = vi.hoisted(() => ({
|
|
clearExecutionWorkspaceEnvironmentSelection: vi.fn(),
|
|
}));
|
|
|
|
const mockProjectService = vi.hoisted(() => ({
|
|
clearExecutionWorkspaceEnvironmentSelection: vi.fn(),
|
|
}));
|
|
|
|
const mockInstanceSettingsService = vi.hoisted(() => ({
|
|
listCompanyIds: vi.fn(),
|
|
getExperimental: vi.fn(),
|
|
}));
|
|
|
|
const mockEnvironmentService = vi.hoisted(() => ({
|
|
list: vi.fn(),
|
|
getById: vi.fn(),
|
|
create: vi.fn(),
|
|
}));
|
|
|
|
const mockEnvironmentCustomImageService = vi.hoisted(() => ({
|
|
getOverview: vi.fn(),
|
|
getActiveTemplate: vi.fn(),
|
|
getSessionById: vi.fn(),
|
|
startSetupSession: vi.fn(),
|
|
refreshSetupSession: vi.fn(),
|
|
finishSetupSession: vi.fn(),
|
|
cancelSetupSession: vi.fn(),
|
|
rollbackTemplate: vi.fn(),
|
|
disableTemplate: vi.fn(),
|
|
cleanupExpiredSetupSessions: vi.fn(),
|
|
}));
|
|
|
|
const mockExecutionWorkspaceService = vi.hoisted(() => ({
|
|
clearEnvironmentSelection: vi.fn(),
|
|
}));
|
|
|
|
const mockLogActivity = vi.hoisted(() => vi.fn());
|
|
|
|
const mockSecretService = vi.hoisted(() => ({
|
|
create: vi.fn(),
|
|
normalizeEnvBindingsForPersistence: vi.fn(),
|
|
listBindingCompanyIdsForTarget: vi.fn(),
|
|
resolveSecretValueForEphemeralAccess: vi.fn(),
|
|
syncEnvBindingsForTarget: vi.fn(),
|
|
syncSecretRefsForTarget: vi.fn(),
|
|
replaceSecretRefsForInstanceTarget: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../services/index.js", () => ({
|
|
issueService: () => mockIssueService,
|
|
instanceSettingsService: () => mockInstanceSettingsService,
|
|
environmentCustomImageService: () => mockEnvironmentCustomImageService,
|
|
logActivity: mockLogActivity,
|
|
projectService: () => mockProjectService,
|
|
}));
|
|
|
|
vi.mock("../services/environments.js", () => ({
|
|
environmentService: () => mockEnvironmentService,
|
|
}));
|
|
|
|
vi.mock("../services/execution-workspaces.js", () => ({
|
|
executionWorkspaceService: () => mockExecutionWorkspaceService,
|
|
}));
|
|
|
|
vi.mock("../services/secrets.js", () => ({
|
|
secretService: () => mockSecretService,
|
|
}));
|
|
|
|
vi.mock("../services/plugin-environment-driver.js", () => ({
|
|
listReadyPluginEnvironmentDrivers: vi.fn(async () => []),
|
|
resolvePluginSandboxProviderDriverByKey: vi.fn(async () => null),
|
|
validatePluginEnvironmentDriverConfig: vi.fn(async ({ config }) => config),
|
|
validatePluginSandboxProviderConfig: vi.fn(async ({ provider, config }) => ({
|
|
normalizedConfig: config,
|
|
pluginId: `plugin-${provider}`,
|
|
pluginKey: `plugin.${provider}`,
|
|
driver: {
|
|
driverKey: provider,
|
|
kind: "sandbox_provider",
|
|
displayName: provider,
|
|
configSchema: { type: "object" },
|
|
},
|
|
})),
|
|
startPluginEnvironmentInteractiveSetup: vi.fn(),
|
|
getPluginEnvironmentInteractiveSetup: vi.fn(),
|
|
capturePluginEnvironmentTemplate: vi.fn(),
|
|
cancelPluginEnvironmentInteractiveSetup: vi.fn(),
|
|
deletePluginEnvironmentTemplate: vi.fn(),
|
|
}));
|
|
|
|
function createEnvironment(overrides: Record<string, unknown> = {}) {
|
|
const now = new Date("2026-06-20T00:00:00.000Z");
|
|
return {
|
|
id: "env-1",
|
|
name: "Local",
|
|
description: "Default execution environment",
|
|
driver: "local",
|
|
status: "active" as const,
|
|
config: {},
|
|
envVars: {},
|
|
metadata: { managedByPaperclip: true },
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function createApp(actor: Record<string, unknown>) {
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
(req as typeof req & { actor: Record<string, unknown> }).actor = actor;
|
|
next();
|
|
});
|
|
app.use("/api", environmentRoutes({
|
|
transaction: async (fn: (tx: unknown) => Promise<unknown>) => fn({}),
|
|
} as never));
|
|
app.use(errorHandler);
|
|
return app;
|
|
}
|
|
|
|
describe("environment instance routes", () => {
|
|
beforeEach(() => {
|
|
mockIssueService.clearExecutionWorkspaceEnvironmentSelection.mockReset();
|
|
mockProjectService.clearExecutionWorkspaceEnvironmentSelection.mockReset();
|
|
mockInstanceSettingsService.listCompanyIds.mockReset();
|
|
mockInstanceSettingsService.getExperimental.mockReset();
|
|
mockInstanceSettingsService.getExperimental.mockResolvedValue({ enableManagedSandboxOnly: false });
|
|
mockEnvironmentService.list.mockReset();
|
|
mockEnvironmentService.getById.mockReset();
|
|
mockEnvironmentService.create.mockReset();
|
|
Object.values(mockEnvironmentCustomImageService).forEach((mock) => mock.mockReset());
|
|
mockEnvironmentCustomImageService.getOverview.mockResolvedValue({
|
|
activeTemplate: null,
|
|
activeSession: null,
|
|
latestSession: null,
|
|
});
|
|
mockEnvironmentCustomImageService.getActiveTemplate.mockResolvedValue(null);
|
|
mockEnvironmentCustomImageService.getSessionById.mockResolvedValue(null);
|
|
mockExecutionWorkspaceService.clearEnvironmentSelection.mockReset();
|
|
mockLogActivity.mockReset();
|
|
mockSecretService.create.mockReset();
|
|
mockSecretService.normalizeEnvBindingsForPersistence.mockReset();
|
|
mockSecretService.listBindingCompanyIdsForTarget.mockReset();
|
|
mockSecretService.resolveSecretValueForEphemeralAccess.mockReset();
|
|
mockSecretService.syncEnvBindingsForTarget.mockReset();
|
|
mockSecretService.syncSecretRefsForTarget.mockReset();
|
|
mockSecretService.replaceSecretRefsForInstanceTarget.mockReset();
|
|
mockSecretService.replaceSecretRefsForInstanceTarget.mockResolvedValue([]);
|
|
|
|
mockInstanceSettingsService.listCompanyIds.mockResolvedValue(["company-1", "company-2"]);
|
|
mockEnvironmentService.list.mockResolvedValue([]);
|
|
mockEnvironmentService.create.mockResolvedValue(createEnvironment());
|
|
mockSecretService.normalizeEnvBindingsForPersistence.mockImplementation(async (_companyId, env) => env ?? {});
|
|
mockSecretService.listBindingCompanyIdsForTarget.mockResolvedValue([]);
|
|
mockSecretService.syncEnvBindingsForTarget.mockResolvedValue([]);
|
|
mockSecretService.syncSecretRefsForTarget.mockResolvedValue([]);
|
|
});
|
|
|
|
it("lists the instance environment catalog for a local board actor", async () => {
|
|
mockEnvironmentService.list.mockResolvedValue([createEnvironment()]);
|
|
const app = createApp({
|
|
type: "board",
|
|
userId: "board-1",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
});
|
|
|
|
const res = await request(app).get("/api/companies/company-1/environments?driver=local");
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toHaveLength(1);
|
|
expect(mockEnvironmentService.list).toHaveBeenCalledWith({
|
|
status: undefined,
|
|
driver: "local",
|
|
});
|
|
});
|
|
|
|
it("allows non-admin board members with company access to read the shared environment catalog", async () => {
|
|
mockEnvironmentService.list.mockResolvedValue([createEnvironment()]);
|
|
const app = createApp({
|
|
type: "board",
|
|
userId: "user-1",
|
|
source: "session",
|
|
companyIds: ["company-1"],
|
|
memberships: [{ companyId: "company-1", membershipRole: "member", status: "active" }],
|
|
isInstanceAdmin: false,
|
|
});
|
|
|
|
const res = await request(app).get("/api/companies/company-1/environments");
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toHaveLength(1);
|
|
expect(mockEnvironmentService.list).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("rejects company agents from enumerating the shared environment catalog", async () => {
|
|
const app = createApp({
|
|
type: "agent",
|
|
agentId: "agent-1",
|
|
companyId: "company-1",
|
|
source: "agent_key",
|
|
runId: "run-1",
|
|
});
|
|
|
|
const res = await request(app).get("/api/companies/company-1/environments");
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.error).toContain("Board access required");
|
|
});
|
|
|
|
it("rejects non-admin signed-in board members from mutating instance environments", async () => {
|
|
const app = createApp({
|
|
type: "board",
|
|
userId: "user-1",
|
|
source: "session",
|
|
companyIds: ["company-1"],
|
|
isInstanceAdmin: false,
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/environments")
|
|
.send({
|
|
name: "Shared Local",
|
|
driver: "local",
|
|
config: {},
|
|
});
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.error).toContain("Instance admin");
|
|
expect(mockEnvironmentService.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("creates an instance-scoped environment and logs the mutation to every company", async () => {
|
|
const app = createApp({
|
|
type: "board",
|
|
userId: "board-1",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/environments")
|
|
.send({
|
|
name: "Shared Local",
|
|
driver: "local",
|
|
config: {},
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
expect(mockEnvironmentService.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
name: "Shared Local",
|
|
driver: "local",
|
|
status: "active",
|
|
}),
|
|
undefined,
|
|
{ db: expect.anything() },
|
|
);
|
|
expect(mockSecretService.replaceSecretRefsForInstanceTarget).toHaveBeenCalledWith(
|
|
{ targetType: "environment", targetId: "env-1" },
|
|
[],
|
|
{ db: expect.anything() },
|
|
);
|
|
expect(mockSecretService.syncEnvBindingsForTarget).toHaveBeenCalledWith(
|
|
"company-1",
|
|
{ targetType: "environment", targetId: "env-1" },
|
|
{},
|
|
{ db: expect.anything() },
|
|
);
|
|
expect(mockLogActivity).toHaveBeenCalledTimes(2);
|
|
expect(mockLogActivity.mock.calls.map((call) => call[1].companyId)).toEqual(["company-1", "company-2"]);
|
|
});
|
|
|
|
it("normalizes and syncs environment envVars on create", async () => {
|
|
const envVars = {
|
|
ANTHROPIC_API_KEY: { type: "secret_ref", secretId: "11111111-1111-4111-8111-111111111111", version: "latest" },
|
|
};
|
|
mockSecretService.normalizeEnvBindingsForPersistence.mockResolvedValue(envVars);
|
|
mockEnvironmentService.create.mockResolvedValue(createEnvironment({ envVars }));
|
|
const app = createApp({
|
|
type: "board",
|
|
userId: "board-1",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/environments")
|
|
.send({
|
|
name: "Shared Local",
|
|
driver: "local",
|
|
config: {},
|
|
envVars,
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
expect(mockSecretService.normalizeEnvBindingsForPersistence).toHaveBeenCalledWith(
|
|
"company-1",
|
|
envVars,
|
|
expect.objectContaining({ fieldPath: "envVars" }),
|
|
);
|
|
expect(mockEnvironmentService.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({ envVars }),
|
|
undefined,
|
|
{ db: expect.anything() },
|
|
);
|
|
expect(mockSecretService.syncEnvBindingsForTarget).toHaveBeenCalledWith(
|
|
"company-1",
|
|
{ targetType: "environment", targetId: "env-1" },
|
|
envVars,
|
|
{ db: expect.anything() },
|
|
);
|
|
});
|
|
|
|
it("returns full environment details for an instance admin", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue(createEnvironment({ config: { shell: "zsh" } }));
|
|
const app = createApp({
|
|
type: "board",
|
|
userId: "board-1",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
});
|
|
|
|
const res = await request(app).get("/api/environments/env-1");
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.config).toEqual({ shell: "zsh" });
|
|
});
|
|
});
|