Files
PaperClipAI/packages/db/src/test-postgres-admin.test.ts
T
c938bdfee4 perf(ci): stop printing Postgres TRUNCATE notices in server test shards (#21903)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The server test suites run on 12 CI shards. Each suite gets a
disposable Postgres database from `packages/db`.
> - Many suites reset their state with `TRUNCATE ... CASCADE`. Postgres
sends one `NOTICE` for each cascaded table.
> - postgres.js prints each notice with `console.log` when a client sets
no `onnotice`. Vitest then carries each line from the worker to the
reporter.
> - On the slowest shard of a recent run, about 600k of the 760k log
lines were these notices. The noise makes the shard slower and the log
hard to read.
> - This pull request sets `client_min_messages = warning` on each
disposable test database. Every client that a suite opens then gets no
notices.
> - The benefit is a shorter slowest server shard and a much smaller
log.

## Linked Issues or Issue Description

Refs #21879 (a separate cause of the same slow shard: missing
shard-duration weights).

**What existing behavior does this improve?**
Disposable test databases send every Postgres `NOTICE` to the test
clients. The test logs print each notice.

**Subsystem affected**
Test database fixtures in `packages/db` (`test-embedded-postgres.ts` and
`test-postgres-admin.ts`). This change touches only test infrastructure.

**Current behavior**
On PR #21872 (run
[43858567594](https://github.com/paperclipai/paperclip/actions/runs/43858567594)),
the longest check was `ci / General tests (server (10/12))` at 768s. The
Vitest step took 722s of that. The other server shards took 597–729s.
The shard log had 760k lines. About 600k lines were `truncate cascades
to table "…"` notices (about 77k notice objects):

| suite | notices |
|---|---|
| heartbeat/workspace-preparation.test.ts | 19,089 |
| agent-timer-deadlines.test.ts | 16,445 |
| idle-incoming-wake.test.ts | 13,846 |
| public-mcp/events-work.test.ts | 13,156 |
| execution-control-work.test.ts | 4,485 |
| 6 more suites | about 10k |

About 20 suites already stop the notices one session at a time (`SET
client_min_messages = warning` or `onnotice = () => {}`).

**Proposed behavior**
The fixture sets `client_min_messages = warning` on the database itself.
It does this for the embedded-cluster path and for the
`PAPERCLIP_TEST_POSTGRES_ADMIN_URL` path. Warnings and errors still
show. A suite that needs notices can lower the level for its own
session.

**Reason and benefit**
The notice-heavy suites ran 15–50% over their manifest weights in this
run (for example, workspace-preparation took 193s against a weight of
173s, and idle-incoming-wake took 40s against 26s). Other suites were
near their weights. Estimate: about 0.5–1 min less on the slowest server
shard. The log on that shard goes from about 760k lines to about 160k
lines. If #21879 also merges, the server lane's critical path should go
from about 12.8 min to about 10–11 min.

**Breaking changes**
None. Only disposable test databases change.

## What Changed

- `packages/db/src/test-postgres-admin.ts`: add
`QUIET_TEST_DATABASE_SQL`. Run it on each new database on the admin-URL
path.
- `packages/db/src/test-embedded-postgres.ts`: run the same statement on
the `paperclip` database at each embedded start. It runs on each start,
not only when the template is built. Thus a template that was cached
before this change also gives a quiet database, and the template key
stays the same.
- `packages/db/src/test-postgres-admin.test.ts`: new test for the
admin-URL path.
- `packages/db/src/test-embedded-postgres.test.ts`: new tests for the
embedded template path and the fresh path.
- Each new test opens a new client, checks `SHOW client_min_messages`,
and runs a `TRUNCATE ... CASCADE`. The test fails if the client gets a
notice.

## Verification

- `cd packages/db && npx vitest run src/test-postgres-admin.test.ts
src/test-embedded-postgres.test.ts`: 2 files, 25 tests pass.
- I changed the statement to `RESET client_min_messages` and ran the new
tests again. All 3 new tests failed. Then I restored the statement.
- `cd packages/db && npx tsc --noEmit -p .`: no errors.
- To measure the time savings, compare the server shard timings of this
PR's CI run with run 43858567594.

## Risks

- Low risk. The change affects only disposable test databases.
Production databases do not use these fixtures.
- A test that expects to receive a `NOTICE` on a new client would now
not get it. Such a test can set `client_min_messages = notice` for its
session. No current suite depends on notices: the full CI run on this PR
passed.
- The time savings is an estimate. The shard planner and other suites
can change the slowest shard.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) by Anthropic, used through Claude
Code as a Paperclip agent with tool use (shell, file edits, GitHub CLI,
CI log inspection). Standard reasoning mode.

