Files
PaperClipAI/packages/shared/src/environment-support.ts
T
Nicky LeachandClaude Opus 5.5 1949a8fa10 fix(onboarding): sign in through a plugin-managed execution environment (#23312)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Onboarding connects the first agent to an AI provider account, often
with a Claude or Codex subscription sign-in
> - A plugin can manage an execution environment whose driver declares
`supportsLoginPty` and `requiresAgent`. Each agent's lifecycle prepares
that environment for the agent, and the sign-in runs in it
> - An instance can make such a plugin-managed execution environment its
default
> - The onboarding wizard offered browser sign-in only in a
sandbox-driver environment, so it blocked the connect step in every
plugin-managed execution environment
> - The sign-in also cannot start until the agent's lifecycle has
prepared the environment
> - This pull request lets onboarding and the new-agent flow sign in
through a plugin-managed execution environment, after the environment is
prepared for the agent
> - The benefit is that subscription sign-in works when the default
environment is plugin-managed

## Linked Issues or Issue Description

Refs #22695 and #20906.

**What happened?**

The default environment is a plugin-managed execution environment whose
driver declares `supportsLoginPty`. A user starts onboarding and chooses
Claude or Codex with a subscription. The connect step shows "This
environment does not support browser sign-in. Go back to use an API key
or choose another model." Connect stays disabled.

**What did you expect to happen?**

The connect step starts a browser sign-in in the plugin-managed
execution environment for the onboarding agent. The agent configuration
form and the AI Connections page already do this for plugin-managed
environments.

**Steps to reproduce**

1. Install an environment plugin with a `requiresAgent` driver that
declares `supportsLoginPty`, and make its environment the instance
default.
2. Start onboarding for a new company.
3. Choose Claude or Codex, and keep the subscription option.
4. See the message above. Connect is disabled.

## What Changed

- The onboarding wizard and the new-agent flow use the shared
`environmentSupportsLoginPty` rule. Before, both flows had their own
sandbox-only check.
- In a plugin-managed execution environment, onboarding sends the
sign-in to its own setup agent (`requiresAgent` and `agentId` on
`AdapterLoginPanel`). The new-agent flow does the same with its draft
agent.
- Onboarding polls the setup agent until the environment can take the
sign-in for it: a Paperclip runner agent on the login environment in
`verifying` or `ready`. Until then the step shows "Preparing your
agent's computer…", the user-facing name for this environment. A failed
preparation shows the error and a "Retry preparation" button. A failed
agent read shows "Could not check your agent's computer." and retries
the read.
- The new-agent flow keeps the login panel unmounted until the
environment is prepared (`loginHostPending` on
`AgentProviderConnection`). The panel starts the sign-in on mount, and
the disabled fieldset does not stop that.
- Both flows latch the readiness per agent and environment
(`useAgentComputerLoginReady`). A configuration change sends the setup
draft through preparation again. That must not unmount a sign-in that is
already on screen.
- The auth-signal route now accepts a plugin-managed execution
environment for `claude_local` and `codex_local`, and answers `unknown`
there. Before, it returned 422, and the onboarding step stayed undecided
and never showed the sign-in. The signal reads only host-local state,
which says nothing about a plugin-managed environment.
- The new-agent flow now says "Preparing your agent's computer…", the
same as onboarding.
- Each part of the login PTY question now has one owner:
- Shared `environmentLoginRequiresAgent` decides whether a sign-in needs
an agent. The four UI flows and the server's login target resolver use
it instead of their own `driver === "plugin"` checks.
- Shared `isPluginEnvironmentLoginAdapter` and
`pluginEnvironmentLoginReadsCredential` decide which adapters sign in
through a plugin-managed execution environment, and which of them must
read the credential back. `environmentSupportsLoginPty` and the server's
login target resolver use them. The server's capability resolver takes
the adapter type instead of a credential-read flag.
- `sandboxProviderSupportsLoginPty` decides sandbox-provider support for
both the capability listing and the sign-in check.
- The credential read uses `pluginSupportsLoginPty`, like the rest of
the plugin login path. Before, it checked only the manifest flag and the
read method.
- The agent configuration form's gate is now the shared rule plus "not
in create mode when an agent is required". Its duplicate driver check
and an always-true condition are gone.

## Verification

- `pnpm typecheck` in `ui/` and `server/`: no errors.
- `npx vitest run src/lib/agent-computer-login.test.ts
src/lib/agent-computer-login.hook.test.tsx
src/components/new-agent/AgentProviderConnection.computer-login.test.tsx
src/components/new-agent/AgentProviderConnection.test.tsx` in `ui/`:
tests pass. The `AgentProviderConnection` sign-in test covers a slow
preparation, and the readiness hook test covers a re-preparation of the
same environment.
- `npx vitest run src/__tests__/adapter-auth-signal-routes.test.ts` in
`server/`: tests pass. The two new plugin-managed environment cases fail
without the route change.
- `pnpm typecheck` in `packages/shared/`: no errors. `npx vitest run
src/environment-support.test.ts` in `packages/shared/`: 9 tests pass.
- In `server/`: the device-login, setup-token, login-session runtime,
sandbox-capability, credential-read and OpenAPI route suites pass (159
tests). A new credential-read case refuses a driver that does not meet
the plugin login rule.
- In `ui/`: the `AgentConfigForm`, `AgentProviderConnection`,
`agent-computer-login` and onboarding analytics suites pass.
- I did not do a live onboarding with a real plugin-managed execution
environment. Reviewers who have such an instance can reproduce with the
steps above. Expect "Preparing your agent's computer…", then the
provider sign-in, then the hire.

## Risks

- Onboarding waits for the environment's preparation before a
subscription sign-in in a plugin-managed execution environment. A slow
preparation makes the connect step wait for that time. An API key or a
saved subscription does not wait.
- The latch keeps the sign-in on screen while a configuration change
prepares the same environment again. If the plugin refuses the sign-in
in that window, the sign-in panel shows its usual error and retry.
- The setup draft must resolve to the Paperclip runner in the
plugin-managed environment. If it does not, the step stays on "Preparing
your agent's computer…". Plugin-managed environments run only the
Paperclip runner, so this is not expected.
- No migration and no API contract change. The auth-signal route accepts
one more environment driver.
- The credential read now requires the full plugin login rule
(`creationOperationVersion` 1 and every login worker method). A plugin
that reaches the credential read already passed that rule when its login
lease was acquired, so no working plugin should change.

## Model Used

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

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [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>
2026-11-30 14:10:58 -08:00

232 lines
9.3 KiB
TypeScript

import type { AgentAdapterType, EnvironmentDriver } from "./constants.js";
import type { SandboxEnvironmentProvider } from "./types/environment.js";
import type {
JsonSchema,
PluginEnvironmentTemplateConfigBinding,
SandboxProviderCapabilities,
} from "./types/plugin.js";
/**
* Resolve the DECLARED sandbox capabilities of a provider driver, with the
* legacy `supportsReusableLeases` flag folded in for compatibility.
*
* The result is a partial: a key is present only when the driver declared it,
* so a caller can tell a declared `false` from an absent key. The nested
* `sandboxCapabilities.reusableLeases` wins over the legacy flag when both are
* present. This is the DECLARATION only; the runtime still intersects it with
* the verified worker methods and any narrowing before it grants a capability.
*/
export function resolveDeclaredSandboxCapabilities(
driver: {
supportsReusableLeases?: boolean;
sandboxCapabilities?: SandboxProviderCapabilities;
},
): SandboxProviderCapabilities {
const declared: SandboxProviderCapabilities = { ...(driver.sandboxCapabilities ?? {}) };
if (declared.reusableLeases === undefined && driver.supportsReusableLeases !== undefined) {
declared.reusableLeases = driver.supportsReusableLeases;
}
return declared;
}
export type EnvironmentSupportStatus = "supported" | "unsupported";
export interface AdapterEnvironmentSupport {
adapterType: AgentAdapterType;
drivers: Record<EnvironmentDriver, EnvironmentSupportStatus>;
sandboxProviders: Record<SandboxEnvironmentProvider, EnvironmentSupportStatus>;
}
export interface EnvironmentProviderCapability {
status: EnvironmentSupportStatus;
supportsSavedProbe: boolean;
supportsUnsavedProbe: boolean;
supportsRunExecution: boolean;
supportsReusableLeases: boolean;
supportsInteractiveSetup: boolean;
interactiveSetupConnectionTypes: string[];
supportsTemplateCapture: boolean;
templateRefKind?: string;
templateConfigBinding?: PluginEnvironmentTemplateConfigBinding;
supportsTemplateDelete: boolean;
supportsLoginPty: boolean;
displayName?: string;
description?: string;
source?: "builtin" | "plugin";
pluginKey?: string;
pluginId?: string;
configSchema?: JsonSchema;
}
export interface EnvironmentCapabilities {
/** Namespaced plugin driver keys that can host a login on a saved agent. */
loginPtyDrivers?: Record<string, { supportsLoginPty: boolean; supportsCodexLogin?: boolean }>;
adapters: AdapterEnvironmentSupport[];
drivers: Record<EnvironmentDriver, EnvironmentSupportStatus>;
sandboxProviders: Record<SandboxEnvironmentProvider, EnvironmentProviderCapability>;
}
const REMOTE_MANAGED_ADAPTERS = new Set<AgentAdapterType>([
"claude_local",
"codex_local",
"paperclip_runner",
"cursor",
"gemini_local",
"grok_local",
"kimi_local",
"opencode_local",
"pi_local",
]);
export function adapterSupportsRemoteManagedEnvironments(adapterType: string): boolean {
return REMOTE_MANAGED_ADAPTERS.has(adapterType as AgentAdapterType);
}
export function supportedEnvironmentDriversForAdapter(adapterType: string): EnvironmentDriver[] {
if (adapterType === "paperclip_runner") return ["local", "ssh", "sandbox", "plugin"];
return adapterSupportsRemoteManagedEnvironments(adapterType)
? ["local", "ssh", "sandbox"]
: ["local"];
}
export function supportedSandboxProvidersForAdapter(
adapterType: string,
additionalProviders: readonly string[] = [],
): SandboxEnvironmentProvider[] {
return adapterSupportsRemoteManagedEnvironments(adapterType)
? Array.from(new Set(additionalProviders)) as SandboxEnvironmentProvider[]
: [];
}
export function isEnvironmentDriverSupportedForAdapter(
adapterType: string,
driver: string,
): boolean {
return supportedEnvironmentDriversForAdapter(adapterType).includes(driver as EnvironmentDriver);
}
export function isSandboxProviderSupportedForAdapter(
adapterType: string,
provider: string | null | undefined,
additionalProviders: readonly string[] = [],
): boolean {
if (!provider) return false;
return supportedSandboxProvidersForAdapter(adapterType, additionalProviders).includes(
provider as SandboxEnvironmentProvider,
);
}
export function getAdapterEnvironmentSupport(
adapterType: AgentAdapterType,
additionalSandboxProviders: readonly string[] = [],
): AdapterEnvironmentSupport {
const supportedDrivers = new Set(supportedEnvironmentDriversForAdapter(adapterType));
const supportedProviders = new Set(supportedSandboxProvidersForAdapter(adapterType, additionalSandboxProviders));
const sandboxProviders: Record<SandboxEnvironmentProvider, EnvironmentSupportStatus> = {
fake: "unsupported",
};
for (const provider of additionalSandboxProviders) {
sandboxProviders[provider as SandboxEnvironmentProvider] = supportedProviders.has(provider as SandboxEnvironmentProvider)
? "supported"
: "unsupported";
}
return {
adapterType,
drivers: {
local: supportedDrivers.has("local") ? "supported" : "unsupported",
ssh: supportedDrivers.has("ssh") ? "supported" : "unsupported",
sandbox: supportedDrivers.has("sandbox") ? "supported" : "unsupported",
plugin: supportedDrivers.has("plugin") ? "supported" : "unsupported",
},
sandboxProviders,
};
}
export function getEnvironmentCapabilities(
adapterTypes: readonly AgentAdapterType[],
options: {
sandboxProviders?: Record<string, Partial<EnvironmentProviderCapability>>;
} = {},
): EnvironmentCapabilities {
const pluginProviderKeys = Object.keys(options.sandboxProviders ?? {});
const sandboxProviders: Record<SandboxEnvironmentProvider, EnvironmentProviderCapability> = {
fake: {
status: "unsupported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: false,
// The fake provider runtime declares false. The presentation must match
// it, so execution and presentation agree.
supportsReusableLeases: false,
supportsInteractiveSetup: false,
interactiveSetupConnectionTypes: [],
supportsTemplateCapture: false,
supportsTemplateDelete: false,
supportsLoginPty: false,
displayName: "Fake",
source: "builtin",
},
};
for (const [provider, capability] of Object.entries(options.sandboxProviders ?? {})) {
sandboxProviders[provider as SandboxEnvironmentProvider] = {
status: capability.status ?? "supported",
supportsSavedProbe: capability.supportsSavedProbe ?? true,
supportsUnsavedProbe: capability.supportsUnsavedProbe ?? true,
supportsRunExecution: capability.supportsRunExecution ?? true,
// Default absent to false. A manifest that does not declare reusable
// leases must present as false, so execution (=== true) agrees.
supportsReusableLeases: capability.supportsReusableLeases ?? false,
supportsInteractiveSetup: capability.supportsInteractiveSetup ?? false,
interactiveSetupConnectionTypes: capability.interactiveSetupConnectionTypes ?? [],
supportsTemplateCapture: capability.supportsTemplateCapture ?? false,
templateRefKind: capability.templateRefKind,
templateConfigBinding: capability.templateConfigBinding,
supportsTemplateDelete: capability.supportsTemplateDelete ?? false,
supportsLoginPty: capability.supportsLoginPty ?? false,
displayName: capability.displayName,
description: capability.description,
source: capability.source ?? "plugin",
pluginKey: capability.pluginKey,
pluginId: capability.pluginId,
configSchema: capability.configSchema,
};
}
return {
adapters: adapterTypes.map((adapterType) => getAdapterEnvironmentSupport(adapterType, pluginProviderKeys)),
drivers: {
local: "supported",
ssh: "supported",
sandbox: "supported",
plugin: adapterTypes.includes("paperclip_runner") ? "supported" : "unsupported",
},
sandboxProviders,
};
}
export const PLUGIN_ENVIRONMENT_LOGIN_ADAPTER_TYPES = ["claude_local", "codex_local"] as const;
export function isPluginEnvironmentLoginAdapter(adapterType: string): boolean {
return (PLUGIN_ENVIRONMENT_LOGIN_ADAPTER_TYPES as readonly string[]).includes(adapterType);
}
export function pluginEnvironmentLoginReadsCredential(adapterType: string): boolean {
return adapterType === "codex_local";
}
export function environmentLoginRequiresAgent(environment: { driver: string } | null | undefined): boolean {
return environment?.driver === "plugin";
}
/** Shared presentation rule for both temporary sandboxes and existing computers. */
export function environmentSupportsLoginPty(environment: { driver: string; config: Record<string, unknown> } | null | undefined, capabilities: EnvironmentCapabilities | null | undefined, adapterType?: string): boolean {
if (!environment || !capabilities) return false;
if (environment.driver === "sandbox") return typeof environment.config.provider === "string" && capabilities.sandboxProviders[environment.config.provider]?.supportsLoginPty === true;
if (environmentLoginRequiresAgent(environment)) {
const driver = capabilities.loginPtyDrivers?.[`${environment.config.pluginKey}:${environment.config.driverKey}`];
return driver?.supportsLoginPty === true
&& (adapterType === undefined || (isPluginEnvironmentLoginAdapter(adapterType)
&& (!pluginEnvironmentLoginReadsCredential(adapterType) || driver.supportsCodexLogin === true)));
}
return false;
}