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: prevent port conflict when starting managed apps with process.env.PORT

Adam Eivyfc99b456ce56
diff --git a/.changelog/v0.12.x.md b/.changelog/v0.12.x.md
index ea06b8d126..b7654a9c1e 100644
--- a/.changelog/v0.12.x.md
+++ b/.changelog/v0.12.x.md
@@ -58,6 +58,11 @@ This release adds branch management and automated release workflow capabilities
## Fixes
+### Managed App Port Conflict — ENV Leak & Detection
+- **Fix**: When PortOS started managed apps via `startFromEcosystem()`, the PortOS server's own `PORT=5554` env var leaked into child processes. Apps with `process.env.PORT || fallback` in their ecosystem configs would bind to 5554 instead of their intended port, crashing portos-server on restart.
+- **Fix**: Port detection regex in `parseEcosystemConfig()` couldn't parse `process.env.PORT || 4420` expressions — it only captured `process.env.PORT` and failed to resolve it. Now extracts the `|| fallback` value.
+- **Solution**: `startFromEcosystem()` now strips `PORT` and `HOST` from the inherited env before spawning PM2, and `resolvePortValue()` handles `expr || fallback` patterns
+
### Runner Config Access Crash
- **Fix**: `server/services/runner.js` was accessing `aiToolkitInstance.config.dataDir` and `aiToolkitInstance.config.hooks`, but the AI Toolkit object returned by `createAIToolkit()` has no `.config` property (config is internal to service closures). This caused a `TypeError: Cannot read properties of undefined (reading 'dataDir')` whenever the PortOS runner shim's `executeCliRun` was invoked.
- **Solution**: Pass `dataDir` and `hooks` explicitly to the runner shim via `setAIToolkit(toolkit, config)` and store them in a local `runnerConfig` variable
diff --git a/server/services/pm2.js b/server/services/pm2.js
index c27f68119e..65951ff055 100644
--- a/server/services/pm2.js
+++ b/server/services/pm2.js
@@ -259,7 +259,13 @@ export async function startFromEcosystem(cwd, processNames = []) {
args.push('--only', processNames.join(','));
}
- const child = spawn('pm2', args, { cwd, shell: false });
+ // Strip PortOS env vars so child ecosystem configs don't inherit them
+ // e.g., process.env.PORT || 4420 would resolve to PortOS's 5554 otherwise
+ const cleanEnv = { ...process.env };
+ delete cleanEnv.PORT;
+ delete cleanEnv.HOST;
+
+ const child = spawn('pm2', args, { cwd, shell: false, env: cleanEnv });
let stdout = '';
let stderr = '';
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index 7045b99e3c..c7c1c816a1 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -163,26 +163,30 @@ export function parseEcosystemConfig(content) {
// 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)
+ // Helper to resolve port value from literal, variable reference, or expression
+ // Handles: 4420, PORTS.API, process.env.PORT || 4420, Number(...) || 4420
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];
+ const trimmed = value.trim();
+ if (/^\d+$/.test(trimmed)) return parseInt(trimmed);
+ // Expression with || fallback: "process.env.PORT || 4420"
+ const fallbackMatch = trimmed.match(/\|\|\s*['"]?(\d+)['"]?\s*$/);
+ if (fallbackMatch) return parseInt(fallbackMatch[1]);
+ // PORTS.XXX or constant reference
+ if (portConstants[trimmed]) return portConstants[trimmed];
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.]+)/);
+ // Capture full expression (up to , or }) to handle process.env.X || fallback
+ const cdpPortMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*\bCDP_PORT\s*:\s*([^,}\n]+)/);
if (cdpPortMatch) {
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.]+)/);
+ // Extract PORT from env (handles literals, variables, and process.env.X || fallback)
+ const portMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*\bPORT\s*:\s*([^,}\n]+)/);
if (portMatch) {
const resolved = resolvePortValue(portMatch[1]);
if (resolved) {
@@ -203,7 +207,7 @@ export function parseEcosystemConfig(content) {
}
// Extract VITE_PORT for Vite processes
- const vitePortMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*VITE_PORT\s*:\s*([\w.]+)/);
+ const vitePortMatch = appBlock.match(/(?:env|env_development|env_production)\s*:\s*\{[^}]*VITE_PORT\s*:\s*([^,}\n]+)/);
if (vitePortMatch) {
const resolved = resolvePortValue(vitePortMatch[1]);
if (resolved) ports.ui = resolved;