Skip to content

Commit 9573ece

Browse files
committed
fix(ios): report a phase that runs out during the xcrun shim probe as the phase budget, not an armed shim
Only the cold-toolchain budget now reads a stopped shim as probe_out_of_budget. A stop by the owning phase's clock, or a phase already spent when the probe starts, fails the redirect with runner_phase_budget_exhausted at the one gate both callers share. The session-side redirect spends the startup time read before the build, like the launch after it.
1 parent a8e807d commit 9573ece

11 files changed

Lines changed: 180 additions & 75 deletions

‎packages/platform-apple/src/core/__tests__/xcrun-shim-first-launch.test.ts‎

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function tempRoot(): Promise<string> {
2929

3030
async function probeShims(options?: XcrunShimProbeOptions): Promise<XctestDeviceSetCleanupArming> {
3131
const probe = await probeXcrunShimFirstLaunchHooks(options);
32-
if (probe.canceled) assert.fail('no request canceled this probe');
32+
if (probe.outcome !== 'read') assert.fail(`the probe read no shim: ${probe.outcome}`);
3333
return probe.xcrunShims;
3434
}
3535

@@ -177,30 +177,59 @@ test('the probe spends the cold-toolchain budget on each xcrun --find and reads
177177
);
178178
});
179179

180-
for (const [phaseRemainingMs, budgetMs] of [
181-
[5_000, 5_000],
182-
[120_000, COLD_TOOLCHAIN_PROBE_TIMEOUT_MS],
183-
] as const) {
184-
test(`a phase with ${phaseRemainingMs} ms left gives the probe a ${budgetMs} ms budget`, async () => {
185-
const budget = new AbortController();
186-
const timeout = vi.spyOn(AbortSignal, 'timeout').mockReturnValue(budget.signal);
187-
let started = 0;
188-
const provider = hangingXcrun(() => {
189-
started += 1;
190-
if (started === XCRUN_SHIM_TOOL_NAMES.length) budget.abort();
191-
});
192-
193-
const shims = await withAppleToolProvider(provider, () =>
194-
probeShims({ deadline: phaseClock(phaseRemainingMs) }),
195-
);
180+
test('a phase with less left than the cold budget caps the probe and owns its stop', async () => {
181+
const budget = new AbortController();
182+
const timeout = vi.spyOn(AbortSignal, 'timeout').mockReturnValue(budget.signal);
183+
let started = 0;
184+
const provider = hangingXcrun(() => {
185+
started += 1;
186+
if (started === XCRUN_SHIM_TOOL_NAMES.length) budget.abort();
187+
});
196188

197-
assert.deepEqual(timeout.mock.calls, [[budgetMs]]);
198-
assert.deepEqual(
199-
shims.map((shim) => shim.hook === 'armed' && shim.armedBy),
200-
XCRUN_SHIM_TOOL_NAMES.map(() => 'probe_out_of_budget'),
201-
);
189+
const probe = await withAppleToolProvider(provider, () =>
190+
probeXcrunShimFirstLaunchHooks({ deadline: phaseClock(5_000) }),
191+
);
192+
193+
assert.deepEqual(timeout.mock.calls, [[5_000]]);
194+
assert.equal(started, XCRUN_SHIM_TOOL_NAMES.length);
195+
assert.deepEqual(probe, { outcome: 'phase_budget_exhausted' });
196+
});
197+
198+
test('a phase with more left than the cold budget reads a cold-budget stop as armed', async () => {
199+
const budget = new AbortController();
200+
const timeout = vi.spyOn(AbortSignal, 'timeout').mockReturnValue(budget.signal);
201+
let started = 0;
202+
const provider = hangingXcrun(() => {
203+
started += 1;
204+
if (started === XCRUN_SHIM_TOOL_NAMES.length) budget.abort();
202205
});
203-
}
206+
207+
const shims = await withAppleToolProvider(provider, () =>
208+
probeShims({ deadline: phaseClock(120_000) }),
209+
);
210+
211+
assert.deepEqual(timeout.mock.calls, [[COLD_TOOLCHAIN_PROBE_TIMEOUT_MS]]);
212+
assert.deepEqual(
213+
shims.map((shim) => shim.hook === 'armed' && shim.armedBy),
214+
XCRUN_SHIM_TOOL_NAMES.map(() => 'probe_out_of_budget'),
215+
);
216+
});
217+
218+
test('a spent phase spawns no xcrun and reads as the phase running out', async () => {
219+
const host = writeFakeXcrunShims(await tempRoot(), {
220+
simctl: { expectedVersion: '1155.4', installedVersion: '1155.4' },
221+
devicectl: { expectedVersion: '629.3', installedVersion: '629.3' },
222+
});
223+
const timeout = vi.spyOn(AbortSignal, 'timeout');
224+
225+
const probe = await withFakeXcrunHost(host, () =>
226+
probeXcrunShimFirstLaunchHooks({ deadline: phaseClock(0) }),
227+
);
228+
229+
assert.deepEqual(probe, { outcome: 'phase_budget_exhausted' });
230+
assert.deepEqual(host.finds, []);
231+
assert.equal(timeout.mock.calls.length, 0);
232+
});
204233

205234
test('a request canceled mid-probe reads as canceled, never as an armed shim', async () => {
206235
const request = new AbortController();
@@ -216,7 +245,7 @@ test('a request canceled mid-probe reads as canceled, never as an armed shim', a
216245
);
217246

218247
assert.equal(started, XCRUN_SHIM_TOOL_NAMES.length);
219-
assert.deepEqual(probe, { canceled: true });
248+
assert.deepEqual(probe, { outcome: 'request_canceled' });
220249
});
221250

222251
test('an already-canceled request spawns no xcrun at all', async () => {
@@ -230,5 +259,5 @@ test('an already-canceled request spawns no xcrun at all', async () => {
230259
);
231260

232261
assert.deepEqual(host.finds, []);
233-
assert.deepEqual(probe, { canceled: true });
262+
assert.deepEqual(probe, { outcome: 'request_canceled' });
234263
});

‎packages/platform-apple/src/core/__tests__/xcrun-shim-fixtures.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export type FakeXcrunHost = {
6060
plistReads: string[];
6161
/** Runs as each Info.plist read starts, before it is answered. */
6262
onPlistRead?: () => void;
63+
/** When set, `xcrun --find` answers only once its signal aborts, as a cold toolchain stalls. */
64+
findStalls?: boolean;
6365
};
6466

6567
const HOOKLESS_SHIM_TEXT = '#!/bin/bash\nexec "${DEVELOPER_DIR}/usr/bin/tool" "${@}"\n';
@@ -114,9 +116,10 @@ export async function withFakeXcrunHost<T>(
114116
task: () => Promise<T>,
115117
): Promise<T> {
116118
const provider = createLocalAppleToolProvider({
117-
runCommand: async (cmd, args): Promise<ExecResult> => {
119+
runCommand: async (cmd, args, options): Promise<ExecResult> => {
118120
const tool = cmd === 'xcrun' && args[0] === '--find' ? args[1] : undefined;
119121
if (tool !== undefined) host.finds.push(tool);
122+
if (tool !== undefined && host.findStalls) await abortOf(options?.signal);
120123
const found = tool === undefined ? undefined : host.xcrunShimPaths[tool as XcrunShimToolName];
121124
return found
122125
? { exitCode: 0, stdout: `${found}\n`, stderr: '' }
@@ -133,3 +136,10 @@ export async function withFakeXcrunHost<T>(
133136
});
134137
return await withAppleToolProvider(provider, task);
135138
}
139+
140+
async function abortOf(signal: AbortSignal | undefined): Promise<void> {
141+
if (!signal || signal.aborted) return;
142+
await new Promise<void>((resolve) => {
143+
signal.addEventListener('abort', () => resolve(), { once: true });
144+
});
145+
}

‎packages/platform-apple/src/core/xcrun-shim-first-launch.ts‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export type XcrunShimFirstLaunchHook =
3939
}
4040
| ArmedXcrunShimFirstLaunchHook;
4141

42-
/** Why a shim reads as armed; every value but `version_mismatch` is a probe that could not decide. */
42+
/**
43+
* Why a shim reads as armed; every value but `version_mismatch` is a probe that could not decide.
44+
* `probe_out_of_budget` is a stop by the cold-toolchain budget, never by the owning phase's clock.
45+
*/
4346
export type XcrunShimArmedBy =
4447
| 'version_mismatch'
4548
| 'version_unreadable'
@@ -64,10 +67,14 @@ export type ArmedXcrunShimFirstLaunchHook = XcrunShimEvidence & {
6467
/** One entry per {@link XCRUN_SHIM_TOOL_NAMES} tool. */
6568
export type XctestDeviceSetCleanupArming = readonly XcrunShimFirstLaunchHook[];
6669

67-
/** A probe its request canceled reads no shim: a cancellation is the request's, never an arming. */
70+
/**
71+
* What one probe came back with. A probe its request canceled, or one the owning phase's clock
72+
* stopped, reads no shim: the stop belongs to the request or the phase, never to an arming.
73+
*/
6874
export type XcrunShimProbe =
69-
| { canceled: true }
70-
| { canceled: false; xcrunShims: XctestDeviceSetCleanupArming };
75+
| { outcome: 'request_canceled' }
76+
| { outcome: 'phase_budget_exhausted' }
77+
| { outcome: 'read'; xcrunShims: XctestDeviceSetCleanupArming };
7178

7279
export type XcrunShimProbeOptions = {
7380
/** The owning request's cancellation. */
@@ -78,20 +85,32 @@ export type XcrunShimProbeOptions = {
7885

7986
/**
8087
* Reads every shim's first-launch hook within one shared budget: the cold-toolchain budget, or what
81-
* the owning phase has left when that is less. A shim the budget stops reads as armed.
88+
* the owning phase has left when that is no more. Only a stop by the cold-toolchain budget reads a
89+
* shim as armed; a stop by the phase's clock is the phase running out.
8290
*/
8391
export async function probeXcrunShimFirstLaunchHooks(
8492
options: XcrunShimProbeOptions = {},
8593
): Promise<XcrunShimProbe> {
86-
const phaseRemainingMs = Math.floor(
87-
options.deadline?.remainingMs() ?? COLD_TOOLCHAIN_PROBE_TIMEOUT_MS,
88-
);
94+
if (options.signal?.aborted) return { outcome: 'request_canceled' };
95+
const phaseRemainingMs = options.deadline
96+
? Math.floor(options.deadline.remainingMs())
97+
: Number.POSITIVE_INFINITY;
98+
if (phaseRemainingMs <= 0) return { outcome: 'phase_budget_exhausted' };
99+
const phaseOwnsBudget = phaseRemainingMs <= COLD_TOOLCHAIN_PROBE_TIMEOUT_MS;
89100
const budget = AbortSignal.timeout(Math.min(COLD_TOOLCHAIN_PROBE_TIMEOUT_MS, phaseRemainingMs));
90101
const signal = options.signal ? AbortSignal.any([budget, options.signal]) : budget;
91102
const xcrunShims = await Promise.all(
92103
XCRUN_SHIM_TOOL_NAMES.map(async (tool) => await probeWithinBudget(tool, signal)),
93104
);
94-
return options.signal?.aborted ? { canceled: true } : { canceled: false, xcrunShims };
105+
if (options.signal?.aborted) return { outcome: 'request_canceled' };
106+
if (phaseOwnsBudget && xcrunShims.some(isStoppedByBudget)) {
107+
return { outcome: 'phase_budget_exhausted' };
108+
}
109+
return { outcome: 'read', xcrunShims };
110+
}
111+
112+
function isStoppedByBudget(shim: XcrunShimFirstLaunchHook): boolean {
113+
return shim.hook === 'armed' && shim.armedBy === 'probe_out_of_budget';
95114
}
96115

97116
async function probeWithinBudget(

‎packages/platform-apple/src/runner/__tests__/runner-device-set-cleanup-arming.test.ts‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import path from 'node:path';
44
import { test, vi } from 'vitest';
55
import { AppError, isRequestCanceledError } from '@agent-device/kernel/errors';
66
import type { DeviceInfo } from '@agent-device/kernel/device';
7+
import type { DeadlineClock } from '@agent-device/host-kit/retry';
78
import {
89
XCRUN_SHIM_TOOL_NAMES,
910
type ArmedXcrunShimFirstLaunchHook,
11+
type XcrunShimProbeOptions,
1012
type XcrunShimToolName,
1113
} from '../../core/xcrun-shim-first-launch.ts';
1214
import { appleRunnerTestHost } from '../test-host.ts';
@@ -65,32 +67,36 @@ function scopedSimulator(setPath: string): DeviceInfo {
6567
};
6668
}
6769

68-
async function acquire(layout: Layout, host: FakeXcrunHost, signal?: AbortSignal) {
70+
async function acquire(layout: Layout, host: FakeXcrunHost, budget: XcrunShimProbeOptions = {}) {
6971
return await withFakeXcrunHost(host, () =>
7072
acquireXcodebuildSimulatorSetRedirect(scopedSimulator(layout.requestedSetPath), {
7173
xctestDeviceSetPath: layout.xctestDeviceSetPath,
7274
backupPath: layout.backupPath,
7375
lockDirPath: layout.lockDirPath,
74-
signal,
76+
...budget,
7577
}),
7678
);
7779
}
7880

7981
async function assertRefused(
8082
layout: Layout,
8183
host: FakeXcrunHost,
82-
signal?: AbortSignal,
84+
budget: XcrunShimProbeOptions = {},
85+
expected: 'armed' | 'request_canceled' | 'phase_budget_exhausted' = 'armed',
8386
): Promise<AppError> {
8487
let refusal: AppError | undefined;
85-
await assert.rejects(acquire(layout, host, signal), (error: unknown) => {
88+
await assert.rejects(acquire(layout, host, budget), (error: unknown) => {
8689
assert.ok(error instanceof AppError);
8790
refusal = error;
8891
return true;
8992
});
9093
assert.ok(refusal);
9194
assert.equal(refusal.code, 'COMMAND_FAILED');
92-
if (signal?.aborted) {
95+
if (expected === 'request_canceled') {
9396
assert.equal(isRequestCanceledError(refusal), true);
97+
} else if (expected === 'phase_budget_exhausted') {
98+
assert.equal(refusal.details?.reason, 'runner_phase_budget_exhausted');
99+
assert.equal(refusal.details?.xcrunShims, undefined);
94100
} else {
95101
assert.equal(refusal.details?.reason, REASON);
96102
assert.equal(refusal.details?.hint, ROW_HINT);
@@ -248,7 +254,7 @@ test('an already-canceled request gives the lock back as a cancellation without
248254
devicectl: DEVICECTL_EQUAL,
249255
});
250256

251-
await assertRefused(layout, host, AbortSignal.abort());
257+
await assertRefused(layout, host, { signal: AbortSignal.abort() }, 'request_canceled');
252258

253259
assert.deepEqual(host.finds, []);
254260
assert.deepEqual(host.plistReads, []);
@@ -263,11 +269,29 @@ test('a request canceled while a shim is read gives the lock back as a cancellat
263269
const request = new AbortController();
264270
host.onPlistRead = () => request.abort();
265271

266-
await assertRefused(layout, host, request.signal);
272+
await assertRefused(layout, host, { signal: request.signal }, 'request_canceled');
267273

268274
assert.notDeepEqual(host.plistReads, [], 'the probe was reading a shim when the request ended');
269275
});
270276

277+
test('a phase that runs out while a stalled xcrun is probed is the phase budget, not an armed shim', async () => {
278+
const layout = makeLayout();
279+
const host = writeFakeXcrunShims(layout.root, {
280+
simctl: SIMCTL_EQUAL,
281+
devicectl: DEVICECTL_EQUAL,
282+
});
283+
host.findStalls = true;
284+
const deadline: DeadlineClock = {
285+
remainingMs: () => 3,
286+
elapsedMs: () => 0,
287+
isExpired: () => false,
288+
};
289+
290+
await assertRefused(layout, host, { deadline }, 'phase_budget_exhausted');
291+
292+
assert.deepEqual(host.finds, XCRUN_SHIM_TOOL_NAMES, 'the probe was waiting on xcrun');
293+
});
294+
271295
test('the message tells a shim xcrun could not locate from one the probe ran out of budget on', async () => {
272296
const messages: string[] = [];
273297
for (const armedBy of ['shim_not_located', 'probe_out_of_budget'] as const) {
@@ -276,7 +300,7 @@ test('the message tells a shim xcrun could not locate from one the probe ran out
276300
if (armedBy === 'probe_out_of_budget') {
277301
appleRunnerTestHost.update({
278302
probeXcrunShimFirstLaunchHooks: async () => ({
279-
canceled: false,
303+
outcome: 'read',
280304
xcrunShims: XCRUN_SHIM_TOOL_NAMES.map((tool): ArmedXcrunShimFirstLaunchHook => ({
281305
tool,
282306
shimPath: null,

‎packages/platform-apple/src/runner/__tests__/runner-device-set.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818

1919
beforeEach(() => {
2020
appleRunnerTestHost.update({
21-
probeXcrunShimFirstLaunchHooks: async () => ({ canceled: false, xcrunShims: [] }),
21+
probeXcrunShimFirstLaunchHooks: async () => ({ outcome: 'read', xcrunShims: [] }),
2222
});
2323
});
2424

‎packages/platform-apple/src/runner/__tests__/runner-session-lifecycle.test.ts‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -403,39 +403,52 @@ test('an armed xcrun shim refuses a scoped-set session before the runner launche
403403
simctl: { expectedVersion: '1155.4', installedVersion: '1155.4' },
404404
devicectl: { expectedVersion: '506.6', installedVersion: '629.3' },
405405
});
406-
const redirectOptions: Array<Parameters<typeof acquireRealSimulatorSetRedirect>[1]> = [];
406+
const redirectRemainingMs: Array<number | undefined> = [];
407407
mockAcquireXcodebuildSimulatorSetRedirect.mockImplementation(
408408
async (device: DeviceInfo, options: Parameters<typeof acquireRealSimulatorSetRedirect>[1]) => {
409-
redirectOptions.push(options);
409+
redirectRemainingMs.push(options?.deadline?.remainingMs());
410410
return await acquireRealSimulatorSetRedirect(device, {
411411
...options,
412412
xctestDeviceSetPath,
413413
lockDirPath: path.join(root, 'xctest-device-set.lock'),
414414
});
415415
},
416416
);
417-
mockEnsureXctestrunArtifact.mockResolvedValue({
418-
xctestrunPath: '/tmp/base-runner.xctestrun',
419-
derived: '/tmp/derived',
420-
cache: 'exact',
421-
artifact: 'valid',
422-
buildMs: 0,
423-
xctestrunPathSource: 'manifest',
417+
const buildMs = 50_000;
418+
const realNow = Date.now.bind(Date);
419+
mockEnsureXctestrunArtifact.mockImplementation(async () => {
420+
vi.spyOn(Date, 'now').mockImplementation(() => realNow() + buildMs);
421+
return {
422+
xctestrunPath: '/tmp/base-runner.xctestrun',
423+
derived: '/tmp/derived',
424+
cache: 'exact',
425+
artifact: 'rebuilt',
426+
buildMs,
427+
xctestrunPathSource: 'build',
428+
};
424429
});
425430
const device = {
426431
...IOS_SIMULATOR,
427432
id: 'runner-lifecycle-armed-shim',
428433
simulatorSetPath: requestedSetPath,
429434
};
430435

431-
await assert.rejects(
432-
withFakeXcrunHost(host, () => ensureRunnerSession(device, { startupTimeoutMs: 60_000 })),
433-
(error: unknown) =>
434-
error instanceof AppError && error.details?.reason === 'xctest_device_set_cleanup_armed',
435-
);
436+
try {
437+
await assert.rejects(
438+
withFakeXcrunHost(host, () => ensureRunnerSession(device, { startupTimeoutMs: 60_000 })),
439+
(error: unknown) =>
440+
error instanceof AppError && error.details?.reason === 'xctest_device_set_cleanup_armed',
441+
);
442+
} finally {
443+
vi.mocked(Date.now).mockRestore();
444+
}
436445

437-
const startupRemainingMs = redirectOptions[0]?.deadline?.remainingMs() ?? Number.NaN;
438-
assert.equal(startupRemainingMs > 0 && startupRemainingMs <= 60_000, true, 'the startup clock');
446+
const remainingMs = redirectRemainingMs[0] ?? Number.NaN;
447+
assert.equal(
448+
remainingMs > 60_000 - buildMs && remainingMs <= 60_000,
449+
true,
450+
`the redirect spends the startup time read before the build, not what the build left: ${remainingMs} ms`,
451+
);
439452
assert.equal(mockRunCmdBackground.mock.calls.length, 0, 'no test-without-building was spawned');
440453
assert.equal(fs.lstatSync(xctestDeviceSetPath).isSymbolicLink(), false);
441454
assert.equal(readRunnerSessionLiveness(device.id), null);

‎packages/platform-apple/src/runner/runner-cache-metadata.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function requireRunnerPhaseRemainingMs(
110110
}
111111

112112
/** Says the phase budget ran out, not that the step it would have run is broken. */
113-
function runnerPhaseBudgetExhaustedError(phase: string): AppError {
113+
export function runnerPhaseBudgetExhaustedError(phase: string): AppError {
114114
return new AppError('COMMAND_FAILED', 'The Apple runner budget ran out before this step began', {
115115
phase,
116116
reason: 'runner_phase_budget_exhausted',

0 commit comments

Comments
 (0)