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

connection-runtime.md

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