Skip to content

fix(agent-server): send sync before error replies on the session socket - #6750

Open
enyst wants to merge 1 commit into
mainfrom
claude/fix-session-socket-sync-first
Open

enyst wants to merge 1 commit into
mainfrom
claude/fix-session-socket-sync-first

Conversation

@enyst

@enyst enyst commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

HUMAN:
Engel Nyst (@enyst) set up a daily automation that keeps the verify-agent-server and verify-sdk feature maps current. This PR fixes a bug its run for main@5ff99f1 found.


AGENT:
I'm an AI agent helping Engel Nyst (@enyst) with project work.

Why

Since #6709, /sockets/session/{conversation_id} reads client frames from connect, before subscribe_to_events returns (it waits for the conversation state lock). An invalid inbound frame (for example role: "assistant") sent while a step holds the lock got its error frame queued at once, so it reached the client before sync. Clients that treat sync as the start of the stream (cursor bookkeeping, through_seq) saw a frame before it.

Summary

  • _ConnectionWriter can hold "reply" frames (hold_replies / release_replies); held replies count against the same pending-bytes budget.
  • session_socket holds replies from connect and releases them right after sync, the replay (when after_seq is given) and go_live are queued; _inbound_loop sends its ErrorFrames as replies. Inbound frames are still read from connect, so fix(agent-server): read session-socket messages while a step runs #6709's behavior (a message sent during a step is saved even if the client leaves) is unchanged.
  • Regression test: test_an_error_for_a_frame_sent_while_a_step_runs_comes_after_sync.

Issue Number

Fixes #6745

How to Test

uv run pytest -q tests/agent_server/test_session_socket.py
uv run pytest -q tests/agent_server/test_session_socket.py tests/agent_server/test_event_socket_races.py \
  tests/agent_server/test_event_router_websocket.py tests/agent_server/test_agent_server_wsproto.py \
  tests/agent_server/test_websocket_first_message_auth.py tests/agent_server/test_sockets_service_getters.py
  • New test without the fix (session_socket.py reverted): 1 failed, 16 passed; the error frame was already on the wire while subscribe was still blocked (assert [{'type': 'error', ...}] == []).
  • With the fix: 17 passed; the full socket set above: 99 passed. The fix(agent-server): read session-socket messages while a step runs #6709 tests (test_a_message_sent_while_a_step_runs_is_saved_after_the_client_leaves, test_a_frame_that_arrives_with_the_close_is_still_saved) pass.
  • uv run pre-commit run --files on both changed files: all hooks pass.

The map recipe from the issue (control-agent-server map run --file F07 --fresh) needs a model to keep a step running; the DeepSeek key is out of balance, so it was not run.

Video/Screenshots

None; server-side frame ordering change, covered by the unit test above.

Type

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

Notes

Errors are released after the buffered live events flushed by go_live; they still follow sync and the replay, which is what the protocol promises. The legacy /sockets/events endpoint is untouched.


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:27f9c06-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:27f9c06-golang-amd64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-golang-amd64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-golang-amd64
ghcr.io/openhands/agent-server:27f9c06-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:27f9c06-golang-arm64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-golang-arm64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-golang-arm64
ghcr.io/openhands/agent-server:27f9c06-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:27f9c06-java-amd64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-java-amd64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-java-amd64
ghcr.io/openhands/agent-server:27f9c06-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:27f9c06-java-arm64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-java-arm64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-java-arm64
ghcr.io/openhands/agent-server:27f9c06-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:27f9c06-python-amd64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python-amd64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python-amd64
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime-amd64
ghcr.io/openhands/agent-server:27f9c06-python-arm64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python-arm64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python-arm64
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime-arm64
ghcr.io/openhands/agent-server:27f9c06-python-minimal-amd64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python-minimal-amd64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python-minimal-amd64
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:27f9c06-python-minimal-arm64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python-minimal-arm64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python-minimal-arm64
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:27f9c06-python-slim-amd64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python-slim-amd64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python-slim-amd64
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:27f9c06-python-slim-arm64
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python-slim-arm64
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python-slim-arm64
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:27f9c06-golang
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-golang
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-golang
ghcr.io/openhands/agent-server:27f9c06-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:27f9c06-java
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-java
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-java
ghcr.io/openhands/agent-server:27f9c06-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:27f9c06-python-minimal
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python-minimal
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python-minimal
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime-minimal
ghcr.io/openhands/agent-server:27f9c06-python-slim
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python-slim
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python-slim
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime-slim
ghcr.io/openhands/agent-server:27f9c06-python
ghcr.io/openhands/agent-server:27f9c06bd863e5012d7e0a34d8f409ec01b0684e-python
ghcr.io/openhands/agent-server:claude-fix-session-socket-sync-first-python
ghcr.io/openhands/agent-server:27f9c06-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 27f9c06-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., 27f9c06-python-amd64) are also available if needed

