fix: Tolerate lossy UI websocket heartbeats #3097
main fromrefs/pull/3097/head·opened View on GitHubSummary#
- 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
WsRpcHeartbeatConfigfor 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.tsPATH="$HOME/.vite-plus/bin:$PATH" vp checkPATH="$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
makeWsRpcTolerantSocketProtocolwhich periodically sends WebSocket Ping frames (default: every 15s) and triggers reconnection after a configurable number of missed Pongs (default: 3). - Replaces
RpcClient.makeProtocolSocketwith the new tolerant protocol increateWsRpcProtocolLayer, wiring inonHeartbeatPingandonHeartbeatTimeoutlifecycle hooks. - Adds
WsRpcHeartbeatConfigtoWsRpcProtocolOptionsso callers can tune ping interval and missed-Pong limits. - After a heartbeat-triggered reconnect, live stream listeners are re-subscribed via an
onResubscribecallback; the thread detail manager marks snapshots as pending until a fresh one arrives. - Behavioral Change: heartbeats are now active by default even when no
heartbeatconfig is passed, meaning the protocol will reconnect after 3 missed Pongs (~45s) without explicit opt-in.
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
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
StiensWout(GitHub profile, opens in a new tab) changed the title
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).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
StiensWout(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
622698c2-699d-4967-9beb-8efb18c2e2bdYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
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