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(mcp): preview_session builds a custom exercise as the app does

Boris 'B' Kurktchiev5859d5e1c1ff
diff --git a/mcp/src/tools.js b/mcp/src/tools.js
index 6b014b03c..2217180ff 100644
--- a/mcp/src/tools.js
+++ b/mcp/src/tools.js
@@ -10,7 +10,7 @@ import {
modeOf, workoutVolume, setsDone, effectiveRoutine, effectiveRoutineIds, lastEntryFor
} from '../../frontend/src/lib/history.js'
import { queueView, queueNext, pinState } from '../../frontend/src/lib/queue.js'
-import { exOr } from '../../frontend/src/lib/exercises.js'
+import { exOr, registerCustom } from '../../frontend/src/lib/exercises.js'
import { isWarmupRow } from '../../frontend/src/lib/workout-model.js'
import {
bestSetOf, best1RM, e1rmSeries, DEFAULT_FORMULA, REP_CAP
@@ -41,6 +41,15 @@ const customOf = (id, S) => (S.customEx || []).find(ex => ex.id === id)
// exOr's miss is a placeholder object, not null — callers that only need a name are fine
// with it, callers feeding muscle resolution are NOT. Use customOf directly there.
const exerciseOf = (id, S) => customOf(id, S) || exOr(id)
+// The session builder, though, reads each exercise's equipment and body part from that global
+// index, so preview_session built a custom leg lift with the small load step where the app (which
+// registers its customs) takes the larger one. It registers the profile's customs for the length
+// of one synchronous build and takes them back out in the same call: nothing else, and no other
+// profile's read, can run in between.
+function withCustomsIndexed(S, build) {
+ registerCustom(Array.isArray(S.customEx) ? S.customEx : [])
+ try { return build() } finally { registerCustom([]) }
+}
function entryView(e, S) {
const ex = exerciseOf(e.id, S)
@@ -555,7 +564,7 @@ export const previewSession = {
// The same builder the app starts a session with (sheets.jsx beginWorkout → session-start.js):
// prescription, step, progression-off targets, deload routines and warm-up ramps all come from
// there, so the preview cannot drift from what the screen shows.
- const built = buildSessionEntries(S, r)
+ const built = withCustomsIndexed(S, () => buildSessionEntries(S, r))
const exercises = (r.ex || []).map((cfg, i) => {
const ex = exerciseOf(cfg.id, S)
const mode = modeOf({ ...cfg, id: cfg.id })
diff --git a/mcp/test/tools.test.js b/mcp/test/tools.test.js
index f32c7c19d..63878fb22 100644
--- a/mcp/test/tools.test.js
+++ b/mcp/test/tools.test.js
@@ -4,7 +4,7 @@
// (rest-day override, missing routine, zero-workout history, no synced state, superset links).
import { describe, beforeAll, afterAll, beforeEach, test, expect, vi } from 'vitest'
import { buildDemoState } from '../../frontend/src/lib/demoSeed.js'
-import { EXDB } from '../../frontend/src/lib/exercises.js'
+import { EXDB, exOr } from '../../frontend/src/lib/exercises.js'
import { _seedStateForTests } from '../src/state.js'
import { TOOLS } from '../src/tools.js'
import { bestSetOf } from '../../frontend/src/lib/onerm.js'
@@ -954,6 +954,23 @@ describe('preview_session', () => {
_seedStateForTests(S)
}
+ // The session builder reads equipment and body part from the global exercise index, which holds
+ // no custom exercises here (see customOf in tools.js). A custom leg lift then ramped on the small
+ // load step, where the app, which registers its customs, takes the larger one for legs.
+ test('a custom leg lift ramps on the same load step as in the app, and the index is left as it was', () => {
+ const custom = { id: 'c-hack', n: 'Hack Squat (mine)', bp: 'upper legs', eq: 'barbell', tg: 'quads', sm: [], custom: true }
+ S.customEx = [custom]
+ only({ id: 'c-hack', sets: 3, reps: 8, weight: 90, warmupSets: 2 }, { prog: 'off' })
+ try {
+ const r = call('preview_session')
+ expect(r.exercises[0].opening_sets.filter(x => x.phase === 'warmup').map(x => x.w)).toEqual([45, 65])
+ expect(exOr('c-hack').missing).toBe(true) // taken back out of the index
+ } finally {
+ S.customEx = []
+ _seedStateForTests(S)
+ }
+ })
+
test('defaults to the routine scheduled for today', () => {
only({ id: '0025', sets: 3, reps: 8, weight: 50 })
const r = call('preview_session')