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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Follow-up to !140: ATG's step-up and per-side sets score at the standard they name
Duarte Santos6502fd6fb942
diff --git a/frontend/src/lib/structuralBalance.js b/frontend/src/lib/structuralBalance.jsindex 5e6849b1d..efff880d2 100644--- a/frontend/src/lib/structuralBalance.js+++ b/frontend/src/lib/structuralBalance.js@@ -1,4 +1,4 @@-import { estimate1RM } from './onerm.js'+import { estimate1RM, FORMULAS } from './onerm.js'import { LB_TO_KG } from './recovery.js'import { isBw } from './history.js'import { EXIDX, isAssisted } from './exercises.js'@@ -60,11 +60,38 @@ function setLoadKg(S, workout, entry, set, bodyweightKg) {return body > 0 ? body + logged : null}-// Best estimated 1RM across a whitelist of exercise ids, in kg. Scans the log directly instead of+// The lifts one logged row stands for, each as the { w, r } it is read from. A unilateral row's+// `r` is the both-sides total and its `w` the heavier side (workout-model.js), so it is read as+// its two sides instead, the better one counting — as bestRepsOf() does below and progression.js+// halves the total for: 20 kg for 6 a leg is 20 kg for 6, not for 12, which would overstate it,+// and a 15-a-leg step-up would read as 30 reps and fall outside every estimate.+const liftsOf = set => (isSideSet(set) ? [set.sides.L, set.sides.R] : [set])++// Epley run the other way: the load a one-rep max shows for `reps` reps.+export const loadForReps = (oneRmKg, reps) => (reps > 1 ? oneRmKg / (1 + reps / 30) : oneRmKg)++// ATG's figures are a load for a number of reps ("Romanian deadlift: body weight for 12"), not a+// one-rep max, so a lift is read as the load it shows for that many. A set of at least that many+// reps proves its own load; a shorter one is carried over with Epley, the formula estimate1RM()+// estimates with, up to the 1RM and back down (a single is its own 1RM, as it is there). Neither+// goes through estimate1RM(): its rep cap guards a 1RM claim, not a 15-rep standard read from a+// 15-rep set, and its 0.1 kg rounding put a set of exactly body weight for 12 a hair under body+// weight — Borderline, next to "100% / 100%" — about half the time.+export function loadAtReps(loadKg, setReps, reps) {+ const w = Number(loadKg)+ const r = Math.round(Number(setReps))+ if (!isFinite(w) || w <= 0 || !(r >= 1)) return null+ if (r >= reps) return w+ return loadForReps(r === 1 ? w : FORMULAS.epley(w, r), reps)+}++// Best reading across a whitelist of exercise ids, in kg: the estimated 1RM (`estKg`), or with+// `reps` the load for that many reps (`repsKg`, loadAtReps). Scans the log directly instead of// calling onerm.js's best1RM(), which reads `s.w` raw and so cannot see the body mass a dip or a-// pull-up moves; the estimate itself still goes through the shared estimate1RM() so the formula,-// its rounding and its rep cap stay in one place.-export function resolveCurrent(S, exerciseIds, bodyweightKg) {+// pull-up moves, nor a unilateral row's sides; the 1RM itself still goes through the shared+// estimate1RM() so the formula, its rounding and its rep cap stay in one place.+export function resolveCurrent(S, exerciseIds, bodyweightKg, reps = null) {+ const key = reps > 1 ? 'repsKg' : 'estKg'let best = nullfor (const exId of exerciseIds) {for (const workout of S.workouts || []) {@@ -72,12 +99,14 @@ export function resolveCurrent(S, exerciseIds, bodyweightKg) {if (!entry) continuefor (const set of entry.sets || []) {if (!set.done || isWarmupRow(set)) continue- const loadKg = setLoadKg(S, workout, entry, set, bodyweightKg)- if (loadKg === null) continue- const estKg = estimate1RM(loadKg, set.r)- if (estKg === null) continue- if (!best || estKg > best.estKg) {- best = { estKg, w: Number(set.w) || 0, r: Math.round(Number(set.r)), d: workout.d, t: workout.start, exId }+ for (const lift of liftsOf(set)) {+ const loadKg = setLoadKg(S, workout, entry, lift, bodyweightKg)+ if (loadKg === null) continue+ const kg = reps > 1 ? loadAtReps(loadKg, lift.r, reps) : estimate1RM(loadKg, lift.r)+ if (kg === null) continue+ if (!best || kg > best[key]) {+ best = { [key]: kg, w: Number(lift.w) || 0, r: Math.round(Number(lift.r)), d: workout.d, t: workout.start, exId }+ }}}}@@ -158,13 +187,6 @@ export function exerciseIdsFor(S, template, role) {return override ? [override] : role.exerciseIds}-// ATG's figures are a load for a number of reps ("Romanian deadlift: body weight for 12"), not a-// one-rep max, so the estimate is read back at the role's reps before it is compared — Epley, the-// formula estimate1RM() estimates with, run the other way, so a set of exactly that many reps-// comes back as its own load. Held against the 1RM itself, a lifter at 72% of body weight for 12-// read as balanced on a body-weight-for-12 standard.-export const loadForReps = (oneRmKg, reps) => (reps > 1 ? oneRmKg / (1 + reps / 30) : oneRmKg)-// Actual percentage a role's current reading represents, given its evaluation mode. Returns// null when it can't be scored yet (anchor has no reading of its own, or bodyweight unknown).export function ratioFor(role, current, ctx) {@@ -174,17 +196,24 @@ export function ratioFor(role, current, ctx) {}if (role.evaluationMode === EVALUATION_MODES.BODYWEIGHT_RATIO) {if (!(ctx.bodyweightKg > 0)) return null- return (loadForReps(current.estKg, role.reps) / ctx.bodyweightKg) * 100+ // A role with reps was resolved as the load for them (resolveCurrent), one without as a 1RM.+ const kg = role.reps > 1 ? current.repsKg : current.estKg+ return kg > 0 ? (kg / ctx.bodyweightKg) * 100 : null}// rep-count: the best completed-set rep count (resolveCurrentReps), as a percentage of the// target rep count.return (current.r / ctx.targetPct) * 100}+// A reading taken from loads exactly at a ratio can still come out a floating-point hair under+// it (a lb load and a lb body weight both converted to kg, one divided by the other), so one+// within this many points of an edge counts as reaching it.+const EDGE_TOLERANCE_PCT = 1e-9+export function classify(actualPct, targetPct) {if (actualPct === null || actualPct === undefined || !isFinite(actualPct)) return BALANCE_STATUSES.NO_DATA- if (actualPct >= targetPct) return BALANCE_STATUSES.BALANCED- if (actualPct >= targetPct - BORDERLINE_BAND_PCT) return BALANCE_STATUSES.BORDERLINE+ if (actualPct >= targetPct - EDGE_TOLERANCE_PCT) return BALANCE_STATUSES.BALANCED+ if (actualPct >= targetPct - BORDERLINE_BAND_PCT - EDGE_TOLERANCE_PCT) return BALANCE_STATUSES.BORDERLINEreturn BALANCE_STATUSES.WEAK}@@ -218,7 +247,7 @@ export function computeBalance(S, template) {const ids = exerciseIdsFor(S, template, role)const current = role.evaluationMode === EVALUATION_MODES.REP_COUNT? resolveCurrentReps(S, ids)- : resolveCurrent(S, ids, bodyweightKg)+ : resolveCurrent(S, ids, bodyweightKg, role.evaluationMode === EVALUATION_MODES.BODYWEIGHT_RATIO ? role.reps : null)return [role.id, current]}))diff --git a/frontend/src/lib/structuralBalance.test.js b/frontend/src/lib/structuralBalance.test.jsindex 7c232f536..d2be893dc 100644--- a/frontend/src/lib/structuralBalance.test.js+++ b/frontend/src/lib/structuralBalance.test.js@@ -1,5 +1,5 @@import { describe, expect, it } from 'vitest'-import { computeBalance, classify, ratioFor, resolveCurrent, resolveCurrentReps, targetFor, overrideKey, overrideIdOf, withOverride, loadForReps } from './structuralBalance.js'+import { computeBalance, classify, ratioFor, resolveCurrent, resolveCurrentReps, targetFor, overrideKey, overrideIdOf, withOverride, loadForReps, loadAtReps } from './structuralBalance.js'import { TEMPLATES, EVALUATION_MODES, BALANCE_STATUSES, BORDERLINE_BAND_PCT } from './structuralBalanceTemplates.js'const NOW = Date.parse('2026-01-15T00:00:00Z')@@ -107,7 +107,7 @@ describe('computeBalance — bodyweight-ratio mode (ATG)', () => {// 58 kg for 12 at 80 kg estimates an 81.2 kg 1RM — over body weight, but 72.5% of the// body-weight-for-12 the standard asks for.const rdl = computeBalance(sWithBw(80, 58), template).find(r => r.roleId === 'romanianDeadlift')- expect(rdl.current.estKg).toBeCloseTo(81.2)+ expect(rdl.current.repsKg).toBeCloseTo(58)expect(rdl.actualPct).toBeCloseTo(72.5, 1)expect(rdl.status).toBe(BALANCE_STATUSES.WEAK)// A heavy single is read forward the same way: 100 kg once is about 71 kg for 12.@@ -122,6 +122,97 @@ describe('computeBalance — bodyweight-ratio mode (ATG)', () => {}expect(loadForReps(112, 12)).toBeCloseTo(80)expect(loadForReps(90, 1)).toBe(90)+ expect(loadAtReps(81.3, 12, 12)).toBe(81.3) // exactly, not 81.3 × 1.4 rounded and divided back+ expect(loadAtReps(40, 20, 15)).toBe(40) // more reps prove the load, they do not raise it+ expect(loadAtReps(20, 6, 15)).toBeCloseTo(16) // 24 kg 1RM, read at 15+ expect(loadAtReps(100, 1, 12)).toBeCloseTo(100 / 1.4) // a single is its own 1RM+ expect(loadAtReps(0, 12, 12)).toBe(null)+ expect(loadAtReps(40, 0, 12)).toBe(null)+ expect(loadAtReps('x', 12, 12)).toBe(null)+ })++ // The step-up's standard is half body weight for 15: past the 12-rep cap of a 1RM estimate,+ // which is no reason to leave the test itself unscored.+ it('scores the step-up at its own 15 reps, and a set with more reps at its load', () => {+ const stepUp = (id, sets, bw = 80) => computeBalance({+ unit: 'kg', body: 'male', bodyweight: [{ d: '2026-01-01', w: bw }], workouts: [workoutAt(id, NOW, sets)],+ }, template).find(r => r.roleId === 'stepUp')++ const atStandard = stepUp('0114', [setDone(40, 15)])+ expect(atStandard.actualPct).toBeCloseTo(50)+ expect(atStandard.status).toBe(BALANCE_STATUSES.BALANCED)+ expect(atStandard.current).toMatchObject({ repsKg: 40, w: 40, r: 15 })++ expect(stepUp('0114', [setDone(40, 20)]).actualPct).toBeCloseTo(50)+ expect(stepUp('0114', [setDone(38, 13)]).status).toBe(BALANCE_STATUSES.BORDERLINE) // 38 × (1 + 13/30) / 1.5 = 36.3 kg, 45.4%+ })++ it('reads a unilateral set by its better side, never by the both-sides total', () => {+ const perSide = (L, R) => ({ done: true, w: Math.max(L[0], R[0]), r: L[1] + R[1], sides: {+ L: { w: L[0], r: L[1], done: true }, R: { w: R[0], r: R[1], done: true },+ } })+ const role = (roleId, id, set) => computeBalance({+ unit: 'kg', body: 'male', bodyweight: [{ d: '2026-01-01', w: 80 }], workouts: [workoutAt(id, NOW, [set])],+ }, template).find(r => r.roleId === roleId)++ // 20 kg for 6 a leg: a 24 kg 1RM, 16 kg for 15 — 20% of 80 kg, not the 23.3% the 12-rep total read.+ const sixALeg = role('stepUp', '0431', perSide([20, 6], [20, 6]))+ expect(sixALeg.actualPct).toBeCloseTo(20)+ expect(sixALeg.current).toMatchObject({ w: 20, r: 6 })+ // 7 a leg (14 in total) and the full 15 a leg (30) score instead of reading as no data.+ expect(role('stepUp', '0431', perSide([20, 7], [20, 7])).actualPct).toBeCloseTo(20 * (1 + 7 / 30) / 1.5 / 80 * 100)+ const fifteenALeg = role('stepUp', '0431', perSide([40, 15], [40, 15]))+ expect(fifteenALeg.status).toBe(BALANCE_STATUSES.BALANCED)+ expect(fifteenALeg.actualPct).toBeCloseTo(50)+ // The stronger side is the reading, whichever it is.+ expect(role('stepUp', '0431', perSide([20, 6], [25, 6])).current).toMatchObject({ w: 25, r: 6 })++ // "Each hand, 8 reps" logged per side on a one-arm or alternating press: 8, not 16.+ const shoulder = role('dbShoulderPress', '0361', perSide([26.4, 8], [26.4, 8]))+ expect(shoulder.status).toBe(BALANCE_STATUSES.BALANCED)+ expect(shoulder.actualPct).toBeCloseTo(33)+ expect(role('dbShoulderPress', '0360', perSide([26.4, 8], [26.4, 8])).actualPct).toBeCloseTo(33)+ const incline = role('inclineDbPress', '3545', perSide([32, 8], [32, 8]))+ expect(incline.status).toBe(BALANCE_STATUSES.BALANCED)+ expect(incline.actualPct).toBeCloseTo(40)+ })++ it('reads a unilateral set by side for a 1RM too', () => {+ const S = { unit: 'kg', workouts: [workoutAt('0431', NOW, [+ { done: true, w: 30, r: 10, sides: { L: { w: 30, r: 5, done: true }, R: { w: 30, r: 5, done: true } } },+ ])] }+ expect(resolveCurrent(S, ['0431'], 80).estKg).toBeCloseTo(35) // 30 × (1 + 5/30), not 30 × (1 + 10/30)+ const eightAHand = { unit: 'kg', workouts: [workoutAt('0361', NOW, [+ { done: true, w: 20, r: 16, sides: { L: { w: 20, r: 8, done: true }, R: { w: 20, r: 8, done: true } } },+ ])] }+ expect(resolveCurrent(eightAHand, ['0361'], 80).estKg).toBeCloseTo(25.3) // 16 in total is past the rep cap+ })++ // ATG's test is to load the bar to the standard, so a lifter lands exactly on it: that has to+ // read as meeting it, in either unit and at any body weight, not as Borderline next to "100%".+ it('a set of exactly the standard is balanced at any body weight, in kg and in lb', () => {+ expect(computeBalance({ unit: 'lb', body: 'male', bodyweight: [{ d: '2026-01-01', w: 180 }],+ workouts: [workoutAt('0085', NOW, [setDone(180, 12)])] }, template)+ .find(r => r.roleId === 'romanianDeadlift').status).toBe(BALANCE_STATUSES.BALANCED)++ const ratioRoles = template.roles.filter(r => r.evaluationMode === EVALUATION_MODES.BODYWEIGHT_RATIO)+ const weighIns = [+ ...Array.from({ length: 401 }, (_, i) => ['kg', 60 + i / 10]),+ ...Array.from({ length: 121 }, (_, i) => ['lb', 140 + i / 2]),+ ]+ const missed = []+ for (const body of ['male', 'female']) {+ for (const role of ratioRoles) {+ for (const [unit, bw] of weighIns) {+ const S = { ...sWithBw(bw, 0), unit, body }+ const target = targetFor(role, S)+ S.workouts = [workoutAt(role.exerciseIds[0], NOW, [setDone(bw * target / 100, role.reps)])]+ const row = computeBalance(S, template).find(r => r.roleId === role.id)+ if (row.status !== BALANCE_STATUSES.BALANCED) missed.push(`${body} ${role.id} ${bw} ${unit}: ${row.actualPct}`)+ }+ }+ }+ expect(missed).toEqual([])})it('no-data when bodyweight was never logged', () => {diff --git a/frontend/src/lib/structuralBalanceTemplates.js b/frontend/src/lib/structuralBalanceTemplates.jsindex bdcc44e93..62d8f37f8 100644--- a/frontend/src/lib/structuralBalanceTemplates.js+++ b/frontend/src/lib/structuralBalanceTemplates.js@@ -35,8 +35,8 @@ export const BORDERLINE_BAND_PCT = 5// targetPctFemale bodyweight-ratio only, when the source gives a distinct female figure for// the same exercise — picked when S.body === 'female'// reps bodyweight-ratio only: how many reps the source's load is for ("body-// weight for 12"). The estimated 1RM is read back at that many reps before-// it is compared (structuralBalance.js loadForReps)+// weight for 12"). A set is read as the load it shows for that many reps+// before it is compared (structuralBalance.js loadAtReps)// repsTarget rep-count only: target rep count at bodyweight// repsTargetFemale rep-count only, when the source gives a distinct female rep target