Skip to content

Commit 5da9f1b

Browse files
committed
refactor: make the selection resolver the sole owner of selection provenance
Simplifies the deterministic device-selection resolver (net -114 lines vs the previous head) while fixing the outstanding app-aware provenance finding: - Move the booted-simulator app-affinity narrowing into the resolver behind an appleSimulatorAppTarget param, with its own typed reason 'single-app-installed-local' (candidateCount 1). This removes the selectedDevice escape hatch that reported 'preferred-local' with candidateCount 2 for the app-narrowed pick, and gives the app-match errors the same platform-aware retry selectors as every other selection failure. - Delete dead code: the allowBootableLocal param (no caller ever passed it, so the eligibleDevices filter was unreachable), the hasExplicitProviderIdentity alias, the duplicated deviceCandidateDetails in dispatch-resolve, and the double candidate computation. - Shrink the public selection contract to what #1777 specifies: drop `booted` (it contradicted its own doc comment once markSelectionBootedAfterPreparation flipped it; bootOccurred plus the reason codes carry the same information) and drop `retrySelectors` from success metadata (the daemon only ever emits retry selectors inside error details). DeviceSelectionRetrySelector leaves the contracts facade. - Replace the typeof-import lazy seam and resolver threading through four context objects with one lazy forwarding wrapper; the dispatch eager closure stays at 83. - Collapse the Apple path to resolve -> optional simulator fallback; the provider branch goes through the generic resolver call directly. - Consolidate the five copy-pasted resolveTargetDeviceSelection test mocks into one shared stub (selectionFromResolveTargetDevice). - Add the requested regression: two booted simulators with the app on one now assert typed selection metadata through resolveTargetDeviceSelection, plus a resolver-level app-affinity provenance test. Validation: typecheck, oxlint, oxfmt, layering (184-check guard OK), DI seams, fallow changed-files, eager-closure 235/235, daemon suite 323 files / 2287 tests, core/commands/mcp/client suites 255 files / 2133 tests.
1 parent effebe5 commit 5da9f1b

21 files changed

Lines changed: 307 additions & 397 deletions

‎packages/contracts/src/client-app.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,19 @@ export type DeviceSelectionReason =
2020
| 'existing-session'
2121
| 'single-booted-local'
2222
| 'single-bootable-local'
23+
| 'single-app-installed-local'
2324
| 'preferred-local'
2425
| 'single-provider-device';
2526

2627
export type DeviceSelectionSource = 'session' | 'local' | 'provider';
2728

28-
export type DeviceSelectionRetrySelector = {
29-
flag: '--device' | '--serial' | '--udid';
30-
value: string;
31-
};
32-
3329
/** Deterministic device-selection evidence shared by daemon responses and the published client. */
3430
export type DeviceSelectionMetadata = {
3531
reason: DeviceSelectionReason;
3632
source: DeviceSelectionSource;
3733
candidateCount: number;
38-
/** Whether the selected inventory record was already booted at resolution time. */
39-
booted: boolean;
40-
/** Whether the selected lifecycle prepared a previously stopped local target. */
34+
/** Whether this request booted a previously stopped local virtual target during open. */
4135
bootOccurred: boolean;
42-
retrySelectors?: DeviceSelectionRetrySelector[];
4336
};
4437

4538
export type AppInstallOptions = AgentDeviceRequestOverrides &

