mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-15 12:28:10 +02:00
## Thinking Path > - Paperclip manages agent work and the tools each agent can use. > - GitHub bots need their own App identity and restricted repository access. > - Manual App configuration and repeated setup gates make that connection hard to finish. > - GitHub can create an App from a manifest, but people must approve creation and repository access. > - Each Paperclip stack should own configuration, credentials, signature verification, and task admission. > - This change uses Cloud as an encrypted callback and event gateway, then completes setup in the stack. > - GitHub review bots have their own experimental setting. Chat connector opt-in does not control them. ## Linked Issues or Issue Description **What existing behavior does this improve?** Dedicated GitHub App setup, bot settings, and task-scoped GitHub replies. **Current behavior** Users must configure an App, copy credentials, repeat repository selection, and complete manual verification gates. Review settings are hard to scan. Mentions in descriptions can be missed, and automatic completion text can produce an extra GitHub comment. **Proposed behavior** Choose an agent and connect GitHub under a personal account or organization. GitHub asks for creation and installation consent. Paperclip resumes the same draft, stores secrets locally, imports access, verifies delivery, and opens Settings. Each accepted request has one editable App-owned comment for progress and results. Review assessments produce deterministic checks for the reviewed commit. **Breaking changes** GitHub bots require their own default-off experimental opt-in. The setup-method and delegated-agent screens are removed. Per-person automatic-run flags remain readable but are replaced by author filters. Existing connections, direct webhooks, saved restrictions, and manual existing-App recovery remain supported. Companion gateway: https://github.com/paperclipai/paperclip-cloud/pull/688 (merged). The roadmap and related PRs were checked; no duplicate dedicated-App wizard was found. ## What Changed - Add an independent **GitHub review bots** setting across the catalog, routes, identity admission, and task controls. Existing bots continue running when the UI setting is off. - Replace setup with Choose agent and Connect GitHub. Support personal and organization owners, inline low-trust conversion, saved drafts, known organization suggestions, automatic completion, and same-App recovery. - Keep manifests, OAuth PKCE, credential exchange, vault storage, installation verification, and original webhook-signature validation in the stack. Use the Cloud sealed gateway for public callbacks and outbound local delivery. - Add resumable owner metadata in migration 0323. Retry a failed registration renewal through the same durable replacement rather than creating another App route. - Support authorized mentions in issue and PR bodies, comments, inline comments, and review summaries. Preserve attachments, edited-source identity, duplicate protection, and lifecycle invalidation. Filter requests explicitly addressed to another connected bot. - Give agents task-scoped review, comment, and update tools. Update one working comment through progress and final output. Close coalesced requests and recover delayed acknowledgements, including deleted earlier working comments. Preserve automatic replies for legacy connections without saved review configuration. Preserve partial publication evidence and quarantine uncertain automatic fallbacks without blocking Board messages or questions. - Reorganize Settings and Access with rich slash-skill instructions, numeric passing scores, clear automation controls, formal-review tooltips, member and contributor sections, and verified App branding links. Preserve invalid score drafts and automation choices across tabs. - Paginate repositories in 20-row batches with search and a true connection-wide toggle. Batch verification at GitHub's 500-ID token limit. Keep saved repository overrides while hiding their editor. - Give each review a detail URL that remains usable beyond the newest 100 records. Synchronize the manager OpenAPI contracts and add recovery, authorization, UI, and browser regressions. - Reconcile the current master branch and retain its agent avatar, feature catalog, and extracted heartbeat preparation code. Preserve a saved test-drive port and recover from an occupied high port. ## Verification - Focused UI, independent experimental-setting, and CLI tests: 199 passed. - `pnpm -r typecheck`, `pnpm build`, and `pnpm check:token-gates` passed. The broad local Vitest attempt was interrupted after resource timeouts and stale expectations; the affected regressions passed focused reruns. The latest-head full CI matrix passed, including all browser shards, server/chat suites, builds, typechecking, canary packaging, and Docker context integrity. A Docker Hub 429 required a CI retry; it passed without changing the code. - Final review repairs: 127 attachment/event tests and server typecheck passed. All 13 final integration regressions passed under the normal CI timeout, covering legacy reply compatibility, deleted coalesced comments, inline-only review completion, progress edits, and native final recovery. Restart-safe description/review attachments passed the attachment suite. - Final regressions: four reply/lifecycle tests, both native GitHub wait tests, 171 feature-gate/reply-contract tests, the routed review preview, and five scheduler/reply regressions passed. The final fixture repair also passed all six setup-continuation cases and all three complete chat-test shards in CI, covering terminal receipts and retries under the normal CI timeout. One loaded-host receipt run exceeded its local timeout; the exact case passed in an isolated rerun and the complete CI shard. - GitHub integration tests cover personal and organization setup, retry recovery, edited mentions, attachment provenance, coalesced requests, delayed acknowledgements, partial review writes, old review URLs, and company boundaries. - Browser qualification: both GitHub journeys, all five provider setup/management journeys, and the GitHub uncertain-send recovery journey passed. GitHub setup, reconnect, and task replies run with chat disabled; chat enabled alone cannot expose GitHub bots. The disposable qualification database was removed after testing. - Earlier live qualification verified App-owned issue replies, native slash-skill invocation, progress comment edits, and a 5/5 PR review with a passing current-head check in the dedicated permissions-smoke repository. These live checks did not use personal credentials as bot execution authority. ## Risks - Deploy the merged Cloud gateway protocol before the stack update. Each callback and encrypted event must retain the original instance and tenant binding. - Migration 0323 uses idempotent column additions for test installations that already applied the earlier draft migration number. - A verified connection does not prove runtime isolation or a completed review. Runtime checks remain separate. - Required merge checks must be configured in GitHub. A passing numeric score does not formally approve a PR; approval and request-changes tools remain opt-in. - Existing-App credentials cannot silently renew an uncertain manifest exchange. Recovery preserves the existing App, draft, connection, and agent. - Repository changes save immediately. Global toggles affect all currently available repositories, including unloaded rows, without granting new installation access. - A failed or uncertain tool write suppresses automatic fallback text; the governed outbox retains its evidence. Deleted or replaced response comments are not recreated through a personal credential fallback. - GitHub logo upload remains an optional human action in GitHub App settings. Runtime provisioning, delegated setup, screenshot publication, and production push delivery remain deferred. ## Model Used OpenAI Codex, based on GPT-6, with tool use and code execution. The exact runtime model identifier and context size are not exposed in this session. ## 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 references) - [x] My branch name describes the change and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass (focused suites; complete test matrix is checked in CI) - [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>
519 lines
18 KiB
TypeScript
519 lines
18 KiB
TypeScript
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { createServer } from "node:net";
|
|
import * as p from "@clack/prompts";
|
|
import pc from "picocolors";
|
|
import { Option, type Command } from "commander";
|
|
import type { Agent, Company, InstanceExperimentalSettings } from "@paperclipai/shared";
|
|
import { PaperclipApiClient } from "../client/http.js";
|
|
import { openUrl } from "../client/board-auth.js";
|
|
import {
|
|
expandHomePrefix,
|
|
resolveDefaultConfigPath,
|
|
resolveDefaultContextPath,
|
|
} from "../config/home.js";
|
|
import { readConfig } from "../config/store.js";
|
|
import type { PaperclipConfig } from "../config/schema.js";
|
|
import { runCommand, type StartedServer } from "./run.js";
|
|
import { isLinkedGitWorktree } from "./git-workspace.js";
|
|
|
|
export const TEST_DRIVE_HARNESSES = ["claude", "codex", "opencode"] as const;
|
|
export type TestDriveHarness = (typeof TEST_DRIVE_HARNESSES)[number];
|
|
|
|
export interface TestDriveOptions {
|
|
dataDir?: string;
|
|
companyName?: string;
|
|
agentName?: string;
|
|
harness?: TestDriveHarness;
|
|
model?: string;
|
|
apiKeyEnv?: string;
|
|
apiKey?: string;
|
|
browser?: boolean;
|
|
}
|
|
|
|
export type TestDriveApi = Pick<PaperclipApiClient, "get" | "post" | "patch" | "delete">;
|
|
|
|
type HarnessDefinition = {
|
|
adapterType: "claude_local" | "codex_local" | "opencode_local";
|
|
credentialTarget: "ANTHROPIC_API_KEY" | "OPENAI_API_KEY" | "OPENROUTER_API_KEY";
|
|
credentialName: string;
|
|
};
|
|
|
|
export type ResolvedTestDriveBootstrap = HarnessDefinition & {
|
|
companyName: string;
|
|
agentName: string;
|
|
model?: string;
|
|
credential: string;
|
|
credentialSource: string;
|
|
};
|
|
|
|
export type TestDriveBootstrapResult = {
|
|
reused: boolean;
|
|
company: Company;
|
|
agent: Agent | null;
|
|
};
|
|
|
|
export interface TestDriveDependencies {
|
|
run: typeof runCommand;
|
|
createApi: (apiBase: string) => TestDriveApi;
|
|
openBrowser: (url: string) => Promise<boolean>;
|
|
}
|
|
|
|
const HARNESS_DEFINITIONS: Record<TestDriveHarness, HarnessDefinition> = {
|
|
claude: {
|
|
adapterType: "claude_local",
|
|
credentialTarget: "ANTHROPIC_API_KEY",
|
|
credentialName: "Anthropic API Key",
|
|
},
|
|
codex: {
|
|
adapterType: "codex_local",
|
|
credentialTarget: "OPENAI_API_KEY",
|
|
credentialName: "OpenAI API Key",
|
|
},
|
|
opencode: {
|
|
adapterType: "opencode_local",
|
|
credentialTarget: "OPENROUTER_API_KEY",
|
|
credentialName: "OpenRouter API Key",
|
|
},
|
|
};
|
|
|
|
const NON_PAPERCLIP_ISOLATED_ENV_KEYS = [
|
|
"DATABASE_URL",
|
|
"DATABASE_MIGRATION_URL",
|
|
"HOST",
|
|
"PORT",
|
|
"SERVE_UI",
|
|
"BETTER_AUTH_URL",
|
|
"BETTER_AUTH_BASE_URL",
|
|
] as const;
|
|
|
|
function requiredApiResult<T>(value: T | null, action: string): T {
|
|
if (value === null) {
|
|
throw new Error(`Paperclip returned no result while ${action}.`);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function errorMessage(error: unknown): string {
|
|
if (error instanceof Error) return error.message || error.name;
|
|
if (typeof error === "string") return error;
|
|
try {
|
|
return JSON.stringify(error);
|
|
} catch {
|
|
return String(error);
|
|
}
|
|
}
|
|
|
|
export function redactTestDriveText(text: string, credentials: Array<string | undefined>): string {
|
|
let redacted = text;
|
|
for (const credential of credentials) {
|
|
if (!credential) continue;
|
|
redacted = redacted.replaceAll(credential, "[REDACTED]");
|
|
}
|
|
return redacted;
|
|
}
|
|
|
|
export function redactTestDriveArgv(
|
|
apiKey: string | undefined,
|
|
argv: string[] = process.argv,
|
|
): void {
|
|
if (!apiKey) return;
|
|
for (let index = 0; index < argv.length; index += 1) {
|
|
if (argv[index] === "--api-key" && argv[index + 1] === apiKey) {
|
|
argv[index + 1] = "[REDACTED]";
|
|
index += 1;
|
|
continue;
|
|
}
|
|
if (argv[index] === `--api-key=${apiKey}`) {
|
|
argv[index] = "--api-key=[REDACTED]";
|
|
}
|
|
}
|
|
}
|
|
|
|
export function resolveTestDriveDataDir(dataDir?: string): string {
|
|
const explicit = dataDir?.trim();
|
|
if (explicit) {
|
|
return path.resolve(expandHomePrefix(explicit));
|
|
}
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-test-drive-"));
|
|
}
|
|
|
|
async function loopbackPortAvailable(port: number): Promise<boolean> {
|
|
return await new Promise<boolean>((resolve) => {
|
|
const server = createServer();
|
|
server.unref();
|
|
server.once("error", () => resolve(false));
|
|
server.listen(port, "127.0.0.1", () => {
|
|
server.close(() => resolve(true));
|
|
});
|
|
});
|
|
}
|
|
|
|
export async function resolveTestDriveServerPort(preferredPort = 3100, available = loopbackPortAvailable): Promise<number> {
|
|
if (await available(preferredPort)) return preferredPort;
|
|
// A saved high port can be occupied. Search the ordinary range instead of
|
|
// trapping the restarted test drive at the top of the TCP port space.
|
|
for (let port = 3100; port <= 65_535; port += 1) {
|
|
if (port !== preferredPort && await available(port)) return port;
|
|
}
|
|
throw new Error("No available loopback port found at or above 3100.");
|
|
}
|
|
|
|
/**
|
|
* Establish isolation before the CLI's normal config and .env loading hook.
|
|
* The selected credential source is preserved in case its name happens to use
|
|
* a PAPERCLIP_ prefix; all other Paperclip routing/configuration is discarded.
|
|
*/
|
|
export async function prepareTestDriveEnvironment(
|
|
options: Pick<TestDriveOptions, "dataDir" | "apiKeyEnv">,
|
|
cwd = process.cwd(),
|
|
): Promise<{ dataDir: string; linkedWorktree: boolean }> {
|
|
const sourceEnvName = options.apiKeyEnv?.trim();
|
|
const preservedCredential = sourceEnvName ? process.env[sourceEnvName] : undefined;
|
|
|
|
for (const key of Object.keys(process.env)) {
|
|
if (key.startsWith("PAPERCLIP_")) {
|
|
delete process.env[key];
|
|
}
|
|
}
|
|
for (const key of NON_PAPERCLIP_ISOLATED_ENV_KEYS) {
|
|
delete process.env[key];
|
|
}
|
|
if (sourceEnvName && preservedCredential !== undefined) {
|
|
process.env[sourceEnvName] = preservedCredential;
|
|
}
|
|
|
|
const dataDir = resolveTestDriveDataDir(options.dataDir);
|
|
const linkedWorktree = isLinkedGitWorktree(cwd);
|
|
process.env.PAPERCLIP_HOME = dataDir;
|
|
process.env.PAPERCLIP_INSTANCE_ID = "default";
|
|
process.env.PAPERCLIP_CONFIG = resolveDefaultConfigPath("default");
|
|
process.env.PAPERCLIP_CONTEXT = resolveDefaultContextPath();
|
|
process.env.PAPERCLIP_IN_WORKTREE = linkedWorktree ? "true" : "false";
|
|
process.env.PAPERCLIP_OPEN_ON_LISTEN = "false";
|
|
process.env.PAPERCLIP_DISABLE_CWD_ENV_FILE = "true";
|
|
process.env.PAPERCLIP_DEPLOYMENT_MODE = "local_trusted";
|
|
process.env.PAPERCLIP_DEPLOYMENT_EXPOSURE = "private";
|
|
process.env.PAPERCLIP_BIND = "loopback";
|
|
process.env.HOST = "127.0.0.1";
|
|
let preferredPort = 3100;
|
|
try {
|
|
preferredPort = readConfig(process.env.PAPERCLIP_CONFIG)?.server.port ?? preferredPort;
|
|
} catch {
|
|
// The normal startup doctor owns invalid-config diagnostics and repair.
|
|
}
|
|
// Reusing a test drive must preserve callback and enrollment origins when
|
|
// its saved port is available. Only move forward if that port is occupied.
|
|
process.env.PORT = String(await resolveTestDriveServerPort(preferredPort));
|
|
|
|
return { dataDir, linkedWorktree };
|
|
}
|
|
|
|
export function assertTestDriveDatabaseIsolation(
|
|
configPath?: string,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
readConfigFile: (path?: string) => PaperclipConfig | null = readConfig,
|
|
): void {
|
|
if (env.DATABASE_URL?.trim() || env.DATABASE_MIGRATION_URL?.trim()) {
|
|
throw new Error(
|
|
"test-drive requires its isolated embedded database. Remove DATABASE_URL and " +
|
|
"DATABASE_MIGRATION_URL from the selected data directory's .env, or choose a fresh --data-dir.",
|
|
);
|
|
}
|
|
|
|
const config = readConfigFile(configPath);
|
|
if (config?.database.mode === "postgres") {
|
|
throw new Error(
|
|
"test-drive cannot reuse a data directory configured for an external PostgreSQL database. " +
|
|
"Choose a fresh data directory or change database.mode to embedded-postgres.",
|
|
);
|
|
}
|
|
}
|
|
|
|
export function resolveTestDriveBootstrap(
|
|
options: TestDriveOptions,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
): ResolvedTestDriveBootstrap {
|
|
if (options.apiKey !== undefined && options.apiKeyEnv !== undefined) {
|
|
throw new Error("--api-key and --api-key-env are mutually exclusive.");
|
|
}
|
|
|
|
const harness = options.harness ?? "claude";
|
|
const definition = HARNESS_DEFINITIONS[harness];
|
|
if (!definition) {
|
|
throw new Error(`Unsupported test-drive harness: ${String(harness)}.`);
|
|
}
|
|
|
|
const companyName = (options.companyName ?? "Test Company").trim();
|
|
const agentName = (options.agentName ?? "CEO").trim();
|
|
if (!companyName) throw new Error("--company-name cannot be empty.");
|
|
if (!agentName) throw new Error("--agent-name cannot be empty.");
|
|
|
|
const model = options.model;
|
|
if (model !== undefined && (!model || model.trim() !== model)) {
|
|
throw new Error("--model cannot be empty or have surrounding whitespace.");
|
|
}
|
|
if (
|
|
harness === "opencode" &&
|
|
(!model || !/^openrouter\/[^/\s]+(?:\/[^/\s]+)*$/.test(model))
|
|
) {
|
|
throw new Error(
|
|
"OpenCode test drives require --model openrouter/<model>, with no empty path segments.",
|
|
);
|
|
}
|
|
|
|
const sourceEnvName = options.apiKeyEnv?.trim() || definition.credentialTarget;
|
|
if (options.apiKeyEnv !== undefined && !/^[A-Za-z_][A-Za-z0-9_]*$/.test(sourceEnvName)) {
|
|
throw new Error("--api-key-env must name a valid environment variable.");
|
|
}
|
|
const credential = options.apiKey ?? env[sourceEnvName];
|
|
if (!credential || credential.trim().length === 0) {
|
|
throw new Error(
|
|
`No credential found. Set ${sourceEnvName}, pass --api-key-env <variable>, or pass --api-key <value>.`,
|
|
);
|
|
}
|
|
|
|
return {
|
|
...definition,
|
|
companyName,
|
|
agentName,
|
|
...(model ? { model } : {}),
|
|
credential,
|
|
credentialSource: options.apiKey !== undefined ? "--api-key" : sourceEnvName,
|
|
};
|
|
}
|
|
|
|
function worktreeExecutionArmed(
|
|
settings: InstanceExperimentalSettings,
|
|
instanceId: string,
|
|
): boolean {
|
|
return settings.enableWorktreeRunExecution === true
|
|
&& Boolean(settings.worktreeRunExecutionActivatedAt)
|
|
&& settings.worktreeRunExecutionActivationInstanceId === instanceId;
|
|
}
|
|
|
|
export async function reconcileTestDriveWorktreeExecution(
|
|
api: TestDriveApi,
|
|
instanceId: string,
|
|
): Promise<void> {
|
|
const current = requiredApiResult(
|
|
await api.get<InstanceExperimentalSettings>("/api/instance/settings/experimental"),
|
|
"reading experimental settings",
|
|
);
|
|
|
|
if (!current.enableWorktreeRunExecution) {
|
|
await api.patch<InstanceExperimentalSettings>("/api/instance/settings/experimental", {
|
|
enableWorktreeRunExecution: true,
|
|
});
|
|
} else if (!worktreeExecutionArmed(current, instanceId)) {
|
|
await api.patch<InstanceExperimentalSettings>("/api/instance/settings/experimental", {
|
|
enableWorktreeRunExecution: false,
|
|
});
|
|
await api.patch<InstanceExperimentalSettings>("/api/instance/settings/experimental", {
|
|
enableWorktreeRunExecution: true,
|
|
});
|
|
}
|
|
|
|
const verified = requiredApiResult(
|
|
await api.get<InstanceExperimentalSettings>("/api/instance/settings/experimental"),
|
|
"verifying experimental settings",
|
|
);
|
|
if (!worktreeExecutionArmed(verified, instanceId)) {
|
|
throw new Error(
|
|
`Could not arm “Run tasks in this worktree” for Paperclip instance ${instanceId}. ` +
|
|
"Check that PAPERCLIP_IN_WORKTREE=true and retry the command.",
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function bootstrapTestDrive(input: {
|
|
api: TestDriveApi;
|
|
options: TestDriveOptions;
|
|
linkedWorktree: boolean;
|
|
instanceId: string;
|
|
env?: NodeJS.ProcessEnv;
|
|
}): Promise<TestDriveBootstrapResult> {
|
|
const companies = requiredApiResult(
|
|
await input.api.get<Company[]>("/api/companies"),
|
|
"reading companies",
|
|
);
|
|
const existingCompany = companies[0];
|
|
if (existingCompany) {
|
|
if (input.linkedWorktree) {
|
|
await reconcileTestDriveWorktreeExecution(input.api, input.instanceId);
|
|
}
|
|
return { reused: true, company: existingCompany, agent: null };
|
|
}
|
|
|
|
// Resolve every bootstrap input before the first mutation. In particular,
|
|
// OpenCode model validation and credential lookup happen before company
|
|
// creation so an invalid invocation leaves the database untouched.
|
|
const resolved = resolveTestDriveBootstrap(input.options, input.env);
|
|
let company: Company | null = null;
|
|
try {
|
|
company = requiredApiResult(
|
|
await input.api.post<Company>("/api/companies", { name: resolved.companyName }),
|
|
"creating the test company",
|
|
);
|
|
await input.api.post(`/api/companies/${company.id}/user-secret-definitions`, {
|
|
key: resolved.credentialTarget,
|
|
name: resolved.credentialName,
|
|
});
|
|
await input.api.post(`/api/companies/${company.id}/me/user-secrets`, {
|
|
definitionKey: resolved.credentialTarget,
|
|
value: resolved.credential,
|
|
});
|
|
|
|
const adapterConfig: Record<string, unknown> = {
|
|
env: {
|
|
[resolved.credentialTarget]: {
|
|
type: "user_secret_ref",
|
|
key: resolved.credentialTarget,
|
|
version: "latest",
|
|
required: true,
|
|
},
|
|
},
|
|
};
|
|
if (resolved.model) adapterConfig.model = resolved.model;
|
|
|
|
const agent = requiredApiResult(
|
|
await input.api.post<Agent>(`/api/companies/${company.id}/agents`, {
|
|
name: resolved.agentName,
|
|
role: "ceo",
|
|
adapterType: resolved.adapterType,
|
|
adapterConfig,
|
|
}),
|
|
"creating the CEO agent",
|
|
);
|
|
|
|
if (input.linkedWorktree) {
|
|
await reconcileTestDriveWorktreeExecution(input.api, input.instanceId);
|
|
}
|
|
return { reused: false, company, agent };
|
|
} catch (error) {
|
|
if (company) {
|
|
try {
|
|
await input.api.delete(`/api/companies/${company.id}`);
|
|
} catch (cleanupError) {
|
|
throw new Error(
|
|
`${errorMessage(error)} Cleanup also failed for newly-created company ${company.id}: ${errorMessage(cleanupError)}`,
|
|
{ cause: error },
|
|
);
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
function dashboardUrl(server: StartedServer): string {
|
|
return server.apiUrl.replace(/\/api\/?$/, "");
|
|
}
|
|
|
|
export async function testDriveCommand(
|
|
options: TestDriveOptions,
|
|
dependencies: TestDriveDependencies = {
|
|
run: runCommand,
|
|
createApi: (apiBase) => new PaperclipApiClient({ apiBase }),
|
|
openBrowser: openUrl,
|
|
},
|
|
): Promise<void> {
|
|
// Commander has already copied the value into options. Remove it from the
|
|
// JavaScript argv view before logging, telemetry, diagnostics, or startup.
|
|
redactTestDriveArgv(options.apiKey);
|
|
const dataDir = path.resolve(process.env.PAPERCLIP_HOME ?? resolveTestDriveDataDir(options.dataDir));
|
|
const linkedWorktree = process.env.PAPERCLIP_IN_WORKTREE === "true";
|
|
const instanceId = process.env.PAPERCLIP_INSTANCE_ID ?? "default";
|
|
// Resolve environment-backed credentials against the CLI environment as it
|
|
// exists before server startup. In-process server initialization must not
|
|
// change which credential the post-listen bootstrap observes.
|
|
const bootstrapEnv = { ...process.env };
|
|
const possibleCredentials = [
|
|
options.apiKey,
|
|
options.apiKeyEnv ? bootstrapEnv[options.apiKeyEnv.trim()] : undefined,
|
|
bootstrapEnv[HARNESS_DEFINITIONS[options.harness ?? "claude"].credentialTarget],
|
|
];
|
|
|
|
p.log.message(pc.dim(`Data directory: ${dataDir}`));
|
|
p.log.message(pc.dim("The data directory is retained when Paperclip exits."));
|
|
if (options.apiKey !== undefined) {
|
|
p.log.warn("A key passed with --api-key may be visible in process arguments and shell history.");
|
|
}
|
|
|
|
try {
|
|
await dependencies.run({
|
|
repair: true,
|
|
yes: true,
|
|
bind: "loopback",
|
|
installService: false,
|
|
// Auto-created directories are private to this process. Explicitly reused
|
|
// directories retain the normal guard against an already-managed instance.
|
|
skipServiceManagerCheck: !options.dataDir?.trim(),
|
|
introLabel: "paperclipai test-drive",
|
|
afterStart: async (server) => {
|
|
const api = dependencies.createApi(server.apiUrl);
|
|
const result = await bootstrapTestDrive({
|
|
api,
|
|
options,
|
|
linkedWorktree,
|
|
instanceId,
|
|
env: bootstrapEnv,
|
|
});
|
|
if (result.reused) {
|
|
p.log.message(
|
|
`Using existing data for ${pc.cyan(result.company.name)}; bootstrap flags were ignored.`,
|
|
);
|
|
} else {
|
|
p.log.success(
|
|
`Created ${pc.cyan(result.company.name)} with agent ${pc.cyan(result.agent?.name ?? "CEO")}.`,
|
|
);
|
|
}
|
|
if (linkedWorktree) {
|
|
p.log.success("Run tasks in this worktree is enabled for this instance.");
|
|
}
|
|
|
|
const url = dashboardUrl(server);
|
|
if (options.browser === false) {
|
|
p.log.success(`Paperclip is ready at ${pc.cyan(url)}.`);
|
|
return;
|
|
}
|
|
const opened = await dependencies.openBrowser(url);
|
|
if (opened) {
|
|
p.log.success(`Paperclip is ready and opened at ${pc.cyan(url)}.`);
|
|
} else {
|
|
p.log.warn(`Paperclip is ready, but the browser could not be opened. Visit ${url}.`);
|
|
}
|
|
},
|
|
});
|
|
} catch (error) {
|
|
throw new Error(redactTestDriveText(errorMessage(error), possibleCredentials), { cause: error });
|
|
}
|
|
}
|
|
|
|
export function registerTestDriveCommand(program: Command): void {
|
|
program
|
|
.command("test-drive")
|
|
.description("Start an isolated, initialized Paperclip instance for manual testing")
|
|
.option("-d, --data-dir <path>", "Paperclip data directory to create or reuse")
|
|
.option("--company-name <name>", "Initial company name", "Test Company")
|
|
.option("--agent-name <name>", "Initial CEO agent name", "CEO")
|
|
.addOption(
|
|
new Option("--harness <harness>", "Initial agent harness")
|
|
.choices(TEST_DRIVE_HARNESSES)
|
|
.default("claude"),
|
|
)
|
|
.option("--model <model-id>", "Initial agent model")
|
|
.addOption(
|
|
new Option("--api-key-env <variable>", "Read the provider key from an environment variable")
|
|
.conflicts("apiKey"),
|
|
)
|
|
.addOption(
|
|
new Option("--api-key <value>", "Provider API key")
|
|
.conflicts("apiKeyEnv"),
|
|
)
|
|
.option("--no-browser", "Do not open the initialized instance in a browser")
|
|
.action(async (options: TestDriveOptions) => {
|
|
await testDriveCommand(options);
|
|
});
|
|
}
|