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: report the real blocker when setup:db:test fails and pin that a failing stage fails pregate (#12833)

Adam Eivyd14d0ecd3fb0
diff --git a/scripts/pregate.test.js b/scripts/pregate.test.js
index 5afeb036fb..ad99c953da 100644
--- a/scripts/pregate.test.js
+++ b/scripts/pregate.test.js
@@ -446,6 +446,18 @@ describe('pregate hidden-content invocation', () => {
expect(result.stderr).toContain('Stage "hidden-content scan" exited with processes still running in its group; killed them.');
});
+ // #12833: a stage's exit status is its only verdict; a failing provisioning
+ // command must never be recorded as `passed` in the receipt.
+ it('records a stage that exits non-zero as failed and ends with its status', () => {
+ write('scripts/scan-diff-hidden-content.js', "console.error('fixture provisioning failure'); process.exit(3);\n");
+ const result = run('--skip-lint');
+ expect(result.status).toBe(3);
+ expect(result.stderr).toContain('hidden-content scan failed after');
+ expect(result.stdout).not.toContain('Pregate passed');
+ const report = JSON.parse(readFileSync(join(root, PREGATE_REPORT_PATH), 'utf8'));
+ expect(report.stages[0]).toMatchObject({ name: 'hidden-content scan', status: 'failed', exitCode: 3 });
+ });
+
it.skipIf(process.platform === 'win32')('reports a stage child signal as incomplete rather than an ordinary assertion failure', () => {
write('scripts/scan-diff-hidden-content.js', "process.kill(process.pid, 'SIGTERM');\n");
const result = run('--skip-lint');
diff --git a/server/scripts/setup-db-test.mjs b/server/scripts/setup-db-test.mjs
index b94656c000..cadf84a6d5 100644
--- a/server/scripts/setup-db-test.mjs
+++ b/server/scripts/setup-db-test.mjs
@@ -25,6 +25,7 @@ import pg from 'pg';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
+import { describeSetupFailure } from './setupDbTestFailure.mjs';
const HERE = dirname(fileURLToPath(import.meta.url));
const TEST_DB = process.env.PGTESTDATABASE || 'portos_test';
@@ -43,25 +44,36 @@ const conn = {
password: process.env.PGPASSWORD || 'portos',
};
-// 1. Create the database if absent (from a maintenance connection — CREATE
-// DATABASE cannot run inside the target DB or a transaction).
-const admin = new pg.Client({ ...conn, database: process.env.PGADMINDATABASE || 'portos' });
-await admin.connect();
-const exists = await admin.query('SELECT 1 FROM pg_database WHERE datname = $1', [TEST_DB]);
-if (exists.rowCount === 0) {
- await admin.query(`CREATE DATABASE ${TEST_DB}`);
- console.log(`🗄️ Created test database '${TEST_DB}'`);
-} else {
- console.log(`🗄️ Test database '${TEST_DB}' already exists`);
+async function provision() {
+ // 1. Create the database if absent (from a maintenance connection — CREATE
+ // DATABASE cannot run inside the target DB or a transaction).
+ const admin = new pg.Client({ ...conn, database: process.env.PGADMINDATABASE || 'portos' });
+ await admin.connect();
+ const exists = await admin.query('SELECT 1 FROM pg_database WHERE datname = $1', [TEST_DB]);
+ if (exists.rowCount === 0) {
+ await admin.query(`CREATE DATABASE ${TEST_DB}`);
+ console.log(`🗄️ Created test database '${TEST_DB}'`);
+ } else {
+ console.log(`🗄️ Test database '${TEST_DB}' already exists`);
+ }
+ await admin.end();
+
+ // 2. Apply the schema. node-postgres runs a multi-statement string in one
+ // simple-query call (no params), so the whole init-db.sql — including the $$
+ // function bodies — applies as a single batch.
+ const sql = readFileSync(join(HERE, 'init-db.sql'), 'utf8');
+ const client = new pg.Client({ ...conn, database: TEST_DB });
+ await client.connect();
+ await client.query(sql);
+ await client.end();
+ console.log(`✅ Schema applied to '${TEST_DB}'. Run the DB suites with: npm run test:db --prefix server`);
}
-await admin.end();
-// 2. Apply the schema. node-postgres runs a multi-statement string in one
-// simple-query call (no params), so the whole init-db.sql — including the $$
-// function bodies — applies as a single batch.
-const sql = readFileSync(join(HERE, 'init-db.sql'), 'utf8');
-const client = new pg.Client({ ...conn, database: TEST_DB });
-await client.connect();
-await client.query(sql);
-await client.end();
-console.log(`✅ Schema applied to '${TEST_DB}'. Run the DB suites with: npm run test:db --prefix server`);
+// A failure must end the process non-zero with one readable line: pregate and CI
+// treat the exit status as the only provisioning verdict.
+try {
+ await provision();
+} catch (error) {
+ console.error(`❌ Failed to provision '${TEST_DB}': ${describeSetupFailure(error, TEST_DB)}`);
+ process.exit(1);
+}
diff --git a/server/scripts/setup-db-test.test.js b/server/scripts/setup-db-test.test.js
new file mode 100644
index 0000000000..e1a6039674
--- /dev/null
+++ b/server/scripts/setup-db-test.test.js
@@ -0,0 +1,36 @@
+// The exit status is pregate's and CI's only provisioning verdict, so a failed
+// run must be non-zero with a readable line, not an uncaught pg stack (#12833).
+import { describe, it, expect } from 'vitest';
+import { spawnSync } from 'node:child_process';
+import { fileURLToPath } from 'node:url';
+import { describeSetupFailure } from './setupDbTestFailure.mjs';
+
+const SCRIPT = fileURLToPath(new URL('./setup-db-test.mjs', import.meta.url));
+
+describe('setup-db-test.mjs failure reporting', () => {
+ it('exits non-zero with one line and no stack when Postgres is unreachable', () => {
+ // Port 1 refuses immediately; no database is needed to exercise the failure path.
+ const result = spawnSync(process.execPath, [SCRIPT], {
+ encoding: 'utf8',
+ env: { ...process.env, PGHOST: '127.0.0.1', PGPORT: '1', PGTESTDATABASE: 'portos_test' },
+ });
+ expect(result.status).toBe(1);
+ expect(result.stdout).not.toContain('Schema applied');
+ const lines = result.stderr.trim().split('\n');
+ expect(lines).toHaveLength(1);
+ expect(lines[0]).toMatch(/^❌ Failed to provision 'portos_test': /);
+ });
+
+ it('names the superuser requirement and remedy for a denied CREATE EXTENSION', () => {
+ const error = Object.assign(new Error('permission denied to create extension "vector"'), { code: '42501' });
+ const line = describeSetupFailure(error, 'portos_test');
+ expect(line).toContain('superuser');
+ expect(line).toContain("psql -d portos_test -c 'CREATE EXTENSION IF NOT EXISTS vector");
+ expect(line).not.toContain('\n');
+ });
+
+ it('does not misattribute an unrelated privilege error to extensions', () => {
+ const error = Object.assign(new Error('permission denied for table beeper_accounts'), { code: '42501' });
+ expect(describeSetupFailure(error, 'portos_test')).not.toContain('superuser');
+ });
+});
diff --git a/server/scripts/setupDbTestFailure.mjs b/server/scripts/setupDbTestFailure.mjs
new file mode 100644
index 0000000000..3c358475dc
--- /dev/null
+++ b/server/scripts/setupDbTestFailure.mjs
@@ -0,0 +1,20 @@
+/**
+ * One actionable line for a failed `setup-db-test.mjs` run, so the operator
+ * sees the real blocker instead of an uncaught pg stack (or, worse, a missing
+ * table in a later suite).
+ */
+
+// 42501 insufficient_privilege; the message check keeps a plain GRANT problem
+// from being misreported as an extension problem.
+const isExtensionPrivilegeError = (error) =>
+ error?.code === '42501' && /extension/i.test(error.message ?? '');
+
+export function describeSetupFailure(error, testDb) {
+ if (isExtensionPrivilegeError(error)) {
+ return `${error.message}. Creating extensions needs a superuser: as one, run `
+ + `\`psql -d ${testDb} -c 'CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS pgcrypto;'\` `
+ + 'and re-run `npm run setup:db:test`, or provision with a role that can create them.';
+ }
+ const detail = error?.message || error?.code || String(error);
+ return `${detail}${error?.code && error.message ? ` (${error.code})` : ''}`;
+}