Skip to content

fix(apple-runner): rebuild the shared runner cache only when a retry also fails - #4880

Merged
thymikee merged 3 commits into
mainfrom
t3/fix-ios-runner-cache-concurrency
Nov 21, 2026
Merged

thymikee merged 3 commits into
mainfrom
t3/fix-ios-runner-cache-concurrency

Conversation

@thymikee

@thymikee thymikee commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #4689. A startup deadline with xcodebuild still running (runner_connect_refused) looks the same for a slow boot on a loaded host and for a corrupt artifact. Prepare used to clean and force-rebuild the shared cache-<key> on the first such failure, which broke concurrent first opens that were reading it. Now the first indictment of a restored artifact retries on the cache. If that retry also fails, prepare runs the one forced rebuild. If no budget is left, prepare cleans the artifact so the next prepare builds a fresh one. A freshly built artifact is never rebuilt. No new timeouts or modes.

Before / after

open --relaunch on a simulator whose restored cache refuses its first connection:

  • Before: clean, forced rebuild on attempt 1.
  • After: ios_runner_prepare_health_retry on attempt 1 with the cache reused, then clean, forced rebuild and ios_runner_prepare_bad_cache_recovered on attempt 2 only.

Validation

At 41ca72cdb7:

  • Device runs (evidence in a PR comment): corrupt cache on iPhone 17 Pro retried, then rebuilt and recovered on attempt 2, exit 0. A single slow boot on iPhone Air was reused on the retry with no clean, exit 0.
  • Regressions for slow boot (no clean), refused twice and refused then a different failure (rebuild) fail at base and pass at head. The refused-then-deadline test guards existing behavior. apple-runner passes 983/983, and pnpm typecheck passes.
  • Remaining risk: concurrent rebuild races, tracked in apple-runner: a cache rebuild must not delete a runner artifact other sessions are reading #4886.

🤖 Generated with Claude Code

…unner cache

A runner startup deadline that expires while xcodebuild is alive is a slow
boot on a loaded host, not evidence of a bad artifact. Classifying it as
artifactSuspect made prepare clean and force-rebuild the shared
cache-<key> that concurrent first opens were reading. Prepare now retries
with a fresh session; only an exhausted endpoint probe indicts the cache.

Fixes #4689

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.48 MB 6.48 MB +182 B
Package (unpacked) 6.48 MB 6.48 MB +182 B
Package (download) 1.97 MB 1.97 MB +67 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 17.4 ms 17.1 ms -0.3 ms
CLI --help 38.1 ms 35.2 ms -2.9 ms

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 8f5a400. The verdict and the full list are in the summary comment.

… fails

A startup deadline with xcodebuild alive looks the same for a slow boot and
for a corrupt artifact, so the classification cannot tell them apart.
Restore runner_connect_refused as artifact-suspect, and let prepare retry once
on the cached artifact before it cleans and rebuilds it. A single slow boot
no longer destroys the shared cache; a corrupt artifact still recovers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee thymikee changed the title fix(apple-runner): a startup deadline no longer rebuilds the shared runner cache fix(apple-runner): rebuild the shared runner cache only when a retry also fails Nov 21, 2026
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on faaa4cd. The verdict and the full list are in the summary comment.

… retry

A restored artifact indicted on the first attempt was rebuilt only when the
retry failed with artifact-suspect evidence again. A retry that failed
differently, or never started because the first attempt spent the prepare
deadline, left the corrupt artifact in place for every later prepare. The
indictment now carries the artifact: any failed retry rebuilds it, and a
spent deadline cleans it so the next prepare builds a fresh one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 41ca72c

Since the review of faaa4cd: 1 new · 0 still open · 1 no longer raised (1 marked fixed)

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
packages/apple-runner/src/runner-lifecycle.ts:134 🆕 One ambiguous refusal still cleans the shared cache when the retry has no budget left

Reviewed with this repository's own Sentinel configuration (c37bb956307c).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 41ca72c. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

The retry-then-rebuild change in 41ca72c reads correctly, but it is not ready to merge until the device run covers this commit. The only live run is at 8f5a400, before the lifecycle logic changed, and it did not reach the changed route. Not verified here: the runner start and app lifecycle behavior at 41ca72c.

Please run open or prepare on an iOS simulator with a restored cache-<key> that refuses connections, for example a corrupted runner bundle in the cached derived data. The diagnostics must show ios_runner_prepare_health_retry on attempt 1 (invalidate prepare_runner_health_retry, no xctestrun clean bad_artifact decision). They must then show the bad_artifact clean, forced_rebuild and ios_runner_prepare_bad_cache_recovered on attempt 2, with exit 0. A slow-boot run where one miss succeeds on retry without a clean decision shows the other half.

Not blocking, and fine to take or leave: on routes without prepareDeadline that carry a request signal (the open prewarm), a truly corrupt artifact now waits for two full startup waits before it is cleaned, and a cancel during attempt 2 drops the suspect without marking it (runner-lifecycle.ts:143). Would a rule at the prepare loop exit, "a loop that exits with a suspect and no rebuild cleans it", cover this and the deadline case at the same time? Also, two concurrent first opens whose retries both fail can delete the artifact the other just rebuilt (runner-cache.ts:146), and one refusal after a spent deadline still cleans the shared cache (runner-lifecycle.ts:140); both are tracked in #4886. The test "a refused restored artifact whose retry runs out of prepare deadline is cleaned" (runner-lifecycle-prepare-artifact.test.ts:150) also passes at base, so the Validation text should say it guards existing behavior.

