<!-- docs/internals/09_connection-runtime.md at be521199aace1a0b446f4db4a1181ef994aaa1e2 · https://github.com/pingdotgg/t3code/blob/be521199aace1a0b446f4db4a1181ef994aaa1e2/docs/internals/09_connection-runtime.md · status: fresh -->

# Connection runtime

Web, desktop, and mobile share one connection owner per environment in `packages/client-runtime`.
Platform code supplies storage, credentials, network signals, and lifecycle events; React views
consume the runtime. Keeping retry and session ownership here prevents competing reconnect loops.

## One transport retry owner

- The [supervisor](../../packages/client-runtime/src/connection/supervisor.ts) is the sole owner of
  transport retries. Endpoint resolution and opening an RPC session are single attempts. Transient
  failures use jittered, capped exponential backoff that resets only after a connection stays up;
  offline and auth failures wait for an explicit wakeup.
- Foregrounding, network changes, explicit retries, offline reports, and a request the socket
  leaves unanswered probe the established session rather than blindly reconnecting (OS offline
  reports are often wrong, especially for loopback). Only a failed probe replaces the socket, with
  one exception: a socket that died while a phone was suspended gives no close event, so a mobile
  resume connects a replacement beside a probe that is slow to answer, and whichever answers first
  wins. On mobile, a passed probe also keeps
  shell and thread subscriptions as they are: a socket that answers it has delivered every earlier
  update.
- Android stops JS timers while the app is in the background (iOS runs them only during its short
  background grace). Pings, debounces, and periodic checks go quiet there, so anything that must
  reach a host as the app backgrounds is sent from the AppState event, not behind a timer.
- The [registry](../../packages/client-runtime/src/connection/registry.ts) scopes connections by
  environment. An involuntary disconnect keeps the registration and cached projections; explicit
  removal closes the scope and clears credentials, caches, and drafts. Cloud-account changes apply
  only to relay registrations and must never discard directly paired environments.

## HTTP authorization vs. socket lifetime

RPC sockets authenticate at upgrade, while HTTP requests pull current credentials from the
[authorization service](../../packages/client-runtime/src/authorization/service.ts). Never tear down
a healthy socket because an HTTP refresh failed—renewal failure belongs to the HTTP operation. Live
sockets whose access token nears expiry renew in place on the same session so open conversations are
not interrupted. Session listings retain unrevoked connected sessions after credential expiry so
active connections stay manageable without extending the credential's lifetime.

## Transport health and data freshness are separate

- An open socket does not mean the environment is usable. The RPC session waits for initial server
  configuration, and shell and thread subscriptions maintain their own sync state. Never label a
  failed subscription "reconnecting" when the transport is healthy.
- Cached projections stay readable offline, but must never imply a live connection or overwrite
  newer live data on reconnect.
- **Shell snapshots are intentionally partial.** Servers omit idle subagent children and older
  settled threads on demand; left-out rows load via thread-family, shelf, and search queries. A
  missing shell row therefore never proves a thread is deleted—code that deletes data or worktrees
  on absence must verify with the family or server first.

## Subscriptions, caching, and mutations

- [Thread detail](../../packages/client-runtime/src/state/threads.ts) separates subscription
  lifetime from cache lifetime: mounted consumers share one live stream (stopping when the last
  unmounts), while a short-lived cache retains state and its replay cursor for fast back navigation.
- Commit cached state and replay cursor together only after an update completes. Cancellation must
  not advance the cursor past applied data, and an old scope must not overwrite a newer scope's
  cache.
- Durable subscriptions follow replacement sessions automatically. Reconnection never auto-replays
  mutations—retry and idempotency belong to each operation.