‎packages/contracts/src/facades/client.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export type {
1313
AppTriggerEventOptions,
1414
DeviceSelectionMetadata,
1515
DeviceSelectionReason,
16-
DeviceSelectionRetrySelector,
1716
DeviceSelectionSource,
1817
MaterializationReleaseOptions,
1918
MaterializationReleaseResult,

‎src/__tests__/client-normalizers.test.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,23 @@ test('normalizeOpenDevice accepts exactly the canonical leaf platforms', () => {
2828
test('normalizeDeviceSelection preserves structured resolver evidence and rejects malformed data', () => {
2929
assert.deepEqual(
3030
normalizeDeviceSelection({
31-
reason: 'single-booted-local',
31+
reason: 'single-app-installed-local',
3232
source: 'local',
33-
candidateCount: 2,
34-
booted: true,
33+
candidateCount: 1,
3534
bootOccurred: false,
36-
retrySelectors: [{ flag: '--udid', value: 'sim-1' }],
3735
}),
3836
{
39-
reason: 'single-booted-local',
37+
reason: 'single-app-installed-local',
4038
source: 'local',
41-
candidateCount: 2,
42-
booted: true,
39+
candidateCount: 1,
4340
bootOccurred: false,
44-
retrySelectors: [{ flag: '--udid', value: 'sim-1' }],
4541
},
4642
);
4743
assert.equal(
4844
normalizeDeviceSelection({
4945
reason: 'single-booted-local',
5046
source: 'local',
5147
candidateCount: -1,
52-
booted: true,
5348
bootOccurred: false,
5449
}),
5550
undefined,

‎src/client/client-normalizers.ts‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -229,44 +229,40 @@ export function normalizeOpenDevice(
229229
};
230230
}
231231

232+
const DEVICE_SELECTION_REASONS: readonly DeviceSelectionMetadata['reason'][] = [
233+
'explicit-selector',
234+
'existing-session',
235+
'single-booted-local',
236+
'single-bootable-local',
237+
'single-app-installed-local',
238+
'preferred-local',
239+
'single-provider-device',
240+
];
241+
242+
const DEVICE_SELECTION_SOURCES: readonly DeviceSelectionMetadata['source'][] = [
243+
'session',
244+
'local',
245+
'provider',
246+
];
247+
232248
export function normalizeDeviceSelection(value: unknown): DeviceSelectionMetadata | undefined {
233249
if (!isRecord(value)) return undefined;
234-
const reason = value.reason;
235-
const source = value.source;
236-
const candidateCount = value.candidateCount;
250+
const { reason, source, candidateCount, bootOccurred } = value;
237251
if (
238-
![
239-
'explicit-selector',
240-
'existing-session',
241-
'single-booted-local',
242-
'single-bootable-local',
243-
'preferred-local',
244-
'single-provider-device',
245-
].includes(reason as string) ||
246-
!['session', 'local', 'provider'].includes(source as string) ||
252+
!DEVICE_SELECTION_REASONS.includes(reason as DeviceSelectionMetadata['reason']) ||
253+
!DEVICE_SELECTION_SOURCES.includes(source as DeviceSelectionMetadata['source']) ||
247254
typeof candidateCount !== 'number' ||
248255
!Number.isInteger(candidateCount) ||
249256
candidateCount < 0 ||
250-
typeof value.booted !== 'boolean' ||
251-
typeof value.bootOccurred !== 'boolean'
257+
typeof bootOccurred !== 'boolean'
252258
) {
253259
return undefined;
254260
}
255-
const retrySelectors = Array.isArray(value.retrySelectors)
256-
? value.retrySelectors.filter(
257-
(selector): selector is { flag: '--device' | '--serial' | '--udid'; value: string } =>
258-
isRecord(selector) &&
259-
['--device', '--serial', '--udid'].includes(selector.flag as string) &&
260-
typeof selector.value === 'string',
261-
)
262-
: [];
263261
return {
264262
reason: reason as DeviceSelectionMetadata['reason'],
265263
source: source as DeviceSelectionMetadata['source'],
266264
candidateCount,
267-
booted: value.booted,
268-
bootOccurred: value.bootOccurred,
269-
...(retrySelectors.length > 0 ? { retrySelectors } : {}),
265+
bootOccurred,
270266
};
271267
}
272268

‎src/commands/management/output.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ describe('openCliOutput', () => {
5353
reason: 'single-booted-local',
5454
source: 'local',
5555
candidateCount: 2,
56-
booted: true,
5756
bootOccurred: false,
5857
},
5958
identifiers: { session: 'selected' },
@@ -64,7 +63,6 @@ describe('openCliOutput', () => {
6463
reason: 'single-booted-local',
6564
source: 'local',
6665
candidateCount: 2,
67-
booted: true,
6866
bootOccurred: false,
6967
},
7068
});

‎src/core/__tests__/device-selection-resolver.test.ts‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import assert from 'node:assert/strict';
2-
import { test } from 'vitest';
2+
import { test, vi } from 'vitest';
33
import { AppError } from '@agent-device/kernel/errors';
44
import {
55
ANDROID_EMULATOR,
66
IOS_SIMULATOR,
77
MACOS_DEVICE,
88
} from '../../__tests__/test-utils/device-fixtures.ts';
99
import {
10-
markSelectionBootedAfterPreparation,
10+
markSelectionBootOccurred,
1111
resolveExistingSessionDeviceSelection,
1212
resolveInventoryDeviceSelection,
1313
} from '../device-selection-resolver.ts';
@@ -16,6 +16,12 @@ import {
1616
STOPPED_ANDROID_EMULATOR,
1717
} from './device-selection-fixtures.ts';
1818

19+
const mockFindIosSimulatorInstalledApp = vi.hoisted(() => vi.fn());
20+
21+
vi.mock('../../platforms/apple/core/apps.ts', () => ({
22+
findIosSimulatorInstalledApp: mockFindIosSimulatorInstalledApp,
23+
}));
24+
1925
test('explicit identity selector wins before local inference', async () => {
2026
const selection = await resolveInventoryDeviceSelection({
2127
devices: [ANDROID_EMULATOR, SECOND_BOOTED_ANDROID_EMULATOR],
@@ -37,7 +43,6 @@ test('a single booted local candidate wins and reports its precedence reason', a
3743

3844
assert.equal(selection.device.id, IOS_SIMULATOR.id);
3945
assert.equal(selection.reason, 'single-booted-local');
40-
assert.equal(selection.booted, true);
4146
});
4247

4348
test('a single bootable local candidate is selectable without a preliminary devices call', async () => {
@@ -49,30 +54,53 @@ test('a single bootable local candidate is selectable without a preliminary devi
4954

5055
assert.equal(selection.device.id, STOPPED_ANDROID_EMULATOR.id);
5156
assert.equal(selection.reason, 'single-bootable-local');
52-
assert.equal(selection.booted, false);
57+
assert.equal(selection.bootOccurred, false);
58+
});
59+
60+
test('the booted simulator with the app installed carries its own selected-by reason', async () => {
61+
const secondBootedSimulator = { ...IOS_SIMULATOR, id: 'sim-2', name: 'iPhone 17' };
62+
mockFindIosSimulatorInstalledApp.mockImplementation(async (device: { id: string }) =>
63+
device.id === secondBootedSimulator.id ? 'com.example.demo' : undefined,
64+
);
65+
66+
const selection = await resolveInventoryDeviceSelection({
67+
devices: [IOS_SIMULATOR, secondBootedSimulator],
68+
selector: { platform: 'ios' },
69+
source: 'local',
70+
appleSimulatorAppTarget: 'com.example.demo',
71+
});
72+
73+
assert.equal(selection.device.id, secondBootedSimulator.id);
74+
assert.equal(selection.reason, 'single-app-installed-local');
75+
assert.equal(selection.candidateCount, 1);
76+
assert.equal(selection.source, 'local');
5377
});
5478

5579
test('successful local virtual preparation records the boot event without changing provider evidence', () => {
56-
const localSelection = markSelectionBootedAfterPreparation({
80+
const localSelection = markSelectionBootOccurred({
5781
device: STOPPED_ANDROID_EMULATOR,
5882
reason: 'single-bootable-local',
5983
source: 'local',
6084
candidateCount: 1,
61-
booted: false,
6285
bootOccurred: false,
6386
});
64-
const providerSelection = markSelectionBootedAfterPreparation({
65-
device: { ...STOPPED_ANDROID_EMULATOR, platform: 'android', kind: 'device' },
87+
const bootedSelection = markSelectionBootOccurred({
88+
device: SECOND_BOOTED_ANDROID_EMULATOR,
89+
reason: 'single-booted-local',
90+
source: 'local',
91+
candidateCount: 1,
92+
bootOccurred: false,
93+
});
94+
const providerSelection = markSelectionBootOccurred({
95+
device: { ...STOPPED_ANDROID_EMULATOR, kind: 'device' },
6696
reason: 'single-provider-device',
6797
source: 'provider',
6898
candidateCount: 1,
69-
booted: false,
7099
bootOccurred: false,
71100
});
72101

73-
assert.equal(localSelection?.booted, true);
74102
assert.equal(localSelection?.bootOccurred, true);
75-
assert.equal(providerSelection?.booted, false);
103+
assert.equal(bootedSelection?.bootOccurred, false);
76104
assert.equal(providerSelection?.bootOccurred, false);
77105
});
78106

@@ -135,7 +163,6 @@ test('existing session binding is a distinct highest-precedence selection', () =
135163
reason: 'existing-session',
136164
source: 'session',
137165
candidateCount: 1,
138-
booted: true,
139166
bootOccurred: false,
140167
});
141168
});

‎src/core/__tests__/dispatch-resolve.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ test('resolveTargetDevice selects the unique booted simulator with the requested
195195
);
196196
});
197197

198+
test('app-narrowed selection reports its own typed provenance, not a generic local reason', async () => {
199+
// Two booted simulators, the app installed on exactly one: the resolver owns
200+
// the narrowing, so the metadata must say so instead of `preferred-local`
201+
// computed against the unnarrowed inventory.
202+
mockListAppleDevices.mockResolvedValue([bootedSimulator, secondBootedSimulator]);
203+
mockFindIosSimulatorInstalledApp.mockImplementation(async (device) =>
204+
device.id === secondBootedSimulator.id ? 'com.example.demo' : undefined,
205+
);
206+
207+
const selection = await withTestDeviceInventory(
208+
{ local: async (request) => await mockListAppleDevices(request) },
209+
async () =>
210+
await resolveTargetDeviceSelectionInContext(
211+
{ platform: 'ios' },
212+
{ appleSimulatorAppTarget: 'com.example.demo' },
213+
),
214+
);
215+
216+
assert.equal(selection.device.id, secondBootedSimulator.id);
217+
assert.equal(selection.reason, 'single-app-installed-local');
218+
assert.equal(selection.source, 'local');
219+
assert.equal(selection.candidateCount, 1);
220+
});
221+
198222
test('resolveTargetDevice leaves platform-less static app selection to normal cross-platform resolution', async () => {
199223
// One booted device: cross-platform resolution answers without probing simulators for the app.
200224
const result = await withDeviceInventoryProvider(

0 commit comments

Comments
 (0)