Skip to content

Commit 71432c5

Browse files
fix(apple): pick the Mac runner and simulator build arch from the host CPU (callstack#3047)
1 parent 19dc639 commit 71432c5

16 files changed

Lines changed: 278 additions & 41 deletions

File tree

‎packages/host-kit/src/internal/host-cpu-arch.test.ts‎

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import assert from 'node:assert/strict';
2-
import { test } from 'vitest';
2+
import { beforeEach, test, vi } from 'vitest';
33
import { type CommandExecutorOverride, withCommandExecutorOverride } from './exec.ts';
4-
import { resolveHostCpuArch } from './host-cpu-arch.ts';
4+
import { readHostCpuArch, readHostCpuArchSync, resolveHostCpuArch } from './host-cpu-arch.ts';
5+
6+
const { mockRunCmdSync } = vi.hoisted(() => ({ mockRunCmdSync: vi.fn() }));
7+
8+
vi.mock('./exec.ts', async (importOriginal) => ({
9+
...(await importOriginal<typeof import('./exec.ts')>()),
10+
runCmdSync: mockRunCmdSync,
11+
}));
12+
13+
beforeEach(() => {
14+
mockRunCmdSync.mockReset();
15+
});
516

617
function sysctlAnswering(result: { stdout?: string; exitCode?: number } | Error) {
718
const calls: string[][] = [];
@@ -45,3 +56,50 @@ test('other platforms report the Node arch without running sysctl', async () =>
4556
});
4657
assert.deepEqual(sysctl.calls, []);
4758
});
59+
60+
async function asDarwinX64Process(run: () => Promise<void>): Promise<void> {
61+
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
62+
const arch = Object.getOwnPropertyDescriptor(process, 'arch')!;
63+
Object.defineProperty(process, 'platform', { ...platform, value: 'darwin' });
64+
Object.defineProperty(process, 'arch', { ...arch, value: 'x64' });
65+
try {
66+
await run();
67+
} finally {
68+
Object.defineProperty(process, 'platform', platform);
69+
Object.defineProperty(process, 'arch', arch);
70+
}
71+
}
72+
73+
test('the per-process value a Rosetta-translated process resolves is the one sync callers read', async () => {
74+
await asDarwinX64Process(async () => {
75+
const sysctl = sysctlAnswering({ stdout: '1\n' });
76+
const resolved = await withCommandExecutorOverride(sysctl.override, () => readHostCpuArch());
77+
assert.equal(resolved, 'arm64');
78+
assert.equal(readHostCpuArchSync(), 'arm64');
79+
assert.equal(sysctl.calls.length, 1);
80+
assert.equal(mockRunCmdSync.mock.calls.length, 0);
81+
});
82+
});
83+
84+
test('a Rosetta-translated process resolves arm64 through the sync path first, and async callers share it', async () => {
85+
await asDarwinX64Process(async () => {
86+
mockRunCmdSync.mockReturnValue({ stdout: '1\n', stderr: '', exitCode: 0 });
87+
assert.equal(readHostCpuArchSync(), 'arm64');
88+
assert.deepEqual(
89+
mockRunCmdSync.mock.calls.map(([command, args]) => [command, ...args]),
90+
[['/usr/sbin/sysctl', '-n', 'hw.optional.arm64']],
91+
);
92+
93+
const sysctl = sysctlAnswering({ stdout: '0\n' });
94+
const resolved = await withCommandExecutorOverride(sysctl.override, () => readHostCpuArch());
95+
assert.equal(resolved, 'arm64');
96+
assert.deepEqual(sysctl.calls, []);
97+
});
98+
});
99+
100+
test('a sync read on an Intel Mac reports x86_64', async () => {
101+
await asDarwinX64Process(async () => {
102+
mockRunCmdSync.mockReturnValue({ stdout: '', stderr: '', exitCode: 1 });
103+
assert.equal(readHostCpuArchSync(), 'x86_64');
104+
});
105+
});

‎packages/host-kit/src/internal/host-cpu-arch.ts‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,77 @@
1-
import { runCmd } from './exec.ts';
1+
import { createTtlMemo } from '@agent-device/kernel/ttl-memo';
2+
import { runCmd, runCmdSync } from './exec.ts';
23

34
const SYSCTL_TIMEOUT_MS = 1_000;
5+
const APPLE_SILICON_SYSCTL = ['-n', 'hw.optional.arm64'] as const;
46

5-
let hostCpuArch: Promise<string> | undefined;
7+
const pendingHostCpuArch = createTtlMemo<'self', Promise<string>>();
8+
const settledHostCpuArch = createTtlMemo<'self', string>();
69

