Skip to content

fix: trigger score pipeline after health sync for recent dates with no prior backend summary#55

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-sleep-score-fetch-issue
Draft

fix: trigger score pipeline after health sync for recent dates with no prior backend summary#55
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-sleep-score-fetch-issue

Conversation

Copilot AI commented Apr 12, 2026

Copy link
Copy Markdown

When the app hasn't been opened for multiple days, recent dates (within 14 days) with HealthKit data but no backend summary never get scored — blocking recommendations from appearing.

Root cause

triggerOnDemandPipelinesIfNeeded is called before syncRecentDateIfNeeded in HomeSummaryStore.load(). When the backend has no entry for a date, resolvedSummary is nil, and the function exits early:

guard let summary else { return .none }  // fires when backend has no summary yet

Health data is then uploaded by syncRecentDateIfNeeded, but the score pipeline is never triggered — so the backend has data but never computes a score.

Fix

After syncRecentDateIfNeeded uploads health data and the summary is refreshed, trigger the score pipeline if:

  • it wasn't already triggered before the sync (!onDemandTriggerResult.attemptedScore)
  • a summary is now available (resolvedSummary != nil)
if !onDemandTriggerResult.attemptedScore, resolvedSummary != nil {
    let postSyncTriggerResult = await triggerOnDemandPipelinesIfNeeded(
        using: authManager,
        for: selectedDate,
        summary: resolvedSummary
    )
    if postSyncTriggerResult.attemptedAny {
        // invalidate cache and refresh summary
    }
}

This preserves existing behavior for dates where a summary was already present (pipeline triggered before sync, attemptedScore == true) and correctly handles the case where health data needed uploading first.

…no prior summary existed

When a user has HealthKit sleep data for recent dates (within 14 days) but the
backend has no summary for those dates (e.g. because the app was not opened and
health data was never uploaded), the previous code called
triggerOnDemandPipelinesIfNeeded with a nil resolvedSummary, which returned
immediately without triggering the score pipeline. The health data was then
uploaded by syncRecentDateIfNeeded, but no score was ever computed.

Fix: after syncRecentDateIfNeeded uploads health data and the summary is
refreshed, check whether the score pipeline was not already triggered
(onDemandTriggerResult.attemptedScore == false) and a summary is now available
(resolvedSummary != nil). If both conditions hold, call
triggerOnDemandPipelinesIfNeeded again so the backend can compute the score and
subsequently surface recommendations.

Agent-Logs-Url: https://github.com/SleepFocus/SleepFocus_Frontend/sessions/721ff11d-c1ae-4ce7-a851-c0e6963ec073

Co-authored-by: austinkimchi <40729751+austinkimchi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix sleep score not triggered for 14-day range fix: trigger score pipeline after health sync for recent dates with no prior backend summary Apr 12, 2026
Copilot AI requested a review from austinkimchi April 12, 2026 09:27
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.

bug: sleep score not triggered when falling in range of 14 days

2 participants