Merge !101: Detecting progress instead of stall

This commit is contained in:
Duarte Santos committed 2026-09-07 19:37:04 +02:00
commit f940c378be
2 files changed
+65 -9

No files matched your search

+22 -8
View File
@@ -160,16 +160,30 @@ export function sessionsFor(S, exId, fallback) {
return out
}
// How many sessions in a row ended in a miss, counting back from the most recent. A change of
// weight starts a new streak, so there won't be a perpetual stall.
// Rationale: deload should reflect the failures in sessions with weight that earned it.
// Not the lighter weight that follows.
export function stallCount(sessions) {
// Checks how many sessions in a row ended in a miss, counting back from the most recent.
// Now two things end a stall streak (besides a hit of course):
// - A change of weight ends the streak (per pr !93).
// Rationale: deload should reflect the failures in sessions with weight that earned it. Not the lighter weight that follows;
// - Under double progression, a session that beat its best at the current weight.
export function stallCount(sessions, policy) {
let n = 0
for (let i = sessions.length - 1; i >= 0; i--) {
if (sessions[i].ok) break
if (i < sessions.length -1 && sessions[i].weight !== sessions[i+1].weight) break
n++
if (i < sessions.length -1 && sessions[i].weight !== sessions[i+1].weight) break
// Next part limits policy to double. Why? Double is the only policy that deliberately asks for less than it grades against
// (Since it is geared towards climbing through a rep range).
// Specifically, `aim` (see `const aim`) climbs from the bottom of the range while `ok` needs the top.
// The following mechanism ensures that a beat of the best at this weight now counts as progress instead of a stall
// Without this a wider range than two reps would lead to a deload, despite progress (since it could never reach the top within the DELOAD_AFTER))
// Linear and greyskull remain unaffected, as they should.
if (policy === 'double') {
// Following checks within scope of the current session's weight. Deliberately not every session ever done at this weight.
// Rationale: Rebuilding after a deload must not be measured against the reps managed before the deload.
const run = []
for (let j = i - 1; j >= 0 && sessions[j].weight === sessions[i].weight; j--) run.push(sessions[j].low) // Checks the lowest rep count of each session in the run
if (run.length && sessions[i].low > Math.max(...run)) break // Beating the best of the current run is progress
}
n++ // increment stall count when no escape conditions were met
}
return n
}
@@ -195,7 +209,7 @@ export function nextPrescription(S, cfg, routine) {
const last = sessions[sessions.length - 1]
if (!last) return { policy, kind: 'first', why: ['Nothing logged yet — this session sets the baseline.'] }
const stalls = stallCount(sessions)
const stalls = stallCount(sessions, policy)
const deloadAt = DELOAD_AFTER[policy] || 3
if (mode === 'time') {
+43 -1
View File
@@ -68,6 +68,24 @@ describe('stallCount', () => {
expect(stallCount([{ ok: false }, { ok: true }, { ok: false }])).toBe(1)
expect(stallCount([])).toBe(0)
})
// 2 additional guardrails to ensure correct behavior with possible patterns
const miss = (weight, low) => ({ ok: false, weight, low })
it('measures progress against the best of the run, not merely the session before', () => {
// Lows 8,9,8,9,8 at one weight: every other session beats the one immediately before it,
// so comparing only to the previous session would let this yo-yo forever without ever
// deloading. So we test: 9 never beats the earlier 9 and the stall streak stands.
const oscillating = [miss(40, 8), miss(40, 9), miss(40, 8), miss(40, 9), miss(40, 8)]
expect(stallCount(oscillating, 'double')).toBe(3)
})
it('ends the streak at a session that made progress rather than skipping past it', () => {
// Lows 9,9,9,10: the most recent session is a personal best for this run. Checks whether the stall streak ends
// there and counts nothing. Otherwise stallCount would return 3 and deload after porgress had just occurred.
const improvedLast = [miss(40, 9), miss(40, 9), miss(40, 9), miss(40, 10)]
expect(stallCount(improvedLast, 'double')).toBe(0)
})
})
describe('policyFor', () => {
@@ -333,7 +351,7 @@ describe('double progression', () => {
const p = nextPrescription(hist(LIFT, [[40, 10, 9, 9]], { sets: 3, reps: 12 }), cfg)
expect(p.kind).toBe('hold')
expect(p.weight).toBe(40)
expect(p.reps).toBe(10) // worst set was 9 → aim for 10
expect(p.reps).toBe(10) // worst set was 9 -> aim for 10
})
it('never asks for more than the top of the range', () => {
@@ -349,6 +367,30 @@ describe('double progression', () => {
expect(p.weight).toBe(35) // 40 × 0.9 = 36 → nearest loadable 2.5 step
})
it('climbs a range wider than the deload budget instead of cutting on the way up', () => {
// We consider a 8-12 rep range, so 4 rep steps one per session.
// This is graded against DELOAD_AFTER.double which allows for 2 stalls towards progress.
// And only a hit at the top clears a stall.
// With our rep range reaching the top thus cannot be achieved within the current budget of 3.
// Therefore we wish to count the climb as progress as well.
expect(cfg.reps - cfg.repsMin).toBeGreaterThan(DELOAD_AFTER.double - 1)
const target = { sets: 3, reps: cfg.reps }
const rows = []
let p = { weight: cfg.weight, reps: cfg.repsMin }
for (let session = 1; session <= 5; session++) {
// Train exactly what was prescribed, every set, every session.
rows.push([p.weight, ...Array(target.sets).fill(p.reps)])
p = nextPrescription(hist(LIFT, rows, target), cfg)
expect(p.kind).not.toBe('deload')
}
// Five compliant sessions later the top of the range is reached and the weight goes up.
expect(p.kind).toBe('up')
expect(p.weight).toBeGreaterThan(cfg.weight)
expect(p.reps).toBe(cfg.repsMin)
})
it('normalizes persisted per-side bounds before prescribing', () => {
const perSide = { ...cfg, reps: 13, repsMin: 7, side: true }
const p = nextPrescription(hist(LIFT, [[40, 12, 12, 12]], { sets: 3, reps: 13 }), perSide)