Files
PaperClipAI/server/src/services/sandbox-provider-runtime.ts
Nicky LeachandPaperclip c1c46f1e4e feat: Claude login on the new-agent page before agent creation (#11347)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The Claude local adapter supports subscription login through a
sandbox
> - The new-agent page must show login before the user creates an agent
> - Test results must not expose raw sandbox diagnostics or secret
values
> - This pull request adds the login UI to both Test lanes and closes
the diagnostic boundary
> - The branch also adds durable cleanup recovery for failed sandbox
teardown
> - Reusable sandboxes must retain both their recorded teardown
configuration and a valid lifecycle path until destruction succeeds
> - The benefit is a usable login flow with fixed public checks,
redacted server logs, and recoverable sandbox cleanup

## Linked Issues or Issue Description

Related public work:
[#9488](https://github.com/paperclipai/paperclip/pull/9488) adds
first-class recognition for `CLAUDE_CODE_OAUTH_TOKEN` in headless and
remote runs. Related public issue:
[#2681](https://github.com/paperclipai/paperclip/issues/2681) requests
Claude Code subscription support. This pull request adds the login
transport and new-agent UI flow that those changes do not provide.

**Subsystem affected:** Claude local adapter, server login probes,
sandbox provider setup, cleanup recovery, and the new-agent UI.

**Problem or motivation:** The Test lanes did not show the sandbox login
panel in all supported cases. Test results also exposed raw probe
diagnostics, and JSON escapes could end secret redaction early.

**Proposed solution:** Surface the login capability through the bundled
provider manifest. Prepare the same probe runtime in the ACP lane. Send
diagnostics only to redacted server logs. Keep Test checks on fixed
public messages. Normalize login URL hints to allowlisted HTTPS Claude
and Anthropic hosts. Consume JSON escapes during redaction. Preserve
failed sandbox cleanup state across retries and restarts, and prevent
deletion from severing the lifecycle context of a live reusable sandbox.

**Alternatives considered:** Keep raw diagnostics in Test checks or
trust login URL text from the sandbox. Both choices increase information
exposure. Keep separate probe behavior in the ACP lane. That choice
would leave the two Test lanes inconsistent.

## What Changed

- Surface the sandbox login panel on both Test lanes.
- Reconcile the bundled Daytona plugin manifest so
`supportsSetupTokenLogin` reaches the UI capability gate.
- Prepare the ACP Test lane with the same probe runtime as the CLI Test
lane.
- Add the `claude_acp_login_probe_unavailable` warning when the ACP
probe cannot run.
- Send raw sandbox diagnostics only to redacted server logs.
- Keep Test checks on fixed public messages in the ACP, managed-config,
and CLI paths.
- Normalize login URL hints to allowlisted HTTPS Claude and Anthropic
hosts.
- Redact JSON and escaped-JSON secret values, including escaped quotes
and backslashes.
- Preserve orphan cleanup records across provider failures, restarts,
and unavailable plugins.
- Atomically block environment deletion while a live reusable sandbox
lease still depends on it.
- Verify pending cleanup destroys plugin sandboxes with the provider
configuration recorded on the lease, even after the current environment
configuration changes.

## Verification

- Head under review: `506b7fa2d83c36bfa5fd722ee9d95b0c7431c241`.
- Focused environment route/service/runtime coverage passes: 196 tests
across 3 files.
- `pnpm -r typecheck` passes.
- `pnpm build` passes.
- The full Vitest run completed with 4,754 passing and 28 failing tests.
All 23 source-test failures reproduce unchanged on parent head
`58cfe61a33191ce03d965d65085d26064b4888ba`; the other 5 are duplicate
executions from stale `server/dist` output. The failures are unrelated
macOS path/listener and scheduler-fixture failures, so there is no new
bad commit for bisect to localize.
- All required CI checks pass for the current head, including build,
typecheck/release registry, all server and workspace shards, serialized
server suites, canary, and e2e.
- A fresh Greptile review for `506b7fa2d83c36bfa5fd722ee9d95b0c7431c241`
reports 5/5, “safe to merge,” with no blocking failure remaining.

## Risks

- A probe or redaction change could hide useful server diagnostics.
- An allowlist change could reject a valid Claude login URL.
- Cleanup recovery changes could affect provider teardown ordering.
- An environment with a live reusable sandbox can no longer be deleted
until the owning issue or execution workspace completes teardown.
- The implementation keeps public Test messages fixed and sends detail
to redacted server logs.

## Model Used

OpenAI GPT-5 via Codex — exact model ID: GPT-5; tool use and code
execution enabled; extended reasoning enabled. The implementation author
used AI-assisted development.

## 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 documented the result
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation or confirmed no separate
documentation change is needed
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-08-17 13:42:51 -07:00

398 lines
13 KiB
TypeScript

import { randomUUID } from "node:crypto";
import type {
EnvironmentLeaseStatus,
EnvironmentProbeResult,
FakeSandboxEnvironmentConfig,
SandboxEnvironmentConfig,
SandboxEnvironmentProvider,
} from "@paperclipai/shared";
export interface SandboxProviderValidationResult {
ok: boolean;
summary: string;
details?: Record<string, unknown>;
}
export interface AcquireSandboxLeaseInput {
config: SandboxEnvironmentConfig;
environmentId: string;
heartbeatRunId: string;
issueId: string | null;
agentId?: string | null;
executionWorkspaceId?: string | null;
/**
* The absolute latest time the acquired lease may stay active, as an ISO 8601
* timestamp. A caller with an independent deadline sets it. The provider must
* configure a provider-side expiry at or before this time, and return the real
* provider expiry in `SandboxLeaseHandle.expiresAt`. When omitted, the provider
* keeps its default lifetime.
*/
requestedExpiresAt?: string | null;
}
export interface ResumeSandboxLeaseInput {
config: SandboxEnvironmentConfig;
providerLeaseId: string;
}
export interface ReleaseSandboxLeaseInput {
config: SandboxEnvironmentConfig;
providerLeaseId: string | null;
status: Extract<EnvironmentLeaseStatus, "released" | "expired" | "failed">;
}
export interface DestroySandboxLeaseInput {
config: SandboxEnvironmentConfig;
providerLeaseId: string | null;
}
export interface PrepareSandboxWorkspaceInput {
config: SandboxEnvironmentConfig;
providerLeaseId: string | null;
workspace: {
localPath?: string;
remotePath?: string;
mode?: string;
metadata?: Record<string, unknown>;
};
}
export interface SandboxExecuteInput {
config: SandboxEnvironmentConfig;
providerLeaseId: string | null;
command: string;
args?: string[];
cwd?: string;
env?: Record<string, string>;
stdin?: string;
timeoutMs?: number;
}
export interface SandboxLeaseHandle {
providerLeaseId: string;
metadata: Record<string, unknown>;
/**
* The real provider-side expiry the provider granted, as an ISO 8601 timestamp.
* A provider that honors a requested deadline returns the actual expiry it
* configured. Absent when the provider granted no bounded lifetime.
*/
expiresAt?: string | null;
}
export interface PreparedSandboxWorkspace {
remotePath?: string | null;
metadata?: Record<string, unknown>;
}
export interface SandboxExecuteResult {
exitCode: number | null;
stdout: string;
stderr: string;
}
export interface SandboxProvider {
readonly provider: SandboxEnvironmentProvider;
readonly supportsReusableLeases?: boolean;
validateConfig(config: SandboxEnvironmentConfig): Promise<SandboxProviderValidationResult>;
probe(config: SandboxEnvironmentConfig): Promise<EnvironmentProbeResult>;
acquireLease(input: AcquireSandboxLeaseInput): Promise<SandboxLeaseHandle>;
resumeLease(input: ResumeSandboxLeaseInput): Promise<SandboxLeaseHandle | null>;
releaseLease(input: ReleaseSandboxLeaseInput): Promise<void>;
destroyLease(input: DestroySandboxLeaseInput): Promise<void>;
matchesReusableLease(input: {
config: SandboxEnvironmentConfig;
lease: { providerLeaseId: string | null; metadata: Record<string, unknown> | null };
}): boolean;
configFromLeaseMetadata(metadata: Record<string, unknown>): SandboxEnvironmentConfig | null;
prepareWorkspace?(input: PrepareSandboxWorkspaceInput): Promise<PreparedSandboxWorkspace>;
execute?(input: SandboxExecuteInput): Promise<SandboxExecuteResult>;
}
function assertProviderConfig<T extends SandboxEnvironmentConfig>(
provider: SandboxEnvironmentProvider,
config: SandboxEnvironmentConfig,
): asserts config is T {
if (config.provider !== provider) {
throw new Error(`Sandbox provider "${provider}" received config for provider "${config.provider}".`);
}
}
function buildFakeSandboxProbe(config: FakeSandboxEnvironmentConfig): EnvironmentProbeResult {
return {
ok: true,
driver: "sandbox",
summary: `Fake sandbox provider is ready for image ${config.image}.`,
details: {
provider: config.provider,
image: config.image,
reuseLease: config.reuseLease,
},
};
}
class FakeSandboxProvider implements SandboxProvider {
readonly provider = "fake" as const;
readonly supportsReusableLeases = false;
async validateConfig(config: SandboxEnvironmentConfig): Promise<SandboxProviderValidationResult> {
assertProviderConfig<FakeSandboxEnvironmentConfig>(this.provider, config);
return {
ok: true,
summary: `Fake sandbox provider config is valid for image ${config.image}.`,
details: {
provider: config.provider,
image: config.image,
reuseLease: config.reuseLease,
},
};
}
async probe(config: SandboxEnvironmentConfig): Promise<EnvironmentProbeResult> {
assertProviderConfig<FakeSandboxEnvironmentConfig>(this.provider, config);
return buildFakeSandboxProbe(config);
}
async acquireLease(input: AcquireSandboxLeaseInput): Promise<SandboxLeaseHandle> {
assertProviderConfig<FakeSandboxEnvironmentConfig>(this.provider, input.config);
const providerLeaseId = input.config.reuseLease && this.supportsReusableLeases
? `sandbox://fake/${input.environmentId}/${input.executionWorkspaceId ?? "workspace"}/${input.agentId ?? "agent"}`
: `sandbox://fake/${input.heartbeatRunId}/${randomUUID()}`;
return {
providerLeaseId,
metadata: {
provider: input.config.provider,
image: input.config.image,
reuseLease: input.config.reuseLease,
},
};
}
async resumeLease(input: ResumeSandboxLeaseInput): Promise<SandboxLeaseHandle | null> {
assertProviderConfig<FakeSandboxEnvironmentConfig>(this.provider, input.config);
return {
providerLeaseId: input.providerLeaseId,
metadata: {
provider: input.config.provider,
image: input.config.image,
reuseLease: input.config.reuseLease,
resumedLease: true,
},
};
}
async releaseLease(): Promise<void> {
return;
}
async destroyLease(): Promise<void> {
return;
}
matchesReusableLease(input: {
config: SandboxEnvironmentConfig;
lease: { providerLeaseId: string | null; metadata: Record<string, unknown> | null };
}): boolean {
assertProviderConfig<FakeSandboxEnvironmentConfig>(this.provider, input.config);
return (
typeof input.lease.providerLeaseId === "string" &&
input.lease.providerLeaseId.length > 0 &&
input.lease.metadata?.provider === input.config.provider &&
input.lease.metadata?.reuseLease === true &&
input.lease.metadata?.image === input.config.image
);
}
configFromLeaseMetadata(metadata: Record<string, unknown>): SandboxEnvironmentConfig | null {
if (metadata.provider !== this.provider || typeof metadata.image !== "string") {
return null;
}
return {
provider: this.provider,
image: metadata.image,
reuseLease: metadata.reuseLease === true,
};
}
}
// ---------------------------------------------------------------------------
// Provider registry — built-in providers only.
// Plugin-backed providers are resolved through the plugin environment driver
// system at the environment-runtime layer.
// ---------------------------------------------------------------------------
const registeredSandboxProviders = new Map<SandboxEnvironmentProvider, SandboxProvider>([
["fake", new FakeSandboxProvider()],
]);
/**
* Returns a built-in sandbox provider, or null if the provider key is not
* registered. Plugin-backed providers are not returned here — they are
* resolved through the plugin worker manager at the environment-runtime level.
*/
export function getSandboxProvider(provider: string): SandboxProvider | null {
return registeredSandboxProviders.get(provider as SandboxEnvironmentProvider) ?? null;
}
export function requireSandboxProvider(provider: string): SandboxProvider {
const sandboxProvider = getSandboxProvider(provider);
if (!sandboxProvider) {
throw new Error(`Sandbox provider "${provider}" is not registered as a built-in provider.`);
}
return sandboxProvider;
}
/**
* Returns true if the given provider key is handled by a built-in sandbox
* provider (as opposed to a plugin-backed provider).
*/
export function isBuiltinSandboxProvider(provider: string): boolean {
return registeredSandboxProviders.has(provider as SandboxEnvironmentProvider);
}
export function listSandboxProviders(): SandboxProvider[] {
return [...registeredSandboxProviders.values()];
}
export async function validateSandboxProviderConfig(
config: SandboxEnvironmentConfig,
): Promise<SandboxProviderValidationResult> {
return await requireSandboxProvider(config.provider).validateConfig(config);
}
export function sandboxConfigFromLeaseMetadata(
lease: Pick<{ metadata: Record<string, unknown> | null }, "metadata">,
): SandboxEnvironmentConfig | null {
const metadata = lease.metadata ?? {};
const provider = typeof metadata.provider === "string" ? getSandboxProvider(metadata.provider) : null;
return provider?.configFromLeaseMetadata(metadata) ?? null;
}
/**
* Reconstruct a sandbox environment config from lease metadata, including
* plugin-backed providers. For plugin-backed providers, the
* config is synthesized from lease metadata fields without requiring the
* built-in provider to be registered.
*/
export function sandboxConfigFromLeaseMetadataLoose(
lease: Pick<{ metadata: Record<string, unknown> | null }, "metadata">,
): SandboxEnvironmentConfig | null {
const metadata = lease.metadata ?? {};
const providerKey = typeof metadata.provider === "string" ? metadata.provider : null;
if (!providerKey) return null;
// Try built-in provider first.
const builtinProvider = getSandboxProvider(providerKey);
if (builtinProvider) {
return builtinProvider.configFromLeaseMetadata(metadata);
}
return {
...metadata,
provider: providerKey,
reuseLease: metadata.reuseLease === true,
} satisfies SandboxEnvironmentConfig;
}
export function findReusableSandboxProviderLeaseId(input: {
config: SandboxEnvironmentConfig;
leases: Array<{ providerLeaseId: string | null; metadata: Record<string, unknown> | null }>;
}): string | null {
const provider = getSandboxProvider(input.config.provider);
if (!provider) {
for (const lease of input.leases) {
const metadata = lease.metadata ?? {};
if (
typeof lease.providerLeaseId === "string" &&
lease.providerLeaseId.length > 0 &&
metadata.provider === input.config.provider &&
metadataMatchesPluginSandboxConfig(input.config, metadata)
) {
return lease.providerLeaseId;
}
}
return null;
}
for (const lease of input.leases) {
if (provider.matchesReusableLease({ config: input.config, lease })) {
return lease.providerLeaseId;
}
}
return null;
}
function metadataMatchesPluginSandboxConfig(
config: SandboxEnvironmentConfig,
metadata: Record<string, unknown>,
): boolean {
if (metadata.reuseLease !== true) return false;
for (const [key, value] of Object.entries(config)) {
if (key === "provider" || key === "reuseLease") continue;
if (value === undefined) continue;
if (JSON.stringify(metadata[key]) !== JSON.stringify(value)) {
return false;
}
}
return true;
}
export async function probeSandboxProvider(
config: SandboxEnvironmentConfig,
): Promise<EnvironmentProbeResult> {
return await requireSandboxProvider(config.provider).probe(config);
}
export async function acquireSandboxProviderLease(input: {
config: SandboxEnvironmentConfig;
environmentId: string;
heartbeatRunId: string;
issueId: string | null;
agentId?: string | null;
executionWorkspaceId?: string | null;
reusableProviderLeaseId?: string | null;
requestedExpiresAt?: string | null;
}): Promise<SandboxLeaseHandle> {
const provider = requireSandboxProvider(input.config.provider);
if (provider.supportsReusableLeases && input.config.reuseLease && input.reusableProviderLeaseId) {
const resumedLease = await provider.resumeLease({
config: input.config,
providerLeaseId: input.reusableProviderLeaseId,
});
if (resumedLease) {
return resumedLease;
}
}
return await provider.acquireLease({
config: input.config,
environmentId: input.environmentId,
heartbeatRunId: input.heartbeatRunId,
issueId: input.issueId,
agentId: input.agentId,
executionWorkspaceId: input.executionWorkspaceId,
requestedExpiresAt: input.requestedExpiresAt,
});
}
export async function resumeSandboxProviderLease(input: {
config: SandboxEnvironmentConfig;
providerLeaseId: string;
}): Promise<SandboxLeaseHandle | null> {
return await requireSandboxProvider(input.config.provider).resumeLease(input);
}
export async function releaseSandboxProviderLease(input: {
config: SandboxEnvironmentConfig;
providerLeaseId: string | null;
status: Extract<EnvironmentLeaseStatus, "released" | "expired" | "failed">;
}): Promise<void> {
await requireSandboxProvider(input.config.provider).releaseLease(input);
}
export async function destroySandboxProviderLease(input: {
config: SandboxEnvironmentConfig;
providerLeaseId: string | null;
}): Promise<void> {
await requireSandboxProvider(input.config.provider).destroyLease(input);
}