mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-30 17:14:54 +01:00
feat(cli): add isolated test-drive command (#12894)
Add a foreground-only test-drive workflow with isolated data, provider-backed CEO bootstrap, OpenCode/OpenRouter support, worktree execution setup, reuse safeguards, and delayed browser opening. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
5da6499860
commit
8f0c1d4548
15 files changed
+1470
-129
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { shouldLoadWorkingDirectoryEnv } from "../env-file-policy.js";
|
||||
|
||||
describe("working-directory environment loading", () => {
|
||||
it("loads a distinct working-directory .env by default", () => {
|
||||
expect(shouldLoadWorkingDirectoryEnv({
|
||||
cwdEnvExists: true,
|
||||
isPaperclipEnvFile: false,
|
||||
env: {},
|
||||
})).toBe(true);
|
||||
});
|
||||
|
||||
it("does not load the working-directory .env when explicitly disabled", () => {
|
||||
expect(shouldLoadWorkingDirectoryEnv({
|
||||
cwdEnvExists: true,
|
||||
isPaperclipEnvFile: false,
|
||||
env: { PAPERCLIP_DISABLE_CWD_ENV_FILE: "true" },
|
||||
})).toBe(false);
|
||||
});
|
||||
|
||||
it("does not load the same file twice", () => {
|
||||
expect(shouldLoadWorkingDirectoryEnv({
|
||||
cwdEnvExists: true,
|
||||
isPaperclipEnvFile: true,
|
||||
env: {},
|
||||
})).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import { resolve } from "node:path";
|
||||
import { config as loadDotenv } from "dotenv";
|
||||
import { resolvePaperclipEnvPath } from "./paths.js";
|
||||
import { maybeRepairLegacyWorktreeConfigAndEnvFiles } from "./worktree-config.js";
|
||||
import { shouldLoadWorkingDirectoryEnv } from "./env-file-policy.js";
|
||||
import {
|
||||
AUTH_BASE_URL_MODES,
|
||||
BIND_MODES,
|
||||
@@ -36,10 +37,14 @@ if (existsSync(PAPERCLIP_ENV_FILE_PATH)) {
|
||||
}
|
||||
|
||||
const CWD_ENV_PATH = resolve(process.cwd(), ".env");
|
||||
const isSameFile = existsSync(CWD_ENV_PATH) && existsSync(PAPERCLIP_ENV_FILE_PATH)
|
||||
const cwdEnvExists = existsSync(CWD_ENV_PATH);
|
||||
const isSameFile = cwdEnvExists && existsSync(PAPERCLIP_ENV_FILE_PATH)
|
||||
? realpathSync(CWD_ENV_PATH) === realpathSync(PAPERCLIP_ENV_FILE_PATH)
|
||||
: CWD_ENV_PATH === PAPERCLIP_ENV_FILE_PATH;
|
||||
if (!isSameFile && existsSync(CWD_ENV_PATH)) {
|
||||
if (shouldLoadWorkingDirectoryEnv({
|
||||
cwdEnvExists,
|
||||
isPaperclipEnvFile: isSameFile,
|
||||
})) {
|
||||
loadDotenv({ path: CWD_ENV_PATH, override: false, quiet: true });
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
export function shouldLoadWorkingDirectoryEnv(input: {
|
||||
cwdEnvExists: boolean;
|
||||
isPaperclipEnvFile: boolean;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
}): boolean {
|
||||
const env = input.env ?? process.env;
|
||||
return env.PAPERCLIP_DISABLE_CWD_ENV_FILE !== "true"
|
||||
&& input.cwdEnvExists
|
||||
&& !input.isPaperclipEnvFile;
|
||||
}
|
||||
+89
-78
@@ -155,6 +155,7 @@ export interface StartedServer {
|
||||
listenPort: number;
|
||||
apiUrl: string;
|
||||
databaseUrl: string;
|
||||
shutdown: (signal?: "SIGINT" | "SIGTERM") => Promise<void>;
|
||||
}
|
||||
|
||||
export async function startServer(): Promise<StartedServer> {
|
||||
@@ -1793,7 +1794,6 @@ export async function startServer(): Promise<StartedServer> {
|
||||
databaseBackupRetentionDays: config.databaseBackupRetentionDays,
|
||||
databaseBackupDir: config.databaseBackupDir,
|
||||
});
|
||||
|
||||
const boardClaimUrl = getBoardClaimWarningUrl(config.host, listenPort);
|
||||
if (boardClaimUrl) {
|
||||
const red = "\x1b[41m\x1b[30m";
|
||||
@@ -1810,96 +1810,106 @@ export async function startServer(): Promise<StartedServer> {
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const shutdown = async (signal: "SIGINT" | "SIGTERM") => {
|
||||
await systemdNotify(["--stopping", `--status=Stopping after ${signal}`]);
|
||||
heartbeatSchedulerStopped = true;
|
||||
if (heartbeatSchedulerInterval) {
|
||||
clearInterval(heartbeatSchedulerInterval);
|
||||
heartbeatSchedulerInterval = null;
|
||||
}
|
||||
const shutdown = async (
|
||||
signal: "SIGINT" | "SIGTERM",
|
||||
exitProcess: boolean,
|
||||
) => {
|
||||
await systemdNotify(["--stopping", `--status=Stopping after ${signal}`]);
|
||||
heartbeatSchedulerStopped = true;
|
||||
if (heartbeatSchedulerInterval) {
|
||||
clearInterval(heartbeatSchedulerInterval);
|
||||
heartbeatSchedulerInterval = null;
|
||||
}
|
||||
|
||||
const heartbeatShutdown = await coordinateHeartbeatSchedulerShutdown({
|
||||
signal,
|
||||
prepareHotRestartShutdown,
|
||||
waitForHeartbeatSchedulerIdle,
|
||||
});
|
||||
const skipHeartbeatDrain = heartbeatShutdown.hotRestart?.skipDrain === true;
|
||||
const selectiveDrainRunIds = heartbeatShutdown.hotRestart?.drainRunIds ?? null;
|
||||
if (skipHeartbeatDrain) {
|
||||
logger.info(
|
||||
{ signal, hotRestart: heartbeatShutdown.hotRestart },
|
||||
"hot-restart shutdown prepared after scheduler quiescence; skipping graceful run drain",
|
||||
);
|
||||
} else if (heartbeatShutdown.preparationError) {
|
||||
logger.error(
|
||||
{ err: heartbeatShutdown.preparationError, signal },
|
||||
"hot-restart shutdown preparation failed; falling back to graceful heartbeat run drain",
|
||||
);
|
||||
}
|
||||
const heartbeatShutdown = await coordinateHeartbeatSchedulerShutdown({
|
||||
signal,
|
||||
prepareHotRestartShutdown,
|
||||
waitForHeartbeatSchedulerIdle,
|
||||
});
|
||||
const skipHeartbeatDrain = heartbeatShutdown.hotRestart?.skipDrain === true;
|
||||
const selectiveDrainRunIds = heartbeatShutdown.hotRestart?.drainRunIds ?? null;
|
||||
if (skipHeartbeatDrain) {
|
||||
logger.info(
|
||||
{ signal, hotRestart: heartbeatShutdown.hotRestart },
|
||||
"hot-restart shutdown prepared after scheduler quiescence; skipping graceful run drain",
|
||||
);
|
||||
} else if (heartbeatShutdown.preparationError) {
|
||||
logger.error(
|
||||
{ err: heartbeatShutdown.preparationError, signal },
|
||||
"hot-restart shutdown preparation failed; falling back to graceful heartbeat run drain",
|
||||
);
|
||||
}
|
||||
|
||||
const telemetryClient = getTelemetryClient();
|
||||
if (telemetryClient) {
|
||||
telemetryClient.stop();
|
||||
await telemetryClient.flush();
|
||||
}
|
||||
const telemetryClient = getTelemetryClient();
|
||||
if (telemetryClient) {
|
||||
telemetryClient.stop();
|
||||
await telemetryClient.flush();
|
||||
}
|
||||
|
||||
if (!skipHeartbeatDrain && drainHeartbeatRunsForShutdown) {
|
||||
try {
|
||||
const drain = await drainHeartbeatRunsForShutdown(signal, selectiveDrainRunIds);
|
||||
logger.info({ signal, drain }, "graceful heartbeat run drain complete");
|
||||
} catch (err) {
|
||||
logger.error({ err, signal }, "graceful heartbeat run drain failed");
|
||||
}
|
||||
}
|
||||
|
||||
if (!skipHeartbeatDrain) {
|
||||
await drainRunExecutionFinalizersForShutdown({
|
||||
signal,
|
||||
drain: drainHeartbeatExecutionFinalizers,
|
||||
log: logger,
|
||||
});
|
||||
}
|
||||
|
||||
// Whatever the drain did not finalize (timed-out runs, the hot-restart
|
||||
// skip path) still has a local-only tail when the in-flight run-log
|
||||
// mirror is enabled; upload those tails now so an orderly restart
|
||||
// never loses run output. No-op when the mirror is off.
|
||||
if (!skipHeartbeatDrain && drainHeartbeatRunsForShutdown) {
|
||||
try {
|
||||
await flushInFlightRunLogMirrors();
|
||||
const drain = await drainHeartbeatRunsForShutdown(signal, selectiveDrainRunIds);
|
||||
logger.info({ signal, drain }, "graceful heartbeat run drain complete");
|
||||
} catch (err) {
|
||||
logger.error({ err, signal }, "run-log in-flight mirror flush failed");
|
||||
logger.error({ err, signal }, "graceful heartbeat run drain failed");
|
||||
}
|
||||
}
|
||||
|
||||
const appShutdown = (app as { locals?: { paperclipShutdown?: () => Promise<void> } }).locals
|
||||
?.paperclipShutdown;
|
||||
const stopEmbeddedPostgres = embeddedPostgres && embeddedPostgresStartedByThisProcess
|
||||
? () => embeddedPostgresSupervisor?.shutdown() ?? embeddedPostgres!.stop()
|
||||
: null;
|
||||
|
||||
// Await the ordered application teardown before the process exits. A live
|
||||
// setup-token login session must stop and release its sandbox lease before
|
||||
// the database and the provider stop, so an orderly shutdown never leaves a
|
||||
// sandbox lease or confidential login state alive past the process exit.
|
||||
await finalizeServerShutdown({
|
||||
if (!skipHeartbeatDrain) {
|
||||
await drainRunExecutionFinalizersForShutdown({
|
||||
signal,
|
||||
shutdownAppServices: appShutdown,
|
||||
stopEmbeddedPostgres,
|
||||
shutdownInstrumentation,
|
||||
shutdownSentry,
|
||||
drain: drainHeartbeatExecutionFinalizers,
|
||||
log: logger,
|
||||
});
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
};
|
||||
// Whatever the drain did not finalize (timed-out runs, the hot-restart
|
||||
// skip path) still has a local-only tail when the in-flight run-log
|
||||
// mirror is enabled; upload those tails now so an orderly restart
|
||||
// never loses run output. No-op when the mirror is off.
|
||||
try {
|
||||
await flushInFlightRunLogMirrors();
|
||||
} catch (err) {
|
||||
logger.error({ err, signal }, "run-log in-flight mirror flush failed");
|
||||
}
|
||||
|
||||
process.once("SIGINT", () => {
|
||||
void shutdown("SIGINT");
|
||||
const appShutdown = (app as { locals?: { paperclipShutdown?: () => Promise<void> } }).locals
|
||||
?.paperclipShutdown;
|
||||
const stopEmbeddedPostgres = embeddedPostgres && embeddedPostgresStartedByThisProcess
|
||||
? () => embeddedPostgresSupervisor?.shutdown() ?? embeddedPostgres!.stop()
|
||||
: null;
|
||||
|
||||
// Await the ordered application teardown before the process exits. A live
|
||||
// setup-token login session must stop and release its sandbox lease before
|
||||
// the database and the provider stop, so an orderly shutdown never leaves a
|
||||
// sandbox lease or confidential login state alive past the process exit.
|
||||
await finalizeServerShutdown({
|
||||
signal,
|
||||
shutdownAppServices: appShutdown,
|
||||
stopEmbeddedPostgres,
|
||||
shutdownInstrumentation,
|
||||
shutdownSentry,
|
||||
log: logger,
|
||||
});
|
||||
process.once("SIGTERM", () => {
|
||||
void shutdown("SIGTERM");
|
||||
});
|
||||
}
|
||||
|
||||
if (!exitProcess && server.listening) {
|
||||
await new Promise<void>((resolveClose, rejectClose) => {
|
||||
server.close((err) => {
|
||||
if (err) rejectClose(err);
|
||||
else resolveClose();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (exitProcess) process.exit(0);
|
||||
};
|
||||
|
||||
process.once("SIGINT", () => {
|
||||
void shutdown("SIGINT", true);
|
||||
});
|
||||
process.once("SIGTERM", () => {
|
||||
void shutdown("SIGTERM", true);
|
||||
});
|
||||
|
||||
return {
|
||||
server,
|
||||
@@ -1907,6 +1917,7 @@ export async function startServer(): Promise<StartedServer> {
|
||||
listenPort,
|
||||
apiUrl: configuredApiUrl,
|
||||
databaseUrl: activeDatabaseConnectionString,
|
||||
shutdown: (signal = "SIGTERM") => shutdown(signal, false),
|
||||
};
|
||||
} catch (error) {
|
||||
if (startupListenerBound) {
|
||||
|
||||
Reference in new issue
Block a user