Skip to content

Commit 37646f7

Browse files
committed
refactor(apple): carry one phase Deadline through the runner interfaces and test cancellation as a matrix
1 parent 3e89f82 commit 37646f7

12 files changed

Lines changed: 421 additions & 264 deletions

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import {
1313
} from '../runner-lease.ts';
1414
import { isIosRunnerDetachEnabled, tryAdoptRunnerSessionFromLease } from '../runner-adoption.ts';
1515
import { sendRunnerCommandOnce } from '../runner-transport.ts';
16-
import { resolveExpectedRunnerCacheMetadata } from '../runner-xctestrun.ts';
16+
import {
17+
createRunnerPhaseBudget,
18+
resolveExpectedRunnerCacheMetadata,
19+
} from '../runner-xctestrun.ts';
1720
import { appleRunnerTestHost } from '../test-host.ts';
1821
import { appleToolchainProbeResult } from './apple-toolchain-fixtures.ts';
1922
import { mkdtempForTestSync } from './tmp-dir.ts';
@@ -161,7 +164,9 @@ test('a request canceled during the fingerprint probe fails adoption instead of
161164
});
162165

163166
await expect(
164-
tryAdoptRunnerSessionFromLease(simulator, { signal: request.signal }),
167+
tryAdoptRunnerSessionFromLease(simulator, {
168+
budget: createRunnerPhaseBudget(undefined, request.signal),
169+
}),
165170
).rejects.toSatisfy(isRequestCanceledError);
166171
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
167172

@@ -186,7 +191,9 @@ test('a request canceled during the fingerprint probe fails adoption instead of
186191
);
187192

188193
await expect(
189-
tryAdoptRunnerSessionFromLease(simulator, { signal: warmRequest.signal }),
194+
tryAdoptRunnerSessionFromLease(simulator, {
195+
budget: createRunnerPhaseBudget(undefined, warmRequest.signal),
196+
}),
190197
).rejects.toSatisfy(isRequestCanceledError);
191198
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
192199
// Ownership was never transferred: the stale lease is untouched.

‎packages/platform-apple/src/runner/__tests__/runner-artifact-phase-budget.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AppError } from '@agent-device/kernel/errors';
66
import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo';
77
import { appleRunnerTestHost } from '../test-host.ts';
88
import type { ExecOptions, ExecResult, ExecStreamOptions } from '../host.ts';
9-
import { ensureXctestrunArtifact } from '../runner-xctestrun.ts';
9+
import { createRunnerPhaseBudget, ensureXctestrunArtifact } from '../runner-xctestrun.ts';
1010
import { appleToolchainProbeResult } from './apple-toolchain-fixtures.ts';
1111
import { MACOS_DEVICE } from './device-fixtures.ts';
1212
import { mkdtempForTestSync } from './tmp-dir.ts';
@@ -67,7 +67,7 @@ afterEach(() => {
6767

6868
test('a warm toolchain leaves the build the whole phase budget', async () => {
6969
await assert.rejects(
70-
ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 120_000 }),
70+
ensureXctestrunArtifact(MACOS_DEVICE, { budget: createRunnerPhaseBudget(120_000, undefined) }),
7171
missingXctestrun,
7272
);
7373

@@ -88,7 +88,7 @@ test('a cold-start probe stall comes out of the build budget instead of being ad
8888
});
8989

9090
await assert.rejects(
91-
ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 120_000 }),
91+
ensureXctestrunArtifact(MACOS_DEVICE, { budget: createRunnerPhaseBudget(120_000, undefined) }),
9292
missingXctestrun,
9393
);
9494

@@ -107,7 +107,7 @@ test('a probe that spends the whole phase fails before xcodebuild is spawned', a
107107
});
108108

109109
await assert.rejects(
110-
ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 30_000 }),
110+
ensureXctestrunArtifact(MACOS_DEVICE, { budget: createRunnerPhaseBudget(30_000, undefined) }),
111111
(error: unknown) =>
112112
error instanceof AppError &&
113113
error.details?.reason === 'runner_phase_budget_exhausted' &&

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

