Files
PaperClipAI/server/src/services/native-runtime/antigravity-runner-credentials.test.ts
T
DottaandPaperclip 2b14ec03a4 fix(ai-connections): remove account execution locks (#18778)
## Thinking Path

> - Paperclip manages AI agents and the accounts that supply their model
credentials.
> - Several runs can use the same AI connection at the same time.
> - Runtime preparation treated unfinished credential cleanup as
exclusive account ownership.
> - Manual account tests also held a company credential mutex for the
entire provider test.
> - A failed cleanup could therefore cancel fresh runs before the
provider started and keep them in automatic retries.
> - This pull request removes that execution gate and those read and
probe locks.
> - Each invocation keeps its own credential home. Credential updates
still use the existing freshness and authorization checks.

## Linked Issues or Issue Description

**What happened?**

Fresh runs stopped with `ai_connection_busy` and the message “The
selected AI account is still completing credential cleanup. Retry
shortly.” Pending cleanup from an earlier invocation blocked every new
invocation of that account. Concurrent manual account tests also waited
on a company mutex.

**Expected behavior**

AI connections support concurrent runs and account tests. Cleanup for
one private runtime must not prevent another private runtime from using
the same account.

**Steps to reproduce**

1. Prepare a managed OpenAI account and retain an earlier runtime whose
cleanup has not finished.
2. Prepare another run with that account. The old implementation rejects
it before provider work starts.
3. Alternatively, keep one manual account test open and start a second
test with the same account. The old implementation prevents the second
provider callback from starting.

**Paperclip version or commit**

Reproduced with two PostgreSQL regressions on
`17e8bd773543d62ab3f444c55255acfd82575efd`. Both fail before the fix and
pass after it.

**Deployment mode**

The reported deployment used authenticated Cloud. Local reproduction
uses disposable PostgreSQL and fixture credentials. No live provider
credentials are required for these regressions.

Related search found #15952, which concerns account removal and provider
application archival. It does not remove this runtime cleanup gate. This
fix does not introduce the account concurrency cap requested in #14564.
ROADMAP.md was checked; this change restores concurrent account use.

## What Changed

- Remove the account-wide cleanup assertion and cleanup mutex from run
admission.
- Read ordinary subscription credentials without acquiring the company
file mutex or grant and secret write locks. Only an actual durable
replacement recovery enters the existing write transaction.
- Remove the mutex held across manual account tests.
- Retry retained cleanup without making new runtime preparation wait for
it. A home-specific cleanup check considers only that exact private
home.
- Keep cleanup attempts for one runtime idempotent. Keep physical
retirement, credential freshness, reconnect, revocation and company
checks.
- Update concurrency, refresh and cleanup regressions, and the
implementation contract.

## Verification

- Before/after: the pending-cleanup and simultaneous-probe regressions
both fail on the original code and pass with the fix.
- Affected suites: 186 tests passed across `ai-connections.test.ts`,
`antigravity-ai-connections.integration.test.ts`, and
`retained-google-credential-cleanup.test.ts`. They use real PostgreSQL,
encrypted fixture credentials and controlled provider callbacks.
- `pnpm -r typecheck` passed.
- `node scripts/check-maps.mjs` and `git diff --check` passed.
- `pnpm build` passed on the rebased branch. The server typecheck and
production server build both passed again on final commit
`03bd3973bdf85108bbd82355abdb217f0e1c83bd` after the runtime-home
ownership changes.
- Caller coverage: all 166 cases pass across native credential handoff,
managed Google subscription writeback, model discovery, heartbeat
configuration and hired-agent execution. The full hiring file passed all
79 cases after its admission assertion was corrected. Together with the
affected suites, 352 targeted cases pass.
- The final home-binding check passed all 188 cases across the account,
managed Google writeback, model discovery and retained-cleanup suites.
- Review follow-up: two real-PostgreSQL cases pass for quota refresh and
concurrent Google account tests. The quota case observes a credential
row update in progress, verifies that the runtime takes no account file
mutex, and confirms that the private home receives the committed
replacement token. The Google case completes a second provider callback
while the first callback remains open in a separate private home.
- Greptile rated the runtime change 4/5 and found only misplaced map
patch notes. Those additions were removed. The implementation
documentation remains in `doc/connections/AI-CONNECTIONS.md` and the
implementation contract.
- `pnpm test:run` was attempted. Chat partition 1 passed 351 expanded
cases. Partition 2 passed 324 cases and failed one Teams file-receipt
API case at its 15-second deadline: `refuses generic resolution with
stale version transfer preconditions without changing receipts`. That
case exercises publication resolution, not AI account execution. Its
isolated rerun passed. The remaining local partitions did not run after
that failure. This is not a claim that the full local suite passed.
- Wider caller checks initially identified old serialization
expectations and two cleanup retry cases. Updated assertions now require
concurrent account use. The partial-removal correction lets a retained
home finish removal after a durable credential handoff. All affected
caller cases pass, including the full hiring file.
- Live provider concurrency and deployment recovery have not been
qualified. This PR does not claim that the reported Cloud deployment
contains the fix.

## Risks

- A new invocation can use the currently saved credential while another
runtime still owns a refreshed copy. The existing writeback checks
decide which credential can replace the saved value.
- Actual credential updates and durable replacement recovery retain
atomic storage transactions. This change removes execution
serialization, not the integrity checks on stored credentials.
- Unfinished cleanup retains its private home and its existing idle
hold. This change does not certify a previous provider process as
stopped.
- There is no database migration, credential format change, permission
expansion or ambient-account fallback.

## Model Used

- OpenAI GPT-6 through Codex desktop, with source editing, shell
execution, PostgreSQL fixture tests and read-only browser diagnosis. The
session does not expose the exact deployment ID or context-window size.

## 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
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-03 07:56:37 -06:00

354 lines
26 KiB
TypeScript

import { access, chmod, link, mkdir, mkdtemp, readFile, readdir, realpath, rm, stat, symlink, writeFile } from "node:fs/promises";
import { constants } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { antigravityCredentialTurn, bindAntigravityCredentialTurn, bindAntigravityTransportRetirement, prepareAntigravityRunnerCredentials } from "./antigravity-runner-credentials.js";
import { completeManagedNativeCredentialTurn } from "./managed-native-credentials.js";
import type { NativeSession, createRunnerdCodexTransport } from "../../vendor/paperclip-runner/index.js";
import { bindRetainedGoogleCredentialHome } from "../retained-google-credential-cleanup.js";
const copyFault = vi.hoisted(() => ({ phase: "", destination: "", source: "", collisionPath: "", failedRemoval: "", removals: [] as string[], hits: 0 }));
vi.mock("./runner-api-files.js", async importOriginal => {
const actual = await importOriginal<typeof import("./runner-api-files.js")>();
return { ...actual, openRunnerWorkspaceFile: async (...args: Parameters<typeof actual.openRunnerWorkspaceFile>) => {
const path = args[0];
if (copyFault.phase === "stage-create" && copyFault.hits === 0 && path.includes("/.acp_token.copyback-") && (args[1] & constants.O_EXCL)) {
copyFault.hits++; copyFault.collisionPath = path;
await writeFile(path, "fixture-unowned-stage", { mode: 0o600, flag: "wx" });
}
const handle = await actual.openRunnerWorkspaceFile(...args);
const staged = path.includes("/.acp_token.copyback-");
const writable = staged || path === copyFault.destination;
return new Proxy(handle, { get(target, key) {
const value = Reflect.get(target, key, target);
if (typeof value !== "function") return value;
return async (...callArgs: unknown[]) => {
if (writable && copyFault.hits === 0 && copyFault.phase === "partial-write" && key === "write") {
copyFault.hits++;
await Reflect.apply(value, target, [callArgs[0], callArgs[1], Math.floor(Number(callArgs[2]) / 2), callArgs[3]]);
throw new Error("fixture partial-write");
}
if (staged && copyFault.hits === 0 && copyFault.phase === "stage-stat" && key === "stat") {
copyFault.hits++; throw new Error("fixture stage-stat");
}
if (writable && copyFault.hits === 0 && copyFault.phase === "stage-sync" && key === "sync") {
copyFault.hits++; throw new Error("fixture stage-sync");
}
if (writable && copyFault.hits === 0 && copyFault.phase === "stage-close" && key === "close") {
copyFault.hits++; await Reflect.apply(value, target, callArgs); throw new Error("fixture stage-close");
}
if (path === copyFault.destination.slice(0, copyFault.destination.lastIndexOf("/"))
&& copyFault.hits === 0 && copyFault.phase === "directory-sync" && key === "sync") {
copyFault.hits++; throw new Error("fixture directory-sync");
}
if (path === copyFault.destination && copyFault.hits === 0 && copyFault.phase === "confirmation-change" && key === "sync") {
copyFault.hits++; await writeFile(copyFault.destination, credential("fixture-concurrent"), { mode: 0o600 });
}
if (staged && copyFault.hits === 0 && copyFault.phase === "generation-change" && key === "sync") {
copyFault.hits++; await writeFile(copyFault.destination, credential("fixture-concurrent"), { mode: 0o600 });
}
return Reflect.apply(value, target, callArgs);
};
} });
} };
});
vi.mock("node:fs/promises", async importOriginal => {
const actual = await importOriginal<typeof import("node:fs/promises")>();
return { ...actual, rename: async (...args: Parameters<typeof actual.rename>) => {
if (String(args[1]) === copyFault.destination && copyFault.hits === 0 && copyFault.phase.startsWith("rename-")) {
copyFault.hits++;
if (copyFault.phase === "rename-after") await actual.rename(...args);
throw new Error("fixture " + copyFault.phase);
}
return actual.rename(...args);
}, rm: async (...args: Parameters<typeof actual.rm>) => {
const path = String(args[0]);
if (path.includes("/.acp_token.copyback-")) copyFault.removals.push(path);
if (copyFault.hits === 0 && ((copyFault.phase === "stage-remove" && path.includes("/.acp_token.copyback-"))
|| (copyFault.phase === "runtime-remove" && path === copyFault.source))) {
copyFault.hits++; copyFault.failedRemoval = path;
throw new Error("fixture " + copyFault.phase);
}
return actual.rm(...args);
} };
});
const roots: string[] = [];
const credential = (token: string) => JSON.stringify({
client_id: "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com", client_secret: "fixture-client-secret",
refresh_token: token, token_uri: "https://oauth2.googleapis.com/token", scopes: ["https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/aicode"],
});
async function home() { const root = await realpath(await mkdtemp(join(tmpdir(), "antigravity-controller-test-"))); roots.push(root); await mkdir(join(root, "antigravity-acp"), { mode: 0o700 }); return root; }
beforeEach(async () => { vi.stubEnv("PAPERCLIP_HOME", await home()); });
afterEach(async () => { copyFault.phase = ""; copyFault.destination = ""; copyFault.source = ""; copyFault.collisionPath = ""; copyFault.failedRemoval = ""; copyFault.removals = []; copyFault.hits = 0; vi.unstubAllEnvs(); await Promise.all(roots.splice(0).map(path => rm(path, { recursive: true, force: true }))); });
describe("Antigravity managed Runner credentials", () => {
it("requires a selected managed account and never borrows the ambient Google home or inline secret", async () => {
const ambient = await home();
await writeFile(join(ambient, "antigravity-acp", "acp_token.json"), credential("fixture-ambient"), { mode: 0o600 });
vi.stubEnv("GEMINI_HOME", ambient);
vi.stubEnv("GEMINI_API_KEY", "fixture-ambient-key");
await expect(prepareAntigravityRunnerCredentials({ remote: false, environment: { GEMINI_HOME: ambient, PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: credential("fixture-spoofed") } })).rejects.toThrow("selected managed Google account");
});
it("uses the explicit API key while removing all subscription home and payload inputs", async () => {
expect(await prepareAntigravityRunnerCredentials({ remote: true, environment: {
GEMINI_API_KEY: "fixture-selected", GOOGLE_API_KEY: "other", GEMINI_HOME: "/ambient", GOOGLE_APPLICATION_CREDENTIALS: "/ambient.json",
GOOGLE_CLOUD_ACCESS_TOKEN: "ambient", PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: "spoofed",
} })).toEqual({ home: null, environment: { GEMINI_API_KEY: "fixture-selected" } });
});
it("projects only the selected private token and rejects remote subscription delivery", async () => {
const managedHome = await home();
await writeFile(join(managedHome, "antigravity-acp", "acp_token.json"), credential("fixture-selected"), { mode: 0o600 });
expect(await prepareAntigravityRunnerCredentials({ remote: false, managedHome, environment: { GEMINI_HOME: "/untrusted" } })).toEqual({
home: managedHome, credential: credential("fixture-selected"), environment: { PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: credential("fixture-selected") },
});
await expect(prepareAntigravityRunnerCredentials({ remote: true, managedHome, environment: {} })).rejects.toThrow("not qualified for remote");
await expect(prepareAntigravityRunnerCredentials({ remote: false, managedHome, environment: {}, lifecycleMode: "warm" })).rejects.toThrow("warm sessions are not qualified");
});
it.each(["symlink", "public", "invalid"])("rejects a %s selected token without using a fallback", async kind => {
const managedHome = await home(), token = join(managedHome, "antigravity-acp", "acp_token.json");
const other = join(managedHome, "other.json");
await writeFile(other, credential("fixture-other"), { mode: 0o600 });
if (kind === "symlink") await symlink(other, token);
else await writeFile(token, kind === "invalid" ? "{}" : credential("fixture-selected"), { mode: kind === "public" ? 0o644 : 0o600 });
await expect(prepareAntigravityRunnerCredentials({ remote: false, managedHome, environment: {} })).rejects.toThrow("selected Google Antigravity account is unavailable");
});
it.each(["acp_token.json", "auth-refresh.json"])("preserves %s refreshes, then scrubs runtime credentials only after provider close", async source => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json"), runtime = join(runtimeHome, "antigravity-acp");
await writeFile(destination, credential("fixture-original"), { mode: 0o600 });
for (const name of [source, "auth-refresh.json.tmp"]) await writeFile(join(runtime, name), credential("fixture-refreshed"), { mode: 0o600 });
const close = vi.fn(async () => { expect(await readFile(join(runtime, source), "utf8")).toBe(credential("fixture-refreshed")); });
const session = bindAntigravityCredentialTurn({ close } as unknown as NativeSession,
{ managedHome, runtimeHome, credential: credential("fixture-original") });
await completeManagedNativeCredentialTurn(session);
expect(await readFile(destination, "utf8")).toBe(credential("fixture-refreshed"));
expect((await stat(destination)).mode & 0o777).toBe(0o600);
expect(close).toHaveBeenCalledOnce();
await expect(access(join(runtime, source))).rejects.toMatchObject({ code: "ENOENT" });
await session.close({ reason: "completed" });
expect(close).toHaveBeenCalledOnce();
for (const name of ["acp_token.json", "auth-refresh.json", "auth-refresh.json.tmp"]) await expect(access(join(runtime, name))).rejects.toMatchObject({ code: "ENOENT" });
});
it("keeps a newer invocation destination and fails closed on an invalid runtime token", async () => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json"), source = join(runtimeHome, "antigravity-acp", "acp_token.json");
await writeFile(destination, credential("fixture-concurrent"), { mode: 0o600 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
const turn = antigravityCredentialTurn({ managedHome, runtimeHome, credential: credential("fixture-original") });
await expect(turn.copyBack()).rejects.toThrow("destination changed");
expect(await readFile(destination, "utf8")).toBe(credential("fixture-concurrent"));
await chmod(source, 0o644);
await expect(turn.copyBack()).rejects.toThrow("handoff is invalid");
expect(await readFile(destination, "utf8")).toBe(credential("fixture-concurrent"));
});
it("can confirm a completed identical copy after losing its prior completion", async () => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json");
const source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
await writeFile(destination, credential("fixture-refreshed"), { mode: 0o600 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
await antigravityCredentialTurn({ managedHome, runtimeHome, credential: credential("fixture-original") }).copyBack();
expect(await readFile(destination, "utf8")).toBe(credential("fixture-refreshed"));
});
it("retains a rotated refresh after failed copy-back and retries the retired owner before scrubbing", async () => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json");
const source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
await writeFile(destination, credential("fixture-original"), { mode: 0o644 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
const close = vi.fn(async () => {});
const session = bindAntigravityCredentialTurn({ close } as unknown as NativeSession,
{ managedHome, runtimeHome, credential: credential("fixture-original") });
await expect(completeManagedNativeCredentialTurn(session)).rejects.toThrow("destination is invalid");
expect(await readFile(source, "utf8")).toBe(credential("fixture-refreshed"));
await chmod(destination, 0o600);
await session.close({ reason: "retry credential handoff" });
expect(close).toHaveBeenCalledOnce();
expect(await readFile(destination, "utf8")).toBe(credential("fixture-refreshed"));
await expect(access(source)).rejects.toMatchObject({ code: "ENOENT" });
});
it("settles the retained runtime refresh before reusing its original home", async () => {
const managedHome = await home(), runtimeHome = await home();
const release = bindRetainedGoogleCredentialHome(managedHome, { companyId: "company", accountKey: managedHome });
const destination = join(managedHome, "antigravity-acp", "acp_token.json");
const source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
await writeFile(destination, credential("fixture-original"), { mode: 0o644 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
const close = vi.fn(async () => {});
const session = bindAntigravityCredentialTurn({ close } as unknown as NativeSession,
{ managedHome, runtimeHome, credential: credential("fixture-original") });
try {
await expect(completeManagedNativeCredentialTurn(session)).rejects.toThrow("destination is invalid");
await expect(prepareAntigravityRunnerCredentials({ managedHome, remote: false, environment: {} }))
.rejects.toMatchObject({ details: { code: "ai_credential_cleanup_pending" } });
expect(await readFile(source, "utf8")).toBe(credential("fixture-refreshed"));
await chmod(destination, 0o600);
expect((await prepareAntigravityRunnerCredentials({ managedHome, remote: false, environment: {} })).credential)
.toBe(credential("fixture-refreshed"));
expect(close).toHaveBeenCalledOnce();
await expect(access(source)).rejects.toMatchObject({ code: "ENOENT" });
} finally { release(); }
});
it.each(["turn completion", "direct close"])("retries original process proof after rejected %s before reusing the original home, without clearing session failure", async origin => {
const managedHome = await home(), runtimeHome = await home();
const release = bindRetainedGoogleCredentialHome(managedHome, { companyId: "company-retirement", accountKey: managedHome });
const destination = join(managedHome, "antigravity-acp", "acp_token.json"), source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
await writeFile(destination, credential("fixture-original"), { mode: 0o600 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
const terminal = Object.assign(new Error("fixture native session durable suspension failed"), { code: "native_session_close_unrecoverable" });
const close = vi.fn(async () => { throw terminal; });
let physicallyRetired = false;
let releaseProof!: () => void;
const proofGate = new Promise<void>(resolve => { releaseProof = resolve; });
const proof = vi.fn(async () => {
if (!physicallyRetired) throw new Error("fixture original process still alive");
await proofGate;
});
const transport = bindAntigravityTransportRetirement({ transport: { close }, retireProbeResources: proof } as unknown as ReturnType<typeof createRunnerdCodexTransport>);
const session = bindAntigravityCredentialTurn({ close: () => transport.close() } as unknown as NativeSession,
{ managedHome, runtimeHome, credential: credential("fixture-original") });
try {
await expect(origin === "turn completion" ? completeManagedNativeCredentialTurn(session) : session.close({ reason: "direct close" })).rejects.toBe(terminal);
await expect(prepareAntigravityRunnerCredentials({ managedHome, remote: false, environment: {} }))
.rejects.toMatchObject({ details: { code: "ai_credential_cleanup_pending" } });
expect(proof).toHaveBeenCalledOnce();
expect(await readFile(source, "utf8")).toBe(credential("fixture-refreshed"));
expect(await readFile(destination, "utf8")).toBe(credential("fixture-original"));
physicallyRetired = true;
const first = prepareAntigravityRunnerCredentials({ managedHome, remote: false, environment: {} });
const second = prepareAntigravityRunnerCredentials({ managedHome, remote: false, environment: {} });
let admitted = false;
const preparing = Promise.all([first, second]).then(result => { admitted = true; return result; });
await vi.waitFor(() => expect(proof).toHaveBeenCalledTimes(2));
expect(admitted).toBe(false);
expect(await readFile(source, "utf8")).toBe(credential("fixture-refreshed"));
releaseProof();
const next = await preparing;
expect(next.every(value => value.credential === credential("fixture-refreshed"))).toBe(true);
expect(proof).toHaveBeenCalledTimes(2);
expect(close).toHaveBeenCalledOnce();
await expect(access(source)).rejects.toMatchObject({ code: "ENOENT" });
await expect(session.close({ reason: "original session remains quarantined" })).rejects.toBe(terminal);
expect(close).toHaveBeenCalledOnce();
} finally { releaseProof(); release(); }
});
it.each(["partial-write", "stage-stat", "stage-sync", "stage-close", "rename-before", "rename-after", "directory-sync", "stage-remove", "runtime-remove"])(
"retains an intact token and retries the retired owner after %s", async phase => {
const managedHome = await home(), runtimeHome = await home();
const directory = join(managedHome, "antigravity-acp"), destination = join(directory, "acp_token.json");
const source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
const original = credential("fixture-original"), refreshed = credential("fixture-refreshed");
await writeFile(destination, original, { mode: 0o600 });
await writeFile(source, refreshed, { mode: 0o600 });
const close = vi.fn(async () => {});
const session = bindAntigravityCredentialTurn({ close } as unknown as NativeSession, { managedHome, runtimeHome, credential: original });
const release = bindRetainedGoogleCredentialHome(managedHome, { companyId: "company-atomic", accountKey: managedHome });
copyFault.phase = phase; copyFault.destination = destination; copyFault.source = source; copyFault.hits = 0;
try {
await expect(completeManagedNativeCredentialTurn(session)).rejects.toThrow("fixture " + phase);
expect(copyFault.hits).toBe(1);
expect(await readFile(destination, "utf8")).toBe(["rename-after", "directory-sync", "stage-remove", "runtime-remove"].includes(phase) ? refreshed : original);
expect(await readFile(source, "utf8")).toBe(refreshed);
expect((await stat(destination)).mode & 0o777).toBe(0o600);
expect(await readdir(directory)).toEqual(["acp_token.json"]);
copyFault.phase = "";
const next = await prepareAntigravityRunnerCredentials({ managedHome, remote: false, environment: {} });
expect(next.credential).toBe(refreshed);
if (phase === "stage-remove") expect(copyFault.removals.filter(path => path === copyFault.failedRemoval)).toHaveLength(2);
expect(close).toHaveBeenCalledOnce();
await expect(access(source)).rejects.toMatchObject({ code: "ENOENT" });
expect(await readdir(directory)).toEqual(["acp_token.json"]);
} finally { copyFault.phase = ""; release(); }
});
it.each(["missing", "reset"])("retains staging cleanup when its runtime refresh is unexpectedly %s", async state => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json");
const source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
const original = credential("fixture-original"), refreshed = credential("fixture-refreshed");
await writeFile(destination, original, { mode: 0o600 });
await writeFile(source, refreshed, { mode: 0o600 });
const turn = antigravityCredentialTurn({ managedHome, runtimeHome, credential: original });
copyFault.phase = "stage-remove"; copyFault.destination = destination;
await expect(turn.copyBack()).rejects.toThrow("fixture stage-remove");
expect(await readFile(destination, "utf8")).toBe(refreshed);
copyFault.phase = "";
if (state === "missing") await rm(source);
else await writeFile(source, original, { mode: 0o600 });
await expect(turn.copyBack()).rejects.toThrow("handoff is invalid");
await writeFile(source, refreshed, { mode: 0o600 });
await turn.copyBack();
expect(copyFault.removals.filter(path => path === copyFault.failedRemoval)).toHaveLength(2);
await turn.remove();
expect(await readFile(destination, "utf8")).toBe(refreshed);
await expect(access(source)).rejects.toMatchObject({ code: "ENOENT" });
});
it("rechecks destination generation after staging without overwriting a concurrent credential", async () => {
const managedHome = await home(), runtimeHome = await home();
const directory = join(managedHome, "antigravity-acp"), destination = join(directory, "acp_token.json");
const source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
await writeFile(destination, credential("fixture-original"), { mode: 0o600 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
copyFault.phase = "generation-change"; copyFault.destination = destination;
await expect(antigravityCredentialTurn({ managedHome, runtimeHome, credential: credential("fixture-original") }).copyBack()).rejects.toThrow("destination changed");
expect(copyFault.hits).toBe(1);
expect(await readFile(destination, "utf8")).toBe(credential("fixture-concurrent"));
expect(await readFile(source, "utf8")).toBe(credential("fixture-refreshed"));
expect(await readdir(directory)).toEqual(["acp_token.json"]);
});
it("does not remove a staging file that it failed to create exclusively", async () => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json");
const source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
await writeFile(destination, credential("fixture-original"), { mode: 0o600 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
copyFault.phase = "stage-create"; copyFault.destination = destination;
const turn = antigravityCredentialTurn({ managedHome, runtimeHome, credential: credential("fixture-original") });
await expect(turn.copyBack()).rejects.toMatchObject({ code: "EEXIST" });
expect(await readFile(copyFault.collisionPath, "utf8")).toBe("fixture-unowned-stage");
expect(await readFile(destination, "utf8")).toBe(credential("fixture-original"));
expect(await readFile(source, "utf8")).toBe(credential("fixture-refreshed"));
copyFault.phase = "";
await turn.copyBack();
expect(await readFile(destination, "utf8")).toBe(credential("fixture-refreshed"));
expect(await readFile(copyFault.collisionPath, "utf8")).toBe("fixture-unowned-stage");
});
it("does not confirm a different generation after an uncertain successful rename", async () => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json");
const source = join(runtimeHome, "antigravity-acp", "auth-refresh.json");
await writeFile(destination, credential("fixture-refreshed"), { mode: 0o600 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
copyFault.phase = "confirmation-change"; copyFault.destination = destination;
await expect(antigravityCredentialTurn({ managedHome, runtimeHome, credential: credential("fixture-original") }).copyBack()).rejects.toThrow("destination changed");
expect(copyFault.hits).toBe(1);
expect(await readFile(destination, "utf8")).toBe(credential("fixture-concurrent"));
expect(await readFile(source, "utf8")).toBe(credential("fixture-refreshed"));
});
it("rejects a hard-linked destination without changing either name", async () => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json"), other = join(managedHome, "other.json");
await writeFile(other, credential("fixture-original"), { mode: 0o600 });
await link(other, destination);
await writeFile(join(runtimeHome, "antigravity-acp", "auth-refresh.json"), credential("fixture-refreshed"), { mode: 0o600 });
await expect(antigravityCredentialTurn({ managedHome, runtimeHome, credential: credential("fixture-original") }).copyBack()).rejects.toThrow("destination is invalid");
expect(await readFile(other, "utf8")).toBe(credential("fixture-original"));
expect(await readFile(destination, "utf8")).toBe(credential("fixture-original"));
});
it("does not copy or scrub a successor's credentials after the controller detached", async () => {
const managedHome = await home(), runtimeHome = await home();
const destination = join(managedHome, "antigravity-acp", "acp_token.json"), source = join(runtimeHome, "antigravity-acp", "acp_token.json");
await writeFile(destination, credential("fixture-original"), { mode: 0o600 });
await writeFile(source, credential("fixture-refreshed"), { mode: 0o600 });
const close = vi.fn(async () => {}), detach = vi.fn(async () => {});
const session = bindAntigravityCredentialTurn({ close, detachControllerForRestart: detach } as unknown as NativeSession,
{ managedHome, runtimeHome, credential: credential("fixture-original") });
await session.detachControllerForRestart!();
await session.close({ reason: "old controller" });
expect(close).not.toHaveBeenCalled();
expect(await readFile(destination, "utf8")).toBe(credential("fixture-original"));
expect(await readFile(source, "utf8")).toBe(credential("fixture-refreshed"));
});
});