Skip to content

Commit d9d7539

Browse files
committed
refactor(selection): declare stopped AVD scope once
1 parent 84e5cce commit d9d7539

9 files changed

Lines changed: 65 additions & 71 deletions

File tree

‎packages/command-registry/src/registry.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ export const RAW_COMMAND_DESCRIPTORS = [
303303
frameworkTier: 'extended',
304304
recordsSessionAction: false,
305305
daemon: {
306+
androidAvdSelection: 'include-stopped',
306307
route: 'session',
307308
refFrameEffect: 'preserve',
308309
sessionKind: 'inventory',
@@ -348,6 +349,7 @@ export const RAW_COMMAND_DESCRIPTORS = [
348349
frameworkTier: 'extended',
349350
recordsSessionAction: false,
350351
daemon: {
352+
androidAvdSelection: 'include-stopped',
351353
route: 'session',
352354
refFrameEffect: 'preserve',
353355
sessionKind: 'inventory',
@@ -368,6 +370,7 @@ export const RAW_COMMAND_DESCRIPTORS = [
368370
frameworkTier: 'extended',
369371
recordsSessionAction: false,
370372
daemon: {
373+
androidAvdSelection: 'include-stopped',
371374
route: 'session',
372375
refFrameEffect: 'may-invalidate',
373376
sessionKind: 'state',
@@ -387,6 +390,7 @@ export const RAW_COMMAND_DESCRIPTORS = [
387390
frameworkTier: 'extended',
388391
recordsSessionAction: false,
389392
daemon: {
393+
androidAvdSelection: 'include-stopped',
390394
route: 'session',
391395
refFrameEffect: 'may-invalidate',
392396
sessionKind: 'state',

‎packages/daemon/src/resources/__tests__/session-device-resolution.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
resolveCommandDevice,
99
refreshSessionDeviceIfNeeded,
1010
pinSessionDeviceSelection,
11-
resolvePowerCommandDevice,
1211
selectorTargetsSessionDevice,
1312
} from '../session-device-resolution.ts';
1413
import { resolveTargetDevice } from '@agent-device/device-selection/dispatch-resolve';
@@ -117,7 +116,7 @@ test('resolveCommandDevice does not prepare a sessionless device', async () => {
117116
expect(mockEnsureDeviceReady).not.toHaveBeenCalled();
118117
});
119118

120-
test('power command selection includes a stopped Android AVD', async () => {
119+
test('command selection forwards stopped Android AVD scope', async () => {
121120
const stopped: DeviceInfo = {
122121
platform: 'android',
123122
id: 'emulator-5554',
@@ -126,7 +125,8 @@ test('power command selection includes a stopped Android AVD', async () => {
126125
booted: false,
127126
};
128127
mockResolveTargetDevice.mockResolvedValue(stopped);
129-
const selected = await resolvePowerCommandDevice({
128+
const selected = await resolveCommandDevice({
129+
androidAvdSelection: 'include-stopped',
130130
session: undefined,
131131
flags: { platform: 'android', device: 'Pixel_9' },
132132
env: process.env,

‎packages/daemon/src/resources/session-device-resolution.ts‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,19 @@ export async function resolveCommandDevice(params: {
7676
return hostApp && device.id === hostApp.hostDeviceId ? params.session!.device : device;
7777
}
7878

79-
type PowerCommandDeviceParams = Omit<
80-
Parameters<typeof resolveCommandDevice>[0],
81-
'androidAvdSelection'
82-
>;
83-
84-
/** Boot and shutdown both allow a stopped Android AVD to be selected by name. */
85-
export async function resolvePowerCommandDevice(
86-
params: PowerCommandDeviceParams,
87-
): Promise<DeviceInfo> {
88-
return await resolveCommandDevice({ ...params, androidAvdSelection: 'include-stopped' });
89-
}
90-
9179
/**
92-
* Boot's device with its selection evidence. Boot alone reads the `--pick` opt-in; shutdown shares
93-
* the stopped-AVD scope above and keeps refusing a tie.
80+
* Boot's device with its selection evidence. Boot alone reads the `--pick` opt-in; other
81+
* commands keep refusing a tie.
9482
*/
9583
export async function resolveBootCommandSelection(
96-
params: PowerCommandDeviceParams,
84+
params: Parameters<typeof resolveCommandDevice>[0],
9785
): Promise<DeviceSelectionResult> {
9886
if (params.session && !hasExplicitDeviceSelector(params.flags)) {
99-
return resolveExistingSessionDeviceSelection(await resolvePowerCommandDevice(params));
87+
return resolveExistingSessionDeviceSelection(await resolveCommandDevice(params));
10088
}
10189
return await resolveTargetDeviceSelection(params.flags ?? {}, {
10290
env: params.env,
103-
androidAvdSelection: 'include-stopped',
91+
androidAvdSelection: params.androidAvdSelection,
10492
boot: { pick: params.flags?.pick },
10593
});
10694
}

‎packages/daemon/src/server/__tests__/request-router-selection.test.ts‎

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ function selectionHandler(
4646
deviceInventoryGateways: createTestDeviceInventoryGateways({
4747
local: async (request) => {
4848
inventoryRequests.push(request);
49-
return devices;
49+
return request.androidAvdSelection === 'running-only'
50+
? devices.filter((device) => device.platform !== 'android' || device.booted === true)
51+
: devices;
5052
},
5153
}),
5254
deviceRuntimeGateway: { ...lifecycleDeviceRuntimeGateway, inspectFacts },
@@ -80,33 +82,37 @@ test('fresh no-app foreground open selects iOS from mixed Android and iOS invent
8082
expect(sessionStore.get('foreground')?.device.id).toBe(ios.id);
8183
});
8284

83-
test('shutdown selects a stopped Android AVD and rejects an invalid selector before facts', async () => {
84-
const inventoryRequests: Array<{ platform?: string; androidAvdSelection?: string }> = [];
85-
const stopped = { ...android, booted: false };
86-
const { handler, inspectFacts } = selectionHandler([stopped], inventoryRequests);
87-
const selected = await handler({
88-
token: 'test-token',
89-
session: 'no-session',
90-
command: 'shutdown',
91-
positionals: [],
92-
flags: { platform: 'android', device: stopped.name },
93-
meta: { requestId: 'shutdown-stopped' },
94-
});
95-
expect(
96-
inventoryRequests.some((request) => request.androidAvdSelection === 'include-stopped'),
97-
).toBe(true);
98-
if (!selected.ok) expect(selected.error.code).not.toBe('DEVICE_NOT_FOUND');
99-
expect(inspectFacts).toHaveBeenCalledWith(stopped);
100-
const factsBeforeInvalid = inspectFacts.mock.calls.length;
85+
test.each(['boot', 'shutdown', 'capabilities', 'apps'] as const)(
86+
'%s keeps stopped-AVD selection consistent through request admission',
87+
async (command) => {
88+
const inventoryRequests: Array<{ platform?: string; androidAvdSelection?: string }> = [];
89+
const stopped = { ...android, id: android.name, booted: false };
90+
const { handler, inspectFacts } = selectionHandler([stopped], inventoryRequests);
91+
const selected = await handler({
92+
token: 'test-token',
93+
session: 'no-session',
94+
command,
95+
positionals: [],
96+
flags: { platform: 'android', device: stopped.name },
97+
meta: { requestId: `${command}-stopped` },
98+
});
99+
expect(
100+
inventoryRequests.length > 0 &&
101+
inventoryRequests.every((request) => request.androidAvdSelection === 'include-stopped'),
102+
).toBe(true);
103+
if (!selected.ok) expect(selected.error.code).not.toBe('DEVICE_NOT_FOUND');
104+
expect(inspectFacts).toHaveBeenCalledWith(stopped);
105+
const factsBeforeInvalid = inspectFacts.mock.calls.length;
101106

102-
const invalid = await handler({
103-
token: 'test-token',
104-
session: 'no-session',
105-
command: 'shutdown',
106-
positionals: [],
107-
flags: { platform: 'android', device: 'missing-avd' },
108-
meta: { requestId: 'shutdown-missing' },
109-
});
110-
expect(invalid).toMatchObject({ ok: false, error: { code: 'DEVICE_NOT_FOUND' } });
111-
expect(inspectFacts).toHaveBeenCalledTimes(factsBeforeInvalid);
112-
});
107+
const invalid = await handler({
108+
token: 'test-token',
109+
session: 'no-session',
110+
command,
111+
positionals: [],
112+
flags: { platform: 'android', device: 'missing-avd' },
113+
meta: { requestId: `${command}-missing` },
114+
});
115+
expect(invalid).toMatchObject({ ok: false, error: { code: 'DEVICE_NOT_FOUND' } });
116+
expect(inspectFacts).toHaveBeenCalledTimes(factsBeforeInvalid);
117+
},
118+
);

‎packages/daemon/src/server/request-execution-scope.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,8 @@ async function resolveBoundedClaimDevice(
138138
env: EnvMap,
139139
platformServices: DaemonPlatformServices,
140140
): Promise<DeviceInfo> {
141-
const {
142-
resolveBootCommandSelection,
143-
resolveCommandDevice,
144-
resolvePowerCommandDevice,
145-
requireSessionOrExplicitSelector,
146-
} = await import('../resources/session-device-resolution.ts');
141+
const { resolveBootCommandSelection, resolveCommandDevice, requireSessionOrExplicitSelector } =
142+
await import('../resources/session-device-resolution.ts');
147143
if (req.command !== 'open') {
148144
const guard = requireSessionOrExplicitSelector(req.command, session, req.flags);
149145
if (guard && !guard.ok) {
@@ -161,8 +157,7 @@ async function resolveBoundedClaimDevice(
161157
androidAvdSelection: resolveCommandAndroidAvdSelection(req.command),
162158
};
163159
if (req.command === 'boot') return (await resolveBootCommandSelection(params)).device;
164-
const resolve = req.command === 'shutdown' ? resolvePowerCommandDevice : resolveCommandDevice;
165-
return await resolve(params);
160+
return await resolveCommandDevice(params);
166161
}
167162

168163
// Production daemon wiring owns one LeaseRegistry per process; scoping locks by registry keeps

‎packages/daemon/src/sessions/handlers/session-app-source-deployment.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export async function handleInstallFromSourceDeploymentCommand(params: {
6969
const retention = resolveRetention(req);
7070
let device = await resolveInstallDevice(
7171
session,
72-
req.flags,
72+
req,
7373
sessionStore.host.env,
7474
params.platformServices,
7575
);
@@ -141,10 +141,11 @@ export async function handleReleaseMaterializedPathsCommand(params: {
141141

142142
async function resolveInstallDevice(
143143
session: SessionState | undefined,
144-
flags: DaemonRequest['flags'] | undefined,
144+
req: DaemonRequest,
145145
env: EnvMap,
146146
platformServices: DaemonPlatformServices,
147147
): Promise<SessionState['device']> {
148+
const flags = req.flags;
148149
const requestedPlatform = normalizePlatform(flags?.platform);
149150
if (session && requestedPlatform && !matchesPlatformSelector(session.device, requestedPlatform)) {
150151
throw new AppError(
@@ -168,7 +169,7 @@ async function resolveInstallDevice(
168169
session,
169170
flags,
170171
env,
171-
androidAvdSelection: resolveCommandAndroidAvdSelection('install_source'),
172+
androidAvdSelection: resolveCommandAndroidAvdSelection(req.command),
172173
platformServices,
173174
});
174175
}

‎packages/daemon/src/sessions/handlers/session-boot.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolveCommandAndroidAvdSelection } from '../../core/daemon-command-registry.ts';
12
import type { DaemonResponse } from '@agent-device/kernel/contracts';
23
import { resolveDeviceReadinessRuntimePlan } from '@agent-device/contracts/platform-runtime-operations';
34
import type { DeviceInfo } from '@agent-device/kernel/device';
@@ -60,6 +61,7 @@ async function resolveBootTargetSelection(
6061
session,
6162
flags,
6263
env: sessionStore.host.env,
64+
androidAvdSelection: resolveCommandAndroidAvdSelection(req.command),
6365
platformServices,
6466
});
6567
const { device } = selection;

‎packages/daemon/src/sessions/handlers/session-shutdown.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolveCommandAndroidAvdSelection } from '../../core/daemon-command-registry.ts';
12
import type { DaemonResponse } from '@agent-device/kernel/contracts';
23
import type { RuntimeOperationFact } from '@agent-device/contracts/platform-runtime';
34
import type { TargetShutdownResult } from '@agent-device/contracts/device';
@@ -6,7 +7,7 @@ import { publicPlatformString } from '@agent-device/kernel/device';
67
import { errorResponse } from '@agent-device/kernel/contracts';
78
import {
89
requireSessionOrExplicitSelector,
9-
resolvePowerCommandDevice,
10+
resolveCommandDevice,
1011
} from '../../resources/session-device-resolution.ts';
1112
import {
1213
requireRuntimeBinding,
@@ -43,10 +44,11 @@ export async function handleShutdownCommand(
4344
const guard = requireSessionOrExplicitSelector(params.req.command, activeSession, flags);
4445
if (guard) return guard;
4546

46-
const device = await resolvePowerCommandDevice({
47+
const device = await resolveCommandDevice({
4748
flags,
4849
session: activeSession,
4950
env: params.sessionStore.host.env,
51+
androidAvdSelection: resolveCommandAndroidAvdSelection(params.req.command),
5052
platformServices: params.platformServices,
5153
});
5254
const inspectFacts = requireRuntimeFacts(params.inspectFacts);

‎packages/daemon/src/sessions/session-lifecycle/internal/inventory.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolveCommandAndroidAvdSelection } from '../../../core/daemon-command-registry.ts';
12
import type { HostEnvironment } from '@agent-device/kernel/source-value';
23
import { resolveSessionRunnerLogPath } from '@agent-device/daemon-contracts/session-artifact-paths';
34
import type { DaemonResponse } from '@agent-device/kernel/contracts';
@@ -273,10 +274,7 @@ async function capabilitiesInventoryResponse(params: {
273274
inspectFacts?: InspectDeviceRuntimeFacts;
274275
platformServices: DaemonPlatformServices;
275276
}): Promise<DaemonResponse> {
276-
const resolution = await resolveInventoryCommandDevice({
277-
...params,
278-
androidAvdSelection: 'include-stopped',
279-
});
277+
const resolution = await resolveInventoryCommandDevice(params);
280278
if ('response' in resolution) return resolution.response;
281279
const { device } = resolution;
282280
const sessionOwnedAppStateAvailable = isSessionOwnedAppStateAvailable(
@@ -349,7 +347,6 @@ async function handleAppsInventory(params: {
349347
req,
350348
sessionName,
351349
sessionStore,
352-
androidAvdSelection: 'include-stopped',
353350
platformServices,
354351
});
355352
if ('response' in resolution) return resolution.response;
@@ -448,10 +445,9 @@ async function resolveInventoryCommandDevice(params: {
448445
req: DaemonRequest;
449446
sessionName: string;
450447
sessionStore: SessionStore;
451-
androidAvdSelection?: 'running-only' | 'include-stopped';
452448
platformServices: DaemonPlatformServices;
453449
}): Promise<{ device: DeviceInfo } | { response: DaemonResponse }> {
454-
const { req, sessionName, sessionStore, androidAvdSelection, platformServices } = params;
450+
const { req, sessionName, sessionStore, platformServices } = params;
455451
const session = sessionStore.get(sessionName);
456452
const flags = req.flags ?? {};
457453
const response = requireSessionOrExplicitSelector(req.command, session, flags);
@@ -463,7 +459,7 @@ async function resolveInventoryCommandDevice(params: {
463459
flags,
464460
env: sessionStore.host.env,
465461
platformServices,
466-
androidAvdSelection,
462+
androidAvdSelection: resolveCommandAndroidAvdSelection(req.command),
467463
}),
468464
};
469465
}

0 commit comments

Comments
 (0)