Skip to content

Commit f4c646b

Browse files
committed
fix: preserve scoped Apple inventory tooling
1 parent ae15898 commit f4c646b

24 files changed

Lines changed: 376 additions & 81 deletions

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export type {
4141
export type { PlatformGatedProviderResolverKey } from '../platform-providers.ts';
4242
export type { RunnerLogicalLeaseContext } from '../runner-lease-context.ts';
4343
export type {
44+
AppleToolHost,
45+
AppleToolRequest,
46+
AppleXcrunTool,
4447
HostCommandRequest,
4548
HostCommandResult,
4649
HostCommandRunner,

‎packages/contracts/src/platform-runtime-host.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,27 @@ export type HostCommandResult = Readonly<{
1717
signal?: string;
1818
}>;
1919

20-
/** The sole process-execution port available to platform packages. */
20+
/** Generic process-execution port; focused Apple foreground tools use AppleToolHost. */
2121
export type HostCommandRunner = Readonly<{
2222
which(executable: string): Promise<string | undefined>;
2323
run(request: HostCommandRequest, signal?: AbortSignal): Promise<HostCommandResult>;
2424
}>;
2525

26+
export type AppleXcrunTool = 'simctl' | 'devicectl' | 'xctrace';
27+
28+
export type AppleToolRequest = Readonly<{
29+
tool: AppleXcrunTool;
30+
args: readonly string[];
31+
timeoutMs?: number;
32+
allowFailure?: boolean;
33+
}>;
34+
35+
/** Request-bound foreground Apple tooling backed by the selected scoped provider. */
36+
export type AppleToolHost = Readonly<{
37+
isXcrunAvailable(signal?: AbortSignal): Promise<boolean>;
38+
run(request: AppleToolRequest, signal?: AbortSignal): Promise<HostCommandResult>;
39+
}>;
40+
2641
export type HostOperatingSystem = 'darwin' | 'linux' | 'win32' | 'other';
2742

2843
export type HostTemporaryTextFile = AsyncDisposable &
@@ -61,6 +76,7 @@ export type HostToolchainPreparer = Readonly<{
6176
*/
6277
export type DeviceInventoryHost = Readonly<{
6378
commands: HostCommandRunner;
79+
appleTools: AppleToolHost;
6480
toolchains: HostToolchainPreparer;
6581
files: DeviceInventoryFileHost;
6682
hostOs: HostOperatingSystem;

‎packages/platform-android/src/inventory.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ function createHost(
120120
which: async (executable) => tools[executable as keyof typeof tools],
121121
run,
122122
},
123+
appleTools: {
124+
isXcrunAvailable: async () => false,
125+
run: async () => {
126+
throw new Error('unused');
127+
},
128+
},
123129
toolchains: { prepare: async () => undefined },
124130
files: {
125131
isExecutable,

‎packages/platform-apple/src/inventory.fixtures.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
AppleToolHost,
23
DeviceInventoryHost,
34
HostCommandRequest,
45
HostCommandResult,
@@ -21,7 +22,7 @@ export function commandResult(
2122

2223
export function createInventoryHost(
2324
options: {
24-
run?: (request: HostCommandRequest, signal?: AbortSignal) => Promise<HostCommandResult>;
25+
run?: AppleToolHost['run'];
2526
which?: (executable: string) => Promise<string | undefined>;
2627
createTemporaryTextFile?: DeviceInventoryHost['files']['createTemporaryTextFile'];
2728
hostOs?: DeviceInventoryHost['hostOs'];
@@ -31,10 +32,17 @@ export function createInventoryHost(
3132
return {
3233
commands: {
3334
which: options.which ?? (async (executable) => `/usr/bin/${executable}`),
35+
run: async (request: HostCommandRequest) => {
36+
throw new Error(`Unexpected command: ${request.executable} ${request.args.join(' ')}`);
37+
},
38+
},
39+
appleTools: {
40+
isXcrunAvailable: async () =>
41+
Boolean(await (options.which ?? (async (executable) => `/usr/bin/${executable}`))('xcrun')),
3442
run:
3543
options.run ??
3644
(async (request) => {
37-
throw new Error(`Unexpected command: ${request.executable} ${request.args.join(' ')}`);
45+
throw new Error(`Unexpected Apple tool: ${request.tool} ${request.args.join(' ')}`);
3846
}),
3947
},
4048
toolchains: { prepare: async () => undefined },

‎packages/platform-apple/src/inventory.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test('simulator-only inventory avoids physical discovery and observes fresh boot
5757
const host = createInventoryHost({
5858
observed,
5959
run: async (request) => {
60-
calls.push([...request.args]);
60+
calls.push([request.tool, ...request.args]);
6161
return commandResult(
6262
JSON.stringify({
6363
devices: {
@@ -92,7 +92,7 @@ test('simulator-set inventory remains scoped while retaining the host Mac', asyn
9292
const calls: string[][] = [];
9393
const host = createInventoryHost({
9494
run: async (request) => {
95-
calls.push([...request.args]);
95+
calls.push([request.tool, ...request.args]);
9696
return commandResult(
9797
JSON.stringify({
9898
devices: {

‎packages/platform-apple/src/inventory.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function discoverAppleDevices(
3333
if (host.hostOs !== 'darwin') {
3434
throw new AppError('UNSUPPORTED_PLATFORM', 'Apple tools are only available on macOS');
3535
}
36-
if (!(await host.commands.which('xcrun'))) {
36+
if (!(await host.appleTools.isXcrunAvailable(scope.signal))) {
3737
throw new AppError('TOOL_MISSING', 'xcrun not found in PATH');
3838
}
3939

‎packages/platform-apple/src/physical-inventory.test.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ test('physical inventory prefers CoreDevice, supplements xctrace, and disposes i
3232
let disposeCount = 0;
3333
const host = createInventoryHost({
3434
run: async (request) => {
35-
calls.push([...request.args]);
36-
if (request.args[0] === 'devicectl') return commandResult();
35+
calls.push([request.tool, ...request.args]);
36+
if (request.tool === 'devicectl') return commandResult();
3737
return commandResult(['== Devices ==', 'Phone [core-1]', 'Apple TV [tv-1]'].join('\n'));
3838
},
3939
createTemporaryTextFile: async () => ({
@@ -76,7 +76,7 @@ test('physical inventory prefers CoreDevice, supplements xctrace, and disposes i
7676
test('physical inventory fails soft when either native source is unavailable', async () => {
7777
const host = createInventoryHost({
7878
run: async (request) => {
79-
if (request.args[0] === 'devicectl') throw new Error('CoreDevice unavailable');
79+
if (request.tool === 'devicectl') throw new Error('CoreDevice unavailable');
8080
return commandResult('== Devices ==\nMy iPhone [phone-1]');
8181
},
8282
createTemporaryTextFile: async () => {
@@ -89,3 +89,22 @@ test('physical inventory fails soft when either native source is unavailable', a
8989
['phone-1'],
9090
);
9191
});
92+
93+
test('physical inventory preserves request cancellation instead of failing soft', async () => {
94+
const controller = new AbortController();
95+
const reason = new Error('inventory cancelled');
96+
const requestScope = { ...inventoryScope, signal: controller.signal };
97+
const host = createInventoryHost({
98+
run: async () => {
99+
controller.abort(reason);
100+
throw reason;
101+
},
102+
createTemporaryTextFile: async () => ({
103+
path: '/tmp/devices.json',
104+
readText: async () => '{}',
105+
[Symbol.asyncDispose]: async () => undefined,
106+
}),
107+
});
108+
109+
await assert.rejects(listPhysicalAppleDevices(host, requestScope), reason);
110+
});

‎packages/platform-apple/src/physical-inventory.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ async function listPhysicalDevicesFromDevicectl(
7676
prefix: 'agent-device-devicectl-',
7777
suffix: '.json',
7878
});
79-
const result = await host.commands.run(
79+
const result = await host.appleTools.run(
8080
{
81-
executable: 'xcrun',
82-
args: ['devicectl', 'list', 'devices', '--json-output', temporaryFile.path],
81+
tool: 'devicectl',
82+
args: ['list', 'devices', '--json-output', temporaryFile.path],
8383
allowFailure: true,
8484
timeoutMs: IOS_DEVICECTL_LIST_TIMEOUT_MS,
8585
},
@@ -92,6 +92,7 @@ async function listPhysicalDevicesFromDevicectl(
9292
return mapped ? [mapped] : [];
9393
});
9494
} catch {
95+
scope.signal.throwIfAborted();
9596
return [];
9697
} finally {
9798
try {
@@ -107,12 +108,13 @@ async function listPhysicalDevicesFromXctrace(
107108
scope: PlatformRequestScope,
108109
): Promise<DeviceInfo[]> {
109110
try {
110-
const result = await host.commands.run(
111-
{ executable: 'xcrun', args: ['xctrace', 'list', 'devices'], allowFailure: true },
111+
const result = await host.appleTools.run(
112+
{ tool: 'xctrace', args: ['list', 'devices'], allowFailure: true },
112113
scope.signal,
113114
);
114115
return result.exitCode === 0 ? parseXctracePhysicalAppleDevices(result.stdout) : [];
115116
} catch {
117+
scope.signal.throwIfAborted();
116118
return [];
117119
}
118120
}

‎packages/platform-apple/src/simulator-inventory.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ test('simctl parser keeps available supported runtimes and their target semantic
6969
});
7070

7171
test('simulator inventory scopes bounded simctl and reports fresh booted observations', async () => {
72-
const calls: Array<{ args: readonly string[]; timeoutMs?: number }> = [];
72+
const calls: Array<{ tool: string; args: readonly string[]; timeoutMs?: number }> = [];
7373
const observed: string[] = [];
7474
const host = createInventoryHost({
7575
observed,
7676
run: async (request) => {
77-
calls.push({ args: request.args, timeoutMs: request.timeoutMs });
77+
calls.push({ tool: request.tool, args: request.args, timeoutMs: request.timeoutMs });
7878
return commandResult(JSON.stringify(simulatorPayload));
7979
},
8080
});
@@ -87,7 +87,8 @@ test('simulator inventory scopes bounded simctl and reports fresh booted observa
8787

8888
assert.deepEqual(calls, [
8989
{
90-
args: ['simctl', '--set', '/tmp/custom-set', 'list', 'devices', '-j'],
90+
tool: 'simctl',
91+
args: ['--set', '/tmp/custom-set', 'list', 'devices', '-j'],
9192
timeoutMs: 3_000,
9293
},
9394
]);
@@ -96,7 +97,7 @@ test('simulator inventory scopes bounded simctl and reports fresh booted observa
9697
['iphone-1'],
9798
);
9899
assert.deepEqual(observed, ['iphone-1']);
99-
assert.deepEqual(buildSimctlListArgs(undefined), ['simctl', 'list', 'devices', '-j']);
100+
assert.deepEqual(buildSimctlListArgs(undefined), ['list', 'devices', '-j']);
100101
});
101102

102103
test('simulator inventory classifies malformed native output as a command failure', async () => {

‎packages/platform-apple/src/simulator-inventory.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ const BOOTED_SIMULATOR_PROBE_TIMEOUT_MS = 3_000;
2727

2828
export function buildSimctlListArgs(simulatorSetPath: string | undefined): string[] {
2929
const path = simulatorSetPath?.trim();
30-
return path
31-
? ['simctl', '--set', path, 'list', 'devices', '-j']
32-
: ['simctl', 'list', 'devices', '-j'];
30+
return path ? ['--set', path, 'list', 'devices', '-j'] : ['list', 'devices', '-j'];
3331
}
3432

3533
export function parseSimctlAppleDevices(
@@ -63,9 +61,9 @@ export async function listAppleSimulators(
6361
scope: PlatformRequestScope,
6462
): Promise<DeviceInfo[]> {
6563
const simulatorSetPath = request.iosSimulatorSetPath?.trim() || undefined;
66-
const result = await host.commands.run(
64+
const result = await host.appleTools.run(
6765
{
68-
executable: 'xcrun',
66+
tool: 'simctl',
6967
args: buildSimctlListArgs(simulatorSetPath),
7068
...(request.booted === true ? { timeoutMs: BOOTED_SIMULATOR_PROBE_TIMEOUT_MS } : {}),
7169
},

0 commit comments

Comments
 (0)