mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 23:06:17 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Each agent run can record spend. Paperclip stores spend in cost
events and finance events, and it links each event to a company, an
agent, a project, and a task.
> - Operators delete a project when the work is cancelled or complete.
The delete service removes the project row and the project workspace
rows.
> - Two kinds of rows keep a reference to the project and block the
delete: tasks, and cost or finance events. The database rejects the
delete. The route returns `500 {"error":"Internal server error"}`.
> - The message hides the cause. A project delete also has no clear
behaviour for remaining tasks.
> - This pull request adds an explicit result. The service returns `409`
with the code `project_tasks_pending` when tasks remain. The service
clears the project link on cost and finance events, so spend history
survives and the delete completes.
> - The benefit is a predictable delete path, no lost spend history, and
no opaque `500`.
## Linked Issues or Issue Description
Related pull requests: Refs #10817, Refs #9145. Related issue: Refs
#11062 (the same foreign key problem on agent delete).
**What happened?**
`DELETE /api/projects/:id` returns `500 {"error":"Internal server
error"}` in two cases:
1. The project still has tasks. `issues.project_id` has no delete
action, so the database rejects the delete of the project row.
2. The project has cost events or finance events.
`cost_events.project_id` and `finance_events.project_id` have no delete
action, so the database rejects the same delete.
The server log shows the raw Drizzle error `update or delete on table
"projects" violates foreign key constraint`. The API client only sees
the generic `500`.
**Expected behavior**
The delete returns a result that names the blocking state:
- Tasks remain: `409` with a message that tells the operator to move or
delete the tasks.
- Cost or finance events remain: the delete completes. The spend rows
stay, and their project link becomes `null`.
**Steps to reproduce**
1. Create a project.
2. Create one task in the project.
3. Call `DELETE /api/projects/<id>`. Result: `500 Internal server
error`.
4. Delete the task, then call `DELETE /api/projects/<id>` again. Result:
`200`.
5. Create a second project. Record one cost event for it, then call
`DELETE /api/projects/<id>`. Result: `500 Internal server error`.
**Paperclip version or commit**
2026.1106.0 (self-hosted). The same code is on `master` at the time of
this pull request.
**Deployment mode**
Self-hosted, local, single operator.
**Installation method**
`npm install -g paperclipai`.
**Agent adapter(s) involved**
Not adapter-specific (core bug).
**Database mode**
External PostgreSQL 17 in Docker.
## What Changed
- `projectService.remove` clears `cost_events.project_id` and
`finance_events.project_id` before it deletes the project row.
- `projectService.remove` returns `409` with the code
`project_tasks_pending` when the project still has tasks.
- New test file `server/src/__tests__/project-delete-references.test.ts`
covers both cases.
## Verification
- `pnpm --filter @paperclipai/plugin-sdk build`
- `cd server && ./node_modules/.bin/vitest run
src/__tests__/project-delete-references.test.ts`
- The first test adds one cost event and one finance event to a project.
It deletes the project, then confirms that both rows still exist with a
`null` project link, and that the project row is gone.
- The second test adds a task to a project. It checks that the delete
returns `409`, and that the project row stays.
- Manual check on a self-hosted instance (2026.1106.0): project with 58
cost events returned `500` before the change. After the same query that
the service now runs, the delete returned `200`.
## Risks
- Behaviour change: a project delete with remaining tasks returns `409`
instead of `500`. The project row stays.
- Cost and finance rows keep their amounts and their company link. They
lose only the project attribution. No spend rows are deleted.
- No migration. No schema change. No data change outside the delete
path.
## Model Used
- Harness: Hermes Agent (Nous Research)
- Provider: opencode-go
- Model: `deepseek-v4.1-flash`
- Capabilities used: tool use, code execution, file edits, browser
- Context window: provider default. The task used a small part of it.
## 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
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge
---------
Co-authored-by: hungpt99-dev <hungpt99-dev@users.noreply.github.com>
Co-authored-by: commitperclip[bot] <284812584+commitperclip[bot]@users.noreply.github.com>
221 lines
17 KiB
TypeScript
221 lines
17 KiB
TypeScript
import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises";
|
|
import { createHash } from "node:crypto";
|
|
import { materializeAsset } from "./runtime-context.js";
|
|
import { taskBridgeApiRequest } from "./paperclip-runner-tool-authority.js";
|
|
import { join } from "node:path";
|
|
import { tmpdir } from "node:os";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { executeWorkspaceTool, readAssignedSkill, readWorkspaceUploadFile, runnerBridgeDefinitions, workspaceCommandSandboxAvailable, settleRunnerBridgeRead } from "./runner-bridge-tools.js";
|
|
const roots: string[] = [];
|
|
afterEach(async () => { await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))); });
|
|
const root = async () => { const directory = await mkdtemp(join(tmpdir(), "dot-bridge-")); roots.push(directory); return directory; };
|
|
const authorize = async () => {};
|
|
describe("Runner workspace bridge", () => {
|
|
it("guards new writes, existing content, traversal and symlinks", async () => {
|
|
const directory = await root(), outside = await root();
|
|
const result = await executeWorkspaceTool(directory, "workspace_write", { path: "hello.txt", text: "hello 🌍", expectedSha256: null }, authorize) as any;
|
|
expect(result.byteSize).toBe(Buffer.byteLength("hello 🌍"));
|
|
expect(await executeWorkspaceTool(directory, "workspace_read", { path: "hello.txt" }, authorize)).toMatchObject({ text: "hello 🌍", sha256: result.sha256 });
|
|
await expect(executeWorkspaceTool(directory, "workspace_write", { path: "hello.txt", text: "overwrite", expectedSha256: null }, authorize)).rejects.toThrow();
|
|
await expect(executeWorkspaceTool(directory, "workspace_write", { path: "hello.txt", text: "overwrite", expectedSha256: "0".repeat(64) }, authorize)).rejects.toThrow("conflict");
|
|
await executeWorkspaceTool(directory, "workspace_write", { path: "hello.txt", text: "updated", expectedSha256: result.sha256 }, authorize);
|
|
await expect(executeWorkspaceTool(directory, "workspace_read", { path: "../hello.txt" }, authorize)).rejects.toThrow();
|
|
await writeFile(join(outside, "private.txt"), "PRIVATE");
|
|
await symlink(outside, join(directory, "outside"));
|
|
await expect(readWorkspaceUploadFile(directory, "../private.txt")).rejects.toThrow();
|
|
await expect(readWorkspaceUploadFile(directory, "outside/private.txt")).rejects.toThrow("symlink_denied");
|
|
expect((await readWorkspaceUploadFile(directory, "hello.txt")).toString()).toBe("updated");
|
|
await expect(executeWorkspaceTool(directory, "workspace_read", { path: "outside/private.txt" }, authorize)).rejects.toThrow("symlink");
|
|
});
|
|
it.each(["workspace", "skill"])("preserves Unicode characters across %s text pages", async mode => {
|
|
const text = "a".repeat(5999) + "🌍" + "z".repeat(6000) + "🚀";
|
|
const directory = await root();
|
|
await writeFile(join(directory, "unicode.txt"), text);
|
|
const bundle = await materializeAsset([{ path: "SKILL.md", content: Buffer.from(text), mode: 0o444 }]);
|
|
const context = { skills: [{ key: "unicode", runtimeName: "unicode", versionId: "version-1", bundle }] } as any;
|
|
const read = async (offset: number) => (mode === "workspace"
|
|
? executeWorkspaceTool(directory, "workspace_read", { path: "unicode.txt", offset }, authorize)
|
|
: readAssignedSkill(context, { skill: "unicode", offset })) as Promise<{ text: string; nextOffset: number | null; sha256: string; byteSize: number }>;
|
|
const first = await read(0);
|
|
expect(first.nextOffset).toBe(5999);
|
|
expect(first.text).toBe("a".repeat(5999));
|
|
let combined = first.text, next = first.nextOffset;
|
|
while (next !== null) {
|
|
const page = await read(next);
|
|
expect(page.sha256).toBe(first.sha256);
|
|
expect(page.byteSize).toBe(Buffer.byteLength(text));
|
|
expect(page.text).not.toMatch(/^[\uDC00-\uDFFF]|[\uD800-\uDBFF]$/u);
|
|
combined += page.text;
|
|
next = page.nextOffset;
|
|
}
|
|
expect(combined).toBe(text);
|
|
await expect(read(6000)).rejects.toThrow("offset_splits_character");
|
|
});
|
|
|
|
it("excludes instance state from workspace reads, writes, listings and upload reads", async () => {
|
|
const directory = await root();
|
|
await mkdir(join(directory, ".paperclip"));
|
|
await writeFile(join(directory, ".paperclip", ".env"), "SYNTHETIC_INSTANCE_SECRET");
|
|
await mkdir(join(directory, "nested", ".PaPeRcLiP"), { recursive: true });
|
|
for (const path of [".paperclip/.env", "nested/.PaPeRcLiP/config.json"]) {
|
|
await expect(executeWorkspaceTool(directory, "workspace_read", { path }, authorize)).rejects.toThrow("instance_state_denied");
|
|
await expect(executeWorkspaceTool(directory, "workspace_write", { path, text: "overwrite", expectedSha256: null }, authorize)).rejects.toThrow("instance_state_denied");
|
|
await expect(readWorkspaceUploadFile(directory, path)).rejects.toThrow("instance_state_denied");
|
|
}
|
|
await expect(executeWorkspaceTool(directory, "workspace_list", { path: ".paperclip" }, authorize)).rejects.toThrow("instance_state_denied");
|
|
expect(await executeWorkspaceTool(directory, "workspace_list", { path: "" }, authorize)).toMatchObject({ entries: [{ name: "nested" }] });
|
|
expect(await readFile(join(directory, ".paperclip", ".env"), "utf8")).toBe("SYNTHETIC_INSTANCE_SECRET");
|
|
});
|
|
|
|
it.skipIf(!workspaceCommandSandboxAvailable())("denies command access to instance keys while preserving ordinary workspace work", async () => {
|
|
const directory = join(await root(), "project.[safe]");
|
|
await mkdir(directory);
|
|
await mkdir(join(directory, ".paperclip"));
|
|
await mkdir(join(directory, "nested", ".PaPeRcLiP"), { recursive: true });
|
|
const keys = [".paperclip/.env", "nested/.PaPeRcLiP/config.json"];
|
|
for (const key of keys) await writeFile(join(directory, key), "SYNTHETIC_INSTANCE_SECRET");
|
|
for (const key of keys) {
|
|
const read = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/cat", args: [key] }, authorize) as any;
|
|
expect(read.exitCode).not.toBe(0);
|
|
expect(read.output).not.toContain("SYNTHETIC_INSTANCE_SECRET");
|
|
const write = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/sh", args: ["-c", 'printf overwrite > "$1"', "sh", key] }, authorize) as any;
|
|
expect(write.exitCode).not.toBe(0);
|
|
expect(await readFile(join(directory, key), "utf8")).toBe("SYNTHETIC_INSTANCE_SECRET");
|
|
}
|
|
const ordinary = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/sh", args: ["-c", "printf hello > result.txt"] }, authorize) as any;
|
|
expect(ordinary.exitCode).toBe(0);
|
|
expect(await readFile(join(directory, "result.txt"), "utf8")).toBe("hello");
|
|
});
|
|
|
|
it("tells agents that a comment on a running task waits for the assignee's next turn", () => {
|
|
// A chat lead routing a requirement change to busy work otherwise reads "without interrupting"
|
|
// as "never delivered" and reaches for the assignee's workspace or a duplicate task instead.
|
|
const comment = runnerBridgeDefinitions({ workspace: false, skills: false, api: true, mode: "standard" }).find(tool => tool.name === "comment_on_task");
|
|
expect(comment?.description).toContain("existing task's assignee a new or changed requirement");
|
|
expect(comment?.description).toContain("rather than creating a duplicate task or editing that task's workspace files");
|
|
expect(comment?.description).toContain("delivers it at the start of the assignee's next turn on that task");
|
|
expect(comment?.description).toContain("set resume: true for such a requirement");
|
|
expect(comment?.description).toContain("delivery still happens if the assignee completes the task first");
|
|
expect(comment?.description).toContain("not as already applied");
|
|
expect((comment?.inputSchema as { properties?: Record<string, unknown> }).properties?.resume).toEqual({ type: "boolean" });
|
|
});
|
|
it("forwards a comment's explicit resume intent to the comment API and omits it otherwise", () => {
|
|
const taskId = "8ba8b5a2-066a-4bb2-b591-e396942ecdf5";
|
|
expect(taskBridgeApiRequest("comment_on_task", { taskId, body: "Set the headline to Launch sooner.", resume: true }).body)
|
|
.toEqual({ body: "Set the headline to Launch sooner.", resume: true });
|
|
expect(taskBridgeApiRequest("comment_on_task", { taskId, body: "FYI only." }).body).toEqual({ body: "FYI only." });
|
|
expect(taskBridgeApiRequest("comment_on_task", { taskId, body: "FYI only.", resume: false }).body).toEqual({ body: "FYI only." });
|
|
});
|
|
|
|
it.skipIf(process.platform === "linux")("does not expose or execute commands without descendant containment", async () => {
|
|
const directory = await root();
|
|
expect(workspaceCommandSandboxAvailable()).toBe(false);
|
|
const tools = runnerBridgeDefinitions({ workspace: true, skills: false, api: false, mode: "standard" }).map(tool => tool.name);
|
|
expect(tools).toEqual(expect.arrayContaining(["workspace_list", "workspace_read", "workspace_write"]));
|
|
expect(tools).not.toContain("workspace_run");
|
|
await expect(executeWorkspaceTool(directory, "workspace_run", {
|
|
program: "/bin/sh", args: ["-c", "printf escaped > unexpected.txt"],
|
|
}, authorize)).rejects.toThrow("command_sandbox_unavailable");
|
|
await expect(readFile(join(directory, "unexpected.txt"))).rejects.toThrow();
|
|
});
|
|
|
|
it.skipIf(!workspaceCommandSandboxAvailable()).each(["completion", "timeout", "revocation"])("terminates detached descendants on %s", async mode => {
|
|
const directory = await root();
|
|
await writeFile(join(directory, "detach.py"), `import os, time, sys
|
|
if os.fork() == 0:
|
|
os.setsid()
|
|
for fd in (0, 1, 2):
|
|
os.close(fd)
|
|
for tick in range(500):
|
|
with open("ticks.txt", "w") as out:
|
|
out.write(str(tick))
|
|
time.sleep(0.02)
|
|
os._exit(0)
|
|
while not os.path.exists("ticks.txt"):
|
|
time.sleep(0.01)
|
|
if sys.argv[1] != "completion":
|
|
time.sleep(20)
|
|
`);
|
|
let revoked = false;
|
|
const result = await executeWorkspaceTool(directory, "workspace_run", {
|
|
program: "/usr/bin/python3", args: ["detach.py", mode], timeoutMs: mode === "timeout" ? 1000 : 5000,
|
|
}, async () => {
|
|
if (revoked) throw new Error("revoked");
|
|
if (mode === "revocation") revoked = await readFile(join(directory, "ticks.txt")).then(() => true, () => false);
|
|
}) as any;
|
|
expect(result.stopped).toBe(mode === "timeout" ? "timeout" : mode === "revocation" ? "authority_revoked" : null);
|
|
const final = await readFile(join(directory, "ticks.txt"), "utf8");
|
|
await new Promise(resolve => setTimeout(resolve, 150));
|
|
expect(await readFile(join(directory, "ticks.txt"), "utf8")).toBe(final);
|
|
});
|
|
|
|
it("serializes overlapping writes so only one observed hash can commit", async () => {
|
|
const directory = await root();
|
|
const initial = await executeWorkspaceTool(directory, "workspace_write", { path: "shared.txt", text: "initial", expectedSha256: null }, authorize) as any;
|
|
let entered!: () => void, release!: () => void, authorizations = 0;
|
|
const waiting = new Promise<void>(resolve => { entered = resolve; });
|
|
const gate = new Promise<void>(resolve => { release = resolve; });
|
|
const first = executeWorkspaceTool(directory, "workspace_write", { path: "shared.txt", text: "first", expectedSha256: initial.sha256 }, async () => {
|
|
if (++authorizations === 2) { entered(); await gate; }
|
|
});
|
|
await waiting;
|
|
const second = executeWorkspaceTool(directory, "workspace_write", { path: "shared.txt", text: "second", expectedSha256: initial.sha256 }, authorize);
|
|
const observed = Promise.allSettled([first, second]);
|
|
release();
|
|
const results = await observed;
|
|
expect(results[0].status).toBe("fulfilled");
|
|
expect(results[1]).toMatchObject({ status: "rejected", reason: expect.objectContaining({ message: "runner_workspace_write_conflict" }) });
|
|
expect(await readFile(join(directory, "shared.txt"), "utf8")).toBe("first");
|
|
});
|
|
|
|
it.skipIf(!workspaceCommandSandboxAvailable())("runs useful commands but denies files outside the workspace and injected credentials", async () => {
|
|
const directory = await root(), outside = await root();
|
|
const secret = join(outside, "private.txt"); await writeFile(secret, "PRIVATE");
|
|
const success = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/sh", args: ["-c", "printf hello > output.txt; printf '%s' \"${PAPERCLIP_SECRETS_MASTER_KEY-unset}\"" ] }, authorize);
|
|
expect(success).toMatchObject({ exitCode: 0, output: "unset", stopped: null });
|
|
expect(await readFile(join(directory, "output.txt"), "utf8")).toBe("hello");
|
|
const read = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/cat", args: [secret] }, authorize);
|
|
expect(read).toMatchObject({ exitCode: 1 }); expect(String((read as any).output)).not.toContain("PRIVATE");
|
|
const write = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/sh", args: ["-c", 'printf stolen > "$1" || exit 1', "sh", secret] }, authorize);
|
|
expect(await readFile(secret, "utf8")).toBe("PRIVATE");
|
|
expect(write).toMatchObject({ stopped: null, exitCode: expect.any(Number) }); expect((write as { exitCode: number }).exitCode).not.toBe(0);
|
|
});
|
|
it.skipIf(!workspaceCommandSandboxAvailable())("stops its owned process on authority loss and bounds output", async () => {
|
|
const directory = await root(); let calls = 0;
|
|
const result = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/sleep", args: ["20"] }, async () => { if (++calls > 2) throw new Error("revoked"); });
|
|
expect(result).toMatchObject({ stopped: "authority_revoked" });
|
|
const large = await executeWorkspaceTool(directory, "workspace_run", { program: "/bin/sh", args: ["-c", "yes x | head -c 50000"] }, authorize) as any;
|
|
expect(large.truncated).toBe(true); expect(Buffer.byteLength(large.output)).toBeLessThanOrEqual(24000);
|
|
});
|
|
it("reads only a pinned skill file and detects tampering", async () => {
|
|
const bundle = await materializeAsset([{ path: "SKILL.md", content: Buffer.from("# Assigned skill\nUse the pinned instructions."), mode: 0o444 }]);
|
|
const context = { skills: [{ key: "assigned", runtimeName: "assigned", versionId: "version-1", bundle }] } as any;
|
|
expect(await readAssignedSkill(context, { skill: "assigned" })).toMatchObject({ versionId: "version-1", text: "# Assigned skill\nUse the pinned instructions." });
|
|
await expect(readAssignedSkill(context, { skill: "not-assigned" })).rejects.toThrow("not_assigned");
|
|
const saved = { skills: [{ key: "company/company-1/qa-saved-skill", runtimeName: "qa-saved-skill--33910b3ce6", versionId: "version-1", bundle }] } as any;
|
|
expect(await readAssignedSkill(saved, { skill: "qa-saved-skill" })).toMatchObject({ skill: "company/company-1/qa-saved-skill", versionId: "version-1" });
|
|
expect(await readAssignedSkill(saved, { skill: "/qa-saved-skill" })).toMatchObject({ skill: "company/company-1/qa-saved-skill" });
|
|
expect(await readAssignedSkill(saved, { skill: " /QA-Saved-Skill " })).toMatchObject({ skill: "company/company-1/qa-saved-skill" });
|
|
const twins = { skills: [saved.skills[0], { ...saved.skills[0], key: "paperclipai/paperclip/qa-saved-skill", runtimeName: "qa-saved-skill--aa" }] } as any;
|
|
await expect(readAssignedSkill(twins, { skill: "qa-saved-skill" })).rejects.toThrow("not_assigned");
|
|
expect(await readAssignedSkill(twins, { skill: "qa-saved-skill--aa" })).toMatchObject({ skill: "paperclipai/paperclip/qa-saved-skill" });
|
|
await expect(readAssignedSkill(context, { skill: "assigned", path: "../private" })).rejects.toThrow();
|
|
await expect(readAssignedSkill(context, { skill: "assigned", path: "not-pinned.txt" })).rejects.toThrow("not_pinned");
|
|
await expect(readAssignedSkill({ ...context, skills: [{ ...context.skills[0], bundle: { ...bundle, manifestDigest: createHash("sha256").update("different").digest("hex") } }] }, { skill: "assigned" })).rejects.toThrow("manifest_digest");
|
|
});
|
|
|
|
it("settles missing-file reads without disclosing host paths or leaving an unknown effect", async () => {
|
|
const directory = await root();
|
|
const result = await settleRunnerBridgeRead(() => executeWorkspaceTool(directory, "workspace_read", { path: "missing.txt" }, authorize));
|
|
expect(result).toMatchObject({ outcome: "failed", code: "runner_bridge_file_not_found" });
|
|
expect(JSON.stringify(result)).not.toContain(directory);
|
|
expect(await settleRunnerBridgeRead(async () => { throw new Error("runner_skill_file_not_pinned"); })).toMatchObject({ outcome: "failed", code: "runner_skill_file_not_pinned" });
|
|
});
|
|
|
|
it("advertises only available tools in the current work mode", () => {
|
|
const tools = runnerBridgeDefinitions({ workspace: false, skills: false, api: false, mode: "ask" }).map(tool => tool.name);
|
|
expect(tools).toEqual(["get_identity", "list_people"]);
|
|
expect(runnerBridgeDefinitions({ workspace: true, skills: true, api: true, mode: "planning" }).map(tool => tool.name)).not.toContain("workspace_run");
|
|
});
|
|
});
|