mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 10:44:52 +01:00
fix(workspaces): guard reopen provisioning and checkpoint ancestry
Resolve authoritative trust during reopening and suppress configured and builtin host provision scripts for low-trust worktrees. Reject returned checkpoint entries whose parent is not a directory before host mutation. Keep Honeycomb links compatible with native and legacy root spans. Remove prose SQL headers without changing statements or generated separators. Update safety docs and reviewed Storybook recipe fingerprints. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
17c9b1fd62
commit
474aed9fe5
16 files changed
+195
-44
No files matched your search
@@ -11,6 +11,7 @@ import { eq } from "drizzle-orm";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
activityLog,
|
||||
agents,
|
||||
authUsers,
|
||||
companyMemberships,
|
||||
issueAccessGrants,
|
||||
@@ -63,6 +64,7 @@ describeEmbeddedPostgres("reopen archived isolated execution workspace", () => {
|
||||
await db.delete(projectWorkspaces);
|
||||
await db.delete(projects);
|
||||
await db.delete(companyMemberships);
|
||||
await db.delete(agents);
|
||||
await db.delete(companies);
|
||||
await db.delete(authUsers);
|
||||
await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
||||
@@ -258,6 +260,57 @@ describeEmbeddedPostgres("reopen archived isolated execution workspace", () => {
|
||||
.then((rows) => rows[0] ?? null);
|
||||
}
|
||||
|
||||
describe.each(["configured", "builtin"] as const)("reopen %s host provision containment", kind => {
|
||||
it.each(["task", "agent", "organization", "source", "same-source", "trusted"] as const)("honors persisted %s policy before running repository scripts", async restriction => {
|
||||
const { companyId, projectId, projectWorkspaceId } = await seedCompanyProject();
|
||||
const directory = await makeExistingDir();
|
||||
const base = join(directory, "source"), target = join(directory, "target"), marker = join(directory, "host-provision-ran");
|
||||
initManagedGitRepo(base, "reopen-feature");
|
||||
const command = `printf 'host execution\n' > '${marker}'`;
|
||||
mkdirSync(join(base, "scripts"));
|
||||
writeFileSync(join(base, "scripts", "provision-worktree.sh"), command + "\n");
|
||||
const git = (...args: string[]) => execFileSync("git", args, { cwd: base, stdio: "ignore",
|
||||
env: { ...process.env, GIT_AUTHOR_NAME: "Test", GIT_AUTHOR_EMAIL: "test@example.com", GIT_COMMITTER_NAME: "Test", GIT_COMMITTER_EMAIL: "test@example.com" } });
|
||||
git("add", "scripts"); git("commit", "-m", "Host provision fixture"); git("branch", "-f", "reopen-feature", "HEAD");
|
||||
await db.update(projectWorkspaces).set({ cwd: base }).where(eq(projectWorkspaces.id, projectWorkspaceId));
|
||||
const workspaceId = await seedClosedGitWorktreeWorkspace({ companyId, projectId, projectWorkspaceId,
|
||||
cwd: target, repoUrl: pathToFileURL(base).href, branchName: "reopen-feature" });
|
||||
if (kind === "configured") await db.update(executionWorkspaces).set({ metadata: {
|
||||
[EXECUTION_WORKSPACE_LIFECYCLE_GENERATION_METADATA_KEY]: 1, config: { provisionCommand: command },
|
||||
} }).where(eq(executionWorkspaces.id, workspaceId));
|
||||
const issueId = await seedIssue({ companyId, projectId, workspaceId, issueNumber: 4199 });
|
||||
let organizationId: string | null = restriction === "same-source" ? projectId : null;
|
||||
if (restriction === "organization") {
|
||||
organizationId = randomUUID();
|
||||
await db.insert(projects).values({ id: organizationId, companyId, name: "Organizational policy" });
|
||||
}
|
||||
const policy = { trustPreset: "low_trust_review", authorizationPolicy: { trustPreset: "low_trust_review",
|
||||
trustBoundary: { mode: "low_trust_review", companyId, rootIssueId: issueId,
|
||||
projectIds: [...new Set([projectId, ...(organizationId ? [organizationId] : [])])] } } };
|
||||
const agentId = randomUUID();
|
||||
await db.insert(agents).values({ id: agentId, companyId, name: "Reopen assignee",
|
||||
permissions: restriction === "agent" ? policy : {} });
|
||||
await db.update(issues).set({ projectId: organizationId, assigneeAgentId: agentId,
|
||||
executionPolicy: restriction === "task" ? policy : null }).where(eq(issues.id, issueId));
|
||||
if (["source", "same-source", "organization"].includes(restriction)) {
|
||||
await db.update(projects).set({ executionWorkspacePolicy: policy })
|
||||
.where(eq(projects.id, restriction === "organization" ? organizationId! : projectId));
|
||||
}
|
||||
const result = await executionWorkspaceService(db).reopenClosedIsolatedExecutionWorkspaceForIssue({
|
||||
workspaceId, issue: { id: issueId, companyId, projectId: organizationId },
|
||||
actor: { agentId: null, actorType: "user" },
|
||||
authorizationActor: { type: "board", source: "local_implicit", isInstanceAdmin: true },
|
||||
});
|
||||
expect(result).toMatchObject({ ok: true, reopened: true });
|
||||
await expect(readFile(join(target, "README.md"), "utf8")).resolves.toBe("seed\n");
|
||||
if (restriction === "trusted") await expect(readFile(marker, "utf8")).resolves.toBe("host execution\n");
|
||||
else await expect(readFile(marker, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
|
||||
expect(await readWorkspace(workspaceId)).toMatchObject({ status: "active", projectId, projectWorkspaceId });
|
||||
expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0])
|
||||
.toMatchObject({ projectId: organizationId, executionWorkspaceId: workspaceId, assigneeAgentId: agentId });
|
||||
});
|
||||
});
|
||||
|
||||
it("reopens the archived row in place, keeps the issue link, and raises the generation", async () => {
|
||||
const { companyId, projectId, projectWorkspaceId } = await seedCompanyProject();
|
||||
const cwd = await makeExistingDir();
|
||||
|
||||
@@ -3323,7 +3323,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
}
|
||||
const result = await db.transaction(async (tx): Promise<ReopenClosedIsolatedExecutionWorkspaceResult> => {
|
||||
await acquireExecutionWorkspaceLifecycleLock(tx, input.workspaceId);
|
||||
const [boundTask] = await tx.select({ id: issues.id }).from(issues).where(and(
|
||||
const [boundTask] = await tx.select().from(issues).where(and(
|
||||
eq(issues.id, issue.id), eq(issues.companyId, issue.companyId),
|
||||
eq(issues.executionWorkspaceId, input.workspaceId),
|
||||
await issueReadSqlCondition(tx, input.authorizationActor),
|
||||
@@ -3395,8 +3395,31 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
|
||||
let rebuildError: string | null = null;
|
||||
try {
|
||||
const [assignee, organization, sourceProject] = await Promise.all([
|
||||
boundTask.assigneeAgentId ? tx.select({ companyId: agents.companyId, permissions: agents.permissions }).from(agents)
|
||||
.where(and(eq(agents.id, boundTask.assigneeAgentId), eq(agents.companyId, row.companyId)))
|
||||
.then(rows => rows[0] ?? null) : null,
|
||||
boundTask.projectId ? tx.select({ companyId: projects.companyId, executionWorkspacePolicy: projects.executionWorkspacePolicy }).from(projects)
|
||||
.where(and(eq(projects.id, boundTask.projectId), eq(projects.companyId, row.companyId)))
|
||||
.then(rows => rows[0] ?? null) : null,
|
||||
row.projectId ? tx.select({ companyId: projects.companyId, executionWorkspacePolicy: projects.executionWorkspacePolicy }).from(projects)
|
||||
.where(and(eq(projects.id, row.projectId), eq(projects.companyId, row.companyId)))
|
||||
.then(rows => rows[0] ?? null) : row.projectWorkspaceId
|
||||
? tx.select({ companyId: projects.companyId, executionWorkspacePolicy: projects.executionWorkspacePolicy })
|
||||
.from(projectWorkspaces).innerJoin(projects, eq(projects.id, projectWorkspaces.projectId))
|
||||
.where(and(eq(projectWorkspaces.id, row.projectWorkspaceId), eq(projectWorkspaces.companyId, row.companyId), eq(projects.companyId, row.companyId)))
|
||||
.then(rows => rows[0] ?? null) : null,
|
||||
]);
|
||||
if ((boundTask.assigneeAgentId && !assignee) || (boundTask.projectId && !organization)
|
||||
|| ((row.projectId || row.projectWorkspaceId) && !sourceProject)) {
|
||||
throw forbidden("Execution workspace trust policy is unavailable");
|
||||
}
|
||||
const trust = resolveCoreTrustPreset({ companyId: row.companyId, agent: assignee, issue: boundTask,
|
||||
project: organization, workspaceSourceProject: sourceProject });
|
||||
if (trust.kind === "denied") throw forbidden(trust.detail);
|
||||
const realized = await ensurePersistedExecutionWorkspaceAvailable({
|
||||
db: tx as unknown as Db,
|
||||
skipHostProvision: trust.kind === "low_trust_review",
|
||||
beforeSourceCopy: verifyCopyIdentity,
|
||||
afterSourceCopy: verifyCopyIdentity,
|
||||
base: {
|
||||
|
||||
Reference in new issue
Block a user