mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 10:44:52 +01:00
fix: share canonical project workspace default precedence
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
ea970f478c
commit
753680b61c
12 files changed
+139
-92
No files matched your search
@@ -302,8 +302,9 @@ describeEmbeddedPostgres("reopen archived isolated execution workspace", () => {
|
||||
.where(eq(issues.id, issueId));
|
||||
}
|
||||
if (restriction === "nondefault") {
|
||||
await db.update(projectWorkspaces).set({ isPrimary: false }).where(eq(projectWorkspaces.id, projectWorkspaceId));
|
||||
await db.insert(projectWorkspaces).values({ companyId, projectId, name: "Earlier default",
|
||||
sourceType: "local_path", cwd: base, createdAt: new Date(0) });
|
||||
sourceType: "local_path", cwd: base, isPrimary: true, createdAt: new Date(0) });
|
||||
}
|
||||
if (restriction === "foreign-source-task") {
|
||||
const sourceIssueId = await seedIssue({ companyId, projectId, workspaceId, issueNumber: 4198 });
|
||||
|
||||
@@ -19,6 +19,7 @@ import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } fro
|
||||
import { heartbeatService } from "../services/heartbeat.ts";
|
||||
import { createHeartbeatWorkspaceResolver } from "../services/heartbeat/workspaces.js";
|
||||
import { resolveManagedProjectWorkspaceDir, resolvePaperclipInstanceRoot } from "../home-paths.js";
|
||||
import { issueService } from "../services/issues.js";
|
||||
import { accessService } from "../services/access.js";
|
||||
import { assertExecutionWorkspaceRootWriteAccess } from "../services/execution-workspace-repositories.js";
|
||||
import { instanceSettingsService } from "../services/instance-settings.ts";
|
||||
@@ -146,8 +147,11 @@ suite("heartbeat physical workspace source trust", () => {
|
||||
["configured", "both", "local"], ["builtin", "both", "local"],
|
||||
["configured", "none", "local"], ["builtin", "none", "local"],
|
||||
["configured", "both", "local_default"], ["builtin", "both", "local_default"],
|
||||
["configured", "both", "local_new_default"], ["builtin", "both", "local_new_default"],
|
||||
["configured", "both", "local_new_primary"], ["builtin", "both", "local_new_primary"],
|
||||
] as const)("places standard source %s provisioning with %s grants on %s", async (provisionKind, grants, placement) => {
|
||||
const implicitDefault = placement === "local_default";
|
||||
const implicitDefault = placement.startsWith("local_");
|
||||
const newerDefault = placement === "local_new_default" || placement === "local_new_primary";
|
||||
const companyId = randomUUID(), projectId = randomUUID(), sourceId = randomUUID(), agentId = randomUUID(), issueId = randomUUID();
|
||||
await instanceSettingsService(db).updateExperimental({ enableIsolatedWorkspaces: true });
|
||||
const cwd = path.join(root, companyId, "read-only-source");
|
||||
@@ -163,14 +167,21 @@ suite("heartbeat physical workspace source trust", () => {
|
||||
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "owner" });
|
||||
await db.insert(projects).values({ id: projectId, companyId, name: "Protected source", executionWorkspacePolicy: {
|
||||
authorizationPolicy: { assignmentPolicy: { mode: "protected" } },
|
||||
...(placement === "local_new_default" ? { defaultProjectWorkspaceId: sourceId } : {}),
|
||||
} });
|
||||
await db.insert(projectWorkspaces).values({ id: sourceId, companyId, projectId, name: "Source", sourceType: "local_path", cwd, repoUrl: pathToFileURL(cwd).href, isPrimary: true });
|
||||
if (newerDefault) await db.insert(projectWorkspaces).values({ id: randomUUID(), companyId, projectId, name: "Older source", sourceType: "local_path", cwd: path.join(root, companyId, "older-source"), isPrimary: placement === "local_new_default", createdAt: new Date(0) });
|
||||
await db.insert(projectWorkspaces).values({ id: sourceId, companyId, projectId, name: "Source", sourceType: "local_path", cwd, repoUrl: pathToFileURL(cwd).href, isPrimary: placement !== "local_new_default" });
|
||||
await db.insert(agents).values({ id: agentId, companyId, name: "Reader", status: "idle", adapterType: "codex_local", adapterConfig: { fullAuto: grants === "both" }, permissions: {} });
|
||||
await db.insert(companyMemberships).values({ companyId, principalType: "agent", principalId: agentId, status: "active", membershipRole: "member" });
|
||||
await db.insert(issues).values({ id: issueId, companyId, projectId: implicitDefault ? projectId : null, title: "Read-only isolated source", status: "todo", assigneeAgentId: agentId,
|
||||
const taskInput = { id: issueId, companyId, projectId: implicitDefault ? projectId : null, title: "Read-only isolated source", status: "todo" as const, assigneeAgentId: agentId,
|
||||
projectWorkspaceId: sourceId, executionWorkspaceSettings: { mode: "isolated_workspace", workspaceStrategy: { type: "git_worktree",
|
||||
worktreeParentDir: path.join(root, companyId, "worktrees"), ...(provisionKind === "configured" ? { provisionCommand: `printf host-script-ran > '${marker}'` } : {}) } },
|
||||
workspaceSelection: implicitDefault ? null : { version: 1, source: "explicit", selection: { kind: "configured_source", projectWorkspaceId: sourceId, mode: "managed_isolated" } } });
|
||||
workspaceSelection: implicitDefault ? null : { version: 1 as const, source: "explicit" as const, selection: { kind: "configured_source" as const, projectWorkspaceId: sourceId, mode: "managed_isolated" as const } } };
|
||||
if (newerDefault) {
|
||||
const { projectWorkspaceId: _source, workspaceSelection: _selection, ...createInput } = taskInput;
|
||||
const created = await issueService(db).create(companyId, createInput);
|
||||
expect(created.projectWorkspaceId).toBe(sourceId);
|
||||
} else await db.insert(issues).values(taskInput);
|
||||
if (grants !== "none") await db.insert(principalPermissionGrants).values({ companyId, principalType: "agent", principalId: agentId,
|
||||
permissionKey: "tasks:assign_scope", scope: { projectId, assigneeAgentId: agentId } });
|
||||
if (grants === "both") await db.insert(principalPermissionGrants).values({ companyId, principalType: "user", principalId: "responsible-user",
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { projects, type Db } from "@paperclipai/db";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { resolveProjectDefaultWorkspaceId } from "../services/project-default-workspace.js";
|
||||
import { workspaceRequiresCopyOnlyProvisioning } from "../services/workspace-host-provision.js";
|
||||
|
||||
describe("controller provision source classification", () => {
|
||||
function fixture(rows: Array<{ id: string }> = [{ id: "default-source" }]) {
|
||||
const query = { from: vi.fn(), where: vi.fn(), orderBy: vi.fn(), limit: vi.fn(async () => rows) };
|
||||
query.from.mockReturnValue(query);
|
||||
query.where.mockReturnValue(query);
|
||||
query.orderBy.mockReturnValue(query);
|
||||
const db = { select: vi.fn(() => query) } as unknown as Db;
|
||||
function fixture(rows: Array<{ id: string }> = [{ id: "default-source" }], defaultId?: string, existingRows = rows) {
|
||||
const db = { select: vi.fn(() => {
|
||||
let result: unknown[] = [];
|
||||
const query = { from: vi.fn((table) => {
|
||||
result = table === projects ? [{ executionWorkspacePolicy: defaultId ? { defaultProjectWorkspaceId: defaultId } : null }]
|
||||
: defaultId ? existingRows : rows;
|
||||
return query;
|
||||
}), where: vi.fn(), orderBy: vi.fn(), limit: vi.fn(async () => result),
|
||||
then: (resolve: (value: unknown[]) => unknown) => Promise.resolve(result).then(resolve) };
|
||||
query.where.mockReturnValue(query);
|
||||
query.orderBy.mockReturnValue(query);
|
||||
return query;
|
||||
}) } as unknown as Db;
|
||||
const input = { companyId: "company", task: { id: "task", projectId: "project" },
|
||||
sourceProjectId: "project", sourceWorkspaceId: "default-source" };
|
||||
return { db, input };
|
||||
@@ -41,4 +49,20 @@ describe("controller provision source classification", () => {
|
||||
expect(await workspaceRequiresCopyOnlyProvisioning(db, input)).toBe(true);
|
||||
expect(await workspaceRequiresCopyOnlyProvisioning(fixture([]).db, input)).toBe(true);
|
||||
});
|
||||
it("uses configured defaults before primary fallback without resolving a foreign or missing pointer into approval", async () => {
|
||||
const { db, input } = fixture([{ id: "older-source" }], "default-source", [{ id: "default-source" }]);
|
||||
expect(await workspaceRequiresCopyOnlyProvisioning(db, input)).toBe(false);
|
||||
const missing = fixture([{ id: "older-source" }], "default-source", []);
|
||||
expect(await workspaceRequiresCopyOnlyProvisioning(missing.db, missing.input)).toBe(true);
|
||||
const foreign = fixture([{ id: "older-source" }], "foreign-source");
|
||||
expect(await resolveProjectDefaultWorkspaceId(foreign.db, { companyId: "company", projectId: "project" })).toBe("foreign-source");
|
||||
expect(await workspaceRequiresCopyOnlyProvisioning(foreign.db, { ...foreign.input, sourceProjectId: "foreign-project", sourceWorkspaceId: "foreign-source" })).toBe(true);
|
||||
});
|
||||
|
||||
it("retains explicit and sticky restrictions when the configured default matches the source", async () => {
|
||||
const { db, input } = fixture([], "default-source", [{ id: "default-source" }]);
|
||||
expect(await workspaceRequiresCopyOnlyProvisioning(db, { ...input, boundWorkspace: { metadata: { _controllerProvisionCopyOnly: true } } })).toBe(true);
|
||||
expect(await workspaceRequiresCopyOnlyProvisioning(db, { ...input, task: { ...input.task,
|
||||
workspaceSelection: { version: 1, source: "explicit", selection: { kind: "configured_source", projectWorkspaceId: "default-source", mode: "managed_isolated" } } } })).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import { resolveProjectDefaultWorkspaceId } from "./project-default-workspace.js";
|
||||
import { type AuthorizationActor, executionWorkspaceReadSqlCondition, projectReadSqlCondition } from "./authorization.js";
|
||||
import { assertTaskWorkspaceAccess, assertTaskWorkspaceSourceProjectAccess } from "./task-workspace-source-access.js";
|
||||
import { type TaskWorkspaceSelection, type TaskWorkspaceIntent } from "@paperclipai/shared";
|
||||
@@ -10244,24 +10245,9 @@ export function issueService(db: Db) {
|
||||
const projectPolicy = parseProjectExecutionWorkspacePolicy(
|
||||
project?.executionWorkspacePolicy,
|
||||
);
|
||||
projectWorkspaceId = projectPolicy?.defaultProjectWorkspaceId ?? null;
|
||||
if (!projectWorkspaceId) {
|
||||
projectWorkspaceId = await tx
|
||||
.select({ id: projectWorkspaces.id })
|
||||
.from(projectWorkspaces)
|
||||
.where(
|
||||
and(
|
||||
eq(projectWorkspaces.projectId, issueData.projectId),
|
||||
eq(projectWorkspaces.companyId, companyId),
|
||||
),
|
||||
)
|
||||
.orderBy(
|
||||
desc(projectWorkspaces.isPrimary),
|
||||
asc(projectWorkspaces.createdAt),
|
||||
asc(projectWorkspaces.id),
|
||||
)
|
||||
.then((rows) => rows[0]?.id ?? null);
|
||||
}
|
||||
projectWorkspaceId = await resolveProjectDefaultWorkspaceId(tx, {
|
||||
companyId, projectId: issueData.projectId, policy: projectPolicy,
|
||||
});
|
||||
}
|
||||
if (projectWorkspaceId && workspaceSelectionActor) {
|
||||
const [readableSource] = await tx.select({ id: projectWorkspaces.id, projectId: projectWorkspaces.projectId, executionWorkspacePolicy: projects.executionWorkspacePolicy }).from(projectWorkspaces)
|
||||
@@ -10593,25 +10579,9 @@ export function issueService(db: Db) {
|
||||
const policy = parseProjectExecutionWorkspacePolicy(
|
||||
projectRow?.executionWorkspacePolicy,
|
||||
);
|
||||
let defaultProjectWorkspaceId =
|
||||
policy?.defaultProjectWorkspaceId ?? null;
|
||||
if (!defaultProjectWorkspaceId) {
|
||||
defaultProjectWorkspaceId = await tx
|
||||
.select({ id: projectWorkspaces.id })
|
||||
.from(projectWorkspaces)
|
||||
.where(
|
||||
and(
|
||||
eq(projectWorkspaces.projectId, projectId),
|
||||
eq(projectWorkspaces.companyId, companyId),
|
||||
),
|
||||
)
|
||||
.orderBy(
|
||||
desc(projectWorkspaces.isPrimary),
|
||||
asc(projectWorkspaces.createdAt),
|
||||
asc(projectWorkspaces.id),
|
||||
)
|
||||
.then((r) => r[0]?.id ?? null);
|
||||
}
|
||||
const defaultProjectWorkspaceId = await resolveProjectDefaultWorkspaceId(tx, {
|
||||
companyId, projectId, policy,
|
||||
});
|
||||
const defaultExecutionWorkspaceSettings =
|
||||
defaultIssueExecutionWorkspaceSettingsForProject(
|
||||
gateProjectExecutionWorkspacePolicy(
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { projects, projectWorkspaces, type Db } from "@paperclipai/db";
|
||||
import { and, asc, desc, eq } from "drizzle-orm";
|
||||
import { parseProjectExecutionWorkspacePolicy } from "./execution-workspace-policy.js";
|
||||
|
||||
type Reader = Db | Parameters<Parameters<Db["transaction"]>[0]>[0];
|
||||
|
||||
export async function resolveProjectDefaultWorkspaceId(db: Reader, input: {
|
||||
companyId: string;
|
||||
projectId: string;
|
||||
policy?: ReturnType<typeof parseProjectExecutionWorkspacePolicy>;
|
||||
}): Promise<string | null> {
|
||||
let policy = input.policy;
|
||||
if (policy === undefined) {
|
||||
const [project] = await db.select({ executionWorkspacePolicy: projects.executionWorkspacePolicy }).from(projects)
|
||||
.where(and(eq(projects.companyId, input.companyId), eq(projects.id, input.projectId)));
|
||||
policy = parseProjectExecutionWorkspacePolicy(project?.executionWorkspacePolicy);
|
||||
}
|
||||
if (policy?.defaultProjectWorkspaceId) return policy.defaultProjectWorkspaceId;
|
||||
const [workspace] = await db.select({ id: projectWorkspaces.id }).from(projectWorkspaces)
|
||||
.where(and(eq(projectWorkspaces.companyId, input.companyId), eq(projectWorkspaces.projectId, input.projectId)))
|
||||
.orderBy(desc(projectWorkspaces.isPrimary), asc(projectWorkspaces.createdAt), asc(projectWorkspaces.id)).limit(1);
|
||||
return workspace?.id ?? null;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import { projectWorkspaces, type Db } from "@paperclipai/db";
|
||||
import type { TaskWorkspaceIntent } from "@paperclipai/shared";
|
||||
import { and, asc, eq } from "drizzle-orm";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
|
||||
import { resolveProjectDefaultWorkspaceId } from "./project-default-workspace.js";
|
||||
|
||||
export const CONTROLLER_PROVISION_COPY_ONLY = "_controllerProvisionCopyOnly";
|
||||
|
||||
@@ -20,8 +22,11 @@ export async function workspaceRequiresCopyOnlyProvisioning(db: Db | Parameters<
|
||||
if (input.boundWorkspace?.sourceIssueId && input.boundWorkspace.sourceIssueId !== task.id) return true;
|
||||
if (intent?.source === "operator_cwd" && !input.sourceProjectId && !input.sourceWorkspaceId) return false;
|
||||
if (!input.sourceProjectId || input.sourceProjectId !== task.projectId || !input.sourceWorkspaceId) return true;
|
||||
const [defaultSource] = await db.select({ id: projectWorkspaces.id }).from(projectWorkspaces)
|
||||
.where(and(eq(projectWorkspaces.companyId, input.companyId), eq(projectWorkspaces.projectId, input.sourceProjectId)))
|
||||
.orderBy(asc(projectWorkspaces.createdAt), asc(projectWorkspaces.id)).limit(1);
|
||||
return !defaultSource || defaultSource.id !== input.sourceWorkspaceId;
|
||||
const defaultSourceId = await resolveProjectDefaultWorkspaceId(db, {
|
||||
companyId: input.companyId, projectId: input.sourceProjectId,
|
||||
});
|
||||
if (defaultSourceId !== input.sourceWorkspaceId) return true;
|
||||
const [source] = await db.select({ id: projectWorkspaces.id }).from(projectWorkspaces)
|
||||
.where(and(eq(projectWorkspaces.companyId, input.companyId), eq(projectWorkspaces.projectId, input.sourceProjectId), eq(projectWorkspaces.id, defaultSourceId))).limit(1);
|
||||
return !source;
|
||||
}
|
||||
Reference in new issue
Block a user