LOOP-5693 Fix CarbMath preconditionFailure when a carb entry predates the ISF window - #945
Merged
Merged
Conversation
…indow CarbMath.map(to:) preconditionFailures if the insulin-sensitivity or carb-ratio timeline doesn't cover a carb entry's start date. The carb-ratio window and carb entries both extend back to carbsStart (baseTime - dateAdjustmentPast), but the ISF window came from timelineIntervalForSensitivity, which is derived from dose and glucose history only. When that history is more recent than carbsStart (e.g. a CGM gap, fresh setup, or a heavily backdated carb entry on the manual-bolus screen), an older carb entry has carb-ratio coverage but no ISF coverage -> closestPrior returns nil -> crash (seen via recommendManualBolus). Extend the ISF and override history back to min(neededSensitivityTimeline.start, carbsStart) so it covers the same carb window as carbRatio.
Camji55
approved these changes
Aug 13, 2026
3 tasks
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.
https://tidepool.atlassian.net/browse/LOOP-5693
Crash
preconditionFailurein LoopAlgorithmCarbMath.map(to:carbRatio:insulinSensitivity:…):It fires when a carb entry's
startDateis earlier than the earliest value in the insulin-sensitivity (or carb-ratio) history —closestPrior(to: entry.startDate)returnsnil. Observed viarecommendManualBolus(…potentialCarbEntry:…)→LoopAlgorithm.run→generatePrediction→carbStatus.map(to:).Root cause
In
fetchData, the two schedule windows are computed independently:carbsStart = baseTime − dateAdjustmentPast.neededSensitivityTimeline.start, fromtimelineIntervalForSensitivity(doses:glucoseHistoryStart:recommendationEffectInterval:)— derived from dose + glucose history only, never the carb entries.When dose/glucose history is more recent than
carbsStart(a CGM gap, a fresh setup, or a carb entry backdated on the manual-bolus screen further than glucose reaches), an older carb entry has carb-ratio coverage but no ISF coverage →insulinSensitivity.closestPrior == nil→ crash.recommendManualBolusis the common trigger because it appends the user's (possibly backdated)potentialCarbEntryon top offetchData.Fix
Extend the ISF and override history back to
min(neededSensitivityTimeline.start, carbsStart), so it covers the same carb window as carbRatio. Small, contained change infetchData(shared by the loop andrecommendManualBolus).Follow-up (not in this PR)
The durable fix is upstream in LoopAlgorithm:
map(to:)should clampclosestPriorto the earliest available schedule value (or drop uncoverable entries) rather thanpreconditionFailure— a data-driven path shouldn't hard-crash on a coverage gap.