Skip to content

Fix prime check and pump view commands for pod setup lost comms recovery - #231

Merged
marionbarker merged 2 commits into
loopandlearn:devfrom
itsmojo:fix/prime-checking-finish-setup-handling
Dec 1, 2026
Merged

marionbarker merged 2 commits into
loopandlearn:devfrom
itsmojo:fix/prime-checking-finish-setup-handling

Conversation

@itsmojo

@itsmojo itsmojo commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This PR obsoletes PR #228 and implements all the items listed at the end of this comment as well as other minor fixes and improvements.

  • prime() now only attempt to do a prime() on the 2nd time through if the read pod status podProgressStatus is pairingCompleted (i.e., it never received the prime bolus command. This is safest and strongest test to determine if it safe to resend the prime bolus command.

  • The OmniUIScreen enum order was updated to be in local order with .confirmAttachment after .pairAndPrime and before .insertCannula to reduce confusion.

  • determineInitialStep() has been updated to return .confirmAttachment in addition to .insertCannula and pairAndPrime based on existing state when podCommState == .activating for improved behavior.

  • The PodLifeState enum has been expanded so that the nextPodLifecycleAction var for the .podActivating case returns a let screen that could be .pairAndPrime, .confirmAttachment or .insertCannula.

  • The PodLifeState enum has been updated so that the nextPodLifecycleActionDescription var for the .podActivating case is now "Finish Setup" for the podActivating case to match the pump HUD semantics for an interrupted pod setup.

  • OmniSettingsViewModel updated for the lifeState OmniPumpManager extension for the podCommState.activating case to select a screen value of either .insertCannula, .confirmAttachment, or .pairAndPrime as determineInitialStep() does.

  • Add new OmniSettingsViewModel.hasPairedNonFaultedPod var for use for the Play Test Beeps diagnostic enabling (i.e., this diagnostic can now be used before pod setup is complete).

  • OmniSettingsView updated to use lifeState.nextPodLifecycleAction (either .pairAndPrime, .confirmAttachment or .insertCannula) along with lifeState.nextPodLifecycleActionDescription (either "Pair Pod" or "Finish Setup") along with hasPairedNonFaultedPod instead of podOk for the PodDiagnosticView to allow Play Test Beeps to be available in the pod setup is interrupted by a unacknowledged command.

  • OmniSettingsView now uses viewModel.hasPairedNonFaultedPod instead of viewModel.podOk for the PodDiagnosticView to allow for Play Test Beeps to be used before pod setup is complete.

  • Many OmniPumpManager tweaks to match documented and tested restrictions for various pod commands that can be invoked from the pump settings view during an interrupted pod setup.

    • Commands that only need a pod which can be faulted:
      getDetailsStatus(), readPulseLog(), readPulseLogPlus(), readActivationTimeI(), readTriggeredAlerts()
    • Beeping related commands that require a paired, non-faulted pod to run:
      playTestBeeps(), setConfirmationBeeps(), setSilencePod(), updateExpirationReminder(), updateLowReservoirReminder()
  • Some old print and log statements were removed or cleaned up in PodCommsSession.

  • The Claude unit test from PR Don't send the prime again once the pod is past priming #228 was included in this PR.

Testing

There was a large # of tests done using the DASH pod sim. Can create the initial failing condition using the Crash after processing the next command button on the frontend while the prime bolus is progressing or before the Insert Cannula operation is started. On recovery, should now go directly to the Attach Pod view (.confirmAttachment screen) view if the prime operation has been started and enough time has passed for it to finish instead to the end of the Pair Pod view that has already waited for the prime to finish. Can also test the going directly to the Attach Pod view without crashing the pod sim by restarting Loop at different times. If done quickly at start of prime, when tapping on Finish Setup in the main view upon Loop restart should display the Pair Pod view showing the remaining priming operation. If restarted more slowly so that Finish Setup isn't tapped before the prime bolus should have been able to finish, then Loop should now skip the .pairAndPrime screen (Pair Pod view) and go directly to the .confirmAttachment screen (AKA the Attach Pod view) from tapping on Finish Setup in the main view.

I also tested using some pretty old Eros and DASH pods and was able to reduce my Eros inventory significantly.;-) I only got one Eros pod from a box of 8 that was made in 2020 to actually beep when filled, but I had no problems testing using my 2021 Eros pods and my 2022 DASH pods. Since it can be a pain trying to test lost pod comms at the wrong time with real pods, I used some special debug code for PodCommsSession to trigger lost comms that remain lost until I manually restored them by patching a debug variable in Xcode. This way I could try all the various commands in the pump view after I entered via a fake unacknowledged command error on the program basal command. Here's the example code that simulates the original failure.

