From 0add0f1ecb06fbf2c0a41dd30d2731fb94550b4a Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 16 Oct 2026 10:05:58 +0200 Subject: [PATCH 1/3] test: reproduce premature iOS installer readiness Require a successful installer probe after bootstatus and preserve the existing readiness deadline for retries and hung probes. --- src/drivers/ios/index.test.ts | 91 ++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/src/drivers/ios/index.test.ts b/src/drivers/ios/index.test.ts index 213cdc793..18bf9084e 100644 --- a/src/drivers/ios/index.test.ts +++ b/src/drivers/ios/index.test.ts @@ -916,10 +916,11 @@ describe("IosSimctlDriver", () => { ); }); - it("boots then waits for bootstatus with the documented timeout", async () => { + it("boots then checks bootstatus and installer readiness with bounded commands", async () => { const runner = new ScriptedProcessRunner([ { match: simctl("boot", driverData.udid) }, { match: simctl("bootstatus", driverData.udid, "-b") }, + { match: simctl("listapps", driverData.udid) }, ]); const driver = await createDriver(runner); @@ -938,6 +939,94 @@ describe("IosSimctlDriver", () => { expect(runner.calls).toEqual([ { ...simctl("boot", driverData.udid), options: { timeoutMs: 30_000 } }, { ...simctl("bootstatus", driverData.udid, "-b"), options: { timeoutMs: 120_000 } }, + { ...simctl("listapps", driverData.udid), options: { timeoutMs: 30_000 } }, + ]); + }); + + it("keeps makeReady pending until a failed installer probe succeeds", async () => { + const clock = new FakeClock(); + const runner = new ScriptedProcessRunner([ + { match: simctl("boot", driverData.udid) }, + { match: simctl("bootstatus", driverData.udid, "-b") }, + { + match: simctl("listapps", driverData.udid), + result: { code: 2, stderr: "installer starting", stdout: "" }, + }, + { match: simctl("listapps", driverData.udid) }, + ]); + const driver = await createDriver(runner, clock); + const ready = driver.makeReady( + { address: driverData.udid, deviceId: driverData.udid, driverData }, + prepareFull, + ); + const state = promiseState(ready); + await flushMicrotasks221(); + expect(state.state).toBe("pending"); + expect(runner.calls.at(-1)?.args).toEqual(simctlArgs("listapps", driverData.udid)); + clock.advance(1_000); + await flushMicrotasks221(); + expect(state.state).toBe("fulfilled"); + expect((await ready).deviceId).toBe(driverData.udid); + }); + + it.each([false, true])( + "shares the bootstatus deadline with a %s-hanging installer probe and shuts down on expiry", + async (hangs) => { + const clock = new FakeClock(); + const runner = new ScriptedProcessRunner([ + { match: simctl("boot", driverData.udid) }, + { + match: simctl("bootstatus", driverData.udid, "-b"), + onSpawn: () => clock.advance(119_500), + }, + { + match: simctl("listapps", driverData.udid), + hangs, + result: { code: 2, stderr: "installer starting", stdout: "" }, + }, + { match: simctl("shutdown", driverData.udid) }, + ]); + const driver = await createDriver(runner, clock); + const ready = driver.makeReady( + { address: driverData.udid, deviceId: driverData.udid, driverData }, + prepareFull, + ); + const state = promiseState(ready); + await flushMicrotasks221(); + expect(state.state).toBe("pending"); + expect(runner.calls.at(-1)).toEqual({ + ...simctl("listapps", driverData.udid), + options: { timeoutMs: 500 }, + }); + clock.advance(499); + await flushMicrotasks221(); + expect(state.state).toBe("pending"); + clock.advance(1); + await flushMicrotasks221(); + expect(state.state).toBe("rejected"); + await expect(ready).rejects.toBeInstanceOf(BootTimeoutError); + expect(runner.calls.at(-1)?.args).toEqual(simctlArgs("shutdown", driverData.udid)); + }, + ); + + it("does not probe the installer when bootstatus has consumed the readiness budget", async () => { + const clock = new FakeClock(); + const runner = new ScriptedProcessRunner([ + { match: simctl("boot", driverData.udid) }, + { match: simctl("bootstatus", driverData.udid, "-b"), onSpawn: () => clock.advance(120_000) }, + { match: simctl("shutdown", driverData.udid) }, + ]); + const driver = await createDriver(runner, clock); + await expect( + driver.makeReady( + { address: driverData.udid, deviceId: driverData.udid, driverData }, + prepareFull, + ), + ).rejects.toBeInstanceOf(BootTimeoutError); + expect(runner.calls.map((call) => call.args)).toEqual([ + simctlArgs("boot", driverData.udid), + simctlArgs("bootstatus", driverData.udid, "-b"), + simctlArgs("shutdown", driverData.udid), ]); }); From bb6ab497958401bc54b03cb7742c6c2a058ed85c Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 16 Oct 2026 10:09:58 +0200 Subject: [PATCH 2/3] fix: wait for the iOS installer within the boot deadline Poll scoped listapps after every simulator boot, retry transient failures within the existing readiness budget, and shut down on expiry. Cover immediate installs after erase in the slow lane. --- docs/CONFIGURATION.md | 6 + docs/EVENTS.md | 2 +- docs/internal/EVENTS.md | 6 +- .../adr/0002-opt-in-slim-ios-simulators.md | 6 +- e2e/slow-ios-smoke.test.ts | 13 +- src/drivers/ios/AGENTS.md | 26 +-- src/drivers/ios/index.test.ts | 177 ++++++++++++------ src/drivers/ios/index.ts | 30 ++- 8 files changed, 186 insertions(+), 80 deletions(-) diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index be27dc777..377653dbf 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -458,6 +458,12 @@ is why `ios.slim.bootTimeoutMs` defaults higher than the normal boot timeout, especially on slower CI runners. The `ios.slim.*` keys apply to every slim device the worker makes, whatever its default mode. +An iOS simulator is ready after boot completes and `simctl listapps` succeeds. +Boot completion and installer polling share a 120-second deadline for a full +device, or `ios.slim.bootTimeoutMs` for each boot of a supported slim device. +If the deadline expires, readiness fails and Simlock attempts to shut the +simulator down. + **`full` is a guarantee; `slim` is best effort.** A `full` request never receives a slim device. A `slim` request on a runtime that cannot be slimmed -- an iOS runtime older than 18.5, or any Android device -- is granted a full diff --git a/docs/EVENTS.md b/docs/EVENTS.md index 61f1b1005..ea1cf6525 100644 --- a/docs/EVENTS.md +++ b/docs/EVENTS.md @@ -39,7 +39,7 @@ the gateway's own lease index before any worker is ever contacted. `lease-id-tak | Event | Payload (key fields) | Emitted when | Emitter | Status | |---|---|---|---|---| | `device.provisioned` | device id, spec (platform, model, os version, `physical: false`, `mode: "slim"` for a device planned slim, and `imageTag` for a device whose request named one), driver, duration | driver `provision` committed to registry | Registry | implemented | -| `device.ready` | device id, boot duration | readiness probe passed | Registry | implemented | +| `device.ready` | device id, boot duration | readiness probe passed (on iOS simulators, boot completion and a successful installer probe) | Registry | implemented | | `device.reclaimed` | device id, strategy (erase/snapshot/wipe/uninstall), duration, `missingApps` for a physical device (the apps on its enrollment list that were no longer installed; they are reported, not reinstalled) | fresh-state reclaim finished. Never emitted for a device created under `lease.identity` `fresh`: nothing is reclaimed, the device is deleted instead | Registry | implemented | | `device.purge-failed` | device id, lease id, attempted strategy (erase/snapshot/wipe/delete/uninstall), duration, stable error summary | release-time purge failed (including the wipe of a device a daemon start put off; a physical device that reads not present emits nothing and becomes absent), or (strategy `delete`) the shutdown or delete that ends a `fresh` device's lease failed; the device enters `quarantined` (see below) rather than rejoining the pool. The strategy list can grow: a consumer must tolerate a strategy it does not know | Reclaim coordinator | implemented | | `device.quarantined` | device id, max retries, next retry deadline | a device committed to `quarantined` — present in the registry, still counted as running when it is a virtual device, not eligible for a grant. Fires immediately after `device.purge-failed` for a release-time purge or delete failure, or on its own for a stalled-transition timeout (see `device.stalled-transition-detected`) or for a returned physical device whose reclaim failed while it was present | QuarantineCoordinator | implemented | diff --git a/docs/internal/EVENTS.md b/docs/internal/EVENTS.md index 768583e69..ea25c74b9 100644 --- a/docs/internal/EVENTS.md +++ b/docs/internal/EVENTS.md @@ -77,7 +77,7 @@ worker is ever contacted. `lease-id-taken` (ADR 0020; widens a published vocabul | Event | Payload (key fields) | Emitted when | Emitter | Status | |---|---|---|---|---| | `device.provisioned` | device id, spec (`physical: false`, always, ADR 0022 §1: a physical device never emits this event, additive; `mode: "slim"` for a device planned slim, absent for a full one -- the one place the planned mode is visible, ADR 0007 §9; `imageTag` for a device whose request named one, #214, additive), driver, duration | driver `provision` committed to registry | Registry | implemented | -| `device.ready` | device id, boot duration | readiness probe passed | Registry | implemented | +| `device.ready` | device id, boot duration | readiness probe passed (on iOS simulators, boot completion and a successful installer probe) | Registry | implemented | | `device.reclaimed` | device id, strategy (erase/snapshot/wipe/uninstall, ADR 0022 §4), duration, `missingApps` for a physical device (enrollment-list apps no longer installed: reported, not reinstalled; additive) | fresh-state reclaim finished. Never emitted for a device created under `lease.identity` `fresh` (#75): nothing is reclaimed, the device is deleted instead | Registry | implemented | | `device.purge-failed` | device id, lease id, attempted strategy (erase/snapshot/wipe/delete/uninstall), duration, stable error summary | release-time purge failed (including the deferred wipe a daemon start runs in the background, ADR 0019 §2; a physical device read not present emits nothing and becomes `absent`), or (strategy `delete`, #75) the shutdown or delete that ends a `fresh` device's lease failed; the device enters `quarantined` (see below) rather than rejoining the pool. `delete` widens a published vocabulary (events rule 6 allows additive changes): a consumer must tolerate a strategy it does not know | ReclaimCoordinator (strategy `delete`: ReclaimCoordinator's spent-device path) | implemented | | `device.quarantined` | device id, max retries, next retry deadline | a device committed to `quarantined` — present in the registry, still counted as running when it is a virtual device, not eligible for a grant. Fires immediately after `device.purge-failed` for a release-time purge or delete failure (`reclaiming`/`shutdown → quarantined`), or on its own for a stalled-transition timeout (see `device.stalled-transition-detected`) or for a returned physical device whose reclaim failed while it was present (ADR 0022 §8: there is no lease for `device.purge-failed` to name) | QuarantineCoordinator | implemented | @@ -96,13 +96,13 @@ worker is ever contacted. `lease-id-taken` (ADR 0020; widens a published vocabul | `device.recovered` | device id, lease id, attempts, duration | a crashed leased device was rebooted under its existing lease and passed readiness | LeaseHealthMonitor | implemented | | `device.recovery-failed` | device id, lease id, attempts, reason, error | recovery could not restore a leased device (absent from driver reality, provenance drift, or attempts exhausted) and its lease was released | LeaseHealthMonitor | implemented | | `device.orphan-purged` | driver device id, platform, device root | `simlock doctor --purge-orphans` destroyed a device that sat inside a validly-marked Simlock device root with no registry record — see [ADR 0001](adr/0001-simlock-owned-device-roots.md) | Doctor | implemented | -| `device.slimmed` | device id, address, platform (ios), categories, label count, duration, signature, unknown labels | *after* the post-slim reboot's `bootstatus` succeeded -- i.e. once the `launchctl disable` overrides applied via `simctl spawn` are actually in force on the simulator | driver-diagnostics | implemented | +| `device.slimmed` | device id, address, platform (ios), categories, label count, duration, signature, unknown labels | *after* the post-slim reboot's `bootstatus` and installer probe succeeded -- i.e. once the `launchctl disable` overrides applied via `simctl spawn` are actually in force on the simulator | driver-diagnostics | implemented | `device.slimmed` reports a fact committed to the *simulator's own launchd database*, not to the Simlock registry (ADR 0002, `docs/internal/adr/0002-opt-in-slim-ios-simulators.md`) -- so events rule 3 ("emit post-commit only") is satisfied by waiting for that commit to become observable, not by waiting on a registry write: the driver applies the `launchctl disable` overrides, reboots the device, and only fires -`onSlimmed` once the second `bootstatus` has passed, proving the overrides survived the reboot and +`onSlimmed` once the second `bootstatus` and installer probe have passed, proving the overrides survived the reboot and are actually in force. The registry's own `device.ready` for that same boot is a separate, later event, emitted through the normal readiness path once the driver call returns. A slim request on a runtime older than iOS 18.5 is resolved to a full spec before planning (ADR 0007 diff --git a/docs/internal/adr/0002-opt-in-slim-ios-simulators.md b/docs/internal/adr/0002-opt-in-slim-ios-simulators.md index 113a58e5a..779effa72 100644 --- a/docs/internal/adr/0002-opt-in-slim-ios-simulators.md +++ b/docs/internal/adr/0002-opt-in-slim-ios-simulators.md @@ -73,7 +73,7 @@ is enabled, the driver: 1. applies the disable list through `simctl spawn launchctl disable`, 2. shuts the device down, -3. boots it again and waits for `bootstatus -b`, +3. boots it again and waits for `bootstatus -b` and a successful `simctl listapps` installer probe, 4. records what it did in `driverData` (see 3), 5. emits `device.slimmed` after the registry commit. @@ -155,7 +155,7 @@ worse than no slim pass. ### 8. The boot deadline accounts for the second boot `ios.slim.bootTimeoutMs` (default ten minutes, matching simslim on CI -runners) replaces the ordinary `bootstatus` deadline, but only for a boot +runners) replaces the ordinary combined `bootstatus` and installer-probe deadline, but only for a boot that will actually slim: a `--full` device, a runtime below the floor, or a recovery boot keeps the normal deadline. `Driver.estimate` quotes the double-boot cost for the same boots so `doctor` does not read a slim boot @@ -165,7 +165,7 @@ as a stalled transition. `device.slimmed` is bridged from the driver's `onSlimmed` callback in the daemon, exactly like `component.install-*`, and fires only once the -post-slim `bootstatus` has passed. The fact it reports is committed to the +post-slim `bootstatus` and installer probe have passed. The fact it reports is committed to the simulator's launchd database, not the registry, which is why the bridge does not wait on a registry write. A skipped or failed slim is a warning log line, not an event. diff --git a/e2e/slow-ios-smoke.test.ts b/e2e/slow-ios-smoke.test.ts index 439dcd1d0..5921dafbf 100644 --- a/e2e/slow-ios-smoke.test.ts +++ b/e2e/slow-ios-smoke.test.ts @@ -2,7 +2,7 @@ import { execFile } from "node:child_process"; import { promisify } from "node:util"; import { describe, expect, it } from "vitest"; -import { withDaemon } from "./helpers/index.js"; +import { IOS, withDaemon } from "./helpers/index.js"; import { iosDeviceSet, setDevices, @@ -221,7 +221,7 @@ describe.skipIf(process.platform !== "darwin")( ); it( - "a platform-only lease grants the first listed phone model booted in Simlock's device set, and the same request after release gets that device back without a second boot", + "a platform-only lease grants the first listed phone model and accepts an immediate install, including after erase and reuse", { timeout: 300_000 }, async () => { await sweepStaleDeviceSets(); @@ -239,6 +239,8 @@ describe.skipIf(process.platform !== "darwin")( expect(first.code, `lease failed: ${first.stderr}`).toBe(0); const firstGrant = first.json as Grant; const udid = firstGrant.device.driverDeviceId; + const firstInstall = await env.cli(["simctl", "install", udid, IOS.app]); + expect(firstInstall.code, `first install failed: ${firstInstall.stderr}`).toBe(0); expect(firstGrant.device.spec.model).toBe(phone); const bootedDevice = (await setDevices(deviceSet)).find((device) => device.udid === udid); expect(bootedDevice?.state).toBe("Booted"); @@ -259,6 +261,13 @@ describe.skipIf(process.platform !== "darwin")( const second = await env.cli(request, { timeout: 120_000 }); expect(second.code, `second lease failed: ${second.stderr}`).toBe(0); const secondGrant = second.json as Grant; + const secondInstall = await env.cli([ + "simctl", + "install", + secondGrant.device.driverDeviceId, + IOS.app, + ]); + expect(secondInstall.code, `install after erase failed: ${secondInstall.stderr}`).toBe(0); expect( secondGrant.device.driverDeviceId, "the same request must reuse the idle device", diff --git a/src/drivers/ios/AGENTS.md b/src/drivers/ios/AGENTS.md index 4dde270ed..9cc088415 100644 --- a/src/drivers/ios/AGENTS.md +++ b/src/drivers/ios/AGENTS.md @@ -37,12 +37,14 @@ Provision — `simctl create simlock- ` → UDID, then Boot — `makeReady`: -1. `simctl boot`, then `simctl bootstatus -b`. Timeout → shut down, `BootTimeoutError`. +1. `simctl boot`, then `simctl bootstatus -b`, then poll `simctl listapps` until it succeeds. + Bootstatus and installer polling share one deadline; each probe is capped at 30 s and + failures retry after 1 s. Deadline expiry → best-effort shutdown, `BootTimeoutError`. 2. Slim pass only for `prepare` + mode `slim` + runtime 18.5+. Too old → skipped, returned `full`. 3. Already slim: `driverData.slimSignature` and `slimMarkToken` match the categories and the current mark → return `slim`, no work. 4. Else `simctl spawn /bin/sh -c` running `launchctl disable system/