Files
PaperClipAI/cli/src/commands/managed-agent.ts
T
Dotta fdf8c8464d feat(runner): add managed provider backends (#12699)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The Paperclip Runner provides durable, provider-neutral agent
execution.
> - The current stack supports qualified local providers but omits the
managed provider paths from the integration branch.
> - Claude Managed Agents and AWS AgentCore need explicit profile
qualification, durable recovery, usage accounting, and cleanup controls.
> - This pull request adds those managed backends as the third part of
the Runner parity stack.
> - The benefit is managed execution without weakening the default-off
Runner rollout gate.

## Linked Issues or Issue Description

**Subsystem affected**

Cross-cutting: Runner, server orchestration, database profiles, CLI, and
adapter configuration UI.

**Problem or motivation**

The current Runner stack cannot select or execute the managed Claude
Agents API or AWS Bedrock AgentCore Harness backends. It also lacks
qualified profile storage and recovery checks for those remote
resources.

**Proposed solution**

Add qualified managed and remote profiles, API and CLI management, exact
provider selection, durable lifecycle handling, cumulative usage
accounting, bounded cleanup, and retention acknowledgement. Keep
`enableNativeRunner` default-off.

**Alternatives considered**

A direct copy of the old integration branch was rejected because its
provider contracts, model values, credential flow, and migration history
no longer match the current base. A single large parity pull request was
also rejected because stacked review keeps each subsystem bounded.

**Roadmap alignment**

This continues the existing Runner architecture and rollout work. It
does not introduce a separate execution system.

**Additional context**

This pull request is based on the merged #12691 and #12685 stack. It
also closes the delayed security-review findings reported on #12691 by
binding qualified ACPX and OpenCode launch artifacts to the bytes
actually executed. A GitHub search for managed agent, AgentCore, and
Claude managed work found no duplicate public issue or pull request.

## What Changed

- Add Claude Managed Agents and AWS AgentCore provider executors to
runnerd.
- Add qualified managed and remote profile storage, routes, OpenAPI
contracts, CLI commands, and migration 0237.
- Validate profile ownership, enabled state, exact qualified revision,
model, agent version, and secret binding before persistence and
recovery.
- Persist durable provider session and owned skill state for
restart-safe cleanup.
- Reconcile uncertain create responses and delete remote sessions before
owned skills.
- Track cumulative provider usage and enforce positive session spend
caps.
- Recover interrupted AgentCore usage at the next turn boundary by
charging the prior invocation ceiling exactly once; keep the session
gated until an explicit monotonic budget raise.
- Isolate AgentCore AWS configuration from host profiles and
credential-process/SSO configuration while preserving workload identity.
- Require OpenCode 1.18.17 and fixed build-owned provider-pack artifact
paths; remove the ambient executable override.
- Snapshot and content-verify ACPX and OpenCode commands, scripts, and
provider executables before launch. Linux executes sealed inherited
descriptors; macOS uses authenticated private snapshots with retry-safe
rematerialization at the spawn boundary.
- Persist canonical ACPX and OpenCode launch-profile digests, reject
drift across fresh recovery, and make recovery failures sticky.
- Close and journal unsafe ACPX active-turn recovery before any provider
bootstrap or reconnect.
- Add managed provider fields to the Runner configuration UI and
permission projection.
- Preserve the default-off `enableNativeRunner` experimental flag.

## Verification

- `pnpm -r typecheck`
- `pnpm build`
- Focused managed server, database, CLI, Runner TypeScript, Rust,
Claude, AgentCore, ACPX, OpenCode, process-supervisor, and
durable-recovery tests passed.
- `cargo test -p paperclip-runner-core --lib --locked` (160 tests)
- `cargo check --workspace --all-targets --locked`
- Native Codex integration tests passed (60 tests); native provider
tests passed (7 tests); server native-runtime tests passed (87 tests).
- Verified-launch replacement, nested-spawn retry, exact-version,
profile-drift, sticky-failure, and no-bootstrap active-recovery tests
passed.
- `git diff --check`
- The PR changes 91 files. `pnpm-lock.yaml` is unchanged. The Rust
workspace lockfile adds the approved `rustix` dependency used for safe
descriptor handling while `#![forbid(unsafe_code)]` remains enabled.

## Risks

- The provider APIs can change while they are in beta. Exact
qualification and fail-closed recovery checks limit drift.
- Remote cleanup can fail after a partial create. Durable ownership
inventories and retry-safe deletion preserve recovery state.
- Migration 0237 adds profile tables. The generated migration and
snapshot pass the repository migration checks.
- Managed execution can incur provider cost. Positive default spend caps
and explicit retention acknowledgement limit accidental use.
- An interrupted AgentCore invocation without final metadata is
conservatively charged to its active session ceiling. This can overstate
cost, but cannot undercount it; later work requires an explicit budget
increase.
- Linux qualified launches use sealed memory descriptors. macOS lacks
executable-descriptor APIs, so the runner uses owner-only private
snapshots and minimizes linked-path lifetime; hostile same-UID processes
remain outside the documented local-host trust boundary.
- The global Runner feature remains default-off.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

OpenAI Codex, GPT-5, with tool use, code execution, and subagent review.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change 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
2026-09-02 00:48:30 -05:00

434 lines
14 KiB
TypeScript

import { Command } from "commander";
import {
addCommonClientOptions,
apiPath,
handleCommandError,
printOutput,
resolveCommandContext,
type BaseClientOptions,
} from "./client/common.js";
const ANTHROPIC_ORIGIN = "https://api.anthropic.com";
const ANTHROPIC_VERSION = "2023-06-01";
export const CLAUDE_MANAGED_BETA_VERSION = "managed-agents-2026-04-01" as const;
export const CLAUDE_MANAGED_QUALIFIED_MODEL = "claude-sonnet-5" as const;
export const CLAUDE_MANAGED_SYSTEM_PROMPT =
"You are a Paperclip remote agent. Follow the current user turn and use only the custom tools supplied for that session. Paperclip tool authority, completion, blocking, review, and yielding are enforced by the runner. Never request or infer a Paperclip endpoint or credential.";
export interface ManagedAgentSetupOptions extends BaseClientOptions {
companyId?: string;
profileKey: string;
displayName: string;
apiKeySecretId: string;
model: string;
maxSessionListCostUsd: string;
agentId?: string;
agentVersion?: string;
environmentId?: string;
probe?: boolean;
acknowledgeRetention?: boolean;
}
interface RemoteResource {
id?: string;
[key: string]: unknown;
}
interface ValidatedSetup {
anthropicApiKey: string;
profileKey: string;
displayName: string;
apiKeySecretId: string;
model: string;
agentId?: string;
agentVersion?: string;
environmentId?: string;
defaultMaxListCostUsd: number;
}
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function record(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
}
function required(value: string | undefined, label: string): string {
const normalized = value?.trim() ?? "";
if (!normalized) throw new Error(`${label} is required`);
return normalized;
}
export function validateManagedAgentSetup(
options: ManagedAgentSetupOptions,
env: NodeJS.ProcessEnv = process.env,
): ValidatedSetup {
const anthropicApiKey = env.ANTHROPIC_API_KEY?.trim();
if (!anthropicApiKey) {
throw new Error("ANTHROPIC_API_KEY is required in the CLI process environment");
}
if (!options.acknowledgeRetention) {
throw new Error(
"Pass --acknowledge-retention to enable the stateful beta Managed Agents service",
);
}
const profileKey = required(options.profileKey, "--profile-key");
const displayName = required(options.displayName, "--display-name");
const apiKeySecretId = required(options.apiKeySecretId, "--api-key-secret-id");
const model = required(options.model, "--model");
if (model !== CLAUDE_MANAGED_QUALIFIED_MODEL) {
throw new Error(
`--model must be the qualified Managed Agents model ${CLAUDE_MANAGED_QUALIFIED_MODEL}`,
);
}
if (!UUID_RE.test(apiKeySecretId)) {
throw new Error("--api-key-secret-id must be a UUID");
}
const defaultMaxListCostUsd = Number(options.maxSessionListCostUsd);
const cents = Math.round(defaultMaxListCostUsd * 100);
if (
!Number.isFinite(defaultMaxListCostUsd)
|| defaultMaxListCostUsd <= 0
|| !Number.isSafeInteger(cents)
|| cents <= 0
) {
throw new Error("--max-session-list-cost-usd must resolve to at least one cent");
}
return {
anthropicApiKey,
profileKey,
displayName,
apiKeySecretId,
model,
agentId: options.agentId?.trim() || undefined,
agentVersion: options.agentVersion?.trim() || undefined,
environmentId: options.environmentId?.trim() || undefined,
defaultMaxListCostUsd,
};
}
async function anthropicRequest(
key: string,
method: "GET" | "POST",
path: string,
body?: Record<string, unknown>,
): Promise<Record<string, unknown>> {
const response = await fetch(`${ANTHROPIC_ORIGIN}${path}`, {
method,
headers: {
"x-api-key": key,
"anthropic-version": ANTHROPIC_VERSION,
"anthropic-beta": CLAUDE_MANAGED_BETA_VERSION,
...(body ? { "content-type": "application/json" } : {}),
},
...(body ? { body: JSON.stringify(body) } : {}),
signal: AbortSignal.timeout(15_000),
});
if (!response.ok) {
throw new Error(`Anthropic Managed Agents request failed with HTTP ${response.status}`);
}
if (response.status === 204) return {};
return record(await response.json());
}
async function listAll(key: string, path: string): Promise<RemoteResource[]> {
const rows: RemoteResource[] = [];
let page: string | null = null;
do {
const suffix = page ? `${path.includes("?") ? "&" : "?"}page=${encodeURIComponent(page)}` : "";
const response = await anthropicRequest(key, "GET", `${path}${suffix}`);
for (const value of Array.isArray(response.data) ? response.data : []) {
rows.push(record(value) as RemoteResource);
}
page = typeof response.next_page === "string" && response.next_page
? response.next_page
: null;
} while (page);
return rows;
}
function resourceByProfile(
resources: RemoteResource[],
profileKey: string,
resourceLabel: string,
): RemoteResource | null {
const matches = resources.filter(
(resource) =>
typeof resource.id === "string"
&& record(resource.metadata).paperclip_profile === profileKey,
);
if (matches.length > 1) {
throw new Error(
`Multiple Anthropic ${resourceLabel} resources use Paperclip profile ${profileKey}; pass an explicit resource ID`,
);
}
return matches[0] ?? null;
}
export function assertSafeManagedEnvironment(environment: Record<string, unknown>): void {
const config = record(environment.config);
const networking = record(config.networking);
const packages = record(config.packages);
const installed = Object.entries(packages)
.filter(([key]) => key !== "type")
.flatMap(([, value]) => (Array.isArray(value) ? value : [value]))
.filter((value) => value !== undefined && value !== null);
if (
environment.archived_at !== null
|| config.type !== "cloud"
|| networking.type !== "limited"
|| networking.allow_mcp_servers !== false
|| networking.allow_package_managers !== false
|| !Array.isArray(networking.allowed_hosts)
|| networking.allowed_hosts.length > 0
|| installed.length > 0
) {
throw new Error(
"Existing Anthropic Environment does not match Paperclip's no-network, no-package profile",
);
}
}
export function assertSafeManagedAgent(agent: Record<string, unknown>): void {
const model = typeof agent.model === "string" ? agent.model : record(agent.model).id;
if (
agent.archived_at !== null
|| agent.system !== CLAUDE_MANAGED_SYSTEM_PROMPT
|| typeof model !== "string"
|| !model
|| !Array.isArray(agent.tools)
|| agent.tools.length > 0
|| !Array.isArray(agent.mcp_servers)
|| agent.mcp_servers.length > 0
|| !Array.isArray(agent.skills)
|| agent.skills.length > 0
|| agent.multiagent != null
) {
throw new Error(
"Existing Anthropic Agent enables or omits the locked tools, MCP, skills, or multi-agent profile",
);
}
}
async function resolveEnvironment(
key: string,
options: ManagedAgentSetupOptions,
): Promise<Record<string, unknown>> {
if (options.environmentId) {
const environment = await anthropicRequest(
key,
"GET",
`/v1/environments/${encodeURIComponent(options.environmentId)}`,
);
assertSafeManagedEnvironment(environment);
return environment;
}
const existing = resourceByProfile(
await listAll(key, "/v1/environments"),
options.profileKey,
"Environment",
);
if (existing) {
assertSafeManagedEnvironment(existing);
return existing;
}
if (options.probe) throw new Error("Probe found no matching Anthropic Environment");
const environment = await anthropicRequest(key, "POST", "/v1/environments", {
name: `Paperclip · ${options.displayName}`,
description: "Paperclip remote-agent environment: no network or added packages.",
config: {
type: "cloud",
networking: {
type: "limited",
allow_mcp_servers: false,
allow_package_managers: false,
allowed_hosts: [],
},
packages: {
apt: [],
cargo: [],
gem: [],
go: [],
npm: [],
pip: [],
},
},
metadata: { paperclip_profile: options.profileKey },
});
assertSafeManagedEnvironment(environment);
return environment;
}
async function resolveAgent(
key: string,
options: ManagedAgentSetupOptions,
): Promise<Record<string, unknown>> {
if (options.agentId) {
const agent = await anthropicRequest(
key,
"GET",
`/v1/agents/${encodeURIComponent(options.agentId)}`,
);
assertSafeManagedAgent(agent);
assertManagedAgentModel(agent, options.model);
return agent;
}
const existing = resourceByProfile(
await listAll(key, "/v1/agents"),
options.profileKey,
"Agent",
);
if (existing) {
assertSafeManagedAgent(existing);
assertManagedAgentModel(existing, options.model);
return existing;
}
if (options.probe) throw new Error("Probe found no matching Anthropic Agent");
const agent = await anthropicRequest(key, "POST", "/v1/agents", {
name: `Paperclip · ${options.displayName}`,
description: "Versioned Paperclip remote agent; runnerd supplies session tools.",
model: options.model,
system: CLAUDE_MANAGED_SYSTEM_PROMPT,
tools: [],
mcp_servers: [],
skills: [],
metadata: { paperclip_profile: options.profileKey },
});
assertSafeManagedAgent(agent);
assertManagedAgentModel(agent, options.model);
return agent;
}
function assertManagedAgentModel(agent: Record<string, unknown>, expectedModel: string): void {
const model = typeof agent.model === "string" ? agent.model : record(agent.model).id;
if (model !== expectedModel) {
throw new Error(
`Existing Anthropic Agent model does not match the requested pinned model ${expectedModel}`,
);
}
}
export async function setupManagedAgent(options: ManagedAgentSetupOptions): Promise<void> {
const validated = validateManagedAgentSetup(options);
const normalizedOptions: ManagedAgentSetupOptions = {
...options,
profileKey: validated.profileKey,
displayName: validated.displayName,
apiKeySecretId: validated.apiKeySecretId,
model: validated.model,
agentId: validated.agentId,
agentVersion: validated.agentVersion,
environmentId: validated.environmentId,
};
const [environment, agent] = await Promise.all([
resolveEnvironment(validated.anthropicApiKey, normalizedOptions),
resolveAgent(validated.anthropicApiKey, normalizedOptions),
]);
const agentId = String(agent.id ?? "");
const environmentId = String(environment.id ?? "");
if (!agentId || !environmentId) {
throw new Error("Anthropic did not return usable Agent and Environment identities");
}
const versions = await listAll(
validated.anthropicApiKey,
`/v1/agents/${encodeURIComponent(agentId)}/versions`,
);
const version = normalizedOptions.agentVersion
?? String(agent.version ?? versions.at(-1)?.version ?? "");
const pinnedAgent = version
? versions.find((entry) => String(entry.version) === version)
: undefined;
if (!version || !pinnedAgent) {
throw new Error("Anthropic did not return a usable pinned Agent version");
}
if (String(pinnedAgent.id ?? "") !== agentId) {
throw new Error("Anthropic pinned Agent version identity does not match the selected Agent");
}
assertSafeManagedAgent(pinnedAgent);
assertManagedAgentModel(pinnedAgent, normalizedOptions.model);
const qualification = {
probedAt: new Date().toISOString(),
betaVersion: CLAUDE_MANAGED_BETA_VERSION,
environmentPolicy: "limited_no_hosts_no_packages",
agentCapabilities: "no_tools_no_mcp_no_skills_no_multiagent",
};
const profile = {
profileKey: normalizedOptions.profileKey,
displayName: normalizedOptions.displayName,
anthropicAgentId: agentId,
agentVersion: version,
environmentId,
defaultModel: normalizedOptions.model,
defaultMaxListCostUsd: validated.defaultMaxListCostUsd,
apiKeySecretId: normalizedOptions.apiKeySecretId,
enabled: !options.probe,
retentionAcknowledged: true,
qualification,
};
if (options.probe) {
printOutput({ mode: "probe", qualified: true, profile }, { json: options.json });
return;
}
const context = resolveCommandContext(options, { requireCompany: true });
const stored = await context.api.post(
apiPath`/api/companies/${context.companyId}/managed-agent-profiles`,
profile,
);
printOutput(stored, { json: context.json });
}
export function registerManagedAgentCommands(program: Command): void {
const command = program
.command("managed-agent")
.description("Provision and qualify remote managed-agent providers");
addCommonClientOptions(
command
.command("setup")
.description(
"Create or adopt a locked-down Anthropic Agent and Environment, then store a company profile",
)
.requiredOption("--profile-key <key>", "Stable company profile key")
.requiredOption("--display-name <name>", "Profile display name")
.requiredOption(
"--api-key-secret-id <id>",
"Existing company secret containing ANTHROPIC_API_KEY",
)
.option("--model <id>", "Pinned Claude model", CLAUDE_MANAGED_QUALIFIED_MODEL)
.option(
"--max-session-list-cost-usd <usd>",
"Default hard session ceiling",
"1.00",
)
.option("--agent-id <id>", "Adopt an existing Anthropic Agent")
.option("--agent-version <version>", "Pin an existing Agent version")
.option("--environment-id <id>", "Adopt an existing Anthropic Environment")
.option("--probe", "Read-only qualification; create or persist nothing", false)
.option(
"--acknowledge-retention",
"Acknowledge beta retention and non-ZDR/non-HIPAA status",
false,
)
.action(async (options: ManagedAgentSetupOptions) => {
try {
await setupManagedAgent(options);
} catch (error) {
handleCommandError(error);
}
}),
{ includeCompany: true },
);
}