Skip to content

Commit 1b192b2

Browse files
feat(devices): lease simulators and emulators from a Simlock pool (alpha)
Adds a Simlock device host behind Settings → Beta. Opening a device takes a lease through the gateway's agent-device front, agents drive it through device_open with the session bound to that lease, and the Device panel shows Simlock's watch-only screen stream on web and mobile. T3 never installs Simlock. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d1f7018 commit 1b192b2

28 files changed

Lines changed: 1844 additions & 197 deletions

‎apps/mobile/src/features/devices/DevicePreviewRouteScreen.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,22 @@ function OpenDevicePreview({
407407
refreshDeviceHubAccess(environmentId);
408408
return () => void onInputConnected(false);
409409
}, [environmentId, onInputConnected]);
410+
if (preview.viewOnly?.unavailableReason) {
411+
return (
412+
<View className="flex-1 items-center justify-center px-6">
413+
<AppText selectable className="text-center text-sm text-foreground-muted">
414+
{preview.viewOnly.unavailableReason}
415+
</AppText>
416+
</View>
417+
);
418+
}
410419
return access ? (
411420
<DeviceStreamWebView
412421
ref={streamRef}
413422
access={access}
414423
platform={session.platform}
415424
deviceId={session.deviceId}
425+
viewOnly={!!preview.viewOnly}
416426
colors={{
417427
background: themeVariables["--color-sheet-solid"],
418428
foreground: themeVariables["--color-foreground"],

‎apps/mobile/src/features/devices/DeviceStreamWebView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function DeviceStreamWebView({
4646
access: props.access,
4747
platform: props.platform,
4848
deviceId: props.deviceId,
49+
...(props.viewOnly ? { viewOnly: true } : {}),
4950
colors: props.colors,
5051
});
5152
return (

‎apps/mobile/src/features/devices/device-stream-document.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface DeviceStreamConfiguration {
66
readonly access: DeviceHubAccess;
77
readonly platform: DevicePlatform;
88
readonly deviceId: string;
9+
readonly viewOnly?: boolean;
910
readonly colors: {
1011
readonly background: string;
1112
readonly foreground: string;

‎apps/mobile/src/features/devices/threadDevicePreviews.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export function threadDevicePreviews(state: DeviceServiceState | null, threadId:
1414
session,
1515
name: device?.name ?? (session.platform === "ios" ? "iOS Simulator" : "Android Emulator"),
1616
description: [device?.version, host?.label].filter(Boolean).join(" · "),
17+
// Simlock streams take no input; without an operator token there is no stream.
18+
viewOnly:
19+
host?.kind === "simlock" ? { unavailableReason: host.streamUnavailable } : undefined,
1720
};
1821
});
1922
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import type { AgentDaemonEndpoint } from "./DeviceHost.ts";
99

1010
const encodeEndpoint = Schema.encodeEffect(
1111
Schema.fromJsonString(
12-
Schema.Struct({ daemonBaseUrl: Schema.String, daemonAuthToken: Schema.String }),
12+
Schema.Struct({
13+
daemonBaseUrl: Schema.String,
14+
daemonAuthToken: Schema.String,
15+
stateDir: Schema.optionalKey(Schema.String),
16+
}),
1317
),
1418
);
1519

@@ -40,14 +44,15 @@ export const agentDeviceSession = (threadId: string, hostId: string, deviceId: s
4044

4145
export const writeAgentDeviceConfig = Effect.fn("AgentDeviceTarget.writeConfig")(function* (
4246
file: string,
43-
endpoint: AgentDaemonEndpoint,
47+
endpoint: AgentDaemonEndpoint & { readonly stateDir?: string | undefined },
4448
) {
4549
const fs = yield* FileSystem.FileSystem;
4650
const path = yield* Path.Path;
4751
yield* fs.makeDirectory(path.dirname(file), { recursive: true });
4852
const content = yield* encodeEndpoint({
4953
daemonBaseUrl: endpoint.baseUrl,
5054
daemonAuthToken: endpoint.token,
55+
...(endpoint.stateDir === undefined ? {} : { stateDir: endpoint.stateDir }),
5156
});
5257
if ((yield* fs.readFileString(file).pipe(Effect.orElseSucceed(() => ""))) === content) return;
5358
const temporary = yield* fs.makeTempFile({ directory: path.dirname(file), prefix: ".endpoint-" });

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ export interface DeviceOpenContext {
8484

8585
export type AgentDaemonEndpoint = Pick<AgentDeviceEndpoint, "baseUrl" | "token">;
8686

87+
export interface AgentDeviceBinding extends AgentDaemonEndpoint {
88+
/** Null when the session alone picks the device and selector flags would be refused. */
89+
readonly agentDeviceId: string | null;
90+
/** The one session the device is leased to, shared by every thread. */
91+
readonly session?: string;
92+
/** agent-device state holding that session's connection. */
93+
readonly stateDir?: string;
94+
/** agent-device accepts installs into the session only from the directory that opened it. */
95+
readonly installDirectory?: string;
96+
}
97+
8798
/** Where the proxy sends a device hub request, for hosts that serve several upstreams. */
8899
export interface HubUpstream {
89100
readonly url: string;
@@ -124,10 +135,7 @@ export interface DeviceOperations {
124135
*/
125136
readonly agentEndpoint?: (
126137
deviceId: string,
127-
) => Effect.Effect<
128-
AgentDaemonEndpoint & { readonly agentDeviceId: string },
129-
DeviceHostUnavailableError
130-
>;
138+
) => Effect.Effect<AgentDeviceBinding, DeviceHostUnavailableError>;
131139
/**
132140
* Set by hosts that end devices on their own (EAS stops sessions at their
133141
* limits), so a session on a device the host stops listing is dropped.

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

Lines changed: 90 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
type DeviceEasAccountInput,
2323
type DeviceHostConfig,
2424
type EasDeviceHostConfig,
25+
type SimlockDeviceHostConfig,
2526
type SshDeviceHostConfig,
2627
type DeviceHostSummary,
2728
LOCAL_DEVICE_HOST_ID,
@@ -59,6 +60,7 @@ import * as ProcessRunner from "../processRunner.ts";
5960
import * as DeviceHost from "./DeviceHost.ts";
6061
import * as SshDeviceHost from "./SshDeviceHost.ts";
6162
import * as EasDeviceHost from "./EasDeviceHost.ts";
63+
import * as SimlockDeviceHost from "./SimlockDeviceHost.ts";
6264
import { makeExpoApi, resolveExpoCredential } from "./ExpoApi.ts";
6365
import * as Exit from "effect/Exit";
6466
import * as LocalDeviceHost from "./LocalDeviceHost.ts";
@@ -121,7 +123,11 @@ export class DeviceService extends Context.Service<
121123
hostId: DeviceHostId;
122124
deviceId: DeviceId;
123125
}) => Effect.Effect<
124-
{ readonly agentDeviceId: string; readonly args: ReadonlyArray<string> },
126+
{
127+
readonly agentDeviceId: string | null;
128+
readonly args: ReadonlyArray<string>;
129+
readonly installDirectory?: string | undefined;
130+
},
125131
DeviceError
126132
>;
127133
readonly state: Effect.Effect<DeviceServiceState>;
@@ -173,7 +179,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
173179
),
174180
configureAgent: (
175181
hostId: DeviceHostId,
176-
endpoint: DeviceHost.AgentDaemonEndpoint,
182+
endpoint: DeviceHost.AgentDaemonEndpoint & { readonly stateDir?: string | undefined },
177183
deviceId?: DeviceId,
178184
) => Effect.Effect<string, DeviceError> = (hostId) =>
179185
Effect.fail(
@@ -209,6 +215,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
209215
supportsToolUpdate: installTool !== undefined,
210216
supportsToolInspection: true,
211217
supportsEasHosts: true,
218+
supportsSimlockHosts: true,
212219
hosts: initialHosts,
213220
hostStatus: initialSettings.enabled ? "idle" : "disabled",
214221
hostStatuses: {},
@@ -841,39 +848,48 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function*
841848
reason:
842849
"Agent device access requires enabled device support, agent access, and an available simulator platform on this host.",
843850
});
844-
const { configPath, agentDeviceId } = yield* lifecycleLock.withPermit(
845-
Effect.gen(function* () {
846-
if (hosts.get(host.id) !== host)
847-
return yield* new DeviceHostUnavailableError({
848-
hostId: host.id,
849-
hostLabel: host.label,
850-
reason: "Host configuration changed. Retry the operation.",
851-
});
852-
if (ready.devices.agentEndpoint) {
853-
const endpoint = yield* ready.devices.agentEndpoint(input.deviceId);
851+
const { configPath, agentDeviceId, boundSession, installDirectory } =
852+
yield* lifecycleLock.withPermit(
853+
Effect.gen(function* () {
854+
if (hosts.get(host.id) !== host)
855+
return yield* new DeviceHostUnavailableError({
856+
hostId: host.id,
857+
hostLabel: host.label,
858+
reason: "Host configuration changed. Retry the operation.",
859+
});
860+
if (ready.devices.agentEndpoint) {
861+
const endpoint = yield* ready.devices.agentEndpoint(input.deviceId);
862+
return {
863+
configPath: yield* configureAgent(input.hostId, endpoint, input.deviceId),
864+
agentDeviceId: endpoint.agentDeviceId,
865+
boundSession: endpoint.session,
866+
installDirectory: endpoint.installDirectory,
867+
};
868+
}
869+
if (!ready.agentDevice)
870+
return yield* new DeviceHostUnavailableError({
871+
hostId: host.id,
872+
hostLabel: host.label,
873+
reason: "This host has no agent-device daemon.",
874+
});
854875
return {
855-
configPath: yield* configureAgent(input.hostId, endpoint, input.deviceId),
856-
agentDeviceId: endpoint.agentDeviceId,
876+
configPath: yield* configureAgent(input.hostId, ready.agentDevice),
877+
agentDeviceId: input.deviceId,
878+
boundSession: undefined,
879+
installDirectory: undefined,
857880
};
858-
}
859-
if (!ready.agentDevice)
860-
return yield* new DeviceHostUnavailableError({
861-
hostId: host.id,
862-
hostLabel: host.label,
863-
reason: "This host has no agent-device daemon.",
864-
});
865-
return {
866-
configPath: yield* configureAgent(input.hostId, ready.agentDevice),
867-
agentDeviceId: input.deviceId,
868-
};
869-
}),
870-
);
871-
const session = yield* agentDeviceSession(
872-
input.threadId,
873-
input.hostId,
874-
input.deviceId,
875-
).pipe(Effect.provideService(Crypto.Crypto, crypto));
876-
return { agentDeviceId, args: ["--config", configPath, "--session", session] };
881+
}),
882+
);
883+
const session =
884+
boundSession ??
885+
(yield* agentDeviceSession(input.threadId, input.hostId, input.deviceId).pipe(
886+
Effect.provideService(Crypto.Crypto, crypto),
887+
));
888+
return {
889+
agentDeviceId,
890+
args: ["--config", configPath, "--session", session],
891+
installDirectory,
892+
};
877893
}),
878894
state: SynchronizedRef.get(stateRef).pipe(Effect.map(({ state }) => state)),
879895
subscribe: PubSub.subscribe(statePubSub),
@@ -932,7 +948,7 @@ export const make = Effect.gen(function* () {
932948
const configureAgent = Effect.fn("DeviceService.configureAgent")(
933949
function* (
934950
hostId: DeviceHostId,
935-
endpoint: DeviceHost.AgentDaemonEndpoint,
951+
endpoint: DeviceHost.AgentDaemonEndpoint & { readonly stateDir?: string | undefined },
936952
deviceId?: DeviceId,
937953
) {
938954
const file = yield* configPath(hostId, deviceId);
@@ -994,6 +1010,23 @@ export const make = Effect.gen(function* () {
9941010
yield* Effect.context<Effect.Services<ReturnType<typeof SshDeviceHost.make>>>();
9951011
const easContext =
9961012
yield* Effect.context<Effect.Services<ReturnType<typeof EasDeviceHost.make>>>();
1013+
const simlockContext =
1014+
yield* Effect.context<Effect.Services<ReturnType<typeof SimlockDeviceHost.make>>>();
1015+
const simlockAgentCli = Effect.fn("DeviceService.simlockAgentCli")(
1016+
function* (hostId: string) {
1017+
const nodePath = yield* resolveNodeExecutable("Device automation");
1018+
const tool = yield* ensureAgentDevice(config.baseDir).pipe(
1019+
Effect.mapError(
1020+
(cause) =>
1021+
new DeviceHost.DeviceHostError({ hostId, step: "installing agent tools", cause }),
1022+
),
1023+
);
1024+
return { nodePath, entryPath: tool.entryPath };
1025+
},
1026+
Effect.provideService(FileSystem.FileSystem, fs),
1027+
Effect.provideService(Path.Path, path),
1028+
Effect.provideService(ProcessRunner.ProcessRunner, runner),
1029+
);
9971030
const configured = new Map<string, { config: DeviceHostConfig; scope: Scope.Closeable }>();
9981031
const unchanged = (current: DeviceHostConfig, previous: DeviceHostConfig) =>
9991032
current.kind === "ssh" && previous.kind === "ssh"
@@ -1038,6 +1071,16 @@ export const make = Effect.gen(function* () {
10381071
if (configured.has(host.id)) continue;
10391072
added = true;
10401073
const hostScope = yield* Scope.fork(scope);
1074+
if (host.kind === "simlock") {
1075+
// Closing this scope releases the host's leases back to the pool.
1076+
const instance = yield* SimlockDeviceHost.make(host, simlockAgentCli(host.id)).pipe(
1077+
Effect.provideService(Scope.Scope, hostScope),
1078+
Effect.provide(simlockContext),
1079+
);
1080+
hosts.set(host.id, instance);
1081+
configured.set(host.id, { config: host, scope: hostScope });
1082+
continue;
1083+
}
10411084
if (host.kind === "eas") {
10421085
// Closing this scope stops the host's running sessions, so a changed
10431086
// limit or token never leaves a billed session behind.
@@ -1079,14 +1122,22 @@ export const make = Effect.gen(function* () {
10791122
);
10801123
});
10811124
const changes = yield* settings.subscribeChanges;
1082-
// Settings keep EAS hosts apart from SSH hosts; an id both lists use stays the SSH host's.
1125+
// Each kind has its own settings list; an id two lists share stays the first one's.
10831126
const configuredHosts = (value: {
10841127
readonly deviceHosts: ReadonlyArray<SshDeviceHostConfig>;
10851128
readonly easDeviceHosts: ReadonlyArray<EasDeviceHostConfig>;
1086-
}): ReadonlyArray<DeviceHostConfig> => [
1087-
...value.deviceHosts,
1088-
...value.easDeviceHosts.filter((host) => !value.deviceHosts.some((ssh) => ssh.id === host.id)),
1089-
];
1129+
readonly simlockDeviceHosts: ReadonlyArray<SimlockDeviceHostConfig>;
1130+
readonly simlockDeviceHostsEnabled: boolean;
1131+
}): ReadonlyArray<DeviceHostConfig> => {
1132+
const hosts: Array<DeviceHostConfig> = [...value.deviceHosts];
1133+
for (const host of [
1134+
...value.easDeviceHosts,
1135+
...(value.simlockDeviceHostsEnabled ? value.simlockDeviceHosts : []),
1136+
]) {
1137+
if (!hosts.some((existing) => existing.id === host.id)) hosts.push(host);
1138+
}
1139+
return hosts;
1140+
};
10901141
yield* reconcile(configuredHosts(yield* settings.getSettings));
10911142
yield* changes.pipe(
10921143
Stream.runForEach((value) => reconcile(configuredHosts(value))),

0 commit comments

Comments
 (0)