13,16d12
< 
< // XXX Debug - selectively enabled during pod startup.
< // Remains active when triggered until it is manually reset in Xcode at a breakpoint.
< var fakeLostResponse: Bool = false
438,442d433
<             }
< 
<             if fakeLostResponse {
<                 log.debug("@@@ Faking lost response for command %{public}@", String(describing: blocksToSend))
<                 throw PodCommsError.unacknowledgedMessage(sequenceNumber: messageNumber, error: MessageError.invalidSequence)
988,990d978
< 
<             fakeLostResponse = true // XXX debug -- force a lost response for this initial basal program command
< 

Another way to use this which I did not have time to try is this code to trigger the unacknowledged command (Unable to Reach Pump view), then put the pod in a faraday bag and then patch the fakeLostResponse back to false to generate keep the Loop waiting on the pod (more naturally simulate an out of range pod issue). After all the commands are exercised in the pod settings view because of the lost comms, you can just remove the pod from the faraday bag and then try the command of your choice that will success and then lead to the Comms Recovered view when any command is done from the pod settings view. If moving the pod into faraday can be done quickly enough, it might be possible to add a fakeLostResponse = false line just before doing the unacknowledgedMessage throw so that no Xcode patching would be needed to control when the comms are allowed to recover when using any command (including the Finish Setup) in the pump settings views.

With Eros pods there might be another even easier (but not yet tried) way to do get this faraday bag effect by simply turning off or switching off the RL immediately when the Unable to Reach Pump modal is displayed when the fake unacknowledged command is triggered. This should also allow the debug code to have an additional fakeLostResponse = false line just before doing the unacknowledgedMessage throw so that no Xcode patching would be needed to control when the comms are allowed to recover. In this method, the pod comms can be recovered when desired by simply turning on (&/or switching on) the RL and then running any command in the Omnipod Classic settings view that does some pod communication (e.g., by tapping on Finish Setrup, the speaker icon to play test beeps, executing any but the last Pod Diagnostic command, etc) to trigger the comms recovery that would then allow the setup process to then finish.

Notes

I found that the insert cannula operation has all the unacknowledged command handling, but there is no such code for either the prime or the insert cannula boluses. Both of these boluses are special one second per pulse boluses and I'd guess would require some additional work to correctly handle using the unacknowledged command facilities, but there already exists a number of tests using the pod progress status and the PodState.setupProgress values. Did not spend any time looking into looking to see if there might be some unacknowledged cases for the special boluses that we might not be able to handle using the current methods or what might be needed to be able to use generic unacknowledged command facilities to handle these boluses.

@marionbarker

marionbarker commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Code Review

I asked Claude to review the code for PR 228, 229 and 231 with the understanding the PR 231 (this PR) replaces 228.

PR 231 prime check is stricter:

  • PR 228 skips the resend only for the states past priming, so other unexpected states (such as activation time exceeded) still get the 2.6 U prime again
    • Marion's comment: of course - if activation time is exceeded, getting an extra prime command would be ignored anyway
  • PR 231 resends only when the pod reports pairing completed, the one state where it never got the prime

PR 231 fixes the routing in a more general way

Both 228 and 231 fix repeat prime and 0x31 fault problem

Finding:

There is a nit for 231 = passing on for review - I think "purely defensive" means it can't happen

231-A (low): setupProgress is not advanced in the "unexpected progress" branch.
When prime() returns 0, setupProgress stays as it is. insertCannula requires readyForCannulaInsertion, which needs !setupProgress.primingNeeded. If setupProgress were still .startingPrime while the pod is past priming, the user would get notReadyForCannulaInsertion and no way forward.

In practice the pod can't reach .basalInitialized without the app first advancing to .settingInitialBasalSchedule, so this is defensive only. Suggestion: for the "moved on" states (basalInitialized and later), keep #228's if podState.setupProgress.primingNeeded { podState.setupProgress = .priming }. For terminal states (.activationTimeExceeded, .faultEventOccurred, .inactive), consider throwing instead of returning 0, so that the UI doesn't say priming is complete and move on.

Comparing dev and next-dev changes

I asked Claude to make a patch for me to apply to next-dev

New nit for 231 (dev): PR 231 touches the line "Accepted unacknowledged command … ${public}@" but leaves the format typo, so the log prints the literal text. next-dev already has %{public}@. Suggest fixing it in 231.

@marionbarker

Copy link
Copy Markdown
Contributor

Test

✅ successful Loop main with code from this PR
✅ successful Loop next-dev build with code from this PR

Configuration

I built a version from main with this PR and tested with the rPi on an SE 2nd gen phone running iOS 18.7.8

I also asked Claude to make a patch compatible with next-dev and applied the patch to the bump-watch-plugin-loading branch for LoopWorkspace (PR 595), commit 3d7a2ed, tested on an SE 3rd gen phone running iOS 27.2.1

Test Details

I did not insert the test code to make this fail or test against actual pods since @itsmojo has already done this.

marionbarker
marionbarker previously approved these changes Nov 29, 2026

@marionbarker marionbarker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve by Claude code review, diff review (LGTM) and test build against Loop main or with suitable adjustment, next-dev.

@ps2

ps2 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

This fixes the re-prime from IR-J7SKK8AG. I tested it end to end against the pod simulator (LoopKit/pod) in the iOS Simulator using ImpossiBLE, alongside a dev build. Both builds also had next-dev's e60e0ab cherry-picked locally so the Simulator talks to the simulated pod.

Repro on dev: I made the pod sim drop the response to the basal-program command and restart. Then I followed the recovery path: "Unable To Reach Pump" → Learn More → "Unable to Reach Pod" → Access Omnipod Screen → Pair Pod. That re-sent the 2.6 U prime with the pod at basalInitialized, and the pod took it as the cannula insertion bolus (5.2 U delivered in total). I then reopened setup while that bolus was still running, which sent a third prime. The pod faulted with 0x31 and Loop showed "Critical Pod Fault 049", the same as the issue report. The pod sim needed LoopKit/pod#38 to model the 0x31 fault.

With this PR:

  • The same failure takes you to "Finish Setup", which goes to Insert Cannula without sending any insulin. The cannula bolus was 10 pulses, for 3.1 U delivered in total. No fault.
  • Relaunching Loop mid-prime returns to Pair Pod and doesn't re-send the prime.
  • Relaunching after the prime finishes goes to Attach Pod.
  • After "Comms Recovered", a stale Insert Cannula screen with a Retry button reappeared. Retry completed setup safely.

A few minor notes, none of them blocking:

  1. prime()'s new guard logs at .error when the pod has already moved past pairingCompleted. That's now an expected recovery path, so .default may fit better.
  2. The new /// comment inside prime() is a doc comment on a statement. A plain // would do.
  3. hasPairedNonFaultedPod is true during setup, so Silence Pod now sends to the pod instead of only updating state, and reprograms the finish-setup reminder. Probably fine, but worth confirming it's intended.
  4. Play Test Beeps is enabled once the pod is paired. During the setup prime and cannula boluses the bolus-in-progress check doesn't seem to apply, so beeps could be sent mid-bolus.

Once this merges, I'll close #228 as superseded.

ps2
ps2 previously approved these changes Nov 30, 2026

@ps2 ps2 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few nits, as noted. No blockers.

@itsmojo

itsmojo commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator Author

Response to the 4 listed (Claude?) nits that ps@ listed:

  1. In the original error case, prime() was incorrectly getting called from the user tapping "Pair Pod" after a lost response from the program initial basal. prime() should never be getting called if the pod progress has progressed to beyond pairingCompleted. The new "Finish Setup" actions from either the main view or the pump view (if there a lost response) will vector to either the .insertCannula or .confirmAttachment screens when activating if the pod has completed its priming. The added guard in prime() and logging as an error is totally appropriate as this was never "an expected recovery path". However, doing a return of 0 if this guard ever fails should still allow for a reasonable recovery patch and will definitely prevent a double/triple prime attempts.

  2. I didn't realize this semantic difference for the two forms and didn't know that I had even used this for this new comment. It probably because my original comment was much longer and it looked better in Xcode with using a "///" comment). I'll change to use "//".

  3. This was indeed intended and is the correct behavior. The previous logic was only intended to have handle the no pod case by changing only the saved state if there was no pod. But since this function can be called before a pod is fully setup if there is a lost response during the basal programming, everything involving any possible pod comms from the pump view needs to work correctly and consistently if the pod communication is successful when the pod is paired but not fully activated.

  4. Being able to play test beeps can only be done by the user if the flow goes to the pump view after lost command recovery for the program of the initial basal. If a user initiated play test beep command succeeds while in the pump view, then the Loop would go through a screen for the Comms Recovered and the user can then tap on "Finish Setup" to start the cannula insert (if the flow didn't automatically return to one of the pod setup screens). There are no possible new pod beeping cases during a prime or cannula insert bolus that would need any additional changes to protect against that I can see.

Regarding Claude's 231-A (low): setupProgress is not advanced in the "unexpected progress" branch. suggestion to add PR #231's code that includes if podState.setupProgress.primingNeeded { podState.setupProgress = .priming } before returning 0 is confusing and not worth doing. If podState.setupProgress.primingNeeded is true, then setupProgress must be be < .priming (one of .addressAssigned, .podPaired, or .startingPriming), but since this in the !setupProgress.primingNextAttemptedBranch, this value already has to be >= .startingPriming. So therefore, this means for this if statement to do anything that setupProgress must be .startPriming. From the logic further down in prime() setting the setupProgress to .priming only occurs when the prime bolus command has been successfully sent. But if this prime bolus command was indeed successfully sent, then setupProgress would already be .priming and so this guard would get executed as the previous if statement of status.podProgressStatus == .priming || status.podProgress == .primingCompleted would have handled this and set podState.setupProgress to .priming and returned the remaining prime wait time (if any).

Regarding Claude's 231-A (low) suggestion to consider throwing for other "terminal states (.activationTimeExceeded, .faultEventOccurred, .inactive)", I'd rather let the existing mechanisms handle these potential cases so that the user would actually be able to see the "Activation time exceeded" or other pod fault error.

@itsmojo
itsmojo dismissed stale reviews from ps2 and marionbarker via dc3f542 December 1, 2026 02:31

@marionbarker marionbarker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve from Claude review, test build and minimal spot-check.

  • at the insert cannula stage, tell rPi to crash after processing next command
  • the recovery steps are much cleaner with the new code

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.

3 participants