Open History to see the last commit
1# Remote Architecture
2
3This document describes the target architecture for first-class remote environments in T3 Code.
4
5It is intentionally architecture-first. It does not define a complete implementation plan or user-facing rollout checklist. The goal is to establish the core model so remote support can be added without another broad rewrite.
6
7## Goals
8
9- Treat remote environments as first-class product primitives, not special cases.
10- Support multiple ways to reach the same environment.
11- Keep the T3 server as the execution boundary.
12- Let desktop, mobile, and web all share the same conceptual model.
13- Avoid introducing a local control plane unless product pressure proves it is necessary.
14
15## Non-goals
16
17- Replacing the existing WebSocket server boundary with a custom transport protocol.
18- Making SSH the only remote story.
19- Syncing provider auth across machines.
20- Shipping every access method in the first iteration.
21
22## High-level architecture
23
24T3 already has a clean runtime boundary: the client talks to a T3 server over HTTP/WebSocket, and the server owns orchestration, providers, terminals, git, and filesystem operations.
25
26Remote support should preserve that boundary.
27
28```text
29┌──────────────────────────────────────────────┐
30│ Client (desktop / mobile / web) │
31│ │
32│ - known environments │
33│ - connection manager │
34│ - environment-aware routing │
35└───────────────┬──────────────────────────────┘
36 │
37 │ resolves one access endpoint
38 │
39┌───────────────▼──────────────────────────────┐
40│ Access method │
41│ │
42│ - direct ws / wss │
43│ - tunneled ws / wss │
44│ - desktop-managed ssh bootstrap + forward │
45└───────────────┬──────────────────────────────┘
46 │
47 │ connects to one T3 server
48 │
49┌───────────────▼──────────────────────────────┐
50│ Execution environment = one T3 server │
51│ │
52│ - environment identity │
53│ - provider state │
54│ - projects / threads / terminals │
55│ - git / filesystem / process runtime │
56└──────────────────────────────────────────────┘
57```
58
59The important decision is that remoteness is expressed at the environment connection layer, not by splitting the T3 runtime itself.
60
61## Domain model
62
63### ExecutionEnvironment
64
65An `ExecutionEnvironment` is one running T3 server instance.
66
67It is the unit that owns:
68
69- provider availability and auth state
70- model availability
71- projects and threads
72- terminal processes
73- filesystem access
74- git operations
75- server settings
76
77It is identified by a stable `environmentId`.
78
79This is the shared cross-client primitive. Desktop, mobile, and web should all reason about the same concept here.
80
81### KnownEnvironment
82
83A `KnownEnvironment` is a client-side saved entry for an environment the client knows how to reach.
84
85It is not server-authored. It is local to a device or client profile.
86
87Examples:
88
89- a saved LAN URL
90- a saved public `wss://` endpoint
91- a desktop-managed SSH host entry
92- a saved tunneled environment
93
94A known environment may or may not know the target `environmentId` before first successful connect.
95
96In the hosted web app, known environments are browser-local. A hosted pairing URL can create the saved entry, but it does not give the hosted app a server-side control plane or a copy of the session state.
97
98### AccessEndpoint
99
100An `AccessEndpoint` is one concrete way to reach a known environment.
101
102This is the key abstraction that keeps SSH from taking over the model.
103
104A single environment may have many endpoints:
105
106- `wss://t3.example.com`
107- `ws://10.0.0.25:3773`
108- a tunneled relay URL
109- a desktop-managed SSH tunnel that resolves to a local forwarded WebSocket URL
110
111The environment stays the same. Only the access path changes.
112
113### AdvertisedEndpoint
114
115An `AdvertisedEndpoint` is a server or desktop-authored candidate endpoint for an environment. It is how the backend tells the client which URLs may be useful for pairing and reconnecting.
116
117`AdvertisedEndpoint` is deliberately narrower than the full access model:
118
119- it describes a concrete HTTP and WebSocket base URL pair
120- it can mark the endpoint as default, available, or unavailable
121- it includes reachability hints such as loopback, LAN, private, public, or tunnel
122- it includes compatibility hints such as whether the endpoint can be used from the hosted HTTPS app
123
124Clients should treat advertised endpoints as hints, not as proof that a route works from the current device. The final connection attempt still decides whether the endpoint is reachable.
125
126The UI presents one default advertised endpoint in the network-access summary and keeps the rest behind an expandable advanced list. The default controls pairing QR codes and primary copy actions. Users can override it, but that override is a UI preference, not backend configuration.
127
128Persist the override by stable endpoint kind rather than raw URL whenever possible. For example, a LAN endpoint should be stored as the desktop LAN endpoint preference, not as `192.168.x.y`, because the address can change when the user switches networks. Provider endpoints should use provider-specific stable keys such as Tailscale IP or Tailscale MagicDNS HTTPS. Custom endpoints may fall back to their concrete identity.
129
130When no user default is saved, endpoint selection should prefer:
131
1321. endpoints compatible with the hosted HTTPS app
1332. explicitly default endpoints
1343. non-loopback endpoints
1354. loopback endpoints only for same-machine clients
136
137This keeps endpoint discovery centralized without making any one provider, such as Tailscale or a future tunnel service, part of the core environment model.
138
139### Endpoint providers
140
141Endpoint providers are add-ons that contribute advertised endpoints for the current environment.
142
143The provider boundary is intentionally outside the core environment model:
144
145- core owns `ExecutionEnvironment`, saved environments, pairing, and connection lifecycle
146- providers discover or synthesize endpoints
147- providers return normalized `AdvertisedEndpoint` records
148- the UI and pairing logic select from those records without knowing provider-specific commands
149
150The first provider is Tailscale. It can discover Tailnet IP and MagicDNS addresses from the local machine and publish them as additional endpoint candidates. Future providers, such as a hosted tunnel service, should plug into the same shape rather than adding a separate remote environment path.
151
152Provider-specific confidence should remain a hint. A Tailscale endpoint still needs a successful browser or desktop connection before the client treats it as connected.
153
154### Hosted pairing request
155
156A hosted pairing request is a bootstrap URL for the static web app, not a transport.
157
158Example:
159
160```text
161https://app.t3.codes/pair?host=https://backend.example.com:3773#token=PAIRCODE
162```
163
164The hosted app reads the `host` parameter and pairing token, exchanges the token directly with that backend, then saves the resulting environment record in browser local storage.
165
166Important constraints:
167
168- the hosted app does not proxy HTTP or WebSocket traffic
169- the backend must still be reachable directly from the browser
170- HTTPS pages can only connect to HTTPS/WSS backends
171- HTTP LAN endpoints should keep using direct desktop or CLI pairing URLs
172- the token belongs in the URL hash so it is not sent to the hosted app origin
173
174### RepositoryIdentity
175
176`RepositoryIdentity` remains a best-effort logical repo grouping mechanism across environments.
177
178It is not used for routing. It is only used for UI grouping and correlation between local and remote clones of the same repository.
179
180### Workspace / Project
181
182The current `Project` model remains environment-local.
183
184That means:
185
186- a local clone and a remote clone are different projects
187- they may share a `RepositoryIdentity`
188- threads still bind to one project in one environment
189
190## Access methods
191
192Access methods answer one question:
193
194How does the client speak WebSocket to a T3 server?
195
196They do not answer:
197
198- how the server got started
199- who manages the server process
200- whether the environment is local or remote
201
202### 1. Direct WebSocket access
203
204Examples:
205
206- `ws://10.0.0.15:3773`
207- `wss://t3.example.com`
208
209This is the base model and should be the first-class default.
210
211Benefits:
212
213- works for desktop, mobile, and web
214- no client-specific process management required
215- best fit for hosted or self-managed remote T3 deployments
216
217Browser security rules are part of this access method. A hosted HTTPS web client can connect to `wss://` backends, but it cannot connect to plain `ws://` or `http://` LAN backends because that would be mixed content.
218
219### 2. Tunneled WebSocket access
220
221Examples:
222
223- public relay URLs
224- private network relay URLs
225- local tunnel products such as pipenet
226
227This is still direct WebSocket access from the client's perspective. The difference is that the route is mediated by a tunnel or relay.
228
229For T3, tunnels are best modeled as another `AccessEndpoint`, not as a different kind of environment.
230
231This is especially useful when:
232
233- the host is behind NAT
234- inbound ports are unavailable
235- mobile must reach a desktop-hosted environment
236- a machine should be reachable without exposing raw LAN or public ports
237
238Tailscale-backed access sits here architecturally even though the current implementation is endpoint discovery rather than a T3-managed tunnel. It contributes private-network endpoints and lets the existing HTTP/WebSocket client path do the actual connection.
239
240### 3. Desktop-managed SSH access
241
242SSH is an access and launch helper, not a separate environment type.
243
244The desktop main process can use SSH to:
245
246- reach a machine
247- probe it
248- launch or reuse a remote T3 server
249- establish a local port forward
250
251After that, the renderer should still connect using an ordinary WebSocket URL against the forwarded local port.
252
253This keeps the renderer transport model consistent with every other access method.
254
255The desktop main process owns the SSH bridge because it can spawn local SSH processes, manage askpass prompts, write temporary launch scripts, and clean up forwards. The renderer receives a saved environment record and connects through the forwarded URL; it should not need SSH-specific RPC paths for normal environment traffic.
256
257## Launch methods
258
259Launch methods answer a different question:
260
261How does a T3 server come to exist on the target machine?
262
263Launch and access should stay separate in the design.
264
265### 1. Pre-existing server
266
267The simplest launch method is no launch at all.
268
269The user or operator already runs T3 on the target machine, and the client connects through a direct or tunneled WebSocket endpoint.
270
271This should be the first remote mode shipped because it validates the environment model with minimal extra machinery.
272
273### 2. Desktop-managed remote launch over SSH
274
275This is the main place where Zed is a useful reference.
276
277Useful ideas to borrow from Zed:
278
279- remote probing
280- platform detection
281- session directories with pid/log metadata
282- reconnect-friendly launcher behavior
283- desktop-owned connection UX
284
285What should be different in T3:
286
287- no custom stdio/socket proxy protocol between renderer and remote runtime
288- no attempt to make the remote runtime look like an editor transport
289- keep the final client-to-server connection as WebSocket
290
291The recommended T3 flow is:
292
2931. Desktop connects over SSH.
2942. Desktop probes the remote machine and verifies T3 availability.
2953. Desktop launches or reuses a remote T3 server.
2964. Desktop establishes local port forwarding.
2975. Renderer connects to the forwarded WebSocket endpoint as a normal environment.
298
299The saved environment should remember that it was created by desktop SSH launch only for reconnect and lifecycle UX. That metadata should not change the server protocol or the environment identity model.
300
301Failure handling should be explicit:
302
303- SSH authentication failure should surface before any environment is saved
304- remote launch failure should include remote logs or the launcher command output when available
305- forwarded-port failure should leave the saved environment disconnected rather than falling back to an unrelated endpoint
306- reconnect should attempt to restore the SSH bridge before reconnecting the normal WebSocket client
307
308### 3. Client-managed local publish
309
310This is the inverse of remote launch: a local T3 server is already running, and the client publishes it through a tunnel.
311
312This is useful for:
313
314- exposing a desktop-hosted environment to mobile
315- temporary remote access without changing router or firewall settings
316
317This is still a launch concern, not a new environment kind.
318
319## Why access and launch must stay separate
320
321These concerns are easy to conflate, but separating them prevents architectural drift.
322
323Examples:
324
325- A manually hosted T3 server might be reached through direct `wss`.
326- The same server might also be reachable through a tunnel.
327- An SSH-managed server might be launched over SSH but then reached through forwarded WebSocket.
328- A local desktop server might be published through a tunnel for mobile.
329
330In all of those cases, the `ExecutionEnvironment` is the same kind of thing.
331
332Only the launch and access paths differ.
333
334## Security model
335
336Remote support must assume that some environments will be reachable over untrusted networks.
337
338That means:
339
340- remote-capable environments should require explicit authentication
341- tunnel exposure should not rely on obscurity
342- client-saved endpoints should carry enough auth metadata to reconnect safely
343
344T3 already supports a WebSocket auth token on the server. That should become a first-class part of environment access rather than remaining an incidental query parameter convention.
345
346For publicly reachable environments, authenticated access should be treated as required.
347
348Hosted pairing should be treated as a client-side convenience only. The hosted app must not receive pairing tokens through query parameters, must not store pairing state server-side, and must not imply that an HTTP backend is safe or reachable from an HTTPS browser context.
349
350## Relationship to Zed
351
352Zed is a useful reference implementation for managed remote launch and reconnect behavior.
353
354The relevant lessons are:
355
356- remote bootstrap should be explicit
357- reconnect should be first-class
358- connection UX belongs in the client shell
359- runtime ownership should stay clearly on the remote host
360
361The important mismatch is transport shape.
362
363Zed needs a custom proxy/server protocol because its remote boundary sits below the editor and project runtime.
364
365T3 should not copy that part.
366
367T3 already has the right runtime boundary:
368
369- one T3 server per environment
370- ordinary HTTP/WebSocket between client and environment
371
372So T3 should borrow Zed's launch discipline, not its transport protocol.
373
374## Recommended rollout
375
3761. First-class known environments and access endpoints.
3772. Direct `ws` / `wss` remote environments.
3783. Authenticated tunnel-backed environments.
3794. Desktop-managed SSH launch and forwarding.
3805. Multi-environment UI improvements after the base runtime path is proven.
381
382This ordering keeps the architecture network-first and transport-agnostic while still leaving room for richer managed remote flows.