Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

fix(coach): a bonus set no longer reads as a stall the athlete never had

Boris 'B' Kurktchieva1c0636995fa
diff --git a/api/coach/core/payload.js b/api/coach/core/payload.js
index a1c144526..b39c5fbde 100644
--- a/api/coach/core/payload.js
+++ b/api/coach/core/payload.js
@@ -26,9 +26,13 @@ export { DATA_CATEGORIES } from './categories.js';
/* ---------- reading a session the way the engine reads it ----------
Duplicated from frontend/src/lib/history.js rather than shared: the two runtimes have no
build step in common, and this is the same trade-off server.js already made for
- effectiveRoutineId. coach-parity.test.js pins these against the frontend's own copies over
- a shared table of configs, so the duplicate cannot drift silently — which it otherwise
- would have, quietly, when v1.2.4 taught the app about bodyweight work. */
+ effectiveRoutineId. frontend/src/lib/coach-parity.test.js pins modeOf, isBw and isPerSide
+ against the frontend's own copies over a shared table of configs, so the duplicate cannot
+ drift silently — which it otherwise would have, quietly, when v1.2.4 taught the app about
+ bodyweight work. isWarmupSet and readSession are duplicated the same way but were pinned by
+ nothing; api/test/payload-parity.test.js pins them now. It has to be a separate file: that
+ test imports the frontend's own readSession, and a vitest test cannot import into a
+ node:test file or back. */
export const modeOf = (cfg, ex) => {
const m = cfg && cfg.mode;
if (m === 'reps' || m === 'time' || m === 'cardio') return m;
@@ -60,9 +64,16 @@ function readSession(entry, fallback) {
const ex = LIB_BY_ID.get(entry?.id);
const mode = modeOf(target, ex);
const bw = isBw(target, ex);
- const sets = ((entry && entry.sets) || []).filter(s => !isWarmupSet(s));
- const planned = target.sets || sets.length;
- const enough = sets.length >= planned;
+ const logged = ((entry && entry.sets) || []).filter(s => !isWarmupSet(s));
+ const planned = target.sets || logged.length;
+ const enough = logged.length >= planned;
+ // Only the sets the plan asked for decide whether the session was hit, exactly as
+ // frontend/src/lib/progression.js readSession has done since issue #233. This copy graded
+ // every logged set, so a fourth set taken short of the goal on a clean 3x10 was a hit in the
+ // app and a miss here — and stallCount, reading only this copy, reported a stall the athlete
+ // never had. `count` below stays the real total: extra sets are exactly how bodyweight work
+ // is meant to grow (#33), they just do not decide whether the prescription was met.
+ const sets = logged.slice(0, Math.max(1, planned));
if (mode === 'time') {
const goal = target.sec || 0;
const held = sets.map(s => (s.done ? (s.sec || 0) : 0));
@@ -72,7 +83,7 @@ function readSession(entry, fallback) {
const reps = sets.map(s => (s.done ? (s.r || 0) : 0));
// Set count is the dimension bodyweight work grows once reps hit their ceiling (upstream
// #33), so it travels alongside the reps rather than being inferred from them downstream.
- const done = sets.filter(s => s.done).length;
+ const done = logged.filter(s => s.done).length;
return { mode, bw, goal, count: done, ok: goal > 0 && enough && reps.length > 0 && reps.every(r => r >= goal) };
}
/** Consecutive misses counting back from the most recent session. */
diff --git a/api/test/payload-parity.test.js b/api/test/payload-parity.test.js
new file mode 100644
index 000000000..572429f25
--- /dev/null
+++ b/api/test/payload-parity.test.js
@@ -0,0 +1,87 @@
+/* payload.js's own header comment says its duplicated reading rules are pinned against the
+ frontend's originals so the copy cannot drift silently, but frontend/src/lib/coach-parity.test.js
+ only ever imported three of the five: modeOf, isBw, isPerSide. isWarmupSet and readSession were
+ pinned by nothing. This file closes that gap.
+
+ It cannot live inside coach-parity.test.js itself: that file runs under vitest, and readSession
+ is not exported from payload.js (only isWarmupSet is), so the only way to observe it from
+ outside is through payload.build()'s aggregates, which needs a real DATA_DIR and the rest of the
+ coach module graph — the same setup payload.test.js already uses under node:test. The frontend
+ imports below are safe under plain node: workout-model.js and progression.js have no Vite- or
+ React-only syntax, the same reason mcp/'s own tests import frontend/src/lib directly. */
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { tempData, sampleState } from './helpers.mjs';
+import { isWarmupRow } from '../../frontend/src/lib/workout-model.js';
+import { readSession as frontendReadSession } from '../../frontend/src/lib/progression.js';
+
+tempData();
+const payload = await import('../coach/core/payload.js');
+const { handleFor } = await import('../coach/handle.js');
+
+test('isWarmupSet agrees with the frontend isWarmupRow', () => {
+ const cases = [
+ {}, null, undefined,
+ { warmup: true }, { warmup: false }, { warmup: 'true' },
+ { phase: 'warmup' }, { phase: 'warm-up' }, { phase: 'warm_up' }, { phase: 'WARMUP' },
+ { phase: 'work' }, { phase: '' }, { phase: 'bogus' },
+ { phase: 'warmup', warmup: false }, { phase: 'work', warmup: true },
+ ];
+ for (const c of cases) assert.equal(payload.isWarmupSet(c), isWarmupRow(c), JSON.stringify(c));
+});
+
+/* readSession decides whether a logged session counts as a hit, feeding stallCount and every
+ progression signal the aggregates carry. It is not exported, so it is read here the way the
+ engine itself reads it: through build()'s aggregates, on an exercise with three sessions (the
+ inclusion floor `aggregates` applies before it will report on an exercise at all). */
+const EX_ID = '0001'; // bodyweight, real catalogue id — same one coach-parity.test.js uses
+const PLAN = { id: EX_ID, sets: 3, reps: 10, mode: 'reps', weight: 20, prog: 'linear' };
+const set = r => ({ w: 0, r, done: true });
+const entry = sets => ({ id: EX_ID, target: { sets: 3, reps: 10 }, sets });
+
+function reviewFor(entries) {
+ const S = sampleState({
+ routines: [{ id: 'r1', name: 'Full body A', ex: [PLAN] }],
+ workouts: entries.map((en, i) => ({
+ id: 'w' + i, d: `2026-08-${String(i + 1).padStart(2, '0')}`, name: 'Full body A',
+ start: 1000, end: 2000, entries: [en],
+ })),
+ });
+ return payload.build(S, { handle: handleFor('u_parity'), kind: 'review' });
+}
+const exOf = p => p.aggregates.exercises.find(e => e.id === EX_ID);
+
+test('readSession agrees with the frontend on an ordinary session', () => {
+ const passing = entry([set(10), set(10), set(10)]);
+ const p = reviewFor([passing, passing, passing]);
+ assert.equal(exOf(p).lastOk, true);
+ assert.equal(frontendReadSession(passing, PLAN).ok, true);
+});
+
+// Exact inputs: a session logged with a fourth set beyond the planned three, whose reps fall
+// short of the goal. frontend/src/lib/progression.js readSession grades only the first
+// `target.sets` sets (issue #233: "a hard [extra set] taken short of the target reps reported
+// the whole session as missed") and calls this a hit. payload.js's copy graded every logged
+// set and called the same session a miss, so stallCount reported a stall the athlete never
+// had, and the Coach's review and proposal were built on it.
+test('readSession agrees with the frontend on a bonus set logged beyond the plan', () => {
+ const passing = entry([set(10), set(10), set(10)]);
+ const bonus = entry([set(10), set(10), set(10), set(5)]);
+ const p = reviewFor([passing, passing, bonus]);
+
+ assert.equal(frontendReadSession(bonus, PLAN).ok, true, 'the frontend grades the first 3 sets and calls this a hit');
+ assert.equal(exOf(p).lastOk, true, 'and so must this, or stallCount invents a stall');
+ assert.equal(exOf(p).stalls, 0);
+
+ // A short set INSIDE the plan is still a miss on both sides: the slicing must not swallow it.
+ const short = entry([set(10), set(10), set(5)]);
+ assert.equal(frontendReadSession(short, PLAN).ok, false);
+ assert.equal(exOf(reviewFor([passing, passing, short])).lastOk, false);
+ assert.equal(exOf(reviewFor([passing, passing, short])).stalls, 1);
+
+ // Fewer sets than planned is short whichever way it is read, and `enough` says so on both
+ // sides before the slicing is reached.
+ const tooFew = entry([set(10), set(10)]);
+ assert.equal(frontendReadSession(tooFew, PLAN).ok, false);
+ assert.equal(exOf(reviewFor([passing, passing, tooFew])).lastOk, false);
+});