Since #6709 the session socket reads client frames before
subscribe_to_events returns, so an ErrorFrame for an invalid frame sent
while a step holds the state lock could reach the client before sync.
The writer now holds reply frames until sync, the replay and the live
flush are queued, then releases them in order. Valid messages are still
read and saved from connect.

Fixes #6745

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013ArwwLxahRVqe2sFw9ukc5
@github-actions

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   session_socket.py3184586%165, 169–170, 174, 176–179, 192–194, 251, 369, 396, 402–403, 406, 408–409, 412, 416, 418–419, 422, 444–447, 450, 466, 469, 477, 483, 500, 559–560, 571, 584, 588–590, 596, 599–600, 608
TOTAL549252090662% 

@enyst enyst mentioned this pull request Nov 11, 2026
1 of 5 tasks

@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).

Scope: this belongs here. The session socket's frame ordering is agent-server behavior this repository owns; no cross-layer change is needed (frame schemas, the OpenAPI contract, and the TypeScript client are untouched, and the fix restores the ordering SyncFrame already documents: sent once, before any replay).

The fix is sound and minimal:

  • hold_replies() runs before _inbound_loop starts, so no error reply can bypass the hold; release_replies() is reached on the only path where the connection stays open (after sync, the after_seq replay, and the go_live flush), and every early-return path closes the writer, so no reply is ever stranded on a live connection.
  • Ordering holds by construction: _queue is drained by exactly one writer task, and both send and release_replies are synchronous, so there is no interleaving window on the event loop.
  • Held replies count against the same MAX_PENDING_BYTES budget (incremented before the held/queued branch and decremented once on drain), so an invalid-frame flood while a step holds the state lock still trips the existing slow_consumer protection instead of growing memory unboundedly — consistent with the writer's documented overflow policy.
  • The #6709 semantics are preserved: inbound frames are still read from connect and _deliver still shields send_message, so a message sent during a step is saved even if the client leaves. The two #6709 regression tests pass.

Evidence:

  • New regression test drives the real session_socket coroutine, real _ConnectionWriter, and real _inbound_loop (only auth and the service getter are patched), and its failure mode mirrors production: LocalConversation.send_message rejects non-user messages with exactly that AssertionError. It asserts no frame is emitted while the lock is held, then sync → durable → error after release. The PR reports it fails on the reverted fix (error already on the wire while subscribe was blocked) and passes with it; I verified the same on this head.
  • uv run pytest -q tests/agent_server/test_session_socket.py tests/agent_server/test_event_socket_races.py tests/agent_server/test_event_router_websocket.py tests/agent_server/test_agent_server_wsproto.py tests/agent_server/test_websocket_first_message_auth.py tests/agent_server/test_sockets_service_getters.py → 99 passed locally on 27f9c06.
  • All 49 GitHub Actions checks pass on this exact head, including agent-server-tests, agent-server-stress-tests, cross-tests, and the REST API / OpenAPI checks.

No material findings. ✅ APPROVED

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]: Session socket can send an error frame before sync while a run holds the state lock

3 participants