fix(ios-runner): resend read-only commands across the RUNNER_BUSY drain window

Merged
OK
2mo ago
+373-18

Summary

Running the Bluesky agentic e2e suite on one simulator (agent-device 0.21.6, iOS 26.2) produced 752 RUNNER_BUSY answers over two days, 723 of them to snapshot. The shape is always the same: a snapshot on a keyboard-up screen has its snapshot_viewport read abandoned after the 1s slice, recovers through private AX and returns ok=1, and the very next snapshot is refused while the abandoned block still drains. Verbatim runner.log:

15:03:47.211 MAIN_THREAD_WORK_ABANDONED operation=snapshot_viewport slice=1.0
15:03:47.528 SNAPSHOT_RECOVERED backend=private-ax ... COMPLETED ok=1
15:03:47.693 RUNNER_BUSY command=snapshot abandonedForSeconds=0.5
15:03:47.921 RUNNER_BUSY command=snapshot abandonedForSeconds=0.7   (daemon retry 1)
15:03:48.334 RUNNER_BUSY command=snapshot abandonedForSeconds=1.1   (daemon retry 2, client gets the error)
15:03:48.395 ABANDONED_WORK_DRAINED

The runner refuses fast on purpose (#1105) and expects the daemon to back off. The daemon's read-only resend in runAppleRunnerCommand used the default retryWithPolicy: three attempts at 200/400ms, about 0.6s total. Across 88 abandonment episodes the drain took 1.2s median, so the resend gave up right before the runner was answering again.

Change

  • runner-client.ts: read-only resends keep retrying a RUNNER_BUSY refusal across eight attempts (200, 400, 800, then 1000ms, no jitter: 5.4s of delay). Transport failures keep the previous three attempts. Both budgets are stated in one place (readOnlyResendBudget). The wait takes the request abort signal, and a caller deadline (a wait poll bounding the capture) that lands mid-window rethrows the last busy refusal so the wait keeps the runner's own code; only a cancelled request reports as a cancellation.
  • runner-error-classification.ts: RUNNER_BUSY is a row in RUNNER_ERROR_RULES (runner_busy_refusal, drainResend verdict) read by isRunnerBusyError. MAIN_THREAD_TIMEOUT is excluded on purpose, that command already spent its wait. isStructuredRunnerFailure moved here from runner-session.ts.
  • runner-lifecycle.ts: a structured runner reply no longer triggers a lifecycle status probe. The reply already says whether the command ran, so a busy refusal is rethrown to the caller's resend policy instead of paying a status round trip per attempt. This applies to mutating commands as well: they meet the refusal once, with no probe and no resend (pinned by a test).

Related: #2783 covers the runner side of the same symptom (query sweep outliving its slice).

Validation

  • runner-command-busy-resend.test.ts replays the logged sequence: three RUNNER_BUSY answers spaced 200/400/800ms, then success. Fails on main with The iOS runner is still finishing a previous command, passes here. It also pins that refusals outlasting the window still fail with RUNNER_BUSY, and that a request abort wakes the delay.
  • The transport resend budget stays at three attempts. isRunnerBusyError has direct negatives for MAIN_THREAD_TIMEOUT and message-only busy errors.
  • vitest --project apple-runner: 57 files, 603 tests pass. tsc -b packages/platform-apple, oxlint, oxfmt, check:layering clean. check:production-exports reports 69 preexisting unused exports, none in touched files.