Repository navigation
Record a bolus cut short by a pod fault when the fault is read after the bolus's scheduled end - #211
Conversation
…fter the bolus's scheduled end The fault's bolusNotDelivered was only applied if the bolus was still scheduled to be running when Loop read the fault, so a pod that ran dry mid-bolus but was read later kept the full amount. Cancel at the pod's fault time instead.
|
Looking at the code, I see that handleCancelDosing() won't call unfinalizedBolus?.cancel to adjust the bolus dose when finishTime <= now even when it has a non-zero bolusNotDelivered that needs to be applied. This is why the Another bulletproofing improvement for these PR's would be to change the |
itsmojo
left a comment
There was a problem hiding this comment.
Looks good, but could also consider adding additional bulletproofing to have handleCancelDosing() with a non-zero bolusNotDelivered always adjust any unfinalizedBolus independent of the timing.
itsmojo
left a comment
There was a problem hiding this comment.
Should also update handleCancelDosing() for the bolus case to test finishTime > now || bolusNotDelivered > 0 to properly handle reset pod faults that occur mid bolus as they won't have a faultTime to properly for set deliveryStoppedAt.
|
From inspection the latest commit should better handle cases like a reset fault with non-zero bolusNotDelivered and an incorrect Since bolusNotDelivered for pods includes the undelivered amounts for any extended boluses (which currently is not used by the apps), the stoppedAt calculation assumes this canceled bolus was not for a combo bolus, although interestingly enough it should work correctly if the pod's extended bolus facilities were ever used to try to implement an alternate bolus delivery rate. |
itsmojo
left a comment
There was a problem hiding this comment.
Latest commit seems to properly handle even reset faults during a bolus being read late. Nice work Pete!
|
Claude Opus 5.5 Medium review: Review: OmnipodKit PR #211Verdict: Fixes the reported case (scenario 1) and reset faults (scenario 2). One regression to fix before merge. Critical: double subtraction of
|
| Scenario | Result |
|---|---|
| 1. Fault mid-bolus, read after scheduled end | Fixed. Bolus ends at fault time; temp basal and suspend also end at fault time. |
2. Reset fault (fault time 0/$FFFF → nil) |
Late read: correct, via finishTime − r/rate. Read before scheduled end: units correct, end time is read time (existing behaviour, minor). |
| 3. Discard | Unchanged. After scheduled end, progress = 1, so r = 0 and the full bolus is recorded (existing limitation). |
Tests already reported — no need to repeat
- ps2: OmniTests 190 run, 3 skipped, 0 failures (reported with the first commit).
- ps2: new
testFaultSeenAfterScheduledBolusEndRecordsUndeliveredBolusgives 4 U before the fix and 2.2 U after. - Second commit adds
testCancelAfterScheduledEndAppliesBolusNotDelivered. No rerun count is posted after it. - itsmojo: approved from code inspection only; no reset-fault simulation run.
I did not build or run the tests (Linux environment).
CommentWe fixed the double subtraction error before. Look at the history for when. OmnipodKit PR 9: Fixes to handle unacknowledged command with pod faults correctly LoopKit/OmniBLE#144 (comment) |
The recovered cancel subtracts the fault's bolus not delivered, then handlePodFault's handleCancelDosing used to subtract it again (4 U bolus, 1.8 U not delivered: 0.4 U recorded instead of 2.2 U).
Test✅ Able to reproduce the problem, but not able to test the solution using the rPi with its current code Reproduce the problemAfter reviewing the tests from OmniBLE PR 144, I remembered that the rPi pod simulator was updated to provide details for an interrupted bolus based on time. Configuration:
Reproduce the Issue(Note - I did a test last night where I had the phone communicate with the rPi during the time the bolus was "still in progress" and an interrupted bolus was reported. This issue only happens when the communication happens after the bolus should have completed.) This test was done on 2026-11-04 Build using LoopWorkspace main v3.14.11, commit 5efba88, with no changes to OmnipodKit
Use the parser to report what that HEX means: We will not be able to use the rPi in its current version to demonstrate the solution. Test when expected bolus duration has not finishedThis test demonstrates the rPi bases the interrupted bolus on the timing of the next pod communication, not when the fault was report to the rPi I started the rPi with -q flag so the time the fault was entered did not show up in terminal output. (I actually sent it twice.)
Use the parser to report what that HEX means: |
…e stale faulted-pod checks When a recovered unacknowledged cancel comes after the fault, the bolus now ends at the fault time instead of the cancel time. The podState.isFaulted checks in unacknowledgedCommandWasReceived and checkCommandAgainstStatus no longer did anything: handlePodFault sets podState.fault only after the recovery, and the recovery is what applies bolus not delivered now. The comment in handlePodFault saying otherwise is removed. New test: a cancel sent before the bolus's scheduled end that the pod answers with the fault records 2.2 U ending at the fault time.
…e fault A pod reports its fault time in whole minutes, so the fault time can be up to a minute early. A bolus that started in that minute ended before it started: a zero-length bolus, with the suspend recorded before it. A bolus runs at a fixed rate, so its pulses not delivered say when it stopped. When that falls within the fault's minute, use it as the fault time, for the bolus, the suspend and any temp basal. The two tests that started a 4 U bolus 30 s before the fault, yet had 2.2 U delivered by it, now start it 90 s before, which is consistent.
Test with the pod simulatorThe pod simulator now stops a bolus at a fault and keeps reporting what it left undelivered, however late the fault is read (LoopKit/pod#30). That makes the case this PR fixes testable, which the rPi couldn't do before. Setup
Steps
Results
New commit: eeae371The run showed a timing problem. A pod reports its fault time in whole minutes, so the fault time Loop works out can be up to a minute early. Here the fault was at 17:00:30 and the bolus started at 17:00:10, but the fault time came out as 17:00:05. Loop recorded the suspend at 17:00:05 and a zero-length bolus. The units were right; only the times were wrong. A bolus runs at a fixed rate, so the pulses not delivered say when it stopped. When that time falls within the fault's minute, eeae371 uses it as the fault time for the bolus, the suspend and any temp basal. New test:
OmniTests on dev: 195 run, 3 skipped, 0 failures. Earlier commit dd97054 (Joe's review):
|
|
EDITED - I was testing at the same time as Pete and this version did not use the final commit for this PR. Test✅ Updated pod code provides the support we need to be able to test the solution
ConfigurationThe pod simulator was updated to trigger proper timing on interrupted bolus when fault is injected. Do a fresh build of this go code. The 2nd gen SE is still configured for PR 209. Demonstrate the problem (with updated pod code)This test was done on 2026-11-04 14:42 Pair, prime and insert rPi DASH pod
Use the parser to report what that HEX means: This is consistent with an interrupted bolus of 0.6 U; which is not what is recorded with the v3.14.11 released code. In other words, this demonstrates the problem. ✅ Updated pod code provides the support we need to be able to test the solution Demonstrate the solution, part 1This test case is where a fault happens but the phone is not able to communicate with the pod until after enough time passed for the bolus to have completed. Modify the configuration
Deactivate pod from previous test 15:50 Pair, prime and insert rPi DASH pod
Use the parser to report what that HEX means: ✅ This is consistent with an interrupted bolus of 0.35 U from the original 5 U requested ✅ Successful test where there are no communications with the phone and Pod until after sufficient time passes for both to finish Demonstrate the solution, part 2The original double subtraction error happened with this scenario: User hears fault See if we can reproduce this scenario Deactivate pod, pair, prime and insert a new rPi one
Use the parser to report what that HEX means: ✅ This is consistent with an interrupted bolus of 0.3 U from the original 4 U requested Pod checkThe pod continues to return the 0x202 fault response until pod is deactivated
This latest update to the rPi code is very helpful. Thanks @ps2. Note for testersIf you don't have a parser handy, you can use the phone, simply tap on Pod Diagnostics and choose Read Pod Status. All the information you need is reported on the screen. |
|
I'm willing to approve by test but want to give Joe another chance to review the final code. I asked Claude for another review with the updated commits and the successful tests. Claude pointed out my test did not include the most recent commit. But says:
And Pete tested the final version. |
|
The comment in PodState.handleCancelDosing() on the new If a Whatever happens with refineDeliveryStoppedAt(), I think its comment could be impoved with something like this. |
A reset fault reports no fault time, so deliveryStoppedAt falls back to now, which can be long after the bolus was cut short. Accept the time the bolus's pulses not delivered give anywhere from its start to now; with a fault time, keep it within the fault's minute, since the pod's time is the better source when the phone's bolus start is off.
Test✅ bolus interrupted by reset fault properly reports delivered amount
ConfigurationLoopWorkspace main, v3.14.11
Test DetailsUse a Reset Fault for this test - more details below 08:47 start a 3 U manual bolus
✅ interrupted amount reported under pump events was 0.25 U Comment / nitThis is probably a nit, but shouldn't the Reset Fault used in line 203 of
Actual reset faults include: 0x0D, 0x0E, 0x0F, 0x10, 0x12, 0x34, 0xD6 and 0xD7 from Fault-Event-Codes Data returned from rPi reset faultI wanted to find out if rPi does handle reset faults differently. 08:47 start a 3 U manual bolus
Parser says: ✅ 3 U - 2.75 U = 0.25 U reported as interrupted bolus |
A reset fault has no fault time and its bolus not delivered can't be trusted: Eros reset faults seen mid-bolus all report zero, and O5 0x22 faults report the $3FF sigil. So when the fault time is unknown, ignore bolus not delivered. If the fault is seen before the bolus's scheduled end, assume every pulse up to now was delivered, rounded up to a whole pulse; if after, assume the full bolus.
A pod's fault time is the start of its minute, and expiresAt can lag a slow pod clock by up to a minute, so the computed fault time can be up to about two minutes early. Comparing a bolus's stop time against that minute rejected good stop times on slow pods and fell back to the early minute, which undercounts insulin and could put the suspend before a bolus that started in the fault minute. An interrupted bolus's stop time comes from the phone's clock: its start plus the pulses delivered. Use it whenever bolus not delivered is valid (no more than the bolus), but not earlier than the fault minute. With no interrupted bolus, use the end of the fault minute plus a minute for clock lag. Either way, not later than now.
|
When the pod fault time is known (i.e., a non-reset fault) without an in-progress bolus (the most typical case), the current Getting rid of this else clause in placeFaultTime() reverts back to more of its previous semantics of just refining the delivery stopped at time when there was an interrupted bolus. Here’s my rewrite for The I’d suggest replacing this line and the subsequent 3 lines of |
refineDeliveryStoppedAt() again only refines deliveryStoppedAt from an interrupted bolus; otherwise the drift-adjusted fault minute is kept, so the Faulted time matches the No Delivery time shown for an 80 hour fault. The fault minute can be up to ~2 min early (the pod truncates it to the minute, and the start estimate can lag a slow pod by up to a minute), so basal delivered up to the fault can be undercounted by up to that long. bolusNotDelivered is now always passed through: reset faults report 0 (or the 0x3FF sigil, which the bolus.units checks reject), so it changes nothing for them.
Test✅ Demonstrated the solution was successful under 3 scenarios
ConfigurationLoopWorkspace main branch, v3.14.11, commit 5efba88
Build onto SE 3rd gen running iOS 27.2 Test Case 1
09:44 Pair, prime and insert rPi DASH pod
Use the parser to report what that HEX means: ✅ This is consistent with an interrupted bolus of 0.45 U from the original 5 U requested Test Case 2
09:56 Pair, prime and insert rPi DASH pod
✅ This is consistent with an interrupted bolus of 0.35 U from the original 1 U requested Test Case 3
The Hex pattern returned by the pod code does not zero out the pulses not delivered register properly. 10:07 Pair, prime and insert pod
subset of output: ✅ Pump Event History shows full bolus delivered - best we can do with a reset fault when the delivery would have completed before we contacted the pod. |
itsmojo
left a comment
There was a problem hiding this comment.
Thanks Pete and Marion for all your work on this one!
…ce SQNs as Int64 for 32-bit watches), loopandlearn#211 (a bolus cut short by a pod fault, read after its scheduled end) loopandlearn#205 replaces this branch's own fix for the same problem (613a882, throw instead of trapping when a resync SQN does not fit in Int) with the reviewed one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013BZnRiHc1U968YXyW3z6Jm # Conflicts: # OmniTests/PodCommsSessionTests.swift # OmniTests/PodStateTests.swift # OmnipodKit/Bluetooth/BlePodComms.swift # OmnipodKit/PumpManager/PodCommsSession.swift
…EAP and nonce SQNs as Int64 for 32-bit watches), loopandlearn#211 (a bolus cut short by a pod fault, read after its scheduled end) As tested on untethered-kk (Trio build 745, 11.11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UUzqfqfsM8BQYJ2yoRaLz8 # Conflicts: # OmniTests/Driver/Comm/endecrypt/SqnEncodingTests.swift # OmniTests/PodCommsSessionTests.swift # OmnipodKit/Bluetooth/BlePodComms.swift # OmnipodKit/Bluetooth/Session/EapSqn.swift
If a pod faults partway through a bolus (for example 0x18, reservoir empty), the fault status reports how much of the bolus was not delivered.
handlePodFaultapplied that only if the bolus was still scheduled to be running when Loop read the fault. When the fault is read after the bolus's scheduled end, the undelivered amount was ignored and the full bolus was recorded.The cancel now uses the pod's fault time (
deliveryStoppedAt, computed fromfaultEventTimeSinceActivationjust above) instead of the time Loop read the fault. An interrupted temp basal or suspend at the fault also ends at the fault time.Reported in LoopKit/Loop#2648: a DASH pod ran dry about 2.2 U into a 4 U bolus and reported 1.8 U not delivered, but Loop recorded 4 U.
New test
testFaultSeenAfterScheduledBolusEndRecordsUndeliveredBolusrecords 4 U without this change and 2.2 U with it.OmniTests: 190 run, 3 skipped, 0 failures.