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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): order combined passes to avoid cross-pass port chaining
Adam Eivy759b7705bc83
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.jsindex 79255817d9..1574d596e5 100644--- a/server/services/streamingDetect.js+++ b/server/services/streamingDetect.js@@ -901,15 +901,42 @@ export async function writeEcosystemPortEdits(repoPath, remap, edits) {if (!existsSync(filePath)) continue;const content = await readFile(filePath, 'utf-8');- // Value-keyed pass first, then targeted — both against the in-memory result- // so the single write reflects both. The two passes operate on disjoint- // literals (distinct values vs shared values), so ordering can't clobber.- const afterRemap = hasRemap ? rewriteEcosystemPorts(content, remap) : content;- const { content: afterTargeted, applied, unapplied } = hasEdits- ? rewriteEcosystemPortsByProcess(afterRemap, edits)- : { content: afterRemap, applied: [], unapplied: [] };+ // The two passes both run against the in-memory result of the other so a+ // single write reflects both — but whichever runs SECOND can match a literal+ // the first pass just created, chaining one edit into another. That happens+ // when the second pass's match-value equals a value the first produced:+ // - value-keyed→targeted chains if a targeted OLD value equals a remap NEW+ // value (forward collision), and+ // - targeted→value-keyed chains if a remap OLD value equals a targeted NEW+ // value (reverse collision).+ // Pick the order that avoids the collision present; if BOTH directions+ // collide there's no safe order, so treat the whole batch as unpersistable+ // (the caller 422s — honest, vs. silently corrupting a sibling port).+ const remapOlds = remap.map(([o]) => o);+ const remapNews = remap.map(([, n]) => n);+ const targetedOlds = (edits || []).map(e => e.oldPort);+ const targetedNews = (edits || []).map(e => e.newPort);+ const forwardCollision = hasRemap && hasEdits && targetedOlds.some(o => remapNews.includes(o));+ const reverseCollision = hasRemap && hasEdits && remapOlds.some(o => targetedNews.includes(o));++ if (forwardCollision && reverseCollision) {+ return { file: name, changed: false, remapApplied: false, applied: [], unapplied: edits || [] };+ }- const remapApplied = afterRemap !== content;+ let result, applied, unapplied, remapApplied;+ if (forwardCollision) {+ // Run targeted FIRST so the value-keyed pass can't feed it a fresh literal.+ const t = hasEdits ? rewriteEcosystemPortsByProcess(content, edits) : { content, applied: [], unapplied: [] };+ const afterRemap = hasRemap ? rewriteEcosystemPorts(t.content, remap) : t.content;+ result = afterRemap; applied = t.applied; unapplied = t.unapplied;+ remapApplied = hasRemap && afterRemap !== t.content;+ } else {+ // Default: value-keyed first, then targeted (safe when no forward collision).+ const afterRemap = hasRemap ? rewriteEcosystemPorts(content, remap) : content;+ const t = hasEdits ? rewriteEcosystemPortsByProcess(afterRemap, edits) : { content: afterRemap, applied: [], unapplied: [] };+ result = t.content; applied = t.applied; unapplied = t.unapplied;+ remapApplied = hasRemap && afterRemap !== content;+ }// Persist NOTHING when ANY part of the request is unpersistable — the caller// rejects the whole update (422) in either of these cases, so a partial@@ -922,8 +949,8 @@ export async function writeEcosystemPortEdits(repoPath, remap, edits) {return { file: name, changed: false, remapApplied: false, applied: [], unapplied };}- if (afterTargeted !== content) {- await atomicWrite(filePath, afterTargeted);+ if (result !== content) {+ await atomicWrite(filePath, result);return { file: name, changed: true, remapApplied, applied, unapplied };}return { file: name, changed: false, remapApplied, applied, unapplied };diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.jsindex 5afce64529..15aedf7ea4 100644--- a/server/services/streamingDetect.test.js+++ b/server/services/streamingDetect.test.js@@ -792,6 +792,30 @@ module.exports = { apps: [expect(readFileSync(join(dir, 'ecosystem.config.cjs'), 'utf-8')).toBe(original);});+ it('does not chain when a value-keyed NEW value equals a targeted OLD value (forward collision)', async () => {+ // One process: ports {api:6000, ui:6000, devUi:5556} + VITE_PORT:5556.+ // Save devUiPort 5556→6000 (value-keyed) AND uiPort 6000→7000 (targeted,+ // shared with api). Naive value-keyed-first would rewrite devUi 5556→6000,+ // then the targeted ui pass would see that fresh 6000 and chain it to 7000,+ // corrupting devUi. Running targeted FIRST avoids it: ui→7000 lands, then+ // devUi 5556→6000 — and api stays 6000.+ dir = mkdtempSync(join(tmpdir(), 'eco-edits-'));+ writeFileSync(join(dir, 'ecosystem.config.cjs'), `module.exports = { apps: [+ { name: 'srv', script: 's.js', ports: { api: 6000, ui: 6000, devUi: 5556 } }+] };+`);+ const result = await writeEcosystemPortEdits(+ dir,+ [[5556, 6000]], // devUiPort distinct → value-keyed+ [{ processName: 'srv', label: 'ui', oldPort: 6000, newPort: 7000 }] // uiPort shared → targeted+ );+ expect(result.changed).toBe(true);+ const ports = parseEcosystemConfig(readFileSync(join(dir, 'ecosystem.config.cjs'), 'utf-8')).processes[0].ports;+ expect(ports.ui).toBe(7000); // edited+ expect(ports.devUi).toBe(6000); // remapped, NOT chained to 7000+ expect(ports.api).toBe(6000); // untouched+ });+it('is a no-op when both remap and edits are empty', async () => {dir = mkdtempSync(join(tmpdir(), 'eco-edits-'));const original = `module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: 6000 } }] };\n`;