feat(devices): scope targets and sessions to their hosts (#10854)
This commit is contained in:
1 parent
dca7b59bea
commit
e022fa430e
19 files changed
+649
-257
No files matched your search
@@ -196,7 +196,7 @@ const handler = Effect.gen(function* () {
|
||||
(!readOnly && /\/api\/stream-(mode|settings)$/.test(hubPath));
|
||||
yield* authenticate(controlsDevice ? AuthOrchestrationOperateScope : AuthOrchestrationReadScope);
|
||||
const devices = yield* DeviceService.DeviceService;
|
||||
const ready = yield* devices.currentReadiness();
|
||||
const ready = yield* devices.currentReadiness(url.value.searchParams.get("hostId") ?? undefined);
|
||||
if (!ready) {
|
||||
return HttpServerResponse.text("Device hub is not running", { status: 503 });
|
||||
}
|
||||
@@ -206,6 +206,7 @@ const handler = Effect.gen(function* () {
|
||||
// The ticket authenticates here and must not travel on to the hub.
|
||||
const upstreamSearch = new URLSearchParams(url.value.search);
|
||||
upstreamSearch.delete("wsTicket");
|
||||
upstreamSearch.delete("hostId");
|
||||
const search = upstreamSearch.size > 0 ? `?${upstreamSearch.toString()}` : "";
|
||||
const upstreamPath = `${hubPath}${search}`;
|
||||
if (upgrade) {
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { expect, it } from "@effect/vitest";
|
||||
import { ThreadId } from "@t3tools/contracts";
|
||||
import * as Effect from "effect/Effect";
|
||||
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
|
||||
import { ServerSettingsService } from "../serverSettings.ts";
|
||||
import { DeviceHostError, DeviceHost } from "./DeviceHost.ts";
|
||||
import { makeWithHosts } from "./DeviceService.ts";
|
||||
|
||||
it.effect("keeps hosts independent when serials collide and another host fails", () =>
|
||||
Effect.gen(function* () {
|
||||
const host = (id: string, failed = false): DeviceHost["Service"] => {
|
||||
const ready = {
|
||||
hub: { origin: `http://${id}` },
|
||||
agentDevice: { baseUrl: `http://${id}`, token: "test", entryPath: "/agent-device" },
|
||||
run: () => Effect.succeed({ stdout: "", stderr: "", code: 0 }),
|
||||
helpers: { serveSimAxSettings: null, serveSimCli: null },
|
||||
};
|
||||
return {
|
||||
id,
|
||||
summary: Effect.succeed({
|
||||
id,
|
||||
label: id,
|
||||
kind: "local",
|
||||
hubInstalled: true,
|
||||
agentDeviceInstalled: true,
|
||||
platforms: [{ platform: "android", available: true }],
|
||||
}),
|
||||
platformAvailability: (platform) => Effect.succeed({ platform, available: true }),
|
||||
ensureReady: () =>
|
||||
failed
|
||||
? Effect.fail(
|
||||
new DeviceHostError({ hostId: id, step: "connect", cause: new Error("offline") }),
|
||||
)
|
||||
: Effect.succeed(ready),
|
||||
ensureAgentReady: () => Effect.succeed(ready),
|
||||
current: Effect.succeed(ready),
|
||||
stopAgent: Effect.void,
|
||||
stop: Effect.void,
|
||||
};
|
||||
};
|
||||
const http = HttpClient.make((request) =>
|
||||
Effect.succeed(
|
||||
HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({
|
||||
simulators: [],
|
||||
emulators: [
|
||||
{
|
||||
id: "emulator-5554",
|
||||
name: "Pixel",
|
||||
version: "36",
|
||||
platform: "android",
|
||||
booted: true,
|
||||
physical: false,
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
const hosts = new Map(["a", "b", "offline"].map((id) => [id, host(id, id === "offline")]));
|
||||
const service = yield* makeWithHosts(hosts).pipe(
|
||||
Effect.provideService(HttpClient.HttpClient, http),
|
||||
);
|
||||
const listed = yield* service.list;
|
||||
expect(listed.devices.map((device) => device.hostId).sort()).toEqual(["a", "b"]);
|
||||
expect(listed.hostStatuses.offline?.status).toBe("failed");
|
||||
const threadId = ThreadId.make("thread");
|
||||
for (const hostId of ["a", "b"])
|
||||
yield* service.open({ threadId, hostId, deviceId: "emulator-5554", platform: "android" });
|
||||
yield* service.close({ threadId, hostId: "a", deviceId: "emulator-5554" });
|
||||
const state = yield* service.state;
|
||||
expect(state.devices).toHaveLength(2);
|
||||
expect(state.sessions.map((session) => session.hostId)).toEqual(["b"]);
|
||||
expect(state.hostStatuses.a?.status).toBe("ready");
|
||||
expect(state.hostStatuses.offline?.status).toBe("failed");
|
||||
yield* service.agentReadinessIfSupported("b");
|
||||
expect((yield* service.state).hostStatuses.b?.status).toBe("ready");
|
||||
yield* service.configure({ enabled: false });
|
||||
expect((yield* service.state).hostStatuses).toEqual({});
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
ServerSettingsService.layerTest({ enableDeviceSupport: true, enableAgentDeviceAccess: true }),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -21,6 +21,7 @@ import { type DeviceService, make, stateStream } from "./DeviceService.ts";
|
||||
const baseState: DeviceServiceState = {
|
||||
hosts: [],
|
||||
hostStatus: "idle",
|
||||
hostStatuses: {},
|
||||
devices: [],
|
||||
sessions: [],
|
||||
onboardingCompleted: false,
|
||||
|
||||
@@ -136,8 +136,9 @@ interface ServiceState {
|
||||
const vendorPrefix = (platform: DevicePlatform) =>
|
||||
platform === "ios" ? "/vendor/serve-sim" : "/vendor/serve-emu";
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const localHost = yield* DeviceHost.DeviceHost;
|
||||
export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* (
|
||||
hosts: ReadonlyMap<DeviceHostId, DeviceHost.DeviceHost["Service"]>,
|
||||
) {
|
||||
const settings = yield* ServerSettings.ServerSettingsService;
|
||||
const lifecycleLock = yield* Semaphore.make(1);
|
||||
const readDeviceSettings = settings.getSettings.pipe(
|
||||
@@ -152,9 +153,7 @@ export const make = Effect.gen(function* () {
|
||||
),
|
||||
);
|
||||
const initialSettings = yield* readDeviceSettings;
|
||||
const hosts: ReadonlyMap<DeviceHostId, DeviceHost.DeviceHost["Service"]> = new Map([
|
||||
[localHost.id, localHost],
|
||||
]);
|
||||
|
||||
const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.withScope);
|
||||
const statePubSub = yield* PubSub.unbounded<DeviceServiceState>();
|
||||
const initialHosts = yield* Effect.forEach(hosts.values(), (host) => host.summary);
|
||||
@@ -162,6 +161,7 @@ export const make = Effect.gen(function* () {
|
||||
state: {
|
||||
hosts: initialHosts,
|
||||
hostStatus: initialSettings.enabled ? "idle" : "disabled",
|
||||
hostStatuses: {},
|
||||
devices: [],
|
||||
sessions: [],
|
||||
onboardingCompleted: initialSettings.onboardingCompleted,
|
||||
@@ -187,6 +187,18 @@ export const make = Effect.gen(function* () {
|
||||
return host;
|
||||
});
|
||||
|
||||
const setHostStatus = (
|
||||
hostId: DeviceHostId,
|
||||
status: DeviceServiceState["hostStatuses"][string],
|
||||
) =>
|
||||
publish((state) => ({
|
||||
...state,
|
||||
...(hostId === LOCAL_DEVICE_HOST_ID
|
||||
? { hostStatus: status.status, hostStatusDetail: status.detail }
|
||||
: {}),
|
||||
hostStatuses: { ...state.hostStatuses, [hostId]: status },
|
||||
}));
|
||||
|
||||
const readiness: DeviceService["Service"]["readiness"] = Effect.fn("DeviceService.readiness")(
|
||||
function* (hostId) {
|
||||
const host = yield* resolveHost(hostId);
|
||||
@@ -198,34 +210,19 @@ export const make = Effect.gen(function* () {
|
||||
});
|
||||
}
|
||||
const ready = yield* host
|
||||
.ensureReady((phase) =>
|
||||
publish((state) => ({ ...state, hostStatus: phase, hostStatusDetail: undefined })).pipe(
|
||||
Effect.asVoid,
|
||||
),
|
||||
)
|
||||
.ensureReady((status) => setHostStatus(host.id, { status }).pipe(Effect.asVoid))
|
||||
.pipe(
|
||||
Effect.tapError((error) =>
|
||||
publish((state) => ({
|
||||
...state,
|
||||
hostStatus: "failed",
|
||||
hostStatusDetail: error.message,
|
||||
})),
|
||||
setHostStatus(host.id, { status: "failed", detail: error.message }),
|
||||
),
|
||||
Effect.mapError(
|
||||
(error) => new DeviceHostUnavailableError({ hostId: host.id, reason: error.message }),
|
||||
),
|
||||
);
|
||||
yield* SynchronizedRef.get(stateRef).pipe(
|
||||
Effect.flatMap(({ state }) =>
|
||||
state.hostStatus === "ready"
|
||||
? Effect.void
|
||||
: publish((current) => ({
|
||||
...current,
|
||||
hostStatus: "ready",
|
||||
hostStatusDetail: undefined,
|
||||
})),
|
||||
),
|
||||
);
|
||||
const { state } = yield* SynchronizedRef.get(stateRef);
|
||||
if (state.hostStatuses[host.id]?.status !== "ready") {
|
||||
yield* setHostStatus(host.id, { status: "ready" });
|
||||
}
|
||||
return { hostId: host.id, ...ready };
|
||||
},
|
||||
lifecycleLock.withPermit,
|
||||
@@ -249,25 +246,18 @@ export const make = Effect.gen(function* () {
|
||||
const summary = yield* host.summary;
|
||||
if (!summary.platforms.some((platform) => platform.available)) return null;
|
||||
const ready = yield* host
|
||||
.ensureAgentReady((phase) =>
|
||||
publish((state) => ({ ...state, hostStatus: phase, hostStatusDetail: undefined })).pipe(
|
||||
Effect.asVoid,
|
||||
),
|
||||
)
|
||||
.ensureAgentReady((phase) => setHostStatus(host.id, { status: phase }).pipe(Effect.asVoid))
|
||||
.pipe(
|
||||
Effect.tapError((error) =>
|
||||
publish((state) => ({
|
||||
...state,
|
||||
hostStatus: "failed",
|
||||
hostStatusDetail: error.message,
|
||||
})),
|
||||
setHostStatus(host.id, { status: "failed", detail: error.message }),
|
||||
),
|
||||
Effect.mapError(
|
||||
(error) => new DeviceHostUnavailableError({ hostId: host.id, reason: error.message }),
|
||||
),
|
||||
);
|
||||
const hostSummaries = yield* Effect.forEach(hosts.values(), (candidate) => candidate.summary);
|
||||
yield* publish((state) => ({ ...state, hosts: hostSummaries, hostStatus: "ready" }));
|
||||
yield* publish((state) => ({ ...state, hosts: hostSummaries }));
|
||||
yield* setHostStatus(host.id, { status: "ready" });
|
||||
return { hostId: host.id, ...ready };
|
||||
}, lifecycleLock.withPermit);
|
||||
|
||||
@@ -357,8 +347,15 @@ export const make = Effect.gen(function* () {
|
||||
return yield* publish((state) => ({
|
||||
...state,
|
||||
hosts: hostSummaries,
|
||||
devices,
|
||||
hostStatusDetail: detail,
|
||||
...(ready.hostId === LOCAL_DEVICE_HOST_ID ? { hostStatusDetail: detail } : {}),
|
||||
devices: [
|
||||
...state.devices.filter((device) => device.hostId !== ready.hostId),
|
||||
...devices,
|
||||
],
|
||||
hostStatuses: {
|
||||
...state.hostStatuses,
|
||||
[ready.hostId]: { status: "ready", ...(detail ? { detail } : {}) },
|
||||
},
|
||||
}));
|
||||
}),
|
||||
);
|
||||
@@ -366,18 +363,21 @@ export const make = Effect.gen(function* () {
|
||||
|
||||
const list: DeviceService["Service"]["list"] = Effect.gen(function* () {
|
||||
if (!(yield* readDeviceSettings).enabled) return (yield* SynchronizedRef.get(stateRef)).state;
|
||||
const ready = yield* readiness();
|
||||
return yield* refresh(ready);
|
||||
}).pipe(
|
||||
Effect.tapError((error) =>
|
||||
publish((state) =>
|
||||
state.hostStatus === "disabled"
|
||||
? state
|
||||
: { ...state, hostStatus: "failed", hostStatusDetail: error.message },
|
||||
),
|
||||
),
|
||||
Effect.withSpan("DeviceService.list"),
|
||||
);
|
||||
yield* Effect.forEach(
|
||||
hosts.values(),
|
||||
(host) =>
|
||||
Effect.gen(function* () {
|
||||
const ready = yield* readinessIfSupported(host.id);
|
||||
if (ready) yield* refresh(ready);
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
setHostStatus(host.id, { status: "failed", detail: error.message }),
|
||||
),
|
||||
),
|
||||
{ concurrency: 4 },
|
||||
);
|
||||
return (yield* SynchronizedRef.get(stateRef)).state;
|
||||
}).pipe(Effect.withSpan("DeviceService.list"));
|
||||
|
||||
const configure: DeviceService["Service"]["configure"] = Effect.fn("DeviceService.configure")(
|
||||
function* (input) {
|
||||
@@ -415,6 +415,7 @@ export const make = Effect.gen(function* () {
|
||||
...state,
|
||||
hostStatus: nextEnabled ? "idle" : "disabled",
|
||||
hostStatusDetail: undefined,
|
||||
hostStatuses: {},
|
||||
devices: nextEnabled ? state.devices : [],
|
||||
sessions: nextEnabled ? state.sessions : [],
|
||||
bootingDevices: nextEnabled ? state.bootingDevices : [],
|
||||
@@ -636,6 +637,7 @@ export const make = Effect.gen(function* () {
|
||||
const closing = state.sessions.filter(
|
||||
(session) =>
|
||||
session.threadId === input.threadId &&
|
||||
(input.hostId === undefined || session.hostId === input.hostId) &&
|
||||
(input.deviceId === undefined || session.deviceId === input.deviceId),
|
||||
);
|
||||
if (closing.length === 0) return;
|
||||
@@ -749,6 +751,11 @@ export const make = Effect.gen(function* () {
|
||||
});
|
||||
});
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const host = yield* DeviceHost.DeviceHost;
|
||||
return yield* makeWithHosts(new Map([[host.id, host]]));
|
||||
});
|
||||
|
||||
export const layer = Layer.effect(DeviceService, make).pipe(Layer.provide(LocalDeviceHost.layer));
|
||||
|
||||
/** State stream for WS subscribers: current snapshot first, then every change. */
|
||||
|
||||
@@ -56,6 +56,7 @@ const state = {
|
||||
},
|
||||
],
|
||||
hostStatus: "ready" as const,
|
||||
hostStatuses: { local: { status: "ready" as const } },
|
||||
devices: [device],
|
||||
sessions: [],
|
||||
onboardingCompleted: true,
|
||||
|
||||
@@ -161,7 +161,9 @@ const handlers = {
|
||||
const target =
|
||||
input.deviceId !== undefined
|
||||
? { hostId: input.hostId ?? LOCAL_DEVICE_HOST_ID, deviceId: input.deviceId }
|
||||
: sessions.at(-1);
|
||||
: sessions
|
||||
.filter((session) => input.hostId === undefined || session.hostId === input.hostId)
|
||||
.at(-1);
|
||||
if (!target) {
|
||||
return yield* new DeviceToolUnavailableError({
|
||||
reason: "No device is open in this thread. Call device_open first.",
|
||||
@@ -183,6 +185,7 @@ const handlers = {
|
||||
const devices = yield* DeviceService.DeviceService;
|
||||
yield* devices.close({
|
||||
threadId: scope.threadId,
|
||||
...(input.hostId === undefined ? {} : { hostId: input.hostId }),
|
||||
...(input.deviceId === undefined ? {} : { deviceId: input.deviceId }),
|
||||
...(input.shutdown === undefined ? {} : { shutdown: input.shutdown }),
|
||||
});
|
||||
|
||||
@@ -1669,7 +1669,8 @@ const NodeHttpServerTestWithWsDeflate = HttpServer.layerTestClient.pipe(
|
||||
|
||||
const EMPTY_DEVICE_STATE: DeviceServiceState = {
|
||||
hosts: [],
|
||||
hostStatus: "idle",
|
||||
hostStatus: "disabled",
|
||||
hostStatuses: {},
|
||||
devices: [],
|
||||
sessions: [],
|
||||
onboardingCompleted: false,
|
||||
|
||||
Reference in new issue
Block a user