710
/**
811
* The machine's native CPU architecture in Apple naming (`arm64`, `x86_64`), which is what
912
* simulators on a Mac run by default. Other CPUs keep Node's `process.arch` name. Resolved once
1013
* per process.
1114
*/
1215
export function readHostCpuArch(): Promise<string> {
13-
hostCpuArch ??= resolveHostCpuArch(process.platform, process.arch);
14-
return hostCpuArch;
16+
const settled = settledHostCpuArch.get('self');
17+
if (settled !== undefined) return Promise.resolve(settled);
18+
let pending = pendingHostCpuArch.get('self');
19+
if (pending === undefined) {
20+
pending = resolveHostCpuArch(process.platform, process.arch).then(settleHostCpuArch);
21+
pendingHostCpuArch.set('self', pending);
22+
}
23+
return pending;
24+
}
25+
26+
/**
27+
* {@link readHostCpuArch} for a caller that cannot await. It shares the same per-process value,
28+
* resolving it with a blocking `sysctl` only when nothing has resolved it yet.
29+
*/
30+
export function readHostCpuArchSync(): string {
31+
return (
32+
settledHostCpuArch.get('self') ??
33+
settleHostCpuArch(
34+
hostCpuArchName(process.platform === 'darwin' && isAppleSiliconMacSync(), process.arch),
35+
)
36+
);
37+
}
38+
39+
function settleHostCpuArch(arch: string): string {
40+
const settled = settledHostCpuArch.get('self');
41+
if (settled !== undefined) return settled;
42+
settledHostCpuArch.set('self', arch);
43+
return arch;
1544
}
1645

1746
export async function resolveHostCpuArch(
1847
platform: NodeJS.Platform,
1948
nodeArch: string,
2049
): Promise<string> {
21-
if (platform === 'darwin' && (await isAppleSiliconMac())) return 'arm64';
50+
return hostCpuArchName(platform === 'darwin' && (await isAppleSiliconMac()), nodeArch);
51+
}
52+
53+
function hostCpuArchName(appleSilicon: boolean, nodeArch: string): string {
54+
if (appleSilicon) return 'arm64';
2255
return nodeArch === 'x64' ? 'x86_64' : nodeArch;
2356
}
2457

