feat(apple): reclaim retained runners under device-claim authority

Merged
TH
3mo ago
+500-33

Summary

Implements the #1320 retained-runner rule (the issue's v1 stop-and-recreate decision): a daemon that holds the host-global device claim may stop and replace a warm XCTest runner whose owner no longer holds that claim.

Before this, the worst remaining cross-worktree failure on iOS was: worktree A closes its session (claim released, runner kept warm by default), worktree B wins the claim at open, then dies in runner preparation with an unstructured COMMAND_FAILED / IOS_RUNNER_OWNED_BY_OTHER_DAEMON for up to ~5 minutes until A's daemon idled out — exactly the late runner-prep contention #1320 set out to remove.

How it works

  • Claim authority proves release. Device claims are exclusive per device, so the requesting daemon holding the claim proves the lease owner released the device and merely kept its runner warm. prepareRunnerLeaseForStartup's busy branch gains a third takeover rule (device-claim-takeover) alongside same-state-dir and proxy logical-lease.
  • Trust boundary respected. The probe is not request metadata: it is daemon-bound through the existing runner-owner seam (@agent-device/platform-apple/runner-owner, same pattern as setRunnerLeaseOwnerStateDir) and answers from the claim store by process identity (processOwnsActiveDeviceClaim). Unbound embedders (package tests, non-daemon use) answer false and keep today's refusal. Abandoned claims grant no authority.
  • Mixed-version safety. Leases now record deviceClaimProtocol: 1; takeover is gated on the lease declaring claim arbitration, so a runner owned by a pre-claims build — which can legitimately be mid-session without holding any claim — is never preempted.
  • Loser-side safety. cleanupRunnerSessionResources now skips the device-wide simctl terminate of the runner container bundles when the on-disk lease is owned by someone else. Without this, the losing daemon's delayed idle-stop or shutdown would kill the successor's active runner on the shared simulator (this also closes the same latent hazard for the proxy logical-lease takeover path). Lease release and token-scoped xcodebuild cleanup already no-op on token mismatch.
  • Help topics (workflow, physical-device) updated: the live-owner rejection now names claim arbitration instead of being unconditional.

Validation

pnpm check:affected --run passed (format, lint, typecheck, layering, fallow, affected tests).

Live two-daemon validation against a throwaway iPhone 17 Pro simulator with a shared claim store and shared lease dir:

  1. Protection intact: while daemon A's session held the claim, daemon B's open was rejected with non-retriable DEVICE_IN_USE, reason DEVICE_CLAIM_LIVE_OWNER, owner session/workspace/stateDir, and the exact device status recovery command.
  2. Takeover works: after A's close (claim gone, lease retained with deviceClaimProtocol: 1, daemon A still alive), B's open succeeded in ~8s and the on-disk lease flipped to B's daemon pid while A was still running — the live-owner path, not stale reclaim.
  3. Guard works: daemon stop on A afterwards left B's session fully functional (appstate healthy).

Part of #1320.