From 565311accce3ec9d867e279d9f249096ad5170b7 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Fri, 4 Sep 2026 09:51:01 +0200 Subject: [PATCH] Handy: 'Use my self-hosted openGym' erkennt den Coach des gepaarten Servers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Der Mobile-Boot und das Pairing haben /api/config nie geladen, nur die Web-Variante und der Login-Screen. Auf dem gepaarten Handy stand deshalb im Coach-Setup immer 'Your server has no Coach enabled' — waehrend der Admin auf einen gruenen Test schaute (Discord-Meldung, 2026-09-04). Jetzt: refreshConfig() fragt immer, loadConfig() cached nur noch darueber; Boot (remote) und connectToServer laden die Config, der Setup-Screen fragt beim Oeffnen neu und zeigt bis dahin 'Loading…'. Regressionstest fuer load/refresh. --- CHANGELOG.md | 8 ++++ frontend/src/store/useStore.config.test.jsx | 43 +++++++++++++++++++++ frontend/src/store/useStore.js | 10 +++++ frontend/src/views/CoachSetup.jsx | 5 ++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 frontend/src/store/useStore.config.test.jsx diff --git a/CHANGELOG.md b/CHANGELOG.md index fd9e4a8..5743e20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## Unreleased + +- 📱 **"Use my self-hosted openGym" works on a paired phone.** The phone app never fetched the + server's `/api/config` after boot or after pairing, so the Coach setup screen told everyone + "your server has no Coach enabled" — while the admin was looking at a passed test. The screen + now asks the server afresh every time it opens, and the boot and pairing paths load the config + like the web app always did. Reported on Discord. + ## v1.3.0 — 2026-09-03 The AI Coach gets the version number it deserves. v1.2.16, earlier today, was the release that diff --git a/frontend/src/store/useStore.config.test.jsx b/frontend/src/store/useStore.config.test.jsx new file mode 100644 index 0000000..47f5835 --- /dev/null +++ b/frontend/src/store/useStore.config.test.jsx @@ -0,0 +1,43 @@ +// @vitest-environment happy-dom +// /api/config is what tells a client whether the server offers the Coach (and guest mode). +// loadConfig() caches it for a boot; refreshConfig() always asks — the Coach setup screen on a +// paired phone relies on that, because the admin may have switched the Coach on since boot. +// The bug this pins: the phone never fetched it at all, and said "your server has no Coach +// enabled" to a server whose admin was looking at a green test. +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +vi.mock('../lib/api.js', () => ({ api: vi.fn() })) + +import { api } from '../lib/api.js' +import { useStore } from './useStore.js' + +beforeEach(() => { api.mockReset(); useStore.setState({ config: null }) }) +afterEach(() => { useStore.setState({ config: null }) }) + +describe('server config', () => { + it('loadConfig fetches once and then answers from the cache', async () => { + api.mockResolvedValue({ invite_only: true, allow_guest: false, coach: { enabled: true } }) + expect(await useStore.getState().loadConfig()).toEqual({ invite_only: true, allow_guest: false, coach: { enabled: true } }) + expect(await useStore.getState().loadConfig()).toMatchObject({ coach: { enabled: true } }) + expect(api).toHaveBeenCalledTimes(1) + expect(api).toHaveBeenCalledWith('/api/config') + }) + + it('refreshConfig always asks the server, so a Coach switched on after boot is seen', async () => { + api.mockResolvedValueOnce({ invite_only: true, allow_guest: false }) + await useStore.getState().loadConfig() + expect(useStore.getState().config.coach).toBeUndefined() + + api.mockResolvedValueOnce({ invite_only: true, allow_guest: false, coach: { enabled: true, provider: 'anthropic' } }) + await useStore.getState().refreshConfig() + expect(useStore.getState().config.coach.enabled).toBe(true) + expect(api).toHaveBeenCalledTimes(2) + }) + + it('an unreachable server leaves the cached config alone', async () => { + useStore.setState({ config: { coach: { enabled: true } } }) + api.mockRejectedValueOnce(new Error('offline')) + expect(await useStore.getState().refreshConfig()).toBeNull() + expect(useStore.getState().config.coach.enabled).toBe(true) + }) +}) diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js index ae5ae6a..c032afa 100644 --- a/frontend/src/store/useStore.js +++ b/frontend/src/store/useStore.js @@ -152,6 +152,11 @@ export const useStore = create((set, get) => { config: null, async loadConfig() { if (get().config) return get().config + return get().refreshConfig() + }, + // Always asks. The cached copy is right for one boot, but an admin can switch the Coach on + // while a paired phone sits on the setup screen — that screen wants today's answer. + async refreshConfig() { try { const c = await api('/api/config'); set({ config: c }); return c } catch { return null } }, @@ -197,6 +202,7 @@ export const useStore = create((set, get) => { async connectToServer(url, code) { const user = await connect(url, code) // throws on a bad URL/expired code — caller shows it get().setUser(user) + await get().refreshConfig() // what this server offers (the Coach, guest mode) — see boot() await get().pullState() syncReminder(get().S) set({ needsMobileOnboarding: false }) @@ -243,6 +249,10 @@ export const useStore = create((set, get) => { try { const me = await api('/api/me') // also catches a token revoked elsewhere (sign out everywhere) get().setUser(me.user) + // The paired server's /api/config, the same one the web boot reads: without it the + // phone never learned whether the server offers the Coach and told everyone "your + // server has no Coach enabled" — with the admin looking at a green test. + await get().loadConfig() await get().pullState() } catch (e) { if (e.status === 401) { await forgetRemote(); get().setGuest(true) } diff --git a/frontend/src/views/CoachSetup.jsx b/frontend/src/views/CoachSetup.jsx index ab82187..e5f8141 100644 --- a/frontend/src/views/CoachSetup.jsx +++ b/frontend/src/views/CoachSetup.jsx @@ -31,6 +31,7 @@ export default function CoachSetup() { const config = useStore(s => s.config) const coachLocal = useStore(s => s.coachLocal) const setCoachLocal = useStore(s => s.setCoachLocal) + const refreshConfig = useStore(s => s.refreshConfig) const toast = useUI(s => s.toast) const openSheet = useUI(s => s.openSheet) @@ -46,6 +47,8 @@ export default function CoachSetup() { useEffect(() => { if (!MOBILE) nav('/settings', { replace: true }) }, []) useEffect(() => { getApiKey().then(k => setHasKey(!!k)) }, []) + // Ask the paired server afresh: the admin may have switched the Coach on since this phone booted. + useEffect(() => { if (user) refreshConfig() }, [user]) useEffect(() => { setModels(null); setModel(coachLocal?.provider === provider ? (coachLocal?.model || '') : '') }, [provider]) const meta = HTTP_PROVIDERS[provider] || {} @@ -112,7 +115,7 @@ export default function CoachSetup() {
{ if (!user || serverHasCoach) useServer(); else setChoice('server') }} />