From 2edae1320e7fc39a78be86c52feb8faf8484d60d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 3 Nov 2026 17:39:20 +0000 Subject: [PATCH 1/9] test(server): reproduce device_open misses for booted AVDs and failed device lists (#30273) These tests reproduce pingdotgg/t3code#30273: an AVD name no longer resolves once the emulator is listed by its adb serial, and a host whose device list failed is reported as having no such device. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.test.ts | 32 +++++++++++++++++--- apps/server/src/mcp/McpDeviceToolkit.test.ts | 24 +++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index 8c7b400a2c5de..90769f964229a 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -1734,12 +1734,14 @@ describe("locating a device id across hosts", () => { const macMini = { id: "mac-mini", label: "Mac Mini" }; const listingHost = ( host: { readonly id: string; readonly label: string }, - ids: ReadonlyArray, + ids: ReadonlyArray, ): DeviceHost.DeviceHost["Service"] => { - const devices = ids.map((id) => ({ - id, - name: id, - platform: id.startsWith("Pixel") ? ("android" as const) : ("ios" as const), + const devices = ids.map((entry) => ({ + ...(typeof entry === "string" ? { id: entry, name: entry } : entry), + platform: + typeof entry !== "string" || entry.startsWith("Pixel") + ? ("android" as const) + : ("ios" as const), version: "test", booted: true, physical: false, @@ -1851,6 +1853,26 @@ describe("locating a device id across hosts", () => { ), ); + it.effect("finds a running emulator by the AVD name it was opened with", () => + Effect.gen(function* () { + // A booted AVD is listed by its adb serial; its AVD name is only its name. + const service = yield* DeviceService.makeWithHosts( + new Map([ + [ + thisMachine.id, + listingHost(thisMachine, [{ id: "emulator-5554", name: "Medium_Phone" }]), + ], + ]), + ).pipe(Effect.provide(NodeCrypto.layer)); + yield* service.list; + const located = yield* service.locateDevice({ deviceId: DeviceId.make("Medium_Phone") }); + expect(Option.getOrUndefined(located)).toMatchObject({ + hostId: LOCAL_DEVICE_HOST_ID, + id: "emulator-5554", + }); + }).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true })), Effect.scoped), + ); + it.effect("finds nothing for a device id that no host lists", () => withTwoHosts((service) => Effect.gen(function* () { diff --git a/apps/server/src/mcp/McpDeviceToolkit.test.ts b/apps/server/src/mcp/McpDeviceToolkit.test.ts index 0dbd6cbe1c177..7fffaf2e2d9c4 100644 --- a/apps/server/src/mcp/McpDeviceToolkit.test.ts +++ b/apps/server/src/mcp/McpDeviceToolkit.test.ts @@ -539,6 +539,30 @@ describe("device targeting across hosts", () => { ), ); + it.effect("says a host's device list failed instead of that a device is missing", () => { + const failedRefresh = { + ...multiHostState, + hostStatuses: { + ...multiHostState.hostStatuses, + local: { status: "failed" as const, detail: "adb devices timed out" }, + }, + }; + return run( + [], + ({ opened, call }) => + Effect.gen(function* () { + for (const args of [{ hostId: "local" }, {}]) { + const result = yield* call("device_open", { deviceId: "emulator-5562", ...args }); + expect(result.isError).toBe(true); + expect(text(result)).not.toContain("No device"); + expect(text(result)).toContain("adb devices timed out"); + } + expect(opened).toEqual([]); + }), + { list: Effect.succeed(failedRefresh), state: Effect.succeed(failedRefresh) }, + ); + }); + it.effect("screenshots an open device by id on whichever host it is on", () => run([session("mac-mini", "MAC-UDID", "ios")], ({ call }) => Effect.gen(function* () { From de2724bd6c6350d8d0b29c5df6bdcffd75ae27c7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 3 Nov 2026 17:44:06 +0000 Subject: [PATCH 2/9] fix(server): device_open finds booted AVDs by name and reports failed device lists (#30273) A running emulator is listed by its adb serial, so the AVD name an agent listed it by before boot stopped resolving. locateDevice now falls back, on a host where no device has the id, to the one emulator running the AVD of that name. A host whose last refresh failed keeps its previous devices, so device_open now says that host's device list failed, with its detail, instead of that the device is missing. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.test.ts | 20 ++++++++++++++++++ apps/server/src/device/DeviceService.ts | 20 +++++++++++++----- .../src/mcp/toolkits/device/handlers.ts | 21 +++++++++++++++---- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index 90769f964229a..83533ecf94f15 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -1873,6 +1873,26 @@ describe("locating a device id across hosts", () => { }).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true })), Effect.scoped), ); + it.effect("does not guess between two emulators running the same AVD", () => + Effect.gen(function* () { + const service = yield* DeviceService.makeWithHosts( + new Map([ + [ + thisMachine.id, + listingHost(thisMachine, [ + { id: "emulator-5554", name: "Medium_Phone" }, + { id: "emulator-5556", name: "Medium_Phone" }, + ]), + ], + ]), + ).pipe(Effect.provide(NodeCrypto.layer)); + yield* service.list; + expect(yield* service.locateDevice({ deviceId: DeviceId.make("Medium_Phone") })).toEqual( + Option.none(), + ); + }).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true })), Effect.scoped), + ); + it.effect("finds nothing for a device id that no host lists", () => withTwoHosts((service) => Effect.gen(function* () { diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 2f6df7dace573..4ebd29bc45f79 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -201,7 +201,8 @@ export class DeviceService extends Context.Service< /** * The device an id names in the last listing. Ids are unique per host, not across hosts (an * Android AVD name can exist on two), so a hostId decides, and without one the id has to be - * on a single host. + * on a single host. On a host where no device has the id, the one emulator running the AVD of + * that name stands in for it. */ readonly locateDevice: (input: { readonly hostId?: DeviceHostId | undefined; @@ -1029,11 +1030,20 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* "DeviceService.locateDevice", )(function* (input) { const { state } = yield* SynchronizedRef.get(stateRef); - return yield* locate( - state, - input, - state.devices.filter((device) => device.id === input.deviceId), + const byId = state.devices.filter((device) => device.id === input.deviceId); + const hostsWithId = new Set(byId.map((device) => device.hostId)); + // A shut-down AVD is listed by its name and a running one by its adb serial. + const byAvdName = state.devices.filter( + (device) => + device.platform === "android" && + !device.physical && + device.name === input.deviceId && + !hostsWithId.has(device.hostId), + ); + const unambiguousByAvdName = byAvdName.filter( + (device) => byAvdName.filter((other) => other.hostId === device.hostId).length === 1, ); + return yield* locate(state, input, [...byId, ...unambiguousByAvdName]); }); const locateSession: DeviceService["Service"]["locateSession"] = Effect.fn( diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index b73661899c85f..be1fd4e019500 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -4,6 +4,7 @@ import { type DeviceHostSummary, type DeviceId, type DevicePlatform, + type DeviceServiceState, type DeviceSummary, DeviceToolUnavailableError, LOCAL_DEVICE_HOST_ID, @@ -144,10 +145,7 @@ const mapLocateErrors = ( ); const pickDevice = Effect.fn("handlers.pickDevice")(function* ( - state: { - readonly hosts: ReadonlyArray; - readonly devices: ReadonlyArray; - }, + state: DeviceServiceState, input: { readonly deviceId?: DeviceId | undefined; readonly platform?: DevicePlatform | undefined; @@ -159,6 +157,21 @@ const pickDevice = Effect.fn("handlers.pickDevice")(function* ( const devices = yield* DeviceService.DeviceService; const located = yield* devices.locateDevice({ deviceId, hostId }).pipe(mapLocateErrors); if (Option.isSome(located)) return located.value; + // A failed refresh keeps the previous devices, so the device may be there unlisted. + const failed = state.hosts.filter( + (host) => + (hostId === undefined || host.id === hostId) && + state.hostStatuses[host.id]?.status === "failed", + ); + if (failed.length > 0) { + const failures = failed.map((host) => { + const detail = state.hostStatuses[host.id]?.detail; + return detail ? `${hostName(host)} (${detail})` : hostName(host); + }); + return yield* new DeviceToolUnavailableError({ + reason: `Device ${deviceId} is not in the last device list, and listing devices failed on ${failures.join(", ")}. Call device_list to retry.`, + }); + } return yield* new DeviceToolUnavailableError({ reason: `No device ${deviceId} on ${hostId === undefined ? "any host" : hostName(state.hosts.find((host) => host.id === hostId) ?? { id: hostId })}. Call device_list for current ids.`, }); From 1feef5ae755ef1c9c6fbc8fa6a06c789023d642d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 3 Nov 2026 17:52:39 +0000 Subject: [PATCH 3/9] fix(server): exact device ids win over AVD names, and a duplicated AVD is never redirected to another host Review round 1 (#30273): the AVD-name fallback dropped a host running two emulators of one AVD and then picked another host's emulator, and it made a listed AVD id ambiguous with another host's running emulator of that name. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.test.ts | 33 +++++++++++++++++++- apps/server/src/device/DeviceService.ts | 23 ++++++++------ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index 83533ecf94f15..d8abfb7af9988 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -1884,12 +1884,43 @@ describe("locating a device id across hosts", () => { { id: "emulator-5556", name: "Medium_Phone" }, ]), ], + [macMini.id, listingHost(macMini, [{ id: "emulator-5554", name: "Medium_Phone" }])], ]), ).pipe(Effect.provide(NodeCrypto.layer)); yield* service.list; - expect(yield* service.locateDevice({ deviceId: DeviceId.make("Medium_Phone") })).toEqual( + const deviceId = DeviceId.make("Medium_Phone"); + expect(yield* service.locateDevice({ deviceId }).pipe(Effect.flip)).toMatchObject({ + _tag: "DeviceHostAmbiguousError", + }); + expect(yield* service.locateDevice({ deviceId, hostId: LOCAL_DEVICE_HOST_ID })).toEqual( Option.none(), ); + expect( + Option.getOrUndefined(yield* service.locateDevice({ deviceId, hostId: "mac-mini" })), + ).toMatchObject({ hostId: "mac-mini", id: "emulator-5554" }); + }).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true })), Effect.scoped), + ); + + it.effect("prefers a listed id to another host's emulator running that AVD", () => + Effect.gen(function* () { + const service = yield* DeviceService.makeWithHosts( + new Map([ + [ + thisMachine.id, + listingHost(thisMachine, [{ id: "Medium_Phone", name: "Medium_Phone" }]), + ], + [macMini.id, listingHost(macMini, [{ id: "emulator-5554", name: "Medium_Phone" }])], + ]), + ).pipe(Effect.provide(NodeCrypto.layer)); + yield* service.list; + const deviceId = DeviceId.make("Medium_Phone"); + expect(Option.getOrUndefined(yield* service.locateDevice({ deviceId }))).toMatchObject({ + hostId: LOCAL_DEVICE_HOST_ID, + id: "Medium_Phone", + }); + expect( + Option.getOrUndefined(yield* service.locateDevice({ deviceId, hostId: "mac-mini" })), + ).toMatchObject({ hostId: "mac-mini", id: "emulator-5554" }); }).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true })), Effect.scoped), ); diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 4ebd29bc45f79..845dbe81984ef 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -201,8 +201,8 @@ export class DeviceService extends Context.Service< /** * The device an id names in the last listing. Ids are unique per host, not across hosts (an * Android AVD name can exist on two), so a hostId decides, and without one the id has to be - * on a single host. On a host where no device has the id, the one emulator running the AVD of - * that name stands in for it. + * on a single host. When no device in scope has the id, the one emulator running the AVD of + * that name on a host stands in for it. */ readonly locateDevice: (input: { readonly hostId?: DeviceHostId | undefined; @@ -1031,19 +1031,22 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* )(function* (input) { const { state } = yield* SynchronizedRef.get(stateRef); const byId = state.devices.filter((device) => device.id === input.deviceId); - const hostsWithId = new Set(byId.map((device) => device.hostId)); + if (byId.some((device) => input.hostId === undefined || device.hostId === input.hostId)) { + return yield* locate(state, input, byId); + } // A shut-down AVD is listed by its name and a running one by its adb serial. const byAvdName = state.devices.filter( (device) => - device.platform === "android" && - !device.physical && - device.name === input.deviceId && - !hostsWithId.has(device.hostId), + device.platform === "android" && !device.physical && device.name === input.deviceId, ); - const unambiguousByAvdName = byAvdName.filter( - (device) => byAvdName.filter((other) => other.hostId === device.hostId).length === 1, + const located = yield* locate(state, input, [...byId, ...byAvdName]); + // Several emulators running one AVD: its name picks none of them. + return Option.filter( + located, + (device) => + device.id === input.deviceId || + byAvdName.filter((other) => other.hostId === device.hostId).length === 1, ); - return yield* locate(state, input, [...byId, ...unambiguousByAvdName]); }); const locateSession: DeviceService["Service"]["locateSession"] = Effect.fn( From dd0fed09d24073641b6c67d57e1916f49b5f8726 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 3 Nov 2026 18:08:26 +0000 Subject: [PATCH 4/9] fix(server): a failed device list on the asked host outranks another host having the id Review round 2 (#30273): with hostId set, DeviceOnOtherHostError returned before the failed-refresh check, so the agent was sent to another host without hearing that the asked host's list had failed. The AVD-name fallback now only matches running emulators. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.ts | 5 +++- apps/server/src/mcp/McpDeviceToolkit.test.ts | 25 +++++++++++++++++++ .../src/mcp/toolkits/device/handlers.ts | 10 ++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 845dbe81984ef..6d389fee8fdb2 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -1037,7 +1037,10 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* // A shut-down AVD is listed by its name and a running one by its adb serial. const byAvdName = state.devices.filter( (device) => - device.platform === "android" && !device.physical && device.name === input.deviceId, + device.platform === "android" && + !device.physical && + device.booted && + device.name === input.deviceId, ); const located = yield* locate(state, input, [...byId, ...byAvdName]); // Several emulators running one AVD: its name picks none of them. diff --git a/apps/server/src/mcp/McpDeviceToolkit.test.ts b/apps/server/src/mcp/McpDeviceToolkit.test.ts index 7fffaf2e2d9c4..fb89043ed9c8d 100644 --- a/apps/server/src/mcp/McpDeviceToolkit.test.ts +++ b/apps/server/src/mcp/McpDeviceToolkit.test.ts @@ -563,6 +563,31 @@ describe("device targeting across hosts", () => { ); }); + it.effect("says the asked host's device list failed even when another host has the id", () => { + const failedRefresh = { + ...multiHostState, + hostStatuses: { + ...multiHostState.hostStatuses, + local: { status: "failed" as const, detail: "adb devices timed out" }, + }, + }; + return run( + [], + ({ opened, call }) => + Effect.gen(function* () { + const result = yield* call("device_open", { deviceId: "MAC-UDID", hostId: "local" }); + expect(result.isError).toBe(true); + expect(text(result)).toContain("adb devices timed out"); + expect(opened).toEqual([]); + }), + { + list: Effect.succeed(failedRefresh), + state: Effect.succeed(failedRefresh), + locateDevice: () => Effect.fail(onTheMac), + }, + ); + }); + it.effect("screenshots an open device by id on whichever host it is on", () => run([session("mac-mini", "MAC-UDID", "ios")], ({ call }) => Effect.gen(function* () { diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index be1fd4e019500..8962e3300a9ce 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -155,14 +155,20 @@ const pickDevice = Effect.fn("handlers.pickDevice")(function* ( if (input.deviceId !== undefined) { const { deviceId, hostId } = input; const devices = yield* DeviceService.DeviceService; - const located = yield* devices.locateDevice({ deviceId, hostId }).pipe(mapLocateErrors); - if (Option.isSome(located)) return located.value; // A failed refresh keeps the previous devices, so the device may be there unlisted. const failed = state.hosts.filter( (host) => (hostId === undefined || host.id === hostId) && state.hostStatuses[host.id]?.status === "failed", ); + const located = yield* devices.locateDevice({ deviceId, hostId }).pipe( + Effect.catchTags({ + DeviceOnOtherHostError: (error) => + failed.length > 0 ? Effect.succeedNone : Effect.fail(error), + }), + mapLocateErrors, + ); + if (Option.isSome(located)) return located.value; if (failed.length > 0) { const failures = failed.map((host) => { const detail = state.hostStatuses[host.id]?.detail; From ca52209b448f006a7b9876fe8b453dfafd837b77 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 3 Nov 2026 18:15:10 +0000 Subject: [PATCH 5/9] refactor(server): drop an unreachable exact-id check from the AVD-name fallback Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 6d389fee8fdb2..547d0ebfb312c 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -1046,9 +1046,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* // Several emulators running one AVD: its name picks none of them. return Option.filter( located, - (device) => - device.id === input.deviceId || - byAvdName.filter((other) => other.hostId === device.hostId).length === 1, + (device) => byAvdName.filter((other) => other.hostId === device.hostId).length === 1, ); }); From 044d489726f845eb5130ae4c0ba89f9bdd7a80d6 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 23:27:31 +0000 Subject: [PATCH 6/9] fix(server): device_open does not open another host's emulator by AVD name while a device list failed A host whose listing failed may be running that AVD unlisted, so an unscoped name match on another host is not trusted; the failed listing is reported instead. Exact device ids keep resolving as before. AI-assisted; verified with the tests and tools listed above. --- apps/server/src/mcp/McpDeviceToolkit.test.ts | 69 ++++++++++++------- .../src/mcp/toolkits/device/handlers.ts | 5 +- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/apps/server/src/mcp/McpDeviceToolkit.test.ts b/apps/server/src/mcp/McpDeviceToolkit.test.ts index fb89043ed9c8d..373ca1a7de5ad 100644 --- a/apps/server/src/mcp/McpDeviceToolkit.test.ts +++ b/apps/server/src/mcp/McpDeviceToolkit.test.ts @@ -539,15 +539,20 @@ describe("device targeting across hosts", () => { ), ); - it.effect("says a host's device list failed instead of that a device is missing", () => { - const failedRefresh = { - ...multiHostState, - hostStatuses: { - ...multiHostState.hostStatuses, - local: { status: "failed" as const, detail: "adb devices timed out" }, - }, - }; - return run( + const failedRefresh = { + ...multiHostState, + hostStatuses: { + ...multiHostState.hostStatuses, + local: { status: "failed" as const, detail: "adb devices timed out" }, + }, + }; + const withFailedRefresh = { + list: Effect.succeed(failedRefresh), + state: Effect.succeed(failedRefresh), + }; + + it.effect("says a host's device list failed instead of that a device is missing", () => + run( [], ({ opened, call }) => Effect.gen(function* () { @@ -559,19 +564,12 @@ describe("device targeting across hosts", () => { } expect(opened).toEqual([]); }), - { list: Effect.succeed(failedRefresh), state: Effect.succeed(failedRefresh) }, - ); - }); + withFailedRefresh, + ), + ); - it.effect("says the asked host's device list failed even when another host has the id", () => { - const failedRefresh = { - ...multiHostState, - hostStatuses: { - ...multiHostState.hostStatuses, - local: { status: "failed" as const, detail: "adb devices timed out" }, - }, - }; - return run( + it.effect("says the asked host's device list failed even when another host has the id", () => + run( [], ({ opened, call }) => Effect.gen(function* () { @@ -580,13 +578,32 @@ describe("device targeting across hosts", () => { expect(text(result)).toContain("adb devices timed out"); expect(opened).toEqual([]); }), + { ...withFailedRefresh, locateDevice: () => Effect.fail(onTheMac) }, + ), + ); + + it.effect("does not open another host's emulator by AVD name while a device list failed", () => + run( + [], + ({ opened, call }) => + Effect.gen(function* () { + const result = yield* call("device_open", { deviceId: "Medium_Phone" }); + expect(result.isError).toBe(true); + expect(text(result)).toContain("adb devices timed out"); + expect(opened).toEqual([]); + }), { - list: Effect.succeed(failedRefresh), - state: Effect.succeed(failedRefresh), - locateDevice: () => Effect.fail(onTheMac), + ...withFailedRefresh, + locateDevice: () => + Effect.succeedSome({ + ...macPixel, + id: "emulator-5554", + name: "Medium_Phone", + booted: true, + }), }, - ); - }); + ), + ); it.effect("screenshots an open device by id on whichever host it is on", () => run([session("mac-mini", "MAC-UDID", "ios")], ({ call }) => diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index 8962e3300a9ce..200ee1ad1be90 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -168,7 +168,10 @@ const pickDevice = Effect.fn("handlers.pickDevice")(function* ( }), mapLocateErrors, ); - if (Option.isSome(located)) return located.value; + // An AVD-name match could be on another host than the one whose list failed. + if (Option.isSome(located) && (failed.length === 0 || located.value.id === deviceId)) { + return located.value; + } if (failed.length > 0) { const failures = failed.map((host) => { const detail = state.hostStatuses[host.id]?.detail; From 0125d139662cf0882e11baf8dc2c480a942ebab7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 14 Nov 2026 00:53:30 +0000 Subject: [PATCH 7/9] refactor(server): DeviceService decides when a failed device list outranks a match The MCP handler interpreted host statuses and swallowed DeviceOnOtherHostError itself. locateDevice now fails with DeviceListFailedError when no device in scope has the exact id and listing failed on a host in scope, so the handler only maps errors. This also reports the failed list when an AVD name matches emulators on two hosts, instead of asking for a hostId. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.test.ts | 52 +++++++++++++++++++ apps/server/src/device/DeviceService.ts | 38 +++++++++++--- apps/server/src/mcp/McpDeviceToolkit.test.ts | 49 ----------------- .../src/mcp/toolkits/device/handlers.ts | 35 +++++++------ 4 files changed, 102 insertions(+), 72 deletions(-) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index e66ad7b20b128..b314969ca6081 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -2218,6 +2218,58 @@ describe("locating a device id across hosts", () => { }).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true })), Effect.scoped), ); + it.effect("reports a failed device list before matching another host or an AVD name", () => + Effect.gen(function* () { + const local = listingHost(thisMachine, [{ id: "emulator-5554", name: "Medium_Phone" }]); + let listed = false; + const failingOnRelist: DeviceHost.DeviceHost["Service"] = { + ...local, + ensureReady: (...args) => + local.ensureReady(...args).pipe( + Effect.map((ready): DeviceHost.DeviceHostReady => ({ + ...ready, + devices: { + ...ready.devices, + list: Effect.suspend(() => { + if (!listed) { + listed = true; + return ready.devices.list; + } + return Effect.fail( + new DeviceOperationError({ + operation: "list devices", + reason: "request_failed", + cause: null, + }), + ); + }), + }, + })), + ), + }; + const service = yield* DeviceService.makeWithHosts( + new Map([ + [thisMachine.id, failingOnRelist], + [ + macMini.id, + listingHost(macMini, ["MAC-UDID", { id: "emulator-5554", name: "Medium_Phone" }]), + ], + ]), + ).pipe(Effect.provide(NodeCrypto.layer)); + yield* service.list; + yield* service.list; + for (const input of [ + { deviceId: DeviceId.make("MAC-UDID"), hostId: LOCAL_DEVICE_HOST_ID }, + { deviceId: DeviceId.make("Medium_Phone") }, + ]) { + expect(yield* service.locateDevice(input).pipe(Effect.flip)).toMatchObject({ + _tag: "DeviceListFailedError", + hosts: [{ id: LOCAL_DEVICE_HOST_ID }], + }); + } + }).pipe(Effect.provide(ServerSettings.layerTest({ enableDeviceSupport: true })), Effect.scoped), + ); + it.effect("finds nothing for a device id that no host lists", () => withTwoHosts((service) => Effect.gen(function* () { diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 0cbf297ce0903..84289613946b1 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -156,6 +156,18 @@ export class DeviceOnOtherHostError extends Schema.TaggedError()( + "DeviceListFailedError", + { + deviceId: DeviceId, + hosts: Schema.Array(Schema.Struct({ ...DeviceHostName.fields, detail: Schema.String })), + }, +) { + override get message(): string { + return `Device ${this.deviceId} is not in the last device list, and listing devices failed on ${this.hosts.map((host) => `${hostName(host)} (${host.detail})`).join(", ")}.`; + } +} + export class DeviceService extends Context.Service< DeviceService, { @@ -222,14 +234,14 @@ export class DeviceService extends Context.Service< * The device an id names in the last listing. Ids are unique per host, not across hosts (an * Android AVD name can exist on two), so a hostId decides, and without one the id has to be * on a single host. When no device in scope has the id, the one emulator running the AVD of - * that name on a host stands in for it. + * that name on a host stands in for it, unless listing devices failed on a host in scope. */ readonly locateDevice: (input: { readonly hostId?: DeviceHostId | undefined; readonly deviceId: DeviceId; }) => Effect.Effect< Option.Option, - DeviceHostAmbiguousError | DeviceOnOtherHostError + DeviceHostAmbiguousError | DeviceOnOtherHostError | DeviceListFailedError >; /** As `locateDevice`, among the devices a thread has open. */ readonly locateSession: (input: { @@ -1038,15 +1050,17 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* Effect.map(({ state }) => state.sessions.filter((session) => session.threadId === threadId)), ); + const namedHost = (state: DeviceServiceState, id: DeviceHostId) => ({ + id, + label: state.hosts.find((host) => host.id === id)?.label, + }); + const locate = ( state: DeviceServiceState, input: { readonly hostId?: DeviceHostId | undefined; readonly deviceId: DeviceId }, matches: ReadonlyArray, ): Effect.Effect, DeviceHostAmbiguousError | DeviceOnOtherHostError> => { - const named = (id: DeviceHostId) => ({ - id, - label: state.hosts.find((host) => host.id === id)?.label, - }); + const named = (id: DeviceHostId) => namedHost(state, id); const onHost = matches.filter( (match) => input.hostId === undefined || match.hostId === input.hostId, ); @@ -1077,6 +1091,18 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* if (byId.some((device) => input.hostId === undefined || device.hostId === input.hostId)) { return yield* locate(state, input, byId); } + // A failed refresh keeps the previous devices, so the device may be there unlisted. + const failed = ( + input.hostId === undefined ? state.hosts.map((host) => host.id) : [input.hostId] + ).flatMap((id) => { + const status = state.hostStatuses[id]; + return status?.status === "failed" && status.detail + ? [{ ...namedHost(state, id), detail: status.detail }] + : []; + }); + if (failed.length > 0) { + return yield* new DeviceListFailedError({ deviceId: input.deviceId, hosts: failed }); + } // A shut-down AVD is listed by its name and a running one by its adb serial. const byAvdName = state.devices.filter( (device) => diff --git a/apps/server/src/mcp/McpDeviceToolkit.test.ts b/apps/server/src/mcp/McpDeviceToolkit.test.ts index 5d8d7a7f16abd..1904a24010bee 100644 --- a/apps/server/src/mcp/McpDeviceToolkit.test.ts +++ b/apps/server/src/mcp/McpDeviceToolkit.test.ts @@ -591,55 +591,6 @@ describe("device targeting across hosts", () => { ), ); - const failedRefresh = { - ...multiHostState, - hostStatuses: { - ...multiHostState.hostStatuses, - local: { status: "failed" as const, detail: "adb devices timed out" }, - }, - }; - const withFailedRefresh = { - list: Effect.succeed(failedRefresh), - state: Effect.succeed(failedRefresh), - }; - - it.effect("says the asked host's device list failed even when another host has the id", () => - run( - [], - ({ opened, call }) => - Effect.gen(function* () { - const result = yield* call("device_open", { deviceId: "MAC-UDID", hostId: "local" }); - expect(result.isError).toBe(true); - expect(text(result)).toContain("adb devices timed out"); - expect(opened).toEqual([]); - }), - { ...withFailedRefresh, locateDevice: () => Effect.fail(onTheMac) }, - ), - ); - - it.effect("does not open another host's emulator by AVD name while a device list failed", () => - run( - [], - ({ opened, call }) => - Effect.gen(function* () { - const result = yield* call("device_open", { deviceId: "Medium_Phone" }); - expect(result.isError).toBe(true); - expect(text(result)).toContain("adb devices timed out"); - expect(opened).toEqual([]); - }), - { - ...withFailedRefresh, - locateDevice: () => - Effect.succeedSome({ - ...macPixel, - id: "emulator-5554", - name: "Medium_Phone", - booted: true, - }), - }, - ), - ); - it.effect("screenshots an open device by id on whichever host it is on", () => run([session("mac-mini", "MAC-UDID", "ios")], ({ call }) => Effect.gen(function* () { diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index 68fe8603523a8..16346ffcda152 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -123,7 +123,9 @@ const hostName = (host: { readonly id: DeviceHostId; readonly label?: string | u const mapLocateErrors = ( effect: Effect.Effect< A, - DeviceService.DeviceHostAmbiguousError | DeviceService.DeviceOnOtherHostError, + | DeviceService.DeviceHostAmbiguousError + | DeviceService.DeviceOnOtherHostError + | DeviceService.DeviceListFailedError, R >, ) => @@ -135,6 +137,12 @@ const mapLocateErrors = ( reason: `Device ${error.deviceId} is on more than one host: ${error.hosts.map(hostName).join(", ")}. Pass hostId.`, }), ), + DeviceListFailedError: (error) => + Effect.fail( + new DeviceToolUnavailableError({ + reason: `Device ${error.deviceId} is not in the last device list, and listing devices failed on ${error.hosts.map((host) => `${hostName(host)} (${host.detail})`).join(", ")}. Call device_list to retry.`, + }), + ), DeviceOnOtherHostError: (error) => Effect.fail( new DeviceToolUnavailableError({ @@ -168,28 +176,21 @@ const pickDevice = Effect.fn("handlers.pickDevice")(function* ( if (input.deviceId !== undefined) { const { deviceId, hostId } = input; const devices = yield* DeviceService.DeviceService; + const located = yield* devices.locateDevice({ deviceId, hostId }).pipe(mapLocateErrors); + if (Option.isSome(located)) return located.value; const target = hostId === undefined ? undefined : (state.hosts.find((host) => host.id === hostId) ?? { id: hostId }); - // A failed refresh keeps the previous devices, so the device may be there unlisted. - const failures = failedHosts(state, target === undefined ? state.hosts : [target]); - const located = yield* devices.locateDevice({ deviceId, hostId }).pipe( - Effect.catchTags({ - DeviceOnOtherHostError: (error) => - failures.length > 0 ? Effect.succeedNone : Effect.fail(error), - }), - mapLocateErrors, + const failures = failedHosts(state, target === undefined ? state.hosts : [target]).map( + ({ host, detail }) => `${hostName(host)} failed to start: ${detail}`, ); - // An AVD-name match could be on another host than the one whose list failed. - if (Option.isSome(located) && (failures.length === 0 || located.value.id === deviceId)) { - return located.value; - } return yield* new DeviceToolUnavailableError({ - reason: - failures.length > 0 - ? `Device ${deviceId} is not in the last device list, and listing devices failed on ${failures.map(({ host, detail }) => `${hostName(host)} (${detail})`).join(", ")}. Call device_list to retry.` - : `No device ${deviceId} on ${target === undefined ? "any host" : hostName(target)}. Call device_list for current ids.`, + reason: [ + `No device ${deviceId} on ${target === undefined ? "any host" : hostName(target)}.`, + ...failures, + "Call device_list for current ids.", + ].join(" "), }); } const hostId = input.hostId ?? LOCAL_DEVICE_HOST_ID; From 4c51df677352bb11f2a4395f8d7f1dd3c4039a68 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 11:12:26 +0000 Subject: [PATCH 8/9] refactor(server): device tools reuse the failed device list message Co-Authored-By: Claude Opus 5.5 --- apps/server/src/mcp/toolkits/device/handlers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index 42acb3adc96cd..9893240e09dad 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -140,7 +140,7 @@ const mapLocateErrors = ( DeviceListFailedError: (error) => Effect.fail( new DeviceToolUnavailableError({ - reason: `Device ${error.deviceId} is not in the last device list, and listing devices failed on ${error.hosts.map((host) => `${hostName(host)} (${host.detail})`).join(", ")}. Call device_list to retry.`, + reason: `${error.message} Call device_list to retry.`, }), ), DeviceOnOtherHostError: (error) => From 12fbdf069453c35a2cb7adaa1089d138a5dc2114 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 11:22:08 +0000 Subject: [PATCH 9/9] fix(server): device_open reports a timed-out device list before saying a device is missing Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.test.ts | 3 +++ apps/server/src/device/DeviceService.ts | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index 1585489530a80..3ddf6579bb87c 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -1086,6 +1086,9 @@ it.effect.each([ detail: "The device hub did not answer within 40 seconds.", incompletePlatforms, }); + expect( + yield* service.locateDevice({ deviceId: DeviceId.make("emulator-5562") }).pipe(Effect.flip), + ).toMatchObject({ _tag: "DeviceListFailedError", hosts: [{ id: LOCAL_DEVICE_HOST_ID }] }); }), ); diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 7bd0ece2f75f5..44f25c0415d04 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -1102,12 +1102,12 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* if (byId.some((device) => input.hostId === undefined || device.hostId === input.hostId)) { return yield* locate(state, input, byId); } - // A failed refresh keeps the previous devices, so the device may be there unlisted. + // A failed or incomplete refresh keeps the previous devices, so the device may be there unlisted. const failed = ( input.hostId === undefined ? state.hosts.map((host) => host.id) : [input.hostId] ).flatMap((id) => { const status = state.hostStatuses[id]; - return status?.status === "failed" && status.detail + return (status?.status === "failed" || status?.incompletePlatforms?.length) && status.detail ? [{ ...namedHost(state, id), detail: status.detail }] : []; });