Skip to content

Commit b6611b6

Browse files
committed
refactor(apple): guard each toolchain probe attempt in one place
Fold the toolchain probe's three duplicated cancellation/budget guard sites (runToolchainProbe's pre-check, runToolchainProbeCommand's retry pre-check, and execToolchainProbeCommand's timeout computation) into one: attemptToolchainProbe checks cancellation and the remaining budget before every exec, first attempt and retry alike. The outer runToolchainProbe now rethrows cancellation and a spent budget instead of swallowing them into a probe failure, and only genuine probe errors become one.
1 parent 47220f6 commit b6611b6

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import crypto from 'node:crypto';
22
import os from 'node:os';
33
import path from 'node:path';
44
import { isMacOs, type DeviceInfo } from '@agent-device/kernel/device';
5-
import { AppError, createRequestCanceledError } from '@agent-device/kernel/errors';
5+
import {
6+
AppError,
7+
createRequestCanceledError,
8+
isRequestCanceledError,
9+
} from '@agent-device/kernel/errors';
610
import {
711
coldToolchainProbeTimeoutMs,
812
createTtlMemo,
@@ -351,19 +355,14 @@ function runToolchainProbe(
351355
clock: ToolchainProbeClock,
352356
): ProbeResult<string> {
353357
const probe = [cmd, ...args].join(' ');
354-
// Both checks are outside the try, and both throw rather than becoming a
355-
// probe failure: a canceled request and a spent budget are the caller's own
356-
// errors to see. Reporting an unreadable toolchain instead would blame the
357-
// toolchain for a probe that never ran.
358-
clock.throwIfCanceled();
359-
if (clock.attemptTimeoutMs() <= 0) {
360-
throw runnerPhaseBudgetExhaustedError('apple_toolchain_probe');
361-
}
362358
let output: { exitCode: number; stdout: string; stderr: string };
363359
try {
364360
output = runToolchainProbeCommand(cmd, args, clock);
365361
} catch (error) {
366-
clock.throwIfCanceled();
362+
// A canceled request and a spent budget are the caller's own errors to
363+
// see, not an unreadable toolchain: only what's left becomes a probe
364+
// failure.
365+
if (isRequestCanceledError(error) || isRunnerPhaseBudgetExhaustedError(error)) throw error;
367366
return probeFailure(probe, 'probe_error', error instanceof Error ? error.message : `${error}`);
368367
}
369368
if (output.exitCode !== 0) {
@@ -385,35 +384,44 @@ function runToolchainProbe(
385384
* reaches this file through the host port); the immediate next exec of the same
386385
* tool is instant, so the retry recovers without widening the per-call budget. Only the
387386
* exec layer's structured timeout is retried -- a tool that failed on its own
388-
* and merely said "timed out" in its output is not this stall.
387+
* and merely said "timed out" in its output is not this stall. The retry calls
388+
* the same guarded attempt below, so a request canceled or a budget spent
389+
* between attempts is caught there rather than trusted from before the first
390+
* one.
389391
*/
390392
function runToolchainProbeCommand(
391393
cmd: string,
392394
args: string[],
393395
clock: ToolchainProbeClock,
394396
): { exitCode: number; stdout: string; stderr: string } {
395397
try {
396-
return execToolchainProbeCommand(cmd, args, clock);
398+
return attemptToolchainProbe(cmd, args, clock);
397399
} catch (error) {
398400
if (!isCommandTimeoutError(error)) throw error;
399-
clock.throwIfCanceled();
400-
if (clock.attemptTimeoutMs() <= 0) throw error;
401-
return execToolchainProbeCommand(cmd, args, clock);
401+
return attemptToolchainProbe(cmd, args, clock);
402402
}
403403
}
404404

405-
function execToolchainProbeCommand(
405+
/** The one guard site for a toolchain probe attempt: cancellation and a spent budget both throw here, before anything execs. */
406+
function attemptToolchainProbe(
406407
cmd: string,
407408
args: string[],
408409
clock: ToolchainProbeClock,
409410
): { exitCode: number; stdout: string; stderr: string } {
411+
clock.throwIfCanceled();
412+
const timeoutMs = clock.attemptTimeoutMs();
413+
if (timeoutMs <= 0) throw runnerPhaseBudgetExhaustedError('apple_toolchain_probe');
410414
return runCmdSync(cmd, args, {
411415
allowFailure: true,
412-
timeoutMs: clock.attemptTimeoutMs(),
416+
timeoutMs,
413417
maxBuffer: TOOLCHAIN_PROBE_MAX_BUFFER,
414418
});
415419
}
416420

421+
function isRunnerPhaseBudgetExhaustedError(error: unknown): boolean {
422+
return error instanceof AppError && error.details?.reason === 'runner_phase_budget_exhausted';
423+
}
424+
417425
function parseXcodeVersionOutput(
418426
output: ProbeResult<string>,
419427
): ProbeResult<{ version: string; buildVersion: string }> {

0 commit comments

Comments
 (0)