From f8ccebdb250411c4f71a9a72f8a5aa7d21869cd0 Mon Sep 17 00:00:00 2001 From: SJ Kim Date: Fri, 20 Nov 2026 13:07:09 -0500 Subject: [PATCH] fix(device): select the only booted iPhone or iPad before ranking iPhones above iPads An iPad session with several shut-down iPhone simulators failed every command without a selector: the iPhone rank dropped the booted iPad before the booted check, so the shut-down iPhones tied and selection refused with AMBIGUOUS_MATCH. iPhones and iPads of one kind now share a tier, and its only booted member wins. With none or several booted, an iPhone still ranks above an iPad. Simulators over hardware and handhelds over TV, desktop, and watch are unchanged. --- packages/kernel/src/device.test.ts | 43 ++++++++++++++++++++++++++++++ packages/kernel/src/device.ts | 21 +++++++++++---- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/packages/kernel/src/device.test.ts b/packages/kernel/src/device.test.ts index 329dd7ec0f..9f3b8c3711 100644 --- a/packages/kernel/src/device.test.ts +++ b/packages/kernel/src/device.test.ts @@ -273,3 +273,46 @@ test('a shut-down tie marks every candidate shut down and an unknown state omits }, ); }); + +const unbootedIphones: DeviceInfo[] = ['iPhone 16', 'iPhone 17', 'iPhone 17 Pro'].map( + (name, i) => ({ + ...shutDownIphone, + id: `sim-iphone-${i}`, + name, + }), +); + +const bootedIpad: DeviceInfo = { + ...shutDownIphone, + id: 'sim-ipad-booted', + name: 'iPad Pro 13-inch (M5)', + appleOs: 'ipados', + booted: true, +}; + +test('the only booted simulator wins over shut-down iPhones, even when it is an iPad', async () => { + const selected = await resolveDevice([...unbootedIphones, bootedIpad], { platform: 'ios' }); + assert.equal(selected.id, bootedIpad.id); +}); + +test('two booted iPads with shut-down iPhones still refuse to choose', async () => { + const secondBootedIpad: DeviceInfo = { ...bootedIpad, id: 'sim-ipad-booted-2', name: 'iPad Air' }; + await assert.rejects( + resolveDevice([...unbootedIphones, bootedIpad, secondBootedIpad], { platform: 'ios' }), + (error: unknown) => error instanceof AppError && error.code === 'AMBIGUOUS_MATCH', + ); +}); + +test('with nothing booted, shut-down iPhones still tie ahead of a shut-down iPad', async () => { + const shutDownIpad: DeviceInfo = { ...bootedIpad, booted: false }; + await assert.rejects( + resolveDevice([...unbootedIphones, shutDownIpad], { platform: 'ios' }), + (error: unknown) => + error instanceof AppError && + error.code === 'AMBIGUOUS_MATCH' && + JSON.stringify(error.details?.devices) === + JSON.stringify( + unbootedIphones.map((device) => ({ id: device.id, name: device.name, booted: false })), + ), + ); +}); diff --git a/packages/kernel/src/device.ts b/packages/kernel/src/device.ts index d43009cd76..c9b9cb6a48 100644 --- a/packages/kernel/src/device.ts +++ b/packages/kernel/src/device.ts @@ -802,9 +802,18 @@ function preferVirtualCandidates(candidates: DeviceInfo[]): DeviceInfo[] { return virtual.length > 0 ? virtual : candidates; } +/** + * Each kind's iPhones and iPads share a tier. Its only booted member wins, because booting one says + * which handheld is meant; with none or several booted, an iPhone still ranks above an iPad. + */ function candidatesWithBestAppleRank(candidates: DeviceInfo[]): DeviceInfo[] { - const bestRank = Math.min(...candidates.map((device) => appleDeviceSelectionRank(device))); - return candidates.filter((device) => appleDeviceSelectionRank(device) === bestRank); + const tierRank = (device: DeviceInfo) => appleDeviceSelectionRank(device, true); + const bestTierRank = Math.min(...candidates.map(tierRank)); + const tier = candidates.filter((device) => tierRank(device) === bestTierRank); + const booted = tier.filter((device) => device.booted); + if (booted.length === 1) return booted; + const bestRank = Math.min(...tier.map((device) => appleDeviceSelectionRank(device))); + return tier.filter((device) => appleDeviceSelectionRank(device) === bestRank); } /** Tie-break policies `boot --pick` accepts. */ @@ -943,12 +952,14 @@ function compareAppleDevicesForSelection( ); } -function appleDeviceSelectionRank(device: DeviceInfo): number { +/** With `ipadAsIphone`, each kind's iPads share its iPhones' rank (the handheld tier). */ +function appleDeviceSelectionRank(device: DeviceInfo, ipadAsIphone = false): number { // A watch answers `--platform ios` only when no other Apple device can. if (device.appleOs === 'watchos') return 15; - if (device.kind === 'simulator') return appleTargetSelectionRank(device, 0, 1, 2, 3); + const ipadStep = ipadAsIphone ? 0 : 1; + if (device.kind === 'simulator') return appleTargetSelectionRank(device, 0, ipadStep, 2, 3); if (device.kind === 'device' && isApplePlatform(device.platform) && !isMacOs(device)) - return appleTargetSelectionRank(device, 10, 11, 12, 13); + return appleTargetSelectionRank(device, 10, 10 + ipadStep, 12, 13); return 14; }