2558
// macOS: `hw.optional.arm64` is 1 on Apple silicon even inside a Rosetta-translated process,
2659
// where `process.arch` reports x64; Intel Macs do not define the key.
2760
async function isAppleSiliconMac(): Promise<boolean> {
2861
try {
29-
const result = await runCmd('/usr/sbin/sysctl', ['-n', 'hw.optional.arm64'], {
62+
const result = await runCmd('/usr/sbin/sysctl', APPLE_SILICON_SYSCTL, {
63+
allowFailure: true,
64+
timeoutMs: SYSCTL_TIMEOUT_MS,
65+
});
66+
return result.exitCode === 0 && result.stdout.trim() === '1';
67+
} catch {
68+
return false;
69+
}
70+
}
71+
72+
function isAppleSiliconMacSync(): boolean {
73+
try {
74+
const result = runCmdSync('/usr/sbin/sysctl', APPLE_SILICON_SYSCTL, {
3075
allowFailure: true,
3176
timeoutMs: SYSCTL_TIMEOUT_MS,
3277
});

‎packages/host-kit/src/process.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export {
2525
writeHostStderr,
2626
} from './internal/host-process.ts';
2727
export { signalProcessGroupBestEffort } from './internal/exec.ts';
28-
export { readHostCpuArch } from './internal/host-cpu-arch.ts';
28+
export { readHostCpuArch, readHostCpuArchSync } from './internal/host-cpu-arch.ts';
2929
export { reapOwnedProcessRecordsAtStartup } from './internal/owned-process-reaper.ts';
3030
export {
3131
createOwnedProcessRecordStore,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import { withCommandExecutorOverride } from '@agent-device/host-kit/command';
4+
import { readHostCpuArch } from '@agent-device/host-kit/process';
5+
import '../runner-client.ts';
6+
import {
7+
resolveRunnerBuildDestination,
8+
resolveRunnerBuildDestinationFamily,
9+
resolveRunnerDestination,
10+
} from '../../runner/apple-runner-platform.ts';
11+
import { MACOS_DEVICE } from '../../runner/__tests__/device-fixtures.ts';
12+
13+
test('the macOS runner destinations name arm64 when Node runs as x64 under Rosetta', async () => {
14+
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
15+
const arch = Object.getOwnPropertyDescriptor(process, 'arch')!;
16+
Object.defineProperty(process, 'platform', { ...platform, value: 'darwin' });
17+
Object.defineProperty(process, 'arch', { ...arch, value: 'x64' });
18+
try {
19+
await withCommandExecutorOverride(
20+
async (command, args) => {
21+
assert.deepEqual([command, ...args], ['/usr/sbin/sysctl', '-n', 'hw.optional.arm64']);
22+
return { stdout: '1\n', stderr: '', exitCode: 0 };
23+
},
24+
() => readHostCpuArch(),
25+
);
26+
for (const destination of [
27+
resolveRunnerDestination(MACOS_DEVICE),
28+
resolveRunnerBuildDestination(MACOS_DEVICE),
29+
resolveRunnerBuildDestinationFamily(MACOS_DEVICE),
30+
]) {
31+
assert.equal(destination, 'platform=macOS,arch=arm64');
32+
}
33+
} finally {
34+
Object.defineProperty(process, 'platform', platform);
35+
Object.defineProperty(process, 'arch', arch);
36+
}
37+
});

‎packages/platform-apple/src/core/runner-host.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
signalPidsBestEffort,
1919
signalProcessGroupBestEffort,
2020
classifyOwnerLiveness,
21+
readHostCpuArchSync,
2122
} from '@agent-device/host-kit/process';
2223
import { Deadline, isEnvTruthy, retryWithPolicy } from '@agent-device/host-kit/retry';
2324

@@ -82,6 +83,7 @@ export const appleRunnerHost: AppleRunnerHost = {
8283
withKeyedLock,
8384
publishFileSync,
8485
classifyOwnerLiveness,
86+
readHostCpuArchSync,
8587
createTtlMemo,
8688
parseBooleanLiteral,
8789
isRecord,

‎packages/platform-apple/src/foldable/fold-helper-cache.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function fakeFoldHelperHost(
2121
const real = createSnapshotSourceHost();
2222
return {
2323
...real,
24+
cpuArch: async () => 'arm64',
2425
run: async (command, args) => {
2526
if (command === 'xcrun' && args.includes('clang')) {
2627
const outputPath = args.at(-1)!;
@@ -34,9 +35,7 @@ function fakeFoldHelperHost(
3435
? args.includes('-buildVersion')
3536
? '24G90'
3637
: '15.6'
37-
: command === 'uname'
38-
? 'arm64'
39-
: '';
38+
: '';
4039
return { stdout, stderr: '', exitCode: 0 };
4140
},
4241
};

‎packages/platform-apple/src/native-build/host.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
renameHostPath,
1111
writeHostTextFile,
1212
} from '@agent-device/host-kit/host-file';
13-
import { hostProcessId, readProcessStartTime } from '@agent-device/host-kit/process';
13+
import {
14+
hostProcessId,
15+
readHostCpuArch,
16+
readProcessStartTime,
17+
} from '@agent-device/host-kit/process';
1418
import { NativeBuildError, nativeBuildError } from './errors.ts';
1519
import { remainingNativeBuildMs, type NativeBuildDeadline } from './deadline.ts';
1620

@@ -35,6 +39,7 @@ export type NativeBuildHost = Readonly<{
3539
options: { deadline: NativeBuildDeadline; description: string },
3640
): Promise<() => Promise<void>>;
3741
processId(): number;
42+
cpuArch(): Promise<string>;
3843
}>;
3944

4045
export function createNativeBuildHost(run: NativeBuildHost['run']): NativeBuildHost {
@@ -50,6 +55,7 @@ export function createNativeBuildHost(run: NativeBuildHost['run']): NativeBuildH
5055
remove: removeHostPath,
5156
acquireLock: acquireNativeBuildLock,
5257
processId: hostProcessId,
58+
cpuArch: readHostCpuArch,
5359
};
5460
}
5561

‎packages/platform-apple/src/native-build/toolchain-identity.test.ts‎

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
22
import { test } from 'vitest';
33
import {
44
isCommandTimeoutError,
5+
withCommandExecutorOverride,
56
type ExecOptions,
67
type ExecResult,
78
} from '@agent-device/host-kit/command';
@@ -19,6 +20,65 @@ import { execKillTimeoutError } from './__tests__/exec-timeout-fixture.ts';
1920
// actually blocks for the timeout it was handed, so a case that claims the budget was spent had to
2021
// spend it.
2122

23+
test('a Rosetta-translated process builds for the arm64 simulator the Mac runs', async () => {
24+
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
25+
const arch = Object.getOwnPropertyDescriptor(process, 'arch')!;
26+
Object.defineProperty(process, 'platform', { ...platform, value: 'darwin' });
27+
Object.defineProperty(process, 'arch', { ...arch, value: 'x64' });
28+
try {
29+
const host = createNativeBuildHost(async (command, args) => toolchainAnswer(command, args));
30+
const identity = await withCommandExecutorOverride(
31+
async (command, args) => {
32+
assert.deepEqual([command, ...args], ['/usr/sbin/sysctl', '-n', 'hw.optional.arm64']);
33+
return { stdout: '1\n', stderr: '', exitCode: 0 };
34+
},
35+
() => readHostToolchainIdentity(host, fakeClockDeadline(40_000, { nowMs: 0 })),
36+
);
37+
assert.equal(identity.architecture, 'arm64');
38+
} finally {
39+
Object.defineProperty(process, 'platform', platform);
40+
Object.defineProperty(process, 'arch', arch);
41+
}
42+
});
43+
44+
test('a request cancelled while the host arch is still resolving stops waiting for it', async () => {
45+
const request = new AbortController();
46+
const host: NativeBuildHost = {
47+
...createNativeBuildHost(async (command, args) => toolchainAnswer(command, args)),
48+
cpuArch: () => {
49+
request.abort();
50+
return new Promise<string>(() => {});
51+
},
52+
};
53+
54+
await assert.rejects(
55+
readHostToolchainIdentity(host, createNativeBuildDeadline(40_000, request.signal)),
56+
(error: unknown) =>
57+
error instanceof NativeBuildError &&
58+
error.buildFailureKind === 'cancelled' &&
59+
error.buildFailureCode === 'abort-signal',
60+
);
61+
});
62+
63+
test('a host arch that outlives the request budget reports the deadline', async () => {
64+
const clock = { nowMs: 0 };
65+
const host: NativeBuildHost = {
66+
...createNativeBuildHost(async (command, args) => {
67+
if (command === 'sw_vers' && args.includes('-buildVersion')) clock.nowMs = 39_990;
68+
return toolchainAnswer(command, args);
69+
}),
70+
cpuArch: () => new Promise<string>(() => {}),
71+
};
72+
73+
await assert.rejects(
74+
readHostToolchainIdentity(host, fakeClockDeadline(40_000, clock)),
75+
(error: unknown) =>
76+
error instanceof NativeBuildError &&
77+
error.buildFailureKind === 'timeout' &&
78+
error.buildFailureCode === 'host-arch-deadline',
79+
);
80+
});
81+
2282
test('a cold-start toolchain probe recovers on retry, and the retry gets only what the stall left', async () => {
2383
const clock = { nowMs: 0 };
2484
const timeouts: number[] = [];
@@ -39,9 +99,9 @@ test('a cold-start toolchain probe recovers on retry, and the retry gets only wh
3999
// retry runs on the 10 s the shared deadline has left, not a second 30 s.
40100
assert.deepEqual(timeouts.slice(0, 2), [30_000, 10_000]);
41101
assert.equal(clock.nowMs, 30_000);
42-
// 4 baseline probes (xcodebuild, sw_vers x2, uname) plus the one
102+
// 3 baseline probes (xcodebuild, sw_vers x2) plus the one
43103
// retry that recovered the first, timed-out call.
44-
assert.equal(calls, 5);
104+
assert.equal(calls, 4);
45105
});
46106

47107
// The identity read is allowed to exec one Xcode-owned binary. The Simulator SDK a second
@@ -58,7 +118,7 @@ test('the toolchain identity execs one Xcode-owned binary, and no xcrun', async
58118

59119
await readHostToolchainIdentity(host, fakeClockDeadline(120_000, clock));
60120

61-
assert.deepEqual(probed, ['xcodebuild', 'sw_vers', 'sw_vers', 'uname']);
121+
assert.deepEqual(probed, ['xcodebuild', 'sw_vers', 'sw_vers']);
62122
});
63123

64124
test('a toolchain host that never returns reports the stalled probe after one retry', async () => {
@@ -301,12 +361,14 @@ function toolchainAnswer(command: string, args: string[]): ExecResult {
301361
if (command === 'sw_vers') {
302362
return { stdout: args.includes('-buildVersion') ? '24G90' : '15.6', stderr: '', exitCode: 0 };
303363
}
304-
if (command === 'uname') return { stdout: 'arm64', stderr: '', exitCode: 0 };
305364
throw new Error(`the identity read execed ${command} ${args.join(' ')}`);
306365
}
307366

308367
function fakeToolchainHost(
309368
run: (command: string, args: string[], options: ExecOptions) => ExecResult | Promise<ExecResult>,
310369
): NativeBuildHost {
311-
return createNativeBuildHost(async (command, args, options) => run(command, args, options ?? {}));
370+
return {
371+
...createNativeBuildHost(async (command, args, options) => run(command, args, options ?? {})),
372+
cpuArch: async () => 'arm64',
373+
};
312374
}

0 commit comments

Comments
 (0)