The two earlier inline threads on the connect-refused classification and the attempt-2 gate are fixed at this head and can be resolved: #4880 (comment) and #4880 (comment). The thread on the deadline cleanup still applies as a non-blocking note: #4880 (comment).

Only Body rules fails in CI, and it is likely unrelated, since the diff touches only apple-runner lifecycle code and a test. The current body counts about 245 words, so the failure probably comes from an earlier body revision. Re-running that job should settle it, and if it still fails the body needs trimming. There are no conflicts. Before merge, the device run above is needed and Body rules must pass. Not verified here: tests and typecheck were not run, the open route's daemon request timeout, and the author's device run at 8f5a400.

@thymikee

Copy link
Copy Markdown
Member Author

Device evidence at 41ca72c, using the worktree CLI (node bin/agent-device.mjs) with an isolated state dir and a contained cache (AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH under .tmp/). Before each run I warmed the cache with prepare ios-runner (cache exact) and ran daemon stop --clean so no runner was left to adopt.

How the cache was corrupted. I set AGENT_DEVICE_RUNNER_BIND_IP=<host LAN IP> in the cached base xctestrun's EnvironmentVariables and updated the manifest's xctestrunDigest and xctestrunSize, so the cache still restores as reuse_ready. The runner then listens on the LAN address while the daemon probes loopback. xcodebuild stays alive and every connection is refused, the same as a corrupt artifact. The route was open com.apple.Preferences --relaunch --debug (session prewarm, so prepareLocalIosRunner with 45 s per attempt and no prepare deadline).

Corrupt cache, iPhone 17 Pro: open exit 0. Request diagnostics:

11:09:08 runner_xctestrun_cache            action=reuse   reason=reuse_ready
11:09:53 ios_runner_session_invalidated    reason=prepare_runner_health_retry
11:09:54 ios_runner_prepare_health_retry   attempt=1 maxAttempts=2 reason="Runner did not accept connection"
11:09:55 runner_xctestrun_cache            action=reuse   reason=reuse_ready        (no clean on attempt 1)
11:10:40 ios_runner_session_invalidated    reason=prepare_cached_runner_health_failed
11:10:40 runner_xctestrun_cache            action=clean   reason="Runner did not accept connection"   (bad_artifact)
11:10:41 runner_xctestrun_cache            action=clean   reason=forced_rebuild
11:10:53 runner_xctestrun_cache            action=build   reason=built_new
11:10:58 ios_runner_prepare_bad_cache_recovered  reason="Runner did not accept connection"
11:10:58 apple_runner_prepare              cache=miss artifact=rebuilt

runner.log shows two test-without-building runs, then build-for-testing and one more test-without-building. The rebuilt xctestrun no longer contains the poisoned key. A following prepare ios-runner exited 0 with a 3 ms health check.

Single slow boot, iPhone Air: same poison, but a watcher restored the original xctestrun and manifest 3 s after attempt 1 wrote its session xctestrun. open exit 0:

11:12:10 runner_xctestrun_cache            action=reuse reason=reuse_ready
11:12:55 ios_runner_session_invalidated    reason=prepare_runner_health_retry
11:12:56 ios_runner_prepare_health_retry   attempt=1 reason="Runner did not accept connection"
11:12:56 runner_xctestrun_cache            action=reuse reason=reuse_ready
11:13:00 apple_runner_prepare              cache=exact artifact=valid recoveryReason="Runner did not accept connection"

No clean decision. The cache was reused on the retry.

Explicit prepare ios-runner: this route hands attempt 1 the whole deadline (see sendRunnerCommandAfterPreflight). With a refusing cache it therefore ends on the spent-deadline branch: the artifact is cleaned and the command fails with prepare_deadline_expired. That is the same outcome as base, so I did not run it separately.

Loop-exit clean (optional note): I'm not taking it. The only exits it would add beyond the deadline branch are a cancel or a closed admission during attempt 2. Neither is evidence about the artifact, and cleaning on them would wipe a shared cache because a caller went away. A corrupt artifact cleared on the next prepare costs one more retry, which is the trade-off #4689 asks for.

All devices were closed and shut down, and the contained caches were removed.

@thymikee

Copy link
Copy Markdown
Member Author

The device evidence at 41ca72c (#4880 (comment)) covers what the review asked for: on a corrupted restored cache, attempt 1 retries without a clean and attempt 2 cleans, force-rebuilds and recovers with exit 0; on a single slow boot, the retry succeeds without a clean. All checks now pass. Nothing from review blocks this head; the non-blocking notes and #4886 stay as follow-ups.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 21, 2026
@thymikee
thymikee merged commit 39da494 into main Nov 21, 2026
21 of 23 checks passed
@thymikee
thymikee deleted the t3/fix-ios-runner-cache-concurrency branch November 21, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iOS: concurrent first opens interfere through a forced rebuild of the shared runner cache

1 participant