Skip to content

Commit 2f4462e

Browse files
committed
fix(apple): keep cancellation typed after a probe timeout and simplify the probe budget
1 parent cf5fbc1 commit 2f4462e

15 files changed

Lines changed: 177 additions & 112 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export {
22
coerceExecResult,
3+
COLD_TOOLCHAIN_PROBE_TIMEOUT_MS,
34
type CommandExecutorOverride,
45
type ExecBackgroundOptions,
56
type ExecBackgroundResult,

‎packages/host-kit/src/internal/exec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,21 @@ export function isCommandTimeoutError(error: unknown): boolean {
608608
);
609609
}
610610

611+
/**
612+
* Ceiling on one Apple toolchain identity probe attempt (`xcodebuild -version`,
613+
* `xcrun --show-sdk-version`, `sw_vers`, `uname`, …). On a fresh macOS host,
614+
* Apple's syspolicyd signature scan blocks the very first `xcodebuild`/`xcrun`/
615+
* large-binary exec after boot for roughly 18 to 19 seconds at 0% CPU; the
616+
* second exec of the same tool is instant. A budget sized for a warm toolchain
617+
* (the old 10 s / 5 s split) trips on that cold-start stall and reports a bogus
618+
* toolchain-probe timeout unrelated to the change under test (#2422).
619+
*
620+
* It sits beside {@link isCommandTimeoutError} because it is a property of
621+
* exec'ing an Apple tool rather than of either prober: both Apple toolchain
622+
* probers read this one value.
623+
*/
624+
export const COLD_TOOLCHAIN_PROBE_TIMEOUT_MS = 30_000;
625+
611626
function createExitError(
612627
executable: string,
613628
cmd: string,

‎packages/platform-apple/src/core/config.ts‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,3 @@ export const IOS_RUNNER_SCREENSHOT_COPY_TIMEOUT_MS = 20_000;
2323
export const IOS_SIMULATOR_SCREENSHOT_RETRY_MAX_ATTEMPTS = 5;
2424
export const IOS_SIMULATOR_SCREENSHOT_RETRY_BASE_DELAY_MS = 1_000;
2525
export const IOS_SIMULATOR_SCREENSHOT_RETRY_MAX_DELAY_MS = 5_000;
26-
27-
/**
28-
* Ceiling on one Apple toolchain identity probe attempt (`xcodebuild -version`,
29-
* `xcrun --show-sdk-version`, `sw_vers`, `uname`, …). On a fresh macOS host,
30-
* Apple's syspolicyd signature scan blocks the very first `xcodebuild`/`xcrun`/
31-
* large-binary exec after boot for roughly 18 to 19 seconds at 0% CPU; the
32-
* second exec of the same tool is instant. A budget sized for a warm toolchain
33-
* (the old 10 s / 5 s split) trips on that cold-start stall and reports a bogus
34-
* toolchain-probe timeout unrelated to the change under test (#2422).
35-
*
36-
* Both toolchain probers read this one value. `snapshot-source/cache-identity.ts`
37-
* imports it from here; `runner/runner-cache-metadata.ts` reads it through the
38-
* Apple runner host port instead, because that file sits in every Apple façade's
39-
* eager import closure and may not add a module to it
40-
* (`scripts/__tests__/eager-closure-budgets.test.ts`).
41-
*/
42-
export const COLD_TOOLCHAIN_PROBE_TIMEOUT_MS = 30_000;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { publishFileSync, acquireProcessLock } from '@agent-device/host-kit/file
33
import { resolveIosSimulatorDeviceSetPath } from '@agent-device/kernel/device-isolation';
44

55
import {
6+
COLD_TOOLCHAIN_PROBE_TIMEOUT_MS,
67
isCommandTimeoutError,
78
requireExecSuccess,
89
runCmdBackground,
@@ -35,7 +36,6 @@ import {
3536
} from '@agent-device/host-kit/request';
3637

3738
import { bootFailureHint, classifyBootFailure } from '@agent-device/provision-kit/boot-diagnostics';
38-
import { COLD_TOOLCHAIN_PROBE_TIMEOUT_MS } from './config.ts';
3939
import { resolveIosPhysicalDeviceControl } from './physical-device-control.ts';
4040
import { visitXmlPlistEntries } from './plist-xml.ts';
4141
import {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
44
import type { DeviceInfo } from '@agent-device/kernel/device';
5+
import { createRequestCanceledError, isRequestCanceledError } from '@agent-device/kernel/errors';
56
import {
67
buildDetachedRunnerLease,
78
buildRunnerLease,
@@ -11,6 +12,7 @@ import {
1112
} from '../runner-lease.ts';
1213
import { isIosRunnerDetachEnabled, tryAdoptRunnerSessionFromLease } from '../runner-adoption.ts';
1314
import { sendRunnerCommandOnce } from '../runner-transport.ts';
15+
import { resolveExpectedRunnerCacheMetadata } from '../runner-xctestrun.ts';
1416
import { appleRunnerTestHost } from '../test-host.ts';
1517
import { mkdtempForTestSync } from './tmp-dir.ts';
1618

@@ -28,6 +30,7 @@ vi.mock('../runner-xctestrun.ts', async (importOriginal) => {
2830
});
2931

3032
const mockSendRunnerCommandOnce = vi.mocked(sendRunnerCommandOnce);
33+
const mockResolveExpectedRunnerCacheMetadata = vi.mocked(resolveExpectedRunnerCacheMetadata);
3134
const mockIsProcessAlive = vi.fn((_pid: number) => false);
3235
const mockReadProcessCommand = vi.fn((_pid: number): string | null => null);
3336
const mockReadProcessStartTime = vi.fn((_pid: number): string | null => 'test-process-start');
@@ -140,6 +143,27 @@ test('adoption succeeds for a live, matching, probe-healthy runner', async () =>
140143
expect(readStaleRunnerLease(simulator.id)).toBeNull();
141144
});
142145

146+
test('a request canceled during the fingerprint probe fails adoption instead of skipping it', async () => {
147+
// The fingerprint check runs the same blocking toolchain probes a fresh
148+
// startup would, and it is handed the request's signal. A cancellation from
149+
// there is not an unresolvable derived path: swallowing it would let startup
150+
// walk on past a client that is already gone (#2422).
151+
writeStaleLease();
152+
mockIsProcessAlive.mockReturnValue(true);
153+
const request = new AbortController();
154+
request.abort();
155+
mockResolveExpectedRunnerCacheMetadata.mockImplementationOnce((_device, _projectRoot, budget) => {
156+
// The probe only cancels because the request's signal reached it.
157+
expect(budget?.signal?.aborted).toBe(true);
158+
throw createRequestCanceledError({ phase: 'apple_toolchain_probe' });
159+
});
160+
161+
await expect(
162+
tryAdoptRunnerSessionFromLease(simulator, { signal: request.signal }),
163+
).rejects.toSatisfy(isRequestCanceledError);
164+
expect(mockSendRunnerCommandOnce).not.toHaveBeenCalled();
165+
});
166+
143167
test('adoption is skipped for a recycled runner pid (start time mismatch)', async () => {
144168
// The live process on the leased pid started at a different time than the
145169
// lease recorded — pid recycled since the owner died (#1596). Adopting it

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import {
1515
resolveRunnerSandboxBuildArgs,
1616
resolveExpectedRunnerCacheMetadata,
1717
} from '../runner-cache-metadata.ts';
18-
// The one owning module for the probe budget: this file's probes reach it
19-
// through the runner host port, snapshot-source imports it directly (#2422).
20-
import { COLD_TOOLCHAIN_PROBE_TIMEOUT_MS } from '../../core/config.ts';
18+
// The one owning module for the probe budget: host-kit's exec layer. The
19+
// snapshot-source prober imports it from there directly; this file's probes read
20+
// the same value through the runner host port (#2422).
21+
import { COLD_TOOLCHAIN_PROBE_TIMEOUT_MS } from '@agent-device/host-kit/command';
2122
import { appleToolchainProbeResult, stubAppleToolchainProbes } from './apple-toolchain-fixtures.ts';
2223

2324
const runCmdSync = stubAppleToolchainProbes();
@@ -289,19 +290,10 @@ describe('toolchain probe budget', () => {
289290

290291
assert.throws(
291292
() => resolveExpectedRunnerCacheMetadata(MACOS_DEVICE),
292-
(error: unknown) => {
293-
assert.ok(error instanceof AppError);
294-
assert.equal(error.code, 'COMMAND_FAILED');
295-
assert.equal(error.details?.reason, 'apple_toolchain_probe_unavailable');
296-
assert.equal(error.details?.retriable, true);
297-
expect(error.details?.hint).toContain('xcode-select -p');
298-
expect(error.message).toContain('xcodebuild -version');
299-
// The reported failure is the last attempt: 30 s, then the 15 s the
300-
// budget still had.
301-
expect(error.message).toContain('xcodebuild timed out after 15000ms');
302-
expect(error.message).toContain('request budget');
303-
return true;
304-
},
293+
// A budget spent before the remaining probes could start is not an
294+
// unreadable toolchain: nothing probed it, so the error says the budget
295+
// ran out rather than pointing at `xcode-select`.
296+
(error: unknown) => expectRunnerPhaseBudgetExhausted(error),
305297
);
306298
// 30 s + a 15 s retry spends the whole budget on the first probe; the two
307299
// xcrun probes then fail on the budget instead of blocking for 30 s each.
@@ -320,15 +312,11 @@ describe('toolchain probe budget', () => {
320312
assert.throws(
321313
() =>
322314
resolveExpectedRunnerCacheMetadata(IOS_SIMULATOR, undefined, { deadline: phaseDeadline }),
323-
(error: unknown) => {
324-
assert.ok(error instanceof AppError);
325-
assert.equal(error.details?.reason, 'apple_toolchain_probe_unavailable');
326-
expect(error.message).toContain('xcodebuild timed out after 4000ms');
327-
expect(error.message).toContain('request budget');
328-
return true;
329-
},
315+
(error: unknown) => expectRunnerPhaseBudgetExhausted(error),
330316
);
331317
assert.equal(runCmdSync.mock.calls.length, 1);
318+
// The one attempt was capped by the phase, not by the 30 s per-call ceiling.
319+
assert.equal(runCmdSync.mock.calls[0]?.[2]?.timeoutMs, 4_000);
332320
assert.equal(clock.nowMs, 4_000);
333321
});
334322

@@ -389,6 +377,17 @@ describe('toolchain probe budget', () => {
389377
});
390378
});
391379

380+
/** The error a runner phase raises when a step is reached with nothing left to spend. */
381+
function expectRunnerPhaseBudgetExhausted(error: unknown): boolean {
382+
assert.ok(error instanceof AppError);
383+
assert.equal(error.code, 'COMMAND_FAILED');
384+
assert.equal(error.details?.reason, 'runner_phase_budget_exhausted');
385+
assert.equal(error.details?.phase, 'apple_toolchain_probe');
386+
assert.equal(error.details?.retriable, true);
387+
expect(error.message).toContain('budget ran out');
388+
return true;
389+
}
390+
392391
/**
393392
* A clock the probes' own budget reads, advanced only by
394393
* {@link blockForWholeTimeout}. Without it a mock that throws immediately

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ export type AppleRunnerHost = {
156156
*/
157157
isCommandTimeoutError(error: unknown): boolean;
158158
/**
159-
* Ceiling on one toolchain identity probe attempt, in milliseconds
160-
* (`COLD_TOOLCHAIN_PROBE_TIMEOUT_MS` in
161-
* `packages/platform-apple/src/core/config.ts`, which owns it for both
162-
* toolchain probers). It arrives through the port rather than by import so
163-
* the runner's cache-metadata module, which every Apple façade evaluates,
164-
* does not grow its eager import closure to read one number.
159+
* Ceiling on one Apple toolchain identity probe attempt, in milliseconds:
160+
* `COLD_TOOLCHAIN_PROBE_TIMEOUT_MS` from `@agent-device/host-kit/command`,
161+
* which owns it for both Apple toolchain probers. Like every other host-kit
162+
* symbol above it arrives through this port rather than by import, because
163+
* `scripts/__tests__/eager-closure-budgets.test.ts` holds the runner entry to
164+
* the modules it evaluates today and a static edge to host-kit adds five.
165165
*/
166166
coldToolchainProbeTimeoutMs(): number;
167167
// Diagnostics (@agent-device/host-kit/diagnostics)

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import path from 'node:path';
22
import {
33
resolveIosSimulatorDeviceSetPath,
4+
type Deadline,
45
emitDiagnostic,
56
isProcessAlive,
67
parseBooleanLiteral,
78
type ExecResult,
89
} from './host.ts';
910
import type { DeviceInfo } from '@agent-device/kernel/device';
11+
import { isRequestCanceledError } from '@agent-device/kernel/errors';
1012
import { sendRunnerCommandOnce } from './runner-transport.ts';
1113
import { withRunnerCommandId } from './runner-contract.ts';
1214
import {
@@ -21,7 +23,6 @@ import {
2123
resolveExpectedRunnerCacheMetadata,
2224
resolveRunnerDerivedPath,
2325
type RunnerCacheProbeBudget,
24-
type RunnerPhaseDeadline,
2526
type RunnerXctestrunArtifact,
2627
} from './runner-xctestrun.ts';
2728
import {
@@ -60,7 +61,9 @@ export async function tryAdoptRunnerSessionFromLease(
6061
* the adopted session must be given what those probes left rather than a
6162
* fresh `startupTimeoutMs` (#2422).
6263
*/
63-
phaseDeadline?: RunnerPhaseDeadline;
64+
phaseDeadline?: Deadline;
65+
/** The owning request's cancellation signal, forwarded to those probes. */
66+
signal?: AbortSignal;
6467
expectedRunnerSessionId?: string;
6568
},
6669
): Promise<RunnerSession | null> {
@@ -101,6 +104,7 @@ export async function tryAdoptRunnerSessionFromLease(
101104
}
102105
const expectedDerived = resolveExpectedDerivedPath(device, {
103106
deadline: options.phaseDeadline,
107+
signal: options.signal,
104108
});
105109
if (!expectedDerived) return skip('expected_derived_unresolved');
106110
if (!lease.xctestrunPath.startsWith(`${expectedDerived}${path.sep}`)) {
@@ -158,7 +162,12 @@ function resolveExpectedDerivedPath(
158162
device,
159163
resolveExpectedRunnerCacheMetadata(device, undefined, budget),
160164
);
161-
} catch {
165+
} catch (error) {
166+
// An unresolvable fingerprint is a miss the caller recovers from by starting
167+
// fresh. A canceled request is not: the client that asked for this startup
168+
// is gone, so it leaves through the catch rather than becoming one more
169+
// reason to keep going.
170+
if (isRequestCanceledError(error)) throw error;
162171
return null;
163172
}
164173
}
@@ -168,7 +177,7 @@ function buildAdoptedRunnerSession(
168177
lease: RunnerLease,
169178
runnerPid: number,
170179
expectedDerived: string,
171-
options: { startupTimeoutMs?: number; phaseDeadline?: RunnerPhaseDeadline },
180+
options: { startupTimeoutMs?: number; phaseDeadline?: Deadline },
172181
): RunnerSession & { lease: RunnerLease } {
173182
const sessionId = lease.sessionId;
174183
const artifact: RunnerXctestrunArtifact = {

‎packages/platform-apple/src/runner/runner-artifact.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import os from 'node:os';
55
import path from 'node:path';
66
import {
77
runCmdStreaming,
8+
type Deadline,
89
type ExecBackgroundResult,
910
withKeyedLock,
1011
emitRequestProgress,
@@ -33,7 +34,6 @@ import {
3334
resolveRunnerSigningBuildSettings,
3435
writeRunnerCacheMetadataForArtifacts,
3536
type ExistingXctestrunState,
36-
type RunnerPhaseDeadline,
3737
type RunnerXctestrunCacheKind,
3838
type RunnerXctestrunCacheMetadata,
3939
} from './runner-cache.ts';
@@ -165,7 +165,7 @@ function resolveExternalXctestDerivedDataPath(xctestrunPath: string): string {
165165
async function ensureXctestrunUnderCacheLock(params: {
166166
device: DeviceInfo;
167167
options: RunnerXctestrunBuildOptions;
168-
phaseDeadline: RunnerPhaseDeadline | undefined;
168+
phaseDeadline: Deadline | undefined;
169169
projectRoot: string;
170170
expectedCacheMetadata: RunnerXctestrunCacheMetadata;
171171
derived: string;
@@ -237,7 +237,7 @@ async function resolveReusableXctestrunArtifact(params: {
237237
async function buildXctestrunArtifact(params: {
238238
device: DeviceInfo;
239239
options: RunnerXctestrunBuildOptions;
240-
phaseDeadline: RunnerPhaseDeadline | undefined;
240+
phaseDeadline: Deadline | undefined;
241241
projectRoot: string;
242242
expectedCacheMetadata: RunnerXctestrunCacheMetadata;
243243
derived: string;

0 commit comments

Comments
 (0)