From dafc80f1d641a9dd8034a9b6e08977c96674d874 Mon Sep 17 00:00:00 2001 From: Konstantinos Thermos Date: Fri, 4 Sep 2026 18:46:39 +0300 Subject: [PATCH] Coach: the create schema requires a routine id and a week MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CREATE_SCHEMA is handed to OpenAI-compatible endpoints as a grammar to decode against, and it left both `routines[].id` and `week` optional. Small local models take that offer: llama3.2:3b and qwen2.5:3b both answer with routines that carry no id, and a week naming what they wrote into `name` instead. Nothing in the week then resolves, validate.js reports "the week schedules 0 days but 3 were asked for", the repair round produces the same answer again, and the job ends as `unusable` — "The Coach answered with something the app couldn't use." Requiring the two fields the week resolves through is enough. With them in the schema, llama3.2:3b produces a plan that passes validatePlan on the first attempt, against the payload that failed twice before. The validator is untouched; this only stops the grammar from offering an answer it can never accept. --- api/coach/core/schemas.js | 9 +++++++-- api/test/prompt-split.test.js | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/api/coach/core/schemas.js b/api/coach/core/schemas.js index 9779e73..072c58f 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: { @@ -75,7 +80,7 @@ export const CREATE_SCHEMA = { id: STR, name: STR, emoji: STR, prog: STR, why: STR, ex: { type: 'array', items: EX_SCHEMA } }, - required: ['name', 'ex'] + required: ['id', 'name', 'ex'] } }, customEx: { @@ -83,7 +88,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..aea87c9 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,25 @@ 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('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' });