fix(adb): list a transport adb reports without a serial number (#10187)

* fix(adb): list a transport adb reports without a serial number

AOSP `transport.cpp append_transport()` substitutes the literal `(no serial number)` in the serial
column when the transport has none to report, which is reachable: `client/usb_linux.cpp` keeps an
empty serial for a device that cannot report one during early bringup (`http://b/20883914`) and
still registers the transport. The long listing pads that column with `%-22s` and the short one
separates it with a tab, so the placeholder carries spaces either way — and `DEVICE_LINE` required
a single `\S+` token.

The consequence was larger than one lost line. `AdbDeviceOperations` rejects any unparsed line
outright, so a single serial-less transport failed the whole `list_adb_devices` call:

| listing | before | after |
| --- | --- | --- |
| two placeholder transports | 0 devices, 2 unparsed | 2 devices, 0 unparsed |
| placeholder in the short form | 0 devices, 1 unparsed | 1 device, 0 unparsed |
| two healthy devices plus one placeholder | 2 devices **and 1 unparsed**, so the call fails and all three are lost | 3 devices, 0 unparsed |

The state, transport identity, USB metadata and inferred kind parse exactly as before; only the
serial column admits the placeholder. `src/adb` — 40 passing, 3 new cases; restoring `\S+` fails
all three.

* fix(adb): model the missing serial instead of the display placeholder

A review finding: the placeholder `(no serial number)` is formatter text, not an identity. Two
transports without a serial became indistinguishable once it was stored as one, and the text cannot
be passed to any device operation, because `deviceSerialSchema` rejects whitespace while the schema
describes the serial as "Device serial exactly as `adb devices` reports it".

The observation now carries `serial: null` with `serial_status: "not_reported"`, which is the shape
this module already uses for a transport ID it was not given (`transport_identity.status` and
`transport_id_status`). The reported value is unchanged for every transport that has a serial, and
`transport_id` still identifies a transport that has none.

Before this change the placeholder line was not parsed at all — `\S+` cannot match text containing
spaces — so it landed in `unparsedLines`, and `AdbDeviceOperations` refuses any unparsed line, which
removed every healthy device from the listing too:

| input | before | after |
| --- | --- | --- |
| placeholder line only | 0 devices, 1 unparsed | 1 device, `serial: null` |
| two placeholder transports | 0 devices | 2 devices, distinct `transport_id` |
| healthy + placeholder | 0 devices, 1 unparsed | 3 devices, one `serial: null` |

`src/adb` and `tests/boundary/cli/adbCli.test.ts` — 189 passing. `tests/boundary/providers/adb`
fails 4, all of which fail on `main` on this Windows host (where it fails 25). `tsc --noEmit`
0 errors, `npm run lint` 0 errors.

* test(adb): give the MCP fixture the serial status the parser reports

A review finding: `adbDeviceObservationSchema.serial_status` is now required, and the fixture in
`tests/boundary/mcp/adbMcp.test.ts` still omitted it, so `projectDeviceList` refused the listing
before the test's success assertion could run:

```
tests/boundary/mcp/adbMcp.test.ts:372
  expect(listed.isError).not.toBe(true);   → expected true not to be true
```

Both fixture devices report a serial, so both take `serial_status: "reported"`.

The reviewer also mentioned three full-object assertions in `AdbProvider.test.ts`; those do not
exist in the tree at this commit — `src/adb` runs 162 passing with the change, and
`git grep serial_status` finds no other fixture that carries a complete device object. I checked
rather than trusting the report.

* test(adb): give the provider device assertions the serial status

A review finding, and the reviewer was right about this file: I had reported it as not containing the
assertions, because `AdbProvider.test.ts:76` is `test.skipIf(process.platform === "win32")`, so all
117 of its tests are skipped on this Windows host and my local run reported 162 passing without ever
executing them. CI runs the adapters lane on Linux, where the three `toEqual` device objects compared
a 9-field observation against an 8-field one:

```
FAIL  src/adb/AdbProvider.test.ts > lists devices with inferred kinds and observed daemon startup
FAIL  src/adb/AdbProvider.test.ts > observes daemon startup when notices are printed to stderr
FAIL  src/adb/AdbProvider.test.ts > lists a USB device adb has no permission to open alongside online devices
  +     "serial_status": "reported",
```

Those are exactly the three, so `serial_status: "reported"` is added to each. Every device in this
file reports a serial, so the value is the same at all three sites.

Correcting my previous message on this PR: the file does carry them. The lesson is the same one that
has bitten this branch twice — `src/adb` passing locally is not evidence about a lane this host
skips.

`tsc --noEmit` 0 errors, `npm run lint` 0 errors.

* refactor(adb): let a null serial carry the not-reported fact

serial_status restated serial === null on every device row. Drop it and
document the null serial at the schema, where transport_identity still
tells two serial-less transports apart.

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
吴佳浩Albenandmorluto authored and GitHub committed 2026-11-26 10:45:11 -05:00
1 parent 3ab1db27e3
commit df70b6685e
3 files changed
+102 -5

No files matched your search

+75
View File
@@ -524,3 +524,78 @@ it("uses only the final transport field and distinguishes a missing field", () =
transport_identity: { status: "not_reported", source_token: null },
});
});
// AOSP `transport.cpp append_transport()` substitutes this placeholder when the transport has no
// serial to report, which `client/usb_linux.cpp` keeps for a device that cannot report one during
// early bringup (http://b/20883914). It contains spaces, and the long listing pads it with `%-22s`,
// so the serial column is not one `\S+` token — and it names no device, so it is not an identity.
it("parses the placeholder adb prints for a transport with no serial", () => {
const output = [
"List of devices attached",
"(no serial number) device usb:1-3 product:beryllium model:POCO_F1 device:beryllium transport_id:2",
"(no serial number) offline usb:1-4 transport_id:3",
"",
].join("\n");
const { devices, unparsedLines } = parseAdbDevicesOutput(output);
expect(unparsedLines).toEqual({ sample: [], total: 0 });
expect(
devices.map(({ serial, state, transport, kind }) => ({
serial,
state,
transport,
kind,
})),
).toEqual([
{
serial: null,
state: "device",
transport: "usb",
kind: "physical",
},
{
serial: null,
state: "offline",
transport: "usb",
kind: "physical",
},
]);
// The transport ID is what still identifies each of them.
expect(devices[0]?.transport_identity).toMatchObject({
status: "reported",
decimal: "2",
});
expect(devices[1]?.transport_identity).toMatchObject({
status: "reported",
decimal: "3",
});
});
it("keeps the other attached devices when one transport has no serial", () => {
// `AdbDeviceOperations` rejects any unparsed line outright, so a placeholder the pattern missed
// removed every healthy device from the result as well.
const output = [
"List of devices attached",
"emulator-5554\tdevice",
"07ef9c2a\tdevice",
"(no serial number) offline usb:1-4 transport_id:3",
"",
].join("\n");
const { devices, unparsedLines } = parseAdbDevicesOutput(output);
expect(unparsedLines.total).toBe(0);
expect(devices.map(({ serial }) => serial)).toEqual([
"emulator-5554",
"07ef9c2a",
null,
]);
});
it("parses the placeholder in the short listing too", () => {
const { devices, unparsedLines } = parseAdbDevicesOutput(
["List of devices attached", "(no serial number)\tdevice", ""].join("\n"),
);
expect(unparsedLines.total).toBe(0);
expect(devices[0]).toMatchObject({
serial: null,
state: "device",
});
});
+18 -4
View File
@@ -21,7 +21,16 @@ import {
import { parseAdbTransportIdentity } from "../domain/adb/adbTransportIdentity.js";
const DEVICE_LINE = /^(\S+)\s+(.*)$/u;
/**
* The serial column of a `devices` line, which is not always a single token.
*
* AOSP `transport.cpp append_transport()` substitutes the literal `(no serial number)` when the
* transport has none to report — `client/usb_linux.cpp` keeps an empty serial for a device that
* cannot report one during early bringup (http://b/20883914) and still registers the transport.
* The long listing pads that column with `%-22s`, the short one separates it with a tab, and in
* both forms the placeholder carries spaces, so `\S+` alone never matched it.
*/
const DEVICE_LINE = /^(\(no serial number\)|\S+)\s+(.*)$/u;
/**
* adb prints a wireless-debugging transport's serial as the mDNS instance
@@ -117,7 +126,10 @@ export const parseAdbDevicesOutput = (
if (rawLine.trim() !== "") unparsed.add(rawLine);
continue;
}
const serial = match[1]!;
// The placeholder names no device; two transports without a serial would otherwise be
// indistinguishable, and the text cannot be passed to any device operation.
const placeholder = match[1]! === "(no serial number)";
const serial = placeholder ? null : match[1]!;
const remainder = match[2]!.slice(state.length);
const usb = metadataValue(remainder, "usb");
// AOSP appends the authoritative field last; earlier metadata may contain
@@ -128,7 +140,7 @@ export const parseAdbDevicesOutput = (
transportToken,
rawLine,
);
const isEmulator = /^emulator-\d+$/u.test(serial);
const isEmulator = serial !== null && /^emulator-\d+$/u.test(serial);
const kind = isEmulator
? ("emulator" as const)
: usb !== null
@@ -140,7 +152,9 @@ export const parseAdbDevicesOutput = (
transport:
usb !== null
? "usb"
: isEmulator || serial.includes(":") || isAdbMdnsTcpSerial(serial)
: isEmulator ||
(serial !== null &&
(serial.includes(":") || isAdbMdnsTcpSerial(serial)))
? "tcp"
: "unknown",
product: metadataValue(remainder, "product"),
+9 -1
View File
@@ -582,7 +582,15 @@ const client = z.strictObject({
});
export const adbDeviceObservationSchema = z.strictObject({
serial: z.string().min(1),
/**
* The serial `adb devices` reports, or null when the transport has none. AOSP
* `transport.cpp append_transport()` prints the literal `(no serial number)` in
* that column for a transport registered without one (`client/usb_linux.cpp`
* keeps an empty serial during early bringup, http://b/20883914). That text is a
* display placeholder, not an identity: it names no device, so it is not repeated
* here as one; `transport_identity` still distinguishes such transports.
*/
serial: z.string().min(1).nullable(),
state: adbDeviceStateSchema,
transport: z.enum(["usb", "tcp", "unknown"]),
product: z.string().nullable(),