Files
PaperClipAI/server/src/services/native-runtime/plugin-runner-credentials.test.ts
T
DottaandClaude Opus 5.5 07d444a717 feat(plugins): carry a Gemini API key to plugin Runners for Google Antigravity (#22930)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Plugin environments (for example, an agent computer) run agents as
plugin Runner tasks. Core sends each task one typed credential.
> - The typed `provider-env` credential carries API keys for Grok,
Copilot and OpenCode. It has no entry for Google Antigravity.
> - So an Antigravity agent cannot get its Gemini API key on a plugin
Runner task. This is gap G3 and work item 1 in the Antigravity Cloud
rollout plan (#22923).
> - This pull request adds `antigravity` to `provider-env`, with the
single variable set `GEMINI_API_KEY`. It follows the Copilot pattern
from #22359.
> - The benefit is that a plugin (for example, the Paperclip Cloud agent
computer) can now accept a Gemini API key for an Antigravity task.
Google sign-in, the plugin-target refusal and the qualification flip
stay out of scope.

## Linked Issues or Issue Description

Refs #22923
Refs #22359

**What problem does this solve?**
A plugin Runner task cannot carry a Gemini API key. `provider-env`
accepts only `grok`, `copilot` and `opencode`, and Core throws "Plugin
Runner does not support the selected credential provider" for
Antigravity.

**Proposed solution**
Add `antigravity: [["GEMINI_API_KEY"]]` to the published key sets and to
the `provider-env` provider enum. Core transfers only that key. Core
refuses a Google sign-in or a missing key with a plain message.

**Alternatives considered**
A new credential type only for Gemini. The generic `provider-env` type
already has the exact-set check, the binding contract and the
cross-check against the Runner's allowlists, so a new type adds no
value.

## What Changed

- `packages/plugins/sdk/src/environment-tasks.ts`:
`ENVIRONMENT_TASK_PROVIDER_CREDENTIAL_KEY_SETS` has `antigravity:
[["GEMINI_API_KEY"]]`. The `provider-env` provider enum accepts
`antigravity`. Mixed, partial and extra variable sets stay rejected.
- `server/src/services/native-runtime/plugin-runner-credentials.ts`: for
provider `antigravity`, Core sends only `GEMINI_API_KEY`. Core refuses a
Google sign-in document (`PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET`)
or a missing or blank key with "Plugin Runner supports Google
Antigravity only with a Gemini API key. Google sign-in is not
supported." No key value is in any error message.
- Tests: SDK schema cases (accept the key; reject a sign-in document, a
key mixed with a sign-in document, `GOOGLE_API_KEY`, extra routing
variables, empty values, and the key under another provider). Core
transfer and refusal cases. Dispatch only for the matching `antigravity`
harness, with no key in persisted lease metadata or in a worker error. A
Runner check that the Antigravity process gets the key and no `GOOGLE_*`
or other `GEMINI_*` variable.
- Docs: `doc/plugins/PLUGIN_ENVIRONMENT_TASKS.md` and
`doc/plugins/PLUGIN_RUNNER_EXECUTION.md` list the new set, the binding
requirement and the sign-in refusal.
- `tests/runner-e2e/source-sdk-public-inputs.json`: refreshed for the
changed SDK source.

## Verification

- Red first: the new tests failed before the change (the schema rejected
`antigravity`, and Core threw "does not support the selected credential
provider").
- Green after the change:
  - `cd packages/plugins/sdk && npx vitest run tests/` (322 passed)
- `cd server && npx vitest run
src/__tests__/plugin-runner-credential-names.test.ts
src/services/native-runtime/plugin-runner-credentials.test.ts
src/__tests__/environment-task-runtime.test.ts` (94 passed)
- `cd packages/plugins/sdk && npx tsc --noEmit -p .` and `cd server &&
npx tsc --noEmit -p .` (clean)
- `node tests/runner-e2e/source-sdk-public-inputs.mjs <HEAD> --check`
(no change)
- `pnpm verify` asks for broad CI coverage (exit 2) because an SDK test
file changed. Required CI supplies that coverage.
- No live run. Antigravity on a plugin target stays refused by Core
until a later change lifts that refusal, so this change is not reachable
in a run yet.

## Risks

- Low risk. The change adds one enum value and one key set. Existing
providers keep the same behavior.
- Plugins that validate with an older SDK reject an `antigravity`
credential. This is the expected fail-closed result.
- The key reaches the Runner only through the typed credential. The
Runner still requires a matching `PAPERCLIP_ACPX_CREDENTIAL_BINDING` and
still removes other `GOOGLE_*` and `GEMINI_*` variables.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, file edits) and extended thinking.

## 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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 07:03:49 -06:00

125 lines
9.5 KiB
TypeScript

import { mkdtemp, writeFile, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { preparePluginRunnerCredentials } from "./plugin-runner-credentials.js";
const homes: string[] = [];
afterEach(async () => { await Promise.all(homes.splice(0).map(home => rm(home, { recursive: true, force: true }))); });
async function home(auth: object) {
const directory = await mkdtemp(join(tmpdir(), "plugin-runner-account-")); homes.push(directory);
await writeFile(join(directory, "auth.json"), JSON.stringify(auth), { mode: 0o600 });
return directory;
}
const auth = { tokens: { account_id: "fixture-account", access_token: "fixture-access", refresh_token: "fixture-refresh" }, last_refresh: "2020-01-01T00:00:00Z" };
describe("plugin Runner selected credentials", () => {
it("sends the selected Codex account and preserves a newer refresh for normal secret writeback", async () => {
const directory = await home(auth);
const transfer = await preparePluginRunnerCredentials({ provider: "codex", identity: "revision", accountIdentity: "account", home: directory, environment: {} });
expect(transfer.accountIdentity).toBe("account");
expect(transfer.needsWriteback).toBe(true);
expect(transfer.credential).toEqual({ type: "codex-auth-json", authJson: JSON.stringify(auth) });
const refreshed = { ...auth, tokens: { ...auth.tokens, refresh_token: "fixture-rotated" }, last_refresh: "2021-01-01T00:00:00Z" };
await transfer.onCredentials!({ type: "codex-auth-json", authJson: JSON.stringify(refreshed) });
expect(JSON.parse(await readFile(join(directory, "auth.json"), "utf8"))).toEqual(refreshed);
});
it("transfers a selected API key without requiring refresh authority", async () => {
const directory = await home({ OPENAI_API_KEY: "fixture-key" });
const transfer = await preparePluginRunnerCredentials({ provider: "codex", identity: "revision", accountIdentity: "account", home: directory, environment: {} });
expect(transfer.needsWriteback).toBe(false);
expect(transfer.credential).toEqual({ type: "codex-auth-json", authJson: JSON.stringify({ OPENAI_API_KEY: "fixture-key" }) });
});
it("transfers only the selected Claude authentication variable", async () => {
const transfer = await preparePluginRunnerCredentials({ provider: "claude", identity: "revision", environment: { CLAUDE_CODE_OAUTH_TOKEN: "fixture-oauth", HOME: "/controller/private", OTHER_SECRET: "fixture-unrelated" } });
expect(transfer.credential).toEqual({ type: "claude-env", environment: { CLAUDE_CODE_OAUTH_TOKEN: "fixture-oauth" } });
await expect(preparePluginRunnerCredentials({ provider: "claude", identity: "revision", environment: { ANTHROPIC_API_KEY: "fixture-key", ANTHROPIC_BASE_URL: "https://router.example.test" } })).rejects.toThrow("routing");
});
it("does not read a controller credential when the provider owns the account", async () => {
expect(await preparePluginRunnerCredentials({ provider: "codex", home: "/must/not/read", environment: {} })).toEqual({});
await expect(preparePluginRunnerCredentials({ provider: "cursor", identity: "revision", environment: { CURSOR_API_KEY: "fixture-key" } })).rejects.toThrow("credential provider");
});
});
describe("plugin Runner Google Antigravity credentials", () => {
const signIn = "{\"refresh_token\":\"fixture-refresh\"}";
it("transfers only the selected Gemini API key", async () => {
const transfer = await preparePluginRunnerCredentials({ provider: "antigravity", identity: "revision", accountIdentity: "account",
environment: { GEMINI_API_KEY: "fixture-gemini", GOOGLE_API_KEY: "fixture-google", GOOGLE_APPLICATION_CREDENTIALS: "/controller/private/adc.json",
GOOGLE_GEMINI_BASE_URL: "https://router.example.test", GEMINI_HOME: "/controller/private/gemini", XAI_API_KEY: "fixture-xai",
HOME: "/controller/private", OTHER_SECRET: "fixture-unrelated" } });
expect(transfer).toEqual({ accountIdentity: "account", credential: { type: "provider-env", provider: "antigravity", environment: { GEMINI_API_KEY: "fixture-gemini" } } });
expect(transfer.needsWriteback).toBeUndefined();
expect(transfer.onCredentials).toBeUndefined();
});
it.each([
["a Google sign-in", { PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: signIn }],
["a Google sign-in beside a Gemini API key", { GEMINI_API_KEY: "fixture-gemini", PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: signIn }],
["no key", {}],
["an empty key", { GEMINI_API_KEY: "" }],
["a blank key", { GEMINI_API_KEY: " " }],
["only a Google API key", { GOOGLE_API_KEY: "fixture-google" }],
] as const)("refuses %s because only a Gemini API key can be transferred", async (_name, environment) => {
const result = preparePluginRunnerCredentials({ provider: "antigravity", identity: "revision", environment });
await expect(result).rejects.toThrow("Google Antigravity only with a Gemini API key");
await result.catch((error: Error) => expect(error.message).not.toContain("fixture"));
});
it("fails closed for an invalid Gemini API key without echoing it", async () => {
const result = preparePluginRunnerCredentials({ provider: "antigravity", identity: "revision", environment: { GEMINI_API_KEY: "fixture\0gemini" } });
await expect(result).rejects.toThrow("Plugin Runner selected credential unavailable");
await result.catch((error: Error) => expect(error.message).not.toContain("fixture"));
});
});
describe("plugin Runner provider environment credentials", () => {
const blanks = { XAI_API_KEY: "", COPILOT_GITHUB_TOKEN: "", OPENROUTER_API_KEY: "", PAPERCLIP_AI_PROVIDER_URL: "", PAPERCLIP_AI_PROVIDER_KEY: "",
ANTHROPIC_API_KEY: "", HOME: "/controller/private", GROK_HOME: "/controller/private/provider", OTHER_SECRET: "fixture-unrelated" };
it.each([
["grok", { XAI_API_KEY: "fixture-xai" }],
["copilot", { COPILOT_GITHUB_TOKEN: "fixture-copilot" }],
["opencode", { OPENROUTER_API_KEY: "fixture-openrouter" }],
["opencode", { PAPERCLIP_AI_PROVIDER_URL: "https://gateway.example.test/v1", PAPERCLIP_AI_PROVIDER_KEY: "fixture-gateway" }],
] as const)("transfers only the selected %s variables", async (provider, environment) => {
const transfer = await preparePluginRunnerCredentials({ provider, identity: "revision", accountIdentity: "account",
environment: { ...blanks, ...environment, OPENCODE_CONFIG_CONTENT: "{\"provider\":{}}" } });
expect(transfer).toEqual({ accountIdentity: "account", credential: { type: "provider-env", provider, environment } });
expect(transfer.needsWriteback).toBeUndefined();
});
it("uses the credential revision as the account when no account identity is supplied", async () => {
const transfer = await preparePluginRunnerCredentials({ provider: "copilot", identity: "revision", environment: { COPILOT_GITHUB_TOKEN: "fixture-copilot" } });
expect(transfer.accountIdentity).toBe("revision");
});
it("keeps the GitHub broker authority beside the provider credential", async () => {
const transfer = await preparePluginRunnerCredentials({ provider: "grok", identity: "revision", environment: {
XAI_API_KEY: "fixture-xai", PAPERCLIP_GITHUB_BROKER_URL: "https://core.example.test", PAPERCLIP_GITHUB_BROKER_TOKEN: "runtime-capability" } });
expect(transfer.githubBroker).toEqual({ url: "https://core.example.test", token: "runtime-capability" });
expect(transfer.credential).toEqual({ type: "provider-env", provider: "grok", environment: { XAI_API_KEY: "fixture-xai" } });
});
it("refuses a Grok subscription, which needs an auth file with refresh and writeback", async () => {
await expect(preparePluginRunnerCredentials({ provider: "grok", identity: "revision", home: "/controller/private/provider",
environment: blanks })).rejects.toThrow("Grok subscription login is not supported");
});
it("refuses Grok provider routing that would redirect the key", async () => {
await expect(preparePluginRunnerCredentials({ provider: "grok", identity: "revision",
environment: { XAI_API_KEY: "fixture-xai", XAI_BASE_URL: "https://router.example.test" } })).rejects.toThrow("routing");
});
it.each([
["copilot", {}],
["opencode", {}],
["opencode", { PAPERCLIP_AI_PROVIDER_URL: "https://gateway.example.test/v1" }],
["opencode", { PAPERCLIP_AI_PROVIDER_KEY: "fixture-gateway" }],
["opencode", { OPENROUTER_API_KEY: "fixture-openrouter", PAPERCLIP_AI_PROVIDER_URL: "https://gateway.example.test/v1", PAPERCLIP_AI_PROVIDER_KEY: "fixture-gateway" }],
["opencode", { OPENROUTER_API_KEY: "fixture\0value" }],
] as const)("fails closed for an incomplete or ambiguous %s credential", async (provider, environment) => {
const result = preparePluginRunnerCredentials({ provider, identity: "revision", environment: { ...blanks, ...environment } });
await expect(result).rejects.toThrow("Plugin Runner selected credential unavailable");
await result.catch((error: Error) => expect(error.message).not.toContain("fixture"));
});
});
it("transfers only broker authority even when the provider owns its AI account", async () => {
const result = await preparePluginRunnerCredentials({provider:"codex",home:"/must/not/read",environment:{
PAPERCLIP_GITHUB_BROKER_URL:"https://core.example.test",PAPERCLIP_GITHUB_BROKER_TOKEN:"runtime-capability",
GH_TOKEN:"ambient-secret",PAPERCLIP_API_KEY:"unrelated-key",HOME:"/controller/private"}});
expect(result).toEqual({githubBroker:{url:"https://core.example.test",token:"runtime-capability"}});
});