Skip to content

Commit 4353e76

Browse files
committed
Revert "refactor(apple): carry one phase Deadline through the runner interfaces and test cancellation as a matrix (#2473)"
This reverts commit f4c8f3d.
1 parent f4c8f3d commit 4353e76

12 files changed

Lines changed: 264 additions & 421 deletions

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

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

166163
await expect(
167-
tryAdoptRunnerSessionFromLease(simulator, {
168-
budget: createRunnerPhaseBudget(undefined, request.signal),
169-
}),
164+
tryAdoptRunnerSessionFromLease(simulator, { signal: request.signal }),
170165
).rejects.toSatisfy(isRequestCanceledError);
171166
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
172167

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

193188
await expect(
194-
tryAdoptRunnerSessionFromLease(simulator, {
195-
budget: createRunnerPhaseBudget(undefined, warmRequest.signal),
196-
}),
189+
tryAdoptRunnerSessionFromLease(simulator, { signal: warmRequest.signal }),
197190
).rejects.toSatisfy(isRequestCanceledError);
198191
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
199192
// 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 { createRunnerPhaseBudget, ensureXctestrunArtifact } from '../runner-xctestrun.ts';
9+
import { 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, { budget: createRunnerPhaseBudget(120_000, undefined) }),
70+
ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 120_000 }),
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, { budget: createRunnerPhaseBudget(120_000, undefined) }),
91+
ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 120_000 }),
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, { budget: createRunnerPhaseBudget(30_000, undefined) }),
110+
ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 30_000 }),
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: 93 additions & 171 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-
createRunnerPhaseBudget,
9+
createRunnerPhaseDeadline,
1010
diffComparableRunnerCacheMetadata,
1111
resolveRunnerBundleBuildSettings,
1212
resolveRunnerMaxConcurrentDestinationsFlag,
@@ -278,16 +278,37 @@ 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+
281301
test('an owning phase with 4 s left gets one 4 s attempt and no retry', () => {
282302
const clock = installFakeToolchainClock();
283-
const phaseBudget = createRunnerPhaseBudget(4_000, undefined);
303+
const phaseDeadline = createRunnerPhaseDeadline(4_000);
284304
runCmdSync.mockImplementation((command: string, args: string[], options: ExecOptions) => {
285305
throw blockForWholeTimeout(clock, command, args, options);
286306
});
287307
runCmdSync.mockClear();
288308

289309
assert.throws(
290-
() => resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, phaseBudget),
310+
() =>
311+
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, { deadline: phaseDeadline }),
291312
(error: unknown) => expectRunnerPhaseBudgetExhausted(error),
292313
);
293314
assert.equal(runCmdSync.mock.calls.length, 1);
@@ -296,6 +317,75 @@ describe('toolchain probe budget', () => {
296317
assert.equal(clock.nowMs, 4_000);
297318
});
298319

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+
299389
test('a probe that failed on its own and merely says "timed out" in its message is not retried', () => {
300390
installFakeToolchainClock();
301391
runCmdSync.mockImplementation((command: string, args: string[]) => {
@@ -319,174 +409,6 @@ describe('toolchain probe budget', () => {
319409
);
320410
expect(runCmdSync.mock.calls.filter(([command]) => command === 'xcodebuild')).toHaveLength(1);
321411
});
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-
}
490412
});
491413

492414
/** 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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import {
5252
} from '../runner-cache.ts';
5353
import { ensureXctestrunArtifact, xctestrunReferencesProjectRoot } from '../runner-artifact.ts';
5454
import {
55-
createRunnerPhaseBudget,
5655
markRunnerXctestrunArtifactBadForRun,
5756
resolveExpectedRunnerCacheMetadata,
5857
resolveRunnerDerivedPath,
@@ -1100,10 +1099,10 @@ test('ensureXctestrunArtifact aborts only the disconnected request build and pre
11001099
});
11011100

11021101
const canceledPromise = ensureXctestrunArtifact(canceledDevice, {
1103-
budget: createRunnerPhaseBudget(undefined, canceledController.signal),
1102+
signal: canceledController.signal,
11041103
});
11051104
const survivorPromise = ensureXctestrunArtifact(survivorDevice, {
1106-
budget: createRunnerPhaseBudget(undefined, survivorController.signal),
1105+
signal: survivorController.signal,
11071106
});
11081107

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

14111410
const rebuilt = await ensureXctestrunArtifact(macOsDevice, {
1412-
budget: createRunnerPhaseBudget(300_000, undefined),
1411+
buildTimeoutMs: 300_000,
14131412
});
14141413

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

0 commit comments

Comments
 (0)