Skip to content

refactor(client): rewrite the wsLink client as state machines - #7925

Open
KATT wants to merge 55 commits into
cursor/state-machine-primitive-b082from
cursor/ws-client-rearchitecture-b082
Open

KATT wants to merge 55 commits into
cursor/state-machine-primitive-b082from
cursor/ws-client-rearchitecture-b082

Conversation

@KATT

@KATT KATT commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Why

What

  • Pure state machines for the client, each connection and each request: transition(state, event) → { state, effects }. wsClient.ts only runs the effects, on chore(server): add a pure state machine primitive to unstable internals #7930's createMachineRuntime. Sockets, connectionParams and timers are async iterables read by one loop; the user's calls and result callbacks stay synchronous.
  • States carry only the data valid in them: queries and mutations can't move or show started, a subscription's replay lives in the states that have one, and the handover carries its own replacement error. Type tests prove the removed states can't be built.
  • Only the socket a request was sent on can answer it. The model gives each request its id on the wire. If the new socket is lost after a move, the subscription is resent on the old one with a fresh id, because the old server still holds the stopped one.
  • A server reconnect switches sockets gracefully and invisibly to subscribers (below). The old socket takes new requests until its successor is ready. Each subscription is sent on the successor, and only stopped on the old socket once the successor has started it.
  • A pong timeout reconnects at once, even with connectionParams pending. If the socket never heard from the server, the backoff keeps growing. onClose gets { code: 1006 }.
  • close() stops every subscription on the server, as unsubscribing does. A lazy client closes a socket with nothing to do while connectionParams are pending, and stops retrying once no requests are left. A replaced socket closes once nothing is left on it, also after a failed send or a lost successor.
  • No public API changes.

Stack

Each PR is based on the one below it, so its diff shows only its own commits. Merge from the bottom up.

  1. fix(server): let mergeAsyncIterables release the last buffered value #7932: mergeAsyncIterables lets go of its last value, based on main
  2. chore(server): add a pure state machine primitive to unstable internals #7930: the state machine primitives
  3. refactor(client): rewrite the wsLink client as state machines #7925 (this PR): the wsLink client rewritten as state machines

How a subscription switches sockets after a reconnect notice

