From a9d169988950a5efb989f141e6f139e57c26bea1 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Mon, 12 Oct 2026 13:10:00 +0200 Subject: [PATCH 1/2] feat(daemon): scope iOS simulator requests to AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET --- .../src/__tests__/dispatch-resolve.test.ts | 39 +++++++++++++++- .../device-selection/src/dispatch-resolve.ts | 4 +- packages/kernel/src/device-isolation.test.ts | 46 +++++++++++++++++++ packages/kernel/src/device-isolation.ts | 29 ++++++++++++ .../daemon-port/session-test-shard-devices.ts | 4 +- .../session-lifecycle/internal/inventory.ts | 4 +- website/docs/docs/commands.md | 1 + website/docs/docs/configuration.md | 2 +- 8 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 packages/kernel/src/device-isolation.test.ts diff --git a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts index f6821dd8fa..3f29a6b2a8 100644 --- a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts +++ b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, test, vi } from 'vitest'; +import { afterEach, beforeEach, test, vi } from 'vitest'; import assert from 'node:assert/strict'; const mockFindIosSimulatorInstalledApp = vi.fn(); @@ -96,6 +96,10 @@ async function resolveTargetDevice( ); } +afterEach(() => { + vi.unstubAllEnvs(); +}); + beforeEach(() => { mockFindIosSimulatorInstalledApp.mockReset(); mockFindIosSimulatorInstalledApp.mockResolvedValue(undefined); @@ -595,6 +599,39 @@ test('resolveTargetDevice keeps simulator-set scope separate from the macOS host assert.equal(requests[1]?.iosSimulatorSetPath, undefined); }); +test('resolveTargetDevice discovers simulators in the daemon operator set when the request names none', async () => { + vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); + const requests: DeviceInventoryRequest[] = []; + await withTestDeviceInventory( + { + local: async (request) => { + requests.push(request); + return [simulator]; + }, + }, + async () => { + const result = await resolveTargetDeviceInContext({ platform: 'ios' }); + assert.equal(result.id, simulator.id); + }, + ); + + assert.equal(requests[0]?.iosSimulatorSetPath, '/tmp/operator-set'); +}); + +test('resolveTargetDevice refuses a request naming another set than the daemon operator set before discovery', async () => { + vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); + + const err = await resolveTargetDevice({ + platform: 'ios', + iosSimulatorDeviceSet: '/tmp/other-set', + }).catch((error) => error); + + assert.ok(err instanceof AppError); + assert.equal(err.code, 'INVALID_ARGS'); + assert.equal(err.details?.reason, 'ios_simulator_device_set_outside_daemon_set'); + assert.equal(mockListAppleDevices.mock.calls.length, 0); +}); + async function expectDeviceNotFound(action: () => Promise): Promise { const err = await action().catch((error) => error); diff --git a/packages/device-selection/src/dispatch-resolve.ts b/packages/device-selection/src/dispatch-resolve.ts index 3847b8764c..c8c7e123d1 100644 --- a/packages/device-selection/src/dispatch-resolve.ts +++ b/packages/device-selection/src/dispatch-resolve.ts @@ -11,7 +11,7 @@ import { AppError } from '@agent-device/kernel/errors'; import { AsyncLocalStorage } from 'node:async_hooks'; import { resolveAndroidSerialAllowlist, - resolveIosSimulatorDeviceSetPath, + resolveRequestIosSimulatorDeviceSetPath, } from '@agent-device/kernel/device-isolation'; import { withDiagnosticTimer } from '@agent-device/host-kit/diagnostics'; import { @@ -210,7 +210,7 @@ export function buildDeviceInventoryRequestFromFlags( ); } const iosSimulatorSetPath = resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: resolveIosSimulatorDeviceSetPath(flags.iosSimulatorDeviceSet), + simulatorSetPath: resolveRequestIosSimulatorDeviceSetPath(flags.iosSimulatorDeviceSet), platform, target: flags.target, }); diff --git a/packages/kernel/src/device-isolation.test.ts b/packages/kernel/src/device-isolation.test.ts new file mode 100644 index 0000000000..366db40cb4 --- /dev/null +++ b/packages/kernel/src/device-isolation.test.ts @@ -0,0 +1,46 @@ +import { afterEach, expect, test, vi } from 'vitest'; +import { AppError } from './errors.ts'; +import { resolveRequestIosSimulatorDeviceSetPath } from './device-isolation.ts'; + +afterEach(() => { + vi.unstubAllEnvs(); +}); + +test('without AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET a request reaches the set it names, or the default set', () => { + vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', ''); + + expect(resolveRequestIosSimulatorDeviceSetPath(' /tmp/request-set ')).toBe('/tmp/request-set'); + expect(resolveRequestIosSimulatorDeviceSetPath(undefined)).toBeUndefined(); +}); + +test('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET is the set of a request that names none', () => { + vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); + + expect(resolveRequestIosSimulatorDeviceSetPath(undefined)).toBe('/tmp/operator-set'); + expect(resolveRequestIosSimulatorDeviceSetPath(' ')).toBe('/tmp/operator-set'); +}); + +test('a request naming the operator set is admitted', () => { + vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); + + expect(resolveRequestIosSimulatorDeviceSetPath('/tmp/operator-set/')).toBe('/tmp/operator-set'); +}); + +test('a request naming another set than the operator set is refused', () => { + vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); + + let refusal: unknown; + try { + resolveRequestIosSimulatorDeviceSetPath('/tmp/other-set'); + } catch (error) { + refusal = error; + } + + expect(refusal).toBeInstanceOf(AppError); + expect(refusal).toMatchObject({ + code: 'INVALID_ARGS', + details: { reason: 'ios_simulator_device_set_outside_daemon_set' }, + }); + expect((refusal as AppError).message).toContain('/tmp/operator-set'); + expect((refusal as AppError).message).toContain('/tmp/other-set'); +}); diff --git a/packages/kernel/src/device-isolation.ts b/packages/kernel/src/device-isolation.ts index c4c9674035..fa4b85ba19 100644 --- a/packages/kernel/src/device-isolation.ts +++ b/packages/kernel/src/device-isolation.ts @@ -1,3 +1,8 @@ +import path from 'node:path'; +import { AppError } from './errors.ts'; + +const DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV = 'AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET'; + function normalizeNonEmpty(value: string | undefined): string | undefined { const trimmed = value?.trim(); return trimmed ? trimmed : undefined; @@ -9,6 +14,30 @@ export function resolveIosSimulatorDeviceSetPath( return normalizeNonEmpty(flagValue); } +/** + * The simulator set a daemon request reaches. `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` in the + * daemon's environment is the operator's set: it applies to every request that names none, and a + * request naming another set is refused because it would reach simulators outside it. + */ +export function resolveRequestIosSimulatorDeviceSetPath( + requestedValue: string | undefined, +): string | undefined { + const requested = normalizeNonEmpty(requestedValue); + const operatorSet = normalizeNonEmpty(process.env[DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV]); + if (!operatorSet) return requested; + if (requested && path.resolve(requested) !== path.resolve(operatorSet)) { + throw new AppError( + 'INVALID_ARGS', + `This daemon only reaches iOS simulators in the set "${operatorSet}" (${DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV}), but the request named --ios-simulator-device-set "${requested}".`, + { + reason: 'ios_simulator_device_set_outside_daemon_set', + hint: `Omit --ios-simulator-device-set, or pass "${operatorSet}".`, + }, + ); + } + return operatorSet; +} + export function parseSerialAllowlist(value: string): Set { return new Set( value diff --git a/packages/replay-port/src/daemon-port/session-test-shard-devices.ts b/packages/replay-port/src/daemon-port/session-test-shard-devices.ts index d99caf3465..25f22024d6 100644 --- a/packages/replay-port/src/daemon-port/session-test-shard-devices.ts +++ b/packages/replay-port/src/daemon-port/session-test-shard-devices.ts @@ -3,7 +3,7 @@ import type { DeviceInventoryRequest } from '@agent-device/contracts/device'; import { listDeviceInventory } from '@agent-device/device-selection/device-inventory-context'; import { resolveAndroidSerialAllowlist, - resolveIosSimulatorDeviceSetPath, + resolveRequestIosSimulatorDeviceSetPath, } from '@agent-device/kernel/device-isolation'; import { isMobilePlatform, @@ -100,7 +100,7 @@ function buildReplayTestShardInventoryRequest( platform: flags?.platform, target: flags?.target, iosSimulatorSetPath: resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: resolveIosSimulatorDeviceSetPath(flags?.iosSimulatorDeviceSet), + simulatorSetPath: resolveRequestIosSimulatorDeviceSetPath(flags?.iosSimulatorDeviceSet), platform: flags?.platform, target: flags?.target, }), diff --git a/src/daemon/session-lifecycle/internal/inventory.ts b/src/daemon/session-lifecycle/internal/inventory.ts index f6fd19ad5b..1099666a7f 100644 --- a/src/daemon/session-lifecycle/internal/inventory.ts +++ b/src/daemon/session-lifecycle/internal/inventory.ts @@ -19,7 +19,7 @@ import { } from '@agent-device/kernel/device'; import { resolveAndroidSerialAllowlist, - resolveIosSimulatorDeviceSetPath, + resolveRequestIosSimulatorDeviceSetPath, } from '@agent-device/kernel/device-isolation'; import { deviceClaimOwnerCannotRelease, @@ -213,7 +213,7 @@ function inventoryIosSimulatorSetPath( target: DeviceInfo['target'], ) { return resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: resolveIosSimulatorDeviceSetPath(configuredPath), + simulatorSetPath: resolveRequestIosSimulatorDeviceSetPath(configuredPath), platform, target, }); diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index ffa611096b..1371ff3f4a 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -196,6 +196,7 @@ agent-device devices --platform android --android-device-allowlist emulator-5554 - If the selected Xcode can't resolve a scoped simulator for the Apple runner, the runner start fails with `details.reason: "simulator_set_destination_not_found"`, naming the set and, when known, the Xcode version. - `--android-device-allowlist ` limits Android discovery and selection to comma- or space-separated serials. - Set the same scopes with the `iosSimulatorDeviceSet` and `androidDeviceAllowlist` config keys, or `AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST`. CLI flags override environment values, except under a `strip` session lock, which ignores conflicting per-call selectors. +- Start the daemon with `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET=` to scope every request to one simulator set. A request without `--ios-simulator-device-set` uses that set; one naming another set fails with `INVALID_ARGS` and `details.reason: "ios_simulator_device_set_outside_daemon_set"`. - Scope applies before `--device`, `--udid`, and `--serial`, so an out-of-scope selector fails with `DEVICE_NOT_FOUND`. - On macOS, daemon startup removes any symlink at `~/Library/Developer/XCTestDevices`, including one you made yourself (for example to move it to another volume), and restores `XCTestDevices.agent-device-backup` when it exists. A symlinked `XCTestDevices` is not supported. Only the link is removed, never the directory it points to; `daemon.log` records each step and any failed restore, which doesn't stop the daemon. diff --git a/website/docs/docs/configuration.md b/website/docs/docs/configuration.md index eea23990a0..3caf86cbfb 100644 --- a/website/docs/docs/configuration.md +++ b/website/docs/docs/configuration.md @@ -122,7 +122,7 @@ Only the variables below, and those documented on command pages, are supported. | Category | Env vars | Decision | | --- | --- | --- | | CLI defaults and config | `AGENT_DEVICE_HOME`, `AGENT_DEVICE_CONFIG`, `AGENT_DEVICE_SESSION`, `AGENT_DEVICE_PLATFORM`, `AGENT_DEVICE_SCREENSHOT_SCALE`, `AGENT_DEVICE_SESSION_LOCK`, `AGENT_DEVICE_DAEMON_BASE_URL`, `AGENT_DEVICE_DAEMON_AUTH_TOKEN`, `AGENT_DEVICE_CLOUD_BASE_URL` | Public | -| Device scoping | `AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST` | Public | +| Device scoping | `AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST`, `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` | Public. `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` is read by the daemon: every request that names no `--ios-simulator-device-set` uses that set, and a request naming another set fails with `INVALID_ARGS` (`details.reason: "ios_simulator_device_set_outside_daemon_set"`). Restart the daemon after changing it. | | Android test IME | `AGENT_DEVICE_TEST_IME` | Public. Same setting as `--test-ime` / `--no-test-ime` on `open`, `test`, and `replay`; see known limitations. | | Local daemon storage | `AGENT_DEVICE_STATE_DIR` | Public | | Metro and install helpers | `AGENT_DEVICE_METRO_BEARER_TOKEN`, `AGENT_DEVICE_BUNDLETOOL_JAR` | Public | From ddf9352e7c7bfa9a27e6a2c0b2ea031b8d5f8f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 14 Oct 2026 12:49:01 +0200 Subject: [PATCH 2/2] fix(daemon): apply the operator simulator set only where a simulator set applies Move the AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET rule into one kernel owner, resolveRequestAppleSimulatorSetPath in device.ts, which drops macOS and desktop selectors first, leaves non-simulator platforms alone, then applies the operator default or the refusal. Each inventory call site makes one call, device-isolation.ts is a leaf again, and the env is injectable for tests. The refusal compares trimmed path strings the way the session selector does. Document that physical iOS devices are hidden and that a CLI-spawned daemon keeps the first value. --- .../session-lifecycle/internal/inventory.ts | 11 +-- .../src/__tests__/dispatch-resolve.test.ts | 27 ++++++ .../device-selection/src/dispatch-resolve.ts | 11 +-- packages/kernel/src/device-isolation.test.ts | 46 --------- packages/kernel/src/device-isolation.ts | 29 ------ packages/kernel/src/device.ts | 34 +++++-- .../daemon-port/session-test-shard-devices.ts | 11 +-- src/core/__tests__/device.test.ts | 93 ++++++++++++++++--- website/docs/docs/commands.md | 2 +- website/docs/docs/configuration.md | 2 +- 10 files changed, 147 insertions(+), 119 deletions(-) delete mode 100644 packages/kernel/src/device-isolation.test.ts diff --git a/packages/daemon/src/sessions/session-lifecycle/internal/inventory.ts b/packages/daemon/src/sessions/session-lifecycle/internal/inventory.ts index 935c6ca7a9..a1f85b2b5a 100644 --- a/packages/daemon/src/sessions/session-lifecycle/internal/inventory.ts +++ b/packages/daemon/src/sessions/session-lifecycle/internal/inventory.ts @@ -16,14 +16,11 @@ import { isMacOs, matchesPlatformSelector, publicPlatformString, - resolveAppleSimulatorSetPathForSelector, + resolveRequestAppleSimulatorSetPath, type DeviceInfo, type PlatformSelector, } from '@agent-device/kernel/device'; -import { - resolveAndroidSerialAllowlist, - resolveRequestIosSimulatorDeviceSetPath, -} from '@agent-device/kernel/device-isolation'; +import { resolveAndroidSerialAllowlist } from '@agent-device/kernel/device-isolation'; import { deviceClaimOwnerCannotRelease, inspectDeviceClaims, @@ -215,8 +212,8 @@ function inventoryIosSimulatorSetPath( platform: PlatformSelector | undefined, target: DeviceInfo['target'], ) { - return resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: resolveRequestIosSimulatorDeviceSetPath(configuredPath), + return resolveRequestAppleSimulatorSetPath({ + requestedSetPath: configuredPath, platform, target, }); diff --git a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts index df4738e1e9..a370eba74e 100644 --- a/packages/device-selection/src/__tests__/dispatch-resolve.test.ts +++ b/packages/device-selection/src/__tests__/dispatch-resolve.test.ts @@ -632,6 +632,33 @@ test('resolveTargetDevice refuses a request naming another set than the daemon o assert.equal(mockListAppleDevices.mock.calls.length, 0); }); +test('resolveTargetDevice serves macOS and Android requests naming another set on a daemon scoped to an operator set', async () => { + vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); + const requests: DeviceInventoryRequest[] = []; + await withTestDeviceInventory( + { + local: async (request) => { + requests.push(request); + return request.platform === 'macos' ? [macDesktop] : [androidEmulator]; + }, + }, + async () => { + const macResult = await resolveTargetDeviceInContext({ + platform: 'macos', + iosSimulatorDeviceSet: '/tmp/other-set', + }); + const androidResult = await resolveTargetDeviceInContext({ + platform: 'android', + iosSimulatorDeviceSet: '/tmp/other-set', + }); + assert.equal(macResult.id, macDesktop.id); + assert.equal(androidResult.id, androidEmulator.id); + }, + ); + + assert.equal(requests[0]?.iosSimulatorSetPath, undefined); +}); + async function expectDeviceNotFound(action: () => Promise): Promise { const err = await action().catch((error) => error); diff --git a/packages/device-selection/src/dispatch-resolve.ts b/packages/device-selection/src/dispatch-resolve.ts index c8c7e123d1..bd0382c93e 100644 --- a/packages/device-selection/src/dispatch-resolve.ts +++ b/packages/device-selection/src/dispatch-resolve.ts @@ -3,16 +3,13 @@ import type { DeviceInventoryRequest } from '@agent-device/contracts/device'; import { hasExplicitDeviceIdentitySelector, isApplePlatform, - resolveAppleSimulatorSetPathForSelector, + resolveRequestAppleSimulatorSetPath, type DeviceInfo, type DeviceTarget, } from '@agent-device/kernel/device'; import { AppError } from '@agent-device/kernel/errors'; import { AsyncLocalStorage } from 'node:async_hooks'; -import { - resolveAndroidSerialAllowlist, - resolveRequestIosSimulatorDeviceSetPath, -} from '@agent-device/kernel/device-isolation'; +import { resolveAndroidSerialAllowlist } from '@agent-device/kernel/device-isolation'; import { withDiagnosticTimer } from '@agent-device/host-kit/diagnostics'; import { listLocalDeviceInventory, @@ -209,8 +206,8 @@ export function buildDeviceInventoryRequestFromFlags( 'Device target selector requires --platform. Use --platform ios|macos|android|vega|linux|apple with --target mobile|tv|desktop.', ); } - const iosSimulatorSetPath = resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: resolveRequestIosSimulatorDeviceSetPath(flags.iosSimulatorDeviceSet), + const iosSimulatorSetPath = resolveRequestAppleSimulatorSetPath({ + requestedSetPath: flags.iosSimulatorDeviceSet, platform, target: flags.target, }); diff --git a/packages/kernel/src/device-isolation.test.ts b/packages/kernel/src/device-isolation.test.ts deleted file mode 100644 index 366db40cb4..0000000000 --- a/packages/kernel/src/device-isolation.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { afterEach, expect, test, vi } from 'vitest'; -import { AppError } from './errors.ts'; -import { resolveRequestIosSimulatorDeviceSetPath } from './device-isolation.ts'; - -afterEach(() => { - vi.unstubAllEnvs(); -}); - -test('without AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET a request reaches the set it names, or the default set', () => { - vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', ''); - - expect(resolveRequestIosSimulatorDeviceSetPath(' /tmp/request-set ')).toBe('/tmp/request-set'); - expect(resolveRequestIosSimulatorDeviceSetPath(undefined)).toBeUndefined(); -}); - -test('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET is the set of a request that names none', () => { - vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); - - expect(resolveRequestIosSimulatorDeviceSetPath(undefined)).toBe('/tmp/operator-set'); - expect(resolveRequestIosSimulatorDeviceSetPath(' ')).toBe('/tmp/operator-set'); -}); - -test('a request naming the operator set is admitted', () => { - vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); - - expect(resolveRequestIosSimulatorDeviceSetPath('/tmp/operator-set/')).toBe('/tmp/operator-set'); -}); - -test('a request naming another set than the operator set is refused', () => { - vi.stubEnv('AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET', '/tmp/operator-set'); - - let refusal: unknown; - try { - resolveRequestIosSimulatorDeviceSetPath('/tmp/other-set'); - } catch (error) { - refusal = error; - } - - expect(refusal).toBeInstanceOf(AppError); - expect(refusal).toMatchObject({ - code: 'INVALID_ARGS', - details: { reason: 'ios_simulator_device_set_outside_daemon_set' }, - }); - expect((refusal as AppError).message).toContain('/tmp/operator-set'); - expect((refusal as AppError).message).toContain('/tmp/other-set'); -}); diff --git a/packages/kernel/src/device-isolation.ts b/packages/kernel/src/device-isolation.ts index fa4b85ba19..c4c9674035 100644 --- a/packages/kernel/src/device-isolation.ts +++ b/packages/kernel/src/device-isolation.ts @@ -1,8 +1,3 @@ -import path from 'node:path'; -import { AppError } from './errors.ts'; - -const DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV = 'AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET'; - function normalizeNonEmpty(value: string | undefined): string | undefined { const trimmed = value?.trim(); return trimmed ? trimmed : undefined; @@ -14,30 +9,6 @@ export function resolveIosSimulatorDeviceSetPath( return normalizeNonEmpty(flagValue); } -/** - * The simulator set a daemon request reaches. `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` in the - * daemon's environment is the operator's set: it applies to every request that names none, and a - * request naming another set is refused because it would reach simulators outside it. - */ -export function resolveRequestIosSimulatorDeviceSetPath( - requestedValue: string | undefined, -): string | undefined { - const requested = normalizeNonEmpty(requestedValue); - const operatorSet = normalizeNonEmpty(process.env[DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV]); - if (!operatorSet) return requested; - if (requested && path.resolve(requested) !== path.resolve(operatorSet)) { - throw new AppError( - 'INVALID_ARGS', - `This daemon only reaches iOS simulators in the set "${operatorSet}" (${DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV}), but the request named --ios-simulator-device-set "${requested}".`, - { - reason: 'ios_simulator_device_set_outside_daemon_set', - hint: `Omit --ios-simulator-device-set, or pass "${operatorSet}".`, - }, - ); - } - return operatorSet; -} - export function parseSerialAllowlist(value: string): Set { return new Set( value diff --git a/packages/kernel/src/device.ts b/packages/kernel/src/device.ts index 3d098dda7a..6ea697a40d 100644 --- a/packages/kernel/src/device.ts +++ b/packages/kernel/src/device.ts @@ -321,17 +321,37 @@ function resolveRunnerPlatformNameForAppleOs( } } -export function resolveAppleSimulatorSetPathForSelector(params: { - simulatorSetPath?: string; +const DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV = 'AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET'; + +/** + * The simulator set a daemon request's device inventory reaches. macOS and desktop selectors + * never reach a simulator set. For selectors that can reach simulators, + * `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` in the daemon's environment is the operator's set: it + * applies when the request names no set, and a request naming another set is refused. + */ +export function resolveRequestAppleSimulatorSetPath(params: { + requestedSetPath?: string; platform?: PlatformSelector; target?: DeviceTarget; + env?: NodeJS.ProcessEnv; }): string | undefined { - const { simulatorSetPath, platform, target } = params; - if (!simulatorSetPath) return undefined; - if (platform === 'macos' || target === 'desktop') { - return undefined; + const { platform, target, env = process.env } = params; + if (platform === 'macos' || target === 'desktop') return undefined; + const requested = params.requestedSetPath?.trim() || undefined; + if (!supportsAppleSimulatorSelection(platform)) return requested; + const operatorSet = env[DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV]?.trim() || undefined; + if (!operatorSet) return requested; + if (requested && requested !== operatorSet) { + throw new AppError( + 'INVALID_ARGS', + `This daemon only reaches iOS simulators in the set "${operatorSet}" (${DAEMON_IOS_SIMULATOR_DEVICE_SET_ENV}), but the request named --ios-simulator-device-set "${requested}".`, + { + reason: 'ios_simulator_device_set_outside_daemon_set', + hint: `Omit --ios-simulator-device-set, or pass exactly "${operatorSet}".`, + }, + ); } - return simulatorSetPath; + return operatorSet; } export function sortAppleDevicesForSelection( diff --git a/packages/replay-port/src/daemon-port/session-test-shard-devices.ts b/packages/replay-port/src/daemon-port/session-test-shard-devices.ts index 25f22024d6..eb0e563c62 100644 --- a/packages/replay-port/src/daemon-port/session-test-shard-devices.ts +++ b/packages/replay-port/src/daemon-port/session-test-shard-devices.ts @@ -1,14 +1,11 @@ import type { CommandFlags } from '@agent-device/contracts/command'; import type { DeviceInventoryRequest } from '@agent-device/contracts/device'; import { listDeviceInventory } from '@agent-device/device-selection/device-inventory-context'; -import { - resolveAndroidSerialAllowlist, - resolveRequestIosSimulatorDeviceSetPath, -} from '@agent-device/kernel/device-isolation'; +import { resolveAndroidSerialAllowlist } from '@agent-device/kernel/device-isolation'; import { isMobilePlatform, matchesPlatformSelector, - resolveAppleSimulatorSetPathForSelector, + resolveRequestAppleSimulatorSetPath, type DeviceInfo, } from '@agent-device/kernel/device'; import { AppError } from '@agent-device/kernel/errors'; @@ -99,8 +96,8 @@ function buildReplayTestShardInventoryRequest( return { platform: flags?.platform, target: flags?.target, - iosSimulatorSetPath: resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: resolveRequestIosSimulatorDeviceSetPath(flags?.iosSimulatorDeviceSet), + iosSimulatorSetPath: resolveRequestAppleSimulatorSetPath({ + requestedSetPath: flags?.iosSimulatorDeviceSet, platform: flags?.platform, target: flags?.target, }), diff --git a/src/core/__tests__/device.test.ts b/src/core/__tests__/device.test.ts index 338d31868f..32a10e91df 100644 --- a/src/core/__tests__/device.test.ts +++ b/src/core/__tests__/device.test.ts @@ -7,7 +7,7 @@ import { platformSelectorsConflict, PLATFORMS, resolveApplePlatformName, - resolveAppleSimulatorSetPathForSelector, + resolveRequestAppleSimulatorSetPath, resolveDevice, runnerSynthesizesTap, } from '@agent-device/kernel/device'; @@ -186,29 +186,94 @@ test('resolveApplePlatformName falls back to target inference for legacy records assert.equal(resolveApplePlatformName('macos', undefined), 'macOS'); }); -test('resolveAppleSimulatorSetPathForSelector ignores simulator scoping for desktop selectors', () => { +const NO_OPERATOR_SET: NodeJS.ProcessEnv = {}; +const OPERATOR_SET_ENV: NodeJS.ProcessEnv = { + AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET: ' /tmp/operator-set ', +}; + +test('resolveRequestAppleSimulatorSetPath ignores simulator scoping for desktop selectors', () => { + for (const env of [NO_OPERATOR_SET, OPERATOR_SET_ENV]) { + assert.equal( + resolveRequestAppleSimulatorSetPath({ + requestedSetPath: '/tmp/other-set', + platform: 'macos', + env, + }), + undefined, + ); + assert.equal( + resolveRequestAppleSimulatorSetPath({ + requestedSetPath: '/tmp/other-set', + platform: 'apple', + target: 'desktop', + env, + }), + undefined, + ); + } +}); + +test('resolveRequestAppleSimulatorSetPath without an operator set reaches the requested set or the default set', () => { assert.equal( - resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: '/tmp/scoped', - platform: 'macos', + resolveRequestAppleSimulatorSetPath({ + requestedSetPath: ' /tmp/scoped ', + platform: 'ios', + target: 'mobile', + env: NO_OPERATOR_SET, }), + '/tmp/scoped', + ); + assert.equal( + resolveRequestAppleSimulatorSetPath({ platform: 'ios', env: NO_OPERATOR_SET }), undefined, ); +}); + +test('resolveRequestAppleSimulatorSetPath applies the operator set to a simulator request that names none', () => { + assert.equal( + resolveRequestAppleSimulatorSetPath({ platform: 'ios', env: OPERATOR_SET_ENV }), + '/tmp/operator-set', + ); + assert.equal( + resolveRequestAppleSimulatorSetPath({ requestedSetPath: ' ', env: OPERATOR_SET_ENV }), + '/tmp/operator-set', + ); assert.equal( - resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: '/tmp/scoped', + resolveRequestAppleSimulatorSetPath({ + requestedSetPath: '/tmp/operator-set', platform: 'apple', - target: 'desktop', + target: 'tv', + env: OPERATOR_SET_ENV, }), - undefined, + '/tmp/operator-set', ); +}); + +test('resolveRequestAppleSimulatorSetPath refuses a simulator request naming another set than the operator set', () => { + for (const requestedSetPath of ['/tmp/other-set', '/tmp/operator-set/']) { + assert.throws( + () => + resolveRequestAppleSimulatorSetPath({ + requestedSetPath, + platform: 'ios', + env: OPERATOR_SET_ENV, + }), + (error: unknown) => + error instanceof AppError && + error.code === 'INVALID_ARGS' && + error.details?.reason === 'ios_simulator_device_set_outside_daemon_set', + ); + } +}); + +test('resolveRequestAppleSimulatorSetPath leaves the operator set out of platforms without simulator sets', () => { assert.equal( - resolveAppleSimulatorSetPathForSelector({ - simulatorSetPath: '/tmp/scoped', - platform: 'ios', - target: 'mobile', + resolveRequestAppleSimulatorSetPath({ + requestedSetPath: '/tmp/other-set', + platform: 'android', + env: OPERATOR_SET_ENV, }), - '/tmp/scoped', + '/tmp/other-set', ); }); diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 8d80c6fa2b..88e42bfbe9 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -196,7 +196,7 @@ agent-device devices --platform android --android-device-allowlist emulator-5554 - If the selected Xcode can't resolve a scoped simulator for the Apple runner, the runner start fails with `details.reason: "simulator_set_destination_not_found"`, naming the set and, when known, the Xcode version. - `--android-device-allowlist ` limits Android discovery and selection to comma- or space-separated serials. - Set the same scopes with the `iosSimulatorDeviceSet` and `androidDeviceAllowlist` config keys, or `AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST`. CLI flags override environment values, except under a `strip` session lock, which ignores conflicting per-call selectors. -- Start the daemon with `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET=` to scope every request to one simulator set. A request without `--ios-simulator-device-set` uses that set; one naming another set fails with `INVALID_ARGS` and `details.reason: "ios_simulator_device_set_outside_daemon_set"`. +- Start the daemon with `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET=` to scope every iOS simulator request to one simulator set. A request without `--ios-simulator-device-set` uses that set, so iOS physical devices are not listed on that daemon. A request naming a different path string, including the same path with a trailing slash, fails with `INVALID_ARGS` and `details.reason: "ios_simulator_device_set_outside_daemon_set"`. macOS and other non-iOS requests ignore the set, and the host Mac stays listed. A daemon that the CLI starts inherits the CLI's environment, so later CLI calls with a different value still use the first value until the daemon restarts. - Scope applies before `--device`, `--udid`, and `--serial`, so an out-of-scope selector fails with `DEVICE_NOT_FOUND`. - On macOS, daemon startup removes any symlink at `~/Library/Developer/XCTestDevices`, including one you made yourself (for example to move it to another volume), and restores `XCTestDevices.agent-device-backup` when it exists. A symlinked `XCTestDevices` is not supported. Only the link is removed, never the directory it points to; `daemon.log` records each step and any failed restore, which doesn't stop the daemon. diff --git a/website/docs/docs/configuration.md b/website/docs/docs/configuration.md index 3caf86cbfb..f72b6af2ae 100644 --- a/website/docs/docs/configuration.md +++ b/website/docs/docs/configuration.md @@ -122,7 +122,7 @@ Only the variables below, and those documented on command pages, are supported. | Category | Env vars | Decision | | --- | --- | --- | | CLI defaults and config | `AGENT_DEVICE_HOME`, `AGENT_DEVICE_CONFIG`, `AGENT_DEVICE_SESSION`, `AGENT_DEVICE_PLATFORM`, `AGENT_DEVICE_SCREENSHOT_SCALE`, `AGENT_DEVICE_SESSION_LOCK`, `AGENT_DEVICE_DAEMON_BASE_URL`, `AGENT_DEVICE_DAEMON_AUTH_TOKEN`, `AGENT_DEVICE_CLOUD_BASE_URL` | Public | -| Device scoping | `AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST`, `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` | Public. `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` is read by the daemon: every request that names no `--ios-simulator-device-set` uses that set, and a request naming another set fails with `INVALID_ARGS` (`details.reason: "ios_simulator_device_set_outside_daemon_set"`). Restart the daemon after changing it. | +| Device scoping | `AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST`, `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` | Public. `AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET` is read by the daemon: every iOS simulator request that names no `--ios-simulator-device-set` uses that set, so iOS physical devices are not listed, and a request naming a different path string fails with `INVALID_ARGS` (`details.reason: "ios_simulator_device_set_outside_daemon_set"`). macOS and non-iOS requests ignore it. A daemon started by the CLI inherits the CLI's value; restart the daemon after changing it. | | Android test IME | `AGENT_DEVICE_TEST_IME` | Public. Same setting as `--test-ime` / `--no-test-ime` on `open`, `test`, and `replay`; see known limitations. | | Local daemon storage | `AGENT_DEVICE_STATE_DIR` | Public | | Metro and install helpers | `AGENT_DEVICE_METRO_BEARER_TOKEN`, `AGENT_DEVICE_BUNDLETOOL_JAR` | Public |