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 failed database start/stop/destroy instead of success (#10889)

Adam Eivyb37a3f881347
diff --git a/client/src/components/settings/DatabaseTab.jsx b/client/src/components/settings/DatabaseTab.jsx
index b1fe84af29..40dca9e302 100644
--- a/client/src/components/settings/DatabaseTab.jsx
+++ b/client/src/components/settings/DatabaseTab.jsx
@@ -402,6 +402,12 @@ export function DatabaseTab() {
setActionInProgress(key);
fn()
.then((result) => {
+ // Legacy/defensive: a resolved `success: false` is a failure, never a success toast.
+ if (result?.success === false) {
+ toast.error(result.output || result.error || 'Database operation failed');
+ loadStatus();
+ return;
+ }
if (successMsg) toast.success(typeof successMsg === 'function' ? successMsg(result) : successMsg);
loadStatus();
})
diff --git a/client/src/components/settings/DatabaseTab.test.jsx b/client/src/components/settings/DatabaseTab.test.jsx
index 6da46ca347..cbeef9aee7 100644
--- a/client/src/components/settings/DatabaseTab.test.jsx
+++ b/client/src/components/settings/DatabaseTab.test.jsx
@@ -181,6 +181,26 @@ describe('DatabaseTab sync and replacement', () => {
await waitFor(() => expect(destroyDatabase).toHaveBeenCalledWith('native'));
});
+ it('shows an error, never a success toast, for a legacy resolved success:false', async () => {
+ getDatabaseStatus.mockResolvedValue({
+ ...dbStatus,
+ docker: { containerRunning: true, installed: true, daemonRunning: true },
+ native: { configured: true, installed: true, running: true },
+ mode: 'docker',
+ });
+ destroyDatabase.mockResolvedValue({ success: false, output: 'volume busy' });
+ render(<DatabaseTab />);
+ await waitFor(() => expect(getDatabaseStatus).toHaveBeenCalled());
+ const callsBefore = getDatabaseStatus.mock.calls.length;
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Delete Native database…' }));
+ fireEvent.click(screen.getByRole('button', { name: 'Delete Native database' }));
+
+ await waitFor(() => expect(toast.error).toHaveBeenCalledWith('volume busy'));
+ expect(toast.success).not.toHaveBeenCalled();
+ await waitFor(() => expect(getDatabaseStatus.mock.calls.length).toBeGreaterThan(callsBefore));
+ });
+
it('does not offer Native deletion when it is active, stopped, or unconfigured', async () => {
const statuses = [
{ ...dbStatus, mode: 'native', native: { configured: true, installed: true, running: true } },
diff --git a/scripts/db-lifecycle-dispatch.test.js b/scripts/db-lifecycle-dispatch.test.js
index 8c2cfd0367..355ef5319c 100644
--- a/scripts/db-lifecycle-dispatch.test.js
+++ b/scripts/db-lifecycle-dispatch.test.js
@@ -39,7 +39,12 @@ describe.skipIf(process.platform === 'win32')('scripts/db.sh start/stop backend
const binDir = join(root, 'bin');
mkdirSync(binDir);
symlinkSync(process.execPath, join(binDir, 'node'));
- for (const name of ['docker', 'psql', 'pg_isready']) writeStub(binDir, name, LOGGING_STUB(name));
+ for (const name of ['docker', 'psql']) writeStub(binDir, name, LOGGING_STUB(name));
+ // Defaults to "accepting connections"; STUB_ISREADY_RC=2 simulates a down endpoint.
+ writeStub(binDir, 'pg_isready', `#!/bin/sh
+echo "pg_isready $*" >> "$STUB_LOG"
+exit "\${STUB_ISREADY_RC:-0}"
+`);
// Not Darwin, so db.sh skips prepending a real Homebrew Postgres to PATH.
writeStub(binDir, 'uname', '#!/bin/sh\necho Linux\n');
stubLog = join(root, 'stub.log');
@@ -84,7 +89,7 @@ describe.skipIf(process.platform === 'win32')('scripts/db.sh start/stop backend
});
it('stops only native when Docker is the active backend', () => {
- const result = run('stop', NATIVE_CHILD);
+ const result = run('stop', { ...NATIVE_CHILD, STUB_ISREADY_RC: '2' });
expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain('Stopping native PostgreSQL');
@@ -101,4 +106,30 @@ describe.skipIf(process.platform === 'win32')('scripts/db.sh start/stop backend
expect(result.stdout).toContain('Stopping Docker PostgreSQL');
expect(readFileSync(stubLog, 'utf8')).toMatch(/^docker compose stop db$/m);
});
+
+ // #10889: a failed stop must not read as "Stopped" with exit 0.
+ it('fails Docker stop when `docker compose stop db` fails', () => {
+ writeStub(join(root, 'bin'), 'docker', '#!/bin/sh\necho "docker $*" >> "$STUB_LOG"\n[ "$1 $2" = "compose stop" ] && { echo "daemon unreachable" >&2; exit 1; }\nexit 0\n');
+
+ const result = run('stop');
+
+ expect(result.status).not.toBe(0);
+ expect(result.stdout).not.toContain('Stopped');
+ expect(result.stderr + result.stdout).toContain('daemon unreachable');
+ });
+
+ it('fails native stop while the endpoint still accepts connections', () => {
+ const result = run('stop', { ...NATIVE_CHILD, STUB_ISREADY_RC: '0' });
+
+ expect(result.status).not.toBe(0);
+ expect(result.stdout).not.toMatch(/Stopped\b/);
+ expect(result.stdout).toContain('still accepting connections');
+ });
+
+ it('keeps native stop idempotent when the endpoint is already down', () => {
+ const result = run('stop', { ...NATIVE_CHILD, STUB_ISREADY_RC: '2' });
+
+ expect(result.status, result.stderr).toBe(0);
+ expect(result.stdout).toContain('Stopped');
+ });
});
diff --git a/scripts/db.sh b/scripts/db.sh
index c07b363687..2b564b1294 100755
--- a/scripts/db.sh
+++ b/scripts/db.sh
@@ -401,28 +401,50 @@ stop_docker() {
info "Stopping Docker PostgreSQL..."
require_docker_compose
cd "$ROOT_DIR"
- docker compose stop db 2>/dev/null || true
+ # Stopping an absent or already-stopped container exits 0, so a nonzero exit
+ # is a genuine failure (daemon down, permissions) and must not read as success.
+ if ! docker compose stop db; then
+ err "Failed to stop Docker PostgreSQL"
+ exit 1
+ fi
log "Stopped"
}
+# True while the selected native endpoint still accepts connections.
+native_accepting() {
+ pg_bound pg_isready -h "$PGHOST" -p "$PGPORT" >/dev/null 2>&1
+}
+
stop_native() {
info "Stopping native PostgreSQL..."
+ local stop_failed=false
# Stop via Homebrew services (macOS)
if [ "$(uname)" = "Darwin" ] && command -v brew >/dev/null 2>&1; then
- brew services stop postgresql@17 2>/dev/null || true
- log "Stopped"
- return
- fi
- # Fallback: pg_ctl
- if command -v pg_ctl >/dev/null 2>&1; then
+ brew services stop postgresql@17 || stop_failed=true
+ elif command -v pg_ctl >/dev/null 2>&1; then
+ # Fallback: pg_ctl
local datadir=""
if [ "$(uname)" = "Darwin" ] && command -v brew >/dev/null 2>&1; then
datadir="$(brew --prefix)/var/postgresql@17"
fi
if [ -n "$datadir" ] && [ -d "$datadir" ]; then
- pg_ctl -D "$datadir" stop -m fast 2>/dev/null || true
+ pg_ctl -D "$datadir" stop -m fast || stop_failed=true
fi
fi
+ # Claim Stopped only once the endpoint stops answering. A failing stop command
+ # against an endpoint that is already down stays an idempotent success.
+ local tries=0
+ while native_accepting && [ "$tries" -lt 8 ]; do
+ sleep 0.25
+ tries=$((tries + 1))
+ done
+ if native_accepting; then
+ err "Native PostgreSQL is still accepting connections on port $PGPORT"
+ exit 1
+ fi
+ if [ "$stop_failed" = true ]; then
+ warn "Stop command failed, but nothing is listening on port $PGPORT"
+ fi
log "Stopped"
}
diff --git a/server/routes/database.test.js b/server/routes/database.test.js
index 45663df1e5..bba60aaa2f 100644
--- a/server/routes/database.test.js
+++ b/server/routes/database.test.js
@@ -96,6 +96,19 @@ function mockExecFile(responses) {
});
}
+const NO_SUCH_VOLUME = { exitCode: 1, stdout: '', stderr: 'Error response from daemon: get x: no such volume' };
+// Docker destroy call order after the status probe: compose stop, compose rm,
+// volume rm x2, then verification (compose ps, volume inspect x2).
+const dockerDestroyResponses = (statusMode = 'native', overrides = {}) => {
+ const calls = [
+ { exitCode: 0, stdout: `Current mode: ${statusMode}` },
+ { exitCode: 0 }, { exitCode: 0 }, { exitCode: 0 }, { exitCode: 0 },
+ { exitCode: 0, stdout: '' }, NO_SUCH_VOLUME, NO_SUCH_VOLUME,
+ ];
+ for (const [index, response] of Object.entries(overrides)) calls[index] = response;
+ return calls;
+};
+
function makeApp() {
const app = express();
app.use(express.json());
@@ -171,6 +184,29 @@ describe('database admin maintenance admission', () => {
// #10888: db.sh picks start/stop from the mode it loads, which is the ACTIVE
// backend (inherited env / saved .env). A native request must pin the child to
// native, and must never touch Docker or the process's own environment.
+describe('lifecycle command failures (#10889)', () => {
+ it.each([
+ ['start', 'docker'], ['start', 'native'], ['stop', 'docker'], ['stop', 'native'],
+ ])('returns a non-2xx coded error when %s %s exits nonzero', async (action, backend) => {
+ mockExecFile([{ exitCode: 1, stderr: 'boom \u001b[31mfailure\u001b[0m' }]);
+
+ const res = await request(makeApp()).post(`/api/database/${action}`).send({ backend });
+
+ expect(res.status).toBe(500);
+ expect(res.body).toMatchObject({ code: 'DATABASE_COMMAND_FAILED' });
+ expect(res.body.error).toMatch(new RegExp(`Failed to ${action} ${backend}`, "i"));
+ expect(res.body.error).toContain('boom failure');
+ expect(res.body.timestamp).toBeDefined();
+ });
+
+ it('bounds the diagnostic included in the error', async () => {
+ mockExecFile([{ exitCode: 1, stderr: 'x'.repeat(5000) }]);
+ const res = await request(makeApp()).post('/api/database/start').send({ backend: 'docker' });
+ expect(res.status).toBe(500);
+ expect(res.body.error.length).toBeLessThan(600);
+ });
+});
+
describe('native lifecycle targeting', () => {
const saved = { PGMODE: process.env.PGMODE, PGPORT: process.env.PGPORT, PGHOST: process.env.PGHOST };
afterEach(() => {
@@ -660,13 +696,7 @@ describe('POST /api/database/destroy', () => {
// 2: docker compose rm -f db
// 3: docker volume rm -f portos_portos-pgdata (first volume attempt)
// 4: docker volume rm -f portos-pgdata (alternate volume attempt)
- mockExecFile([
- { exitCode: 0, stdout: 'Current mode: native', stderr: '' },
- { exitCode: 0, stdout: '', stderr: '' }, // compose stop
- { exitCode: 0, stdout: '', stderr: '' }, // compose rm
- { exitCode: 0, stdout: '', stderr: '' }, // volume rm primary
- { exitCode: 0, stdout: '', stderr: '' }, // volume rm alternate
- ]);
+ mockExecFile(dockerDestroyResponses());
const app = makeApp();
const res = await request(app)
@@ -692,7 +722,71 @@ describe('POST /api/database/destroy', () => {
});
});
+ describe('docker destroy failures (#10889)', () => {
+ beforeEach(() => {
+ vi.stubEnv('PGHOST', 'localhost');
+ vi.stubEnv('PGPORT', '5432');
+ });
+
+ it.each([
+ ['compose stop', 1],
+ ['compose rm', 2],
+ ['primary volume rm', 3],
+ ['alternate volume rm', 4],
+ ])('reports a failed %s instead of success and stops issuing commands', async (_step, index) => {
+ mockExecFile(dockerDestroyResponses('native', { [index]: { exitCode: 1, stderr: 'permission denied' } }));
+
+ const res = await request(makeApp()).post('/api/database/destroy').send({ backend: 'docker' });
+
+ expect(res.status).toBe(500);
+ expect(res.body).toMatchObject({ code: 'DATABASE_COMMAND_FAILED' });
+ expect(res.body.error).toMatch(/permission denied/);
+ expect(res.body.timestamp).toBeDefined();
+ expect(execFile).toHaveBeenCalledTimes(index + 1);
+ });
+
+ it('fails when a selected volume still exists after removal', async () => {
+ mockExecFile(dockerDestroyResponses('native', { 6: { exitCode: 0, stdout: '[]' } }));
+ const res = await request(makeApp()).post('/api/database/destroy').send({ backend: 'docker' });
+ expect(res.status).toBe(500);
+ expect(res.body.error).toMatch(/still exists/);
+ });
+
+ it('does not mistake a daemon failure during verification for an absent volume', async () => {
+ mockExecFile(dockerDestroyResponses('native', { 7: { exitCode: 1, stderr: 'Cannot connect to the Docker daemon' } }));
+ const res = await request(makeApp()).post('/api/database/destroy').send({ backend: 'docker' });
+ expect(res.status).toBe(500);
+ expect(res.body.error).toMatch(/Cannot connect to the Docker daemon/);
+ });
+
+ it('fails when the container is still listed after removal', async () => {
+ mockExecFile(dockerDestroyResponses('native', { 5: { exitCode: 0, stdout: 'abc123\n' } }));
+ const res = await request(makeApp()).post('/api/database/destroy').send({ backend: 'docker' });
+ expect(res.status).toBe(500);
+ expect(res.body.error).toMatch(/container still exists/);
+ });
+
+ it('treats an already-absent target as an idempotent success', async () => {
+ mockExecFile(dockerDestroyResponses());
+ const res = await request(makeApp()).post('/api/database/destroy').send({ backend: 'docker' });
+ expect(res.status).toBe(200);
+ expect(res.body.success).toBe(true);
+ });
+ });
+
describe('native destroy path', () => {
+ it('reports a failed DROP DATABASE instead of success (#10889)', async () => {
+ vi.stubEnv('PGHOST', 'localhost');
+ vi.stubEnv('PGPORT', '5561');
+ mockExecFile([
+ { exitCode: 0, stdout: 'Current mode: docker' },
+ { exitCode: 2, stderr: 'connection refused' },
+ ]);
+ const res = await request(makeApp()).post('/api/database/destroy').send({ backend: 'native' });
+ expect(res.status).toBe(500);
+ expect(res.body.error).toMatch(/connection refused/);
+ });
+
it('targets the canonical native endpoint instead of the active Docker port', async () => {
vi.stubEnv('PGHOST', 'localhost');
vi.stubEnv('PGPORT', '5561');
@@ -851,7 +945,7 @@ describe('POST /api/database/sync endpoint safety', () => {
expect(execFile).toHaveBeenCalledTimes(1);
finishProbe(new Error('probe failed'), '', 'probe failed');
expect((await pending).status).toBe(409);
- mockExecFile([{ exitCode: 0, stdout: 'Current mode: native' }]);
+ mockExecFile(dockerDestroyResponses());
const res = await request(app).post('/api/database/destroy').send({ backend: 'docker' });
expect(res.status).toBe(200);
});
diff --git a/server/services/dbAdmin.js b/server/services/dbAdmin.js
index 98d8b3fd0f..4a8ab4b56d 100644
--- a/server/services/dbAdmin.js
+++ b/server/services/dbAdmin.js
@@ -102,6 +102,26 @@ function runCmd(cmd, args, timeout = 120_000, env = process.env) {
});
}
+const DIAGNOSTIC_LIMIT = 400;
+
+/** Bounded, single-line, secret-free command diagnostics for an error envelope. */
+function commandDetail(result) {
+ let detail = (result.stderr || result.stdout || '').replace(/\s+/g, ' ').trim();
+ if (pgPassword) detail = detail.split(pgPassword).join('***');
+ return detail.length > DIAGNOSTIC_LIMIT ? `${detail.slice(0, DIAGNOSTIC_LIMIT)}...` : detail;
+}
+
+/** Throw a coded 500 when a lifecycle command exited nonzero; else return its result. */
+function requireCommandSuccess(result, failureMessage) {
+ if (result.exitCode === 0) return result;
+ const detail = commandDetail(result);
+ throw new ServerError(detail ? `${failureMessage}: ${detail}` : failureMessage, {
+ status: 500,
+ code: 'DATABASE_COMMAND_FAILED',
+ context: { exitCode: result.exitCode, details: detail }
+ });
+}
+
const runDbScript = (args) => runCmd(bashBinary, [dbScript, ...args]);
function _parseDbMode(stdout) {
@@ -644,25 +664,36 @@ function nativeLifecycleEnv() {
async function startDatabaseImpl(backend) {
if (backend === 'docker') {
const result = await runCmd('docker', ['compose', 'up', '-d', 'db'], 60_000);
- return { success: result.exitCode === 0, output: result.stdout };
+ return { success: true, output: requireCommandSuccess(result, 'Failed to start Docker database').stdout };
}
// Native: db.sh handles brew services / pg_ctl, but picks its operation from
// the mode it loads. Bind the child to the native backend explicitly.
const result = await runCmd(bashBinary, [dbScript, 'start'], 30_000, nativeLifecycleEnv());
- return { success: result.exitCode === 0, output: result.stdout };
+ return { success: true, output: requireCommandSuccess(result, 'Failed to start native database').stdout };
}
/** Stop a specific database backend. */
async function stopDatabaseImpl(backend) {
if (backend === 'docker') {
const result = await runCmd('docker', ['compose', 'stop', 'db'], 30_000);
- return { success: result.exitCode === 0, output: result.stdout };
+ return { success: true, output: requireCommandSuccess(result, 'Failed to stop Docker database').stdout };
}
// Native stop: never let the active (possibly Docker) mode choose the target.
const result = await runCmd(bashBinary, [dbScript, 'stop'], 15_000, nativeLifecycleEnv());
- return { success: result.exitCode === 0, output: result.stdout };
+ return { success: true, output: requireCommandSuccess(result, 'Failed to stop native database').stdout };
+}
+
+const DOCKER_VOLUMES = ['portos_portos-pgdata', 'portos-pgdata'];
+
+/** True when `docker volume inspect` proves the volume is gone; throws on any other failure. */
+async function dockerVolumeAbsent(name) {
+ const result = await runCmd('docker', ['volume', 'inspect', name], 15_000);
+ if (result.exitCode === 0) return false;
+ if (/no such volume/i.test(`${result.stderr} ${result.stdout}`)) return true;
+ requireCommandSuccess(result, `Could not verify Docker volume ${name} was removed`);
+ return false;
}
/** Destroy an inactive database backend's data. */
@@ -692,13 +723,28 @@ async function destroyDatabaseImpl(backend) {
}
if (backend === 'docker') {
- // Stop and remove container + volume
- await runCmd('docker', ['compose', 'stop', 'db'], 15_000);
- await runCmd('docker', ['compose', 'rm', '-f', 'db'], 15_000);
- const result = await runCmd('docker', ['volume', 'rm', '-f', 'portos_portos-pgdata'], 15_000);
- // Also try alternate volume name
- await runCmd('docker', ['volume', 'rm', '-f', 'portos-pgdata'], 15_000);
- return { success: true, output: result.stdout };
+ // Stop and remove container + volumes. `rm -f` already treats an absent
+ // container/volume as success (the alternate volume name usually is), so any
+ // nonzero exit is a genuine failure and aborts before claiming deletion.
+ requireCommandSuccess(await runCmd('docker', ['compose', 'stop', 'db'], 15_000), 'Failed to stop Docker database before deletion');
+ requireCommandSuccess(await runCmd('docker', ['compose', 'rm', '-f', 'db'], 15_000), 'Failed to remove Docker database container');
+ let output = '';
+ for (const volume of DOCKER_VOLUMES) {
+ const result = requireCommandSuccess(await runCmd('docker', ['volume', 'rm', '-f', volume], 15_000), `Failed to remove Docker volume ${volume}`);
+ output ||= result.stdout;
+ }
+ // Success only once the selected resources are provably gone.
+ const remaining = await runCmd('docker', ['compose', 'ps', '-a', '-q', 'db'], 15_000);
+ requireCommandSuccess(remaining, 'Could not verify the Docker database container was removed');
+ if (remaining.stdout.trim()) {
+ throw new ServerError('Docker database container still exists after removal. Data was not fully destroyed.', { status: 500, code: 'DATABASE_COMMAND_FAILED' });
+ }
+ for (const volume of DOCKER_VOLUMES) {
+ if (!(await dockerVolumeAbsent(volume))) {
+ throw new ServerError(`Docker volume ${volume} still exists after removal. Data was not fully destroyed.`, { status: 500, code: 'DATABASE_COMMAND_FAILED' });
+ }
+ }
+ return { success: true, output };
}
// Native: drop the portos database (system pg stays running)
@@ -707,7 +753,7 @@ async function destroyDatabaseImpl(backend) {
'-h', 'localhost', '-p', NATIVE_PORT, '-U', sysUser, '-d', 'postgres',
'-c', `DROP DATABASE IF EXISTS ${pgQuoteIdentifier(pgDb)}`
], 15_000);
- return { success: result.exitCode === 0, output: result.stdout };
+ return { success: true, output: requireCommandSuccess(result, 'Failed to delete native database').stdout };
}
/** Install and configure native PostgreSQL. */