Repository navigation
Conversation
A session's socket that drops (a network blink, a laptop asleep, a router restart) is dialled again with resumable=true, cursor and request_id, and the router sends what was missed. live, events(), the stores and pending approvals carry on through it. - session.connection (and dispatch.connection, AgentChat state.connection, useConnection) says connecting, connected (resumed), reconnecting (attempt, retryAt, cause), offline or disconnected (closed, ended, refused, gave_up, unwatched). - Backoff 500 ms to 30 s with jitter, for 5 minutes; each dial, the first included, gives up after dialTimeoutMs. A refused dial looks the session up: one ended or not ours stops the dialling. - healthcheck=true: a socket that reads nothing for 2.5 of the router's intervals is taken for dead (SocketSilentError), watched from its own first healthcheck so a router without them keeps its quiet sockets. A late check reads what waited behind a stall before deciding. - Tool answers are kept until the router says the call settled, and sent again on the socket that comes back. say, interrupt and close go over HTTP while the socket is away; close stops the dialling even when the router could not be told. - A call the router took off on its way down (1012) ends rather than come back in writing without the agent. - reconnect options now reach the session from sessions.create, resume and fork, and are no longer sent to the router. Checked against a local router through a fault proxy: resets, blackholes, refused dials, flapping, Wi-Fi off and on, latency, idle-killing balancers, router crash, restart and freeze, stalls and laptop sleep.
👋 Hi Oliver, want a second pair of eyes on this?I'm Jeffery, Stream's review agent. I look for bugs, regressions, missing tests and slow paths, and I only post findings I've checked against the code.
Or comment |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
When a session's socket dropped (a network blink, a laptop asleep, a router redeploy closing with 1012, a node relay ending with 1011), the JS SDK ended the session for good:
livewent false, approvals were cleared, running tools were aborted. Between "connected" and "dead" a page had nothing to show.The router already resumes (
resumable=true,cursor,request_id,resumed.complete), and since #2811 it sends an app-levelhealthcheckframe, which is the only way page JS can notice a half-open socket (the browser never shows it WebSocket pings). This PR uses both: the sameSessioncarries on through a drop, and says where it is.How it works
dialTimeoutMs(10 s). After a failed dial,GET /sessions/{id}decides: 404 or not live →ended, 401/403 →refused, anything else keeps trying. That lookup is bounded the same way.healthcheck=true: a socket that reads nothing for 2.5 of the router's intervals is dropped withSocketSilentErrorand dialled again. Each socket is watched only from its own first healthcheck, so a router without them keeps its quiet sockets. A check that fires late (busy event loop, sleep, frozen tab) lets the waiting reads run first before deciding.say,interruptandclosego over HTTP while the socket is away (or quiet);closestops the dialling even when the router couldn't be told.resumed{complete:false}resetsstateand approvals, andAgentChatre-reads the channel. A call the router took down with it (1012) ends rather than come back without the agent.Changes
src/reconnect.ts(new):ConnectionState,SocketConnectionstore,ReconnectOptions/RECONNECT_DEFAULTS, backoff, network source, healthcheckWatchdog.src/session.ts: the resume loop,session.connection,session.reconnect(), the answer ledger, HTTP fallbacks, healthcheck handling.src/sessions.ts:reconnectreaches the session fromcreate,resumeandfork, and is not sent in the request body.src/dispatch.ts:dispatch.connection, shared backoff helpers.src/agent-chat.ts:state.connection, re-read after an incomplete resume.src/react.ts:useConnection.src/errors.ts,src/socket.ts:SocketClosedError.code,SocketSilentError.README.md: "Staying connected"..claude/skills/sdk/notes/2026-11-30-session-socket-resume.md: what the other SDKs should port.API change:
session.say()andsession.interrupt()returnPromise<void>instead ofvoid, so the HTTP fallback can report a failure. Source-compatible for callers that ignore the result.Validation
npm run typecheck,npm run build,npm test(923 tests) and the generated-types check pass on top of currentmain.tests/reconnect.test.tsruns against the real HTTP/WSTestRouter; each new test was mutation-checked (it fails with the behaviour it guards removed). Among them: resume with cursor and request id, 1011/1012 resume and 1000/leftend, ended/refused lookups, giving up andreconnect(), flapping backoff, offline/online, a hung first dial and a hung lookup, a socket silent past its healthchecks, a socket watched only from its own healthcheck, short and long stalls, quietclose()over HTTP, and thereconnectoptions passing through.sessions.resume, busy event loop, laptop sleep, WebSocket-only outage, stop button while away, idle-killing load balancer, retry-now, first open into a dead network. The last full run covered 17 of 20 with 56 checks passing and none failing (stopped at the load balancer); the remaining three passed in the run before. The harness found thereconnectpass-through and stall bugs fixed here; it isn't committed.