fix(ios): budget cold toolchain probes for the first-exec signature stall

Merged
TH
3mo ago
+975-74

Cause

Cold Apple toolchain probes carried per-call budgets sized for a warm toolchain, below the ~18-19 s syspolicyd signature-verification stall that blocks the first xcodebuild/xcrun/large-binary exec after a fresh macOS host boots (the second exec of the same tool is instant):

  • packages/platform-apple/src/snapshot-source/cache-identity.ts: toolOutput ran xcodebuild -version, sw_vers, uname, xcrun --show-sdk-version with timeoutMs: Math.min(10_000, remaining).
  • packages/platform-apple/src/runner/runner-cache-metadata.ts: TOOLCHAIN_PROBE_TIMEOUT_MS = 5_000 for the runner cache key's xcodebuild -version / xcrun --show-sdk-version / xcrun --show-sdk-build-version probes.

Both tripped on cold CI runners even though the surrounding deadlines had room, producing a toolchain-probe timeout unrelated to the change under test (#2422).

Reviewing the first fix surfaced the real defect underneath: these probes block the calling request but were charged to nobody. A build or startup handed the probes a timeout and then handed itself the same number again, so a stalled probe spent the phase's budget twice.

Fix

One owner for the probe budget. COLD_TOOLCHAIN_PROBE_TIMEOUT_MS = 30_000 lives in packages/host-kit/src/internal/exec.ts, beside isCommandTimeoutError, and is exported through @agent-device/host-kit/command — it is a property of exec'ing an Apple tool, not of either prober. cache-identity.ts imports it directly. runner-cache-metadata.ts reads it through the Apple runner host port, like every other host-kit symbol that file uses: a static edge from that file to host-kit adds five modules to the packages/platform-apple/src/runner/index.ts eager closure, which scripts/__tests__/eager-closure-budgets.test.ts holds at 13.

One clock per runner phase. createRunnerPhaseDeadline opens a single Deadline when a build or a startup begins; the cache decision's toolchain probes and the step the phase exists for both read it, and requireRunnerPhaseRemainingMs throws runner_phase_budget_exhausted rather than starting a process it would have to kill at once. The toolchain fingerprint opens its own deadline at whichever ceiling is nearer — the phase's remainder, or TOOLCHAIN_FINGERPRINT_BUDGET_MS (45 s) for a caller with no phase clock — so the three probes and their retries share one budget instead of one each.

Retry exactly once, on the shared remainder. Both probers retry a timed-out attempt once, classified by the exec layer's structured details.timeoutMs rather than by message text, so a tool that failed on its own and merely said "timed out" is not retried.

Cancellation stays typed. A request canceled while a probe blocked surfaces its own cancellation, not the timeout that happened to be in flight: cache-identity.ts raises the snapshot-source cancelled / abort-signal error, runner-cache-metadata.ts raises createRequestCanceledError. runner-adoption.ts forwards the request signal into the fingerprint probes and rethrows a cancellation from resolveExpectedDerivedPath instead of turning it into skip('expected_derived_unresolved') and walking on past a client that is gone.

Budget exhaustion is not a broken toolchain. A probe reached with nothing left throws the same runner_phase_budget_exhausted error the build and startup steps throw, instead of reporting apple_toolchain_probe_unavailable with a "check xcode-select" hint for a toolchain that was never probed.

Tests

  • packages/platform-apple/src/snapshot-source/cache-identity.test.ts (new file): cold-start recovery on retry with the retry charged the remainder; a probe that spends the whole deadline is not retried; a never-returning host retries exactly once; a self-reported "timed out" is not retried; and a request aborted while a probe blocked throws the typed cancelled/abort-signal error after exactly one exec.
  • packages/platform-apple/src/runner/__tests__/runner-cache-metadata.test.ts: the same cases against resolveExpectedRunnerCacheMetadata, plus the shared-budget stop, a 4 s phase capping the single attempt, and cancellation both mid-probe and before the first exec. A fake clock advances only when a probe actually blocks for the timeout it was handed, so a case claiming the budget was spent had to spend it.
  • packages/platform-apple/src/runner/__tests__/runner-artifact-phase-budget.test.ts (new file): the build phase's remaining time after the probes.
  • packages/platform-apple/src/runner/__tests__/runner-adoption.test.ts: an aborted request during the fingerprint probe fails adoption instead of skipping it.
  • packages/host-kit/src/internal/exec.test.ts: isCommandTimeoutError accepts only the exec layer's own structured timeout.

Verification

  • The touched test files, then pnpm check:affected --run: all green.
  • pnpm typecheck, pnpm lint, pnpm check:layering: pass.
  • scripts/__tests__/eager-closure-budgets.test.ts against the merge-base: no closure grows.
  • No simulator/device runs; the change is budget, cancellation and error-shape only.

This unblocks the iOS smoke/preflight lane that was failing on cold runners for #2418, #2420, #2421.

The third site named on #2422 (the fixed 45 s clang compile budget in native-runtime.test.ts) is split out as #2439 and not covered here.

Closes #2422