Skip to content

LOOP-5693 Fix CarbMath preconditionFailure when a carb entry predates the ISF window - #945

Merged
ps2 merged 1 commit into
devfrom
tidepool/fix-carbmath-isf-window
Aug 13, 2026
Merged

LOOP-5693 Fix CarbMath preconditionFailure when a carb entry predates the ISF window#945
ps2 merged 1 commit into
devfrom
tidepool/fix-carbmath-isf-window

Conversation

@ps2

@ps2 ps2 commented Aug 11, 2026

Copy link
Copy Markdown

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

Crash

preconditionFailure in LoopAlgorithm CarbMath.map(to:carbRatio:insulinSensitivity:…):

Insulin sensitivity and carb ratio timelines must cover carb entry start dates

It fires when a carb entry's startDate is earlier than the earliest value in the insulin-sensitivity (or carb-ratio) history — closestPrior(to: entry.startDate) returns nil. Observed via recommendManualBolus(…potentialCarbEntry:…)LoopAlgorithm.rungeneratePredictioncarbStatus.map(to:).

Root cause

In fetchData, the two schedule windows are computed independently:

  • carbRatio and the fetched carb entries go back to carbsStart = baseTime − dateAdjustmentPast.
  • insulinSensitivity goes back to neededSensitivityTimeline.start, from timelineIntervalForSensitivity(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 coverageinsulinSensitivity.closestPrior == nil → crash. recommendManualBolus is the common trigger because it appends the user's (possibly backdated) potentialCarbEntry on top of fetchData.

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 in fetchData (shared by the loop and recommendManualBolus).

Follow-up (not in this PR)

The durable fix is upstream in LoopAlgorithm: map(to:) should clamp closestPrior to the earliest available schedule value (or drop uncoverable entries) rather than preconditionFailure — a data-driven path shouldn't hard-crash on a coverage gap.

…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.
@ps2 ps2 changed the title Fix CarbMath preconditionFailure when a carb entry predates the ISF window LOOP-5693 Fix CarbMath preconditionFailure when a carb entry predates the ISF window Aug 13, 2026
@ps2
ps2 requested review from Camji55 and nhamming August 13, 2026 19:39
@ps2
ps2 merged commit 2867ceb into dev Aug 13, 2026
3 checks passed
@ps2
ps2 deleted the tidepool/fix-carbmath-isf-window branch August 13, 2026 19:50
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