Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: raise Postgres connect timeout to 10s and pool max to 20

[._.]/ Adam Eivy4226bd8312e7
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index b94c26e76f..e9cdd67148 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -42,6 +42,8 @@
## Fixed
+- **Postgres pool no longer throws spurious "timeout exceeded when trying to connect"** — On a single-user box that periodically runs heavy local workloads (Ollama model pulls, CoS agents), establishing a fresh loopback connection could briefly exceed the 2s `connectionTimeoutMillis`, surfacing pool connection errors against a perfectly healthy Postgres. The connect timeout is raised to 10s (absorbs busy moments while still failing fast on a real outage) and `max` connections bumped 10→20 so concurrent in-process work isn't starved while one connection is slow to establish. (`server/lib/db.js`)
+
- **DB-adapter test suites run again — against a throwaway `portos_test`, never your real data** — Blocking the test runner from the production `portos` database (previous fix) left the 12 DB-backed adapter suites *skipping everywhere*: CI has no Postgres, and locally they're now refused on the real DB — so the universe/series/issue/story/writers-room/catalog/media DB layer had effectively zero executed coverage. This restores it the safe way. `npm run setup:db:test` provisions a throwaway `portos_test` database (creates it + applies `init-db.sql`), and `npm run test:db` runs just the DB-backed suites against it via a dedicated `vitest.config.db.js` that runs them serially (`fileParallelism: false`) so suites truncating the same tables can't clobber each other. CI now spins up a `pgvector/pgvector:pg17` service and runs those suites against `portos_test` on every PR — the default `npm test` still skips them (no test DB in scope), so nobody needs Postgres to run the rest of the suite and nobody's real `portos` is ever touched. A drift guard in `db.guards.test.js` fails if a new `checkHealth()`-gated suite is left out of the DB config (silently losing coverage). (`server/scripts/setup-db-test.mjs`, `server/vitest.config.db.js`, `server/lib/db.guards.test.js`, `.github/workflows/ci.yml`, `server/package.json`, `package.json`)
- **Tests can no longer wipe your real universes, series, and writing — and every deletion is now audited** — The DB-backed `*.db.test.js` suites (universe, pipeline series/issues, story builder, writers room, catalog, creative director, media index) connect to the single real `portos` Postgres whenever it's reachable and run `DELETE FROM …` in their setup, restoring a snapshot afterward. Because git worktrees share that one database, a CoS agent running the suite in its worktree wiped all 12 universes + 4 series + the writers-room works when its process was torn down before the restore finished (the snapshot/restore swallowed every error). Two new guards in `server/lib/db.js` close this off: (1) `checkHealth()` reports "disconnected" under `NODE_ENV=test` against any non-test database, so every db-backed suite **skips cleanly** instead of deleting — a database is "safe for destructive tests" only when its name ends in `_test` (e.g. `portos_test`) or `TEST_DB_OK=1` is set (`isTestDatabase()`); (2) `query()` is a hard backstop that **throws** on `DELETE FROM` / `TRUNCATE` under the test runner on a non-test DB even if a suite reaches it without gating first. Separately, a new **`record_audit` table + generic DB trigger** now logs every tombstone, un-tombstone, and hard-delete of user-authored content (universes, pipeline series/issues, story-builder sessions, writers-room works/folders/drafts, catalog ingredients/scraps/types, creative-director projects, LoRA runs) — captured at the database layer so it records deletions from *any* source (the app, a stray test, a manual `psql`), with the old row snapshot kept for recovery and the originating SQL for forensics. Inspect it with `node scripts/show-deletions.mjs`. (`server/lib/db.js`, `server/scripts/init-db.sql`, `server/lib/db.guards.test.js`, `server/lib/db.catalogDdlParity.test.js`, `scripts/show-deletions.mjs`)
diff --git a/server/lib/db.js b/server/lib/db.js
index b6bbb16213..f2ea05d58a 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -20,9 +20,14 @@ const pool = new Pool({
database: process.env.PGDATABASE || 'portos',
user: process.env.PGUSER || 'portos',
password: process.env.PGPASSWORD || 'portos',
- max: 10,
+ max: 20,
idleTimeoutMillis: 30000,
- connectionTimeoutMillis: 2000
+ // 10s (was 2s) — a single-user box periodically runs heavy local workloads
+ // (Ollama model pulls, CoS agents) that can briefly delay establishing a
+ // fresh loopback connection past a 2s window, causing spurious "timeout
+ // exceeded when trying to connect" pool errors against a perfectly healthy
+ // Postgres. 10s absorbs the busy moments while still failing fast on a real outage.
+ connectionTimeoutMillis: 10000
});
pool.on('error', (err) => {