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
20 changes: 14 additions & 6 deletions packages/kernel/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ function resolveDeviceByName(
deviceName: string | undefined,
): DeviceInfo | undefined {
if (!deviceName) return undefined;
const normalizedName = normalizeDeviceName(deviceName);
const match = candidates.find((device) => normalizeDeviceName(device.name) === normalizedName);
const match = candidates.find((device) => matchesDeviceNameSelector(device, deviceName));
if (!match) {
const hint = deviceIdentityMistakenForNameHint(candidates, deviceName);
throw new AppError(
Expand Down Expand Up @@ -570,10 +569,7 @@ function matchesExplicitDeviceSelector(device: DeviceInfo, selector: DeviceSelec
) {
return false;
}
if (
selector.deviceName &&
normalizeDeviceName(device.name) !== normalizeDeviceName(selector.deviceName)
) {
if (selector.deviceName && !matchesDeviceNameSelector(device, selector.deviceName)) {
return false;
}
return true;
Expand Down Expand Up @@ -610,6 +606,18 @@ function throwNoDevicesFound(selector: DeviceSelector, context: DeviceSelectionC
throw new AppError('DEVICE_NOT_FOUND', 'No devices found', { selector });
}

/**
* Whether a `--device` name selects `device`. Selection and session-binding checks share this
* matcher so a name that picked a device keeps matching it, e.g. AVD `Pixel_9_API_37` for the
* displayed `Pixel 9 API 37`.
*/
export function matchesDeviceNameSelector(
device: Pick<DeviceInfo, 'name'>,
deviceName: string,
): boolean {
return normalizeDeviceName(device.name) === normalizeDeviceName(deviceName);
}

function normalizeDeviceName(value: string): string {
return value.toLowerCase().replaceAll('_', ' ').replaceAll(/\s+/g, ' ').trim();
}
Expand Down
15 changes: 15 additions & 0 deletions src/daemon/__tests__/session-selector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { test } from 'vitest';
import assert from 'node:assert/strict';
import { assertSessionSelectorMatches } from '../session-selector.ts';
import { AppError } from '@agent-device/kernel/errors';
import { resolveDevice } from '@agent-device/kernel/device';
import type { SessionRef, SessionState } from '../session-state.ts';

function makeSession(overrides?: Partial<SessionState>): SessionState {
Expand Down Expand Up @@ -132,6 +133,20 @@ test('accepts matching device selector (case-insensitive)', () => {
);
});

test('accepts the AVD name selector that selected the bound device', async () => {
const avdName = 'Pixel_9_API_37';
const device = await resolveDevice(
[{ platform: 'android', id: 'emulator-5554', name: 'Pixel 9 API 37', kind: 'emulator' }],
{ platform: 'android', deviceName: avdName },
);
assert.doesNotThrow(() =>
assertSessionSelectorMatches(ref(makeSession({ device })), {
platform: 'android',
device: avdName,
}),
);
});

test('rejects mismatched target selector', () => {
const session = makeSession();
assert.throws(
Expand Down
3 changes: 2 additions & 1 deletion src/daemon/session-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { SessionRef, SessionState } from './session-state.ts';
import {
isIosFamily,
isSerialAddressablePlatform,
matchesDeviceNameSelector,
matchesPlatformSelector,
} from '@agent-device/kernel/device';
import { parseSerialAllowlist } from '@agent-device/kernel/device-isolation';
Expand Down Expand Up @@ -71,7 +72,7 @@ export function listSessionSelectorConflicts(
mismatches.push({ key: 'serial', value: flags.serial });
}

if (flags.device && flags.device.trim().toLowerCase() !== device.name.trim().toLowerCase()) {
if (flags.device && !matchesDeviceNameSelector(device, flags.device)) {
mismatches.push({ key: 'device', value: flags.device });
}

Expand Down
Loading