From a97ae06c6867e528d012bf6bff3e34bb8333ab18 Mon Sep 17 00:00:00 2001 From: Space_Hermes Date: Fri, 28 Aug 2026 14:32:56 +0200 Subject: [PATCH] fix: use history snapshots in recovery maps --- frontend/src/lib/recovery.js | 15 ++++++++---- frontend/src/lib/recovery.test.js | 38 ++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/frontend/src/lib/recovery.js b/frontend/src/lib/recovery.js index 7ab6f45..5412f42 100644 --- a/frontend/src/lib/recovery.js +++ b/frontend/src/lib/recovery.js @@ -61,6 +61,12 @@ function emptyMuscleMap(value) { return Object.fromEntries(MUSCLES.map(slug => [slug, value])) } +// Current catalogue metadata stays authoritative. Finished entries retain a nested muscle +// snapshot specifically so deleted custom exercises can still contribute to recovery maps. +function exerciseFor(entry) { + return EXIDX[entry?.id] || entry +} + // Epley one-rep-max estimate, matching onerm.js (REP_CAP included so high-rep sets do not // inflate the estimate). Used only to express a set's intensity relative to the lifter's own // capacity - the same formula the app already shows for estimated 1RM. @@ -161,7 +167,7 @@ function loadKgFor(ex, entry, set, workout, opts = {}) { function session1RMs(workout, opts = {}) { const best = new Map() for (const entry of workout?.entries || []) { - const ex = EXIDX[entry.id] + const ex = exerciseFor(entry) for (const set of entry.sets || []) { const load = loadKgFor(ex, entry, set, workout, opts) if (set?.done !== true || !(load > 0) || !(set.r > 0)) continue @@ -221,10 +227,11 @@ function sessionTonnages(workout, opts = {}) { const sums = emptyMuscleMap(0) const oneRms = session1RMs(workout, opts) for (const entry of workout?.entries || []) { - const weights = musclesOf(EXIDX[entry.id]) + const ex = exerciseFor(entry) + const weights = musclesOf(ex) for (const set of entry.sets || []) { if (set?.done !== true) continue - const measured = setTonnage(EXIDX[entry.id], entry, set, workout, oneRms.get(entry.id), opts) + const measured = setTonnage(ex, entry, set, workout, oneRms.get(entry.id), opts) const tonnage = Number.isFinite(measured) && measured > 0 ? measured : ZERO_LOAD_SET_STIMULUS for (const [slug, weight] of Object.entries(weights)) { if (Object.prototype.hasOwnProperty.call(MUSCLES_BY_SLUG, slug)) sums[slug] += tonnage * weight @@ -326,7 +333,7 @@ export function strengthOf(workouts, now, opts = {}) { if (!Number.isFinite(timestamp)) continue for (const entry of workout.entries || []) { if (!(entry.sets || []).some(set => set?.done === true && !isWarmupRow(set))) continue - for (const slug of Object.keys(musclesOf(EXIDX[entry.id]))) { + for (const slug of Object.keys(musclesOf(exerciseFor(entry)))) { if (Object.prototype.hasOwnProperty.call(MUSCLES_BY_SLUG, slug) && timestamp > latest[slug]) { latest[slug] = timestamp } diff --git a/frontend/src/lib/recovery.test.js b/frontend/src/lib/recovery.test.js index 4ec2007..1c7ca21 100644 --- a/frontend/src/lib/recovery.test.js +++ b/frontend/src/lib/recovery.test.js @@ -17,7 +17,7 @@ import { strengthOf, } from './recovery.js' import { EXDB, registerCustom } from './exercises.js' -import { MUSCLES, musclesOf } from './muscles.js' +import { MUSCLES, exerciseMuscleSnapshot, musclesOf } from './muscles.js' import { fatigueStateOf } from './recovery-view.js' const HOUR = 60 * 60 * 1000 @@ -128,6 +128,42 @@ describe('fatigueOf and strengthOf', () => { expect(fatigueOf(highRep, NOW)[SINGLE_SLUG]).toBeGreaterThan(0.5) }) + it('uses a deleted exercise snapshot for the same bounded primary and secondary fatigue', () => { + const resolved = doneWorkoutAt(WEIGHTED.id, NOW) + const deleted = { + ...resolved, + entries: [{ + ...resolved.entries[0], + id: 'deleted-weighted-exercise', + muscleSnapshot: exerciseMuscleSnapshot(WEIGHTED), + }], + } + + expect(fatigueOf([deleted], NOW)).toEqual(fatigueOf([resolved], NOW)) + expect(fatigueOf([deleted], NOW)[WEIGHTED_PRIMARY_SLUG]).toBeGreaterThan(0) + expect(fatigueOf([deleted], NOW)[SECONDARY_SLUG]).toBeGreaterThan(0) + }) + + it('uses a deleted exercise snapshot for strength without changing decay or floor semantics', () => { + const start = NOW - 15 * DAY + const resolved = doneWorkoutAt(WEIGHTED.id, start) + const deleted = { + ...resolved, + entries: [{ + ...resolved.entries[0], + id: 'deleted-weighted-exercise', + muscleSnapshot: exerciseMuscleSnapshot(WEIGHTED), + }], + } + const untouchedSlug = MUSCLES.find(slug => !WEIGHTED_WEIGHTS[slug]) + const strength = strengthOf([deleted], NOW) + + expect(strength).toEqual(strengthOf([resolved], NOW)) + expect(strength[WEIGHTED_PRIMARY_SLUG]).toBeCloseTo(0.5 ** (1 / 28), 10) + expect(strength[SECONDARY_SLUG]).toBeCloseTo(0.5 ** (1 / 28), 10) + expect(strength[untouchedSlug]).toBe(STRENGTH_FLOOR) + }) + it('raises starting fatigue with volume, never pins, and fades without a cliff', () => { const at0 = count => fatigueOf([doneWorkoutAt(SINGLE.id, NOW, count)], NOW)[SINGLE_SLUG] expect(at0(1)).toBeCloseTo(1 - Math.exp(-V / FATIGUE_REF_VOLUME), 10)