End the watch workout session from durable state, not a view

The HKWorkoutSession that keeps the watch app foregrounded was ended only
by ActiveWorkoutGateView's onChange(of: activeWorkouts) — a view-level side
effect. When a run ended from the phone while the watch app was
backgrounded (kept alive only by the session) or torn down and rebuilt with
an already-empty list, that onChange never fired: the session leaked (the
app kept re-foregrounding on every wrist raise) and finishAndSave() never
ran, so the HR/energy summary was neither saved to Health nor forwarded.

Move session-end off the view into a long-lived WorkoutSessionCoordinator
owned by WatchAppServices, driven by a new bridge.onWorkoutsChanged
callback fired after every authoritative cache mutation (phone push or the
watch's own optimistic edit). The decision is a pure SessionEndPlanner
seam (mirrors WatchCacheApplier): a running session ends only on a genuine
non-empty -> empty transition of the active set, so the launch race (session
running before the run doc syncs) resolves to .none and never discards a
run we haven't heard about yet. Same move as the live-mirror's
repairFromDurable, one layer down.

Watch-only; no schema or wire change. SessionEndPlannerTests pins the
decision table; the OS-initiated-end path (system ends the session itself)
stays a documented residual in PLAN-watch-session-end.md.
This commit is contained in:
2026-07-09 08:12:06 -04:00
parent 9ea5e54b15
commit 04523c875a
8 changed files with 480 additions and 39 deletions
+2
View File
@@ -1,5 +1,7 @@
**July 2026**
The watch workout view now closes reliably when you end a workout on your iPhone, instead of the app reappearing on every wrist raise.
Watch and iPhone set timers no longer drift apart when the watch sleeps through the end of a rest.
Swiping through sets on the iPhone now reliably moves the watch along too, instead of the two drifting apart mid-workout.