Skip to content

feat(remote): relay terminal.multiplex for paired desktop and web clients - #35631

Draft
jutaz wants to merge 6 commits into
stablyai:mainfrom
jutaz:feat/runtime-client-relay-terminal
Draft

jutaz wants to merge 6 commits into
stablyai:mainfrom
jutaz:feat/runtime-client-relay-terminal

Conversation

@jutaz

@jutaz jutaz commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

ELI5

#35195 lets your desktop pass a paired desktop or web client's requests through to a server it manages, the way it already does for phones. Terminals were left out: desktop and web terminals talk over a stream of binary frames, and the desktop refused to pass those along. This PR teaches the desktop to pass one terminal stream per connection through to the server, unchanged in both directions. It only does so when the client asks for it and the desktop says it supports it. Nothing uses it yet.

What Changed

Topology as in #35195: client (desktop/web, paired with runtime scope) → server A → managed server B, which A reaches as runtime:<env>. All changes are on A.

  • New capability runtime.client-relay.terminal-multiplex.v1 (src/shared/mobile-desktop-relay-contract.ts, next to runtime.client-relay.v1). A stamps it on status.get through the same per-connection stamp, and only where it stamps runtime.client-relay.v1: an encrypted runtime-scope socket on a server with relay host wiring.

  • Negotiated on both sides. A relays terminal.multiplex only when the client also declared the capability in its E2EE auth clientCapabilities. A client that knows only runtime.client-relay.v1 keeps today's forbidden for a targeted terminal.multiplex, which is what feat(remote): relay paired desktop and web clients to a server's managed hosts #35195 promised it. Phones never relay it, whatever they declare.

  • New carrier kind client-stream-ids (relayed-stream-carriers.ts) replaces phone-binary-frames for terminal.multiplex. Multiplex stream ids are allocated by the client, so A leaves them alone in both directions. RelayedPhoneReplies does not renumber a client-stream-ids request's result.streamId. It passes B→client binary frames through byte for byte. A per-stream end does not close the request: only the multiplex's final reply, the client's socket closing, or the link ending does.

  • Binary for streams the client never opened passes through. B owns multiplex stream state, and on that socket only B sends binary. The client's multiplexer already handles an unknown stream: it answers stray output with Unsubscribe, which releases B's credit for it. Dropping on A would mean A tracking stream lifetimes from the subscribe and end traffic, which can diverge from B's, and B would never hear the client's Unsubscribe for a stream A swallowed.

  • Client→B binary: handleWebSocketBinaryMessage first asks the relay whether a live relayed multiplex owns this connection (MobileDesktopRelay.forwardClientBinary). If one does, the frame goes out unchanged on that link's passthrough socket via a new RemoteRuntimePassthroughSocket.sendBinary. If not, it falls through to the local binaryMessageRouter.dispatch exactly as before. sendBinary mirrors the subscription transport (frame size limit, sealBinary, outbound.enqueueBinary), so client input goes through that socket's bounded binary backpressure queue, beside the separate request queue for text. The passthrough has no process memory budget, so the binary queue runs on the per-socket defaults (64 MB / 4096 frames). The multiplex orders itself through ready; text and binary are not globally ordered. Frames that arrive while the link is still opening are swallowed: B registers no stream before its multiplex handler runs, clients send nothing before ready, and the frames must not reach A's local router either.

  • One lane per connection. Binary frames carry no target, so one relayed multiplex owns its connection's whole binary lane. A refuses (forbidden) a second terminal.multiplex, local or relayed, on a connection that already relays one. The local refusal is the existing binary_handler_mode_conflict, now also raised for a connection with a relayed lane. It refuses a relayed one on a connection that already has local binary handlers or relayed binary streams (terminal.subscribe, or browser.screencast, whose frames carry no stream id), and refuses either of those beside a relayed multiplex. browser.screencast gets its own carrier kind, untagged-binary, for this; the other none carriers send no binary. The renderer opens multiplex on its own socket, so none of these happen in practice; they keep the frames of two streams from being mixed up.

  • Teardown. Each of these releases the lane, so later binary goes to the local router (and is dropped there as today if nothing local claims it):

    • B's final reply to the multiplex request;
    • the client's socket closing (closePhoneConnection). The renderer opens multiplex on a dedicated socket and ends it by closing that socket;
    • the link ending: revocation on B, server retired on A, B's socket dropping, or A disposing the relay.

    Transport loss answers the open request with remote_runtime_unavailable, never an exit. A also ends the link with remote_runtime_unavailable when B's socket cannot queue client input, or the client's socket discards a B→client frame. A lost multiplex frame desyncs the stream, and only a reconnect resyncs it (the same rule terminal-multiplex-frame-delivery.ts applies on B).

  • RelayLink moved to relay-link.ts and the test E2EE client to relay-test-client.test-fixture.ts, to stay under the file-size caps. Neither changed behaviour.

