Files
PaperClipAI/server/src/services/native-runtime/plugin-runner-credentials.test.ts
T
ab099dc379 feat(plugin-sdk): declare supported adapters per environment driver (#22827)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Environment plugins can run agent tasks on remote computers, and can
run the provider sign-in for those computers
> - Core decides which adapters can sign in on a plugin environment from
a fixed Claude and Codex rule and a `supportsCodexLogin` flag
> - A plugin cannot say which adapters it supports, so Grok has no
sign-in on a plugin computer, and a plugin can run an adapter that it
cannot sign in
> - This pull request lets a Runner task driver declare its supported
adapters, and makes support mean the full adapter lifecycle: runs and,
where Paperclip offers one, sign-in
> - The benefit is one extensible support contract per driver, with no
partial matrix, and Grok subscription sign-in on plugin computers

## Linked Issues or Issue Description

**Subsystem affected**

Plugin SDK environment drivers, plugin sign-in (login pseudo-terminal),
plugin Runner task credentials, and agent Runner selection.

**Problem or motivation**

Core allows plugin-computer sign-in only for Claude, and for Codex when
the plugin can read back a credential file. The flag for that is named
`supportsCodexLogin`, which does not describe what it does. A plugin
cannot advertise which adapters it supports. Grok has a device sign-in
in Core, but it has no sign-in on a plugin computer, and a Grok
subscription cannot reach a plugin task. A plugin can also run an
adapter that has no sign-in on that plugin. This gives a partial support
matrix.

**Proposed solution**

A Runner task driver declares `supportedAdapters`. Support for an
adapter means the full lifecycle. The driver must run the adapter, and
it must run the Paperclip sign-in when the adapter has one. Core
validates the declaration in the manifest, and Core checks it again
against the methods of the running worker. An adapter that fails either
check is not offered for sign-in, for save, or for runs. The sign-in
contract moves to one shared map, so a new adapter sign-in is one entry
in that map.

**Alternatives considered**

A separate per-capability table (runs, sign-in, credential read-back)
was rejected. It permits the partial matrix that this change removes.
The existing sign-in map and credential sets already contain the
necessary data.

**Additional context**

Related open pull requests: Refs #14001 (Kubernetes plugin login
pseudo-terminal), Refs #21327 (Grok device link in the sign-in UI).

## What Changed

- Environment drivers can declare `supportedAdapters`. The manifest is
rejected when the list has duplicates, when the driver is not a Runner
task driver, or when it lists an adapter that has a Paperclip sign-in
and the driver does not declare the login pseudo-terminal with
`creationOperationVersion: 1`.
- Core removes a listed adapter when the running worker does not
implement the methods it needs (task methods, and sign-in methods with
credential read-back for file sign-ins). Core logs a warning one time
for each removed adapter.
- `supportsCodexLogin` is replaced by `loginAdapters`, the adapters that
can sign in on that environment. Drivers without `supportedAdapters`
keep the current behaviour: Claude, and Codex when the worker reads back
credentials.
- A new shared sign-in map holds the adapters that Paperclip signs in
and where each sign-in leaves its credential (terminal output or a
file). Manifest validation, server admission, environment presentation,
and the Connections sign-in step use this map. Two hard-coded Claude and
Codex lists are removed.
- Credential read-back accepts any file sign-in key and returns `{ kind:
"auth-file", content }`.
- The plugin task credential `codex-auth-json` becomes `{ type:
"auth-file", provider: "codex" | "grok", content }`.
- A Grok subscription is delivered to plugin tasks as an `auth-file`
credential. A refreshed Grok file returns to the account through the
existing write-back. The write-back record now stores its provider.
- On a driver that lists adapters, Core refuses to save, offer, or
submit a Runner harness that is not on the list. The error is
`agent_runner_unavailable`.
- The SDK README and the plugin environment task and AI connection docs
describe the contract.

## Verification

- `pnpm --filter @paperclipai/server typecheck`, `pnpm --filter
@paperclipai/plugin-sdk typecheck`, and `pnpm --filter @paperclipai/ui
typecheck` pass.
- Targeted server tests pass: plugin environment driver capabilities,
plugin login credential, plugin Runner credentials, plugin task
credential write-back (Codex and Grok, with a provider mismatch case),
environment task runtime submit gating, agent Runner selection, login
environment, login session runtime, device login routes and service,
setup token, login command, and adapter routes.
- Plugin Runner probe and integration tests pass (run from the
repository root).
- Shared tests (77) and plugin SDK tests (129) pass, including the new
manifest validation cases.
- UI AI connection tests (67) pass.
- Not run locally: the full suite (CI runs it), a render test for the
Connections credential step, and an end-to-end route test for computer
device sign-in admission. Unit tests cover that admission.

## Risks

