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): don't false-persist port-reference blocks; split ui/devUi siblings

Adam Eivyee3def146fc7
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 488cd12afb..785522f84d 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -22,4 +22,4 @@
- Scheduled-task descriptions in the Automation UI no longer fall back to a raw dasherized label. `claim-work` (the single-source work-tracker router), `code-reviewer-a`/`code-reviewer-b`, and `do-replan` now show real one-line descriptions instead of "claim work" / "code reviewer a", which read as orphaned/legacy tasks. A parity guard test now keeps the description map in sync with the task-type list.
- PM2 standardization no longer assigns ports that are already in use. The analysis now feeds the AI the set of listening/reserved ports (plus the common Vite defaults 5173/5174) to avoid, and a deterministic post-pass reassigns any port that still collides into the 6000–6099 range — so standardizing an app never lands on a port another app already holds.
- Editing an app's ports now persists. Port fields are derived from `ecosystem.config.cjs`, so saving only updated PortOS's registry while PM2 kept the old ports and the next config refresh reverted the change. Saving a changed port now rewrites the matching value in the app's `ecosystem.config.cjs` (the source of truth). Restart the app for the new port to take effect.
-- Editing a **shared or derived** port now persists too. When two port fields resolve to the same literal (e.g. the prod UI is served by the API, so `uiPort === apiPort`), the value-keyed rewrite couldn't tell them apart and the edit returned a 422. The PUT route now falls back to a **per-process-label-targeted** rewrite: it locates the specific process block in `ecosystem.config.cjs` and rewrites only the touched label's port (`api`/`ui`/`devUi`), leaving the sibling field — and any same-valued unrelated number — untouched. Edits to ports that live only under a process's `env`/`args` (no top-level field) persist as well. The 422 is now reserved for the genuinely unpersistable case (the literal isn't in the config — e.g. computed entirely at runtime).
+- Editing a **shared or derived** port now persists too. When two port fields resolve to the same literal (e.g. the prod UI is served by the API, so `uiPort === apiPort`), the value-keyed rewrite couldn't tell them apart and the edit returned a 422. The PUT route now falls back to a **per-process-label-targeted** rewrite: it locates the specific process block in `ecosystem.config.cjs` and rewrites only the touched label's port (`api`/`ui`/`devUi`), leaving the sibling field — and any same-valued unrelated number — untouched. Edits to ports that live only under a process's `env`/`args` (no top-level field) persist as well. The 422 is now reserved for the genuinely unpersistable case (the literal isn't in the touched process block — e.g. it lives in a shared `const PORTS = {…}` referenced via `ports: PORTS.server`, or is computed entirely at runtime), so a shared-value edit never falsely reports success while the displayed port silently reverts.
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index 1d809e6012..50ddfe1d6d 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -561,10 +561,22 @@ function findAppBlock(content, processName) {
* Sources rewritten per label mirror parseEcosystemConfig's derivation AND the
* PM2 runtime env (so the config the parser reads and the env PM2 launches with
* both move together):
- * - api → `ports: { api: N }`, env `PORT`, `PORT: … || N` fallback, `port:`
- * - ui → `ports: { ui|devUi: N }`, env `VITE_PORT`, `--port N`, and env
- * `PORT` ONLY when the block has no `ports:` object (a UI-named
- * process whose bare `PORT` IS the UI port).
+ * - api → `ports: { api: N }`, plus env `PORT`/`PORT: … || N`/`port:` ONLY
+ * when the block has no `ports:` REFERENCE (`ports: PORTS.server`) —
+ * for a reference the parser reads the external const, not env, so
+ * rewriting env would falsely report success while the real source
+ * (and displayed port) reverts on refresh.
+ * - ui → exact `ui:` key, env `VITE_PORT`, `--port N`, and bare env `PORT`
+ * only when there's no explicit ports source (inline object OR
+ * reference) — a UI-named process whose bare `PORT` IS the UI port.
+ * - devUi → exact `devUi:` key (same env/args/bare-PORT sources as ui), PLUS
+ * a Vite process's `ui:` literal when the block has no explicit
+ * `devUi:` key (parseEcosystemConfig relabels ui→devUi when an api
+ * sibling exists, so the on-disk source for that devUi IS `ui:`).
+ *
+ * Matching the EXACT label key (not a combined `ui|devUi`) means a block that
+ * carries both `ui:` and `devUi:` with the same value only rewrites the one the
+ * user edited — the sibling keeps its literal.
*
* @returns {{ block: string, changed: boolean }}
*/
@@ -575,26 +587,33 @@ function rewriteLabelInBlock(block, label, oldP, newP) {
// the number changes (no whitespace artifact).
const ph = 'PORT_TGT_PLACEHOLDER';
const NUM = `${oldP}\\b`;
- const hasPortsObj = /\bports\s*:\s*\{/.test(block);
-
- let pats;
+ // An explicit ports source is what parseEcosystemConfig derives from. An
+ // inline `ports: { ... }` object is rewritable in-block; a `ports: PORTS.x`
+ // REFERENCE points at an external const this block can't reach, so the
+ // same-valued env literal must NOT be used as a fallback for it (false
+ // success + silent runtime-env change). Both forms mean env `PORT` is
+ // metadata, not the parser's value.
+ const hasInlinePortsObj = /\bports\s*:\s*\{/.test(block);
+ const hasPortsReference = /\bports\s*:\s*[A-Za-z_$]/.test(block);
+ const hasExplicitPortsSource = hasInlinePortsObj || hasPortsReference;
+ // `PORT`/`'PORT'`/`` `PORT` `` followed by `:` and optional whitespace.
+ const PORT_KEY = "['\"`]?\\bPORT\\b['\"`]?\\s*:\\s*";
+
+ const pats = [];
if (label === 'api') {
- pats = [
- `(\\bapi\\s*:\\s*)${NUM}`, // ports: { api: N }
- `(['"\`]?\\bPORT\\b['"\`]?\\s*:\\s*[^,}\\n]*?\\|\\|\\s*['"]?)${NUM}`, // PORT: … || N (fallback)
- `(['"\`]?\\bPORT\\b['"\`]?\\s*:\\s*)${NUM}`, // env PORT: N
- `(\\bport\\s*:\\s*)${NUM}`, // port: N
- ];
+ pats.push(`(\\bapi\\s*:\\s*)${NUM}`); // ports: { api: N }
+ if (!hasPortsReference) {
+ pats.push(`(${PORT_KEY}[^,}\\n]*?\\|\\|\\s*['"]?)${NUM}`); // PORT: … || N (fallback)
+ pats.push(`(${PORT_KEY})${NUM}`); // env PORT: N
+ pats.push(`(\\bport\\s*:\\s*)${NUM}`); // port: N
+ }
} else {
- // ui / devUi share on-disk sources (devUi is a parse-time relabel of ui).
- pats = [
- `(\\b(?:ui|devUi)\\s*:\\s*)${NUM}`, // ports: { ui|devUi: N }
- `(['"\`]?\\bVITE_PORT\\b['"\`]?\\s*:\\s*)${NUM}`, // env VITE_PORT: N
- `(--port\\s+)${NUM}`, // args --port N
- ];
- // Bare PORT is the UI port only when there's no explicit ports object (a
- // ports object means env is metadata-only and PORT belongs to the api).
- if (!hasPortsObj) pats.push(`(['"\`]?\\bPORT\\b['"\`]?\\s*:\\s*)${NUM}`);
+ const hasDevUiKey = /\bdevUi\s*:\s*\d/.test(block);
+ pats.push(label === 'devUi' ? `(\\bdevUi\\s*:\\s*)${NUM}` : `(\\bui\\s*:\\s*)${NUM}`); // exact key
+ if (label === 'devUi' && !hasDevUiKey) pats.push(`(\\bui\\s*:\\s*)${NUM}`); // relabeled-from-ui source
+ pats.push(`(['"\`]?\\bVITE_PORT\\b['"\`]?\\s*:\\s*)${NUM}`); // env VITE_PORT: N
+ pats.push(`(--port\\s+)${NUM}`); // args --port N
+ if (!hasExplicitPortsSource) pats.push(`(${PORT_KEY})${NUM}`); // bare PORT of a UI-named process
}
let out = block;
@@ -609,10 +628,14 @@ function rewriteLabelInBlock(block, label, oldP, newP) {
/**
* Per-process-label-targeted port rewrite — the disambiguating counterpart to
* the value-keyed `rewriteEcosystemPorts`. Each edit names the process and the
- * label (`api`/`ui`/`devUi`) to change, so a value shared by two labels (e.g.
- * prod UI served by the API where `uiPort === apiPort`, or an explicit
- * `ports: { api: N, ui: N }` map) is rewritten on exactly the one the user
- * touched. Pure (no I/O) for unit-testability.
+ * label (`api`/`ui`/`devUi`) to change, so a value shared by two labels in the
+ * SAME block (e.g. an explicit `ports: { api: N, ui: N }` map, or a UI process
+ * whose bare `PORT` is its only port) is rewritten on exactly the one the user
+ * touched. An edit whose label has no rewritable literal inside the process
+ * block — e.g. the value lives in an external `const PORTS = {…}` reached via
+ * `ports: PORTS.server`, which this in-block rewrite can't see — is returned in
+ * `unapplied` (the caller 422s) rather than falsely rewriting a same-valued env
+ * literal the parser ignores. Pure (no I/O) for unit-testability.
*
* @param {string} content
* @param {Array<{processName:string,label:string,oldPort:number,newPort:number}>} edits
diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.js
index 4f4751c13a..08627c8788 100644
--- a/server/services/streamingDetect.test.js
+++ b/server/services/streamingDetect.test.js
@@ -506,6 +506,42 @@ describe('rewriteEcosystemPortsByProcess', () => {
expect(parseEcosystemConfig(r.content).processes.find(p => p.name === 'srv-ui').ports.devUi).toBe(7002);
});
+ it('does NOT rewrite env PORT as a fallback for a ports: PORTS.x reference block (honest unapplied, no false success)', () => {
+ // The block derives api/ui from `ports: PORTS.server` (a reference to a
+ // const outside the app block), so the parser ignores env.PORT. A targeted
+ // edit can't reach the external const from inside the block — so it must
+ // report `unapplied` (→ caller 422s) rather than rewriting env.PORT, which
+ // would falsely report success while the displayed/derived port reverts AND
+ // silently change the runtime API env port.
+ const content = `const PORTS = { server: { api: 6000, ui: 6000 } };
+module.exports = { apps: [
+ { name: 'srv', script: 's.js', ports: PORTS.server, env: { PORT: 6000 } }
+] };
+`;
+ const r = rewriteEcosystemPortsByProcess(content, [
+ { processName: 'srv', label: 'api', oldPort: 6000, newPort: 7000 },
+ ]);
+ expect(r.applied).toHaveLength(0);
+ expect(r.unapplied).toHaveLength(1);
+ expect(r.content).toBe(content); // env.PORT untouched, nothing falsely rewritten
+ });
+
+ it('rewrites only the edited key when an inline ports object has ui and devUi sharing a value', () => {
+ // ui and devUi both 6000 in one block. Editing only ui must leave devUi at
+ // 6000 — a combined (ui|devUi) pattern would clobber the sibling.
+ const content = `module.exports = { apps: [
+ { name: 'srv', script: 's.js', ports: { ui: 6000, devUi: 6000 } }
+] };
+`;
+ const r = rewriteEcosystemPortsByProcess(content, [
+ { processName: 'srv', label: 'ui', oldPort: 6000, newPort: 7000 },
+ ]);
+ expect(r.applied).toHaveLength(1);
+ const ports = parseEcosystemConfig(r.content).processes[0].ports;
+ expect(ports.ui).toBe(7000);
+ expect(ports.devUi).toBe(6000); // sibling untouched
+ });
+
it('reports an edit as unapplied when the process name is not found', () => {
const content = `module.exports = { apps: [{ name: 'srv', script: 's.js', env: { PORT: 6000 } }] };`;
const r = rewriteEcosystemPortsByProcess(content, [