diff --git a/api/coach/core/schemas.js b/api/coach/core/schemas.js index 9779e73..163b457 100644 --- a/api/coach/core/schemas.js +++ b/api/coach/core/schemas.js @@ -59,6 +59,11 @@ const EX_SCHEMA = { required: ['id', 'sets'] }; +// `week` and `routines[].id` are required, not optional: the week is the only thing that says +// which day trains which routine, and it points at a routine by id. A schema that leaves either +// out lets a small local model answer with routines that have no id and a week naming "r1" — +// grammar-valid, and something validate.js can only ever reject ("the week schedules 0 days but +// 3 were asked for"), through the repair round and out as a failed job. export const CREATE_SCHEMA = { type: 'object', properties: { @@ -67,15 +72,22 @@ export const CREATE_SCHEMA = { summary: STR, basedOn: STR, week: { type: 'object' }, + // The caps are the validator's own (MAX_ROUTINES, MAX_EX_PER_ROUTINE), stated here as well + // because "1-7 routines, each 3-12 exercises" in create.md is only a request. A small model + // that starts repeating itself does not stop at a request: qwen2.5:3b walks the library id by + // id, three sets of eight apiece, until it hits the output limit — twelve minutes for an + // answer that arrives cut in half and cannot be parsed. maxItems ends that run at seven + // routines instead. routines: { type: 'array', + maxItems: 7, items: { type: 'object', properties: { id: STR, name: STR, emoji: STR, prog: STR, why: STR, - ex: { type: 'array', items: EX_SCHEMA } + ex: { type: 'array', maxItems: 20, items: EX_SCHEMA } }, - required: ['name', 'ex'] + required: ['id', 'name', 'ex'] } }, customEx: { @@ -83,7 +95,7 @@ export const CREATE_SCHEMA = { items: { type: 'object', properties: { id: STR, n: STR, bp: STR, desc: STR }, required: ['id', 'n'] } } }, - required: ['coach_contract', 'routines'] + required: ['coach_contract', 'week', 'routines'] }; export const DEBRIEF_SCHEMA = { diff --git a/api/test/prompt-split.test.js b/api/test/prompt-split.test.js index 1b0072b..deb80c3 100644 --- a/api/test/prompt-split.test.js +++ b/api/test/prompt-split.test.js @@ -4,6 +4,7 @@ import { buildPrompt, buildPromptParts, taskOf } from '../coach/core/prompt.js'; import { SCHEMAS } from '../coach/core/schemas.js'; import { build, FULL_DETAIL_SESSIONS } from '../coach/core/payload.js'; import { chatCompletionsSpec } from '../coach/core/adapters/openai.js'; +import { validatePlan } from '../coach/core/validate.js'; const S = (workouts = []) => ({ lang: 'en', unit: 'kg', routines: [{ id: 'r1', name: 'A', ex: [{ id: '0001', sets: 3, reps: 8 }] }], @@ -48,6 +49,32 @@ test('every task has a flat schema — no $ref, no anyOf/oneOf (llama.cpp gramma assert.equal(taskOf('create', { refine: 'x' }), 'refine'); }); +test('the create schema requires what the week resolves through: a routine id, and a week', () => { + // A schema handed to a grammar-constrained decoder is the only thing standing between a small + // local model and an answer the validator can only reject. These two fields are where that + // showed: llama3.2:3b and qwen2.5:3b both answer with routines that carry no `id` and a week + // naming what they put in `name`, which costs a job and a repair round to find out. + assert.ok(SCHEMAS.create.properties.routines.items.required.includes('id')); + assert.ok(SCHEMAS.create.required.includes('week')); + assert.equal(SCHEMAS.refine, SCHEMAS.create); // refine reuses it, same guarantee + + const noId = { + coach_contract: 1, + routines: [{ name: 'r1', ex: [{ id: '0001', sets: 3, reps: 8 }] }], + week: { 1: 'r1' } + }; + const checked = validatePlan(noId, { daysPerWeek: 1 }); + assert.ok(!checked.ok); + assert.ok(checked.errors.some(e => e.includes('week[1] points at "r1"'))); +}); + +test('the create schema caps the two arrays the validator caps, so a repeating model stops', () => { + // validate.js slices at MAX_ROUTINES / MAX_EX_PER_ROUTINE; a model that never stops emitting + // otherwise runs to the output limit first and the answer arrives truncated and unparseable. + assert.equal(SCHEMAS.create.properties.routines.maxItems, 7); + assert.equal(SCHEMAS.create.properties.routines.items.properties.ex.maxItems, 20); +}); + test('older sessions in a review window are compacted, the recent ones keep full sets', () => { const many = Array.from({ length: FULL_DETAIL_SESSIONS + 4 }, (_, i) => workout('2026-08-' + String(i + 1).padStart(2, '0'))); const p = build(S(many), { handle: 'h1', kind: 'review' });