Files
kuentagym/scripts/build-coach-library.mjs
Alex 7be3978476 Coach: server plumbing, consent, payload boundary, credential model
First of six. Everything here is transport and boundary: a job can run, its
answer can be parsed, and nothing yet judges whether that answer is safe to
act on. The validator and the apply path are the next PR, and the seam is
explicit — jobs.js carries an answer no further than `unvalidated`, and a
test pins that so the next PR has something to replace.

Whose account pays, settled first because it decides what the rest may be:

  instance  one account in coach.json, for the single-profile instance
  profile   one account per profile, in its own coach-auth-<uid>.json

In instance mode the credential binds to the first profile that spends it and
every other profile is refused — no job, not a dismissible warning. A warning
moves the decision onto whoever clicks past it and the decision is about
somebody's personal subscription. A profile credential is deliberately not a
field on state-<uid>.json: that blob syncs and travels in the user's export,
and a credential in a backup is the mistake we are avoiding, not repeating.
openGym does not interpret any provider's terms; it offers the shape that
does not need the interpretation and refuses the shape that does.

The privilege drop now fails closed. It only ever engaged when the server ran
as root, so adding a USER line to the Dockerfile for unrelated hardening would
have silently left the provider runtime on the server's uid. On Linux, no drop
means no job, and the admin card says so.

./data is no longer sealed with a blanket 0700. It is a host bind mount and
this container is root, so sealing it landed on the host as root-owned and
took the owner's own directory hostage — backup scripts, jq, the MCP server in
issue #19. The files that hold secrets carry 0600 themselves instead, which is
what the directory mode was standing in for; db.json gained an explicit mode
rather than inheriting umask once the blanket came off.

Bodyweight and per-side, folded in rather than deferred: v1.2.4 made `weight`
mean *added* load on a bodyweight exercise, so it is 0 on a good session and
every load-shaped signal reads flat. Uncorrected, the Coach would have looked
at a working push-up progression and proposed adding weight to a push-up. The
payload carries bodyweight/side/repsMax, the session reader carries set count,
and the prompts say reps then sets are the progression and per-side targets
step in twos. coach-parity.test.js pins the server's three copied reading
rules against the frontend's originals over a table of configs — modeOf was
never actually pinned, and it is the one that was predicted to drift.

No AI dependency lands here: api/ still installs @simplewebauthn/server and
web-push and nothing else. Jobs run against the in-repo fixture provider, so
CI needs no account, no network and no image build.

33 api tests, 361 frontend tests (169 of them parity), locales in sync.
2026-08-03 01:00:56 +01:00

41 lines
2.0 KiB
JavaScript

#!/usr/bin/env node
/* Generates api/coach/library.json from the frontend's exercise dataset.
*
* node scripts/build-coach-library.mjs # write
* node scripts/build-coach-library.mjs --check # fail if stale (CI)
*
* The API needs the catalogue for two things the client cannot do for it: validating that
* every exercise id a proposal references actually exists (FR-16), and building payloads for
* scheduled reviews, which run with nobody's browser open. Rather than teach the api build to
* reach into frontend/, the index is generated and committed — the same arrangement the
* translated instruction packs in src/instr/ already use.
*
* Only the fields the Coach reasons over are kept: an id, a name, and the three taxonomy
* fields it filters on. Instructions, images and secondary muscles stay out — they would
* quadruple a payload the model has no use for.
*/
import { readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
const src = join(root, 'frontend', 'src', 'lib', 'exercises-data.js');
const out = join(root, 'api', 'coach', 'library.json');
const { EXDB } = await import(pathToFileURL(src).href);
const index = EXDB.map(e => ({ id: e.id, n: e.n, bp: e.bp, tg: e.tg, eq: e.eq }));
const json = JSON.stringify({ generated_from: 'frontend/src/lib/exercises-data.js', count: index.length, exercises: index }) + '\n';
if (process.argv.includes('--check')) {
let current = null;
try { current = readFileSync(out, 'utf8'); } catch { /* missing counts as stale */ }
if (current !== json) {
console.error('api/coach/library.json is out of date — run: node scripts/build-coach-library.mjs');
process.exit(1);
}
console.log(`api/coach/library.json in sync (${index.length} exercises).`);
} else {
writeFileSync(out, json);
console.log(`Wrote ${out} — ${index.length} exercises, ${(json.length / 1024).toFixed(0)} KB.`);
}