From 417c695a9a5050c419230c0acf35967dfe4777ce Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 23 Nov 2026 23:02:04 -0600 Subject: [PATCH] feat(antigravity): refuse /swarm in Plan mode; route tests for the trusted request Co-Authored-By: Claude Opus 5.5 --- .../issue-comment-reopen-routes.test.ts | 47 +++++++++++++++++++ server/src/services/antigravity-swarm.test.ts | 1 + server/src/services/antigravity-swarm.ts | 11 +++-- .../services/heartbeat/runtime-selection.ts | 2 +- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/server/src/__tests__/issue-comment-reopen-routes.test.ts b/server/src/__tests__/issue-comment-reopen-routes.test.ts index 1a1f7d52f1b..52e4e759001 100644 --- a/server/src/__tests__/issue-comment-reopen-routes.test.ts +++ b/server/src/__tests__/issue-comment-reopen-routes.test.ts @@ -162,6 +162,17 @@ vi.mock("../services/access.js", () => ({ accessService: () => mockAccessService, })); +const mockSwarm = vi.hoisted(() => ({ + refusal: vi.fn(async (): Promise => null), + record: vi.fn(async () => undefined), +})); +vi.mock("../services/antigravity-swarm.js", () => ({ + ANTIGRAVITY_SWARM_COMMAND: "swarm", + ANTIGRAVITY_SWARM_UNAVAILABLE: "antigravity_swarm_unavailable", + antigravitySwarmCommentRefusal: mockSwarm.refusal, + recordAntigravitySwarmRequest: mockSwarm.record, +})); + vi.mock("../services/activity-log.js", () => ({ logActivity: mockLogActivity, })); @@ -1301,6 +1312,42 @@ describe("issue comment reopen routes", () => { ); }); + it("records a signed-in user's /swarm request and marks the assignee's wake with it", async () => { + mockSwarm.refusal.mockResolvedValue(null); + mockIssueService.getById.mockResolvedValue(makeIssue("blocked")); + mockIssueService.update.mockImplementation(async (_id: string, patch: Record) => ({ ...makeIssue("blocked"), ...patch })); + const res = await request(await installActor(createApp())) + .post("/api/issues/11111111-1111-4111-8111-111111111111/comments") + .send({ body: "two helpers write a.txt and b.txt", nativeCommand: "swarm" }); + expect(res.status).toBe(201); + expect(mockSwarm.record).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ commentId: "comment-1", userId: "local-board" })); + await waitForWakeup(() => expect(mockHeartbeatService.wakeup).toHaveBeenCalledWith( + "22222222-2222-4222-8222-222222222222", + expect.objectContaining({ contextSnapshot: expect.objectContaining({ antigravitySwarmCommentId: "comment-1" }) }), + )); + }); + + it("refuses an unavailable /swarm in plain language and posts nothing", async () => { + mockSwarm.refusal.mockResolvedValue("Swarm is not turned on for this Paperclip."); + mockIssueService.getById.mockResolvedValue(makeIssue("todo")); + const res = await request(await installActor(createApp())) + .post("/api/issues/11111111-1111-4111-8111-111111111111/comments") + .send({ body: "do it", nativeCommand: "swarm" }); + expect(res.status).toBe(422); + expect(res.body).toMatchObject({ error: "Swarm is not turned on for this Paperclip.", code: "antigravity_swarm_unavailable" }); + expect(mockIssueService.addComment).not.toHaveBeenCalled(); + expect(mockHeartbeatService.wakeup).not.toHaveBeenCalled(); + }); + + it("accepts a swarm request only from a signed-in user", async () => { + mockIssueService.getById.mockResolvedValue(makeIssue("todo")); + const res = await request(await installActor(createApp(), { type: "agent", agentId: "33333333-3333-4333-8333-333333333333", companyId: "company-1" })) + .post("/api/issues/11111111-1111-4111-8111-111111111111/comments") + .send({ body: "do it", nativeCommand: "swarm" }); + expect(res.status).toBe(403); + expect(mockIssueService.addComment).not.toHaveBeenCalled(); + }); + it("moves in-progress issues with a scheduled retry back to todo via POST human comments", async () => { const issue = { ...makeIssue("in_progress"), diff --git a/server/src/services/antigravity-swarm.test.ts b/server/src/services/antigravity-swarm.test.ts index b56bfed47c3..e223ca6aa60 100644 --- a/server/src/services/antigravity-swarm.test.ts +++ b/server/src/services/antigravity-swarm.test.ts @@ -11,6 +11,7 @@ describe("trusted /swarm for Google Antigravity", () => { expect(antigravitySwarmRefusal({ enabled: true, agent: { adapterType: "paperclip_runner", adapterConfig: { provider: "acpx", acpxAgent: "claude" } } })) .toMatch(/Google Antigravity agent/); expect(antigravitySwarmRefusal({ enabled: true, agent: antigravity, remote: true })).toMatch(/this computer/); + expect(antigravitySwarmRefusal({ enabled: true, agent: antigravity, planning: true })).toMatch(/Plan mode/); }); it("reads a swarm request only from the wake's structured marker, never from comment text", () => { diff --git a/server/src/services/antigravity-swarm.ts b/server/src/services/antigravity-swarm.ts index cc559ec1b29..b52907b26dd 100644 --- a/server/src/services/antigravity-swarm.ts +++ b/server/src/services/antigravity-swarm.ts @@ -34,11 +34,13 @@ export function antigravitySwarmRefusal(input: { enabled: boolean; agent: { adapterType: string; adapterConfig: unknown } | null; remote?: boolean; + planning?: boolean; }): string | null { if (!input.enabled) { return "Swarm is not turned on for this Paperclip. An instance admin can turn on \"Google Antigravity swarm\" in Settings, Experimental."; } if (!isAntigravityAgent(input.agent)) return "Swarm works only for tasks assigned to a Google Antigravity agent."; + if (input.planning) return "Swarm is not available while the task is in Plan mode: helpers change files. Approve the plan first, then send /swarm."; if (input.remote) return "Swarm is not available for this agent: it runs only on this computer, not in a cloud or remote environment."; return null; } @@ -56,12 +58,12 @@ async function swarmEnabled(db: Db): Promise { } /** Comment-time check, so an unavailable swarm is refused before anything is posted. */ -export async function antigravitySwarmCommentRefusal(db: Db, issue: { companyId: string; assigneeAgentId: string | null }): Promise { +export async function antigravitySwarmCommentRefusal(db: Db, issue: { companyId: string; assigneeAgentId: string | null; workMode?: string | null }): Promise { const [agent] = issue.assigneeAgentId ? await db.select({ adapterType: agents.adapterType, adapterConfig: agents.adapterConfig }).from(agents) .where(and(eq(agents.id, issue.assigneeAgentId), eq(agents.companyId, issue.companyId))) : []; - return antigravitySwarmRefusal({ enabled: await swarmEnabled(db), agent: agent ?? null }); + return antigravitySwarmRefusal({ enabled: await swarmEnabled(db), agent: agent ?? null, planning: issue.workMode === "planning" }); } /** @@ -89,6 +91,7 @@ export async function resolveAntigravitySwarmRun(db: Db, input: { agent: { id: string; adapterType: string; adapterConfig: unknown }; context: unknown; remote: boolean; + planning: boolean; }): Promise { const commentId = requestedAntigravitySwarmCommentId(input.context); if (!commentId) return false; @@ -99,14 +102,14 @@ export async function resolveAntigravitySwarmRun(db: Db, input: { sql`${activityLog.details}->>'commentId' = ${commentId}`)) .limit(1); if (!requested) return false; - const refusal = antigravitySwarmRefusal({ enabled: await swarmEnabled(db), agent: input.agent, remote: input.remote }); + const refusal = antigravitySwarmRefusal({ enabled: await swarmEnabled(db), agent: input.agent, remote: input.remote, planning: input.planning }); if (refusal) throw new PaperclipRunnerProviderProfileError(ANTIGRAVITY_SWARM_UNAVAILABLE, refusal); await logActivity(db, { companyId: input.companyId, actorType: "system", actorId: "paperclip", agentId: input.agent.id, runId: input.runId, issueId: input.issueId, action: "issue.antigravity_swarm_started", entityType: "issue", entityId: input.issueId, details: { commentId, - summary: "Started a Google Antigravity swarm with helpers. Helper actions inside the task folder are approved automatically; actions outside it are blocked. Usage is reported as one total for the coordinator and its helpers.", + summary: "Started a Google Antigravity swarm with helpers. Helper actions are approved automatically by Antigravity's auto mode, which keeps file changes in the task folder; helper shell commands are not visible to Paperclip and Paperclip does not sandbox them on this computer. Usage is one total for the coordinator and its helpers.", }, }); return true; diff --git a/server/src/services/heartbeat/runtime-selection.ts b/server/src/services/heartbeat/runtime-selection.ts index 1bd2d9981cf..02a65565df2 100644 --- a/server/src/services/heartbeat/runtime-selection.ts +++ b/server/src/services/heartbeat/runtime-selection.ts @@ -841,7 +841,7 @@ export async function selectHeartbeatRuntime(db: Db, input: HeartbeatRuntimeSele const antigravitySwarm = agent.adapterType === "paperclip_runner" && await resolveAntigravitySwarmRun(db, { companyId: agent.companyId, issueId: issueRef.id, runId: run.id, agent, - context, remote: executionTarget?.kind === "remote", + context, remote: executionTarget?.kind === "remote", planning: executionMode === "plan", }); const buildExecution = ({ normalizedSessionId, resumedSession }: { normalizedSessionId: string; resumedSession: boolean }) => buildNativeExecutionInput({