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): accept UI-follows-new-API saves; scope label keys to inline ports

Adam Eivy46472f1dbab2
diff --git a/server/routes/apps.js b/server/routes/apps.js
index d78bfdc68f..3fb436af9d 100644
--- a/server/routes/apps.js
+++ b/server/routes/apps.js
@@ -512,17 +512,19 @@ router.put('/:id', asyncHandler(async (req, res, next) => {
// Derived-uiPort guard. When an app has an API process plus a Vite dev UI
// but no literal `ports.ui`, the prod UI is served by the API and the
// displayed `uiPort` is *derived* (= apiPort). There's no independent ui
- // literal to rewrite, so a uiPort edit to anything OTHER than that derived
- // value can't persist — the modal echoes the derived value unchanged on a
- // rename (a no-op), but a genuine change must be rejected rather than
- // silently stored in apps.json only to revert on the next config refresh.
- // The user changes this port by editing the API port instead.
- const derivedUiPort = deriveUiPort(currentPort.uiPort, currentPort.apiPort, currentPort.devUiPort);
+ // literal to rewrite, so a uiPort edit can't persist on its own. Compare the
+ // submitted uiPort against the NEW derived value — i.e. the API port AFTER
+ // this same request's apiPort change (if any), so a valid combined save like
+ // `apiPort: 6000→7000, uiPort: 7000` (UI still following the API) is accepted
+ // rather than rejected against the stale old value. Only a uiPort that
+ // diverges from the new API port is a genuine independent change → 422 (the
+ // user must change the API port instead). The modal echoes the derived value
+ // on every save; that echo is stripped below so it's never stored explicitly.
+ const effectiveApiPort = Number.isInteger(data.apiPort) ? data.apiPort : currentPort.apiPort;
+ const derivedUiPort = deriveUiPort(undefined, effectiveApiPort, currentPort.devUiPort);
+ const uiIsDerived = currentPort.uiPort === undefined && Number.isInteger(derivedUiPort);
const derivedUiChangeRejected =
- currentPort.uiPort === undefined &&
- Number.isInteger(derivedUiPort) &&
- Number.isInteger(data.uiPort) &&
- data.uiPort !== derivedUiPort;
+ uiIsDerived && Number.isInteger(data.uiPort) && data.uiPort !== derivedUiPort;
const remap = [];
const targetedEdits = []; // shared-value keys: rewritten by process + label
for (const key of changedKeys) {
@@ -555,7 +557,7 @@ router.put('/:id', asyncHandler(async (req, res, next) => {
// apiPort change (e.g. API 6000→7000 must re-derive uiPort 7000, not keep a
// stored 6000). Any genuine change was already rejected above, so a
// remaining uiPort here is the harmless echo — strip it.
- if (currentPort.uiPort === undefined && Number.isInteger(derivedUiPort) && 'uiPort' in data) {
+ if (uiIsDerived && 'uiPort' in data) {
delete data.uiPort;
}
diff --git a/server/routes/apps.test.js b/server/routes/apps.test.js
index ef71d0739b..a3020c390e 100644
--- a/server/routes/apps.test.js
+++ b/server/routes/apps.test.js
@@ -398,11 +398,12 @@ describe('Apps Routes', () => {
expect(updateArg.name).toBe('Renamed');
});
- it('changing apiPort on a served-by-API app persists the API port and drops the echoed derived uiPort', async () => {
- // The real bug: API 6000→7000 with the modal echoing the OLD derived
- // uiPort 6000. The apiPort edit must persist via the value-keyed rewrite,
- // and the echoed uiPort must be stripped so it re-derives to 7000 (not
- // frozen at 6000).
+ it('changing apiPort on a served-by-API app (UI following the new API port) persists the API port and drops the echoed uiPort', async () => {
+ // Valid combined save: API 6000→7000 with the UI following to 7000 (the
+ // derived port tracks the new API port). The apiPort edit persists via the
+ // value-keyed rewrite; the echoed uiPort is compared against the NEW
+ // derived value (7000), accepted, and stripped so it re-derives from the
+ // new apiPort rather than being frozen as an explicit field.
const mockApp = { id: 'app-001', name: 'App', type: 'node', repoPath: process.cwd(), apiPort: 6000, uiPort: 6000, devUiPort: 5556 };
appsService.getAppById.mockResolvedValue(mockApp);
streamingDetect.parseEcosystemFromPath.mockResolvedValue({
@@ -412,10 +413,10 @@ describe('Apps Routes', () => {
const response = await request(app)
.put('/api/apps/app-001')
- .send({ apiPort: 7000, uiPort: 6000 });
+ .send({ apiPort: 7000, uiPort: 7000 });
expect(response.status).toBe(200);
- // apiPort (distinct, value-keyed) persisted; uiPort echo not in the remap.
+ // apiPort (distinct, value-keyed) persisted; uiPort follows → not in remap.
expect(streamingDetect.writeEcosystemPortEdits).toHaveBeenCalledWith(
process.cwd(),
[[6000, 7000]],
@@ -426,6 +427,25 @@ describe('Apps Routes', () => {
expect(updateArg.apiPort).toBe(7000);
});
+ it('rejects (422) a served-by-API uiPort that diverges from the new API port', async () => {
+ // apiPort→7000 but uiPort sent as 6000: the user is asking for a UI port
+ // that does NOT follow the API port, which a served-by-API app can't do.
+ // Compared against the NEW derived value (7000), 6000 diverges → 422.
+ const mockApp = { id: 'app-001', name: 'App', type: 'node', repoPath: process.cwd(), apiPort: 6000, uiPort: 6000, devUiPort: 5556 };
+ appsService.getAppById.mockResolvedValue(mockApp);
+ streamingDetect.parseEcosystemFromPath.mockResolvedValue({
+ processes: [{ name: 'srv', ports: { api: 6000, devUi: 5556 } }]
+ });
+
+ const response = await request(app)
+ .put('/api/apps/app-001')
+ .send({ apiPort: 7000, uiPort: 6000 });
+
+ expect(response.status).toBe(422);
+ expect(streamingDetect.writeEcosystemPortEdits).not.toHaveBeenCalled();
+ expect(appsService.updateApp).not.toHaveBeenCalled();
+ });
+
it('does not reject a non-port edit on an app whose top-level ports are not stored (derived)', async () => {
// apps.json has no apiPort/uiPort (derived from processes); a rename
// submits the echoed display values. Those must not read as port changes.
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index 5f096943b2..42f43312fe 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -599,9 +599,15 @@ function rewriteLabelInBlock(block, label, oldP, newP) {
// `PORT`/`'PORT'`/`` `PORT` `` followed by `:` and optional whitespace.
const PORT_KEY = "['\"`]?\\bPORT\\b['\"`]?\\s*:\\s*";
+ // The `api:`/`ui:`/`devUi:` label-key patterns are only valid inside an
+ // INLINE `ports: { ... }` object — that's the only place parseEcosystemConfig
+ // reads them. In a `ports: PORTS.x` reference block (or a bare env/args
+ // block) an unscoped `api: N` could match a same-valued lowercase metadata
+ // field and falsely report applied while the real (external/env) source is
+ // untouched, so the label-key patterns are gated on hasInlinePortsObj.
const pats = [];
if (label === 'api') {
- pats.push(`(\\bapi\\s*:\\s*)${NUM}`); // ports: { api: N }
+ if (hasInlinePortsObj) 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
@@ -609,8 +615,10 @@ function rewriteLabelInBlock(block, label, oldP, newP) {
}
} else {
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
+ if (hasInlinePortsObj) {
+ 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
+ }
if (!hasPortsReference) {
// VITE_PORT/--port mirror the runtime UI/dev port: rewrite them alongside
// an inline ports key (desirable — config + runtime move together) or as
diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.js
index 024056e023..9e828c25bc 100644
--- a/server/services/streamingDetect.test.js
+++ b/server/services/streamingDetect.test.js
@@ -542,6 +542,25 @@ module.exports = { apps: [
expect(ports.devUi).toBe(6000); // sibling untouched
});
+ it('does NOT match a same-valued lowercase metadata field in a ports-reference block (no false api success)', () => {
+ // api is derived from `ports: PORTS.server` (external const). The block also
+ // carries a lowercase metadata field that happens to share the value. An
+ // unscoped `api:`/`ui:` key pattern would rewrite that metadata and falsely
+ // report applied while the real const reverts — so label-key patterns are
+ // scoped to inline ports objects only → this edit is unapplied.
+ const content = `const PORTS = { server: { api: 6000 } };
+module.exports = { apps: [
+ { name: 'srv', script: 's.js', ports: PORTS.server, max_restarts: 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); // metadata field untouched
+ });
+
it('does NOT report success from an env-only ui rewrite when the ui port comes from a ports reference', () => {
// ui is derived from `ports: PORTS.client` (external const), but the block
// ALSO carries a same-valued VITE_PORT. Rewriting only VITE_PORT would be