Repository navigation
fix(sdk): pass events missed during a WebSocket reconnect to RemoteConversation callbacks - #5950
Conversation
…nversation callbacks On reconnect, RemoteConversation only reconciled state.events over REST, so callbacks never received the events the server recorded while the events socket was down. Replay the reconciled events the callbacks have not seen, in order, and skip them if the new socket delivers them again. Drop the known-bug marker from F33.conversation-reconnect-callbacks. Fixes #5938 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg
Coverage Report •
|
||||||||||||||||||||
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Summary
Reviewed af4b7b26d5d570df6a48840f3d72b4d6d4f2191b against base 6e7a8532. The change is correctly scoped to this repository (Python SDK remote client) and to the ready-for-dev issue #5938; it does not touch LocalConversation, the REST fallback, the server /sockets/events contract, or the TypeScript client, matching the issue's stated non-goals.
The fix is sound: RemoteConversation now wraps the composed callback (socket_callback) to record delivered event ids, and its on_reconnect handler (replay_missed_events) reconciles state.events and forwards every not-yet-delivered event to the composed callback in cache (timestamp) order, before the reconnected socket's first frame is dispatched. Because the WebSocket client invokes _handle_reconnect() before self.callback(event) for the first ConversationStateUpdateEvent of the reconnected subscription, the "replay before live events" ordering is guaranteed. Dedup is handled by replayed_ids (an id already replayed and then re-delivered live is skipped) and by the initial delivered_ids.update(...) snapshot after create/attach (history is never replayed). Both delivered_ids/replayed_ids are mutated only from the WebSocket client thread (_client_loop and asyncio.to_thread(on_reconnect) both run on the client's own event loop thread), so the added state is not a new data race.
Verification
uv run --frozen pytest tests/sdk/conversation/remote/test_remote_conversation.py -q→ 60 passed.uv run --frozen pytest tests/sdk/conversation/remote tests/agent_server -k "remote or websocket or conversation"→ 645 passed, 9 failed. The 9 failures (test_agent_server_wsproto.py,test_credential_binding.py,test_local_storage.py) are environmental, not regressions: they fail withPermissionError: [Errno 13] Permission denied: '/var/openhands/conversations/...'in this sandbox. They are outside this PR's diff.ruff checkandruff format --checkpass on both changed Python files.- All 53 check runs for the head are
success/skipped(includingtest (24.x),security,check,cross-tests,REST API (OpenAPI)); the PR is not a draft andall-hands-botis the requested reviewer.
Notes (non-blocking)
- The feature-map edits are internally consistent: the
F33.conversation-reconnect-callbacksbullet drops itsknown buglabel and# bug:marker, and the summary (line 64), bullet prose, and Gotchas paragraph all now describe the replay. I verified there is no remaining# bug:mark orknown buglabel for this bullet. - The linked issue's acceptance criteria require the F33 map bullet to pass against a real agent server via the
f33.Proxyprobe. That live evidence is described in the PR body (xfailon base6e7a853,xpasson this head,map checkok after marker removal). I could not re-run the live agent-server probe in this environment, and the map is not exercised by CI, so that live-evidence claim rests on the PR description; it is not contradicted by anything in the head. Per the repo review guide this does not by itself justify withholding approval, but the human maintainer may want to confirm the recorded map run before merge. - The new unit test exercises the extracted
callback/on_reconnectclosures (captured fromWebSocketCallbackClientkwargs) rather than drivingWebSocketCallbackClient._client_loopend-to-end. The real reconnect trigger (_handle_reconnectbefore the first live callback) is already covered bytests/sdk/conversation/remote/test_websocket_client.py::test_websocket_client_calls_on_reconnect_after_subscription_restored, so the combination covers the path; this is a test-shape observation, not a defect. delivered_idsandreplayed_idsgrow with the conversation and are never pruned. This mirrors the unbounded local event cache it shadows (state.eventsalready retains every event), so it is not a new leak.
No material correctness, security, compatibility, or acceptance-criterion defect found on the current head.
✅ APPROVED
HUMAN:
This PR proposes a fix for a bug uncovered during the first full agentic run of
verify-agent-server.AGENT:
I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.
Requested by Engel · project thread
Why
When a live
RemoteConversation's events socket drops and reconnects (server restart, network hiccup), the events the server stored in between reachstate.eventsthrough the REST reconcile on reconnect, but never reach the conversation'scallbacks. A message sent right after a server restart is stored and shows instate.events, while a UI or visualizer streaming through callbacks never sees it.Summary
RemoteConversationnow wraps the socket callback: it records which event ids the callbacks have received, and itson_reconnecthandler reconcilesstate.eventsand then passes every cached event the callbacks have not seen to them, in cache (timestamp) order, before the reconnected socket's first event is dispatched.F33.conversation-reconnect-callbacksdrops its# bugmarker and "known bug" label; the bullet prose and the F33 Gotchas line now describe the replay.Issue Number
Fixes #5938
How to Test
tests/sdk/conversation/remote/test_remote_conversation.py::TestRemoteConversation::test_reconnect_replays_missed_events_to_callbacks_once: fails on main (callbacks see only the live events), passes here.uv run pytest tests/sdk/conversation/remote tests/agent_server -k "remote or websocket or conversation": 654 passed.--checkoutat 6e7a853):control-agent-server map run --file F33 --only F33.conversation-reconnect-callbacks --fresh --repeat 2->xfailin both attempts (callback_got_messagefalse).xpassin both attempts.control-agent-server map checkok;map run --only F33.conversation-reconnect-callbacks --fresh->pass.uv run pre-commit run --files <changed files>: all hooks pass.Video/Screenshots
n/a
Type
Notes
resend_mode=since; the replay uses the REST reconcile the client already did on reconnect, so it also covers events that a REST reconcile pulled in duringrun()while the socket was down.🤖 Generated with Claude Code
https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg
Generated by Claude Code
🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:af4b7b2-pythonRun
All tags pushed for this build
About Multi-Architecture Support
af4b7b2-python) is a multi-arch manifest supporting both amd64 and arm64af4b7b2-python-amd64) are also available if needed