1- import { retryWithPolicy , emitDiagnostic } from './host.ts' ;
1+ import { retryWithPolicy , emitDiagnostic , getRequestSignal , isRequestCanceled } from './host.ts' ;
22import { isIosFamily , type DeviceInfo } from '@agent-device/kernel/device' ;
3+ import { isRequestCanceledError } from '@agent-device/kernel/errors' ;
34import {
45 ensureRunnerSession ,
56 readRunnerSessionLiveness ,
@@ -36,22 +37,38 @@ import { RUNNER_COMMAND_TIMEOUT_MS } from './runner-transport.ts';
3637/**
3738 * Attempts a read-only command may spend on one error class; 1 means no resend. A `RUNNER_BUSY`
3839 * refusal is the runner refusing fast on purpose while it drains abandoned XCTest work (#1105), so
39- * that budget has to outlast the drain. The window is a heuristic, not a measurement: with 200ms
40- * doubling to a 1s cap it spans roughly 4 to 6.5s of delay across eight attempts before jitter and
41- * round trips, and a drain that outlives it still surfaces as `RUNNER_BUSY` (the runner's own
42- * `abandonedForSeconds=` marker in runner.log is the evidence for tuning it). Transport failures
43- * keep the pre-existing three attempts. The budget is positional: the error on each attempt sets
44- * how many attempts the loop may reach in total, so three busy refusals followed by a transport
45- * failure resend no further.
40+ * that budget has to outlast the drain. The window is a heuristic, not a measurement: 200ms doubling
41+ * to a 1s cap, no jitter, is 5.4s of delay across eight attempts before round trips, and a drain
42+ * that outlives it still surfaces as `RUNNER_BUSY` (the runner's own `abandonedForSeconds=` marker
43+ * in runner.log is the evidence for tuning it). Transport failures keep the pre-existing three
44+ * attempts. The budget is positional: the error on each attempt sets how many attempts the loop may
45+ * reach in total, so three busy refusals followed by a transport failure resend no further.
4646 */
4747const RUNNER_BUSY_RESEND_ATTEMPTS = 8 ;
4848const TRANSPORT_RESEND_ATTEMPTS = 3 ;
49+ const READ_ONLY_RESEND_POLICY = {
50+ maxAttempts : RUNNER_BUSY_RESEND_ATTEMPTS ,
51+ baseDelayMs : 200 ,
52+ maxDelayMs : 1_000 ,
53+ jitter : 0 ,
54+ } ;
4955
5056function readOnlyResendBudget ( error : unknown ) : number {
5157 if ( isRunnerBusyError ( error ) ) return RUNNER_BUSY_RESEND_ATTEMPTS ;
5258 return isRetryableRunnerError ( error ) ? TRANSPORT_RESEND_ATTEMPTS : 1 ;
5359}
5460
61+ /**
62+ * Whether the request itself was cancelled, as opposed to one caller's deadline on this command.
63+ * A `wait` bounds each poll with its own abort signal; that deadline landing inside the resend
64+ * window must not erase the refusal the runner already gave.
65+ */
66+ function isRunnerRequestCancelled ( options : AppleRunnerCommandOptions ) : boolean {
67+ return (
68+ isRequestCanceled ( options . requestId ) || getRequestSignal ( options . requestId ) ?. aborted === true
69+ ) ;
70+ }
71+
5572export async function runAppleRunnerCommand (
5673 device : DeviceInfo ,
5774 command : RunnerCommand ,
@@ -61,27 +78,37 @@ export async function runAppleRunnerCommand(
6178 assertRunnerRequestActive ( options . requestId ) ;
6279 const runnerCommand = withRunnerCommandId ( command ) ;
6380 const provider = resolveAppleRunnerRuntime ( device , options ) ;
64- if ( isReadOnlyRunnerCommand ( runnerCommand ) ) {
65- return retryWithPolicy (
81+ if ( ! isReadOnlyRunnerCommand ( runnerCommand ) ) {
82+ return provider . runCommand ( device , runnerCommand , options ) ;
83+ }
84+ let lastBusyRefusal : unknown ;
85+ try {
86+ return await retryWithPolicy (
6687 ( ) => {
6788 assertRunnerRequestActive ( options . requestId ) ;
6889 return provider . runCommand ( device , runnerCommand , options ) ;
6990 } ,
7091 {
71- maxAttempts : RUNNER_BUSY_RESEND_ATTEMPTS ,
72- baseDelayMs : 200 ,
73- maxDelayMs : 1_000 ,
92+ ...READ_ONLY_RESEND_POLICY ,
7493 shouldRetry : ( error , attempt ) => {
7594 assertRunnerRequestActive ( options . requestId ) ;
95+ if ( isRunnerBusyError ( error ) ) lastBusyRefusal = error ;
7696 return attempt < readOnlyResendBudget ( error ) ;
7797 } ,
7898 } ,
79- // The busy window is seconds long, so a cancelled request must wake the delay instead of
80- // sleeping it out and sending one more attempt.
99+ // The busy window is seconds long, so an abort must wake the delay instead of sleeping it
100+ // out and sending one more attempt.
81101 { signal : resolveRunnerRequestSignal ( options ) } ,
82102 ) ;
103+ } catch ( error ) {
104+ // A caller's deadline (a `wait` poll bounding this capture) that lands mid-window still has an
105+ // answer: the runner refused, and that typed refusal is what the caller can act on. Only a
106+ // cancelled request reports as a bare cancellation.
107+ if ( isRequestCanceledError ( error ) && lastBusyRefusal && ! isRunnerRequestCancelled ( options ) ) {
108+ throw lastBusyRefusal ;
109+ }
110+ throw error ;
83111 }
84- return provider . runCommand ( device , runnerCommand , options ) ;
85112}
86113
87114export async function notifyIosRunnerAppRelaunched (
0 commit comments