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

refactor: DRY port configuration with flat PORTS object pattern

Adam Eivye5f04c9df4c8
diff --git a/docs/PORTS.md b/docs/PORTS.md
index 1d36aab4ec..eff8eda2d6 100644
--- a/docs/PORTS.md
+++ b/docs/PORTS.md
@@ -33,45 +33,61 @@ Common port labels:
## Defining Ports in ecosystem.config.cjs
-Use the `ports` object to define all ports used by a process:
+Define all ports in a top-level `PORTS` object as the single source of truth:
```javascript
-{
- name: 'my-app',
- script: 'server.js',
- // PortOS convention: define all ports used by this process
- ports: {
- api: 5570, // REST API
- health: 5571 // Health check endpoint
- },
- env: {
- PORT: 5570,
- HEALTH_PORT: 5571
- }
-}
+// =============================================================================
+// Port Configuration - All ports defined here as single source of truth
+// =============================================================================
+const PORTS = {
+ API: 5570, // REST API server
+ UI: 5571, // Web UI
+ CDP: 5572 // Chrome DevTools Protocol
+};
+
+module.exports = {
+ PORTS, // Export for other configs to reference
+
+ apps: [
+ {
+ name: 'my-api',
+ script: 'server.js',
+ env: {
+ PORT: PORTS.API
+ }
+ },
+ {
+ name: 'my-ui',
+ script: 'node_modules/.bin/vite',
+ args: `--port ${PORTS.UI}`,
+ env: {
+ VITE_PORT: PORTS.UI
+ }
+ }
+ ]
+};
```
-### Multiple Ports Example
+### Benefits
-For processes that expose multiple ports (like the browser with CDP and health):
+- **Single Source of Truth**: Each port defined once
+- **Importable**: Other configs can `require('./ecosystem.config.cjs').PORTS`
+- **Clear Comments**: Document what each port is for
+- **DRY**: No duplication between `ports` object and `env` vars
-```javascript
-{
- name: 'portos-browser',
- script: '.browser/server.js',
- ports: { cdp: 5556, health: 5557 },
- env: {
- CDP_PORT: 5556,
- PORT: 5557
- }
-}
-```
+### Port Detection
+
+PortOS automatically detects ports from env vars:
+- `PORT` → labeled as `api` (or `ui` for `-ui`/`-client` processes, `health` for `-browser` processes with CDP)
+- `CDP_PORT` → labeled as `cdp`
+- `VITE_PORT` → labeled as `ui`
+- `--port` in args → labeled as `ui`
## Guidelines for New Apps
1. **Check Available Ports**: Use PortOS apps list to see which ports are in use
2. **Pick a Contiguous Range**: Choose a starting port and allocate contiguously
-3. **Document in ports object**: Always define the `ports` object in ecosystem.config
+3. **Define PORTS Object**: Always define ports in a top-level `PORTS` constant
4. **Avoid Common Ports**: Stay away from well-known ports (80, 443, 3000, 8080, etc.)
## Recommended Port Ranges
diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs
index 1ac388f0a6..5117689036 100644
--- a/ecosystem.config.cjs
+++ b/ecosystem.config.cjs
@@ -1,15 +1,28 @@
+// =============================================================================
+// Port Configuration - All ports defined here as single source of truth
+// =============================================================================
+const PORTS = {
+ API: 5554, // Express API server
+ UI: 5555, // Vite dev server (client)
+ CDP: 5556, // Chrome DevTools Protocol (browser automation)
+ CDP_HEALTH: 5557, // Browser health check endpoint
+ COS: 5558, // Chief of Staff agent runner
+ AUTOFIXER: 5559, // Autofixer API
+ AUTOFIXER_UI: 5560 // Autofixer UI
+};
+
module.exports = {
+ PORTS, // Export for other configs to reference
+
apps: [
{
name: 'portos-server',
script: 'server/index.js',
cwd: __dirname,
interpreter: 'node',
- // PortOS convention: define all ports used by this process
- ports: { api: 5554 },
env: {
NODE_ENV: 'development',
- PORT: 5554,
+ PORT: PORTS.API,
HOST: '0.0.0.0'
},
watch: false,
@@ -23,10 +36,9 @@ module.exports = {
// CoS Agent Runner - isolated process for spawning Claude CLI agents
// Does NOT restart when portos-server restarts, preventing orphaned agents
// Security: Binds to localhost only - not exposed externally
- ports: { api: 5558 },
env: {
NODE_ENV: 'development',
- PORT: 5558,
+ PORT: PORTS.COS,
HOST: '127.0.0.1'
},
watch: false,
@@ -40,13 +52,13 @@ module.exports = {
kill_timeout: 30000
},
{
- name: 'portos-client',
+ name: 'portos-ui',
script: 'node_modules/.bin/vite',
cwd: `${__dirname}/client`,
- args: '--host 0.0.0.0 --port 5555',
- ports: { ui: 5555 },
+ args: `--host 0.0.0.0 --port ${PORTS.UI}`,
env: {
- NODE_ENV: 'development'
+ NODE_ENV: 'development',
+ VITE_PORT: PORTS.UI
},
watch: false
},
@@ -55,10 +67,9 @@ module.exports = {
script: 'autofixer/server.js',
cwd: __dirname,
interpreter: 'node',
- ports: { api: 5559 },
env: {
NODE_ENV: 'development',
- PORT: 5559
+ PORT: PORTS.AUTOFIXER
},
watch: false,
autorestart: true,
@@ -71,10 +82,9 @@ module.exports = {
script: 'autofixer/ui.js',
cwd: __dirname,
interpreter: 'node',
- ports: { ui: 5560 },
env: {
NODE_ENV: 'development',
- PORT: 5560
+ PORT: PORTS.AUTOFIXER_UI
},
watch: false,
autorestart: true,
@@ -89,12 +99,11 @@ module.exports = {
interpreter: 'node',
// Security: CDP binds to 127.0.0.1 by default (set CDP_HOST=0.0.0.0 to expose)
// Remote access should go through portos-server proxy with authentication
- ports: { cdp: 5556, health: 5557 },
env: {
NODE_ENV: 'development',
- CDP_PORT: 5556,
+ CDP_PORT: PORTS.CDP,
CDP_HOST: '127.0.0.1',
- PORT: 5557
+ PORT: PORTS.CDP_HEALTH
},
watch: false,
autorestart: true,
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index bad3f21420..d1f60e072e 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -20,6 +20,49 @@ export function parseEcosystemConfig(content) {
portConstants[match[1]] = parseInt(match[2]);
}
+ // Extract PORTS object - handles both flat and nested structures
+ // Flat: const PORTS = { WEB: 5550, API: 5551 }
+ // Nested: const PORTS = { server: { api: 5554 }, client: { ui: 5555 } }
+ const portsObjStart = content.match(/(?:const|let|var)\s+PORTS\s*=\s*\{/);
+ if (portsObjStart) {
+ const startIdx = portsObjStart.index + portsObjStart[0].length - 1;
+ let braceCount = 0;
+ let endIdx = startIdx;
+ for (let i = startIdx; i < content.length; i++) {
+ if (content[i] === '{') braceCount++;
+ if (content[i] === '}') {
+ braceCount--;
+ if (braceCount === 0) {
+ endIdx = i;
+ break;
+ }
+ }
+ }
+ const portsBlock = content.substring(startIdx, endIdx + 1);
+
+ // Parse flat entries: KEY: 5550
+ const flatEntries = portsBlock.matchAll(/(\w+)\s*:\s*(\d+)/g);
+ for (const entry of flatEntries) {
+ portConstants[`PORTS.${entry[1]}`] = parseInt(entry[2]);
+ }
+
+ // Parse nested entries: key: { subkey: 5550 }
+ const nestedMatches = portsBlock.matchAll(/(\w+)\s*:\s*\{([^}]+)\}/g);
+ for (const nestedMatch of nestedMatches) {
+ const parentKey = nestedMatch[1];
+ const nestedContent = nestedMatch[2];
+ const nestedPorts = {};
+ const subEntries = nestedContent.matchAll(/(\w+)\s*:\s*(\d+)/g);
+ for (const subEntry of subEntries) {
+ const port = parseInt(subEntry[2]);
+ portConstants[`PORTS.${parentKey}.${subEntry[1]}`] = port;
+ nestedPorts[subEntry[1]] = port;
+ }
+ // Also store the whole nested object for `ports: PORTS.server` references
+ portConstants[`PORTS.${parentKey}`] = nestedPorts;
+ }
+ }
+
// Match each app block: { name: '...', ... }
// This regex captures app objects including nested braces
const appBlockRegex = /\{\s*name\s*:\s*['"]([^'"]+)['"]/g;
@@ -95,7 +138,7 @@ export function parseEcosystemConfig(content) {
// Extract ports - first try the PortOS standard 'ports' object
let ports = {};
- // Look for ports: { label: port, ... } object (PortOS standard)
+ // Look for ports: { label: port, ... } object with literal values
const portsObjMatch = appBlock.match(/\bports\s*:\s*\{([^}]+)\}/);
if (portsObjMatch) {
const portsContent = portsObjMatch[1];
@@ -105,26 +148,66 @@ export function parseEcosystemConfig(content) {
}
}
- // Fall back to legacy parsing if no ports object found
+ // Look for ports: VARIABLE reference (e.g., ports: PORTS.server)
if (Object.keys(ports).length === 0) {
- // Extract PORT from env
- const portMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*\bPORT\s*:\s*(\d+)/);
- if (portMatch) {
- ports.api = parseInt(portMatch[1]);
+ const portsVarMatch = appBlock.match(/\bports\s*:\s*([\w.]+)/);
+ if (portsVarMatch) {
+ const varRef = portsVarMatch[1];
+ const resolved = portConstants[varRef];
+ if (resolved && typeof resolved === 'object') {
+ ports = { ...resolved };
+ }
}
+ }
- // Extract CDP_PORT (Chrome DevTools Protocol port for browser processes)
- const cdpPortMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*CDP_PORT\s*:\s*(\d+)/);
+ // Fall back to legacy parsing if no ports object found
+ if (Object.keys(ports).length === 0) {
+ // Helper to resolve port value (literal or variable reference)
+ const resolvePortValue = (value) => {
+ if (/^\d+$/.test(value)) return parseInt(value);
+ // Try PORTS.XXX reference
+ if (portConstants[value]) return portConstants[value];
+ // Try direct constant reference
+ if (portConstants[value]) return portConstants[value];
+ return null;
+ };
+
+ // Extract CDP_PORT first (Chrome DevTools Protocol port for browser processes)
+ // Word boundary ensures we don't match VITE_CDP_PORT
+ const cdpPortMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*\bCDP_PORT\s*:\s*([\w.]+)/);
if (cdpPortMatch) {
- ports.cdp = parseInt(cdpPortMatch[1]);
- } else {
- // Check for variable reference (CDP_PORT: CDP_PORT)
- const cdpVarMatch = appBlock.match(/CDP_PORT\s*:\s*(\w+)/);
- if (cdpVarMatch && portConstants[cdpVarMatch[1]]) {
- ports.cdp = portConstants[cdpVarMatch[1]];
+ const resolved = resolvePortValue(cdpPortMatch[1]);
+ if (resolved) ports.cdp = resolved;
+ }
+
+ // Extract PORT from env (handles both literals and variable references)
+ const portMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*\bPORT\s*:\s*([\w.]+)/);
+ if (portMatch) {
+ const resolved = resolvePortValue(portMatch[1]);
+ if (resolved) {
+ // Smart labeling based on process name and context
+ const isUiProcess = /[-_](ui|client)$/i.test(processName);
+ const isBrowserProcess = /[-_]browser$/i.test(processName);
+ const hasCdpPort = ports.cdp !== undefined;
+
+ if (isUiProcess) {
+ ports.ui = resolved;
+ } else if (isBrowserProcess && hasCdpPort) {
+ // Browser processes with CDP_PORT use PORT for health checks
+ ports.health = resolved;
+ } else {
+ ports.api = resolved;
+ }
}
}
+ // Extract VITE_PORT for Vite processes
+ const vitePortMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*VITE_PORT\s*:\s*([\w.]+)/);
+ if (vitePortMatch) {
+ const resolved = resolvePortValue(vitePortMatch[1]);
+ if (resolved) ports.ui = resolved;
+ }
+
// Also check for --port in args
if (Object.keys(ports).length === 0) {
const argsPortMatch = appBlock.match(/args\s*:\s*['"][^'"]*--port\s+(\d+)/);