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

address review (codex): rewrite top-level port constants and align write-back file order

[._.]/ Adam Eivyd8b9477734bb
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index ec25180be2..695c407cd7 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -9,6 +9,15 @@ import { detectAppIcon } from './appIconDetect.js';
/** App types that do not use PM2 for process management */
export const NON_PM2_TYPES = new Set(['ios-native', 'macos-native', 'xcode', 'swift']);
+/**
+ * Ecosystem config filenames in the order they're resolved. The READER
+ * (parseEcosystemFromPath) and the WRITER (writeEcosystemPorts) must agree on
+ * this order: if a repo has both, the writer has to rewrite the same file the
+ * reader derives ports from, or a port edit lands in a file detection ignores
+ * and silently reverts on the next refresh.
+ */
+const ECOSYSTEM_CONFIG_FILENAMES = ['ecosystem.config.js', 'ecosystem.config.cjs'];
+
/** Check if an app type uses PM2 for process management */
export const usesPm2 = (type) => !NON_PM2_TYPES.has(type);
@@ -401,7 +410,7 @@ function extractVitePort(content) {
* @returns {{ processes: Array, pm2Home: string|null }}
*/
export async function parseEcosystemFromPath(dirPath) {
- for (const ecosystemFile of ['ecosystem.config.js', 'ecosystem.config.cjs']) {
+ for (const ecosystemFile of ECOSYSTEM_CONFIG_FILENAMES) {
const ecosystemPath = join(dirPath, ecosystemFile);
if (existsSync(ecosystemPath)) {
const content = await readFile(ecosystemPath, 'utf-8');
@@ -487,7 +496,8 @@ function findValuePortRegions(content) {
* 1. numeric values inside a `const PORTS = {...}` block AND each per-app
* `ports: {...}` object (arbitrary keys — matched by value),
* 2. `--port <n>` tokens in args strings,
- * 3. port-ish env keys (`PORT`, `*_PORT`, `VITE_PORT`) and `port:`.
+ * 3. port-ish env keys (`PORT`, `*_PORT`, `VITE_PORT`) and `port:`,
+ * 4. top-level port constants (`const API_PORT = <n>`).
*
* Each old value is first swapped to a unique placeholder, then placeholders
* resolve to new values — so a remap like 6000→6001, 6001→6002 can't chain.
@@ -535,6 +545,16 @@ export function rewriteEcosystemPorts(content, remap) {
);
});
+ // 4) Top-level port constants — `const API_PORT = 5555` / `let CDP_PORT = …`.
+ // parseEcosystemConfig derives ports from these (a `\w*PORT\w*` name `=`
+ // number), so an edit that skipped them would revert on the next refresh.
+ pairs.forEach(([oldP], i) => {
+ out = out.replace(
+ new RegExp(`((?:const|let|var)\\s+\\w*PORT\\w*\\s*=\\s*)${oldP}\\b`, 'g'),
+ `$1${ph(i)}`
+ );
+ });
+
// Resolve placeholders → new values.
pairs.forEach(([, newP], i) => {
out = out.split(ph(i)).join(String(newP));
@@ -552,7 +572,9 @@ export function rewriteEcosystemPorts(content, remap) {
* @param {Map<number,number>|Array<[number,number]>} remap
*/
export async function writeEcosystemPorts(repoPath, remap) {
- for (const name of ['ecosystem.config.cjs', 'ecosystem.config.js']) {
+ // Same resolution order as parseEcosystemFromPath — rewrite the file the
+ // reader actually derives ports from (see ECOSYSTEM_CONFIG_FILENAMES).
+ for (const name of ECOSYSTEM_CONFIG_FILENAMES) {
const filePath = join(repoPath, name);
if (!existsSync(filePath)) continue;
const content = await readFile(filePath, 'utf-8');
diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.js
index f9bbb53fc6..cfcb2f26e9 100644
--- a/server/services/streamingDetect.test.js
+++ b/server/services/streamingDetect.test.js
@@ -1,5 +1,8 @@
-import { describe, it, expect } from 'vitest';
-import { parseEcosystemConfig, rewriteEcosystemPorts } from './streamingDetect.js';
+import { describe, it, expect, afterEach } from 'vitest';
+import { mkdtempSync, writeFileSync, readFileSync, rmSync } from 'fs';
+import { tmpdir } from 'os';
+import { join } from 'path';
+import { parseEcosystemConfig, rewriteEcosystemPorts, writeEcosystemPorts } from './streamingDetect.js';
describe('parseEcosystemConfig', () => {
it('captures arbitrary *_PORT env vars and labels them by camelCased stem', () => {
@@ -352,9 +355,47 @@ module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: PORTS.API, V
for (const [oldP] of pairs) expect(out).not.toContain(`PORT: ${oldP}`);
});
+ it('rewrites top-level port constants the parser derives ports from', () => {
+ // parseEcosystemConfig reads `const API_PORT = 5555` and resolves env refs
+ // to it; a rewrite skipping the constant would let the edit revert.
+ const content = `const API_PORT = 5555;
+const CDP_PORT = 5549;
+module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: API_PORT, CDP_PORT: CDP_PORT } }] };
+`;
+ const out = rewriteEcosystemPorts(content, [[5555, 6000]]);
+ expect(out).toContain('const API_PORT = 6000;');
+ expect(out).toContain('const CDP_PORT = 5549;'); // not in remap — untouched
+ expect(parseEcosystemConfig(out).processes[0].ports.api).toBe(6000);
+ });
+
it('is a no-op when remap is empty or only contains identity pairs', () => {
const content = `module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: 5173 } }] };`;
expect(rewriteEcosystemPorts(content, [])).toBe(content);
expect(rewriteEcosystemPorts(content, [[5173, 5173]])).toBe(content);
});
});
+
+describe('writeEcosystemPorts', () => {
+ let dir;
+ afterEach(() => { if (dir) rmSync(dir, { recursive: true, force: true }); dir = null; });
+
+ it('rewrites the .js file first (the same one parseEcosystemFromPath reads) when both exist', async () => {
+ dir = mkdtempSync(join(tmpdir(), 'eco-write-'));
+ const body = (port) => `module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: ${port} } }] };\n`;
+ writeFileSync(join(dir, 'ecosystem.config.js'), body(5173));
+ writeFileSync(join(dir, 'ecosystem.config.cjs'), body(5173));
+
+ const result = await writeEcosystemPorts(dir, [[5173, 6000]]);
+ expect(result).toEqual({ file: 'ecosystem.config.js', changed: true });
+ // The reader's file is updated; the .cjs is left as-is (reader never sees it).
+ expect(readFileSync(join(dir, 'ecosystem.config.js'), 'utf-8')).toContain('PORT: 6000');
+ expect(readFileSync(join(dir, 'ecosystem.config.cjs'), 'utf-8')).toContain('PORT: 5173');
+ });
+
+ it('reports changed:false when no port literal matches', async () => {
+ dir = mkdtempSync(join(tmpdir(), 'eco-write-'));
+ writeFileSync(join(dir, 'ecosystem.config.cjs'), `module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: 4321 } }] };\n`);
+ const result = await writeEcosystemPorts(dir, [[5173, 6000]]);
+ expect(result).toEqual({ file: 'ecosystem.config.cjs', changed: false });
+ });
+});