[LOOP-5693] Cover carb history range in sensitivity/override queries#936
Open
ps2 wants to merge 1 commit into
Open
[LOOP-5693] Cover carb history range in sensitivity/override queries#936ps2 wants to merge 1 commit into
ps2 wants to merge 1 commit into
Conversation
`recommendManualBolus(potentialCarbEntry:)` appends a synthetic carb entry whose startDate can be up to ~12h in the past (the UI's allowed range). The sensitivity and override timelines are derived from doses, glucose history, and the recommendation effect interval, so they often do not extend back that far. When a past potential carb entry is then fed into the algorithm, `Collection<CarbEntry>.map(to:carbRatio: insulinSensitivity:...)` calls `closestPrior(to: entry.startDate)` on the sensitivity timeline, gets nil, and trips a `preconditionFailure` inside `CarbStatusBuilder` construction — a hard crash on the main thread (Crashlytics report against `LoopAlgorithm/CarbMath.swift:717`). Widen the sensitivity and override queries to also cover `carbsStart` (= baseTime - 12h - 1min), which already bounds the carb history. Add two tests in LoopDataManagerTests: - `testFetchDataSensitivityCoversCarbHistoryStart` asserts that the returned sensitivity timeline's first entry covers `carbsStart`. - `testRecommendManualBolusWithPastPotentialCarbEntryDoesNotCrash` reproduces the crash (verified to trap on the unpatched code) and confirms the fix lets the recommendation flow complete.
Camji55
approved these changes
Jun 18, 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.
https://tidepool.atlassian.net/browse/LOOP-5693
Summary
recommendManualBolus(potentialCarbEntry:)appends a synthetic carb entry whosestartDatecan be up to ~12h in the past (the UI's allowed range). The sensitivity and override timelines, derived only from doses / glucose history / recommendation effect interval, often do not extend that far back.Collection<CarbEntry>.map(to:carbRatio:insulinSensitivity:…)callsclosestPrior(to: entry.startDate)on the sensitivity timeline, getsnil, and tripspreconditionFailureinsideCarbStatusBuilderconstruction — a hard crash on the main thread (Crashlytics reports point atLoopAlgorithm/CarbMath.swift:717).fetchDatato also covercarbsStart(=baseTime - 12h - 1min), which already bounds the carb history.Test plan
testRecommendManualBolusWithPastPotentialCarbEntryDoesNotCrashreproduces the crash ondev(verified to trap withLoopAlgorithm/CarbMath.swift:717: Fatal error: Insulin sensitivity and carb ratio timelines must cover carb entry start dates) and passes with this PR's fix applied.testFetchDataSensitivityCoversCarbHistoryStartassertsinput.sensitivity.first.startDate <= carbsStart.LoopDataManagerTestssuite passes (14/14) on Xcode 26.5 iOS Simulator.