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
address review (codex): gate boot on catalog schema; treat null backend as PG-required in backup
Adam Eivycc5b64e40f8a
diff --git a/docs/BACKUP.md b/docs/BACKUP.mdindex d512a5f7bd..b946dc846d 100644--- a/docs/BACKUP.md+++ b/docs/BACKUP.md@@ -40,7 +40,7 @@ The effective exclude list is computed by the pure `computeEffectiveExcludes()`|---|---|---|| `{ status: 'ok', sizeBytes, tableCount }` | Dump succeeded and is non-empty | Backup `ok` || `{ status: 'failed', reason: 'pg_unreachable' \| 'pg_dump_missing' \| 'dump_error' \| 'empty_dump' }` | Postgres is the active backend but the dump failed | Backup **`degraded`** + warning toast |-| `{ status: 'skipped', reason: 'not_configured' }` | Postgres is not the active backend — `MEMORY_BACKEND=file`, OR the memory backend hasn't initialized yet (`getBackendName()` is still `null`, so auto-detect hasn't resolved to `postgres`) | Backup stays `ok` (benign) |+| `{ status: 'skipped', reason: 'not_configured' }` | The explicit file escape hatch is active — `MEMORY_BACKEND=file` or the backend resolved to `file` (dev/test, unsupported for production) | Backup stays `ok` (benign) |Key behaviors, accurate to the code:diff --git a/server/index.js b/server/index.jsindex 7f964ec2af..af5f3d815b 100644--- a/server/index.js+++ b/server/index.js@@ -781,7 +781,7 @@ ensureSelf()// tables absent) makes ensureSchema() throw — we catch, log, and fall// through to the fail-fast below. (try/catch is appropriate here: this runs// outside the request lifecycle, so an uncaught throw would crash boot.)- if (health.connected && !health.hasSchema) {+ if (health.connected && (!health.hasSchema || !health.hasCatalogSchema)) {try {await ensureSchema();health = await checkHealth();@@ -789,7 +789,12 @@ ensureSelf()console.error(`🗄️ Schema upgrade on boot failed: ${err.message}`);}}- const dbReady = health.connected && health.hasSchema;+ // Both the memory schema AND the creative-catalog schema are required —+ // the catalog has no file-backed equivalent. ensureSchema() creates the+ // catalog tables idempotently, but if that DDL fails (e.g. the role can't+ // CREATE) the swallowed error in the migration block below would otherwise+ // let the server boot with the catalog missing. Gate boot on both.+ const dbReady = health.connected && health.hasSchema && health.hasCatalogSchema;if (!dbEscapeHatch && !dbReady) {const reason = health.connected ? 'required schema missing' : `unreachable (${health.error || 'connection failed'})`;console.error(`❌ PostgreSQL is required but ${reason} — refusing to start.`);diff --git a/server/services/backup.js b/server/services/backup.jsindex 2a08456be5..f0c7adf026 100644--- a/server/services/backup.js+++ b/server/services/backup.js@@ -230,31 +230,32 @@ export async function runBackup(destPath, io = null, { excludePaths = [], disabl/*** Run pg_dump to create a PostgreSQL backup alongside the rsync snapshot.- * Returns an explicit status so the caller can distinguish "no PG configured"- * (benign, file mode) from "PG configured but dump failed" (data at risk):+ * Returns an explicit status so the caller can distinguish the benign file+ * escape hatch from "PG required but dump failed" (data at risk):* { status: 'ok', sizeBytes, tableCount }- * { status: 'skipped', reason: 'not_configured' } (PG not the active backend)+ * { status: 'skipped', reason: 'not_configured' } (explicit file escape hatch only)* { status: 'failed', reason: 'pg_unreachable'|'pg_dump_missing'|'dump_error'|'empty_dump', error }- * (pg_unreachable fires when Postgres is the active backend — explicit or- * auto-detected — but the DB is down at backup time)+ * (pg_unreachable fires whenever Postgres is required — i.e. not the file+ * escape hatch — but the DB is down at backup time)* @param {string} outputPath - Path to write the SQL dump file*/export async function dumpPostgres(outputPath) {const health = await checkHealth();if (!health.connected || !health.hasSchema) {- // PG unreachable or uninitialized. When the install legitimately runs- // without Postgres this is the expected, benign case. But when Postgres is- // the ACTIVE memory backend, an unreachable DB is a real backup failure —- // data that lives only in PG won't be captured — so degrade the backup and- // alert rather than silently skip. Postgres is active when it's required- // explicitly (MEMORY_BACKEND=postgres) OR when it was auto-detected at- // startup (MEMORY_BACKEND unset and the resolved backend is 'postgres').- // The auto-detect case is the common default install, so gating only on the- // env var would let a real DB outage read as a green "not configured" run.+ // PG unreachable or uninitialized. Since PostgreSQL is now a mandatory+ // dependency, the ONLY benign "no PG to back up" case is the explicit file+ // escape hatch (MEMORY_BACKEND=file, or the backend resolved to 'file' in+ // test/dev mode). Every other state — including a default install whose+ // memory backend simply hasn't initialized yet (getBackendName() === null)+ // — means Postgres is required, so an unreachable DB is a real backup+ // failure (data that lives only in PG won't be captured). Degrade and alert+ // rather than silently skip; gating on getBackendName() === 'postgres'+ // alone would let an outage-before-first-memory-access read as a green+ // "not configured" run.const env = process.env.MEMORY_BACKEND;- const postgresIsActive = env === 'postgres' || (env !== 'file' && getBackendName() === 'postgres');- if (postgresIsActive) {- return { status: 'failed', reason: 'pg_unreachable', error: health.error || 'PostgreSQL is the active memory backend but is not reachable' };+ const fileEscapeHatch = env === 'file' || getBackendName() === 'file';+ if (!fileEscapeHatch) {+ return { status: 'failed', reason: 'pg_unreachable', error: health.error || 'PostgreSQL is required but is unreachable or uninitialized' };}return { status: 'skipped', reason: 'not_configured' };}diff --git a/server/services/backup.test.js b/server/services/backup.test.jsindex 974439c89a..a2e9f4d3ca 100644--- a/server/services/backup.test.js+++ b/server/services/backup.test.js@@ -161,10 +161,10 @@ describe('dumpPostgres status classification', () => {({ dumpPostgres } = await import('./backup.js'));});- it('returns skipped/not_configured when PG is not connected and not the active backend', async () => {+ it('returns skipped/not_configured when PG is down and the backend resolved to file (escape hatch)', async () => {const prev = process.env.MEMORY_BACKEND;delete process.env.MEMORY_BACKEND;- getBackendName.mockReturnValue('file'); // auto-detect resolved to file+ getBackendName.mockReturnValue('file'); // dev/test escape hatch resolved to filecheckHealth.mockResolvedValue({ connected: false, hasSchema: false });const result = await dumpPostgres('/tmp/x.sql');expect(result).toEqual({ status: 'skipped', reason: 'not_configured' });@@ -173,6 +173,22 @@ describe('dumpPostgres status classification', () => {if (prev === undefined) delete process.env.MEMORY_BACKEND; else process.env.MEMORY_BACKEND = prev;});+ it('returns failed/pg_unreachable when PG is down, env unset, and backend not yet initialized (null)', async () => {+ // Post-mandatory-Postgres contract: a default install whose memory backend+ // hasn't initialized yet (getBackendName() === null) still REQUIRES Postgres.+ // A DB outage before the first memory access must degrade the backup, not+ // read as a benign "not configured" skip.+ const prev = process.env.MEMORY_BACKEND;+ delete process.env.MEMORY_BACKEND;+ getBackendName.mockReturnValue(null);+ checkHealth.mockResolvedValue({ connected: false, hasSchema: false, error: 'ECONNREFUSED' });+ const result = await dumpPostgres('/tmp/x.sql');+ expect(result.status).toBe('failed');+ expect(result.reason).toBe('pg_unreachable');+ expect(spawn).not.toHaveBeenCalled();+ if (prev === undefined) delete process.env.MEMORY_BACKEND; else process.env.MEMORY_BACKEND = prev;+ });+it('returns failed/pg_unreachable when PG is required (MEMORY_BACKEND=postgres) but down', async () => {const prev = process.env.MEMORY_BACKEND;process.env.MEMORY_BACKEND = 'postgres';@@ -213,10 +229,25 @@ describe('dumpPostgres status classification', () => {if (prev === undefined) delete process.env.MEMORY_BACKEND; else process.env.MEMORY_BACKEND = prev;});- it('returns skipped/not_configured when connected but no schema', async () => {+ it('returns failed/pg_unreachable when connected but schema missing and PG required', async () => {+ // Post-mandatory-Postgres: a reachable-but-uninitialized DB on a non-file+ // install is a real backup failure (required schema/data not capturable),+ // not a benign skip.+ getBackendName.mockReturnValue(null);+ checkHealth.mockResolvedValue({ connected: true, hasSchema: false });+ const result = await dumpPostgres('/tmp/x.sql');+ expect(result.status).toBe('failed');+ expect(result.reason).toBe('pg_unreachable');+ });++ it('returns skipped/not_configured when connected but no schema in file escape-hatch mode', async () => {+ const prev = process.env.MEMORY_BACKEND;+ process.env.MEMORY_BACKEND = 'file';checkHealth.mockResolvedValue({ connected: true, hasSchema: false });const result = await dumpPostgres('/tmp/x.sql');expect(result.status).toBe('skipped');+ expect(result.reason).toBe('not_configured');+ if (prev === undefined) delete process.env.MEMORY_BACKEND; else process.env.MEMORY_BACKEND = prev;});it('returns failed/pg_dump_missing when spawn errors', async () => {