From 5d8332d11d0e3e39db9a9af724f280cd1c57e8ab Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 7 Aug 2026 15:27:02 +0100 Subject: [PATCH] Coach: the store field the whole UI reads, which was never ported MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The entry point in the previous commit was necessary but not sufficient: every Coach surface gates on coachAvailable(config, user), config comes from useStore(s => s.config), and that field does not exist. 4/6 brought the views across and left behind the one line of store they all depend on. So `config` was undefined at all five call sites, the gate was false on every instance, and the feature was invisible everywhere — no row on Plan, and /#/coach redirecting straight back to /home. Enabled, credentialed, a real round trip through the provider passing, and no way in. Indistinguishable from "this instance has no Coach", which is exactly why it survived review: the failure mode is silence. Adds `config: null` and the boot-time fetch, ahead of /api/me because instance capabilities are public and needed whether or not anyone is signed in. Two tests pin it. One asserts the gate opens for the exact shape GET /api/config returns, and stays shut for `undefined` — the bug itself. The other asserts the store source still declares the field and still fetches it; that one reads the file rather than the live store, because useStore touches `document` at import time and cannot be instantiated in this environment. Crude, but it fails on precisely the two edits that broke this. frontend 406/406, build clean. Co-Authored-By: Claude Opus 5 --- frontend/src/lib/coach.test.js | 23 +++++++++++++++++++++++ frontend/src/store/useStore.js | 8 ++++++++ 2 files changed, 31 insertions(+) diff --git a/frontend/src/lib/coach.test.js b/frontend/src/lib/coach.test.js index e2f048a..65c13cd 100644 --- a/frontend/src/lib/coach.test.js +++ b/frontend/src/lib/coach.test.js @@ -443,3 +443,26 @@ describe('validation', () => { expect(validateProposal({ changes: [change()] })).toBe(true) }) }) + +describe('the gate has something real to read', () => { + it('the store actually exposes the `config` field every Coach entry point reads', async () => { + // coachAvailable(config, …) is false when `config` is undefined, which is indistinguishable + // from "this instance has no Coach". The views were ported once without the store field they + // depend on, so every Coach surface silently vanished on every instance: no entry point, and + // /#/coach redirecting straight back to /home. A missing key must fail here, not in a browser. + // Asserted against the source rather than the live store: useStore touches `document` at + // import time, so it cannot be instantiated in this environment. Crude, but it fails on + // exactly the two edits that broke it — dropping the field, or dropping the fetch. + const { readFileSync } = await import('node:fs') + const src = readFileSync(new URL('../store/useStore.js', import.meta.url), 'utf8') + expect(src).toMatch(/\bconfig:\s*null\b/) + expect(src).toMatch(/set\(\s*\{\s*config:\s*await api\(['"]\/api\/config['"]\)/) + }) + + it('a configured instance with a signed-in user opens the gate', () => { + // The exact shape GET /api/config returns when the Coach is on. + const config = { invite_only: false, coach: { enabled: true, provider: 'claude', authMode: 'instance' } } + expect(coachAvailable(config, { id: 'u' })).toBe(true) + expect(coachAvailable(undefined, { id: 'u' })).toBe(false) // the bug, pinned + }) +}) diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js index 2b41633..a6367d4 100644 --- a/frontend/src/store/useStore.js +++ b/frontend/src/store/useStore.js @@ -83,6 +83,11 @@ export const useStore = create((set, get) => { S: (() => { const s = loadState(); registerCustom(s.customEx); return s })(), user: (() => { try { return JSON.parse(localStorage.getItem('gym_user')) || null } catch { return null } })(), ready: false, + /* Instance capabilities from GET /api/config. `config.coach` is present only when the owner + has both enabled the Coach and connected a provider — every Coach entry point in the app + hangs off it via coachAvailable(), so an unconfigured instance renders exactly what it + always did, and a configured one is the only place any of it appears. */ + config: null, // Mutate a draft of S via producer fn, then persist + schedule sync. update(mut, push = true) { @@ -172,6 +177,9 @@ export const useStore = create((set, get) => { set({ ready: true }) return } + // Instance capabilities are public and are needed whether or not anyone is signed in — + // fetched before /api/me so the first render already knows what this instance offers. + try { set({ config: await api('/api/config') }) } catch { /* offline — assume nothing extra */ } try { const me = await api('/api/me') get().setUser(me.user)