Not covered, and why no client uses this yet. A usable relayed terminal needs more than the multiplex. The client must also route terminal.create, subscriptions and writes through executionHost. That client work waits on the maintainer questions on #35195 (child kind on B, the route table, the virtual environment id). No renderer or main-IPC code changes here, and nothing sends executionHost or declares the new capability yet.

Why

#35195 question 5 asked whether terminal binary forwarding should come behind its own capability. This is that follow-up. The alternatives:

  • Renumbering multiplex ids like terminal.subscribe: the client allocates multiplex ids and B echoes them in frames and in result.streamId. Renumbering would have to rewrite every client→B frame as well, including the subscribe payload's JSON streamId. Passing them through is correct as long as one connection carries one multiplex. The renderer already guarantees that, and A now enforces it.
  • registerRawMessage for the hook: exclusive with terminal handlers per connection (binary_handler_mode_conflict), and it would have to be registered from inside dispatch. A pre-dispatch check keeps the local router untouched for every connection without a relayed lane.
  • Gating on the server capability alone: a client that knows only runtime.client-relay.v1 was promised forbidden for terminal.multiplex. It would suddenly get a relayed multiplex whose binary it may not expect.

Mixed versions:

Client Server A Outcome
declares the capability new + relay wiring sees runtime.client-relay.terminal-multiplex.v1; a targeted terminal.multiplex relays to B with ids unchanged
knows only runtime.client-relay.v1 new + relay wiring targeted terminal.multiplex → forbidden, as in #35195
new #35195 only, or no relay wiring capability not advertised → the client must not send a targeted terminal.multiplex
phone new forbidden, unchanged
any new untargeted, local and ssh: multiplexes run on A as before

B needs no change. It already accepts terminal.multiplex from the delegated mobile child (it is in EXECUTION_HOST_METHODS). The subscribe frame's client.type: 'desktop' comes from the payload, not from the child's scope.

Linked Issue

Part of #29396.

Stacked on #35195, so this diff includes its commits until it merges. Review only the last commit, feat(remote): relay terminal.multiplex for paired desktop and web clients. It doesn't depend on #35242 or #35478.

Visual Proof

N/A: server-side relay only. No renderer or main-IPC code changes, and no client sends a relayed terminal.multiplex yet.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

