66 deviceShellExecutableOf ,
77 type ShellWord ,
88} from '@agent-device/kernel/device-shell' ;
9+ import { AppError } from '@agent-device/kernel/errors' ;
910import {
1011 androidAdbInvocation ,
1112 androidAdbPayloadWithoutSerial ,
@@ -35,7 +36,7 @@ import {
3536 type AndroidAdbCommandExecutorOverride ,
3637 type AndroidAdbHostTransport ,
3738} from './adb-host.ts' ;
38- import { isAndroidAdbDeviceOffline , withAdbFailureHints } from './adb-failure.ts' ;
39+ import { isAndroidAdbDeviceOfflineRefusal , withAdbFailureHints } from './adb-failure.ts' ;
3940import { createExecAndroidPortReverseProvider } from './adb-port-reverse.ts' ;
4041import { normalizeAndroidAdbProvider } from './adb-provider-normalization.ts' ;
4142
@@ -73,14 +74,16 @@ function createSerialAdbExecutor(serial: string, serverPort?: number): AndroidAd
7374 ) ;
7475 } ;
7576 return await retryOnceAfterDeviceOffline (
76- serial ,
77+ ` ${ options ?. serverPort ?? serverPort ?? '' } / ${ serial } ` ,
7778 options ,
7879 async ( attemptOptions ) => await exec ( args , attemptOptions ) ,
7980 async ( timeoutMs ) => {
8081 await exec ( [ 'wait-for-device' ] , {
8182 allowFailure : true ,
8283 timeoutMs,
8384 signal : options ?. signal ,
85+ env : options ?. env ,
86+ serverPort : options ?. serverPort ,
8487 } ) . catch ( ( ) => undefined ) ;
8588 } ,
8689 ) ;
@@ -96,60 +99,63 @@ const ANDROID_DEVICE_OFFLINE_WAIT_MS = 15_000;
9699/** How long a device that stayed offline through a wait has its refusals surface without another. */
97100const ANDROID_DEVICE_STAYED_OFFLINE_MS = 30_000 ;
98101
99- /** Serials that stayed offline through a wait, mapped to when a refusal may wait again. */
102+ /** Devices (keyed by adb server and serial) that stayed offline through a wait, mapped to when a refusal may wait again. */
100103const devicesStayingOffline = new Map < string , number > ( ) ;
101104
102105/**
103- * Runs `run`, and once more after `waitForDevice` when adb refused it as `device offline`. The
104- * refusal comes from the host adb before the command reaches the device, so the retry cannot
105- * repeat an effect. The wait and the retry share the caller's `timeoutMs`, the wait taking at most
106- * half. A failed wait is not reported: the retry's outcome is the device's state. A device that
107- * stayed offline through its wait fails fast for a while instead of making every call wait.
106+ * Runs `run`, and once more after `waitForDevice` when the host adb refused it as `device offline`.
107+ * The refusal comes before the command reaches the device, so the retry cannot repeat an effect.
108+ * The wait and the retry stay inside the caller's `timeoutMs`, the wait taking at most half of what
109+ * is left; with no budget left the refusal stands. A failed wait is not reported: the retry's
110+ * outcome is the device's state. A device that stayed offline through its wait fails fast for a
111+ * while instead of making every call wait.
108112 */
109113async function retryOnceAfterDeviceOffline (
110- serial : string ,
114+ device : string ,
111115 options : AndroidAdbExecutorOptions | undefined ,
112116 run : ( options : AndroidAdbExecutorOptions | undefined ) => Promise < AndroidAdbExecutorResult > ,
113117 waitForDevice : ( timeoutMs : number ) => Promise < void > ,
114118) : Promise < AndroidAdbExecutorResult > {
115119 const startedAt = Date . now ( ) ;
116- const first = await attemptAdb ( serial , async ( ) => await run ( options ) ) ;
117- if ( ! first . offline || ( devicesStayingOffline . get ( serial ) ?? 0 ) > Date . now ( ) ) {
120+ const first = await attemptAdb ( device , async ( ) => await run ( options ) ) ;
121+ if ( ! first . offline || ( devicesStayingOffline . get ( device ) ?? 0 ) > Date . now ( ) ) {
118122 return first . outcome ( ) ;
119123 }
120124 const budgetMs = options ?. timeoutMs ;
121- await waitForDevice (
122- budgetMs === undefined
123- ? ANDROID_DEVICE_OFFLINE_WAIT_MS
124- : Math . min ( ANDROID_DEVICE_OFFLINE_WAIT_MS , Math . floor ( budgetMs / 2 ) ) ,
125- ) ;
125+ const remainingMs = ( ) =>
126+ budgetMs === undefined ? Infinity : Math . floor ( budgetMs - ( Date . now ( ) - startedAt ) ) ;
127+ const waitMs = Math . min ( ANDROID_DEVICE_OFFLINE_WAIT_MS , Math . floor ( remainingMs ( ) / 2 ) ) ;
128+ if ( waitMs < 1 ) return first . outcome ( ) ;
129+ await waitForDevice ( waitMs ) ;
126130 options ?. signal ?. throwIfAborted ( ) ;
127- const retryOptions =
128- budgetMs === undefined
129- ? options
130- : { ...options , timeoutMs : Math . max ( 1 , budgetMs - ( Date . now ( ) - startedAt ) ) } ;
131- const retry = await attemptAdb ( serial , async ( ) => await run ( retryOptions ) ) ;
132- if ( retry . offline )
133- devicesStayingOffline . set ( serial , Date . now ( ) + ANDROID_DEVICE_STAYED_OFFLINE_MS ) ;
131+ const retryMs = remainingMs ( ) ;
132+ if ( retryMs < 1 ) return first . outcome ( ) ;
133+ const retry = await attemptAdb (
134+ device ,
135+ async ( ) => await run ( budgetMs === undefined ? options : { ...options , timeoutMs : retryMs } ) ,
136+ ) ;
137+ if ( retry . offline ) {
138+ devicesStayingOffline . set ( device , Date . now ( ) + ANDROID_DEVICE_STAYED_OFFLINE_MS ) ;
139+ }
134140 return retry . outcome ( ) ;
135141}
136142
137143/**
138- * Runs one adb attempt and reports whether adb refused it as `device offline`. Any other outcome
139- * means the device answered, so it forgets that the device stayed offline.
144+ * Runs one adb attempt and reports whether the host adb refused it as `device offline`. An outcome
145+ * the device answered forgets that the device stayed offline; a timeout or cancellation does not .
140146 */
141147async function attemptAdb (
142- serial : string ,
148+ device : string ,
143149 run : ( ) => Promise < AndroidAdbExecutorResult > ,
144150) : Promise < { offline : boolean ; outcome : ( ) => AndroidAdbExecutorResult } > {
145151 try {
146152 const result = await run ( ) ;
147- const offline = result . exitCode !== 0 && isAndroidAdbDeviceOffline ( result ) ;
148- if ( ! offline ) devicesStayingOffline . delete ( serial ) ;
153+ const offline = result . exitCode !== 0 && isAndroidAdbDeviceOfflineRefusal ( result ) ;
154+ if ( ! offline ) devicesStayingOffline . delete ( device ) ;
149155 return { offline, outcome : ( ) => result } ;
150156 } catch ( error ) {
151- const offline = isAndroidAdbDeviceOffline ( error ) ;
152- if ( ! offline ) devicesStayingOffline . delete ( serial ) ;
157+ const offline = isAndroidAdbDeviceOfflineRefusal ( error ) ;
158+ if ( ! offline && deviceAnswered ( error ) ) devicesStayingOffline . delete ( device ) ;
153159 return {
154160 offline,
155161 outcome : ( ) => {
@@ -159,6 +165,13 @@ async function attemptAdb(
159165 }
160166}
161167
168+ /** Whether a thrown adb failure is an exit the device reached, not a timeout or cancellation. */
169+ function deviceAnswered ( error : unknown ) : boolean {
170+ if ( ! ( error instanceof AppError ) || error . details ?. timeoutMs !== undefined ) return false ;
171+ const exitCode = error . details ?. exitCode ;
172+ return typeof exitCode === 'number' && exitCode >= 0 ;
173+ }
174+
162175function createSerialAdbSpawner ( serial : string , serverPort ?: number ) : AndroidAdbSpawner {
163176 return guardDeviceShellSpawn ( ( args , options ) => {
164177 const request = deviceAdbRouteRequest ( serial , serverPort , args , options ) ;
0 commit comments