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

address review (codex): degrade backup when required Postgres (MEMORY_BACKEND=postgres) is unreachable; defer manifest-hash restore-verify

Adam Eivy7067793e98fe
diff --git a/docs/superpowers/plans/2026-06-05-verified-pg-backup.md b/docs/superpowers/plans/2026-06-05-verified-pg-backup.md
index 1ac97e0de7..5a9236bbc4 100644
--- a/docs/superpowers/plans/2026-06-05-verified-pg-backup.md
+++ b/docs/superpowers/plans/2026-06-05-verified-pg-backup.md
@@ -974,6 +974,19 @@ Per the project workflow, run `/simplify` to review the changed code for reuse/q
## Deferred follow-ups (from review)
+- [ ] **Verify the DB dump against its manifest hash before restore** (codex P2,
+ declined for this PR). `restorePostgres` trusts a non-empty `portos-db.sql` on
+ size alone; the manifest now records `../portos-db.sql`'s sha256, so a restore
+ could re-hash and compare before replaying. Declined now because it's outside
+ PortOS's stated threat model (single-user, Tailscale-private, never public — so
+ post-write tampering isn't a defended threat), and the truncation case it
+ guards is already largely covered: the empty-dump guard rejects 0-byte files
+ and the `--single-transaction` restore rolls back a syntactically broken dump.
+ The residual risk is a dump truncated at a clean statement boundary — narrow.
+ Add the hash check if PortOS ever relaxes the private-network assumption, or if
+ a real truncated-restore incident occurs. `server/services/backup.js`
+ `restorePostgres` ~line 444.
+
- [ ] **Integration test for `runBackup` pg-status wiring.** Task 3 covered only
the pure `backupStatusForPg` helper (the plan scoped out `runBackup` integration
testing because it touches many fs helpers). The wiring — `saveState` writing
diff --git a/server/services/backup.js b/server/services/backup.js
index 120aee3e5b..ab4d89a454 100644
--- a/server/services/backup.js
+++ b/server/services/backup.js
@@ -232,13 +232,21 @@ export async function runBackup(destPath, io = null, { excludePaths = [], disabl
* Returns an explicit status so the caller can distinguish "no PG configured"
* (benign, file mode) from "PG configured but dump failed" (data at risk):
* { status: 'ok', sizeBytes, tableCount, path }
- * { status: 'skipped', reason: 'not_configured' }
- * { status: 'failed', reason: 'pg_dump_missing'|'dump_error'|'empty_dump', error }
+ * { status: 'skipped', reason: 'not_configured' } (file/auto mode, no PG)
+ * { status: 'failed', reason: 'pg_unreachable'|'pg_dump_missing'|'dump_error'|'empty_dump', error }
* @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. In file/auto mode this is the expected,
+ // benign case (the install legitimately runs without Postgres). But when the
+ // install EXPLICITLY requires Postgres (MEMORY_BACKEND=postgres), 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.
+ if (process.env.MEMORY_BACKEND === 'postgres') {
+ return { status: 'failed', reason: 'pg_unreachable', error: health.error || 'PostgreSQL required (MEMORY_BACKEND=postgres) but not reachable' };
+ }
return { status: 'skipped', reason: 'not_configured' };
}
diff --git a/server/services/backup.test.js b/server/services/backup.test.js
index ca2bd1f2fb..b3637da4ab 100644
--- a/server/services/backup.test.js
+++ b/server/services/backup.test.js
@@ -154,11 +154,25 @@ describe('dumpPostgres status classification', () => {
({ dumpPostgres } = await import('./backup.js'));
});
- it('returns skipped/not_configured when PG is not connected', async () => {
+ it('returns skipped/not_configured when PG is not connected (file/auto mode)', async () => {
+ const prev = process.env.MEMORY_BACKEND;
+ delete process.env.MEMORY_BACKEND;
checkHealth.mockResolvedValue({ connected: false, hasSchema: false });
const result = await dumpPostgres('/tmp/x.sql');
expect(result).toEqual({ status: 'skipped', reason: 'not_configured' });
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';
+ 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 skipped/not_configured when connected but no schema', async () => {