Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions apps/server/src/device/DeviceService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
26 changes: 24 additions & 2 deletions apps/server/src/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion apps/server/src/mcp/toolkits/device/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
Loading