Coach: the validator, and the apply path it exists to protect

PR 1 stopped at transport and held every answer as `unvalidated`. This closes
that seam.

validate.js is the gate: a closed list of eighteen change types with no default
case, every exercise id resolved against the real catalogue, every target
resolved against the plan it claims to be about, and a rationale required on
every change. A hostile note in someone's free text can talk a model into saying
anything at all; it cannot invent a change type. Its errors join the parse
failures on the same single repair round, so a model that named an exercise
nobody has is told which one — and a model that cannot be told is a failed job
rather than a retry loop.

frontend/src/lib/coach.js is the other half: snapshot, apply, revert, and the
staleness rules. Applying happens on the client because it is ordinary local
editing — offline, synced like everything else, reversible without the server
knowing. A change-set is all-or-nothing, and a change aimed at something the
user has since edited by hand is disabled with its reason rather than silently
overwriting them.

Two things the seam was hiding, both from PR 1:

hashPlan hashed the pre-1.2.4 field list while canonicalPlan had already learned
repsMax, bodyweight and side — so it computed all three and threw them away. A
rep ceiling raised by hand fingerprinted as an untouched plan, on exactly the
exercises where that ceiling is how progression works. Both runtimes now hash
what canonicalPlan reports, with a test per field.

`refine` reads pending.bundle, and pending only ever carried `unvalidated`, so
`previous` resolved to null every time: the model was asked to refine a plan it
had never been shown. It works now, and the fixture echoes whether the previous
bundle arrived so a test can tell the difference.

The validator also enforces the two rules prompts/common.md states and could
not guarantee: unilateral reps are a total across both sides and must be even,
and a rep ceiling may not sit below its own floor.

"Nothing to change" keeps the reading it came with, on the profile's own record
— deliberately not in the instance log, which stays counts and outcomes only.

api 65/65, frontend 403/403, build clean, locales in sync, library in sync at
1324 exercises. No new dependency.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
AlexandClaude Opus 5 committed 2026-08-06 08:25:42 +01:00
1 parent de7f25cbe7
commit 34d41b8e64
10 files changed
+1726 -30

No files matched your search

+5 -2
View File
@@ -160,8 +160,11 @@ export function coachRoutes({ json, readBody, readSession, requireAdmin }) {
json(res, 200, r);
},
/* The gap here is `authMode` and connect/clear-credential, and it lands with PR 3 rather
than in this file today. Both exist to serve a credential, and the only provider this
build ships is the fixture, which has none: `credentialFor` returns before it consults
the mode. A switch whose two positions do the same thing is worse than no switch — so it
arrives with the setup-token path, in the PR that gives it something to switch between. */
/* Whose account this profile is about to spend. Its own route because both the Coach screen
and the admin card must state it, and neither should be inferring it from settings. */