diff --git a/docs/internal/adr/0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md b/docs/internal/adr/0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md index 8db8a509b..8663ef862 100644 --- a/docs/internal/adr/0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md +++ b/docs/internal/adr/0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md @@ -1,6 +1,6 @@ # 0028. The uplink carries byte channels beside its messages, and small messages go first -- **Status:** Accepted +- **Status:** Accepted; §4 narrowed by [ADR 0049](0049-a-lease-holder-watches-and-drives-its-devices-screen.md) - **Date:** 2026-10-14 - **Issue:** [#552](https://github.com/callstackincubator/simlock/issues/552) - **Narrows:** [ADR 0026](0026-agent-device-through-a-gateway-relays-over-the-uplink.md) diff --git a/docs/internal/adr/0030-optional-modules-share-one-contract-and-load-only-when-turned-on.md b/docs/internal/adr/0030-optional-modules-share-one-contract-and-load-only-when-turned-on.md index 054a75603..06bf8691f 100644 --- a/docs/internal/adr/0030-optional-modules-share-one-contract-and-load-only-when-turned-on.md +++ b/docs/internal/adr/0030-optional-modules-share-one-contract-and-load-only-when-turned-on.md @@ -1,6 +1,6 @@ # 0030. Optional modules share one contract and load only when turned on -- **Status:** Accepted — not yet implemented; §2 narrowed by [ADR 0046](0046-a-standalone-worker-serves-agent-device-by-default-with-a-private-profile.md) +- **Status:** Accepted — not yet implemented; §2 narrowed by [ADR 0046](0046-a-standalone-worker-serves-agent-device-by-default-with-a-private-profile.md); §3 narrowed by [ADR 0049](0049-a-lease-holder-watches-and-drives-its-devices-screen.md) - **Date:** 2026-10-15 - **Issue:** [#591](https://github.com/callstackincubator/simlock/issues/591) - **Supersedes:** nothing. Narrows [ADR diff --git a/docs/internal/adr/0045-a-devices-screen-streams-to-operators-not-a-leases.md b/docs/internal/adr/0045-a-devices-screen-streams-to-operators-not-a-leases.md index b3809bd83..312f25c9d 100644 --- a/docs/internal/adr/0045-a-devices-screen-streams-to-operators-not-a-leases.md +++ b/docs/internal/adr/0045-a-devices-screen-streams-to-operators-not-a-leases.md @@ -1,6 +1,6 @@ # 0045. A device's screen streams to operators, not a lease's -- **Status:** Accepted — not yet implemented +- **Status:** Accepted; §2 narrowed by 0049 (a lease holder streams its own lease) - **Date:** 2026-10-23 - **Issue:** [#906](https://github.com/callstackincubator/simlock/issues/906) - **Depends on:** ADR 0012 (a worker answers fleet routes as a fleet of one), ADR 0030 (stream is an optional module), ADR 0031 (screen-source seam), ADR 0039 (joined workers serve no HTTP of their own). diff --git a/docs/internal/adr/0049-a-lease-holder-watches-and-drives-its-devices-screen.md b/docs/internal/adr/0049-a-lease-holder-watches-and-drives-its-devices-screen.md new file mode 100644 index 000000000..0a4baadb8 --- /dev/null +++ b/docs/internal/adr/0049-a-lease-holder-watches-and-drives-its-devices-screen.md @@ -0,0 +1,146 @@ +# 0049. A lease holder watches and drives its device's screen + +- **Status:** Accepted — not yet implemented +- **Date:** 2026-10-26 +- **Issue:** [#1013](https://github.com/callstackincubator/simlock/issues/1013) +- **Narrows:** [ADR 0045](0045-a-devices-screen-streams-to-operators-not-a-leases.md) + §2 ("Operators only") and its rejected alternative "Keep the lease route + beside the device route": a lease holder now streams its own lease. The + device route, its operator-only rule and §4's lifetime stay. +- **Narrows:** [ADR 0028](0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md) + §4 (a relay is one HTTP exchange; §5 here adds a two-way relay that keeps + both directions of its byte channel open) and + [ADR 0030](0030-optional-modules-share-one-contract-and-load-only-when-turned-on.md) + §3 (a module's routes sit under its name; §2 here lets a module own one + route under a lease). + +## Context + +Only an operator token can watch a device (ADR 0045 §2), and nobody can +tap it: the stream is view-only (`src/stream/AGENTS.md`, Known gaps). An +app that shows a person the device its agent leased must hold an operator +token, which sees and stops every device in the fleet, and still cannot +take over for a sign-in (#1010). + +ADR 0045 dropped the lease route because the console was its only caller, +and a second access model costs tests. It names no security flaw in the +lease-owner model. A second caller now exists: T3 Code's device panel +(pingdotgg/t3code PR #25259), which today asks for an operator token and +runs a view-only client mode. + +T3 Code, EAS and expo-device-hub already speak serve-sim's input messages: +one tag byte, then UTF-8 JSON, on a WebSocket. Expo's serve-sim adds +`0x83` (input admitted) and `inputUnavailable` in `0x82`; T3 Code sends no +input before `0x83`. + +Today's relay carries one HTTP exchange: once the worker's target answers, +the rest of the request body is cut off (`src/daemon/relay.ts:240`). An +input socket needs both directions open at once. + +```mermaid +flowchart LR + App[Agent app] -- operator token only --> GW[Gateway] + GW -- HTTP relay --> W[Worker stream] + App -. no input .-> W +``` + +## Decision + +1. **Two lease routes.** `GET /v1/leases/{id}/stream` serves today's stream + frames for one lease. `WS /v1/leases/{id}/input` takes input for it. Both + answer the lease's owner and any operator; anyone else, and an unknown + lease, get 404 `UNKNOWN_LEASE`, as `GET /v1/leases/{id}` does. Both run on + a gateway and on a standalone worker. +2. **A module may own one route under a lease.** Besides `/v1//*`, an + optional module may own `/v1/leases/{id}/`. The HTTP layer resolves + the lease for the caller once, as `lease.get` does, and hands the module + the lease; the module never checks ownership itself. `stream` owns + `/stream`; a new optional module `input` owns `/input`. A segment no loaded + module owns answers 404. The module host refuses to load a module whose name + is a core lease sub-route (`renew`, `exec`), as it refuses a name that + clashes with a core first segment: the core route wins. +3. **Input speaks expo serve-sim's messages**, pinned at + expo/expo-device-hub `1357114`: `0x03` touch, `0x04` button, `0x06` key, + `0x0b` scroll in; `0x83` admitted, `0x82` screen config, `0x90` control + reply out. Positions are fractions of the screen. The same messages serve + iOS and Android; Simlock adds the button `back` for Android. Rotation + (`0x07`) is dropped on every platform: agents rotate through + agent-device, and the device input re-reads the screen size every 2 s so + taps stay right after they do. A broken message is dropped, as serve-sim + drops it. When + serve-sim changes, Simlock adds a compatibility layer rather than + following silently. +4. **Input is its own capability**, `capabilities.input.enabled`, off by + default, and needs no open stream. A lease has one device input, shared + by up to 8 sockets as serve-sim shares its injector; a 9th socket is + closed with 1013. On Android it is a control-only scrcpy server, on an + iOS simulator a HID helper; a physical iPhone has none and says + `inputUnavailable`. Where keys cannot be injected (an iOS 27 runtime), + `0x82` says `keyboardUnavailable`, as serve-sim does. On an iOS + simulator only Home is pressed (through `simctl`); the app switcher and + Lock are dropped. +5. **The relay gains a two-way mode.** A two-way relay keeps both directions + of its link byte channel open until either side ends it, carries + length-prefixed messages, and ends after 15 s with no bytes from the + worker. The worker's input session sends a heartbeat every 5 s; the + gateway drops it. Every input socket, on a gateway or a standalone + worker, pings its client every 15 s and ends when no pong comes within + 15 s. +6. **The worker ends both when the lease ends.** The stream's last frame is + a new tag `0x07` with `{"reason"}`, on the device route too; readers that + do not know it skip it. The input socket closes with 4001 and the reason. + A link reset carries no text, so the worker sends the reason as a message + before it ends the channel. The gateway also closes what is left when it + sees the lease go. + +```mermaid +sequenceDiagram + participant App as Agent app + participant GW as Gateway + participant W as Worker + App->>GW: WS /v1/leases/L/input (agent token) + GW->>GW: resolve L for the caller (404 if not theirs) + GW->>W: two-way relay on a byte channel + W-->>App: 0x83, 0x82 + App->>W: 0x03 touch … + W-->>GW: heartbeat every 5 s (dropped) + Note over W: lease ends + W-->>App: close 4001 "explicit" + Note over GW,W: 15 s silent, or uplink lost + GW-->>App: close 1011 +``` + +## Consequences + +- An agent token can now see and touch its own lease's screen. It could + already do anything to that device through agent-device, so no new power + is granted; another agent's lease stays 404. +- The HTTP layer gains a lease-route seam, the module host gains a socket + handler and a two-way relay, and `ports` gains upgrade routing beside the + uplink's. The protocol is bumped once for the relay. +- A person cannot rotate a device through Simlock. On iOS simulators they + also cannot type hardware keys on iOS 27 runtimes or press the app + switcher or Lock. On 2026-10-26, on Xcode 27, neither serve-sim's + GSEvent, nor Simulator.app (Xcode 27 ships none), nor CoreDevice's vendor + channel (it cannot see a custom device set, and does not rotate a + non-foldable iPhone) rotated a Simlock simulator. #1024 follows up. +- Two viewers still run two captures (#602). + +## Alternatives considered + +- **A short-lived preview `{url, token}` per lease.** Lets a browser connect + directly. Rejected for now: the first caller proxies every request. +- **serve-emu on Android.** No change for T3 Code on Android, but a second + third-party protocol to follow. Rejected. +- **EvanBacon serve-sim, the copy we vendored.** It has no `0x83`, so T3 + Code would never send input. Rejected. +- **Routes under the module prefixes** (`/v1/stream/leases/{id}`, + `/v1/input/leases/{id}`). Fits the one-prefix rule with no new seam. + Rejected: the maintainer chose the lease-scoped paths. +- **Rotation through Simulator.app or CoreDevice on iOS, and through + `user_rotation` on Android.** Rejected: the iOS paths do not work on + Xcode 27 (see Consequences), and rotating Android alone would give the + two platforms different input. +- **A heartbeat in the link layer.** It proves only that the worker process + runs, which the uplink already proves. Rejected for one sent by the input + session. diff --git a/docs/internal/adr/README.md b/docs/internal/adr/README.md index 59df76343..51f6a88ed 100644 --- a/docs/internal/adr/README.md +++ b/docs/internal/adr/README.md @@ -66,9 +66,9 @@ the status is stale. | [0025](0025-simlock-ships-android-profiles-and-a-model-pairs-only-with-images-that-fit.md) | Simlock ships Android device profiles, and a model pairs only with images that fit it | Accepted | | [0026](0026-agent-device-through-a-gateway-relays-over-the-uplink.md) | agent-device through a gateway: workers advertise capabilities, and the host link relays over the uplink | Accepted; §2 narrowed by 0028 for relayed bodies; §3's in-process link narrowed by 0043 | | [0027](0027-dependency-cruiser-checks-every-component-boundary.md) | dependency-cruiser checks every component's boundary and the module graph | Accepted | -| [0028](0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md) | The uplink carries byte channels beside its messages, and small messages go first | Accepted | +| [0028](0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md) | The uplink carries byte channels beside its messages, and small messages go first | Accepted; §4 narrowed by 0049 | | [0029](0029-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Accepted | -| [0030](0030-optional-modules-share-one-contract-and-load-only-when-turned-on.md) | Optional modules share one contract and load only when turned on | Accepted — not yet implemented; §2 narrowed by 0046 | +| [0030](0030-optional-modules-share-one-contract-and-load-only-when-turned-on.md) | Optional modules share one contract and load only when turned on | Accepted — not yet implemented; §2 narrowed by 0046; §3 narrowed by 0049 | | [0031](0031-a-physical-iphone-streams-through-coredevice-behind-a-screen-source-seam.md) | A physical iPhone streams through CoreDevice's media stream, behind a screen-source seam | Accepted | | [0032](0032-the-event-history-is-a-sqlite-database-with-projections-beside-it.md) | The event history is a SQLite database, with projections beside it | Accepted — not yet implemented; §1 narrowed by 0036 | | [0033](0033-a-sign-in-proxy-is-a-second-way-to-sign-in-and-auth-is-one-module.md) | A sign-in proxy is a second way to sign in, and auth is one module | Accepted — not yet implemented | @@ -83,7 +83,8 @@ the status is stale. | [0042](0042-simlock-updates-itself-through-a-frozen-call-and-a-detached-updater.md) | Simlock updates itself through a frozen call and a detached updater | Accepted — not yet implemented; narrows 0009's `takes-requests` stage | | [0043](0043-physical-devices-reach-agent-device-through-a-second-daemon.md) | Physical devices reach agent-device through a second daemon on the worker | Accepted — not yet implemented; supersedes 0023 §3–4 in part; §4 narrowed by 0046 | | [0044](0044-the-console-reads-what-its-page-shows-and-events-mark-it-stale.md) | The console reads what its page shows, and events mark it stale | Accepted — not yet implemented; supersedes 0013 §3 | -| [0045](0045-a-devices-screen-streams-to-operators-not-a-leases.md) | A device's screen streams to operators, not a lease's | Accepted — not yet implemented | +| [0045](0045-a-devices-screen-streams-to-operators-not-a-leases.md) | A device's screen streams to operators, not a lease's | Accepted; §2 narrowed by 0049 | | [0046](0046-a-standalone-worker-serves-agent-device-by-default-with-a-private-profile.md) | A standalone worker serves agent-device by default and hands local agents a private profile | Accepted — not yet implemented; narrows 0023 §1, §3–4, 0030 §2, 0039 and 0043 §4 | | [0047](0047-device-exec-fills-in-the-leases-device.md) | `device.exec` fills in the lease's device, and refuses another | Accepted — not yet implemented; extends 0005 §19a; narrows 0003 §11 for one MCP tool | | [0048](0048-an-ios-worker-may-keep-its-simulators-in-xcodes-device-set.md) | An iOS worker may keep its simulators in Xcode's device set | Accepted; narrows 0001 §1, §2, §3, §5, §6 and §7 for iOS under `deviceSet: "default"` | +| [0049](0049-a-lease-holder-watches-and-drives-its-devices-screen.md) | A lease holder watches and drives its device's screen | Accepted — not yet implemented |