mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-26 18:31:34 +01:00
## Thinking Path > - Paperclip is an open-source AI agent management platform; its test suite spans a `server` package that mounts real embedded Postgres databases in `beforeAll`/`afterAll` hooks > - The `server` package CI shard runs all ~93 suites serially (`maxWorkers=1`) on a loaded CI host; each suite boots and tears down its own embedded Postgres in hook callbacks > - vitest's default `hookTimeout` is 10 seconds; under load, graceful embedded-Postgres shutdown occasionally crosses that threshold > - This produces intermittent `Error: Hook timed out in 10000ms` failures in `afterAll` hooks — not test assertion failures — and the suites pass on re-run, making them textbook flaky tests > - Inspecting `embedded-postgres@18.1.0-beta.16` shows that `stop()` takes no argument (no fast-shutdown mode), SIGINTs postgres (already PostgreSQL "fast shutdown"), and resolves only on the child's `exit` event with no internal time bound > - Two targeted fixes: (1) raise `hookTimeout` and `teardownTimeout` to 30 s in `server/vitest.config.ts` — one config change that eliminates the flake for all ~93 suites at once; (2) wrap `stop()` in a 5 s bounded `Promise.race` in the test helper so a slow shutdown can never hang the hook regardless of OS scheduling variance > - This PR changes only test-infra and test-config; no production-code behavior changes ## Linked Issues or Issue Description No public GitHub issue exists for this flake. Inline bug description (bug report template): **What happened?** The `General tests (server (N/3))` CI shards intermittently fail with `Error: Hook timed out in 10000ms` in `afterAll` hooks and pass on re-run. Every test assertion passes; only the teardown hook exceeds vitest's default timeout. **Expected behavior** CI passes reliably. Teardown timeouts should not be a source of flake. **Steps to reproduce** Run the server test suite repeatedly on a loaded host or in CI with `maxWorkers=1` — the shard occasionally crosses 10 s in `afterAll` during embedded-Postgres shutdown. **Paperclip version** `master`, any build that includes `server/vitest.config.ts` without an explicit `hookTimeout`. **Deployment mode** Self-hosted (CI). ## What Changed - **`server/vitest.config.ts`** — added `hookTimeout: 30000` and `teardownTimeout: 30000`. Removes flake across all ~93 server suites at once. 30 s gives generous headroom over observed worst-case teardown while still catching a genuinely hung hook. - **`packages/db/src/test-embedded-postgres.ts`** — added `stopEmbeddedPostgresBounded()`, a 5 s `Promise.race` wrapper around `stop()`. Applied at all three call sites inside `cleanup()`. Data dir is still removed unconditionally; errors are still swallowed; the null-instance guard is preserved. Existing behavior unchanged except the shutdown can no longer block indefinitely. ## Verification - `tsc --noEmit` clean on `packages/db` (built against worktree-local `shared`) - `packages/db` `client.test.ts` passes 14/14 — boots embedded Postgres and exercises the bounded teardown via `cleanup()` in `afterEach` - Standalone bounded-race semantics verified: hang resolves at the 5 s bound; late or immediate `stop()` rejection swallowed; no unhandled rejection; null-instance path safe - CI: all 3 server shards + split-verify lane (Async-Verification Gate) expected green after this PR ```bash # Reproduce the teardown test locally: cd packages/db && npx vitest run src/client.test.ts # Type-check packages/db: npx tsc --noEmit -p packages/db/tsconfig.json ``` ## Risks Low risk. No product-code changes — test-infra and test-config only. The vitest timeout increase is additive (raises the ceiling; never lowers it). The bounded race wrapper preserves prior teardown behavior exactly: data dir always removed, errors always swallowed, stop is still attempted. A worst-case outcome is that a genuinely hung `stop()` now surfaces as a test timeout at 30 s instead of 10 s — still caught, just later. ## Model Used - **Provider:** Anthropic - **Model:** Claude Sonnet 4.6 (`claude-sonnet-4-6`) - **Context window:** 200 k tokens - **Capabilities:** tool use, code execution, extended reasoning - **Mode:** Paperclip agent heartbeat (autonomous execution with human board oversight) ## 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: Harold Kim <harold@paperclip.ing> Co-authored-by: Paperclip <noreply@paperclip.ing>
238 lines
7.8 KiB
TypeScript
238 lines
7.8 KiB
TypeScript
import fs from "node:fs";
|
|
import net from "node:net";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { applyPendingMigrations, ensurePostgresDatabase } from "./client.js";
|
|
import { prepareEmbeddedPostgresNativeRuntime } from "./embedded-postgres-native.js";
|
|
|
|
type EmbeddedPostgresInstance = {
|
|
initialise(): Promise<void>;
|
|
start(): Promise<void>;
|
|
stop(): Promise<void>;
|
|
};
|
|
|
|
type EmbeddedPostgresCtor = new (opts: {
|
|
databaseDir: string;
|
|
user: string;
|
|
password: string;
|
|
port: number;
|
|
persistent: boolean;
|
|
initdbFlags?: string[];
|
|
onLog?: (message: unknown) => void;
|
|
onError?: (message: unknown) => void;
|
|
}) => EmbeddedPostgresInstance;
|
|
|
|
export type EmbeddedPostgresTestSupport = {
|
|
supported: boolean;
|
|
reason?: string;
|
|
};
|
|
|
|
export type EmbeddedPostgresTestDatabase = {
|
|
connectionString: string;
|
|
cleanup(): Promise<void>;
|
|
};
|
|
|
|
let embeddedPostgresSupportPromise: Promise<EmbeddedPostgresTestSupport> | null = null;
|
|
|
|
const DEFAULT_PAPERCLIP_EMBEDDED_POSTGRES_PORT = 54329;
|
|
|
|
function getReservedTestPorts(): Set<number> {
|
|
const configuredPorts = [
|
|
DEFAULT_PAPERCLIP_EMBEDDED_POSTGRES_PORT,
|
|
Number.parseInt(process.env.PAPERCLIP_EMBEDDED_POSTGRES_PORT ?? "", 10),
|
|
...String(process.env.PAPERCLIP_TEST_POSTGRES_RESERVED_PORTS ?? "")
|
|
.split(",")
|
|
.map((value) => Number.parseInt(value.trim(), 10)),
|
|
];
|
|
return new Set(configuredPorts.filter((port) => Number.isInteger(port) && port > 0 && port <= 65535));
|
|
}
|
|
|
|
async function getEmbeddedPostgresCtor(): Promise<EmbeddedPostgresCtor> {
|
|
const mod = await import("embedded-postgres");
|
|
await prepareEmbeddedPostgresNativeRuntime();
|
|
return mod.default as EmbeddedPostgresCtor;
|
|
}
|
|
|
|
async function getAvailablePort(): Promise<number> {
|
|
const reservedPorts = getReservedTestPorts();
|
|
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
const port = 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 test port")));
|
|
return;
|
|
}
|
|
const { port } = address;
|
|
server.close((error) => {
|
|
if (error) reject(error);
|
|
else resolve(port);
|
|
});
|
|
});
|
|
});
|
|
|
|
if (!reservedPorts.has(port)) return port;
|
|
}
|
|
|
|
throw new Error(
|
|
`Failed to allocate embedded Postgres test port outside reserved Paperclip ports: ${[
|
|
...reservedPorts,
|
|
].join(", ")}`,
|
|
);
|
|
}
|
|
|
|
async function createEmbeddedPostgresTestInstance(tempDirPrefix: string) {
|
|
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), tempDirPrefix));
|
|
const port = await getAvailablePort();
|
|
const EmbeddedPostgres = await getEmbeddedPostgresCtor();
|
|
const instance = new EmbeddedPostgres({
|
|
databaseDir: dataDir,
|
|
user: "paperclip",
|
|
password: "paperclip",
|
|
port,
|
|
persistent: true,
|
|
initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"],
|
|
onLog: () => {},
|
|
onError: () => {},
|
|
});
|
|
|
|
return { dataDir, port, instance };
|
|
}
|
|
|
|
function cleanupEmbeddedPostgresTestDirs(dataDir: string) {
|
|
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
}
|
|
|
|
// Upper bound (ms) on how long we wait for the embedded Postgres cluster to
|
|
// stop gracefully before abandoning the wait and returning from the hook.
|
|
const EMBEDDED_POSTGRES_STOP_TIMEOUT_MS = 5000;
|
|
|
|
// `embedded-postgres@18.1.0-beta.16` exposes only `stop(): Promise<void>` — no
|
|
// shutdown-mode argument. Internally it SIGINTs the postgres process (already
|
|
// PostgreSQL "fast shutdown") and resolves *only* on the child's `exit` event,
|
|
// with no time bound of its own. Under the loaded serial server shard a slow
|
|
// shutdown checkpoint can push that past vitest's hookTimeout and hang the
|
|
// afterAll hook. So we bound the graceful stop: if it overruns, we stop waiting
|
|
// and return so the hook completes. The SIGINT has already been delivered, so
|
|
// the abandoned process still exits on its own (and again when the runner exits).
|
|
// Errors are swallowed, matching prior behavior.
|
|
//
|
|
// `cleanupFn` (data-dir reclaim) is chained on the raw `stop()` promise, not on
|
|
// the timeout race, so the disposable data dir is removed *only after* `stop()`
|
|
// actually settles — i.e. once the child Postgres process has exited. Removing
|
|
// it on the timeout path would pull the data files out from under a still-running
|
|
// cluster and provoke checkpoint / WAL I/O errors. In the fast path `cleanupFn`
|
|
// has run by the time this resolves; in the timeout path it runs asynchronously
|
|
// once the abandoned process finally exits.
|
|
async function stopEmbeddedPostgresBounded(
|
|
instance: EmbeddedPostgresInstance | null,
|
|
cleanupFn?: () => void,
|
|
): Promise<void> {
|
|
if (!instance) {
|
|
cleanupFn?.();
|
|
return;
|
|
}
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
const stopped = instance
|
|
.stop()
|
|
.catch(() => {
|
|
// Swallow shutdown errors — the data dir is reclaimed regardless.
|
|
})
|
|
.finally(() => {
|
|
try {
|
|
cleanupFn?.();
|
|
} catch {
|
|
// Best-effort reclaim; ignore removal errors.
|
|
}
|
|
});
|
|
try {
|
|
await Promise.race([
|
|
stopped,
|
|
new Promise<void>((resolve) => {
|
|
timer = setTimeout(resolve, EMBEDDED_POSTGRES_STOP_TIMEOUT_MS);
|
|
timer.unref?.();
|
|
}),
|
|
]);
|
|
} finally {
|
|
if (timer) clearTimeout(timer);
|
|
}
|
|
}
|
|
|
|
function formatEmbeddedPostgresError(error: unknown): string {
|
|
if (error instanceof Error && error.message.length > 0) return error.message;
|
|
if (typeof error === "string" && error.length > 0) return error;
|
|
return "embedded Postgres startup failed";
|
|
}
|
|
|
|
async function probeEmbeddedPostgresSupport(): Promise<EmbeddedPostgresTestSupport> {
|
|
let dataDir: string | null = null;
|
|
let instance: EmbeddedPostgresInstance | null = null;
|
|
|
|
try {
|
|
const created = await createEmbeddedPostgresTestInstance(
|
|
"paperclip-embedded-postgres-probe-",
|
|
);
|
|
dataDir = created.dataDir;
|
|
instance = created.instance;
|
|
await instance.initialise();
|
|
await instance.start();
|
|
return { supported: true };
|
|
} catch (error) {
|
|
return {
|
|
supported: false,
|
|
reason: formatEmbeddedPostgresError(error),
|
|
};
|
|
} finally {
|
|
await stopEmbeddedPostgresBounded(instance, () => {
|
|
if (dataDir) cleanupEmbeddedPostgresTestDirs(dataDir);
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function getEmbeddedPostgresTestSupport(): Promise<EmbeddedPostgresTestSupport> {
|
|
if (!embeddedPostgresSupportPromise) {
|
|
embeddedPostgresSupportPromise = probeEmbeddedPostgresSupport();
|
|
}
|
|
return await embeddedPostgresSupportPromise;
|
|
}
|
|
|
|
export async function startEmbeddedPostgresTestDatabase(
|
|
tempDirPrefix: string,
|
|
): Promise<EmbeddedPostgresTestDatabase> {
|
|
let dataDir: string | null = null;
|
|
let instance: EmbeddedPostgresInstance | null = null;
|
|
|
|
try {
|
|
const created = await createEmbeddedPostgresTestInstance(tempDirPrefix);
|
|
dataDir = created.dataDir;
|
|
instance = created.instance;
|
|
const { port } = created;
|
|
await instance.initialise();
|
|
await instance.start();
|
|
|
|
const adminConnectionString = `postgres://paperclip:paperclip@127.0.0.1:${port}/postgres`;
|
|
await ensurePostgresDatabase(adminConnectionString, "paperclip");
|
|
const connectionString = `postgres://paperclip:paperclip@127.0.0.1:${port}/paperclip`;
|
|
await applyPendingMigrations(connectionString);
|
|
|
|
return {
|
|
connectionString,
|
|
cleanup: async () => {
|
|
await stopEmbeddedPostgresBounded(instance, () => {
|
|
if (dataDir) cleanupEmbeddedPostgresTestDirs(dataDir);
|
|
});
|
|
},
|
|
};
|
|
} catch (error) {
|
|
await stopEmbeddedPostgresBounded(instance, () => {
|
|
if (dataDir) cleanupEmbeddedPostgresTestDirs(dataDir);
|
|
});
|
|
throw new Error(
|
|
`Failed to start embedded PostgreSQL test database: ${formatEmbeddedPostgresError(error)}`,
|
|
);
|
|
}
|
|
}
|