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

connection-runtime.md

125 lines7.5 KB

1# Connection runtime
2​
3Web, the desktop renderer, and mobile share one connection owner per environment
4in `packages/client-runtime`. Platform code supplies storage, credentials, network
5signals, and application lifecycle events. React views consume the runtime.
6Keeping retries and session lifetime here prevents competing reconnect loops when
7several views need the same environment.
8​
9## One transport retry owner
10​
11The [supervisor](../../packages/client-runtime/src/connection/supervisor.ts) owns
12transport retry policy; resolving an endpoint and opening an RPC session are single
13attempts. Transient failures retry with jittered exponential backoff, capped at
14five minutes, that resets only after a connection stays up. Without jitter, every
15client of a restarted server reconnects in the same second; with a short cap, a
16client that can never connect retries all day. Offline states and authentication
17failures wait for a wakeup instead of spending attempts on unchanged conditions.
18​
19Foregrounding, a network change, an explicit retry, and an offline report probe
20the established session, and only a failed probe reconnects. Offline reports are
21often wrong, for example for a loopback server. The OS can kill a suspended
22socket without a close event, so a probe still unanswered after 500 ms asks the
23host for its public descriptor over a fresh request, again each second until it
24answers. If the host answers while the socket delivers no frame, the socket is
25dead and is replaced, even while the network reports offline: at once on
26loopback and LAN, and 2.5 seconds after the check starts elsewhere, where one
27lost packet can stall a live socket. Without that evidence the probe deadline
28decides: 3 seconds on loopback and LAN routes and after a long mobile
29suspension, when the data on screen is stale; 15 seconds elsewhere, because a
30tailnet over DERP or a VPN can be slower than that on a live socket. A long
31suspension's probe that times out also replaces the socket regardless of the
32network report. The web and desktop foreground probe skips the fresh request,
33because a busy host can answer it before a queued probe. Foregrounding also
34wakes a pending retry immediately. A long suspension restarts an in-flight
35attempt; a short one leaves it alone.
36​
37The [registry](../../packages/client-runtime/src/connection/registry.ts) scopes
38connections by environment. An involuntary disconnect retains the registration
39and cached data. Explicit removal closes the scope and clears credentials,
40projections, and platform-owned state such as drafts. Cloud-account changes apply
41to relay registrations; they must not discard directly paired environments.
42​
43## HTTP authorization
44​
45RPC sessions authenticate at socket upgrade, while HTTP requests need current
46credentials from the
47[authorization service](../../packages/client-runtime/src/authorization/service.ts).
48Replacing a healthy socket for HTTP renewal would interrupt conversations and
49change the transport generation without a transport failure. Credential expiry
50does not close the socket, and refresh failure belongs to the HTTP operation.
51​
52Session listings must retain unrevoked connected sessions after credential expiry
53so an open connection does not disappear from connection management. This does
54not extend the credential's lifetime. New HTTP requests and socket upgrades still
55require valid credentials.
56​
57## Transport health and data freshness are separate
58​
59A socket opening is insufficient evidence that the environment is usable. The
60[RPC session](../../packages/client-runtime/src/rpc/session.ts) waits for the
61initial server configuration before becoming ready. Shell and thread data then
62have their own synchronization state. A failed shell subscription can coexist
63with a healthy connection; labeling that state "reconnecting" promises a
64transport retry that will never happen.
65​
66Cached projections remain readable offline. They must neither imply a live
67connection nor overwrite newer live data during a reconnect. Loading and
68resuming snapshots belongs to the shared state services, so every view agrees
69on which data is current.
70​
71Shell snapshots can leave rows out when the client asks. Servers that advertise
72`shellThreadFamilies` leave out idle subagent children: a child stays while it
73has a live or queued run, a pending request or background work, or a subagent
74child of its own, so badge and working walks never meet a gap. Servers that also
75advertise `shellOlderSettled` keep only the 25 newest settled top-level threads
76(and any tied with the 25th, pinned, snoozed, live, or above a kept child); the
77snapshot's `olderSettled` names that cut and counts the rest per project. Left-out
78rows load on demand and merge behind shell rows
79([thread families](../../packages/client-runtime/src/state/threadFamilies.ts)):
80the family a thread view or a queued message mounts holds every active thread
81sharing the thread's root, so it also brings back an old settled thread opened by
82link; the Settled shelf reads pages below the cut; searches return the rows of
83older matches. A missing shell row therefore does not prove a thread is gone:
84code that deletes data or a worktree on absence must check the family or the
85server first.
86​
87The Settled count is local Settled rows, minus local rows below the cut, plus the
88server's counts
89([older settled threads](../../packages/client-runtime/src/state/olderSettledThreads.ts)).
90The counts are re-read when a removal or an old row's arrival or change could
91move them, so archiving any thread costs one small read. A shelf lists an older
92row only above the highest page frontier of the environments that can still
93load, so the merged list stays a correct prefix while a page loads. An
94environment with no snapshot this session, or a failed page, holds no one back.
95On-demand reads wait for the session's first authoritative snapshot
96(`generation > 0`), and each is keyed by generation. A resume that skips that
97snapshot must still bump `generation` once verified, and may reuse a cached
98snapshot only when its omissions match what the session asks for.
99​
100[Thread detail](../../packages/client-runtime/src/state/threads.ts) separates
101subscription lifetime from cache lifetime. Mounted consumers share one live
102stream, which stops when the last consumer unmounts; hidden mounted routes still
103count. A registry-local cache retains state and its replay cursor for five idle
104minutes so back navigation can resume without another snapshot download.
105​
106The desktop app adds one consumer: a
107[keep-alive](../../apps/web/src/state/threads.ts) mounts every thread whose
108session is starting or running, in each enabled environment. Opening a running
109thread then needs no replay. The shell and detail streams are independent, so
110the shell can report a stop before the detail loads or catches up. A stopped
111thread stays mounted until its own stream is live and shows the stop, and the
112stream then closes and saves the settled state.
113Web and mobile do not keep threads alive.
114​
115Retain state and cursor together only after an update finishes. Cancellation must
116not advance the cached cursor beyond the applied data, and an old scope must not
117overwrite its successor's cache. Preserve pagination data on reuse, but clear
118canceled loading state.
119​
120The [RPC boundary](../../packages/client-runtime/src/rpc/client.ts) resolves
121requests against the current session at execution time. Durable subscriptions
122follow replacement sessions. After a transport failure they wait for the
123supervisor; an expected domain failure may resubscribe on the same healthy
124session. Reconnection does not automatically replay mutations, whose retry and
125idempotency rules belong to the operation.