Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/session-device-udid-selector.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'agent-device': patch
---

When a session bound to an Apple device rejects `--device=<UDID>` whose value is that device's UDID, the conflict error now points to `--udid <UDID>`. `--device` stays a name selector and `--udid` the ID selector.
41 changes: 41 additions & 0 deletions packages/daemon/src/core/__tests__/session-selector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,47 @@ test('accepts the AVD name selector that selected the bound device', async () =>
);
});

test('rejects the bound Apple device UDID as a device selector and points to --udid', () => {
const session = makeSession({
device: {
platform: 'apple',
id: 'B2C1-SIM-001',
name: 'iPhone 17 Pro',
kind: 'simulator',
target: 'mobile',
booted: true,
},
});
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('--udid'),
);
});

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') &&
!err.message.includes('--udid'),
);
});

test('rejects mismatched target selector', () => {
const session = makeSession();
assert.throws(
Expand Down
22 changes: 21 additions & 1 deletion packages/daemon/src/core/session-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -98,6 +100,24 @@ export function listSessionSelectorConflicts(
return mismatches;
}

/**
* `--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 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 {
return `${flagNameForConflictKey(conflict.key)}=${conflict.value}`;
}
Expand Down
21 changes: 21 additions & 0 deletions packages/daemon/src/server/__tests__/request-lock-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
});
5 changes: 4 additions & 1 deletion packages/daemon/src/server/request-lock-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Loading