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
refactor: address code review findings
Adam Eivy8d6ffc36d87b
diff --git a/scripts/db.sh b/scripts/db.shindex 45d03bec99..d5c79b45ca 100755--- a/scripts/db.sh+++ b/scripts/db.sh@@ -474,7 +474,8 @@ cmd_setup_native() {sys_user="$(whoami)"if ! psql -h "$PGHOST" -p "$PGPORT" -U "$sys_user" -d postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$PGUSER'" 2>/dev/null | grep -q 1; theninfo "Creating database user: $PGUSER"- psql -h "$PGHOST" -p "$PGPORT" -U "$sys_user" -d postgres -c "CREATE ROLE $PGUSER WITH LOGIN PASSWORD '$PGPASSWORD' CREATEDB;"+ psql -h "$PGHOST" -p "$PGPORT" -U "$sys_user" -d postgres \+ -v pw="$PGPASSWORD" -v user="$PGUSER" -c "CREATE ROLE :\"user\" WITH LOGIN PASSWORD :'pw' CREATEDB;"log "User $PGUSER created"elselog "User $PGUSER already exists"diff --git a/server/routes/database.js b/server/routes/database.jsindex 8fb5e95959..1066f3188e 100644--- a/server/routes/database.js+++ b/server/routes/database.js@@ -16,9 +16,9 @@ const stripAnsi = (s) => s.replace(/\x1b\[[0-9;]*m/g, '');/*** Run a command and return { stdout, stderr, exitCode }.*/-function runCmd(cmd, args, timeout = 120_000) {+function runCmd(cmd, args, timeout = 120_000, env = process.env) {return new Promise((resolve) => {- execFile(cmd, args, { cwd: rootDir, timeout, env: process.env }, (err, stdout, stderr) => {+ execFile(cmd, args, { cwd: rootDir, timeout, env }, (err, stdout, stderr) => {const exitCode = err ? (typeof err.code === 'number' ? err.code : 1) : 0;if (exitCode !== 0) {const detail = stripAnsi(stderr || stdout || err.message || '');@@ -131,9 +131,10 @@ async function getNativeStats() {*/async function getNativeDiskUsage() {const nativePort = process.env.PGPORT || '5432';- const result = await runCmd('bash', ['-c',- `PGPASSWORD=${pgPassword} psql -h localhost -p ${nativePort} -U ${pgUser} -d ${pgDb} -tAc "SELECT pg_size_pretty(pg_database_size(current_database()))"`- ], 5_000);+ const result = await runCmd('psql', [+ '-h', 'localhost', '-p', nativePort, '-U', pgUser, '-d', pgDb,+ '-tAc', 'SELECT pg_size_pretty(pg_database_size(current_database()))'+ ], 5_000, pgEnv(nativePort));if (result.exitCode !== 0) return null;return result.stdout.trim() || null;}@@ -256,6 +257,19 @@ const pgPassword = process.env.PGPASSWORD || 'portos';const NATIVE_PORT = '5432';const DOCKER_PORT = '5561';+/**+ * Build env object with PGPASSWORD for passing to child processes.+ * Avoids interpolating password into shell strings (shell metacharacter safety).+ */+const pgEnv = (port) => ({+ ...process.env,+ PGPASSWORD: pgPassword,+ PGHOST: 'localhost',+ PGPORT: String(port),+ PGUSER: pgUser,+ PGDATABASE: pgDb+});+// POST /api/database/sync — copy data from active to non-active backend// Since native (5432) and Docker (5561) use different ports, both can be// running simultaneously. No need to stop the active backend.@@ -266,7 +280,6 @@ router.post('/sync', asyncHandler(async (req, res) => {const statusResult = await runDbScript(['status']);const currentMode = parseDbMode(statusResult.stdout);const targetMode = currentMode === 'docker' ? 'native' : 'docker';- const sourcePort = currentMode === 'docker' ? DOCKER_PORT : NATIVE_PORT;const targetPort = targetMode === 'docker' ? DOCKER_PORT : NATIVE_PORT;// Step 1: Export from active database (stays running)@@ -282,9 +295,9 @@ router.post('/sync', asyncHandler(async (req, res) => {// Step 2: Ensure target is runningemit('start', { message: `Checking ${targetMode} is running...` });- const readyResult = await runCmd('bash', ['-c',- `PGPASSWORD=${pgPassword} pg_isready -h localhost -p ${targetPort} -U ${pgUser}`- ], 5_000);+ const readyResult = await runCmd('pg_isready', [+ '-h', 'localhost', '-p', targetPort, '-U', pgUser+ ], 5_000, pgEnv(targetPort));if (readyResult.exitCode !== 0) {emit('error', { message: `${targetMode} database not running on port ${targetPort}. Start it first.` });return res.status(400).json({ error: `${targetMode} database not running on port ${targetPort}. Start it first.` });@@ -293,8 +306,8 @@ router.post('/sync', asyncHandler(async (req, res) => {// Step 3: Import into target (active backend untouched — different port)emit('start', { message: `Importing into ${targetMode} (port ${targetPort})...` });const importResult = await runCmd('bash', ['-c',- `PGPASSWORD=${pgPassword} psql -h localhost -p ${targetPort} -U ${pgUser} -d ${pgDb} -v ON_ERROR_STOP=1 --single-transaction < "${dumpFile}"`- ], 120_000);+ `psql -h localhost -p ${targetPort} -U ${pgUser} -d ${pgDb} -v ON_ERROR_STOP=1 --single-transaction < "${dumpFile}"`+ ], 120_000, pgEnv(targetPort));if (importResult.exitCode !== 0) {emit('error', { message: `Import into ${targetMode} failed` });@@ -366,8 +379,9 @@ router.post('/destroy', asyncHandler(async (req, res) => {// Native: drop the portos database (system pg stays running)const sysUser = process.env.USER || 'portos';const nativePort = process.env.PGPORT || '5432';- const result = await runCmd('bash', ['-c',- `psql -h localhost -p ${nativePort} -U ${sysUser} -d postgres -c "DROP DATABASE IF EXISTS ${pgDb}"`+ const result = await runCmd('psql', [+ '-h', 'localhost', '-p', nativePort, '-U', sysUser, '-d', 'postgres',+ '-c', `DROP DATABASE IF EXISTS ${pgDb}`], 15_000);res.json({ success: result.exitCode === 0, output: result.stdout });}));@@ -400,9 +414,10 @@ router.post('/export', asyncHandler(async (req, res) => {if (backend && ['docker', 'native'].includes(backend)) {// Export from a specific backend by connecting directly to its portconst port = backend === 'docker' ? DOCKER_PORT : NATIVE_PORT;- const readyResult = await runCmd('bash', ['-c',- `PGPASSWORD=${pgPassword} pg_isready -h localhost -p ${port} -U ${pgUser}`- ], 5_000);+ const env = pgEnv(port);+ const readyResult = await runCmd('pg_isready', [+ '-h', 'localhost', '-p', port, '-U', pgUser+ ], 5_000, env);if (readyResult.exitCode !== 0) {return res.status(400).json({ error: `${backend} database not running on port ${port}` });}@@ -410,8 +425,8 @@ router.post('/export', asyncHandler(async (req, res) => {await runCmd('mkdir', ['-p', dumpDir], 5_000);const dumpFile = join(dumpDir, `portos-${backend}-${label}.sql`);const result = await runCmd('bash', ['-c',- `PGPASSWORD=${pgPassword} pg_dump -h localhost -p ${port} -U ${pgUser} -d ${pgDb} --no-owner --no-privileges --if-exists --clean > "${dumpFile}"`- ], 120_000);+ `pg_dump -h localhost -p ${port} -U ${pgUser} -d ${pgDb} --no-owner --no-privileges --if-exists --clean > "${dumpFile}"`+ ], 120_000, env);if (result.exitCode !== 0) {return res.status(500).json({ error: 'Export failed', details: result.stderr || result.stdout });}