Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Coach: the Codex provider, and a credential that stays out of your backups
Alexef61df50f2f4
diff --git a/.github/workflows/test.yml b/.github/workflows/test.ymlindex a7e39d284..60ad8c4e3 100644--- a/.github/workflows/test.yml+++ b/.github/workflows/test.yml@@ -98,6 +98,21 @@ jobs:console.log(r.version);'+ - name: the coach image carries the Codex runtime and its sandbox+ run: |+ docker run --rm opengym-api:coach codex --version+ docker run --rm opengym-api:coach bwrap --version+ # Owned by the user jobs drop to, and 0700 — a credential cache readable by anything+ # else in the container would undo the point of dropping privileges at all.+ test "$(docker run --rm opengym-api:coach stat -c '%U %a' /coach-auth)" = "coach 700"++ - name: neither Codex nor its sandbox is in the default image+ run: |+ # bubblewrap is a Codex requirement, so an owner who never opted in should not be+ # carrying it. Both commands must fail.+ ! docker run --rm opengym-api:default codex --version 2>/dev/null+ ! docker run --rm opengym-api:default bwrap --version 2>/dev/null+- name: the privilege drop can actually be performed in the imagerun: |# canDropPrivileges() fails closed, so a missing `coach` user disables every job —diff --git a/api/Dockerfile b/api/Dockerfileindex 234e45124..72538b85d 100644--- a/api/Dockerfile+++ b/api/Dockerfile@@ -42,6 +42,20 @@ CMD ["node", "server.js"]FROM base AS coachRUN npm ci --omit=dev && npm cache clean --force+# Codex ships as a CLI rather than a library, so it is installed rather than resolved, and+# bubblewrap is its sandbox — a Codex requirement, which is why neither appears in the default+# target or anywhere in this tree until now.+RUN apk add --no-cache bubblewrap \+ && npm install -g @openai/codex \+ && npm cache clean --force++# Where Codex keeps its refreshable login cache. Deliberately a sibling of /data rather than a+# path inside it: docs/SELF_HOSTING tells owners to back up with `tar czf … data/`, and a live+# refresh token has no business riding along in every archive that instruction produces. Owned+# by `coach` because the job that writes it has already dropped to that user.+RUN mkdir -p /coach-auth && chown coach:coach /coach-auth && chmod 700 /coach-auth+VOLUME ["/coach-auth"]+# ---------------------------------------------------------------------------------------------# Last, so that a plain `docker build ./api` keeps producing the runtime-free image.FROM base AS defaultdiff --git a/api/coach/adapters/codex.js b/api/coach/adapters/codex.jsnew file mode 100644index 000000000..b46f0d63b--- /dev/null+++ b/api/coach/adapters/codex.js@@ -0,0 +1,42 @@+/* OpenAI's Codex CLI as a Coach provider.+ *+ * `codex exec` is the non-interactive mode; the bare `-` argument makes it read the+ * prompt from stdin, matching the adapter interface. Everything else here exists to+ * stop the CLI reaching for state a sandboxed job does not have.+ */+import { run } from './spawn.js';++const CLI = 'codex';++export default {+ id: 'codex',+ cli: CLI,++ async check(cfg, env) {+ const r = await run(CLI, ['--version'], { env, timeoutMs: 20000 });+ if (r.spawnError) return { ok: false, error: `the ${CLI} CLI is not installed in this image` };+ if (r.timedOut) return { ok: false, error: `the ${CLI} CLI did not respond` };+ if (r.code !== 0) return { ok: false, error: (r.stderr || '').trim().slice(0, 300) || `${CLI} --version exited ${r.code}` };+ return { ok: true, version: (r.stdout || '').trim() };+ },++ async invoke({ prompt, jobDir, env, model, timeoutMs }) {+ const argv = [+ 'exec', '-', // non-interactive; '-' reads the prompt from stdin+ '--skip-git-repo-check', // the job dir is a bare mkdtemp, not a repo -- without this it refuses to run+ '--ephemeral', // do not write session files; the job dir dies with the job anyway+ '--ignore-user-config' // $CODEX_HOME/config.toml would be an admin-invisible input to every job+ ];+ // $CODEX_HOME itself is still set (config.jobEnv), because that is where the CLI keeps its+ // refreshable login cache and a job whose HOME is a temp dir would otherwise find no login+ // at all. --ignore-user-config narrows that to credentials only: the cache is read, the+ // config file beside it is not, so an admin cannot be handed job behaviour they never saw.+ if (model) argv.push('--model', model);++ // Sandbox mode is left at the CLI's default (read-only). The job only needs the model+ // to write an answer to stdout, and this process is already an unprivileged user in a+ // container -- widening it here would trade that away for nothing.+ const r = await run(CLI, argv, { stdin: prompt, cwd: jobDir, env, timeoutMs });+ return { ...r, text: (r.stdout || '').trim() };+ }+};diff --git a/api/coach/adapters/index.js b/api/coach/adapters/index.jsindex 3b635c2c2..971e93c77 100644--- a/api/coach/adapters/index.js+++ b/api/coach/adapters/index.js@@ -8,6 +8,7 @@*/import { run } from './spawn.js';import claude from './claude.js';+import codex from './codex.js';/*** The in-repo fake provider. Ships with the image on purpose: it is what CI drives, and it@@ -32,7 +33,8 @@ const fixture = {/* claude is registered unconditionally, and that is safe because claude.js imports the SDKlazily: on the default image the module loads, check() reports the runtime as absent, and- isConnected() keeps the Coach out of /api/config entirely. Codex arrives with 6/6. */-const ADAPTERS = { fixture, claude };+ isConnected() keeps the Coach out of /api/config entirely. Codex is here too, and unlike+ the SDK its runtime is a CLI binary, so its absence shows up as a spawn error from check(). */+const ADAPTERS = { fixture, claude, codex };export const adapterFor = provider => ADAPTERS[provider] || null;export default ADAPTERS;diff --git a/api/coach/config.js b/api/coach/config.jsindex 2f0b24254..7a85c36c6 100644--- a/api/coach/config.js+++ b/api/coach/config.js@@ -27,6 +27,17 @@ import path from 'node:path';const DATA = process.env.DATA_DIR || '/data';const FILE = path.join(DATA, 'coach.json');++/* Where a provider runtime that manages its own credential cache is allowed to keep it.+ *+ * A sibling of ./data, never inside it, and that placement is the whole point. docs/SELF_HOSTING+ * tells people to back up with `tar czf … data/`; anything under ./data therefore ends up in+ * every backup archive an owner is told to make. A refresh token is not workout history — it is+ * a live credential that keeps working after the archive is copied to a laptop or a cloud drive.+ *+ * So the runtime gets its own mount, and the backup instructions stay true rather than being+ * quietly wrong about what they capture. */+export const CREDENTIAL_HOME = process.env.COACH_CREDENTIAL_DIR || '/coach-auth';export const COACH_DISABLED = /^(1|true|yes|on)$/i.test(process.env.COACH_DISABLED || '');// Providers this build can drive. `runtime` is what the adapter runs. Adding one is an adapter@@ -42,6 +53,13 @@ export const PROVIDERS = {claude: {label: 'Claude (Anthropic)', runtime: 'Claude Agent SDK',apiKeyEnv: 'ANTHROPIC_API_KEY', oauthEnv: 'CLAUDE_CODE_OAUTH_TOKEN', setupToken: true+ },+ // Codex keeps a refreshable login cache in $CODEX_HOME rather than taking a token on the+ // environment, so it is the one provider that needs somewhere durable to write. That+ // somewhere is CREDENTIAL_HOME — outside ./data, so `tar czf … data/` cannot capture it.+ codex: {+ label: 'Codex (OpenAI)', runtime: 'Codex CLI',+ apiKeyEnv: 'CODEX_API_KEY', oauthEnv: null, credentialHomeEnv: 'CODEX_HOME'}};@@ -233,6 +251,10 @@ export function jobEnv(jobDir, resolved) {: resolved.type === 'apikey' ? meta.apiKeyEnv : null;if (name) env[name] = auth.token;}+ // A provider whose runtime keeps its own credential cache needs a home that survives the job,+ // because HOME is a temp dir that dies with it. It is deliberately NOT under ./data — see+ // CREDENTIAL_HOME — so the documented backup of ./data cannot pick up a live refresh token.+ if (meta.credentialHomeEnv) env[meta.credentialHomeEnv] = CREDENTIAL_HOME;return env;}diff --git a/api/test/adapters.test.js b/api/test/adapters.test.jsindex 36abc2c42..1ffa78a12 100644--- a/api/test/adapters.test.js+++ b/api/test/adapters.test.js@@ -50,3 +50,21 @@ test('the fixture provider needs no credential and the Claude one names both itassert.equal(cfg.PROVIDERS.claude.apiKeyEnv, 'ANTHROPIC_API_KEY');assert.equal(cfg.PROVIDERS.claude.setupToken, true);});++test('the Codex credential cache is a sibling of ./data, never inside it', () => {+ // The documented backup is `tar czf … data/`. A refresh token under ./data would ride along in+ // every archive an owner is told to make, and keep working wherever that archive is copied.+ const home = cfg.CREDENTIAL_HOME;+ assert.ok(home, 'a provider that writes a credential cache needs somewhere to write it');+ assert.equal(home.startsWith(process.env.DATA_DIR + '/'), false, `${home} is inside ./data`);++ // …and it is actually handed to the runtime, under the variable that provider declares.+ cfg.reset();+ cfg.save({ enabled: true, provider: 'codex' });+ assert.equal(cfg.jobEnv('/tmp/job', { ok: true }).CODEX_HOME, home);++ // A provider with no cache of its own is given no such variable at all.+ cfg.reset();+ cfg.save({ enabled: true, provider: 'claude' });+ assert.equal(cfg.jobEnv('/tmp/job', { ok: true }).CODEX_HOME, undefined);+});diff --git a/docker-compose.yml b/docker-compose.ymlindex 3afa9c89f..7f31bb043 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -40,6 +40,10 @@ services:- DATA_DIR=/datavolumes:- ./data:/data # users, passkeys, per-user state, session secret — BACK THIS UP+ # Provider credential cache (Codex). A sibling of ./data on purpose: the documented backup+ # is `tar czf … data/`, and a live refresh token must not end up inside it. Nothing here+ # needs backing up — if you lose it, sign the provider in again.+ - ./coach-auth:/coach-auth# Builds the React frontend (multi-stage) and serves it, proxying /api → api# and the exercise media from the shared volume. Single origin for passkeys.diff --git a/docs/AI_COACH.md b/docs/AI_COACH.mdindex 61f4ff873..f8cce4de3 100644--- a/docs/AI_COACH.md+++ b/docs/AI_COACH.md@@ -178,6 +178,27 @@ job environment is built from nothing rather than filtered, and the SDK's `env`the child environment rather than extending it — so `RP_ID`, `ADMIN_UIDS` and the VAPID keyscannot reach it by inheritance even by accident.+## Where a credential lives++Two shapes, because the two providers work differently:++- **Claude** takes its credential on the environment, so nothing is written to disk beyond the+ encrypted blob in `coach.json` — it reaches the model process as `CLAUDE_CODE_OAUTH_TOKEN` (or+ `ANTHROPIC_API_KEY`) and by no other route.+- **Codex** keeps a refreshable sign-in cache of its own, so it needs somewhere durable to write.+ That is `./coach-auth`, mounted at `/coach-auth`, owned by the `coach` user, mode `0700`.++`./coach-auth` is a **sibling of `./data`, never a folder inside it**, and that placement is the+whole point. §5 of `docs/SELF_HOSTING.md` tells owners to back up with `tar czf … data/`.+Anything under `./data` is therefore in every backup archive people are instructed to produce —+and unlike workout history, a refresh token keeps working after that archive is copied to a+laptop or a cloud drive. Keeping the cache outside `./data` is what lets the backup instructions+stay true about what they capture.++Per-profile credentials follow the same rule for the same reason: they live in+`coach-auth-<uid>.json` at mode `0600`, never in the synced state blob, so they cannot ride along+in a device sync or in a user's own JSON export.+## Off is really off`COACH_DISABLED=1` forces the Coach off everywhere regardless of what is stored — the fleetdiff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.mdindex da61930e0..b254faade 100644--- a/docs/SELF_HOSTING.md+++ b/docs/SELF_HOSTING.md@@ -118,6 +118,13 @@ tar czf opengym-backup-$(date +%F).tar.gz data/That archive contains all profiles, passkeys and workout history. Restore by unpacking it backinto the project folder. (Individual users can also export their own data as JSON from Settings.)+If you enabled the AI Coach with the Codex provider, note what this archive deliberately does+**not** contain: `./coach-auth`, where that provider keeps its refreshable sign-in. It is a+sibling of `./data` rather than a folder inside it precisely so that a live credential does not+end up in every backup you are told to make — an archive like this gets copied to laptops and+cloud drives, and a refresh token keeps working wherever it lands. Nothing in `./coach-auth`+needs backing up: if you lose it, sign the provider in again.+## 6. NotificationsopenGym can push two kinds of alert to your phone/desktop, even when the app isn't open: