fix(close): stop an Apple simulator only on its open's own boot proof

Merged
TH
3d ago
+591-40

Closes #4791. Design context: https://github.com/callstack/agent-device/pull/4539#discussion_r4640258510

What changed

Plain close now stops an Apple simulator only when the session's own open proved it performed the boot. The pre-open inventory snapshot ("device was stopped") is an inference the platform's answer can confirm or refute, never proof on its own.

  1. Optional port on the open preparation — OpenApplicationPreparationInput.onBootPerformance?: (report) => void (@agent-device/contracts/device-boot: DeviceBootPerformance). Optional so external runtime implementers stay source-compatible.
  2. Apple readiness answers for its own boot step on every exit: { performed: true }, { performed: false, reason: 'already-booted' } (fresh Booted listing, recent boot observation, or simctl refusing the boot), or no-boot-attempt for a target this call readies without a simulator boot. macOS and physical devices report the no-attempt answer; nothing else writes already-booted.
  3. The one read of the refusal text is simctlBootRefusedAlreadyBooted in packages/platform-apple/src/core/simctl.ts, now shared with ensureBootedSimulator. CoreSimulator refuses simctl boot with prose and exit 149 and no distinguishable code, so this stays a string read — platform-owned, documented, pinned at both readers against CoreSimulator's exact output.
  4. One decision, exclusive by type — CloseBootOwnership = 'proved' | 'inferred' | 'disproved' | 'none' in contracts; the deciding function resolveSessionBootOwnership lives in the daemon session lifecycle (ADR 0038: contracts keeps vocabulary, not algorithms). A proved boot owns the stop on its own; inferred keeps Android's snapshot answer in charge (Android boots outside the preparation and answers nothing); disproved refuses the stop and says why.
  5. Session state — bootOwnerUnverified?: true is written only through SessionStore.updateOpenDevice, which also classifies it in the layering table. A 'none' write — a re-open that proves nothing, including one that then fails before dispatch — keeps the recorded disproval: the same foreign boot is still running, so a later plain close still warns.
  6. Close's outcome — keeps the simulator Running and appends to warnings: "This session's open found this device stopped, but reported boot-owner-unverified: its own boot step never booted the device, so close did not request a stop. The device is still running." The token is read from CLOSE_BOOT_OWNER_UNVERIFIED_REASONS.
  7. Docs/help — commands.md, sessions.md, the close command metadata, both cli-help-workflows.ts surfaces (now enforced by the published-reason guard), and the ios-simulator skill card.

What is unchanged

  • close --shutdown passes explicitShutdown: true and still commands the stop regardless of ownership, including on a disproved session (routed case + live).
  • Android: no onBootPerformance is answered anywhere in platform-android; inferAndroidEmulatorBootOwnership is untouched; session-open-launch-cleanup.ts reads bootedByOpen only with a matching androidLaunchIdentity, so #4764 cleanup is unaffected.
  • A device found already running keeps running.

Evidence

Automated (pnpm exec vitest run --project unit-core <path>):

  • Routed session-close-foreign-boot.test.ts: a disproved open keeps the device and warns; a proved open stops it; close --shutdown on a disproved session commands the stop; a failed re-open cannot silence the recorded disproval. Mutation-checked: dropping the 'none' retention in the store fails it.
  • Base-vs-head probe: the routed scenario run at base commit e937e57176 fails at base (bootedByOpen: true, stop asked for) and passes at head.
  • session-boot-ownership.test.ts pins every ownership answer pair at the decision owner.
  • session-open-state.test.ts and the store cases: 'disproved' records it; a disproved re-open cannot take back a proved boot; a 'none' re-open keeps the disproval.
  • readiness/runtime.test.ts and lifecycle-readied-device.test.ts: the report shape on every exit, and the lifecycle forwarding of the report (mutation-checked: deleting onSimulatorBoot: input.onBootPerformance fails).
  • core/__tests__/simulator.test.ts: ensureBootedSimulator absorbs CoreSimulator's exact refusal, so the shared detector is pinned at both readers.

Live — dedicated simulator agent-device-4791-live only (created and deleted afterwards; no other simulator touched), branch CLI over an isolated --state-dir, JSON transcripts, plus a base-CLI comparison of the same transcript. Posted as a comment on this PR: cold stop (data.shutdown.success: true), prebooted retention, the foreign-boot race (head keeps Booted with the warning; base stops it — the bug), and close --shutdown.

Known limitations

  • The proof comes from simctl reporting its own call, not from a boot claim; booting a Simulator takes no claim (#4749), and this PR does not add one. Mis-detection keeps the device running — the conservative direction.
  • A Simulator whose 5-second bootedSimulators memo expired mid-open can make readiness re-boot an already-Booted device and answer performed: true. Retaining boot-time provenance would need the ownership store this issue rules out, so the memo stays as-is; the residual window is the memo's staleness bound, and it can only ever stop a device the session itself found stopped.
  • Reproducing the foreign-boot race by real timing is flaky, because readiness's fresh simctl list usually sees the foreign boot and infers nothing at all. The live case therefore holds the session open at the readiness boundary with the foreign boot landing during it — the same window the routed test models — with the base-vs-head contrast as the behavioral proof.
  • The live evidence was recorded at bece9b32a4. The follow-up commit changes only re-open/after-failure retention and doc surfaces, which the routed tests cover.
  • No new wire types: check:daemon-wire-compat passes with the protocol hash unchanged.

Not in this PR

  • #4749 — a Simulator boot takes no device claim, so two actors can race a boot by design.
  • session-open-launch-cleanup.ts reads bootedByOpen === true as launch-miss cleanup permission for non-Android devices too. The proof gate narrows that, but it is not boot ownership's contract.
  • The 5-second bootedSimulators memo as a source of "already up" answers predates #4539.