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: keep the HTTPS loopback mirror on the configured port (#10950)

Adam Eivy2b4c28be7026
diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs
index 585308876c..16e55cd4a3 100644
--- a/ecosystem.config.cjs
+++ b/ecosystem.config.cjs
@@ -22,7 +22,7 @@ const BASE_ENV = {
const envFile = path.join(__dirname, '.env');
// Parsed by the same dependency-free grammar setup uses (scripts/lib/envFile.cjs),
// so quoted values resolve identically here and in setup (#9471).
-const { parseEnvFile } = require('./scripts/lib/envFile.cjs');
+const { parseEnvFile, resolveHttpMirrorPort } = require('./scripts/lib/envFile.cjs');
const dotenv = parseEnvFile(envFile); // {} when .env is missing
const envValue = (key) => dotenv[key] || null;
// Same precedence as scripts/setup-db.js getMode(): a NONEMPTY exported PGMODE →
@@ -207,7 +207,7 @@ module.exports = {
// real headroom at negligible cost — idle threads are just parked.
UV_THREADPOOL_SIZE: '16',
PORT: PORTS.API,
- PORTOS_HTTP_PORT: PORTS.API_LOCAL, // Loopback HTTP mirror when HTTPS is active
+ PORTOS_HTTP_PORT: resolveHttpMirrorPort({ dotenv }), // exported > .env > PORTS.API_LOCAL (#10950); Loopback HTTP mirror when HTTPS is active
HOST: '0.0.0.0',
PGHOST: PG_HOST,
PGPORT: PG_PORT,
diff --git a/scripts/ecosystemEnv.test.js b/scripts/ecosystemEnv.test.js
index 9b86a26105..6daa4d156c 100644
--- a/scripts/ecosystemEnv.test.js
+++ b/scripts/ecosystemEnv.test.js
@@ -20,7 +20,7 @@ import { mkdirSync, mkdtempSync, rmSync, readFileSync, writeFileSync } from 'fs'
import { tmpdir } from 'os';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
-import { parseEnvFile } from './lib/envFile.js';
+import { parseEnvFile, resolveHttpMirrorPortForRoot } from './lib/envFile.js';
const require = createRequire(import.meta.url);
const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
@@ -235,3 +235,37 @@ describe('ecosystem.config.cjs parses .env with the setup grammar (#9471)', () =
});
});
});
+
+describe('ecosystem.config.cjs loopback HTTP mirror port (#10950)', () => {
+ const mirrorOf = (envContent, env) => {
+ const { config, dir } = loadConfigIn(envContent, { PORTOS_HTTP_PORT: undefined, ...env });
+ return { port: config.apps.find((a) => a.name === 'portos-server').env.PORTOS_HTTP_PORT, dir };
+ };
+
+ it('defaults to the canonical mirror port', () => {
+ expect(mirrorOf(null, {}).port).toBe(5553);
+ });
+
+ it('honors a port saved only in .env, and advertises the same one to setup helpers', () => {
+ const { port, dir } = mirrorOf('PORTOS_HTTP_PORT=5599\n', {});
+ expect(port).toBe(5599);
+ expect(resolveHttpMirrorPortForRoot(dir)).toBe(port);
+ });
+
+ it('lets a nonempty exported value win over .env, matching setup helpers', () => {
+ const saved = { PORTOS_HTTP_PORT: '5588' };
+ const { config, dir } = loadConfigIn('PORTOS_HTTP_PORT=5599\n', saved);
+ const port = config.apps.find((a) => a.name === 'portos-server').env.PORTOS_HTTP_PORT;
+ expect(port).toBe(5588);
+ const prev = process.env.PORTOS_HTTP_PORT;
+ process.env.PORTOS_HTTP_PORT = '5588';
+ try { expect(resolveHttpMirrorPortForRoot(dir)).toBe(5588); }
+ finally { if (prev === undefined) delete process.env.PORTOS_HTTP_PORT; else process.env.PORTOS_HTTP_PORT = prev; }
+ });
+
+ it.each(['abc', '0', '70000', '55.5', ''])('skips invalid value %j and falls through to the next source', (bad) => {
+ expect(mirrorOf(null, { PORTOS_HTTP_PORT: bad }).port).toBe(5553);
+ expect(mirrorOf('PORTOS_HTTP_PORT=5599\n', { PORTOS_HTTP_PORT: bad }).port).toBe(5599);
+ expect(mirrorOf(`PORTOS_HTTP_PORT=${bad}\n`, {}).port).toBe(5553);
+ });
+});
diff --git a/scripts/lib/envFile.cjs b/scripts/lib/envFile.cjs
index 87bb17973e..029c3876b6 100644
--- a/scripts/lib/envFile.cjs
+++ b/scripts/lib/envFile.cjs
@@ -50,4 +50,26 @@ function parseEnvFile(filePath) {
return parseEnvContent(content);
}
-module.exports = { parseEnvContent, parseEnvFile };
+const DEFAULT_HTTP_MIRROR_PORT = 5553;
+
+/**
+ * Effective loopback HTTP-mirror port (`PORTOS_HTTP_PORT`). Precedence matches
+ * the other machine-local settings: NONEMPTY exported value, then saved `.env`,
+ * then the canonical default. A value that is not an integer TCP port (1-65535)
+ * is skipped (falls through to the next source), so every consumer — PM2 config,
+ * setup/access/browser helpers — resolves the same port (#10950).
+ *
+ * @param {{ env?: Record<string, string|undefined>, dotenv?: Record<string, string> }} [sources]
+ * @returns {number}
+ */
+function resolveHttpMirrorPort({ env = process.env, dotenv = {} } = {}) {
+ for (const raw of [env.PORTOS_HTTP_PORT, dotenv.PORTOS_HTTP_PORT]) {
+ const text = String(raw ?? '').trim();
+ if (!/^\d+$/.test(text)) continue;
+ const port = Number(text);
+ if (port >= 1 && port <= 65535) return port;
+ }
+ return DEFAULT_HTTP_MIRROR_PORT;
+}
+
+module.exports = { parseEnvContent, parseEnvFile, resolveHttpMirrorPort, DEFAULT_HTTP_MIRROR_PORT };
diff --git a/scripts/lib/envFile.js b/scripts/lib/envFile.js
index 53905766ae..e5d4109c18 100644
--- a/scripts/lib/envFile.js
+++ b/scripts/lib/envFile.js
@@ -6,11 +6,17 @@
*/
import { readFileSync, writeFileSync } from 'fs';
+import { join } from 'path';
import envFileParser from './envFile.cjs';
// The grammar lives in envFile.cjs so ecosystem.config.cjs (PM2) parses `.env`
// exactly as setup does; see that file for the supported syntax.
-export const { parseEnvContent, parseEnvFile } = envFileParser;
+export const { parseEnvContent, parseEnvFile, resolveHttpMirrorPort } = envFileParser;
+
+/** Mirror port for a repo root, from exported env then that root's saved `.env`. */
+export function resolveHttpMirrorPortForRoot(root) {
+ return resolveHttpMirrorPort({ dotenv: parseEnvFile(join(root, '.env')) });
+}
/**
* Set (or add) a single key in a .env file without touching other lines.
diff --git a/scripts/lib/envFile.test.js b/scripts/lib/envFile.test.js
index c918d02d76..6637ea0158 100644
--- a/scripts/lib/envFile.test.js
+++ b/scripts/lib/envFile.test.js
@@ -68,7 +68,7 @@ describe('pre-install module boundary', () => {
it('loads the CommonJS parser without any installed dependency', async () => {
const { createRequire } = await import('module');
const cjs = createRequire(import.meta.url)('./envFile.cjs');
- expect(Object.keys(cjs).sort()).toEqual(['parseEnvContent', 'parseEnvFile']);
+ expect(Object.keys(cjs).sort()).toEqual(['DEFAULT_HTTP_MIRROR_PORT', 'parseEnvContent', 'parseEnvFile', 'resolveHttpMirrorPort']);
expect(cjs.parseEnvContent('A = "x y"\r\nB=\'z\'\n')).toEqual({ A: 'x y', B: 'z' });
});
});
diff --git a/scripts/open-ui-in-browser.js b/scripts/open-ui-in-browser.js
index 83061dce0d..bc6b00beab 100644
--- a/scripts/open-ui-in-browser.js
+++ b/scripts/open-ui-in-browser.js
@@ -22,6 +22,8 @@ import { hasTailscaleCert } from '../lib/tailscale-https.js';
import { certPaths } from '../lib/certPaths.js';
import { getCliSetupGuide } from './setup-guide.js';
import { isDirectlyInvoked } from './lib/directInvocation.js';
+import { resolveHttpMirrorPortForRoot } from './lib/envFile.js';
+
import { navigateToUrlPinned, cdpRequest } from '../server/services/browserService.js';
import { assertPublicHttpUrl } from '../server/lib/safeUrlFetch.js';
import { isBlockedIngestHost } from '../server/lib/catalogValidation.js';
@@ -29,7 +31,7 @@ import { isBlockedIngestHost } from '../server/lib/catalogValidation.js';
const ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
const { dir: CERT_DIR } = certPaths(join(ROOT, 'data'));
const API_PORT = Number(process.env.PORT) || 5555;
-const HTTP_LOOPBACK_PORT = Number(process.env.PORTOS_HTTP_PORT) || 5553;
+const HTTP_LOOPBACK_PORT = resolveHttpMirrorPortForRoot(ROOT);
const DEV_UI_PORT = Number(process.env.PORTOS_UI_PORT) || 5554;
const CDP_TIMEOUT_MS = 3_000;
diff --git a/scripts/print-access-url.js b/scripts/print-access-url.js
index 252e257557..bae63bc502 100644
--- a/scripts/print-access-url.js
+++ b/scripts/print-access-url.js
@@ -11,12 +11,14 @@ import { hasTailscaleCert } from '../lib/tailscale-https.js';
import { certPaths } from '../lib/certPaths.js';
import { getTailscaleHttpsUrl, readCertMeta } from '../lib/certMeta.js';
import { join, dirname } from 'path';
+import { resolveHttpMirrorPortForRoot } from './lib/envFile.js';
+
import { fileURLToPath } from 'url';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
const { dir: CERT_DIR, meta: META_PATH } = certPaths(join(ROOT, 'data'));
const API_PORT = Number(process.env.PORT) || 5555;
-const MIRROR_PORT = Number(process.env.PORTOS_HTTP_PORT) || 5553;
+const MIRROR_PORT = resolveHttpMirrorPortForRoot(ROOT);
if (!hasTailscaleCert(CERT_DIR)) {
console.log(`Access at: http://localhost:${API_PORT}`);
diff --git a/scripts/setup-guide.js b/scripts/setup-guide.js
index c630fe49e4..7453406027 100644
--- a/scripts/setup-guide.js
+++ b/scripts/setup-guide.js
@@ -18,12 +18,14 @@ import { hasTailscaleCert } from '../lib/tailscale-https.js';
import { buildNetworkSetupGuide } from '../server/lib/networkExposure.js';
import { getTailscaleStatus } from '../server/lib/tailscale.js';
import { isDirectlyInvoked } from './lib/directInvocation.js';
+import { resolveHttpMirrorPortForRoot } from './lib/envFile.js';
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
const DATA_DIR = join(ROOT, 'data');
const { dir: CERT_DIR, meta: META_PATH } = certPaths(DATA_DIR);
const API_PORT = Number(process.env.PORT) || 5555;
-const MIRROR_PORT = Number(process.env.PORTOS_HTTP_PORT) || 5553;
+const MIRROR_PORT = resolveHttpMirrorPortForRoot(ROOT);
const MARK = {
complete: '✓',
diff --git a/scripts/verify-server-health.js b/scripts/verify-server-health.js
index f9c2b79c84..17846a4d63 100644
--- a/scripts/verify-server-health.js
+++ b/scripts/verify-server-health.js
@@ -32,6 +32,11 @@ import http from 'node:http';
import https from 'node:https';
import { PORTS } from '../server/lib/ports.js';
import { isDirectlyInvoked } from './lib/directInvocation.js';
+import { resolveHttpMirrorPortForRoot } from './lib/envFile.js';
+import { dirname, join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
const HEALTH_PATH = '/api/system/health';
const DEFAULT_TIMEOUT_MS = 120_000;
@@ -151,7 +156,7 @@ export async function waitForHealthy({
async function runCli() {
const apiPort = Number(process.env.PORT) || PORTS.API;
- const mirrorPort = Number(process.env.PORTOS_HTTP_PORT) || PORTS.API_LOCAL;
+ const mirrorPort = resolveHttpMirrorPortForRoot(ROOT);
const timeoutMs = parseTimeoutMs(process.env.PORTOS_HEALTH_WAIT_MS);
const urls = healthProbeUrls({ apiPort, mirrorPort });