Skip to content

fix: dispatch notifications received before the peer ends its stream - #347

Merged
benbrandt merged 2 commits into
agentclientprotocol:mainfrom
nyssance:fix/dispatch-before-end-of-input
Nov 17, 2026
Merged

benbrandt merged 2 commits into
agentclientprotocol:mainfrom
nyssance:fix/dispatch-before-end-of-input

Conversation

@nyssance

@nyssance nyssance commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #346.

When the input ends, receive() closes the connection, and processIncomingMessage() stops dispatching as soon as the connection is aborted. Messages received just before the end, whose dispatch had not reached their handler yet, were dropped, so a peer's last notifications were lost.

With this change, notifications received before the connection closed are still dispatched, unless local code closed it with close():

  • close() does nothing once the connection is closed, as before. The SDK's own paths that close after end of input (runUntil/connectWith, failed writes, onConnect errors) no longer drop the peer's last notifications. Internal failures close the connection without counting as a local close.
  • Requests received before the close are still dropped: no response can reach the peer, and handlers such as fs/write_text_file or terminal/create should not run for a peer that is gone.
  • In-process app pairs (client.connect(agentApp)) pass a peer's close along as if the peer ended its stream, so they behave like stream transports.
  • Errors thrown by notifications dispatched after the close are logged again.
  • The closed and signal docs note that notifications can still reach their handlers after the connection closes.

Tests:

  • src/core/jsonrpc.test.ts ("JSON-RPC end of input"): delivery after end of input and after a read failure, requests dropped, close() after end of input, connectWith, a pending write failing after end of input, error logging, and a local close() still dropping notifications.
  • src/v1/acp.test.ts and src/v2/acp.test.ts: an agent's last notification reaches a client handler behind 20 other handlers, over a stream with connectWith and in process.

npm run check passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RaizxxJBbHnYoG9L4g1u5b

nyssance and others added 2 commits November 17, 2026 22:37
When the input closed the connection, messages already received were
dropped before reaching their handlers. Keep dispatching them; a local
close() still stops dispatch.

Fixes agentclientprotocol#346

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RaizxxJBbHnYoG9L4g1u5b
close() is a no-op again once the connection is closed, so the SDK's own
close() calls after end of input (runUntil, failed writes, onConnect
errors) no longer drop the peer's last notifications. Internal failures
close the connection without counting as a local close, and in-process
app pairs pass a peer's close along as end of input.

Requests received before the close are dropped: no response can reach
the peer. Errors from notifications dispatched after the close are
logged again.
@benbrandt benbrandt changed the title fix: dispatch messages received before the peer ends its stream fix: dispatch notifications received before the peer ends its stream Nov 17, 2026
@benbrandt
benbrandt enabled auto-merge (squash) November 17, 2026 15:16
@benbrandt
benbrandt merged commit 833a115 into agentclientprotocol:main Nov 17, 2026
2 checks passed
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.

Messages read before the stream ends are dropped instead of dispatched

2 participants