mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 18:35:33 +01:00
fix(antigravity): decide the swarm under the task lock the /swarm save takes
Swarm selection read the newest recorded request, attempts and Retry before locking the task row, so a newer /swarm committing while selection waited for the lock could still let the older, replaced request start. Every decision input is now read on the transaction that holds the task row lock (the same lock the comment-and-record save takes) and records the selection. Runs with no recorded request still skip the lock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8dab083135
commit
fc81774086
2 files changed
+163
-61
No files matched your search
@@ -1,10 +1,11 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { and, eq, sql } from "drizzle-orm";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { activityLog, agentWakeupRequests, agents, companies, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, heartbeatRuns, issueComments, issues } from "@paperclipai/db";
|
||||
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../__tests__/helpers/embedded-postgres.js";
|
||||
import { ANTIGRAVITY_SWARM_REQUESTED_ACTION, recordAntigravitySwarmRequest, resolveAntigravitySwarmRun } from "./antigravity-swarm.js";
|
||||
import { instanceSettingsService } from "./instance-settings.js";
|
||||
import { issueService } from "./issues.js";
|
||||
import { deliveredQueuedCommentIds } from "./queued-comment-delivery.js";
|
||||
import { queuedCommentIdsFromWakePayload } from "../lib/queued-comment-context.js";
|
||||
import { mergeCoalescedContextSnapshot } from "./heartbeat/run-preparation.js";
|
||||
@@ -317,6 +318,83 @@ describe.skipIf(!support.supported)("trusted /swarm run selection (database)", (
|
||||
expect(await automatic(f, (await insertRun(f, {})).id)).toEqual({ commentId: later!.id, request: "three helpers write c.txt" });
|
||||
});
|
||||
|
||||
/**
|
||||
* The comment route saves a user comment and its /swarm record in one
|
||||
* transaction that locks the task row (issues.addComment), the same lock
|
||||
* selection takes. This is that save, held open until `commit` is called.
|
||||
*/
|
||||
function saveSwarmCommentHeldOpen(f: Awaited<ReturnType<typeof seed>>, body: string) {
|
||||
let commit!: () => void;
|
||||
const released = new Promise<void>(resolve => { commit = resolve; });
|
||||
let lockedResolve!: (seenSelection: boolean) => void;
|
||||
const locked = new Promise<boolean>(resolve => { lockedResolve = resolve; });
|
||||
let commentId = "";
|
||||
const done = db.transaction(async (tx) => {
|
||||
const saved = await issueService(tx as unknown as typeof db).addComment(f.issue.id, body, { userId: USER }, {}, tx);
|
||||
commentId = saved.id;
|
||||
await recordAntigravitySwarmRequest(tx as unknown as typeof db, { companyId: f.company.id, issueId: f.issue.id, commentId: saved.id, userId: USER });
|
||||
// Whether a selection already committed before this save took the task lock.
|
||||
const started = await tx.select({ id: activityLog.id }).from(activityLog)
|
||||
.where(and(eq(activityLog.entityId, f.issue.id), eq(activityLog.action, "issue.antigravity_swarm_started")));
|
||||
lockedResolve(started.length > 0);
|
||||
await released;
|
||||
}).then(() => commentId);
|
||||
return { commit, locked, done };
|
||||
}
|
||||
|
||||
/** Waits until a statement on this database is blocked on a row lock. */
|
||||
async function untilSomeoneWaitsOnALock() {
|
||||
for (let poll = 0; poll < 400; poll++) {
|
||||
const rows = await db.execute(sql`select count(*)::int as waiting from pg_stat_activity
|
||||
where datname = current_database() and wait_event_type = 'Lock'`);
|
||||
if (Number((rows as unknown as Array<{ waiting: number }>)[0]?.waiting ?? 0) > 0) return;
|
||||
await new Promise(resolve => setTimeout(resolve, 5));
|
||||
}
|
||||
throw new Error("no statement waited on the task lock");
|
||||
}
|
||||
|
||||
it("never starts an older /swarm whose newer request committed while selection waited for the task lock", async () => {
|
||||
const f = await seed();
|
||||
const run = await insertRun(f, { contextSnapshot: { issueId: f.issue.id, antigravitySwarmCommentId: f.comment.id, wakeCommentIds: [f.comment.id] } });
|
||||
// A second /swarm is being saved: its transaction holds the task lock.
|
||||
const newer = saveSwarmCommentHeldOpen(f, "three helpers write c.txt");
|
||||
expect(await newer.locked).toBe(false);
|
||||
const selection = resolve(f, { runId: run.id, context: { issueId: f.issue.id, antigravitySwarmCommentId: f.comment.id, wakeCommentIds: [f.comment.id] } });
|
||||
const settled = selection.then(() => null, (error: unknown) => error);
|
||||
await untilSomeoneWaitsOnALock();
|
||||
newer.commit();
|
||||
await newer.done;
|
||||
// The newer request committed first, so the older one is replaced and never runs.
|
||||
expect(await settled).toMatchObject({ code: "antigravity_swarm_unavailable", message: expect.stringMatching(/newer \/swarm/) });
|
||||
expect(await db.select().from(activityLog).where(and(eq(activityLog.entityId, f.issue.id), eq(activityLog.action, "issue.antigravity_swarm_started")))).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("runs only the newest /swarm when a second request races the selection (20 rounds)", async () => {
|
||||
for (let round = 0; round < 20; round++) {
|
||||
const f = await seed();
|
||||
const wake = { issueId: f.issue.id, antigravitySwarmCommentId: f.comment.id, wakeCommentIds: [f.comment.id] };
|
||||
const run = await insertRun(f, { contextSnapshot: wake });
|
||||
const newer = saveSwarmCommentHeldOpen(f, `round ${round} write c.txt`);
|
||||
const selection = (async () => {
|
||||
await new Promise(resolve => setTimeout(resolve, Math.random() * 4));
|
||||
return resolve(f, { runId: run.id, context: wake });
|
||||
})().then(value => ({ value }), (error: unknown) => ({ error }));
|
||||
setTimeout(() => newer.commit(), Math.random() * 6);
|
||||
const [outcome, seenSelection] = await Promise.all([selection, newer.locked, newer.done]);
|
||||
const started = await db.select().from(activityLog)
|
||||
.where(and(eq(activityLog.entityId, f.issue.id), eq(activityLog.action, "issue.antigravity_swarm_started")));
|
||||
if (seenSelection) {
|
||||
// The older request was selected before the newer one was saved: it was the newest then.
|
||||
expect(outcome).toMatchObject({ value: { commentId: f.comment.id } });
|
||||
expect(started).toHaveLength(1);
|
||||
} else {
|
||||
// The newer request was saved first: the older one is replaced and never runs.
|
||||
expect(outcome).toMatchObject({ error: { message: expect.stringMatching(/newer \/swarm/) } });
|
||||
expect(started).toHaveLength(0);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* One wake can carry several user comments (coalescing merges them, newest
|
||||
* wake context last). The rule, in comment order: the newest recorded /swarm
|
||||
|
||||
@@ -141,6 +141,29 @@ export async function resolveAntigravitySwarmRun(db: Db, input: {
|
||||
remote: boolean;
|
||||
planning: boolean;
|
||||
}): Promise<AntigravitySwarmSelection | null> {
|
||||
// Most runs carry no swarm request: a cheap look without the task lock lets
|
||||
// them through. A request's record commits with its comment, so a run that
|
||||
// sees none here was not woken by one.
|
||||
if (!await recordedRequest(db, input)) return null;
|
||||
// Every fact the decision depends on (which request is newest, whether it is
|
||||
// fulfilled, running or stopped, its attempts, the user's Retry) is read again
|
||||
// under the task lock, in the transaction that records the selection. Saving
|
||||
// a user comment and its /swarm record takes the same lock (issues.addComment),
|
||||
// so a newer request either committed before these reads or saves after the
|
||||
// selection is recorded.
|
||||
const decision = await db.transaction(async (tx) => {
|
||||
await tx.select({ id: issues.id }).from(issues)
|
||||
.where(and(eq(issues.id, input.issueId), eq(issues.companyId, input.companyId))).for("update");
|
||||
return decideSwarm(tx as unknown as Db, input);
|
||||
});
|
||||
if (decision && "refusal" in decision) throw new PaperclipRunnerProviderProfileError(ANTIGRAVITY_SWARM_UNAVAILABLE, decision.refusal);
|
||||
return decision;
|
||||
}
|
||||
|
||||
type SwarmDecision = AntigravitySwarmSelection | { refusal: string } | null;
|
||||
|
||||
/** The recorded user /swarm request this run serves, if any (before any replacement). */
|
||||
async function recordedRequest(db: Db, input: Parameters<typeof resolveAntigravitySwarmRun>[1]): Promise<{ commentId: string; body: string } | null> {
|
||||
const marked = requestedAntigravitySwarmCommentId(input.context);
|
||||
const explicit = marked ?? await inheritedSwarmCommentId(db, input.companyId, input.issueId, input.runId, input.context);
|
||||
// Only an explicit request (a fresh /swarm or its retry chain) is refused for
|
||||
@@ -154,20 +177,26 @@ export async function resolveAntigravitySwarmRun(db: Db, input: {
|
||||
eq(activityLog.entityId, input.issueId), eq(activityLog.actorType, "user"), eq(activityLog.actorId, issueComments.authorUserId),
|
||||
sql`${activityLog.details}->>'commentId' = ${commentId}`))
|
||||
.limit(1);
|
||||
return requested ? { commentId, body: requested.body } : null;
|
||||
}
|
||||
|
||||
/** Runs under the task lock. Writes made before a refusal (re-queued comments) commit with it. */
|
||||
async function decideSwarm(tx: Db, input: Parameters<typeof resolveAntigravitySwarmRun>[1]): Promise<SwarmDecision> {
|
||||
const requested = await recordedRequest(tx, input);
|
||||
if (!requested) return null;
|
||||
const refuse = (message: string): never => { throw new PaperclipRunnerProviderProfileError(ANTIGRAVITY_SWARM_UNAVAILABLE, message); };
|
||||
const { commentId } = requested;
|
||||
// A later user /swarm replaces an earlier one (queued-comment-delivery.ts
|
||||
// counts the earlier one handled). When the newest request rode into this
|
||||
// wake, this run is that swarm; otherwise this request was replaced and
|
||||
// never runs, neither as a swarm nor as ordinary work.
|
||||
const wakeIds = await wakeCommentIds(db, input);
|
||||
const recordedInWake = await recordedSwarmRequestIds(db, input.companyId, input.issueId, wakeIds);
|
||||
const newest = await newestSwarmRequest(db, input.companyId, input.issueId);
|
||||
const wakeIds = await wakeCommentIds(tx, input);
|
||||
const recordedInWake = await recordedSwarmRequestIds(tx, input.companyId, input.issueId, wakeIds);
|
||||
const newest = await newestSwarmRequest(tx, input.companyId, input.issueId);
|
||||
let selected = { commentId, body: requested.body };
|
||||
if (newest && newest.id !== commentId) {
|
||||
if (!wakeIds.includes(newest.id)) {
|
||||
await queueCommentsTheSwarmDoesNotSend(db, { ...input, commentId });
|
||||
refuse("A newer /swarm request on this task replaced this one, so this one did not run.");
|
||||
await queueCommentsTheSwarmDoesNotSend(tx, { ...input, commentId });
|
||||
return { refusal: "A newer /swarm request on this task replaced this one, so this one did not run." };
|
||||
}
|
||||
selected = { commentId: newest.id, body: newest.body };
|
||||
}
|
||||
@@ -176,63 +205,58 @@ export async function resolveAntigravitySwarmRun(db: Db, input: {
|
||||
const request = antigravitySwarmRequestFromComment(selected.body);
|
||||
// A requested swarm with nothing (or too much) to run is refused, never run as ordinary work.
|
||||
const requestRefusal = antigravitySwarmRequestRefusal(request);
|
||||
if (requestRefusal) refuse(requestRefusal);
|
||||
return selectSwarm(db, { ...input, commentId: selected.commentId, request, replacedCommentIds, refuse });
|
||||
if (requestRefusal) return { refusal: requestRefusal };
|
||||
return selectSwarm(tx, { ...input, commentId: selected.commentId, request, replacedCommentIds });
|
||||
}
|
||||
|
||||
async function selectSwarm(db: Db, input: Parameters<typeof resolveAntigravitySwarmRun>[1] & {
|
||||
commentId: string; request: string; replacedCommentIds: string[]; refuse: (message: string) => never;
|
||||
}): Promise<AntigravitySwarmSelection | null> {
|
||||
const { commentId, request, refuse } = input;
|
||||
return db.transaction(async (tx) => {
|
||||
// One selection at a time per task, so one request starts one swarm.
|
||||
await tx.select({ id: issues.id }).from(issues)
|
||||
.where(and(eq(issues.id, input.issueId), eq(issues.companyId, input.companyId))).for("update");
|
||||
const attempts = await tx.select({ id: heartbeatRuns.id, status: heartbeatRuns.status, createdAt: heartbeatRuns.createdAt,
|
||||
finishedAt: heartbeatRuns.finishedAt, updatedAt: heartbeatRuns.updatedAt }).from(heartbeatRuns)
|
||||
.where(and(eq(heartbeatRuns.companyId, input.companyId), ne(heartbeatRuns.id, input.runId),
|
||||
sql`${heartbeatRuns.contextSnapshot}->>'antigravitySwarmCommentId' = ${commentId}`));
|
||||
if (attempts.some(run => run.status === "succeeded")) return null;
|
||||
if (attempts.some(run => ["queued", "running"].includes(run.status))) refuse("A swarm for this request is already running.");
|
||||
// The user asked for this attempt: it is the request's first run (the
|
||||
// fresh /swarm comment), or this run is the one the user's Retry started,
|
||||
// clicked after the last attempt began and after any Stop. Run context and
|
||||
// wake reasons are not proof (recoveries copy context, and an agent can
|
||||
// name any wake reason), so Retry is read from the user's own retry
|
||||
// request. A click that joined an attempt already running belongs to that
|
||||
// attempt. Everything else (recovery, sweeps, continuations, automatic
|
||||
// retries) is automatic and bounded below.
|
||||
const latestOf = (dates: Date[]) => dates.reduce<Date | null>((latest, at) => !latest || at > latest ? at : latest, null);
|
||||
const lastAttemptAt = latestOf(attempts.map(run => run.createdAt));
|
||||
const stoppedAt = latestOf(attempts.filter(run => run.status === "cancelled").map(run => run.finishedAt ?? run.updatedAt));
|
||||
const manual = lastAttemptAt === null || await userRetryStartedRun(tx as unknown as Db, {
|
||||
companyId: input.companyId, agentId: input.agent.id, issueId: input.issueId, runId: input.runId,
|
||||
since: stoppedAt && stoppedAt > lastAttemptAt ? stoppedAt : lastAttemptAt });
|
||||
if (!manual && attempts.some(run => run.status === "cancelled")) {
|
||||
refuse("The swarm for this request was stopped. Send /swarm again, or use Retry, to start it again.");
|
||||
}
|
||||
if (!manual && attempts.filter(run => ["failed", "timed_out"].includes(run.status)).length >= ANTIGRAVITY_SWARM_AUTOMATIC_ATTEMPTS) {
|
||||
refuse("The swarm did not finish after three attempts, so Paperclip stopped retrying it. Send /swarm again to start a new one.");
|
||||
}
|
||||
const refusal = antigravitySwarmRefusal({ enabled: await swarmEnabled(tx as unknown as Db), agent: input.agent, remote: input.remote, planning: input.planning });
|
||||
if (refusal) refuse(refusal);
|
||||
await queueCommentsTheSwarmDoesNotSend(tx as unknown as Db, { ...input, commentId });
|
||||
// The one stored fact every later decision reads: this run serves this request.
|
||||
await tx.update(heartbeatRuns)
|
||||
.set({ contextSnapshot: sql`coalesce(${heartbeatRuns.contextSnapshot}, '{}'::jsonb) || ${JSON.stringify({ antigravitySwarmCommentId: commentId })}::jsonb` })
|
||||
.where(and(eq(heartbeatRuns.id, input.runId), eq(heartbeatRuns.companyId, input.companyId)));
|
||||
await logActivity(tx as unknown as 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,
|
||||
...(input.replacedCommentIds.length ? { replacedCommentIds: input.replacedCommentIds } : {}),
|
||||
summary: (input.replacedCommentIds.length ? "Your latest /swarm request replaced the earlier one, which did not run. " : "") +
|
||||
"Started a Google Antigravity swarm with helpers. Helper actions are approved automatically by Antigravity's auto mode. Paperclip does not sandbox the helpers on this computer and cannot see their shell commands, which can write outside the task folder (for example in temp directories). Usage is one total for the coordinator and its helpers.",
|
||||
},
|
||||
});
|
||||
return { commentId, request };
|
||||
async function selectSwarm(tx: Db, input: Parameters<typeof resolveAntigravitySwarmRun>[1] & {
|
||||
commentId: string; request: string; replacedCommentIds: string[];
|
||||
}): Promise<SwarmDecision> {
|
||||
const { commentId, request } = input;
|
||||
const attempts = await tx.select({ id: heartbeatRuns.id, status: heartbeatRuns.status, createdAt: heartbeatRuns.createdAt,
|
||||
finishedAt: heartbeatRuns.finishedAt, updatedAt: heartbeatRuns.updatedAt }).from(heartbeatRuns)
|
||||
.where(and(eq(heartbeatRuns.companyId, input.companyId), ne(heartbeatRuns.id, input.runId),
|
||||
sql`${heartbeatRuns.contextSnapshot}->>'antigravitySwarmCommentId' = ${commentId}`));
|
||||
if (attempts.some(run => run.status === "succeeded")) return null;
|
||||
if (attempts.some(run => ["queued", "running"].includes(run.status))) return { refusal: "A swarm for this request is already running." };
|
||||
// The user asked for this attempt: it is the request's first run (the
|
||||
// fresh /swarm comment), or this run is the one the user's Retry started,
|
||||
// clicked after the last attempt began and after any Stop. Run context and
|
||||
// wake reasons are not proof (recoveries copy context, and an agent can
|
||||
// name any wake reason), so Retry is read from the user's own retry
|
||||
// request. A click that joined an attempt already running belongs to that
|
||||
// attempt. Everything else (recovery, sweeps, continuations, automatic
|
||||
// retries) is automatic and bounded below.
|
||||
const latestOf = (dates: Date[]) => dates.reduce<Date | null>((latest, at) => !latest || at > latest ? at : latest, null);
|
||||
const lastAttemptAt = latestOf(attempts.map(run => run.createdAt));
|
||||
const stoppedAt = latestOf(attempts.filter(run => run.status === "cancelled").map(run => run.finishedAt ?? run.updatedAt));
|
||||
const manual = lastAttemptAt === null || await userRetryStartedRun(tx, {
|
||||
companyId: input.companyId, agentId: input.agent.id, issueId: input.issueId, runId: input.runId,
|
||||
since: stoppedAt && stoppedAt > lastAttemptAt ? stoppedAt : lastAttemptAt });
|
||||
if (!manual && attempts.some(run => run.status === "cancelled")) {
|
||||
return { refusal: "The swarm for this request was stopped. Send /swarm again, or use Retry, to start it again." };
|
||||
}
|
||||
if (!manual && attempts.filter(run => ["failed", "timed_out"].includes(run.status)).length >= ANTIGRAVITY_SWARM_AUTOMATIC_ATTEMPTS) {
|
||||
return { refusal: "The swarm did not finish after three attempts, so Paperclip stopped retrying it. Send /swarm again to start a new one." };
|
||||
}
|
||||
const refusal = antigravitySwarmRefusal({ enabled: await swarmEnabled(tx), agent: input.agent, remote: input.remote, planning: input.planning });
|
||||
if (refusal) return { refusal };
|
||||
await queueCommentsTheSwarmDoesNotSend(tx, { ...input, commentId });
|
||||
// The one stored fact every later decision reads: this run serves this request.
|
||||
await tx.update(heartbeatRuns)
|
||||
.set({ contextSnapshot: sql`coalesce(${heartbeatRuns.contextSnapshot}, '{}'::jsonb) || ${JSON.stringify({ antigravitySwarmCommentId: commentId })}::jsonb` })
|
||||
.where(and(eq(heartbeatRuns.id, input.runId), eq(heartbeatRuns.companyId, input.companyId)));
|
||||
await logActivity(tx, {
|
||||
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,
|
||||
...(input.replacedCommentIds.length ? { replacedCommentIds: input.replacedCommentIds } : {}),
|
||||
summary: (input.replacedCommentIds.length ? "Your latest /swarm request replaced the earlier one, which did not run. " : "") +
|
||||
"Started a Google Antigravity swarm with helpers. Helper actions are approved automatically by Antigravity's auto mode. Paperclip does not sandbox the helpers on this computer and cannot see their shell commands, which can write outside the task folder (for example in temp directories). Usage is one total for the coordinator and its helpers.",
|
||||
},
|
||||
});
|
||||
return { commentId, request };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user