## 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
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: devinfoley <139239+devinfoley@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 13:07:15 -08:00

102 lines
5.2 KiB
TypeScript

import { randomUUID } from "node:crypto";
import postgres from "postgres";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import { createDb } from "./client.js";
import {
__setEmbeddedPostgresCtorProviderForTests,
getEmbeddedPostgresTestSupport,
startEmbeddedPostgresTestDatabase,
type EmbeddedPostgresTestDatabase,
} from "./test-embedded-postgres.js";
const previousAdmin = process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL;
const previousTemplate = process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE;
let server: EmbeddedPostgresTestDatabase;
const databases: EmbeddedPostgresTestDatabase[] = [];
const support = await getEmbeddedPostgresTestSupport();
describe.skipIf(!support.supported)("shared PostgreSQL fixtures", () => {
beforeAll(async () => {
server = await startEmbeddedPostgresTestDatabase("paperclip-admin-regression-");
process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL = server.connectionString;
}, 90_000);
afterEach(async () => {
__setEmbeddedPostgresCtorProviderForTests(null);
process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL = server.connectionString;
if (previousTemplate === undefined) delete process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE;
else process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE = previousTemplate;
while (databases.length) await databases.pop()!.cleanup();
});
afterAll(async () => {
await server?.cleanup();
if (previousAdmin === undefined) delete process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL;
else process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL = previousAdmin;
if (previousTemplate === undefined) delete process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE;
else process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE = previousTemplate;
});
it("uses one server for isolated databases and closes clients before dropping them", async () => {
__setEmbeddedPostgresCtorProviderForTests(async () => { throw new Error("Must not start another PostgreSQL server"); });
expect(await getEmbeddedPostgresTestSupport()).toEqual({ supported: true });
const first = await startEmbeddedPostgresTestDatabase("ignored-"); databases.push(first);
const second = await startEmbeddedPostgresTestDatabase("ignored-"); databases.push(second);
expect(first.connectionString).not.toBe(second.connectionString);
const a = createDb(first.connectionString), b = createDb(second.connectionString);
await a.$client`CREATE TABLE isolated_probe (id integer)`;
expect((await b.$client`SELECT to_regclass('isolated_probe') AS name`)[0]?.name).toBeNull();
await first.cleanup();
await first.cleanup();
const control = createDb(server.connectionString);
try {
const name = new URL(first.connectionString).pathname.slice(1);
expect(await control.$client`SELECT datname FROM pg_database WHERE datname = ${name}`).toHaveLength(0);
expect((await b.$client`SELECT 1 AS value`)[0]?.value).toBe(1);
} finally { await control.$client.end(); }
}, 90_000);
it("rejects unavailable configured servers rather than reporting support", async () => {
process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL = "postgres://paperclip:paperclip@127.0.0.1:1/postgres";
await expect(getEmbeddedPostgresTestSupport()).rejects.toThrow();
});
it("rejects unsafe templates and treats zero as a disabled template", async () => {
process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE = 'postgres"; DROP DATABASE postgres; --';
await expect(startEmbeddedPostgresTestDatabase("ignored-")).rejects.toThrow("Invalid disposable test template name");
process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE = "0";
databases.push(await startEmbeddedPostgresTestDatabase("ignored-"));
}, 90_000);
it("applies pending migrations when a supplied database template is not current", async () => {
const template = `paperclip_test_template_${randomUUID().replaceAll("-", "")}`;
const control = createDb(server.connectionString);
await control.$client.unsafe(`CREATE DATABASE "${template}"`);
try {
process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE = template;
const database = await startEmbeddedPostgresTestDatabase("ignored-");
databases.push(database);
const db = createDb(database.connectionString);
expect((await db.$client`SELECT to_regclass('companies') AS name`)[0]?.name).toBe("companies");
} finally {
await control.$client.unsafe(`DROP DATABASE "${template}"`);
await control.$client.end();
}
}, 90_000);
it("hides NOTICE messages from new clients of a disposable database", async () => {
__setEmbeddedPostgresCtorProviderForTests(async () => { throw new Error("Must not start another PostgreSQL server"); });
const database = await startEmbeddedPostgresTestDatabase("ignored-");
databases.push(database);
const notices: unknown[] = [];
const sql = postgres(database.connectionString, { max: 1, onnotice: (notice) => notices.push(notice) });
try {
expect((await sql`SHOW client_min_messages`)[0]?.client_min_messages).toBe("warning");
await sql`CREATE TABLE quiet_parent (id integer PRIMARY KEY)`;
await sql`CREATE TABLE quiet_child (parent_id integer REFERENCES quiet_parent (id))`;
await sql`TRUNCATE quiet_parent CASCADE`;
expect(notices).toEqual([]);
} finally { await sql.end(); }
}, 90_000);
});