fix(coach): weekly avg steps divides by days-with-data, not fixed 7 slots - #68
Merged
saksham2001 merged 3 commits intoJul 12, 2026
Merged
Conversation
CoachContextBuilder built the week's average steps as total / weekSteps.count. But steps7d is always 7 entries (current Mon-Sun week, missing days zero-filled by PulseServices alignedRows), so the denominator was always 7 regardless of how many days had data. Early in the week / with sparse sync this badly understates the average fed to the coach — e.g. 8,000 steps on Monday reported as 1,142/day — and contradicts the sibling daysAvailable / totalSteps fields. Divide by daysAvailable (days with steps > 0) instead, guarding the zero case.
saksham2001
approved these changes
Jul 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
CoachContextBuildercomputes the week's average steps asweekSteps.reduce(0,+) / weekSteps.count. Butsummary.trends.steps7dis built (PulseServices.swift:330) as(0..<7).map { byDay[date]?.first ?? ActivityDaily(source: "none") }— always 7 entries for the current Mon–Sun week, with missing days zero-filled. So the denominator is always 7, regardless of how many days actually have data.Impact
The average is understated whenever the week isn't full of data (early in the week, sparse sync). Example: it's Monday, the user walked 8,000 steps, no other data →
daysAvailable = 1,totalSteps = 8000, butavgSteps = 8000 / 7 = 1142. The coach is handed "average 1,142 steps/day" and reasons/advises on a wrong figure. It also contradicts the siblingdaysAvailableandtotalStepsfields in the same packet.Fix
Divide by
daysAvailable(days with steps > 0), guarding the zero case:Now
avgSteps,daysAvailable, andtotalStepsare mutually consistent (average over days-with-data). One-line change; builds clean.