Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

remote.md

179 lines10.6 KB

1# Remote architecture
2​
3Each connection joins a client to one environment over HTTP and WebSocket. The
4environment owns providers, execution, files, and durable state. Direct access,
5Tailscale, SSH, and T3 Connect change how the client reaches that server; they do
6not introduce another execution model. See
7[remote access](../user/remote-access.md) for setup.
8​
9## Identity is independent of the route
10​
11An environment keeps its ID across server restarts and endpoint changes. Saved
12connections are local to a client profile; the server's identity and state are
13not. A repository identity can correlate clones across environments, but never
14routes work between them. A project and its threads belong to one environment;
15an agent reaches another environment's threads only through a peer link.
16The canonical key follows the `upstream` remote when one exists, so pull request
17features target the repository a fork tracks. A fork also reports its own
18`origin`, and clients group and label by that, so a fork never collapses into a
19checkout of its upstream.
20​
21[Environment ID initialization](../../apps/server/src/environment/ServerEnvironment.ts)
22must publish a complete ID atomically. Repair of an empty ID file retains a
23recovery file so concurrent or delayed initializers choose the same winner.
24Removing that recovery state as ordinary temporary-file cleanup can change the
25identity underneath an already-running server.
26​
27Advertised endpoints are reachability hints. Only the connecting device can
28prove that a route works. In particular, a host's loopback address refers to a
29different machine when another device opens it. Endpoint selection must not
30silently fall back to loopback when a shareable endpoint is unavailable.
31​
32A saved environment holds an ordered list of routes, and the
33[driver](../../packages/client-runtime/src/connection/driver.ts) connects over
34the first that works. Each direct route is first checked with the public
35descriptor, so a saved LAN address that a different machine answers on another
36network receives no credential. That check is not proof of a working route:
37when every route stays silent, each is still tried. A route that fails to
38connect, including a blocked one such as a signed-out T3 Connect, moves on to
39the next; only an incompatible server stops the walk, because it is the same
40server on every route. While connected over a later route the
41[supervisor](../../packages/client-runtime/src/connection/supervisor.ts)
42preflights the earlier ones and replaces the session when one would connect.
43Preflight includes authorization so a route that answers but rejects this
44client never costs a working session; a route that still fails afterwards is
45held back for a cooldown so a flaky network cannot bounce the connection.
46​
47A connected server reports the LAN and tailnet addresses it is bound to, and the
48client saves them as learned routes. A learned route reuses the credential of
49the route it was learned over: the T3 Connect access token, which is not bound
50to an origin because each DPoP proof names the URL it signs, or the paired
51bearer token. A paired token only learns a plain HTTP address off the tailnet
52when it was itself paired over plain HTTP, so a token paired over HTTPS never
53starts travelling unencrypted. Learned routes the server stops reporting are
54dropped, which is how a changed LAN address replaces the old one; routes the
55user saved are never touched. The reported addresses are hints like any advertised endpoint, so a
56learned route still has to answer as this environment before it is used.
57​
58A background service with no configured host is what makes this work for
59headless machines. It listens on every IPv4 interface and closes each
60connection unless both addresses of the direct peer are loopback, private, or
61tailnet IPv4. That keeps a VPS's public address closed, including a cloud VM
62whose public address is NATed to a private interface. It is not a firewall:
63T3 Connect and Tailscale Serve reach the server over loopback by design, and a
64proxy that rewrites the source address looks private. One listener per address would
65miss a DHCP change, and the wildcard keeps the reported addresses current.
66​
67Pull request routing trust covers the whole route list. Adding or changing a route
68revokes it; reordering does not, because the same addresses remain trusted.
69​
70## Peer links
71​
72A [peer link](../../apps/server/src/peer/PeerLinks.ts) lets one environment's
73agents work in another. The linking environment signs in to the other's `/mcp`
74as an outside MCP client, with a pairing code from it, and keeps that session's
75token in its own secret store. The other environment needs nothing new: it
76sees one more OAuth client, lists it in Connections, and revokes it there.
77Unlinking on the linking side only forgets the token.
78​
79- **A tool runs in a linked environment when its parameters take
80 `OrchestratorMcpEnvironmentTarget`** as `environmentId`.
81 [`McpToolAccess.toLayer`](../../apps/server/src/mcp/McpToolAccess.ts) sends
82 such a call there once the tool's access declaration has checked the caller,
83 so a read-only or ended caller is refused before anything is sent. The
84 thread or project the call names is checked where it lives. A tool that acts
85 as the calling thread itself cannot take a linked environment.
86- **Every forwarded call carries the calling agent's modes** in
87 `T3-Mode-Limit`, which the receiving `/mcp` only ever narrows the client's
88 approved access with. A plan-mode agent therefore cannot start full-access
89 work through a full-access link.
90- **The token goes only to an address that proves it is the linked
91 environment**: its descriptor must report the linked environment id. A LAN
92 address that now belongs to another machine never receives it.
93- **All callers on one side share the link's single session on the other**,
94 so idempotency keys are hashed with the caller's namespace before they
95 leave. Without that, two agents reusing a key would collide on the far side.
96- **A forwarded wait is split into calls of at most 50 s**, because the
97 receiving `/mcp` sends nothing while a wait is open and T3 Connect's edge
98 drops idle requests after about 100 s.
99​
100**Work a link paired below full access starts stays within that link.** The
101receiving environment knows a session is a peer link because the linking side
102registers its OAuth client with the `t3code-peer-link` software id, and the
103marker is signed into the session. Threads that session launches carry it, with
104the access it was paired with, as an immutable `linkOrigin`, and so do the
105subagents, forks and `create_threads` they derive. The rules live in
106[`mcp/linkOrigin.ts`](../../apps/server/src/mcp/linkOrigin.ts) and run in the
107shared access declarations, not tool by tool:
108​
109- linked work may change only threads with the same origin, not the user's own
110 threads and not another link's;
111- it cannot change projects, settings or scheduled tasks, nor use the receiving
112 environment's own links;
113- its project setup scripts are skipped
114 ([`ProjectSetupScriptRunner`](../../apps/server/src/project/ProjectSetupScriptRunner.ts)
115 checks every path that runs them).
116​
117Reads are not fenced, and a client's own `thread.create` can never carry
118`linkOrigin`.
119​
120**A delegated task can run in a linked environment.** Its child is then an
121ordinary thread there, so the parent here records the task without a child
122thread of its own (`remoteChild` instead of `childThreadId`). A follower in
123[`RemoteDelegation`](../../apps/server/src/peer/RemoteDelegation.ts) waits on
124that thread and completes the task with `delegated_task.remote.complete`. That
125internal command reuses the parent half of a local finalize, so the parent
126wakes the same way. Open remote tasks are followed again at startup, which is
127also what keeps restart recovery from treating them as abandoned provider work.
128​
129The link is routing, not isolation: an agent the link starts runs as the
130receiving environment's user. **A full-access link is therefore not fenced**:
131its agents can already do anything that user can, so the fence would only stop
132T3's own tools. Its work still carries `linkOrigin`, so clients show where it
133came from. A stamp without the access, from before it was recorded, is fenced.
134​
135## Hosted web is a client
136​
137The hosted web app stores its connection catalog in the browser and connects
138directly to each environment. It does not proxy traffic or hold server-side
139pairing state. Hosting the UI over HTTPS therefore cannot make a plain HTTP LAN
140backend accessible from that browser context.
141​
142A [hosted pairing URL](../../apps/web/src/hostedPairing.ts) identifies the backend
143in its query and carries the pairing secret in its fragment. Fragments stay out
144of requests to the hosted origin. The browser exchanges the secret with the
145environment and strips it from its history. Moving the token into a query
146parameter would disclose it to the wrong origin.
147​
148## Access and process ownership are different
149​
150Tailscale supplies an endpoint for ordinary pairing, so it needs no separate
151environment type. Authentication remains the environment's responsibility for
152every route. See [environment authentication](./environment-auth.md) and the
153[T3 Connect trust boundary](./t3-connect.md).
154​
155SSH can launch a server as well as forward a port. Desktop main owns that
156lifecycle because it can spawn SSH and handle authentication prompts. The
157renderer uses the forwarded endpoint through the shared connection runtime.
158[SSH cleanup](../../packages/ssh/src/tunnel.ts) stops a remote server only if the
159launcher owns it; a server it discovered already running must survive a client
160disconnect. Reconnection restores the forward before opening the application
161transport.
162​
163Remote servers can outlive several client releases. Clients must use advertised
164capabilities and handle their absence, rather than assume their own version
165describes the server. Process replacement belongs to the launcher's
166[update protocol](./server-updates.md); the connection runtime handles the
167resulting disconnect.
168​
169### Desktop without a local environment
170​
171Desktop normally launches its own primary server, but the desktop setting `localEnvironmentEnabled`
172(`apps/desktop/src/settings/DesktopAppSettings.ts`) turns that off. Changing it relaunches the app;
173no local state is deleted. On the next start the main process skips port selection, server exposure,
174and the primary and WSL backends, and opens the window right away. The renderer sees this through
175`desktopBridge.getLocalEnvironmentEnabled()`: `readPrimaryEnvironmentTarget` returns null, so primary
176auth and platform-managed discovery are skipped and only saved environments (pairing, relay, SSH)
177connect. This is possible because the desktop renderer is not served by the backend: the `t3code://`
178scheme serves the bundled client from disk (Vite in development) and API traffic always goes to the
179environment's own URL.