fix(apple-runner): trim build scratch from runner cache keys and keep packaging out of the shared cache

Merged
JA
2mo ago
+516-12

Summary

Follow-up to #3246, covering the three items it lists after the eviction PR. This PR is independent of #3247: it touches buildXctestrunArtifact rather than ensureXctestrunArtifact. Merging the two textually conflicts in two places, both trivial: the decision-reason union in runner-cache.ts (each adds one member) and the Apple runner setup row in configuration.md (each adds one env var). Whichever lands second rebases.

1. Trim build scratch from a runner cache key after its build. After the manifest is written, and still under the cache lock, the build removes everything in the key except Build/Products and .agent-device-runner-cache.json: Build/Intermediates.noindex, SDKExplicitPrecompiledModules, ModuleCache.noindex, Logs, CompilationCache.noindex, SDKStatCaches.noindex, SourcePackages, info.plist. Reuse and launch read only the products and the metadata. The kept set is derived from the .xctestrun and its product paths, resolved through realpath: a product outside Build/Products is kept too, a symlinked product keeps its target's directory, and a product outside the key makes the trim a no-op.

  • A set AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH is never trimmed, and neither is a directory that is not a cache-<hash> key. A fixed override path is the development loop that rebuilds into the same tree, and eviction already skips it.
  • A trim failure never fails the runner start, and the module loads lazily, so the eager closure budgets are unchanged.

Trade-off: a trimmed key cannot be built into incrementally. Nothing in the daemon does that today. A source or Xcode change mints a new key (a cold build in either case), a key that fails certification is deleted and rebuilt from scratch (cleanRunnerDerivedArtifacts), and only a key with no metadata is built into, which a trimmed key never is. So the default costs no rebuild time. The only incremental build left is pnpm build:xcuitest:*, which uses its own path and is not trimmed.

2. Legacy ~/.agent-device/ios-runner. Documented, not deleted. Current versions never read it, but an older agent-device installed on the same machine still uses that layout, including leases next to it, and an automatic delete would pull products from under it. commands.md says to rm -rf ~/.agent-device/ios-runner once no old version runs.

3. pnpm build:package / prepack no longer write to the shared cache root. scripts/build-package-xcuitest.mjs (pnpm package:xcuitest, run by build:package) builds ios, macos, tvos and visionos with build-xcuitest-apple.sh into <repo>/.tmp/package-xcuitest/<platform>, and removes that directory before the builds (an interrupted run leaves nothing behind) and after them, also when a build fails. The script is not named build:xcuitest:* because setup-apple-runner-build hashes those script names into the CI runner cache key. Nothing reads those products (the package ships the runner source), so the compile check is unchanged. Packaging now always builds from scratch, so the isolation scan covers every file. pnpm build:xcuitest:<platform> is unchanged. npm users are unaffected: scripts are not in the tarball, and the daemon's keyed cache is not touched.

Validation

Measured on macOS 27 / Xcode 27.0, an iOS 27.0 simulator created for the run, with HOME pointed at a scratch directory so the shared ~/.agent-device was never touched.

Untrimmed keyTrimmed key
Total165.7 MB5.9 MB (-96%)
Build/Products5.8 MB5.8 MB
Build/Intermediates.noindex67.0 MBremoved
SDKExplicitPrecompiledModules76.1 MBremoved
ModuleCache.noindex15.1 MBremoved
  • prepare ios-runner on a fresh home built the key (about 10 s) and trimmed it; the runner then launched from the trimmed key and open + snapshot -i returned the Settings tree through the xctest backend. The trim itself took 66 ms for 165 MB.
  • After stopping the daemon and the runner, a second prepare ios-runner logged reuse_ready and no new built_new: no rebuild.
  • After appending a line to a runner Swift source, prepare ios-runner built a second key (about 8 s, trimmed to 5.9 MB) and left the first in place.
  • For scale: build-xcuitest-apple.sh in one directory takes 8.0 s cold and 3.6 s incrementally, which is what an in-place rebuild would save, and the daemon never takes that path.
  • buildPackageXcuitest({ platforms: ['ios'] }) built into .tmp/package-xcuitest, removed it, and left the shared derived/ untouched.

Checks: pnpm check:affected --run (the full set, because package.json is workflow tooling) passes, including check:fallow --base origin/main, the eager-closure and package-closure tests, and all of packages/platform-apple/src/runner. src/__tests__/npm-package-scripts.test.ts now expects package:xcuitest in build:package. New tests cover the kept and removed entries, a build under a derived path override that keeps its scratch, a product outside the key, a symlinked product, a real ensureXctestrunArtifact build (stubbed xcodebuild) followed by a reuse with no second build, and the packaging script's scratch cleanup on success, failure and after an interrupted run.

Not run: macOS, tvOS and visionOS runners and a physical device from a trimmed key (the trim walks the same product paths for every platform, and the macOS product repair reads only those paths); the packaging script for macos, tvos and visionos; pnpm package:npm end to end.