forked from farhoodlabs/paperclip
f4a05dc35c
## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies > - The company import/export e2e exercises the local CLI startup path that boots the dev server inside a workspace > - That startup path loads server and plugin code which depends on built workspace package artifacts such as `@paperclipai/shared` and `@paperclipai/plugin-sdk` > - In a clean worktree those `dist/*` artifacts may not exist yet even though `paperclipai run` can still attempt to import the local server entry > - That mismatch caused the import/export e2e to fail before the actual company package flow ran > - This pull request adds a CLI preflight step that prepares the needed workspace build dependencies before the local server import and fails closed if that preflight is interrupted or stalls > - The benefit is that clean worktrees can boot `paperclipai run` reliably without silently continuing after incomplete dependency preparation ## What Changed - Updated `cli/src/commands/run.ts` to execute `scripts/ensure-plugin-build-deps.mjs` before importing `server/src/index.ts` for local dev startup. - Ensured `paperclipai run` can materialize missing workspace artifacts such as `packages/shared/dist` and `packages/plugins/sdk/dist` automatically in clean worktrees. - Made the preflight fail closed when the child process exits via signal and bounded it with a 120-second timeout so the CLI does not hang indefinitely. - Kept the fix isolated to the CLI startup path; no API contract, schema, or UI behavior changed. - Reused the existing `cli/src/__tests__/company-import-export-e2e.test.ts` coverage that already exercises the failing boot path, so no additional test file was needed. ## Verification - `pnpm test:run cli/src/__tests__/company-import-export-e2e.test.ts` - `pnpm --filter paperclipai typecheck` - On the isolated branch, confirmed `packages/shared/dist/index.js` and `packages/plugins/sdk/dist/index.js` were absent before the run, then reran the targeted e2e and observed a passing result. ## Risks - Low risk: the change only affects the local CLI dev startup path before the server import. - Residual risk: other entrypoints still rely on their own preflight/build behavior, so this does not normalize every workspace startup path. - The 120-second timeout is intentionally generous, but unusually slow machines could still hit it and surface a startup error instead of waiting forever. ## Model Used - OpenAI Codex, GPT-5-based coding agent in the Codex CLI environment, with shell/tool execution enabled. The exact runtime revision and context window are not exposed by this environment. ## 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 run tests locally and they pass - [x] I have added or updated tests where applicable - [x] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge
216 lines
7.1 KiB
TypeScript
216 lines
7.1 KiB
TypeScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { spawnSync } from "node:child_process";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
import * as p from "@clack/prompts";
|
|
import pc from "picocolors";
|
|
import { bootstrapCeoInvite } from "./auth-bootstrap-ceo.js";
|
|
import { onboard } from "./onboard.js";
|
|
import { doctor } from "./doctor.js";
|
|
import { loadPaperclipEnvFile } from "../config/env.js";
|
|
import { configExists, resolveConfigPath } from "../config/store.js";
|
|
import type { PaperclipConfig } from "../config/schema.js";
|
|
import { readConfig } from "../config/store.js";
|
|
import {
|
|
describeLocalInstancePaths,
|
|
resolvePaperclipHomeDir,
|
|
resolvePaperclipInstanceId,
|
|
} from "../config/home.js";
|
|
|
|
interface RunOptions {
|
|
config?: string;
|
|
instance?: string;
|
|
repair?: boolean;
|
|
yes?: boolean;
|
|
}
|
|
|
|
interface StartedServer {
|
|
apiUrl: string;
|
|
databaseUrl: string;
|
|
host: string;
|
|
listenPort: number;
|
|
}
|
|
|
|
export async function runCommand(opts: RunOptions): Promise<void> {
|
|
const instanceId = resolvePaperclipInstanceId(opts.instance);
|
|
process.env.PAPERCLIP_INSTANCE_ID = instanceId;
|
|
|
|
const homeDir = resolvePaperclipHomeDir();
|
|
fs.mkdirSync(homeDir, { recursive: true });
|
|
|
|
const paths = describeLocalInstancePaths(instanceId);
|
|
fs.mkdirSync(paths.instanceRoot, { recursive: true });
|
|
|
|
const configPath = resolveConfigPath(opts.config);
|
|
process.env.PAPERCLIP_CONFIG = configPath;
|
|
loadPaperclipEnvFile(configPath);
|
|
|
|
p.intro(pc.bgCyan(pc.black(" paperclipai run ")));
|
|
p.log.message(pc.dim(`Home: ${paths.homeDir}`));
|
|
p.log.message(pc.dim(`Instance: ${paths.instanceId}`));
|
|
p.log.message(pc.dim(`Config: ${configPath}`));
|
|
|
|
if (!configExists(configPath)) {
|
|
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
p.log.error("No config found and terminal is non-interactive.");
|
|
p.log.message(`Run ${pc.cyan("paperclipai onboard")} once, then retry ${pc.cyan("paperclipai run")}.`);
|
|
process.exit(1);
|
|
}
|
|
|
|
p.log.step("No config found. Starting onboarding...");
|
|
await onboard({ config: configPath, invokedByRun: true });
|
|
}
|
|
|
|
p.log.step("Running doctor checks...");
|
|
const summary = await doctor({
|
|
config: configPath,
|
|
repair: opts.repair ?? true,
|
|
yes: opts.yes ?? true,
|
|
});
|
|
|
|
if (summary.failed > 0) {
|
|
p.log.error("Doctor found blocking issues. Not starting server.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const config = readConfig(configPath);
|
|
if (!config) {
|
|
p.log.error(`No config found at ${configPath}.`);
|
|
process.exit(1);
|
|
}
|
|
|
|
p.log.step("Starting Paperclip server...");
|
|
const startedServer = await importServerEntry();
|
|
|
|
if (shouldGenerateBootstrapInviteAfterStart(config)) {
|
|
p.log.step("Generating bootstrap CEO invite");
|
|
await bootstrapCeoInvite({
|
|
config: configPath,
|
|
dbUrl: startedServer.databaseUrl,
|
|
baseUrl: resolveBootstrapInviteBaseUrl(config, startedServer),
|
|
});
|
|
}
|
|
}
|
|
|
|
function resolveBootstrapInviteBaseUrl(
|
|
config: PaperclipConfig,
|
|
startedServer: StartedServer,
|
|
): string {
|
|
const explicitBaseUrl =
|
|
process.env.PAPERCLIP_PUBLIC_URL ??
|
|
process.env.PAPERCLIP_AUTH_PUBLIC_BASE_URL ??
|
|
process.env.BETTER_AUTH_URL ??
|
|
process.env.BETTER_AUTH_BASE_URL ??
|
|
(config.auth.baseUrlMode === "explicit" ? config.auth.publicBaseUrl : undefined);
|
|
|
|
if (typeof explicitBaseUrl === "string" && explicitBaseUrl.trim().length > 0) {
|
|
return explicitBaseUrl.trim().replace(/\/+$/, "");
|
|
}
|
|
|
|
return startedServer.apiUrl.replace(/\/api$/, "");
|
|
}
|
|
|
|
function formatError(err: unknown): string {
|
|
if (err instanceof Error) {
|
|
if (err.message && err.message.trim().length > 0) return err.message;
|
|
return err.name;
|
|
}
|
|
if (typeof err === "string") return err;
|
|
try {
|
|
return JSON.stringify(err);
|
|
} catch {
|
|
return String(err);
|
|
}
|
|
}
|
|
|
|
function isModuleNotFoundError(err: unknown): boolean {
|
|
if (!(err instanceof Error)) return false;
|
|
const code = (err as { code?: unknown }).code;
|
|
if (code === "ERR_MODULE_NOT_FOUND") return true;
|
|
return err.message.includes("Cannot find module");
|
|
}
|
|
|
|
function getMissingModuleSpecifier(err: unknown): string | null {
|
|
if (!(err instanceof Error)) return null;
|
|
const packageMatch = err.message.match(/Cannot find package '([^']+)' imported from/);
|
|
if (packageMatch?.[1]) return packageMatch[1];
|
|
const moduleMatch = err.message.match(/Cannot find module '([^']+)'/);
|
|
if (moduleMatch?.[1]) return moduleMatch[1];
|
|
return null;
|
|
}
|
|
|
|
function maybeEnableUiDevMiddleware(entrypoint: string): void {
|
|
if (process.env.PAPERCLIP_UI_DEV_MIDDLEWARE !== undefined) return;
|
|
const normalized = entrypoint.replaceAll("\\", "/");
|
|
if (normalized.endsWith("/server/src/index.ts") || normalized.endsWith("@paperclipai/server/src/index.ts")) {
|
|
process.env.PAPERCLIP_UI_DEV_MIDDLEWARE = "true";
|
|
}
|
|
}
|
|
|
|
function ensureDevWorkspaceBuildDeps(projectRoot: string): void {
|
|
const buildScript = path.resolve(projectRoot, "scripts/ensure-plugin-build-deps.mjs");
|
|
if (!fs.existsSync(buildScript)) return;
|
|
|
|
const result = spawnSync(process.execPath, [buildScript], {
|
|
cwd: projectRoot,
|
|
stdio: "inherit",
|
|
timeout: 120_000,
|
|
});
|
|
|
|
if (result.error) {
|
|
throw new Error(
|
|
`Failed to prepare workspace build artifacts before starting the Paperclip dev server.\n${formatError(result.error)}`,
|
|
);
|
|
}
|
|
|
|
if ((result.status ?? 1) !== 0) {
|
|
throw new Error(
|
|
"Failed to prepare workspace build artifacts before starting the Paperclip dev server.",
|
|
);
|
|
}
|
|
}
|
|
|
|
async function importServerEntry(): Promise<StartedServer> {
|
|
// Dev mode: try local workspace path (monorepo with tsx)
|
|
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../..");
|
|
const devEntry = path.resolve(projectRoot, "server/src/index.ts");
|
|
if (fs.existsSync(devEntry)) {
|
|
ensureDevWorkspaceBuildDeps(projectRoot);
|
|
maybeEnableUiDevMiddleware(devEntry);
|
|
const mod = await import(pathToFileURL(devEntry).href);
|
|
return await startServerFromModule(mod, devEntry);
|
|
}
|
|
|
|
// Production mode: import the published @paperclipai/server package
|
|
try {
|
|
const mod = await import("@paperclipai/server");
|
|
return await startServerFromModule(mod, "@paperclipai/server");
|
|
} catch (err) {
|
|
const missingSpecifier = getMissingModuleSpecifier(err);
|
|
const missingServerEntrypoint = !missingSpecifier || missingSpecifier === "@paperclipai/server";
|
|
if (isModuleNotFoundError(err) && missingServerEntrypoint) {
|
|
throw new Error(
|
|
`Could not locate a Paperclip server entrypoint.\n` +
|
|
`Tried: ${devEntry}, @paperclipai/server\n` +
|
|
`${formatError(err)}`,
|
|
);
|
|
}
|
|
throw new Error(
|
|
`Paperclip server failed to start.\n` +
|
|
`${formatError(err)}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
function shouldGenerateBootstrapInviteAfterStart(config: PaperclipConfig): boolean {
|
|
return config.server.deploymentMode === "authenticated" && config.database.mode === "embedded-postgres";
|
|
}
|
|
|
|
async function startServerFromModule(mod: unknown, label: string): Promise<StartedServer> {
|
|
const startServer = (mod as { startServer?: () => Promise<StartedServer> }).startServer;
|
|
if (typeof startServer !== "function") {
|
|
throw new Error(`Paperclip server entrypoint did not export startServer(): ${label}`);
|
|
}
|
|
return await startServer();
|
|
}
|