@@ -32,21 +32,12 @@ const ANDROID_ADB_DEVICE_OFFLINE_FAILURE = {
3232 retriable : true ,
3333} as const satisfies AndroidAdbFailureClassification ;
3434
35- /** The entire stderr, lowercased, of a command the host adb refused because the device was offline. */
36- const ADB_DEVICE_OFFLINE_HOST_REFUSAL = / ^ (?: a d b | e r r o r ) : d e v i c e (?: ' [ ^ ' ] * ' ) ? o f f l i n e $ / ;
37-
38- const ANDROID_ADB_DEVICE_OFFLINE_HOST_REFUSAL : AndroidAdbFailureClassification = Object . freeze ( {
39- ...ANDROID_ADB_DEVICE_OFFLINE_FAILURE ,
40- hostRefusal : true ,
41- } ) ;
42-
4335const ANDROID_ADB_FAILURE_MATCHERS = [
4436 [
4537 / d e v i c e u n a u t h o r i z e d | d e v i c e s t i l l a u t h o r i z i n g / ,
4638 {
4739 reason : 'device_unauthorized' ,
4840 hint : 'USB debugging is not authorized — accept the authorization prompt on the device screen (re-plug the cable if none appears), then retry.' ,
49- hostRefusal : true ,
5041 } ,
5142 ] ,
5243 [ / d e v i c e o f f l i n e / , ANDROID_ADB_DEVICE_OFFLINE_FAILURE ] ,
@@ -55,23 +46,20 @@ const ANDROID_ADB_FAILURE_MATCHERS = [
5546 {
5647 reason : 'multiple_devices' ,
5748 hint : 'Multiple Android devices are connected — pass --serial <serial> (see adb devices) to select one.' ,
58- hostRefusal : true ,
5949 } ,
6050 ] ,
6151 [
6252 / n o d e v i c e s \/ e m u l a t o r s f o u n d | n o d e v i c e s f o u n d / ,
6353 {
6454 reason : 'no_devices' ,
6555 hint : 'No Android devices detected — boot an emulator or connect a device and verify it appears in adb devices.' ,
66- hostRefusal : true ,
6756 } ,
6857 ] ,
6958 [
7059 / d e v i c e (?: ' [ ^ ' ] * ' ) ? n o t f o u n d / ,
7160 {
7261 reason : 'device_not_found' ,
7362 hint : 'The device disconnected or is restarting — verify it is listed in adb devices, then retry.' ,
74- hostRefusal : true ,
7563 retriable : true ,
7664 } ,
7765 ] ,
@@ -80,7 +68,6 @@ const ANDROID_ADB_FAILURE_MATCHERS = [
8068 {
8169 reason : 'server_version_mismatch' ,
8270 hint : 'Multiple adb installs conflict — adb restarts its server automatically, so retry; align PATH to a single adb to stop recurrences.' ,
83- hostRefusal : true ,
8471 retriable : true ,
8572 } ,
8673 ] ,
@@ -134,15 +121,59 @@ const ANDROID_ADB_TIMEOUT_FAILURE: AndroidAdbFailureClassification = Object.free
134121const ANDROID_HELPER_INSTALL_TIMEOUT_HINT =
135122 'The helper install timed out — some OEM builds (ColorOS is one) hold the first install of each package behind a system install-confirmation dialog and adb waits for it. Check the device screen, confirm any pending install prompt, then retry; if none is showing, run adb kill-server && adb start-server and retry.' ;
136123
124+ type HostRefusalShape = readonly [ head : RegExp , reason : AndroidAdbFailureReason ] ;
125+
126+ const HOST_REFUSAL_PREFIX = String . raw `(?:adb|error): ` ;
127+
128+ /** First stderr line, lowercased, of each refusal the host adb issues before the command leaves the host. */
129+ const HOST_REFUSAL_SHAPES : readonly HostRefusalShape [ ] = [
130+ [
131+ new RegExp ( `^${ HOST_REFUSAL_PREFIX } device (?:unauthorized|still authorizing)\\.?$` ) ,
132+ 'device_unauthorized' ,
133+ ] ,
134+ [ new RegExp ( `^${ HOST_REFUSAL_PREFIX } device (?:'[^']*' )?offline$` ) , 'device_offline' ] ,
135+ [
136+ new RegExp ( `^${ HOST_REFUSAL_PREFIX } more than one (?:device/emulator|device and emulator)$` ) ,
137+ 'multiple_devices' ,
138+ ] ,
139+ [ new RegExp ( `^${ HOST_REFUSAL_PREFIX } no devices(?:/emulators)? found$` ) , 'no_devices' ] ,
140+ [ new RegExp ( `^${ HOST_REFUSAL_PREFIX } device (?:'[^']*' )?not found$` ) , 'device_not_found' ] ,
141+ ] ;
142+
143+ /** The fixed trailer lines adb appends to a device_unauthorized refusal. */
144+ const HOST_REFUSAL_TRAILER =
145+ / ^ (?: t h i s a d b s e r v e r ' s \$ a d b _ v e n d o r _ k e y s i s n o t s e t | t r y ' a d b k i l l - s e r v e r ' i f t h a t s e e m s w r o n g \. | (?: o t h e r w i s e | p l e a s e ) c h e c k (?: f o r | t h e ) (?: a ) ? c o n f i r m a t i o n d i a l o g o n y o u r d e v i c e \. ) $ / ;
146+
147+ const ANDROID_ADB_FAILURES_BY_REASON = new Map <
148+ AndroidAdbFailureReason ,
149+ AndroidAdbFailureClassification
150+ > ( ANDROID_ADB_FAILURE_MATCHERS . map ( ( [ , failure ] ) => [ failure . reason , failure ] ) ) ;
151+
152+ function classifyHostRefusal (
153+ stderrText : string ,
154+ stdoutText : string ,
155+ ) : AndroidAdbFailureClassification | undefined {
156+ if ( stdoutText !== '' ) return undefined ;
157+ const [ head , ...trailer ] = stderrText
158+ . split ( '\n' )
159+ . map ( ( line ) => line . trim ( ) )
160+ . filter ( ( line ) => line !== '' ) ;
161+ if ( head === undefined || ! trailer . every ( ( line ) => HOST_REFUSAL_TRAILER . test ( line ) ) ) {
162+ return undefined ;
163+ }
164+ const reason = HOST_REFUSAL_SHAPES . find ( ( [ shape ] ) => shape . test ( head ) ) ?. [ 1 ] ;
165+ const failure = reason && ANDROID_ADB_FAILURES_BY_REASON . get ( reason ) ;
166+ return failure && { ...failure , hostRefusal : true } ;
167+ }
168+
137169export function classifyAndroidAdbFailure (
138170 stderr : string ,
139171 stdout = '' ,
140172) : AndroidAdbFailureClassification | undefined {
141173 const stderrText = stderr . toLowerCase ( ) ;
142174 const stdoutText = stdout . toLowerCase ( ) ;
143- if ( stdoutText === '' && ADB_DEVICE_OFFLINE_HOST_REFUSAL . test ( stderrText . trim ( ) ) ) {
144- return ANDROID_ADB_DEVICE_OFFLINE_HOST_REFUSAL ;
145- }
175+ const refusal = classifyHostRefusal ( stderrText , stdoutText ) ;
176+ if ( refusal ) return refusal ;
146177 for ( const [ pattern , classification , matchStdout ] of ANDROID_ADB_FAILURE_MATCHERS ) {
147178 if ( pattern . test ( stderrText ) || ( matchStdout && pattern . test ( stdoutText ) ) ) {
148179 return classification ;
0 commit comments