The core of "build a routine with AI": muscles, cardio, intensity
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s

The screen that will drive this needs three things underneath it, and all three
are here, tested, with no UI yet.

1. `librarySlice` can be narrowed to the muscles someone picked. A selector is
   `{ bp }` or `{ tg }` — the target muscle matters because "biceps" and "triceps"
   are both `upper arms`, and asking for one must not hand back the other — and an
   exercise is kept when it matches any of them, so "back and biceps" is one list.
   `catalogueOnly` drops the person's own exercises, which are the only rows in
   the app with no animation; the builder promises every pick can be watched.

   Both answers ride in the existing `intake` object rather than a second channel:
   the intake is already how "which muscles" reaches the payload, and it travels
   the whole way (route → job → build, and local → build) untouched. `cleanGroups`
   sanitises it, since it is the one intake field that reaches a filter rather than
   the model's reading.

2. `lib/ai-routine.js` — the three questions and what to do with the answer: the
   intake the model is asked with, and the routine it hands back, normalised for
   this app. Cardio lands in the library and not only in the sentence (a model told
   to start with cardio but handed no cardio exercise can only improvise, which is
   what it did the first time this was tried), a cardio row becomes one bout
   whatever set count came back, and a missing mode is read from the exercise the
   same way `modeOf` reads it everywhere else.

3. A validator fix found on the way: the cardio branch set `min` and `speed` but
   never `clean.mode = 'cardio'`, unlike the time and reps branches. The app
   recovered it from the catalogue, so nothing was visibly broken — but the bundle
   did not describe its own prescription, and a model that omitted `mode` (which is
   what a model prescribing "8 minutes of cardio" usually does) got "3 × 10 reps"
   with a stray `min` nothing reads. Now the mode is stated, and inferred from the
   catalogue when it is missing. `time` is deliberately not inferred: nothing in the
   catalogue says an exercise is held for seconds.

Verified against the real router behind gym.dploy.lol with the real pipeline: a
back+biceps request produced a 6-exercise routine from an 80/80/22 library slice.
This commit is contained in:
dsantillan committed 2026-09-21 15:12:27 -04:00
1 parent ea1af681c2
commit 327aa5211b
7 files changed
+409 -7

No files matched your search

