Open History to see the last commit
1# Devices
2
3The environment server owns simulators and emulators the way it owns
4terminals: discovery, streaming, and agent access all run there, and every
5client reaches them through the environment connection. This is what makes the
6Device panel work over Tailscale and T3 Connect, including when an SSH host runs the devices.
7
8## Two external tools, one seam
9
10[expo-device-hub](../../apps/server/src/device/LocalDeviceHost.ts) streams and
11[agent-device](../../apps/server/src/device/AgentDeviceShim.ts) drives. Each is
12npm-installed at a pinned version into the T3 home after its matching Device
13panel consent step. Manual setup installs and starts only expo-device-hub;
14agent-device remains absent and stopped until agent access is granted. Both run
15with the server's Node; `npx` would make the first `device_open` after a reboot
16depend on the registry. The hub is a supervised child rather than an imported
17middleware because serve-sim loads private CoreSimulator frameworks through a
18native addon, and a crash there must not take the server down.
19
20Everything platform-specific sits behind
21[`DeviceHost`](../../apps/server/src/device/DeviceHost.ts). The service lists,
22boots, and shuts down devices through the host's device operations, and the
23proxy and the MCP tools only see a hub origin and an agent-device endpoint.
24Hosts that run expo-device-hub share one hub-backed implementation of those
25operations, so a host whose devices live elsewhere only replaces them.
26SSH hosts forward both endpoints to server loopback. Every proxied request
27also carries the host id; device ids alone are not unique across hosts.
28
29## EAS Simulator hosts are billed sessions
30
31An [EAS host](../../apps/server/src/device/EasDeviceHost.ts) has no machine:
32each opened device is an EAS Simulator session that bills the Expo account
33until it stops. Every session is created with the host's run and idle limits,
34the host refuses to start past its session limit, and closing the host stops
35its sessions. Sessions it starts are recorded under the state directory and
36stopped by the next server if this one dies; sessions other tools started on
37the same account are never touched.
38
39Each session brings its own serve-sim preview host and agent-device daemon,
40with their own tokens. The proxy asks the host to route each request
41(`DeviceHostReady.upstream`) and adds the session token server-side, so
42clients never hold it. Agent config files are written per device
43(`DeviceOperations.agentEndpoint`), which is why `device_open` opens the
44device before targeting it. Device settings run `simctl` on the simulator's
45machine, which EAS does not expose, so they are unavailable there.
46
47## The hub is never exposed
48
49serve-sim has a shell-exec route whose token is readable from its own
50unauthenticated `/api`, and serve-emu's action routes have no auth at all. The
51hub binds loopback and the only way in is the
52[proxy](../../apps/server/src/device/DeviceHubProxy.ts), which allowlists the
53stream, config, and screenshot routes and authenticates every request as an
54environment session. `<img>` and `WebSocket` cannot carry headers, so the proxy
55authenticates like the `/ws` upgrade: cookie, or a short-lived `wsTicket` that
56bearer and DPoP clients mint over authenticated HTTP. The ticket is stripped
57before the request reaches the hub.
58
59Stream responses carry `Cache-Control: no-transform`; the compression
60middleware would otherwise buffer an MJPEG body that never ends. Browsers paint an
61MJPEG part only once the next part begins and the hub writes one only when the
62screen changes, so the proxy
63[re-sends the last part](../../apps/server/src/device/mjpegStillFrame.ts) after a
64short quiet period. In browser dev, the Vite proxy must forward WebSocket
65upgrades for `/api`, not only `/ws`.
66
67## Device settings never go through the hub
68
69serve-sim's preview drives its Tools panel by sending shell commands over that
70same exec channel. Proxying it, even allowlisted, would hand any environment
71session arbitrary command execution on the host, so T3 does not. The
72[`device.action`](../../apps/server/src/device/DeviceActions.ts) RPC runs the
73underlying `simctl`, `adb`, and serve-sim helper binaries itself through
74`DeviceHostReady.run`, one typed action per control, and returns the settings
75it reads back. The proxy allowlist grows only with read routes (accessibility
76tree, foreground app, event log) and refuses non-GET methods everywhere except
77screenshot capture and stream tuning.
78
79## Agents drive through the CLI
80
81The `device_*` toolkit is deliberately four tools: list, open, screenshot, and
82close. Driving happens through the `agent-device` CLI, which has the semantic
83snapshot model agents need and stays current with its own releases. T3 prepends
84a shim directory to the provider's PATH. The CLI installs on the environment
85server even when that server cannot run simulators. Hosts start on demand.
86
87That environment is fixed when the provider subprocess spawns, so
88[`prepareMcpSession`](../../apps/server/src/orchestration-v2/ProviderSessionManager.ts)
89starts agent-device only when device support and agent access have both been
90enabled, the session has the `device` capability, and the machine can run at
91least one platform. Starting it later from `device_open` would leave the
92already-running agent without the CLI.
93
94How to drive a device is returned from `device_open`, not kept in an
95always-loaded prompt or skill: it costs nothing in threads that never open a
96device and cannot drift from the pinned CLI version. The always-on prompt block
97is a few lines that point at the tools and prefer them over raw `simctl` and
98`adb`, which stay available for what the tools do not cover.
99
100## The viewer decodes both vendored protocols
101
102The hub vendors two streaming servers with different wire formats. iOS video is
103an HTTP body of AVCC envelopes decoded with WebCodecs, with input on a separate
104binary WebSocket; Android multiplexes SEMU-framed H.264 and JSON gestures over
105one WebSocket. [`deviceStream.ts`](../../apps/web/src/components/device/deviceStream.ts)
106speaks both so one panel covers both platforms.
107
108Simulators encode H.264 High 5.1 without frame reordering, but the hub's fallback
109encoder can omit the SPS VUI that declares its reorder limits. Chromium's hardware
110decoder then retains frames for seconds despite `optimizeForLatency` and an empty
111`decodeQueueSize`. The iOS AVCC viewer restores the missing metadata before
112configuring the decoder so hardware decoding can remain enabled. WebCodecs is
113secure-context only, so the viewer probes `isConfigSupported` and falls back to
114MJPEG on iOS when decoding is unavailable. Android has no MJPEG; there the panel
115reports that it cannot decode.
116
117serve-sim can also be locked to WebRTC, as on EAS Simulator, and then refuses
118AVCC with a 409 naming the transport. The viewer still asks for AVCC first, so
119hubs that serve it pay nothing, and switches on that 409, or when its MJPEG
120image fails in a browser that has `RTCPeerConnection`. Offers are not trickled:
121serve-sim takes one offer with every candidate. The video element only feeds
122the same canvas sink, so the panel and the mobile WebView keep one surface.