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: resolve the primary process in config order and ignore role-suffixed siblings (#7357)

[._.]/ Adam Eivyfaa6eb9613dc
diff --git a/server/lib/README.md b/server/lib/README.md
index 5363f10026..3e34a1a340 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -529,7 +529,7 @@ pm` default, `NPM_CONFIG_PREFIX`, nvm/Volta) installed `codex` successfully and
| `db/` | Boot DDL for the PostgreSQL schema, split per domain (#2832). `db/schema/index.js` re-exports each module's statement array and composes the two ordered lists `ensureSchemaImpl()` runs on every boot (`buildUpgradeDdl()` then `buildCatalogDdl()`). **Statement order is load-bearing** — append inside the domain module and leave the composer order alone. See `db/schema/README.md` for the module catalog. |
| `pgTimestamp.js` | `mirrorTimestamp(value, fallback)` — coerce a hand-editable timestamp into a value Postgres TIMESTAMPTZ always accepts (or fall back), guarding boot-time binds against `Date.parse` rollover + out-of-range years. |
| `pgTools.js` | `pg_dump` binary resolution shared by the backup snapshot path and the native↔Docker export path: `resolvePgDumpBinary(serverMajor)` (PORTOS_PGDUMP override → version-aware auto-select → bare `pg_dump`), plus the lower-level `pickPgDump` / `discoverPgDumpCandidates` / `resolvePgDump`. Picks the closest installed `pg_dump` whose major is ≥ the running server's. |
-| `ecosystemProcessPorts.js` | Attributes a parsed PM2 ecosystem config's port labels to the app that OWNS them (#7357): `processSurface(name)` (a process name minus one trailing role suffix, so `portos-server`/`portos-ui` share surface `portos` while `portos-autofixer-ui` does not), `attributeProcessPorts(processes, app)` (`api`/`ui`/`devUi` resolved from the app's own surface first, then from any carrier not provably a sibling surface, and left undefined when none — which is what lets the served-by-API `uiPort` derivation take over), and `findCrossProcessPortCollision(processes, edits)` (the pre-write guard: a port another process in the same config already holds). Shared by `services/appPortConfig.js` (write-back) and `services/appListEnrichment.js` (read/derive) so a displayed port and a rewritten port can never disagree about whose it is. |
+| `ecosystemProcessPorts.js` | Attributes a parsed PM2 ecosystem config's port labels to the app that OWNS them (#7357): `processSurface(name)` (a process name minus one trailing role suffix, so `portos-server`/`portos-ui` share surface `portos` while `portos-autofixer-ui` does not), `attributeProcessPorts(processes, app)` (`api`/`ui`/`devUi` resolved from the app's own surface first, then from a carrier whose name declares no role at all, and left undefined when none — which is what lets the served-by-API `uiPort` derivation take over), and `findCrossProcessPortCollision(processes, edits)` (the pre-write guard: a port another process in the same config already holds). Shared by `services/appPortConfig.js` (write-back) and `services/appListEnrichment.js` (read/derive) so a displayed port and a rewritten port can never disagree about whose it is. |
| `ports.js` | Canonical `PORTS` object (mirroring `ecosystem.config.cjs`, pinned by `ports.test.js`), `DEFAULT_PEER_PORT`, and `resolvePostgresPort(pgMode)`. A pure leaf: `client/src/lib/ports.js` re-exports it, so nothing here may read `process.env` at module scope — the env-derived origins live in `portosUrls.js`. |
| `portosUrls.js` | `PORTOS_UI_URL` / `PORTOS_API_URL` — the origins PortOS interpolates into prompts and links, resolved from `PORTOS_*` / `PORT` at module load. Split out of `ports.js` so that module stays free of module-scope `process.env` reads and can be re-exported to the client. |
| `platform.js` | Platform/OS detection helpers — listening-port probes plus `isAppleSilicon()` (arm64 darwin; gates MLX model features, detect at the route boundary). |
diff --git a/server/lib/ecosystemProcessPorts.js b/server/lib/ecosystemProcessPorts.js
index 0c2a34de5f..5f033266ec 100644
--- a/server/lib/ecosystemProcessPorts.js
+++ b/server/lib/ecosystemProcessPorts.js
@@ -15,32 +15,31 @@
* 1. Prefer a process on the app's own SURFACE — the process name with a
* trailing role suffix (`-server`, `-ui`, …) stripped. `portos-server` and
* `portos-ui` share surface `portos`; `portos-autofixer-ui` does not.
- * 2. Otherwise accept the first process carrying the label UNLESS it is
- * provably a sibling surface — its surface differs from the app's AND at
- * least one other process in the config shares that surface, which is what
- * makes it a distinct multi-process product surface rather than an
- * unconventionally-named process of this app.
+ * 2. Otherwise accept a carrier only when its name declares no role at all —
+ * a suffixed name like `portos-autofixer-ui` names the surface it serves
+ * and thereby says it is not this app's, while a bare `frontend` beside a
+ * `backend` declares nothing and is still taken as this app's UI, exactly
+ * as before.
*
- * Step 2 is what keeps an app whose processes are named `backend`/`frontend`
- * working exactly as before: `frontend` is not on `backend`'s surface, but
- * nothing else in the config claims surface `frontend`, so its `ui` port is
- * still the app's. A label with no attributable process resolves to `undefined`,
- * which is the signal callers use to fall back to derivation (a served-by-API
- * app's `uiPort` = its `apiPort`).
+ * Where the two are genuinely indistinguishable, ignoring is the safe default:
+ * it costs a 422 the user can act on, whereas attributing costs a silent rewrite
+ * of a sibling's port literal. A label with no attributable process resolves to
+ * `undefined`, which is the signal callers use to fall back to derivation (a
+ * served-by-API app's `uiPort` = its `apiPort`).
*
* Pure and import-free: both the write-back path (`services/appPortConfig.js`)
* and the read/derive path (`services/appListEnrichment.js`) share it so a
* displayed port and a rewritten port can never disagree about whose it is.
*/
+/** A declared port value: a positive integer (0 / null / a string is not a port). */
+const isPort = (value) => Number.isInteger(value) && value > 0;
+
/**
* Role suffixes a process name carries to mark WHICH part of one surface it is.
* Stripping them collapses `portos-server` and `portos-ui` onto surface
* `portos` while leaving `portos-autofixer` (no role suffix) as its own.
*/
-/** A declared port value: a positive integer (0 / null / a string is not a port). */
-const isPort = (value) => Number.isInteger(value) && value > 0;
-
const ROLE_SUFFIXES = ['server', 'api', 'backend', 'ui', 'client', 'web', 'frontend'];
/**
@@ -58,34 +57,37 @@ export function processSurface(name) {
}
/**
- * The process whose surface defines the app. Prefers a name the app record
- * already claims as its own (`processes[]`/`pm2ProcessNames[]`, in order) and
- * that the config actually declares — the record is the only place that knows
- * which of several surfaces in one config is the app. Falls back to the first
- * process carrying an `api` port (the historical primary), then the first
- * process at all.
+ * The process whose surface defines the app: the first process in CONFIG order
+ * that the app record claims and that carries an `api` port, else the first
+ * claimed process, else the same walk over every process when the record claims
+ * none of them.
+ *
+ * CONFIG order, never the record's order — an ecosystem file declares the app's
+ * own process first, while a record's `processes[]`/`pm2ProcessNames[]` is just
+ * the supervised set and may be sorted or reordered. Picking by record order
+ * would let an alphabetically-sorted list (`portos-autofixer` before
+ * `portos-server`) name a sibling as primary and invert the whole attribution.
*
- * The owned-name list is used for ORDER only, never as a membership filter: a
- * record lists every PM2 process it supervises, siblings included (PortOS's own
- * record claims `portos-autofixer-ui`), so filtering by it would re-admit the
- * very process this module exists to exclude.
+ * The record is consulted for MEMBERSHIP only, and it is a weak signal at that:
+ * it claims every process it supervises, siblings included, so it narrows the
+ * field without identifying the app's own surface on its own.
*/
function resolvePrimaryProcess(processes, ownedNames) {
- for (const name of ownedNames || []) {
- const match = processes.find(proc => proc?.name === name);
- if (match) return match;
- }
- return processes.find(proc => isPort(proc?.ports?.api)) || processes[0] || null;
+ const claimed = ownedNames.size > 0
+ ? processes.filter(proc => ownedNames.has(proc?.name))
+ : [];
+ const candidates = claimed.length > 0 ? claimed : processes;
+ return candidates.find(proc => isPort(proc?.ports?.api)) || candidates[0] || null;
}
-/** Process names the app record claims, in declaration order. */
+/** Process names the app record claims. */
function ownedProcessNames(app) {
- const names = [];
+ const names = new Set();
for (const proc of Array.isArray(app?.processes) ? app.processes : []) {
- if (typeof proc?.name === 'string') names.push(proc.name);
+ if (typeof proc?.name === 'string') names.add(proc.name);
}
for (const name of Array.isArray(app?.pm2ProcessNames) ? app.pm2ProcessNames : []) {
- if (typeof name === 'string') names.push(name);
+ if (typeof name === 'string') names.add(name);
}
return names;
}
@@ -98,7 +100,7 @@ function ownedProcessNames(app) {
* @param {Array<{name?: string, ports?: Record<string, number>}>} processes
* parsed ecosystem processes (or an app record's `processes[]` — same shape)
* @param {object} [app] the app record, consulted only for which process names
- * it owns; omit it and the primary falls back to the first `api` process
+ * it claims; omit it and the primary is resolved over every process
* @returns {{ ports: Record<string, number|undefined>, processNames: Record<string, string|undefined> }}
* `ports[label]` is undefined when no process can be attributed the label;
* `processNames[label]` names the process the value came from (what a targeted
@@ -113,22 +115,19 @@ export function attributeProcessPorts(processes, app) {
const primary = resolvePrimaryProcess(procs, ownedProcessNames(app));
const appSurface = processSurface(primary?.name);
- // Surfaces with two or more processes of their own are distinct product
- // surfaces in this config — that is the evidence needed to IGNORE one of their
- // labels rather than mis-attribute it to the app.
- const surfaceCounts = new Map();
- for (const proc of procs) {
- const surface = processSurface(proc.name);
- surfaceCounts.set(surface, (surfaceCounts.get(surface) || 0) + 1);
- }
-
for (const label of ['api', 'ui', 'devUi']) {
const carriers = procs.filter(proc => isPort(proc.ports?.[label]));
- const owned = carriers.find(proc => processSurface(proc.name) === appSurface);
- // No process on the app's own surface declares this label: accept the first
- // carrier that is not provably a sibling surface (step 2 above).
- const attributed = owned
- || carriers.find(proc => (surfaceCounts.get(processSurface(proc.name)) || 0) < 2);
+ const attributed = carriers.find(proc => processSurface(proc.name) === appSurface)
+ // No process on the app's own surface declares this label. Accept a
+ // carrier only when its name does NOT declare a role on some other
+ // surface: `portos-autofixer-ui` names the surface it serves and says it
+ // is not this app's, so it is ignored (the label then resolves to
+ // undefined and the served-by-API derivation takes over), while a
+ // `frontend` beside a `backend` strips to no role at all and is still
+ // taken as this app's UI, exactly as before. Where the two are genuinely
+ // indistinguishable, ignoring costs a 422 the user can act on; attributing
+ // costs a silent rewrite of a sibling's port literal.
+ || carriers.find(proc => processSurface(proc.name) === proc.name);
if (!attributed) continue;
ports[label] = attributed.ports[label];
processNames[label] = attributed.name;
@@ -160,13 +159,23 @@ export function findCrossProcessPortCollision(processes, edits) {
// Post-edit view: an edited label vacates its old value, so an edit that moves
// a port out of the way cannot collide with the value it just released.
const claims = [];
+ const claimed = new Set();
for (const proc of procs) {
for (const [label, port] of Object.entries(proc.ports || {})) {
if (!isPort(port)) continue;
const edit = (edits || []).find(e => e?.processName === proc.name && e?.label === label);
claims.push({ processName: proc.name, label, port: edit ? edit.newPort : port });
+ claimed.add(`${proc.name}\u0000${label}`);
}
}
+ // An edit can also introduce a label the process does not declare yet. Seed
+ // those too, so two edits that would hand the same new port to two different
+ // processes still collide with each other.
+ for (const edit of edits || []) {
+ if (!edit || !isPort(edit.newPort)) continue;
+ if (claimed.has(`${edit.processName}\u0000${edit.label}`)) continue;
+ claims.push({ processName: edit.processName, label: edit.label, port: edit.newPort });
+ }
for (const edit of edits || []) {
if (!edit || !isPort(edit.newPort)) continue;
diff --git a/server/services/appPortConfig.test.js b/server/services/appPortConfig.test.js
index 5b47cf9059..fca5d82c0f 100644
--- a/server/services/appPortConfig.test.js
+++ b/server/services/appPortConfig.test.js
@@ -37,6 +37,25 @@ module.exports = {
};
`;
+/**
+ * Same defect, one sibling process instead of a pair: `admin-ui` owns the only
+ * `ports.ui` and has no `admin` sibling to prove it is a separate surface.
+ */
+const SOLO_SIBLING_UI_CONFIG = `const PORTS = {
+ API: 6000, // Example API server
+ UI: 6001, // Vite dev server
+ ADMIN: 6004, // Standalone admin console
+};
+
+module.exports = {
+ apps: [
+ { name: 'example-server', script: 'server/index.js', env: { PORT: PORTS.API } },
+ { name: 'example-ui', script: 'npm', args: 'run dev', env: { VITE_PORT: PORTS.UI } },
+ { name: 'admin-ui', script: 'admin/index.js', env: { PORT: PORTS.ADMIN } }
+ ]
+};
+`;
+
/** Two-process app with unconventional names — neither shares a surface. */
const UNCONVENTIONAL_CONFIG = `const PORTS = {
API: 7000,
@@ -134,6 +153,45 @@ describe('applyEcosystemPortEdits', () => {
expect(after).toContain('UI: 6001,');
});
+
+ it('picks the app\'s own surface even when the record lists sibling processes first', async () => {
+ // An app record claims every process it supervises, siblings included, and
+ // nothing guarantees the app's own process is listed first (a sorted list
+ // puts `example-helper` before `example-server`). The primary is resolved in
+ // CONFIG order for that reason — reading it from the record's order here
+ // would name the helper as primary and invert the whole attribution,
+ // recreating #7357.
+ const before = await writeConfig(SIBLING_UI_CONFIG);
+ const existing = {
+ id: 'example',
+ name: 'Example',
+ repoPath,
+ type: 'node',
+ pm2ProcessNames: ['example-helper', 'example-helper-ui', 'example-server', 'example-ui'],
+ };
+
+ const result = await applyEcosystemPortEdits(existing, { apiPort: 6000, uiPort: 6000, devUiPort: 6001 });
+
+ expect(await readConfig()).toBe(before);
+ expect(result.changedKeys).toEqual([]);
+ expect(result.uiPortOverride).toBe(6000);
+ });
+
+ it('ignores a single-process sibling whose name declares another surface\'s role', async () => {
+ // `admin-ui` is the only `ports.ui` in the file and has no sibling of its
+ // own, so a "is this surface multi-process?" test would read it as this
+ // app's UI and rewrite its literal. The name itself is the evidence: it
+ // declares a role on surface `admin`, which is not this app's.
+ const before = await writeConfig(SOLO_SIBLING_UI_CONFIG);
+ const existing = { id: 'example', name: 'Example', repoPath, type: 'node', processes: [{ name: 'example-server' }, { name: 'example-ui' }] };
+
+ const result = await applyEcosystemPortEdits(existing, { apiPort: 6000, uiPort: 6000, devUiPort: 6001 });
+
+ expect(await readConfig()).toBe(before);
+ expect(result.changedKeys).toEqual([]);
+ expect(result.uiPortOverride).toBe(6000);
+ });
+
it('still attributes a UI port to a process that shares no naming convention with the primary', async () => {
// `frontend` is not on `backend`'s surface, but nothing else in the config
// claims surface `frontend` — so it is this app's UI, not a sibling's, and a