diff --git a/frontend/package-lock.json b/frontend/package-lock.json
index 7ca26e4..fbdf56d 100644
--- a/frontend/package-lock.json
+++ b/frontend/package-lock.json
@@ -24,6 +24,7 @@
"@capacitor/cli": "^7.6.8",
"@capacitor/ios": "^7.6.8",
"@vitejs/plugin-react": "^6.0.3",
+ "linkedom": "^0.18.13",
"vite": "^8.1.5",
"vitest": "^4.1.10"
}
@@ -2594,6 +2595,13 @@
"url": "https://github.com/sponsors/fb55"
}
},
+ "node_modules/cssom": {
+ "version": "0.5.0",
+ "resolved": "https://registry.npmjs.org/cssom/-/cssom-0.5.0.tgz",
+ "integrity": "sha512-iKuQcq+NdHqlAcwUY0o/HL69XQrUaQdMjmStJ8JFmUaiiQErlhrmuigkg/CU4E2J0IyUKUrMAgl36TvN67MqTw==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/dargs": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/dargs/-/dargs-7.0.0.tgz",
@@ -3337,6 +3345,105 @@
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
"dev": true
},
+ "node_modules/html-escaper": {
+ "version": "3.0.3",
+ "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz",
+ "integrity": "sha512-RuMffC89BOWQoY0WKGpIhn5gX3iI54O6nRA0yC124NYVtzjmFWBIiFd8M0x+ZdX0P9R4lADg1mgP8C7PxGOWuQ==",
+ "dev": true,
+ "license": "MIT"
+ },
+ "node_modules/htmlparser2": {
+ "version": "10.1.0",
+ "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-10.1.0.tgz",
+ "integrity": "sha512-VTZkM9GWRAtEpveh7MSF6SjjrpNVNNVJfFup7xTY3UpFtm67foy9HDVXneLtFVt4pMz5kZtgNcvCniNFb1hlEQ==",
+ "dev": true,
+ "funding": [
+ "https://github.com/fb55/htmlparser2?sponsor=1",
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/fb55"
+ }
+ ],
+ "license": "MIT",
+ "dependencies": {
+ "domelementtype": "^2.3.0",
+ "domhandler": "^5.0.3",
+ "domutils": "^3.2.2",
+ "entities": "^7.0.1"
+ }
+ },
+ "node_modules/htmlparser2/node_modules/dom-serializer": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz",
+ "integrity": "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "domelementtype": "^2.3.0",
+ "domhandler": "^5.0.2",
+ "entities": "^4.2.0"
+ },
+ "funding": {
+ "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1"
+ }
+ },
+ "node_modules/htmlparser2/node_modules/dom-serializer/node_modules/entities": {
+ "version": "4.5.0",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz",
+ "integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "engines": {
+ "node": ">=0.12"
+ },
+ "funding": {
+ "url": "https://github.com/fb55/entities?sponsor=1"
+ }
+ },
+ "node_modules/htmlparser2/node_modules/domhandler": {
+ "version": "5.0.3",
+ "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-5.0.3.tgz",
+ "integrity": "sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "dependencies": {
+ "domelementtype": "^2.3.0"
+ },
+ "engines": {
+ "node": ">= 4"
+ },
+ "funding": {
+ "url": "https://github.com/fb55/domhandler?sponsor=1"
+ }
+ },
+ "node_modules/htmlparser2/node_modules/domutils": {
+ "version": "3.2.2",
+ "resolved": "https://registry.npmjs.org/domutils/-/domutils-3.2.2.tgz",
+ "integrity": "sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "dependencies": {
+ "dom-serializer": "^2.0.0",
+ "domelementtype": "^2.3.0",
+ "domhandler": "^5.0.3"
+ },
+ "funding": {
+ "url": "https://github.com/fb55/domutils?sponsor=1"
+ }
+ },
+ "node_modules/htmlparser2/node_modules/entities": {
+ "version": "7.0.1",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-7.0.1.tgz",
+ "integrity": "sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "engines": {
+ "node": ">=0.12"
+ },
+ "funding": {
+ "url": "https://github.com/fb55/entities?sponsor=1"
+ }
+ },
"node_modules/ieee754": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
@@ -3826,6 +3933,181 @@
"integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==",
"dev": true
},
+ "node_modules/linkedom": {
+ "version": "0.18.13",
+ "resolved": "https://registry.npmjs.org/linkedom/-/linkedom-0.18.13.tgz",
+ "integrity": "sha512-ES/o9qotMpzpN2MHs+Iq/JcVoOj8Fa5wiQYrTdFpvAnwXL0g66XHHUc9WUMk6nAlBtGsFQ24ne+SYnvnaQ2FSw==",
+ "dev": true,
+ "license": "ISC",
+ "dependencies": {
+ "css-select": "^7.0.0",
+ "cssom": "^0.5.0",
+ "html-escaper": "^3.0.3",
+ "htmlparser2": "^10.1.0",
+ "uhyphen": "^0.2.0"
+ },
+ "engines": {
+ "node": ">=16"
+ },
+ "peerDependencies": {
+ "canvas": ">= 2"
+ },
+ "peerDependenciesMeta": {
+ "canvas": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/linkedom/node_modules/boolbase": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-2.0.0.tgz",
+ "integrity": "sha512-DkVaaQHymRhpYEYo9x1oo7Q7B0Y6KJUsjm3c9eTyFDby4MHLBTwZ6ZDWBel5zrYxj1WsZgC5oLpiz+93MluXeA==",
+ "dev": true,
+ "license": "ISC",
+ "engines": {
+ "node": ">=20.19.0"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/fb55"
+ }
+ },
+ "node_modules/linkedom/node_modules/css-select": {
+ "version": "7.0.0",
+ "resolved": "https://registry.npmjs.org/css-select/-/css-select-7.0.0.tgz",
+ "integrity": "sha512-snmjEVXy+1LnwXdxhYvTMj1d9tOh4HxkA1YmoayVBeeyR2C14Pum7fcxJIm4SswYspVy866eYNwlH6xC3/VH5g==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "dependencies": {
+ "boolbase": "^2.0.0",
+ "css-what": "^8.0.0",
+ "domhandler": "^6.0.1",
+ "domutils": "^4.0.2",
+ "nth-check": "^3.0.1"
+ },
+ "engines": {
+ "node": ">=20.19.0"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/fb55"
+ }
+ },
+ "node_modules/linkedom/node_modules/css-what": {
+ "version": "8.0.0",
+ "resolved": "https://registry.npmjs.org/css-what/-/css-what-8.0.0.tgz",
+ "integrity": "sha512-DH0Bqq3DNp5tdOReuNyAA+Ev4Y2GS5FMbZpeTLP6C4CDi0h5nL0BmUPChXw3o/qbHLDWHl49sbNqQVY7bMSDdw==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "engines": {
+ "node": ">=20.19.0"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/fb55"
+ }
+ },
+ "node_modules/linkedom/node_modules/dom-serializer": {
+ "version": "3.1.1",
+ "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-3.1.1.tgz",
+ "integrity": "sha512-4MEa38/QexBob6gFNwu+EGdWvhJ1OKuNwdYY3Y3NyeWDQfnGeDYQUDfIRzWu5B5gsv03so2Uxd28YC6zrsx3Lw==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "domelementtype": "^3.0.0",
+ "domhandler": "^6.0.0",
+ "entities": "^8.0.0"
+ },
+ "engines": {
+ "node": ">=20.19.0"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1"
+ }
+ },
+ "node_modules/linkedom/node_modules/domelementtype": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-3.0.0.tgz",
+ "integrity": "sha512-umCQid3jKbDmVjx8jGaW7uUykm4DEUeyV21hPxNMo2nV955DhUThwqyOIDtreepP31hl84X7G5U9ZfsWvIB3Pg==",
+ "dev": true,
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/fb55"
+ }
+ ],
+ "license": "BSD-2-Clause",
+ "engines": {
+ "node": ">=20.19.0"
+ }
+ },
+ "node_modules/linkedom/node_modules/domhandler": {
+ "version": "6.0.1",
+ "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-6.0.1.tgz",
+ "integrity": "sha512-gYzvtM72ZtxQO0T048kd6HWSbbGCNOUwcnfQ01cqIJ4X2IYKFFHZ5mKvrQETcFXxsRObZulDaKmy//R7TPtsBg==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "dependencies": {
+ "domelementtype": "^3.0.0"
+ },
+ "engines": {
+ "node": ">=20.19.0"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/fb55/domhandler?sponsor=1"
+ }
+ },
+ "node_modules/linkedom/node_modules/domutils": {
+ "version": "4.0.2",
+ "resolved": "https://registry.npmjs.org/domutils/-/domutils-4.0.2.tgz",
+ "integrity": "sha512-qI4JLRKnSzqFqr7hAlS5xQDusBCjKSEG4t4+7aNrIQMHBcsC2TGEhuyABJdYkgSewL57PNLYEiibY2iPKhKpaA==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "dependencies": {
+ "dom-serializer": "^3.0.0",
+ "domelementtype": "^3.0.0",
+ "domhandler": "^6.0.0"
+ },
+ "engines": {
+ "node": ">=20.19.0"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/fb55/domutils?sponsor=1"
+ }
+ },
+ "node_modules/linkedom/node_modules/entities": {
+ "version": "8.0.0",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-8.0.0.tgz",
+ "integrity": "sha512-zwfzJecQ/Uej6tusMqwAqU/6KL2XaB2VZ2Jg54Je6ahNBGNH6Ek6g3jjNCF0fG9EWQKGZNddNjU5F1ZQn/sBnA==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "engines": {
+ "node": ">=20.19.0"
+ },
+ "funding": {
+ "url": "https://github.com/fb55/entities?sponsor=1"
+ }
+ },
+ "node_modules/linkedom/node_modules/nth-check": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-3.0.1.tgz",
+ "integrity": "sha512-GX0gsdbGVCgnRgbeGaubfjpBXyYRWOOCVeYh08bSQvDZqxz5ndXs1OTfAt/h36G1xvI94YIspsI0sVFqAV9+RQ==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "dependencies": {
+ "boolbase": "^2.0.0"
+ },
+ "engines": {
+ "node": ">=20.19.0"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/fb55/nth-check?sponsor=1"
+ }
+ },
"node_modules/load-json-file": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz",
@@ -5886,6 +6168,13 @@
"node": ">=0.8.0"
}
},
+ "node_modules/uhyphen": {
+ "version": "0.2.0",
+ "resolved": "https://registry.npmjs.org/uhyphen/-/uhyphen-0.2.0.tgz",
+ "integrity": "sha512-qz3o9CHXmJJPGBdqzab7qAYuW8kQGKNEuoHFYrBwV6hWIMcpAmxDLXojcHfFr9US1Pe6zUswEIJIbLI610fuqA==",
+ "dev": true,
+ "license": "ISC"
+ },
"node_modules/undici-types": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.3.0.tgz",
diff --git a/frontend/package.json b/frontend/package.json
index b8b70ca..3f58498 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -28,6 +28,7 @@
"@capacitor/cli": "^7.6.8",
"@capacitor/ios": "^7.6.8",
"@vitejs/plugin-react": "^6.0.3",
+ "linkedom": "^0.18.13",
"vite": "^8.1.5",
"vitest": "^4.1.10"
}
diff --git a/frontend/src/components/BodyMap.jsx b/frontend/src/components/BodyMap.jsx
index fe71c47..022f180 100644
--- a/frontend/src/components/BodyMap.jsx
+++ b/frontend/src/components/BodyMap.jsx
@@ -47,11 +47,13 @@ function View({ view, levels, onMuscle, selected }) {
/**
*
* `load` is effective sets per muscle (see lib/muscles.js); shading is relative to
- * the hardest-worked muscle in that same load, so it always reads as a balance.
+ * the hardest-worked muscle in that same load, so it always reads as a balance. Pass ordered
+ * `{ at, level, exclusive? }` `thresholds` for a fixed absolute scale (recovery views use this
+ * to keep their semantic bands stable); omitting it preserves the balance behavior.
*/
-export default function BodyMap({ load = {}, body = 'male', onMuscle, selected, className = '' }) {
+export default function BodyMap({ load = {}, thresholds, body = 'male', onMuscle, selected, className = '' }) {
const paths = useBodyPaths()
- const levels = levelsOf(load)
+ const levels = levelsOf(load, thresholds)
const g = paths && (paths[body] || paths.male)
return (
diff --git a/frontend/src/lib/muscles.js b/frontend/src/lib/muscles.js
index c744812..32db0d9 100644
--- a/frontend/src/lib/muscles.js
+++ b/frontend/src/lib/muscles.js
@@ -119,11 +119,31 @@ export const loadOfActive = active =>
loadOf((active?.entries || []).map(e => ({ id: e.id, sets: (e.sets || []).filter(s => s.done).length })))
/**
- * Shade buckets 0–4 per muscle, relative to the hardest-worked muscle in the same
- * window. Relative rather than absolute on purpose: the map answers "is my training
- * balanced", which only means anything as a comparison within one period.
+ * Shade buckets 0–4 per muscle.
+ *
+ * With no `thresholds`, levels remain relative to the hardest-worked muscle in the same
+ * window. This is the original balance-map behavior. When `thresholds` is supplied, levels
+ * use an absolute scale instead: it is an ordered array of `{ at, level, exclusive? }` rules,
+ * and the last matching rule wins. An exclusive rule matches values strictly greater than
+ * `at`; otherwise the boundary is inclusive. Values below the first matching rule are l0.
+ * Absolute rules let recovery views keep fixed semantic bands instead of renormalizing to the
+ * strongest muscle on screen.
*/
-export function levelsOf(load) {
+export function levelsOf(load, thresholds) {
+ if (thresholds) {
+ const lv = {}
+ MUSCLES.forEach(m => {
+ const value = Number(load[m] || 0)
+ let level = 0
+ for (const rule of thresholds) {
+ const matches = rule.exclusive ? value > rule.at : value >= rule.at
+ if (matches) level = Math.max(0, Math.min(4, rule.level))
+ }
+ lv[m] = level
+ })
+ return lv
+ }
+
const max = Math.max(0, ...MUSCLES.map(m => load[m] || 0))
const lv = {}
MUSCLES.forEach(m => {
diff --git a/frontend/src/lib/recovery-view.js b/frontend/src/lib/recovery-view.js
new file mode 100644
index 0000000..1abc6ff
--- /dev/null
+++ b/frontend/src/lib/recovery-view.js
@@ -0,0 +1,17 @@
+import { FATIGUE_STATES } from './recovery.js'
+
+/**
+ * Select the consumer-facing fatigue state for one numeric recovery value.
+ * Values below .25 are ready, .25 through .5 are recovering, and values above .5 are fatigued.
+ * Keeping this boundary selector in production code prevents views and tests from drifting apart.
+ *
+ * @param {number} value Numeric fatigue value from fatigueOf().
+ * @returns {'ready'|'recovering'|'fatigued'} The stable state label for the UI.
+ */
+export function fatigueStateOf(value) {
+ return value < 0.25
+ ? FATIGUE_STATES.READY
+ : value <= 0.5
+ ? FATIGUE_STATES.RECOVERING
+ : FATIGUE_STATES.FATIGUED
+}
diff --git a/frontend/src/lib/recovery-view.test.js b/frontend/src/lib/recovery-view.test.js
new file mode 100644
index 0000000..6ccc7e6
--- /dev/null
+++ b/frontend/src/lib/recovery-view.test.js
@@ -0,0 +1,58 @@
+import { describe, expect, it } from 'vitest'
+import { FATIGUE_STATES, fatigueOf, strengthOf } from './recovery.js'
+import { fatigueStateOf } from './recovery-view.js'
+import { MUSCLES, levelsOf } from './muscles.js'
+
+const FATIGUE_BANDS = [
+ { at: 0, level: 4 },
+ { at: 0.25, level: 2 },
+ { at: 0.5, level: 0, exclusive: true },
+]
+const STRENGTH_BANDS = [
+ { at: 0.5, level: 0 },
+ { at: 0.625, level: 1 },
+ { at: 0.75, level: 2 },
+ { at: 0.875, level: 3 },
+ { at: 1, level: 4 },
+]
+
+describe('production recovery view selectors', () => {
+ it('preserves the relative balance scale when no thresholds are supplied', () => {
+ const levels = levelsOf({ chest: 10, biceps: 5 })
+ expect(levels.chest).toBe(4)
+ expect(levels.biceps).toBe(2)
+ })
+
+ it('uses the production fatigue selector at both approved boundaries', () => {
+ expect(fatigueStateOf(0.2499)).toBe(FATIGUE_STATES.READY)
+ expect(fatigueStateOf(0.25)).toBe(FATIGUE_STATES.RECOVERING)
+ expect(fatigueStateOf(0.5)).toBe(FATIGUE_STATES.RECOVERING)
+ expect(fatigueStateOf(0.5001)).toBe(FATIGUE_STATES.FATIGUED)
+ })
+
+ it('renders fatigue bands on a fixed absolute scale rather than relative to the map maximum', () => {
+ const levels = levelsOf({
+ chest: 0.25,
+ biceps: 0.6,
+ triceps: 0.1,
+ }, FATIGUE_BANDS)
+ expect(levels.chest).toBe(2)
+ expect(levels.biceps).toBe(0)
+ expect(levels.triceps).toBe(4)
+ })
+
+ it('renders strength on fixed .5-to-1 bands with full retention at l4', () => {
+ const levels = levelsOf({ chest: 0.9, biceps: 1, triceps: 0.5 }, STRENGTH_BANDS)
+ expect(levels.chest).toBe(3)
+ expect(levels.biceps).toBe(4)
+ expect(levels.triceps).toBe(0)
+ })
+
+ it('keeps selectors tied to the same render-time clock', () => {
+ const now = Date.UTC(2026, 0, 22)
+ const workout = { start: now - 15 * 86400000, d: '2026-01-07', entries: [] }
+ expect(strengthOf([workout], now).chest).toBe(0.5)
+ expect(fatigueOf([workout], now).chest).toBe(0)
+ expect(strengthOf([workout], now + 7 * 86400000).chest).toBe(0.5)
+ })
+})
diff --git a/frontend/src/lib/recovery.js b/frontend/src/lib/recovery.js
new file mode 100644
index 0000000..54a2793
--- /dev/null
+++ b/frontend/src/lib/recovery.js
@@ -0,0 +1,188 @@
+import { EXIDX } from './exercises.js'
+import { MUSCLES, musclesOf } from './muscles.js'
+
+/** Completed-workout window used when calculating current fatigue. */
+export const FATIGUE_WINDOW_MS = 259200000
+
+/** Exponential half-life for fatigue stimulus. */
+export const FATIGUE_HALF_LIFE_MS = 129600000
+
+/** Period after training during which retained strength remains at full value. */
+export const STRENGTH_FULL_MS = 1209600000
+
+/** Exponential half-life for retained strength after the full-retention period. */
+export const STRENGTH_HALF_LIFE_MS = 2419200000
+
+/** Minimum retained-strength value for an untrained or fully detrained muscle. */
+export const STRENGTH_FLOOR = 0.5
+
+/**
+ * Stable labels for consumer fatigue buckets: values below 0.25 are ready, values from 0.25
+ * through 0.5 are recovering, and values above 0.5 are fatigued.
+ */
+export const FATIGUE_STATES = Object.freeze({
+ READY: 'ready',
+ RECOVERING: 'recovering',
+ FATIGUED: 'fatigued',
+})
+
+/**
+ * Return exponential decay expressed as a fraction of one half-life.
+ *
+ * @param {number} ageMs Elapsed age of the stimulus in milliseconds.
+ * @param {number} halfLifeMs Duration of one half-life in milliseconds.
+ * @returns {number} Remaining fraction, using the exact `0.5 ** (age / halfLife)` formula.
+ */
+export function halfLifeDecay(ageMs, halfLifeMs) {
+ return 0.5 ** (ageMs / halfLifeMs)
+}
+
+// The v2 data contract has one timestamp per workout, not per set. Keep this fallback in one
+// place so fatigue and strength use exactly the same stimulus time as effort.js.
+function workoutTimestamp(workout) {
+ const timestamp = workout?.start || new Date(workout?.d).getTime()
+ return Number.isFinite(timestamp) ? timestamp : Number(timestamp)
+}
+
+function emptyMuscleMap(value) {
+ return Object.fromEntries(MUSCLES.map(slug => [slug, value]))
+}
+
+function clamp(value, min, max) {
+ return Math.min(max, Math.max(min, value))
+}
+
+// Yield one weighted stimulus per completed set. Repeating the exercise's muscle weights for
+// every done set is equivalent to multiplying musclesOf(ex) by the completed-set count while
+// preserving the workout/set shape already used by the app.
+function completedStimuli(workouts, include) {
+ const stimuli = []
+ for (const workout of workouts || []) {
+ const timestamp = workoutTimestamp(workout)
+ if (!Number.isFinite(timestamp) || !include(timestamp)) continue
+ for (const entry of workout.entries || []) {
+ const weights = musclesOf(EXIDX[entry.id])
+ for (const set of entry.sets || []) {
+ if (set?.done !== true) continue
+ for (const [slug, weight] of Object.entries(weights)) {
+ if (Object.prototype.hasOwnProperty.call(MUSCLES_BY_SLUG, slug)) {
+ stimuli.push({ slug, timestamp, stimulus: weight })
+ }
+ }
+ }
+ }
+ }
+ return stimuli
+}
+
+const MUSCLES_BY_SLUG = Object.fromEntries(MUSCLES.map(slug => [slug, true]))
+
+function fatigueValue(events, now) {
+ if (!events.length) return 0
+ events.sort((a, b) => a.timestamp - b.timestamp)
+
+ let value = 0
+ let lastTimestamp = events[0].timestamp
+ for (const event of events) {
+ value *= halfLifeDecay(event.timestamp - lastTimestamp, FATIGUE_HALF_LIFE_MS)
+ value += event.stimulus
+ lastTimestamp = event.timestamp
+ }
+ value *= halfLifeDecay(now - lastTimestamp, FATIGUE_HALF_LIFE_MS)
+ return clamp(value, 0, 1)
+}
+
+/**
+ * Calculate current per-muscle fatigue from completed sets in the recent window.
+ *
+ * Stimulus time is `workout.start`, falling back to the workout date `workout.d`. Each completed
+ * set contributes the exercise's `musclesOf` weights; sets exactly 72 hours old are excluded.
+ * Stimuli are accumulated chronologically with a 36-hour half-life, decayed to `now`, and
+ * clamped to [0, 1]. The result always contains every drawable muscle slug.
+ *
+ * @param {Array