diff --git a/frontend/src/lib/history.js b/frontend/src/lib/history.js index ab5e36a..edac277 100644 --- a/frontend/src/lib/history.js +++ b/frontend/src/lib/history.js @@ -388,7 +388,14 @@ export function workoutVolume(w) { // No special case for unilateral work: a per-side set logs its total, so both sides are // already in the rep count that arrives here. Drop-set drops and rest-pause bursts add their // own weight x reps on top of the row's main/activation set (see extraVolumeOf). - w.entries.forEach(e => e.sets.forEach(s => { if (s.done) v += (s.w || 0) * (s.r || 0) + extraVolumeOf(s) })) + // Warm-ups are excluded here as everywhere else. The config sheet promises it in so many + // words ("left out of volume, records and progression") and every other consumer already + // does it; this line was the one that did not, which only stopped being harmless when a + // routine started planning warm-ups by default. The number is written into the saved + // workout, so an inflated one would stay wrong forever. + w.entries.forEach(e => e.sets.forEach(s => { + if (s.done && !isWarmupRow(s)) v += (s.w || 0) * (s.r || 0) + extraVolumeOf(s) + })) return v } export function setsDone(w) { @@ -460,6 +467,36 @@ export function cascadeWeight(rows, from, value) { * `at` is 0, and reading `rows[-1]` used to fall through to the *last* row — the heaviest * work set — so "add warm-up set" handed you a full-weight set to correct by hand. */ +/** + * Recompute the warm-up block so it ramps toward the weight the work rows ACTUALLY carry. + * + * buildSets prepends the warm-ups before a prescription is applied, and applyPrescription + * deliberately rewrites work rows only — so without this the ramp still aims at last + * session's weight. On a deload that put the last warm-up above every work set, which is + * the exact opposite of what a warm-up is for. + * + * A warm-up already logged keeps its weight and becomes what the next one ramps from: it + * happened, and rewriting performed work is data loss. Entries with nothing to ramp toward + * — cardio, bodyweight, an unloaded hold — are returned untouched. + */ +export function rerampWarmups(rows, step = 2.5) { + const firstWork = rows.findIndex(x => !isWarmupRow(x)) + if (firstWork <= 0) return rows + const target = rows[firstWork].w || 0 + if (!(target > 0)) return rows + const out = rows.slice() + let from = 0 + for (let i = 0; i < firstWork; i++) { + if (out[i].done) { from = out[i].w || 0; continue } + const w = target > from + ? Math.max(0, Math.min(target, Math.floor((from + (target - from) / 2) / step) * step)) + : target + out[i] = { ...out[i], w } + from = w + } + return out +} + export function insertWarmupRow(rows, mode, target, step = 2.5) { const firstWork = rows.findIndex(x => !isWarmupRow(x)) const at = firstWork === -1 ? rows.length : firstWork @@ -467,7 +504,12 @@ export function insertWarmupRow(rows, mode, target, step = 2.5) { const work = firstWork === -1 ? null : rows[firstWork] const rampTo = to => { const from = prev ? (prev.w || 0) : 0 - if (!(to > 0) || to <= from) return from + // Nothing to ramp toward: bodyweight, cardio, a timed hold with no load. + if (!(to > 0)) return 0 + // Already at or past the work weight — which happens when a warm-up was edited by hand + // above it. Returning `from` here handed the next warm-up that same too-heavy number and + // let it propagate down the block. A warm-up is never heavier than the set it warms up for. + if (to <= from) return to // Rounded DOWN to the step: a warm-up that lands a notch light costs nothing, one that // lands a notch heavy is a set you have to strip plates off before you can use it. return Math.max(0, Math.min(to, Math.floor((from + (to - from) / 2) / step) * step)) diff --git a/frontend/src/lib/progression.js b/frontend/src/lib/progression.js index cd80ece..eb96818 100644 --- a/frontend/src/lib/progression.js +++ b/frontend/src/lib/progression.js @@ -16,7 +16,7 @@ // · fewer sets than prescribed → miss // So a session that fell apart can never advance the load as though it had succeeded. -import { modeOf, repStep } from './history.js' +import { modeOf, repStep, rerampWarmups } from './history.js' import { EXIDX } from './exercises.js' import { isWarmupRow } from './workout-model.js' @@ -250,7 +250,7 @@ export function nextPrescription(S, cfg, routine) { * Apply a prescription to freshly built sets. Only the fields the policy actually decided * are touched, and only on sets that have not been logged yet. */ -export function applyPrescription(sets, p) { +export function applyPrescription(sets, p, step = 2.5) { if (!p || p.kind === 'off' || p.kind === 'first') return sets const out = sets.map(s => { // Never rewrite a logged set, and never rewrite a warm-up: the prescription speaks to @@ -270,7 +270,7 @@ export function applyPrescription(sets, p) { if (p.sets > workRows.length) { // An all-warm-up entry has no work row to seed growth from - growing warm-up copies // would both invent work and never terminate the loop. Leave the entry untouched. - if (!workRows.length) return out + if (!workRows.length) return rerampWarmups(out, step) const seed = workRows[workRows.length - 1] // A freshly appended row hasn't been performed, so it never inherits a seed's already- // logged drops/clusters — that would invent extra work the row never actually did. Its @@ -279,5 +279,7 @@ export function applyPrescription(sets, p) { const { drops, clusters, ...plainSeed } = seed while (out.filter(s => !isWarmupRow(s)).length < p.sets) out.push({ ...plainSeed, done: false }) } - return out + // Last, because the work rows now carry their final weight: the warm-up block ramps toward + // what you are actually about to lift, not toward what you lifted last time. + return rerampWarmups(out, step) } diff --git a/frontend/src/lib/warmup-invariants.test.js b/frontend/src/lib/warmup-invariants.test.js new file mode 100644 index 0000000..8ee3e1c --- /dev/null +++ b/frontend/src/lib/warmup-invariants.test.js @@ -0,0 +1,76 @@ +import { describe, expect, it } from 'vitest' +import { workoutVolume, insertWarmupRow, buildSets } from './history.js' +import { applyPrescription } from './progression.js' + +// The config sheet tells users warm-ups are "left out of volume, records and progression", +// and history.js repeats that as an invariant in a comment. Volume was the one place it +// was false — harmless while warm-ups were hand-added and rare, wrong now that a routine +// plans them by default and the number is written into the saved workout for good. +describe('warm-ups and volume', () => { + const w = { + entries: [{ + id: 'bench', + sets: [ + { w: 50, r: 5, done: true, phase: 'warmup', warmup: true }, + { w: 75, r: 5, done: true, phase: 'warmup', warmup: true }, + { w: 87.5, r: 5, done: true, phase: 'warmup', warmup: true }, + { w: 100, r: 5, done: true }, + { w: 100, r: 5, done: true }, + { w: 100, r: 5, done: true }, + ], + }], + } + + it('counts only the work sets', () => { + expect(workoutVolume(w)).toBe(1500) + }) + + it('a warm-up adds nothing, whatever it weighs', () => { + const heavy = { entries: [{ id: 'b', sets: [{ w: 200, r: 10, done: true, phase: 'warmup', warmup: true }] }] } + expect(workoutVolume(heavy)).toBe(0) + }) +}) + +// A warm-up must never be heavier than the set it warms you up for. The ramp branch clamps +// to the work weight; the early-return branch did not, so a hand-edited warm-up above the +// work weight propagated into every warm-up added after it. +describe('a warm-up never outweighs the work set', () => { + // The 120 is something the user typed, and it stays: silently rewriting their own edit + // would be worse than leaving it. What must not happen is the NEW row inheriting it, which + // is how one bad number used to spread through the whole warm-up block. + it('does not copy a hand-edited warm-up that sits above the work weight', () => { + const rows = [{ warmup: true, phase: 'warmup', w: 120, r: 5 }, { w: 100, r: 5 }] + const out = insertWarmupRow(rows, 'reps', { reps: 5 }, 2.5) + expect(out.map(r => r.w)).toEqual([120, 100, 100]) + expect(out[1].phase).toBe('warmup') + }) + + it('still ramps normally when the previous warm-up is below the work weight', () => { + const rows = [{ warmup: true, phase: 'warmup', w: 50, r: 5 }, { w: 100, r: 5 }] + const out = insertWarmupRow(rows, 'reps', { reps: 5 }, 2.5) + expect(out.filter(r => r.phase === 'warmup').map(r => r.w)).toEqual([50, 75]) + }) +}) + +// buildSets prepends the warm-ups, then applyPrescription rewrites the WORK rows only — so a +// ramp built against last session's weight is stale the moment progression moves. On a deload +// that put the last warm-up above every work set. +describe('the ramp follows the prescribed weight', () => { + const S = { workouts: [], exWeights: {}, routines: [] } + const cfg = { id: 'bench', sets: 3, reps: 5, weight: 100, warmupSets: 2 } + + it('re-ramps after a deload instead of aiming at the old weight', () => { + const rows = applyPrescription(buildSets(S, cfg, { step: 2.5 }), { kind: 'deload', weight: 50 }) + const warm = rows.filter(r => r.phase === 'warmup').map(r => r.w) + const work = rows.filter(r => r.phase !== 'warmup').map(r => r.w) + expect(work).toEqual([50, 50, 50]) + for (const x of warm) expect(x).toBeLessThanOrEqual(50) + expect(warm).toEqual([25, 37.5]) + }) + + it('re-ramps after a bump', () => { + const rows = applyPrescription(buildSets(S, cfg, { step: 2.5 }), { kind: 'up', weight: 150 }) + expect(rows.filter(r => r.phase !== 'warmup').map(r => r.w)).toEqual([150, 150, 150]) + expect(rows.filter(r => r.phase === 'warmup').map(r => r.w)).toEqual([75, 112.5]) + }) +}) diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx index b7a5448..c70163d 100644 --- a/frontend/src/sheets.jsx +++ b/frontend/src/sheets.jsx @@ -1014,7 +1014,8 @@ export function beginWorkout(routineId, bw) { // kept on the entry purely so the workout can explain the number it chose. const entries = (r ? r.ex : []).map(cfg => { const plan = nextPrescription(st, cfg, r) - const sets = applyIntensifierPlan(applyPrescription(buildSets(st, cfg, { step: defaultIncrement(cfg.id, st.unit) }), plan), cfg) + const step = defaultIncrement(cfg.id, st.unit) + const sets = applyIntensifierPlan(applyPrescription(buildSets(st, cfg, { step }), plan, step), cfg) return { id: cfg.id, sg: cfg.sg, target: { ...cfg }, plan, sets } }) update(s => { diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index f2aaf53..27ca2e9 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -520,7 +520,7 @@ function ActiveWorkout() { const full = { ...cfg, id: ex.id } const plan = freestyle ? null : nextPrescription(s, full, s.routines.find(r => r.id === s.active.routineId)) const sets = buildSets(s, full, { step: defaultIncrement(ex.id, s.unit), ...(freestyle ? { preferLast: true } : {}) }) - const progressed = freestyle ? sets : applyPrescription(sets, plan) + const progressed = freestyle ? sets : applyPrescription(sets, plan, defaultIncrement(ex.id, s.unit)) s.active.entries.push({ id: ex.id, target: { ...cfg }, plan, sets: applyIntensifierPlan(progressed, full) }) s.active.cur = s.active.entries.length - 1 }), null, routine, seed)