Lines changed: 171 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { IOS_DEVICE, IOS_SIMULATOR, MACOS_DEVICE } from './device-fixtures.ts';
66
import { appleRunnerTestHost } from '../test-host.ts';
77
import type { ExecOptions } from '../host.ts';
88
import {
9-
createRunnerPhaseDeadline,
9+
createRunnerPhaseBudget,
1010
diffComparableRunnerCacheMetadata,
1111
resolveRunnerBundleBuildSettings,
1212
resolveRunnerMaxConcurrentDestinationsFlag,
@@ -278,37 +278,16 @@ describe('toolchain probe budget', () => {
278278
assert.deepEqual(xcodebuildTimeouts, [COLD_TOOLCHAIN_PROBE_TIMEOUT_MS, 15_000]);
279279
});
280280

281-
test('a toolchain host that never returns stops at the shared budget instead of once per probe', () => {
282-
const clock = installFakeToolchainClock();
283-
runCmdSync.mockImplementation((command: string, args: string[], options: ExecOptions) => {
284-
throw blockForWholeTimeout(clock, command, args, options);
285-
});
286-
runCmdSync.mockClear();
287-
288-
assert.throws(
289-
() => resolveExpectedRunnerCacheMetadata(MACOS_DEVICE),
290-
// A budget spent before the remaining probes could start is not an
291-
// unreadable toolchain: nothing probed it, so the error says the budget
292-
// ran out rather than pointing at `xcode-select`.
293-
(error: unknown) => expectRunnerPhaseBudgetExhausted(error),
294-
);
295-
// 30 s + a 15 s retry spends the whole budget on the first probe; the two
296-
// xcrun probes then fail on the budget instead of blocking for 30 s each.
297-
assert.equal(runCmdSync.mock.calls.length, 2);
298-
assert.equal(clock.nowMs, 45_000);
299-
});
300-
301281
test('an owning phase with 4 s left gets one 4 s attempt and no retry', () => {
302282
const clock = installFakeToolchainClock();
303-
const phaseDeadline = createRunnerPhaseDeadline(4_000);
283+
const phaseBudget = createRunnerPhaseBudget(4_000, undefined);
304284
runCmdSync.mockImplementation((command: string, args: string[], options: ExecOptions) => {
305285
throw blockForWholeTimeout(clock, command, args, options);
306286
});
307287
runCmdSync.mockClear();
308288

309289
assert.throws(
310-
() =>
311-
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, { deadline: phaseDeadline }),
290+
() => resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, phaseBudget),
312291
(error: unknown) => expectRunnerPhaseBudgetExhausted(error),
313292
);
314293
assert.equal(runCmdSync.mock.calls.length, 1);
@@ -317,75 +296,6 @@ describe('toolchain probe budget', () => {
317296
assert.equal(clock.nowMs, 4_000);
318297
});
319298

320-
test('a request canceled while a probe blocked surfaces the cancellation instead of retrying', () => {
321-
const clock = installFakeToolchainClock();
322-
const request = new AbortController();
323-
runCmdSync.mockImplementation((command: string, args: string[], options: ExecOptions) => {
324-
const timeout = blockForWholeTimeout(clock, command, args, options);
325-
request.abort();
326-
throw timeout;
327-
});
328-
runCmdSync.mockClear();
329-
330-
assert.throws(
331-
() =>
332-
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, { signal: request.signal }),
333-
(error: unknown) => isRequestCanceledError(error),
334-
);
335-
assert.equal(runCmdSync.mock.calls.length, 1);
336-
});
337-
338-
test('a non-timeout error that also cancels the request on the last probe surfaces cancellation, not an unavailable toolchain', () => {
339-
const request = new AbortController();
340-
runCmdSync.mockImplementation((command: string, args: string[]) => {
341-
// The final probe: abort the request and fail with a plain command
342-
// error, not the exec layer's structured timeout -- there is no next
343-
// attempt left to catch the cancellation, so the catch here must.
344-
if (command === 'xcrun' && args.includes('--show-sdk-build-version')) {
345-
request.abort();
346-
throw new AppError('COMMAND_FAILED', 'xcrun: unexpected error', {});
347-
}
348-
return appleToolchainProbeResult(command, args);
349-
});
350-
runCmdSync.mockClear();
351-
352-
assert.throws(
353-
() =>
354-
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, { signal: request.signal }),
355-
(error: unknown) => isRequestCanceledError(error),
356-
);
357-
assert.equal(runCmdSync.mock.calls.length, 3);
358-
});
359-
360-
test('an already-canceled request runs no toolchain probe at all, cold or with the fingerprint cache warm', () => {
361-
installFakeToolchainClock();
362-
runCmdSync.mockClear();
363-
364-
assert.throws(
365-
() =>
366-
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, {
367-
signal: AbortSignal.abort(),
368-
}),
369-
(error: unknown) => isRequestCanceledError(error),
370-
);
371-
assert.equal(runCmdSync.mock.calls.length, 0);
372-
373-
// Warm the real fingerprint memo with an ordinary request, then repeat
374-
// with an already-aborted signal: the cache-hit path must check
375-
// cancellation before it returns the memoized value, not skip it (#2422).
376-
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR);
377-
runCmdSync.mockClear();
378-
379-
assert.throws(
380-
() =>
381-
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, {
382-
signal: AbortSignal.abort(),
383-
}),
384-
(error: unknown) => isRequestCanceledError(error),
385-
);
386-
assert.equal(runCmdSync.mock.calls.length, 0);
387-
});
388-
389299
test('a probe that failed on its own and merely says "timed out" in its message is not retried', () => {
390300
installFakeToolchainClock();
391301
runCmdSync.mockImplementation((command: string, args: string[]) => {
@@ -409,6 +319,174 @@ describe('toolchain probe budget', () => {
409319
);
410320
expect(runCmdSync.mock.calls.filter(([command]) => command === 'xcodebuild')).toHaveLength(1);
411321
});
322+
323+
/**
324+
* One row per way a fingerprint read can be interrupted: when the owning request aborts,
325+
* what the probe that abort lands on does, and what the phase must then surface.
326+
* `spawnSync` cannot be interrupted once it has started, so a cancellation is only ever
327+
* observed between attempts -- the exec count is what pins which attempt each row stopped.
328+
*/
329+
type ToolchainProbeCancellationCase = {
330+
label: string;
331+
/** Which execs refuse to answer, and how they end; every other exec answers. */
332+
failing?: { at: 'first' | 'final' | 'every'; as: 'exec-timeout' | 'command-failure' };
333+
/**
334+
* When the owning request aborts: never, before the phase runs a probe at all, while
335+
* the failing exec is still blocked, or as that exec's timeout unwinds.
336+
*/
337+
aborts: 'never' | 'before-any-probe' | 'while-it-blocks' | 'as-it-unwinds';
338+
/** Whether the process-wide fingerprint memo already holds an answer. */
339+
fingerprintCache?: 'warm';
340+
expected: 'request-canceled' | 'budget-exhausted' | 'fingerprint';
341+
/** Execs the phase is allowed to have run by the time it settles. */
342+
execs: number;
343+
/** Wall clock the phase spent: only an exec that blocks for its whole timeout moves it. */
344+
clockMs: number;
345+
};
346+
347+
const CANCELLATION_CASES: ToolchainProbeCancellationCase[] = [
348+
{
349+
label: 'aborted before the first probe, cold cache',
350+
aborts: 'before-any-probe',
351+
expected: 'request-canceled',
352+
execs: 0,
353+
clockMs: 0,
354+
},
355+
{
356+
// A cache hit must not answer a request that is already gone (#2422 round 4).
357+
label: 'aborted before the first probe, fingerprint cache warm',
358+
aborts: 'before-any-probe',
359+
fingerprintCache: 'warm',
360+
expected: 'request-canceled',
361+
execs: 0,
362+
clockMs: 0,
363+
},
364+
{
365+
label: 'aborted while the first probe blocks, and it then times out',
366+
failing: { at: 'first', as: 'exec-timeout' },
367+
aborts: 'while-it-blocks',
368+
expected: 'request-canceled',
369+
execs: 1,
370+
clockMs: COLD_TOOLCHAIN_PROBE_TIMEOUT_MS,
371+
},
372+
{
373+
label: 'aborted while the first probe fails with a non-timeout error',
374+
failing: { at: 'first', as: 'command-failure' },
375+
aborts: 'while-it-blocks',
376+
expected: 'request-canceled',
377+
execs: 1,
378+
clockMs: 0,
379+
},
380+
{
381+
// The budget still had 15 s: only the cancellation stops the retry.
382+
label: "aborted as the first attempt's timeout unwinds, before its retry",
383+
failing: { at: 'first', as: 'exec-timeout' },
384+
aborts: 'as-it-unwinds',
385+
expected: 'request-canceled',
386+
execs: 1,
387+
clockMs: COLD_TOOLCHAIN_PROBE_TIMEOUT_MS,
388+
},
389+
{
390+
// Nothing is left to catch the cancellation on a later attempt, so the failing
391+
// probe's own catch must: an unavailable toolchain would be the wrong verdict.
392+
label: 'aborted while the final probe fails with a non-timeout error',
393+
failing: { at: 'final', as: 'command-failure' },
394+
aborts: 'while-it-blocks',
395+
expected: 'request-canceled',
396+
execs: 3,
397+
clockMs: 0,
398+
},
399+
{
400+
label: 'aborted while the final probe blocks, and it then times out',
401+
failing: { at: 'final', as: 'exec-timeout' },
402+
aborts: 'while-it-blocks',
403+
expected: 'request-canceled',
404+
execs: 3,
405+
clockMs: COLD_TOOLCHAIN_PROBE_TIMEOUT_MS,
406+
},
407+
{
408+
// 30 s plus a 15 s retry spends the whole 45 s ceiling on the first probe. The two
409+
// xcrun probes never ran, so the verdict is the budget, not an unreadable toolchain:
410+
// an `xcode-select` hint here would point at the wrong thing.
411+
label: 'never aborted, the first probe and its retry spend the whole budget',
412+
failing: { at: 'every', as: 'exec-timeout' },
413+
aborts: 'never',
414+
expected: 'budget-exhausted',
415+
execs: 2,
416+
clockMs: 45_000,
417+
},
418+
{
419+
label: 'never aborted, the first probe times out and its retry recovers',
420+
failing: { at: 'first', as: 'exec-timeout' },
421+
aborts: 'never',
422+
expected: 'fingerprint',
423+
execs: 4,
424+
clockMs: COLD_TOOLCHAIN_PROBE_TIMEOUT_MS,
425+
},
426+
];
427+
428+
test.each(CANCELLATION_CASES)('cancellation matrix: $label', (testCase) => {
429+
const clock = installFakeToolchainClock();
430+
const request = new AbortController();
431+
if (testCase.fingerprintCache === 'warm') {
432+
runCmdSync.mockImplementation(appleToolchainProbeResult);
433+
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR);
434+
}
435+
if (testCase.aborts === 'before-any-probe') request.abort();
436+
437+
let execs = 0;
438+
runCmdSync.mockImplementation((command: string, args: string[], options: ExecOptions) => {
439+
execs += 1;
440+
if (!failsOnExec(testCase, execs)) return appleToolchainProbeResult(command, args);
441+
if (testCase.aborts === 'while-it-blocks') request.abort();
442+
const failure =
443+
testCase.failing?.as === 'exec-timeout'
444+
? blockForWholeTimeout(clock, command, args, options)
445+
: // No `timeoutMs` detail: the tool failed on its own, so nothing retries it.
446+
new AppError('COMMAND_FAILED', `${command}: unexpected error`, { cmd: command, args });
447+
if (testCase.aborts === 'as-it-unwinds') request.abort();
448+
throw failure;
449+
});
450+
451+
const readFingerprint = () =>
452+
resolveExpectedRunnerCacheMetadata(
453+
IOS_SIMULATOR,
454+
undefined,
455+
createRunnerPhaseBudget(undefined, request.signal),
456+
);
457+
458+
if (testCase.expected === 'fingerprint') {
459+
const metadata = readFingerprint();
460+
assert.equal(metadata.xcodeVersion, '26.2', testCase.label);
461+
assert.equal(metadata.xcodeBuildVersion, '17C52', testCase.label);
462+
} else {
463+
assert.throws(readFingerprint, (error: unknown) => {
464+
assert.ok(
465+
testCase.expected === 'request-canceled'
466+
? isRequestCanceledError(error)
467+
: expectRunnerPhaseBudgetExhausted(error),
468+
`${testCase.label}: expected ${testCase.expected}, got ${String(error)}`,
469+
);
470+
return true;
471+
});
472+
}
473+
assert.equal(execs, testCase.execs, `${testCase.label}: exec count`);
474+
assert.equal(clock.nowMs, testCase.clockMs, `${testCase.label}: wall clock spent`);
475+
});
476+
477+
/** Which exec a row's failing probe is: the first, the last of the three, or all of them. */
478+
function failsOnExec(testCase: ToolchainProbeCancellationCase, exec: number): boolean {
479+
switch (testCase.failing?.at) {
480+
case 'first':
481+
return exec === 1;
482+
case 'final':
483+
return exec === 3;
484+
case 'every':
485+
return true;
486+
default:
487+
return false;
488+
}
489+
}
412490
});
413491

