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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: make DB test fixtures independent of stray objects and overlapping runs (#12308)
[._.]/ Adam Eivy731f6ef5e807
diff --git a/server/lib/db.js b/server/lib/db.jsindex 8e8ef54c74..94134e926a 100644--- a/server/lib/db.js+++ b/server/lib/db.js@@ -503,16 +503,64 @@ export async function ensureSchema(options = {}) {return databaseOperation(() => ensureSchemaReady(options));}+/**+ * Owned function signatures declared by the schema DDL: name -> argument type+ * lists. Argument names, modes and defaults are stripped so each list can be+ * resolved through `to_regprocedure`.+ */+export function ownedFunctionSignatures(statements) {+ const owned = new Map();+ const pattern = /CREATE\s+(?:OR\s+REPLACE\s+)?FUNCTION\s+(?:public\.)?([a-z_][a-z0-9_]*)\s*\(([^)]*)\)/gi;+ for (const sql of statements) {+ for (const [, name, rawArgs] of sql.matchAll(pattern)) {+ const types = rawArgs.split(',').map((arg) => arg.trim()).filter(Boolean).map((arg) => {+ const words = arg.replace(/\s+(?:DEFAULT|=)\s.*$/i, '').replace(/^(?:IN|INOUT|VARIADIC)\s+/i, '').split(/\s+/);+ return words.length > 1 ? words.slice(1).join(' ') : words[0];+ });+ if (!owned.has(name)) owned.set(name, []);+ owned.get(name).push(types);+ }+ }+ return owned;+}++// `CREATE OR REPLACE FUNCTION` only replaces the same argument list, so an+// overload another branch left behind survives a forced reset and can later+// collide with a restore. Drops public overloads of schema-owned names that no+// owned definition declares. Test database only: on a real install an unknown+// overload may belong to the operator.+async function dropStrayOwnedFunctionOverloads(client, statements) {+ for (const [name, signatures] of ownedFunctionSignatures(statements)) {+ const ownedOids = [];+ for (const types of signatures) {+ const { rows: [row] } = await client.query('SELECT to_regprocedure($1)::oid AS oid', [`public.${name}(${types.join(',')})`]);+ if (!row?.oid) { ownedOids.length = 0; break; }+ ownedOids.push(row.oid);+ }+ if (!ownedOids.length) continue;+ const { rows } = await client.query(+ `SELECT p.oid::regprocedure::text AS signature FROM pg_proc p+ JOIN pg_namespace n ON n.oid = p.pronamespace+ WHERE n.nspname = 'public' AND p.proname = $1 AND p.prokind = 'f' AND NOT (p.oid = ANY($2::oid[]))`,+ [name, ownedOids],+ );+ for (const { signature } of rows) {+ await client.query(`DROP FUNCTION ${signature}`);+ console.warn(`🧹 Dropped stray test-database function overload ${signature}`);+ }+ }+}+async function ensureSchemaReady({ force = false } = {}) {if (ensureSchemaInFlight) return ensureSchemaInFlight;if (schemaEnsured && !force) return;schemaEnsured = false;- ensureSchemaInFlight = ensureSchemaImpl().then(() => { schemaEnsured = true; })+ ensureSchemaInFlight = ensureSchemaImpl({ force }).then(() => { schemaEnsured = true; }).finally(() => { ensureSchemaInFlight = null; });return ensureSchemaInFlight;}-async function ensureSchemaImpl() {+async function ensureSchemaImpl({ force = false } = {}) {// The DDL composer is imported lazily, not at module load. `db.js` is reached// by ~400 server test files through pgFileFacade.js, and eagerly importing// ./db/schema/index.js pulled its 17 per-domain DDL modules into every one of@@ -586,6 +634,7 @@ async function ensureSchemaImpl() {for (const sql of catalogDDL) {await client.query(sql);}+ if (force && isTestDatabase()) await dropStrayOwnedFunctionOverloads(client, [...upgrades, ...catalogDDL]);if (!schemaUpgradeLogged) {console.log('🗄️ Database schema upgrades applied');schemaUpgradeLogged = true;diff --git a/server/services/backup.db.test.js b/server/services/backup.db.test.jsindex 772d3751a6..37ae46b340 100644--- a/server/services/backup.db.test.js+++ b/server/services/backup.db.test.js@@ -5,6 +5,7 @@ import { tmpdir } from 'node:os';import { basename, join } from 'node:path';import { spawnSync } from 'node:child_process';import { randomUUID } from 'node:crypto';+import pg from 'pg';import { checkHealth, ensureSchema, query, close, getServerMajorVersion, POOL_CONFIG } from '../lib/db.js';import { requireDbOrSkip } from '../lib/dbTestGate.js';import { ownTestBodies } from '../lib/mockPathsDataRoot.js';@@ -64,11 +65,26 @@ const health = await checkHealth();const ready = requireDbOrSkip('services/backup.db.test',health.connected && spawnSync('psql', ['--version']).status === 0,'test database or PostgreSQL client tools unavailable');+// Every restore replaces the whole database, so two runs cannot share+// portos_test. A session advisory lock queues an overlapping run behind this one.+const RESTORE_SUITE_LOCK_KEY = 5977002;+let suiteLock = null;+if (ready) {+ suiteLock = new pg.Client(POOL_CONFIG);+ await suiteLock.connect();+ await suiteLock.query('SELECT pg_advisory_lock($1)', [RESTORE_SUITE_LOCK_KEY]);+}let dest;let dumpPath;-const folderId = 'restore-schema-folder-probe';-const issueId = 'restore-schema-issue-probe';-const personId = '00000000-0000-4000-8000-000000000862';+// Per-run identifiers: a row an earlier or overlapping run left behind can+// never collide with this run's inserts, and teardown deletes only its own.+const runId = randomUUID();+const folderId = `restore-schema-folder-${runId}`;+const issueId = `restore-schema-issue-${runId}`;+const seriesId = `restore-schema-series-${runId}`;+const probeAppId = `restore-probe-${runId}`;+const personId = randomUUID();+const postSnapshotPersonId = randomUUID();const executionHostId = randomUUID();const pendingMigration = '007-storyboard-scene-durable-ids.js';const connectionArgs = ['-h', POOL_CONFIG.host, '-p', String(POOL_CONFIG.port), '-U', POOL_CONFIG.user, '-d', POOL_CONFIG.database];@@ -92,8 +108,8 @@ afterAll(async () => {await query('DELETE FROM peer_execution_generation_floors WHERE host_instance_id = $1', [executionHostId]);await query('DELETE FROM writers_room_folders WHERE id = $1', [folderId]);await query('DELETE FROM pipeline_issues WHERE id = $1', [issueId]);- await query("DELETE FROM tribe_people WHERE id = $1 OR id = '00000000-0000-4000-8000-000000000863'", [personId]);- await query("DELETE FROM app_quality_measurements WHERE app_id = 'restore-probe'");+ await query('DELETE FROM tribe_people WHERE id = ANY($1::uuid[])', [[personId, postSnapshotPersonId]]);+ await query('DELETE FROM app_quality_measurements WHERE app_id = $1', [probeAppId]);await runDbMigrations();}} finally {@@ -102,6 +118,7 @@ afterAll(async () => {close(),dest && rm(dest, { recursive: true, force: true }),dataRoot.path && rm(dataRoot.path, { recursive: true, force: true }),+ suiteLock?.end(),]);}await drained;@@ -162,8 +179,8 @@ describe.skipIf(!ready)('restore older database schema', () => {await runDbMigrations();appliedBefore = await query('SELECT id, applied_at FROM schema_migrations WHERE id <> $1 ORDER BY id', [pendingMigration]);await query('INSERT INTO writers_room_folders (id, name, data) VALUES ($1, $2, $3)', [folderId, folder.name, folder]);- await query("INSERT INTO pipeline_issues (id, series_id, data) VALUES ($1, 'restore-schema-series', $2)", [- issueId, { stages: { storyboards: { scenes: [{ description: 'Synthetic scene' }] } } },+ await query('INSERT INTO pipeline_issues (id, series_id, data) VALUES ($1, $2, $3)', [+ issueId, seriesId, { stages: { storyboards: { scenes: [{ description: 'Synthetic scene' }] } } },]);await query("INSERT INTO tribe_people (id, name) VALUES ($1, 'Snapshot person')", [personId]);// The snapshot predates this table and its inbound FK to tribe_people.@@ -185,10 +202,10 @@ describe.skipIf(!ready)('restore older database schema', () => {await ensureSchema({ force: true });await runDbMigrations();await query("UPDATE tribe_people SET name = 'After backup' WHERE id = $1", [personId]);- await query("INSERT INTO tribe_people (id, name) VALUES ('00000000-0000-4000-8000-000000000863', 'Post-snapshot person')");+ await query("INSERT INTO tribe_people (id, name) VALUES ($1, 'Post-snapshot person')", [postSnapshotPersonId]);// A simpler approved additive table holds post-snapshot rows to prove a// full replacement, independently of Beeper's account/conversation graph.- await query("INSERT INTO app_quality_measurements VALUES ('restore-probe', 'test', 'agent', NOW(), '{}')");+ await query("INSERT INTO app_quality_measurements VALUES ($1, 'test', 'agent', NOW(), '{}')", [probeAppId]);// Feed positions handed out after the dump must never be reissued (#8710).await query("SELECT nextval('memories_sync_feed_seq') FROM generate_series(1, 5)");feedBefore = await feedSequenceValues();@@ -205,8 +222,8 @@ describe.skipIf(!ready)('restore older database schema', () => {expect(BigInt(next)).toBeGreaterThan(BigInt(feedBefore.memories_sync_feed_seq));expect(await listFolders()).toContainEqual(folder);expect((await query('SELECT name FROM tribe_people WHERE id = $1', [personId])).rows).toEqual([{ name: 'Snapshot person' }]);- expect((await query("SELECT id FROM tribe_people WHERE id = '00000000-0000-4000-8000-000000000863'")).rowCount).toBe(0);- expect((await query("SELECT * FROM app_quality_measurements WHERE app_id = 'restore-probe'")).rowCount).toBe(0);+ expect((await query('SELECT id FROM tribe_people WHERE id = $1', [postSnapshotPersonId])).rowCount).toBe(0);+ expect((await query('SELECT * FROM app_quality_measurements WHERE app_id = $1', [probeAppId])).rowCount).toBe(0);expect((await query('SELECT * FROM beeper_participants')).rowCount).toBe(0);expect((await query("SELECT 1 FROM pg_constraint WHERE conrelid = 'beeper_participants'::regclass AND confrelid = 'tribe_people'::regclass")).rowCount).toBe(1);const issue = await query('SELECT data FROM pipeline_issues WHERE id = $1', [issueId]);diff --git a/server/vitest.config.db.js b/server/vitest.config.db.jsindex 163cfebfa7..9dbfeb9aa0 100644--- a/server/vitest.config.db.js+++ b/server/vitest.config.db.js@@ -44,6 +44,9 @@ export default defineConfig({// Environment teardown also owns collection-only/skipped suite fixtures.environment: fileURLToPath(new URL('./test/mediaRegistryEnvironment.js', import.meta.url)),testTimeout: process.platform === 'win32' ? 30000 : 15000,+ // Vitest's 10 s default hook budget is below the schema rebuild, migrations+ // and pg_dump a DB fixture runs in beforeAll/beforeEach on a loaded host.+ hookTimeout: 30000,// Explicit 'vitest' imports only, matching vitest.config.js (#9049).globals: false,globalSetup: [fileURLToPath(new URL('../scripts/vitestTempRootSetup.js', import.meta.url))],