mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-03 01:27:09 +01:00
## Thinking Path > - Paperclip manages agents and their work. > - Users choose providers and managed accounts while adding agents. > - Antigravity was missing from the ordinary additional-agent picker and its saved Google path did not complete native readiness. > - A user needs a successful Test, an enabled task action, and a stable account binding. > - This pull request connects the ordinary setup flow to the native candidate. > - It also preserves retired credential refreshes across database failures and server restarts. ## Linked Issues or Issue Description Built on merged #18188 and #18187. Refs: #12503, #14938 for related legacy CLI proposals. **Agent or provider** Google Antigravity ACP 1.4.3. **Why this adapter is useful** Users need to set up an Antigravity agent and run real work through their managed Google subscription. **How the agent is invoked** Native Paperclip Runner uses the pinned Antigravity ACP runtime. Exact operator admission remains required; the local candidate result does not enable general release. ## What Changed - Show Google Antigravity in the ordinary adapter grid and preserve the draft choice when setup resumes. - Reuse the same user’s saved personal default Google subscription and discover the exact low model through the native account. - Run a prompt-free native Test and wait for authoritative readiness before enabling task assignment. - Carry the managed account through native runs and recover retired refreshes through the existing encrypted, fsynced journal with exact generation checks. - Retain the latest rotated credential through verification, storage, and partial home-removal failures. Retry the exact account’s retained cleanup before another provider use. - Reconcile cleanup before taking the credential mutation lock, fence owners arriving during lock acquisition, and support nested lock holders without a join cycle. - Bound sign-in retirement outside database transactions and preserve claims for the existing cleanup owner; one pending sign-in does not block unrelated accounts. - Cover Gemini and Pi API-key recovery, keep API-key bulk manual-model editing usable, and keep adapter discovery available after candidate expiry. - Skip taskless native timer work before run or credential admission. Preserve current master monitor and deadline scheduling. ## Verification - The registered local saved-Google-subscription journey passed all 16 matchers in 196,520 ms using eval source `12aba` against actual serving source `4a37ee7c`. Both turns stayed in one conversation, produced exact downloaded files of 61 and 119 bytes after reload, and used distinct native primary processes. All observed owners retired, leases released, and credential fences settled. Independent readback confirmed Done work, no active owned runs, a connected saved account, and the retained agent paused for inspection. - The measured runtime is production-equivalent to pre-restack `65ef31dc` except its test-only transport fixture correction. Current-master head `ff7c9249` preserves all 99 product paths: 98 exact blobs and only eight inherited plugin-panel lines in `agents.ts`. Its native binary was rebuilt for inherited Codex-only diagnostic changes; the changed Rust behavior is outside Antigravity's ACPX execution path. Shared native/UI changes have focused regression coverage. The recorded live result is not represented as a measured replay of `ff7c9249`. - Full workspace build and typecheck passed on `ff7c9249`. All 376 focused tests in 14 files passed, covering Google credential/retirement controls and the inherited native-tool, redaction, route, and UI recovery changes. Token gates, maps, and diff checks passed. The required-close fixture was separately demonstrated red, then passed all 317 selected provider-wiring cases. - [Current-head CI](https://github.com/paperclipai/paperclip/actions/runs/40959387972) is green: 55 successful checks, two skipped workflow checks, and an optional neutral review check. Required `ci / verify` and `ci / e2e` passed. Greptile and Paperclip Review both report 5/5 on exact `ff7c9249`, with zero unresolved review threads. - This qualifies the selected operator-authorized local saved-subscription candidate. The original exploratory journey remains historical evidence; the repeatable fixed-driver eval is in the next PR. ## Risks - Google refresh recovery must preserve the exact grant, account, and credential generation. An uncertain native owner or failed encrypted handoff retains the private home and blocks cleanup. - A successful local saved-subscription candidate does not qualify fresh OAuth, API keys, Cloud, Linux, other models, or general release. - Timer filtering changes only taskless native Runner admissions. Existing task-bound monitors and legacy adapters retain their owners. ## Model Used OpenAI Codex (GPT-6). The host did not expose the exact model revision or context limit. The model used tools for code edits, tests, and review. ## 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 --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
224 lines
15 KiB
TypeScript
224 lines
15 KiB
TypeScript
import { access, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
import { promises as fs } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { Db } from "@paperclipai/db";
|
|
|
|
const account = vi.hoisted(() => ({
|
|
credential: "", epoch: 4, version: 8, status: "active",
|
|
refs: [{ configPath: "ai.credential", secretId: "22222222-2222-4222-8222-222222222222", versionSelector: "latest" }],
|
|
rotation: vi.fn(), locks: [] as string[],
|
|
journal: undefined as undefined | Record<string, unknown>, journalWrite: vi.fn(),
|
|
}));
|
|
vi.mock("node:fs/promises", async importOriginal => {
|
|
const actual = await importOriginal<typeof import("node:fs/promises")>();
|
|
const { promises } = await import("node:fs");
|
|
return { ...actual, rm: (...args: Parameters<typeof actual.rm>) => promises.rm(...args) };
|
|
});
|
|
const selection = () => ({
|
|
grant: { id: "11111111-1111-4111-8111-111111111111", subjectUserId: "alice", credentialSecretRefs: account.refs, providerTenant: { name: "alice@example.test" } },
|
|
connection: { id: "33333333-3333-4333-8333-333333333333", name: "Google account fixture", config: { ai: { provider: "google", method: "subscription" } } },
|
|
attribution: { connectionId: "33333333-3333-4333-8333-333333333333", grantId: "11111111-1111-4111-8111-111111111111",
|
|
provider: "google", method: "subscription", mode: "responsible_user", responsibleUserId: "alice" },
|
|
});
|
|
vi.mock("../services/ai-connections.js", () => ({ aiConnectionService: () => ({
|
|
select: async () => selection(), runtimeCredential: async () => account.credential, credential: async () => account.credential,
|
|
}) }));
|
|
vi.mock("../services/subscriptions.js", () => ({ subscriptionService: () => ({ register: async () => "subscription-fixture" }) }));
|
|
vi.mock("../services/subscription-refresh.js", () => ({ refreshSubscriptionConnection: vi.fn() }));
|
|
vi.mock("../services/secrets.js", () => ({ secretService: () => ({ rotate: account.rotation }) }));
|
|
vi.mock("../services/quota-credential-recovery.js", () => ({
|
|
quotaCredentialHash: (value: string) => value,
|
|
quotaCredentialRecovery: () => ({ read: async () => account.journal,
|
|
write: account.journalWrite, clear: async () => { account.journal = undefined; } }),
|
|
RetiredCredentialRecoveryError: class extends Error {
|
|
constructor(cause: unknown) { super("The selected account credential is awaiting durable recovery. Retry shortly.", { cause }); }
|
|
},
|
|
}));
|
|
vi.mock("@paperclipai/adapter-codex-local/server", () => ({
|
|
decideCodexAuthMerge: vi.fn(), withAccountHomeSecretMutationLock: async (_environment: unknown, _companyId: unknown, work: () => Promise<void>) => work(),
|
|
}));
|
|
import { prepareManagedAiRuntime, withManagedAiProbe } from "../services/ai-connection-runtime.js";
|
|
import { NativeEnvironmentProbeCleanupError } from "../vendor/paperclip-runner/index.js";
|
|
|
|
const credential = (project?: string, token = "fixture-selected-refresh") => 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"],
|
|
...(project ? { project_id: project } : {}),
|
|
});
|
|
const homes: string[] = [];
|
|
const runtimes: Awaited<ReturnType<typeof prepareManagedAiRuntime>>[] = [];
|
|
function database(): Db {
|
|
const db = {
|
|
select: (fields?: Record<string, unknown>) => ({ from: () => ({ where: () => {
|
|
const rows = !fields ? [{ ...selection().grant, status: account.status }]
|
|
: "refs" in fields ? [{ refs: account.refs, status: account.status }]
|
|
: [{ id: account.refs[0].secretId, epoch: account.epoch, version: account.version }];
|
|
return Object.assign(Promise.resolve(rows), {
|
|
limit: async () => rows,
|
|
for: async (lock: string) => { account.locks.push(`${fields ? "secret" : "grant"}:${lock}`); return rows; },
|
|
});
|
|
} }) }),
|
|
transaction: async (work: (transaction: Db) => Promise<void>) => work(db as unknown as Db),
|
|
};
|
|
return db as unknown as Db;
|
|
}
|
|
async function prepare() {
|
|
const runtime = await prepareManagedAiRuntime(database(), {
|
|
companyId: "44444444-4444-4444-8444-444444444444", agentId: "55555555-5555-4555-8555-555555555555", responsibleUserId: "alice",
|
|
adapterType: "paperclip_runner", binding: { provider: "google", method: "subscription", mode: "responsible_user" },
|
|
config: { provider: "acpx", acpxAgent: "antigravity", model: "caller-model", env: { GEMINI_API_KEY: "ambient", GEMINI_HOME: "/ambient" } },
|
|
});
|
|
homes.push(runtime.home!);
|
|
runtimes.push(runtime);
|
|
return runtime;
|
|
}
|
|
beforeEach(() => {
|
|
account.credential = credential(); account.epoch = 4; account.version = 8; account.status = "active"; account.locks = [];
|
|
account.refs = [{ configPath: "ai.credential", secretId: "22222222-2222-4222-8222-222222222222", versionSelector: "latest" }];
|
|
account.rotation.mockReset().mockResolvedValue(undefined);
|
|
account.journal = undefined;
|
|
account.journalWrite.mockReset().mockImplementation(async value => { account.journal = value; });
|
|
});
|
|
afterEach(async () => {
|
|
account.journalWrite.mockImplementation(async value => { account.journal = value; });
|
|
vi.stubGlobal("fetch", vi.fn(async (url: string) => Response.json(url.includes("userinfo")
|
|
? { email_verified: true, email: "alice@example.test" } : { access_token: "fixture-access", token_type: "Bearer" })));
|
|
for (const runtime of runtimes.splice(0)) await runtime.cleanup().catch(() => undefined);
|
|
vi.unstubAllGlobals();
|
|
await Promise.all(homes.splice(0).map(path => rm(path, { recursive: true, force: true })));
|
|
});
|
|
|
|
describe("managed Antigravity subscription projection and writeback", () => {
|
|
it("returns a bounded cleanup diagnostic and retains the selected home until native retirement and refresh handoff", async () => {
|
|
let confirmRetirement!: () => void;
|
|
const retired = new Promise<void>(resolve => { confirmRetirement = resolve; });
|
|
let home = "", token = "";
|
|
const result = await withManagedAiProbe(database(), {
|
|
companyId: "44444444-4444-4444-8444-444444444444", agentId: "55555555-5555-4555-8555-555555555555", responsibleUserId: "alice",
|
|
adapterType: "paperclip_runner", binding: { provider: "google", method: "subscription", mode: "responsible_user" },
|
|
config: { provider: "acpx", acpxAgent: "antigravity", model: "caller-model" },
|
|
}, async runtime => {
|
|
home = runtime.home!; homes.push(home);
|
|
token = join(String(runtime.config.env.GEMINI_HOME), "antigravity-acp", "acp_token.json");
|
|
throw new NativeEnvironmentProbeCleanupError(retired, new Error("private fixture operation"), [new Error("fixture retirement is pending")]);
|
|
}).catch(error => error);
|
|
expect(result).toMatchObject({ status: 422, details: { code: "native_probe_cleanup_pending" } });
|
|
expect(result.message).not.toContain("private fixture operation");
|
|
expect(account.rotation).not.toHaveBeenCalled();
|
|
expect(await readFile(token, "utf8")).toBe(credential());
|
|
// The native owner completes its one refresh handoff before publishing exit.
|
|
await writeFile(token, credential("retired-project"));
|
|
confirmRetirement();
|
|
await expect.poll(async () => access(home).then(() => false, () => true)).toBe(true);
|
|
expect(account.rotation).toHaveBeenCalledExactlyOnceWith(account.refs[0].secretId,
|
|
{ value: credential("retired-project"), preserveAiSessionEpoch: true }, { userId: "alice" });
|
|
});
|
|
it("writes a private native token with explicit OAuth settings and saves same-token project resolution under both row locks", async () => {
|
|
const runtime = await prepare(), provider = String(runtime.config.env.GEMINI_HOME), token = join(provider, "antigravity-acp", "acp_token.json");
|
|
expect(runtime.config.env).toMatchObject({ GEMINI_API_KEY: "", GOOGLE_API_KEY: "", PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: "" });
|
|
expect(await readFile(token, "utf8")).toBe(credential());
|
|
expect((await stat(token)).mode & 0o777).toBe(0o600);
|
|
expect(JSON.parse(await readFile(join(provider, "antigravity-acp", "settings.json"), "utf8"))).toEqual({ auth: { type: "oauth-personal" } });
|
|
await expect(access(join(provider, "auth.json"))).rejects.toMatchObject({ code: "ENOENT" });
|
|
await writeFile(token, credential("resolved-project"));
|
|
await runtime.cleanup();
|
|
expect(account.locks).toEqual(["grant:update", "secret:update"]);
|
|
expect(account.rotation).toHaveBeenCalledWith(account.refs[0].secretId,
|
|
{ value: credential("resolved-project"), preserveAiSessionEpoch: true }, { userId: "alice" });
|
|
await expect(access(runtime.home!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
it.each(["revoked", "remapped", "epoch", "version", "concurrent-refresh"] as const)("discards stale refreshes when the selected credential is %s", async change => {
|
|
const runtime = await prepare(), token = join(String(runtime.config.env.GEMINI_HOME), "antigravity-acp", "acp_token.json");
|
|
await writeFile(token, credential("stale-project"));
|
|
if (change === "revoked") account.status = "revoked";
|
|
if (change === "remapped") account.refs = [{ ...account.refs[0], secretId: "66666666-6666-4666-8666-666666666666" }];
|
|
if (change === "epoch") account.epoch += 1;
|
|
if (change === "version") account.version += 1;
|
|
if (change === "concurrent-refresh") account.credential = credential("saved-newer-project");
|
|
await runtime.cleanup();
|
|
expect(account.rotation).not.toHaveBeenCalled();
|
|
await expect(access(runtime.home!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
it("transfers a retired refresh to the durable journal when the database write fails", async () => {
|
|
const runtime = await prepare(), token = join(String(runtime.config.env.GEMINI_HOME), "antigravity-acp", "acp_token.json");
|
|
await writeFile(token, credential("resolved-project"));
|
|
account.rotation.mockRejectedValueOnce(new Error("fixture database write failure"));
|
|
await expect(runtime.cleanup()).rejects.toMatchObject({ cause: { message: "fixture database write failure" } });
|
|
expect(account.journal).toMatchObject({ provider: "google", value: credential("resolved-project"),
|
|
google: { sessionEpoch: 4, baseVersion: 8, verified: true, accountEmail: "alice@example.test" } });
|
|
expect(account.rotation).toHaveBeenCalledTimes(1);
|
|
await expect(access(runtime.home!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
it("retains the private refresh home when encrypted journal storage fails", async () => {
|
|
const runtime = await prepare(), token = join(String(runtime.config.env.GEMINI_HOME), "antigravity-acp", "acp_token.json");
|
|
await writeFile(token, credential("resolved-project"));
|
|
account.journalWrite.mockRejectedValueOnce(new Error("fixture journal unavailable"));
|
|
await expect(runtime.cleanup()).rejects.toThrow("fixture journal unavailable");
|
|
expect(await readFile(token, "utf8")).toBe(credential("resolved-project"));
|
|
expect(account.rotation).not.toHaveBeenCalled();
|
|
});
|
|
it("finishes partial removal after durable handoff without rereading a removed token", async () => {
|
|
const runtime = await prepare(), token = join(String(runtime.config.env.GEMINI_HOME), "antigravity-acp", "acp_token.json");
|
|
await writeFile(token, credential("resolved-project"));
|
|
account.rotation.mockRejectedValueOnce(new Error("fixture database unavailable"));
|
|
const realRm = fs.rm.bind(fs);
|
|
let unavailable = true;
|
|
const remove = vi.spyOn(fs, "rm").mockImplementation(async (...args: Parameters<typeof fs.rm>) => {
|
|
if (unavailable && args[0] === runtime.home) {
|
|
await realRm(token, { force: true });
|
|
throw new Error("fixture partial removal");
|
|
}
|
|
return realRm(...args);
|
|
});
|
|
try {
|
|
await expect(runtime.cleanup()).rejects.toThrow("fixture partial removal");
|
|
expect(account.journal).toMatchObject({ value: credential("resolved-project"), google: { verified: true } });
|
|
unavailable = false;
|
|
await runtime.cleanup();
|
|
await expect(access(runtime.home!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
expect(account.rotation).toHaveBeenCalledOnce();
|
|
} finally { unavailable = false; remove.mockRestore(); }
|
|
});
|
|
it("retains the latest verified rotation when replacing its earlier journal fails", async () => {
|
|
const runtime = await prepare(), token = join(String(runtime.config.env.GEMINI_HOME), "antigravity-acp", "acp_token.json");
|
|
await writeFile(token, credential("resolved-project", "fixture-first-rotation"));
|
|
const request = vi.fn().mockResolvedValueOnce(Response.json({ access_token: "fixture-access", token_type: "Bearer", refresh_token: "fixture-latest-rotation" }))
|
|
.mockResolvedValueOnce(Response.json({ email_verified: true, email: "alice@example.test" }));
|
|
vi.stubGlobal("fetch", request);
|
|
account.journalWrite.mockImplementationOnce(async value => { account.journal = value; })
|
|
.mockRejectedValueOnce(new Error("fixture latest journal unavailable"));
|
|
await expect(runtime.cleanup()).rejects.toThrow();
|
|
expect(await readFile(token, "utf8")).toBe(credential("resolved-project", "fixture-latest-rotation"));
|
|
expect(account.journal).toMatchObject({ value: credential("resolved-project", "fixture-first-rotation"), google: { verified: false } });
|
|
expect(account.rotation).not.toHaveBeenCalled();
|
|
account.rotation.mockImplementationOnce(async (_secretId, input) => { account.credential = input.value; account.version += 1; });
|
|
request.mockResolvedValueOnce(Response.json({ access_token: "fixture-access", refresh_token: "fixture-retry-rotation" }))
|
|
.mockResolvedValueOnce(Response.json({ email_verified: true, email: "alice@example.test" }));
|
|
const next = await prepare();
|
|
expect(new URLSearchParams(request.mock.calls[2][1].body).get("refresh_token")).toBe("fixture-latest-rotation");
|
|
expect(await readFile(join(String(next.config.env.GEMINI_HOME), "antigravity-acp", "acp_token.json"), "utf8"))
|
|
.toBe(credential("resolved-project", "fixture-retry-rotation"));
|
|
await expect(access(runtime.home!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
it.each(["alice@example.test", "other@example.test"])("verifies %s before a changed refresh token can reach the database", async email => {
|
|
const runtime = await prepare(), token = join(String(runtime.config.env.GEMINI_HOME), "antigravity-acp", "acp_token.json");
|
|
await writeFile(token, credential("resolved-project", "fixture-rotated-refresh"));
|
|
const request = vi.fn().mockResolvedValueOnce(Response.json({ access_token: "fixture-access", token_type: "Bearer" }))
|
|
.mockResolvedValueOnce(Response.json({ email_verified: true, email }));
|
|
vi.stubGlobal("fetch", request);
|
|
if (email === "alice@example.test") {
|
|
await runtime.cleanup();
|
|
expect(account.rotation).toHaveBeenCalledWith(account.refs[0].secretId,
|
|
{ value: credential("resolved-project", "fixture-rotated-refresh"), preserveAiSessionEpoch: true }, { userId: "alice" });
|
|
await expect(access(runtime.home!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
} else {
|
|
await expect(runtime.cleanup()).rejects.toThrow("The refreshed Google account does not match the selected connection.");
|
|
expect(account.rotation).not.toHaveBeenCalled();
|
|
expect(account.locks).toEqual([]);
|
|
expect(account.journal).toMatchObject({ provider: "google", google: { verified: false }, value: credential("resolved-project", "fixture-rotated-refresh") });
|
|
expect(await readFile(token, "utf8")).toBe(credential("resolved-project", "fixture-rotated-refresh"));
|
|
}
|
|
});
|
|
});
|