414492
/** The error a runner phase raises when a step is reached with nothing left to spend. */

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
} from '../runner-cache.ts';
5353
import { ensureXctestrunArtifact, xctestrunReferencesProjectRoot } from '../runner-artifact.ts';
5454
import {
55+
createRunnerPhaseBudget,
5556
markRunnerXctestrunArtifactBadForRun,
5657
resolveExpectedRunnerCacheMetadata,
5758
resolveRunnerDerivedPath,
@@ -1099,10 +1100,10 @@ test('ensureXctestrunArtifact aborts only the disconnected request build and pre
10991100
});
11001101

11011102
const canceledPromise = ensureXctestrunArtifact(canceledDevice, {
1102-
signal: canceledController.signal,
1103+
budget: createRunnerPhaseBudget(undefined, canceledController.signal),
11031104
});
11041105
const survivorPromise = ensureXctestrunArtifact(survivorDevice, {
1105-
signal: survivorController.signal,
1106+
budget: createRunnerPhaseBudget(undefined, survivorController.signal),
11061107
});
11071108

11081109
await Promise.all([canceledBuildStarted.promise, survivorBuildStarted.promise]);
@@ -1408,7 +1409,7 @@ test('ensureXctestrunArtifact stress-recovers after a bad restored artifact', as
14081409
});
14091410

14101411
const rebuilt = await ensureXctestrunArtifact(macOsDevice, {
1411-
buildTimeoutMs: 300_000,
1412+
budget: createRunnerPhaseBudget(300_000, undefined),
14121413
});
14131414

14141415
assert.equal(rebuilt.xctestrunPath, rebuiltXctestrunPath);

0 commit comments

Comments
 (0)