Skip to content

Commit d2eeacd

Browse files
feat(devices): connect simulator hosts over SSH (#10856)
1 parent 7734c6d commit d2eeacd

30 files changed

Lines changed: 1794 additions & 78 deletions

‎apps/server/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@effect/vitest": "catalog:",
4141
"@t3tools/contracts": "workspace:*",
4242
"@t3tools/shared": "workspace:*",
43+
"@t3tools/ssh": "workspace:*",
4344
"@t3tools/tailscale": "workspace:*",
4445
"@t3tools/web": "workspace:*",
4546
"@types/bun": "1.3.14",

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export const RPC_REQUIRED_SCOPES = {
145145
[WS_METHODS.subscribePreviewEvents]: AuthOrchestrationReadScope,
146146
[WS_METHODS.subscribeDiscoveredLocalServers]: AuthOrchestrationReadScope,
147147
[WS_METHODS.deviceConfigure]: AuthOrchestrationOperateScope,
148+
[WS_METHODS.deviceTestHost]: AuthOrchestrationOperateScope,
148149
[WS_METHODS.deviceList]: AuthOrchestrationReadScope,
149150
[WS_METHODS.deviceOpen]: AuthOrchestrationOperateScope,
150151
[WS_METHODS.deviceClose]: AuthOrchestrationOperateScope,

‎apps/server/src/device/DeviceActions.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const makeReady = (
1616
) => {
1717
const calls: Call[] = [];
1818
const ready: DeviceHostReady = {
19+
nodePath: process.execPath,
1920
hub: { origin: "http://127.0.0.1:1" },
2021
helpers,
2122
run: (command, args, options) => {

‎apps/server/src/device/DeviceActions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ const serveSimPermissions = (
327327
reason: "helper_missing",
328328
});
329329
yield* ready
330-
.run(process.execPath, [
330+
.run(ready.nodePath, [
331331
cli,
332332
"permissions",
333333
input.decision,

‎apps/server/src/device/DeviceHost.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ export interface DeviceHubEndpoint {
4646
export interface AgentDeviceEndpoint {
4747
readonly baseUrl: string;
4848
readonly token: string;
49-
/** Absolute path of the agent-device entry script for the provider PATH shim. */
49+
/** Host-local path of the agent-device entry script. The provider uses a separate local CLI install. */
5050
readonly entryPath: string;
5151
}
5252

5353
export interface DeviceHostReady {
54+
readonly nodePath: string;
5455
readonly hub: DeviceHubEndpoint;
5556
/**
5657
* Runs a host command (`xcrun`, `adb`, or a helper bundled with the hub)

‎apps/server/src/device/DeviceMultiHost.test.ts‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { expect, it } from "@effect/vitest";
22
import { ThreadId } from "@t3tools/contracts";
3+
import * as Deferred from "effect/Deferred";
4+
import * as Fiber from "effect/Fiber";
35
import * as Effect from "effect/Effect";
46
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
57
import { ServerSettingsService } from "../serverSettings.ts";
@@ -10,6 +12,7 @@ it.effect("keeps hosts independent when serials collide and another host fails",
1012
Effect.gen(function* () {
1113
const host = (id: string, failed = false): DeviceHost["Service"] => {
1214
const ready = {
15+
nodePath: process.execPath,
1316
hub: { origin: `http://${id}` },
1417
agentDevice: { baseUrl: `http://${id}`, token: "test", entryPath: "/agent-device" },
1518
run: () => Effect.succeed({ stdout: "", stderr: "", code: 0 }),
@@ -20,10 +23,10 @@ it.effect("keeps hosts independent when serials collide and another host fails",
2023
summary: Effect.succeed({
2124
id,
2225
label: id,
23-
kind: "local",
26+
kind: id === "b" ? "ssh" : "local",
2427
hubInstalled: true,
2528
agentDeviceInstalled: true,
26-
platforms: [{ platform: "android", available: true }],
29+
platforms: id === "b" ? [] : [{ platform: "android", available: true }],
2730
}),
2831
platformAvailability: (platform) => Effect.succeed({ platform, available: true }),
2932
ensureReady: () =>
@@ -59,9 +62,19 @@ it.effect("keeps hosts independent when serials collide and another host fails",
5962
),
6063
);
6164
const hosts = new Map(["a", "b", "offline"].map((id) => [id, host(id, id === "offline")]));
62-
const service = yield* makeWithHosts(hosts).pipe(
63-
Effect.provideService(HttpClient.HttpClient, http),
64-
);
65+
const writeStarted = yield* Deferred.make<void>();
66+
const finishWrite = yield* Deferred.make<void>();
67+
const order: string[] = [];
68+
const service = yield* makeWithHosts(hosts, undefined, () =>
69+
Effect.gen(function* () {
70+
order.push("write started");
71+
yield* Deferred.succeed(writeStarted, undefined);
72+
yield* Deferred.await(finishWrite);
73+
order.push("write finished");
74+
return "/host-config.json";
75+
}),
76+
).pipe(Effect.provideService(HttpClient.HttpClient, http));
77+
expect(yield* service.agentReadinessIfSupported("b")).not.toBeNull();
6578
const listed = yield* service.list;
6679
expect(listed.devices.map((device) => device.hostId).sort()).toEqual(["a", "b"]);
6780
expect(listed.hostStatuses.offline?.status).toBe("failed");
@@ -74,8 +87,35 @@ it.effect("keeps hosts independent when serials collide and another host fails",
7487
expect(state.sessions.map((session) => session.hostId)).toEqual(["b"]);
7588
expect(state.hostStatuses.a?.status).toBe("ready");
7689
expect(state.hostStatuses.offline?.status).toBe("failed");
77-
yield* service.agentReadinessIfSupported("b");
78-
expect((yield* service.state).hostStatuses.b?.status).toBe("ready");
90+
const targeting = yield* service
91+
.agentTarget({ threadId, hostId: "b", deviceId: "emulator-5554" })
92+
.pipe(Effect.forkChild);
93+
yield* Deferred.await(writeStarted);
94+
const replacing = yield* service
95+
.withLifecycleLock(
96+
Effect.gen(function* () {
97+
order.push("replace");
98+
hosts.set("b", host("b"));
99+
yield* service.refreshHosts;
100+
}),
101+
)
102+
.pipe(Effect.forkChild);
103+
yield* Deferred.succeed(finishWrite, undefined);
104+
yield* Fiber.join(targeting);
105+
yield* Fiber.join(replacing);
106+
expect(order).toEqual(["write started", "write finished", "replace"]);
107+
const replaced = yield* service.state;
108+
expect(replaced.sessions).toEqual([]);
109+
expect(replaced.devices.map((device) => device.hostId)).toEqual(["a"]);
110+
expect(replaced.hostStatuses.b).toBeUndefined();
111+
yield* service.open({ threadId, hostId: "b", deviceId: "emulator-5554", platform: "android" });
112+
hosts.delete("b");
113+
yield* service.refreshHosts;
114+
yield* service.setHostStatus("b", { status: "ready" });
115+
expect((yield* service.state).hostStatuses.b).toBeUndefined();
116+
expect((yield* service.state).sessions).toEqual([]);
117+
yield* service.agentReadinessIfSupported("a");
118+
expect((yield* service.state).hostStatuses.a?.status).toBe("ready");
79119
yield* service.configure({ enabled: false });
80120
expect((yield* service.state).hostStatuses).toEqual({});
81121
}).pipe(

‎apps/server/src/device/DeviceService.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const fixture = Effect.fn("fixture")(function* (
7070
let booted = false;
7171
let shutDown = false;
7272
const ready: DeviceHost.DeviceHostReady = {
73+
nodePath: process.execPath,
7374
hub: { origin: "http://device.test" },
7475
helpers: { serveSimAxSettings: null, serveSimCli: null },
7576
run: () => Effect.succeed({ code: 0, stdout: "Pixel_API_35\n", stderr: "" }),

0 commit comments

Comments
 (0)