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: share maintenance endpoint resolution with PM2

[._.]/ Adam Eivya0a660154693
diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs
index 9d6b91660d..2780785a06 100644
--- a/ecosystem.config.cjs
+++ b/ecosystem.config.cjs
@@ -164,11 +164,20 @@ const PORTS = {
// mapping PGPORT_DOCKER — see docker-compose.yml), so resolve whichever one
// applies to the active pgMode; PORTS.POSTGRES (above) is already the correct
// per-mode default when neither is set.
-const PG_PORT = Number(
- (pgMode === 'native' ? (process.env.PGPORT || envPgPort) : (process.env.PGPORT_DOCKER || envPgPortDocker)) || PORTS.POSTGRES
-);
+// Expose the exact, credential-free endpoint identities to offline maintenance
+// tools. Both modes must use the same precedence as managed-process startup.
+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.PGPORT || envPgPort || 5432),
+ docker: databaseEndpoint('docker', process.env.PGPORT_DOCKER || envPgPortDocker || PORTS.POSTGRES_DOCKER)
+};
+const PG_PORT = DATABASE_ENDPOINTS[pgMode === 'native' ? 'native' : 'docker'].port;
module.exports = {
+ DATABASE_MODE: pgMode,
+ DATABASE_ENDPOINTS,
PORTS, // Export for other configs to reference
apps: [
diff --git a/scripts/database-maintenance.mjs b/scripts/database-maintenance.mjs
index aa418cbedd..9e5fe04b9d 100644
--- a/scripts/database-maintenance.mjs
+++ b/scripts/database-maintenance.mjs
@@ -1,7 +1,6 @@
#!/usr/bin/env node
// Operator interface for the admission boundary. This does NOT transfer data,
// stop existing writers, change mode, or authorize invoking db.sh migrate.
-import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { join } from 'node:path';
import { PATHS } from '../server/lib/paths.js';
@@ -10,16 +9,12 @@ import { createDatabaseMaintenanceJournal } from '../server/lib/databaseMaintena
const journal = createDatabaseMaintenanceJournal();
const require = createRequire(import.meta.url);
-function configuredSource() {
- const text = (() => {
- try { return readFileSync(join(PATHS.installRoot, '.env'), 'utf8'); }
- catch (err) { if (err.code === 'ENOENT') return ''; throw err; }
- })();
- const mode = text.match(/^PGMODE=(\S+)/m)?.[1] || 'docker';
- const config = require(join(PATHS.installRoot, 'ecosystem.config.cjs'));
- const env = config.apps.find(app => app.name === 'portos-server')?.env;
- if (!env || !['native', 'docker'].includes(mode)) throw new Error('Database configuration is not a supported backend.');
- return { mode, host: env.PGHOST, port: Number(env.PGPORT), database: env.PGDATABASE, user: env.PGUSER };
+function configuredEndpoints() {
+ const { DATABASE_MODE, DATABASE_ENDPOINTS } = require(join(PATHS.installRoot, 'ecosystem.config.cjs'));
+ if (!['native', 'docker'].includes(DATABASE_MODE) || !DATABASE_ENDPOINTS) {
+ throw new Error('Database configuration is not a supported backend.');
+ }
+ return { source: DATABASE_ENDPOINTS[DATABASE_MODE], endpoints: DATABASE_ENDPOINTS };
}
function main() {
@@ -31,23 +26,15 @@ function main() {
}
if (command === 'begin' && args.length === 2) {
const [sourceMode, targetMode] = args;
- const source = configuredSource();
+ const { source, endpoints } = configuredEndpoints();
if (source.mode !== sourceMode || !['native', 'docker'].includes(targetMode) || targetMode === sourceMode) {
throw new Error('Explicit source/target must match saved mode and name different backends.');
}
- // Target ports are resolved from the saved mode-specific override, never
- // from the current process PGPORT (which belongs to the SOURCE).
- const text = (() => {
- try { return readFileSync(join(PATHS.installRoot, '.env'), 'utf8'); }
- catch (err) { if (err.code === 'ENOENT') return ''; throw err; }
- })();
- const key = targetMode === 'native' ? 'PGPORT' : 'PGPORT_DOCKER';
- const port = Number(text.match(new RegExp('^' + key + '=(\\S+)', 'm'))?.[1] || (targetMode === 'native' ? 5432 : 5561));
- const record = journal.begin({ source, target: { ...source, mode: targetMode, port } });
+ const record = journal.begin({ source, target: endpoints[targetMode] });
return { id: record.id, stage: record.stage, source: sourceMode, target: targetMode };
}
if (command === 'cancel' && args.length === 1) {
- return journal.cancel(args[0], configuredSource());
+ return journal.cancel(args[0], configuredEndpoints().source);
}
throw new Error('Usage: node scripts/database-maintenance.mjs status | begin <native|docker> <native|docker> | cancel <operation-id>');
}
diff --git a/server/lib/databaseMaintenanceJournal.test.js b/server/lib/databaseMaintenanceJournal.test.js
index f95dedf44a..ad18cdaa12 100644
--- a/server/lib/databaseMaintenanceJournal.test.js
+++ b/server/lib/databaseMaintenanceJournal.test.js
@@ -166,10 +166,11 @@ describe('persistent database maintenance boundary', () => {
const config = 'PGMODE=native\nPGPORT=6543\nPGPORT_DOCKER=6544\n';
writeFileSync(join(root, '.env'), config);
const cli = new URL('../../scripts/database-maintenance.mjs', import.meta.url);
+ let overrides = {};
const run = (...args) => spawnSync(process.execPath, [fileURLToPath(cli), ...args], {
encoding: 'utf8', timeout: 10_000,
env: { ...childEnv, PORTOS_DATA_ROOT: root, PGPORT: '', PGPORT_DOCKER: '',
- PGHOST: 'localhost', PGUSER: 'example', PGDATABASE: 'example_test', PGPASSWORD: 'example-only' },
+ PGHOST: 'localhost', PGUSER: 'example', PGDATABASE: 'example_test', PGPASSWORD: 'example-only', ...overrides },
});
expect(JSON.parse(run('status').stdout)).toEqual({ stage: 'idle' });
expect(run('begin', 'docker', 'native').status).toBe(1);
@@ -185,6 +186,20 @@ describe('persistent database maintenance boundary', () => {
writeFileSync(join(root, '.env'), config);
expect(JSON.parse(run('cancel', operation.id).stdout)).toEqual({ id: operation.id, stage: 'cancelled' });
expect(JSON.parse(run('status').stdout)).toEqual({ stage: 'idle' });
+ // Mode follows the ecosystem's saved PGMODE contract; each endpoint uses
+ // its own environment port override, rather than mixing env and file ports.
+ overrides = { PGPORT: '7001', PGPORT_DOCKER: '7002', PGMODE: 'docker', PGHOST: 'example.invalid' };
+ const overridden = run('begin', 'native', 'docker');
+ expect(overridden.status, overridden.stderr).toBe(0);
+ expect(journal.read()).toMatchObject({
+ source: { mode: 'native', host: 'example.invalid', port: 7001 },
+ target: { mode: 'docker', host: 'example.invalid', port: 7002 },
+ });
+ const overrideId = JSON.parse(overridden.stdout).id;
+ overrides = {};
+ expect(run('cancel', overrideId).status).toBe(1);
+ overrides = { PGPORT: '7001', PGPORT_DOCKER: '7002', PGHOST: 'example.invalid' };
+ expect(run('cancel', overrideId).status).toBe(0);
});
it.skipIf(process.platform === 'win32')('never releases a symlinked active directory or journal', () => {