One subscription, sent on socket A, has delivered events 1–3 when the server asks for a reconnect. Three machines take part: the client's (clientTransition, which routes each event to the steps in clientStep.ts, over the state in clientModel.ts), one per socket (connectionTransition) and one per request (requestTransition). They return effects as data; runEffect in wsClient.ts performs them. Nothing on A stops until its successor B has taken over: make before break, for the socket and for each subscription.

  1. The reconnect notice arrives. The server sends { id: null, method: 'reconnect' } on every socket (broadcastReconnectNotification), and keeps serving them. A's frames reach the client machine as frame events (transportEvents). onFrame retires A only if it is the current connection and not already retired (reconnect).
  2. A is retired, and still serves everything. retire moves A from ready to draining (transition) and makes it the client's handover. It starts the backoff for B at retry index 0, and the public state stays pending.
  3. B opens, then connectionParams resolve. The backoff timer (timer) calls connect, which emits openSocket (interpreter). On socketOpened, B goes to handshaking and asks for params (transition, resolveParams, connectionParamsEvents). On paramsResolved, B becomes ready and sends them (transition), then onReady runs.
  4. Make: the subscription is sent on B too. startMoves, called from onReady, puts every subscription sent on a draining connection into moving from A to B (moveSubscription). flush sends them on B in one batch with the queued requests. Each carries the lastEventId of the last delivered event, which sendRequests puts in the message's params. Nothing is sent to A.
  5. A keeps delivering until B's started. A's events for a moving subscription reach the subscriber (from A). Their ids go into the state's replayed, since B resends them from the lastEventId it got.
  6. Break: B's started switches the subscription. B's first frame for it, its started, is the ack: it makes B serve the subscription and sends subscription.stop to A, in that order (from B, sendStop). No event from B is needed. A tRPC server sends started before any event; if another server sent an event first, that would switch it instead. The subscription is then leaving A until A has handled the stop (step 7).
  7. Late frames from A. A can send events after B's started, before it reads the stop. Frames go only to requests their connection serves (onFrame). B resends events with ids, so A's copies are dropped. B can't resend events without ids, so the subscription is leaving A until A has handled the stop (entered, onLeaving). The client sends A a PING after the stop, the fence (Fence). A's events without ids are still delivered (as fix(client): isolate retired WebSocket close and error events #7727 does), its events with ids and started are dropped, and the PONG to that PING (fenced, onFenced), A's stopped or A's loss makes it sent on B, whichever comes first. The transport matches each PONG to its own PING, keepAlive's included (pings). The fence relies on a server contract: a PING is answered in order, after the frames before it, and no subscription data is sent after its stop is handled. v11's adapter keeps it, so the PONG comes after everything A sent for the subscription, and a procedure that ignores its abort no longer keeps A open. A server that doesn't answer PING (v10, dangerouslyDisablePong) falls back to stopped or loss. A's connection-level frames, such as another reconnect or an id: null error, are ignored while it drains.
  8. If either socket goes mid-move. If B is lost, the subscription is sent on A again (B lost), and moves to the next connection once that's ready. If A is lost, it stays on B, which is already resending (A lost); nothing is shown. A leaving subscription is sent on B if A is lost. If B is lost, it resumes on the next connection and keeps listening to A until A has handled its stop (queued); each Left keeps the id it was sent with there, so A's late frames aren't mistaken for a resend on A with a fresh id. If B is replaced before it has confirmed the subscription, the subscription is stopped on B and moves to the next connection as soon as that's ready, and A serves until that one confirms it (moveSubscription). A subscription still leaving A when B is replaced moves on to the next connection at once, and keeps delivering A's events without ids until A has handled its stop: leaving holds every connection being left (Left). If B is lost while A is still open, A takes over as the handover, so the subscription is sent on A again at once (onLost). moveStragglers moves on a subscription left on a draining connection.
  9. A closes once it's drained. closeIfDrained closes A once A takes no new requests and serves nothing (servingConnections): no request in flight on it, and no subscription moving from or leaving it. It runs after the moves (onReady), after each frame (onFrame) and on unsubscribe (unsubscribe). Only the handover takes new requests, until B is ready, so an older replaced socket closes once drained even while the handover stands in for B, and close() closes the drained ones at once (close).
sequenceDiagram
  participant U as Subscriber
  participant C as Client
  participant A as Socket A (old)
  participant B as Socket B (new)
  participant S as Server
  Note over U,S: subscription sent on A, events 1–3 delivered
  S->>A: 1. reconnect notice
  A->>C: 1. frame
  C->>C: 2. A draining, takes new requests, state stays pending
  C->>A: 2. new query, answered on A
  S->>A: event 4
  A-->>U: 2. event 4
  C->>B: 3. openSocket after the backoff timer
  Note over C,B: 3. slow params: A serves. Throwing params: B retries, state stays pending
  C->>B: 3. connectionParams, B ready
  C->>B: 4. subscription with lastEventId 4
  S->>A: event 5
  A-->>U: 5. event 5, recorded as replayed
  S->>B: 6. started, then events 5 and 6
  B->>C: 6. started, hidden: the ack
  C->>A: 6. subscription.stop, before any event from B
  C->>A: 6. PING, the fence
  B->>C: 6. event 5, dropped as replayed
  B-->>U: 6. event 6
  A->>C: 7. event 6 from A, sent before the stop, dropped
  A->>C: 7. PONG: A has handled the stop
  C->>A: 9. closeSocket once A serves nothing
Loading

The request machine for this subscription. s is started, whether the subscriber has seen started:

stateDiagram-v2
  direction LR
  state "sent on A, s" as sentA
  state "moving A to B, s" as moving
  state "leaving A for B, s" as leaving
  state "sent on B, s" as sentB
  state "queued" as queued
  [*] --> sentA
  sentA --> sentA : data, records lastEventId
  sentA --> moving : move once B is ready, sent on B with lastEventId
  moving --> moving : data from A, delivered, id recorded as replayed
  moving --> sentA : B lost
  moving --> sentB : A lost
  moving --> leaving : B's started (its first frame), sendStop then PING to A
  leaving --> leaving : A's data without ids delivered. B's frames as on B
  leaving --> sentB : A's PONG to that PING, A's stopped, or A lost
  leaving --> queued : B lost
  sentB --> sentB : started dropped if s. Replayed data dropped once. Data records lastEventId
  sentA --> queued : A lost, clears s
Loading

Which socket delivers which event:

Event Sent by A Sent by B Reaches the subscriber Why
1–3 yes from A before the notice
4 yes from A B isn't ready (step 2)
move B gets the subscription with lastEventId: '4'; A keeps it (step 4)
5 yes resent from A B hasn't sent started (step 5); its copy is dropped as replayed (step 6)
B's started hidden A gets subscription.stop and a PING (step 6)
6 late yes from B A's copy is dropped (step 7)

The subscriber gets 1–5 from A and 6… from B, with no duplicates and no gaps. Every event A delivers after the move is one B resends and the client drops, and from B's started on, only B's frames reach the subscription. This relies on the procedure resending, in order, every event after lastEventId, with ids unique to the subscription. The real-server test checks this with A paused while events are pushed every 2 ms, and the model invariant after every step of random runs. Those runs reach every point of a move: A's events before and after B's started, B's replays, data before started, a second reconnect notice mid-move (goals), and either socket lost at each point (goals), from scripted starts. After each run, every live subscription must get the event after its last, so none is lost.

Events without ids, from an untracked subscription or a tracked one, can't be resumed or deduplicated, so the client listens to A for them until A has handled the stop: its PONG to the PING sent after the stop, its stopped, or its loss (step 7). No event is lost while A stays up: each reaches the subscriber from A, from B or from both. An event published while both listen arrives twice, and the two sockets' events can interleave during the overlap (test). One only A had is delivered even if it arrives after B's started (test). If A is lost first, events only A had are lost, as on any reconnect. A server that answers a PING before the frames sent ahead of it, or sends a PONG unasked, could end the fence early and lose events only A had; tRPC's servers don't.

Generated state diagrams (from websockets.clientMachine.test.ts, which fails if they drift)

Client lifecycle (every change the seeded model test reaches):

stateDiagram-v2
  [*] --> idle : lazy
  [*] --> connecting
  backoff --> connecting : backoff timer, request
  backoff --> idle : backoff timer, close, inactivity timer
  connecting --> backoff : badFrame, openFailed, pongTimeout, reconnect, socketClosed, socketError
  connecting --> idle : close, inactivity timer
  connecting --> open : paramsResolved
  idle --> connecting : request
  open --> backoff : badFrame, pongTimeout, reconnect, socketClosed, socketError, socketUnwritable
  open --> idle : badFrame, close, inactivity timer, pongTimeout, socketClosed, socketError, socketUnwritable
Loading

Connection (closeReported: closed, and onClose has been called or there was no socket):

stateDiagram-v2
  [*] --> connecting
  connecting --> handshaking : socketOpened
  connecting --> closeReported : openFailed, socketClosed
  connecting --> closed : socketError, close
  handshaking --> ready : paramsResolved
  handshaking --> closed : openFailed, socketError, close
  handshaking --> closeReported : socketClosed, pongTimeout, badFrame
  handshaking --> handshaking : received, rejection
  ready --> closeReported : socketClosed, pongTimeout, badFrame
  ready --> closed : socketError, socketUnwritable, close
  ready --> draining : drain
  ready --> ready : received, result, rejection
  draining --> closeReported : socketClosed, pongTimeout, badFrame
  draining --> closed : socketError, socketUnwritable, close
  draining --> draining : received
  closed --> closed : socketOpened, paramsResolved, openFailed, socketError, socketUnwritable, pongTimeout, badFrame, close, received
  closed --> closeReported : socketClosed, released
  closeReported --> closeReported : socketOpened, paramsResolved, openFailed, socketClosed, socketError, socketUnwritable, pongTimeout, badFrame, close, received, released
  closeReported --> [*]
Loading

Request, query and mutation:

stateDiagram-v2
  [*] --> queued
  queued --> queued : batch, unbatch
  queued --> sent : send
  queued --> settled : connectionLost, unsubscribe, clientClose
  sent --> settled : connectionLost, started, data, stopped, error, unsubscribe, sendFailed
  sent --> sent : clientClose
  settled --> [*]
Loading

Request, subscription (on new: from the connection it's moving to; (tracked): it has a lastEventId):

stateDiagram-v2
  [*] --> queued
  queued --> queued : batch, unbatch, connectionLost, started, data, stopped, error, fenced, an earlier move's fence
  queued --> sent : send
  queued --> settled : unsubscribe, clientClose
  queued --> leaving : send
  sent --> moving : move, move to a newer one
  sent --> queued : connectionLost
  sent --> sent : started, data
  sent --> settled : stopped, error, unsubscribe, clientClose, sendFailed
  moving --> moving : move to a newer one, started, data
  moving --> sent : connectionLost, connectionLost on new
  moving --> leaving : started on new, data on new, started on new (tracked), data on new (tracked)
  moving --> settled : stopped, stopped on new, error, error on new, unsubscribe, clientClose, sendFailed
  leaving --> moving : move to a newer one
  leaving --> sent : connectionLost, stopped, error, fenced
  leaving --> queued : connectionLost on new
  leaving --> leaving : started, started on new, data, data on new, an earlier move's fence, started on new (tracked), data on new (tracked)
  leaving --> settled : stopped on new, error on new, unsubscribe, clientClose, sendFailed
  settled --> [*]
Loading

Links

None

Alternatives considered

Testing

  • Regression check: websockets.retiredSocket.test.ts (cases from the fix(client): fail or resend only the requests sent on a closing WebSocket #7910 review) and websockets.serverReconnect.test.ts fail on main. websockets.memory.test.ts's "lets go of the last frame" fails if the socket's events are read with for await or yield*. websockets.serverReconnect.test.ts's "an untracked subscription gets an event only the old socket had, even when it arrives after the new socket's started" fails ([1, 3]) if an untracked subscription stops listening to A at B's started, and "a tracked subscription gets an event without an id that only the old socket had" fails the same way if only untracked subscriptions leave A. Its "leaves the old socket at the PONG to the PING after its stop, even if the procedure never stops" fails without the fence, and websockets.transport.test.ts fails if keepAlive PINGs aren't queued with fences. websockets.test.ts and websockets.encoder.test.ts are main's, from fix(client): isolate retired WebSocket close and error events #7727.
  • Regression check for fix(client): isolate retired WebSocket close and error events #7727's third independent review: websockets.makeBeforeBreak.test.ts holds its real-server probes. "The new socket lost after the move: the subscription is resent on the old socket with a fresh id…" fails with Duplicate id 1 if the resend reuses the stopped id. The late-stopped and second-notice cases fail too. The simulated server in websockets.clientMachine.test.ts now fails any run that sends an id a socket still holds. The connectionParams cases already passed.
  • Regression check for fix(client): isolate retired WebSocket close and error events #7727's 554d4e030: its "a subscription moving to a socket that a newer one replaces is stopped there, and can be sent there again" fails on 3dacb422e, as does the request machine's redirect test. Its other four new cases (a fresh id on the old socket, the old socket's late stopped, and the two where a new socket closes while its connectionParams are pending) already passed.
  • Regression check for fix(client): isolate retired WebSocket close and error events #7727's 2cd024579: its "a subscription lost with the new socket is sent to the old one while a newer socket is still opening" and "an untracked subscription that moves on again keeps the first socket it left until that one's events have arrived" fail on db2b5c6fb. The model's new coverage goals, a lost handover handed to the socket it replaced and a subscription moved on while still leaving a socket, are unreachable without the fixes, and a type test proves a leaving subscription leaves at least one connection.
  • Regression check for CodeRabbit's review of fix(client): isolate retired WebSocket close and error events #7727 at 2cd024579, which applies here too: websockets.serverReconnect.test.ts's "an untracked subscription gets an event only the old socket had when the new socket is lost before it arrives" fails ([1]) on 8cbb2eff0, as does fix(client): isolate retired WebSocket close and error events #7727's "an untracked subscription lost with the new socket still gets the old socket's late events from before it was sent again" from 6a7202a0a, and so does the model invariant that no event without an id is dropped from a socket the subscription left while that socket is up. An event like this can reach the subscriber before the resumed subscription's started, since it belongs to the earlier run.
  • Regression check for CodeRabbit's review: websockets.close.test.ts and websockets.lazy.test.ts fail on 771429e05. So does the connectionParams case in websockets.transport.test.ts. The model invariant I16, that a replaced connection closes once nothing is left on it, fails with one-action counterexamples there.
  • Regression check for fix(client): isolate retired WebSocket close and error events #7727's fourth independent review: websockets.makeBeforeBreak.test.ts's "an older replaced socket closes once drained while a newer one stands in for a replacement that hasn't opened" fails on 362de2658 (A stays OPEN until the next socket opens), as does fix(client): isolate retired WebSocket close and error events #7727's "a subscription sent again on a restored socket closes the first socket it was still draining once it ends" from a23c8a635. I16, now checked in every lifecycle, fails there with one-action counterexamples.
  • websockets.clientMachine.test.ts checks the invariants after every step of random event sequences, three quarters of which start mid-move; websockets.clientModel.realServer.test.ts runs them against a real server. Both run with @fast-check/vitest's test.prop on fast-check 4: the seeded model run must reach every coverage goal, unseeded runs show their seed in the test name, and a failure prints the seed and path that replay it.
  • websockets.clientMachine.types.test.ts proves with @ts-expect-error that a query can't move, show started or resume, a queued subscription has no started and no events to replay, and a replacement error can't exist without its handover.
  • Mutation-checked: stopping the old subscription when the move starts, waiting for an event instead of started, delivering the old socket's late tracked events, not dropping replayed events, resuming from the wrong lastEventId, holding new requests until the successor is ready, closing the old socket while a move is pending, dropping an untracked subscription's events from A at B's started, not sending the fence, ending leaving on an earlier move's fence, leaving A only for untracked subscriptions, and dropping the replay on entering leaving each fail the model or real-server tests.
  • requestManager.test.ts: flush() → all(), assertion unchanged. The request machine now does the queueing, so RequestManager has no flush().

AI model

Claude Opus 5.5

Open in Web Open in Cursor 

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Summary by CodeRabbit

  • New Features
    • WebSocket connections now transition to replacement connections while the existing connection remains available, helping queries, mutations, and subscriptions continue during reconnects.
    • Subscription handovers avoid replaying events already received.
  • Bug Fixes
    • Unresponsive connections now trigger reconnection after a keep-alive timeout.
    • Closing a client now completes subscription cleanup and handles outstanding requests more reliably.
    • Invalid incoming frames and connection failures are handled more consistently.

@vercel

vercel Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
examples-next-minimal-starter Ready Ready Preview Nov 11, 2026 3:03pm UTC
examples-todomvc Ready Ready Preview Nov 11, 2026 3:03pm UTC
next-prisma-starter Ready Ready Preview Nov 11, 2026 3:03pm UTC
next-prisma-websockets-starter Ready Ready Preview Nov 11, 2026 3:03pm UTC
tanstack-start-sse-chat Ready Ready Preview Nov 11, 2026 3:03pm UTC
trpc-next-app-dir Ready Ready Preview Nov 11, 2026 3:03pm UTC
www Ready Ready Preview Nov 11, 2026 3:03pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

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

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
Walkthrough

The WebSocket client implementation is reorganized around client, connection, and request state machines, a transport layer, and a runtime that applies machine effects. Added tests cover reconnects, subscription handoffs, transport behavior, request lifecycle, and state-machine transitions.

Changes: WebSocket client state-machine runtime

Layer / File(s) Summary
Client and connection state contracts
packages/client/src/links/wsLink/wsClient/machine/clientModel.ts, packages/client/src/links/wsLink/wsClient/machine/connectionMachine.ts
New types describe client lifecycle, connection phases, events, effects, and handover state.
Request transitions and subscription handoff
packages/client/src/links/wsLink/wsClient/machine/requestMachine.ts
Request transitions handle query and mutation settlement, subscription delivery, replay filtering, and movement between connections.
Connection and client lifecycle transitions
packages/client/src/links/wsLink/wsClient/machine/clientMachine.ts, packages/client/src/links/wsLink/wsClient/machine/clientStep.ts, packages/client/src/links/wsLink/wsClient/machine/connectionMachine.ts
Client and connection transitions process socket events, frames, timers, retries, closure, and request effects.
Transport and resource lifecycle
packages/client/src/links/wsLink/wsClient/transport.ts, packages/client/src/links/wsLink/wsClient/scope.ts, packages/client/src/links/wsLink/wsClient/wsConnection.ts
The transport handles socket events, connection parameters, frame decoding, keepalive, fences, and disposal. The former connection implementation is removed.
Runtime effects and request management
packages/client/src/links/wsLink/wsClient/wsClient.ts, packages/client/src/links/wsLink/wsClient/requestManager.ts, packages/client/src/links/wsLink/wsClient/utils.ts, packages/client/src/links/wsLink/createWsClient.ts
The runtime applies machine effects through transports, timers, requests, and callbacks. RequestManager stores requests by ID, and createWSClient delegates to the runtime.
State-machine validation
packages/tests/server/websockets.clientMachine.*
Model-based and type-level tests exercise transition invariants, request ownership, subscription movement, replay handling, and generated state diagrams.
Reconnect and subscription handoff tests
packages/tests/server/websockets.{clientModel.realServer,keepAlivePongTimeout,makeBeforeBreak,retiredSocket,serverReconnect}.test.ts
Server-backed tests cover connection replacement, request routing, subscription movement, replay, socket loss, and retries.
Transport and client lifecycle tests
packages/tests/server/websockets.{close,encoder,lazy,memory,transport,undecodableFrame}.test.ts, packages/client/src/links/wsLink/wsClient/requestManager.test.ts
Tests cover transport disposal and fencing, frame decoding, memory release, client closure, lazy mode, and request lifecycle.

Estimated code review effort: 4 (Complex) | ~60 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant createWSClient
  participant createWsClientRuntime
  participant clientTransition
  participant openTransport
  createWSClient->>createWsClientRuntime: create client runtime
  createWsClientRuntime->>clientTransition: dispatch client event
  clientTransition->>createWsClientRuntime: return transition effects
  createWsClientRuntime->>openTransport: open WebSocket transport
  openTransport->>createWsClientRuntime: emit transport events
  createWsClientRuntime->>clientTransition: dispatch connection or frame event
Loading

Possibly related issues

  • trpc/trpc#4774 — The PR changes connection failure and retry state handling, but the supplied context does not establish that it fully fixes the reported fast retry loop.

Possibly related PRs

  • trpc/trpc#7914 — Both changes handle pong timeouts and reconnect behavior.
  • trpc/trpc#7911 — Both changes address request batching during connection changes.
  • trpc/trpc#7727 — Both changes handle late events from retired sockets during reconnect handoffs.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 54.05% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: rewriting the wsLink client around client, connection, and request state machines.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 54.05% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 74 functions across 26 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Review coverage is incomplete: 1 file could not be fully reviewed. Findings from completed review steps are included; see review info for details.

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

@vercel
vercel Bot temporarily deployed to Preview – trpc-next-app-dir November 4, 2026 22:19 Inactive
@vercel
vercel Bot temporarily deployed to Preview – examples-next-minimal-starter November 4, 2026 22:19 Inactive
@vercel
vercel Bot temporarily deployed to Preview – www November 4, 2026 22:19 Inactive
@vercel
vercel Bot temporarily deployed to Preview – examples-todomvc November 4, 2026 22:19 Inactive
@vercel
vercel Bot temporarily deployed to Preview – next-prisma-starter November 4, 2026 22:19 Inactive
@pkg-pr-new

pkg-pr-new Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/@trpc/client@7925

@trpc/next

npm i https://pkg.pr.new/@trpc/next@7925

@trpc/openapi

npm i https://pkg.pr.new/@trpc/openapi@7925

@trpc/react-query

npm i https://pkg.pr.new/@trpc/react-query@7925

@trpc/server

npm i https://pkg.pr.new/@trpc/server@7925

@trpc/tanstack-react-query

npm i https://pkg.pr.new/@trpc/tanstack-react-query@7925

@trpc/upgrade

npm i https://pkg.pr.new/@trpc/upgrade@7925

commit: a634034

cursoragent and others added 28 commits November 5, 2026 12:03
…r start

Queries and mutations get their own state type without moving, leaving or
started; only subscriptions carry lastEventId and replayed. A queued
subscription has no started flag. Moving a subscription is its own function
that only takes a subscription.

Type tests prove the removed states can't be built.

Co-authored-by: alexander <[email protected]>
…moving states

Only a tracked subscription that is sent or moving has ids to replay, so a
queued or leaving one can't carry any, and resuming needn't clear them.

Co-authored-by: alexander <[email protected]>
…t error

The handover is no longer an id that has to be the newest draining
connection, with a replacement error alongside that only means something
while it exists. The handover record carries its own error, and the older
draining connections are listed apart from it, so neither can disagree.

Co-authored-by: alexander <[email protected]>
clientModel.ts holds the client's state, events and effects, and the queries
on them; clientStep.ts the mutable scratchpad one transition runs on; and
clientMachine.ts routes each event. servingConnections moves next to the
request states it reads.

Co-authored-by: alexander <[email protected]>
The seeded model property checks its coverage goals with the shared
reporter, and the real-server property keeps its seed, run count, timeout
and endOnFailure; each run still closes its server and client before the
next.

Co-authored-by: alexander <[email protected]>
…ones, PING and PONG included

A real socket can't let a PONG overtake the frames sent before it. The
untracked-event test now holds the old socket's frames in order, so it
stays meaningful once the client listens for that PONG.

Co-authored-by: alexander <[email protected]>
…PING sent after its stop

A procedure that ignores its abort never sends `stopped`, so a moved
untracked subscription kept listening to the old socket, and kept it
open, until that socket was lost. After the stop, the client now sends a
PING on the old socket. The server answers PINGs in order, after the
frames before them, and sends nothing for a subscription once it has
handled its stop, so that PING's PONG ends the move without a timeout.
A server that doesn't answer PING still ends it with `stopped` or by
going away.

Every PING, keepAlive's included, goes through one queue on the
transport, so each PONG is matched to its own PING. A fence names its
request and the connection the request moved to, so a fence from an
earlier move off the same socket can't end a later one.

Co-authored-by: alexander <[email protected]>
… subscription too, until it has handled the stop

A moved subscription with event ids skipped the leaving state, so an event
without an id that only the old socket had was dropped if it arrived after
the new socket's started. Every confirmed move now leaves the old socket,
fenced, as #7727 does, and leaving keeps the replay owed. The model sends
such events in tracked runs too.

Co-authored-by: alexander <[email protected]>
…ay still hold the stopped one

When the new socket is lost after a move, the subscription was resent on
the old socket with the id that socket had just been sent the stop for.
Until its server ended the stopped subscription, it answered
`Duplicate id`, or its late `stopped` ended the resent one.

The model now gives each request its id on the wire, and a subscription
resent on a connection no newer than the newest its id was sent on gets a
fresh one. The model test's server answers a duplicate id as a failure,
and the independent review's real-server probes are regression tests.

Co-authored-by: alexander <[email protected]>
… end lazy and connectionParams work with nothing left

- A replaced socket closes once a failed move batch or a lost successor leaves it serving nothing.
- close() stops every subscription on the server, as unsubscribing does.
- A lazy client's inactivity timer starts when the socket opens, and a lazy client with no requests goes idle instead of retrying.
- The connectionParams intake ends when its transport is disposed.
- Tests: a model invariant for drained sockets and for request batching, an opaque-id replay test, and the CodeRabbit test nitpicks.

Co-authored-by: alexander <[email protected]>
… it was moving to is replaced

A subscription moving from A to B, when a second reconnect notice replaced B before B confirmed it, waited for B to start it and for A's fence before moving on to C, so it depended on a socket the server had asked to retire. It's now stopped on B and sent on C at once, and A serves it until C confirms it. Ports #7727's 01c8328.

Co-authored-by: alexander <[email protected]>
… one on while it still leaves others

When the handover was lost while an older replaced socket was still open, nothing took new or lost requests until the next connection was ready. The newest replaced socket now becomes the handover, so a subscription lost with the new socket goes back to the old one at once.

A subscription leaving A for B couldn't move on when B was replaced: it waited for A's fence first. `leaving` now holds every connection being left, and `moving` carries those an earlier move left, so it moves to the next connection at once and still delivers A's events without ids until A has handled its stop. Ports #7727's ec7d5bb and 88a607d.

Co-authored-by: alexander <[email protected]>
…til the newest confirms it

Assert B's stop, and no stop on A, while C's started is still held.

Co-authored-by: alexander <[email protected]>
A subscription leaving its old socket that loses the new one is queued
again, and dropped what it was leaving: an event without an id that only
the old socket had was lost while that socket was up. Queued
subscriptions now keep `left`, and each `Left` keeps the wire it was
sent with there, so a resend with a fresh wire on the same socket is told
apart from the old one's late frames.

Co-authored-by: alexander <[email protected]>
…omma

Edge labels are comma-separated, so ', tracked' read as an event of its own.

Co-authored-by: alexander <[email protected]>
…tands in

A replaced connection only closed once its replacement was ready or the
client was idle. While a newer replaced connection stood in during a
backoff, an older one with nothing left stayed open until the next socket
opened, which a failing server could put off indefinitely. close() also
left such connections open until its pending queries finished.

closeIfDrained now closes any drained connection that takes no new
requests, and close() closes the drained ones at once. Model invariant I16
now holds in every lifecycle.

Co-authored-by: alexander <[email protected]>
…ent-rearchitecture-b082

Brings in main and the trimmed state machine primitive:

- Timers call back instead of being read through `intake`.
- The diagram helpers come from the test harness.
- `ws` no longer exports `WebSocket.Server` as a type; use `WebSocketServer`.
- The server no longer starts a subscription it held on a socket that has
  closed (#7976), so the restart-mid-handover test waits for the
  procedure's second run, not its third.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_019Un67fVtH18NTKvJTnoc8E

This branch was successfully deployed

5 active deployments
Preview – www — a634034c Deployed Nov 11, 2026 by vercel[bot]
Preview – trpc-next-app-dir — a634034c Deployed Nov 11, 2026 by vercel[bot]
Preview – examples-todomvc — a634034c Deployed Nov 11, 2026 by vercel[bot]
Preview – next-prisma-starter — a634034c Deployed Nov 11, 2026 by vercel[bot]
Preview – examples-next-minimal-starter — a634034c Deployed Nov 11, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants