From d1b3cb55cdcfac4470e149fb9e312467b057ea2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 12 Nov 2026 09:56:34 +0100 Subject: [PATCH 1/2] fix(device-selection): keep host env out of the request device cache key Android SDK discovery adds ANDROID_HOME, ANDROID_SDK_ROOT and PATH to the host env during the first inventory read. The request-scoped resolution cache keyed on the serialized env, so the open handler missed the selection the request lock had just made and read the whole inventory again: simctl, devicectl, xctrace and adb. The request part of the key already holds every value selection derives from env. Co-Authored-By: Claude Opus 5.5 --- .../src/__tests__/dispatch-resolve.test.ts | 21 +++++++++++++++++++ .../device-selection/src/dispatch-resolve.ts | 5 ++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts index 5f2378ff6c..46ce17a6f0 100644 --- a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts +++ b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts @@ -278,6 +278,27 @@ test('resolveTargetDevice reuses request-scoped device resolution cache for iden assert.equal(mockListAppleDevices.mock.calls.length, 1); }); +test('resolveTargetDevice reuses the request cache after the inventory configures the host env', async () => { + // Android SDK discovery writes ANDROID_HOME and PATH into the host env during the first + // inventory read; the request's later resolution must still reuse that selection. + const env: Record = { PATH: '/usr/bin' }; + mockListAppleDevices.mockImplementation(async () => { + env.ANDROID_HOME = '/sdk'; + env.PATH = `/sdk/platform-tools:${env.PATH}`; + return [bootedSimulator]; + }); + + await withResolveTargetDeviceCacheScope(async () => { + const first = await resolveTargetDevice({ udid: 'sim-2' }, { env }); + const inventoryReads = mockListAppleDevices.mock.calls.length; + const second = await resolveTargetDevice({ udid: 'sim-2' }, { env }); + + assert.equal(first.id, 'sim-2'); + assert.equal(second.id, 'sim-2'); + assert.equal(mockListAppleDevices.mock.calls.length, inventoryReads); + }); +}); + test('resolveTargetDevice request cache key separates device selectors', async () => { mockListAppleDevices.mockResolvedValue([simulator, bootedSimulator]); diff --git a/packages/device-selection/src/dispatch-resolve.ts b/packages/device-selection/src/dispatch-resolve.ts index 321536b560..7125545d70 100644 --- a/packages/device-selection/src/dispatch-resolve.ts +++ b/packages/device-selection/src/dispatch-resolve.ts @@ -309,8 +309,11 @@ function buildResolveTargetDeviceCacheKey( ): string { // The app target only informs the first device choice. Once a request has // chosen a device, every later resolution must reuse that same device even - // when dispatch has no app target to pass back through this seam. + // when dispatch has no app target to pass back through this seam. The request + // already holds what selection reads from env, and Android SDK discovery adds + // to that env during the first inventory read. const { + env: _env, appleSimulatorAppTarget: _appleSimulatorAppTarget, appDeviceTarget: _appDeviceTarget, webSession: _webSession, From e92c0ce3736aa4715b4d14b06ebe6f0250b24e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 12 Nov 2026 10:25:37 +0100 Subject: [PATCH 2/2] test(device-selection): pin env-derived allowlists as separate cache entries Co-Authored-By: Claude Opus 5.5 --- .../src/__tests__/dispatch-resolve.test.ts | 17 +++++++++++++++-- .../device-selection/src/dispatch-resolve.ts | 3 +-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts index 46ce17a6f0..0939d25d3d 100644 --- a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts +++ b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts @@ -279,8 +279,6 @@ test('resolveTargetDevice reuses request-scoped device resolution cache for iden }); test('resolveTargetDevice reuses the request cache after the inventory configures the host env', async () => { - // Android SDK discovery writes ANDROID_HOME and PATH into the host env during the first - // inventory read; the request's later resolution must still reuse that selection. const env: Record = { PATH: '/usr/bin' }; mockListAppleDevices.mockImplementation(async () => { env.ANDROID_HOME = '/sdk'; @@ -299,6 +297,21 @@ test('resolveTargetDevice reuses the request cache after the inventory configure }); }); +test('resolveTargetDevice request cache key separates env-derived device allowlists', async () => { + mockListAppleDevices.mockResolvedValue([bootedSimulator]); + + await withResolveTargetDeviceCacheScope(async () => { + await resolveTargetDevice({ udid: 'sim-2' }, { env: {} }); + const inventoryReads = mockListAppleDevices.mock.calls.length; + await resolveTargetDevice( + { udid: 'sim-2' }, + { env: { AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST: 'emulator-5554' } }, + ); + + assert.equal(mockListAppleDevices.mock.calls.length, inventoryReads * 2); + }); +}); + test('resolveTargetDevice request cache key separates device selectors', async () => { mockListAppleDevices.mockResolvedValue([simulator, bootedSimulator]); diff --git a/packages/device-selection/src/dispatch-resolve.ts b/packages/device-selection/src/dispatch-resolve.ts index 7125545d70..e686abf428 100644 --- a/packages/device-selection/src/dispatch-resolve.ts +++ b/packages/device-selection/src/dispatch-resolve.ts @@ -310,8 +310,7 @@ function buildResolveTargetDeviceCacheKey( // The app target only informs the first device choice. Once a request has // chosen a device, every later resolution must reuse that same device even // when dispatch has no app target to pass back through this seam. The request - // already holds what selection reads from env, and Android SDK discovery adds - // to that env during the first inventory read. + // holds every value selection derives from env, so env itself stays out. const { env: _env, appleSimulatorAppTarget: _appleSimulatorAppTarget,