Skip to content

refactor(effort): resolve the day's HRmax in one place (#2438) - #1

Open
bartmuskala wants to merge 1 commit into
mainfrom
refactor/effort-hrmax-resolver
Open

bartmuskala wants to merge 1 commit into
mainfrom
refactor/effort-hrmax-resolver

Conversation

@bartmuskala

Copy link
Copy Markdown
Owner

refactor(effort): resolve the day's HRmax in one place (ryanbr#2438)

What this PR does

Prepares ryanbr#2438 step 0 without changing any score.

The HRmax a day's Effort is scored against ("manual override, else Tanaka from age, else the default") was written out by hand at six call sites. Two such copies had already dropped the override, which became ryanbr#2460. In the thread, @ryanbr noted that step 0 changes what "the effective HRmax" means, so every one of these sites has to move together or they drift apart. This PR gives them one place to move.

  • Adds StrainScorer.effortHRmax(override:age:) (Swift) and StrainScorer.effortHRmax(override, age) (Kotlin). Each returns the value and its source: override, tanaka or default.
  • Routes every day-Effort reader through it: AnalyticsEngine on both platforms, DayCycleIntelligenceIntegration, PhysiologicalStepCycleEngine, TodayScreen.kt and ProfileStore.effortHRmax (which the two iOS Today views already read since ios: score today's Effort against the same HRmax as the day it belongs to (#2460) ryanbr/noop#2461).
  • The src= field on the effort calib line (feat(diag): name the day's HRmax source and its observed peak (#2438) ryanbr/noop#2459) now comes from the same call that produces the HRmax, instead of a second hand-written branch beside it. The value and its label can no longer disagree.
  • Adds Tools/test_effort_hrmax_resolver.py. The existing EffortHRmaxOverrideTests header says plainly that it cannot see the view call sites, so a hand-rolled Tanaka in a view would leave it green. This guard closes that gap from source, following AGENTS.md's "assert the single resolver":
    • it fails on any override-then-age-formula fallback written by hand in product source;
    • it asserts each known day-Effort reader names the resolver;
    • it fails if an exemption goes stale, so the exemption list can only shrink.
  • Refreshes the parity authority (Tools/parity_twin_map.json). The new resolver registers as a twin pair: function pairs 226 → 227.

Not a scoring change. The resolver returns exactly what each replaced expression returned, so there is no recipe bump, no rescore, and docs/VALIDATION_PROTOCOL.md does not apply.

Type of change

  • Refactor / cleanup

How it was tested

  • Python (CI linux-capture): the full Tools/ suite passes (346 tests), including the new guard and test_parity_governance_acceptance. doc_comment_lint and parity_ledger report nothing new.
  • The guard fails on main: run against main's copies of the six routed files, it flags every original hand-written site (AnalyticsEngine.swift 920/943, AnalyticsEngine.kt 832/864, DayCycleIntelligenceIntegration 203, PhysiologicalStepCycleEngine 219, TodayScreen 1042, Profile 203). It passes on this branch.
  • Kotlin resolver oracle: StrainScorer.kt was compiled standalone (kotlinc 2.0.21) and run over the shared case table, plus 100 ages against tanakaHRmax. It was also checked for exact == equality against the old hand-written expression, value and source, on a 5 × 8 grid of overrides and ages that includes negative, zero and fractional ages.
  • New unit tests: EffortHRmaxResolverTests.swift and EffortHRmaxResolverTest.kt carry the same literal case table, so each platform guards the other.
  • Not run locally: I had no Swift toolchain and no Android SDK available, so this PR relies on CI for the Swift package tests, the app build, the Kotlin call-site compile and the Android unit tests (swift-packages, app-build, android). The Swift change is small: one function, plus three call sites that now destructure a labelled tuple.

Checklist

  • Swift package tests pass for any package I touched: relying on CI (no local toolchain)
  • Android unit tests pass: relying on CI (resolver verified standalone, see above)
  • No new build warnings introduced (Kotlin resolver compiled clean; Swift via CI)
  • UI changes use only StrandDesign tokens: no UI changes
  • No hardcoded hex frame bytes
  • Follows docs/CONTRIBUTING.md (both platforms in one PR, per the parity contract)
  • No generated output or secrets committed

Out of scope, and found while doing this

The audit for this PR found HRmax resolved in more places than the six listed in the thread. These sites score workouts, not days. Routing them through the resolver would change stored values, so they are left alone here and proposed as a separate issue (draft attached to this PR's discussion):

  • Saved, live and HealthKit workouts, plus the workout-strain backfill, score against ProfileStore.hrMax. That value rounds Tanaka to an Int and returns 208 for an age-less profile, so a manual workout and an auto-detected bout in the same day use different HRmax values.
  • Cross-platform divergence: the manual-workout rescore uses the rounded Double(profile.hrMax) on Swift (IntelligenceEngine.swift, rescoreManualWorkouts) but the unrounded 208.0 - 0.7 * profile.age on Kotlin (IntelligenceEngine.kt:2334, whose comment says it matches endWorkout; it does not). The two platforms can store different Effort values for the same manual workout. That site is the one named exemption in the guard, with this reason attached.

Refs ryanbr#2438, ryanbr#2460

The HRmax a day's Effort is scored against ("manual override, else Tanaka
from age, else the default") was written out by hand at six call sites
across Swift and Kotlin. Two such copies had already dropped the override
(ryanbr#2460). Step 0 of ryanbr#2438 changes what the effective HRmax means, so every
reader has to change together.

- Add StrainScorer.effortHRmax(override:age:) and its Kotlin twin, returning
  the value and its source (override / tanaka / default).
- Route AnalyticsEngine (both), DayCycleIntelligenceIntegration,
  PhysiologicalStepCycleEngine, TodayScreen and ProfileStore.effortHRmax
  through it. The effort calib line's src field now comes from the same
  call instead of a second hand-written branch.
- Tools/test_effort_hrmax_resolver.py fails on any new hand-written
  override-else-formula fallback in product source, and asserts each
  day-Effort reader names the resolver.
- Refresh the parity authority for the new twin pair (226 -> 227).

No scoring change: the resolver returns exactly what each replaced
expression returned. No recipe bump, no rescore.

Refs ryanbr#2438, ryanbr#2460
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant