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 (claude): psql ON_ERROR_STOP, empty-dump guard on restore, DEFAULT_STATE.pgBackup, restore-path tests

Adam Eivyb75d67598d24
diff --git a/server/services/backup.js b/server/services/backup.js
index 9f1177953d..6ecdf0c311 100644
--- a/server/services/backup.js
+++ b/server/services/backup.js
@@ -57,6 +57,7 @@ const DEFAULT_STATE = {
status: 'never',
lastSnapshotId: null,
filesChanged: 0,
+ pgBackup: null,
error: null
};
@@ -424,7 +425,10 @@ export async function restorePostgres(destPath, snapshotId, { dryRun = true } =
}
const info = await stat(sqlPath).catch(() => null);
- if (!info || !info.isFile?.()) {
+ // An empty/0-byte dump is as good as absent — restoring it is a silent no-op.
+ // Mirror dumpPostgres's empty-dump guard so a truncated snapshot can't read
+ // as a successful "0 tables" restore.
+ if (!info || !info.isFile?.() || info.size === 0) {
return { status: 'skipped', reason: 'no_dump' };
}
const sql = await readFile(sqlPath, 'utf-8').catch(() => '');
@@ -446,7 +450,11 @@ export async function restorePostgres(destPath, snapshotId, { dryRun = true } =
const pgUser = process.env.PGUSER || 'portos';
return new Promise((resolveP) => {
+ // ON_ERROR_STOP=1 makes psql abort and exit non-zero on the first failed
+ // statement. Without it psql replays best-effort and exits 0 even when
+ // statements fail — a partial restore would falsely report success.
const proc = spawn('psql', [
+ '-v', 'ON_ERROR_STOP=1',
'-h', pgHost, '-p', pgPort, '-U', pgUser, '-d', pgDb, '-f', sqlPath
], { shell: false, env: { ...process.env, PGPASSWORD: process.env.PGPASSWORD || 'portos' } });
diff --git a/server/services/backup.test.js b/server/services/backup.test.js
index 5f01498b97..12615d5c96 100644
--- a/server/services/backup.test.js
+++ b/server/services/backup.test.js
@@ -262,6 +262,46 @@ describe('restorePostgres', () => {
expect(result).toEqual({ status: 'skipped', reason: 'not_configured' });
expect(spawn).not.toHaveBeenCalled();
});
+
+ it('treats a 0-byte dump as no_dump (does not restore a truncated snapshot)', async () => {
+ vi.spyOn(fs, 'stat').mockResolvedValue({ size: 0, isFile: () => true });
+ const result = await restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: false });
+ expect(result).toEqual({ status: 'skipped', reason: 'no_dump' });
+ expect(spawn).not.toHaveBeenCalled();
+ });
+
+ it('real restore spawns psql with ON_ERROR_STOP and shell:false, returns ok on exit 0', async () => {
+ vi.spyOn(fs, 'stat').mockResolvedValue({ size: 4096, isFile: () => true });
+ vi.spyOn(fs, 'readFile').mockResolvedValue('CREATE TABLE a (...);\n');
+ checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
+ const proc = fakeProc();
+ spawn.mockReturnValue(proc);
+ const p = restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: false });
+ await flush();
+ proc.emit('close', 0);
+ const result = await p;
+ expect(result).toEqual({ status: 'ok', dryRun: false, sizeBytes: 4096, tableCount: 1 });
+ const [bin, args, opts] = spawn.mock.calls[0];
+ expect(bin).toBe('psql');
+ expect(args).toEqual(expect.arrayContaining(['-v', 'ON_ERROR_STOP=1', '-f']));
+ expect(opts.shell).toBe(false);
+ });
+
+ it('real restore returns failed/restore_error on non-zero psql exit', async () => {
+ vi.spyOn(fs, 'stat').mockResolvedValue({ size: 4096, isFile: () => true });
+ vi.spyOn(fs, 'readFile').mockResolvedValue('CREATE TABLE a (...);\n');
+ checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
+ const proc = fakeProc();
+ spawn.mockReturnValue(proc);
+ const p = restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: false });
+ await flush();
+ proc.stderr.emit('data', Buffer.from('ERROR: relation already exists'));
+ proc.emit('close', 1);
+ const result = await p;
+ expect(result.status).toBe('failed');
+ expect(result.reason).toBe('restore_error');
+ expect(result.error).toContain('already exists');
+ });
});
describe('runBackup pg status propagation', () => {