Skip to content

fix(acp): return session/new errors from start_session - #428

Open
OldKrab wants to merge 1 commit into
agentclientprotocol:mainfrom
OldKrab:shushakov/start-session-returns-request-error
Open

OldKrab wants to merge 1 commit into
agentclientprotocol:mainfrom
OldKrab:shushakov/start-session-returns-request-error

Conversation

@OldKrab

@OldKrab OldKrab commented Oct 18, 2026

Copy link
Copy Markdown

Problem

SessionBuilder::start_session (protocol v1) sends session/new from a task spawned with ConnectionTo::spawn. When the agent answers with an error, for example auth_required, that task returns Err, and a failed spawned task shuts down the whole connection. The caller never receives the error: start_session().await resolves only as the connection ends, and connect_with ends with the agent's error.

The v1 restore builders (load_session / resume_session) and the protocol v2 session builder already return setup errors to the caller and keep the connection running. Only the v1 new-session path did not.

Change

start_session now follows start_restored_session. It sends session/new with send_ordered_request_to and attaches the session inside block_task_with_ordered_result.

  • Error response: returned to the caller. The session's dynamic handler registrations are dropped, so its runners (for example MCP servers) stop once their in-flight cleanup finishes. The connection keeps running.
  • Success: session routing is installed before later inbound messages are dispatched. on_session_start already guarantees this.
  • Dropping a pending start: standard SentRequest drop-time cancellation plus handler cleanup. Before, a late response made the connection fail.

Tests

  • session_ordering::start_session_returns_session_new_error_without_closing_connection: rejects one session/new, then starts another session on the same connection. Before the fix, the connection ended with the agent's -32602.
  • session_mcp_scope::start_session_failure_and_cancellation_keep_runner_until_cleanup: the v1 counterpart of the existing v2 setup-cleanup test, covering both rejection and cancellation with an attached MCP server. It fails before the fix.
  • session_ordering::start_session_installs_routing_before_later_batch_entry: an update in the same transport batch as the response reaches the returned session.
  • just test, cargo clippy --workspace --all-targets --all-features -- -D warnings, cargo fmt --check.

`SessionBuilder::start_session` sent `session/new` from a task spawned on
the connection. An error response made that task fail, which shuts down
the whole connection, so the caller never received the error.

Send the request as an ordered request and attach the session in its
ordered result, as the restore builders do. An error response now reaches
the caller, the session's dynamic handlers are dropped so its runners stop
after their cleanup, and the connection keeps running. Dropping a pending
start cancels the request instead of failing the connection when the late
response arrives.

This branch has not been deployed

No deployments
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.

1 participant