mobile-desktop-relay.integration.test.ts (real A and B RPC servers over E2EE WebSockets):

  • A runtime client that declares the capability sees it on status.get and opens a targeted terminal.multiplex via A. A Subscribe on stream 4242 (an id neither computer's allocator would produce) reaches B as 4242, since B answers on that id. B's snapshot and Output reach the client on 4242. Client Input on 4242 is written to B's terminal. A second multiplex on the same connection is refused, relayed (forbidden) or local (binary_handler_mode_conflict), and a phone is refused even when it declares the capability.
  • A runtime client without the capability is refused (forbidden), and no server socket opens.
  • When B revokes the child mid-multiplex, the client gets remote_runtime_unavailable. After the next sync a fresh relayed multiplex carries input again.
  • A local multiplex on a client connection keeps its binary on A, and A then refuses a relayed one on that connection.

Unit (mobile-desktop-relay.test.ts, relayed-phone-frames.test.ts): the hook claims frames only while the multiplex is open. Frames that race the open are swallowed, not sent late, and other connections fall through. The final reply releases the lane. A relayed screencast and a relayed multiplex refuse each other on one connection. B's socket dropping answers remote_runtime_unavailable and releases it. A full input queue or a discarded output frame ends the link. The carrier keeps ids in both directions, passes frames for unannounced streams, and survives a per-stream end.

Mutation checks: each guard was removed in turn and its test failed. The guards: the client-capability gate, the server advertisement, the one-per-connection guard, the local-streams guard, the reverse binary_handler_mode_conflict, the screencast refusal both ways, swallowing pre-open frames, result.streamId pass-through, binary pass-through, per-stream end, the pre-dispatch hook, release on close, and ending the link on input or output send failure.

Commands (Linux): pnpm tc; pnpm test --maxWorkers=2 on src/main/runtime/mobile-desktop-relay, src/main/runtime/runtime-rpc, src/shared/remote-runtime, src/main/runtime/runtime-binary-message-router.test.ts and src/main/runtime/rpc/terminal-multiplex* (89 files, 538 passed, 1 skipped); node config/scripts/check-changed-code-quality.mjs upstream/main; oxlint and oxfmt --check on the changed files.

AI Disclosure

Written with Claude (Anthropic) via an agent harness; reviewed by the author.

Review

  • Security: no new privilege. B already allows terminal.multiplex to the mobile child it minted for this client. A still requires the request to pass feat(remote): relay paired desktop and web clients to a server's managed hosts #35195's admission, including denyRpcMethodForCaller with the client's own grants (workspace). Client frames are forwarded opaque. They cannot change which server or child they reach, and only a connection with an open relayed multiplex forwards anything. B's token never appears in binary frames: the multiplex carries client-chosen client.ids, not device tokens.
  • Remote/SSH: transport loss is remote_runtime_unavailable and never surfaces as "exited". local and ssh: targets are untouched.
  • Mobile: phones keep the refusal, and the phone path's renumbering and unknown-stream drop are unchanged.
  • Backpressure: client input goes through the passthrough socket's bounded binary queue (the 64 MB / 4096-frame defaults; the passthrough has no process memory budget). On overflow A ends the link instead of dropping keystrokes. A discarded B→client frame also ends the link instead of desyncing the stream.
  • Cross-platform / performance: no platform-specific code. Each client binary frame costs one scan of A's open relay links, one per (connection, server) pair. That is small next to the frame's decryption, but a map keyed by connection would make it constant time if it ever shows up.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Open questions:

  1. Client declaration: the gate requires the client to declare runtime.client-relay.terminal-multiplex.v1 in its auth clientCapabilities. runtime.clientCapabilities.update is phone-only on A, so a runtime client declares it once per socket. Is that the right handshake, or should the server advertisement alone be enough?
  2. Stray streams pass through (reasoning above). The alternative is to drop frames for ids the client never subscribed on A, which means A parsing Subscribe payloads.
  3. Seat identity on B: B sees the mobile child and client.type: 'desktop' from the payload. Floors and drivers on B are keyed by the client's client.id and the child's connection, which looks right. It depends on feat(remote): relay paired desktop and web clients to a server's managed hosts #35195 question 1 (child scope) all the same.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@jutaz

jutaz commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Local CI for e23c9ff (stacked on #35195): typecheck, changed-code quality, lint and format pass. The mobile-desktop-relay, binary-message-router and terminal-multiplex suites pass: 30 files, 195 tests, 1 skipped. Before the latest rebase, the broader run including runtime-rpc and shared/remote-runtime was 90 files / 542 tests, all passing.

jutaz added 6 commits November 7, 2026 03:21
…ged hosts

A server with relay host wiring advertises runtime.client-relay.v1 to
runtime-scope sockets and relays their execution-host calls that name a
runtime:<env> target, reusing the phone relay and its delegated devices.
Every other runtime-client request keeps ignoring executionHost.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant