Skip to content

[LOOP-5693] Cover carb history range in sensitivity/override queries#936

Open
ps2 wants to merge 1 commit into
devfrom
ps2/LOOP-5693/extend-schedules-for-potential-carb-entry
Open

[LOOP-5693] Cover carb history range in sensitivity/override queries#936
ps2 wants to merge 1 commit into
devfrom
ps2/LOOP-5693/extend-schedules-for-potential-carb-entry

Conversation

@ps2

@ps2 ps2 commented Jun 16, 2026

Copy link
Copy Markdown

https://tidepool.atlassian.net/browse/LOOP-5693

Summary

  • 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, derived only from doses / glucose history / recommendation effect interval, often do not extend that far back.
  • Once the synthetic entry is fed into the algorithm, Collection<CarbEntry>.map(to:carbRatio:insulinSensitivity:…) calls closestPrior(to: entry.startDate) on the sensitivity timeline, gets nil, and trips preconditionFailure inside CarbStatusBuilder construction — a hard crash on the main thread (Crashlytics reports point at LoopAlgorithm/CarbMath.swift:717).
  • Widen the sensitivity and override queries in fetchData to also cover carbsStart (= baseTime - 12h - 1min), which already bounds the carb history.

Test plan

  • New testRecommendManualBolusWithPastPotentialCarbEntryDoesNotCrash reproduces the crash on dev (verified to trap with LoopAlgorithm/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.
  • New testFetchDataSensitivityCoversCarbHistoryStart asserts input.sensitivity.first.startDate <= carbsStart.
  • Full LoopDataManagerTests suite passes (14/14) on Xcode 26.5 iOS Simulator.

`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.
@ps2 ps2 requested review from Camji55 and nhamming June 18, 2026 16:43
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.

2 participants