Skip to content

fix(sdk): pass events missed during a WebSocket reconnect to RemoteConversation callbacks - #5950

Merged
enyst merged 1 commit into
mainfrom
claude/umbrella-bug-fixes-tgz9ai-reconnect-callbacks
Oct 19, 2026
Merged

enyst merged 1 commit into
mainfrom
claude/umbrella-bug-fixes-tgz9ai-reconnect-callbacks

Conversation

@enyst

@enyst enyst commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

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 reach state.events through the REST reconcile on reconnect, but never reach the conversation's callbacks. A message sent right after a server restart is stored and shows in state.events, while a UI or visualizer streaming through callbacks never sees it.

Summary

  • RemoteConversation now wraps the socket callback: it records which event ids the callbacks have received, and its on_reconnect handler reconciles state.events and 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.
  • Events replayed this way are skipped if the new socket delivers them again, so each reaches the callbacks once. History present when the conversation was created/attached is marked as seen and is not replayed.
  • Map: F33.conversation-reconnect-callbacks drops its # bug marker and "known bug" label; the bullet prose and the F33 Gotchas line now describe the replay.

Issue Number

Fixes #5938

How to Test

  • New unit 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.
  • Map proof against a real agent server (the probe drops and blocks the events socket at a TCP proxy, sends a message over REST, then lets the socket back):
    • main (--checkout at 6e7a853): control-agent-server map run --file F33 --only F33.conversation-reconnect-callbacks --fresh --repeat 2 -> xfail in both attempts (callback_got_message false).
    • this branch: same command -> xpass in both attempts.
    • with the marker removed: control-agent-server map check ok; 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

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • The client still reconnects without the server's 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 during run() while the socket was down.
  • The set of delivered ids grows with the conversation, like the local event cache it mirrors.

🤖 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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:af4b7b2-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-af4b7b2-python \
  ghcr.io/openhands/agent-server:af4b7b2-python

All tags pushed for this build

ghcr.io/openhands/agent-server:af4b7b2-golang-amd64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-golang-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-golang-amd64
ghcr.io/openhands/agent-server:af4b7b2-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:af4b7b2-golang-arm64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-golang-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-golang-arm64
ghcr.io/openhands/agent-server:af4b7b2-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:af4b7b2-java-amd64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-java-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-java-amd64
ghcr.io/openhands/agent-server:af4b7b2-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:af4b7b2-java-arm64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-java-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-java-arm64
ghcr.io/openhands/agent-server:af4b7b2-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:af4b7b2-python-amd64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python-amd64
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime-amd64
ghcr.io/openhands/agent-server:af4b7b2-python-arm64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python-arm64
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime-arm64
ghcr.io/openhands/agent-server:af4b7b2-python-minimal-amd64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python-minimal-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python-minimal-amd64
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:af4b7b2-python-minimal-arm64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python-minimal-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python-minimal-arm64
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:af4b7b2-python-slim-amd64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python-slim-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python-slim-amd64
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:af4b7b2-python-slim-arm64
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python-slim-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python-slim-arm64
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:af4b7b2-golang
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-golang
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-golang
ghcr.io/openhands/agent-server:af4b7b2-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:af4b7b2-java
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-java
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-java
ghcr.io/openhands/agent-server:af4b7b2-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:af4b7b2-python-minimal
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python-minimal
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python-minimal
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime-minimal
ghcr.io/openhands/agent-server:af4b7b2-python-slim
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python-slim
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python-slim
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime-slim
ghcr.io/openhands/agent-server:af4b7b2-python
ghcr.io/openhands/agent-server:af4b7b26d5d570df6a48840f3d72b4d6d4f2191b-python
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-reconnect-callbacks-python
ghcr.io/openhands/agent-server:af4b7b2-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., af4b7b2-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., af4b7b2-python-amd64) are also available if needed

…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
@enyst enyst self-assigned this Oct 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/conversation/impl
   remote_conversation.py8218889%111, 186, 213, 226, 228–231, 257, 275–276, 287–290, 297–298, 303–306, 390, 400–402, 408, 492–493, 628–631, 633, 659–663, 668–671, 674, 690, 794, 828, 886–887, 891–892, 906, 972, 978, 1082–1085, 1134–1135, 1159–1160, 1193–1194, 1227–1228, 1249, 1259, 1263, 1272–1273, 1312, 1466–1467, 1566–1567, 1571, 1576–1580, 1604, 1609, 1656, 1904–1905, 1913–1914
TOTAL484681305773% 

@github-actions

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@enyst
enyst marked this pull request as ready for review October 14, 2026 02:54
@enyst
enyst requested a review from all-hands-bot October 14, 2026 02:55

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with PermissionError: [Errno 13] Permission denied: '/var/openhands/conversations/...' in this sandbox. They are outside this PR's diff.
  • ruff check and ruff format --check pass on both changed Python files.
  • All 53 check runs for the head are success/skipped (including test (24.x), security, check, cross-tests, REST API (OpenAPI)); the PR is not a draft and all-hands-bot is the requested reviewer.

Notes (non-blocking)

  • The feature-map edits are internally consistent: the F33.conversation-reconnect-callbacks bullet drops its known bug label 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 or known bug label for this bullet.
  • The linked issue's acceptance criteria require the F33 map bullet to pass against a real agent server via the f33.Proxy probe. That live evidence is described in the PR body (xfail on base 6e7a853, xpass on this head, map check ok 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_reconnect closures (captured from WebSocketCallbackClient kwargs) rather than driving WebSocketCallbackClient._client_loop end-to-end. The real reconnect trigger (_handle_reconnect before the first live callback) is already covered by tests/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_ids and replayed_ids grow with the conversation and are never pruned. This mirrors the unbounded local event cache it shadows (state.events already 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

@enyst
enyst merged commit 3f32945 into main Oct 19, 2026
57 checks passed
@enyst
enyst deleted the claude/umbrella-bug-fixes-tgz9ai-reconnect-callbacks branch October 19, 2026 01:22
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.

[Bug]: RemoteConversation callbacks never receive events the server recorded while the events WebSocket was reconnecting

3 participants