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): make targeted port write all-or-nothing

Adam Eivybc800d782568
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index 50ddfe1d6d..1f771311f9 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -803,6 +803,16 @@ export async function writeEcosystemPorts(repoPath, remap) {
* `{ file, changed, applied, unapplied }`; `unapplied` lists edits whose process
* block or label literal wasn't found (caller decides whether that's a failure).
*
+ * **All-or-nothing.** The rewrite is computed in full (pure) before any I/O, and
+ * the file is written ONLY when every edit applied (`unapplied` is empty). A
+ * batch where some labels are rewritable and others aren't (e.g. one port is a
+ * literal `PORT: 6000` but a same-valued sibling lives in an external
+ * `ports: PORTS.client` reference) must NOT persist its applied subset — the
+ * caller turns a non-empty `unapplied` into a 422 and skips the registry update,
+ * so a partial on-disk write would leave config and registry inconsistent for a
+ * request that "failed". When `unapplied` is non-empty the file is left
+ * untouched and `applied` is reported empty (nothing was persisted).
+ *
* @param {string} repoPath
* @param {Array<{processName:string,label:string,oldPort:number,newPort:number}>} edits
*/
@@ -812,6 +822,12 @@ export async function writeEcosystemPortsByProcess(repoPath, edits) {
if (!existsSync(filePath)) continue;
const content = await readFile(filePath, 'utf-8');
const { content: updated, applied, unapplied } = rewriteEcosystemPortsByProcess(content, edits);
+ // Don't persist a partial batch: if any edit couldn't be applied, the
+ // caller rejects the whole request, so writing the applied subset would
+ // desync config from the (un-updated) registry.
+ if (unapplied.length > 0) {
+ return { file: name, changed: false, applied: [], unapplied };
+ }
if (updated !== content) {
await atomicWrite(filePath, updated);
return { file: name, changed: true, applied, unapplied };
diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.js
index 08627c8788..386bd2974f 100644
--- a/server/services/streamingDetect.test.js
+++ b/server/services/streamingDetect.test.js
@@ -2,7 +2,7 @@ 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, rewriteEcosystemPortsByProcess, writeEcosystemPorts } from './streamingDetect.js';
+import { parseEcosystemConfig, rewriteEcosystemPorts, rewriteEcosystemPortsByProcess, writeEcosystemPorts, writeEcosystemPortsByProcess } from './streamingDetect.js';
describe('parseEcosystemConfig', () => {
it('captures arbitrary *_PORT env vars and labels them by camelCased stem', () => {
@@ -606,3 +606,44 @@ describe('writeEcosystemPorts', () => {
expect(result).toEqual({ file: 'ecosystem.config.cjs', changed: false });
});
});
+
+describe('writeEcosystemPortsByProcess', () => {
+ let dir;
+ afterEach(() => { if (dir) rmSync(dir, { recursive: true, force: true }); dir = null; });
+
+ it('writes the file when every targeted edit applies', async () => {
+ dir = mkdtempSync(join(tmpdir(), 'eco-tgt-'));
+ writeFileSync(join(dir, 'ecosystem.config.cjs'),
+ `module.exports = { apps: [{ name: 'srv', script: 's.js', ports: { api: 6000, ui: 6000 } }] };\n`);
+ const result = await writeEcosystemPortsByProcess(dir, [
+ { processName: 'srv', label: 'ui', oldPort: 6000, newPort: 7000 },
+ ]);
+ expect(result.changed).toBe(true);
+ expect(result.unapplied).toHaveLength(0);
+ expect(readFileSync(join(dir, 'ecosystem.config.cjs'), 'utf-8')).toContain('ui: 7000');
+ });
+
+ it('does NOT persist a partial batch — leaves the file untouched when any edit is unapplied', async () => {
+ // 'srv' has a literal api PORT (rewritable) but the same-valued ui lives in
+ // a ports: PORTS.client reference (not reachable in-block). The route 422s
+ // such a request, so the applied subset must NOT be written to disk — else
+ // config diverges from the un-updated registry for a "failed" request.
+ dir = mkdtempSync(join(tmpdir(), 'eco-tgt-'));
+ const original = `const PORTS = { client: { ui: 6000 } };
+module.exports = { apps: [
+ { name: 'srv', script: 's.js', env: { PORT: 6000 } },
+ { name: 'srv-ui', script: 'npx', ports: PORTS.client }
+] };
+`;
+ writeFileSync(join(dir, 'ecosystem.config.cjs'), original);
+ const result = await writeEcosystemPortsByProcess(dir, [
+ { processName: 'srv', label: 'api', oldPort: 6000, newPort: 7000 }, // applies
+ { processName: 'srv-ui', label: 'ui', oldPort: 6000, newPort: 7001 }, // unapplied (external const)
+ ]);
+ expect(result.changed).toBe(false);
+ expect(result.applied).toHaveLength(0); // nothing persisted
+ expect(result.unapplied.length).toBeGreaterThan(0);
+ // File is byte-for-byte unchanged — no partial write.
+ expect(readFileSync(join(dir, 'ecosystem.config.cjs'), 'utf-8')).toBe(original);
+ });
+});