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
refactor([issue-1842]): name the CoS runner port and complete the ports.js mirror
[._.]/ Adam Eivy7eb69db6d433
diff --git a/server/cos-runner/index.js b/server/cos-runner/index.jsindex 194a52f7be..d10824b59d 100644--- a/server/cos-runner/index.js+++ b/server/cos-runner/index.js@@ -21,6 +21,13 @@ import { createStreamJsonParser } from './streamJsonParser.js';import { loadState, saveState, withState } from './runnerState.js';import { getProcessStats, checkProcessRunning } from './processStats.js';import { ALLOWED_COMMANDS, isAllowedCommand } from './allowedCommands.js';+import { PORTS } from '../lib/ports.js';++// Timing constants for process termination/cleanup (ms). Named so the grace+// windows are obvious at each call site instead of bare literals.+const SIGKILL_GRACE_MS = 5000; // wait after SIGTERM before forcing SIGKILL+const ORPHAN_CLEANUP_DELAY_MS = 3000; // delay on boot before reaping orphaned agents+const SHUTDOWN_DRAIN_MS = 5000; // SIGTERM drain window before closing the server// Path + listen constants. These lived adjacent to the command allowlist before// it was extracted to ./allowedCommands.js; they must stay here — they're@@ -30,7 +37,7 @@ const ROOT_DIR = PATHS.root;const AGENTS_DIR = PATHS.cosAgents;const RUNS_DIR = PATHS.runs;-const PORT = process.env.PORT || 5558;+const PORT = process.env.PORT || PORTS.COS;const HOST = process.env.HOST || '127.0.0.1';// Active agent processes (in memory)@@ -387,13 +394,13 @@ app.post('/terminate/:agentId', (req, res) => {agent.process.kill('SIGTERM');// Force kill after timeout; store handle so it can be cancelled if the- // process exits cleanly before the 5s window expires.+ // process exits cleanly before the grace window expires.agent.killTimer = setTimeout(() => {if (activeAgents.has(agentId)) {agent.process.kill('SIGKILL');activeAgents.delete(agentId);}- }, 5000);+ }, SIGKILL_GRACE_MS);res.json({ success: true, agentId });});@@ -451,7 +458,7 @@ app.post('/pause/:agentId', async (req, res) => {agent.killTimer = setTimeout(() => {const current = activeAgents.get(agentId);if (current?.paused) current.process.kill('SIGKILL');- }, 5000);+ }, SIGKILL_GRACE_MS);res.json({ success: true, agentId, pid: agent.pid, pausedAt });});@@ -475,7 +482,7 @@ app.post('/terminate-all', async (req, res) => {agent.process.kill('SIGKILL');activeAgents.delete(agentId);}- }, 5000);+ }, SIGKILL_GRACE_MS);}}@@ -737,7 +744,7 @@ app.post('/runs/:runId/stop', (req, res) => {run.process.kill('SIGKILL');activeRuns.delete(runId);}- }, 5000);+ }, SIGKILL_GRACE_MS);res.json({ success: true, runId });});@@ -812,7 +819,7 @@ server.listen(PORT, HOST, async () => {if (orphaned.length > 0) {console.log(`🧹 Cleaned ${orphaned.length} orphaned agent(s)`);}- }, 3000);+ }, ORPHAN_CLEANUP_DELAY_MS);});// Graceful shutdown@@ -826,7 +833,7 @@ process.on('SIGTERM', async () => {}// Wait for agents to terminate- await new Promise(resolve => setTimeout(resolve, 5000));+ await new Promise(resolve => setTimeout(resolve, SHUTDOWN_DRAIN_MS));server.close(() => {console.log('👋 CoS Agent Runner stopped');diff --git a/server/lib/ports.js b/server/lib/ports.jsindex 78b6333819..5aee24deee 100644--- a/server/lib/ports.js+++ b/server/lib/ports.js@@ -1,3 +1,6 @@+// Importable mirror of the `PORTS` object in ecosystem.config.cjs (the source of+// truth — see docs/PORTS.md). ESM server code can't require() the CommonJS+// ecosystem config, so these literals are duplicated here and must stay in sync.export const PORTS = {API: 5555, // HTTPS API (or HTTP if cert not configured)API_LOCAL: 5553, // Loopback-only HTTP mirror — only binds when HTTPS is active on API.@@ -5,7 +8,12 @@ export const PORTS = {// https://localhost:5555 trips a warning; this sibling port// serves the same app over plain HTTP for local dev.// Overridable via PORTOS_HTTP_PORT.- UI: 5554 // Vite dev server+ UI: 5554, // Vite dev server+ CDP: 5556, // Chrome DevTools Protocol (browser automation)+ CDP_HEALTH: 5557, // Browser health check endpoint+ COS: 5558, // Chief of Staff agent runner (portos-cos)+ AUTOFIXER: 5559, // Autofixer API+ AUTOFIXER_UI: 5560 // Autofixer UI};export const DEFAULT_PEER_PORT = PORTS.API;export const PORTOS_UI_URL = process.env.PORTOS_UI_URLdiff --git a/server/services/apps.js b/server/services/apps.jsindex ee0f1ff6b4..f9c7f3a486 100644--- a/server/services/apps.js+++ b/server/services/apps.js@@ -48,11 +48,11 @@ function buildPortosApp() {// on API. If no cert is present, don't advertise api-local — nothing is listening// there and Overview would otherwise show a dead port.{ name: 'portos-server', port: PORTS.API, ports: certPresent ? { api: PORTS.API, 'api-local': PORTS.API_LOCAL } : { api: PORTS.API } },- { name: 'portos-cos', port: 5558, ports: { api: 5558 } },+ { name: 'portos-cos', port: PORTS.COS, ports: { api: PORTS.COS } },{ name: 'portos-ui', port: PORTS.UI, ports: { devUi: PORTS.UI } },- { name: 'portos-autofixer', port: 5559, ports: { api: 5559 } },- { name: 'portos-autofixer-ui', port: 5560, ports: { ui: 5560 } },- { name: 'portos-browser', port: 5556, ports: { cdp: 5556, health: 5557 } }+ { name: 'portos-autofixer', port: PORTS.AUTOFIXER, ports: { api: PORTS.AUTOFIXER } },+ { name: 'portos-autofixer-ui', port: PORTS.AUTOFIXER_UI, ports: { ui: PORTS.AUTOFIXER_UI } },+ { name: 'portos-browser', port: PORTS.CDP, ports: { cdp: PORTS.CDP, health: PORTS.CDP_HEALTH } }],envFile: '.env',icon: 'portos',diff --git a/server/services/cosRunnerClient.js b/server/services/cosRunnerClient.jsindex 0e01672d79..7d398264e0 100644--- a/server/services/cosRunnerClient.js+++ b/server/services/cosRunnerClient.js@@ -8,8 +8,9 @@import { io } from 'socket.io-client';import { fetchWithTimeout } from '../lib/fetchWithTimeout.js';import { readResponseJson } from '../lib/readResponseJson.js';+import { PORTS } from '../lib/ports.js';-const COS_RUNNER_URL = process.env.COS_RUNNER_URL || 'http://localhost:5558';+const COS_RUNNER_URL = process.env.COS_RUNNER_URL || `http://localhost:${PORTS.COS}`;/*** Read a runner response body as JSON, tolerating a non-JSON body.