fix(close): stop an Apple simulator only on its open's own boot proof
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.
- Optional port on the open preparation —
OpenApplicationPreparationInput.onBootPerformance?: (report) => void(@agent-device/contracts/device-boot:DeviceBootPerformance). Optional so external runtime implementers stay source-compatible. - Apple readiness answers for its own boot step on every exit:
{ performed: true },{ performed: false, reason: 'already-booted' }(fresh Booted listing, recent boot observation, orsimctlrefusing the boot), orno-boot-attemptfor a target this call readies without a simulator boot. macOS and physical devices report the no-attempt answer; nothing else writesalready-booted. - The one read of the refusal text is
simctlBootRefusedAlreadyBootedinpackages/platform-apple/src/core/simctl.ts, now shared withensureBootedSimulator. CoreSimulator refusessimctl bootwith 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. - One decision, exclusive by type —
CloseBootOwnership = 'proved' | 'inferred' | 'disproved' | 'none'in contracts; the deciding functionresolveSessionBootOwnershiplives in the daemon session lifecycle (ADR 0038: contracts keeps vocabulary, not algorithms). A proved boot owns the stop on its own;inferredkeeps Android's snapshot answer in charge (Android boots outside the preparation and answers nothing);disprovedrefuses the stop and says why. - Session state —
bootOwnerUnverified?: trueis written only throughSessionStore.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 plainclosestill warns. - 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 fromCLOSE_BOOT_OWNER_UNVERIFIED_REASONS. - Docs/help —
commands.md,sessions.md, theclosecommand metadata, bothcli-help-workflows.tssurfaces (now enforced by the published-reason guard), and the ios-simulator skill card.
What is unchanged
close --shutdownpassesexplicitShutdown: trueand still commands the stop regardless of ownership, including on a disproved session (routed case + live).- Android: no
onBootPerformanceis answered anywhere in platform-android;inferAndroidEmulatorBootOwnershipis untouched;session-open-launch-cleanup.tsreadsbootedByOpenonly with a matchingandroidLaunchIdentity, 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 --shutdownon 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
e937e57176fails at base (bootedByOpen: true, stop asked for) and passes at head. session-boot-ownership.test.tspins every ownership answer pair at the decision owner.session-open-state.test.tsand 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.tsandlifecycle-readied-device.test.ts: the report shape on every exit, and the lifecycle forwarding of the report (mutation-checked: deletingonSimulatorBoot: input.onBootPerformancefails).core/__tests__/simulator.test.ts:ensureBootedSimulatorabsorbs 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
simctlreporting 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
bootedSimulatorsmemo expired mid-open can make readiness re-boot an already-Booted device and answerperformed: 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 listusually 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-compatpasses 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.tsreadsbootedByOpen === trueas 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
bootedSimulatorsmemo as a source of "already up" answers predates #4539.