Keep agent chat turns alive across websocket disconnects, replay on reconnect - #4711
Merged
Merged
Conversation
…econnect
A dropped socket used to abort the in-flight chat/agent turn
(disconnect() -> cancelChatTurn()): closing the laptop lid killed the
agent mid-work with nothing to reattach to. Now the turn detaches from
the socket and keeps executing, and a reconnecting client replays what
it missed.
Server: a new ChatTurnRegistry keys detachable sessions by user+thread.
Every frame a turn emits is stamped with a monotonic chat_seq and kept
in a bounded replay buffer; delivery goes to whichever connection is
attached. On disconnect the session detaches (a grace timer aborts a
turn nobody reclaims, default 10 min); on `resume_chat {thread_id,
last_seq}` the server replies `chat_resumed` and replays the missed
tail, then streams live. Finished sessions stay replayable for a
retention window (default 5 min). tool_result / approvals / stop frames
arriving on the new connection are routed to the runner executing the
adopted turn, and a detached turn's pending client tool calls survive
the disconnect so a reconnected client can still answer them.
Web: GlobalChatStore tracks the per-thread chat_seq high-water mark and,
on every socket reopen, sends resume_chat for threads with a generation
in flight. chat_resumed with status "unknown" (or an incomplete replay)
resets the thread runtime and reconciles from persisted REST history.
Protocol: resume_chat command + chat_resumed outbound frame schemas.
Knobs: NODETOOL_CHAT_REPLAY_BUFFER_EVENTS,
NODETOOL_CHAT_DETACH_GRACE_MS, NODETOOL_CHAT_REPLAY_RETENTION_MS.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MWpARdXPyxfxgJnLR7SV6C
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.
Problem
A dropped websocket aborted the in-flight chat/agent turn (
disconnect()→cancelChatTurn()), so a laptop lid-close or network blip killed the agent mid-work. The client had no way to see what happened while it was away: chat frames carried no sequence numbers and the server kept no event buffer.What changed
Server — detachable chat-turn sessions (
packages/websocket/src/chat-turn-registry.ts)chat_messageturn now runs inside aChatTurnSession, keyed by user+thread in a process-wide registry. Every outbound frame is stamped with a monotonicchat_seqand kept in a bounded replay buffer (NODETOOL_CHAT_REPLAY_BUFFER_EVENTS, default 2000); delivery goes to whichever connection is attached.NODETOOL_CHAT_DETACH_GRACE_MS, default 10 min) aborts a turn nobody reclaims. Sessionlessinferenceturns and job behavior are unchanged.resume_chat {thread_id, last_seq}command: the server replies with achat_resumedheader (statusrunning/finished/unknown,replay_incompletewhen the buffer evicted the requested tail) and replays the missed frames, then live frames follow in seq order. Finished turns stay replayable for a retention window (NODETOOL_CHAT_REPLAY_RETENTION_MS, default 5 min); after that the client reconciles from persisted REST history.tool_result/ approval /stopframes arriving on the new connection are routed to the runner executing the adopted turn, and a detached turn's pending client tool calls survive the disconnect (replay re-delivers thetool_callframes so the reconnected client can still answer them).last_seqcan never skip newer frames. A new turn supersedes and aborts a detached one, matching existing single-turn semantics.Web client
GlobalChatStoretracks the per-threadchat_seqhigh-water mark and, on every socket reopen, sendsresume_chatfor threads with a generation in flight (registered after the tools manifest so replayedtool_callframes find the manifest in place).chat_resumedwith statusunknownor an incomplete replay resets the thread runtime to idle and reloads the thread's messages over REST.Protocol
resume_chatadded toUnifiedCommandType/ command schemas;chat_resumedadded to the outbound frame schemas. All schemas passthrough, so older clients ignore the newchat_seqfield.Tests
packages/websocket/tests/chat-turn-registry.test.ts— seq stamping, detach/attach replay ordering, stale-target guard, buffer eviction →replay_incomplete, supersede-abort, cross-turn seq continuity, detach-grace and retention timers.packages/websocket/tests/unified-websocket-runner-chat-resume.test.ts— end-to-end: turn survivesdisconnect(), finishes unattended, replays on a fresh connection with no duplicates;resume_chatfor an unknown thread;stopfrom a later connection aborts a detached turn.web/src/core/chat/__tests__/chatResume.test.ts— cursor tracking andchat_resumedhandling.npm run lintand web+electron typecheck clean (mobile typecheck fails only on uninstalled Expo deps in this sandbox, unrelated).Notes / scope
reconnect_jobunchanged); this PR covers chat/agent turns. A chat turn that routes into a workflow run still loses that job on disconnect.🤖 Generated with Claude Code
https://claude.ai/code/session_01MWpARdXPyxfxgJnLR7SV6C
Generated by Claude Code