fix(antigravity): approve the swarm's own Paperclip tools as the CLI names them, and never auto-retry a swarm that ended without a result

Live run6 (2026-11-24): the coordinator's first call was Paperclip's own
get_task_context. CLI 1.5.15 names a client MCP call mcp_<server>_<tool> with
no _meta (recorded payload now in test-fixtures), but the swarm handler only
approved _meta.mcp.server, so it refused the call; the CLI then cancelled its
whole turn on the denial (cli.log: approved=false, then CancelCascadeInvocation).
The turn ended without a result, became native_session_interrupted with the
Runner's raw continuation text, and recovery started two more billed swarms.

- Approve a permission request only for a tool the run's Paperclip bridge
  admits, by the CLI's own tool name (fails closed otherwise); the fake CLI
  replays the recorded request and hands in through paperclip_finish.
- Report a refused Paperclip tool as such; the CLI echoing Paperclip's own
  denial is not 'a helper blocked outside the task folder'.
- A swarm turn without a hand-in fails as ANTIGRAVITY_SWARM_NO_RESULT with a
  plain reason. The server settles any swarm-route failure code as
  antigravity_swarm_unavailable (terminal, no continuation recovery), and run
  selection never starts another swarm automatically after one; the person's
  Retry still does. A lost CLI process still recovers on its own.
- Record whether the CLI's sign-in matches the connection account in the run
  history every time, with the reason when it cannot tell.
- Scope usage-limit evidence by the executed provider mode, so limits from
  swarms started before scopes existed (and their retries) hold only swarms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-24 18:17:59 -06:00
1 parent 74454a17f3
commit ea2753c3d7
13 files changed
+395 -27

No files matched your search

@@ -9327,6 +9327,11 @@ function classifyNativeSessionFailure(
if (error instanceof NativeProviderSideEffectPendingError) return NATIVE_PROVIDER_SIDE_EFFECT_PENDING;
if (error instanceof NativeProviderTerminalFailure) {
if (error.providerCode === "approval_required") return "native_provider_approval_required";
// A /swarm turn the swarm route failed by its own closed code (no result,
// helpers timed out, a blocked escape): each automatic attempt is a full,
// billed swarm, so the person decides whether to retry it. Interruptions
// (a lost CLI process) keep their own code and still recover on their own.
if (/^ANTIGRAVITY_SWARM_[A-Z_]+$/.test(error.providerCode)) return ANTIGRAVITY_SWARM_UNAVAILABLE;
// Failed terminals retain their security meaning across the provider facade.
// A stopped process is insufficient evidence to recover an integrity breach.
if (
@@ -5,6 +5,7 @@ import { readProviderFailure, type ProviderFailure } from "@paperclipai/shared";
import { logger } from "../../middleware/logger.js";
import { infrastructureRetryDelayMs, PROVIDER_LIMIT_DEFAULT_COOLDOWN_MS } from "../system-recovery-state.js";
import { isProviderQuotaActionRequired, parseProviderQuotaClockReset } from "../provider-quota-reset.js";
import { ANTIGRAVITY_CLI_QUOTA_SCOPE } from "../antigravity-swarm-refusal.js";
/** Categories that report an exhausted account or request allowance. */
const PROVIDER_LIMIT_CATEGORIES = ["rate_limit", "quota_exhausted"];
@@ -54,15 +55,28 @@ const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{
export const PROVIDER_QUOTA_SCOPE_CONTEXT_KEY = "providerQuotaScope";
const QUOTA_SCOPE_PATTERN = /^[a-z][a-z0-9_]{0,63}$/;
/** The quota scope a run recorded; null is the credential's default quota. */
export function runProviderQuotaScope(contextSnapshot: unknown): string | null {
/**
* The quota scope a run drew on; null is the credential's default quota. A run
* that executed the Antigravity swarm route draws on the CLI's own pool even
* when its context lacks the field (runs started before scopes existed, and
* their retries), so the executed provider mode in its runner profile decides.
*/
export function runProviderQuotaScope(contextSnapshot: unknown, runnerProfileJson?: unknown): string | null {
const value = contextSnapshot && typeof contextSnapshot === "object"
? (contextSnapshot as Record<string, unknown>)[PROVIDER_QUOTA_SCOPE_CONTEXT_KEY] : undefined;
return typeof value === "string" && QUOTA_SCOPE_PATTERN.test(value) ? value : null;
if (typeof value === "string" && QUOTA_SCOPE_PATTERN.test(value)) return value;
const profile = runnerProfileJson && typeof runnerProfileJson === "object" ? runnerProfileJson as Record<string, unknown> : {};
const input = profile.nativeExecutionInput && typeof profile.nativeExecutionInput === "object" ? profile.nativeExecutionInput as Record<string, unknown> : {};
const provider = input.provider && typeof input.provider === "object" ? input.provider as Record<string, unknown> : {};
return provider.kind === "acpx" && provider.agent === "antigravity" && provider.mode === "swarm" ? ANTIGRAVITY_CLI_QUOTA_SCOPE : null;
}
function quotaScopeEvidenceFilter(scope: string | null) {
const recorded = sql`${heartbeatRuns.contextSnapshot} ->> ${PROVIDER_QUOTA_SCOPE_CONTEXT_KEY}`;
// The same derivation as runProviderQuotaScope, in SQL, so old swarm evidence holds only swarms.
const executed = sql`${heartbeatRuns.runnerProfileJson} -> 'nativeExecutionInput' -> 'provider'`;
const recorded = sql`coalesce(${heartbeatRuns.contextSnapshot} ->> ${PROVIDER_QUOTA_SCOPE_CONTEXT_KEY},
case when ${executed} ->> 'kind' = 'acpx' and ${executed} ->> 'agent' = 'antigravity' and ${executed} ->> 'mode' = 'swarm'
then ${ANTIGRAVITY_CLI_QUOTA_SCOPE} end)`;
return scope === null ? sql`${recorded} is null` : sql`${recorded} = ${scope}`;
}
@@ -233,7 +247,7 @@ export async function planNativeStartupRetry(input: {
// Limit evidence only refines the schedule. A failed read must not stop the
// failure from settling, so the retry falls back to plain backoff.
const providerCooldownAt = await input.db
.select({ contextSnapshot: heartbeatRuns.contextSnapshot }).from(heartbeatRuns)
.select({ contextSnapshot: heartbeatRuns.contextSnapshot, runnerProfileJson: heartbeatRuns.runnerProfileJson }).from(heartbeatRuns)
.where(and(eq(heartbeatRuns.companyId, input.binding.companyId), eq(heartbeatRuns.id, input.binding.runId)))
.then(([run]) => agentProviderLimitCooldown(input.db, {
companyId: input.binding.companyId,
@@ -242,7 +256,7 @@ export async function planNativeStartupRetry(input: {
failure: input.failure,
// The failure was reported on the account this run started with.
credential: runProviderLimitCredential(run?.contextSnapshot),
quotaScope: runProviderQuotaScope(run?.contextSnapshot),
quotaScope: runProviderQuotaScope(run?.contextSnapshot, run?.runnerProfileJson),
now: input.now,
})).then((cooldown) => cooldown?.resetAt ?? null, (error: unknown) => {
logger.warn({ err: error, runId: input.binding.runId }, "native provider limit evidence unavailable");