From bfbe2e3a7f880964e01cca4fb585a7abc800ca74 Mon Sep 17 00:00:00 2001 From: Rick Glenn <24476447+exocognosis@users.noreply.github.com> Date: Sat, 5 Dec 2026 03:11:27 -0700 Subject: [PATCH] fix(issues): refuse execution policy stage removal by non-board actors (#17503) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Issues carry an execution policy with review and approval stages > - `PATCH /api/issues/:id` replaces the policy, and the assigned agent can send `{ stages: [] }` or `null` > - The stages go away, a pending review is dissolved, and the issue can then go `done` unreviewed > - #16431 locks a policy that has a user participant. It does not cover a policy with only agent stages. > - This pull request refuses stage removal by non-board actors for any staged policy > - The benefit is that an agent cannot remove the gate that exists to check its work ## Linked Issues or Issue Description Fixes #17301 Refs #16431 This change extends the #16431 lock to the agent-only gap that #16431 does not cover. Both PRs add a check at the same place in the PATCH handler. The PR that merges second needs a small rebase. ## What Changed - `server/src/routes/issues.ts`: a non-board PATCH that sends `executionPolicy` gets `403 { code: "execution_stage_removal_locked" }` if a stage id in the current policy is not in the new policy. This includes `null` and `{ stages: [] }`. Board actors are not affected. - `server/src/__tests__/issue-execution-policy-routes.test.ts`: four tests. An agent that removes an agent-only stage is refused. An agent that sets the policy to `null` is refused. A board user can remove stages. An agent can change only the monitor when it sends the stages again. ## Verification - `pnpm exec vitest run server/src/__tests__/issue-execution-policy-routes.test.ts`: 41 pass (37 existing and 4 new). - Without the route change, the two refusal tests fail. - The 13 route test files that send agent PATCHes with `executionPolicy`: 500 pass. - `tsc --noEmit` in `server` reports no errors in the changed files. ## Risks - An agent that sends only `{ monitor }` on an issue with stages now gets 403. Before this change, that request removed the stages without a warning. The 403 message tells the agent to send every current stage with its id together with the monitor. - The check compares stage ids only. An agent can still change the participants of a stage and keep its id. #16431 covers this for user participants. > 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 - Anthropic Claude Opus 5.5 (claude-opus-5-5), used in Claude Code with tool use (file reads, shell, test runs). ## 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 - [x] I will address all Greptile and reviewer comments before requesting merge --- .../issue-execution-policy-routes.test.ts | 142 ++++++++++++++++++ server/src/routes/issues.ts | 18 ++- 2 files changed, 159 insertions(+), 1 deletion(-) diff --git a/server/src/__tests__/issue-execution-policy-routes.test.ts b/server/src/__tests__/issue-execution-policy-routes.test.ts index c3fdac21a55..b9e21bd6ae1 100644 --- a/server/src/__tests__/issue-execution-policy-routes.test.ts +++ b/server/src/__tests__/issue-execution-policy-routes.test.ts @@ -1392,4 +1392,146 @@ describe("issue execution policy routes", () => { }), ); }); + + const stagedPolicyIssue = () => { + const issue = { + id: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", + companyId: "company-1", + status: "in_progress", + assigneeAgentId: "33333333-3333-4333-8333-333333333333", + assigneeUserId: null, + createdByUserId: "local-board", + identifier: "PAP-1010", + title: "Staged execution policy", + executionPolicy: normalizeIssueExecutionPolicy({ + stages: [ + { + id: "11111111-1111-4111-8111-111111111111", + type: "review", + participants: [{ type: "agent", agentId: "44444444-4444-4444-8444-444444444444" }], + }, + { + id: "22222222-2222-4222-8222-222222222222", + type: "approval", + participants: [{ type: "agent", agentId: "66666666-6666-4666-8666-666666666666" }], + }, + ], + })!, + executionState: null, + monitorAttemptCount: 0, + monitorNextCheckAt: null, + monitorLastTriggeredAt: null, + monitorNotes: null, + monitorScheduledBy: null, + }; + mockIssueService.getById.mockResolvedValue(issue); + mockIssueService.update.mockImplementation(async (_id: string, patch: Record) => ({ + ...issue, + ...patch, + updatedAt: new Date(), + })); + return issue; + }; + const assigneeAgentActor = { + type: "agent" as const, + agentId: "33333333-3333-4333-8333-333333333333", + companyId: "company-1", + runId: "55555555-5555-4555-8555-555555555555", + }; + + it("rejects an agent patch that removes an agent-only stage", async () => { + const issue = stagedPolicyIssue(); + + const res = await request(await createApp(assigneeAgentActor)) + .patch("/api/issues/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa") + .send({ executionPolicy: { stages: [issue.executionPolicy.stages[1]] } }); + + expect(res.status).toBe(403); + expect(res.body.code).toBe("execution_stage_removal_locked"); + expect(mockIssueService.update).not.toHaveBeenCalled(); + }); + + it("rejects an agent patch that nulls a staged policy", async () => { + stagedPolicyIssue(); + + const res = await request(await createApp(assigneeAgentActor)) + .patch("/api/issues/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa") + .send({ executionPolicy: null }); + + expect(res.status).toBe(403); + expect(res.body.code).toBe("execution_stage_removal_locked"); + expect(mockIssueService.update).not.toHaveBeenCalled(); + }); + + it("allows a board user to remove stages", async () => { + stagedPolicyIssue(); + + const res = await request(await createApp()) + .patch("/api/issues/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa") + .send({ executionPolicy: { stages: [] } }); + + expect(res.status).toBe(200); + expect(mockIssueService.update).toHaveBeenCalledWith( + "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", + expect.objectContaining({ executionPolicy: null }), + ); + }); + + it("allows an agent to change only the monitor of a staged policy", async () => { + const issue = stagedPolicyIssue(); + + const res = await request(await createApp(assigneeAgentActor)) + .patch("/api/issues/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa") + .send({ + executionPolicy: { + stages: issue.executionPolicy.stages, + monitor: { + nextCheckAt: "2026-12-01T12:00:00.000Z", + scheduledBy: "assignee", + notes: "Wait for the reviewer.", + }, + }, + }); + + expect(res.status).toBe(200); + expect(mockIssueService.update).toHaveBeenCalledWith( + "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", + expect.objectContaining({ + executionPolicy: expect.objectContaining({ stages: issue.executionPolicy.stages }), + monitorNextCheckAt: new Date("2026-12-01T12:00:00.000Z"), + }), + ); + }); + + it("rejects an agent patch from a stale snapshot that would drop a stage added after it", async () => { + const issue = stagedPolicyIssue(); + const savedPolicy = normalizeIssueExecutionPolicy({ + stages: [ + ...issue.executionPolicy.stages, + { + id: "77777777-7777-4777-8777-777777777777", + type: "approval", + participants: [{ type: "user", userId: "local-board" }], + }, + ], + }); + const { conflict } = await import("../errors.js"); + const { isDeepStrictEqual } = await import("node:util"); + mockIssueService.update.mockImplementation(async (_id: string, patch: Record) => { + if (!isDeepStrictEqual(patch.expectedExecutionPolicy, savedPolicy)) { + throw conflict("The task execution settings changed", { code: "execution_policy_changed" }); + } + return { ...issue, ...patch, updatedAt: new Date() }; + }); + + const res = await request(await createApp(assigneeAgentActor)) + .patch("/api/issues/aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa") + .send({ executionPolicy: { stages: issue.executionPolicy.stages } }); + + expect(res.status).toBe(409); + expect(mockIssueService.update).toHaveBeenCalledWith( + "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", + expect.objectContaining({ expectedExecutionPolicy: issue.executionPolicy }), + ); + }); }); diff --git a/server/src/routes/issues.ts b/server/src/routes/issues.ts index 60612eb4649..2019a29d2e9 100644 --- a/server/src/routes/issues.ts +++ b/server/src/routes/issues.ts @@ -15722,6 +15722,19 @@ export function issueRoutes( updateFields.executionPolicy !== undefined ? (updateFields.executionPolicy as NormalizedExecutionPolicy | null) : previousExecutionPolicy; + if ( + req.body.executionPolicy !== undefined && + req.actor.type !== "board" && + previousExecutionPolicy?.stages.some( + (stage) => !nextExecutionPolicy?.stages.some((nextStage) => nextStage.id === stage.id), + ) + ) { + res.status(403).json({ + error: "Only the board can remove execution policy stages. To change the monitor, send every current stage with its id.", + code: "execution_stage_removal_locked", + }); + return; + } if (normalizedAssigneeAgentId !== undefined) { updateFields.assigneeAgentId = normalizedAssigneeAgentId; } @@ -16299,7 +16312,10 @@ export function issueRoutes( ...(updateFields.parentId !== undefined ? { parentDepthPolicy } : {}), ...(expectedParentId !== undefined ? { expectedParentId } : {}), workspaceSelectionActor: req.actor, - expectedExecutionPolicy, + expectedExecutionPolicy: + expectedExecutionPolicy === undefined && req.body.executionPolicy !== undefined && req.actor.type !== "board" + ? existing.executionPolicy ?? null + : expectedExecutionPolicy, expectedStatusVersion, ...(checkedExecutionLock ? { expectedExecutionLock: checkedExecutionLock } : {}), actorAgentId: actor.agentId ?? null,