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): make health fail-closed real + correct doc overclaims

[._.]/ Adam Eivy4398b1ca7a79
diff --git a/docs/COMPANION_APP_API.md b/docs/COMPANION_APP_API.md
index 34bf3a7b70..2a12cd19eb 100644
--- a/docs/COMPANION_APP_API.md
+++ b/docs/COMPANION_APP_API.md
@@ -142,7 +142,10 @@ 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"`).
+ `text` 400s. `source` records the **input modality** and is a controlled
+ vocabulary — one of `text`, `voice`, or `edit` (any other value is silently
+ normalized to `text` by `brainJournal.normalizeSource`). A dictated capture sends
+ `"voice"`; a typed one sends `"text"`. It is **not** a free-form app-identity tag.
- `GET /api/brain/daily-log/:date` — read a day's log.
(The `daily_log_append` / `daily_log_read` palette actions wrap this same path —
@@ -150,9 +153,17 @@ 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.
+`/api/meatspace/post/*` (`server/routes/meatspacePostRoutes.js`). These are the
+read/write endpoints for POST config, sessions, and progress on a single instance.
+
+> **Note — POST progress does not cross-peer federate today.** The `meatspace`
+> peer-sync category (`MEATSPACE_FILES` in `server/services/dataSync.js`) covers the
+> daily-log and health-record files but **not** the POST files
+> (`post-sessions.json`, `post-config.json`, …), so POST progress written to one
+> instance stays local to it. Cross-instance POST reconciliation is a deferred
+> follow-up (see below) — either add the POST files to the sync category or ship the
+> iCloud import endpoint. A companion app that shows POST progress across instances
+> must read each instance's `/api/meatspace/post/*` directly until then.
| Method | Path | Purpose |
|--------|------|---------|
@@ -178,8 +189,9 @@ is MortalLoom (`server/routes/mortalloom.js`, `server/services/mortalLoomStore.j
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.
+it lands there is no cross-instance POST reconciliation — a companion app reads and
+writes each instance's `/api/meatspace/post/*` routes directly (see the note in
+§5).
## Deferred follow-ups (filed separately)
diff --git a/server/lib/authGate.js b/server/lib/authGate.js
index 50b3d60cda..eae3e4a293 100644
--- a/server/lib/authGate.js
+++ b/server/lib/authGate.js
@@ -121,7 +121,20 @@ const isCrossOrigin = (req) => {
// allows the small public set above; gates the rest behind a valid token in
// the cookie or Authorization: Bearer header.
export const authGate = async (req, res, next) => {
- const enabled = await isAuthEnabled();
+ let enabled;
+ try {
+ enabled = await isAuthEnabled();
+ } catch {
+ // Fail closed: if the auth state can't be read (e.g. a transient
+ // settings-read failure on the cold path before isAuthEnabled's cache is
+ // primed), assume auth is ON so no gated route slips through — but do NOT
+ // 500 the whole request here. Fall through to the public-path checks below
+ // so the intentionally pre-auth endpoints (/api/system/health discovery,
+ // the login UI) stay reachable. This is what makes the health route's
+ // documented fail-closed `authRequired: true` actually observable — its own
+ // isAuthEnabled().catch(() => true) never runs if the gate rejects first.
+ enabled = true;
+ }
if (!enabled) return next();
// CSRF guard runs FIRST, before isPublicPath — public endpoints like
// /api/auth/logout still mutate state (clear the cookie + revoke the
diff --git a/server/lib/authGate.test.js b/server/lib/authGate.test.js
index e380be8200..cd6de4dd91 100644
--- a/server/lib/authGate.test.js
+++ b/server/lib/authGate.test.js
@@ -63,6 +63,26 @@ describe('authGate middleware', () => {
expect(result.called).toBe(true);
});
+ it('fails closed but keeps public discovery paths reachable when the auth-state read throws', async () => {
+ // Simulate isAuthEnabled() rejecting on the cold path (e.g. a transient
+ // settings read failure before its cache is primed). The gate must not
+ // 500 the request: public paths stay reachable (so /api/system/health
+ // discovery works pre-auth) while gated routes fail closed as if auth is on.
+ const auth = await import('../services/auth.js');
+ const spy = vi.spyOn(auth, 'isAuthEnabled').mockRejectedValue(new Error('settings unreadable'));
+ const { authGate } = await import('./authGate.js');
+
+ const health = await runGate(authGate, { path: '/api/system/health', headers: {} });
+ expect(health.called).toBe(true);
+
+ const gated = await runGate(authGate, { path: '/api/cos', headers: {} });
+ expect(gated.called).toBe(false);
+ expect(gated.res.statusCode).toBe(401);
+ expect(gated.res.body).toEqual({ error: 'Authentication required', code: 'AUTH_REQUIRED' });
+
+ spy.mockRestore();
+ });
+
it('blocks /api routes with no token', async () => {
const auth = await import('../services/auth.js');
await auth.setPassword({ newPassword: 'correct-horse' });