mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-25 07:27:13 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The native Runner starts each agent harness and owns its lifecycle, credentials and recovery > - Google Antigravity is Google's only first-party coding harness, and it is the harness for Gemini 4 Neon > - The Runner had an Antigravity integration, but only as a pending candidate: ACP server 1.4.3, macOS ARM64 only, Google sign-in only, closed unless an operator opened it for one company and one model > - Cloud sandboxes run Linux and use API keys, so the candidate could not run there, and it had one attended test cell > - This pull request moves the integration to ACP server 1.5.2, makes it run on Linux and with a Gemini API key, adds the tests the other ACPX harnesses have, and removes the code paths that would block recovery after release > - The benefit is that the release of Antigravity becomes a small declaration change after the live suites pass. The profile stays `pending` in this pull request. ## Linked Issues or Issue Description Refs #98 Refs #6559 Refs #8523 Refs #21121 (a separate legacy-style `agy_local` adapter that uses the CLI print protocol; this pull request does not replace or include it) ## What Changed **Runtime** - Pin Antigravity ACP server 1.5.2 (was 1.4.3). Owned admission patch is `1.5.2-paperclip.2`. - Verified patched closures for `darwin-arm64`, `linux-x64` and `linux-arm64`. `darwin-x64` stays inventory-only. - The patcher reads Mach-O and ELF builds. It signs macOS output only. One admission source serves all platforms. - Platform gates in TypeScript, Rust, the sidecar schema and the server admit each host that has a verified closure. **Auth and models** - Gemini API-key auth reaches an exact-model session receipt. Admission accepts a model only when the base model and the thinking level both match the requested ID. - API-key connections get a model list on local and cloud environments. The list is generated at build time from the pinned runtime. The server starts no process to read it. - Gemini 4 Neon, Carbon, Argon and Hydrogen are selectable. The picker marks them "needs a paid Gemini API plan" for API-key connections. - One shared module maps model ID to family and thinking level. - `google/gemini-4-neon` is added to the OpenCode model list. A new model audit is in `doc/adapter-model-audit-2026-11-22.md`. **Lifecycle and failures** - A provider error no longer completes the turn. The driver classifies it: overloaded, rate limit, quota, auth rejected, model unavailable. - Overload retries are bounded (3 attempts, about 14 seconds). - A refused session reload maps to incompatible saved state, so the automatic replacement conversation applies. - Server gates that refused restart recovery, remote targets and shared state for Antigravity now apply only while the profile is pending. - A Google sign-in connection with a cloud environment is refused at save and at Test, with a plain message. - A released profile rejects candidate grants in TypeScript and in Rust. **Launch cost and integrity (all native ACPX providers)** - Each closure is verified one time into a sealed read-only store. Later launches check file identities and hash the exact executable they start. - Remote provider-pack verification is recorded per sandbox lease. Later runs check kernel identity and fall back to the full hash on any difference. - Superseded stores are removed in the background after a launch. **Provider pack and image** - The provider pack carries Antigravity for `linux-x64` as a candidate provider. It becomes an ordinary provider from the profile status alone. - The Daytona image build accepts the candidate. A credential-free smoke runs in the build. **Tests and CI** - New `antigravity-qualification` suite: 24 cells, local and Daytona. - New model lineup suites: 14 API-key cells and 14 Gemini 4 cells. Each cell requires the exact model in the Runner receipt. - Antigravity can run the `chat-features` cases with `--profile runner-acpx-antigravity`. - `runner-full-stack-e2e.yml` passes `GEMINI_API_KEY` for the new profile and derives candidate providers from the selected Daytona cells. **Docs** - `doc/plans/2026-11-22-antigravity-qualification.md` is the current release contract, acceptance ledger and operator recipe. ## Verification Commands that passed on the final revision: - `pnpm build` - Typecheck for all packages, including the runner-e2e test projects - `pnpm test:e2e:runner:unit`: 11,665 Vitest tests passed, 25 skipped; 386 node tests passed - `cargo test -p paperclip-runner-core -- --test-threads=4`: 1,199 passed, 2 failed. The 2 failures are Codex transport timing tests that pass alone and on `master`. - `cargo fmt --check`, `check:acpx-profiles`, `check:protocol-types`, `node scripts/check-maps.mjs` - Native patch tests: 9 of 9 on `darwin-arm64` and 9 of 9 on `linux-x64` - UI picker tests: 397 of 397 Known failures that also fail on `master`: - 11 runner package script tests (browser, Pi and package-boundary checks) - 6 `runtime-selection` tests that read an untracked local skill file Live evidence, without a prompt: - Clean provisioning on `darwin-arm64` and on `linux-x64` in a Daytona sandbox. The closure digests reproduce. A changed or missing file is rejected. - `initialize` reports 1.5.2. An API-key session returns a receipt for exactly `gemini-3.8-flash-low` and the exact instruction bytes, on both platforms. - All 28 model IDs (14 Gemini 3, 14 Gemini 4) return an exact receipt under an API key. - A session survives a process kill and resumes in a new process with exact receipts, on both platforms. - The server's remote path verifies the provider pack in a Daytona sandbox. - Run start to native `initialize`: 3.45 s to 1.46 s on macOS, 3.65 s to 1.74 s on Linux x64, before the per-launch executable hash was restored (about 60 ms on macOS; not measured on Linux). - Remote pack verification on a reused sandbox lease: about 4.3 s to about 0.25 s. Live evidence, with a prompt, on an already-released harness: - Pi native, `pi-native.runner-acpx-pi.local.agent-files-fresh-run`, on `darwin-arm64` at `427e9b088b`: 1 of 1 passed, 14 of 14 checks, both native runs succeeded (25.5 s and 25.7 s). The process started from the new launch store with no fallback warning. - The same cell and `native-pending-controller-restart` failed in the paid workflow on this branch and on `master` in the same way, before any native start. The workflow does not install the Pi runtime for local cells, so the agent setup check fails. That is a workflow gap, not part of this change. **Not run.** No Antigravity prompt completed on 1.5.2 on any platform. The only Gemini key available is on the free tier, and Google returned 503 or 429 for each attempt on two days. These gates are open: - All 24 `antigravity-qualification` cells - Both model lineup suites, including inference on Gemini 4 Neon - The attended Google sign-in journey on 1.5.2 - The chat cases under Antigravity - Image pull time and startup time on a published image - A native start on `linux-arm64` - A live provider-death cell for Kimi, Pi or Copilot on the new launch store (one ordinary Pi cell passed live; see above) ## Risks - **The launch store changes how every native ACPX provider starts** (Kimi, Copilot, Cursor, Pi), not only Antigravity. A review found two defects in the first version (an executable swap was possible, and Pi launches broke). Both are fixed with tests. One live Pi cell passed on this branch on macOS. No live provider-death cell and no Linux cell ran for those providers. - The profile digest changes. Saved Antigravity candidate sessions and existing candidate grants stop working. The new release refuses old state by name and starts a replacement conversation. - The Linux provider pack and the Daytona image grow by about 834 MB (about 274 MB compressed) when Antigravity is included. - The pack verification record is in server memory. The first run after a server restart pays the full hash again. - Antigravity 1.5.2 reads the workspace `GEMINI.md` and `AGENTS.md` as user rules. This is deliberate and matches Codex. Any other rule file is refused. - Orchestrator mode is not available. ACP server 1.5.2 does not expose it. - Antigravity stays `pending`. Normal users cannot run it until a later change removes the pending status. ## Model Used - Claude Fable 5.2 (`claude-fable-5-2`) planned, briefed and integrated the work, with tool use. - Claude Opus 5.5 (`claude-opus-5-5`) subagents wrote the code and ran the tests, with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
563 lines
46 KiB
TypeScript
563 lines
46 KiB
TypeScript
import { expect, it, vi } from "vitest";
|
|
import { runnerMatrix } from "./catalog.js";
|
|
import { ANTIGRAVITY_JOURNEY_EXECUTION } from "./antigravity-journey-cases.js";
|
|
import { parseAntigravityJourneyConfig } from "./antigravity-journey-config.js";
|
|
import { ConnectionFailure } from "./connection-config.js";
|
|
import { antigravityNoLaunchReceipt, cleanupAntigravityJourney, observeAntigravityRun, parseAntigravityOwnedResources, selectAntigravityJourneyEnvironment, type AntigravityJourneyResources, type AntigravityProcessObservers } from "./antigravity-journey-fixtures.js";
|
|
import { parseRunnerSelectors, selectRunnerExecutions } from "./selectors.js";
|
|
import type { ConnectionApi } from "./connection-target.js";
|
|
import { antigravityKnownCostObservation } from "./antigravity-journey-launch.js";
|
|
|
|
// These cleanup-ordering calibrations use synthetic retired process receipts.
|
|
vi.mock("node:child_process", async original => ({ ...await original<typeof import("node:child_process")>(), execFileSync: vi.fn(() => "") }));
|
|
vi.mock("./antigravity-journey-evidence.js", async original => ({ ...await original<typeof import("./antigravity-journey-evidence.js")>(),
|
|
reacquireAntigravityNativeFences: vi.fn(async () => {}) }));
|
|
|
|
const companyId = "11111111-1111-4111-8111-111111111111", environmentId = "22222222-2222-4222-8222-222222222222";
|
|
const connectionId = "33333333-3333-4333-8333-333333333333", agentId = "44444444-4444-4444-8444-444444444444", runId = "55555555-5555-4555-8555-555555555555";
|
|
function fixture() { return { version: 1, setupId: "synthetic", hosting: "self-hosted", executionEnvironment: "local",
|
|
target: { mode: "attach", baseURL: "http://127.0.0.1:3104", expectedCommit: "a".repeat(40), deploymentMode: "local_trusted", companyId, environmentId },
|
|
browser: { headed: false, channel: "chromium", freshness: "signed-in" }, savedConnection: { id: connectionId, name: "Synthetic Google subscription", ownerUserId: "synthetic-user" },
|
|
candidate: { platform: "darwin", architecture: "arm64", nativeVersion: "1.5.2-paperclip.2", profileDigest: `sha256:${"b".repeat(64)}`, nativeClosureDigest: `sha256:${"c".repeat(64)}` },
|
|
limits: { durationMs: 900_000, turnTimeoutMs: 300_000, maxRuns: 2, budgetCents: 100 } }; }
|
|
function resources(): AntigravityJourneyResources { return { version: 1, executionId: ANTIGRAVITY_JOURNEY_EXECUTION, origin: "http://127.0.0.1:3104",
|
|
companyId, environmentId, connectionId, agentName: "Antigravity QA 0123456789ab", agentId, runIds: [], processes: {}, retained: false, setupProbe: "not_started" }; }
|
|
function neverLaunched() {
|
|
const owned = resources(); owned.runIds = [runId]; owned.issueId = "66666666-6666-4666-8666-666666666666"; owned.setupProbe = "retired";
|
|
const leaseId = "77777777-7777-4777-8777-777777777777";
|
|
const run: Record<string, any> = { id: runId, companyId, agentId, issueId: owned.issueId, status: "cancelled", executionStage: "settled", costAccountingPending: false,
|
|
runtimeMode: "legacy", runtimeModeResolvedAt: null, controllerLeaseExpiresAt: null, processPid: null, processGroupId: null, processStartedAt: null,
|
|
runnerInstanceId: null, nativeSessionId: null, driverKind: null, completionContractId: null, runnerProfileJson: { adapterDispatch: { adapterType: "paperclip_runner" } },
|
|
resultJson: { startupCancellation: { beforeNativeSelection: true, errorCode: "cancelled", requestedAt: "2026-01-01T00:00:00Z" }, startupPreparationSettledAt: "2026-01-01T00:00:02Z" },
|
|
contextSnapshot: { paperclipEnvironment: { id: environmentId, driver: "local", leaseId } } };
|
|
const lease: Record<string, any> = { id: leaseId, companyId, heartbeatRunId: runId, environmentId, status: "expired", releasedAt: "2026-01-01T00:00:01Z" };
|
|
return { owned, run, lease };
|
|
}
|
|
it("retains accepted physical ownership when a primary report projection throws", () => {
|
|
const { owned, run } = neverLaunched(), pid = 900001, start = "2026-01-01T00:00:00Z";
|
|
Object.assign(run, { processPid: pid, processGroupId: pid, processStartedAt: start });
|
|
owned.processes[runId] = { captured: false, identityChanged: false, journal: [] };
|
|
const journal = [{ pid, parent: 1, start }, { pid: pid + 1, parent: pid, start }];
|
|
const observers = new Map([[runId, { sample: () => ({ captured: true, identityChanged: false, live: [pid, pid + 1], journal,
|
|
authorityCheck: { accepted: true, commandSha256: "malformed-projection-digest", authority: { runId, pid, groupId: pid, startedAt: start }, observed: journal[0] } }) }]]) as unknown as AntigravityProcessObservers;
|
|
expect(() => observeAntigravityRun(owned, run, observers)).toThrow(/primary_owner_/u);
|
|
expect(owned.processes[runId]).toEqual({ captured: true, identityChanged: false, journal });
|
|
expect(owned.processes[runId]!.primaryOwner).toBeUndefined();
|
|
});
|
|
|
|
it.each(["released", "expired"])("persists authoritative pre-native cancellation with a %s lease before purge", async status => {
|
|
const { owned, run, lease } = neverLaunched(); lease.status = status;
|
|
let lifecycleState = "ready", saved = false;
|
|
const agent = () => ({ id: agentId, companyId, name: owned.agentName, lifecycleState });
|
|
const get = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/agents")) return [agent()];
|
|
if (route.includes("heartbeat-runs?")) return [run];
|
|
if (route === `/api/heartbeat-runs/${runId}`) return run;
|
|
if (route === `/api/environment-leases/${lease.id}`) return lease;
|
|
if (route === `/api/issues/${owned.issueId}`) return { id: owned.issueId, companyId, assigneeAgentId: agentId, status: "cancelled" };
|
|
if (route === `/api/agents/${agentId}`) return agent();
|
|
if (route.endsWith("/ai-connections")) return { connections: [{ id: connectionId, provider: "google", method: "subscription", ownerUserId: "synthetic-user", isDefault: true, status: "connected" }] };
|
|
throw new Error(`Unexpected cleanup read ${route}`);
|
|
});
|
|
const remove = vi.fn(async () => { expect(saved).toBe(true); expect(owned.processes[runId]?.captured).toBe(false); });
|
|
const post = vi.fn(async (route: string) => { expect(route).toBe(`/api/agents/${agentId}/terminate`); lifecycleState = "terminated"; });
|
|
const cleanup = { agent: false, runs: false, connectionPreserved: false };
|
|
await cleanupAntigravityJourney({ api: { get, post, patch: async () => {}, delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: owned, cleanup, captureRun: async () => {}, persistResources: async () => {
|
|
if (owned.noLaunch?.[runId]) { expect(parseAntigravityOwnedResources(owned, parseAntigravityJourneyConfig(fixture()))).toBe(owned); saved = true; }
|
|
} });
|
|
expect(remove).toHaveBeenCalledOnce(); expect(cleanup).toEqual({ agent: true, runs: true, connectionPreserved: true });
|
|
expect(owned.noLaunch?.[runId]).toMatchObject({ kind: "cancelled-before-native-selection", runId, leaseStatus: status, leaseReleasedAt: lease.releasedAt });
|
|
});
|
|
|
|
it.each([
|
|
["missing settlement", (f: ReturnType<typeof neverLaunched>) => { delete f.run.resultJson.startupPreparationSettledAt; }],
|
|
["false before-selection receipt", (f: ReturnType<typeof neverLaunched>) => { f.run.resultJson.startupCancellation.beforeNativeSelection = false; }],
|
|
["preparation pending", (f: ReturnType<typeof neverLaunched>) => { f.run.executionStage = "preparing"; }],
|
|
["unsettled accounting", (f: ReturnType<typeof neverLaunched>) => { f.run.costAccountingPending = true; }],
|
|
["native owner", (f: ReturnType<typeof neverLaunched>) => { f.run.runnerInstanceId = "native-owner"; }],
|
|
["missing null owner", (f: ReturnType<typeof neverLaunched>) => { delete f.run.processPid; }],
|
|
["captured process", (f: ReturnType<typeof neverLaunched>) => { f.owned.processes[runId] = { captured: true, identityChanged: false, journal: [] }; }],
|
|
["unreleased expired lease", (f: ReturnType<typeof neverLaunched>) => { f.lease.releasedAt = null; }],
|
|
["active lease", (f: ReturnType<typeof neverLaunched>) => { f.lease.status = "active"; }],
|
|
["late lease release", (f: ReturnType<typeof neverLaunched>) => { f.lease.releasedAt = "2026-01-01T00:00:03Z"; }],
|
|
["foreign lease", (f: ReturnType<typeof neverLaunched>) => { f.lease.heartbeatRunId = agentId; }],
|
|
["foreign environment", (f: ReturnType<typeof neverLaunched>) => { f.lease.environmentId = agentId; }],
|
|
])("rejects no-launch cleanup with %s", (_label, edit) => {
|
|
const f = neverLaunched(); edit(f);
|
|
expect(() => antigravityNoLaunchReceipt(f.run, f.lease, f.owned)).toThrow("cleanup_no_launch_receipt_unconfirmed");
|
|
});
|
|
it("retains the agent if authoritative no-launch evidence cannot be persisted", async () => {
|
|
const { owned, run, lease } = neverLaunched(), post = vi.fn(), remove = vi.fn();
|
|
const get = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/agents")) return [{ id: agentId, companyId, name: owned.agentName }];
|
|
if (route.includes("heartbeat-runs?")) return [run];
|
|
if (route === `/api/heartbeat-runs/${runId}`) return run;
|
|
if (route === `/api/environment-leases/${lease.id}`) return lease;
|
|
throw new Error("Unexpected cleanup read");
|
|
});
|
|
await expect(cleanupAntigravityJourney({ api: { get, post, patch: vi.fn(), delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: owned, cleanup: { agent: false, runs: false, connectionPreserved: false }, captureRun: async () => {},
|
|
persistResources: async () => { if (owned.noLaunch?.[runId]) throw new Error("fixture receipt storage unavailable"); } })).rejects.toThrow("fixture receipt storage unavailable");
|
|
expect(remove).not.toHaveBeenCalled(); expect(post).not.toHaveBeenCalled();
|
|
});
|
|
it("rejects altered retained no-launch identity, release timing, or extra fields", () => {
|
|
for (const patch of [{ runId: agentId }, { leaseStatus: "active" }, { leaseReleasedAt: "2026-01-01T00:00:03Z" }, { rawPrivatePayload: "unexpected" }]) {
|
|
const { owned, run, lease } = neverLaunched();
|
|
owned.noLaunch = { [runId]: { ...antigravityNoLaunchReceipt(run, lease, owned), ...patch } as any };
|
|
expect(() => parseAntigravityOwnedResources(owned, parseAntigravityJourneyConfig(fixture()))).toThrow("invalid_antigravity_owned_resources");
|
|
}
|
|
});
|
|
it.each(["settles", "timeout", "wrong_owner", "malformed"])("waits for an original pre-native cleanup owner without weakening proof (%s)", async scenario => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const { owned, run, lease } = neverLaunched(), settledRun = structuredClone(run), settledLease = structuredClone(lease);
|
|
run.executionStage = "preparing"; run.costAccountingPending = true; delete run.resultJson.startupPreparationSettledAt;
|
|
run.controllerLeaseExpiresAt = "2026-01-01T00:00:04Z"; lease.status = "active"; lease.releasedAt = null;
|
|
if (scenario === "wrong_owner") lease.companyId = agentId;
|
|
if (scenario === "malformed") run.resultJson.startupCancellation.requestedAt = "invalid-timestamp";
|
|
let fullReads = 0, lifecycleState = "ready", persisted = false;
|
|
const agent = () => ({ id: agentId, companyId, name: owned.agentName, lifecycleState });
|
|
const get = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/agents")) return [agent()];
|
|
if (route.includes("heartbeat-runs?")) return [structuredClone(run)];
|
|
if (route === `/api/heartbeat-runs/${runId}`) {
|
|
if (++fullReads >= 3 && scenario === "settles") { Object.assign(run, settledRun); Object.assign(lease, settledLease); }
|
|
return structuredClone(run);
|
|
}
|
|
if (route === `/api/environment-leases/${lease.id}`) return structuredClone(lease);
|
|
if (route === `/api/issues/${owned.issueId}`) return { id: owned.issueId, companyId, assigneeAgentId: agentId, status: "cancelled" };
|
|
if (route === `/api/agents/${agentId}`) return agent();
|
|
if (route.endsWith("/ai-connections")) return { connections: [{ id: connectionId, provider: "google", method: "subscription", ownerUserId: "synthetic-user", isDefault: true, status: "connected" }] };
|
|
throw new Error("Unexpected cleanup read");
|
|
});
|
|
const remove = vi.fn(async () => { expect(persisted).toBe(true); });
|
|
const post = vi.fn(async (route: string) => { expect(route).toBe(`/api/agents/${agentId}/terminate`); lifecycleState = "terminated"; });
|
|
const outcome: { completed: boolean; error?: unknown } = { completed: false };
|
|
const operation = cleanupAntigravityJourney({ api: { get, post, patch: vi.fn(), delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: owned, cleanup: { agent: false, runs: false, connectionPreserved: false }, captureRun: async () => {},
|
|
persistResources: async () => { persisted ||= Boolean(owned.noLaunch?.[runId]); } }).then(() => { outcome.completed = true; }, error => { outcome.error = error; });
|
|
await vi.advanceTimersByTimeAsync(scenario === "timeout" ? 60_500 : 1_000);
|
|
await operation;
|
|
if (scenario === "settles") {
|
|
expect(outcome.error).toBeUndefined(); expect(outcome.completed).toBe(true); expect(fullReads).toBeGreaterThanOrEqual(3);
|
|
expect(remove).toHaveBeenCalledOnce(); expect(owned.noLaunch?.[runId]).toMatchObject({ leaseStatus: "expired", startupPreparationSettledAt: settledRun.resultJson.startupPreparationSettledAt });
|
|
} else {
|
|
expect(String(outcome.error)).toContain(scenario === "timeout" ? "cleanup_run_retirement_unconfirmed" : scenario === "wrong_owner" ? "cleanup_lease_owner_mismatch" : "cleanup_no_launch_receipt_unconfirmed");
|
|
expect(outcome.completed).toBe(false); expect(remove).not.toHaveBeenCalled(); expect(post).not.toHaveBeenCalled(); expect(persisted).toBe(false);
|
|
if (scenario !== "timeout") expect(fullReads).toBeLessThan(3);
|
|
}
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it("discovers one explicit-only real-agent cell without ambient API-key requirements", () => {
|
|
const selected = selectRunnerExecutions(parseRunnerSelectors(["--id", ANTIGRAVITY_JOURNEY_EXECUTION, "--antigravity-journey-config", "/private/binding.json", "--max-automatic-retries", "0"]), runnerMatrix);
|
|
expect(selected).toHaveLength(1); expect(selected[0]!.suite.manualOnly).toBe(true); expect(selected[0]!.requiredCredentials).toEqual([]);
|
|
expect(selected[0]!.task.automaticRetryPolicy).toBe("single_attempt");
|
|
expect(selectRunnerExecutions(parseRunnerSelectors(["--all"]), runnerMatrix).some(row => row.id === ANTIGRAVITY_JOURNEY_EXECUTION)).toBe(false);
|
|
expect(() => selected[0]!.profile.buildAgent({} as never)).toThrow("must be created through the ordinary UI");
|
|
});
|
|
|
|
it.each(["https://paperclip.example", "http://localhost:3104", "http://192.168.0.1:3104"])("does not claim same-host native retirement on %s", baseURL => {
|
|
const value = fixture(); value.target.baseURL = baseURL;
|
|
expect(() => parseAntigravityJourneyConfig(value)).toThrow("invalid_antigravity_journey_configuration");
|
|
});
|
|
it("rejects widened turns, Linux inventory, live credential values and a signed-out setup", () => {
|
|
for (const edit of [
|
|
(v: any) => { v.limits.maxRuns = 3; }, (v: any) => { v.candidate.platform = "linux"; },
|
|
(v: any) => { v.savedConnection.accessToken = "must-never-be-a-config-value"; }, (v: any) => { v.browser.freshness = "signed-out"; },
|
|
(v: any) => { v.limits.turnTimeoutMs = "300000"; },
|
|
]) { const value = fixture(); edit(value); expect(() => parseAntigravityJourneyConfig(value)).toThrow("invalid_antigravity_journey_configuration"); }
|
|
});
|
|
it("binds owned-resource recovery to the exact company, environment and saved account", () => {
|
|
const config = parseAntigravityJourneyConfig(fixture());
|
|
expect(parseAntigravityOwnedResources(resources(), config).agentId).toBe(agentId);
|
|
for (const patch of [{ companyId: environmentId }, { environmentId: companyId }, { connectionId: agentId }, { origin: "https://other.example" }, { agentName: "Existing user agent" }])
|
|
expect(() => parseAntigravityOwnedResources({ ...resources(), ...patch }, config)).toThrow("invalid_antigravity_owned_resources");
|
|
});
|
|
it("accepts a company-allowed instance environment without inventing company ownership", () => {
|
|
const environment = { id: environmentId, name: "Local", driver: "local", status: "active" };
|
|
expect(selectAntigravityJourneyEnvironment([environment], environmentId)).toBe(environment);
|
|
expect(() => selectAntigravityJourneyEnvironment([{ ...environment, id: agentId }], environmentId)).toThrow("configured_execution_environment_unavailable");
|
|
expect(() => selectAntigravityJourneyEnvironment([{ ...environment, status: "disabled" }], environmentId)).toThrow("configured_execution_environment_unavailable");
|
|
});
|
|
it.each(["failed", "interrupted", "timed_out"])("retains an owned %s run when physical ownership was never captured", async status => {
|
|
const owned = resources(); owned.runIds = [runId];
|
|
const agent = { id: agentId, companyId, name: owned.agentName }, run = { id: runId, agentId, companyId, status };
|
|
const get = vi.fn(async (route: string) => route.includes("/agents") ? [agent] : route.includes("heartbeat-runs?") ? [run] : run);
|
|
const remove = vi.fn(), patch = vi.fn();
|
|
await expect(cleanupAntigravityJourney({ api: { get, patch, delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: owned, cleanup: { agent: false, runs: false, connectionPreserved: false }, persistResources: async () => {}, captureRun: async () => {} }))
|
|
.rejects.toThrow("cleanup_physical_owner_not_captured");
|
|
expect(remove).not.toHaveBeenCalled(); expect(patch).toHaveBeenCalledWith(`/api/agents/${agentId}`, { status: "paused" });
|
|
});
|
|
|
|
it("terminates the owned ready agent before purge and preserves the shared account", async () => {
|
|
const owned = resources(); owned.setupProbe = "retired";
|
|
const cleanup = { agent: false, runs: false, connectionPreserved: false };
|
|
let lifecycleState = "ready";
|
|
const agent = () => ({ id: agentId, companyId, name: owned.agentName, lifecycleState });
|
|
const get = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/agents")) return [agent()];
|
|
if (route.includes("heartbeat-runs?")) return [];
|
|
if (route === `/api/agents/${agentId}`) return agent();
|
|
if (route.endsWith("/ai-connections")) return { connections: [{ id: connectionId, provider: "google", method: "subscription", ownerUserId: "synthetic-user", isDefault: true, status: "connected" }] };
|
|
throw new Error("Unexpected cleanup read");
|
|
});
|
|
const post = vi.fn(async (route: string) => { expect(route).toBe(`/api/agents/${agentId}/terminate`); lifecycleState = "terminated"; return agent(); });
|
|
const remove = vi.fn(async () => { expect(lifecycleState).toBe("terminated"); });
|
|
await cleanupAntigravityJourney({ api: { get, post, patch: async () => { lifecycleState = "paused"; }, delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: owned, cleanup, persistResources: async () => {}, captureRun: async () => {} });
|
|
expect(remove).toHaveBeenCalledWith(`/api/agents/${agentId}`);
|
|
expect(cleanup).toEqual({ agent: true, runs: true, connectionPreserved: true });
|
|
});
|
|
it.each([false, true])("captures a cleanup-discovered run's settled usage before purge (storage failure: %s)", async storageFailure => {
|
|
const owned = resources(); owned.setupProbe = "retired";
|
|
owned.processes[runId] = { captured: true, identityChanged: false, journal: [
|
|
{ pid: 9000001, parent: 1, start: "2020-01-01T00:00:00Z" }, { pid: 9000002, parent: 9000001, start: "2020-01-01T00:00:00Z" },
|
|
] };
|
|
let status = "running", lifecycleState = "ready";
|
|
const agent = () => ({ id: agentId, companyId, name: owned.agentName, lifecycleState });
|
|
const run = () => ({ id: runId, agentId, companyId, status, costAccountingPending: status === "running",
|
|
usageJson: { inputTokens: 100, outputTokens: 20, costUsd: status === "running" ? 0.01 : 0.25 },
|
|
contextSnapshot: { paperclipEnvironment: { leaseId: "lease" } } });
|
|
const snapshots: any[] = [];
|
|
const captureRun = vi.fn(async row => {
|
|
if (storageFailure && row.status === "cancelled") throw new Error("fixture accounting storage unavailable");
|
|
snapshots.push(structuredClone(row));
|
|
});
|
|
const get = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/agents")) return [agent()];
|
|
if (route.includes("heartbeat-runs?")) return [run()];
|
|
if (route === `/api/heartbeat-runs/${runId}`) return run();
|
|
if (route === "/api/environment-leases/lease") return { companyId, heartbeatRunId: runId, environmentId, status: "released" };
|
|
if (route === `/api/agents/${agentId}`) return agent();
|
|
if (route.endsWith("/ai-connections")) return { connections: [{ id: connectionId, provider: "google", method: "subscription", ownerUserId: "synthetic-user", isDefault: true, status: "connected" }] };
|
|
throw new Error("Unexpected cleanup read");
|
|
});
|
|
const post = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/cancel")) status = "cancelled";
|
|
else if (route.endsWith("/terminate")) lifecycleState = "terminated";
|
|
else throw new Error("Unexpected cleanup mutation");
|
|
});
|
|
const remove = vi.fn(async () => { expect(snapshots.at(-1)).toMatchObject({ status: "cancelled", costAccountingPending: false, usageJson: { costUsd: 0.25 } }); });
|
|
const cleanup = cleanupAntigravityJourney({ api: { get, post, patch: async () => {}, delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: owned, cleanup: { agent: false, runs: false, connectionPreserved: false }, persistResources: async () => {}, captureRun });
|
|
if (storageFailure) { await expect(cleanup).rejects.toThrow("fixture accounting storage unavailable"); expect(remove).not.toHaveBeenCalled(); }
|
|
else { await cleanup; expect(remove).toHaveBeenCalledOnce(); }
|
|
expect(owned.runIds).toEqual([runId]);
|
|
expect(snapshots[0]).toMatchObject({ status: "running", usageJson: { costUsd: 0.01 } });
|
|
});
|
|
it("does not erase a failed native setup Test's unconfirmed cleanup with an empty task-run list", async () => {
|
|
const owned = resources(); owned.setupProbe = "retirement_unconfirmed";
|
|
const agent = { id: agentId, companyId, name: owned.agentName }, remove = vi.fn();
|
|
const get = vi.fn(async (route: string) => route.endsWith("/agents") ? [agent] : []);
|
|
await expect(cleanupAntigravityJourney({ api: { get, patch: vi.fn(), delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: owned, cleanup: { agent: false, runs: true, connectionPreserved: true }, persistResources: async () => {}, captureRun: async () => {} }))
|
|
.rejects.toThrow("setup_probe_retirement_unconfirmed");
|
|
expect(remove).not.toHaveBeenCalled();
|
|
});
|
|
it.each(["interrupted", "timed_out"].flatMap(status => [false, true].map(pending => ({ status, pending }))))("retires a $status run only after its accounting settles (pending: $pending)", async ({ status, pending }) => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const owned = resources(); owned.setupProbe = "retired"; owned.runIds = [runId];
|
|
owned.processes[runId] = { captured: true, identityChanged: false, journal: [
|
|
{ pid: 9000001, parent: 1, start: "2020-01-01T00:00:00Z" }, { pid: 9000002, parent: 9000001, start: "2020-01-01T00:00:00Z" },
|
|
] };
|
|
const agent = { id: agentId, companyId, name: owned.agentName, status: "active" }, run = { id: runId, agentId, companyId, status,
|
|
costAccountingPending: pending, contextSnapshot: { paperclipEnvironment: { leaseId: "lease" } } };
|
|
const get = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/agents")) return [agent];
|
|
if (route === `/api/agents/${agentId}`) return agent;
|
|
if (route.includes("heartbeat-runs?")) return [run];
|
|
if (route === `/api/heartbeat-runs/${runId}`) return run;
|
|
if (route === "/api/environment-leases/lease") return { companyId, heartbeatRunId: runId, environmentId, status: "released" };
|
|
if (route.endsWith("/ai-connections")) return { connections: [{ id: connectionId, provider: "google", method: "subscription", ownerUserId: "synthetic-user", isDefault: true, status: "connected" }] };
|
|
throw new Error("Unexpected cleanup read");
|
|
});
|
|
const post = vi.fn(), remove = vi.fn(), cleanup = { agent: false, runs: false, connectionPreserved: false };
|
|
const started = Date.now(); let error: unknown, finishedAt: number | undefined;
|
|
const operation = cleanupAntigravityJourney({ api: { get, post, patch: vi.fn(async () => { agent.status = "paused"; }), delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig({ ...fixture(), retainAgent: true }), resources: owned, cleanup, captureRun: async () => {}, persistResources: async () => {} })
|
|
.then(() => { finishedAt = Date.now(); }, caught => { error = caught; finishedAt = Date.now(); });
|
|
await vi.advanceTimersByTimeAsync(60_500); await operation;
|
|
if (pending) { expect(String(error)).toContain("cleanup_run_retirement_unconfirmed"); expect(cleanup.runs).toBe(false); }
|
|
else { expect(error).toBeUndefined(); expect(finishedAt! - started).toBeLessThan(1_000); expect(cleanup).toEqual({ agent: true, runs: true, connectionPreserved: true }); expect(owned.retained).toBe(true); }
|
|
expect(post).not.toHaveBeenCalled(); expect(remove).not.toHaveBeenCalled();
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
it("does not claim retained-agent cleanup when the pause PATCH was ignored", async () => {
|
|
const owned = resources(); owned.setupProbe = "retired";
|
|
const agent = { id: agentId, companyId, name: owned.agentName, status: "active" };
|
|
const get = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/agents")) return [agent];
|
|
if (route === `/api/agents/${agentId}`) return agent;
|
|
if (route.includes("heartbeat-runs?")) return [];
|
|
if (route.endsWith("/ai-connections")) return { connections: [{ id: connectionId, provider: "google", method: "subscription", ownerUserId: "synthetic-user", isDefault: true, status: "connected" }] };
|
|
throw new Error("Unexpected cleanup read");
|
|
});
|
|
const post = vi.fn(), remove = vi.fn(), patch = vi.fn(), cleanup = { agent: false, runs: false, connectionPreserved: false };
|
|
await expect(cleanupAntigravityJourney({ api: { get, post, patch, delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig({ ...fixture(), retainAgent: true }), resources: owned, cleanup, captureRun: async () => {}, persistResources: async () => {} }))
|
|
.rejects.toThrow("cleanup_retained_agent_pause_unconfirmed");
|
|
expect(patch).toHaveBeenCalledWith(`/api/agents/${agentId}`, { status: "paused" });
|
|
expect(cleanup.agent).toBe(false); expect(post).not.toHaveBeenCalled(); expect(remove).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("stops on combined reported and estimated known cost while retaining unknown subscription prices", () => {
|
|
const usage = (id: string, usageJson: Record<string, unknown>) => ({ id, usageJson });
|
|
expect(antigravityKnownCostObservation([
|
|
usage("first", { inputTokens: 100, outputTokens: 20, costUsd: 0.6 }),
|
|
usage("second", { inputTokens: 100, outputTokens: 20, costUsd: 0.41, costStatus: "estimated" }),
|
|
], 100)).toMatchObject({ knownCostUsd: 1.01, exceeded: true, hardCurrencyCap: false });
|
|
expect(antigravityKnownCostObservation([
|
|
usage("first", { inputTokens: 100, outputTokens: 20, costUsd: 0.1 }),
|
|
usage("second", { inputTokens: 100, outputTokens: 20, costUsd: 0.2, costStatus: "estimated" }),
|
|
], 30).exceeded).toBe(false);
|
|
expect(antigravityKnownCostObservation([
|
|
usage("first", { inputTokens: 100, outputTokens: 20, costUsd: 0, costStatus: "unpriced", billingType: "subscription" }),
|
|
], 1)).toMatchObject({ exceeded: false, costStatus: "unpriced", priceCoverageComplete: false, hardCurrencyCap: false });
|
|
});
|
|
|
|
function deferredOwnerCleanup(discovery: "initial" | "poll" | "transition" = "initial") {
|
|
const owned = resources(); owned.setupProbe = "retired"; owned.issueId = "66666666-6666-4666-8666-666666666666";
|
|
owned.processes[runId] = { captured: true, identityChanged: false, journal: [
|
|
{ pid: 9000001, parent: 1, start: "2020-01-01T00:00:00Z" }, { pid: 9000002, parent: 9000001, start: "2020-01-01T00:00:00Z" },
|
|
] };
|
|
const agent = { id: agentId, companyId, name: owned.agentName, status: "active" };
|
|
const run: Record<string, any> = { id: runId, companyId, agentId, issueId: owned.issueId, nativeIssueId: owned.issueId,
|
|
status: "failed", runtimeMode: "native", nativePhase: "retryable_failure", costAccountingPending: false,
|
|
contextSnapshot: { paperclipEnvironment: { leaseId: "owned-lease" } }, resultJson: {} };
|
|
const lease = { id: "owned-lease", companyId, heartbeatRunId: runId, environmentId, status: "released" };
|
|
const state = { listReads: 0, fullReads: 0, stopAtListReads: [] as number[], fullPatch: {} as Record<string, unknown>,
|
|
onStop: async (_body: Record<string, any>) => {} };
|
|
const acknowledge = (body: Record<string, any>, patch: Record<string, unknown> = {}) => {
|
|
run.status = "cancelled"; run.nativePhase = "terminal_failure";
|
|
run.resultJson = { startupCancellation: { cancellationRequestId: body.cancellationRequestId }, nativeCancellation: {
|
|
schema: "paperclip.native-cancellation.v1", intentId: `native-cancellation:${body.cancellationRequestId}`,
|
|
companyId, runId, issueId: owned.issueId, scope: "run", reasonCode: "cancellation_run_only", dispatched: false,
|
|
dispatchState: "acknowledged", intentAuditId: "owned-intent-audit", acknowledgementAuditId: "owned-ack-audit", ...patch,
|
|
} };
|
|
};
|
|
state.onStop = async body => { if (run.runtimeMode === "native") acknowledge(body); else { run.status = "cancelled"; run.resultJson = {}; } };
|
|
const get = vi.fn(async (route: string) => {
|
|
if (route.endsWith("/agents")) return [structuredClone(agent)];
|
|
if (route === `/api/agents/${agentId}`) return structuredClone(agent);
|
|
if (route.includes("heartbeat-runs?")) {
|
|
state.listReads++;
|
|
if (discovery === "poll" && state.listReads === 1) return [];
|
|
return [{ ...structuredClone(run), ...(discovery === "transition" ? { nativePhase: "committed" } : {}) }];
|
|
}
|
|
if (route === `/api/heartbeat-runs/${runId}`) {
|
|
state.fullReads++;
|
|
return { ...structuredClone(run), ...(discovery === "transition" && state.fullReads === 1 ? { nativePhase: "committed" } : {}), ...state.fullPatch };
|
|
}
|
|
if (route === "/api/environment-leases/owned-lease") return structuredClone(lease);
|
|
if (route === `/api/issues/${owned.issueId}`) return { id: owned.issueId, companyId, assigneeAgentId: agentId, status: "done" };
|
|
if (route.endsWith("/ai-connections")) return { connections: [{ id: connectionId, provider: "google", method: "subscription", ownerUserId: "synthetic-user", isDefault: true, status: "connected" }] };
|
|
throw new Error(`Unexpected deferred cleanup read ${route}`);
|
|
});
|
|
const post = vi.fn(async (route: string, body: Record<string, any> = {}) => {
|
|
expect(route).toBe(`/api/heartbeat-runs/${runId}/cancel`);
|
|
expect(owned.runIds).toContain(runId);
|
|
if (run.runtimeMode === "native") expect(body.cancellationRequestId).toMatch(/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/iu);
|
|
else expect(body.cancellationRequestId).toBeUndefined();
|
|
state.stopAtListReads.push(state.listReads); await state.onStop(body); return structuredClone(run);
|
|
});
|
|
const remove = vi.fn(), cleanup = { agent: false, runs: false, connectionPreserved: false };
|
|
const start = () => cleanupAntigravityJourney({ api: { get, post, patch: vi.fn(async () => { agent.status = "paused"; }), delete: remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig({ ...fixture(), retainAgent: true }), resources: owned, cleanup, captureRun: async () => {}, persistResources: async () => {} });
|
|
return { owned, run, lease, state, post, remove, cleanup, acknowledge, start };
|
|
}
|
|
|
|
it.each(["initial", "poll"] as const)("owned deferred cleanup stops a failed native retry at %s discovery and waits for acknowledgement and lease retirement", async discovery => {
|
|
vi.useFakeTimers();
|
|
const f = deferredOwnerCleanup(discovery); f.lease.status = "active";
|
|
let release!: () => void; const stop = new Promise<void>(resolve => { release = resolve; });
|
|
f.state.onStop = async body => { await stop; f.acknowledge(body, { dispatched: discovery === "initial" }); };
|
|
const outcome: { done: boolean; error?: unknown } = { done: false };
|
|
const operation = f.start().then(() => { outcome.done = true; }, error => { outcome.error = error; });
|
|
try {
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(f.post).toHaveBeenCalledOnce(); expect(f.state.stopAtListReads).toEqual([discovery === "initial" ? 1 : 2]);
|
|
expect(outcome.done).toBe(false); expect(f.cleanup.runs).toBe(false); expect(f.cleanup.agent).toBe(false);
|
|
release(); await vi.advanceTimersByTimeAsync(0);
|
|
expect(outcome.done).toBe(false); expect(f.cleanup.runs).toBe(false);
|
|
f.lease.status = "released"; await vi.advanceTimersByTimeAsync(300); await operation;
|
|
expect(outcome.error).toBeUndefined(); expect(outcome.done).toBe(true);
|
|
expect(f.cleanup).toEqual({ agent: true, runs: true, connectionPreserved: true }); expect(f.owned.retained).toBe(true); expect(f.remove).not.toHaveBeenCalled();
|
|
} finally { release(); f.lease.status = "released"; await vi.advanceTimersByTimeAsync(61_000); await operation; vi.useRealTimers(); }
|
|
});
|
|
|
|
it.each(["initial", "poll"] as const)("owned deferred cleanup requires Stop despite already retired processes, released lease and settled accounting (%s)", async discovery => {
|
|
const f = deferredOwnerCleanup(discovery); await f.start();
|
|
expect(f.post).toHaveBeenCalledOnce(); expect(f.state.stopAtListReads).toEqual([discovery === "initial" ? 1 : 2]);
|
|
expect(f.cleanup.runs).toBe(true);
|
|
});
|
|
|
|
it("owned deferred cleanup observes a known run's transition into retry ownership", async () => {
|
|
const f = deferredOwnerCleanup("transition"); await f.start();
|
|
expect(f.state.fullReads).toBeGreaterThanOrEqual(2); expect(f.post).toHaveBeenCalledOnce(); expect(f.cleanup.runs).toBe(true);
|
|
});
|
|
|
|
it.each(["initial", "poll"] as const)("owned deferred cleanup stops a scheduled retry at %s discovery", async discovery => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const f = deferredOwnerCleanup(discovery); Object.assign(f.run, { status: "scheduled_retry", runtimeMode: "legacy", nativePhase: null });
|
|
let error: unknown; const operation = f.start().catch(caught => { error = caught; });
|
|
await vi.advanceTimersByTimeAsync(61_000); await operation;
|
|
expect(error).toBeUndefined(); expect(f.post).toHaveBeenCalledOnce(); expect(f.state.stopAtListReads).toEqual([discovery === "initial" ? 1 : 2]); expect(f.cleanup.runs).toBe(true);
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it.each([
|
|
["failed", "native", "terminal_failure"], ["failed", "legacy", "retryable_failure"],
|
|
["succeeded", "native", "retryable_failure"], ["cancelled", "native", "retryable_failure"], ["timed_out", "native", "retryable_failure"],
|
|
])("owned deferred cleanup does not Stop ordinary terminal %s/%s/%s", async (status, runtimeMode, nativePhase) => {
|
|
const f = deferredOwnerCleanup(); Object.assign(f.run, { status, runtimeMode, nativePhase });
|
|
await f.start(); expect(f.post).not.toHaveBeenCalled(); expect(f.cleanup.runs).toBe(true);
|
|
});
|
|
|
|
it.each(["id", "companyId", "agentId", "issueId"])("owned deferred cleanup rejects foreign hydrated %s before Stop", async field => {
|
|
const f = deferredOwnerCleanup(); f.state.fullPatch[field] = "ffffffff-ffff-4fff-8fff-ffffffffffff";
|
|
await expect(f.start()).rejects.toThrow(/owner_mismatch/u); expect(f.post).not.toHaveBeenCalled(); expect(f.cleanup.agent).toBe(false);
|
|
});
|
|
|
|
it.each(["pending", "foreign_run", "foreign_company", "foreign_issue", "missing_audit", "same_audit", "stale_intent", "accounting_pending", "stop_error"])("owned deferred cleanup keeps failure when Stop proof is %s", async scenario => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const f = deferredOwnerCleanup();
|
|
f.state.onStop = async body => {
|
|
if (scenario === "stop_error") throw new Error("synthetic Stop rejected");
|
|
f.acknowledge(body, scenario === "pending" ? { dispatchState: "pending" }
|
|
: scenario === "foreign_run" ? { runId: agentId } : scenario === "foreign_company" ? { companyId: agentId }
|
|
: scenario === "foreign_issue" ? { issueId: agentId } : scenario === "missing_audit" ? { acknowledgementAuditId: "" }
|
|
: scenario === "same_audit" ? { acknowledgementAuditId: "owned-intent-audit" }
|
|
: scenario === "stale_intent" ? { intentId: "native-cancellation:ffffffff-ffff-4fff-8fff-ffffffffffff" } : {});
|
|
if (scenario === "accounting_pending") f.run.costAccountingPending = true;
|
|
};
|
|
let error: unknown; const operation = f.start().catch(caught => { error = caught; });
|
|
await vi.advanceTimersByTimeAsync(61_000); await operation;
|
|
expect(f.post).toHaveBeenCalledOnce(); expect(error).toBeDefined(); expect(f.cleanup.agent).toBe(false); expect(f.cleanup.runs).toBe(false); expect(f.remove).not.toHaveBeenCalled();
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it("owned deferred cleanup recovers a pending native Stop with its original request identity", async () => {
|
|
const f = deferredOwnerCleanup(), requestId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
|
f.acknowledge({ cancellationRequestId: requestId }, { dispatchState: "pending", acknowledgementAuditId: null });
|
|
f.run.status = "failed";
|
|
await f.start(); expect(f.post).toHaveBeenCalledOnce(); expect(f.post.mock.calls[0]![1]).toEqual({ cancellationRequestId: requestId });
|
|
expect(f.cleanup.runs).toBe(true);
|
|
});
|
|
|
|
it("owned deferred cleanup preserves cancelled unknown-accounting debt without a new Stop", async () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const f = deferredOwnerCleanup(); Object.assign(f.run, { status: "cancelled", costAccountingPending: true });
|
|
let error: unknown; const operation = f.start().catch(caught => { error = caught; });
|
|
await vi.advanceTimersByTimeAsync(61_000); await operation;
|
|
expect(error).toBeDefined(); expect(f.post).not.toHaveBeenCalled(); expect(f.cleanup.runs).toBe(false); expect(f.cleanup.agent).toBe(false);
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it("owned deferred cleanup retains a terminal native reservation without inventing Stop authority", async () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const f = deferredOwnerCleanup(); f.run.nativePhase = "terminal_failure";
|
|
f.run.resultJson = { startupCancellation: { cancellationRequestId: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa" } };
|
|
let error: unknown; const operation = f.start().catch(caught => { error = caught; });
|
|
await vi.advanceTimersByTimeAsync(61_000); await operation;
|
|
expect(error).toBeDefined(); expect(f.post).not.toHaveBeenCalled(); expect(f.cleanup.runs).toBe(false); expect(f.cleanup.agent).toBe(false); expect(f.remove).not.toHaveBeenCalled();
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it("owned deferred cleanup reuses a reservation-only UUID while its native retry is eligible", async () => {
|
|
const f = deferredOwnerCleanup(), requestId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
|
f.run.resultJson = { startupCancellation: { cancellationRequestId: requestId } };
|
|
await f.start(); expect(f.post).toHaveBeenCalledOnce(); expect(f.post.mock.calls[0]![1]).toEqual({ cancellationRequestId: requestId });
|
|
expect(f.cleanup.runs).toBe(true);
|
|
});
|
|
|
|
function interruptedAgentCreation() {
|
|
const owned = resources(); delete owned.agentId;
|
|
const witness = { setupId: agentId, companyId, agentName: owned.agentName, provider: "antigravity", creatorUserId: "synthetic-user", origin: owned.origin };
|
|
(owned as any).agentCreationRequest = witness;
|
|
let lifecycleState = "draft", persisted = false;
|
|
const record = () => ({ id: agentId, companyId, name: owned.agentName, createdByUserId: "synthetic-user", lifecycleState });
|
|
const get = vi.fn(async (route: string): Promise<Record<string, any> | Record<string, any>[]> => {
|
|
if (route === `/api/companies/${companyId}/agents`) return [record()];
|
|
if (route === `/api/agents/${agentId}?companyId=${companyId}`) return record();
|
|
if (route.includes("heartbeat-runs?")) return [];
|
|
if (route === `/api/agents/${agentId}`) return record();
|
|
if (route.endsWith("/ai-connections")) return { connections: [{ id: connectionId, provider: "google", method: "subscription", ownerUserId: "synthetic-user", isDefault: true, status: "connected" }] };
|
|
throw new Error(`Unexpected creation recovery read ${route}`);
|
|
});
|
|
const patch = vi.fn(async (route: string) => {
|
|
expect(route).toBe(`/api/agents/${agentId}`); expect(persisted).toBe(true); lifecycleState = "paused";
|
|
});
|
|
const post = vi.fn(async (route: string) => { expect(route).toBe(`/api/agents/${agentId}/terminate`); lifecycleState = "terminated"; });
|
|
const remove = vi.fn();
|
|
const persistResources = vi.fn(async () => { expect(owned.agentId).toBe(agentId); persisted = true; });
|
|
return { owned, witness, record, get, patch, post, remove, persistResources };
|
|
}
|
|
it("parses the minimal pre-forward creation witness without storing the UI request body", () => {
|
|
const f = interruptedAgentCreation();
|
|
expect(parseAntigravityOwnedResources(f.owned, parseAntigravityJourneyConfig(fixture()))).toMatchObject({ agentCreationRequest: f.witness });
|
|
});
|
|
it("recovers a committed UI draft by exact setupId before any pause or purge", async () => {
|
|
const f = interruptedAgentCreation(), cleanup = { agent: false, runs: false, connectionPreserved: false };
|
|
await cleanupAntigravityJourney({ api: { get: f.get, patch: f.patch, post: f.post, delete: f.remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: f.owned, cleanup, persistResources: f.persistResources, captureRun: async () => {} });
|
|
expect(f.owned.agentId).toBe(agentId);
|
|
expect(f.get).toHaveBeenCalledWith(`/api/agents/${agentId}?companyId=${companyId}`);
|
|
expect(f.persistResources).toHaveBeenCalled(); expect(f.remove).toHaveBeenCalledWith(`/api/agents/${agentId}`);
|
|
expect(cleanup).toEqual({ agent: true, runs: true, connectionPreserved: true });
|
|
});
|
|
it.each([
|
|
["foreign company", { companyId: environmentId }], ["different name", { agentName: "Antigravity QA fedcba987654" }],
|
|
["wrong provider", { provider: "codex" }], ["noncanonical setup UUID", { setupId: "NOT-A-UUID" }],
|
|
["uppercase setup UUID", { setupId: "AAAAAAAA-AAAA-4AAA-8AAA-AAAAAAAAAAAA" }],
|
|
["foreign origin", { origin: "http://127.0.0.1:9999" }], ["foreign creator", { creatorUserId: "another-user" }], ["missing creator", { creatorUserId: undefined }],
|
|
["extra private payload", { rawBody: { runtimeConfig: { env: { PRIVATE_VALUE: "never-store" } } } }],
|
|
["mismatched known agent", { setupId: environmentId }],
|
|
])("rejects a %s creation witness before recovery", (_label, patch) => {
|
|
const f = interruptedAgentCreation();
|
|
(f.owned as any).agentCreationRequest = { ...f.witness, ...patch };
|
|
if (_label === "mismatched known agent") f.owned.agentId = agentId;
|
|
expect(() => parseAntigravityOwnedResources(f.owned, parseAntigravityJourneyConfig(fixture()))).toThrow("invalid_antigravity_owned_resources");
|
|
});
|
|
it.each(["company", "name", "id", "creator", "missing-creator", "same-name-only", "duplicate-name"])("does not reclaim a creation draft with mismatched %s", async field => {
|
|
const f = interruptedAgentCreation(), originalGet = f.get.getMockImplementation()!;
|
|
f.get.mockImplementation(async route => {
|
|
if (route === `/api/companies/${companyId}/agents` && field === "same-name-only") return [{ ...f.record(), id: environmentId }];
|
|
if (route === `/api/companies/${companyId}/agents` && field === "duplicate-name") return [f.record(), { ...f.record(), id: environmentId }];
|
|
if (route === `/api/agents/${agentId}?companyId=${companyId}`) return { ...f.record(),
|
|
...(field === "company" ? { companyId: environmentId } : field === "name" ? { name: "Existing user agent" } : field === "id" ? { id: environmentId } : field === "creator" ? { createdByUserId: "another-user" } : field === "missing-creator" ? { createdByUserId: undefined } : {}) };
|
|
return originalGet(route);
|
|
});
|
|
await expect(cleanupAntigravityJourney({ api: { get: f.get, patch: f.patch, post: f.post, delete: f.remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: f.owned, cleanup: { agent: false, runs: false, connectionPreserved: false },
|
|
persistResources: f.persistResources, captureRun: async () => {} })).rejects.toThrow(/cleanup_.*(?:mismatch|unconfirmed)/u);
|
|
expect(f.patch).not.toHaveBeenCalled(); expect(f.post).not.toHaveBeenCalled(); expect(f.remove).not.toHaveBeenCalled();
|
|
});
|
|
it("keeps a witnessed404 unconfirmed rather than treating an in-flight creation as absent", async () => {
|
|
const f = interruptedAgentCreation(), cleanup = { agent: false, runs: false, connectionPreserved: false };
|
|
f.get.mockImplementation(async route => {
|
|
if (route === `/api/companies/${companyId}/agents`) return [];
|
|
if (route === `/api/agents/${agentId}?companyId=${companyId}`) throw new ConnectionFailure("api_status_404");
|
|
throw new Error(`Unexpected witnessed404 read ${route}`);
|
|
});
|
|
await expect(cleanupAntigravityJourney({ api: { get: f.get, patch: f.patch, post: f.post, delete: f.remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: f.owned, cleanup, persistResources: f.persistResources, captureRun: async () => {} }))
|
|
.rejects.toThrow(/cleanup_agent_creation_unconfirmed/u);
|
|
expect(f.patch).not.toHaveBeenCalled(); expect(f.post).not.toHaveBeenCalled(); expect(f.remove).not.toHaveBeenCalled();
|
|
expect(cleanup.agent).toBe(false); expect(cleanup.runs).toBe(false);
|
|
});
|
|
it("does not mutate a recovered draft if its exact identity cannot be persisted", async () => {
|
|
const f = interruptedAgentCreation(); f.persistResources.mockRejectedValue(new Error("owned creation receipt write failed"));
|
|
await expect(cleanupAntigravityJourney({ api: { get: f.get, patch: f.patch, post: f.post, delete: f.remove } as unknown as ConnectionApi,
|
|
config: parseAntigravityJourneyConfig(fixture()), resources: f.owned, cleanup: { agent: false, runs: false, connectionPreserved: false },
|
|
persistResources: f.persistResources, captureRun: async () => {} })).rejects.toThrow("owned creation receipt write failed");
|
|
expect(f.patch).not.toHaveBeenCalled(); expect(f.post).not.toHaveBeenCalled(); expect(f.remove).not.toHaveBeenCalled();
|
|
});
|