Files
PaperClipAI/packages/shared/src/adapter-auth-session.ts
T
Nicky LeachandPaperclip e5a7fd7038 Add sandbox device-login for the Codex adapter (#11237)
## Thinking Path

> - Paperclip helps people manage AI agents for work.
> - Agent adapters connect Paperclip to tools such as the Codex command
line tool.
> - A sandboxed Codex agent may start without a credential.
> - The operator needs a safe sign-in flow that does not expose
credentials to the shared package or the sandbox.
> - This pull request adds a company-scoped device-login flow with a
temporary Daytona sandbox.
> - The flow promotes the credential only after readiness checks pass
and removes the temporary sandbox after use.
> - The result lets an operator sign in to a sandboxed Codex agent from
the agent form.

## Linked Issues or Issue Description

**Subsystem affected**

Cross-cutting (multiple of the above)

**Problem or motivation**

A Codex adapter that runs in a sandbox cannot authenticate when the
company has no pre-provisioned Codex credential.

**Proposed solution**

Add a company-scoped device-login session. Start a temporary sandbox,
run `codex login --device-auth`, stream the code and URL, verify
readiness, promote the credential, and delete the sandbox.

**Alternatives considered**

Pre-provisioning a credential does not support first-time sandbox login.
Keeping the credential in the login sandbox does not provide a durable
company credential.

**Roadmap alignment**

This supports the roadmap item for cloud and sandbox agents.

**Additional context**

The flow uses a five-minute cleanup reaper, compare-and-set status
changes, and a PostgreSQL advisory lock to protect promotion and
cleanup.

## What Changed

- Add the adapter login-session contract, database table, and migration.
- Add company-scoped server routes and a service for sandbox device
login.
- Add credential promotion, readiness checks, and cleanup after login.
- Add restart-safe cleanup for abandoned login sandboxes.
- Add sandbox login controls to the agent creation and edit forms.
- Keep device-login and vendor identifiers out of public shared and
adapter UI symbols.

## Verification

- `pnpm --filter @paperclipai/adapter-codex-local exec vitest run`
passed with 310 tests at the submitted commit.
- The server login route, service, and reaper tests passed with 45 tests
at the submitted commit.
- The agent form render tests passed with 26 tests at the submitted
commit.
- The public-symbol leak check passed at the submitted commit.
- A live Daytona sign-in flow still requires confirmation by a user with
a live sandbox.

## Risks

The migration adds a new company-scoped table. A promotion or cleanup
race could remove a credential or leave a sandbox active, so the service
uses claims, compare-and-set transitions, and an advisory lock. The live
Daytona flow needs operator confirmation because local tests do not
provide a real browser sign-in.

## Model Used

OpenAI Codex, GPT-5, tool use and code execution, extended reasoning.

## 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-08-12 08:58:25 -07:00

57 lines
2.1 KiB
TypeScript

import {
type AdapterAuthSessionInternalStatus,
type AdapterAuthSessionStatus,
} from "./types/agent.js";
// The status helpers for an adapter login session. The server and the user
// interface import these helpers, so both sides use one source. The helpers map
// the internal status to the public status and name the active statuses.
// The active internal statuses. The company credential slot allows one active
// session at a time, so the concurrency index applies to exactly these three
// statuses. The `promoting` state stays active because the slot still holds the
// company credential until the promotion window ends.
export const ADAPTER_AUTH_SESSION_ACTIVE_STATUSES = [
"starting",
"waiting_for_user",
"promoting",
] as const satisfies readonly AdapterAuthSessionInternalStatus[];
export type AdapterAuthSessionActiveStatus =
(typeof ADAPTER_AUTH_SESSION_ACTIVE_STATUSES)[number];
const ACTIVE_STATUS_SET: ReadonlySet<AdapterAuthSessionInternalStatus> = new Set(
ADAPTER_AUTH_SESSION_ACTIVE_STATUSES,
);
/** Returns true when the status holds the company credential slot. */
export function isActiveAdapterAuthSessionStatus(
status: AdapterAuthSessionInternalStatus,
): status is AdapterAuthSessionActiveStatus {
return ACTIVE_STATUS_SET.has(status);
}
/**
* Maps an internal status to the public status. The map hides the two internal
* states from a public response:
*
* - `promoting` maps to `waiting_for_user`.
* - `cleanup_pending` throws. It is a terminal-cleanup bookkeeping state. The
* caller must resolve the terminal status from the row before it builds a
* public response. The throw stops any accidental leak of the cleanup state.
*/
export function toPublicAdapterAuthSessionStatus(
status: AdapterAuthSessionInternalStatus,
): AdapterAuthSessionStatus {
switch (status) {
case "promoting":
return "waiting_for_user";
case "cleanup_pending":
throw new Error(
"cleanup_pending is an internal cleanup state; resolve the terminal status before you build a public response",
);
default:
return status;
}
}