Skip to content

Commit df909a1

Browse files
committed
fix(android): mark a host refusal only when the whole adb output is the refusal
1 parent 59abf2c commit df909a1

4 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎contracts/fixtures/dispatch-disclosure.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@
344344
"dispatched": "no"
345345
},
346346
{
347-
"id": "android-adb.input-tap.host-refused.server-version-mismatch",
347+
"id": "android-adb.input-tap.refusal-text-beside-other-output",
348348
"producer": "android-adb",
349-
"trigger": "adb input tap refused by the host adb (server_version_mismatch) before it reached the device; the classifier's hostRefusal verdict, stamped as details.adbHostRefusal, makes it no",
350-
"dispatched": "no"
349+
"trigger": "adb input tap failed with refusal-like stderr that is not the whole output (server restart line, then a device-side exit); only a refusal that is the entire output makes it no",
350+
"dispatched": "unknown"
351351
},
352352
{
353353
"id": "android-adb.input-tap.double-tap-second-refused",

‎packages/platform-android/src/__tests__/dispatch-disclosure.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ const HOST_REFUSAL_STDERR: Record<string, string> = {
191191
'multiple-devices': 'error: more than one device/emulator',
192192
'no-devices': 'adb: no devices/emulators found',
193193
'device-not-found': "error: device 'emulator-5554' not found",
194-
'server-version-mismatch': "adb server version (40) doesn't match this client (41); killing...",
195194
};
196195

197196
const DRIVERS: Record<string, { drive: () => Promise<unknown>; dispatchedSteps?: number }> = {
@@ -201,6 +200,13 @@ const DRIVERS: Record<string, { drive: () => Promise<unknown>; dispatchedSteps?:
201200
'android-adb.input-tap.failed': {
202201
drive: () => tapWithAdbAnswer({ exitCode: 1, stderr: 'Killed' }),
203202
},
203+
'android-adb.input-tap.refusal-text-beside-other-output': {
204+
drive: () =>
205+
tapWithAdbAnswer({
206+
exitCode: 137,
207+
stderr: "adb server version (40) doesn't match this client (41); killing...\nKilled",
208+
}),
209+
},
204210
...Object.fromEntries(
205211
Object.entries(HOST_REFUSAL_STDERR).map(([name, stderr]) => [
206212
`android-adb.input-tap.host-refused.${name}`,

‎packages/platform-android/src/adb-failure.test.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ const HOST_REFUSALS = [
6161
['multiple_devices', 'error: more than one device/emulator'],
6262
['no_devices', 'adb: no devices/emulators found'],
6363
['device_not_found', "error: device 'emulator-5554' not found"],
64-
['server_version_mismatch', "adb server version (40) doesn't match this client (41); killing..."],
6564
] as const;
6665

6766
const DEVICE_SIDE_FAILURES = [
@@ -84,6 +83,29 @@ test('every host-refusal reason is classified hostRefusal and stamped as adbHost
8483
}
8584
});
8685

86+
test('refusal text never proves a refusal unless it is the entire adb output', () => {
87+
for (const [reason, refusal] of HOST_REFUSALS) {
88+
for (const [stderr, stdout] of [
89+
[refusal, 'partial'],
90+
[`${refusal}\nKilled`, ''],
91+
[`adb server version (40) doesn't match this client (41); killing...\n${refusal}`, ''],
92+
[`${refusal}, giving up`, ''],
93+
] as const) {
94+
const failure = classifyAndroidAdbFailure(stderr, stdout);
95+
assert.equal(failure?.hostRefusal, undefined, `${reason}: ${stderr} | ${stdout}`);
96+
const error = attachAdbFailureHint(
97+
new AppError('COMMAND_FAILED', 'adb exited with code 1', { stderr, stdout }),
98+
);
99+
assert.equal(Object.hasOwn(error.details ?? {}, 'adbHostRefusal'), false, reason);
100+
}
101+
}
102+
const mismatch = classifyAndroidAdbFailure(
103+
"adb server version (40) doesn't match this client (41); killing...",
104+
);
105+
assert.equal(mismatch?.reason, 'server_version_mismatch');
106+
assert.equal(mismatch?.hostRefusal, undefined);
107+
});
108+
87109
test('reasons a device-side command can produce are never classified hostRefusal', () => {
88110
for (const [reason, stderr, stdout] of DEVICE_SIDE_FAILURES) {
89111
const failure = classifyAndroidAdbFailure(stderr, stdout);

‎packages/platform-android/src/adb-failure.ts‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = /^(?:adb|error): device (?:'[^']*' )?offline$/;
37-
38-
const ANDROID_ADB_DEVICE_OFFLINE_HOST_REFUSAL: AndroidAdbFailureClassification = Object.freeze({
39-
...ANDROID_ADB_DEVICE_OFFLINE_FAILURE,
40-
hostRefusal: true,
41-
});
42-
4335
const ANDROID_ADB_FAILURE_MATCHERS = [
4436
[
4537
/device unauthorized|device still authorizing/,
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
[/device offline/, 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
/no devices\/emulators found|no devices found/,
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
/device (?:'[^']*' )?not found/,
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
134121
const 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+
/^(?:this adb server's \$adb_vendor_keys is not set|try 'adb kill-server' if that seems wrong\.|(?:otherwise|please) check (?:for|the) (?:a )?confirmation dialog on your device\.)$/;
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+
137169
export 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

Comments
 (0)