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
feat([issue-2678]): expose instance identity (name/authRequired/scheme) pre-auth on /api/system/health
[._.]/ Adam Eivy85213df51cfc
diff --git a/docs/API.md b/docs/API.mdindex cfaf63163b..1aff5473bc 100644--- a/docs/API.md+++ b/docs/API.md@@ -12,6 +12,8 @@ When a TLS cert is provisioned (`npm run setup:cert`), `:5555` serves HTTPS instThis document covers the most commonly used endpoints plus a [complete route-domain index](#route-domain-index). A machine-readable OpenAPI 3.1 spec for the public API surface is served at `GET /api/api-docs/openapi.json` and rendered in the UI at `/api-access`.+Building a native companion client? See [COMPANION_APP_API.md](./COMPANION_APP_API.md) — the stable, pre-auth-discoverable contract (discovery/identity, HTTP Basic auth, instance management, palette actions, daily-log, POST progress, and the iCloud-sync precedent) that the PortDeck app consumes.+## Security ModelPortOS is designed for personal/developer use on trusted networks. It implements the following security measures:@@ -488,8 +490,8 @@ Every mounted API prefix (see `server/index.js` for the authoritative list). Dom| `/api/datadog`, `/api/jira`, `/api/github`, `/api/telegram` | External integrations || `/api/health` | Health check || `/api/insights` | Cross-domain insights |-| `/api/instances`, `/api/sync`, `/api/peer-sync`, `/api/sharing` | Federation / peer sync |-| `/api/mortalloom` | MortalLoom |+| `/api/instances`, `/api/sync`, `/api/peer-sync`, `/api/sharing` | Federation / peer sync (see [COMPANION_APP_API.md](./COMPANION_APP_API.md)) |+| `/api/mortalloom` | MortalLoom (iCloud-JSON sync precedent) || `/api/review` | Review queue || `/api/settings` | App settings || `/api/update` | Self-update flow |diff --git a/docs/COMPANION_APP_API.md b/docs/COMPANION_APP_API.mdnew file mode 100644index 0000000000..34bf3a7b70--- /dev/null+++ b/docs/COMPANION_APP_API.md@@ -0,0 +1,197 @@+# Companion-App API Contract++The stable HTTP contract a native companion client (working name **PortDeck**;+repo `atomantic/PortDeck`) consumes to discover, authenticate to, and drive one+or more PortOS instances across a Tailscale tailnet.++> **Scope.** This documents the **PortOS-side** contract only. The iOS app itself+> (Swift/SwiftUI, Keychain, iCloud store, UI) lives in its own repository per the+> Scope Boundary rule in `CLAUDE.md` — its code, plan, and docs never land in this+> repo. Everything below already exists in PortOS today unless explicitly marked.++## Deployment shape the app targets++A single user commonly runs **several PortOS installs federated as sync peers**+over Tailscale. Each install:++- Serves its API on **`:5555`** at the tailnet host (MagicDNS name or Tailscale IP).+- Speaks **HTTP or HTTPS** depending on whether a TLS cert is provisioned+ (`npm run setup:cert`). When HTTPS is on, `:5555` is TLS-only and a loopback HTTP+ mirror runs on `127.0.0.1:5553` (not reachable over the tailnet). See+ [PORTS.md](./PORTS.md).+- Has an **optional single password** gate. When off, the tailnet-private trust+ model means the app needs no credential; when on, every `/api/*` request needs+ credentials (see [Authentication](#authentication)).++The app therefore treats each instance as `{ scheme, host, port: 5555, password? }`+and must handle both the auth-on/auth-off and HTTP/HTTPS cases per instance.++## 1. Discovery & identity (pre-auth)++`GET /api/system/health` — **public**, bypasses the auth gate even when the+password is on (`PUBLIC_API_PATHS` in `server/lib/authGate.js`), and is the same+endpoint Tailscale reachability checks hit. Use it to confirm a tailnet host is a+PortOS instance and to label it on a connection screen **before** the app holds+any credential.++**Response:**++```json+{+ "status": "ok",+ "timestamp": "2026-07-16T00:00:00.000Z",+ "uptime": 12345.6,+ "version": "1.2.3",+ "hostname": "host-XXXX",+ "instanceId": "3f2a…-uuid",+ "name": "Example Instance",+ "authRequired": true,+ "scheme": "https"+}+```++| Field | Meaning |+|-------|---------|+| `instanceId` | Stable per-install UUID (`crypto.randomUUID()`, persisted in `data/instances.json`). The identity key the app stores per connection; survives hostname changes. `null` before the self-identity is first created. |+| `name` | User-set display name for the instance (`self.name`), falling back to `hostname` when unset. This is what to show in the instance list. |+| `hostname` | OS hostname of the machine. |+| `authRequired` | `true` when the password gate is on — the app must obtain a password and send it on subsequent requests. `false` when off — no credential needed. Lets the app decide whether to prompt **without** a second round-trip to `/api/auth/status`. Fails **closed** (reports `true`) if the auth state can't be read. |+| `scheme` | `"http"` or `"https"` — the scheme `:5555` serves, decided once at boot. Use it to build request URLs and label the connection's security. |+| `version` | PortOS release the instance is running — useful for compatibility gating. |++These fields are additive and non-sensitive: exposing `name`/`hostname`/+`instanceId` to tailnet peers is within the trust model. No mutation or config+route is exposed pre-auth.++## 2. Authentication++PortOS auth is a single optional password (`server/services/auth.js`,+`server/lib/authGate.js`). The app authenticates as a **full session via HTTP+Basic**, reusing the exact path peer-to-peer federation uses — nothing new to+build server-side.++- **When `authRequired` is `false`:** send no credential. All `/api/*` routes are+ open on the tailnet.+- **When `authRequired` is `true`:** send+ `Authorization: Basic base64(":" + password)` on every `/api/*` request. PortOS+ is single-user, so the username half is ignored — only the password is verified.+ Store the password **per instance in the iOS Keychain**.++**CSRF note.** When auth is on, PortOS 403s cross-origin requests that carry an+`Origin` header (the browser-session CSRF guard). A native `URLSession` sends **no**+`Origin` header, so it passes the guard cleanly — no special handling needed. Do+not set an `Origin` header manually.++A dedicated per-device API-key surface (a companion group in `apiRegistry.js` /+long-lived device token) is a **possible future enhancement**, not built here —+the single-password Basic posture works today and reuses the whole gate.++## 3. Instance management++Full CRUD + peer operations at `/api/instances/*` (`server/routes/instances.js`,+`server/services/instances.js`). The foundation the app's instance-management UI+builds on:++| Method | Path | Purpose |+|--------|------|---------|+| `GET` | `/api/instances` | Self + all configured peers. |+| `GET` | `/api/instances/self` | This instance's identity (`instanceId`, `name`). |+| `PUT` | `/api/instances/self` | Rename this instance. |+| `GET` | `/api/instances/tailnet-suffix` | The tailnet's MagicDNS suffix. |+| `GET` | `/api/instances/sync-status` | Federation sync status. |+| `POST` | `/api/instances/peers` | Add a peer. |+| `PUT` / `DELETE` | `/api/instances/peers/:id` | Update / remove a peer. |+| `POST` | `/api/instances/peers/:id/connect` | Establish a peer connection. |+| `POST` | `/api/instances/peers/:id/reciprocate` | Reciprocate a peer connection. |+| `POST` | `/api/instances/peers/:id/probe` | Probe a peer's reachability. |+| `POST` | `/api/instances/peers/:id/sync` | Trigger a sync with a peer. |+| `GET` | `/api/instances/peers/:id/query?path=/api/…` | Proxy a request through a peer. |++## 4. Quick actions, brain capture & daily log — the palette bridge++Non-DOM voice/palette actions are dispatchable over plain HTTP via the command+palette bridge (`server/routes/palette.js`) — the app drives these directly and+navigates its own UI from the manifest's nav list.++- `GET /api/palette/manifest` — returns the navigable-page list (`nav`) plus the+ whitelisted action schemas (`actions`). DOM-driving `ui_*` tools are+ intentionally excluded, so the app renders its own UI and uses `nav` for routing.+- `POST /api/palette/action/:id` — dispatch a whitelisted action. Body:+ `{ "args": { … } }` (args object optional, defaults to `{}`). Returns+ `{ ok, result }`. Unknown ids 404.++Palette action ids the app is expected to use (`PALETTE_ACTIONS` in+`server/routes/palette.js` — always read the live manifest for the authoritative+list and each action's parameter schema):++| id | Purpose |+|----|---------|+| `brain_capture` | Capture a note to the Brain. |+| `brain_search` / `brain_list_recent` | Search / list recent Brain entries. |+| `daily_log_append` | Append a line to today's daily log. |+| `daily_log_read` | Read today's daily log. |+| `goal_list` / `goal_update_progress` / `goal_log_note` | List goals, update progress, log a note. |+| `meatspace_log_drink` / `_nicotine` / `_weight` / `_workout` | Log health events. |+| `meatspace_summary_today` | Today's health summary. |++### Daily-log append (direct route)++Dictation is transcribed **on device** (or server-side over Socket.IO); the+resulting text is POSTed as plain text. There is **no** server-side audio/STT+upload endpoint.++- `POST /api/brain/daily-log/:date/append` — body `{ "text": "…", "source": "…" }`.+ `:date` accepts `today` (resolved server-side) or `YYYY-MM-DD`. Empty/whitespace+ `text` 400s. The optional `source` tags the entry's origin (e.g. `"portdeck"`).+- `GET /api/brain/daily-log/:date` — read a day's log.++(The `daily_log_append` / `daily_log_read` palette actions wrap this same path —+either surface works.)++## 5. MeatSpace POST training & testing++`/api/meatspace/post/*` (`server/routes/meatspacePostRoutes.js`). POST progress is+a first-class federation sync category (`meatspace`), so it already reconciles+across peers.++| Method | Path | Purpose |+|--------|------|---------|+| `GET` / `PUT` | `/api/meatspace/post/config` | Read / update POST config. |+| `GET` | `/api/meatspace/post/sessions` | List training sessions. |+| `POST` | `/api/meatspace/post/sessions` | Record a session. |+| `GET` | `/api/meatspace/post/progress` | Current progress. |+| `GET` | `/api/meatspace/post/stats` | Aggregate stats. |+| `GET` | `/api/meatspace/post/recommendations` | Recommended next drills. |++The app can *prompt* the user to run POST training from the phone and POST the+resulting session, or sync progress via the iCloud pattern below.++## 6. iCloud-JSON sync precedent (POST-progress reconciliation)++The working reference for "iOS app writes an iCloud JSON file, PortOS ingests it"+is MortalLoom (`server/routes/mortalloom.js`, `server/services/mortalLoomStore.js`):++- `GET /api/mortalloom/status` — store status.+- `POST /api/mortalloom/import` — non-destructive **by-id merge** of the shared+ iCloud JSON into `data/`.++A POST-progress iCloud reconciliation endpoint mirroring this+(`POST /api/…/import`, non-destructive by-id merge into `data/meatspace/post/*`) is+**out of scope for this foundation** and will be filed as its own follow-up. Until+it lands, POST progress reconciles through the standard `/api/meatspace/post/*`+routes plus federation sync.++## Deferred follow-ups (filed separately)++- POST-progress iCloud reconciliation import endpoint (mirrors `mortalloom.js`).+- Server-side audio/STT upload endpoint — only if on-device transcription is+ abandoned.+- Push-notification / reminder plumbing to prompt POST training from the phone.+- A dedicated companion `apiRegistry.js` public group + per-device API token, if+ the single-password posture proves insufficient.++## See also++- [API.md](./API.md) — full REST/WebSocket reference and route-domain index.+- [PORTS.md](./PORTS.md) — port allocation and HTTP/HTTPS scheme.+- Machine-readable spec: `GET /api/api-docs/openapi.json` (public-API surface only).diff --git a/server/routes/health.test.js b/server/routes/health.test.jsindex c8de49cdff..812dfc7977 100644--- a/server/routes/health.test.js+++ b/server/routes/health.test.js@@ -1,13 +1,28 @@import { describe, it, expect, vi } from 'vitest';+import os from 'os';import express from 'express';import { request } from '../lib/testHelper.js';import systemHealthRoutes from './systemHealth.js';import { listProcesses } from '../services/pm2.js';+import { getSelf } from '../services/instances.js';+import { isAuthEnabled } from '../services/auth.js';vi.mock('../services/pm2.js', () => ({listProcesses: vi.fn().mockResolvedValue([])}));+// Mock instances + auth directly (rather than through settings) so the pre-auth+// companion-app identity fields on GET /health are deterministic. Mocking auth.js+// directly also sidesteps its module-load `settingsEvents.on(...)` side effect,+// which the partial settings.js mock below does not provide.+vi.mock('../services/instances.js', () => ({+ getSelf: vi.fn().mockResolvedValue({ instanceId: 'test-instance-id', name: 'Example Instance' })+}));++vi.mock('../services/auth.js', () => ({+ isAuthEnabled: vi.fn().mockResolvedValue(false)+}));+vi.mock('../services/apps.js', () => ({getAllApps: vi.fn().mockResolvedValue([]),getAppStatusSummary: vi.fn().mockResolvedValue({@@ -57,6 +72,28 @@ describe('System Health Routes', () => {expect(response.body.version).toBeDefined();});+ it('exposes companion-app identity fields (name, instanceId, authRequired, scheme)', async () => {+ const response = await request(app).get('/api/system/health');++ expect(response.status).toBe(200);+ expect(response.body).toHaveProperty('name', 'Example Instance');+ expect(response.body).toHaveProperty('instanceId', 'test-instance-id');+ expect(response.body).toHaveProperty('authRequired', false);+ expect(['http', 'https']).toContain(response.body.scheme);+ });++ it('reports authRequired true when the password gate is on and falls back to hostname for name', async () => {+ isAuthEnabled.mockResolvedValueOnce(true);+ getSelf.mockResolvedValueOnce(null);++ const response = await request(app).get('/api/system/health');++ expect(response.status).toBe(200);+ expect(response.body.authRequired).toBe(true);+ expect(response.body.name).toBe(os.hostname());+ expect(response.body.instanceId).toBeNull();+ });+it('should return health details with version', async () => {const response = await request(app).get('/api/system/health/details');diff --git a/server/routes/systemHealth.js b/server/routes/systemHealth.jsindex 47abf465fa..4c17d0b01d 100644--- a/server/routes/systemHealth.js+++ b/server/routes/systemHealth.js@@ -11,6 +11,8 @@ import { asyncHandler, ServerError } from '../lib/errorHandler.js';import { getMemoryStats } from '../lib/memoryStats.js';import { formatBytes } from '../lib/fileUtils.js';import { getSettings, updateSettingsWith } from '../services/settings.js';+import { isAuthEnabled } from '../services/auth.js';+import { getHttpsEnabledAtBoot } from '../lib/httpsState.js';// Defaults are tuned for a real dev machine: memory routinely sits in the// 75-85% band on a host with a couple of LLMs loaded, and big SSDs commonly@@ -38,17 +40,31 @@ async function loadThresholds() {const router = Router();router.get('/health', asyncHandler(async (req, res) => {- const [self, version] = await Promise.all([+ const [self, version, authRequired] = await Promise.all([getSelf().catch(() => null),- getCurrentVersion()+ getCurrentVersion(),+ // Fail closed: if the auth state can't be read, tell the client a password+ // may be required so it prompts rather than silently sending unauthenticated+ // requests that would 401. This endpoint stays public (PUBLIC_API_PATHS) so a+ // companion client can read identity before it holds any credential.+ isAuthEnabled().catch(() => true)]);+ const hostname = os.hostname();res.json({status: 'ok',timestamp: new Date().toISOString(),uptime: process.uptime(),version,- hostname: os.hostname(),- instanceId: self?.instanceId ?? null+ hostname,+ instanceId: self?.instanceId ?? null,+ // Companion-app (PortDeck) instance-identity fields — pre-auth so a native+ // client can identify/label a PortOS instance across the tailnet and decide+ // whether to prompt for a password before it has credentials. Additive and+ // non-sensitive (name/hostname/instanceId are within the tailnet trust model).+ // See docs/COMPANION_APP_API.md.+ name: self?.name ?? hostname,+ authRequired,+ scheme: getHttpsEnabledAtBoot().value ? 'https' : 'http'});}));