fix(apple-runner): evict stale runner cache keys after a build
Summary
Runner cache keys under ~/.agent-device/apple-runner/derived/<platform>/cache-<hash> are never removed. Any runner source or Xcode change mints a new 150-230 MB key and the old one can never match again; one Mac reached 3.45 GB across 20 keys. Details and measurements are in #3246.
After a build (the only event that adds a key), the sweep removes sibling keys in the same platform folder unless they are:
- the key just built, or among the 3 most recently used (new key included) by
.agent-device-runner-cache.jsonmtime, which every reuse rewrites; - used within the last day, re-checked under the lock, which covers a runner that resolved its key but has not written its lease yet;
- held by a build (
cache-<hash>.lock, acquired without waiting); - named, by xctestrun path or cache key, by a live runner lease (owner live or unknown, or the leased runner process still running).
AGENT_DEVICE_IOS_RUNNER_CACHE_KEEP=<n> sets the count, 0 disables. A set AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH is never swept. The sweep runs off the start path, is best-effort, and loads lazily so the eager closure budgets are unchanged.
Not in this PR: trimming build scratch inside a kept key (about 97% of each key), and the un-keyed pnpm build:package output. Both are listed in #3246.
Closes #3246
Validation
Tested at 0542d32. pnpm check:affected --run passed, plus pnpm check:fallow --base origin/main, the eager-closure and package-closure tests, and all of packages/platform-apple/src/runner. New tests cover keep count, the one-day floor, stubs, non-key entries, a held lock, live, dead and handed-off leases, 0, the path override, and the sweep after a real ensureXctestrunArtifact build with a stubbed xcodebuild.
I ran the sweep against a metadata-only copy of a real 20-key cache and its 44 leases. It kept 3 keys per platform and would free about 2.1 GB of 3.45 GB (1.5 GB iOS simulator, 0.6 GB macOS).
Not run: a live simulator build through the hook (it would delete other agents' keys in the shared cache home), and a physical device.