Files
PaperClipAI/tests/e2e/start-e2e-server.ts
T
Dotta 7563842165 perf(ci): cut E2E ceiling under 95s via 16 AWS shards, 8-core speedup, top serial splits, and parallel UI build (#22960)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Live 8-core PR runs (`45668834558`, `45667106472`) showed
`e2e_shards` was the #1 critical-path bottleneck on normal PRs
(`152–171s` test step) due to four compounding causes:
> 1. `scripts/e2e-shard.mjs` still used the 4-vCPU
`EFFECTIVE_PARALLEL_SPEEDUP = 1.85` even on 8-vCPU runners running 4
Playwright workers (`~3.35x` measured speedup), under-allocating serial
shards (`5` serial shards running `96–124s` while parallel shards sat
idle after `55–78s`).
> 2. `scripts/e2e-shard-durations.json` had stale weights
(`chat-adapters-ui-providers.spec.ts` `86.8s` vs `117–165s` actual;
`acp-stop-continuation.spec.ts` `69.3s` vs `88–102s` actual).
> 3. Three serial (`1-worker`) specs pinned the floor at `75–91s`:
`agent-configuration.spec.ts` (`91.4s`), `agent-chat-sessions.spec.ts`
(`79.9s`), and `onboarding.spec.ts` (`75.3s`).
> 4. `playwright.config.ts` ran `vite build ui` (`~5.4s`) serially
before starting embedded Postgres + 512 migrations (`~21s`) even though
database bootstrap does not read `server/ui-dist`.
> - This pull request overlaps the unminified Vite UI build with
Postgres bootstrap inside `start-e2e-server.ts`, splits the top 3 serial
specs into 6 balanced specs (keeping all 3 anchor filenames and 100% of
20 tests intact), makes `e2e-shard.mjs` CPU-aware (`3.2x` on 8-vCPU /
`1.85x` on 4-vCPU), refreshes all 58 specs in
`e2e-shard-durations.json`, and scales AWS `e2e_shards` `12 -> 16` while
keeping `ubuntu-latest` hosted at `4` — bringing simulated E2E test
execution to `~90–96s` (`-60 to -75s`).

## Linked Issues or Issue Description

Refs #22804

**What existing behavior does this improve?**
E2E shard wall-clock ceiling on 8-core PR runners (`tests/e2e/`,
`scripts/e2e-shard.mjs`, `scripts/e2e-shard-durations.json`,
`.github/ci-task-matrix.json`, `.github/workflows/pr-trusted.yml`).

**Subsystem affected**
tests/e2e/ and CI — Playwright server bootstrap, serial spec sharding,
and E2E shard matrix

**Current behavior**
E2E shards take `152–171s` on 8-core runners because `vite build ui`
runs serially before Postgres boot, 3 serial specs run `75–91s` each,
`e2e-shard.mjs` assumes 4-vCPU `1.85x` parallel speedup, and AWS runs 12
shards.

**Proposed behavior**
Overlap unminified Vite UI build with embedded Postgres boot in
`tests/e2e/start-e2e-server.ts`, split the top 3 serial specs into 6
balanced specs (preserving anchor paths and all tests), use CPU-aware
parallel speedup (`3.2x` on 8-vCPU / `1.85x` on 4-vCPU) with refreshed
58-spec durations, and run 16 AWS E2E shards (`8 serial + 8 parallel`,
`59.7–76.5s` Playwright) while keeping `ubuntu-latest` hosted at `4`.

## What Changed

- **Overlapped UI build with Postgres bootstrap
(`tests/e2e/playwright.config.ts`, `tests/e2e/start-e2e-server.ts`):**
- Launches `start-e2e-server.ts` directly with `PAPERCLIP_E2E_BUILD_UI`
(`0` when `PAPERCLIP_E2E_SKIP_UI_BUILD=1`), overlapping `vite build ui
--outDir ../server/ui-dist --emptyOutDir --minify false` concurrently
inside `Promise.all([dbPromise, import("../../server/src/index.js"),
uiReady])` with `stopUiBuild()` and `dbPromise` cleanup on
signals/failure, plus `maintenance_work_mem = 128MB` / `work_mem = 32MB`
on disposable PG.
- **Split top 3 serial E2E specs (100% of 20 tests preserved, all 3
anchor filenames & `maps/` links intact, all 3 new siblings registered
in BOTH `playwright.config.ts` and `scripts/e2e-shard.mjs`
`SERIAL_SPECS`):**
- `agent-configuration.spec.ts` (3 tests) +
`agent-configuration-revisions.spec.ts` (5 tests) +
`agent-configuration.shared.ts`
- `agent-chat-sessions.spec.ts` (3 tests) +
`agent-chat-sessions-navigation.spec.ts` (3 tests)
- `onboarding.spec.ts` (3 tests) + `onboarding-resume.spec.ts` (3 tests)
- **CPU-aware speedup, refreshed 58-spec manifest, & 16 AWS E2E shards
(`scripts/e2e-shard.mjs`, `scripts/e2e-shard-durations.json`,
`.github/ci-task-matrix.json`, `.github/workflows/pr-trusted.yml`,
`.github/scripts/tests/ci-plan.test.mjs`,
`scripts/__tests__/e2e-shard.test.mjs`):**
- Exported `EFFECTIVE_PARALLEL_SPEEDUP_4W = 3.2` and
`resolveEffectiveParallelSpeedup(cpus)` matching `playwright.config.ts`
worker scaling.
- Refreshed all 58/58 spec weights from live 8-core runs `45668834558` &
`45667106472`.
- Scaled AWS `e2e_shards` `12 -> 16` while keeping `ubuntu-latest`
hosted at `4`.

## Verification

- `node --test scripts/__tests__/e2e-shard.test.mjs
.github/scripts/tests/ci-plan.test.mjs
scripts/check-test-budget.test.mjs
scripts/__tests__/merge-queue-checks-workflow.test.mjs` — **45/45 PASS**
- `npx playwright test --config tests/e2e/playwright.config.ts --list`
on all 6 split specs — **20 tests in 6 files**
- `tsc --strict --noEmit` across all 9 edited `tests/e2e/*.ts` files —
**0 errors**
- `node scripts/check-test-budget.mjs --base origin/master` and `node
scripts/check-maps.mjs` — **PASS**
- `ruby -ryaml -e 'YAML.load_file(".github/workflows/pr-trusted.yml")'`
— **YAML OK**

## Risks

- Low risk: all 3 original serial spec anchor filenames remain intact,
`PAPERCLIP_E2E_SKIP_UI_BUILD=1` is preserved, signal/failure handlers
clean up both the Vite child and embedded PG, `ubuntu-latest` stays at 4
hosted shards, and full CI runs all E2E shards live before merge.

## Model Used

- Gemini 4 Neon (High) via Antigravity agent swarm (`e2e-opt`,
`verifier`, `ci-pipeline`).

## 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
2026-11-25 13:40:52 -06:00

472 lines
16 KiB
TypeScript

import { spawn, type ChildProcess } from "node:child_process";
import { randomBytes } from "node:crypto";
import fs from "node:fs";
import { createRequire } from "node:module";
import net from "node:net";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import {
applyPendingMigrations,
closeRegisteredClients,
createDb,
ensurePostgresDatabase,
} from "../../packages/db/src/client.js";
import { loadWithoutEmbeddedPostgresExitHooks } from "../../packages/db/src/embedded-postgres-lifecycle.js";
import { prepareEmbeddedPostgresNativeRuntime } from "../../packages/db/src/embedded-postgres-native.js";
import {
computeEmbeddedPostgresTemplateKey,
startEmbeddedPostgresTestDatabase,
} from "../../packages/db/src/test-embedded-postgres.js";
import {
beforeTestPostgresShutdown,
ownTestPostgres,
type TestPostgresInstance,
} from "../../packages/db/src/test-postgres-owner.js";
// Fast throwaway server bootstrap for Playwright E2E shards.
// Replaces `cli onboard --yes --run` (and avoids building/copying a durable
// template cluster on ephemeral CI runners where only one server boots per job):
// 1. Writes the minimal throwaway local_trusted config + secrets key directly.
// 2. Boots a single disposable embedded Postgres with `initdb --no-sync` and
// non-durable settings (`fsync=off`, `synchronous_commit=off`,
// `full_page_writes=off`, `autovacuum=off`) written to `postgresql.conf`
// BEFORE `start()`, so all migrations and E2E test writes run at memory
// speed without stop/copy/restart overhead.
// 3. Overlaps `import("../../server/src/index.js")` with Postgres bootstrap
// and starts `server/src/index.ts` directly via `startServer()`.
type EmbeddedPostgresCtor = new (opts: {
databaseDir: string;
user: string;
password: string;
port: number;
persistent: boolean;
initdbFlags?: string[];
onLog?: (message: unknown) => void;
onError?: (message: unknown) => void;
}) => TestPostgresInstance;
type DisposableDatabase = {
connectionString: string;
cleanup(): Promise<void>;
};
const EMBEDDED_USER = "paperclip";
const EMBEDDED_PASSWORD = "paperclip";
const EMBEDDED_DB = "paperclip";
const INITDB_FLAGS = ["--encoding=UTF8", "--locale=C", "--lc-messages=C", "--no-sync"];
const DISPOSABLE_PG_CONF = [
"",
"# Disposable E2E test cluster tuning — zero crash durability required",
"fsync = off",
"synchronous_commit = off",
"full_page_writes = off",
"autovacuum = off",
"checkpoint_timeout = 1h",
"max_wal_size = 1GB",
"maintenance_work_mem = 128MB",
"work_mem = 32MB",
"log_min_messages = warning",
"",
].join("\n");
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const UI_BUILD_ARGS = [
"ui/node_modules/vite/bin/vite.js",
"build",
"ui",
"--outDir",
"../server/ui-dist",
"--emptyOutDir",
"--minify",
"false",
];
function startParallelUiBuild(): { child: ChildProcess; done: Promise<void> } | null {
if (process.env.PAPERCLIP_E2E_BUILD_UI !== "1") return null;
const child = spawn(process.execPath, UI_BUILD_ARGS, {
cwd: REPO_ROOT,
stdio: "inherit",
env: process.env,
});
const done = new Promise<void>((resolve, reject) => {
child.once("error", reject);
child.once("close", (code, signal) => {
if (code === 0) {
resolve();
return;
}
reject(
new Error(
`E2E UI build failed (${signal ? `signal ${signal}` : `exit code ${code ?? "unknown"}`})`,
),
);
});
});
done.catch(() => {});
return { child, done };
}
const MAX_START_ATTEMPTS = 5;
async function getAvailablePort(): Promise<number> {
return await new Promise<number>((resolve, reject) => {
const server = net.createServer();
server.unref();
server.on("error", reject);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
if (!address || typeof address === "string") {
server.close(() => reject(new Error("Failed to allocate E2E Postgres port")));
return;
}
const { port } = address;
server.close((error) => (error ? reject(error) : resolve(port)));
});
});
}
async function loadEmbeddedPostgresCtor(): Promise<EmbeddedPostgresCtor> {
const dbRequire = createRequire(new URL("../../packages/db/package.json", import.meta.url));
const mod = await loadWithoutEmbeddedPostgresExitHooks(async () => {
const loaded = dbRequire("embedded-postgres") as { default?: EmbeddedPostgresCtor } | EmbeddedPostgresCtor;
return typeof loaded === "function" ? loaded : loaded.default;
});
if (!mod) {
throw new Error("Failed to load embedded-postgres constructor from @paperclipai/db");
}
await prepareEmbeddedPostgresNativeRuntime();
return mod;
}
type FastPostgresHandle = {
ready: Promise<DisposableDatabase>;
};
async function startFastDisposablePostgres(
EmbeddedPostgres: EmbeddedPostgresCtor,
): Promise<FastPostgresHandle> {
// Allocate port and kick off `initdb` BEFORE returning so the `initdb` child
// process runs in parallel on another core while the caller concurrently
// transpiles `server/src/index.ts` via `tsx`.
const firstDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-e2e-pg-"));
const firstPort = await getAvailablePort();
const attemptStart = async (
dataDir: string,
pgPort: number,
preStartedInit?: Promise<void>,
preOwnedInstance?: ReturnType<typeof ownTestPostgres>,
preLogs?: string[],
): Promise<DisposableDatabase> => {
const recentLogs = preLogs ?? [];
const appendLog = (message: unknown) => {
const text = String(message ?? "").trim();
if (!text) return;
recentLogs.push(text);
if (recentLogs.length > 40) recentLogs.shift();
};
const instance =
preOwnedInstance ??
ownTestPostgres(
new EmbeddedPostgres({
databaseDir: dataDir,
user: EMBEDDED_USER,
password: EMBEDDED_PASSWORD,
port: pgPort,
persistent: true,
initdbFlags: INITDB_FLAGS,
onLog: appendLog,
onError: appendLog,
}),
dataDir,
);
try {
await (preStartedInit ?? instance.initialise());
fs.appendFileSync(path.join(dataDir, "postgresql.conf"), DISPOSABLE_PG_CONF, "utf8");
await instance.start();
const adminUrl = `postgres://${EMBEDDED_USER}:${EMBEDDED_PASSWORD}@127.0.0.1:${pgPort}/postgres`;
const connectionString = `postgres://${EMBEDDED_USER}:${EMBEDDED_PASSWORD}@127.0.0.1:${pgPort}/${EMBEDDED_DB}`;
await ensurePostgresDatabase(adminUrl, EMBEDDED_DB);
await applyPendingMigrations(connectionString);
let cleaned = false;
return {
connectionString,
cleanup: async () => {
if (cleaned) return;
cleaned = true;
try {
await closeRegisteredClients(connectionString).catch(() => {});
} finally {
await instance.stop().catch(() => {});
await fs.promises.rm(dataDir, { recursive: true, force: true });
}
},
};
} catch (error) {
try {
await instance.stop();
} catch {
// Ignore stop error on failed start attempt.
}
await fs.promises.rm(dataDir, { recursive: true, force: true });
const combined = `${error instanceof Error ? error.message : String(error)}\n${recentLogs.join("\n")}`;
if (!/address already in use/i.test(combined)) {
throw new Error(`Fast E2E embedded Postgres startup failed: ${combined}`);
}
throw error;
}
};
const firstLogs: string[] = [];
const appendFirstLog = (message: unknown) => {
const text = String(message ?? "").trim();
if (!text) return;
firstLogs.push(text);
if (firstLogs.length > 40) firstLogs.shift();
};
const firstInstance = ownTestPostgres(
new EmbeddedPostgres({
databaseDir: firstDataDir,
user: EMBEDDED_USER,
password: EMBEDDED_PASSWORD,
port: firstPort,
persistent: true,
initdbFlags: INITDB_FLAGS,
onLog: appendFirstLog,
onError: appendFirstLog,
}),
firstDataDir,
);
// Spawns the `initdb` child process synchronously before returning.
const firstInitPromise = firstInstance.initialise();
const ready = (async () => {
let lastError: unknown = new Error("embedded Postgres startup failed");
try {
return await attemptStart(firstDataDir, firstPort, firstInitPromise, firstInstance, firstLogs);
} catch (error) {
lastError = error;
if (!/address already in use/i.test(error instanceof Error ? error.message : String(error))) {
throw error;
}
}
for (let attempt = 2; attempt <= MAX_START_ATTEMPTS; attempt += 1) {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-e2e-pg-"));
const pgPort = await getAvailablePort();
try {
return await attemptStart(dataDir, pgPort);
} catch (error) {
lastError = error;
if (!/address already in use/i.test(error instanceof Error ? error.message : String(error))) {
throw error;
}
}
}
throw lastError instanceof Error ? lastError : new Error(String(lastError));
})();
return { ready };
}
const port = Number(process.env.PORT ?? process.env.PAPERCLIP_E2E_PORT ?? 3199);
const instanceId = process.env.PAPERCLIP_INSTANCE_ID ?? "playwright-e2e";
const homeDir =
process.env.PAPERCLIP_HOME ??
fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-e2e-home-"));
const instanceRoot = path.join(homeDir, "instances", instanceId);
const configPath =
process.env.PAPERCLIP_CONFIG ?? path.join(instanceRoot, "config.json");
const logsDir = path.join(instanceRoot, "logs");
const storageDir = path.join(instanceRoot, "data", "storage");
const masterKeyPath = path.join(instanceRoot, "secrets", "master.key");
fs.mkdirSync(logsDir, { recursive: true });
fs.mkdirSync(storageDir, { recursive: true });
fs.mkdirSync(path.dirname(masterKeyPath), { recursive: true });
fs.mkdirSync(path.dirname(configPath), { recursive: true });
if (!fs.existsSync(masterKeyPath)) {
fs.writeFileSync(masterKeyPath, randomBytes(32).toString("base64"), {
encoding: "utf8",
mode: 0o600,
});
}
process.env.PAPERCLIP_HOME = homeDir;
process.env.PAPERCLIP_INSTANCE_ID = instanceId;
process.env.PAPERCLIP_CONFIG = configPath;
process.env.PAPERCLIP_STORAGE_LOCAL_DIR = storageDir;
process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE = masterKeyPath;
process.env.PAPERCLIP_DB_BACKUP_ENABLED = "false";
let db: DisposableDatabase | undefined;
let shutdownServer: ((signal?: "SIGINT" | "SIGTERM") => Promise<void>) | undefined;
let shuttingDown = false;
let uiBuild: { child: ChildProcess; done: Promise<void> } | null = null;
function stopUiBuild() {
if (!uiBuild || uiBuild.child.exitCode !== null || uiBuild.child.signalCode !== null) return;
try {
uiBuild.child.kill("SIGTERM");
} catch {
}
}
function hasWarmEmbeddedPostgresTemplate(): boolean {
if (process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE === "0") return false;
const uid = process.getuid?.();
if (uid === undefined) return false;
const templateDir = path.join(
os.tmpdir(),
`paperclip-embedded-postgres-templates-${uid}`,
computeEmbeddedPostgresTemplateKey(),
);
return (
fs.existsSync(path.join(templateDir, "PG_VERSION")) &&
!fs.existsSync(path.join(templateDir, "postmaster.pid"))
);
}
async function tuneDisposableCluster(connectionString: string): Promise<void> {
const adminUrl = new URL(connectionString);
adminUrl.pathname = "/postgres";
const admin = createDb(adminUrl.toString(), { maxConnections: 1 });
try {
await admin.$client.unsafe("ALTER SYSTEM SET fsync = off");
await admin.$client.unsafe("ALTER SYSTEM SET synchronous_commit = off");
await admin.$client.unsafe("ALTER SYSTEM SET full_page_writes = off");
await admin.$client.unsafe("ALTER SYSTEM SET autovacuum = off");
await admin.$client.unsafe("SELECT pg_reload_conf()");
} finally {
await admin.$client.end({ timeout: 1 });
}
}
const usesExternalPostgres = Boolean(process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL?.trim());
const useWarmTemplate = !usesExternalPostgres && hasWarmEmbeddedPostgresTemplate();
if (usesExternalPostgres) {
const shutdownExternal = async (signal: "SIGINT" | "SIGTERM") => {
if (shuttingDown) return;
shuttingDown = true;
stopUiBuild();
try {
await shutdownServer?.(signal);
} finally {
await db?.cleanup();
}
process.exit(0);
};
process.once("SIGINT", () => void shutdownExternal("SIGINT"));
process.once("SIGTERM", () => void shutdownExternal("SIGTERM"));
} else {
beforeTestPostgresShutdown({
stop: async (signal) => {
if (shuttingDown) return;
shuttingDown = true;
stopUiBuild();
await shutdownServer?.(signal === "SIGINT" ? "SIGINT" : "SIGTERM");
if (db) await closeRegisteredClients(db.connectionString).catch(() => {});
},
interrupt() {},
afterStop: async () => {
await db?.cleanup();
},
});
}
let dbPromise: Promise<DisposableDatabase> | undefined;
try {
uiBuild = startParallelUiBuild();
const uiReady = uiBuild?.done ?? Promise.resolve();
let startedDb: DisposableDatabase;
let serverModule: typeof import("../../server/src/index.js");
if (usesExternalPostgres) {
dbPromise = startEmbeddedPostgresTestDatabase("paperclip-e2e-pg-");
[startedDb, serverModule] = await Promise.all([
dbPromise,
import("../../server/src/index.js"),
uiReady,
]).then(([pg, mod]) => [pg, mod] as const);
} else if (useWarmTemplate) {
dbPromise = (async () => {
const pg = await startEmbeddedPostgresTestDatabase("paperclip-e2e-pg-");
await tuneDisposableCluster(pg.connectionString);
return pg;
})();
[startedDb, serverModule] = await Promise.all([
dbPromise,
import("../../server/src/index.js"),
uiReady,
]).then(([pg, mod]) => [pg, mod] as const);
} else {
// Load `embedded-postgres` and spawn `initdb` BEFORE calling
// `import("../../server/src/index.js")` so synchronous `createRequire`
const embeddedPostgresCtor = await loadEmbeddedPostgresCtor();
const fastPg = await startFastDisposablePostgres(embeddedPostgresCtor);
dbPromise = fastPg.ready;
[startedDb, serverModule] = await Promise.all([
dbPromise,
import("../../server/src/index.js"),
uiReady,
]).then(([pg, mod]) => [pg, mod] as const);
}
db = startedDb;
const config = {
$meta: {
version: 1,
updatedAt: new Date().toISOString(),
source: "onboard",
},
database: {
mode: "postgres",
connectionString: db.connectionString,
backup: {
enabled: false,
intervalMinutes: 60,
retentionDays: 7,
dir: path.join(instanceRoot, "data", "backups"),
},
},
logging: { mode: "file", logDir: logsDir },
server: {
deploymentMode: "local_trusted",
exposure: "private",
bind: "loopback",
host: "127.0.0.1",
port,
allowedHostnames: [],
serveUi: true,
},
auth: { baseUrlMode: "auto", disableSignUp: false },
storage: { provider: "local_disk", localDisk: { baseDir: storageDir } },
secrets: {
provider: "local_encrypted",
strictMode: false,
localEncrypted: { keyFilePath: masterKeyPath },
},
};
fs.writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`, {
encoding: "utf8",
mode: 0o600,
});
process.env.DATABASE_URL = db.connectionString;
const started = await serverModule.startServer();
shutdownServer = started.shutdown;
} catch (error) {
stopUiBuild();
const cleanupDb = db ?? (await dbPromise?.catch(() => undefined));
await cleanupDb?.cleanup();
throw error;
}