From 62b60fc4420e67bc3a0f2877ec9ff7db25e52364 Mon Sep 17 00:00:00 2001 From: Dotta Date: Tue, 24 Nov 2026 16:13:38 -0600 Subject: [PATCH] fix(antigravity): a deleted /swarm comment withdraws its request Deleting a swarm comment keeps its request record but clears its text, so a later wake found an empty request and failed with "This swarm has no request to run", even after the swarm had succeeded. A deleted request is now withdrawn (no swarm, no failure, ordinary work), a deleted latest message never falls back to an older request, and a fulfilled request is checked before its text so it can never fail a later wake. Co-Authored-By: Claude Opus 5.5 --- .../src/services/antigravity-swarm.db.test.ts | 36 +++++++++++++++++++ server/src/services/antigravity-swarm.ts | 21 +++++++---- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/server/src/services/antigravity-swarm.db.test.ts b/server/src/services/antigravity-swarm.db.test.ts index a27a7ca3615..5c2e1651548 100644 --- a/server/src/services/antigravity-swarm.db.test.ts +++ b/server/src/services/antigravity-swarm.db.test.ts @@ -205,6 +205,42 @@ describe.skipIf(!support.supported)("trusted /swarm run selection (database)", ( await expect(automatic(g, (await insertRun(g, {})).id)).rejects.toMatchObject({ message: expect.stringMatching(/three attempts/) }); }); + describe("a deleted /swarm comment withdraws its request", () => { + const remove = (f: Awaited>) => issueService(db).tombstoneComment(f.comment.id, { actorType: "user", userId: USER }); + + it.each(["issue_assigned", "issue_blocker_stalled", "issue_continuation_needed"])( + "runs a %s wake as ordinary work, with no failure, after an unfulfilled request was deleted", async wakeReason => { + const f = await seed(); + await remove(f); + expect(await automatic(f, (await insertRun(f, {})).id, wakeReason)).toBeNull(); + // Even a wake that still names the deleted request (its fresh wake, or a retry of it) is ordinary work. + expect(await resolve(f, { runId: (await insertRun(f, {})).id })).toBeNull(); + const failed = await insertRun(f, { status: "failed", contextSnapshot: { issueId: f.issue.id, antigravitySwarmCommentId: f.comment.id } }); + expect(await resolve(f, { runId: (await insertRun(f, { retryOfRunId: failed.id })).id, context: { issueId: f.issue.id } })).toBeNull(); + expect(await antigravitySwarmQuotaScope(db, { companyId: f.company.id, issueId: f.issue.id, runId: randomUUID(), agent: f.agent, + context: { issueId: f.issue.id, antigravitySwarmCommentId: f.comment.id } })).toBeNull(); + }); + + it("runs ordinary work after a fulfilled request was deleted", async () => { + const f = await seed(); + const first = await insertRun(f, {}); + expect(await automatic(f, first.id)).toMatchObject({ request: "two helpers" }); + await setStatus(first.id, "succeeded"); + await remove(f); + expect(await automatic(f, (await insertRun(f, {})).id, "issue_continuation_needed")).toBeNull(); + }); + + it("never lets a fulfilled request fail a later wake, even when its text no longer has a request", async () => { + const f = await seed(); + const first = await insertRun(f, {}); + expect(await automatic(f, first.id)).toMatchObject({ request: expect.any(String) }); + await setStatus(first.id, "succeeded"); + // Comments have no edit path today; this stands in for any later change to the stored text. + await db.update(issueComments).set({ body: "/swarm" }).where(eq(issueComments.id, f.comment.id)); + expect(await automatic(f, (await insertRun(f, {})).id, "issue_assigned")).toBeNull(); + }); + }); + it("starts exactly one swarm for one request when two wakes arrive together", async () => { const f = await seed(); const [a, b] = [await insertRun(f, {}), await insertRun(f, {})]; diff --git a/server/src/services/antigravity-swarm.ts b/server/src/services/antigravity-swarm.ts index 89c759a1a23..0b52677fdd9 100644 --- a/server/src/services/antigravity-swarm.ts +++ b/server/src/services/antigravity-swarm.ts @@ -113,11 +113,12 @@ async function inheritedSwarmCommentId(db: Db, companyId: string, issueId: strin } async function pendingSwarmCommentId(db: Db, companyId: string, issueId: string): Promise { - const [latest] = await db.select({ id: issueComments.id }).from(issueComments) + // A deleted latest message withdraws whatever it asked for; it never falls back to an older request. + const [latest] = await db.select({ id: issueComments.id, deletedAt: issueComments.deletedAt }).from(issueComments) .where(and(eq(issueComments.companyId, companyId), eq(issueComments.issueId, issueId), isNotNull(issueComments.authorUserId), isNull(issueComments.authorAgentId))) .orderBy(desc(issueComments.createdAt), desc(issueComments.id)).limit(1); - return latest?.id ?? null; + return latest && !latest.deletedAt ? latest.id : null; } export async function recordAntigravitySwarmRequest(db: Db, input: { companyId: string; issueId: string; commentId: string; userId: string }): Promise { @@ -176,10 +177,14 @@ export async function antigravitySwarmQuotaScope(db: Db, input: { if (!isAntigravityAgent(input.agent)) return null; const requested = await recordedRequest(db, { ...input, remote: false, planning: false }); if (!requested) return null; + return await requestFulfilled(db, input.companyId, requested.commentId) ? null : ANTIGRAVITY_CLI_QUOTA_SCOPE; +} + +async function requestFulfilled(db: Db, companyId: string, commentId: string): Promise { const [fulfilled] = await db.select({ id: heartbeatRuns.id }).from(heartbeatRuns) - .where(and(eq(heartbeatRuns.companyId, input.companyId), eq(heartbeatRuns.status, "succeeded"), - sql`${heartbeatRuns.contextSnapshot}->>'antigravitySwarmCommentId' = ${requested.commentId}`)).limit(1); - return fulfilled ? null : ANTIGRAVITY_CLI_QUOTA_SCOPE; + .where(and(eq(heartbeatRuns.companyId, companyId), eq(heartbeatRuns.status, "succeeded"), + sql`${heartbeatRuns.contextSnapshot}->>'antigravitySwarmCommentId' = ${commentId}`)).limit(1); + return Boolean(fulfilled); } /** The recorded user /swarm request this run serves, if any (before any replacement). */ @@ -191,8 +196,10 @@ async function recordedRequest(db: Db, input: Parameters>'commentId' = ${commentId}`)) @@ -220,6 +227,8 @@ async function decideSwarm(tx: Db, input: Parameters id !== selected.commentId); // The swarm's only instruction is this recorded comment's own text. const request = antigravitySwarmRequestFromComment(selected.body);