+48 -3
View File
@@ -12,6 +12,9 @@ export const LIB_BY_ID = new Map(LIBRARY.map(e => [e.id, e]));
export const libraryHas = id => LIB_BY_ID.has(id);
export const libraryName = id => LIB_BY_ID.get(id)?.n || null;
// The body part, for the two places that have to reason about an exercise rather than name it:
// the validator (a cardio row prescribed in reps is not a prescription) and the builder's filter.
export const libraryBp = id => LIB_BY_ID.get(id)?.bp || null;
/* ---------- the library slice the model gets to choose from ----------
Bounded. The whole catalogue is 1,324 rows — 10k+ tokens on every job, which costs real money
@@ -26,14 +29,56 @@ export const MAX_LIBRARY = 160;
// fields beyond body part never appear in a rationale and cost ~30 tokens an entry.
const slim = e => ({ id: e.id, n: e.n, bp: e.bp, ...(e.custom ? { custom: true } : {}) });
export function librarySlice(S, equipment, { keep = [], max = MAX_LIBRARY } = {}) {
/**
* The muscle selectors from an intake answer, sanitised.
*
* This is the one intake field that reaches a filter rather than the model's reading, so it is
* the one that has to be treated as input: the payload is built from whatever the client sent.
* A selector is `{ bp }` ("back", "chest") or `{ tg }` ("biceps", "triceps" — both live under the
* single `upper arms` body part, so the body part alone cannot tell them apart); anything else is
* dropped, since an unparseable selector could only ever narrow the library for no reason. The
* list is capped because more than a handful of muscles is not a routine, it is a whole week.
*/
export function cleanGroups(groups) {
if (!Array.isArray(groups)) return null;
const out = [];
for (const g of groups.slice(0, 12)) {
const bp = typeof g?.bp === 'string' ? g.bp.trim().toLowerCase().slice(0, 30) : '';
const tg = typeof g?.tg === 'string' ? g.tg.trim().toLowerCase().slice(0, 30) : '';
if (bp || tg) out.push({ ...(bp ? { bp } : {}), ...(tg ? { tg } : {}) });
}
return out.length ? out : null;
}
/**
* The catalogue narrowed to the muscles someone picked in the routine builder.
*
* An exercise is kept when it matches any selector: "back and biceps" is one list, not two.
*
* A selector that matches nothing returns the whole pool instead. That is the same call the
* equipment filter makes, for the same reason — a Coach that cannot propose anything is a worse
* failure than one proposing from a wider list — and it is only reachable from a hand-written
* payload, since the builder's buttons are built from this same taxonomy.
*/
function withGroups(list, groups) {
if (!groups || !groups.length) return list;
const kept = list.filter(e => groups.some(g => (g.tg && e.tg === g.tg) || (g.bp && e.bp === g.bp)));
return kept.length ? kept : list;
}
export function librarySlice(S, equipment, { keep = [], max = MAX_LIBRARY, groups = null, catalogueOnly = false } = {}) {
const wanted = (equipment || []).map(x => String(x).toLowerCase());
const customs = (S.customEx || []).map(c => ({ id: c.id, n: c.n, bp: c.bp, tg: null, eq: 'custom', custom: true }));
// `catalogueOnly` drops the person's own exercises, which carry no animation: the routine
// builder promises every exercise it picks can be watched, and those rows are the only ones
// in the app that cannot be.
const customs = catalogueOnly
? []
: (S.customEx || []).map(c => ({ id: c.id, n: c.n, bp: c.bp, tg: null, eq: 'custom', custom: true }));
// No equipment stated (or "everything") ⇒ the whole catalogue. Filtering to nothing would
// leave the Coach unable to propose anything at all, which is a worse failure than a
// slightly larger payload.
const filtered = wanted.length ? LIBRARY.filter(e => wanted.includes((e.eq || '').toLowerCase())) : LIBRARY;
const base = filtered.length ? filtered : LIBRARY;
const base = withGroups(filtered.length ? filtered : LIBRARY, groups);
const pinned = new Set(keep.filter(id => LIB_BY_ID.has(id)));
const out = [];
+10 -2
View File
@@ -10,7 +10,7 @@
* handle stands in), passkey and credential material, push subscriptions, invite data, theme
* and appearance settings, and every other profile's everything.
*/
import { LIBRARY, LIB_BY_ID, libraryHas, libraryName, librarySlice, MAX_LIBRARY } from './library.js';
import { LIBRARY, LIB_BY_ID, libraryHas, libraryName, librarySlice, cleanGroups, MAX_LIBRARY } from './library.js';
export const CONTRACT = 1;
// Bounds from FR-22. A review reads a training block, not a training career: more history
@@ -408,7 +408,15 @@ export function build(S, opts = {}) {
// A review names mostly what is already trained; 60 candidates is plenty for a swap.
p.library = librarySlice(S, profile?.equipment, { keep: trainedIds(S, workouts), max: 60 });
} else {
p.library = librarySlice(S, profile?.equipment, { keep: trainedIds(S, S.workouts || []) });
// The routine builder narrows the list to the muscles the person picked and, with it, drops
// their own exercises: the builder promises every exercise it chooses has an animation, and
// a custom row is the only kind that has none. Both answers ride in the intake, which is
// already how "which muscles" reaches the payload — no second channel for one screen.
p.library = librarySlice(S, profile?.equipment, {
keep: trainedIds(S, S.workouts || []),
groups: cleanGroups(profile?.groups),
catalogueOnly: profile?.catalogueOnly === true
});
// Creation for a returning user: what they have actually handled, so proposed baselines
// start from evidence rather than optimism (B2/FR-20).
const best = {};
+9 -2
View File
@@ -14,7 +14,7 @@
* model verbatim for the one repair round (FR-48) — and a list of six problems produces a
* better second attempt than the first of them does.
*/
import { libraryHas, libraryName } from './library.js';
import { libraryHas, libraryName, libraryBp } from './library.js';
// The closed list (FR-23 / C3). Adding a member here is a deliberate act with an apply
// implementation on the client to match; there is no default case anywhere.
@@ -99,9 +99,16 @@ export function validatePlan(data, ctx = {}) {
return;
}
const clean = { id: e.id, sets: isInt(e.sets, 1, 10) ? e.sets : 3 };
const mode = MODES.includes(e.mode) ? e.mode : 'reps';
// The mode has to agree with the exercise. The catalogue is the authority on what is
// cardio, and the app resolves an absent mode the same way (modeOf in history.js), so a
// model that leaves `mode` off now gets a cardio row that behaves like cardio instead of
// "3 × 10 reps" carrying a stray `min` nothing reads. `time` is deliberately NOT inferred
// this way: nothing in the catalogue says an exercise is held for seconds, and guessing it
// from one field would rewrite a rep prescription.
const mode = MODES.includes(e.mode) ? e.mode : (libraryBp(e.id) === 'cardio' ? 'cardio' : 'reps');
const perSide = !!e.side;
if (mode === 'cardio') {
clean.mode = 'cardio';
clean.min = isInt(e.min, 1, 180) ? e.min : 20;
clean.speed = isNum(e.speed) && e.speed > 0 && e.speed <= MAX_SPEED ? e.speed : 8;
} else if (mode === 'time') {