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
fix: use consistent database settings for setup and runtime (#11254)
Adam Eivy98a9923ac286
diff --git a/ecosystem.config.cjs b/ecosystem.config.cjsindex 8aaec69806..5ce71c29bd 100644--- a/ecosystem.config.cjs+++ b/ecosystem.config.cjs@@ -25,25 +25,14 @@ const envFile = path.join(__dirname, '.env');const { parseEnvFile, resolveHttpMirrorPort } = require('./scripts/lib/envFile.cjs');const dotenv = parseEnvFile(envFile); // {} when .env is missingconst envValue = (key) => dotenv[key] || null;-// Same precedence as scripts/setup-db.js getMode(): a NONEMPTY exported PGMODE →-// saved .env → docker, so setup and PM2 always provision/dial the same backend-// (#10758). An empty export is unset, never a third mode.-const pgMode = process.env.PGMODE || envValue('PGMODE') || 'docker';+const { resolveDatabaseSettings } = require('./scripts/lib/databaseSettings.cjs');+const databaseSettings = resolveDatabaseSettings({ saved: dotenv });+const pgMode = databaseSettings.mode;const envServerMaxMemory = envValue('PORTOS_SERVER_MAX_MEMORY');-const envPgPassword = envValue('PGPASSWORD');-const envPgUser = envValue('PGUSER');-const envPgDatabase = envValue('PGDATABASE');-const envPgHost = envValue('PGHOST');-const envPgPort = envValue('PGPORT');-const envPgPortDocker = envValue('PGPORT_DOCKER');--// Same precedence as scripts/setup-db.js: process.env → .env → default. Keep the-// 'portos' password fallback (AGENTS.md: intentional backward-compatible-// default) — this only changes WHERE a user-set value can come from.-const PG_USER = process.env.PGUSER || envPgUser || 'portos';-const PG_DATABASE = process.env.PGDATABASE || envPgDatabase || 'portos';-const PG_PASSWORD = process.env.PGPASSWORD || envPgPassword || 'portos';-const PG_HOST = process.env.PGHOST || envPgHost || 'localhost';+const PG_USER = databaseSettings.user;+const PG_DATABASE = databaseSettings.database;+const PG_PASSWORD = databaseSettings.password;+const PG_HOST = databaseSettings.host;// Express bind for portos-server only: nonempty exported HOST → .env → all interfaces (#10946).const SERVER_HOST = process.env.HOST || envValue('HOST') || '0.0.0.0';@@ -171,8 +160,8 @@ const databaseEndpoint = (mode, port) => ({mode, host: PG_HOST, port: Number(port), database: PG_DATABASE, user: PG_USER});const DATABASE_ENDPOINTS = {- native: databaseEndpoint('native', process.env.PORTOS_NATIVE_PGPORT || process.env.PGPORT || envPgPort || 5432),- docker: databaseEndpoint('docker', process.env.PGPORT_DOCKER || envPgPortDocker || PORTS.POSTGRES_DOCKER)+ native: databaseEndpoint('native', databaseSettings.nativePort),+ docker: databaseEndpoint('docker', databaseSettings.dockerPort)};const PG_PORT = DATABASE_ENDPOINTS[pgMode === 'native' ? 'native' : 'docker'].port;diff --git a/scripts/db-lifecycle-dispatch.test.js b/scripts/db-lifecycle-dispatch.test.jsindex 355ef5319c..b1ec251098 100644--- a/scripts/db-lifecycle-dispatch.test.js+++ b/scripts/db-lifecycle-dispatch.test.js@@ -34,6 +34,7 @@ describe.skipIf(process.platform === 'win32')('scripts/db.sh start/stop backendroot = mkdtempSync(join(tmpdir(), 'portos-db-lifecycle-'));mkdirSync(join(root, 'scripts', 'lib'), { recursive: true });copyFileSync(join(here, 'lib', 'envFile.cjs'), join(root, 'scripts', 'lib', 'envFile.cjs'));+ copyFileSync(join(here, 'lib', 'databaseSettings.cjs'), join(root, 'scripts', 'lib', 'databaseSettings.cjs'));copyFileSync(join(here, 'db.sh'), join(root, 'scripts', 'db.sh'));writeFileSync(join(root, 'package.json'), '{"type":"module"}');const binDir = join(root, 'bin');diff --git a/scripts/db-migrate.test.js b/scripts/db-migrate.test.jsindex 701364caae..0f41e62e64 100644--- a/scripts/db-migrate.test.js+++ b/scripts/db-migrate.test.js@@ -91,6 +91,7 @@ describe.skipIf(process.platform === 'win32')('scripts/db.sh export + migrate',root = mkdtempSync(join(tmpdir(), 'portos-db-sh-'));mkdirSync(join(root, 'scripts', 'lib'), { recursive: true });copyFileSync(join(here, 'lib', 'envFile.cjs'), join(root, 'scripts', 'lib', 'envFile.cjs'));+ copyFileSync(join(here, 'lib', 'databaseSettings.cjs'), join(root, 'scripts', 'lib', 'databaseSettings.cjs'));copyFileSync(join(here, 'db.sh'), join(root, 'scripts', 'db.sh'));copyFileSync(join(here, 'prepare-database-replay.mjs'), join(root, 'scripts', 'prepare-database-replay.mjs'));mkdirSync(join(root, 'server', 'services'), { recursive: true });diff --git a/scripts/db-repair.test.js b/scripts/db-repair.test.jsindex e321bdebf4..716d6e5280 100644--- a/scripts/db-repair.test.js+++ b/scripts/db-repair.test.js@@ -27,6 +27,7 @@ describe.skipIf(process.platform === 'win32')('database Docker repair safety', (mkdirSync(join(root, 'volume'));copyFileSync(join(here, 'db.sh'), join(root, 'scripts', 'db.sh'));copyFileSync(join(here, 'lib', 'envFile.cjs'), join(root, 'scripts', 'lib', 'envFile.cjs'));+ copyFileSync(join(here, 'lib', 'databaseSettings.cjs'), join(root, 'scripts', 'lib', 'databaseSettings.cjs'));copyFileSync(join(here, '..', 'docker-compose.yml'), join(root, 'docker-compose.yml'));symlinkSync(process.execPath, join(root, 'bin', 'node'));executable(join(root, 'bin', 'uname'), '#!/bin/sh\necho Linux\n');diff --git a/scripts/db.sh b/scripts/db.shindex 0bc4268081..ad1049d46e 100755--- a/scripts/db.sh+++ b/scripts/db.sh@@ -36,20 +36,19 @@ read_db_config() {const { join } = require('node:path');const { parseEnvFile } = require(join(process.argv[2], 'scripts/lib/envFile.cjs'));const saved = parseEnvFile(join(process.argv[2], '.env'));-const value = (key, fallback = '') => process.env[key] || saved[key] || fallback;-const mode = value('PGMODE', 'docker');+const { resolveDatabaseSettings, parsePort } = require(join(process.argv[2], 'scripts/lib/databaseSettings.cjs'));+const resolved = resolveDatabaseSettings({ saved });const nativeSetup = process.argv[3] === 'setup-native';-const selectedHost = value('PGHOST');-const dockerPort = value('PGPORT_DOCKER', '5561');-const selectedPort = nativeSetup- ? process.env.PORTOS_NATIVE_PGPORT || value('PGPORT')- : process.env.PGPORT || (mode === 'native' ? saved.PGPORT || '' : dockerPort);+// Ordinary CLI commands retain an explicit exported endpoint override, even in+// Docker mode (offline maintenance uses this to address a selected cluster).+const selectedPort = nativeSetup ? resolved.selectedNativePort : parsePort(process.env.PGPORT);const settings = {- PGMODE: mode, PGUSER: value('PGUSER', 'portos'),- PGDATABASE: value('PGDATABASE', 'portos'), PGPASSWORD: value('PGPASSWORD', 'portos'),- PGHOST: selectedHost || 'localhost', SELECTED_PGHOST: selectedHost,- PGPORT: selectedPort || (mode === 'native' || nativeSetup ? '5432' : dockerPort),- SELECTED_PGPORT: selectedPort, PGPORT_DOCKER: dockerPort,+ PGMODE: resolved.mode, PGUSER: resolved.user,+ PGDATABASE: resolved.database, PGPASSWORD: resolved.password,+ PGHOST: resolved.host, SELECTED_PGHOST: resolved.selectedHost,+ PGPORT: String(selectedPort ?? (nativeSetup ? resolved.nativePort : resolved.port)),+ SELECTED_PGPORT: selectedPort === undefined ? '' : String(selectedPort),+ PGPORT_DOCKER: String(resolved.dockerPort),};process.stdout.write(Object.entries({ ...settings, complete: '1' }).flat().join('\0') + '\0');diff --git a/scripts/ecosystemEnv.test.js b/scripts/ecosystemEnv.test.jsindex e624662c74..f322f3975a 100644--- a/scripts/ecosystemEnv.test.js+++ b/scripts/ecosystemEnv.test.js@@ -16,6 +16,9 @@*/import { describe, it, expect, afterEach } from 'vitest';import { createRequire } from 'module';+import { spawnSync } from 'node:child_process';+import { runInNewContext } from 'node:vm';+import databaseSettings from './lib/databaseSettings.cjs';import { mkdirSync, mkdtempSync, rmSync, readFileSync, writeFileSync } from 'fs';import { tmpdir } from 'os';import { dirname, join } from 'path';@@ -53,6 +56,7 @@ function loadConfigIn(envContent, overrideEnv = {}) {writeFileSync(configPath, CONFIG_SOURCE);mkdirSync(join(dir, 'scripts', 'lib'), { recursive: true });writeFileSync(join(dir, 'scripts', 'lib', 'envFile.cjs'), PARSER_SOURCE);+ writeFileSync(join(dir, 'scripts', 'lib', 'databaseSettings.cjs'), readFileSync(join(REPO_ROOT, 'scripts/lib/databaseSettings.cjs')));if (envContent !== null) writeFileSync(join(dir, '.env'), envContent);const savedEnv = {};@@ -301,3 +305,73 @@ describe('ecosystem.config.cjs server bind HOST (#10946)', () => {expect(envs['portos-browser'].HOST).not.toBe('192.0.2.11');});});++// Compare real configuration bodies without running setup or db.sh operations.+describe('database settings agree across setup, PM2 and CLI (#11254)', () => {+ const setupSource = readFileSync(join(REPO_ROOT, 'scripts/setup-db.js'), 'utf8');+ const setupConfig = setupSource.slice(setupSource.indexOf('const settings ='), setupSource.indexOf('function getMode()'));+ const cliConfig = readFileSync(join(REPO_ROOT, 'scripts/db.sh'), 'utf8').split("<<'NODE'\n")[1].split('\nNODE')[0];+ const saved = 'PGMODE=docker\nPGPORT=5433\nPGPORT_DOCKER=5570\nPGHOST="db.example.com"\nPGUSER="example user"\nPGDATABASE=example-db\nPGPASSWORD=\'example $literal # secret\'\n';+ const readCli = (dir, env, action = 'status') => {+ const child = spawnSync(process.execPath, ['--input-type=commonjs', '-', dir, action], {+ input: cliConfig, encoding: 'utf8', env,+ });+ expect(child.status, child.stderr).toBe(0);+ const fields = child.stdout.split('\0');+ const config = Object.fromEntries(Array.from({ length: (fields.length - 1) / 2 }, (_, i) => fields.slice(i * 2, i * 2 + 2)));+ expect(config.complete).toBe('1');+ return config;+ };++ it.each(['docker', 'native'].flatMap(mode =>+ ['', 'bad', '5571suffix', '5571.5', '0', '65536', '-1', '0x15c2'].map(raw => [mode, raw])))(+ '%s falls through invalid exported port %j and empty credentials in every consumer', (mode, raw) => {+ const exported = Object.fromEntries(PG_KEYS.map(key => [key, '']));+ exported.PGPORT = raw;+ exported.PGPORT_DOCKER = raw;+ const { config, dir } = loadConfigIn(saved.replace('PGMODE=docker', `PGMODE=${mode}`), exported);+ const setup = runInNewContext(setupConfig + '\n({ PG_CHILD_ENV, COMPOSE_ENV })', {+ process: { env: exported }, envFile: parseEnvFile(join(dir, '.env')), databaseSettings,+ });+ const cli = readCli(dir, exported);+ for (const app of config.apps.filter(app => ['portos-server', 'portos-cos'].includes(app.name))) {+ expect(app.env).toMatchObject({ PGPORT: mode === 'native' ? 5433 : 5570, PORTOS_NATIVE_PGPORT: 5433 });+ for (const key of ['PGHOST', 'PGUSER', 'PGDATABASE', 'PGPASSWORD']) {+ expect(cli[key]).toBe(app.env[key]);+ expect(setup.PG_CHILD_ENV[key]).toBe(app.env[key]);+ }+ expect(Number(cli.PGPORT)).toBe(app.env.PGPORT);+ expect(Number(setup.COMPOSE_ENV.PGPORT_DOCKER)).toBe(config.DATABASE_ENDPOINTS.docker.port);+ expect(Number(setup.PG_CHILD_ENV.PGPORT)).toBe(app.env.PORTOS_NATIVE_PGPORT);+ }+ },+ );++ it.each([null, 'PGPORT=bad\nPGPORT_DOCKER=65536\n'])(+ 'uses identical defaults for missing or invalid saved settings %j', content => {+ const { config, dir } = loadConfigIn(content, clearedPgEnv);+ const setup = runInNewContext(setupConfig + '\n({ PG_CHILD_ENV, COMPOSE_ENV })', {+ process: { env: {} }, envFile: parseEnvFile(join(dir, '.env')), databaseSettings,+ });+ const cli = readCli(dir, {});+ expect(config.DATABASE_ENDPOINTS.native.port).toBe(5432);+ expect(config.DATABASE_ENDPOINTS.docker.port).toBe(5561);+ expect(cli).toMatchObject({ PGPORT: '5561', PGPASSWORD: 'portos' });+ expect(setup.PG_CHILD_ENV.PGPORT).toBe('5432');+ expect(setup.COMPOSE_ENV.PGPORT_DOCKER).toBe('5561');+ },+ );++ it('retains inherited native identity and explicit CLI endpoint selection', () => {+ const inherited = { PGPORT: '5572', PORTOS_NATIVE_PGPORT: '5434' };+ const { config, dir } = loadConfigIn(saved, { ...clearedPgEnv, ...inherited });+ const setup = runInNewContext(setupConfig + '\nPG_CHILD_ENV', {+ process: { env: inherited }, envFile: parseEnvFile(join(dir, '.env')), databaseSettings,+ });+ expect(config.DATABASE_ENDPOINTS.native.port).toBe(5434);+ expect(config.DATABASE_ENDPOINTS.docker.port).toBe(5570);+ expect(setup.PGPORT).toBe('5434');+ expect(readCli(dir, inherited).PGPORT).toBe('5572');+ expect(readCli(dir, inherited, 'setup-native').SELECTED_PGPORT).toBe('5434');+ });+});diff --git a/scripts/lib/databaseSettings.cjs b/scripts/lib/databaseSettings.cjsnew file mode 100644index 0000000000..33d72ded53--- /dev/null+++ b/scripts/lib/databaseSettings.cjs@@ -0,0 +1,29 @@+/** Dependency-free database configuration shared by pre-install setup, PM2 and db.sh. */+function parsePort(value, fallback) {+ const text = String(value ?? '').trim();+ if (!/^[0-9]+$/.test(text)) return fallback;+ const port = Number(text);+ return Number.isInteger(port) && port >= 1 && port <= 65535 ? port : fallback;+}++function firstPort(...values) {+ return values.map(value => parsePort(value)).find(port => port !== undefined);+}++function resolveDatabaseSettings({ env = process.env, saved = {} } = {}) {+ const value = (key, fallback) => env[key] || saved[key] || fallback;+ // PGPORT in a managed process names its active pool. The inherited native+ // identity must win when that process reloads configuration or provisions native.+ const selectedNativePort = firstPort(env.PORTOS_NATIVE_PGPORT, env.PGPORT, saved.PGPORT);+ const nativePort = selectedNativePort ?? 5432;+ const dockerPort = firstPort(env.PGPORT_DOCKER, saved.PGPORT_DOCKER) ?? 5561;+ const mode = value('PGMODE', 'docker');+ return {+ mode, user: value('PGUSER', 'portos'), database: value('PGDATABASE', 'portos'),+ password: value('PGPASSWORD', 'portos'), host: value('PGHOST', 'localhost'),+ selectedHost: value('PGHOST', ''), selectedNativePort,+ nativePort, dockerPort, port: mode === 'native' ? nativePort : dockerPort,+ };+}++module.exports = { parsePort, resolveDatabaseSettings };diff --git a/scripts/lib/setupDbChoice.js b/scripts/lib/setupDbChoice.jsindex 1a8bcaadff..bb17379f2c 100644--- a/scripts/lib/setupDbChoice.js+++ b/scripts/lib/setupDbChoice.js@@ -1,19 +1,6 @@-/**- * Pure setup-db helpers. setup-db.js is a CLI entrypoint that runs on import,- * so the port resolvers live here and are imported by both the script and- * its tests.- */+/** Compatibility API for setup callers; configuration is owned by the CJS resolver. */+import databaseSettings from './databaseSettings.cjs';-/** Tolerate whitespace / junk in a .env port; fall back rather than leaking NaN. */-export function parsePort(value, fallback) {- const parsed = Number.parseInt(String(value).trim(), 10);- return Number.isFinite(parsed) ? parsed : fallback;-}--export function parseNativePort(value) {- return parsePort(value, 5432);-}--export function parseDockerPort(value) {- return parsePort(value, 5561);-}+export const parsePort = databaseSettings.parsePort;+export const parseNativePort = value => parsePort(value, 5432);+export const parseDockerPort = value => parsePort(value, 5561);diff --git a/scripts/setup-db.js b/scripts/setup-db.jsindex 222ddd1f77..4ca00c383e 100644--- a/scripts/setup-db.js+++ b/scripts/setup-db.js@@ -14,31 +14,20 @@ import { createRequire } from 'module';import { dirname, join } from 'path';import { fileURLToPath } from 'url';import { parseEnvFile } from './lib/envFile.js';-import { parseDockerPort, parseNativePort } from './lib/setupDbChoice.js';+import databaseSettings from './lib/databaseSettings.cjs';import { resolveBashBinary } from '../server/lib/bashResolver.js';const __dirname = dirname(fileURLToPath(import.meta.url));const rootDir = join(__dirname, '..');const envFile = parseEnvFile(join(rootDir, '.env'));-// Resolve PG config from process.env first, then .env, then defaults — so a-// user who sets PGPASSWORD in .env (without exporting it into the shell) is-// respected the same way getMode() respects PGMODE in .env.-const envVar = (key, fallback) => process.env[key] ?? envFile[key] ?? fallback;-const PG_USER = envVar('PGUSER', 'portos');-const PG_DATABASE = envVar('PGDATABASE', 'portos');-const PG_PASSWORD = envVar('PGPASSWORD', 'portos');-// Same env > .env > default rule as PM2 (ecosystem.config.cjs), so a PGHOST set-// only in .env reaches both db.sh and the readiness probes.-const PG_HOST = envVar('PGHOST', 'localhost');-// A managed parent exports PGPORT for its active pool (possibly Docker), and-// preserves the native identity separately. Ordinary shells keep legacy PGPORT.-const PG_PORT_NATIVE = parseNativePort(process.env.PORTOS_NATIVE_PGPORT || envVar('PGPORT', 5432));-// Docker host-port mapping (docker-compose.yml maps `${PGPORT_DOCKER:-5561}:5432`).-// Resolve it the same tolerant way so the "ready on port N" log can't lie when a-// user overrides PGPORT_DOCKER — a misleading success port is exactly the kind of-// "looks fine but points at the wrong place" footgun this script exists to avoid.-const PG_PORT_DOCKER = parseDockerPort(envVar('PGPORT_DOCKER', 5561));+const settings = databaseSettings.resolveDatabaseSettings({ env: process.env, saved: envFile });+const PG_USER = settings.user;+const PG_DATABASE = settings.database;+const PG_PASSWORD = settings.password;+const PG_HOST = settings.host;+const PG_PORT_NATIVE = settings.nativePort;+const PG_PORT_DOCKER = settings.dockerPort;// Environment to pass to `db.sh` and other psql-wrapping subprocesses, so// values configured in .env (but not exported into the shell) reach the@@ -80,8 +69,7 @@ const COMPOSE_ENV = {};function getMode() {- // Nonempty exported PGMODE → .env → docker; must match ecosystem.config.cjs (#10758).- return process.env.PGMODE || envFile.PGMODE || 'docker';+ return settings.mode;}// Check if Docker is availablediff --git a/scripts/setup-db.test.js b/scripts/setup-db.test.jsindex 60c950f43f..d4e5f0c4bf 100644--- a/scripts/setup-db.test.js+++ b/scripts/setup-db.test.js@@ -1,4 +1,5 @@import { describe, it, expect } from 'vitest';+import databaseSettings from './lib/databaseSettings.cjs';import { spawnSync } from 'node:child_process';import { mkdtempSync, rmSync, writeFileSync, readFileSync } from 'fs';import { tmpdir } from 'node:os';@@ -51,13 +52,13 @@ describe('native setup inherited endpoint', () => {it('builds native subprocess settings from native identity, not the active Docker port', () => {// Execute the real setup configuration boundary without starting its menu,// probing PostgreSQL, or loading the install's .env.- const start = setupDbSrc.indexOf('const envVar =');+ const start = setupDbSrc.indexOf('const settings =');const end = setupDbSrc.indexOf('function getMode()');expect(start).toBeGreaterThan(0);expect(end).toBeGreaterThan(start);const configure = (env) => runInNewContext(setupDbSrc.slice(start, end) + '\nPG_CHILD_ENV',- { process: { env }, envFile: {}, parseNativePort, parseDockerPort },+ { process: { env }, envFile: {}, databaseSettings },);expect(configure({ PGPORT: '5570', PORTOS_NATIVE_PGPORT: '5433' }).PGPORT).toBe('5433');expect(configure({ PGPORT: '5434' }).PGPORT).toBe('5434');@@ -86,7 +87,7 @@ async function runSetup({ mode = 'docker', unavailable, tty = false, nativeReadytry {await runInNewContext(`(async () => {${source}\n})()`, {scriptUrl: new URL('./setup-db.js', import.meta.url).href, dirname, join, fileURLToPath,- parseNativePort, parseDockerPort,+ databaseSettings,createRequire: () => (name) => {if (name !== 'pg' || pgMissing) throw new Error("Cannot find module 'pg'");return { Client: class {