- Breaking change for plugins that use the plugin SDK task credential or
credential read-back. `codex-auth-json` becomes `auth-file`, and the
read-back result kind changes. A plugin must update to the new SDK
shapes in the same release.
- Plugins that do not declare `supportedAdapters` keep the current
behaviour. No warning is logged for them.
- `supportedAdapters` applies to Runner task drivers only. Sandbox
providers reject the field. Support for sandbox providers is a separate
change.
- The new-agent screen does not offer sign-in on a plugin computer. That
sign-in needs a target computer, and the Connections flow selects it.
This is the current behaviour for Claude and Codex.
- No migration. The write-back record treats a missing provider as
Codex, so records written before this change still work.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, file edits, test runs) 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
- [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 Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-12-01 08:13:39 -08:00

182 lines
14 KiB
TypeScript

import { mkdir, 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: "auth-file", provider: "codex", content: JSON.stringify(auth) });
const refreshed = { ...auth, tokens: { ...auth.tokens, refresh_token: "fixture-rotated" }, last_refresh: "2021-01-01T00:00:00Z" };
await transfer.onCredentials!({ type: "auth-file", provider: "codex", content: 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: "auth-file", provider: "codex", content: 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 = (extra: Record<string, unknown> = {}) => JSON.stringify({
client_id: "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com", client_secret: "fixture-client-secret",
refresh_token: "fixture-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"],
...extra,
});
async function googleHome(contents: string) {
const directory = await mkdtemp(join(tmpdir(), "plugin-runner-google-")); homes.push(directory);
await mkdir(join(directory, "antigravity-acp"), { mode: 0o700 });
await writeFile(join(directory, "antigravity-acp", "acp_token.json"), contents, { mode: 0o600 });
return directory;
}
const blankManagedEnvironment = { GEMINI_API_KEY: "", GOOGLE_API_KEY: "", PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: "",
GEMINI_HOME: "/controller/private/provider", HOME: "/controller/private", OTHER_SECRET: "fixture-unrelated" };
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([
["the required scopes", signIn()],
["the optional project and OpenID scope", signIn({ project_id: "fixture-project",
scopes: ["https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/aicode", "openid"] })],
])("transfers the saved Google sign-in with %s exactly as the sanitizer outputs it", async (_name, saved) => {
const directory = await googleHome(JSON.stringify({ ...JSON.parse(saved), access_token: "fixture-access", expiry: "2020-01-01T00:00:00Z", email: "alice@example.test" }));
const transfer = await preparePluginRunnerCredentials({ provider: "antigravity", identity: "revision", accountIdentity: "account",
home: directory, environment: blankManagedEnvironment });
expect(transfer).toEqual({ accountIdentity: "account", needsWriteback: true,
credential: { type: "provider-env", provider: "antigravity", environment: { PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: saved } } });
const delivered = transfer.credential!.type === "provider-env" ? transfer.credential!.environment.PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET : "";
expect(delivered).not.toContain("fixture-access");
expect(delivered).not.toContain("alice@example.test");
expect(transfer.onCredentials).toBeUndefined();
});
it("prefers a selected Gemini API key over a managed home", async () => {
const directory = await googleHome(signIn());
const transfer = await preparePluginRunnerCredentials({ provider: "antigravity", identity: "revision", home: directory,
environment: { ...blankManagedEnvironment, GEMINI_API_KEY: "fixture-gemini" } });
expect(transfer.credential).toEqual({ type: "provider-env", provider: "antigravity", environment: { GEMINI_API_KEY: "fixture-gemini" } });
expect(transfer.needsWriteback).toBeUndefined();
});
it.each([
["a sign-in supplied through the environment", { PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: "{\"refresh_token\":\"fixture-refresh\"}" }, "selected credential unavailable"],
["a sign-in beside a Gemini API key", { GEMINI_API_KEY: "fixture-gemini", PAPERCLIP_ACPX_ANTIGRAVITY_AUTH_JSON_SECRET: "{\"refresh_token\":\"fixture-refresh\"}" }, "selected credential unavailable"],
["no key and no saved sign-in", {}, "Gemini API key or a saved Google sign-in"],
["a blank key and no saved sign-in", { GEMINI_API_KEY: " " }, "Gemini API key or a saved Google sign-in"],
["only a Google API key", { GOOGLE_API_KEY: "fixture-google" }, "Gemini API key or a saved Google sign-in"],
] as const)("refuses %s", async (_name, environment, message) => {
const result = preparePluginRunnerCredentials({ provider: "antigravity", identity: "revision", environment });
await expect(result).rejects.toThrow(message);
await result.catch((error: Error) => expect(error.message).not.toContain("fixture"));
});
it.each([
["a missing token file", null],
["a token for another OAuth client", JSON.stringify({ ...JSON.parse(signIn()), client_id: "fixture-other-client" })],
["a token without the required scopes", JSON.stringify({ ...JSON.parse(signIn()), scopes: [] })],
["unreadable JSON", "{fixture-refresh"],
])("refuses %s in plain words without echoing it", async (_name, contents) => {
const directory = contents === null ? await mkdtemp(join(tmpdir(), "plugin-runner-google-")) : await googleHome(contents);
if (contents === null) homes.push(directory);
const result = preparePluginRunnerCredentials({ provider: "antigravity", identity: "revision", home: directory, environment: blankManagedEnvironment });
await expect(result).rejects.toThrow("The selected Google Antigravity account is unavailable. Reconnect the Google account, then retry.");
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("sends a Grok subscription as its saved sign-in file and requires the managed writeback", async () => {
const grokAuth = { "https://auth.x.ai::00000000-0000-4000-8000-000000000000": { key: "fixture-key", refresh_token: "fixture-refresh" } };
const directory = await home(grokAuth);
const transfer = await preparePluginRunnerCredentials({ provider: "grok", identity: "revision", accountIdentity: "account",
home: directory, environment: blanks });
expect(transfer).toEqual({ accountIdentity: "account", needsWriteback: true,
credential: { type: "auth-file", provider: "grok", content: JSON.stringify(grokAuth) } });
});
it("refuses a Grok subscription without a usable sign-in file", async () => {
await expect(preparePluginRunnerCredentials({ provider: "grok", identity: "revision", environment: blanks }))
.rejects.toThrow("credential home unavailable");
const directory = await home({ "https://auth.x.ai::00000000-0000-4000-8000-000000000000": { key: "fixture-key" } });
await expect(preparePluginRunnerCredentials({ provider: "grok", identity: "revision", home: directory, environment: blanks }))
.rejects.toThrow("credential invalid");
});
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"}});
});