diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index 39a76ab16451e..09c9f29c84b39 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -1278,6 +1278,45 @@ it.effect("keeps shutdown successful when subsequent discovery fails", () => }), ); +it.effect("shuts a device down when a close with shutdown follows a close without it", () => + Effect.gen(function* () { + const { service, requests } = yield* fixture(Effect.void, undefined, true); + yield* service.configure({ enabled: true }); + const threadId = ThreadId.make("close-then-shutdown"); + const session = yield* service.open({ + threadId, + deviceId: "Pixel_API_35", + platform: "android", + }); + yield* service.close({ threadId, deviceId: session.deviceId, shutdown: false }); + yield* service.close({ threadId, deviceId: session.deviceId, shutdown: true }); + expect(requests.some((url) => url.endsWith("/shutdown"))).toBe(true); + const state = yield* service.state; + expect(state.devices.find((device) => device.id === session.deviceId)?.booted).toBe(false); + }), +); + +it.effect("refuses to shut down a closed device that another thread still has open", () => + Effect.gen(function* () { + const { service, requests } = yield* fixture(Effect.void, undefined, true); + yield* service.configure({ enabled: true }); + const threadId = ThreadId.make("closed-thread"); + const other = ThreadId.make("other-thread"); + const session = yield* service.open({ + threadId, + deviceId: "Pixel_API_35", + platform: "android", + }); + yield* service.open({ threadId: other, deviceId: session.deviceId, platform: "android" }); + yield* service.close({ threadId, deviceId: session.deviceId }); + const error = yield* Effect.flip( + service.close({ threadId, deviceId: session.deviceId, shutdown: true }), + ); + expect(error._tag).toBe("DeviceHostUnavailableError"); + expect(requests.some((url) => url.endsWith("/shutdown"))).toBe(false); + }), +); + it.effect.each(["shutdown", "close"] as const)( "%s releases iOS capture and the session's launcher so reopening uses a fresh session", (operation) => diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 0c508b0118d65..57662b8907ab2 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -978,8 +978,30 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* } } if (input.shutdown) yield* releaseLaunchers(ending); + // A close without shutdown already retired this thread's session, so turn off the device it names. + const hostId = input.hostId ?? LOCAL_DEVICE_HOST_ID; + const unclaimed = + input.shutdown && input.deviceId !== undefined && closing.length === 0 + ? state.devices + .filter( + (device) => + device.hostId === hostId && device.id === input.deviceId && device.booted, + ) + .map((device) => ({ hostId, deviceId: device.id, platform: device.platform })) + : []; + if ( + unclaimed.length > 0 && + state.sessions.some( + (session) => session.hostId === hostId && session.deviceId === input.deviceId, + ) + ) + return yield* new DeviceHostUnavailableError({ + hostId, + reason: `Device ${input.deviceId} is open in another thread. Close it there first.`, + }); + const shuttingDown = [...closing, ...unclaimed]; if (input.shutdown) { - for (const session of closing) { + for (const session of shuttingDown) { const ready = yield* currentReadiness(session.hostId); if (!ready) return yield* new DeviceHostUnavailableError({ @@ -993,7 +1015,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* ...current, sessions: current.sessions.filter((session) => !ending.includes(session)), })); - return closing; + return shuttingDown; }), ); if (input.shutdown) diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index 0d7dc00f20caa..1c6bc308e459b 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -440,7 +440,15 @@ const handlers = { : yield* devices .locateSession({ threadId: scope.thread.threadId, deviceId, hostId: inputHostId }) .pipe(mapLocateErrors); - const hostId = Option.isSome(open) ? open.value.hostId : inputHostId; + const located = + Option.isNone(open) && input.shutdown && deviceId !== undefined + ? yield* devices.locateDevice({ deviceId, hostId: inputHostId }).pipe(mapLocateErrors) + : Option.none(); + const hostId = Option.isSome(open) + ? open.value.hostId + : Option.isSome(located) + ? located.value.hostId + : inputHostId; yield* devices.close({ threadId: scope.thread.threadId, ...(hostId === undefined ? {} : { hostId }),