diff --git a/server/src/__tests__/agent-permissions-routes.test.ts b/server/src/__tests__/agent-permissions-routes.test.ts index 7c319913884..d670a5ac301 100644 --- a/server/src/__tests__/agent-permissions-routes.test.ts +++ b/server/src/__tests__/agent-permissions-routes.test.ts @@ -49,6 +49,7 @@ const mockAgentService = vi.hoisted(() => ({ activatePendingApproval: vi.fn(), terminate: vi.fn(), update: vi.fn(), + updateAndTransition: vi.fn(), rollbackConfigRevision: vi.fn(), updatePermissions: vi.fn(), getChainOfCommand: vi.fn(), @@ -244,6 +245,7 @@ function createDbStub(options: { requireBoardApprovalForNewAgents?: boolean } = Promise.resolve(resolve([{ id: companyId, name: "Paperclip", + issuePrefix: "PAP", requireBoardApprovalForNewAgents: options.requireBoardApprovalForNewAgents ?? false, }])), ), @@ -1708,6 +1710,50 @@ describe("agent permission routes", () => { expect(mockAgentService.terminate).not.toHaveBeenCalled(); }); + it.each([ + ["PATCH status", (base: string) => request(base).patch(`/api/agents/${agentId}`).send({ status: "terminated" })], + ["POST terminate", (base: string) => request(base).post(`/api/agents/${agentId}/terminate`).send({})], + ])("keeps agent termination board-only through %s, even for an agent allowed to configure the target", async (_route, send) => { + mockAgentService.getById.mockResolvedValue({ ...baseAgent, status: "idle" }); + const app = await createApp({ + type: "agent", + agentId: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", + companyId, + source: "agent_key", + runId: "run-1", + }); + + const res = await requestApp(app, send); + + expect(res.status).toBe(403); + expect(res.body.error).toContain("only a board user can terminate an agent"); + expect(res.body.error).toContain(`[the agent's profile](/PAP/agents/${agentId})`); + expect(mockAgentService.terminate).not.toHaveBeenCalled(); + expect(mockAgentService.update).not.toHaveBeenCalled(); + expect(mockHeartbeatService.cancelInvocationsForAgents).not.toHaveBeenCalled(); + expect(mockLogActivity).not.toHaveBeenCalled(); + }); + + it("still lets a board user terminate through PATCH status", async () => { + mockAgentService.getById.mockResolvedValue({ ...baseAgent, status: "idle" }); + mockAgentService.updateAndTransition.mockResolvedValue({ ...baseAgent, status: "terminated" }); + const app = await createApp({ + type: "board", + userId: "board-user", + source: "local_implicit", + isInstanceAdmin: true, + companyIds: [companyId], + }); + + const res = await requestApp(app, (baseUrl) => request(baseUrl) + .patch(`/api/agents/${agentId}`) + .send({ status: "terminated" })); + + expect(res.status, JSON.stringify(res.body)).toBe(200); + expect(res.body.status).toBe("terminated"); + expect(mockAgentService.updateAndTransition).toHaveBeenCalledWith(agentId, "terminate", expect.anything(), expect.anything()); + }); + it("terminates directly when no open hire approval is linked", async () => { const idleAgent = { ...baseAgent, status: "idle" }; const terminatedAgent = { ...baseAgent, status: "terminated" }; @@ -2400,5 +2446,6 @@ vi.mock("../services/agent-lifecycle.js", async (importOriginal) => { requestHire: (...args: unknown[]) => mockAgentService.create(...args), approveHire: (...args: unknown[]) => mockAgentService.activatePendingApproval(...args), terminateAgent: (...args: unknown[]) => mockAgentService.terminate(...args), + updateAndTransition: (...args: unknown[]) => mockAgentService.updateAndTransition(...args), }) }; }); diff --git a/server/src/routes/agents.ts b/server/src/routes/agents.ts index ccdb43ab484..8a5c87482b3 100644 --- a/server/src/routes/agents.ts +++ b/server/src/routes/agents.ts @@ -1777,6 +1777,18 @@ export function agentRoutes( }; } + /** + * Terminating an agent is a board-only governed action. The denial gives + * other actors a ready profile link, so they can send a person to the control. + */ + async function assertBoardCanTerminateAgent(req: Request, target: { id: string; companyId: string }) { + if (req.actor.type === "board") return; + const [company] = await db.select({ issuePrefix: companies.issuePrefix }).from(companies) + .where(eq(companies.id, target.companyId)); + const profile = company?.issuePrefix ? `[the agent's profile](/${company.issuePrefix}/agents/${target.id})` : "the agent's profile"; + throw forbidden(`Board access required: only a board user can terminate an agent. Ask a person with board access to open ${profile} and choose Terminate from its actions menu.`); + } + async function assertCanUpdateAgent(req: Request, targetAgent: { id: string; companyId: string }) { if (!hasCompanyAccess(req, targetAgent.companyId)) { throw notFound("Agent not found"); @@ -5508,6 +5520,8 @@ export function agentRoutes( const requestedStatus = patchData.status; delete patchData.status; if (requestedStatus !== undefined && !["paused", "idle", "terminated"].includes(String(requestedStatus))) throw badRequest("Use an agent lifecycle command to change status"); + // Termination is board-only and irreversible on every route, not just POST /terminate. + if (requestedStatus === "terminated") await assertBoardCanTerminateAgent(req, existing); const replaceAdapterConfig = patchData.replaceAdapterConfig === true; delete patchData.replaceAdapterConfig; // The apply-existing flag is not an agent column. The server binds the fixed @@ -5945,12 +5959,12 @@ export function agentRoutes( }); router.post("/agents/:id/terminate", async (req, res) => { - assertBoard(req); const id = req.params.id as string; const existing = await getAccessibleAgent(req, res, id); if (!existing) { return; } + await assertBoardCanTerminateAgent(req, existing); const agent = existing.status === "pending_approval" ? (await approvalsSvc.rejectHire(id, req.actor.userId ?? "board"))?.agent