Files
PaperClipAI/server/src/services/login-environment.ts
T
Nicky LeachandPaperclip 07af22c57f feat(plugins): support agentless harness sign-in (#23392)
## Thinking Path

> - Paperclip manages agents and the AI accounts they use.
> - A company can connect an account before creating its first agent.
> - Some plugin drivers can run sign-in in a dedicated sandbox.
> - Core currently selects a saved agent for every plugin login.
> - This change lets each driver declare that it can sign in without an
agent.

## Linked Issues or Issue Description

**Subsystem affected**

Environment drivers, managed AI connections, and the agent setup UI.

**Current behavior**

A company with zero agents cannot start a provider login on a plugin
environment, even when that driver can provision a temporary
environment.

**Proposed behavior**

A driver can declare `supportsAgentlessLogin`. Core uses this capability
when it selects a login target and acquires the lease. Drivers that omit
it retain the existing agent selection.

**Reason and benefit**

Operators can finish connecting the account before they configure its
first agent.

**Additional context**

The driver capability follows the per-driver adapter declaration in
#22827. The onboarding flow keeps the agent readiness check from #23383.

## What Changed

- Added the optional capability to the manifest, capabilities response,
and login UI.
- Kept company and driver validation at sign-in and lease acquisition.
- Applied the plugin device-login deadline to sessions with no agent.
Ordinary task provisioning keeps its existing contract.

## Verification

- 122 tests passed across shared capabilities, environment target
resolution, lease construction, device-login HTTP routes, and session
lifecycle.
- The server typecheck passed.
- The SDK input registry matches the source revision. `pnpm verify`
requests broader CI coverage for that registry.
- After merging the current base, 82 focused tests passed, including the
onboarding tests. The UI typecheck passed. All required GitHub checks
passed.

## Risks

Drivers must advertise this capability only when they can run and clean
up a login without an agent. Existing manifests retain their previous
behavior. No database migration is required.

> 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-6, with local code inspection and test execution.

## 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

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-12-01 10:22:32 -08:00

76 lines
4.7 KiB
TypeScript

import { agents, environmentLeases, type Db } from "@paperclipai/db";
import { adapterLoginCommandKey } from "@paperclipai/shared";
import { and, asc, eq, isNull, notExists, notInArray, or, sql } from "drizzle-orm";
import { badRequest, forbidden, notFound } from "../errors.js";
import { assertEnvironmentSelectionForCompany } from "./environment-selection.js";
import { environmentService } from "./environments.js";
import { pluginRegistryService } from "./plugin-registry.js";
import { instanceSettingsService } from "./instance-settings.js";
type LoginEnvironmentTarget = { companyId: string; environmentId: string; agentId?: string; adapterType?: string };
export async function assertLoginEnvironmentTarget(db: Db, input: LoginEnvironmentTarget) {
return (await resolveLoginEnvironmentTarget(db, input)).environment;
}
/** Resolve the transport target once at start. Subsequent lease checks use its exact agent ID. */
export async function resolveLoginEnvironmentTarget(
db: Db,
input: LoginEnvironmentTarget & { selectComputer?: boolean },
) {
const environments = environmentService(db);
await assertEnvironmentSelectionForCompany(environments, input.companyId, input.environmentId, {
allowedDrivers: ["sandbox", "plugin"],
});
const environment = await environments.getById(input.environmentId);
if (!environment) throw notFound("Environment not found");
const companies = await environments.listBoundCompanyIds(environment.id);
if (companies.length && !companies.includes(input.companyId)) throw forbidden("The selected environment belongs to another company.", { code: "environment_company_mismatch" });
if (environment.driver !== "plugin") {
if (input.agentId) throw badRequest("A temporary sandbox login does not take an agent computer target.");
return { environment, agentId: undefined };
}
if (input.adapterType && !adapterLoginCommandKey(input.adapterType)) throw badRequest("This provider login is not supported on agent computers.", { code: "login_adapter_unsupported" });
if (!input.agentId) {
const config = environment.config;
if (typeof config?.pluginKey === "string" && typeof config.driverKey === "string") {
const plugin = await pluginRegistryService(db).getByKey(config.pluginKey);
const driver = plugin?.manifestJson.environmentDrivers?.find(item => item.driverKey === config.driverKey);
if (plugin?.status === "ready" && driver?.kind !== "sandbox_provider" && driver?.supportsLoginPty && driver.supportsAgentlessLogin) {
return { environment, agentId: undefined };
}
}
if (!input.selectComputer) throw badRequest("Select an agent computer for sign-in.", { code: "login_agent_required" });
const settings = await instanceSettingsService(db).get();
const matchesEnvironment = eq(agents.defaultEnvironmentId, environment.id);
const activeLogin = db.select({ id: environmentLeases.id }).from(environmentLeases).where(and(
eq(environmentLeases.companyId, input.companyId),
eq(environmentLeases.environmentId, environment.id),
eq(environmentLeases.status, "active"),
sql`${environmentLeases.metadata}->>'purpose' = 'login'`,
sql`${environmentLeases.metadata}->>'agentId' = ${agents.id}::text`,
));
const [agent] = await db.select({ id: agents.id }).from(agents).where(and(
eq(agents.companyId, input.companyId),
eq(agents.adapterType, "paperclip_runner"),
notExists(activeLogin),
settings.defaultEnvironmentId === environment.id
? or(matchesEnvironment, isNull(agents.defaultEnvironmentId))
: matchesEnvironment,
or(
and(eq(agents.lifecycleState, "ready"), notInArray(agents.status, ["paused", "terminated", "pending_approval"])),
and(eq(agents.lifecycleState, "verifying"), notInArray(agents.status, ["terminated", "pending_approval"])),
),
)).orderBy(asc(agents.lifecycleState), asc(agents.id)).limit(1);
if (!agent) throw badRequest("No agent computer is available for sign-in.", { code: "login_agent_unavailable" });
return { environment, agentId: agent.id };
}
const [agent] = await db.select().from(agents).where(and(eq(agents.id, input.agentId), eq(agents.companyId, input.companyId))).limit(1);
if (!agent || agent.companyId !== input.companyId) throw notFound("Agent not found");
const inherited = agent.defaultEnvironmentId ?? (await instanceSettingsService(db).get()).defaultEnvironmentId;
if (inherited !== environment.id || agent.adapterType !== "paperclip_runner" || !["ready", "verifying"].includes(agent.lifecycleState)) {
throw badRequest("The selected agent computer is not available for sign-in.", { code: "login_agent_unavailable" });
}
return { environment, agentId: agent.id };
}