mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 20:09:33 +01:00
## Thinking Path > - Paperclip manages the connections agents use to work. > - A run can outlive the Google refresh token that started it. > - Antigravity writes a rotated token to a private file. Its two runtimes use different paths and JSON layouts. > - The task already records which connection supplied its sign-in. > - Runner reports a rotation on PRP. Core saves it to that connection if the saved generation still matches. > - The next run can start with the rotated token. ## Linked Issues or Issue Description Refs #23395. Refs #23168. This PR targets `master` and includes the capability-based handler from #23395. Merging this PR lands the handler and Antigravity writeback together. **Current behavior** A managed Antigravity sign-in reaches the plugin as an environment credential. Core waits for task retirement to collect a refresh. **Proposed behavior** Seed a private auth-file. Observe refresh-token changes for the ordinary ACP server and the unified CLI. Use the existing credential update event to save them during execution. **Reason and benefit** A long-running task can keep its source connection current across successive rotations. ## What Changed - Dispatch PRP credential updates to the live writeback capability declared for the bound harness and execution target. - Send Antigravity subscriptions as auth-file credentials to environment task providers. Keep Gemini API keys and previously admitted environment credentials on their existing paths. - Watch the ACP token file and the unified CLI token file. Compare the refresh token, copy it to the bound file, and emit the existing secret-free event. - Bind writeback to the original grant, secret, session epoch, and credential generation. Save the next generation in the same transaction. ## Verification - Reproduced the failed final Google save hanging on the next session close. After commit `2b3e7897cd`, both focused cleanup suites passed (43 tests). The regression covers two overlapping close retries after the credential home is released and checks that the saved credential is removed only after persistence succeeds. - All 21 Rust observer tests passed on both builds. They cover interruption between the host file and event journal writes, a further rotation before restart, and a reconnect that supersedes the task. - On the legacy build, 422 Runner TypeScript tests passed. With the merged unified CLI on this branch, all 126 tests in the auth, credential staging, and runtime host suites passed. - The two plugin writeback database suites passed (39 tests). A refresh that starts before task stop cannot overwrite the final token after stop. - One run timed out in an unrelated process-retirement test. All three suites passed together on repeat. All OAuth documents in these tests are fixtures. No live Google sign-in was exercised. ## Risks - The agent and any process it starts are trusted not to substitute another account’s refresh token in the harness credential file. Core accepts the file’s refresh token for the original connection only while the grant, secret, session epoch, and expected credential generation still match. Substitution within that boundary is an accepted risk. [Review finding](https://github.com/paperclipai/paperclip/pull/23397#discussion_r4759261984) - Deploy with a plugin and Launcher that support the new auth-file provider. This branch includes the unified CLI from #23168 and still observes the legacy token format. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used OpenAI GPT-6 in Codex, with local code execution and GitHub CLI. ## 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>
165 lines
9.9 KiB
TypeScript
165 lines
9.9 KiB
TypeScript
import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from "vitest";
|
|
import type { AdapterExecutionTarget } from "@paperclipai/adapter-utils/execution-target";
|
|
|
|
const release = vi.hoisted(() => ({ pending: false }));
|
|
const grant = vi.hoisted(() => ({ admitted: false, targets: [] as Array<{ platform: string; architecture: string } | undefined> }));
|
|
const backend = vi.hoisted(() => ({ options: undefined as undefined | { codexTransportFactory?: () => unknown } }));
|
|
|
|
vi.mock("../../vendor/paperclip-runner/index.js", async importOriginal => {
|
|
const original = await importOriginal<typeof import("../../vendor/paperclip-runner/index.js")>();
|
|
const pending = {
|
|
...original.QUALIFIED_ACPX_PROFILES.antigravity,
|
|
agentProfileVersion: original.QUALIFIED_ACPX_PROFILES.antigravity.agentProfileVersion - 1,
|
|
qualificationStatus: "pending" as const,
|
|
};
|
|
const profiles = { ...original.QUALIFIED_ACPX_PROFILES };
|
|
Object.defineProperty(profiles, "antigravity", { enumerable: true,
|
|
get: () => release.pending ? pending : original.QUALIFIED_ACPX_PROFILES.antigravity });
|
|
return {
|
|
...original,
|
|
QUALIFIED_ACPX_PROFILES: profiles,
|
|
createNativeSessionBackend: vi.fn((_session: unknown, options: { codexTransportFactory?: () => unknown }) => {
|
|
backend.options = options;
|
|
return { kind: "test", openSession: vi.fn() };
|
|
}),
|
|
createRunnerdCodexTransport: vi.fn(() => ({ transport: { close: vi.fn(async () => undefined) } })),
|
|
executeNativeSession: vi.fn(async () => { throw new Error("fixture: provider execution is out of scope"); }),
|
|
};
|
|
});
|
|
|
|
vi.mock("./acpx-qualification.js", async importOriginal => {
|
|
const original = await importOriginal<typeof import("./acpx-qualification.js")>();
|
|
return {
|
|
...original,
|
|
resolveAntigravityCandidateAdmission: vi.fn((input: Parameters<typeof original.resolveAntigravityCandidateAdmission>[0]) => {
|
|
grant.targets.push(input.target);
|
|
if (!grant.admitted || input.target?.platform !== "linux" || input.target.architecture !== "x64") return undefined;
|
|
return { schema: "paperclip.acpx-candidate-admission.v1", companyId: input.companyId, agent: "antigravity", model: input.model,
|
|
platform: "linux", architecture: "x64" };
|
|
}),
|
|
};
|
|
});
|
|
|
|
vi.mock("./native-runner-file-handoff.js", () => ({
|
|
stageNativeRunnerWakeAttachments: vi.fn(async () => ({ attachments: [], cleanup: async () => undefined })),
|
|
renderNativeRunnerStagedAttachmentPrompt: vi.fn(() => null),
|
|
}));
|
|
|
|
vi.mock("./native-controller-recovery.js", async importOriginal => ({
|
|
...await importOriginal<typeof import("./native-controller-recovery.js")>(),
|
|
resolveNativeControllerRecovery: vi.fn(async () => ({ kind: "no_prior_execution" })),
|
|
}));
|
|
|
|
vi.mock("../../runtime-api.js", async importOriginal => ({
|
|
...await importOriginal<typeof import("../../runtime-api.js")>(),
|
|
resolveLocalRuntimeApiUrl: vi.fn(() => "http://127.0.0.1:43219"),
|
|
}));
|
|
|
|
import { NativeProviderStateIncompatibleError, type NativeExecutionInput } from "../../vendor/paperclip-runner/index.js";
|
|
import { antigravityReleasePending } from "./antigravity-release.js";
|
|
import { createRunnerdBackend, executePaperclipNativeSession } from "./native-session-executor.js";
|
|
import * as pluginTaskRuntime from "./plugin-runner-task.js";
|
|
import * as pluginCredentialWriteback from "../plugin-task-credential-writeback.js";
|
|
import { antigravityExecution, emptyDb } from "./antigravity.test-fixture.js";
|
|
|
|
const describeTask = vi.fn(async (operation: { kind: string }) => operation.kind === "describe"
|
|
? { kind: "description", launchPreparationVersion: 1, projectAuthCheckVersion: 1, taskId: "task",
|
|
runner: { version: "fixture", digest: `sha256:${"a".repeat(64)}` } }
|
|
: { kind: "accepted", taskId: "task" });
|
|
const pluginTarget = () => ({ kind: "remote", transport: "sandbox", providerKey: "plugin", leaseId: "lease",
|
|
remoteCwd: "/unused", runnerTask: describeTask }) as unknown as AdapterExecutionTarget;
|
|
const sandboxTarget = () => ({ kind: "remote", transport: "sandbox", remoteCwd: "/workspace",
|
|
runner: { execute: vi.fn() } }) as unknown as AdapterExecutionTarget;
|
|
const runStart = (execution: NativeExecutionInput, runnerExecutionTarget: AdapterExecutionTarget, db = emptyDb()) =>
|
|
executePaperclipNativeSession({ db, execution, useRunnerd: true, runnerInstanceId: "antigravity-run-start",
|
|
runnerIngressAuthorized: true, runnerExecutionTarget });
|
|
const INELIGIBLE = /paperclip_runner_antigravity_candidate_ineligible/;
|
|
const COORDINATOR_CLAIM = /^native_finalization_coordinator_missing$/;
|
|
const coordinatorReads = [{ table: "native_run_finalizations", locked: false }, { table: "native_run_finalizations", locked: true }];
|
|
|
|
let ownerFactory: MockInstance<typeof pluginTaskRuntime.createPluginRunnerTaskOwner>;
|
|
beforeEach(() => {
|
|
grant.admitted = false; grant.targets = []; backend.options = undefined;
|
|
describeTask.mockClear();
|
|
ownerFactory = vi.spyOn(pluginTaskRuntime, "createPluginRunnerTaskOwner");
|
|
});
|
|
afterEach(() => { release.pending = false; ownerFactory.mockRestore(); vi.unstubAllEnvs(); });
|
|
|
|
describe("Antigravity run start while the profile is pending", () => {
|
|
beforeEach(() => { release.pending = true; });
|
|
|
|
it.each([["plugin Runner-task", pluginTarget], ["Core sandbox", sandboxTarget]] as const)(
|
|
"refuses a %s run without an operator grant before any launch", async (_name, target) => {
|
|
expect(antigravityReleasePending()).toBe(true);
|
|
await expect(runStart(antigravityExecution(), target())).rejects.toThrow(INELIGIBLE);
|
|
expect(describeTask).not.toHaveBeenCalled();
|
|
expect(ownerFactory).not.toHaveBeenCalled();
|
|
expect(backend.options).toBeUndefined();
|
|
});
|
|
|
|
it("admits a granted Core sandbox run past the gate, and still refuses the same grant on a plugin Runner-task target", async () => {
|
|
grant.admitted = true;
|
|
const db = emptyDb();
|
|
await expect(runStart(antigravityExecution(), sandboxTarget(), db)).rejects.toThrow(COORDINATOR_CLAIM);
|
|
expect(db.reads).toEqual(coordinatorReads);
|
|
expect(grant.targets).toContainEqual({ platform: "linux", architecture: "x64" });
|
|
await expect(runStart(antigravityExecution(), pluginTarget())).rejects.toThrow(INELIGIBLE);
|
|
expect(describeTask).not.toHaveBeenCalled();
|
|
expect(ownerFactory).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("Antigravity run start once the profile is released", () => {
|
|
it("passes the gate on a plugin Runner-task target and refuses state saved under another profile version", async () => {
|
|
expect(antigravityReleasePending()).toBe(false);
|
|
const db = emptyDb();
|
|
await expect(runStart(antigravityExecution(), pluginTarget(), db)).rejects.toThrow(COORDINATOR_CLAIM);
|
|
expect(db.reads).toEqual([{ table: "environment_leases", locked: false }, ...coordinatorReads]);
|
|
const older = antigravityExecution();
|
|
(older.provider as { profile: { agentProfileVersion: number } }).profile.agentProfileVersion -= 1;
|
|
const olderDb = emptyDb();
|
|
await expect(runStart(older, pluginTarget(), olderDb)).rejects.toBeInstanceOf(NativeProviderStateIncompatibleError);
|
|
expect(olderDb.reads).toEqual([]);
|
|
});
|
|
|
|
it.each(["api_key", "sign_in"] as const)("hands a %s plugin Runner task only its typed credential", async method => {
|
|
const directory = await realpath(await mkdtemp(join(tmpdir(), "antigravity-run-start-")));
|
|
vi.stubEnv("PAPERCLIP_RUNNER_STATE_DIR", join(directory, "runner"));
|
|
const managedHome = join(directory, "managed-home");
|
|
await mkdir(join(managedHome, "antigravity-acp"), { recursive: true, mode: 0o700 });
|
|
const signIn = JSON.stringify({
|
|
client_id: "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com", client_secret: "fixture-client-secret",
|
|
refresh_token: "fixture-google-refresh", 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"],
|
|
});
|
|
await writeFile(join(managedHome, "antigravity-acp", "acp_token.json"), signIn, { mode: 0o600 });
|
|
const owner = { launcher: vi.fn(), registration: vi.fn(), readWorkspaceFile: vi.fn(), isRetired: () => false, rebind: vi.fn(),
|
|
driver: { authorityRunId: "run", prepareLaunch: vi.fn(), runnerProcessLauncher: vi.fn(), controlPlaneRegistration: vi.fn() } };
|
|
ownerFactory.mockReturnValue(owner as never);
|
|
const handoff = vi.spyOn(pluginCredentialWriteback, "registerPluginTaskCredentialHandoff").mockResolvedValue(undefined as never);
|
|
try {
|
|
await createRunnerdBackend({ db: emptyDb(), execution: antigravityExecution(), runnerInstanceId: "antigravity-run-start",
|
|
runnerIngressAuthorized: true, managedAiCredentialHome: managedHome, managedAiCredentialIdentity: "revision",
|
|
managedAiAccountIdentity: "account",
|
|
...(method === "sign_in" ? { managedAiCredentialWriteback: { grantId: "10000000-0000-4000-8000-0000000000a1",
|
|
secretId: "10000000-0000-4000-8000-0000000000a2", sessionEpoch: 1, versionSelector: "latest" as const,
|
|
google: { credentialSha256: "c".repeat(64) } } } : {}),
|
|
runnerEnvironment: { ...(method === "api_key" ? { GEMINI_API_KEY: "fixture-gemini" } : {}),
|
|
GOOGLE_API_KEY: "ambient-google", GEMINI_HOME: "/ambient" },
|
|
runnerExecutionTarget: pluginTarget() });
|
|
await backend.options!.codexTransportFactory!();
|
|
expect(ownerFactory).toHaveBeenCalledOnce();
|
|
expect(ownerFactory).toHaveBeenCalledWith(expect.objectContaining({ credential: method === "api_key"
|
|
? { type: "provider-env", provider: "antigravity", environment: { GEMINI_API_KEY: "fixture-gemini" } }
|
|
: { type: "auth-file", provider: "antigravity", content: signIn } }));
|
|
expect(handoff).toHaveBeenCalledTimes(method === "sign_in" ? 1 : 0);
|
|
} finally {
|
|
handoff.mockRestore();
|
|
await rm(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|