mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-16 03:31:44 +01:00
fix(projects): make workspace storage the project's only placement choice (#20835)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - A repository project gets its working files from core staging or from a workspace storage plugin > - #20459 reported that an installed storage plugin attached every new repository project, so sandbox agents could not use those projects > - #20587 fixed creation with an **Available in** choice that selects storage > - #20651 then saved an environment on every new project and used it before the agent default to place runs. That solved a problem that did not exist: the project needs storage, not a run environment > - Creation saves the first environment of the chosen group, so a project silently sends every agent's runs to that environment. Project Configuration also shows two controls (Environment and Workspace storage) for one decision > - This pull request removes the project run environment and makes storage the only placement choice a project owns. Storage is fixed at creation > - The benefit is that runs follow the agent again, and project settings show one clear, read-only fact about storage ## Linked Issues or Issue Description Refs #20459. Follow-up to #20587 and #20651. Refs #11644. That issue asks for a project-level run environment override. This pull request removes the override that #20651 added for it, because the override is not necessary to fix #20459. #11644 stays a separate feature request. Related: #18176 (open) repairs placement through the legacy workspace-policy field. This pull request does not restore project placement, so it conflicts with the direction of #18176. ## What Changed - **Remove the project run environment.** Migration `0466` drops `projects.environment_id`. Run placement no longer reads the project. Runs use the agent default, then the instance default, then local execution. The low-trust task sandbox and the enforced sandbox policies keep their precedence. - **Keep the #20651 recovery and binding guards.** A recovered run still uses its admitted environment lease (source `run`). Company-binding checks before admission and during lease creation stay. - **Keep the creation choice.** **Available in** in New Project still selects storage. It still shows only when the environments need more than one storage owner. That can only occur when a storage plugin is installed. `POST /projects` with `environmentId` selects the storage of that environment and does not save the environment. - **Fix storage at creation.** `PATCH /projects/:id` rejects any storage change with `409 project_storage_locked`, from core to a plugin and from a plugin to core. Before, a core project could move to a plugin. - **Drop legacy writes.** Older clients can still send `executionWorkspacePolicy.environmentId`. The service removes it on create and update. - **Simplify Project Configuration.** Remove the Environment and Workspace storage selectors. Show an advisory **Available in** list of the environments that can use the project's storage. Show it when a storage plugin is installed or when the project uses one. When the project's plugin is not installed, show **Storage plugin unavailable**. Without a storage plugin, show nothing. - **Issue workspace card.** Remove the "project default" environment label. - **Shared contract.** Remove `Project.environmentId`, the `environmentId` project field, and `expectedStorageProviderPluginId` from the update schema. The create schema keeps `environmentId` and `expectedStorageProviderPluginId` from #20587. - **Docs and maps.** Update `doc/project-environment-compatibility.md`, `docs/api/goals-and-projects.md`, and the project feature and agent-access maps. - **SDK source pins.** Refresh `tests/runner-e2e/source-sdk-public-inputs.json` for the two shared files. The shared file blobs return to their values before #20651. The refresh also corrects the `pnpm-lock.yaml` pin and `lockSha256`: `master` pins an older lockfile blob than the one it contains. ## Verification - Targeted server suites pass (227 tests): `project-environments`, `project-workspace-provider.integration`, `heartbeat/environment-selection`, `execution-workspace-policy`, `environment-selection-route-guards`, `environment-run-orchestrator`, `openapi-routes`. - Related server suites pass (349 tests): `environment-service`, `environment-routes`, `plugin-workspaces.integration`, `issues-service`. - Targeted UI suites pass: `ProjectProperties.concurrency`, `IssueWorkspaceCard`, `NewProjectDialog`, `NewProjectDialog.managed-sandbox`, `ProjectDetail`. - New tests: - Creation saves storage, not an environment. Core and plugin projects both reject a storage change with `project_storage_locked`. - A legacy policy `environmentId` is dropped on create and update. - A new run follows the agent default for any project. A recovered run keeps its admitted lease after the agent default changes. - Project Configuration has no storage or environment selector. The note is hidden without a plugin. It lists the core or plugin environments for the project's storage. It shows **Storage plugin unavailable** for a removed plugin. - `pnpm --filter @paperclipai/server typecheck`, `pnpm --filter @paperclipai/ui typecheck`, and `pnpm --filter @paperclipai/shared typecheck` pass. - `pnpm --filter @paperclipai/db check:migrations`, `pnpm check:token-gates`, `node scripts/check-maps.mjs`, and `node tests/runner-e2e/source-sdk-public-inputs.mjs <HEAD> --check` pass. - Not run locally: the full `pnpm test:run`, `pnpm build`, and browser suites. CI supplies that coverage. - Manual check: open Project Configuration on an instance with a storage plugin. Confirm that there is no Environment or Workspace storage selector, and that **Available in** lists the environments. ## Risks - **Migration.** `0466` drops `projects.environment_id` and its foreign key. The migration deletes the saved project environments. Apply it before you serve the new revision. An old server revision that reads `projects` fails until it stops. This is the same pattern as earlier drop-column migrations (`0229`, `0289`). - **Behavior change for existing projects.** Projects created after #20651 shipped have a saved environment that overrides the agent. After this change, their runs follow the agent default again. This is the intended fix. Before #20651, a project environment selection was not applied to runs (#11644), so no older behavior is lost. - **No path from core to a plugin.** A core project, including one created before a plugin was installed, cannot move to the plugin. To use plugin storage, create a new project. - **API.** A `PATCH` that sends `environmentId` or `expectedStorageProviderPluginId` no longer has an effect. The schema strips these fields. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) by Anthropic, in Claude Code, with tool use (shell, file edits, test runs). Extended thinking was available. ## 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 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
39442400b7
commit
369e238295
28 files changed
+295
-496
No files matched your search
@@ -6,8 +6,6 @@ import { errorHandler } from "../middleware/index.js";
|
||||
import { projectRoutes } from "../routes/projects.js";
|
||||
import { issueRoutes } from "../routes/issues.js";
|
||||
|
||||
const projectPermissions = vi.hoisted(() => ({ canWrite: true }));
|
||||
|
||||
const mockProjectService = vi.hoisted(() => ({
|
||||
create: vi.fn(),
|
||||
getById: vi.fn(),
|
||||
@@ -65,7 +63,7 @@ vi.mock("../services/index.js", () => ({
|
||||
accessService: () => ({
|
||||
canUser: vi.fn(),
|
||||
decide: vi.fn(async (input: { action: string }) => ({
|
||||
allowed: ["project:read", "issue:read"].includes(input.action) || (projectPermissions.canWrite && ["tasks:assign", "project:write_workspace"].includes(input.action)),
|
||||
allowed: ["project:read", "issue:read"].includes(input.action),
|
||||
action: input.action,
|
||||
reason: ["project:read", "issue:read"].includes(input.action) ? "allow_explicit_grant" : "deny_missing_grant",
|
||||
explanation: ["project:read", "issue:read"].includes(input.action) ? "Allowed by test default." : "Missing permission.",
|
||||
@@ -185,7 +183,6 @@ describe("execution environment route guards", () => {
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
projectPermissions.canWrite = true;
|
||||
mockProjectService.create.mockReset();
|
||||
mockProjectService.getById.mockReset();
|
||||
mockProjectService.update.mockReset();
|
||||
@@ -214,17 +211,12 @@ describe("execution environment route guards", () => {
|
||||
mockLogActivity.mockReset();
|
||||
});
|
||||
|
||||
it("accepts sandbox environments on project create", async () => {
|
||||
mockEnvironmentService.getById.mockResolvedValue({
|
||||
id: sandboxEnvironmentId,
|
||||
companyId: "company-1",
|
||||
driver: "sandbox",
|
||||
config: { provider: "fake-plugin" },
|
||||
});
|
||||
it("ignores a stale legacy policy environment on project create", async () => {
|
||||
mockEnvironmentService.getById.mockResolvedValue(null);
|
||||
mockProjectService.create.mockResolvedValue({
|
||||
id: "project-1",
|
||||
companyId: "company-1",
|
||||
name: "Sandboxed Project",
|
||||
name: "Legacy Project",
|
||||
status: "backlog",
|
||||
});
|
||||
const app = createProjectApp();
|
||||
@@ -232,35 +224,31 @@ describe("execution environment route guards", () => {
|
||||
const res = await request(app)
|
||||
.post("/api/companies/company-1/projects")
|
||||
.send({
|
||||
name: "Sandboxed Project",
|
||||
name: "Legacy Project",
|
||||
executionWorkspacePolicy: {
|
||||
enabled: true,
|
||||
environmentId: sandboxEnvironmentId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).not.toBe(422);
|
||||
expect(res.status).toBe(201);
|
||||
expect(mockEnvironmentService.getById).not.toHaveBeenCalled();
|
||||
expect(mockProjectService.create).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("accepts sandbox environments on project update", async () => {
|
||||
it("ignores a stale legacy policy environment on project update", async () => {
|
||||
mockProjectService.getById.mockResolvedValue({
|
||||
id: "project-1",
|
||||
companyId: "company-1",
|
||||
name: "Sandboxed Project",
|
||||
name: "Legacy Project",
|
||||
status: "backlog",
|
||||
archivedAt: null,
|
||||
});
|
||||
mockEnvironmentService.getById.mockResolvedValue({
|
||||
id: sandboxEnvironmentId,
|
||||
companyId: "company-1",
|
||||
driver: "sandbox",
|
||||
config: { provider: "fake-plugin" },
|
||||
});
|
||||
mockEnvironmentService.getById.mockResolvedValue(null);
|
||||
mockProjectService.update.mockResolvedValue({
|
||||
id: "project-1",
|
||||
companyId: "company-1",
|
||||
name: "Sandboxed Project",
|
||||
name: "Legacy Project",
|
||||
status: "backlog",
|
||||
});
|
||||
const app = createProjectApp();
|
||||
@@ -274,18 +262,11 @@ describe("execution environment route guards", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).not.toBe(422);
|
||||
expect(res.status).toBe(200);
|
||||
expect(mockEnvironmentService.getById).not.toHaveBeenCalled();
|
||||
expect(mockProjectService.update).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([sandboxEnvironmentId, null])("rejects a project environment change or clear without workspace-write permission (%s)", async environmentId => {
|
||||
projectPermissions.canWrite = false;
|
||||
mockProjectService.getById.mockResolvedValue({ id: "project-1", companyId: "company-1", environmentId: "other-environment" });
|
||||
const res = await request(createProjectApp()).patch("/api/projects/project-1").send({ environmentId });
|
||||
expect(res.status).toBe(403);
|
||||
expect(mockProjectService.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("accepts sandbox environments on issue create", async () => {
|
||||
mockEnvironmentService.getById.mockResolvedValue({
|
||||
id: sandboxEnvironmentId,
|
||||
|
||||
@@ -780,15 +780,6 @@ describe("operator default isolated execution workspaces", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("project-owned environment", () => {
|
||||
it("gives the project priority while keeping task trust and managed-only restrictions", () => {
|
||||
const defaults = { agentDefaultEnvironmentId: "agent-env", instanceDefaultEnvironmentId: "instance-env", localDefaultEnvironmentId: "local-env" };
|
||||
expect(resolveExecutionWorkspaceEnvironmentId({ ...defaults, projectEnvironmentId: "project-env" })).toEqual({ environmentId: "project-env", source: "project" });
|
||||
expect(resolveExecutionWorkspaceEnvironmentId({ ...defaults, projectEnvironmentId: "project-env", lowTrustIssueEnvironmentId: "task-sandbox" })).toEqual({ environmentId: "task-sandbox", source: "issue" });
|
||||
expect(resolveExecutionWorkspaceEnvironmentId({ ...defaults, projectEnvironmentId: "local-env", managedSandboxOnly: true, managedSandboxEnvironmentId: "managed-env" })).toEqual({ environmentId: "managed-env", source: "managed" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("task-scoped low-trust environment", () => {
|
||||
it("uses the explicit phone-task sandbox while preserving ordinary agent defaults", () => {
|
||||
const defaults = {agentDefaultEnvironmentId: "agent-env", instanceDefaultEnvironmentId: "instance-env", localDefaultEnvironmentId: "local-env"};
|
||||
|
||||
@@ -590,7 +590,7 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
svc.update(f.input.projectId, {
|
||||
executionWorkspacePolicy: { storageProviderPluginId: null },
|
||||
}),
|
||||
).rejects.toThrow("stays attached");
|
||||
).rejects.toMatchObject({ status: 409, details: { code: "project_storage_locked" } });
|
||||
await expect(
|
||||
svc.updateWorkspace(f.input.projectId, f.input.projectWorkspaceId, {
|
||||
repoUrl: "https://github.com/example/other.git",
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import express from "express";
|
||||
import request from "supertest";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { agents, authUsers, companyMemberships, builtInManagedResources, companies, createDb, environments, instanceSettings, plugins, projects, userCompanyPreferences } from "@paperclipai/db";
|
||||
import { projectEnvironmentOptions, resolveProjectEnvironment } from "../services/project-environments.js";
|
||||
import { agents, authUsers, companyMemberships, builtInManagedResources, companies, createDb, environments, instanceSettings, plugins, userCompanyPreferences } from "@paperclipai/db";
|
||||
import { projectEnvironmentOptions, withDefaultProjectStorage } from "../services/project-environments.js";
|
||||
import { assertProjectStorageEnvironment, projectStorageForEnvironment } from "../services/project-workspace-provider.js";
|
||||
import { environmentRunOrchestrator } from "../services/environment-run-orchestrator.js";
|
||||
import { environmentService } from "../services/environments.js";
|
||||
import { issueService } from "../services/issues.js";
|
||||
import { instanceSettingsService } from "../services/instance-settings.js";
|
||||
import { applyDefaultIsolatedExecutionWorkspacePolicy, gateProjectExecutionWorkspacePolicy, parseProjectExecutionWorkspacePolicy, resolveExecutionWorkspaceMode, buildExecutionWorkspaceAdapterConfig, resolveEffectiveWorkspaceStrategyType } from "../services/execution-workspace-policy.js";
|
||||
@@ -83,8 +80,8 @@ describe.skipIf(!support.supported)("project environment compatibility", () => {
|
||||
expect((await projectEnvironmentOptions(db, f.companyId)).defaultEnvironmentId).toBe(f.sandbox.id);
|
||||
expect((await projectEnvironmentOptions(db, f.companyId, { userId: "person" })).defaultEnvironmentId).toBe(f.computer.id);
|
||||
expect((await projectEnvironmentOptions(db, f.companyId, { agentId: f.input.agentId })).defaultEnvironmentId).toBe(f.computer.id);
|
||||
expect(await resolveProjectEnvironment(db, f.companyId, null)).toEqual({ environmentId: f.sandbox.id, executionWorkspacePolicy: null });
|
||||
expect(await resolveProjectEnvironment(db, f.companyId, null, { userId: "person" })).toMatchObject({ executionWorkspacePolicy: { storageProviderPluginId: f.pluginId } });
|
||||
expect(await withDefaultProjectStorage(db, f.companyId, null)).toBeNull();
|
||||
expect(await withDefaultProjectStorage(db, f.companyId, null, { userId: "person" })).toMatchObject({ storageProviderPluginId: f.pluginId });
|
||||
await db.update(agents).set({ defaultEnvironmentId: null }).where(eq(agents.id, f.input.agentId));
|
||||
expect((await projectEnvironmentOptions(db, f.companyId, { userId: "person" })).defaultEnvironmentId).toBe(f.sandbox.id);
|
||||
await db.update(instanceSettings).set({ defaultEnvironmentId: null });
|
||||
@@ -101,69 +98,36 @@ describe.skipIf(!support.supported)("project environment compatibility", () => {
|
||||
const computerProject = await svc.create(f.companyId, { name: "Computer project" }, { environmentId: f.computer.id });
|
||||
expect(computerProject.workspaceProvider?.pluginId).toBe(f.pluginId);
|
||||
const explicit = { enabled: true, storageProviderPluginId: null };
|
||||
expect(await resolveProjectEnvironment(db, f.companyId, explicit)).toEqual({ environmentId: null, executionWorkspacePolicy: explicit });
|
||||
await expect(resolveProjectEnvironment(db, f.companyId, explicit, { environmentId: f.computer.id })).rejects.toMatchObject({ status: 409 });
|
||||
expect(await withDefaultProjectStorage(db, f.companyId, explicit)).toEqual(explicit);
|
||||
await expect(withDefaultProjectStorage(db, f.companyId, explicit, { environmentId: f.computer.id })).rejects.toMatchObject({ status: 409 });
|
||||
await db.update(instanceSettings).set({ defaultEnvironmentId: f.computer.id });
|
||||
expect((await svc.getById(sandboxProject.id))?.workspaceProvider?.pluginId).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps environment changes on one project and snapshots creation defaults", async () => {
|
||||
it("saves storage, not a run environment, and keeps that storage after creation", async () => {
|
||||
const f = await fixture();
|
||||
const svc = projectService(db);
|
||||
const first = await svc.create(f.companyId, { name: "First project" });
|
||||
const second = await svc.create(f.companyId, { name: "Second project" });
|
||||
expect(first.environmentId).toBe(f.sandbox.id);
|
||||
expect(second.environmentId).toBe(f.sandbox.id);
|
||||
await svc.update(first.id, { environmentId: f.ssh.id, expectedStorageProviderPluginId: null });
|
||||
await db.update(instanceSettings).set({ defaultEnvironmentId: f.local.id });
|
||||
expect((await svc.getById(first.id))?.environmentId).toBe(f.ssh.id);
|
||||
expect((await svc.getById(second.id))?.environmentId).toBe(f.sandbox.id);
|
||||
expect((await svc.getById(first.id))?.executionWorkspacePolicy).toBeNull();
|
||||
expect((await db.select().from(agents).where(eq(agents.id, f.input.agentId)))[0].defaultEnvironmentId).toBeNull();
|
||||
expect((await environmentService(db).getDeleteBlastRadius(f.ssh.id))?.staticReferences.projectSelectionCount).toBe(1);
|
||||
expect(await svc.clearExecutionWorkspaceEnvironmentSelection(f.companyId, f.ssh.id)).toBe(1);
|
||||
expect((await svc.getById(first.id))?.environmentId).toBeNull();
|
||||
expect((await svc.getById(second.id))?.environmentId).toBe(f.sandbox.id);
|
||||
const core = await svc.create(f.companyId, { name: "Core project" }, { environmentId: f.ssh.id, expectedStorageProviderPluginId: null });
|
||||
const managed = await svc.create(f.companyId, { name: "Managed project" }, { environmentId: f.computer.id });
|
||||
expect(core).not.toHaveProperty("environmentId");
|
||||
expect(core.workspaceProvider?.pluginId).toBeNull();
|
||||
expect(managed.workspaceProvider?.pluginId).toBe(f.pluginId);
|
||||
await expect(svc.update(core.id, { executionWorkspacePolicy: { enabled: true, storageProviderPluginId: f.pluginId } }))
|
||||
.rejects.toMatchObject({ status: 409, details: { code: "project_storage_locked" } });
|
||||
await expect(svc.update(managed.id, { executionWorkspacePolicy: { enabled: true, storageProviderPluginId: null } }))
|
||||
.rejects.toMatchObject({ status: 409, details: { code: "project_storage_locked" } });
|
||||
expect((await svc.getById(core.id))?.workspaceProvider?.pluginId).toBeNull();
|
||||
expect((await svc.getById(managed.id))?.workspaceProvider?.pluginId).toBe(f.pluginId);
|
||||
});
|
||||
|
||||
it.each([{ enabled: true }, null])("preserves the storage owner before validating a combined environment/policy edit (%j)", async policy => {
|
||||
it("drops a legacy policy environment instead of saving it", async () => {
|
||||
const f = await fixture();
|
||||
const svc = projectService(db);
|
||||
const project = await svc.create(f.companyId, { name: "Managed project" }, { environmentId: f.computer.id });
|
||||
await expect(svc.update(project.id, { environmentId: f.local.id, executionWorkspacePolicy: policy }))
|
||||
.rejects.toMatchObject({ status: 409 });
|
||||
const saved = await svc.getById(project.id);
|
||||
expect(saved?.environmentId).toBe(f.computer.id);
|
||||
expect(saved?.executionWorkspacePolicy?.storageProviderPluginId).toBe(f.pluginId);
|
||||
});
|
||||
|
||||
it.each([false, true])("migrates a saved legacy project selection without changing its checkout policy (uppercase=%s)", async uppercase => {
|
||||
const f = await fixture();
|
||||
const migration = await readFile(new URL("../../../packages/db/src/migrations/0464_project_environment.sql", import.meta.url), "utf8");
|
||||
const project = await projectService(db).create(f.companyId, { name: "Existing project" });
|
||||
const [target] = await db.insert(environments).values({ id: "abcdefab-cdef-4abc-8def-abcdefabcdef", name: "Legacy target", driver: "ssh", config: { host: "legacy.example.test" } }).returning();
|
||||
await expect(db.transaction(async tx => {
|
||||
await tx.update(projects).set({ executionWorkspacePolicy: { enabled: false, environmentId: uppercase ? target.id.toUpperCase() : target.id } }).where(eq(projects.id, project.id));
|
||||
await tx.execute(sql`alter table projects drop column environment_id`);
|
||||
for (const statement of migration.split("--> statement-breakpoint").filter(value => value.trim())) {
|
||||
await tx.execute(sql.raw(statement));
|
||||
}
|
||||
const [saved] = await tx.select().from(projects).where(eq(projects.id, project.id));
|
||||
expect(saved.environmentId).toBe(target.id);
|
||||
expect(saved.executionWorkspacePolicy).toEqual({ enabled: false });
|
||||
throw new Error("rollback migration fixture");
|
||||
})).rejects.toThrow("rollback migration fixture");
|
||||
});
|
||||
|
||||
it("accepts legacy project policy environment edits without changing other projects", async () => {
|
||||
const f = await fixture();
|
||||
const svc = projectService(db);
|
||||
const first = await svc.create(f.companyId, { name: "Legacy editor" });
|
||||
const second = await svc.create(f.companyId, { name: "Other project" });
|
||||
const saved = await svc.update(first.id, { executionWorkspacePolicy: { enabled: false, environmentId: f.ssh.id.toUpperCase() } });
|
||||
expect(saved?.environmentId).toBe(f.ssh.id);
|
||||
expect(saved?.executionWorkspacePolicy).toEqual({ enabled: false });
|
||||
expect((await svc.getById(second.id))?.environmentId).toBe(f.sandbox.id);
|
||||
const created = await svc.create(f.companyId, { name: "Legacy create", executionWorkspacePolicy: { enabled: false, environmentId: f.ssh.id } });
|
||||
expect(created.executionWorkspacePolicy).toEqual({ enabled: false });
|
||||
const updated = await svc.update(created.id, { executionWorkspacePolicy: { enabled: false, environmentId: f.local.id } });
|
||||
expect(updated?.executionWorkspacePolicy).toEqual({ enabled: false });
|
||||
expect(updated).not.toHaveProperty("environmentId");
|
||||
});
|
||||
|
||||
it("preserves inherited task isolation when checking the observed core storage owner", async () => {
|
||||
@@ -198,15 +162,12 @@ describe.skipIf(!support.supported)("project environment compatibility", () => {
|
||||
const [otherCompany] = await db.insert(companies).values({ name: "Other", issuePrefix: "OTHER" }).returning();
|
||||
await db.insert(builtInManagedResources).values({ companyId: otherCompany.id, bundleKey: "test", resourceKind: "environment",
|
||||
resourceKey: "environment", resourceId: f.ssh.id, stockVersion: "1", stockHash: "test" });
|
||||
await expect(environmentRunOrchestrator(db).resolveEnvironment({ companyId: f.companyId, selectedEnvironmentId: f.ssh.id,
|
||||
localEnvironmentId: f.local.id })).rejects.toMatchObject({ status: 403, details: { code: "environment_company_mismatch" } });
|
||||
|
||||
await db.update(environments).set({ status: "archived" }).where(eq(environments.id, f.local.id));
|
||||
await db.update(plugins).set({ status: "disabled" }).where(eq(plugins.id, f.pluginId));
|
||||
const options = await projectEnvironmentOptions(db, f.companyId);
|
||||
expect(options.groups.flatMap(group => group.environments).map(value => value.id)).toEqual([f.sandbox.id]);
|
||||
for (const environmentId of [f.local.id, f.ssh.id, f.computer.id, randomUUID()]) {
|
||||
await expect(resolveProjectEnvironment(db, f.companyId, null, { environmentId })).rejects.toMatchObject({ status: 409 });
|
||||
await expect(withDefaultProjectStorage(db, f.companyId, null, { environmentId })).rejects.toMatchObject({ status: 409 });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -214,10 +175,10 @@ describe.skipIf(!support.supported)("project environment compatibility", () => {
|
||||
const f = await fixture();
|
||||
await db.update(instanceSettings).set({ defaultEnvironmentId: f.computer.id });
|
||||
await db.update(plugins).set({ status: "error" }).where(eq(plugins.id, f.pluginId));
|
||||
expect(await resolveProjectEnvironment(db, f.companyId, null)).toMatchObject({ executionWorkspacePolicy: { storageProviderPluginId: f.pluginId } });
|
||||
expect(await withDefaultProjectStorage(db, f.companyId, null)).toMatchObject({ storageProviderPluginId: f.pluginId });
|
||||
await db.update(environments).set({ status: "archived" }).where(eq(environments.id, f.computer.id));
|
||||
expect((await projectEnvironmentOptions(db, f.companyId)).error).toBeTruthy();
|
||||
await expect(resolveProjectEnvironment(db, f.companyId, null)).rejects.toMatchObject({ status: 409 });
|
||||
await expect(withDefaultProjectStorage(db, f.companyId, null)).rejects.toMatchObject({ status: 409 });
|
||||
});
|
||||
|
||||
it.each(["instance", "primary"] as const)("creates repository-free private tasks despite an archived %s default", async source => {
|
||||
@@ -251,7 +212,7 @@ describe.skipIf(!support.supported)("project environment compatibility", () => {
|
||||
await db.update(instanceSettings).set({ general: { executionMode: "kubernetes" } });
|
||||
const forced = await projectEnvironmentOptions(db, f.companyId);
|
||||
expect(forced.groups).toEqual([{ storageProviderPluginId: null, environments: [{ id: f.sandbox.id, name: f.sandbox.name }] }]);
|
||||
await expect(resolveProjectEnvironment(db, f.companyId, null, { environmentId: f.computer.id })).rejects.toMatchObject({ status: 409 });
|
||||
await expect(withDefaultProjectStorage(db, f.companyId, null, { environmentId: f.computer.id })).rejects.toMatchObject({ status: 409 });
|
||||
});
|
||||
|
||||
it("exposes safe groups and saves a validated choice through the normal API", async () => {
|
||||
@@ -261,8 +222,6 @@ describe.skipIf(!support.supported)("project environment compatibility", () => {
|
||||
result.use((req, _res, next) => { req.actor = actor; next(); });
|
||||
result.use("/api", projectRoutes(db)); result.use(errorHandler); return result;
|
||||
}
|
||||
await db.insert(authUsers).values({ id: "person", name: "Person", email: "person@example.test", createdAt: new Date(), updatedAt: new Date() }).onConflictDoNothing();
|
||||
await db.insert(companyMemberships).values({ companyId: f.companyId, principalType: "user", principalId: "person", status: "active", membershipRole: "member" });
|
||||
const board = app({ type: "board", userId: "person", source: "session", companyIds: [f.companyId] });
|
||||
const url = `/api/companies/${f.companyId}`;
|
||||
const options = await request(board).get(`${url}/project-environments`);
|
||||
@@ -271,20 +230,7 @@ describe.skipIf(!support.supported)("project environment compatibility", () => {
|
||||
const created = await request(board).post(`${url}/projects`).send({ name: "Selected", environmentId: f.computer.id });
|
||||
expect(created.status).toBe(201);
|
||||
expect(created.body.executionWorkspacePolicy).toMatchObject({ storageProviderPluginId: f.pluginId });
|
||||
expect(created.body.environmentId).toBe(f.computer.id);
|
||||
const first = await request(board).post(`${url}/projects`).send({ name: "First core project" });
|
||||
const second = await request(board).post(`${url}/projects`).send({ name: "Second core project" });
|
||||
const updateUrl = `/api/projects/${first.body.id}`;
|
||||
const saved = await request(board).patch(updateUrl).send({ environmentId: f.ssh.id, expectedStorageProviderPluginId: null });
|
||||
expect(saved.status).toBe(200);
|
||||
expect(saved.body.environmentId).toBe(f.ssh.id);
|
||||
expect((await request(board).get(`/api/projects/${second.body.id}`)).body.environmentId).toBe(f.sandbox.id);
|
||||
expect((await db.select().from(instanceSettings))[0].defaultEnvironmentId).toBe(f.sandbox.id);
|
||||
expect((await request(app({ type: "none" })).patch(updateUrl).send({ environmentId: f.local.id })).status).toBe(404);
|
||||
expect((await request(app({ type: "agent", agentId: f.input.agentId, companyId: randomUUID() }))
|
||||
.patch(updateUrl).send({ environmentId: f.local.id })).status).toBe(404);
|
||||
expect((await request(board).patch(updateUrl).send({ environmentId: f.computer.id, expectedStorageProviderPluginId: null })).status).toBe(409);
|
||||
expect((await projectService(db).getById(first.body.id))?.environmentId).toBe(f.ssh.id);
|
||||
expect(created.body).not.toHaveProperty("environmentId");
|
||||
const denied = app({ type: "agent", agentId: f.input.agentId, companyId: randomUUID() });
|
||||
expect((await request(denied).get(`${url}/project-environments`)).status).toBe(403);
|
||||
expect((await request(app({ type: "none" })).get(`${url}/project-environments`)).status).toBe(401);
|
||||
|
||||
@@ -114,18 +114,24 @@ describe.skipIf(!support.supported)("installed project workspace defaults", () =
|
||||
expect((await svc.update(f.input.projectId, { executionWorkspacePolicy: null }))!.workspaceProvider).toMatchObject({ source: "project", pluginId: f.pluginId });
|
||||
for (const storageProviderPluginId of [null, other.pluginId]) {
|
||||
await expect(svc.update(f.input.projectId, { executionWorkspacePolicy: { enabled: true, storageProviderPluginId } }))
|
||||
.rejects.toThrow("stays attached");
|
||||
.rejects.toMatchObject({ status: 409, details: { code: "project_storage_locked" } });
|
||||
}
|
||||
expect((await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, workspace!.id)))[0]).toEqual(workspace);
|
||||
});
|
||||
|
||||
it("lets a Core project move to a storage plugin, and then keeps it there", async () => {
|
||||
it("keeps a Core project on Core storage", async () => {
|
||||
const f = await fixture();
|
||||
const other = await createPluginWorkspaceFixture(db, f.companyId);
|
||||
await db.update(projects).set({ executionWorkspacePolicy: null }).where(eq(projects.id, f.input.projectId));
|
||||
const svc = projectService(db);
|
||||
expect((await svc.update(f.input.projectId, { executionWorkspacePolicy: { enabled: true, storageProviderPluginId: f.pluginId } }))!.workspaceProvider)
|
||||
.toMatchObject({ source: "project", pluginId: f.pluginId });
|
||||
await expect(svc.update(f.input.projectId, { executionWorkspacePolicy: { enabled: true, storageProviderPluginId: null } })).rejects.toThrow("stays attached");
|
||||
for (const storageProviderPluginId of [f.pluginId, other.pluginId]) {
|
||||
await expect(svc.update(f.input.projectId, { executionWorkspacePolicy: { enabled: true, storageProviderPluginId } }))
|
||||
.rejects.toMatchObject({ status: 409, details: { code: "project_storage_locked" } });
|
||||
}
|
||||
expect((await svc.update(f.input.projectId, { executionWorkspacePolicy: { enabled: true, storageProviderPluginId: null } }))!.workspaceProvider)
|
||||
.toMatchObject({ pluginId: null });
|
||||
expect((await svc.update(f.input.projectId, { executionWorkspacePolicy: { enabled: true, sharedWorkspaceConcurrency: "serialize" } }))!.workspaceProvider)
|
||||
.toMatchObject({ pluginId: null });
|
||||
});
|
||||
|
||||
it("preserves explicit alternatives and the explicit Core/local choice", async () => {
|
||||
|
||||
@@ -54,8 +54,6 @@ import {
|
||||
import { assertCanManageProjectWorkspaceRuntimeServices } from "./workspace-runtime-service-authz.js";
|
||||
import { getTelemetryClient } from "../telemetry.js";
|
||||
import { appendWithCap } from "../adapters/utils.js";
|
||||
import { assertEnvironmentSelectionForCompany } from "./environment-selection.js";
|
||||
import { environmentService } from "../services/environments.js";
|
||||
import { secretService } from "../services/secrets.js";
|
||||
|
||||
const WORKSPACE_CONTROL_OUTPUT_MAX_CHARS = 256 * 1024;
|
||||
@@ -87,7 +85,6 @@ export function projectRoutes(db: Db) {
|
||||
enabled: async () => (await instanceSettings.getExperimental()).enableExternalObjects === true,
|
||||
});
|
||||
const strictSecretsMode = process.env.PAPERCLIP_SECRETS_STRICT_MODE === "true";
|
||||
const environmentsSvc = environmentService(db);
|
||||
|
||||
/**
|
||||
* Managed-sandbox-only policy (`enableManagedSandboxOnly`): a project
|
||||
@@ -114,21 +111,6 @@ export function projectRoutes(db: Db) {
|
||||
);
|
||||
}
|
||||
|
||||
async function assertProjectEnvironmentSelection(companyId: string, environmentId: string | null | undefined) {
|
||||
if (environmentId === undefined || environmentId === null) return;
|
||||
await assertEnvironmentSelectionForCompany(environmentsSvc, companyId, environmentId, {
|
||||
allowedDrivers: ["local", "ssh", "sandbox"],
|
||||
});
|
||||
}
|
||||
|
||||
function readProjectPolicyEnvironmentId(policy: unknown): string | null | undefined {
|
||||
if (!policy || typeof policy !== "object" || !("environmentId" in policy)) {
|
||||
return undefined;
|
||||
}
|
||||
const environmentId = (policy as { environmentId?: unknown }).environmentId;
|
||||
return typeof environmentId === "string" || environmentId === null ? environmentId : undefined;
|
||||
}
|
||||
|
||||
async function resolveCompanyIdForProjectReference(req: Request) {
|
||||
const companyIdQuery = req.query.companyId;
|
||||
const requestedCompanyId =
|
||||
@@ -173,9 +155,9 @@ export function projectRoutes(db: Db) {
|
||||
return false;
|
||||
}
|
||||
|
||||
async function assertProjectWorkspaceWriteAllowed(req: Request, res: Response, project: { id?: string; companyId: string }) {
|
||||
async function assertProjectWorkspaceWriteAllowed(req: Request, res: Response, project: { id: string; companyId: string }) {
|
||||
const assigneeAgentId = req.actor.type === "agent" ? req.actor.agentId ?? null : null;
|
||||
const decision = await access.decide({ actor: req.actor, action: project.id ? "project:write_workspace" : "tasks:assign",
|
||||
const decision = await access.decide({ actor: req.actor, action: "project:write_workspace",
|
||||
resource: { type: "issue", companyId: project.companyId, projectId: project.id, assigneeAgentId },
|
||||
scope: { projectId: project.id, assigneeAgentId } });
|
||||
if (decision.allowed) return true;
|
||||
@@ -401,12 +383,6 @@ export function projectRoutes(db: Db) {
|
||||
projectData.privacyOwnerUserId = req.actor.type === "board" ? req.actor.userId ?? null : req.actor.type === "agent" ? req.actor.onBehalfOfUserId ?? null : null;
|
||||
const runContext = req.actor.type === "agent" && req.actor.source === "agent_jwt" && req.actor.runId
|
||||
? await projectToolContext(db, req.actor, true) : null;
|
||||
if ((environmentId || readProjectPolicyEnvironmentId(projectData.executionWorkspacePolicy))
|
||||
&& !(await assertProjectWorkspaceWriteAllowed(req, res, { companyId }))) return;
|
||||
await assertProjectEnvironmentSelection(
|
||||
companyId,
|
||||
readProjectPolicyEnvironmentId(projectData.executionWorkspacePolicy),
|
||||
);
|
||||
assertNoAgentHostWorkspaceCommandMutation(
|
||||
req,
|
||||
[
|
||||
@@ -524,9 +500,6 @@ export function projectRoutes(db: Db) {
|
||||
if (!existing) return;
|
||||
if (!(await assertProjectReadAllowed(req, res, existing))) return;
|
||||
const body = { ...req.body };
|
||||
const nextEnvironmentId = body.environmentId !== undefined ? body.environmentId : readProjectPolicyEnvironmentId(body.executionWorkspacePolicy);
|
||||
const environmentChanged = nextEnvironmentId !== undefined && nextEnvironmentId !== (existing.environmentId ?? null);
|
||||
if (environmentChanged && !(await assertProjectWorkspaceWriteAllowed(req, res, existing))) return;
|
||||
if (body.visibility !== undefined) {
|
||||
if (!(await assertCanManageProjectPrivacy(req, res, existing))) return;
|
||||
}
|
||||
@@ -535,9 +508,6 @@ export function projectRoutes(db: Db) {
|
||||
req,
|
||||
collectProjectExecutionWorkspaceCommandPaths(body.executionWorkspacePolicy),
|
||||
);
|
||||
if (environmentChanged && body.environmentId === undefined) {
|
||||
await assertProjectEnvironmentSelection(existing.companyId, nextEnvironmentId);
|
||||
}
|
||||
if (typeof body.archivedAt === "string") {
|
||||
body.archivedAt = new Date(body.archivedAt);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import { withEnvironmentLeaseController } from "./environment-lease-controller.j
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import { SANDBOX_PLATFORM_PROBE_METADATA_KEY } from "./environment-custom-image-runtime.js";
|
||||
import { and, asc, desc, eq, inArray, isNull, ne, or, sql, type SQL } from "drizzle-orm";
|
||||
import { and, asc, desc, eq, inArray, isNull, ne, sql, type SQL } from "drizzle-orm";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import {
|
||||
agents,
|
||||
@@ -1390,7 +1390,7 @@ export function environmentService(db: Db) {
|
||||
db
|
||||
.select({ count: sql<number>`count(*)::int` })
|
||||
.from(projects)
|
||||
.where(or(eq(projects.environmentId, id), sql`${projects.executionWorkspacePolicy} ->> 'environmentId' = ${id}`)),
|
||||
.where(sql`${projects.executionWorkspacePolicy} ->> 'environmentId' = ${id}`),
|
||||
db
|
||||
.select({ count: sql<number>`count(*)::int` })
|
||||
.from(companySecretBindings)
|
||||
|
||||
@@ -274,7 +274,6 @@ export function selectEnvironmentExecutionWorkspaceSettings(
|
||||
|
||||
export type ExecutionWorkspaceEnvironmentSource =
|
||||
| "issue"
|
||||
| "project"
|
||||
| "run"
|
||||
| "agent"
|
||||
| "instance"
|
||||
@@ -300,7 +299,6 @@ export class ManagedSandboxUnavailableError extends Error {
|
||||
export function resolveExecutionWorkspaceEnvironmentId(input: {
|
||||
/** Explicit operator-selected sandbox for a task with a low-trust boundary. */
|
||||
lowTrustIssueEnvironmentId?: string | null;
|
||||
projectEnvironmentId?: string | null;
|
||||
agentDefaultEnvironmentId: string | null;
|
||||
instanceDefaultEnvironmentId: string | null;
|
||||
localDefaultEnvironmentId: string;
|
||||
@@ -319,9 +317,6 @@ export function resolveExecutionWorkspaceEnvironmentId(input: {
|
||||
if (input.lowTrustIssueEnvironmentId) {
|
||||
return {environmentId: input.lowTrustIssueEnvironmentId, source: "issue"};
|
||||
}
|
||||
if (input.projectEnvironmentId) {
|
||||
return { environmentId: input.projectEnvironmentId, source: "project" };
|
||||
}
|
||||
if (input.agentDefaultEnvironmentId) {
|
||||
return {
|
||||
environmentId: input.agentDefaultEnvironmentId,
|
||||
|
||||
@@ -82,40 +82,24 @@ describe.skipIf(!support.supported)("heartbeat environment selection boundary",
|
||||
return { input, issue, project, workspace, recover };
|
||||
}
|
||||
|
||||
it("uses each project's saved environment before the shared agent and instance defaults", async () => {
|
||||
it("places a new run by the agent default, not by its project", async () => {
|
||||
const f = await fixture();
|
||||
const local = await environmentService(db).ensureLocalEnvironment(f.input.agent.companyId);
|
||||
const [target] = await db.insert(environments).values({ name: `Project host ${randomUUID()}`, driver: "ssh", config: { host: "project.example.test" } }).returning();
|
||||
const [other] = await db.insert(projects).values({ companyId: f.input.agent.companyId, name: "Other project", environmentId: local.id }).returning();
|
||||
await db.update(projects).set({ environmentId: target.id }).where(eq(projects.id, f.project.id));
|
||||
const [other] = await db.insert(projects).values({ companyId: f.input.agent.companyId, name: "Other project" }).returning();
|
||||
f.input.agent.defaultEnvironmentId = local.id;
|
||||
f.input.policy.isolatedWorkspacesEnabled = false;
|
||||
expect((await selectHeartbeatEnvironment(db, f.input)).environmentResolution).toEqual({ environmentId: target.id, source: "project" });
|
||||
expect((await selectHeartbeatEnvironment(db, f.input)).environmentResolution).toEqual({ environmentId: local.id, source: "agent" });
|
||||
f.input.task.executionProjectId = other.id;
|
||||
f.input.task.issueRef!.projectId = other.id;
|
||||
expect((await selectHeartbeatEnvironment(db, f.input)).environmentResolution).toEqual({ environmentId: local.id, source: "project" });
|
||||
f.input.task.executionProjectId = f.project.id;
|
||||
f.input.task.issueRef!.projectId = f.project.id;
|
||||
await db.update(projects).set({ environmentId: null }).where(eq(projects.id, f.project.id));
|
||||
expect((await selectHeartbeatEnvironment(db, f.input)).environmentResolution).toEqual({ environmentId: local.id, source: "agent" });
|
||||
});
|
||||
|
||||
it("keeps task project placement when files come from another project", async () => {
|
||||
const f = await fixture();
|
||||
const local = await environmentService(db).ensureLocalEnvironment(f.input.agent.companyId);
|
||||
const [source] = await db.insert(projects).values({ companyId: f.project.companyId, name: "File source" }).returning();
|
||||
await db.update(projects).set({ environmentId: local.id }).where(eq(projects.id, f.project.id));
|
||||
f.input.task.executionProjectId = source.id;
|
||||
expect((await selectHeartbeatEnvironment(db, f.input)).environmentResolution).toEqual({ environmentId: local.id, source: "project" });
|
||||
});
|
||||
|
||||
it("restores the admitted environment after a project edit and rejects a foreign lease", async () => {
|
||||
it("restores the admitted environment after a default change and rejects a foreign lease", async () => {
|
||||
const f = await fixture();
|
||||
const local = await environmentService(db).ensureLocalEnvironment(f.input.agent.companyId);
|
||||
const [next] = await db.insert(environments).values({ name: `Next target ${randomUUID()}`, driver: "ssh", config: { host: "next.example.test" } }).returning();
|
||||
const saved = await f.workspace();
|
||||
f.recover(saved.id);
|
||||
await db.update(projects).set({ environmentId: next.id }).where(eq(projects.id, f.project.id));
|
||||
f.input.agent.defaultEnvironmentId = next.id;
|
||||
const [lease] = await db.insert(environmentLeases).values({ companyId: f.project.companyId, environmentId: local.id,
|
||||
heartbeatRunId: f.input.run.id, issueId: f.issue.id, executionWorkspaceId: saved.id, metadata: { agentId: f.input.agent.id } }).returning();
|
||||
f.input.run.contextSnapshot = { paperclipEnvironment: { id: local.id, leaseId: lease.id } };
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { assertProviderApiPlacementPolicy, resolveProviderApiPlacement } from "../native-runtime/provider-api-placement.js";
|
||||
import { heartbeatExternalProvider, nativeExecutionUsesTaskWorkspace } from "./external-provider.js";
|
||||
import { heartbeatRuns, issues, projects, type Db, type agents } from "@paperclipai/db";
|
||||
import { heartbeatRuns, issues, type Db, type agents } from "@paperclipai/db";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { readGitCacheExpiration } from "../git-workspace-restoration.js";
|
||||
import type { ExecutionWorkspace, IssueExecutionWorkspaceSettings, ProjectExecutionWorkspacePolicy } from "@paperclipai/shared";
|
||||
@@ -47,7 +47,7 @@ export interface HeartbeatEnvironmentSelectionInput {
|
||||
"id" | "companyId" | "adapterType" | "adapterConfig" | "defaultEnvironmentId">;
|
||||
task: {
|
||||
freshWorkspace?: import("../fresh-workspace-continuation.js").ContinuationReadiness | null;
|
||||
issueRef: Pick<Issue, "id" | "projectWorkspaceId" | "executionWorkspaceId" | "executionWorkspacePreference"> & { projectId?: string | null } | null;
|
||||
issueRef: Pick<Issue, "id" | "projectWorkspaceId" | "executionWorkspaceId" | "executionWorkspacePreference"> | null;
|
||||
issueContext: Pick<Issue, "assigneeAgentId" | "executionWorkspaceSettings"> | null;
|
||||
issueId: string | null;
|
||||
context: Record<string, unknown>;
|
||||
@@ -203,12 +203,6 @@ export async function selectHeartbeatEnvironment(db: Db, input: HeartbeatEnviron
|
||||
const managedSandboxEnvironment = managedSandboxOnly && !providerApiPlacement
|
||||
? await environmentsSvc.findManagedSandboxEnvironment(agent.companyId)
|
||||
: null;
|
||||
const placementProjectId = issueRef ? issueRef.projectId ?? null : executionProjectId;
|
||||
const [projectEnvironment] = placementProjectId && !controllerOnly && !persistedNativeExecutionInput
|
||||
? await db.select({ environmentId: projects.environmentId }).from(projects)
|
||||
.where(and(eq(projects.id, placementProjectId), eq(projects.companyId, agent.companyId)))
|
||||
: [];
|
||||
// A project edit affects new admissions, never a surviving provider process.
|
||||
const savedEnvironment = parseObject(parseObject(run.contextSnapshot).paperclipEnvironment);
|
||||
const admittedLeaseId = persistedNativeExecutionInput && !controllerOnly
|
||||
? readNonEmptyString(savedEnvironment.leaseId) ?? readNativeWorkspaceSyncReference(persistedRunnerProfile.nativeWorkspaceSync)?.leaseId
|
||||
@@ -227,7 +221,6 @@ export async function selectHeartbeatEnvironment(db: Db, input: HeartbeatEnviron
|
||||
: resolveExecutionWorkspaceEnvironmentId({
|
||||
lowTrustIssueEnvironmentId: trustPreset.kind === "low_trust_review"
|
||||
? parseIssueExecutionWorkspaceSettings(issueContext?.executionWorkspaceSettings, {includeEnvironmentId: true})?.environmentId : null,
|
||||
projectEnvironmentId: projectEnvironment?.environmentId,
|
||||
agentDefaultEnvironmentId: agent.defaultEnvironmentId,
|
||||
instanceDefaultEnvironmentId:
|
||||
resolvedInstanceSettings.defaultEnvironmentId ?? null,
|
||||
|
||||
@@ -70,9 +70,9 @@ export async function projectEnvironmentOptions(db: Db, companyId: string, actor
|
||||
};
|
||||
}
|
||||
|
||||
export async function resolveProjectEnvironment(db: Db, companyId: string, rawPolicy: unknown, actor: ProjectCreationEnvironment = {}) {
|
||||
export async function withDefaultProjectStorage(db: Db, companyId: string, rawPolicy: unknown, actor: ProjectCreationEnvironment = {}) {
|
||||
const policy = parseProjectExecutionWorkspacePolicy(rawPolicy);
|
||||
if (policy?.storageProviderPluginId !== undefined && !actor.environmentId && actor.expectedStorageProviderPluginId === undefined) return { environmentId: null, executionWorkspacePolicy: rawPolicy };
|
||||
if (policy?.storageProviderPluginId !== undefined && !actor.environmentId && actor.expectedStorageProviderPluginId === undefined) return rawPolicy;
|
||||
const options = await projectEnvironmentOptions(db, companyId, actor);
|
||||
const environmentId = actor.environmentId?.toLowerCase() ?? options.defaultEnvironmentId;
|
||||
const group = options.groups.find(value => value.environments.some(environment => environment.id === environmentId));
|
||||
@@ -85,7 +85,7 @@ export async function resolveProjectEnvironment(db: Db, companyId: string, rawPo
|
||||
if (policy?.storageProviderPluginId !== undefined && policy.storageProviderPluginId !== pluginId) {
|
||||
throw conflict("The selected environment cannot use the requested project workspace storage.", { code: "project_environment_incompatible" });
|
||||
}
|
||||
return { environmentId: environmentId ?? null, executionWorkspacePolicy: pluginId || policy?.storageProviderPluginId !== undefined
|
||||
return pluginId || policy?.storageProviderPluginId !== undefined
|
||||
? { enabled: true, ...policy, storageProviderPluginId: pluginId }
|
||||
: rawPolicy };
|
||||
: rawPolicy;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { notifyStatusCardIssueChange } from "./status-card-notifications.js";
|
||||
import { notifyChatInboundWakeNotices } from "./chat-inbound-wake-notice-notifications.js";
|
||||
import { resolveProjectEnvironment, type ProjectCreationEnvironment } from "./project-environments.js";
|
||||
import { withDefaultProjectStorage, type ProjectCreationEnvironment } from "./project-environments.js";
|
||||
import { listProjectWorkspaceProviders, resolveProjectWorkspaceProvider } from "./project-workspace-provider.js";
|
||||
import { retireRemovedPluginWorkspaceDependents } from "./plugin-workspaces.js";
|
||||
import { DELIVERY_QUEUES, notifyDeliveryWork } from "./delivery-work-notifications.js";
|
||||
@@ -59,6 +59,12 @@ function legacyProjectEnvironmentId(policy: Record<string, unknown> | null | und
|
||||
return typeof id === "string" || id === null ? id : undefined;
|
||||
}
|
||||
|
||||
function withoutLegacyProjectEnvironment<T extends Record<string, unknown> | null | undefined>(policy: T): T {
|
||||
if (legacyProjectEnvironmentId(policy) === undefined) return policy;
|
||||
const normalized = parseProjectExecutionWorkspacePolicy(policy);
|
||||
return (normalized ? { ...normalized } : null) as T;
|
||||
}
|
||||
|
||||
type ProjectWorkspaceRow = typeof projectWorkspaces.$inferSelect;
|
||||
type WorkspaceRuntimeServiceRow = typeof workspaceRuntimeServices.$inferSelect;
|
||||
const REPO_ONLY_CWD_SENTINEL = "/__paperclip_repo_only__";
|
||||
@@ -681,14 +687,8 @@ export function projectService(db: Db, options: { captureWorkspaceUpdates?: bool
|
||||
// skipped validation whenever `goalIds: []` and `goalId` arrive
|
||||
// together (goalIds wins resolution, mirroring the update path).
|
||||
const legacyGoalId = ids?.[0] ?? null;
|
||||
const legacyEnvironmentId = legacyProjectEnvironmentId(projectData.executionWorkspacePolicy);
|
||||
const workspacePolicy = parseProjectExecutionWorkspacePolicy(projectData.executionWorkspacePolicy);
|
||||
const selected = await resolveProjectEnvironment(db, companyId,
|
||||
legacyEnvironmentId !== undefined ? workspacePolicy : projectData.executionWorkspacePolicy ?? null, {
|
||||
...environment, environmentId: environment.environmentId ?? projectData.environmentId ?? legacyEnvironmentId ?? undefined,
|
||||
});
|
||||
const executionWorkspacePolicy = selected.executionWorkspacePolicy as typeof projectData.executionWorkspacePolicy;
|
||||
projectData.environmentId = selected.environmentId;
|
||||
const executionWorkspacePolicy = await withDefaultProjectStorage(db, companyId,
|
||||
withoutLegacyProjectEnvironment(projectData.executionWorkspacePolicy) ?? null, environment) as typeof projectData.executionWorkspacePolicy;
|
||||
|
||||
return db.transaction(async (tx) => {
|
||||
const txDb = tx as unknown as Db;
|
||||
@@ -984,48 +984,28 @@ export function projectService(db: Db, options: { captureWorkspaceUpdates?: bool
|
||||
|
||||
update: async (
|
||||
id: string,
|
||||
data: Partial<typeof projects.$inferInsert> & { goalIds?: string[]; expectedStorageProviderPluginId?: string | null },
|
||||
data: Partial<typeof projects.$inferInsert> & { goalIds?: string[] },
|
||||
): Promise<ProjectWithGoals | null> => db.transaction(async tx => {
|
||||
const db = tx as unknown as Db;
|
||||
const { goalIds: inputGoalIds, expectedStorageProviderPluginId, ...projectData } = data;
|
||||
const { goalIds: inputGoalIds, ...projectData } = data;
|
||||
const ids = resolveGoalIds({ goalIds: inputGoalIds, goalId: projectData.goalId });
|
||||
const existingProject = await db
|
||||
.select({ id: projects.id, companyId: projects.companyId, name: projects.name, archivedAt: projects.archivedAt, environmentId: projects.environmentId, executionWorkspacePolicy: projects.executionWorkspacePolicy })
|
||||
.select({ id: projects.id, companyId: projects.companyId, name: projects.name, archivedAt: projects.archivedAt, executionWorkspacePolicy: projects.executionWorkspacePolicy })
|
||||
.from(projects)
|
||||
.where(eq(projects.id, id)).for("update")
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (!existingProject) return null;
|
||||
const legacyEnvironmentId = legacyProjectEnvironmentId(projectData.executionWorkspacePolicy);
|
||||
if (projectData.environmentId === undefined && legacyEnvironmentId !== undefined) {
|
||||
projectData.environmentId = legacyEnvironmentId;
|
||||
}
|
||||
if (legacyEnvironmentId !== undefined) {
|
||||
const normalized = parseProjectExecutionWorkspacePolicy(projectData.executionWorkspacePolicy);
|
||||
projectData.executionWorkspacePolicy = normalized ? { ...normalized } : null;
|
||||
}
|
||||
projectData.executionWorkspacePolicy = withoutLegacyProjectEnvironment(projectData.executionWorkspacePolicy);
|
||||
const currentStorage = parseProjectExecutionWorkspacePolicy(existingProject.executionWorkspacePolicy)?.storageProviderPluginId;
|
||||
if (currentStorage && projectData.executionWorkspacePolicy !== undefined) {
|
||||
if (projectData.executionWorkspacePolicy !== undefined) {
|
||||
const requestedStorage = parseProjectExecutionWorkspacePolicy(projectData.executionWorkspacePolicy)?.storageProviderPluginId;
|
||||
if (requestedStorage !== undefined && requestedStorage !== currentStorage) {
|
||||
throw conflict("A plugin-backed project stays attached to its storage plugin");
|
||||
if (requestedStorage !== undefined && requestedStorage !== (currentStorage ?? null)) {
|
||||
throw conflict("A project stays on the workspace storage chosen when it was created", { code: "project_storage_locked" });
|
||||
}
|
||||
if (requestedStorage === undefined) {
|
||||
if (requestedStorage === undefined && currentStorage) {
|
||||
projectData.executionWorkspacePolicy = { enabled: true, ...projectData.executionWorkspacePolicy, storageProviderPluginId: currentStorage };
|
||||
}
|
||||
}
|
||||
const requestedStorage = parseProjectExecutionWorkspacePolicy(projectData.executionWorkspacePolicy)?.storageProviderPluginId;
|
||||
const storageChanged = requestedStorage !== undefined && requestedStorage !== currentStorage;
|
||||
const selectedEnvironmentId = projectData.environmentId === undefined && storageChanged
|
||||
? existingProject.environmentId : projectData.environmentId;
|
||||
if (selectedEnvironmentId && (selectedEnvironmentId !== existingProject.environmentId || storageChanged || expectedStorageProviderPluginId !== undefined)) {
|
||||
const selected = await resolveProjectEnvironment(db, existingProject.companyId,
|
||||
projectData.executionWorkspacePolicy ?? existingProject.executionWorkspacePolicy,
|
||||
{ environmentId: selectedEnvironmentId, expectedStorageProviderPluginId });
|
||||
projectData.executionWorkspacePolicy = selected.executionWorkspacePolicy as typeof projectData.executionWorkspacePolicy;
|
||||
}
|
||||
if (!currentStorage && requestedStorage) {
|
||||
await retireRemovedPluginWorkspaceDependents(db, id);
|
||||
}
|
||||
if (ids && ids.length > 0) {
|
||||
await assertGoalsBelongToCompany(db, existingProject.companyId, ids);
|
||||
}
|
||||
@@ -1101,7 +1081,6 @@ export function projectService(db: Db, options: { captureWorkspaceUpdates?: bool
|
||||
const rows = await db
|
||||
.select({
|
||||
id: projects.id,
|
||||
environmentId: projects.environmentId,
|
||||
executionWorkspacePolicy: projects.executionWorkspacePolicy,
|
||||
})
|
||||
.from(projects)
|
||||
@@ -1109,12 +1088,10 @@ export function projectService(db: Db, options: { captureWorkspaceUpdates?: bool
|
||||
|
||||
let cleared = 0;
|
||||
for (const row of rows) {
|
||||
const policy = parseProjectExecutionWorkspacePolicy(row.executionWorkspacePolicy);
|
||||
if (row.environmentId !== environmentId && legacyProjectEnvironmentId(row.executionWorkspacePolicy) !== environmentId) continue;
|
||||
if (legacyProjectEnvironmentId(row.executionWorkspacePolicy) !== environmentId) continue;
|
||||
|
||||
await projectService(db).update(row.id, {
|
||||
environmentId: null,
|
||||
...(legacyProjectEnvironmentId(row.executionWorkspacePolicy) === environmentId ? { executionWorkspacePolicy: policy ? { ...policy } : null } : {}),
|
||||
executionWorkspacePolicy: withoutLegacyProjectEnvironment(row.executionWorkspacePolicy),
|
||||
});
|
||||
cleared += 1;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user