From f92b3a31dac8412c0b2187021a63bb6a1abc10cb Mon Sep 17 00:00:00 2001 From: Raaj Yedida <166455374+RaajYedida@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:22:54 -0600 Subject: [PATCH] fix(app-flow): target the booted device when several simulators are booted App Flow boots a specific simulator but then lists installed apps, installs Expo Go and opens the app without naming it. Before a session exists the agent-device daemon resolves those requests on their own, so with a second simulator booted they fail with AMBIGUOUS_MATCH. The listing error was swallowed as an empty list, and the sweep stopped with "Native ios app was not installed after running the ios script" even though the build had just installed it on the booted device. Pass the boot result's id as udid to apps.list, apps.install and the first apps.open, and log a failed listing instead of hiding it. Co-Authored-By: Claude Opus 5.5 --- packages/core/src/app-flow/capture.ts | 54 +++++++++++----- .../test/app-flow-device-selection.test.ts | 63 +++++++++++++++++++ 2 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 packages/core/test/app-flow-device-selection.test.ts diff --git a/packages/core/src/app-flow/capture.ts b/packages/core/src/app-flow/capture.ts index 073992bc..fb4e3f31 100644 --- a/packages/core/src/app-flow/capture.ts +++ b/packages/core/src/app-flow/capture.ts @@ -209,7 +209,7 @@ function agentDeviceInstallCommand(app: string, appPath: string): string { export { agentDeviceOpenCommand } from './walk-commands.ts' type AgentDeviceClient = Awaited> -type LaunchMode = 'native-run' | 'expo-go' +export type LaunchMode = 'native-run' | 'expo-go' export interface CaptureDevice { client: AgentDeviceClient @@ -834,18 +834,42 @@ function installedAppLaunchTarget(app: string): string { return formattedApp?.[1]?.trim() || app } -async function listInstalledApps( - client: AgentDeviceClient, - platform: 'ios' | 'android' +/** + * The apps installed on the sweep's device. + * + * Pass the booted device's id. Before a session is open the daemon resolves the target from the + * request alone, so an unscoped listing fails with AMBIGUOUS_MATCH as soon as a second simulator + * is booted. That failure used to be swallowed as an empty list, and the sweep then reported the + * freshly built app as "not installed". + */ +export async function listInstalledApps( + client: Pick, + platform: 'ios' | 'android', + deviceId?: string ): Promise { - return (await client.apps.list({ platform }).catch(() => [])) as string[] + try { + return (await client.apps.list({ platform, ...deviceSelector(platform, deviceId) })) as string[] + } catch (error) { + log('apps.list', `failed — ${errorMessage(error)}`) + return [] + } +} + +/** The request field that pins a call to one device: `udid` on iOS, `serial` on Android. */ +function deviceSelector( + platform: 'ios' | 'android', + deviceId: string | undefined +): { udid?: string; serial?: string } { + if (!deviceId) return {} + return platform === 'android' ? { serial: deviceId } : { udid: deviceId } } async function ensureExpoGoInstalled( client: AgentDeviceClient, - ctx: CaptureContext + ctx: CaptureContext, + deviceId: string ): Promise<{ launchTarget: string; installPath: string | null }> { - const apps = await listInstalledApps(client, ctx.platform) + const apps = await listInstalledApps(client, ctx.platform, deviceId) const existing = matchInstalledApp(apps, [EXPO_GO_APP_NAME, 'host.exp.Exponent']) if (existing) { log('Expo Go already installed', existing) @@ -856,7 +880,8 @@ async function ensureExpoGoInstalled( log('apps.install', appPath) const result = await client.apps.install({ appPath, - platform: ctx.platform + platform: ctx.platform, + ...deviceSelector(ctx.platform, deviceId) }) return { launchTarget: result.app || result.appId || EXPO_GO_APP_NAME, @@ -918,7 +943,7 @@ function isMissingSessionError(error: unknown): boolean { } async function openAppSession( - client: AgentDeviceClient, + client: Pick, app: string, options?: { url?: string | null @@ -945,6 +970,7 @@ async function openAppSession( await client.apps.open({ app: launchTarget, platform: 'ios', + ...(options?.udid ? { udid: options.udid } : {}), ...(url ? { url } : {}), ...(options?.relaunch ? { relaunch: true } : {}) }) @@ -1209,8 +1235,8 @@ async function launchBoundApp(device: CaptureDevice, timing?: PhaseLedger): Prom await dismissOpenInAppPrompt(device, device.launchUrl) } -async function openInstalledApp( - client: AgentDeviceClient, +export async function openInstalledApp( + client: Pick, ctx: CaptureContext, mode: LaunchMode, options: { @@ -1240,7 +1266,7 @@ async function openInstalledApp( return { launchTarget, launchUrl, useCustomScheme: false } } - const apps = await listInstalledApps(client, ctx.platform) + const apps = await listInstalledApps(client, ctx.platform, options.udid) const nativeTarget = matchInstalledApp(apps, [ctx.bundleId, ctx.appName, ctx.slug]) if (!nativeTarget) { const lookedFor = @@ -1311,7 +1337,7 @@ async function resolveReusableNativeBuild( ) return null } - const apps = await listInstalledApps(client, ctx.platform) + const apps = await listInstalledApps(client, ctx.platform, deviceId) const installed = matchInstalledApp(apps, [recorded]) if (!installed) { log('checkInstalledBuild', `${recorded} is no longer installed — rebuilding`) @@ -1401,7 +1427,7 @@ export async function bootDevice(ctx: CaptureContext): Promise { } } } else { - const expoGo = await ensureExpoGoInstalled(client, ctx) + const expoGo = await ensureExpoGoInstalled(client, ctx, bootResult.id) expoGoTarget = expoGo.launchTarget if (expoGo.installPath) { setupCommands.push(agentDeviceInstallCommand(expoGo.launchTarget, expoGo.installPath)) diff --git a/packages/core/test/app-flow-device-selection.test.ts b/packages/core/test/app-flow-device-selection.test.ts new file mode 100644 index 00000000..e21fc97a --- /dev/null +++ b/packages/core/test/app-flow-device-selection.test.ts @@ -0,0 +1,63 @@ +import assert from 'node:assert/strict' +import test from 'node:test' +import { + listInstalledApps, + openInstalledApp, + type CaptureContext +} from '../src/app-flow/capture.ts' + +const UDID = 'BCB87837-5051-41B7-9E91-74DDAADA1C0F' + +const ctx: CaptureContext = { + projectPath: '/tmp/project', + projectId: 'project', + scheme: 'example', + appName: 'Example', + bundleId: 'com.example.app', + slug: 'example', + platform: 'ios', + sessionName: 'session' +} + +/** A daemon with two booted simulators: any request that does not name one is ambiguous. */ +function twoBootedSimulators() { + const calls: Array<{ command: string; options: Record }> = [] + const requireDevice = (command: string, options: Record) => { + calls.push({ command, options }) + if (!options.udid) { + throw new Error('2 devices match this request equally; select one explicitly.') + } + } + const client = { + apps: { + list: async (options: Record = {}) => { + requireDevice('apps.list', options) + return ['Example (com.example.app)'] + }, + open: async (options: Record = {}) => { + requireDevice('apps.open', options) + return {} + } + } + } + return { client: client as never, calls } +} + +test('listInstalledApps scopes the listing to the booted device', async () => { + const { client, calls } = twoBootedSimulators() + assert.deepEqual(await listInstalledApps(client, 'ios', UDID), ['Example (com.example.app)']) + assert.equal(calls[0]?.options.udid, UDID) +}) + +test('openInstalledApp finds and opens the native build on the booted device when another simulator is booted', async () => { + const { client, calls } = twoBootedSimulators() + const result = await openInstalledApp(client, ctx, 'native-run', { udid: UDID, metroPort: 8081 }) + assert.equal(result.launchTarget, 'com.example.app') + assert.deepEqual( + calls.map((call) => [call.command, call.options.udid]), + [ + ['apps.list', UDID], + ['apps.open', UDID] + ] + ) +})