Make the watch own its workout-session lifecycle
The HKWorkoutSession could only ever be born via the phone's one-shot startWatchApp handoff (BULLETPROOFING.md H1-H3): a dropped handoff, a watch crash/reboot, or a run engaged manually on the wrist left the whole workout sessionless - no heart rate, no Health save, app suspending wrist-down - and "End Current & Start New" swallowed the handoff against the old session's idempotency guard. Now the coordinator self-starts a session on any reconcile that finds an active run with none running (SessionEndPlanner.shouldStart / runToStart, activity type from the run's split), recover() re-adopts a crash-orphaned session at launch, and a system-ended session salvages its Health save instead of dropping it. A .finish decided for a session younger than 30s demotes to .discard so the stale-context races can't save junk workouts attributed to the wrong run; parallel completions now pick the survivor deterministically. Claude-Session: https://claude.ai/code/session_01PVNBVKp5bcq52X722uMjwT
This commit is contained in:
+9
-3
@@ -21,7 +21,9 @@ Re-verified against code, not just prior docs — these need no work and should
|
||||
|
||||
## HIGH — the session lifecycle has a single point of failure
|
||||
|
||||
### H1 — The `HKWorkoutSession` can only ever be born via the T1 one-shot · open
|
||||
### H1 — The `HKWorkoutSession` can only ever be born via the T1 one-shot · **fixed**
|
||||
|
||||
> **Fix (2026-07-09):** the coordinator now self-starts a session on any reconcile that finds an active run with none running (`SessionEndPlanner.shouldStart` / `runToStart`; activity type resolved from the run's split), and `WorkoutSessionManager.recover()` re-adopts a crash/reboot-orphaned session at launch (`recoverActiveWorkoutSession`). T1 is now a fast-launch optimization, not the sole session source.
|
||||
|
||||
`WorkoutSessionManager.start(with:)` has exactly one caller: `WatchAppDelegate.handle(_:)` (`WatchAppDelegate.swift:31`), i.e. the phone's `startWatchApp(toHandle:)` handoff. That handoff is best-effort `try?` with **no retry** (`WorkoutLauncher.swift:37-40`). There is no `HKHealthStore.recoverActiveWorkoutSession` anywhere, no `WKExtendedRuntimeSession`, and nothing gates run-flow entry on `sessionManager.isRunning`.
|
||||
|
||||
@@ -33,7 +35,9 @@ Failure scenarios (all real):
|
||||
|
||||
**Fix direction:** make the watch own its session lifecycle. Self-start a session when entering (or reconciling into) an active run with `isRunning == false`; implement session recovery at launch. T1 then becomes a fast-launch optimization rather than the sole source of sessions. This also absorbs most of H2 and much of the M-tier degradation.
|
||||
|
||||
### H2 — "End Current & Start New" swallows the new run's session · open
|
||||
### H2 — "End Current & Start New" swallows the new run's session · **fixed**
|
||||
|
||||
> **Fix (2026-07-09):** absorbed by H1's self-start (a swallowed T1 heals at the next reconcile), plus a session-age rule in the planner: a `.finish` decided for a session younger than `minimumFinishAge` (30 s) demotes to `.discard`, so the transient "old run completed, new run not yet pushed" context can no longer save a junk seconds-long HKWorkout attributed to the old run — the session is discarded and self-start brings it back when the new run's push lands.
|
||||
|
||||
`endActiveThenStart` (`WorkoutLogsView.swift:271-281`) runs the end-saves in a `Task` but calls `start(with:)` synchronously — so **T1 goes out while the old run's session is still running on the watch**. `handle(_:)` → `start(with:)` hits the `session == nil` idempotency guard (`WorkoutSessionManager.swift:61`) and is **silently dropped**. Moments later the "A completed" push arrives and the coordinator finishes A's session — leaving run B sessionless (all of H1's fallout). The one-shot was consumed while blocked; nothing replays it.
|
||||
|
||||
@@ -41,7 +45,9 @@ Timing variant: if B's session *does* start (old one already ended) but the tran
|
||||
|
||||
**Fix direction:** on the watch, when `handle(_:)` arrives while a session is running, stash the configuration and start it immediately after the current session finishes; and/or a short grace window after `session.startActivity(...)` during which `decide` refuses `.finish`/`.discard`. H1's self-start largely absorbs this too.
|
||||
|
||||
### H3 — A system-ended session silently drops the Health save · open
|
||||
### H3 — A system-ended session silently drops the Health save · **fixed**
|
||||
|
||||
> **Fix (2026-07-09):** `salvageSystemEndedSession` — a system-driven `.ended`/`.stopped` now ends collection and finishes the builder's workout (saving the `HKWorkout` to Health) instead of clearing everything. Residual: the salvaged workout isn't linked to a run document (`healthKitWorkoutUUID` stays nil) and no metrics are forwarded — the runs are still active, so there's no completed run to attach them to.
|
||||
|
||||
`workoutSession(_:didChangeTo:)` for `.ended`/`.stopped` only calls `clear()` (`WorkoutSessionManager.swift:199-202`) — no `endCollection`/`finishWorkout`, no metrics capture, no forward to the phone. `PLAN-watch-session-end.md` lists this as a known residual. Rare (OS reclaim under resource pressure), but it is a "my workout never made it to Health" support report waiting to happen.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user