From 8253d3d0ec71e9f84a1b50430174cd5b1bbb808b Mon Sep 17 00:00:00 2001 From: zerone0x <39543393+zerone0x@users.noreply.github.com> Date: Tue, 27 Oct 2026 19:27:33 +0000 Subject: [PATCH 1/2] fix(daemon): accept the bound Apple device UDID as --device Fixes #3945 --- .changeset/session-device-udid-selector.md | 5 +++ .../core/__tests__/session-selector.test.ts | 34 +++++++++++++++++++ packages/daemon/src/core/session-selector.ts | 14 +++++++- .../session-device-resolution.test.ts | 33 ++++++++++++++++++ .../resources/session-device-resolution.ts | 24 ++++++++++--- 5 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 .changeset/session-device-udid-selector.md diff --git a/.changeset/session-device-udid-selector.md b/.changeset/session-device-udid-selector.md new file mode 100644 index 0000000000..6487c7966e --- /dev/null +++ b/.changeset/session-device-udid-selector.md @@ -0,0 +1,5 @@ +--- +'agent-device': patch +--- + +Accept `--device=` for a session bound to an Apple device when the UDID is that bound device's. Previously only the device name matched, so a request that named the bound device by its UDID failed with `INVALID_ARGS`. Commands that re-resolve an explicit selector resolve that value as the bound device's UDID. A different device is still rejected. diff --git a/packages/daemon/src/core/__tests__/session-selector.test.ts b/packages/daemon/src/core/__tests__/session-selector.test.ts index a51470fe26..28604dd403 100644 --- a/packages/daemon/src/core/__tests__/session-selector.test.ts +++ b/packages/daemon/src/core/__tests__/session-selector.test.ts @@ -148,6 +148,40 @@ test('accepts the AVD name selector that selected the bound device', async () => ); }); +test('accepts the bound Apple device UDID as a device selector', () => { + const session = makeSession({ + device: { + platform: 'apple', + id: 'B2C1-SIM-001', + name: 'iPhone 17 Pro', + kind: 'simulator', + target: 'mobile', + booted: true, + }, + }); + assert.doesNotThrow(() => + assertSessionSelectorMatches(ref(session), { platform: 'ios', device: 'B2C1-SIM-001' }), + ); + assert.throws( + () => assertSessionSelectorMatches(ref(session), { device: 'B2C1-SIM-002' }), + (err: unknown) => + err instanceof AppError && + err.code === 'INVALID_ARGS' && + err.message.includes('--device=B2C1-SIM-002'), + ); +}); + +test('rejects an Android serial as a device selector', () => { + const session = makeSession(); + assert.throws( + () => assertSessionSelectorMatches(ref(session), { device: 'emulator-5554' }), + (err: unknown) => + err instanceof AppError && + err.code === 'INVALID_ARGS' && + err.message.includes('--device=emulator-5554'), + ); +}); + test('rejects mismatched target selector', () => { const session = makeSession(); assert.throws( diff --git a/packages/daemon/src/core/session-selector.ts b/packages/daemon/src/core/session-selector.ts index da80b1e7dd..e89b41c1ba 100644 --- a/packages/daemon/src/core/session-selector.ts +++ b/packages/daemon/src/core/session-selector.ts @@ -72,7 +72,7 @@ export function listSessionSelectorConflicts( mismatches.push({ key: 'serial', value: flags.serial }); } - if (flags.device && !matchesDeviceNameSelector(device, flags.device)) { + if (flags.device && !matchesBoundDeviceSelector(device, flags.device)) { mismatches.push({ key: 'device', value: flags.device }); } @@ -98,6 +98,18 @@ export function listSessionSelectorConflicts( return mismatches; } +function matchesBoundDeviceSelector(device: SessionState['device'], selector: string): boolean { + return matchesDeviceNameSelector(device, selector) || isBoundAppleDeviceUdid(device, selector); +} + +/** + * A bound Apple device also answers `--device=`: its UDID is the one identity that stays + * unique when two simulators share a name. + */ +export function isBoundAppleDeviceUdid(device: SessionState['device'], selector: string): boolean { + return isIosFamily(device) && selector === device.id; +} + export function formatSessionSelectorConflict(conflict: SessionSelectorConflict): string { return `${flagNameForConflictKey(conflict.key)}=${conflict.value}`; } diff --git a/packages/daemon/src/resources/__tests__/session-device-resolution.test.ts b/packages/daemon/src/resources/__tests__/session-device-resolution.test.ts index 5776a120f3..ca18528837 100644 --- a/packages/daemon/src/resources/__tests__/session-device-resolution.test.ts +++ b/packages/daemon/src/resources/__tests__/session-device-resolution.test.ts @@ -287,3 +287,36 @@ test('resolveCommandDevice keeps an app device session on its app when a selecto expect(device).toBe(session.device); }); + +test('resolveCommandDevice resolves the bound Apple device UDID given as --device by UDID', async () => { + mockResolveTargetDevice.mockResolvedValue(iosSimulatorSession.device); + + const device = await resolveCommandDevice({ + platformServices, + session: iosSimulatorSession, + flags: { platform: 'ios', device: 'sim-1' }, + env: process.env, + }); + + expect(device).toBe(iosSimulatorSession.device); + expect(mockResolveTargetDevice).toHaveBeenCalledWith( + { platform: 'ios', udid: 'sim-1' }, + expect.anything(), + ); +}); + +test('resolveCommandDevice keeps --device as a name when it is not the bound device UDID', async () => { + mockResolveTargetDevice.mockResolvedValue(iosSimulatorSession.device); + + await resolveCommandDevice({ + platformServices, + session: iosSimulatorSession, + flags: { platform: 'ios', device: 'iPhone 17 Pro' }, + env: process.env, + }); + + expect(mockResolveTargetDevice).toHaveBeenCalledWith( + { platform: 'ios', device: 'iPhone 17 Pro' }, + expect.anything(), + ); +}); diff --git a/packages/daemon/src/resources/session-device-resolution.ts b/packages/daemon/src/resources/session-device-resolution.ts index 412db2fdc8..9f531b8a3a 100644 --- a/packages/daemon/src/resources/session-device-resolution.ts +++ b/packages/daemon/src/resources/session-device-resolution.ts @@ -11,7 +11,7 @@ import { hasDeviceSelectionInput, hasExplicitDeviceSelector, } from '../core/device/device-selector-intent.ts'; -import { listSessionSelectorConflicts } from '../core/session-selector.ts'; +import { isBoundAppleDeviceUdid, listSessionSelectorConflicts } from '../core/session-selector.ts'; import { errorResponse } from '@agent-device/kernel/contracts'; const pinnedSessionDevices = new WeakMap< @@ -59,16 +59,30 @@ export async function resolveCommandDevice(params: { pinSessionDeviceSelection(params.platformServices, params.session.device, selected); return selected; } - const device = await resolveTargetDevice(params.flags ?? {}, { - env: params.env, - androidAvdSelection: params.androidAvdSelection, - }); + const device = await resolveTargetDevice( + boundUdidAsIdentitySelector(params.session, params.flags), + { + env: params.env, + androidAvdSelection: params.androidAvdSelection, + }, + ); // A selector that names the host of the session's app device stays on that app device: the // session holds one app, not the host (ADR 0035). const hostApp = params.session?.device.hostApp; return hostApp && device.id === hostApp.hostDeviceId ? params.session!.device : device; } +function boundUdidAsIdentitySelector( + session: SessionState | undefined, + flags: DaemonRequest['flags'] | undefined, +): NonNullable { + if (!session || !flags?.device || !isBoundAppleDeviceUdid(session.device, flags.device)) { + return flags ?? {}; + } + const { device: udid, ...rest } = flags; + return { ...rest, udid }; +} + /** Boot and shutdown both allow a stopped Android AVD to be selected by name. */ export async function resolvePowerCommandDevice( params: Omit[0], 'androidAvdSelection'>, From 417cfb5233010076d3001eb706f4201483f51888 Mon Sep 17 00:00:00 2001 From: zerone0x Date: Wed, 28 Oct 2026 18:40:09 +0000 Subject: [PATCH 2/2] fix(daemon): point a --device UDID conflict to --udid Per review on #3945, --device stays a name selector and --udid the ID selector. Keep rejecting --device= for a session bound to an Apple device, but when the value is the bound device's UDID, the conflict error now says to use --udid . Drop the resolveCommandDevice change. Co-Authored-By: Claude Opus 5.5 --- .changeset/session-device-udid-selector.md | 2 +- .../core/__tests__/session-selector.test.ts | 17 +++++++--- packages/daemon/src/core/session-selector.ts | 28 ++++++++++------ .../session-device-resolution.test.ts | 33 ------------------- .../resources/session-device-resolution.ts | 24 +++----------- .../__tests__/request-lock-policy.test.ts | 21 ++++++++++++ .../daemon/src/server/request-lock-policy.ts | 5 ++- 7 files changed, 61 insertions(+), 69 deletions(-) diff --git a/.changeset/session-device-udid-selector.md b/.changeset/session-device-udid-selector.md index 6487c7966e..22bc7e7cd9 100644 --- a/.changeset/session-device-udid-selector.md +++ b/.changeset/session-device-udid-selector.md @@ -2,4 +2,4 @@ 'agent-device': patch --- -Accept `--device=` for a session bound to an Apple device when the UDID is that bound device's. Previously only the device name matched, so a request that named the bound device by its UDID failed with `INVALID_ARGS`. Commands that re-resolve an explicit selector resolve that value as the bound device's UDID. A different device is still rejected. +When a session bound to an Apple device rejects `--device=` whose value is that device's UDID, the conflict error now points to `--udid `. `--device` stays a name selector and `--udid` the ID selector. diff --git a/packages/daemon/src/core/__tests__/session-selector.test.ts b/packages/daemon/src/core/__tests__/session-selector.test.ts index 28604dd403..39ea0ba563 100644 --- a/packages/daemon/src/core/__tests__/session-selector.test.ts +++ b/packages/daemon/src/core/__tests__/session-selector.test.ts @@ -148,7 +148,7 @@ test('accepts the AVD name selector that selected the bound device', async () => ); }); -test('accepts the bound Apple device UDID as a device selector', () => { +test('rejects the bound Apple device UDID as a device selector and points to --udid', () => { const session = makeSession({ device: { platform: 'apple', @@ -159,15 +159,21 @@ test('accepts the bound Apple device UDID as a device selector', () => { booted: true, }, }); - assert.doesNotThrow(() => - assertSessionSelectorMatches(ref(session), { platform: 'ios', device: 'B2C1-SIM-001' }), + assert.throws( + () => assertSessionSelectorMatches(ref(session), { platform: 'ios', device: 'B2C1-SIM-001' }), + (err: unknown) => + err instanceof AppError && + err.code === 'INVALID_ARGS' && + err.message.includes('--device=B2C1-SIM-001') && + err.message.includes('--udid B2C1-SIM-001'), ); assert.throws( () => assertSessionSelectorMatches(ref(session), { device: 'B2C1-SIM-002' }), (err: unknown) => err instanceof AppError && err.code === 'INVALID_ARGS' && - err.message.includes('--device=B2C1-SIM-002'), + err.message.includes('--device=B2C1-SIM-002') && + !err.message.includes('--udid'), ); }); @@ -178,7 +184,8 @@ test('rejects an Android serial as a device selector', () => { (err: unknown) => err instanceof AppError && err.code === 'INVALID_ARGS' && - err.message.includes('--device=emulator-5554'), + err.message.includes('--device=emulator-5554') && + !err.message.includes('--udid'), ); }); diff --git a/packages/daemon/src/core/session-selector.ts b/packages/daemon/src/core/session-selector.ts index e89b41c1ba..df5e6cb128 100644 --- a/packages/daemon/src/core/session-selector.ts +++ b/packages/daemon/src/core/session-selector.ts @@ -29,9 +29,11 @@ export function assertSessionSelectorMatches(ref: SessionRef, flags?: CommandFla const mismatches = listSessionSelectorConflicts(session, flags); if (mismatches.length === 0) return; + const udidHint = describeDeviceUdidSelectorConflict(session, mismatches); throw new AppError( 'INVALID_ARGS', - `Session "${address}" is already bound to ${describeSessionDevice(session)}, but this request selected ${mismatches.map(formatSessionSelectorConflict).join(', ')}.`, + `Session "${address}" is already bound to ${describeSessionDevice(session)}, but this request selected ${mismatches.map(formatSessionSelectorConflict).join(', ')}.` + + (udidHint ? ` ${udidHint}` : ''), { session: address, conflicts: mismatches.map(formatSessionSelectorConflict), @@ -72,7 +74,7 @@ export function listSessionSelectorConflicts( mismatches.push({ key: 'serial', value: flags.serial }); } - if (flags.device && !matchesBoundDeviceSelector(device, flags.device)) { + if (flags.device && !matchesDeviceNameSelector(device, flags.device)) { mismatches.push({ key: 'device', value: flags.device }); } @@ -98,16 +100,22 @@ export function listSessionSelectorConflicts( return mismatches; } -function matchesBoundDeviceSelector(device: SessionState['device'], selector: string): boolean { - return matchesDeviceNameSelector(device, selector) || isBoundAppleDeviceUdid(device, selector); -} - /** - * A bound Apple device also answers `--device=`: its UDID is the one identity that stays - * unique when two simulators share a name. + * `--device` selects by name and `--udid` by ID (#3945). A `--device` conflict whose value is the + * bound Apple device's UDID is the caller naming the right device with the wrong flag, so the + * refusal says which flag takes that value instead of only rejecting it. */ -export function isBoundAppleDeviceUdid(device: SessionState['device'], selector: string): boolean { - return isIosFamily(device) && selector === device.id; +export function describeDeviceUdidSelectorConflict( + session: SessionState, + conflicts: SessionSelectorConflict[], +): string | undefined { + const device = session.device; + if (!isIosFamily(device)) return undefined; + const udidAsDevice = conflicts.some( + (conflict) => conflict.key === 'device' && conflict.value === device.id, + ); + if (!udidAsDevice) return undefined; + return `--device selects a device by name; select this device by UDID with --udid ${device.id}.`; } export function formatSessionSelectorConflict(conflict: SessionSelectorConflict): string { diff --git a/packages/daemon/src/resources/__tests__/session-device-resolution.test.ts b/packages/daemon/src/resources/__tests__/session-device-resolution.test.ts index ca18528837..5776a120f3 100644 --- a/packages/daemon/src/resources/__tests__/session-device-resolution.test.ts +++ b/packages/daemon/src/resources/__tests__/session-device-resolution.test.ts @@ -287,36 +287,3 @@ test('resolveCommandDevice keeps an app device session on its app when a selecto expect(device).toBe(session.device); }); - -test('resolveCommandDevice resolves the bound Apple device UDID given as --device by UDID', async () => { - mockResolveTargetDevice.mockResolvedValue(iosSimulatorSession.device); - - const device = await resolveCommandDevice({ - platformServices, - session: iosSimulatorSession, - flags: { platform: 'ios', device: 'sim-1' }, - env: process.env, - }); - - expect(device).toBe(iosSimulatorSession.device); - expect(mockResolveTargetDevice).toHaveBeenCalledWith( - { platform: 'ios', udid: 'sim-1' }, - expect.anything(), - ); -}); - -test('resolveCommandDevice keeps --device as a name when it is not the bound device UDID', async () => { - mockResolveTargetDevice.mockResolvedValue(iosSimulatorSession.device); - - await resolveCommandDevice({ - platformServices, - session: iosSimulatorSession, - flags: { platform: 'ios', device: 'iPhone 17 Pro' }, - env: process.env, - }); - - expect(mockResolveTargetDevice).toHaveBeenCalledWith( - { platform: 'ios', device: 'iPhone 17 Pro' }, - expect.anything(), - ); -}); diff --git a/packages/daemon/src/resources/session-device-resolution.ts b/packages/daemon/src/resources/session-device-resolution.ts index 9f531b8a3a..412db2fdc8 100644 --- a/packages/daemon/src/resources/session-device-resolution.ts +++ b/packages/daemon/src/resources/session-device-resolution.ts @@ -11,7 +11,7 @@ import { hasDeviceSelectionInput, hasExplicitDeviceSelector, } from '../core/device/device-selector-intent.ts'; -import { isBoundAppleDeviceUdid, listSessionSelectorConflicts } from '../core/session-selector.ts'; +import { listSessionSelectorConflicts } from '../core/session-selector.ts'; import { errorResponse } from '@agent-device/kernel/contracts'; const pinnedSessionDevices = new WeakMap< @@ -59,30 +59,16 @@ export async function resolveCommandDevice(params: { pinSessionDeviceSelection(params.platformServices, params.session.device, selected); return selected; } - const device = await resolveTargetDevice( - boundUdidAsIdentitySelector(params.session, params.flags), - { - env: params.env, - androidAvdSelection: params.androidAvdSelection, - }, - ); + const device = await resolveTargetDevice(params.flags ?? {}, { + env: params.env, + androidAvdSelection: params.androidAvdSelection, + }); // A selector that names the host of the session's app device stays on that app device: the // session holds one app, not the host (ADR 0035). const hostApp = params.session?.device.hostApp; return hostApp && device.id === hostApp.hostDeviceId ? params.session!.device : device; } -function boundUdidAsIdentitySelector( - session: SessionState | undefined, - flags: DaemonRequest['flags'] | undefined, -): NonNullable { - if (!session || !flags?.device || !isBoundAppleDeviceUdid(session.device, flags.device)) { - return flags ?? {}; - } - const { device: udid, ...rest } = flags; - return { ...rest, udid }; -} - /** Boot and shutdown both allow a stopped Android AVD to be selected by name. */ export async function resolvePowerCommandDevice( params: Omit[0], 'androidAvdSelection'>, diff --git a/packages/daemon/src/server/__tests__/request-lock-policy.test.ts b/packages/daemon/src/server/__tests__/request-lock-policy.test.ts index 7d84fdb1d0..249cb35e1d 100644 --- a/packages/daemon/src/server/__tests__/request-lock-policy.test.ts +++ b/packages/daemon/src/server/__tests__/request-lock-policy.test.ts @@ -431,3 +431,24 @@ test('strips only conflicting scope selectors for existing sessions, keeping mat assert.equal(req.flags?.device, 'iPhone 16'); assert.equal(req.flags?.androidDeviceAllowlist, undefined); }); + +test('points a bound-session --device UDID conflict to --udid', () => { + assert.throws( + () => + applyRequestLockPolicy( + { + token: 'token', + session: 'qa-ios', + command: 'snapshot', + positionals: [], + flags: { device: 'SIM-001' }, + meta: { lockPolicy: 'reject' }, + }, + ref(IOS_SESSION), + ), + (error: unknown) => + error instanceof Error && + error.message.includes('--device=SIM-001') && + error.message.includes('--udid SIM-001'), + ); +}); diff --git a/packages/daemon/src/server/request-lock-policy.ts b/packages/daemon/src/server/request-lock-policy.ts index 56d2f7410c..b0af776c6e 100644 --- a/packages/daemon/src/server/request-lock-policy.ts +++ b/packages/daemon/src/server/request-lock-policy.ts @@ -3,6 +3,7 @@ import { AppError } from '@agent-device/kernel/errors'; import type { DaemonRequest } from '../core/daemon-request.ts'; import type { SessionRef, SessionState } from '../core/session-state.ts'; import { + describeDeviceUdidSelectorConflict, formatSessionSelectorConflict, listSessionSelectorConflicts, type SessionSelectorConflict, @@ -118,9 +119,11 @@ function buildLockPolicyConflictMessage( ): string { const conflictList = conflicts.map(formatSessionSelectorConflict).join(', '); if (existingRef) { + const udidHint = describeDeviceUdidSelectorConflict(existingRef.session, conflicts); return ( `Session "${existingRef.address}" is already bound to ${describeSessionDevice(existingRef.session)}, ` + - `but ${req.command} selected ${conflictList}.` + `but ${req.command} selected ${conflictList}.` + + (udidHint ? ` ${udidHint}` : '') ); } const lockPlatform = req.meta?.lockPlatform;