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
fix: mirror both PostgreSQL ports in server/lib/ports.js with a drift test (#3911)
[._.]/ Adam Eivy163c932c7e57
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex c4f07cd8d7..8b04c36d15 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -39,6 +39,7 @@## Changed- The series arc verifier can now see the world canon it was already judging against. A Universe Builder world keeps its factions, locations, vehicles, artifacts and interfaces as category variations; the named-canon trunks the verify prompt rendered stay empty until prose mints entities into them. So the verifier was handed a block labelled "world canon" that omitted the entire built world, and reported locked faction cards as invented and "absent from the World Canon" — findings no arc edit can ever close. That is a stall rather than a nuisance: the foundation gate's structure repair reverts its whole rewrite whenever verification leaves any blocker, so one permanent false positive discards every real fix beside it and the autopilot pauses. The sibling arc-resolve and volume-verify prompts already rendered this block, which is exactly why the verify/resolve pair could never converge. The arc-verify prompt now renders the category and composite canon, states that a listed entity is grounded, and a new contract test holds the pair to one world — plus the field-level half of the same class, asserting that every record field the checklist cites is one the context actually renders.+- `server/lib/ports.js` — the ESM mirror of the `PORTS` object in `ecosystem.config.cjs` — was missing both PostgreSQL ports, so server code reaching for a database port found nothing to import and hardcoded 5432/5561 by hand. The mirror now carries `POSTGRES_DOCKER` and `POSTGRES_NATIVE`, with the config's mode-dependent `POSTGRES` exposed as `resolvePostgresPort(pgMode)` rather than a constant, since resolving it eagerly would mean reading `.env` from a module nearly every server file imports. A new drift test compares the mirror against the config key by key and fails if either side gains, loses, or renumbers a port.- The three collapsible result sections in the digital-twin list enrichment view — Item-by-Item Analysis, Patterns Detected and Personality Insights — now announce whether they are open. Their headers were plain `<button>`s whose only expanded/collapsed signal was the chevron glyph swapping between down and right, so a screen reader read the same label before and after activation with no state to report. Each header now carries `aria-expanded` bound to its own visibility flag and `aria-controls` pointing at the panel it toggles, and the panels carry the matching ids.diff --git a/docs/PORTS.md b/docs/PORTS.mdindex a03358996c..e2d74043b2 100644--- a/docs/PORTS.md+++ b/docs/PORTS.md@@ -7,7 +7,7 @@ PortOS uses a contiguous port allocation scheme to make it easy to understand wh### Convention1. **Contiguous Ranges**: Each app should use a contiguous block of ports-2. **Labeled Ports**: Define all ports in the top-level `PORTS` object in `ecosystem.config.cjs` (mirrored — manually kept in sync — in `server/lib/ports.js`, since the ESM server can't `require()` the CommonJS config); the per-process label map lives in `server/services/apps.js`+2. **Labeled Ports**: Define all ports in the top-level `PORTS` object in `ecosystem.config.cjs` (mirrored — manually kept in sync — in `server/lib/ports.js`, since the ESM server can't `require()` the CommonJS config); the per-process label map lives in `server/services/apps.js`. The mirror carries every port literal, including both PostgreSQL ports; the config's mode-dependent `POSTGRES` (resolved from `PGMODE` at load time) is exposed in the mirror as `resolvePostgresPort(pgMode)` over the `POSTGRES_NATIVE` / `POSTGRES_DOCKER` literals, so `server/lib/ports.js` stays free of filesystem reads. `server/lib/ports.test.js` fails if the two drift apart3. **No Gaps**: Avoid leaving gaps between port allocations within an app### Port Labelsdiff --git a/server/lib/ports.js b/server/lib/ports.jsindex 5aee24deee..233af4ce4f 100644--- a/server/lib/ports.js+++ b/server/lib/ports.js@@ -13,8 +13,18 @@ export const PORTS = {CDP_HEALTH: 5557, // Browser health check endpointCOS: 5558, // Chief of Staff agent runner (portos-cos)AUTOFIXER: 5559, // Autofixer API- AUTOFIXER_UI: 5560 // Autofixer UI+ AUTOFIXER_UI: 5560, // Autofixer UI+ POSTGRES_DOCKER: 5561, // PostgreSQL Docker container (host port mapping)+ POSTGRES_NATIVE: 5432 // System PostgreSQL (PGMODE=native)};++// The ecosystem config resolves a single active `PORTS.POSTGRES` by reading+// PGMODE out of .env at load time. This module stays free of filesystem reads+// (it is imported by nearly every server module), so the mode-dependent value is+// a function instead of a constant — callers pass the mode they already know.+export const resolvePostgresPort = (pgMode) =>+ (pgMode === 'native' ? PORTS.POSTGRES_NATIVE : PORTS.POSTGRES_DOCKER);+export const DEFAULT_PEER_PORT = PORTS.API;export const PORTOS_UI_URL = process.env.PORTOS_UI_URL|| `http://${process.env.PORTOS_HOST || 'localhost'}:${process.env.PORT_UI || PORTS.UI}`;diff --git a/server/lib/ports.test.js b/server/lib/ports.test.jsnew file mode 100644index 0000000000..2256f8f0fa--- /dev/null+++ b/server/lib/ports.test.js@@ -0,0 +1,68 @@+import { describe, it, expect } from 'vitest';+import { createRequire } from 'module';+import { readFileSync } from 'fs';+import { fileURLToPath } from 'url';+import path from 'path';+import { PORTS, resolvePostgresPort } from './ports.js';++// `ecosystem.config.cjs` is the source of truth for port numbers; `ports.js` is a+// hand-maintained ESM mirror of it (the ESM server can't require() the CJS+// config). These tests fail when the two drift apart — see docs/PORTS.md.+const require = createRequire(import.meta.url);+const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');+const ecosystemPath = path.join(repoRoot, 'ecosystem.config.cjs');+const { PORTS: ECOSYSTEM_PORTS } = require(ecosystemPath);++// `POSTGRES` is resolved from PGMODE at config-load time, so it has no single+// literal counterpart in the mirror — it's covered by its own assertions below.+const RESOLVED_ONLY = ['POSTGRES'];+// The mirror keeps both PostgreSQL literals so callers can resolve either mode.+const MIRROR_ONLY = ['POSTGRES_NATIVE'];++describe('PORTS mirror of ecosystem.config.cjs', () => {+ it('mirrors every fixed port from the ecosystem config', () => {+ const expected = Object.fromEntries(+ Object.entries(ECOSYSTEM_PORTS).filter(([key]) => !RESOLVED_ONLY.includes(key))+ );+ const actual = Object.fromEntries(+ Object.entries(PORTS).filter(([key]) => !MIRROR_ONLY.includes(key))+ );+ expect(actual).toEqual(expected);+ });++ it('has no extra ports beyond the ecosystem config', () => {+ const extras = Object.keys(PORTS).filter(+ (key) => !(key in ECOSYSTEM_PORTS) && !MIRROR_ONLY.includes(key)+ );+ expect(extras).toEqual([]);+ });++ it('resolves the active PostgreSQL port to one of the mirrored literals', () => {+ expect([PORTS.POSTGRES_NATIVE, PORTS.POSTGRES_DOCKER]).toContain(ECOSYSTEM_PORTS.POSTGRES);+ });++ it('keeps the native PostgreSQL literal in sync with the config source', () => {+ // The native port only appears inside the config's PGMODE ternary, so there is+ // no exported constant to compare against — assert against the source line.+ const source = readFileSync(ecosystemPath, 'utf8');+ const postgresLine = source.split('\n').find((line) => /^\s*POSTGRES:/.test(line));+ expect(postgresLine).toBeTruthy();+ expect(postgresLine).toContain(String(PORTS.POSTGRES_NATIVE));+ expect(postgresLine).toContain(String(PORTS.POSTGRES_DOCKER));+ });+});++describe('resolvePostgresPort', () => {+ it('returns the native port for PGMODE=native', () => {+ expect(resolvePostgresPort('native')).toBe(5432);+ });++ it('returns the Docker port for PGMODE=docker', () => {+ expect(resolvePostgresPort('docker')).toBe(5561);+ });++ it('defaults to the Docker port when the mode is unset or unknown', () => {+ expect(resolvePostgresPort(undefined)).toBe(5561);+ expect(resolvePostgresPort('file')).toBe(5561);+ });+});