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

connection-runtime.md

137 lines5.9 KB

1# Connection Runtime
2​
3The connection runtime is shared by web and mobile. It owns connectivity,
4authentication, retries, transport lifetime, cached environment data, and
5environment-scoped operations.
6​
7Web and mobile mount this runtime once at the application root. There is no
8legacy connection owner or supported mixed mode.
9​
10## Ownership
11​
12Each registered environment has one scoped Effect `Context` containing focused
13services:
14​
15- `EnvironmentSupervisor` owns desired state, retry scheduling, and the active
16 session scope.
17- `ConnectionBroker` prepares credentials and endpoints for primary, bearer,
18 relay, and SSH targets.
19- `RpcSessionFactory` performs one transport attempt. It does not retry.
20- `EnvironmentRpc` exposes the active session without leaking the transport.
21- `EnvironmentProjectCommands` and `EnvironmentThreadCommands` construct
22 orchestration commands, IDs, and timestamps.
23- `EnvironmentShell` and `EnvironmentThreads` own live subscriptions and cached
24 snapshots.
25​
26`EnvironmentServicesFactory` assembles that context, and `EnvironmentRegistry`
27owns its scope. There is no aggregate environment runtime facade. React
28components do not create connections, transports, retry loops, or RPC clients.
29​
30## Connection State
31​
32The supervisor is the only retry owner.
33​
341. A persisted or platform registration marks an environment as desired.
352. If the device is offline, the supervisor releases the active session and
36 waits without consuming retry attempts.
373. When online, the supervisor asks the broker for one prepared connection and
38 asks the session factory for one RPC session.
394. Transient failures retry forever with exponential backoff capped at 16
40 seconds.
415. Connectivity changes, application activation, credential changes, and
42 explicit user retry interrupt the current wait and trigger a fresh attempt.
436. Authentication or configuration failures remain blocked until an external
44 wakeup changes the relevant input.
457. An involuntary session close keeps the registration and cache, then retries.
468. Explicit removal closes the session and deletes the registration,
47 credentials, shell cache, and thread cache.
48​
49The UI derives `available`, `offline`, `connecting`, `reconnecting`,
50`connected`, and `error` from supervisor state plus explicit data-sync state.
51It does not infer connection health from cached data or the existence of a
52transport object. An environment becomes `connected` after the socket opens and
53the initial config RPC succeeds, proving that the server is responsive. Shell
54and thread synchronization are independent data states. A healthy RPC
55transport with a failed shell subscription is shown as connected with a
56synchronization error, not as a reconnect that is not actually scheduled.
57​
58## Data Boundary
59​
60Finite requests, durable subscriptions, and commands are separate APIs:
61​
62- Query atoms revalidate when the RPC generation changes.
63- Subscription atoms switch to replacement sessions.
64- Expected subscription failures update domain sync state and wait for a
65 replacement session; they do not take down a healthy transport.
66- Mutations resolve the current environment runtime at execution time.
67- Shell and thread snapshots are available while offline.
68- A connected transport may have `empty`, `cached`, `synchronizing`, `live`, or
69 failed shell and thread data independently.
70- Cached shell and thread projections are never allowed to overwrite newer live
71 data during a fast reconnect.
72- Domain atom factories route effects through the environment registry and
73 resolve the current scoped service at execution time.
74- Web and mobile own their Atom runtimes, React hooks, and feature composition.
75​
76The Promise bridge exists only at the React/Atom boundary. Runtime and business
77logic remain Effect-native.
78​
79## Platform Layers
80​
81Web and mobile provide:
82​
83- network status and network-change streams;
84- application lifecycle wakeups;
85- cloud session credentials;
86- device identity;
87- platform registrations;
88- persistent catalog, credential, shell, and thread stores;
89- HTTP, crypto, and telemetry layers.
90​
91Platform layers adapt operating-system capabilities. They do not implement
92connection policy.
93​
94## Source Boundaries
95​
96The public package subpaths mirror the runtime layers:
97​
98- `connection/core` contains state, catalog, retry policy, and connectivity.
99- `connection/transport` contains brokerage, authorization, attempts, and RPC
100 sessions.
101- `connection/platform` declares capabilities and persistence contracts.
102- `connection/services` contains environment-scoped data services.
103- `connection/application` assembles registries, discovery, and startup.
104- `connection/atoms` adapts shared services to application-owned Atom runtimes.
105- `connection/presentation` contains pure UI projections.
106​
107Other reusable state lives in domain subpaths such as `shell`, `threads`,
108`terminal`, and `vcs`. Applications must import explicit package subpaths; the
109package intentionally has no root export.
110​
111## Application Boundary
112​
113The application root mounts the shared connection application layer, creates
114its own Atom runtime, and selects the domain atom factories required by that
115platform. Web and mobile may expose different hooks and features without
116changing connection ownership.
117​
118Application code must not construct `WsTransport`, RPC clients, retry loops, or
119raw orchestration commands. Persistence paths belong to the platform
120registration and cache stores, with explicit migration or invalidation policy.
121​
122## Verification
123​
124Core state-machine tests use `@effect/vitest` and deterministic service layers.
125Required coverage includes:
126​
127- offline startup and online wakeup;
128- forever retry with the 16-second cap;
129- explicit retry interrupting backoff;
130- authentication wakeups;
131- involuntary close and reconnect;
132- explicit removal clearing all owned state;
133- relay token reuse and refresh;
134- progressive relay discovery;
135- shell and thread cache hydration;
136- durable subscriptions switching sessions;
137- command metadata and idempotent queued-command metadata.