Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix: Tolerate lossy UI websocket heartbeats #3097

ClosedStiensWout(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/3097/headopened View on GitHub

Summary

  • Replace the UI WebSocket RPC client heartbeat behavior with a tolerant client-runtime protocol that waits through several missed pongs before timing out.
  • Keep the existing RPC wire protocol, request routing, stream handling, and retry behavior while adding an internal WsRpcHeartbeatConfig for heartbeat tuning.
  • Make heartbeat-triggered reconnects fail active streams so shell/thread subscriptions resubscribe and fetch fresh server snapshots.
  • Add regression coverage for missed pongs, timeout reconnect/resubscribe, delayed pong reset, and thread-detail snapshot replacement after resubscribe.

Root cause

Effect's current RpcClient.makeProtocolSocket pings every 5 seconds and fails the socket after a single missed pong. On mildly lossy or packet-reordering links, TCP can still recover, but the RPC WebSocket layer declares a ping timeout and churns the UI connection.

That churn can also interrupt active stream subscriptions. If a thread stream misses the tail of a turn, the client can keep showing stale detail until it receives a fresh snapshot.

Impact

Users on VPN, mobile, or otherwise imperfect links should see fewer false UI WebSocket disconnect/reconnect loops. With the new defaults, the client pings every 15 seconds and only times out after 3 missed pongs.

If the connection really stops responding, the client still reconnects, active streams resubscribe, and thread detail state is marked pending until the fresh server snapshot arrives. This keeps recovery explicit instead of leaving stale streamed state in place.

Notes

  • #3054 still has needs-triage; this PR is deliberately scoped to the client-runtime transport/recovery path.

Validation

  • PATH="$HOME/.vite-plus/bin:$PATH" vp run --filter @t3tools/client-runtime test -- src/wsTransport.test.ts src/threadDetailState.test.ts
  • PATH="$HOME/.vite-plus/bin:$PATH" vp check
  • PATH="$HOME/.vite-plus/bin:$PATH" vp run typecheck

vp check reports the repository's existing 11 unrelated React nested-component warnings and no errors.

Closes #3054

Note

Add heartbeat Ping/Pong tolerance to the UI WebSocket RPC protocol

  • Introduces makeWsRpcTolerantSocketProtocol which periodically sends WebSocket Ping frames (default: every 15s) and triggers reconnection after a configurable number of missed Pongs (default: 3).
  • Replaces RpcClient.makeProtocolSocket with the new tolerant protocol in createWsRpcProtocolLayer, wiring in onHeartbeatPing and onHeartbeatTimeout lifecycle hooks.
  • Adds WsRpcHeartbeatConfig to WsRpcProtocolOptions so callers can tune ping interval and missed-Pong limits.
  • After a heartbeat-triggered reconnect, live stream listeners are re-subscribed via an onResubscribe callback; the thread detail manager marks snapshots as pending until a fresh one arrives.
  • Behavioral Change: heartbeats are now active by default even when no heartbeat config is passed, meaning the protocol will reconnect after 3 missed Pongs (~45s) without explicit opt-in.
Macroscope summarized 4725fe5.

Note

Medium Risk Changes core UI WebSocket keepalive and reconnect behavior for all clients using this layer; incorrect heartbeat or resubscribe handling could cause extra reconnect churn or briefly misleading thread state, but scope is limited to client-runtime transport and recovery.

Overview Replaces Effect’s strict single missed pong WebSocket RPC protocol with makeWsRpcTolerantSocketProtocol, which pings on a slower interval (default 15s) and only fails the socket after several consecutive missed pongs (default 3). createWsRpcProtocolLayer wires this in place of RpcClient.makeProtocolSocket, exposes optional WsRpcHeartbeatConfig, and forwards onHeartbeatPing / onHeartbeatTimeout lifecycle hooks.

On a real heartbeat timeout the client still reconnects and retries streams; WsTransport.subscribe invokes onResubscribe when a live subscription restarts after reconnect. Thread detail uses that hook to set isPending: true while keeping the last snapshot visible until a newer server snapshot arrives—avoiding stale “still streaming” UI after a dropped tail event.

Regression tests cover missed-pong tolerance, timeout reconnect, delayed pong reset, stream resubscribe after heartbeat reconnect, and thread-detail pending → fresh snapshot behavior.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 4725fe5bc56d8b6473e799340ff1f7c42f86c5ce. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro

      Run ID: 622698c2-699d-4967-9beb-8efb18c2e2bd

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. Approvability

      Verdict: Needs human review

      This PR introduces a new websocket heartbeat mechanism with ping/pong tolerance and automatic reconnection logic. While framed as a fix, it adds 265 lines of new protocol code that changes core transport layer behavior, warranting human review of the new connection lifecycle logic.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. Theo gg roasted this PR

      So sorry @StiensWout, you (and/or your agent) deserved the roast.

      Your heartbeat's been lossy since the day that we met — you skip a couple pongs, and I act like there's nothing to regret. I should call it, hang it up, admit that you're gone, but I wrote five hundred lines just to keep us holding on.

      https://github.com/user-attachments/assets/46aea3f1-d0bb-456b-ad1a-7a6071cdd6d0

      Want one in your own voice? Make songs with custom personas at mozartai.com

Sign in to join this conversation.