mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-27 11:20:42 +01:00
## 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>
77 lines
3.4 KiB
TypeScript
77 lines
3.4 KiB
TypeScript
import { open, rename, rm } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { randomUUID } from "node:crypto";
|
|
import { withDirectoryMergeLock } from "@paperclipai/adapter-utils/workspace-restore-merge";
|
|
import { USE_SOURCE_EXIT, decideCodexAuthMerge } from "./codex-auth-merge-decision.js";
|
|
|
|
// The one atomic credential writer. It stages the source bytes into a private
|
|
// (0600) temp next to the destination, runs the shared decision predicate, and
|
|
// on a use-source decision renames the temp over the destination. The rename is
|
|
// an atomic same-directory swap that preserves mode 0600. The whole write runs
|
|
// under the directory merge lock, so a concurrent restore, copy-back, or
|
|
// promotion can never interleave. The per-identity cache slot and the
|
|
// device-login company home both use this writer, so the staged-rename and lock
|
|
// logic lives in one place. It never logs token bytes; the caller passes the
|
|
// fixed status lines.
|
|
|
|
export type WriteCredentialSeedOrNewerOutcome = "written" | "kept";
|
|
|
|
export interface WriteCredentialSeedOrNewerInput {
|
|
/** The source credential bytes to (maybe) install. */
|
|
sourceBytes: Buffer;
|
|
/** The absolute destination path to (maybe) overwrite. */
|
|
destinationPath: string;
|
|
/** Fill an absent destination from a usable subscription source. */
|
|
seedIfDestAbsent: boolean;
|
|
/** A non-leaking progress sink. It receives only the two fixed status lines. */
|
|
log: (line: string) => void | Promise<void>;
|
|
/** The fixed status line for a use-source write. It carries no secret data. */
|
|
writtenLine: string;
|
|
/** The fixed status line for a keep-destination decision. */
|
|
keptLine: string;
|
|
/** A safe, non-secret prefix for the staged temp file name. */
|
|
tempPrefix: string;
|
|
/** The caller name that prefixes a predicate error. */
|
|
errorLabel: string;
|
|
}
|
|
|
|
/**
|
|
* Writes `sourceBytes` over `destinationPath` when the shared predicate returns a
|
|
* use-source decision, else keeps the destination. Seeds an absent destination
|
|
* only when `seedIfDestAbsent` is set and the source is a usable subscription
|
|
* credential. The staged temp is always removed, so a failure never leaves a
|
|
* partial file.
|
|
*/
|
|
export async function writeCredentialSeedOrNewer(
|
|
input: WriteCredentialSeedOrNewerInput,
|
|
): Promise<WriteCredentialSeedOrNewerOutcome> {
|
|
const destinationDir = path.dirname(input.destinationPath);
|
|
return withDirectoryMergeLock(destinationDir, async () => {
|
|
const stagedTempPath = path.join(
|
|
destinationDir,
|
|
`.${input.tempPrefix}-${process.pid}-${randomUUID()}.tmp`,
|
|
);
|
|
// `wx` + explicit mode create the temp private (0600) and fail if it already
|
|
// exists, so the writer never writes through a pre-existing symlink.
|
|
const handle = await open(stagedTempPath, "wx", 0o600);
|
|
try {
|
|
await handle.writeFile(input.sourceBytes);
|
|
await handle.close();
|
|
const decision = await decideCodexAuthMerge(stagedTempPath, input.destinationPath, {
|
|
seedIfDestAbsent: input.seedIfDestAbsent,
|
|
errorLabel: input.errorLabel,
|
|
});
|
|
if (decision === USE_SOURCE_EXIT) {
|
|
await rename(stagedTempPath, input.destinationPath);
|
|
await input.log(input.writtenLine);
|
|
return "written";
|
|
}
|
|
await input.log(input.keptLine);
|
|
return "kept";
|
|
} finally {
|
|
await handle.close().catch(() => undefined);
|
|
await rm(stagedTempPath, { force: true }).catch(() => undefined);
|
|
}
|
|
});
|
|
}
|