Files
PaperClipAI/server/src/services/native-runtime/plugin-runner-task.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

1188 lines
77 KiB
TypeScript

import { createHash } from "node:crypto";
import { describe, expect, it, vi } from "vitest";
import type { RunnerProviderLaunchRequirements } from "@paperclipai/paperclip-runner";
import { runnerLaunchPreparationDigest, runnerLaunchRunDigest, type RunnerProcessLaunchSpec } from "../../vendor/paperclip-runner/index.js";
import { createPluginRunnerTaskOwner } from "./plugin-runner-task.js";
import { NativeExecutionContext } from "./native-execution-context.js";
const ingress = vi.hoisted(() => ({ connect: vi.fn() }));
vi.mock("../../realtime/runner-prp-outbound.js", () => ({ connectRunnerPrpIngress: ingress.connect }));
function launchSpec(): RunnerProcessLaunchSpec {
return { command: "unused", args: [], cwd: "/unused", environment: {}, nativeRunner: {
identity: { runnerInstanceId: "runner", environmentLeaseId: "lease", runId: "run",
normalizedSessionId: "session", turnId: "turn", itemId: "item" }, ticket: "synthetic-ticket",
diagnosticsDirectory: "/controller/private/diagnostics",
stateDirectory: "/state", runnerVersion: "fixture", runnerDigest: `sha256:${"a".repeat(64)}`,
maxOutboxBytes: 262144, p0ReserveBytes: 65536, lifecyclePolicy: { mode: "per_turn", idleTimeoutMs: null },
} };
}
const requirements: RunnerProviderLaunchRequirements = {
schema: "paperclip.runner.launch-requirements.v1", identity: launchSpec().nativeRunner!.identity,
provider: "codex", model: "fixture", instructions: "Core policy", runtimeContext: null,
settings: {}, approvalPolicy: "never", readOnly: false, preparedConversation: false,
includeCollaborationModeInstructions: false,
};
function launchRequirements(harness: string): RunnerProviderLaunchRequirements {
return harness === "claude" ? { ...requirements, provider: "acpx", settings: { acpxAgent: "claude" } } : requirements;
}
async function prepare(owner: ReturnType<typeof createPluginRunnerTaskOwner>, current = requirements) {
await owner.driver.prepareLaunch!(current);
}
function provider() {
let stopped = false;
const task = vi.fn(async (operation: any): Promise<unknown> => {
if (operation.kind === "check_project_auth") return { kind: "project_auth", taskId: "task", allowed: true,
provider: operation.provider, configurationId: operation.configurationId };
if (operation.kind === "prepare") return { kind: "prepared", taskId: "task", launch: {
configurationId: "configuration", requirementsDigest: `sha256:${"a".repeat(64)}`, provider: { kind: "codex" },
} };
if (operation.kind === "read_state") return { kind: "state", taskId: "task", state: {
schema: "paperclip.runner.durable.state.v1", ...operation.identity, lifecycle: "suspended",
} };
if (operation.kind === "stop") stopped = true;
if (operation.kind === "status") return { kind: "status", taskId: "task", phase: stopped ? "cancelled" : "running", executionStopped: stopped, sessionStateAvailable: stopped };
if (operation.kind === "connection") return { kind: "connection", taskId: "task", endpoint: {
kind: "authenticated_websocket", websocketUrl: "wss://fixture.example.test/connect", generation: "fixture", secretHeaders: [],
} };
return { kind: "accepted", taskId: "task" };
});
return { task };
}
describe("plugin native Runner ownership", () => {
it("requires an explicit physical task binding", () => {
const { task } = provider();
expect(() => createPluginRunnerTaskOwner({ task, projectIds: [], harness: "codex" } as never)).toThrow("physical task unavailable");
});
it("publishes current trusted recovery authority through warm owner rebind", () => {
const { task } = provider();
const input = { taskId: "task", task, projectIds: [], harness: "codex", authorityRunId: "first-owner", codexSourceRecoveryVersion: 1 as const, adoptIdentity: requirements.identity };
const owner = createPluginRunnerTaskOwner(input);
expect(owner.driver.authorityRunId).toBe("first-owner");
expect(owner.driver.codexSourceRecoveryVersion).toBe(1);
owner.rebind({ ...input, authorityRunId: "next-owner", codexSourceRecoveryVersion: undefined });
expect(owner.driver.authorityRunId).toBe("next-owner");
expect(owner.driver.codexSourceRecoveryVersion).toBeUndefined();
});
it.each(["capability", "source", "identity", "account", "provider", "adoption"])("refuses an unqualified predecessor preparation: %s", async mode => {
const { task } = provider();
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: mode === "provider" ? "claude" : "codex",
authorityRunId: "next", codexSourceRecoveryVersion: mode === "capability" ? undefined : 1,
resumeFrom: mode === "source" ? undefined : "source-task", priorIdentity: mode === "identity" ? undefined : requirements.identity,
accountIdentity: mode === "account" ? undefined : "selected-account", ...(mode === "adoption" ? { adoptIdentity: requirements.identity } : {}),
});
await expect(prepare(owner)).rejects.toThrow("source reconciliation unavailable");
expect(task).not.toHaveBeenCalled();
});
it("reuses exact adopted A preparation under current B without a source launch", async () => {
const { task } = provider();
const context = new NativeExecutionContext({ binding: { companyId: "company", agentId: "agent", runId: "next" } } as never, 1_000);
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex", projectAuthCheckVersion: 1,
executionContext: context, adoptIdentity: requirements.identity });
const recovery = { configurationId: "retained-configuration", requirementsDigest: `sha256:${"a".repeat(64)}` as const, provider: { kind: "codex" } };
try {
await expect(owner.driver.prepareLaunch!(requirements, recovery)).resolves.toEqual(recovery);
expect(task.mock.calls.map(([operation]) => operation.kind)).toEqual(["check_project_auth"]);
} finally { context.finish(); }
});
it.each(["same-run", "suspended-predecessor"])("keeps ordinary %s preparation independent of predecessor capability", async mode => {
const { task } = provider();
const identity = mode === "same-run" ? requirements.identity : { ...requirements.identity, runId: "next" };
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex", projectAuthCheckVersion: 1,
authorityRunId: identity.runId, priorIdentity: requirements.identity, resumeFrom: "source-task" });
await prepare(owner, { ...requirements, identity });
expect(task.mock.calls[0][0]).toMatchObject({ kind: "prepare", resumeFrom: "source-task", requirements: { identity } });
expect(task.mock.calls[0][0]).not.toHaveProperty("recoveryPurpose");
});
it("keeps the resume parent on ordinary repeated preparation after launch", async () => {
const { task } = provider();
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex", projectAuthCheckVersion: 1, pollMs: 1,
authorityRunId: "run", resumeFrom: "source-task", priorIdentity: requirements.identity });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
try {
owner.launcher(launchSpec());
await vi.waitFor(() => expect(task.mock.calls.some(([op]) => op.kind === "submit")).toBe(true));
await prepare(owner);
expect(task.mock.calls.map(([op]) => op).filter(op => op.kind === "prepare").at(-1)).toMatchObject({ resumeFrom: "source-task" });
} finally { await registration.release("detach"); }
});
it("prepares and submits exact A, then prepares B without copying or reconciling A again", async () => {
const { task } = provider();
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex", projectAuthCheckVersion: 1, pollMs: 1,
authorityRunId: "next", codexSourceRecoveryVersion: 1, resumeFrom: "source-task", priorIdentity: requirements.identity,
accountIdentity: "selected-account" });
await expect(prepare(owner, { ...requirements, identity: { ...requirements.identity, runId: "foreign" } })).rejects.toThrow("source reconciliation unavailable");
await prepare(owner);
const registration = await owner.registration({} as never, "run");
try {
owner.launcher(launchSpec());
await vi.waitFor(() => expect(task.mock.calls.some(([op]) => op.kind === "submit")).toBe(true));
const initial = task.mock.calls.map(([op]) => op).filter(op => ["prepare", "submit"].includes(op.kind));
expect(initial).toEqual([expect.objectContaining({ kind: "prepare", resumeFrom: "source-task", requirements: expect.objectContaining({ identity: requirements.identity }) }),
expect.objectContaining({ kind: "submit", resumeFrom: "source-task", runner: expect.objectContaining({ runId: "run", turnId: "turn", itemId: "item" }) })]);
await prepare(owner);
expect(task.mock.calls.map(([op]) => op).filter(op => op.kind === "prepare").at(-1)).toMatchObject({ resumeFrom: "source-task" });
await prepare(owner, { ...requirements, identity: { ...requirements.identity, runId: "next", turnId: "next-turn", itemId: "next-item" } });
const attached = task.mock.calls.map(([op]) => op).filter(op => op.kind === "prepare").at(-1);
expect(attached).toMatchObject({ priorIdentity: requirements.identity, requirements: { identity: { runId: "next" } } });
expect(attached).not.toHaveProperty("recoveryPurpose"); expect(attached).not.toHaveProperty("resumeFrom");
} finally { await registration.release("detach"); }
});
it.each(["finish", "rebind"])("does not send startup Stop after the context %s", mode => {
const context = new NativeExecutionContext({ binding: { companyId: "company", agentId: "agent", runId: "run" } } as never, 1_000);
const next = new NativeExecutionContext(context.execution, 1_000);
const remove = vi.spyOn(context.signal, "removeEventListener");
const { task } = provider();
const input = { taskId: "task", task, projectIds: [], harness: "codex", executionContext: context, adoptIdentity: requirements.identity };
const owner = createPluginRunnerTaskOwner(input);
try {
if (mode === "finish") context.finish();
else { owner.rebind({ ...input, executionContext: next }); expect(remove).toHaveBeenCalledWith("abort", expect.any(Function)); }
context.abort(new Error("later lease loss"));
expect(task).not.toHaveBeenCalled();
} finally { context.finish(); next.finish(); }
});
it("rechecks revoked grants for unchanged project IDs before adopting retained resources", async () => {
const { task } = provider();
const perform = task.getMockImplementation()!;
let allowed = true;
task.mockImplementation(async op => op.kind === "check_project_auth"
? { ...await perform(op) as object, allowed } : perform(op));
const input = { taskId: "task", task, projectIds: ["project-retained"], harness: "codex", projectAuthCheckVersion: 1 as const,
adoptIdentity: requirements.identity };
const owner = createPluginRunnerTaskOwner(input);
await owner.driver.authorizeRunnerAdoption!("retained-configuration", requirements.identity);
allowed = false;
owner.rebind({ ...input });
await expect(owner.driver.authorizeRunnerAdoption!("retained-configuration", requirements.identity)).rejects.toThrow("project authentication conflicts");
expect(task.mock.calls.map(([op]) => op)).toEqual(Array(2).fill({ kind: "check_project_auth",
configurationId: "retained-configuration", provider: "openai", projectIds: ["project-retained"] }));
});
it.each(["identity", "unsupported", "released", "late-release"])("refuses adoption authorization after %s", async mode => {
const { task } = provider();
const perform = task.getMockImplementation()!;
let finish!: () => void;
const gate = new Promise<void>(resolve => { finish = resolve; });
task.mockImplementation(async op => { if (mode === "late-release" && op.kind === "check_project_auth") await gate; return perform(op); });
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: ["project-current"], harness: "codex",
projectAuthCheckVersion: mode === "unsupported" ? undefined : 1, adoptIdentity: requirements.identity });
const registration = await owner.registration({} as never, "run");
if (mode === "released") await registration.release("detach");
const attempt = owner.driver.authorizeRunnerAdoption!("retained-configuration",
mode === "identity" ? { ...requirements.identity, runId: "unrelated" } : requirements.identity);
const rejection = expect(attempt).rejects.toThrow(mode === "identity" ? "identity mismatch"
: mode === "unsupported" ? "validation unavailable" : "preparation cancelled");
if (mode === "late-release") { await registration.release("detach"); finish(); }
await rejection;
await registration.release("detach");
expect(task.mock.calls.map(([op]) => op.kind)).toEqual(mode === "late-release" ? ["check_project_auth"] : []);
});
it("adopts the saved task and detaches without preparing, submitting, or stopping it", async () => {
const { task } = provider();
const close = vi.fn(async () => {});
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close });
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex",
adoptIdentity: requirements.identity });
const registration = await owner.registration({} as never, "run");
registration.activate(); await registration.ready();
expect(await owner.driver.adoptExistingRunner!.isAlive()).toBe(true);
expect(owner.driver.adoptExistingRunner!.pid).toBeNull();
expect(() => owner.launcher(launchSpec())).toThrow("replacement requires a new task lease");
await registration.release("detach");
expect(close).toHaveBeenCalledOnce();
expect(task.mock.calls.map(([op]) => op.kind)).toEqual(["status", "read_state", "connection", "status"]);
});
it.each(["stopped", "wrong-identity", "revoked", "cancelled"])("refuses adoption when the saved task is %s", async mode => {
const { task } = provider();
const perform = task.getMockImplementation()!;
const abort = new AbortController();
task.mockImplementation(async op => {
if (mode === "revoked" && op.kind === "read_state") throw new Error("execution lease is not active");
const result = await perform(op) as any;
if (mode === "stopped" && op.kind === "status") result.executionStopped = true;
if (mode === "wrong-identity" && op.kind === "read_state") result.state.runId = "other-run";
if (mode === "cancelled" && op.kind === "read_state") abort.abort();
return result;
});
ingress.connect.mockClear();
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex",
adoptIdentity: requirements.identity });
const registration = await owner.registration({} as never, "run", abort.signal);
registration.activate();
await expect(registration.ready()).rejects.toThrow();
await registration.release("detach");
expect(ingress.connect).not.toHaveBeenCalled();
expect(task.mock.calls.some(([op]) => ["prepare", "submit", "stop", "complete"].includes(op.kind))).toBe(false);
});
it("settles an adopted task through confirmed termination and retained-session ownership", async () => {
const { task } = provider();
const retained = vi.fn(async () => {});
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex",
adoptIdentity: requirements.identity, onSessionRetained: retained });
const registration = await owner.registration({} as never, "run");
registration.activate(); await registration.ready();
await registration.release(); await registration.checkpoint("settled");
expect(retained).toHaveBeenCalledWith(expect.objectContaining(requirements.identity));
expect(task.mock.calls.map(([op]) => op.kind)).toEqual(["status", "read_state", "connection", "stop", "status", "read_state", "complete"]);
});
it.each(["settled", "unsettled"] as const)("proves retirement independently of %s checkpoint reuse", async settlement => {
const { task } = provider();
const retained = vi.fn(async () => {});
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex",
adoptIdentity: requirements.identity, onSessionRetained: retained });
const registration = await owner.registration({} as never, "run");
expect(owner.isRetired()).toBe(false);
await registration.release();
expect(owner.isRetired()).toBe(false);
await registration.checkpoint(settlement);
expect(owner.isRetired()).toBe(true);
expect(retained).toHaveBeenCalledTimes(settlement === "settled" ? 1 : 0);
expect(task.mock.calls.map(([op]) => op.kind)).toEqual(settlement === "settled"
? ["stop", "status", "read_state", "complete"] : ["stop", "status", "complete"]);
});
it.each(["credentials", "read_state", "complete"])("does not prove retirement when %s obligations fail after execution stops", async failingOperation => {
const { task } = provider();
const perform = task.getMockImplementation()!;
task.mockImplementation(async operation => {
if (operation.kind === failingOperation) throw new Error("handoff failed");
if (operation.kind === "credentials") return { kind: "credentials", taskId: "task", credential: null };
return perform(operation);
});
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex", accountIdentity: "account",
adoptIdentity: requirements.identity, onCredentials: async () => {}, onSessionRetained: async () => {} });
const registration = await owner.registration({} as never, "run");
await registration.release();
await expect(registration.checkpoint("settled")).rejects.toThrow("handoff failed");
expect(await owner.driver.adoptExistingRunner!.isAlive()).toBe(false);
expect(owner.isRetired()).toBe(false);
});
it("submits only a prepared identity and configuration ID", async () => {
const { task } = provider();
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", pollMs: 1 });
expect(() => owner.launcher(launchSpec())).toThrow("requires preparation");
await prepare(owner);
expect(task.mock.calls[0][0]).toEqual({ kind: "prepare", requirements, projectIds: [] });
expect(task.mock.calls[1][0]).toMatchObject({ kind: "check_project_auth", configurationId: "configuration", provider: "openai" });
expect(() => owner.launcher({ ...launchSpec(), nativeRunner: { ...launchSpec().nativeRunner!,
identity: { ...requirements.identity, runId: "other" } } })).toThrow("structured per-turn");
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const registration = await owner.registration({} as never, "run");
const handle = owner.launcher(launchSpec());
registration.activate(); await registration.ready();
await registration.release(); await registration.checkpoint("unsettled"); await handle.completion;
expect(task.mock.calls.find(([op]) => op.kind === "submit")?.[0]).toMatchObject({ preparedLaunchId: "configuration" });
});
it("omits duplicate project validation only when the provider enforces it at submission", async () => {
const { task } = provider();
const perform = task.getMockImplementation()!;
task.mockImplementation(async op => op.kind === "prepare"
? { ...await perform(op) as object, projectAuthCheckAtSubmit: true } : perform(op));
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1,
task, projectIds: [], harness: "codex", pollMs: 1 });
await prepare(owner);
expect(task.mock.calls.map(([op]) => op.kind)).toEqual(["prepare"]);
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const registration = await owner.registration({} as never, "run");
const handle = owner.launcher(launchSpec());
registration.activate(); await registration.ready();
await registration.release(); await registration.checkpoint("unsettled"); await handle.completion;
expect(task.mock.calls.filter(([op]) => op.kind === "submit")).toHaveLength(1);
const unsupported = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex" });
await expect(prepare(unsupported)).rejects.toThrow("validation unavailable");
});
it("does not accept a late preparation after task release", async () => {
let finish!: () => void;
const gate = new Promise<void>(resolve => { finish = resolve; });
const { task } = provider(); const perform = task.getMockImplementation()!;
task.mockImplementation(async op => { if (op.kind === "prepare") await gate; return perform(op); });
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex" });
const prepared = prepare(owner);
const registration = await owner.registration({} as never, "run");
await registration.release(); finish();
await expect(prepared).rejects.toThrow("preparation cancelled");
expect(task.mock.calls.some(([op]) => op.kind === "submit")).toBe(false);
});
it("sends Stop before a late preparation can publish a launch", async () => {
const context = new NativeExecutionContext({ binding: { companyId: "company", agentId: "agent", runId: "run" } } as never, 1_000);
const { task } = provider(), perform = task.getMockImplementation()!;
let release!: () => void;
const gate = new Promise<void>(resolve => { release = resolve; });
task.mockImplementation(async operation => { if (operation.kind === "prepare") await gate; return perform(operation); });
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex", projectAuthCheckVersion: 1, executionContext: context });
const preparing = prepare(owner);
await vi.waitFor(() => expect(task).toHaveBeenCalledWith(expect.objectContaining({ kind: "prepare" }), { expectedTaskId: "task" }));
context.abort(new Error("operator Stop"));
expect(task).toHaveBeenCalledWith({ kind: "stop" }, { expectedTaskId: "task" });
release();
await expect(preparing).rejects.toThrow("preparation cancelled");
expect(task.mock.calls.some(([operation]) => ["check_project_auth", "connection", "submit"].includes(operation.kind))).toBe(false);
context.finish();
});
it.each(["unsupported", "denied", "wrong-configuration", "wrong-provider"])("withholds credentials when project validation is %s", async mode => {
const { task } = provider(); const perform = task.getMockImplementation()!;
task.mockImplementation(async op => op.kind === "check_project_auth"
? { kind: "project_auth", taskId: "task", allowed: mode !== "denied",
configurationId: mode === "wrong-configuration" ? "other" : op.configurationId,
provider: mode === "wrong-provider" ? "anthropic" : op.provider } : perform(op));
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex",
projectAuthCheckVersion: mode === "unsupported" ? undefined : 1,
credentialIdentity: "revision", accountIdentity: "account", credential: { type: "auth-file", provider: "codex", content: '{"OPENAI_API_KEY":"never-transfer"}' } });
await expect(prepare(owner)).rejects.toThrow(mode === "unsupported" ? "validation unavailable" : "conflicts");
expect(() => owner.launcher(launchSpec())).toThrow("requires preparation");
expect(JSON.stringify(task.mock.calls)).not.toContain("never-transfer");
expect(task.mock.calls.some(([op]) => op.kind === "submit")).toBe(false);
});
it("rejects an installed account when the plugin lacks project validation", async () => {
const { task } = provider();
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness: "codex" });
await expect(prepare(owner)).rejects.toThrow("validation unavailable");
expect(task.mock.calls.some(([op]) => op.kind === "submit")).toBe(false);
});
it("collects refreshed credentials after process termination and before releasing task grants", async () => {
const { task } = provider();
const perform = task.getMockImplementation()!;
const credential = { type: "auth-file" as const, provider: "codex" as const, content: '{"OPENAI_API_KEY":"fixture-key"}' };
let copied = false;
task.mockImplementation(async operation => {
if (operation.kind === "credentials") {
expect(task.mock.calls.some(([op]) => op.kind === "stop")).toBe(true);
return { kind: "credentials", taskId: "task", credential };
}
if (operation.kind === "complete") expect(copied).toBe(true);
return perform(operation);
});
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", credential,
credentialIdentity: "revision", accountIdentity: "stable-account", pollMs: 1,
onCredentials: async value => { expect(value).toEqual(credential); copied = true; } });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
const process = owner.launcher(launchSpec()); registration.activate(); await registration.ready();
expect(task.mock.calls.find(([op]) => op.kind === "submit")?.[0]).toMatchObject({ credential, credentialIdentity: "revision", accountIdentity: "stable-account" });
await registration.release(); await registration.checkpoint("settled"); await process.completion;
expect(copied).toBe(true);
});
it("retains grants when the credential handoff fails", async () => {
const { task } = provider();
const perform = task.getMockImplementation()!;
task.mockImplementation(async op => op.kind === "credentials" ? { kind: "credentials", taskId: "task", credential: { type: "auth-file", provider: "codex", content: "{}" } } : perform(op));
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", accountIdentity: "account", pollMs: 1,
onCredentials: async () => { throw new Error("credential handoff failed"); } });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
const process = owner.launcher(launchSpec()); registration.activate(); await registration.ready(); await registration.release();
await expect(registration.checkpoint("settled")).rejects.toThrow("credential handoff failed");
await process.completion;
expect(task.mock.calls.some(([op]) => op.kind === "complete")).toBe(false);
});
it.each([false, true])("records retained sessions after status failures (retry exhausted: %s)", async exhausted => {
const { task } = provider();
const perform = task.getMockImplementation()!;
let failStatus = false;
let clock = Date.now();
const now = vi.spyOn(Date, "now").mockImplementation(() => clock);
task.mockImplementation(async operation => {
if (operation.kind === "stop") failStatus = false;
if (operation.kind === "status" && failStatus) {
failStatus = exhausted;
if (exhausted) clock += 16_000;
throw new Error("status connection lost");
}
return perform(operation);
});
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const checkpoint = vi.fn(async () => {});
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", onSessionRetained: checkpoint, pollMs: 1,
});
await prepare(owner);
const registration = await owner.registration({} as never, "run");
const handle = owner.launcher(launchSpec());
const completed = vi.fn(); void handle.completion.then(completed);
try {
registration.activate(); await registration.ready();
expect(handle.restart).toBeNull();
failStatus = true;
if (exhausted) await expect(registration.failure).rejects.toThrow("reconcile the saved task");
else await vi.waitFor(() => expect(failStatus).toBe(false));
expect(completed).not.toHaveBeenCalled();
await registration.release(); await registration.checkpoint("settled");
await expect(handle.completion).resolves.toMatchObject({ code: 1 });
expect(checkpoint).toHaveBeenCalledOnce();
expect(task.mock.calls.filter(([op]) => op.kind === "submit")).toHaveLength(1);
expect(task.mock.calls.filter(([op]) => op.kind === "complete")).toHaveLength(1);
} finally { now.mockRestore(); handle.child.kill(); }
});
it("exposes connection failure before activation and forwards late recovery exhaustion", async () => {
const { task } = provider();
let fail!: (error: Error) => void;
const failure = new Promise<never>((_resolve, reject) => { fail = reject; });
ingress.connect.mockReturnValue({ ready: Promise.resolve(), failure, close: async () => {} });
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", pollMs: 1 });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
const observed = registration.failure.catch(error => error);
const handle = owner.launcher(launchSpec());
registration.activate(); await registration.ready();
const error = new Error("runner ingress recovery exhausted"); fail(error);
expect(await observed).toBe(error);
expect(task.mock.calls.find(([op]) => op.kind === "stop")).toBeUndefined();
await registration.release(); await registration.checkpoint("settled"); await handle.completion;
});
it("quiesces the Runner's ingress when its exit is committed, before release closes it", async () => {
const { task } = provider();
const quiesce = vi.fn();
const close = vi.fn(async () => {});
ingress.connect.mockReset().mockReturnValue({ ready: Promise.resolve(), failure: new Promise<never>(() => {}), quiesce, close });
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", pollMs: 1 });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
registration.quiesce();
const handle = owner.launcher(launchSpec());
registration.activate(); await registration.ready();
registration.quiesce();
expect(quiesce).toHaveBeenCalledOnce();
expect(close).not.toHaveBeenCalled();
expect(task.mock.calls.find(([op]) => op.kind === "stop")).toBeUndefined();
await registration.release(); await registration.checkpoint("settled"); await handle.completion;
expect(close).toHaveBeenCalledOnce();
});
it("surfaces a proven admission rejection without polling or stopping the task", async () => {
const { task } = provider();
const perform = task.getMockImplementation()!;
task.mockImplementation(async op => op.kind === "submit"
? { kind: "admission_rejected", taskId: "task", reason: "session_state_unavailable" } : perform(op));
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex" });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
const process = owner.launcher(launchSpec());
registration.activate();
await expect(registration.ready()).rejects.toThrow("runner_task_admission_rejected: session_state_unavailable");
await expect(registration.failure).rejects.toThrow("runner_task_admission_rejected: session_state_unavailable");
await expect(process.completion).resolves.toMatchObject({ code: 1 });
await registration.checkpoint("unsettled");
expect(task.mock.calls.map(([op]) => op.kind)).toEqual(["prepare", "check_project_auth", "submit"]);
});
it("reconciles a lost submit reply without launching another attempt", async () => {
const { task } = provider();
const perform = task.getMockImplementation()!;
let lost = true, statusLost = true;
task.mockImplementation(async op => {
if (op.kind === "submit" && lost) { lost = false; throw new Error("lost submit reply"); }
if (op.kind === "status" && statusLost) { statusLost = false; throw new Error("status temporarily unavailable"); }
return perform(op);
});
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex",
pollMs: 1 });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
const process = owner.launcher(launchSpec());
try {
registration.activate(); await registration.ready();
expect(task.mock.calls.filter(([operation]) => operation.kind === "submit")).toHaveLength(1);
expect(task.mock.calls.find(([operation]) => operation.kind === "submit")?.[0].launch).not.toHaveProperty("diagnosticsDirectory");
expect(() => owner.launcher(launchSpec())).toThrow("new task lease");
await registration.release(); await registration.checkpoint("settled"); await process.completion;
expect(task.mock.calls.filter(([operation]) => operation.kind === "complete")).toHaveLength(1);
} finally { process.child.kill(); await process.completion.catch(() => {}); }
});
it("retains task grants when retention recording fails after route release", async () => {
const { task } = provider();
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", onSessionRetained: async () => { throw new Error("retention write failed"); }, pollMs: 1 });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
const process = owner.launcher(launchSpec());
registration.activate(); await registration.ready(); await registration.release();
expect(task.mock.calls.some(([operation]) => operation.kind === "complete")).toBe(false);
await expect(registration.checkpoint("settled")).rejects.toThrow("retention write failed");
await process.completion;
expect(task.mock.calls.some(([operation]) => operation.kind === "complete")).toBe(false);
});
it("detaches a controller without stopping its durable task", async () => {
const { task } = provider();
const close = vi.fn(async () => {});
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close });
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex",
pollMs: 1 });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
owner.launcher(launchSpec());
registration.activate(); await registration.ready(); await registration.release("detach");
expect(close).toHaveBeenCalledOnce();
expect(task.mock.calls.some(([operation]) => operation.kind === "stop" || operation.kind === "complete")).toBe(false);
});
it("prevents a late submission after cancellation during dispatch recording", async () => {
let finish!: () => void;
const prepared = new Promise<void>(resolve => { finish = resolve; });
const { task } = provider();
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex",
onDispatched: () => prepared, pollMs: 1 });
await prepare(owner);
const registration = await owner.registration({} as never, "run");
const process = owner.launcher(launchSpec());
registration.activate();
process.child.kill(); await registration.release(); finish();
await expect(process.completion).resolves.toMatchObject({ code: 1 });
await expect(registration.ready()).rejects.toThrow("cancelled");
expect(task.mock.calls.some(([op]) => op.kind === "submit")).toBe(false);
});
it("does not connect a released or aborted startup to the PRP authority", async () => {
const { task } = provider();
ingress.connect.mockClear();
const controller = new AbortController(); controller.abort();
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex",
pollMs: 1 });
await prepare(owner);
const registration = await owner.registration({} as never, "run", controller.signal);
const process = owner.launcher(launchSpec());
try {
registration.activate();
await expect(registration.ready()).rejects.toThrow("cancelled");
expect(task.mock.calls.filter(([operation]) => operation.kind === "connection")).toHaveLength(0);
expect(ingress.connect).not.toHaveBeenCalled();
await registration.release(); await registration.checkpoint("settled"); await process.completion;
} finally { process.child.kill(); await process.completion.catch(() => {}); }
});
});
describe("plugin Runner release/resume facade", () => {
const input = (harness: string) => ({ taskId: "task", projectIds: ["project"], harness,
warmTaskVersion: 1 as const, releaseResumeVersion: 1 as const,
accountIdentity: "selected-account", credentialIdentity: "selected-credential",
adoptIdentity: requirements.identity, authorityRunId: "run" });
const lifecycleTask = () => vi.fn(async (operation: any): Promise<unknown> => operation.kind === "release"
? { kind: "released", taskId: "task" }
: { kind: "resume", taskId: "task", outcome: "resumed" });
it.each([true, false])("sends the current Claude credential only for session handoff (%s)", async sessionHandoff => {
const task = lifecycleTask();
const environment = { CLAUDE_CODE_OAUTH_TOKEN: "fixture-current-token" };
const owner = createPluginRunnerTaskOwner({ ...input("claude"), task, sessionHandoff,
credential: { type: "claude-env", environment } });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("resumed");
expect(task).toHaveBeenCalledExactlyOnceWith({
kind: "resume", identity: requirements.identity, bootstrapTicket: "transient-ticket",
...(sessionHandoff ? { handoffCredential: { accountIdentity: "selected-account", environment } } : {}),
}, { expectedTaskId: "task" });
});
it.each([
["sends the claimant's Claude credential to a supporting provider", { type: "claude-env" as const, environment: { CLAUDE_CODE_OAUTH_TOKEN: "fixture-current-token" } }, 1 as const, true],
["omits the credential when submit would omit it", undefined, 1 as const, false],
["omits the credential for a provider without support", { type: "claude-env" as const, environment: { ANTHROPIC_API_KEY: "fixture-current-key" } }, undefined, false],
])("%s on resume", async (_label, credential, resumeLaunchInputsVersion, sent) => {
const task = lifecycleTask();
const owner = createPluginRunnerTaskOwner({ ...input("claude"), task, resumeLaunchInputsVersion, ...(credential ? { credential } : {}) });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("resumed");
expect(task).toHaveBeenCalledExactlyOnceWith({
kind: "resume", identity: requirements.identity, bootstrapTicket: "transient-ticket",
...(sent ? { credential } : {}),
}, { expectedTaskId: "task" });
});
it.each([
["reports a relaunched build when the provider supports it", 1 as const, true],
["ignores a reported build when the provider never advertised support", undefined, false],
])("%s", async (_label, resumeRelaunchedRunnerVersion, reported) => {
const runner = { version: "0.4.1", digest: `sha256:${"b".repeat(64)}` };
const task = vi.fn(async (): Promise<unknown> => ({ kind: "resume", taskId: "task", outcome: "resumed", runner }));
const owner = createPluginRunnerTaskOwner({ ...input("codex"), task, resumeRelaunchedRunnerVersion });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toEqual(reported ? { outcome: "resumed", runner } : "resumed");
expect(task).toHaveBeenCalledExactlyOnceWith({
kind: "resume", identity: requirements.identity, bootstrapTicket: "transient-ticket",
...(reported ? { relaunchedRunnerVersion: 1 } : {}),
}, { expectedTaskId: "task" });
});
it.each([
["the parked build", undefined],
["a relaunched build", { version: "0.4.1", digest: `sha256:${"b".repeat(64)}` }],
])("passes the provider's resume outcome through on %s", async (_label, runner) => {
const task = vi.fn(async (): Promise<unknown> => ({ kind: "resume", taskId: "task", outcome: "resumed", ...(runner ? { runner } : {}) }));
const owner = createPluginRunnerTaskOwner({ ...input("codex"), task, resumeRelaunchedRunnerVersion: 1 });
const outcome = await owner.driver.releaseResume!.resume(requirements.identity, "transient-ticket");
expect(outcome).toEqual(runner ? { outcome: "resumed", runner } : "resumed");
});
it("resumes with the credential resolved for the rebound claimant run", async () => {
const task = lifecycleTask();
const owner = createPluginRunnerTaskOwner({ ...input("claude"), task, resumeLaunchInputsVersion: 1,
credential: { type: "claude-env", environment: { CLAUDE_CODE_OAUTH_TOKEN: "fixture-source-token" } } });
const claimant = { type: "claude-env" as const, environment: { CLAUDE_CODE_OAUTH_TOKEN: "fixture-claimant-token" } };
owner.rebind({ ...input("claude"), task, resumeLaunchInputsVersion: 1, authorityRunId: "claimant-run", credential: claimant });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("resumed");
expect(task).toHaveBeenCalledExactlyOnceWith({
kind: "resume", identity: requirements.identity, bootstrapTicket: "transient-ticket", credential: claimant,
}, { expectedTaskId: "task" });
});
it.each(["codex", "claude"])("quiesces %s before release and closes only after its ACK", async harness => {
const { task } = provider();
const perform = task.getMockImplementation()!;
const order: string[] = [];
let acknowledge!: (value: unknown) => void;
task.mockImplementation(op => {
if (op.kind !== "release") return perform(op);
order.push("release");
return new Promise(resolve => { acknowledge = resolve; });
});
const quiesce = vi.fn(() => { order.push("quiesce"); });
const close = vi.fn(async () => { order.push("close"); });
ingress.connect.mockReset().mockReturnValue({ ready: Promise.resolve(), quiesce, close });
const owner = createPluginRunnerTaskOwner({ ...input(harness), task });
const registration = await owner.registration({} as never, "run");
registration.activate(); await registration.ready();
try {
const releasing = owner.driver.releaseResume!.release(requirements.identity);
expect(order).toEqual(["quiesce", "release"]);
expect(close).not.toHaveBeenCalled();
acknowledge({ kind: "released", taskId: "task" });
await releasing;
expect(order).toEqual(["quiesce", "release", "close"]);
expect(task.mock.calls.at(-1)![0]).toEqual({ kind: "release", identity: requirements.identity });
expect(ingress.connect).toHaveBeenCalledOnce();
} finally { await registration.release("detach"); }
});
it.each(["codex", "claude"])("keeps an uncertain %s release quiet for an exact retry", async harness => {
const { task } = provider();
const perform = task.getMockImplementation()!;
const lost = new Error("fixture release reply lost");
let attempts = 0;
task.mockImplementation(async op => {
if (op.kind !== "release") return perform(op);
if (attempts++ === 0) throw lost;
return { kind: "released", taskId: "task" };
});
const quiesce = vi.fn(), close = vi.fn(async () => {});
ingress.connect.mockReset().mockReturnValue({ ready: Promise.resolve(), quiesce, close });
const owner = createPluginRunnerTaskOwner({ ...input(harness), task });
const registration = await owner.registration({} as never, "run");
registration.activate(); await registration.ready();
try {
await expect(owner.driver.releaseResume!.release(requirements.identity)).rejects.toBe(lost);
expect(quiesce).toHaveBeenCalledOnce();
expect(close).not.toHaveBeenCalled();
await owner.driver.releaseResume!.release(requirements.identity);
expect(quiesce).toHaveBeenCalledTimes(2);
expect(close).toHaveBeenCalledOnce();
expect(task.mock.calls.filter(([op]) => op.kind === "release").map(([op]) => op))
.toEqual([{ kind: "release", identity: requirements.identity }, { kind: "release", identity: requirements.identity }]);
expect(ingress.connect).toHaveBeenCalledOnce();
} finally { await registration.release("detach"); }
});
it.each(["codex", "claude"])("forwards both %s operations through the current checked owner", async harness => {
const firstTask = lifecycleTask();
const currentTask = lifecycleTask();
const original = { ...input(harness), task: firstTask };
const owner = createPluginRunnerTaskOwner(original);
await expect(owner.release(requirements.identity)).resolves.toBeUndefined();
expect(firstTask).toHaveBeenCalledExactlyOnceWith({ kind: "release", identity: requirements.identity }, { expectedTaskId: "task" });
expect(() => owner.rebind({ ...original, task: currentTask, authorityRunId: "next", accountIdentity: "changed" })).toThrow("cannot be rebound");
expect(currentTask).not.toHaveBeenCalled();
owner.rebind({ ...original, task: currentTask, authorityRunId: "next" });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("resumed");
expect(currentTask).toHaveBeenCalledExactlyOnceWith({ kind: "resume", identity: requirements.identity, bootstrapTicket: "transient-ticket" }, { expectedTaskId: "task" });
currentTask.mockResolvedValueOnce({ kind: "resume", taskId: "task", outcome: "unavailable" });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("unavailable");
expect(firstTask).toHaveBeenCalledTimes(1);
});
it.each(["codex", "claude"])("requires capability, warm support and account for %s", async harness => {
for (const missing of ["releaseResumeVersion", "warmTaskVersion", "accountIdentity"] as const) {
const task = lifecycleTask();
const owner = createPluginRunnerTaskOwner({ ...input(harness), task, [missing]: undefined });
await expect(owner.release(requirements.identity)).rejects.toThrow("release/resume unavailable");
await expect(owner.resume(requirements.identity, "transient-ticket")).rejects.toThrow("release/resume unavailable");
expect(task).not.toHaveBeenCalled();
}
});
it.each(["released", "detached", "cancelled"] as const)("does not dispatch after the owner is %s", async state => {
const task = provider().task;
const context = new NativeExecutionContext({ binding: { companyId: "company", agentId: "agent", runId: "run" } } as never, 1_000);
const owner = createPluginRunnerTaskOwner({ ...input("codex"), task, executionContext: context });
try {
if (state === "cancelled") context.abort(new Error("fixture cancellation"));
else await (await owner.registration({} as never, "run")).release(state === "detached" ? "detach" : undefined);
task.mockClear();
await expect(owner.release(requirements.identity)).rejects.toThrow("operation cancelled");
await expect(owner.resume(requirements.identity, "transient-ticket")).rejects.toThrow("operation cancelled");
expect(task).not.toHaveBeenCalled();
} finally { context.finish(); }
});
it.each(["release", "resume"] as const)("does not accept a late %s receipt after detachment", async operation => {
let reply!: (value: unknown) => void;
const task = vi.fn(() => new Promise<unknown>(resolve => { reply = resolve; }));
const owner = createPluginRunnerTaskOwner({ ...input("codex"), task });
const pending = operation === "release" ? owner.release(requirements.identity) : owner.resume(requirements.identity, "transient-ticket");
await (await owner.registration({} as never, "run")).release("detach");
reply(operation === "release" ? { kind: "released", taskId: "task" } : { kind: "resume", taskId: "task", outcome: "resumed" });
await expect(pending).rejects.toThrow("operation cancelled");
expect(task).toHaveBeenCalledTimes(1);
});
it("can receive unavailable after an observed natural exit", async () => {
const task = vi.fn(async (operation: any): Promise<unknown> => operation.kind === "status"
? { kind: "status", taskId: "task", phase: "completed", executionStopped: true, exitCode: 0 }
: { kind: "resume", taskId: "task", outcome: "unavailable" });
const owner = createPluginRunnerTaskOwner({ ...input("codex"), task });
expect(await owner.driver.adoptExistingRunner!.isAlive()).toBe(false);
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("unavailable");
expect(task.mock.calls.map(([operation]) => operation.kind)).toEqual(["status", "resume", "status"]);
expect(owner.retiredBeforeClaim()).toBe(false); // stopped, but no saved session state
});
it("records a refused resume as retired only when the exact task stopped with saved session state", async () => {
for (const [status, retired] of [
[{ kind: "status", taskId: "task", phase: "cancelled", executionStopped: true, sessionStateAvailable: true }, true],
[{ kind: "status", taskId: "task", phase: "running", executionStopped: false, sessionStateAvailable: false }, false],
[new Error("fixture status reply lost"), false],
] as const) {
const task = vi.fn(async (operation: any): Promise<unknown> => {
if (operation.kind === "resume") return { kind: "resume", taskId: "task", outcome: "unavailable" };
if (status instanceof Error) throw status;
return status;
});
const owner = createPluginRunnerTaskOwner({ ...input("codex"), task });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("unavailable");
expect(owner.retiredBeforeClaim()).toBe(retired);
}
});
it.each(["release", "resume"] as const)("validates exact task and closed %s result", async operation => {
const task = lifecycleTask();
const owner = createPluginRunnerTaskOwner({ ...input("codex"), task });
const invoke = () => operation === "release" ? owner.release(requirements.identity) : owner.resume(requirements.identity, "transient-ticket");
for (const result of [
{ kind: operation === "release" ? "released" : "resume", taskId: "other-task", ...(operation === "resume" ? { outcome: "resumed" } : {}) },
{ kind: "accepted", taskId: "task" },
{ kind: "resume", taskId: "task", outcome: "unknown" },
]) {
task.mockResolvedValueOnce(result);
await expect(invoke()).rejects.toThrow();
}
});
it.each(["release", "resume"] as const)("preserves ambiguous %s errors without another operation", async operation => {
const task = lifecycleTask();
const owner = createPluginRunnerTaskOwner({ ...input("codex"), task });
const error = new Error("fixture transport lost its reply");
task.mockRejectedValueOnce(error);
await expect(operation === "release" ? owner.release(requirements.identity) : owner.resume(requirements.identity, "transient-ticket")).rejects.toBe(error);
expect(task).toHaveBeenCalledTimes(1);
});
});
describe("plugin Runner relaunch as the claimant run", () => {
const source = requirements.identity;
const next = { ...source, runId: "next-run", turnId: "turn-next-run", itemId: "item-next-run" };
const parked = { version: "0.4.0", digest: `sha256:${"c".repeat(64)}` };
const build = { version: "0.4.1", digest: `sha256:${"b".repeat(64)}` };
const base = { taskId: "task", projectIds: ["00000000-0000-4000-8000-000000000001"], projectAuthCheckVersion: 1 as const, harness: "codex", warmTaskVersion: 1 as const, releaseResumeVersion: 1 as const,
accountIdentity: "selected-account", credentialIdentity: "selected-credential" };
const relaunchSpec = (): RunnerProcessLaunchSpec => {
const spec = launchSpec();
return { ...spec, nativeRunner: { ...spec.nativeRunner!, identity: next, lifecyclePolicy: { mode: "warm", idleTimeoutMs: 60_000 } } };
};
async function relaunchOwner(resume: (operation: any) => unknown,
options: { resumeIdentityVersion?: 1; fresh?: true } = { resumeIdentityVersion: 1 }) {
const { task } = provider();
const perform = task.getMockImplementation()!;
task.mockImplementation(async operation => operation.kind === "resume" ? resume(operation) : perform(operation));
const close = vi.fn(async () => {});
ingress.connect.mockReset().mockReturnValue({ ready: Promise.resolve(), quiesce: vi.fn(), close });
const relaunchInput = { ...base, task, authorityRunId: next.runId, resumeRelaunchedRunnerVersion: 1 as const,
resumeIdentityVersion: options.resumeIdentityVersion, relaunch: { source, runner: parked } };
const owner = createPluginRunnerTaskOwner(options.fresh ? relaunchInput : { ...base, task, adoptIdentity: source, authorityRunId: source.runId });
if (!options.fresh) owner.rebind(relaunchInput);
await prepare(owner, { ...requirements, identity: next });
const authority = { acceptRelaunchedRunner: vi.fn() };
const registration = await owner.registration(authority as never, next.runId, undefined, next);
return { owner, task, authority, registration };
}
it("starts the stopped Runner again as the claimant run and accepts the reported build", async () => {
const { owner, task, authority, registration } = await relaunchOwner(
() => ({ kind: "resume", taskId: "task", outcome: "resumed", runner: build }));
expect(owner.driver.relaunchReleasedRunner).toEqual(source);
expect(owner.driver.adoptExistingRunner).toBeUndefined();
expect(owner.relaunchMayHaveStarted()).toBe(false);
owner.launcher(relaunchSpec());
registration.activate();
try {
await registration.ready();
expect(task.mock.calls.filter(([operation]) => operation.kind === "resume").map(([operation]) => operation)).toEqual([{
kind: "resume", identity: source, bootstrapTicket: "synthetic-ticket", relaunchedRunnerVersion: 1,
runner: { runId: next.runId, turnId: next.turnId, itemId: next.itemId },
}]);
expect(task.mock.calls.some(([operation]) => operation.kind === "submit")).toBe(false);
expect(authority.acceptRelaunchedRunner).toHaveBeenCalledExactlyOnceWith("synthetic-ticket", build, parked);
expect(ingress.connect).toHaveBeenCalledOnce();
expect(owner.relaunchMayHaveStarted()).toBe(true);
owner.rebind({ ...base, task, authorityRunId: "later-run", resumeRelaunchedRunnerVersion: 1, resumeIdentityVersion: 1,
relaunch: { source: next, runner: build } });
expect(owner.relaunchMayHaveStarted()).toBe(false);
} finally { await registration.release("detach"); }
});
it("relaunches through a new owner, as after a Core restart, without submitting or adopting", async () => {
const { owner, task, authority, registration } = await relaunchOwner(
() => ({ kind: "resume", taskId: "task", outcome: "resumed", runner: build }),
{ resumeIdentityVersion: 1, fresh: true });
expect(task.mock.calls.find(([operation]) => operation.kind === "prepare")?.[0]).toMatchObject({ priorIdentity: source });
expect(owner.driver.relaunchReleasedRunner).toEqual(source);
expect(owner.driver.adoptExistingRunner).toBeUndefined();
owner.launcher(relaunchSpec());
registration.activate();
try {
await registration.ready();
expect(task.mock.calls.filter(([operation]) => operation.kind === "resume").map(([operation]) => operation.runner))
.toEqual([{ runId: next.runId, turnId: next.turnId, itemId: next.itemId }]);
expect(task.mock.calls.some(([operation]) => operation.kind === "submit")).toBe(false);
expect(authority.acceptRelaunchedRunner).toHaveBeenCalledExactlyOnceWith("synthetic-ticket", build, parked);
} finally { await registration.release("detach"); }
});
it("treats a new owner as a new task when the provider does not advertise relaunch", async () => {
const { owner, task } = await relaunchOwner(() => { throw new Error("unexpected resume"); }, { fresh: true });
expect(owner.driver.relaunchReleasedRunner).toBeUndefined();
expect(task.mock.calls.find(([operation]) => operation.kind === "prepare")?.[0]).not.toHaveProperty("priorIdentity");
});
it("retries a lost reply with the same ticket and identity", async () => {
let attempts = 0;
const { task, registration, owner } = await relaunchOwner(() => {
if (attempts++ === 0) throw new Error("fixture resume reply lost");
return { kind: "resume", taskId: "task", outcome: "resumed", runner: build };
});
owner.launcher(relaunchSpec());
registration.activate();
try {
await registration.ready();
const resumes = task.mock.calls.filter(([operation]) => operation.kind === "resume").map(([operation]) => operation);
expect(resumes).toHaveLength(2);
expect(resumes[1]).toEqual(resumes[0]);
} finally { await registration.release("detach"); }
});
it("reports an unavailable relaunch for ordinary recovery", async () => {
const { registration, owner } = await relaunchOwner(() => ({ kind: "resume", taskId: "task", outcome: "unavailable" }));
owner.launcher(relaunchSpec());
registration.activate();
try {
await expect(registration.ready()).rejects.toThrow("native_runner_reuse_unavailable");
expect(ingress.connect).not.toHaveBeenCalled();
expect(owner.relaunchMayHaveStarted()).toBe(false);
} finally { await registration.release("detach"); }
});
it("keeps the claimant handover when the provider does not advertise relaunch", async () => {
const { owner, task } = await relaunchOwner(() => { throw new Error("unexpected resume"); }, {});
expect(owner.driver.relaunchReleasedRunner).toBeUndefined();
expect(() => owner.launcher(relaunchSpec())).toThrow("Plugin Runner replacement requires a new task lease");
expect(task.mock.calls.some(([operation]) => operation.kind === "resume")).toBe(false);
});
});
describe("plugin task workspace file transfer", () => {
it("reads only a submitted task, validates bytes, and rejects late responses after release", async () => {
const {task}=provider(); const perform=task.getMockImplementation()!;
const bytes=Buffer.from("VM proof\n");
let file={kind:"file",taskId:"task",base64:bytes.toString("base64"),byteSize:bytes.length,sha256:createHash("sha256").update(bytes).digest("hex")};
let afterRead=async()=>{};
task.mockImplementation(async op=>{if(op.kind==="read_file"){await afterRead();return file;}return perform(op);});
const owner=createPluginRunnerTaskOwner({ taskId: "task",projectAuthCheckVersion:1,task,projectIds:[],harness:"codex",pollMs:1});
await expect(owner.readWorkspaceFile("proof.txt",1024)).rejects.toThrow("unavailable");
await prepare(owner);
ingress.connect.mockReturnValue({ready:Promise.resolve(),close:async()=>{}});
const registration=await owner.registration({} as never,"run");
const handle=owner.launcher(launchSpec());registration.activate();await registration.ready();
try {
await expect(owner.readWorkspaceFile("proof.txt",1024)).resolves.toEqual(bytes);
await expect(owner.readWorkspaceFile("/home/agent/proof.txt",1024)).resolves.toEqual(bytes);
expect(task.mock.calls.find(([op])=>op.kind==="read_file")?.[0]).toMatchObject({identity:requirements.identity,path:"proof.txt",projectIds:[]});
for(const path of ["../escape","//etc/passwd","file:/secret","a/../b","a\\b"]) await expect(owner.readWorkspaceFile(path,1024)).rejects.toThrow();
await expect(owner.readWorkspaceFile("proof.txt",1)).rejects.toThrow("unavailable");
file={...file,sha256:"f".repeat(64)};
await expect(owner.readWorkspaceFile("proof.txt",1024)).rejects.toThrow("integrity");
afterRead=()=>registration.release();
await expect(owner.readWorkspaceFile("proof.txt",1024)).rejects.toThrow("unavailable");
} finally {await registration.release();await registration.checkpoint("unsettled");await handle.completion;}
});
});
it.each(["codex", "claude"])("reuses a warm %s process across run authorities without idle status polling", async harness => {
const {task} = provider();
ingress.connect.mockReturnValue({ready:Promise.resolve(),close:async()=>{}});
const retained = vi.fn(async()=>{});
const input = {taskId:"task",task,projectIds:[],harness,projectAuthCheckVersion:1 as const,warmTaskVersion:1 as const,
accountIdentity:"account",credentialIdentity:"revision",pollMs:1,onSessionRetained:retained, githubCredentialBrokerVersion:1 as const,
githubBroker:{url:"https://core.example.test",token:"first-run-capability"}};
const owner = createPluginRunnerTaskOwner(input);
await prepare(owner, launchRequirements(harness));
const registration = await owner.registration({} as never,"run");
const spec = launchSpec(); spec.nativeRunner!.lifecyclePolicy = {mode:"warm",idleTimeoutMs:300000};
const handle = owner.launcher(spec); registration.activate(); await registration.ready();
const observed = task.mock.calls.length;
await new Promise(resolve=>setTimeout(resolve,10));
expect(task.mock.calls).toHaveLength(observed);
const originalTask=task.getMockImplementation()!;
const nextTask=vi.fn(async op=>{
const result=await originalTask(op) as Record<string,unknown>;
return op.kind === "prepare" ? {...result,projectAuthCheckAtSubmit:true} : result;
});
owner.rebind({...input,task:nextTask,githubBroker:{url:"https://core.example.test",token:"next-run-capability"}});
const nextRegistration=await owner.registration({} as never,"next-run");
const nextIdentity={...requirements.identity,runId:"next-run",turnId:"next-turn",itemId:"next-item"};
await owner.driver.prepareLaunch!({...launchRequirements(harness),identity:nextIdentity});
expect(nextTask.mock.calls[0][0]).toMatchObject({kind:"prepare",priorIdentity:requirements.identity,accountIdentity:"account",githubBroker:{url:"https://core.example.test",token:"next-run-capability"}});
expect(task.mock.calls.find(([op])=>op.kind === "prepare")?.[0]).toMatchObject({githubBroker:input.githubBroker});
expect(nextTask.mock.calls.map(([op])=>op.kind)).toEqual(["prepare","check_project_auth"]);
await registration.release(); nextRegistration.activate(); await nextRegistration.ready();
expect(nextTask.mock.calls.some(([op])=>op.kind==="submit" || op.kind==="stop" || op.kind==="complete")).toBe(false);
await nextRegistration.release(); await nextRegistration.checkpoint("settled"); await handle.completion;
expect(retained).toHaveBeenCalledWith(expect.objectContaining(nextIdentity));
expect(nextTask.mock.calls.filter(([op])=>op.kind==="stop")).toHaveLength(1);
});
it.each([
["advertised", 1 as const],
["not advertised", undefined],
] as const)("launch digests are sent on prepare only when the plugin has %s them", async (_label, version) => {
const {task} = provider();
ingress.connect.mockReturnValue({ready:Promise.resolve(),close:async()=>{}});
const input = {taskId:"task",task,projectIds:[],harness:"codex",projectAuthCheckVersion:1 as const,warmTaskVersion:1 as const,
accountIdentity:"account",credentialIdentity:"revision",pollMs:1,launchDigestVersion:version};
const owner = createPluginRunnerTaskOwner(input);
await prepare(owner, launchRequirements("codex"));
const registration = await owner.registration({} as never,"run");
const spec = launchSpec(); spec.nativeRunner!.lifecyclePolicy = {mode:"warm",idleTimeoutMs:300000};
const handle = owner.launcher(spec); registration.activate(); await registration.ready();
const nextIdentity={...requirements.identity,runId:"next-run",turnId:"next-turn",itemId:"next-item"};
const nextRegistration=await owner.registration({} as never,"next-run");
await owner.driver.prepareLaunch!({...launchRequirements("codex"),identity:nextIdentity});
const prepares=task.mock.calls.map(([op])=>op).filter(op=>op.kind==="prepare");
expect(prepares).toHaveLength(2);
if (version === 1) {
const [first, next] = prepares as Array<{preparationDigest?:string;runDigest?:string}>;
expect(first.preparationDigest).toBe(runnerLaunchPreparationDigest(launchRequirements("codex")));
expect(next.preparationDigest).toBe(first.preparationDigest);
expect(first.runDigest).toBe(runnerLaunchRunDigest(first.preparationDigest!, launchRequirements("codex")));
expect(next.runDigest).toBe(runnerLaunchRunDigest(first.preparationDigest!, {...launchRequirements("codex"),identity:nextIdentity}));
expect(next.runDigest).not.toBe(first.runDigest);
} else {
for (const op of prepares) expect(op).not.toHaveProperty("preparationDigest");
for (const op of prepares) expect(op).not.toHaveProperty("runDigest");
}
await registration.release(); nextRegistration.activate(); await nextRegistration.ready();
await nextRegistration.release(); await nextRegistration.checkpoint("settled"); await handle.completion;
});
it.each([
["checked inside prepare", true, ["prepare"]],
["left to Core", false, ["prepare", "check_project_auth"]],
] as const)("a warm attach whose project authentication is %s makes only the round trips it needs", async (_label, checked, kinds) => {
const {task} = provider();
ingress.connect.mockReturnValue({ready:Promise.resolve(),close:async()=>{}});
const input = {taskId:"task",task,projectIds:[],harness:"codex",projectAuthCheckVersion:1 as const,warmTaskVersion:1 as const,
accountIdentity:"account",credentialIdentity:"revision",pollMs:1};
const owner = createPluginRunnerTaskOwner(input);
await prepare(owner, launchRequirements("codex"));
const registration = await owner.registration({} as never,"run");
const spec = launchSpec(); spec.nativeRunner!.lifecyclePolicy = {mode:"warm",idleTimeoutMs:300000};
const handle = owner.launcher(spec); registration.activate(); await registration.ready();
const originalTask=task.getMockImplementation()!;
const nextTask=vi.fn(async op=>{
const result=await originalTask(op) as Record<string,unknown>;
return op.kind === "prepare" && checked ? {...result,projectAuthChecked:true} : result;
});
owner.rebind({...input,task:nextTask});
const nextRegistration=await owner.registration({} as never,"next-run");
const nextIdentity={...requirements.identity,runId:"next-run",turnId:"next-turn",itemId:"next-item"};
await owner.driver.prepareLaunch!({...launchRequirements("codex"),identity:nextIdentity});
expect(nextTask.mock.calls.map(([op])=>op.kind)).toEqual(kinds);
await registration.release(); nextRegistration.activate(); await nextRegistration.ready();
await nextRegistration.release(); await nextRegistration.checkpoint("settled"); await handle.completion;
});
it.each(["codex", "claude"])("adopts a verified retained %s task without submitting a replacement", async harness => {
const {task}=provider();
ingress.connect.mockReturnValue({ready:Promise.resolve(),close:async()=>{}});
const owner=createPluginRunnerTaskOwner({ taskId: "task",task,projectIds:[],harness,projectAuthCheckVersion:1,
warmTaskVersion:1,accountIdentity:"account",adoptIdentity:requirements.identity,pollMs:1});
const recovered = {configurationId:"saved",requirementsDigest:`sha256:${"a".repeat(64)}`,provider:{}};
await expect(owner.driver.prepareLaunch!(launchRequirements(harness),recovered)).resolves.toEqual(recovered);
expect(task.mock.calls.map(([op])=>op.kind)).toEqual(["check_project_auth"]);
expect(task.mock.calls[0][0]).toMatchObject({configurationId:"saved",projectIds:[]});
expect(await owner.driver.adoptExistingRunner!.isAlive()).toBe(true);
const registration=await owner.registration({} as never,"run");
registration.activate(); await registration.ready();
await registration.release(); await registration.checkpoint("settled");
expect(task.mock.calls.some(([op])=>op.kind==="submit")).toBe(false);
expect(task.mock.calls.some(([op])=>op.kind==="complete")).toBe(true);
});
it.each(["codex", "claude"])("denies retained %s process adoption when current project authentication is revoked",async harness=>{
const {task}=provider();
task.mockImplementation(async op=>op.kind === "check_project_auth"
? {kind:"project_auth",taskId:"task",configurationId:op.configurationId,provider:op.provider,allowed:false} as never
: {kind:"accepted",taskId:"task"} as never);
const owner=createPluginRunnerTaskOwner({ taskId: "task",task,projectIds:["project"],harness,projectAuthCheckVersion:1,
warmTaskVersion:1,accountIdentity:"account",adoptIdentity:requirements.identity});
await expect(owner.driver.prepareLaunch!(launchRequirements(harness),{configurationId:"saved",requirementsDigest:`sha256:${"a".repeat(64)}`,provider:{}}))
.rejects.toThrow("project authentication conflicts");
expect(task.mock.calls.map(([op])=>op.kind)).toEqual(["check_project_auth"]);
});
it.each(["codex", "claude"])("refuses warm %s account, harness, project, or credential generation changes", async harness=>{
const {task}=provider();
const input={taskId:"task",task,projectIds:[],harness,projectAuthCheckVersion:1 as const,warmTaskVersion:1 as const,
accountIdentity:"account",credentialIdentity:"revision",pollMs:1};
const owner=createPluginRunnerTaskOwner(input);
await prepare(owner, launchRequirements(harness));
const spec=launchSpec();spec.nativeRunner!.lifecyclePolicy={mode:"warm",idleTimeoutMs:300000};
const process=owner.launcher(spec);
expect(()=>owner.rebind({...input,taskId:"replacement"})).toThrow("cannot be rebound");
expect(()=>owner.rebind({...input,accountIdentity:"other"})).toThrow("cannot be rebound");
expect(()=>owner.rebind({...input,credentialIdentity:"rotated"})).toThrow("cannot be rebound");
expect(()=>owner.rebind({...input,harness:harness === "codex" ? "claude" : "codex"})).toThrow("cannot be rebound");
expect(()=>owner.rebind({...input,projectIds:["foreign"]})).toThrow("cannot be rebound");
const registration=await owner.registration({} as never,"run");
await registration.checkpoint("unsettled"); await process.completion;
});
it("rejects a missing broker capability before handing authority to a plugin", async () => {
const {task} = provider();
const owner = createPluginRunnerTaskOwner({ taskId: "task",task,projectIds:[],harness:"codex",
githubBroker:{url:"https://core.example.test",token:"runtime-capability"}});
await expect(prepare(owner)).rejects.toThrow("credential broker v1");
expect(task).not.toHaveBeenCalled();
});
it.each((["codex", "claude"] as const).flatMap(harness => ["prepared", "unprepared", "foreign target"].map(mode => [harness, mode] as const)))(
"authenticates adopted %s source before activating only its exact %s successor", async (harness, mode) => {
const { task } = provider(), perform = task.getMockImplementation()!;
const source = launchRequirements(harness), target = { ...source, identity: { ...source.identity, runId: "successor", turnId: "next-turn", itemId: "next-item" } };
let nativeIdentity = source.identity;
task.mockImplementation(async operation => operation.kind === "read_state"
? { kind: "state", taskId: "task", state: { schema: "paperclip.runner.durable.state.v1", lifecycle: "ready", ...nativeIdentity } }
: perform(operation));
const connect = vi.fn(() => ({ ready: Promise.resolve(), close: async () => {} }));
ingress.connect.mockImplementation(connect);
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness, authorityRunId: target.identity.runId,
projectAuthCheckVersion: 1, accountIdentity: "account", adoptIdentity: source.identity });
const initial = await owner.driver.controlPlaneRegistration!({} as never, source.identity);
await initial.activate!(); await initial.ready!();
expect(task.mock.calls.some(([operation]) => operation.kind === "read_state" && operation.identity.runId === source.identity.runId)).toBe(true);
const requested = mode === "foreign target" ? { ...target.identity, itemId: "unprepared-item" } : target.identity;
const next = await owner.driver.controlPlaneRegistration!({} as never, requested);
if (mode !== "unprepared") await prepare(owner, target);
nativeIdentity = target.identity;
try {
await next.activate!();
if (mode === "prepared") { await expect(next.ready!()).resolves.toBeUndefined(); expect(connect).toHaveBeenCalledTimes(2); }
else { await expect(next.ready!()).rejects.toThrow("adoption identity mismatch"); expect(connect).toHaveBeenCalledTimes(1); }
} finally { await next.release!("detach"); await initial.release!(); }
},
);
it.each(["codex", "claude"] as const)("rejects a foreign %s source tuple before opening adopted ingress", async harness => {
const { task } = provider();
const connect = vi.fn(() => ({ ready: Promise.resolve(), close: async () => {} }));
ingress.connect.mockImplementation(connect);
const source = launchRequirements(harness).identity;
const owner = createPluginRunnerTaskOwner({ taskId: "task", task, projectIds: [], harness, adoptIdentity: source });
const registration = await owner.driver.controlPlaneRegistration!({} as never, { ...source, itemId: "foreign-item" });
try {
await registration.activate!();
await expect(registration.ready!()).rejects.toThrow("adoption identity mismatch");
expect(task).not.toHaveBeenCalled(); expect(connect).not.toHaveBeenCalled();
} finally { await registration.release!("detach"); }
});
it("routes credential notifications to the current warm lease after rebind", async () => {
const { task } = provider();
const first = vi.fn(async () => {}), successor = vi.fn(async () => {});
const input = { taskId: "task", task, projectIds: [], harness: "codex", adoptIdentity: requirements.identity,
onCredentialsUpdated: first };
const owner = createPluginRunnerTaskOwner(input);
const event = { eventType: "auth.credentials.updated" } as never;
await owner.credentialsUpdated(event);
owner.rebind({ ...input, onCredentialsUpdated: successor });
await owner.credentialsUpdated(event);
expect(first).toHaveBeenCalledExactlyOnceWith(event);
expect(successor).toHaveBeenCalledExactlyOnceWith(event);
expect(task).not.toHaveBeenCalled();
});
describe("plugin task configured environment", () => {
const configuredEnvironment = { STRIPE_SECRET_KEY: "fixture-secret", FEATURE_FLAG: "on" };
it.each([1, undefined] as const)("submits the variables only to a provider that supports them (%s)", async version => {
const { task } = provider();
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", pollMs: 1,
configuredEnvironmentVersion: version, configuredEnvironment });
await prepare(owner);
ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} });
const registration = await owner.registration({} as never, "run");
const handle = owner.launcher(launchSpec());
registration.activate(); await registration.ready();
await registration.release(); await registration.checkpoint("unsettled"); await handle.completion;
const submit = task.mock.calls.find(([op]) => op.kind === "submit")?.[0];
if (version === 1) expect(submit).toMatchObject({ configuredEnvironment });
else expect(submit).not.toHaveProperty("configuredEnvironment");
expect(owner.configuredEnvironmentDelivered()).toBe(version === 1);
expect(task.mock.calls.find(([op]) => op.kind === "prepare")?.[0]).not.toHaveProperty("configuredEnvironment");
});
it.each([true, false])("sends the variables on resume only for a session handoff (%s)", async sessionHandoff => {
const task = vi.fn(async (_operation: any): Promise<unknown> => ({ kind: "resume", taskId: "task", outcome: "resumed" }));
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectIds: ["project"], harness: "codex", task,
warmTaskVersion: 1, releaseResumeVersion: 1, accountIdentity: "selected-account", adoptIdentity: requirements.identity,
authorityRunId: "run", sessionHandoff, configuredEnvironmentVersion: 1, configuredEnvironment });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("resumed");
expect(task).toHaveBeenCalledExactlyOnceWith({
kind: "resume", identity: requirements.identity, bootstrapTicket: "transient-ticket",
...(sessionHandoff ? { configuredEnvironment } : {}),
}, { expectedTaskId: "task" });
expect(owner.configuredEnvironmentDelivered()).toBe(sessionHandoff ? true : undefined);
});
it.each([1, undefined] as const)("sends the launch inputs on every resume to a provider that supports them (%s)", async configuredEnvironmentVersion => {
const task = vi.fn(async (_operation: any): Promise<unknown> => ({ kind: "resume", taskId: "task", outcome: "resumed" }));
const credential = { type: "claude-env" as const, environment: { CLAUDE_CODE_OAUTH_TOKEN: "fixture-claimant-token" } };
const owner = createPluginRunnerTaskOwner({ taskId: "task", projectIds: ["project"], harness: "claude", task,
warmTaskVersion: 1, releaseResumeVersion: 1, resumeLaunchInputsVersion: 1, accountIdentity: "selected-account", adoptIdentity: requirements.identity,
authorityRunId: "run", credential, configuredEnvironmentVersion, configuredEnvironment });
await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("resumed");
expect(task).toHaveBeenCalledExactlyOnceWith({
kind: "resume", identity: requirements.identity, bootstrapTicket: "transient-ticket", credential,
...(configuredEnvironmentVersion === 1 ? { configuredEnvironment } : {}),
}, { expectedTaskId: "task" });
expect(owner.configuredEnvironmentDelivered()).toBeUndefined();
});
});