agent_client_protocol/jsonrpc.rs
1//! Core JSON-RPC server support.
2
3use agent_client_protocol_schema::v1::{
4 JsonRpcMessage as VersionedJsonRpcMessage, Notification as RpcNotification,
5 Request as RpcRequest, RequestId, SessionId,
6};
7
8// Types re-exported from crate root
9use serde::ser::SerializeSeq as _;
10use serde::{Deserialize, Serialize};
11use std::any::TypeId;
12use std::collections::HashMap;
13use std::fmt::Debug;
14use std::marker::PhantomData;
15use std::panic::Location;
16use std::pin::pin;
17use std::sync::{
18 Arc, Mutex, Weak,
19 atomic::{AtomicBool, Ordering},
20};
21use uuid::Uuid;
22
23use futures::FutureExt;
24use futures::channel::{mpsc, oneshot};
25use futures::future::{self, BoxFuture, Either};
26use futures::{AsyncRead, AsyncWrite, StreamExt};
27
28pub(crate) mod close;
29mod dynamic_handler;
30pub(crate) mod handlers;
31mod incoming_actor;
32mod outgoing_actor;
33#[cfg(test)]
34mod prepared_request_tests;
35mod protocol_compat;
36mod raw_error;
37pub(crate) mod run;
38mod task_actor;
39mod transport_actor;
40
41use crate::jsonrpc::close::{ChainedClose, CloseCallback};
42pub use crate::jsonrpc::close::{HandleConnectionClose, NullClose};
43use crate::jsonrpc::dynamic_handler::DynamicHandlerMessage;
44pub use crate::jsonrpc::handlers::NullHandler;
45use crate::jsonrpc::handlers::{ChainedHandler, NamedHandler};
46use crate::jsonrpc::handlers::{MessageHandler, NotificationHandler, RequestHandler};
47use crate::jsonrpc::outgoing_actor::{OutgoingMessageTx, send_raw_message};
48use crate::jsonrpc::protocol_compat::{ProtocolCompat, ProtocolMode};
49pub use crate::jsonrpc::raw_error::{RawJsonRpcError, RawJsonRpcResponse};
50use crate::jsonrpc::run::SpawnedRun;
51use crate::jsonrpc::run::{ChainRun, NullRun, RunWithConnectionTo};
52use crate::jsonrpc::task_actor::{Task, TaskTx};
53#[cfg(feature = "unstable_mcp_over_acp")]
54use crate::mcp_server::McpServer;
55use crate::role::HasPeer;
56use crate::role::Role;
57use crate::{Agent, Client, ConnectTo, Proxy, RoleId};
58
59/// One valid JSON-RPC message carried inside a [`TransportFrame`].
60///
61/// This uses the JSON-RPC envelope types from `agent-client-protocol-schema`
62/// while keeping method params and response errors protocol-neutral at the
63/// transport boundary.
64#[derive(Debug, Clone)]
65pub enum RawJsonRpcMessage {
66 /// A JSON-RPC request with an id and expected response.
67 Request(RpcRequest<RawJsonRpcParams>),
68 /// A JSON-RPC notification without a response.
69 Notification(RpcNotification<RawJsonRpcParams>),
70 /// A JSON-RPC response to a prior request.
71 Response(RawJsonRpcResponse),
72}
73
74/// A JSON-RPC frame exchanged between protocol components and transports.
75///
76/// A frame preserves the boundary between a single JSON-RPC value and a batch.
77/// Malformed wire input is represented explicitly; transport failures are
78/// reported by the future that drives the transport rather than sent through a
79/// [`Channel`].
80#[derive(Clone, Debug)]
81pub enum TransportFrame {
82 /// One valid JSON-RPC message.
83 Single(RawJsonRpcMessage),
84 /// One malformed or invalid wire value retained for relays.
85 Malformed {
86 /// The original wire representation.
87 raw: String,
88 /// The JSON-RPC error associated with the malformed value.
89 error: crate::Error,
90 },
91 /// Entries retained from one non-empty JSON-RPC batch, kept in source order.
92 Batch(TransportBatch),
93}
94
95/// A structurally non-empty JSON-RPC batch retained across framed relays.
96#[derive(Clone, Debug)]
97pub struct TransportBatch {
98 first: TransportBatchEntry,
99 rest: Vec<TransportBatchEntry>,
100}
101
102/// One entry in a [`TransportBatch`].
103#[derive(Clone, Debug)]
104pub enum TransportBatchEntry {
105 /// A valid JSON-RPC message.
106 Message(RawJsonRpcMessage),
107 /// A malformed or invalid JSON-RPC value retained for relays.
108 Malformed {
109 /// The original JSON value.
110 raw: serde_json::Value,
111 /// The JSON-RPC error associated with the malformed value.
112 error: crate::Error,
113 },
114}
115
116pub(crate) fn is_response_only_shape(value: &serde_json::Value) -> bool {
117 value.as_object().is_some_and(|object| {
118 !object.contains_key("method")
119 && (object.contains_key("result") || object.contains_key("error"))
120 })
121}
122
123pub(crate) fn raw_is_response_only_shape(raw: &str) -> bool {
124 serde_json::from_str(raw).is_ok_and(|value| is_response_only_shape(&value))
125}
126
127impl TransportBatchEntry {
128 /// Create a valid batch entry.
129 #[must_use]
130 pub fn message(message: RawJsonRpcMessage) -> Self {
131 Self::Message(message)
132 }
133
134 /// Create a malformed batch entry.
135 #[must_use]
136 pub fn malformed(raw: serde_json::Value, error: crate::Error) -> Self {
137 Self::Malformed { raw, error }
138 }
139
140 #[cfg(test)]
141 fn as_result(&self) -> Result<&RawJsonRpcMessage, &crate::Error> {
142 match self {
143 Self::Message(message) => Ok(message),
144 Self::Malformed { error, .. } => Err(error),
145 }
146 }
147
148 fn message_ref(&self) -> Option<&RawJsonRpcMessage> {
149 match self {
150 Self::Message(message) => Some(message),
151 Self::Malformed { .. } => None,
152 }
153 }
154}
155
156impl Serialize for TransportBatchEntry {
157 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
158 where
159 S: serde::Serializer,
160 {
161 match self {
162 Self::Message(message) => message.serialize(serializer),
163 Self::Malformed { raw, .. } => raw.serialize(serializer),
164 }
165 }
166}
167
168impl TransportBatch {
169 /// Create a non-empty batch from entries.
170 ///
171 /// Returns `None` when the iterator is empty.
172 pub fn from_entries(entries: impl IntoIterator<Item = TransportBatchEntry>) -> Option<Self> {
173 let mut entries = entries.into_iter();
174 Some(Self {
175 first: entries.next()?,
176 rest: entries.collect(),
177 })
178 }
179
180 /// Create a non-empty batch from valid messages.
181 ///
182 /// Returns `None` when the iterator is empty.
183 pub fn from_messages(messages: impl IntoIterator<Item = RawJsonRpcMessage>) -> Option<Self> {
184 Self::from_entries(messages.into_iter().map(TransportBatchEntry::message))
185 }
186
187 /// Iterate over entries in source order.
188 pub fn entries(&self) -> impl Iterator<Item = &TransportBatchEntry> {
189 std::iter::once(&self.first).chain(&self.rest)
190 }
191
192 /// Iterate mutably over entries in source order.
193 pub fn entries_mut(&mut self) -> impl Iterator<Item = &mut TransportBatchEntry> {
194 std::iter::once(&mut self.first).chain(&mut self.rest)
195 }
196
197 /// Consume this batch and iterate over its entries in source order.
198 pub fn into_entries(self) -> impl Iterator<Item = TransportBatchEntry> {
199 std::iter::once(self.first).chain(self.rest)
200 }
201
202 /// Return the number of entries in this non-empty batch.
203 #[must_use]
204 pub fn len(&self) -> usize {
205 1 + self.rest.len()
206 }
207
208 /// Return whether this batch is empty.
209 ///
210 /// A `TransportBatch` is structurally non-empty, so this always returns
211 /// `false`.
212 #[must_use]
213 pub const fn is_empty(&self) -> bool {
214 false
215 }
216
217 #[cfg(test)]
218 pub(crate) fn iter_results(
219 &self,
220 ) -> impl Iterator<Item = Result<&RawJsonRpcMessage, &crate::Error>> {
221 self.entries().map(TransportBatchEntry::as_result)
222 }
223
224 fn messages(&self) -> impl Iterator<Item = &RawJsonRpcMessage> {
225 self.entries().filter_map(TransportBatchEntry::message_ref)
226 }
227}
228
229impl Serialize for TransportBatch {
230 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
231 where
232 S: serde::Serializer,
233 {
234 let mut sequence = serializer.serialize_seq(Some(1 + self.rest.len()))?;
235 sequence.serialize_element(&self.first)?;
236 for entry in &self.rest {
237 sequence.serialize_element(entry)?;
238 }
239 sequence.end()
240 }
241}
242
243impl TransportFrame {
244 fn inspect_messages(
245 &self,
246 observer: &mut impl FnMut(&RawJsonRpcMessage) -> Result<(), crate::Error>,
247 ) -> Result<(), crate::Error> {
248 match self {
249 Self::Single(message) => observer(message),
250 Self::Malformed { .. } => Ok(()),
251 Self::Batch(batch) => {
252 for message in batch.messages() {
253 observer(message)?;
254 }
255 Ok(())
256 }
257 }
258 }
259}
260
261/// Raw JSON-RPC request or notification parameters.
262///
263/// JSON-RPC params, when present, must be either an array or an object.
264#[derive(Debug, Clone, PartialEq)]
265pub enum RawJsonRpcParams {
266 /// Positional JSON-RPC params.
267 Array(Vec<serde_json::Value>),
268 /// Named JSON-RPC params.
269 Object(serde_json::Map<String, serde_json::Value>),
270}
271
272impl RawJsonRpcParams {
273 /// Convert a JSON value into JSON-RPC params.
274 pub fn from_value(value: serde_json::Value) -> Result<Option<Self>, crate::Error> {
275 match value {
276 serde_json::Value::Null => Ok(None),
277 serde_json::Value::Array(array) => Ok(Some(Self::Array(array))),
278 serde_json::Value::Object(object) => Ok(Some(Self::Object(object))),
279 _ => {
280 Err(crate::Error::invalid_params()
281 .data("JSON-RPC params must be an object or array"))
282 }
283 }
284 }
285
286 /// Convert params back into a JSON value.
287 #[must_use]
288 pub fn into_value(self) -> serde_json::Value {
289 match self {
290 Self::Array(array) => serde_json::Value::Array(array),
291 Self::Object(object) => serde_json::Value::Object(object),
292 }
293 }
294}
295
296impl Serialize for RawJsonRpcParams {
297 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
298 where
299 S: serde::Serializer,
300 {
301 match self {
302 Self::Array(array) => array.serialize(serializer),
303 Self::Object(object) => object.serialize(serializer),
304 }
305 }
306}
307
308impl<'de> Deserialize<'de> for RawJsonRpcParams {
309 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
310 where
311 D: serde::Deserializer<'de>,
312 {
313 let value = serde_json::Value::deserialize(deserializer)?;
314 match value {
315 serde_json::Value::Array(array) => Ok(Self::Array(array)),
316 serde_json::Value::Object(object) => Ok(Self::Object(object)),
317 _ => Err(serde::de::Error::custom(
318 "JSON-RPC params must be an object or array",
319 )),
320 }
321 }
322}
323
324impl RawJsonRpcMessage {
325 /// Build a raw JSON-RPC request message.
326 pub fn request(
327 method: String,
328 params: serde_json::Value,
329 id: RequestId,
330 ) -> Result<Self, crate::Error> {
331 Ok(Self::Request(RpcRequest {
332 id,
333 method: Arc::from(method),
334 params: RawJsonRpcParams::from_value(params)?,
335 }))
336 }
337
338 /// Build a raw JSON-RPC notification message.
339 pub fn notification(method: String, params: serde_json::Value) -> Result<Self, crate::Error> {
340 Ok(Self::Notification(RpcNotification {
341 method: Arc::from(method),
342 params: RawJsonRpcParams::from_value(params)?,
343 }))
344 }
345
346 /// Build a JSON-RPC response from an ACP result.
347 ///
348 /// For other protocols, construct [`RawJsonRpcResponse`] directly so error
349 /// codes and fields are not interpreted as ACP.
350 #[must_use]
351 pub fn response(id: RequestId, response: Result<serde_json::Value, crate::Error>) -> Self {
352 Self::Response(RawJsonRpcResponse::new(
353 id,
354 response.map_err(|error| Box::new(error.into())),
355 ))
356 }
357
358 /// The response id, if this is a response.
359 #[must_use]
360 pub fn response_id(&self) -> Option<&RequestId> {
361 match self {
362 Self::Response(
363 RawJsonRpcResponse::Result { id, .. } | RawJsonRpcResponse::Error { id, .. },
364 ) => Some(id),
365 Self::Request(_) | Self::Notification(_) => None,
366 }
367 }
368}
369
370impl Serialize for RawJsonRpcMessage {
371 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
372 where
373 S: serde::Serializer,
374 {
375 match self {
376 Self::Request(request) => {
377 VersionedJsonRpcMessage::wrap(request.clone()).serialize(serializer)
378 }
379 Self::Notification(notification) => {
380 VersionedJsonRpcMessage::wrap(notification.clone()).serialize(serializer)
381 }
382 Self::Response(response) => {
383 VersionedJsonRpcMessage::wrap(response.clone()).serialize(serializer)
384 }
385 }
386 }
387}
388
389impl<'de> Deserialize<'de> for RawJsonRpcMessage {
390 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
391 where
392 D: serde::Deserializer<'de>,
393 {
394 let value = serde_json::Value::deserialize(deserializer)?;
395 let Some(object) = value.as_object() else {
396 return Err(serde::de::Error::custom("invalid JSON-RPC message"));
397 };
398
399 let has_method = object.contains_key("method");
400 let has_id = object.contains_key("id");
401 let has_result = object.contains_key("result");
402 let has_error = object.contains_key("error");
403
404 if has_method && !has_result && !has_error {
405 if has_id {
406 let request = serde_json::from_value::<
407 VersionedJsonRpcMessage<RpcRequest<RawJsonRpcParams>>,
408 >(value)
409 .map_err(serde::de::Error::custom)?
410 .into_inner();
411 Ok(Self::Request(request))
412 } else {
413 let notification = serde_json::from_value::<
414 VersionedJsonRpcMessage<RpcNotification<RawJsonRpcParams>>,
415 >(value)
416 .map_err(serde::de::Error::custom)?
417 .into_inner();
418 Ok(Self::Notification(notification))
419 }
420 } else if !has_method && has_id && has_result != has_error {
421 let response =
422 serde_json::from_value::<VersionedJsonRpcMessage<RawJsonRpcResponse>>(value)
423 .map_err(serde::de::Error::custom)?
424 .into_inner();
425 Ok(Self::Response(response))
426 } else {
427 Err(serde::de::Error::custom("invalid JSON-RPC message"))
428 }
429 }
430}
431
432fn params_from_transport(params: Option<RawJsonRpcParams>) -> serde_json::Value {
433 params.map_or(serde_json::Value::Null, RawJsonRpcParams::into_value)
434}
435
436/// Handlers process incoming JSON-RPC messages on a connection.
437///
438/// When messages arrive, they flow through a chain of handlers. Each handler can
439/// either **claim** the message (handle it) or **decline** it (pass to the next handler).
440///
441/// # Message Flow
442///
443/// Messages flow through three layers of handlers in order:
444///
445/// ```text
446/// ┌─────────────────────────────────────────────────────────────────┐
447/// │ Incoming Message │
448/// └─────────────────────────────────────────────────────────────────┘
449/// │
450/// ▼
451/// ┌─────────────────────────────────────────────────────────────────┐
452/// │ 1. User Handlers (registered via on_receive_request, etc.) │
453/// │ - Tried in registration order │
454/// │ - First handler to return Handled::Yes claims the message │
455/// └─────────────────────────────────────────────────────────────────┘
456/// │ Handled::No
457/// ▼
458/// ┌─────────────────────────────────────────────────────────────────┐
459/// │ 2. Dynamic Handlers (added at runtime) │
460/// │ - Used for session-specific message handling │
461/// │ - Added via ConnectionTo::add_dynamic_handler │
462/// └─────────────────────────────────────────────────────────────────┘
463/// │ Handled::No
464/// ▼
465/// ┌─────────────────────────────────────────────────────────────────┐
466/// │ 3. Role Default Handler │
467/// │ - Fallback based on the connection's Role │
468/// │ - Handles protocol-level messages (e.g., proxy forwarding) │
469/// └─────────────────────────────────────────────────────────────────┘
470/// │ Handled::No
471/// ▼
472/// ┌─────────────────────────────────────────────────────────────────┐
473/// │ Unhandled: requests error, notifications ignored │
474/// └─────────────────────────────────────────────────────────────────┘
475/// ```
476///
477/// # The `Handled` Return Value
478///
479/// Each handler returns [`Handled`] to indicate whether it processed the message:
480///
481/// - **`Handled::Yes`** - Message was handled. No further handlers are invoked.
482/// - **`Handled::No { message, retry }`** - Message was not handled. The message
483/// (possibly modified) is passed to the next handler in the chain.
484///
485/// For convenience, handlers can return `()` which is equivalent to `Handled::Yes`.
486///
487/// # The Retry Mechanism
488///
489/// The `retry` flag in `Handled::No` controls what happens when no handler claims a message:
490///
491/// - **`retry: false`** (default) - Send a "method not found" error
492/// response immediately for requests, or ignore notifications.
493/// - **`retry: true`** - Queue the message and retry it when new dynamic handlers are added.
494///
495/// This mechanism exists because of a timing issue with sessions: when a `session/new`
496/// response is being processed, the dynamic handler for that session hasn't been registered
497/// yet, but `session/update` notifications for that session may already be arriving.
498/// By setting `retry: true`, these early notifications are queued until the session's
499/// dynamic handler is added.
500///
501/// # Handler Registration
502///
503/// Most users register handlers using the builder methods on [`Builder`]:
504///
505/// ```
506/// # use agent_client_protocol::{Agent, Client, ConnectTo};
507/// # use agent_client_protocol::schema::v1::{AgentCapabilities, InitializeRequest, InitializeResponse};
508/// # use agent_client_protocol_test::StatusUpdate;
509/// # async fn example(transport: impl ConnectTo<Agent>) -> Result<(), agent_client_protocol::Error> {
510/// Agent.builder()
511/// .on_receive_request(async |req: InitializeRequest, responder, cx| {
512/// responder.respond(
513/// InitializeResponse::new(req.protocol_version)
514/// .agent_capabilities(AgentCapabilities::new()),
515/// )
516/// }, agent_client_protocol::on_receive_request!())
517/// .on_receive_notification(async |notif: StatusUpdate, cx| {
518/// // Process notification
519/// Ok(())
520/// }, agent_client_protocol::on_receive_notification!())
521/// .connect_to(transport)
522/// .await?;
523/// # Ok(())
524/// # }
525/// ```
526///
527/// The type parameter on the closure determines which messages are dispatched to it.
528/// Messages that don't match the type are automatically passed to the next handler.
529///
530/// # Implementing Custom Handlers
531///
532/// For advanced use cases, you can implement [`HandleDispatchFrom`] directly:
533///
534/// ```no_run
535/// use agent_client_protocol::{
536/// Client, ConnectionTo, Dispatch, Error, HandleDispatchFrom, Handled,
537/// };
538///
539/// struct MyHandler;
540///
541/// impl HandleDispatchFrom<Client> for MyHandler {
542/// async fn handle_dispatch_from(
543/// &mut self,
544/// message: Dispatch,
545/// _connection: ConnectionTo<Client>,
546/// ) -> Result<Handled<Dispatch>, Error> {
547/// if message.method() == "my/custom/method" {
548/// // Handle it
549/// Ok(Handled::Yes)
550/// } else {
551/// // Pass to next handler
552/// Ok(Handled::No { message, retry: false })
553/// }
554/// }
555///
556/// fn describe_chain(&self) -> impl std::fmt::Debug {
557/// "MyHandler"
558/// }
559/// }
560/// ```
561///
562/// # Important: Handlers Must Not Block
563///
564/// The connection processes messages on a single async task. While a handler is running,
565/// no other messages can be processed. For expensive operations, use [`ConnectionTo::spawn`]
566/// to run work concurrently:
567///
568/// ```
569/// # use agent_client_protocol::{Client, Agent, ConnectTo};
570/// # use agent_client_protocol_test::{expensive_operation, ProcessComplete};
571/// # async fn example(transport: impl ConnectTo<Client>) -> Result<(), agent_client_protocol::Error> {
572/// # Client.builder().connect_with(transport, async |cx| {
573/// cx.spawn({
574/// let connection = cx.clone();
575/// async move {
576/// let result = expensive_operation("data").await?;
577/// connection.send_notification(ProcessComplete { result })?;
578/// Ok(())
579/// }
580/// })?;
581/// # Ok(())
582/// # }).await?;
583/// # Ok(())
584/// # }
585/// ```
586#[allow(async_fn_in_trait)]
587/// A handler for incoming JSON-RPC messages.
588///
589/// This trait is implemented by types that can process incoming messages on a connection.
590/// Handlers are registered with a [`Builder`] and are called in order until
591/// one claims the message.
592///
593/// The type parameter is the counterpart role that messages arrive from and
594/// that the supplied [`ConnectionTo`] addresses. An agent handler therefore
595/// implements `HandleDispatchFrom<Client>`, while a client handler implements
596/// `HandleDispatchFrom<Agent>`.
597pub trait HandleDispatchFrom<Counterpart: Role>: Send {
598 /// Attempt to claim an incoming dispatch (request, notification, or response).
599 ///
600 /// # Important: do not block
601 ///
602 /// The server will not process new messages until this handler returns.
603 /// You should avoid blocking in this callback unless you wish to block the server (e.g., for rate limiting).
604 /// The recommended approach to manage expensive operations is to the [`ConnectionTo::spawn`] method available on the message context.
605 ///
606 /// # Parameters
607 ///
608 /// * `message` - The incoming message to handle.
609 /// * `connection` - The connection, used to send messages and access connection state.
610 ///
611 /// # Returns
612 ///
613 /// * `Ok(Handled::Yes)` if the message was claimed. It will not be propagated further.
614 /// * `Ok(Handled::No(message))` if not; the (possibly changed) message will be passed to the remaining handlers.
615 /// * `Err` if processing fails. Requests receive an Error Response, response
616 /// errors are routed to the local request awaiter, and notification errors
617 /// are logged without a wire reply.
618 fn handle_dispatch_from(
619 &mut self,
620 message: Dispatch,
621 connection: ConnectionTo<Counterpart>,
622 ) -> impl Future<Output = Result<Handled<Dispatch>, crate::Error>> + Send;
623
624 /// Returns a debug description of the registered handlers for diagnostics.
625 fn describe_chain(&self) -> impl std::fmt::Debug;
626}
627
628impl<Counterpart: Role, H> HandleDispatchFrom<Counterpart> for &mut H
629where
630 H: HandleDispatchFrom<Counterpart>,
631{
632 fn handle_dispatch_from(
633 &mut self,
634 message: Dispatch,
635 cx: ConnectionTo<Counterpart>,
636 ) -> impl Future<Output = Result<Handled<Dispatch>, crate::Error>> + Send {
637 H::handle_dispatch_from(self, message, cx)
638 }
639
640 fn describe_chain(&self) -> impl std::fmt::Debug {
641 H::describe_chain(self)
642 }
643}
644
645/// Selects the connection context exposed by a [`Builder`]'s callbacks.
646///
647/// This trait is an implementation detail of the typed builder aliases. It is
648/// public so the callback connection type remains expressible in public API
649/// signatures.
650#[doc(hidden)]
651#[allow(private_bounds)]
652pub trait ConnectionContext: connection_context::Sealed + Send + Sync + 'static {
653 /// The connection type exposed to callbacks for `Counterpart`.
654 type Connection<Counterpart: Role>: Clone + Send + Sync + 'static;
655}
656
657mod connection_context {
658 use super::{ConnectionContext, ConnectionTo, Role};
659
660 pub trait Sealed {
661 fn from_raw<Counterpart: Role>(
662 connection: ConnectionTo<Counterpart>,
663 ) -> <Self as ConnectionContext>::Connection<Counterpart>
664 where
665 Self: ConnectionContext;
666 }
667
668 pub(crate) fn from_raw<Context: ConnectionContext, Counterpart: Role>(
669 connection: ConnectionTo<Counterpart>,
670 ) -> Context::Connection<Counterpart> {
671 <Context as Sealed>::from_raw(connection)
672 }
673}
674
675/// The default callback context used by stable and low-level builders.
676#[doc(hidden)]
677#[derive(Copy, Clone, Debug, Default)]
678pub struct RawConnectionContext;
679
680impl connection_context::Sealed for RawConnectionContext {
681 fn from_raw<Counterpart: Role>(
682 connection: ConnectionTo<Counterpart>,
683 ) -> <Self as ConnectionContext>::Connection<Counterpart> {
684 connection
685 }
686}
687
688impl ConnectionContext for RawConnectionContext {
689 type Connection<Counterpart: Role> = ConnectionTo<Counterpart>;
690}
691
692/// The callback context used by ACP protocol v2 builders.
693#[cfg(feature = "unstable_protocol_v2")]
694#[doc(hidden)]
695#[derive(Copy, Clone, Debug, Default)]
696pub struct V2ConnectionContext;
697
698#[cfg(feature = "unstable_protocol_v2")]
699impl connection_context::Sealed for V2ConnectionContext {
700 fn from_raw<Counterpart: Role>(
701 connection: ConnectionTo<Counterpart>,
702 ) -> <Self as ConnectionContext>::Connection<Counterpart> {
703 V2ConnectionTo { inner: connection }
704 }
705}
706
707#[cfg(feature = "unstable_protocol_v2")]
708impl ConnectionContext for V2ConnectionContext {
709 type Connection<Counterpart: Role> = V2ConnectionTo<Counterpart>;
710}
711
712/// A JSON-RPC connection builder whose callbacks receive [`V2ConnectionTo`].
713#[cfg(feature = "unstable_protocol_v2")]
714pub type V2Builder<Host, Handler = NullHandler, Runner = NullRun, Close = NullClose> =
715 Builder<Host, Handler, Runner, Close, V2ConnectionContext>;
716
717/// A JSON-RPC connection that can act as either a server, client, or both.
718///
719/// [`Builder`] provides a builder-style API for creating JSON-RPC servers and clients.
720/// You start by calling `Role.builder()` (e.g., `Client.builder()`), then add message
721/// handlers, and finally drive the connection with either [`connect_to`](Builder::connect_to)
722/// or [`connect_with`](Builder::connect_with), providing a component implementation
723/// (e.g., [`ByteStreams`] for byte streams).
724///
725/// # JSON-RPC Primer
726///
727/// JSON-RPC 2.0 has two fundamental message types:
728///
729/// * **Requests** - Messages that expect a response. They have an `id` field that gets
730/// echoed back in the response so the sender can correlate them.
731/// * **Notifications** - Fire-and-forget messages with no `id` field. The sender doesn't
732/// expect or receive a response.
733///
734/// # Type-Driven Message Dispatch
735///
736/// The handler registration methods use Rust's type system to determine which messages
737/// to handle. The type parameter you provide controls what gets dispatched to your handler:
738///
739/// ## Single Message Types
740///
741/// The simplest case - handle one specific message type:
742///
743/// ```no_run
744/// # use agent_client_protocol_test::*;
745/// # use agent_client_protocol::schema::v1::{InitializeRequest, InitializeResponse, SessionNotification};
746/// # async fn example() -> Result<(), agent_client_protocol::Error> {
747/// # let connection = mock_connection();
748/// connection
749/// .on_receive_request(async |req: InitializeRequest, responder, cx| {
750/// // Handle only InitializeRequest messages
751/// responder.respond(InitializeResponse::make())
752/// }, agent_client_protocol::on_receive_request!())
753/// .on_receive_notification(async |notif: SessionNotification, cx| {
754/// // Handle only SessionUpdate notifications
755/// Ok(())
756/// }, agent_client_protocol::on_receive_notification!())
757/// # .connect_to(agent_client_protocol_test::MockTransport).await?;
758/// # Ok(())
759/// # }
760/// ```
761///
762/// ## Enum Message Types
763///
764/// You can also handle multiple related messages with a single handler by defining an enum
765/// that implements the appropriate trait ([`JsonRpcRequest`] or [`JsonRpcNotification`]):
766///
767/// ```no_run
768/// # use agent_client_protocol_test::*;
769/// # use agent_client_protocol::{JsonRpcRequest, JsonRpcMessage, UntypedMessage};
770/// # use agent_client_protocol::schema::v1::{InitializeRequest, InitializeResponse, PromptRequest, PromptResponse};
771/// # async fn example() -> Result<(), agent_client_protocol::Error> {
772/// # let connection = mock_connection();
773/// // Define an enum for multiple request types
774/// #[derive(Debug, Clone)]
775/// enum MyRequests {
776/// Initialize(InitializeRequest),
777/// Prompt(PromptRequest),
778/// }
779///
780/// // Implement JsonRpcRequest for your enum
781/// # impl JsonRpcMessage for MyRequests {
782/// # fn matches_method(_method: &str) -> bool { false }
783/// # fn method(&self) -> &str { "myRequests" }
784/// # fn to_untyped_message(&self) -> Result<UntypedMessage, agent_client_protocol::Error> { todo!() }
785/// # fn parse_message(_method: &str, _params: &impl serde::Serialize) -> Result<Self, agent_client_protocol::Error> { Err(agent_client_protocol::Error::method_not_found()) }
786/// # }
787/// impl JsonRpcRequest for MyRequests { type Response = serde_json::Value; }
788///
789/// // Handle all variants in one place
790/// connection.on_receive_request(async |req: MyRequests, responder, cx| {
791/// match req {
792/// MyRequests::Initialize(init) => { responder.respond(serde_json::json!({})) }
793/// MyRequests::Prompt(prompt) => { responder.respond(serde_json::json!({})) }
794/// }
795/// }, agent_client_protocol::on_receive_request!())
796/// # .connect_to(agent_client_protocol_test::MockTransport).await?;
797/// # Ok(())
798/// # }
799/// ```
800///
801/// ## Mixed Message Types
802///
803/// To handle requests, notifications, and responses in one callback, use
804/// [`on_receive_dispatch`](Self::on_receive_dispatch):
805///
806/// ```no_run
807/// # use agent_client_protocol_test::*;
808/// # use agent_client_protocol::Dispatch;
809/// # use agent_client_protocol::schema::v1::{InitializeRequest, InitializeResponse, SessionNotification};
810/// # async fn example() -> Result<(), agent_client_protocol::Error> {
811/// # let connection = mock_connection();
812/// // on_receive_dispatch receives requests, notifications, and responses
813/// connection.on_receive_dispatch(async |msg: Dispatch<InitializeRequest, SessionNotification>, _cx| {
814/// match msg {
815/// Dispatch::Request(req, responder) => {
816/// responder.respond(InitializeResponse::make())
817/// }
818/// Dispatch::Notification(notif) => {
819/// Ok(())
820/// }
821/// Dispatch::Response(result, router) => {
822/// // Forward response to its destination
823/// router.route_with_result(result)
824/// }
825/// }
826/// }, agent_client_protocol::on_receive_dispatch!())
827/// # .connect_to(agent_client_protocol_test::MockTransport).await?;
828/// # Ok(())
829/// # }
830/// ```
831///
832/// # Handler Registration
833///
834/// Register handlers using these methods (listed from most common to most flexible):
835///
836/// * [`on_receive_request`](Self::on_receive_request) - Handle JSON-RPC requests (messages expecting responses)
837/// * [`on_receive_notification`](Self::on_receive_notification) - Handle JSON-RPC notifications (fire-and-forget)
838/// * [`on_receive_dispatch`](Self::on_receive_dispatch) - Handle requests, notifications, and responses in one callback
839/// * [`with_handler`](Self::with_handler) - Low-level primitive for maximum flexibility
840///
841/// ## Handler Ordering
842///
843/// Handlers are tried in the order you register them. The first handler that claims a message
844/// (by matching its type) will process it. Subsequent handlers won't see that message:
845///
846/// ```no_run
847/// # use agent_client_protocol_test::*;
848/// # use agent_client_protocol::schema::v1::{InitializeRequest, InitializeResponse, PromptRequest, PromptResponse};
849/// # async fn example() -> Result<(), agent_client_protocol::Error> {
850/// # let connection = mock_connection();
851/// connection
852/// .on_receive_request(async |req: InitializeRequest, responder, cx| {
853/// // This runs first for InitializeRequest
854/// responder.respond(InitializeResponse::make())
855/// }, agent_client_protocol::on_receive_request!())
856/// .on_receive_request(async |req: PromptRequest, responder, cx| {
857/// // This runs first for PromptRequest
858/// responder.respond(PromptResponse::make())
859/// }, agent_client_protocol::on_receive_request!())
860/// // Unknown requests receive Method not found automatically; unhandled
861/// // notifications are ignored.
862/// # .connect_to(agent_client_protocol_test::MockTransport).await?;
863/// # Ok(())
864/// # }
865/// ```
866///
867/// # Event Loop and Concurrency
868///
869/// Understanding the event loop is critical for writing correct handlers.
870///
871/// ## The Event Loop
872///
873/// [`Builder`] runs all handler callbacks on a single async task - the event loop.
874/// While a handler is running, **the server cannot receive new messages**. This means
875/// any blocking or expensive work in your handlers will stall the entire connection.
876///
877/// To avoid blocking the event loop, use [`ConnectionTo::spawn`] to offload serious
878/// work to concurrent tasks:
879///
880/// ```no_run
881/// # use agent_client_protocol_test::*;
882/// # async fn example() -> Result<(), agent_client_protocol::Error> {
883/// # let connection = mock_connection();
884/// connection.on_receive_request(async |req: AnalyzeRequest, responder, cx| {
885/// // Clone cx for the spawned task
886/// cx.spawn({
887/// let connection = cx.clone();
888/// async move {
889/// let result = expensive_analysis(&req.data).await?;
890/// connection.send_notification(AnalysisComplete { result })?;
891/// Ok(())
892/// }
893/// })?;
894///
895/// // Respond immediately without blocking
896/// responder.respond(AnalysisStarted { job_id: 42 })
897/// }, agent_client_protocol::on_receive_request!())
898/// # .connect_to(agent_client_protocol_test::MockTransport).await?;
899/// # Ok(())
900/// # }
901/// ```
902///
903/// Note that the entire connection runs within one async task, so parallelism must be
904/// managed explicitly using [`spawn`](ConnectionTo::spawn).
905///
906/// ## The Connection Context
907///
908/// Handler callbacks receive a context object (`cx`) for interacting with the connection:
909///
910/// * **For request handlers** - [`Responder<R>`] provides [`respond`](Responder::respond)
911/// to send the response, plus methods to send other messages
912/// * **For notification handlers** - [`ConnectionTo`] provides methods to send messages
913/// and spawn tasks
914///
915/// Both context types support:
916/// * [`send_request`](ConnectionTo::send_request) - Send requests to the other side
917/// * [`send_notification`](ConnectionTo::send_notification) - Send notifications
918/// * [`spawn`](ConnectionTo::spawn) - Run tasks concurrently without blocking the event loop
919///
920/// The [`SentRequest`] returned by `send_request` provides methods like
921/// [`on_receiving_result`](SentRequest::on_receiving_result) that help you
922/// avoid accidentally blocking the event loop while waiting for responses.
923///
924/// # Driving the Connection
925///
926/// After adding handlers, you must drive the connection using one of two modes:
927///
928/// ## Server Mode: `connect_to()`
929///
930/// Use [`connect_to`](Self::connect_to) when you only need to respond to incoming messages:
931///
932/// ```no_run
933/// # use agent_client_protocol_test::*;
934/// # async fn example() -> Result<(), agent_client_protocol::Error> {
935/// # let connection = mock_connection();
936/// connection
937/// .on_receive_request(async |req: MyRequest, responder, cx| {
938/// responder.respond(MyResponse { status: "ok".into() })
939/// }, agent_client_protocol::on_receive_request!())
940/// .connect_to(MockTransport) // Runs until connection closes or error occurs
941/// .await?;
942/// # Ok(())
943/// # }
944/// ```
945///
946/// The connection will process incoming messages and invoke your handlers until the
947/// connection is closed or an error occurs.
948///
949/// ## Client Mode: `connect_with()`
950///
951/// Use [`connect_with`](Self::connect_with) when you need to both handle incoming messages
952/// AND send your own requests/notifications:
953///
954/// ```no_run
955/// # use agent_client_protocol_test::*;
956/// # use agent_client_protocol::schema::v1::InitializeRequest;
957/// # async fn example() -> Result<(), agent_client_protocol::Error> {
958/// # let connection = mock_connection();
959/// connection
960/// .on_receive_request(async |req: MyRequest, responder, cx| {
961/// responder.respond(MyResponse { status: "ok".into() })
962/// }, agent_client_protocol::on_receive_request!())
963/// .connect_with(MockTransport, async |cx| {
964/// // You can send requests to the other side
965/// let response = cx.send_request(InitializeRequest::make())
966/// .block_task()
967/// .await?;
968///
969/// // And send notifications
970/// cx.send_notification(StatusUpdate { message: "ready".into() })?;
971///
972/// Ok(())
973/// })
974/// .await?;
975/// # Ok(())
976/// # }
977/// ```
978///
979/// The connection will serve incoming messages in the background while your client closure
980/// runs. When the closure returns, the connection shuts down.
981///
982/// # Example: Complete Agent
983///
984/// ```no_run
985/// # use agent_client_protocol::UntypedRole;
986/// # use agent_client_protocol::{Builder};
987/// # use agent_client_protocol::schema::v1::{InitializeRequest, InitializeResponse, PromptRequest, PromptResponse, SessionNotification};
988/// # async fn example(transport: impl agent_client_protocol::ConnectTo<UntypedRole>) -> Result<(), agent_client_protocol::Error> {
989///
990/// UntypedRole.builder()
991/// .name("my-agent") // Optional: for debugging logs
992/// .on_receive_request(async |init: InitializeRequest, responder, cx| {
993/// let response: InitializeResponse = todo!();
994/// responder.respond(response)
995/// }, agent_client_protocol::on_receive_request!())
996/// .on_receive_request(async |prompt: PromptRequest, responder, cx| {
997/// // You can send notifications while processing a request
998/// let notif: SessionNotification = todo!();
999/// cx.send_notification(notif)?;
1000///
1001/// // Then respond to the request
1002/// let response: PromptResponse = todo!();
1003/// responder.respond(response)
1004/// }, agent_client_protocol::on_receive_request!())
1005/// .connect_to(transport)
1006/// .await?;
1007/// # Ok(())
1008/// # }
1009/// ```
1010#[must_use]
1011#[derive(Debug)]
1012pub struct Builder<
1013 Host: Role,
1014 Handler = NullHandler,
1015 Runner = NullRun,
1016 Close = NullClose,
1017 Context = RawConnectionContext,
1018> where
1019 Handler: HandleDispatchFrom<Host::Counterpart>,
1020 Runner: RunWithConnectionTo<Host::Counterpart>,
1021 Close: HandleConnectionClose<Host::Counterpart>,
1022 Context: ConnectionContext,
1023{
1024 /// My role.
1025 host: Host,
1026
1027 /// Name of the connection, used in tracing logs.
1028 name: Option<String>,
1029
1030 /// Handler for incoming messages.
1031 handler: Handler,
1032
1033 /// Runner for background connection tasks.
1034 runner: Runner,
1035
1036 /// Protocol version mode for the public API and wire compatibility layer.
1037 protocol_mode: ProtocolMode,
1038
1039 /// Handler run when the incoming transport reaches clean EOF.
1040 on_close: Close,
1041
1042 /// Selects the connection type exposed to user callbacks.
1043 context: PhantomData<fn() -> Context>,
1044}
1045
1046fn default_protocol_mode<Host: Role>() -> ProtocolMode {
1047 let role = TypeId::of::<Host>();
1048
1049 if role == TypeId::of::<Agent>() {
1050 ProtocolMode::v1_agent()
1051 } else if role == TypeId::of::<Client>() {
1052 ProtocolMode::v1_client()
1053 } else if role == TypeId::of::<Proxy>() {
1054 ProtocolMode::v1_proxy()
1055 } else {
1056 ProtocolMode::disabled()
1057 }
1058}
1059
1060impl<Host: Role> Builder<Host, NullHandler, NullRun, NullClose> {
1061 /// Create a new connection builder for the given role.
1062 /// This type follows a builder pattern; use other methods to configure and then invoke
1063 /// [`Self::connect_to`] (to use as a server) or [`Self::connect_with`] to use as a client.
1064 pub fn new(role: Host) -> Self {
1065 Self {
1066 host: role,
1067 name: None,
1068 handler: NullHandler,
1069 runner: NullRun,
1070 protocol_mode: default_protocol_mode::<Host>(),
1071 on_close: NullClose,
1072 context: PhantomData,
1073 }
1074 }
1075}
1076
1077impl<Host: Role, Handler> Builder<Host, Handler, NullRun, NullClose>
1078where
1079 Handler: HandleDispatchFrom<Host::Counterpart>,
1080{
1081 /// Create a new connection builder with the given handler.
1082 pub fn new_with(role: Host, handler: Handler) -> Self {
1083 Self {
1084 host: role,
1085 name: None,
1086 handler,
1087 runner: NullRun,
1088 protocol_mode: default_protocol_mode::<Host>(),
1089 on_close: NullClose,
1090 context: PhantomData,
1091 }
1092 }
1093}
1094
1095#[cfg(feature = "unstable_protocol_v2")]
1096impl<
1097 Host: Role,
1098 Handler: HandleDispatchFrom<Host::Counterpart>,
1099 Runner: RunWithConnectionTo<Host::Counterpart>,
1100 Close: HandleConnectionClose<Host::Counterpart>,
1101> Builder<Host, Handler, Runner, Close>
1102{
1103 pub(crate) fn v2_agent(self) -> V2Builder<Host, Handler, Runner, Close> {
1104 Builder {
1105 host: self.host,
1106 name: self.name,
1107 handler: self.handler,
1108 runner: self.runner,
1109 protocol_mode: ProtocolMode::v2_agent(),
1110 on_close: self.on_close,
1111 context: PhantomData,
1112 }
1113 }
1114
1115 pub(crate) fn v2_client(self) -> V2Builder<Host, Handler, Runner, Close> {
1116 Builder {
1117 host: self.host,
1118 name: self.name,
1119 handler: self.handler,
1120 runner: self.runner,
1121 protocol_mode: ProtocolMode::v2_client(),
1122 on_close: self.on_close,
1123 context: PhantomData,
1124 }
1125 }
1126
1127 pub(crate) fn v2_proxy(self) -> V2Builder<Host, Handler, Runner, Close> {
1128 Builder {
1129 host: self.host,
1130 name: self.name,
1131 handler: self.handler,
1132 runner: self.runner,
1133 protocol_mode: ProtocolMode::v2_proxy(),
1134 on_close: self.on_close,
1135 context: PhantomData,
1136 }
1137 }
1138
1139 /// Disable all automatic ACP protocol-version tracking and validation.
1140 ///
1141 /// This is a low-level escape hatch for protocol-routing infrastructure
1142 /// that inspects and validates raw initialize requests and responses
1143 /// itself before selecting a version-specific implementation. It also
1144 /// disables the version checks applied to messages after initialization.
1145 ///
1146 /// This method is deliberately available only on builders whose callbacks
1147 /// receive raw [`ConnectionTo`] values. Applications should normally use
1148 /// [`Client::builder`](crate::Client::builder),
1149 /// [`Agent::builder`](crate::Agent::builder),
1150 /// [`Proxy::builder`](crate::Proxy::builder), [`Client::v2`](crate::Client::v2),
1151 /// [`Agent::v2`](crate::Agent::v2), or [`Proxy::v2`](crate::Proxy::v2)
1152 /// instead.
1153 ///
1154 /// ```compile_fail
1155 /// # use agent_client_protocol::Client;
1156 /// let _ = Client.v2().without_acp_version_guard();
1157 /// ```
1158 pub fn without_acp_version_guard(mut self) -> Self {
1159 self.protocol_mode = ProtocolMode::disabled();
1160 self
1161 }
1162}
1163
1164#[cfg(feature = "unstable_protocol_v2")]
1165impl<
1166 Handler: HandleDispatchFrom<Agent>,
1167 Runner: RunWithConnectionTo<Agent>,
1168 Close: HandleConnectionClose<Agent>,
1169> Builder<Client, Handler, Runner, Close>
1170{
1171 /// Apply protocol-v2 wire validation while retaining raw callback contexts.
1172 ///
1173 /// This is intended for protocol-routing infrastructure that has already
1174 /// selected v2 but still needs protocol-neutral [`ConnectionTo`] values in
1175 /// its callbacks. The guarded child must still send and receive the
1176 /// `initialize` round trip; a router that consumes initialization itself
1177 /// must use [`Builder::without_acp_version_guard`] for the selected child.
1178 /// Most clients should use [`Client::v2`](crate::Client::v2), which also
1179 /// exposes the version-typed [`V2ConnectionTo`] API.
1180 pub fn with_v2_protocol_guard(mut self) -> Self {
1181 self.protocol_mode = ProtocolMode::v2_client();
1182 self
1183 }
1184}
1185
1186#[cfg(feature = "unstable_protocol_v2")]
1187impl<
1188 Handler: HandleDispatchFrom<Client>,
1189 Runner: RunWithConnectionTo<Client>,
1190 Close: HandleConnectionClose<Client>,
1191> Builder<Agent, Handler, Runner, Close>
1192{
1193 /// Apply protocol-v2 wire validation while retaining raw callback contexts.
1194 ///
1195 /// This is intended for protocol-routing infrastructure that has already
1196 /// selected v2 but still needs protocol-neutral [`ConnectionTo`] values in
1197 /// its callbacks. The guarded child must still receive and answer the
1198 /// `initialize` request; a router that consumes initialization itself must
1199 /// use [`Builder::without_acp_version_guard`] for the selected child. Most
1200 /// agents should use [`Agent::v2`](crate::Agent::v2), which also exposes the
1201 /// version-typed [`V2ConnectionTo`] API.
1202 pub fn with_v2_protocol_guard(mut self) -> Self {
1203 self.protocol_mode = ProtocolMode::v2_agent();
1204 self
1205 }
1206}
1207
1208impl<
1209 Host: Role,
1210 Handler: HandleDispatchFrom<Host::Counterpart>,
1211 Runner: RunWithConnectionTo<Host::Counterpart>,
1212 Close: HandleConnectionClose<Host::Counterpart>,
1213 Context: ConnectionContext,
1214> Builder<Host, Handler, Runner, Close, Context>
1215{
1216 /// Set the "name" of this connection -- used only for debugging logs.
1217 pub fn name(mut self, name: impl ToString) -> Self {
1218 self.name = Some(name.to_string());
1219 self
1220 }
1221
1222 pub(crate) fn v1_agent(mut self) -> Self {
1223 self.protocol_mode = ProtocolMode::v1_agent();
1224 self
1225 }
1226
1227 pub(crate) fn v1_client(mut self) -> Self {
1228 self.protocol_mode = ProtocolMode::v1_client();
1229 self
1230 }
1231
1232 /// Merge another [`Builder`] into this one.
1233 ///
1234 /// Prefer [`Self::on_receive_request`] or [`Self::on_receive_notification`].
1235 /// This is a low-level method that is not intended for general use.
1236 pub fn with_connection_builder(
1237 self,
1238 other: Builder<
1239 Host,
1240 impl HandleDispatchFrom<Host::Counterpart>,
1241 impl RunWithConnectionTo<Host::Counterpart>,
1242 impl HandleConnectionClose<Host::Counterpart>,
1243 Context,
1244 >,
1245 ) -> Builder<
1246 Host,
1247 impl HandleDispatchFrom<Host::Counterpart>,
1248 impl RunWithConnectionTo<Host::Counterpart>,
1249 impl HandleConnectionClose<Host::Counterpart>,
1250 Context,
1251 > {
1252 let Builder {
1253 name: other_name,
1254 handler: other_handler,
1255 runner: other_runner,
1256 protocol_mode: other_protocol_mode,
1257 on_close: other_on_close,
1258 context: _,
1259 host: _,
1260 } = other;
1261 Builder {
1262 host: self.host,
1263 name: self.name,
1264 handler: ChainedHandler::new(
1265 self.handler,
1266 NamedHandler::new(other_name, other_handler),
1267 ),
1268 runner: ChainRun::new(self.runner, other_runner),
1269 protocol_mode: self.protocol_mode.merge(other_protocol_mode),
1270 on_close: ChainedClose::new(self.on_close, other_on_close),
1271 context: PhantomData,
1272 }
1273 }
1274
1275 /// Add a new [`HandleDispatchFrom`] to the chain.
1276 ///
1277 /// Prefer [`Self::on_receive_request`] or [`Self::on_receive_notification`].
1278 /// This is a low-level method that is not intended for general use.
1279 pub fn with_handler(
1280 self,
1281 handler: impl HandleDispatchFrom<Host::Counterpart>,
1282 ) -> Builder<Host, impl HandleDispatchFrom<Host::Counterpart>, Runner, Close, Context> {
1283 Builder {
1284 host: self.host,
1285 name: self.name,
1286 handler: ChainedHandler::new(self.handler, handler),
1287 runner: self.runner,
1288 protocol_mode: self.protocol_mode,
1289 on_close: self.on_close,
1290 context: PhantomData,
1291 }
1292 }
1293
1294 /// Add a new [`RunWithConnectionTo`] to the chain.
1295 pub fn with_runner<Run1>(
1296 self,
1297 runner: Run1,
1298 ) -> Builder<Host, Handler, impl RunWithConnectionTo<Host::Counterpart>, Close, Context>
1299 where
1300 Run1: RunWithConnectionTo<Host::Counterpart>,
1301 {
1302 Builder {
1303 host: self.host,
1304 name: self.name,
1305 handler: self.handler,
1306 runner: ChainRun::new(self.runner, runner),
1307 protocol_mode: self.protocol_mode,
1308 on_close: self.on_close,
1309 context: PhantomData,
1310 }
1311 }
1312
1313 /// Enqueue a task to run once the connection is actively serving traffic.
1314 #[track_caller]
1315 pub fn with_spawned<F, Fut>(
1316 self,
1317 task: F,
1318 ) -> Builder<Host, Handler, impl RunWithConnectionTo<Host::Counterpart>, Close, Context>
1319 where
1320 F: FnOnce(Context::Connection<Host::Counterpart>) -> Fut + Send,
1321 Fut: Future<Output = Result<(), crate::Error>> + Send,
1322 {
1323 let location = Location::caller();
1324 self.with_runner(SpawnedRun::<_, Context>::new(location, task))
1325 }
1326
1327 /// Run a callback when the incoming transport reaches clean EOF.
1328 ///
1329 /// Each callback runs at most once and receives the connection context. A
1330 /// successful callback observes the close without otherwise changing the
1331 /// lifetime of [`connect_with`](Self::connect_with). Returning an error
1332 /// shuts down the connection and cancels a still-running `connect_with`
1333 /// future.
1334 ///
1335 /// Multiple callbacks run sequentially in registration order. All of them
1336 /// run even if an earlier callback fails, after which the first error is
1337 /// returned. Pending requests are failed before callbacks begin, while the
1338 /// selected connection context's `incoming_closed` future completes only
1339 /// after they finish. A callback must therefore not await that close
1340 /// future itself.
1341 ///
1342 /// This separation lets applications choose their cancellation policy. A
1343 /// callback can notify application-owned tasks and return `Ok(())` for
1344 /// graceful cleanup, or return an error to stop them immediately.
1345 ///
1346 /// ```
1347 /// # use agent_client_protocol::{Client, ConnectTo, Error};
1348 /// # async fn example(transport: impl ConnectTo<Client>) -> Result<(), Error> {
1349 /// Client.builder()
1350 /// .on_close(async |_cx| {
1351 /// Err(Error::internal_error().data("agent transport closed"))
1352 /// })
1353 /// .connect_with(transport, async |_cx| {
1354 /// std::future::pending().await
1355 /// })
1356 /// .await?;
1357 /// # Ok(())
1358 /// # }
1359 /// ```
1360 pub fn on_close<F, Fut>(
1361 self,
1362 callback: F,
1363 ) -> Builder<Host, Handler, Runner, impl HandleConnectionClose<Host::Counterpart>, Context>
1364 where
1365 F: FnOnce(Context::Connection<Host::Counterpart>) -> Fut + Send,
1366 Fut: Future<Output = Result<(), crate::Error>> + Send,
1367 {
1368 Builder {
1369 host: self.host,
1370 name: self.name,
1371 handler: self.handler,
1372 runner: self.runner,
1373 protocol_mode: self.protocol_mode,
1374 on_close: ChainedClose::new(self.on_close, CloseCallback::<_, Context>::new(callback)),
1375 context: PhantomData,
1376 }
1377 }
1378
1379 /// Register a handler for requests, notifications, and responses.
1380 ///
1381 /// Use this when you want to handle all JSON-RPC message kinds in one callback.
1382 /// Your handler receives a [`Dispatch<Req, Notif>`] with three variants:
1383 ///
1384 /// - `Dispatch::Request(request, responder)` - A request with its response context
1385 /// - `Dispatch::Notification(notification)` - A notification
1386 /// - `Dispatch::Response(result, router)` - A response to a request we sent
1387 ///
1388 /// # Example
1389 ///
1390 /// ```no_run
1391 /// # use agent_client_protocol_test::*;
1392 /// # use agent_client_protocol::Dispatch;
1393 /// # async fn example() -> Result<(), agent_client_protocol::Error> {
1394 /// # let connection = mock_connection();
1395 /// connection.on_receive_dispatch(async |message: Dispatch<MyRequest, StatusUpdate>, _cx| {
1396 /// match message {
1397 /// Dispatch::Request(req, responder) => {
1398 /// // Handle request and send response
1399 /// responder.respond(MyResponse { status: "ok".into() })
1400 /// }
1401 /// Dispatch::Notification(notif) => {
1402 /// // Handle notification (no response needed)
1403 /// Ok(())
1404 /// }
1405 /// Dispatch::Response(result, router) => {
1406 /// // Forward response to its destination
1407 /// router.route_with_result(result)
1408 /// }
1409 /// }
1410 /// }, agent_client_protocol::on_receive_dispatch!())
1411 /// # .connect_to(agent_client_protocol_test::MockTransport).await?;
1412 /// # Ok(())
1413 /// # }
1414 /// ```
1415 ///
1416 /// For most use cases, prefer [`on_receive_request`](Self::on_receive_request) or
1417 /// [`on_receive_notification`](Self::on_receive_notification) which provide cleaner APIs
1418 /// for handling requests or notifications separately.
1419 ///
1420 /// # Ordering
1421 ///
1422 /// This callback runs inside the dispatch loop and blocks further message processing
1423 /// until it completes. See the [`ordering`](crate::concepts::ordering) module for details on
1424 /// ordering guarantees and how to avoid deadlocks.
1425 pub fn on_receive_dispatch<Req, Notif, F, T, ToFut>(
1426 self,
1427 op: F,
1428 to_future_hack: ToFut,
1429 ) -> Builder<Host, impl HandleDispatchFrom<Host::Counterpart>, Runner, Close, Context>
1430 where
1431 Host::Counterpart: HasPeer<Host::Counterpart>,
1432 Req: JsonRpcRequest,
1433 Notif: JsonRpcNotification,
1434 F: AsyncFnMut(
1435 Dispatch<Req, Notif>,
1436 Context::Connection<Host::Counterpart>,
1437 ) -> Result<T, crate::Error>
1438 + Send,
1439 T: IntoHandled<Dispatch<Req, Notif>>,
1440 ToFut: Fn(
1441 &mut F,
1442 Dispatch<Req, Notif>,
1443 Context::Connection<Host::Counterpart>,
1444 ) -> crate::BoxFuture<'_, Result<T, crate::Error>>
1445 + Send
1446 + Sync,
1447 {
1448 let handler = MessageHandler::<_, _, _, _, _, _, Context>::new(
1449 self.host.counterpart(),
1450 self.host.counterpart(),
1451 op,
1452 to_future_hack,
1453 );
1454 self.with_handler(handler)
1455 }
1456
1457 /// Register a handler for JSON-RPC requests of type `Req`.
1458 ///
1459 /// Your handler receives three arguments:
1460 /// 1. The request (type `Req`)
1461 /// 2. A [`Responder<Req::Response>`] for sending the response
1462 /// 3. The builder-selected connection context for the peer that sent the
1463 /// request (`ConnectionTo` by default, or `V2ConnectionTo` for a
1464 /// `V2Builder`)
1465 ///
1466 /// The request context allows you to:
1467 /// - Send the response with [`Responder::respond`]
1468 /// - Send notifications to the client with the context's
1469 /// `send_notification` method
1470 /// - Send requests to the client with the context's `send_request` method
1471 ///
1472 /// # Example
1473 ///
1474 /// ```no_run
1475 /// # use agent_client_protocol::{Agent, ConnectTo};
1476 /// # use agent_client_protocol::schema::v1::{PromptRequest, PromptResponse, SessionNotification};
1477 /// # async fn example(transport: impl ConnectTo<Agent>) -> Result<(), agent_client_protocol::Error> {
1478 /// Agent.builder().on_receive_request(async |request: PromptRequest, responder, cx| {
1479 /// // Send a notification while processing
1480 /// let notif: SessionNotification = todo!();
1481 /// cx.send_notification(notif)?;
1482 ///
1483 /// // Send the response
1484 /// let response: PromptResponse = todo!();
1485 /// responder.respond(response)
1486 /// }, agent_client_protocol::on_receive_request!())
1487 /// .connect_to(transport)
1488 /// .await
1489 /// # }
1490 /// ```
1491 ///
1492 /// # Type Parameter
1493 ///
1494 /// `Req` can be either a single request type or an enum of multiple request types.
1495 /// See the [type-driven dispatch](Self#type-driven-message-dispatch) section for details.
1496 ///
1497 /// # Ordering
1498 ///
1499 /// This callback runs inside the dispatch loop and blocks further message processing
1500 /// until it completes. See the [`ordering`](crate::concepts::ordering) module for details on
1501 /// ordering guarantees and how to avoid deadlocks.
1502 pub fn on_receive_request<Req: JsonRpcRequest, F, T, ToFut>(
1503 self,
1504 op: F,
1505 to_future_hack: ToFut,
1506 ) -> Builder<Host, impl HandleDispatchFrom<Host::Counterpart>, Runner, Close, Context>
1507 where
1508 Host::Counterpart: HasPeer<Host::Counterpart>,
1509 F: AsyncFnMut(
1510 Req,
1511 Responder<Req::Response>,
1512 Context::Connection<Host::Counterpart>,
1513 ) -> Result<T, crate::Error>
1514 + Send,
1515 T: IntoHandled<(Req, Responder<Req::Response>)>,
1516 ToFut: Fn(
1517 &mut F,
1518 Req,
1519 Responder<Req::Response>,
1520 Context::Connection<Host::Counterpart>,
1521 ) -> crate::BoxFuture<'_, Result<T, crate::Error>>
1522 + Send
1523 + Sync,
1524 {
1525 let handler = RequestHandler::<_, _, _, _, _, Context>::new(
1526 self.host.counterpart(),
1527 self.host.counterpart(),
1528 op,
1529 to_future_hack,
1530 );
1531 self.with_handler(handler)
1532 }
1533
1534 /// Register a handler for JSON-RPC notifications of type `Notif`.
1535 ///
1536 /// Notifications are fire-and-forget messages that don't expect a response.
1537 /// Your handler receives:
1538 /// 1. The notification (type `Notif`)
1539 /// 2. The builder-selected connection context for sending messages to the
1540 /// other side
1541 ///
1542 /// Unlike request handlers, you cannot send a response (notifications don't have IDs),
1543 /// but you can still send your own requests and notifications using the context.
1544 ///
1545 /// # Example
1546 ///
1547 /// ```no_run
1548 /// # use agent_client_protocol_test::*;
1549 /// # async fn example() -> Result<(), agent_client_protocol::Error> {
1550 /// # let connection = mock_connection();
1551 /// connection.on_receive_notification(async |notif: SessionUpdate, cx| {
1552 /// // Process the notification
1553 /// update_session_state(¬if)?;
1554 ///
1555 /// // Optionally send a notification back
1556 /// cx.send_notification(StatusUpdate {
1557 /// message: "Acknowledged".into(),
1558 /// })?;
1559 ///
1560 /// Ok(())
1561 /// }, agent_client_protocol::on_receive_notification!())
1562 /// # .connect_to(agent_client_protocol_test::MockTransport).await?;
1563 /// # Ok(())
1564 /// # }
1565 /// ```
1566 ///
1567 /// # Type Parameter
1568 ///
1569 /// `Notif` can be either a single notification type or an enum of multiple notification types.
1570 /// See the [type-driven dispatch](Self#type-driven-message-dispatch) section for details.
1571 ///
1572 /// # Ordering
1573 ///
1574 /// This callback runs inside the dispatch loop and blocks further message processing
1575 /// until it completes. See the [`ordering`](crate::concepts::ordering) module for details on
1576 /// ordering guarantees and how to avoid deadlocks.
1577 pub fn on_receive_notification<Notif, F, T, ToFut>(
1578 self,
1579 op: F,
1580 to_future_hack: ToFut,
1581 ) -> Builder<Host, impl HandleDispatchFrom<Host::Counterpart>, Runner, Close, Context>
1582 where
1583 Host::Counterpart: HasPeer<Host::Counterpart>,
1584 Notif: JsonRpcNotification,
1585 F: AsyncFnMut(Notif, Context::Connection<Host::Counterpart>) -> Result<T, crate::Error>
1586 + Send,
1587 T: IntoHandled<(Notif, Context::Connection<Host::Counterpart>)>,
1588 ToFut: Fn(
1589 &mut F,
1590 Notif,
1591 Context::Connection<Host::Counterpart>,
1592 ) -> crate::BoxFuture<'_, Result<T, crate::Error>>
1593 + Send
1594 + Sync,
1595 {
1596 let handler = NotificationHandler::<_, _, _, _, _, Context>::new(
1597 self.host.counterpart(),
1598 self.host.counterpart(),
1599 op,
1600 to_future_hack,
1601 );
1602 self.with_handler(handler)
1603 }
1604
1605 /// Register a handler for messages from a specific peer.
1606 ///
1607 /// This is similar to [`on_receive_dispatch`](Self::on_receive_dispatch), but allows
1608 /// specifying the source peer explicitly. This is useful when receiving messages
1609 /// from a peer that requires message transformation (e.g., unwrapping `SuccessorMessage`
1610 /// envelopes when receiving from an agent via a proxy).
1611 ///
1612 /// For the common case of receiving from the default counterpart, use
1613 /// [`on_receive_dispatch`](Self::on_receive_dispatch) instead.
1614 ///
1615 /// # Ordering
1616 ///
1617 /// This callback runs inside the dispatch loop and blocks further message processing
1618 /// until it completes. See the [`ordering`](crate::concepts::ordering) module for details on
1619 /// ordering guarantees and how to avoid deadlocks.
1620 pub fn on_receive_dispatch_from<
1621 Req: JsonRpcRequest,
1622 Notif: JsonRpcNotification,
1623 Peer: Role,
1624 F,
1625 T,
1626 ToFut,
1627 >(
1628 self,
1629 peer: Peer,
1630 op: F,
1631 to_future_hack: ToFut,
1632 ) -> Builder<Host, impl HandleDispatchFrom<Host::Counterpart>, Runner, Close, Context>
1633 where
1634 Host::Counterpart: HasPeer<Peer>,
1635 F: AsyncFnMut(
1636 Dispatch<Req, Notif>,
1637 Context::Connection<Host::Counterpart>,
1638 ) -> Result<T, crate::Error>
1639 + Send,
1640 T: IntoHandled<Dispatch<Req, Notif>>,
1641 ToFut: Fn(
1642 &mut F,
1643 Dispatch<Req, Notif>,
1644 Context::Connection<Host::Counterpart>,
1645 ) -> crate::BoxFuture<'_, Result<T, crate::Error>>
1646 + Send
1647 + Sync,
1648 {
1649 let handler = MessageHandler::<_, _, _, _, _, _, Context>::new(
1650 self.host.counterpart(),
1651 peer,
1652 op,
1653 to_future_hack,
1654 );
1655 self.with_handler(handler)
1656 }
1657
1658 /// Register a handler for JSON-RPC requests from a specific peer.
1659 ///
1660 /// This is similar to [`on_receive_request`](Self::on_receive_request), but allows
1661 /// specifying the source peer explicitly. This is useful when receiving messages
1662 /// from a peer that requires message transformation (e.g., unwrapping `SuccessorRequest`
1663 /// envelopes when receiving from an agent via a proxy).
1664 ///
1665 /// For the common case of receiving from the default counterpart, use
1666 /// [`on_receive_request`](Self::on_receive_request) instead.
1667 ///
1668 /// # Example
1669 ///
1670 /// ```ignore
1671 /// use agent_client_protocol::Agent;
1672 /// use agent_client_protocol::schema::v1::InitializeRequest;
1673 ///
1674 /// // Conductor receiving from agent direction - messages will be unwrapped from SuccessorMessage
1675 /// connection.on_receive_request_from(Agent, async |req: InitializeRequest, responder, cx| {
1676 /// // Handle the request
1677 /// responder.respond(InitializeResponse::make())
1678 /// })
1679 /// ```
1680 ///
1681 /// # Ordering
1682 ///
1683 /// This callback runs inside the dispatch loop and blocks further message processing
1684 /// until it completes. See the [`ordering`](crate::concepts::ordering) module for details on
1685 /// ordering guarantees and how to avoid deadlocks.
1686 pub fn on_receive_request_from<Req: JsonRpcRequest, Peer: Role, F, T, ToFut>(
1687 self,
1688 peer: Peer,
1689 op: F,
1690 to_future_hack: ToFut,
1691 ) -> Builder<Host, impl HandleDispatchFrom<Host::Counterpart>, Runner, Close, Context>
1692 where
1693 Host::Counterpart: HasPeer<Peer>,
1694 F: AsyncFnMut(
1695 Req,
1696 Responder<Req::Response>,
1697 Context::Connection<Host::Counterpart>,
1698 ) -> Result<T, crate::Error>
1699 + Send,
1700 T: IntoHandled<(Req, Responder<Req::Response>)>,
1701 ToFut: Fn(
1702 &mut F,
1703 Req,
1704 Responder<Req::Response>,
1705 Context::Connection<Host::Counterpart>,
1706 ) -> crate::BoxFuture<'_, Result<T, crate::Error>>
1707 + Send
1708 + Sync,
1709 {
1710 let handler = RequestHandler::<_, _, _, _, _, Context>::new(
1711 self.host.counterpart(),
1712 peer,
1713 op,
1714 to_future_hack,
1715 );
1716 self.with_handler(handler)
1717 }
1718
1719 /// Register a handler for JSON-RPC notifications from a specific peer.
1720 ///
1721 /// This is similar to [`on_receive_notification`](Self::on_receive_notification), but allows
1722 /// specifying the source peer explicitly. This is useful when receiving messages
1723 /// from a peer that requires message transformation (e.g., unwrapping `SuccessorNotification`
1724 /// envelopes when receiving from an agent via a proxy).
1725 ///
1726 /// For the common case of receiving from the default counterpart, use
1727 /// [`on_receive_notification`](Self::on_receive_notification) instead.
1728 ///
1729 /// # Ordering
1730 ///
1731 /// This callback runs inside the dispatch loop and blocks further message processing
1732 /// until it completes. See the [`ordering`](crate::concepts::ordering) module for details on
1733 /// ordering guarantees and how to avoid deadlocks.
1734 pub fn on_receive_notification_from<Notif: JsonRpcNotification, Peer: Role, F, T, ToFut>(
1735 self,
1736 peer: Peer,
1737 op: F,
1738 to_future_hack: ToFut,
1739 ) -> Builder<Host, impl HandleDispatchFrom<Host::Counterpart>, Runner, Close, Context>
1740 where
1741 Host::Counterpart: HasPeer<Peer>,
1742 F: AsyncFnMut(Notif, Context::Connection<Host::Counterpart>) -> Result<T, crate::Error>
1743 + Send,
1744 T: IntoHandled<(Notif, Context::Connection<Host::Counterpart>)>,
1745 ToFut: Fn(
1746 &mut F,
1747 Notif,
1748 Context::Connection<Host::Counterpart>,
1749 ) -> crate::BoxFuture<'_, Result<T, crate::Error>>
1750 + Send
1751 + Sync,
1752 {
1753 let handler = NotificationHandler::<_, _, _, _, _, Context>::new(
1754 self.host.counterpart(),
1755 peer,
1756 op,
1757 to_future_hack,
1758 );
1759 self.with_handler(handler)
1760 }
1761
1762 /// Run in server mode with the provided transport.
1763 ///
1764 /// This drives the connection by continuously processing messages from the transport
1765 /// and dispatching them to your registered handlers. The connection will run until:
1766 /// - The transport closes (e.g., EOF on byte streams)
1767 /// - An error occurs
1768 ///
1769 /// Handler errors are normally contained: requests receive an Error Response,
1770 /// response-handler errors are routed to the pending local request, and
1771 /// notification errors are logged without a wire reply.
1772 ///
1773 /// On clean EOF, messages already accepted by the outgoing queue—including
1774 /// handler responses and close-callback notifications—are drained through
1775 /// the transport sink before this returns `Ok(())`.
1776 ///
1777 /// The transport boundary carries [`TransportFrame`] values. Physical stream adapters
1778 /// serialize and deserialize frames, while channel-based components relay them directly.
1779 ///
1780 /// Use this mode when you only need to respond to incoming messages and don't need
1781 /// to initiate your own requests. If you need to send requests to the other side,
1782 /// use [`connect_with`](Self::connect_with) instead.
1783 ///
1784 /// # Example: Byte Stream Transport
1785 ///
1786 /// ```no_run
1787 /// # use agent_client_protocol::UntypedRole;
1788 /// # use agent_client_protocol::{Builder};
1789 /// # use agent_client_protocol_test::*;
1790 /// # async fn example(transport: impl agent_client_protocol::ConnectTo<UntypedRole>) -> Result<(), agent_client_protocol::Error> {
1791 ///
1792 /// UntypedRole.builder()
1793 /// .on_receive_request(async |req: MyRequest, responder, cx| {
1794 /// responder.respond(MyResponse { status: "ok".into() })
1795 /// }, agent_client_protocol::on_receive_request!())
1796 /// .connect_to(transport)
1797 /// .await?;
1798 /// # Ok(())
1799 /// # }
1800 /// ```
1801 pub async fn connect_to(
1802 self,
1803 transport: impl ConnectTo<Host> + 'static,
1804 ) -> Result<(), crate::Error> {
1805 let (_, future) = self.into_connection_and_future(transport, true, async move |cx| {
1806 cx.incoming_closed().await;
1807 Ok(())
1808 });
1809 future.await
1810 }
1811
1812 /// Run the connection until the provided closure completes.
1813 ///
1814 /// This drives the connection by:
1815 /// 1. Running your registered handlers in the background to process incoming messages
1816 /// 2. Executing your `main_fn` closure with the builder-selected connection
1817 /// context for sending requests and notifications
1818 ///
1819 /// The connection stays active until your `main_fn` returns, then shuts down.
1820 /// Clean incoming EOF fails every pending request and makes future
1821 /// requests fail immediately. It does not cancel unrelated work in
1822 /// `main_fn`: that future may observe the context's `incoming_closed`
1823 /// future, or the builder can use [`on_close`](Self::on_close) to notify it
1824 /// or return an error and stop it.
1825 ///
1826 /// Use this mode when you need to initiate communication (send requests/notifications)
1827 /// in addition to responding to incoming messages. For server-only mode where you just
1828 /// respond to messages, use [`connect_to`](Self::connect_to) instead.
1829 ///
1830 /// # Example
1831 ///
1832 /// ```no_run
1833 /// # use agent_client_protocol::UntypedRole;
1834 /// # use agent_client_protocol::{Builder};
1835 /// # use agent_client_protocol::ByteStreams;
1836 /// # use agent_client_protocol::schema::v1::InitializeRequest;
1837 /// # use agent_client_protocol_test::*;
1838 /// # async fn example(transport: impl agent_client_protocol::ConnectTo<UntypedRole>) -> Result<(), agent_client_protocol::Error> {
1839 ///
1840 /// UntypedRole.builder()
1841 /// .on_receive_request(async |req: MyRequest, responder, cx| {
1842 /// // Handle incoming requests in the background
1843 /// responder.respond(MyResponse { status: "ok".into() })
1844 /// }, agent_client_protocol::on_receive_request!())
1845 /// .connect_with(transport, async |cx| {
1846 /// // Initialize the protocol
1847 /// let init_response = cx.send_request(InitializeRequest::make())
1848 /// .block_task()
1849 /// .await?;
1850 ///
1851 /// // Send more requests...
1852 /// let result = cx.send_request(MyRequest {})
1853 /// .block_task()
1854 /// .await?;
1855 ///
1856 /// // When this closure returns, the connection shuts down
1857 /// Ok(())
1858 /// })
1859 /// .await?;
1860 /// # Ok(())
1861 /// # }
1862 /// ```
1863 ///
1864 /// # Parameters
1865 ///
1866 /// - `main_fn`: Your client logic. Receives the builder-selected connection
1867 /// context for sending messages.
1868 ///
1869 /// # Errors
1870 ///
1871 /// Returns an error if a handler, background task, transport, or close
1872 /// callback fails, or if `main_fn` returns an error. Clean incoming EOF is
1873 /// observable through the context's `incoming_closed` future and is not
1874 /// itself an error in this mode.
1875 pub async fn connect_with<R>(
1876 self,
1877 transport: impl ConnectTo<Host> + 'static,
1878 main_fn: impl AsyncFnOnce(Context::Connection<Host::Counterpart>) -> Result<R, crate::Error>,
1879 ) -> Result<R, crate::Error> {
1880 let (_, future) =
1881 self.into_connection_and_future(transport, false, async move |connection| {
1882 main_fn(connection_context::from_raw::<Context, _>(connection)).await
1883 });
1884 future.await
1885 }
1886
1887 /// Helper that returns a [`ConnectionTo<R>`] and a future that runs this connection until `main_fn` returns.
1888 fn into_connection_and_future<R>(
1889 self,
1890 transport: impl ConnectTo<Host> + 'static,
1891 wait_owned_transport: bool,
1892 main_fn: impl AsyncFnOnce(ConnectionTo<Host::Counterpart>) -> Result<R, crate::Error>,
1893 ) -> (
1894 ConnectionTo<Host::Counterpart>,
1895 impl Future<Output = Result<R, crate::Error>>,
1896 ) {
1897 let Self {
1898 name,
1899 handler,
1900 runner,
1901 host: me,
1902 protocol_mode,
1903 on_close,
1904 context: _,
1905 } = self;
1906
1907 let (outgoing_tx, outgoing_rx) = mpsc::unbounded();
1908 let (new_task_tx, new_task_rx) = mpsc::unbounded();
1909 let (dynamic_handler_tx, dynamic_handler_rx) = mpsc::unbounded();
1910 let (foreground_succeeded_tx, foreground_succeeded) = completion_signal();
1911 let (foreground_done_tx, foreground_done) = completion_signal();
1912 let pending_replies = PendingReplies::default();
1913
1914 // Normalize the transport without losing ownership or finish metadata.
1915 let transport_component = crate::DynConnectTo::new(transport);
1916 let (transport_channel, mut transport_future) =
1917 transport_component.into_channel_and_future();
1918 let owned_transport = transport_future.is_some();
1919 let transport_finish = transport_future
1920 .as_mut()
1921 .and_then(crate::ConnectionDriver::take_finish);
1922 let (transport_completion_tx, transport_completion_rx) = oneshot::channel();
1923 let transport_completion = transport_completion_rx
1924 .map(|result| {
1925 result.unwrap_or_else(|error| {
1926 Err(crate::util::internal_error(format!(
1927 "transport task dropped before reporting completion: {error}"
1928 )))
1929 })
1930 })
1931 .boxed()
1932 .shared();
1933
1934 let connection = ConnectionTo::new(
1935 me.counterpart(),
1936 outgoing_tx,
1937 new_task_tx,
1938 dynamic_handler_tx,
1939 transport_completion,
1940 pending_replies.registrar(),
1941 protocol_mode,
1942 );
1943 // Transport progress must outlive successful foreground completion.
1944 // Application tasks remain cancellable. The inherited close-callback
1945 // phase still polls them for cleanup, but physical drain alone does not.
1946 let transport_driver = if let Some(driver) = transport_future {
1947 async move {
1948 let result = driver.await;
1949 drop(transport_completion_tx.send(result.clone()));
1950 result
1951 }
1952 .boxed()
1953 } else {
1954 // Channel-only endpoints have no physical sink work to await.
1955 // Their protocol drain marker still orders accepted output.
1956 drop(transport_completion_tx.send(Ok(())));
1957 future::ready(Ok(())).boxed()
1958 };
1959
1960 // Destructure the channel endpoints
1961 let Channel {
1962 rx: mut transport_incoming_rx,
1963 tx: transport_outgoing_tx,
1964 } = transport_channel;
1965
1966 let transport_incoming = futures::stream::poll_fn({
1967 let mut completion = connection.transport_completion.clone();
1968 let mut completed = false;
1969 move |cx| {
1970 if owned_transport
1971 && !completed
1972 && let std::task::Poll::Ready(Ok(())) =
1973 std::pin::Pin::new(&mut completion).poll(cx)
1974 {
1975 // Owned completion closes the producer boundary, not the
1976 // accepted buffer. The incoming actor still dispatches every
1977 // accepted frame and completes its close callbacks in order.
1978 transport_incoming_rx.close();
1979 completed = true;
1980 }
1981 transport_incoming_rx.poll_next_unpin(cx)
1982 }
1983 });
1984
1985 let protocol_compat = ProtocolCompat::new(protocol_mode);
1986
1987 let future = crate::util::instrument_with_connection_name(name, {
1988 let connection = connection.clone();
1989 async move {
1990 let background = async {
1991 let incoming = {
1992 let pending_replies = pending_replies.clone();
1993 let protocol_compat = protocol_compat.clone();
1994 async {
1995 let mut transport_incoming = std::pin::pin!(transport_incoming);
1996 let incoming = incoming_actor::incoming_protocol_actor(
1997 me.counterpart(),
1998 &connection,
1999 transport_incoming.as_mut(),
2000 dynamic_handler_rx,
2001 pending_replies,
2002 incoming_actor::IncomingHandlers::new(
2003 handler,
2004 on_close,
2005 foreground_succeeded.clone(),
2006 ),
2007 protocol_compat,
2008 );
2009 // Success stops delivery, not physical I/O. An
2010 // underway close callback finishes before sealing.
2011 let result = run_incoming_until_foreground_succeeds(
2012 incoming,
2013 foreground_succeeded,
2014 connection.incoming_closed.clone(),
2015 )
2016 .await;
2017 if result.is_err() {
2018 connection.request_shutdown();
2019 }
2020 result?;
2021 // Keep the raw producer boundary alive while the
2022 // physical driver drains. Discard without application
2023 // delivery, rather than fail a late read-side send.
2024 while transport_incoming.next().await.is_some() {}
2025 Ok(())
2026 }
2027 };
2028 let other_actors = async {
2029 let result = futures::try_join!(
2030 // A ready driver error is authoritative even if its
2031 // closed channel would also make output forwarding fail.
2032 transport_driver,
2033 // Protocol layer: OutgoingMessage -> RawJsonRpcMessage
2034 outgoing_actor::outgoing_protocol_actor(
2035 outgoing_rx,
2036 pending_replies,
2037 transport_outgoing_tx,
2038 protocol_compat,
2039 foreground_done,
2040 ),
2041 );
2042 // Signal before awaiting an underway close callback.
2043 if result.is_err() {
2044 connection.request_shutdown();
2045 }
2046 result?;
2047 Ok(())
2048 };
2049
2050 // Keep close callbacks alive when another core actor fails.
2051 // The outer coordination provides the same protection when
2052 // EOF wakes an application task or the foreground.
2053 run_until_connection_close(
2054 incoming,
2055 other_actors,
2056 connection.incoming_closed.clone(),
2057 )
2058 .await
2059 };
2060
2061 run_until_connection_close(
2062 finish_actor_error(background, &connection),
2063 async {
2064 let application = async {
2065 futures::try_join!(
2066 finish_actor_error(
2067 task_actor::task_actor(new_task_rx, &connection),
2068 &connection,
2069 ),
2070 finish_actor_error(
2071 runner.run_with_connection_to(connection.clone()),
2072 &connection,
2073 ),
2074 )?;
2075 Ok(())
2076 };
2077 let result = run_until_connection_close(
2078 application,
2079 async {
2080 let result = main_fn(connection.clone()).await;
2081 connection.request_shutdown();
2082 if result.is_ok() {
2083 // Stop new incoming delivery immediately,
2084 // including during the callback cleanup phase.
2085 let _ = foreground_succeeded_tx.send(());
2086 }
2087 // Shutdown cancels local consumers, not remote
2088 // requests. Do not add cancellation traffic while
2089 // dropping those consumers before the drain.
2090 connection.pending_replies.disarm_cancellations();
2091 // The application actor (including actual scoped
2092 // runners) remains polled while supervisors finish.
2093 // Only then may ordinary application tasks drop.
2094 connection.wait_protected_operations().await;
2095 result
2096 },
2097 connection.incoming_closed.clone(),
2098 )
2099 .await?;
2100 let _ = foreground_done_tx.send(());
2101 connection
2102 .drain_outgoing(transport_finish, wait_owned_transport)
2103 .await?;
2104 Ok(result)
2105 },
2106 connection.incoming_closed.clone(),
2107 )
2108 .await
2109 }
2110 });
2111
2112 (connection, future)
2113 }
2114}
2115
2116/// Defer an actor error without dropping the other actors that drive owned
2117/// cleanup or an underway close callback. Physical output drain stays separate.
2118async fn finish_actor_error<R: Role>(
2119 actor: impl Future<Output = Result<(), crate::Error>>,
2120 connection: &ConnectionTo<R>,
2121) -> Result<(), crate::Error> {
2122 let result = actor.await;
2123 if result.is_err() {
2124 connection.request_shutdown();
2125 if connection.incoming_closed.is_closing() {
2126 connection.incoming_closed.closed().await;
2127 }
2128 connection.wait_protected_operations().await;
2129 }
2130 result
2131}
2132
2133#[cfg(feature = "unstable_mcp_over_acp")]
2134impl<
2135 Host: Role,
2136 Handler: HandleDispatchFrom<Host::Counterpart>,
2137 Runner: RunWithConnectionTo<Host::Counterpart>,
2138 Close: HandleConnectionClose<Host::Counterpart>,
2139> Builder<Host, Handler, Runner, Close, RawConnectionContext>
2140{
2141 /// Add an MCP server to protocol v1 session setup requests proxied through
2142 /// this connection.
2143 ///
2144 /// The same native MCP server declaration is added to new, load, and resume
2145 /// requests, plus fork requests when `unstable_session_fork` is enabled.
2146 ///
2147 /// Only applicable to proxies. Use the same method on `V2Builder` to
2148 /// attach the server to protocol v2 setup requests.
2149 pub fn with_mcp_server(
2150 self,
2151 mcp_server: McpServer<Host::Counterpart, impl RunWithConnectionTo<Host::Counterpart>>,
2152 ) -> Builder<
2153 Host,
2154 impl HandleDispatchFrom<Host::Counterpart>,
2155 impl RunWithConnectionTo<Host::Counterpart>,
2156 Close,
2157 RawConnectionContext,
2158 >
2159 where
2160 Host::Counterpart: HasPeer<Agent> + HasPeer<Client>,
2161 {
2162 let (handler, runner) = mcp_server.into_handler_and_runner();
2163 self.with_handler(handler).with_runner(runner)
2164 }
2165}
2166
2167#[cfg(all(feature = "unstable_mcp_over_acp", feature = "unstable_protocol_v2"))]
2168impl<
2169 Host: Role,
2170 Handler: HandleDispatchFrom<Host::Counterpart>,
2171 Runner: RunWithConnectionTo<Host::Counterpart>,
2172 Close: HandleConnectionClose<Host::Counterpart>,
2173> Builder<Host, Handler, Runner, Close, V2ConnectionContext>
2174{
2175 /// Add an MCP server to protocol v2 session setup requests proxied through
2176 /// this connection.
2177 ///
2178 /// The same native MCP server declaration is added to new and resume
2179 /// requests, plus fork requests when `unstable_session_fork` is enabled.
2180 /// Unrelated request fields are preserved exactly.
2181 ///
2182 /// Only applicable to proxies.
2183 pub fn with_mcp_server(
2184 self,
2185 mcp_server: McpServer<Host::Counterpart, impl RunWithConnectionTo<Host::Counterpart>>,
2186 ) -> Builder<
2187 Host,
2188 impl HandleDispatchFrom<Host::Counterpart>,
2189 impl RunWithConnectionTo<Host::Counterpart>,
2190 Close,
2191 V2ConnectionContext,
2192 >
2193 where
2194 Host::Counterpart: HasPeer<Agent> + HasPeer<Client>,
2195 {
2196 let (handler, runner) = mcp_server.into_v2_handler_and_runner();
2197 self.with_handler(handler).with_runner(runner)
2198 }
2199}
2200
2201impl<R, H, Run, Close, Context> ConnectTo<R::Counterpart> for Builder<R, H, Run, Close, Context>
2202where
2203 R: Role,
2204 H: HandleDispatchFrom<R::Counterpart> + 'static,
2205 Run: RunWithConnectionTo<R::Counterpart> + 'static,
2206 Close: HandleConnectionClose<R::Counterpart> + 'static,
2207 Context: ConnectionContext,
2208{
2209 async fn connect_to(self, client: impl ConnectTo<R>) -> Result<(), crate::Error> {
2210 Builder::connect_to(self, client).await
2211 }
2212}
2213
2214/// The payload sent through the response oneshot channel.
2215///
2216/// Includes the response value and an optional ack channel for dispatch loop
2217/// synchronization.
2218pub(crate) struct ResponsePayload {
2219 /// The response result - either the JSON value or an error.
2220 pub(crate) result: Result<serde_json::Value, crate::Error>,
2221
2222 /// Optional acknowledgment channel for dispatch loop synchronization.
2223 ///
2224 /// When present, the receiver must send on this channel to signal that
2225 /// response processing is complete, allowing the dispatch loop to continue
2226 /// to the next message.
2227 ///
2228 /// This is present when ordered response consumption was selected before
2229 /// the response was routed during its original dispatch. Public callback
2230 /// consumption and framework-owned ordered blocking transforms can hold
2231 /// the dispatch loop; ordinary blocking consumers, local error paths, and
2232 /// responses routed later do not.
2233 pub(crate) ack_tx: Option<oneshot::Sender<()>>,
2234}
2235
2236type ResponseRouteHook =
2237 Box<dyn FnOnce(&str, &serde_json::Value) -> Result<(), crate::Error> + Send>;
2238
2239/// A prerequisite that must complete before an outgoing request is published
2240/// to the transport.
2241struct RequestReadiness {
2242 future: BoxFuture<'static, Result<(), crate::Error>>,
2243}
2244
2245impl RequestReadiness {
2246 fn new(future: impl Future<Output = Result<(), crate::Error>> + Send + 'static) -> Self {
2247 Self {
2248 future: future.boxed(),
2249 }
2250 }
2251}
2252
2253impl Future for RequestReadiness {
2254 type Output = Result<(), crate::Error>;
2255
2256 fn poll(
2257 mut self: std::pin::Pin<&mut Self>,
2258 cx: &mut std::task::Context<'_>,
2259 ) -> std::task::Poll<Self::Output> {
2260 self.future.as_mut().poll(cx)
2261 }
2262}
2263
2264impl Debug for RequestReadiness {
2265 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2266 formatter
2267 .debug_struct("RequestReadiness")
2268 .finish_non_exhaustive()
2269 }
2270}
2271
2272impl std::fmt::Debug for ResponsePayload {
2273 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2274 f.debug_struct("ResponsePayload")
2275 .field("result", &self.result)
2276 .field("ack_tx", &self.ack_tx.as_ref().map(|_| "..."))
2277 .finish()
2278 }
2279}
2280
2281#[derive(Clone, Debug, Default)]
2282struct ResponseOrdering {
2283 ordered: Arc<AtomicBool>,
2284}
2285
2286impl ResponseOrdering {
2287 fn mark_ordered(&self) {
2288 self.ordered.store(true, Ordering::Release);
2289 }
2290
2291 fn is_ordered(&self) -> bool {
2292 self.ordered.load(Ordering::Acquire)
2293 }
2294}
2295
2296struct PendingReply {
2297 method: String,
2298 role_id: RoleId,
2299 sender: oneshot::Sender<ResponsePayload>,
2300 cancellation_disarm: SentRequestCancellationDisarm,
2301 ordering: ResponseOrdering,
2302 response_route_hook: Option<ResponseRouteHook>,
2303}
2304
2305impl PendingReply {
2306 fn fail(self, error: crate::Error) {
2307 self.cancellation_disarm.disarm();
2308 if self
2309 .sender
2310 .send(ResponsePayload {
2311 result: Err(error),
2312 ack_tx: None,
2313 })
2314 .is_err()
2315 {
2316 tracing::trace!(method = %self.method, "Pending request was already dropped");
2317 }
2318 }
2319
2320 fn fail_incoming_closed(self) {
2321 let error = incoming_transport_closed_error(&self.method);
2322 self.fail(error);
2323 }
2324}
2325
2326#[derive(Default)]
2327struct PendingRepliesInner {
2328 incoming_closed: bool,
2329 replies: HashMap<RequestId, PendingReply>,
2330}
2331
2332#[derive(Clone, Default)]
2333struct PendingReplies {
2334 inner: Arc<Mutex<PendingRepliesInner>>,
2335}
2336
2337impl PendingReplies {
2338 fn registrar(&self) -> PendingRepliesRegistrar {
2339 PendingRepliesRegistrar {
2340 inner: Arc::downgrade(&self.inner),
2341 }
2342 }
2343
2344 fn contains(&self, id: &RequestId) -> bool {
2345 self.inner
2346 .lock()
2347 .expect("pending replies mutex poisoned")
2348 .replies
2349 .contains_key(id)
2350 }
2351
2352 fn remove(&self, id: &RequestId) -> Option<PendingReply> {
2353 self.inner
2354 .lock()
2355 .expect("pending replies mutex poisoned")
2356 .replies
2357 .remove(id)
2358 }
2359
2360 /// Atomically reject new subscriptions and fail every existing one.
2361 fn close_incoming(&self) -> usize {
2362 let replies = {
2363 let mut inner = self.inner.lock().expect("pending replies mutex poisoned");
2364 inner.incoming_closed = true;
2365 std::mem::take(&mut inner.replies)
2366 };
2367 let count = replies.len();
2368 for (_, reply) in replies {
2369 reply.fail_incoming_closed();
2370 }
2371 count
2372 }
2373}
2374
2375/// A non-owning handle used to register a request before it enters the
2376/// outgoing queue. Keeping this weak prevents escaped [`ConnectionTo`] clones
2377/// from extending the lifetime of response senders after the driver stops.
2378#[derive(Clone)]
2379struct PendingRepliesRegistrar {
2380 inner: Weak<Mutex<PendingRepliesInner>>,
2381}
2382
2383impl PendingRepliesRegistrar {
2384 fn disarm_cancellations(&self) {
2385 if let Some(inner) = self.inner.upgrade() {
2386 let inner = inner.lock().expect("pending replies mutex poisoned");
2387 for reply in inner.replies.values() {
2388 reply.cancellation_disarm.disarm();
2389 }
2390 }
2391 }
2392
2393 /// Register a response destination before the request becomes observable.
2394 ///
2395 /// Returns an error after failing `reply` when EOF has already made a
2396 /// response impossible or the connection driver is no longer running.
2397 fn subscribe(
2398 &self,
2399 id: RequestId,
2400 reply: PendingReply,
2401 incoming_closed: &IncomingClosed,
2402 ) -> Result<(), crate::Error> {
2403 let Some(inner) = self.inner.upgrade() else {
2404 let error = if incoming_closed.is_closing() {
2405 incoming_transport_closed_error(&reply.method)
2406 } else {
2407 crate::util::internal_error(format!(
2408 "failed to send outgoing request `{}`: connection is no longer running",
2409 reply.method
2410 ))
2411 };
2412 reply.fail(error.clone());
2413 return Err(error);
2414 };
2415
2416 let result = {
2417 let mut inner = inner.lock().expect("pending replies mutex poisoned");
2418 if inner.incoming_closed {
2419 Err(reply)
2420 } else {
2421 Ok(inner.replies.insert(id, reply))
2422 }
2423 };
2424
2425 match result {
2426 Err(rejected) => {
2427 let error = incoming_transport_closed_error(&rejected.method);
2428 rejected.fail(error.clone());
2429 Err(error)
2430 }
2431 Ok(replaced) => {
2432 if let Some(replaced) = replaced {
2433 replaced.fail(
2434 crate::Error::internal_error()
2435 .data("outgoing request ID was reused before its response arrived"),
2436 );
2437 }
2438 Ok(())
2439 }
2440 }
2441 }
2442
2443 fn remove(&self, id: &RequestId) -> Option<PendingReply> {
2444 self.inner
2445 .upgrade()?
2446 .lock()
2447 .expect("pending replies mutex poisoned")
2448 .replies
2449 .remove(id)
2450 }
2451}
2452
2453impl Debug for PendingRepliesRegistrar {
2454 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2455 formatter
2456 .debug_struct("PendingRepliesRegistrar")
2457 .field("is_connected", &(self.inner.strong_count() > 0))
2458 .finish()
2459 }
2460}
2461
2462/// A request-local marker that is set when the peer asks to cancel the request.
2463///
2464/// Request handlers can get this handle from [`Responder::cancellation`] and
2465/// use it from spawned work to stop long-running request processing
2466/// cooperatively.
2467#[derive(Clone)]
2468pub struct RequestCancellation {
2469 state: Arc<RequestCancellationState>,
2470}
2471
2472struct RequestCancellationState {
2473 cancelled: AtomicBool,
2474 signal_tx: Mutex<Option<oneshot::Sender<()>>>,
2475 signal_rx: future::Shared<BoxFuture<'static, ()>>,
2476}
2477
2478impl RequestCancellation {
2479 fn new() -> Self {
2480 let (signal_tx, signal_rx) = oneshot::channel();
2481 let signal_rx = signal_rx.map(|_| ()).boxed().shared();
2482 Self {
2483 state: Arc::new(RequestCancellationState {
2484 cancelled: AtomicBool::new(false),
2485 signal_tx: Mutex::new(Some(signal_tx)),
2486 signal_rx,
2487 }),
2488 }
2489 }
2490
2491 /// Wait until the peer sends `$/cancel_request` for this request.
2492 ///
2493 /// If cancellation was already requested, this returns immediately.
2494 pub async fn cancelled(&self) {
2495 self.state.signal_rx.clone().await;
2496 }
2497
2498 /// Run request work until it completes or the peer asks to cancel it.
2499 ///
2500 /// If cancellation is requested first, this returns
2501 /// [`Error::request_cancelled`]. This is a convenience for request handlers
2502 /// that want to respond with the normal result or the standard
2503 /// cancellation error.
2504 ///
2505 /// When cancellation wins, `future` is dropped: work stops at its next
2506 /// await point, partial results are lost, and any cleanup must happen in
2507 /// `Drop` implementations. Handlers that need to flush partial results or
2508 /// run async cleanup should instead watch [`cancelled`](Self::cancelled)
2509 /// or poll [`is_cancelled`](Self::is_cancelled) from inside the work.
2510 ///
2511 /// [`Error::request_cancelled`]: crate::Error::request_cancelled
2512 pub async fn run_until_cancelled<T>(
2513 &self,
2514 future: impl std::future::Future<Output = Result<T, crate::Error>>,
2515 ) -> Result<T, crate::Error> {
2516 if self.is_cancelled() {
2517 return Err(crate::Error::request_cancelled());
2518 }
2519
2520 match future::select(pin!(future), pin!(self.cancelled())).await {
2521 Either::Left((result, _)) => result,
2522 Either::Right(((), _)) => Err(crate::Error::request_cancelled()),
2523 }
2524 }
2525
2526 /// Returns whether the peer has already requested cancellation.
2527 #[must_use]
2528 pub fn is_cancelled(&self) -> bool {
2529 self.state.cancelled.load(Ordering::Acquire)
2530 }
2531
2532 fn cancel(&self) {
2533 if self.state.cancelled.swap(true, Ordering::AcqRel) {
2534 return;
2535 }
2536
2537 let signal_tx = self
2538 .state
2539 .signal_tx
2540 .lock()
2541 .expect("request cancellation signal mutex poisoned")
2542 .take();
2543
2544 // Complete the oneshot outside the lock: it wakes waiters, and
2545 // arbitrary waker code must not observe the lock held.
2546 if let Some(signal_tx) = signal_tx {
2547 let _ = signal_tx.send(());
2548 }
2549 }
2550}
2551
2552impl Debug for RequestCancellation {
2553 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2554 formatter
2555 .debug_struct("RequestCancellation")
2556 .field("is_cancelled", &self.is_cancelled())
2557 .finish_non_exhaustive()
2558 }
2559}
2560
2561/// Per-request cancellation state tracked by [`RequestCancellationRegistry`].
2562///
2563/// The full [`RequestCancellation`] marker (with its wakeup machinery) is only
2564/// allocated once a handler asks for it via [`Responder::cancellation`]; until
2565/// then an incoming `$/cancel_request` just flips the entry to `Cancelled`.
2566/// This keeps the per-request cost of the registry to a single map entry.
2567#[derive(Debug)]
2568enum RequestCancellationEntry {
2569 /// The request is in flight; no marker handed out, no cancellation yet.
2570 Armed,
2571 /// `$/cancel_request` arrived before a marker was handed out.
2572 Cancelled,
2573 /// A marker was handed out via [`Responder::cancellation`].
2574 Marker(RequestCancellation),
2575}
2576
2577/// A registered request's cancellation state, tagged with the generation of
2578/// its registration.
2579///
2580/// The generation distinguishes a registration from earlier ones that used
2581/// the same request ID, so that when a (protocol-violating) peer reuses the
2582/// ID of a request that is still in flight, the stale request's responder can
2583/// neither remove nor observe the cancellation state of the newer request.
2584#[derive(Debug)]
2585struct RequestCancellationSlot {
2586 generation: u64,
2587 entry: RequestCancellationEntry,
2588}
2589
2590#[derive(Debug, Default)]
2591struct RequestCancellationRegistryInner {
2592 slots: HashMap<RequestId, RequestCancellationSlot>,
2593 next_generation: u64,
2594}
2595
2596#[derive(Clone, Debug, Default)]
2597struct RequestCancellationRegistry {
2598 inner: Arc<Mutex<RequestCancellationRegistryInner>>,
2599}
2600
2601#[derive(Debug)]
2602struct ResponderCancellation {
2603 id: RequestId,
2604 generation: u64,
2605 registry: RequestCancellationRegistry,
2606}
2607
2608impl RequestCancellationRegistry {
2609 fn new() -> Self {
2610 Self::default()
2611 }
2612
2613 fn register(&self, id: &RequestId) -> ResponderCancellation {
2614 let generation = {
2615 let mut inner = self
2616 .inner
2617 .lock()
2618 .expect("request cancellation registry mutex poisoned");
2619 let generation = inner.next_generation;
2620 inner.next_generation += 1;
2621 if inner
2622 .slots
2623 .insert(
2624 id.clone(),
2625 RequestCancellationSlot {
2626 generation,
2627 entry: RequestCancellationEntry::Armed,
2628 },
2629 )
2630 .is_some()
2631 {
2632 tracing::debug!(
2633 ?id,
2634 "peer reused the ID of a request that is still in flight"
2635 );
2636 }
2637 generation
2638 };
2639 ResponderCancellation {
2640 id: id.clone(),
2641 generation,
2642 registry: self.clone(),
2643 }
2644 }
2645
2646 /// Get the cancellation marker for a registered request, creating it on
2647 /// first use. Repeated calls return markers that share the same state.
2648 ///
2649 /// Exception: when the registration is stale (a protocol-violating peer
2650 /// reused this request ID and the slot now belongs to a newer request, or
2651 /// was already removed by it), every call returns a fresh *detached*
2652 /// marker. Detached markers can never fire, and detached markers from
2653 /// repeated calls do not share state with each other.
2654 fn marker(&self, id: &RequestId, generation: u64) -> RequestCancellation {
2655 let mut inner = self
2656 .inner
2657 .lock()
2658 .expect("request cancellation registry mutex poisoned");
2659 let Some(slot) = inner.slots.get_mut(id) else {
2660 // The slot lives as long as the responder that owns it, so this
2661 // is only reachable if the peer reused this request ID and the
2662 // newer request's responder already removed the replacement slot.
2663 // Hand out a detached marker rather than panicking.
2664 return RequestCancellation::new();
2665 };
2666 if slot.generation != generation {
2667 // The peer reused this request ID while the request was still in
2668 // flight, and the slot now belongs to the newer request. Hand the
2669 // stale responder a detached marker instead of cross-wiring the
2670 // two requests' cancellation states.
2671 return RequestCancellation::new();
2672 }
2673 let entry = &mut slot.entry;
2674 match entry {
2675 RequestCancellationEntry::Marker(marker) => marker.clone(),
2676 RequestCancellationEntry::Armed => {
2677 let marker = RequestCancellation::new();
2678 *entry = RequestCancellationEntry::Marker(marker.clone());
2679 marker
2680 }
2681 RequestCancellationEntry::Cancelled => {
2682 // No one can be waiting on a marker that did not exist yet,
2683 // so firing it while holding the registry lock is fine.
2684 let marker = RequestCancellation::new();
2685 marker.cancel();
2686 *entry = RequestCancellationEntry::Marker(marker.clone());
2687 marker
2688 }
2689 }
2690 }
2691
2692 fn cancel_if_requested(&self, dispatch: &Dispatch) -> Result<bool, crate::Error> {
2693 let Some(request_id) = cancellation_request_id(dispatch)? else {
2694 return Ok(false);
2695 };
2696 Ok(self.cancel(&request_id))
2697 }
2698
2699 /// Mark whichever request currently owns `request_id` as cancelled.
2700 fn cancel(&self, request_id: &RequestId) -> bool {
2701 let marker = {
2702 let mut inner = self
2703 .inner
2704 .lock()
2705 .expect("request cancellation registry mutex poisoned");
2706 let Some(slot) = inner.slots.get_mut(request_id) else {
2707 return false;
2708 };
2709 let entry = &mut slot.entry;
2710 match entry {
2711 RequestCancellationEntry::Marker(marker) => marker.clone(),
2712 RequestCancellationEntry::Cancelled => return true,
2713 RequestCancellationEntry::Armed => {
2714 *entry = RequestCancellationEntry::Cancelled;
2715 return true;
2716 }
2717 }
2718 };
2719
2720 // Fire the marker outside the registry lock: waking waiters runs
2721 // arbitrary waker code that must not observe the lock held.
2722 marker.cancel();
2723 true
2724 }
2725
2726 /// Remove the slot for `request_id`, but only if it still belongs to the
2727 /// registration identified by `generation`.
2728 fn remove(&self, request_id: &RequestId, generation: u64) {
2729 let mut inner = self
2730 .inner
2731 .lock()
2732 .expect("request cancellation registry mutex poisoned");
2733 if inner
2734 .slots
2735 .get(request_id)
2736 .is_some_and(|slot| slot.generation == generation)
2737 {
2738 inner.slots.remove(request_id);
2739 }
2740 }
2741}
2742
2743impl ResponderCancellation {
2744 fn cancellation(&self) -> RequestCancellation {
2745 self.registry.marker(&self.id, self.generation)
2746 }
2747}
2748
2749impl Drop for ResponderCancellation {
2750 fn drop(&mut self) {
2751 self.registry.remove(&self.id, self.generation);
2752 }
2753}
2754
2755fn cancellation_request_id(dispatch: &Dispatch) -> Result<Option<RequestId>, crate::Error> {
2756 let Dispatch::Notification(message) = dispatch else {
2757 return Ok(None);
2758 };
2759 cancellation_request_id_from_message(message)
2760}
2761
2762fn cancellation_request_id_from_message(
2763 message: &UntypedMessage,
2764) -> Result<Option<RequestId>, crate::Error> {
2765 let (method, params) = peel_successor_envelopes(&message.method, &message.params);
2766 if !crate::schema::v1::CancelRequestNotification::matches_method(method) {
2767 return Ok(None);
2768 }
2769
2770 let notification = crate::schema::v1::CancelRequestNotification::parse_message(method, params)?;
2771 Ok(Some(notification.request_id))
2772}
2773
2774/// Peel any [`SuccessorMessage`] envelopes off a notification by reference,
2775/// returning the innermost method and params.
2776///
2777/// This only peeks at the envelope's `method`/`params` fields instead of
2778/// deserializing the envelope, for two reasons:
2779///
2780/// - It avoids deep-cloning the params of every wrapped notification on the
2781/// hot dispatch path just to inspect the inner method name.
2782/// - It is deliberately lenient: a malformed envelope is left as-is here and
2783/// flows on to the handler chain, which is responsible for reporting it.
2784///
2785/// [`SuccessorMessage`]: crate::schema::SuccessorMessage
2786fn peel_successor_envelopes<'message>(
2787 mut method: &'message str,
2788 mut params: &'message serde_json::Value,
2789) -> (&'message str, &'message serde_json::Value) {
2790 while crate::schema::SuccessorMessage::<UntypedMessage>::matches_method(method) {
2791 let Some(inner_method) = params.get("method").and_then(serde_json::Value::as_str) else {
2792 break;
2793 };
2794 method = inner_method;
2795 params = params.get("params").unwrap_or(&serde_json::Value::Null);
2796 }
2797 (method, params)
2798}
2799
2800/// Whether a notification is a `$/cancel_request`, even when it is still
2801/// wrapped in `_proxy/successor` envelopes.
2802///
2803/// `$/cancel_request` is connection-scoped: its `requestId` was allocated on
2804/// the connection the notification arrived over and means nothing on any
2805/// other connection. Generic forwarding code (such as
2806/// [`ConnectionTo::send_proxied_message_to`]) uses this check to drop the raw
2807/// notification instead of tunneling it across a hop; the cancellation still
2808/// propagates because [`forward_response_to`](SentRequest::forward_response_to)
2809/// re-issues it with the forwarded request's own ID.
2810///
2811/// Checking a notification whose method is not the successor envelope is a
2812/// plain method-name comparison. Only successor-wrapped notifications pay for
2813/// a serialization to peel the envelope.
2814#[must_use]
2815pub fn is_cancel_request_notification<N: JsonRpcNotification>(notification: &N) -> bool {
2816 let method = notification.method();
2817 if crate::schema::v1::CancelRequestNotification::matches_method(method) {
2818 return true;
2819 }
2820 if !crate::schema::SuccessorMessage::<UntypedMessage>::matches_method(method) {
2821 return false;
2822 }
2823
2824 match notification.to_untyped_message() {
2825 Ok(untyped) => {
2826 let (method, _params) = peel_successor_envelopes(&untyped.method, &untyped.params);
2827 crate::schema::v1::CancelRequestNotification::matches_method(method)
2828 }
2829 Err(error) => {
2830 tracing::debug!(
2831 ?error,
2832 "failed to inspect successor-wrapped notification for cancellation"
2833 );
2834 false
2835 }
2836 }
2837}
2838
2839/// Messages send to be serialized over the transport.
2840#[derive(Clone)]
2841enum ResponseDestination {
2842 Individual(IndividualResponseSlot),
2843 Batch(BatchResponseSlot),
2844}
2845
2846impl std::fmt::Debug for ResponseDestination {
2847 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2848 match self {
2849 Self::Individual(slot) => formatter.debug_tuple("Individual").field(slot).finish(),
2850 Self::Batch(slot) => formatter.debug_tuple("Batch").field(slot).finish(),
2851 }
2852 }
2853}
2854
2855impl ResponseDestination {
2856 fn individual() -> Self {
2857 Self::Individual(IndividualResponseSlot::default())
2858 }
2859
2860 fn batch(slot_count: usize) -> (impl Iterator<Item = Self>, BatchDispatchCompletion) {
2861 let state = Arc::new(Mutex::new(BatchResponseState {
2862 remaining: slot_count,
2863 responses: (0..slot_count).map(|_| None).collect(),
2864 abandoned: (0..slot_count).map(|_| None).collect(),
2865 active_handler_attempts: (0..slot_count).map(|_| 0).collect(),
2866 dispatch_complete: false,
2867 emitted: false,
2868 }));
2869
2870 (
2871 (0..slot_count).map({
2872 let state = state.clone();
2873 move |index| {
2874 Self::Batch(BatchResponseSlot {
2875 state: state.clone(),
2876 index,
2877 })
2878 }
2879 }),
2880 BatchDispatchCompletion { state },
2881 )
2882 }
2883
2884 fn complete(self, response: RawJsonRpcMessage) -> Option<TransportFrame> {
2885 match self {
2886 Self::Individual(slot) => slot.complete(response),
2887 Self::Batch(slot) => slot.complete(response).map(batch_response_frame),
2888 }
2889 }
2890
2891 fn abandon(self, fallback: RawJsonRpcMessage) -> Option<TransportFrame> {
2892 match self {
2893 Self::Individual(_) => None,
2894 Self::Batch(slot) => slot.abandon(fallback).map(batch_response_frame),
2895 }
2896 }
2897
2898 fn is_batch(&self) -> bool {
2899 matches!(self, Self::Batch(_))
2900 }
2901
2902 fn begin_handler_attempt(
2903 &self,
2904 message_tx: OutgoingMessageTx,
2905 ) -> Option<ResponderHandlerAttempt> {
2906 let Self::Batch(slot) = self else {
2907 return None;
2908 };
2909 slot.begin_handler_attempt();
2910 Some(ResponderHandlerAttempt {
2911 message_tx,
2912 destination: self.clone(),
2913 })
2914 }
2915
2916 fn finish_handler_attempt(self) -> Option<TransportFrame> {
2917 match self {
2918 Self::Individual(_) => None,
2919 Self::Batch(slot) => slot.finish_handler_attempt().map(batch_response_frame),
2920 }
2921 }
2922}
2923
2924#[derive(Clone, Debug, Default)]
2925struct IndividualResponseSlot {
2926 completed: Arc<AtomicBool>,
2927}
2928
2929impl IndividualResponseSlot {
2930 fn complete(self, response: RawJsonRpcMessage) -> Option<TransportFrame> {
2931 if self.completed.swap(true, Ordering::AcqRel) {
2932 tracing::warn!("Ignoring duplicate completion of JSON-RPC request");
2933 return None;
2934 }
2935
2936 Some(TransportFrame::Single(response))
2937 }
2938}
2939
2940fn batch_response_frame(responses: Vec<RawJsonRpcMessage>) -> TransportFrame {
2941 TransportFrame::Batch(
2942 TransportBatch::from_messages(responses)
2943 .expect("a completed JSON-RPC response batch is non-empty"),
2944 )
2945}
2946
2947#[derive(Clone)]
2948struct BatchDispatchCompletion {
2949 state: Arc<Mutex<BatchResponseState>>,
2950}
2951
2952impl std::fmt::Debug for BatchDispatchCompletion {
2953 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2954 formatter
2955 .debug_struct("BatchDispatchCompletion")
2956 .finish_non_exhaustive()
2957 }
2958}
2959
2960impl BatchDispatchCompletion {
2961 fn complete(self) -> Option<TransportFrame> {
2962 let mut state = self
2963 .state
2964 .lock()
2965 .expect("batch response accumulator mutex poisoned");
2966 if state.dispatch_complete {
2967 tracing::warn!("Ignoring duplicate JSON-RPC batch dispatch completion");
2968 return None;
2969 }
2970 state.dispatch_complete = true;
2971 for index in 0..state.responses.len() {
2972 promote_abandoned_response(&mut state, index);
2973 }
2974 take_completed_batch(&mut state).map(batch_response_frame)
2975 }
2976}
2977
2978fn promote_abandoned_response(state: &mut BatchResponseState, index: usize) {
2979 if state.active_handler_attempts[index] == 0
2980 && state.responses[index].is_none()
2981 && let Some(fallback) = state.abandoned[index].take()
2982 {
2983 state.responses[index] = Some(fallback);
2984 state.remaining -= 1;
2985 }
2986}
2987
2988fn take_completed_batch(state: &mut BatchResponseState) -> Option<Vec<RawJsonRpcMessage>> {
2989 if !state.dispatch_complete || state.remaining != 0 || state.emitted {
2990 return None;
2991 }
2992
2993 state.emitted = true;
2994 Some(
2995 state
2996 .responses
2997 .iter_mut()
2998 .map(|response| {
2999 response
3000 .take()
3001 .expect("completed JSON-RPC batch has every response slot")
3002 })
3003 .collect(),
3004 )
3005}
3006
3007#[derive(Clone)]
3008struct BatchResponseSlot {
3009 state: Arc<Mutex<BatchResponseState>>,
3010 index: usize,
3011}
3012
3013impl std::fmt::Debug for BatchResponseSlot {
3014 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3015 formatter
3016 .debug_struct("BatchResponseSlot")
3017 .field("index", &self.index)
3018 .finish_non_exhaustive()
3019 }
3020}
3021
3022impl BatchResponseSlot {
3023 fn begin_handler_attempt(&self) {
3024 let mut state = self
3025 .state
3026 .lock()
3027 .expect("batch response accumulator mutex poisoned");
3028 state.active_handler_attempts[self.index] += 1;
3029 }
3030
3031 fn finish_handler_attempt(self) -> Option<Vec<RawJsonRpcMessage>> {
3032 let mut state = self
3033 .state
3034 .lock()
3035 .expect("batch response accumulator mutex poisoned");
3036 state.active_handler_attempts[self.index] = state.active_handler_attempts[self.index]
3037 .checked_sub(1)
3038 .expect("handler attempt completion without a matching start");
3039 if state.dispatch_complete {
3040 promote_abandoned_response(&mut state, self.index);
3041 }
3042 take_completed_batch(&mut state)
3043 }
3044
3045 fn complete(self, response: RawJsonRpcMessage) -> Option<Vec<RawJsonRpcMessage>> {
3046 let mut state = self
3047 .state
3048 .lock()
3049 .expect("batch response accumulator mutex poisoned");
3050 if state.emitted {
3051 tracing::warn!(
3052 index = self.index,
3053 "Ignoring response after JSON-RPC batch was already completed"
3054 );
3055 return None;
3056 }
3057 if self.index >= state.responses.len() {
3058 tracing::error!(index = self.index, "Invalid JSON-RPC batch response slot");
3059 return None;
3060 }
3061 if state.responses[self.index].is_some() {
3062 tracing::warn!(
3063 index = self.index,
3064 "Ignoring duplicate completion of JSON-RPC batch response slot"
3065 );
3066 return None;
3067 }
3068
3069 state.abandoned[self.index] = None;
3070 state.responses[self.index] = Some(response);
3071 state.remaining -= 1;
3072 take_completed_batch(&mut state)
3073 }
3074
3075 fn abandon(self, fallback: RawJsonRpcMessage) -> Option<Vec<RawJsonRpcMessage>> {
3076 let mut state = self
3077 .state
3078 .lock()
3079 .expect("batch response accumulator mutex poisoned");
3080 if state.emitted || state.responses[self.index].is_some() {
3081 return None;
3082 }
3083 if state.abandoned[self.index].is_some() {
3084 tracing::warn!(
3085 index = self.index,
3086 "Ignoring duplicate abandonment of JSON-RPC batch response slot"
3087 );
3088 return None;
3089 }
3090
3091 if state.dispatch_complete && state.active_handler_attempts[self.index] == 0 {
3092 state.responses[self.index] = Some(fallback);
3093 state.remaining -= 1;
3094 } else {
3095 state.abandoned[self.index] = Some(fallback);
3096 }
3097 take_completed_batch(&mut state)
3098 }
3099}
3100
3101struct BatchResponseState {
3102 remaining: usize,
3103 responses: Vec<Option<RawJsonRpcMessage>>,
3104 abandoned: Vec<Option<RawJsonRpcMessage>>,
3105 active_handler_attempts: Vec<usize>,
3106 dispatch_complete: bool,
3107 emitted: bool,
3108}
3109
3110#[derive(Clone, Debug)]
3111struct RequestReplyTarget {
3112 id: RequestId,
3113 method: String,
3114 destination: ResponseDestination,
3115}
3116
3117struct ResponderHandlerAttempt {
3118 message_tx: OutgoingMessageTx,
3119 destination: ResponseDestination,
3120}
3121
3122impl Drop for ResponderHandlerAttempt {
3123 fn drop(&mut self) {
3124 if let Err(error) = send_raw_message(
3125 &self.message_tx,
3126 OutgoingMessage::BatchHandlerAttemptComplete {
3127 destination: self.destination.clone(),
3128 },
3129 ) {
3130 tracing::debug!(?error, "could not complete JSON-RPC batch handler attempt");
3131 }
3132 }
3133}
3134
3135#[derive(Clone)]
3136struct ResponseReplyTarget {
3137 id: RequestId,
3138 method: String,
3139 sender: Arc<Mutex<Option<oneshot::Sender<ResponsePayload>>>>,
3140 ordering: ResponseOrdering,
3141 dispatch: ResponseDispatch,
3142}
3143
3144impl ResponseReplyTarget {
3145 fn route(self, result: Result<serde_json::Value, crate::Error>) {
3146 let sender = self
3147 .sender
3148 .lock()
3149 .expect("response reply mutex poisoned")
3150 .take();
3151 let Some(sender) = sender else {
3152 tracing::debug!(
3153 method = %self.method,
3154 id = ?self.id,
3155 "response was already routed to its local awaiter"
3156 );
3157 return;
3158 };
3159
3160 let ack_tx = self.dispatch.acknowledgment(&self.ordering);
3161 if sender.send(ResponsePayload { result, ack_tx }).is_err() {
3162 tracing::debug!(
3163 method = %self.method,
3164 id = ?self.id,
3165 "dropped response because local receiver was gone"
3166 );
3167 }
3168 }
3169}
3170
3171#[derive(Clone, Default)]
3172struct ResponseDispatch {
3173 state: Arc<Mutex<ResponseDispatchState>>,
3174}
3175
3176#[derive(Default)]
3177struct ResponseDispatchState {
3178 complete: bool,
3179 ack_rx: Option<oneshot::Receiver<()>>,
3180}
3181
3182impl ResponseDispatch {
3183 fn acknowledgment(&self, ordering: &ResponseOrdering) -> Option<oneshot::Sender<()>> {
3184 if !ordering.is_ordered() {
3185 return None;
3186 }
3187
3188 let mut state = self.state.lock().expect("response dispatch mutex poisoned");
3189 if state.complete {
3190 return None;
3191 }
3192
3193 let (ack_tx, ack_rx) = oneshot::channel();
3194 let previous_ack = state.ack_rx.replace(ack_rx);
3195 debug_assert!(
3196 previous_ack.is_none(),
3197 "a response dispatch can only be routed once"
3198 );
3199 Some(ack_tx)
3200 }
3201
3202 fn complete(&self) -> Option<oneshot::Receiver<()>> {
3203 let mut state = self.state.lock().expect("response dispatch mutex poisoned");
3204 state.complete = true;
3205 state.ack_rx.take()
3206 }
3207}
3208
3209enum HandlerErrorTarget {
3210 Request(RequestReplyTarget),
3211 Response(ResponseReplyTarget),
3212}
3213
3214impl HandlerErrorTarget {
3215 fn begin_handler_attempt(
3216 &self,
3217 message_tx: &OutgoingMessageTx,
3218 ) -> Option<ResponderHandlerAttempt> {
3219 match self {
3220 Self::Request(target) => target.destination.begin_handler_attempt(message_tx.clone()),
3221 Self::Response(_) => None,
3222 }
3223 }
3224}
3225
3226#[derive(Debug)]
3227enum OutgoingMessage {
3228 /// Close the outgoing application queue and acknowledge after every
3229 /// already-accepted message has entered the raw transport queue.
3230 CloseAfterDraining { done: oneshot::Sender<()> },
3231
3232 /// Mark every entry in an incoming batch as dispatched. A completed
3233 /// response array may only be emitted after this barrier.
3234 BatchDispatchComplete { completion: BatchDispatchCompletion },
3235
3236 /// Finish arbitration for a handler attempt that may have dropped a batch
3237 /// responder immediately before returning an error.
3238 BatchHandlerAttemptComplete { destination: ResponseDestination },
3239
3240 /// Record that a claimed batch request dropped its responder without
3241 /// replying. The fallback remains provisional while its handler attempt is
3242 /// active so a handler error can supply the authoritative response.
3243 AbandonedBatchResponse {
3244 id: RequestId,
3245 method: String,
3246 destination: ResponseDestination,
3247 },
3248
3249 /// Send a request to the server.
3250 Request {
3251 /// id assigned to this request (generated by sender)
3252 id: RequestId,
3253
3254 /// the original method
3255 method: String,
3256
3257 /// The logical message before peer-direction wrapping.
3258 untyped: UntypedMessage,
3259
3260 /// How to transform the logical message for its target peer.
3261 remote_style: crate::role::RemoteStyle,
3262
3263 /// Optional prerequisite that must finish before the request becomes
3264 /// visible on the transport.
3265 readiness: Option<RequestReadiness>,
3266 },
3267
3268 /// Send a notification to the server.
3269 Notification {
3270 /// the message to send; this may have a distinct method
3271 /// depending on the peer
3272 untyped: UntypedMessage,
3273 },
3274
3275 /// Send a response to a message from the server
3276 Response {
3277 id: RequestId,
3278
3279 /// Method of the incoming request this response completes.
3280 method: String,
3281
3282 response: Result<serde_json::Value, crate::Error>,
3283
3284 destination: ResponseDestination,
3285 },
3286
3287 /// Send an Error Response that cannot be correlated to a request ID.
3288 UncorrelatedErrorResponse {
3289 error: crate::Error,
3290 destination: ResponseDestination,
3291 },
3292}
3293
3294/// Return type from JrHandler; indicates whether the request was handled or not.
3295#[must_use]
3296#[derive(Debug)]
3297pub enum Handled<T> {
3298 /// The message was handled
3299 Yes,
3300
3301 /// The message was not handled; returns the original value.
3302 ///
3303 /// If `retry` is true,
3304 No {
3305 /// The message to be passed to subsequent handlers
3306 /// (typically the original message, but it may have been
3307 /// mutated.)
3308 message: T,
3309
3310 /// If true, request the message to be queued and retried with
3311 /// dynamic handlers as they are added.
3312 ///
3313 /// This is used for managing session updates since the dynamic
3314 /// handler for a session cannot be added until the response to the
3315 /// new session request has been processed and there may be updates
3316 /// that get processed at the same time.
3317 retry: bool,
3318 },
3319}
3320
3321/// Trait for converting handler return values into [`Handled`].
3322///
3323/// This trait allows handlers to return either `()` (which becomes `Handled::Yes`)
3324/// or an explicit `Handled<T>` value for more control over handler propagation.
3325pub trait IntoHandled<T> {
3326 /// Convert this value into a `Handled<T>`.
3327 fn into_handled(self) -> Handled<T>;
3328}
3329
3330impl<T> IntoHandled<T> for () {
3331 fn into_handled(self) -> Handled<T> {
3332 Handled::Yes
3333 }
3334}
3335
3336impl<T> IntoHandled<T> for Handled<T> {
3337 fn into_handled(self) -> Handled<T> {
3338 self
3339 }
3340}
3341
3342/// A protocol-v2 connection context.
3343///
3344/// Values of this type are supplied to callbacks registered on a
3345/// [`V2Builder`]. It exposes the general connection operations that are valid
3346/// for protocol v2 while keeping version-specific high-level helpers for other
3347/// protocol versions out of the typed context. The generic JSON-RPC send
3348/// methods remain intentionally schema-agnostic.
3349///
3350/// This is a thin, cheaply cloneable handle to the underlying JSON-RPC
3351/// connection. It intentionally does not implement [`Deref`](std::ops::Deref)
3352/// to [`ConnectionTo`].
3353#[cfg(feature = "unstable_protocol_v2")]
3354#[derive(Clone, Debug)]
3355pub struct V2ConnectionTo<Counterpart: Role> {
3356 inner: ConnectionTo<Counterpart>,
3357}
3358
3359#[cfg(feature = "unstable_protocol_v2")]
3360impl<Counterpart: Role> V2ConnectionTo<Counterpart> {
3361 /// Access the underlying version-neutral connection inside the SDK.
3362 pub(crate) fn raw_connection(&self) -> &ConnectionTo<Counterpart> {
3363 &self.inner
3364 }
3365
3366 /// Return the counterpart role this connection is talking to.
3367 pub fn counterpart(&self) -> Counterpart {
3368 self.inner.counterpart()
3369 }
3370
3371 /// Wait until the incoming transport reaches clean EOF.
3372 pub async fn incoming_closed(&self) {
3373 self.inner.incoming_closed().await;
3374 }
3375
3376 /// Return whether clean incoming-EOF processing has completed.
3377 #[must_use]
3378 pub fn is_incoming_closed(&self) -> bool {
3379 self.inner.is_incoming_closed()
3380 }
3381
3382 /// Spawn a task that runs for as long as the JSON-RPC connection is served.
3383 #[track_caller]
3384 pub fn spawn(
3385 &self,
3386 task: impl IntoFuture<Output = Result<(), crate::Error>, IntoFuture: Send + 'static>,
3387 ) -> Result<(), crate::Error> {
3388 self.inner.spawn(task)
3389 }
3390
3391 /// Spawn a JSON-RPC connection in the background.
3392 ///
3393 /// The returned connection context is selected by `builder`; spawning a
3394 /// [`V2Builder`] therefore returns another [`V2ConnectionTo`].
3395 ///
3396 /// ```no_run
3397 /// # use agent_client_protocol::{
3398 /// # Agent, Client, ConnectTo, Error, V2ConnectionTo,
3399 /// # };
3400 /// # fn example(
3401 /// # connection: V2ConnectionTo<Agent>,
3402 /// # transport: impl ConnectTo<Client> + 'static,
3403 /// # ) -> Result<(), Error> {
3404 /// let child: V2ConnectionTo<Agent> =
3405 /// connection.spawn_connection(Client.v2(), transport)?;
3406 /// # drop(child);
3407 /// # Ok(())
3408 /// # }
3409 /// ```
3410 #[track_caller]
3411 pub fn spawn_connection<R: Role, Context: ConnectionContext>(
3412 &self,
3413 builder: Builder<
3414 R,
3415 impl HandleDispatchFrom<R::Counterpart> + 'static,
3416 impl RunWithConnectionTo<R::Counterpart> + 'static,
3417 impl HandleConnectionClose<R::Counterpart> + 'static,
3418 Context,
3419 >,
3420 transport: impl ConnectTo<R> + 'static,
3421 ) -> Result<Context::Connection<R::Counterpart>, crate::Error> {
3422 self.inner.spawn_connection_with_context(builder, transport)
3423 }
3424
3425 /// Send a request or notification and forward its response appropriately.
3426 pub fn send_proxied_message<Req: JsonRpcRequest<Response: Send>, Notif: JsonRpcNotification>(
3427 &self,
3428 message: Dispatch<Req, Notif>,
3429 ) -> Result<(), crate::Error>
3430 where
3431 Counterpart: HasPeer<Counterpart>,
3432 {
3433 self.inner.send_proxied_message(message)
3434 }
3435
3436 /// Send a request or notification to a specific peer and forward its
3437 /// response appropriately.
3438 pub fn send_proxied_message_to<
3439 Peer: Role,
3440 Req: JsonRpcRequest<Response: Send>,
3441 Notif: JsonRpcNotification,
3442 >(
3443 &self,
3444 peer: Peer,
3445 message: Dispatch<Req, Notif>,
3446 ) -> Result<(), crate::Error>
3447 where
3448 Counterpart: HasPeer<Peer>,
3449 {
3450 self.inner.send_proxied_message_to(peer, message)
3451 }
3452
3453 /// Send an outgoing request to the default counterpart peer.
3454 pub fn send_request<Req: JsonRpcRequest>(&self, request: Req) -> SentRequest<Req::Response>
3455 where
3456 Counterpart: HasPeer<Counterpart>,
3457 {
3458 self.inner.send_request(request)
3459 }
3460
3461 /// Send an outgoing request to a specific peer.
3462 pub fn send_request_to<Peer: Role, Req: JsonRpcRequest>(
3463 &self,
3464 peer: Peer,
3465 request: Req,
3466 ) -> SentRequest<Req::Response>
3467 where
3468 Counterpart: HasPeer<Peer>,
3469 {
3470 self.inner.send_request_to(peer, request)
3471 }
3472
3473 /// Prepare a request without sending it until response handling is selected.
3474 ///
3475 /// See [`ConnectionTo::prepare_request`] for publication and ordering semantics.
3476 pub fn prepare_request<Req: JsonRpcRequest>(
3477 &self,
3478 request: Req,
3479 ) -> PreparedRequest<Req::Response>
3480 where
3481 Counterpart: HasPeer<Counterpart>,
3482 {
3483 self.inner.prepare_request(request)
3484 }
3485
3486 /// Prepare a request to a specific peer without sending it.
3487 ///
3488 /// See [`ConnectionTo::prepare_request_to`] for publication and ordering semantics.
3489 pub fn prepare_request_to<Peer: Role, Req: JsonRpcRequest>(
3490 &self,
3491 peer: Peer,
3492 request: Req,
3493 ) -> PreparedRequest<Req::Response>
3494 where
3495 Counterpart: HasPeer<Peer>,
3496 {
3497 self.inner.prepare_request_to(peer, request)
3498 }
3499
3500 /// Send an outgoing notification to the default counterpart peer.
3501 pub fn send_notification<N: JsonRpcNotification>(
3502 &self,
3503 notification: N,
3504 ) -> Result<(), crate::Error>
3505 where
3506 Counterpart: HasPeer<Counterpart>,
3507 {
3508 self.inner.send_notification(notification)
3509 }
3510
3511 /// Send an outgoing notification to a specific peer.
3512 pub fn send_notification_to<Peer: Role, N: JsonRpcNotification>(
3513 &self,
3514 peer: Peer,
3515 notification: N,
3516 ) -> Result<(), crate::Error>
3517 where
3518 Counterpart: HasPeer<Peer>,
3519 {
3520 self.inner.send_notification_to(peer, notification)
3521 }
3522
3523 /// Send a `$/cancel_request` notification to the default counterpart peer.
3524 pub fn send_cancel_request(
3525 &self,
3526 request_id: impl Into<crate::schema::v1::RequestId>,
3527 ) -> Result<(), crate::Error>
3528 where
3529 Counterpart: HasPeer<Counterpart>,
3530 {
3531 self.inner.send_cancel_request(request_id)
3532 }
3533
3534 /// Send a `$/cancel_request` notification to a specific peer.
3535 pub fn send_cancel_request_to<Peer: Role>(
3536 &self,
3537 peer: Peer,
3538 request_id: impl Into<crate::schema::v1::RequestId>,
3539 ) -> Result<(), crate::Error>
3540 where
3541 Counterpart: HasPeer<Peer>,
3542 {
3543 self.inner.send_cancel_request_to(peer, request_id)
3544 }
3545
3546 /// Register a low-level dynamic message handler.
3547 ///
3548 /// Dynamic handlers use the version-neutral [`HandleDispatchFrom`] trait,
3549 /// so their callback receives the underlying [`ConnectionTo`]. Prefer
3550 /// typed handlers on [`V2Builder`] when registration can happen before the
3551 /// connection starts.
3552 ///
3553 /// ```no_run
3554 /// # use agent_client_protocol::{
3555 /// # Agent, Client, ConnectTo, DynamicHandlerGuard, Error, NullHandler,
3556 /// # };
3557 /// # async fn example(
3558 /// # transport: impl ConnectTo<Client> + 'static,
3559 /// # ) -> Result<(), Error> {
3560 /// Client.v2().connect_with(transport, async |connection| {
3561 /// let guard: DynamicHandlerGuard<Agent> =
3562 /// connection.add_dynamic_handler(NullHandler)?;
3563 ///
3564 /// // Keep `guard` alive for as long as the modal handler is needed.
3565 /// drop(guard);
3566 /// Ok(())
3567 /// }).await
3568 /// # }
3569 /// ```
3570 pub fn add_dynamic_handler(
3571 &self,
3572 handler: impl HandleDispatchFrom<Counterpart> + 'static,
3573 ) -> Result<DynamicHandlerGuard<Counterpart>, crate::Error> {
3574 self.inner.add_dynamic_handler(handler)
3575 }
3576}
3577
3578/// Connection context for sending messages and spawning tasks.
3579///
3580/// This is the primary handle for interacting with the JSON-RPC connection from
3581/// within handler callbacks. You can use it to:
3582///
3583/// * Send requests and notifications to the other side
3584/// * Spawn concurrent tasks that run alongside the connection
3585/// * Respond to requests (via [`Responder`] which wraps this)
3586///
3587/// # Cloning
3588///
3589/// `ConnectionTo` is cheaply cloneable - all clones refer to the same underlying connection.
3590/// This makes it easy to share across async tasks.
3591///
3592/// # Event Loop and Concurrency
3593///
3594/// Handler callbacks run on the event loop, which means the connection cannot process new
3595/// messages while your handler is running. Use [`spawn`](Self::spawn) to offload any
3596/// expensive or blocking work to concurrent tasks.
3597///
3598/// See the [Event Loop and Concurrency](Builder#event-loop-and-concurrency) section
3599/// for more details.
3600#[derive(Clone, Debug)]
3601pub struct ConnectionTo<Counterpart: Role> {
3602 counterpart: Counterpart,
3603 message_tx: OutgoingMessageTx,
3604 task_tx: TaskTx,
3605 dynamic_handler_tx: mpsc::UnboundedSender<DynamicHandlerMessage<Counterpart>>,
3606 transport_completion: SharedTransportCompletion,
3607 pending_replies: PendingRepliesRegistrar,
3608 #[cfg_attr(
3609 not(feature = "unstable_protocol_v2"),
3610 allow(
3611 dead_code,
3612 reason = "retained so ConnectionTo has one constructor shape"
3613 )
3614 )]
3615 protocol_mode: ProtocolMode,
3616 incoming_closed: IncomingClosed,
3617 protected_operations: Arc<Mutex<ProtectedOperations>>,
3618 runner_error_scope: Option<run::RunnerErrorScope>,
3619}
3620
3621type SharedTransportCompletion = future::Shared<BoxFuture<'static, Result<(), crate::Error>>>;
3622
3623type SharedCompletionSignal = future::Shared<BoxFuture<'static, ()>>;
3624
3625#[derive(Default)]
3626struct ProtectedOperations {
3627 pending: Vec<oneshot::Receiver<()>>,
3628 joining: Option<SharedCompletionSignal>,
3629}
3630
3631impl Debug for ProtectedOperations {
3632 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3633 formatter
3634 .debug_struct("ProtectedOperations")
3635 .field("pending", &self.pending.len())
3636 .field("joining", &self.joining.is_some())
3637 .finish_non_exhaustive()
3638 }
3639}
3640
3641fn completion_signal() -> (oneshot::Sender<()>, SharedCompletionSignal) {
3642 let (tx, rx) = oneshot::channel();
3643 let signal = async move {
3644 // Dropping a sender (e.g. foreground failure) is not success.
3645 if rx.await.is_err() {
3646 future::pending::<()>().await;
3647 }
3648 }
3649 .boxed()
3650 .shared();
3651 (tx, signal)
3652}
3653
3654#[derive(Clone)]
3655struct IncomingClosed {
3656 state: Arc<IncomingClosedState>,
3657}
3658
3659struct IncomingClosedState {
3660 closing: AtomicBool,
3661 closed: AtomicBool,
3662 signal_tx: Mutex<Option<oneshot::Sender<()>>>,
3663 signal_rx: future::Shared<BoxFuture<'static, ()>>,
3664 shutdown_tx: Mutex<Option<oneshot::Sender<()>>>,
3665 #[cfg(any(feature = "unstable_mcp_over_acp", test))]
3666 shutdown_rx: SharedCompletionSignal,
3667}
3668
3669impl IncomingClosed {
3670 fn new() -> Self {
3671 let (signal_tx, signal_rx) = oneshot::channel();
3672 let (shutdown_tx, shutdown_rx) = oneshot::channel();
3673 #[cfg(not(any(feature = "unstable_mcp_over_acp", test)))]
3674 drop(shutdown_rx);
3675 Self {
3676 state: Arc::new(IncomingClosedState {
3677 closing: AtomicBool::new(false),
3678 closed: AtomicBool::new(false),
3679 signal_tx: Mutex::new(Some(signal_tx)),
3680 signal_rx: signal_rx.map(|_| ()).boxed().shared(),
3681 shutdown_tx: Mutex::new(Some(shutdown_tx)),
3682 #[cfg(any(feature = "unstable_mcp_over_acp", test))]
3683 shutdown_rx: shutdown_rx.map(|_| ()).boxed().shared(),
3684 }),
3685 }
3686 }
3687
3688 fn begin_close(&self) {
3689 self.state.closing.store(true, Ordering::Release);
3690 self.request_shutdown();
3691 }
3692
3693 fn request_shutdown(&self) {
3694 if let Some(tx) = self
3695 .state
3696 .shutdown_tx
3697 .lock()
3698 .expect("shutdown signal mutex poisoned")
3699 .take()
3700 {
3701 let _ = tx.send(());
3702 }
3703 }
3704
3705 fn finish_close(&self) {
3706 self.state.closed.store(true, Ordering::Release);
3707 let signal_tx = self
3708 .state
3709 .signal_tx
3710 .lock()
3711 .expect("incoming-close signal mutex poisoned")
3712 .take();
3713
3714 if let Some(signal_tx) = signal_tx {
3715 let _ = signal_tx.send(());
3716 }
3717 }
3718
3719 async fn closed(&self) {
3720 self.state.signal_rx.clone().await;
3721 }
3722
3723 fn is_closed(&self) -> bool {
3724 self.state.closed.load(Ordering::Acquire)
3725 }
3726
3727 fn is_closing(&self) -> bool {
3728 self.state.closing.load(Ordering::Acquire)
3729 }
3730}
3731
3732impl Debug for IncomingClosed {
3733 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3734 formatter
3735 .debug_struct("IncomingClosed")
3736 .field("is_closing", &self.is_closing())
3737 .field("is_closed", &self.is_closed())
3738 .finish_non_exhaustive()
3739 }
3740}
3741
3742/// Stable discriminator stored in the `data.reason` field of errors produced
3743/// when the incoming transport reaches clean EOF before a request receives its
3744/// response.
3745pub const INCOMING_TRANSPORT_CLOSED_REASON: &str = "incoming_transport_closed";
3746
3747/// Return whether `error` reports that the incoming transport reached clean
3748/// EOF before a request received its response.
3749#[must_use]
3750pub fn is_incoming_transport_closed(error: &crate::Error) -> bool {
3751 error
3752 .data
3753 .as_ref()
3754 .and_then(|data| data.get("reason"))
3755 .and_then(serde_json::Value::as_str)
3756 == Some(INCOMING_TRANSPORT_CLOSED_REASON)
3757}
3758
3759fn incoming_transport_closed_error(method: &str) -> crate::Error {
3760 let mut error = crate::Error::internal_error();
3761 error.message = "Incoming transport closed".to_string();
3762 error.data(serde_json::json!({
3763 "reason": INCOMING_TRANSPORT_CLOSED_REASON,
3764 "method": method,
3765 }))
3766}
3767
3768/// Unlike the cleanup coordinator below, check success before polling delivery:
3769/// an already-ready success must not resume a message handler into another
3770/// dispatch (including another entry of the same batch).
3771fn run_incoming_until_foreground_succeeds(
3772 incoming: impl Future<Output = Result<(), crate::Error>>,
3773 foreground_succeeded: SharedCompletionSignal,
3774 incoming_closed: IncomingClosed,
3775) -> impl Future<Output = Result<(), crate::Error>> {
3776 let mut incoming = Box::pin(incoming);
3777 future::poll_fn(move |cx| {
3778 if foreground_succeeded.clone().poll_unpin(cx).is_ready() && !incoming_closed.is_closing() {
3779 return std::task::Poll::Ready(Ok(()));
3780 }
3781 // A close callback already underway is protected. Its result is polled
3782 // before stopping, so callback errors retain their existing precedence.
3783 incoming.as_mut().poll(cx)
3784 })
3785}
3786
3787/// Run the connection background alongside its foreground while ensuring that
3788/// a foreground woken by incoming EOF cannot cancel close callbacks midway.
3789fn run_until_connection_close<R>(
3790 background: impl Future<Output = Result<(), crate::Error>>,
3791 foreground: impl Future<Output = Result<R, crate::Error>>,
3792 incoming_closed: IncomingClosed,
3793) -> impl Future<Output = Result<R, crate::Error>> {
3794 // Box these before constructing the returned future. Keeping the generic
3795 // connection actors directly in this async state would substantially grow
3796 // every `connect_*` future.
3797 let background = Box::pin(background);
3798 let foreground = Box::pin(foreground);
3799
3800 async move {
3801 match future::select(background, foreground).await {
3802 Either::Left((background_result, foreground)) => {
3803 background_result?;
3804 foreground.await
3805 }
3806 Either::Right((foreground_result, background)) => {
3807 if !incoming_closed.is_closing() {
3808 return foreground_result;
3809 }
3810
3811 match future::select(background, Box::pin(incoming_closed.closed())).await {
3812 Either::Left((background_result, _)) => {
3813 background_result?;
3814 foreground_result
3815 }
3816 Either::Right(((), background)) => {
3817 // Poll the background first once more so an error returned
3818 // by the just-finished close callback wins over the ready
3819 // foreground result.
3820 crate::util::run_until(background, future::ready(foreground_result)).await
3821 }
3822 }
3823 }
3824 }
3825 }
3826}
3827
3828impl<Counterpart: Role> ConnectionTo<Counterpart> {
3829 fn new(
3830 counterpart: Counterpart,
3831 message_tx: mpsc::UnboundedSender<OutgoingMessage>,
3832 task_tx: mpsc::UnboundedSender<Task>,
3833 dynamic_handler_tx: mpsc::UnboundedSender<DynamicHandlerMessage<Counterpart>>,
3834 transport_completion: SharedTransportCompletion,
3835 pending_replies: PendingRepliesRegistrar,
3836 protocol_mode: ProtocolMode,
3837 ) -> Self {
3838 Self {
3839 counterpart,
3840 message_tx,
3841 task_tx,
3842 dynamic_handler_tx,
3843 transport_completion,
3844 pending_replies,
3845 protocol_mode,
3846 incoming_closed: IncomingClosed::new(),
3847 protected_operations: Arc::default(),
3848 runner_error_scope: None,
3849 }
3850 }
3851
3852 pub(crate) fn with_runner_error_scope(mut self, scope: run::RunnerErrorScope) -> Self {
3853 self.runner_error_scope = Some(scope);
3854 self
3855 }
3856
3857 pub(crate) fn finish_runner_error(
3858 &self,
3859 error: crate::Error,
3860 ) -> impl Future<Output = ()> + Send + '_ {
3861 if let Some(scope) = &self.runner_error_scope {
3862 Either::Left(scope.finish(error))
3863 } else {
3864 // Unscoped runners belong to the connection itself.
3865 self.request_shutdown();
3866 Either::Right(self.wait_protected_operations())
3867 }
3868 }
3869
3870 /// Spawn only a connection-owned supervisor whose async cleanup must finish
3871 /// before the driver returns. Ordinary application work must use `spawn`.
3872 #[cfg(any(feature = "unstable_mcp_over_acp", test))]
3873 #[track_caller]
3874 pub(crate) fn spawn_protected(
3875 &self,
3876 task: impl IntoFuture<Output = Result<(), crate::Error>, IntoFuture: Send + 'static>,
3877 ) -> Result<(), crate::Error> {
3878 let mut state = self
3879 .protected_operations
3880 .lock()
3881 .expect("protected operations mutex poisoned");
3882 if state.joining.is_some() {
3883 return Err(crate::Error::request_cancelled());
3884 }
3885 // Reap completed acknowledgments at admission, rather than retaining
3886 // every operation for the entire lifetime of the connection.
3887 state
3888 .pending
3889 .retain_mut(|done| matches!(done.try_recv(), Ok(None)));
3890 let (done_tx, done_rx) = oneshot::channel();
3891 let task = task.into_future();
3892 self.spawn(async move {
3893 let result = task.await;
3894 let _ = done_tx.send(());
3895 result
3896 })?;
3897 state.pending.push(done_rx);
3898 Ok(())
3899 }
3900
3901 pub(crate) async fn wait_protected_operations(&self) {
3902 let joining = {
3903 let mut state = self
3904 .protected_operations
3905 .lock()
3906 .expect("protected operations mutex poisoned");
3907 if state.joining.is_none() {
3908 let operations = std::mem::take(&mut state.pending);
3909 state.joining = Some(
3910 async move {
3911 for operation in operations {
3912 let _ = operation.await;
3913 }
3914 }
3915 .boxed()
3916 .shared(),
3917 );
3918 }
3919 state.joining.as_ref().expect("join initialized").clone()
3920 };
3921 joining.await;
3922 }
3923
3924 pub(crate) fn request_shutdown(&self) {
3925 self.incoming_closed.request_shutdown();
3926 }
3927
3928 /// Early cancellation for owned native work, before close callbacks or drain.
3929 #[cfg(any(feature = "unstable_mcp_over_acp", test))]
3930 pub(crate) async fn shutdown_requested(&self) {
3931 self.incoming_closed.state.shutdown_rx.clone().await;
3932 }
3933
3934 #[cfg(feature = "unstable_protocol_v2")]
3935 pub(crate) fn acp_protocol_version(&self) -> Option<crate::schema::ProtocolVersion> {
3936 self.protocol_mode.api_protocol_version()
3937 }
3938
3939 /// Return the counterpart role this connection is talking to.
3940 pub fn counterpart(&self) -> Counterpart {
3941 self.counterpart.clone()
3942 }
3943
3944 /// Wait until the incoming transport reaches clean EOF.
3945 ///
3946 /// Transport closure means that no more messages or responses can arrive.
3947 /// Pending requests are failed first; this completes after registered
3948 /// [`Builder::on_close`] callbacks finish.
3949 /// It does not automatically cancel the future passed to
3950 /// [`Builder::connect_with`]; use [`Builder::on_close`] when the connection
3951 /// should run application-specific cleanup or terminate that future.
3952 pub async fn incoming_closed(&self) {
3953 self.incoming_closed.closed().await;
3954 }
3955
3956 /// Return whether clean incoming-EOF processing has completed.
3957 ///
3958 /// This remains `false` while [`Builder::on_close`] callbacks are running.
3959 #[must_use]
3960 pub fn is_incoming_closed(&self) -> bool {
3961 self.incoming_closed.is_closed()
3962 }
3963
3964 /// Stop accepting outgoing messages, drain routable output through the
3965 /// protocol actor, and finish cooperative physical sinks. Reactive serving also
3966 /// joins owned transport work after incoming EOF.
3967 async fn drain_outgoing(
3968 &self,
3969 finish: Option<crate::component::FinishControl>,
3970 wait_owned_transport: bool,
3971 ) -> Result<(), crate::Error> {
3972 let (done_tx, done_rx) = oneshot::channel();
3973 let marker_result = send_raw_message(
3974 &self.message_tx,
3975 OutgoingMessage::CloseAfterDraining { done: done_tx },
3976 );
3977 let marker_result = match marker_result {
3978 Ok(()) => done_rx.await.map_err(|error| {
3979 crate::util::internal_error(format!(
3980 "outgoing drain marker was dropped before completion: {error}"
3981 ))
3982 }),
3983 Err(error) => Err(error),
3984 };
3985
3986 let physical_finish = finish.is_some();
3987 if let Some(mut finish) = finish {
3988 // Only finish the physical sink after the protocol actor has handed
3989 // off its accepted output. Closing it earlier races the drain.
3990 finish.request();
3991 }
3992 if physical_finish || wait_owned_transport {
3993 // Cooperative completion proves physical sink drain. Reactive serving
3994 // also waits for owned work after EOF (e.g. child exit status).
3995 self.transport_completion.clone().await?;
3996 }
3997 // Opaque application drivers have no physical finish contract. Keep
3998 // polling their errors in the background, but do not globally join work
3999 // which may intentionally run forever after the foreground returns.
4000 marker_result
4001 }
4002
4003 fn is_incoming_closing(&self) -> bool {
4004 self.incoming_closed.is_closing()
4005 }
4006
4007 pub(super) fn begin_incoming_close(&self) {
4008 self.incoming_closed.begin_close();
4009 }
4010
4011 pub(super) fn finish_incoming_close(&self) {
4012 self.incoming_closed.finish_close();
4013 }
4014
4015 /// Spawns a task that will run so long as the JSON-RPC connection is being served.
4016 ///
4017 /// This is the primary mechanism for offloading expensive work from handler callbacks
4018 /// to avoid blocking the event loop. Spawned tasks run concurrently with the connection,
4019 /// allowing the server to continue processing messages.
4020 ///
4021 /// # Event Loop
4022 ///
4023 /// Handler callbacks run on the event loop, which cannot process new messages while
4024 /// your handler is running. Use `spawn` for any expensive operations:
4025 ///
4026 /// ```no_run
4027 /// # use agent_client_protocol_test::*;
4028 /// # async fn example() -> Result<(), agent_client_protocol::Error> {
4029 /// # let connection = mock_connection();
4030 /// connection.on_receive_request(async |req: ProcessRequest, responder, cx| {
4031 /// // Clone cx for the spawned task
4032 /// cx.spawn({
4033 /// let connection = cx.clone();
4034 /// async move {
4035 /// let result = expensive_operation(&req.data).await?;
4036 /// connection.send_notification(ProcessComplete { result })?;
4037 /// Ok(())
4038 /// }
4039 /// })?;
4040 ///
4041 /// // Respond immediately
4042 /// responder.respond(ProcessResponse { result: "started".into() })
4043 /// }, agent_client_protocol::on_receive_request!())
4044 /// # .connect_to(agent_client_protocol_test::MockTransport).await?;
4045 /// # Ok(())
4046 /// # }
4047 /// ```
4048 ///
4049 /// # Errors
4050 ///
4051 /// If the spawned task returns an error, the entire server will shut down.
4052 #[track_caller]
4053 pub fn spawn(
4054 &self,
4055 task: impl IntoFuture<Output = Result<(), crate::Error>, IntoFuture: Send + 'static>,
4056 ) -> Result<(), crate::Error> {
4057 let location = std::panic::Location::caller();
4058 let task = task.into_future();
4059 Task::new(location, task).spawn(&self.task_tx)
4060 }
4061
4062 /// Spawn a JSON-RPC connection in the background and return a raw
4063 /// [`ConnectionTo`] for it.
4064 ///
4065 /// This is useful for creating multiple connections that communicate with each other,
4066 /// such as implementing proxy patterns or connecting to multiple backend services.
4067 ///
4068 /// # Arguments
4069 ///
4070 /// - `builder`: The connection builder with handlers configured
4071 /// - `transport`: The transport component to connect to
4072 ///
4073 /// # Returns
4074 ///
4075 /// The child builder may select any callback context. For example, this
4076 /// method can spawn a `V2Builder`, whose callbacks receive
4077 /// `V2ConnectionTo`, while preserving this method's existing raw return
4078 /// type and single explicit role parameter.
4079 ///
4080 /// When a raw parent also needs the builder-selected child handle, use the
4081 /// protocol-v2 `spawn_connection_with_context` method.
4082 ///
4083 /// # Example: Proxying to a backend connection
4084 ///
4085 /// ```
4086 /// # use agent_client_protocol::UntypedRole;
4087 /// # use agent_client_protocol::{Builder, ConnectionTo};
4088 /// # use agent_client_protocol_test::*;
4089 /// # async fn example(cx: ConnectionTo<UntypedRole>) -> Result<(), agent_client_protocol::Error> {
4090 /// // Set up a backend connection builder
4091 /// let backend = UntypedRole.builder()
4092 /// .on_receive_request(async |req: MyRequest, responder, _cx| {
4093 /// responder.respond(MyResponse { status: "ok".into() })
4094 /// }, agent_client_protocol::on_receive_request!());
4095 ///
4096 /// // Spawn it and get a context to send requests to it
4097 /// let backend_connection = cx.spawn_connection::<UntypedRole>(backend, MockTransport)?;
4098 ///
4099 /// // Now you can forward requests to the backend
4100 /// let response = backend_connection.send_request(MyRequest {}).block_task().await?;
4101 /// # Ok(())
4102 /// # }
4103 /// ```
4104 #[track_caller]
4105 pub fn spawn_connection<R: Role>(
4106 &self,
4107 builder: Builder<
4108 R,
4109 impl HandleDispatchFrom<R::Counterpart> + 'static,
4110 impl RunWithConnectionTo<R::Counterpart> + 'static,
4111 impl HandleConnectionClose<R::Counterpart> + 'static,
4112 impl ConnectionContext,
4113 >,
4114 transport: impl ConnectTo<R> + 'static,
4115 ) -> Result<ConnectionTo<R::Counterpart>, crate::Error> {
4116 self.spawn_connection_raw(builder, transport)
4117 }
4118
4119 /// Spawn a JSON-RPC connection and return the connection context selected
4120 /// by its builder.
4121 ///
4122 /// This is the low-level counterpart to
4123 /// [`V2ConnectionTo::spawn_connection`] for code that intentionally works
4124 /// with a raw [`ConnectionTo`], such as custom [`HandleDispatchFrom`] or
4125 /// [`RunWithConnectionTo`] implementations. Prefer [`Self::spawn_connection`]
4126 /// when a raw child handle is sufficient.
4127 ///
4128 /// ```no_run
4129 /// # use agent_client_protocol::{
4130 /// # Agent, Client, ConnectTo, ConnectionTo, Error, UntypedRole,
4131 /// # V2ConnectionTo,
4132 /// # };
4133 /// # fn example(
4134 /// # connection: ConnectionTo<UntypedRole>,
4135 /// # transport: impl ConnectTo<Client> + 'static,
4136 /// # ) -> Result<(), Error> {
4137 /// let child: V2ConnectionTo<Agent> =
4138 /// connection.spawn_connection_with_context(Client.v2(), transport)?;
4139 /// # drop(child);
4140 /// # Ok(())
4141 /// # }
4142 /// ```
4143 #[cfg(feature = "unstable_protocol_v2")]
4144 #[track_caller]
4145 pub fn spawn_connection_with_context<R: Role, Context: ConnectionContext>(
4146 &self,
4147 builder: Builder<
4148 R,
4149 impl HandleDispatchFrom<R::Counterpart> + 'static,
4150 impl RunWithConnectionTo<R::Counterpart> + 'static,
4151 impl HandleConnectionClose<R::Counterpart> + 'static,
4152 Context,
4153 >,
4154 transport: impl ConnectTo<R> + 'static,
4155 ) -> Result<Context::Connection<R::Counterpart>, crate::Error> {
4156 let connection = self.spawn_connection_raw(builder, transport)?;
4157 Ok(connection_context::from_raw::<Context, _>(connection))
4158 }
4159
4160 #[track_caller]
4161 fn spawn_connection_raw<R: Role, Context: ConnectionContext>(
4162 &self,
4163 builder: Builder<
4164 R,
4165 impl HandleDispatchFrom<R::Counterpart> + 'static,
4166 impl RunWithConnectionTo<R::Counterpart> + 'static,
4167 impl HandleConnectionClose<R::Counterpart> + 'static,
4168 Context,
4169 >,
4170 transport: impl ConnectTo<R> + 'static,
4171 ) -> Result<ConnectionTo<R::Counterpart>, crate::Error> {
4172 let (connection, future) =
4173 builder.into_connection_and_future(transport, false, |_| std::future::pending());
4174 Task::new(std::panic::Location::caller(), future).spawn(&self.task_tx)?;
4175 Ok(connection)
4176 }
4177
4178 /// Send a request/notification and forward the response appropriately.
4179 ///
4180 /// The request context's response type matches the request's response type,
4181 /// enabling type-safe message forwarding.
4182 pub fn send_proxied_message<Req: JsonRpcRequest<Response: Send>, Notif: JsonRpcNotification>(
4183 &self,
4184 message: Dispatch<Req, Notif>,
4185 ) -> Result<(), crate::Error>
4186 where
4187 Counterpart: HasPeer<Counterpart>,
4188 {
4189 self.send_proxied_message_to(self.counterpart(), message)
4190 }
4191
4192 /// Send a request/notification and forward the response appropriately.
4193 ///
4194 /// The request context's response type matches the request's response type,
4195 /// enabling type-safe message forwarding.
4196 ///
4197 /// `$/cancel_request` notifications are *not* forwarded: their `requestId`
4198 /// refers to a request on the connection they arrived over and would be
4199 /// meaningless to `peer`. Cancellation instead propagates hop by hop,
4200 /// because the responders passed to
4201 /// [`forward_response_to`](SentRequest::forward_response_to) observe it
4202 /// and re-issue the cancellation with the forwarded request's own ID.
4203 pub fn send_proxied_message_to<
4204 Peer: Role,
4205 Req: JsonRpcRequest<Response: Send>,
4206 Notif: JsonRpcNotification,
4207 >(
4208 &self,
4209 peer: Peer,
4210 message: Dispatch<Req, Notif>,
4211 ) -> Result<(), crate::Error>
4212 where
4213 Counterpart: HasPeer<Peer>,
4214 {
4215 match message {
4216 Dispatch::Request(request, responder) => self
4217 .send_ordered_request_to(peer, request)
4218 .forward_response_to(responder),
4219 Dispatch::Notification(notification) => {
4220 // `$/cancel_request` is connection-scoped: its `requestId` was
4221 // allocated on the connection the notification arrived over
4222 // and means nothing to `peer`. The cancellation has already
4223 // been recorded on this connection's responder markers, and
4224 // `forward_response_to` re-issues it for the forwarded request
4225 // with the correct per-hop ID, so drop the raw notification
4226 // instead of tunneling a meaningless ID across the hop.
4227 if is_cancel_request_notification(¬ification) {
4228 tracing::debug!(
4229 "not forwarding hop-scoped `$/cancel_request` notification across proxy hop"
4230 );
4231 return Ok(());
4232 }
4233 self.send_notification_to(peer, notification)
4234 }
4235 Dispatch::Response(result, router) => {
4236 // Responses are forwarded directly to their destination
4237 router.route_with_result(result)
4238 }
4239 }
4240 }
4241
4242 /// Send an outgoing request and return a [`SentRequest`] for handling the reply.
4243 ///
4244 /// The returned [`SentRequest`] makes the response-consumption mode explicit:
4245 ///
4246 /// * [`on_receiving_result`](SentRequest::on_receiving_result) - Register a callback and
4247 /// return immediately. If registered before the response is routed during its original
4248 /// dispatch, the loop waits for the callback to complete.
4249 /// * [`block_task`](SentRequest::block_task) - Wait on the current task until the response
4250 /// arrives. This is only safe when that task already runs outside the dispatch loop.
4251 ///
4252 /// For callback ordering selected before publication, use
4253 /// [`prepare_request`](Self::prepare_request) instead. Even an immediately
4254 /// chained callback can race with a fast response on a concurrent connection.
4255 ///
4256 /// # Anti-Footgun Design
4257 ///
4258 /// The API intentionally makes it difficult to block on the result directly to prevent
4259 /// the common mistake of blocking the event loop while waiting for a response:
4260 ///
4261 /// ```compile_fail
4262 /// # use agent_client_protocol_test::*;
4263 /// # async fn example(cx: agent_client_protocol::ConnectionTo<agent_client_protocol::UntypedRole>) -> Result<(), agent_client_protocol::Error> {
4264 /// // ❌ This doesn't compile - prevents blocking the event loop
4265 /// let response = cx.send_request(MyRequest {}).await?;
4266 /// # Ok(())
4267 /// # }
4268 /// ```
4269 ///
4270 /// ```no_run
4271 /// # use agent_client_protocol_test::*;
4272 /// # async fn example(cx: agent_client_protocol::ConnectionTo<agent_client_protocol::UntypedRole>) -> Result<(), agent_client_protocol::Error> {
4273 /// // ✅ Option 1: Register an ordered callback (safe in handlers)
4274 /// cx.send_request(MyRequest {})
4275 /// .on_receiving_result(async |result| {
4276 /// // Handle the response
4277 /// Ok(())
4278 /// })?;
4279 ///
4280 /// // ✅ Option 2: Block in spawned task (safe because task is concurrent)
4281 /// cx.spawn({
4282 /// let cx = cx.clone();
4283 /// async move {
4284 /// let response = cx.send_request(MyRequest {})
4285 /// .block_task()
4286 /// .await?;
4287 /// // Process response...
4288 /// Ok(())
4289 /// }
4290 /// })?;
4291 /// # Ok(())
4292 /// # }
4293 /// ```
4294 /// Send an outgoing request to the default counterpart peer.
4295 ///
4296 /// This is a convenience method that sends to the counterpart role `R`.
4297 /// For explicit control over the target peer, use [`send_request_to`](Self::send_request_to).
4298 pub fn send_request<Req: JsonRpcRequest>(&self, request: Req) -> SentRequest<Req::Response>
4299 where
4300 Counterpart: HasPeer<Counterpart>,
4301 {
4302 self.send_request_to(self.counterpart.clone(), request)
4303 }
4304
4305 /// Send an outgoing request to a specific peer.
4306 ///
4307 /// The message will be transformed according to the [`HasPeer`](crate::role::HasPeer)
4308 /// implementation before being sent.
4309 pub fn send_request_to<Peer: Role, Req: JsonRpcRequest>(
4310 &self,
4311 peer: Peer,
4312 request: Req,
4313 ) -> SentRequest<Req::Response>
4314 where
4315 Counterpart: HasPeer<Peer>,
4316 {
4317 self.send_request_to_with_options(peer, request, false, None, None)
4318 }
4319
4320 /// Prepare a request without sending it until response handling is selected.
4321 ///
4322 /// Unlike [`send_request`](Self::send_request), this does not register a
4323 /// pending reply or enqueue the request. A consuming method on the returned
4324 /// [`PreparedRequest`] publishes it synchronously. Callback-style methods
4325 /// select ordered consumption before publication, closing the race with a
4326 /// fast peer response even when the connection runs on another task.
4327 ///
4328 /// Dropping the prepared request sends nothing. See [`PreparedRequest`] for
4329 /// the available consumption modes and their error and cancellation behavior.
4330 ///
4331 /// # Examples
4332 ///
4333 /// ```no_run
4334 /// # use agent_client_protocol::{ConnectionTo, Error, UntypedRole};
4335 /// # use agent_client_protocol_test::MyRequest;
4336 /// # fn example(connection: ConnectionTo<UntypedRole>) -> Result<(), Error> {
4337 /// connection.prepare_request(MyRequest {}).on_receiving_result(async |result| {
4338 /// let response = result?;
4339 /// // Apply bounded response work before later inbound messages.
4340 /// Ok(())
4341 /// })?;
4342 /// # Ok(())
4343 /// # }
4344 /// ```
4345 pub fn prepare_request<Req: JsonRpcRequest>(
4346 &self,
4347 request: Req,
4348 ) -> PreparedRequest<Req::Response>
4349 where
4350 Counterpart: HasPeer<Counterpart>,
4351 {
4352 self.prepare_request_to(self.counterpart.clone(), request)
4353 }
4354
4355 /// Prepare a request to a specific peer without sending it.
4356 ///
4357 /// The request is serialized now. A consuming method synchronously registers
4358 /// its pending reply and enqueues the request; peer transformation and
4359 /// transmission happen later in the connection driver.
4360 /// See [`prepare_request`](Self::prepare_request).
4361 pub fn prepare_request_to<Peer: Role, Req: JsonRpcRequest>(
4362 &self,
4363 peer: Peer,
4364 request: Req,
4365 ) -> PreparedRequest<Req::Response>
4366 where
4367 Counterpart: HasPeer<Peer>,
4368 {
4369 self.prepare_request_to_with_options(peer, request, None, None)
4370 }
4371
4372 /// Send a request and run a synchronous side effect when its valid success
4373 /// response is routed, after `before_send` completes and independently
4374 /// from how the returned request is eventually consumed.
4375 #[cfg(feature = "unstable_protocol_v2")]
4376 pub(crate) fn send_request_to_with_response_hook_after<
4377 Peer: Role,
4378 Req: JsonRpcRequest,
4379 BeforeSend: Future<Output = Result<(), crate::Error>> + Send + 'static,
4380 >(
4381 &self,
4382 peer: Peer,
4383 request: Req,
4384 before_send: BeforeSend,
4385 response_hook: impl FnOnce(&Req::Response) -> Result<(), crate::Error> + Send + 'static,
4386 ) -> SentRequest<Req::Response>
4387 where
4388 Counterpart: HasPeer<Peer>,
4389 {
4390 let hook: ResponseRouteHook = Box::new(move |method, value| {
4391 let response = Req::Response::from_value(method, value.clone())?;
4392 response_hook(&response)
4393 });
4394 self.send_request_to_with_options(
4395 peer,
4396 request,
4397 false,
4398 Some(RequestReadiness::new(before_send)),
4399 Some(hook),
4400 )
4401 }
4402
4403 /// Send an ordered request with readiness and valid-success hooks.
4404 #[cfg(feature = "unstable_protocol_v2")]
4405 pub(crate) fn send_ordered_request_to_with_response_hook_after<
4406 Peer: Role,
4407 Req: JsonRpcRequest,
4408 BeforeSend: Future<Output = Result<(), crate::Error>> + Send + 'static,
4409 >(
4410 &self,
4411 peer: Peer,
4412 request: Req,
4413 before_send: BeforeSend,
4414 response_hook: impl FnOnce(&Req::Response) -> Result<(), crate::Error> + Send + 'static,
4415 ) -> SentRequest<Req::Response>
4416 where
4417 Counterpart: HasPeer<Peer>,
4418 {
4419 let hook: ResponseRouteHook = Box::new(move |method, value| {
4420 let response = Req::Response::from_value(method, value.clone())?;
4421 response_hook(&response)
4422 });
4423 self.send_request_to_with_options(
4424 peer,
4425 request,
4426 true,
4427 Some(RequestReadiness::new(before_send)),
4428 Some(hook),
4429 )
4430 }
4431
4432 /// Send a request whose callback must run before later inbound messages.
4433 ///
4434 /// The ordering marker is installed before the request enters the outgoing
4435 /// queue, closing the race between a fast peer response and the immediate
4436 /// [`SentRequest::on_receiving_result`] call. Callers must consume the
4437 /// returned request with a callback-style method without yielding.
4438 pub(crate) fn send_ordered_request_to<Peer: Role, Req: JsonRpcRequest>(
4439 &self,
4440 peer: Peer,
4441 request: Req,
4442 ) -> SentRequest<Req::Response>
4443 where
4444 Counterpart: HasPeer<Peer>,
4445 {
4446 self.send_request_to_with_options(peer, request, true, None, None)
4447 }
4448
4449 /// Send an ordered request after `before_send` completes successfully.
4450 ///
4451 /// The ordering marker and readiness prerequisite are both registered
4452 /// before the request enters the outgoing queue. This is used by framework
4453 /// setup paths that must acknowledge local routing before the peer can
4454 /// observe the request.
4455 pub(crate) fn send_ordered_request_to_after<
4456 Peer: Role,
4457 Req: JsonRpcRequest,
4458 BeforeSend: Future<Output = Result<(), crate::Error>> + Send + 'static,
4459 >(
4460 &self,
4461 peer: Peer,
4462 request: Req,
4463 before_send: BeforeSend,
4464 ) -> SentRequest<Req::Response>
4465 where
4466 Counterpart: HasPeer<Peer>,
4467 {
4468 self.send_request_to_with_options(
4469 peer,
4470 request,
4471 true,
4472 Some(RequestReadiness::new(before_send)),
4473 None,
4474 )
4475 }
4476
4477 fn send_request_to_with_options<Peer: Role, Req: JsonRpcRequest>(
4478 &self,
4479 peer: Peer,
4480 request: Req,
4481 ordered: bool,
4482 readiness: Option<RequestReadiness>,
4483 response_route_hook: Option<ResponseRouteHook>,
4484 ) -> SentRequest<Req::Response>
4485 where
4486 Counterpart: HasPeer<Peer>,
4487 {
4488 self.prepare_request_to_with_options(peer, request, readiness, response_route_hook)
4489 .into_sent_request(ordered)
4490 }
4491
4492 fn prepare_request_to_with_options<Peer: Role, Req: JsonRpcRequest>(
4493 &self,
4494 peer: Peer,
4495 request: Req,
4496 readiness: Option<RequestReadiness>,
4497 response_route_hook: Option<ResponseRouteHook>,
4498 ) -> PreparedRequest<Req::Response>
4499 where
4500 Counterpart: HasPeer<Peer>,
4501 {
4502 let method = request.method().to_string();
4503 let id = RequestId::Str(uuid::Uuid::new_v4().to_string());
4504 let (response_tx, response_rx) = oneshot::channel();
4505 let response_ordering = ResponseOrdering::default();
4506 let role_id = peer.role_id();
4507 let remote_style = self.counterpart.remote_style(peer);
4508 let cancellation =
4509 SentRequestCancellation::new(self.message_tx.clone(), remote_style, id.clone());
4510 cancellation.disarm();
4511 let pending_reply = PendingReply {
4512 method: method.clone(),
4513 role_id,
4514 sender: response_tx,
4515 cancellation_disarm: cancellation.disarm_handle(),
4516 ordering: response_ordering.clone(),
4517 response_route_hook,
4518 };
4519 let message = if self.is_incoming_closing() {
4520 Err(incoming_transport_closed_error(&method))
4521 } else {
4522 request
4523 .to_untyped_message()
4524 .map(|untyped| OutgoingMessage::Request {
4525 id: id.clone(),
4526 method: method.clone(),
4527 untyped,
4528 remote_style,
4529 readiness,
4530 })
4531 .map_err(|error| {
4532 crate::util::internal_error(format!(
4533 "failed to create untyped request for `{method}`: {error}"
4534 ))
4535 })
4536 };
4537 let sent = SentRequest::new(
4538 id,
4539 method.clone(),
4540 self.task_tx.clone(),
4541 response_rx,
4542 cancellation,
4543 response_ordering,
4544 )
4545 .map(move |json| <Req::Response>::from_value(&method, json));
4546 PreparedRequest {
4547 sent,
4548 publication: RequestPublication {
4549 message,
4550 pending_reply,
4551 message_tx: self.message_tx.clone(),
4552 pending_replies: self.pending_replies.clone(),
4553 incoming_closed: self.incoming_closed.clone(),
4554 },
4555 }
4556 }
4557
4558 /// Send an outgoing notification to the default counterpart peer (no reply expected).
4559 ///
4560 /// Notifications are fire-and-forget messages that don't have IDs and don't expect responses.
4561 /// This method sends the notification immediately and returns.
4562 ///
4563 /// This is a convenience method that sends to the counterpart role `R`.
4564 /// For explicit control over the target peer, use [`send_notification_to`](Self::send_notification_to).
4565 ///
4566 /// ```no_run
4567 /// # use agent_client_protocol_test::*;
4568 /// # async fn example(cx: agent_client_protocol::ConnectionTo<agent_client_protocol::Agent>) -> Result<(), agent_client_protocol::Error> {
4569 /// cx.send_notification(StatusUpdate {
4570 /// message: "Processing...".into(),
4571 /// })?;
4572 /// # Ok(())
4573 /// # }
4574 /// ```
4575 pub fn send_notification<N: JsonRpcNotification>(
4576 &self,
4577 notification: N,
4578 ) -> Result<(), crate::Error>
4579 where
4580 Counterpart: HasPeer<Counterpart>,
4581 {
4582 self.send_notification_to(self.counterpart.clone(), notification)
4583 }
4584
4585 /// Send an outgoing notification to a specific peer (no reply expected).
4586 ///
4587 /// The message will be transformed according to the [`HasPeer`](crate::role::HasPeer)
4588 /// implementation before being sent.
4589 pub fn send_notification_to<Peer: Role, N: JsonRpcNotification>(
4590 &self,
4591 peer: Peer,
4592 notification: N,
4593 ) -> Result<(), crate::Error>
4594 where
4595 Counterpart: HasPeer<Peer>,
4596 {
4597 let remote_style = self.counterpart.remote_style(peer);
4598 tracing::debug!(
4599 role = std::any::type_name::<Counterpart>(),
4600 peer = std::any::type_name::<Peer>(),
4601 notification_type = std::any::type_name::<N>(),
4602 ?remote_style,
4603 original_method = notification.method(),
4604 "send_notification_to"
4605 );
4606 let transformed = remote_style.transform_outgoing_message(notification)?;
4607 tracing::debug!(
4608 transformed_method = %transformed.method,
4609 "send_notification_to transformed"
4610 );
4611 send_raw_message(
4612 &self.message_tx,
4613 OutgoingMessage::Notification {
4614 untyped: transformed,
4615 },
4616 )
4617 }
4618
4619 /// Send a `$/cancel_request` notification for an arbitrary request ID to
4620 /// the default counterpart peer.
4621 ///
4622 /// Prefer [`SentRequest::cancel`] when you have the request handle: it
4623 /// already knows the correct peer, request ID, and proxy wrapping. Use this
4624 /// low-level method only when implementing custom routing with a request ID
4625 /// that is valid on this connection.
4626 pub fn send_cancel_request(
4627 &self,
4628 request_id: impl Into<crate::schema::v1::RequestId>,
4629 ) -> Result<(), crate::Error>
4630 where
4631 Counterpart: HasPeer<Counterpart>,
4632 {
4633 self.send_cancel_request_to(self.counterpart.clone(), request_id)
4634 }
4635
4636 /// Send a `$/cancel_request` notification for an arbitrary request ID to a
4637 /// specific peer.
4638 ///
4639 /// Prefer [`SentRequest::cancel`] when you have the request handle: it
4640 /// already knows the correct peer, request ID, and proxy wrapping. Use this
4641 /// low-level method only when implementing custom routing with a request ID
4642 /// that is valid on the target peer's connection.
4643 pub fn send_cancel_request_to<Peer: Role>(
4644 &self,
4645 peer: Peer,
4646 request_id: impl Into<crate::schema::v1::RequestId>,
4647 ) -> Result<(), crate::Error>
4648 where
4649 Counterpart: HasPeer<Peer>,
4650 {
4651 self.send_notification_to(
4652 peer,
4653 crate::schema::v1::CancelRequestNotification::new(request_id),
4654 )
4655 }
4656
4657 /// Register a dynamic message handler, used to intercept messages specific to a particular session
4658 /// or some similar modal thing.
4659 ///
4660 /// Dynamic message handlers run after the handlers registered on [`Builder`] and before the
4661 /// role's default handler. They receive messages that the builder handlers decline.
4662 ///
4663 /// The handler will stay registered until the returned registration guard is dropped.
4664 pub fn add_dynamic_handler(
4665 &self,
4666 handler: impl HandleDispatchFrom<Counterpart> + 'static,
4667 ) -> Result<DynamicHandlerGuard<Counterpart>, crate::Error> {
4668 let uuid = Uuid::new_v4();
4669 let active = Arc::new(AtomicBool::new(true));
4670 self.dynamic_handler_tx
4671 .unbounded_send(DynamicHandlerMessage::AddDynamicHandler(
4672 uuid,
4673 Box::new(GuardedDynamicHandler {
4674 active: active.clone(),
4675 handler,
4676 }),
4677 ))
4678 .map_err(crate::util::internal_error)?;
4679
4680 Ok(DynamicHandlerGuard::new(uuid, active, self.clone()))
4681 }
4682
4683 /// Wait until every dynamic-handler update queued before this call has
4684 /// been applied by the incoming protocol actor.
4685 pub(crate) fn dynamic_handler_barrier(&self) -> BoxFuture<'static, Result<(), crate::Error>> {
4686 let (acknowledgment_tx, acknowledgment_rx) = oneshot::channel();
4687 if let Err(error) =
4688 self.dynamic_handler_tx
4689 .unbounded_send(DynamicHandlerMessage::AcknowledgedBarrier(
4690 acknowledgment_tx,
4691 ))
4692 {
4693 return future::ready(Err(crate::Error::into_internal_error(error))).boxed();
4694 }
4695
4696 async move {
4697 acknowledgment_rx.await.map_err(|error| {
4698 crate::util::internal_error(format!(
4699 "dynamic-handler barrier was dropped before acknowledgment: {error}"
4700 ))
4701 })
4702 }
4703 .boxed()
4704 }
4705
4706 fn remove_dynamic_handler(&self, uuid: Uuid) {
4707 // Ignore errors
4708 drop(
4709 self.dynamic_handler_tx
4710 .unbounded_send(DynamicHandlerMessage::RemoveDynamicHandler(uuid)),
4711 );
4712 }
4713}
4714
4715struct GuardedDynamicHandler<Handler> {
4716 active: Arc<AtomicBool>,
4717 handler: Handler,
4718}
4719
4720impl<Counterpart, Handler> HandleDispatchFrom<Counterpart> for GuardedDynamicHandler<Handler>
4721where
4722 Counterpart: Role,
4723 Handler: HandleDispatchFrom<Counterpart>,
4724{
4725 async fn handle_dispatch_from(
4726 &mut self,
4727 message: Dispatch,
4728 connection: ConnectionTo<Counterpart>,
4729 ) -> Result<Handled<Dispatch>, crate::Error> {
4730 if !self.active.load(Ordering::Acquire) {
4731 return Ok(Handled::No {
4732 message,
4733 retry: false,
4734 });
4735 }
4736 self.handler.handle_dispatch_from(message, connection).await
4737 }
4738
4739 fn describe_chain(&self) -> impl Debug {
4740 self.handler.describe_chain()
4741 }
4742}
4743
4744/// A guard that keeps a dynamic message handler registered.
4745///
4746/// Dropping the guard immediately deactivates the handler and queues its
4747/// removal from the connection. Use [`detach`](Self::detach) to keep the
4748/// handler registered for the remaining lifetime of the connection.
4749#[must_use = "dropping this guard unregisters the dynamic handler"]
4750#[derive(Debug)]
4751pub struct DynamicHandlerGuard<R: Role> {
4752 uuid: Option<Uuid>,
4753 active: Arc<AtomicBool>,
4754 cx: ConnectionTo<R>,
4755 cleanup: Option<Arc<dyn DynamicHandlerCleanup>>,
4756}
4757
4758/// Private registration-local cleanup, independent of connection task admission.
4759pub(crate) trait DynamicHandlerCleanup: std::fmt::Debug + Send + Sync {
4760 fn close(&self);
4761 fn wait(&self) -> futures::future::BoxFuture<'static, ()>;
4762}
4763
4764impl<R: Role> DynamicHandlerGuard<R> {
4765 fn new(uuid: Uuid, active: Arc<AtomicBool>, cx: ConnectionTo<R>) -> Self {
4766 Self {
4767 uuid: Some(uuid),
4768 active,
4769 cx,
4770 cleanup: None,
4771 }
4772 }
4773
4774 #[cfg(feature = "unstable_mcp_over_acp")]
4775 pub(crate) fn with_cleanup(mut self, cleanup: Arc<dyn DynamicHandlerCleanup>) -> Self {
4776 self.cleanup = Some(cleanup);
4777 self
4778 }
4779
4780 pub(crate) fn cleanup(&self) -> Option<Arc<dyn DynamicHandlerCleanup>> {
4781 self.cleanup.clone()
4782 }
4783
4784 /// Keep the dynamic handler registered after this guard is dropped.
4785 ///
4786 /// The handler remains registered until the connection itself shuts down.
4787 /// Unlike leaking the guard, detaching does not retain an extra
4788 /// [`ConnectionTo`] handle.
4789 pub fn detach(mut self) {
4790 self.uuid = None;
4791 }
4792}
4793
4794impl<R: Role> Drop for DynamicHandlerGuard<R> {
4795 fn drop(&mut self) {
4796 if let Some(uuid) = self.uuid {
4797 self.active.store(false, Ordering::Release);
4798 if let Some(cleanup) = &self.cleanup {
4799 cleanup.close();
4800 }
4801 self.cx.remove_dynamic_handler(uuid);
4802 }
4803 }
4804}
4805
4806/// The context to respond to an incoming request.
4807///
4808/// This context is provided to request handlers and serves a dual role:
4809///
4810/// 1. **Respond to the request** - Use [`respond`](Self::respond) or
4811/// [`respond_with_result`](Self::respond_with_result) to send the response
4812/// 2. **Send other messages** - Use the [`ConnectionTo`] parameter passed to your
4813/// handler, which provides [`send_request`](`ConnectionTo::send_request`),
4814/// [`send_notification`](`ConnectionTo::send_notification`), and
4815/// [`spawn`](`ConnectionTo::spawn`)
4816///
4817/// # Example
4818///
4819/// ```no_run
4820/// # use agent_client_protocol_test::*;
4821/// # async fn example() -> Result<(), agent_client_protocol::Error> {
4822/// # let connection = mock_connection();
4823/// connection.on_receive_request(async |req: ProcessRequest, responder, cx| {
4824/// // Send a notification while processing
4825/// cx.send_notification(StatusUpdate {
4826/// message: "processing".into(),
4827/// })?;
4828///
4829/// // Do some work...
4830/// let result = process(&req.data)?;
4831///
4832/// // Respond to the request
4833/// responder.respond(ProcessResponse { result })
4834/// }, agent_client_protocol::on_receive_request!())
4835/// # .connect_to(agent_client_protocol_test::MockTransport).await?;
4836/// # Ok(())
4837/// # }
4838/// ```
4839///
4840/// # Event Loop Considerations
4841///
4842/// Like all handlers, request handlers run on the event loop. Use
4843/// [`spawn`](ConnectionTo::spawn) for expensive operations to avoid blocking
4844/// the connection.
4845///
4846/// See the [Event Loop and Concurrency](Builder#event-loop-and-concurrency)
4847/// section for more details.
4848///
4849/// # Drop behavior
4850///
4851/// Dropping a responder for a request that arrived in a batch completes that
4852/// slot with an Internal Error, so one abandoned request cannot withhold valid
4853/// sibling responses forever. A responder for an individual request retains
4854/// the historical behavior: dropping it does not automatically send a reply.
4855#[must_use]
4856pub struct Responder<T: JsonRpcResponse = serde_json::Value> {
4857 /// The method of the request.
4858 method: String,
4859
4860 /// The `id` of the message we are replying to.
4861 id: RequestId,
4862
4863 /// Request-local cancellation state.
4864 cancellation: ResponderCancellation,
4865
4866 /// Whether this response is emitted on its own or collected into a batch.
4867 destination: ResponseDestination,
4868
4869 /// Function to send the response to its destination.
4870 ///
4871 /// For incoming requests: serializes to JSON and sends over the wire.
4872 /// For incoming responses: sends to the waiting oneshot channel.
4873 send_fn: Box<dyn FnOnce(Result<T, crate::Error>) -> Result<(), crate::Error> + Send>,
4874
4875 /// Completes an abandoned batch slot unless an explicit response disarms it.
4876 drop_guard: ResponderDropGuard,
4877}
4878
4879struct ResponderDropGuard {
4880 message_tx: OutgoingMessageTx,
4881 id: RequestId,
4882 method: String,
4883 destination: ResponseDestination,
4884 armed: bool,
4885}
4886
4887impl ResponderDropGuard {
4888 fn disarm(&mut self) {
4889 self.armed = false;
4890 }
4891}
4892
4893impl Drop for ResponderDropGuard {
4894 fn drop(&mut self) {
4895 if !self.armed || !self.destination.is_batch() {
4896 return;
4897 }
4898
4899 if let Err(error) = send_raw_message(
4900 &self.message_tx,
4901 OutgoingMessage::AbandonedBatchResponse {
4902 id: self.id.clone(),
4903 method: self.method.clone(),
4904 destination: self.destination.clone(),
4905 },
4906 ) {
4907 tracing::debug!(
4908 id = ?self.id,
4909 method = %self.method,
4910 ?error,
4911 "could not complete abandoned JSON-RPC batch response slot"
4912 );
4913 }
4914 }
4915}
4916
4917impl<T: JsonRpcResponse> std::fmt::Debug for Responder<T> {
4918 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4919 f.debug_struct("Responder")
4920 .field("method", &self.method)
4921 .field("id", &self.id)
4922 .field("response_type", &std::any::type_name::<T>())
4923 .finish_non_exhaustive()
4924 }
4925}
4926
4927impl Responder<serde_json::Value> {
4928 /// Create a new request context for an incoming request.
4929 ///
4930 /// The response will be serialized to JSON and sent over the wire.
4931 fn new(
4932 message_tx: OutgoingMessageTx,
4933 method: String,
4934 id: RequestId,
4935 cancellation_registry: &RequestCancellationRegistry,
4936 destination: ResponseDestination,
4937 ) -> Self {
4938 let id_clone = id.clone();
4939 let method_clone = method.clone();
4940 let cancellation = cancellation_registry.register(&id);
4941 let send_destination = destination.clone();
4942 let drop_guard = ResponderDropGuard {
4943 message_tx: message_tx.clone(),
4944 id: id.clone(),
4945 method: method.clone(),
4946 destination: destination.clone(),
4947 armed: true,
4948 };
4949 Self {
4950 method,
4951 id,
4952 cancellation,
4953 destination,
4954 send_fn: Box::new(move |response: Result<serde_json::Value, crate::Error>| {
4955 send_raw_message(
4956 &message_tx,
4957 OutgoingMessage::Response {
4958 id: id_clone,
4959 method: method_clone,
4960 response,
4961 destination: send_destination,
4962 },
4963 )
4964 }),
4965 drop_guard,
4966 }
4967 }
4968
4969 /// Cast this request context to a different response type.
4970 ///
4971 /// The provided type `T` will be serialized to JSON before sending.
4972 pub fn cast<T: JsonRpcResponse>(self) -> Responder<T> {
4973 self.wrap_params(move |method, value| match value {
4974 Ok(value) => T::into_json(value, method),
4975 Err(e) => Err(e),
4976 })
4977 }
4978}
4979
4980impl<T: JsonRpcResponse> Responder<T> {
4981 /// Method of the incoming request
4982 #[must_use]
4983 pub fn method(&self) -> &str {
4984 &self.method
4985 }
4986
4987 /// ID of the incoming request.
4988 #[must_use]
4989 pub fn id(&self) -> &RequestId {
4990 &self.id
4991 }
4992
4993 /// Returns the cancellation marker for this request.
4994 ///
4995 /// The marker is set when the peer sends `$/cancel_request` for this
4996 /// request's JSON-RPC ID. Cancellation is cooperative: handlers should use
4997 /// the marker to stop long-running work and then decide whether to respond
4998 /// with [`Error::request_cancelled`] or partial data.
4999 ///
5000 /// [`Error::request_cancelled`]: crate::Error::request_cancelled
5001 #[must_use]
5002 pub fn cancellation(&self) -> RequestCancellation {
5003 self.cancellation.cancellation()
5004 }
5005
5006 /// Convert to a `Responder` that expects a JSON value
5007 /// and which checks (dynamically) that the JSON value it receives
5008 /// can be converted to `T`.
5009 pub fn erase_to_json(self) -> Responder<serde_json::Value> {
5010 self.wrap_params(|method, value| T::from_value(method, value?))
5011 }
5012
5013 /// Return a new Responder with a different method name.
5014 pub fn wrap_method(mut self, method: String) -> Responder<T> {
5015 self.drop_guard.method.clone_from(&method);
5016 Responder {
5017 method,
5018 id: self.id,
5019 cancellation: self.cancellation,
5020 destination: self.destination,
5021 send_fn: self.send_fn,
5022 drop_guard: self.drop_guard,
5023 }
5024 }
5025
5026 /// Return a new Responder that expects a response of type U.
5027 ///
5028 /// `wrap_fn` will be invoked with the method name and the result to transform
5029 /// type `U` into type `T` before sending.
5030 pub fn wrap_params<U: JsonRpcResponse>(
5031 self,
5032 wrap_fn: impl FnOnce(&str, Result<U, crate::Error>) -> Result<T, crate::Error> + Send + 'static,
5033 ) -> Responder<U> {
5034 let method = self.method.clone();
5035 Responder {
5036 method: self.method,
5037 id: self.id,
5038 cancellation: self.cancellation,
5039 destination: self.destination,
5040 send_fn: Box::new(move |input: Result<U, crate::Error>| {
5041 let t_value = wrap_fn(&method, input);
5042 (self.send_fn)(t_value)
5043 }),
5044 drop_guard: self.drop_guard,
5045 }
5046 }
5047
5048 /// Respond to the JSON-RPC request with either a value (`Ok`) or an error (`Err`).
5049 pub fn respond_with_result(
5050 mut self,
5051 response: Result<T, crate::Error>,
5052 ) -> Result<(), crate::Error> {
5053 tracing::debug!(id = ?self.id, "respond called");
5054 self.drop_guard.disarm();
5055 (self.send_fn)(response)
5056 }
5057
5058 /// Respond to the JSON-RPC request with a value.
5059 pub fn respond(self, response: T) -> Result<(), crate::Error> {
5060 self.respond_with_result(Ok(response))
5061 }
5062
5063 /// Respond to the JSON-RPC request with an internal error containing a message.
5064 pub fn respond_with_internal_error(self, message: impl ToString) -> Result<(), crate::Error> {
5065 self.respond_with_error(crate::util::internal_error(message))
5066 }
5067
5068 /// Respond to the JSON-RPC request with an error.
5069 pub fn respond_with_error(self, error: crate::Error) -> Result<(), crate::Error> {
5070 tracing::debug!(id = ?self.id, ?error, "respond_with_error called");
5071 self.respond_with_result(Err(error))
5072 }
5073
5074 fn reply_target(&self) -> RequestReplyTarget {
5075 RequestReplyTarget {
5076 id: self.id.clone(),
5077 method: self.method.clone(),
5078 destination: self.destination.clone(),
5079 }
5080 }
5081}
5082
5083/// Context for handling an incoming JSON-RPC response.
5084///
5085/// This is the response-side counterpart to [`Responder`]. While `Responder` handles
5086/// incoming requests (where you send a response over the wire), `ResponseRouter` handles
5087/// incoming responses (where you route the response to a local task waiting for it).
5088///
5089/// Both are fundamentally "sinks" that push the message through a `send_fn`, but they
5090/// represent different points in the message lifecycle and carry different metadata.
5091///
5092/// # Drop Behavior
5093///
5094/// Dropping a `ResponseRouter` without routing the response (for example, from a
5095/// dispatch handler that claims a [`Dispatch::Response`]) discards the
5096/// response: the local awaiter observes the response as never received. The
5097/// request still counts as settled: routing a response this far disarms the
5098/// originating [`SentRequest`]'s drop-time auto-cancellation even if the router
5099/// is never invoked, since the peer has already answered.
5100#[must_use]
5101pub struct ResponseRouter<T: JsonRpcResponse = serde_json::Value> {
5102 /// The method of the original request.
5103 method: String,
5104
5105 /// The `id` of the original request.
5106 id: RequestId,
5107
5108 /// The RoleId to which the original request was sent
5109 /// (and hence from which the reply is expected).
5110 role_id: RoleId,
5111
5112 /// Function to send the response to the waiting task.
5113 send_fn: Box<dyn FnOnce(Result<T, crate::Error>) -> Result<(), crate::Error> + Send>,
5114
5115 /// Shared route used to deliver a dispatch-handler error to the same waiter.
5116 reply_target: ResponseReplyTarget,
5117}
5118
5119impl<T: JsonRpcResponse> std::fmt::Debug for ResponseRouter<T> {
5120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5121 f.debug_struct("ResponseRouter")
5122 .field("method", &self.method)
5123 .field("id", &self.id)
5124 .field("response_type", &std::any::type_name::<T>())
5125 .finish_non_exhaustive()
5126 }
5127}
5128
5129impl ResponseRouter<serde_json::Value> {
5130 /// Create a new response context for routing a response to a local awaiter.
5131 ///
5132 /// When [`route_with_result`](Self::route_with_result) is called, the response is sent through the oneshot
5133 /// channel to the code that originally sent the request. If that receiver was
5134 /// dropped, the response is discarded because there is no local awaiter left.
5135 fn new(id: RequestId, pending_reply: PendingReply, dispatch: ResponseDispatch) -> Self {
5136 let PendingReply {
5137 method,
5138 role_id,
5139 sender,
5140 cancellation_disarm,
5141 ordering,
5142 response_route_hook,
5143 } = pending_reply;
5144 let reply_target = ResponseReplyTarget {
5145 id: id.clone(),
5146 method: method.clone(),
5147 sender: Arc::new(Mutex::new(Some(sender))),
5148 ordering,
5149 dispatch,
5150 };
5151 let send_target = reply_target.clone();
5152 // A response for the request reached this router, so the request is
5153 // settled from the peer's perspective and a `$/cancel_request` could
5154 // only ever be redundant. Disarm immediately so handlers may retain
5155 // the router without leaving auto-cancellation armed.
5156 cancellation_disarm.disarm();
5157 let hook_method = method.clone();
5158 Self {
5159 method,
5160 id,
5161 role_id,
5162 send_fn: Box::new(move |response: Result<serde_json::Value, crate::Error>| {
5163 let response = match response {
5164 Ok(value) => match response_route_hook {
5165 Some(hook) => hook(&hook_method, &value).map(|()| value),
5166 None => Ok(value),
5167 },
5168 Err(error) => Err(error),
5169 };
5170 send_target.route(response);
5171 Ok(())
5172 }),
5173 reply_target,
5174 }
5175 }
5176
5177 /// Cast this response context to a different response type.
5178 ///
5179 /// The provided type `T` will be serialized to JSON before sending.
5180 pub fn cast<T: JsonRpcResponse>(self) -> ResponseRouter<T> {
5181 self.wrap_params(move |method, value| match value {
5182 Ok(value) => T::into_json(value, method),
5183 Err(e) => Err(e),
5184 })
5185 }
5186}
5187
5188impl<T: JsonRpcResponse> ResponseRouter<T> {
5189 /// Method of the original request
5190 #[must_use]
5191 pub fn method(&self) -> &str {
5192 &self.method
5193 }
5194
5195 /// ID of the original request.
5196 #[must_use]
5197 pub fn id(&self) -> &RequestId {
5198 &self.id
5199 }
5200
5201 /// The peer to which the original request was sent.
5202 ///
5203 /// This is the peer from which we expect to receive the response.
5204 #[must_use]
5205 pub fn role_id(&self) -> RoleId {
5206 self.role_id.clone()
5207 }
5208
5209 /// Convert to a `ResponseRouter` that expects a JSON value
5210 /// and which checks (dynamically) that the JSON value it receives
5211 /// can be converted to `T`.
5212 pub fn erase_to_json(self) -> ResponseRouter<serde_json::Value> {
5213 self.wrap_params(|method, value| T::from_value(method, value?))
5214 }
5215
5216 /// Return a new ResponseRouter that expects a response of type U.
5217 ///
5218 /// `wrap_fn` will be invoked with the method name and the result to transform
5219 /// type `U` into type `T` before sending.
5220 fn wrap_params<U: JsonRpcResponse>(
5221 self,
5222 wrap_fn: impl FnOnce(&str, Result<U, crate::Error>) -> Result<T, crate::Error> + Send + 'static,
5223 ) -> ResponseRouter<U> {
5224 let method = self.method.clone();
5225 ResponseRouter {
5226 method: self.method,
5227 id: self.id,
5228 role_id: self.role_id,
5229 send_fn: Box::new(move |input: Result<U, crate::Error>| {
5230 let t_value = wrap_fn(&method, input);
5231 (self.send_fn)(t_value)
5232 }),
5233 reply_target: self.reply_target,
5234 }
5235 }
5236
5237 /// Route the response result to the waiting task.
5238 pub fn route_with_result(self, response: Result<T, crate::Error>) -> Result<(), crate::Error> {
5239 tracing::debug!(id = ?self.id, "response routed to awaiter");
5240 (self.send_fn)(response)
5241 }
5242
5243 /// Route a successful response value to the waiting task.
5244 pub fn route(self, response: T) -> Result<(), crate::Error> {
5245 self.route_with_result(Ok(response))
5246 }
5247
5248 /// Route an internal error to the waiting task.
5249 pub fn route_with_internal_error(self, message: impl ToString) -> Result<(), crate::Error> {
5250 self.route_with_error(crate::util::internal_error(message))
5251 }
5252
5253 /// Route an error response to the waiting task.
5254 pub fn route_with_error(self, error: crate::Error) -> Result<(), crate::Error> {
5255 tracing::debug!(id = ?self.id, ?error, "error routed to awaiter");
5256 self.route_with_result(Err(error))
5257 }
5258}
5259
5260/// Common bounds for any JSON-RPC message.
5261///
5262/// # Derive Macro
5263///
5264/// For simple message types, you can use the `JsonRpcRequest` or `JsonRpcNotification` derive macros
5265/// which will implement both `JsonRpcMessage` and the respective trait. See [`JsonRpcRequest`] and
5266/// [`JsonRpcNotification`] for examples.
5267pub trait JsonRpcMessage: 'static + Debug + Sized + Send + Clone {
5268 /// Check if this message type matches the given method name.
5269 fn matches_method(method: &str) -> bool;
5270
5271 /// The method name for the message.
5272 fn method(&self) -> &str;
5273
5274 /// Convert this message into an untyped message.
5275 fn to_untyped_message(&self) -> Result<UntypedMessage, crate::Error>;
5276
5277 /// Parse this type from a method name and parameters.
5278 ///
5279 /// Returns an error if the method doesn't match or deserialization fails.
5280 /// Callers should use `matches_method` first to check if this type handles the method.
5281 fn parse_message(method: &str, params: &impl Serialize) -> Result<Self, crate::Error>;
5282}
5283
5284/// Defines the "payload" of a successful response to a JSON-RPC request.
5285///
5286/// # Derive Macro
5287///
5288/// Use `#[derive(JsonRpcResponse)]` to automatically implement this trait:
5289///
5290/// ```ignore
5291/// use agent_client_protocol::JsonRpcResponse;
5292/// use serde::{Serialize, Deserialize};
5293///
5294/// #[derive(Debug, Clone, Serialize, Deserialize, JsonRpcResponse)]
5295/// struct HelloResponse {
5296/// greeting: String,
5297/// }
5298/// ```
5299pub trait JsonRpcResponse: 'static + Debug + Sized + Send + Clone {
5300 /// Convert this message into a JSON value.
5301 fn into_json(self, method: &str) -> Result<serde_json::Value, crate::Error>;
5302
5303 /// Parse a JSON value into the response type.
5304 fn from_value(method: &str, value: serde_json::Value) -> Result<Self, crate::Error>;
5305}
5306
5307impl JsonRpcResponse for serde_json::Value {
5308 fn from_value(_method: &str, value: serde_json::Value) -> Result<Self, crate::Error> {
5309 Ok(value)
5310 }
5311
5312 fn into_json(self, _method: &str) -> Result<serde_json::Value, crate::Error> {
5313 Ok(self)
5314 }
5315}
5316
5317/// A struct that represents a notification (JSON-RPC message that does not expect a response).
5318///
5319/// # Derive Macro
5320///
5321/// Use `#[derive(JsonRpcNotification)]` to automatically implement both `JsonRpcMessage` and `JsonRpcNotification`:
5322///
5323/// ```ignore
5324/// use agent_client_protocol::JsonRpcNotification;
5325/// use serde::{Serialize, Deserialize};
5326///
5327/// #[derive(Debug, Clone, Serialize, Deserialize, JsonRpcNotification)]
5328/// #[notification(method = "_ping")]
5329/// struct PingNotification {
5330/// timestamp: u64,
5331/// }
5332/// ```
5333pub trait JsonRpcNotification: JsonRpcMessage {}
5334
5335/// A struct that represents a request (JSON-RPC message expecting a response).
5336///
5337/// # Derive Macro
5338///
5339/// Use `#[derive(JsonRpcRequest)]` to automatically implement both `JsonRpcMessage` and `JsonRpcRequest`:
5340///
5341/// ```ignore
5342/// use agent_client_protocol::{JsonRpcRequest, JsonRpcResponse};
5343/// use serde::{Serialize, Deserialize};
5344///
5345/// #[derive(Debug, Clone, Serialize, Deserialize, JsonRpcRequest)]
5346/// #[request(method = "_hello", response = HelloResponse)]
5347/// struct HelloRequest {
5348/// name: String,
5349/// }
5350///
5351/// #[derive(Debug, Clone, Serialize, Deserialize, JsonRpcResponse)]
5352/// struct HelloResponse {
5353/// greeting: String,
5354/// }
5355/// ```
5356pub trait JsonRpcRequest: JsonRpcMessage {
5357 /// The type of data expected in response.
5358 type Response: JsonRpcResponse;
5359}
5360
5361/// An incoming request, notification, or response being dispatched through handlers.
5362/// Requests include the context used to answer them; responses include the context
5363/// used to route them to the local requester.
5364///
5365/// Type parameters allow specifying the concrete request and notification types.
5366/// By default, both are `UntypedMessage` for dynamic dispatch.
5367/// The request context's response type matches the request's response type.
5368#[derive(Debug)]
5369pub enum Dispatch<Req: JsonRpcRequest = UntypedMessage, Notif: JsonRpcNotification = UntypedMessage>
5370{
5371 /// Incoming request and the context where the response should be sent.
5372 Request(Req, Responder<Req::Response>),
5373
5374 /// Incoming notification.
5375 Notification(Notif),
5376
5377 /// Incoming response to a request we sent.
5378 ///
5379 /// The first field is the response result (success or error from the remote).
5380 /// The second field is the context for forwarding the response to its destination
5381 /// (typically a waiting oneshot channel).
5382 Response(
5383 Result<Req::Response, crate::Error>,
5384 ResponseRouter<Req::Response>,
5385 ),
5386}
5387
5388impl<Req: JsonRpcRequest, Notif: JsonRpcNotification> Dispatch<Req, Notif> {
5389 /// Map the request and notification types to new types.
5390 ///
5391 /// Note: Response variants are passed through unchanged since they don't
5392 /// contain a parseable message payload.
5393 pub fn map<Req1, Notif1>(
5394 self,
5395 map_request: impl FnOnce(Req, Responder<Req::Response>) -> (Req1, Responder<Req1::Response>),
5396 map_notification: impl FnOnce(Notif) -> Notif1,
5397 ) -> Dispatch<Req1, Notif1>
5398 where
5399 Req1: JsonRpcRequest<Response = Req::Response>,
5400 Notif1: JsonRpcNotification,
5401 {
5402 match self {
5403 Dispatch::Request(request, responder) => {
5404 let (new_request, new_responder) = map_request(request, responder);
5405 Dispatch::Request(new_request, new_responder)
5406 }
5407 Dispatch::Notification(notification) => {
5408 let new_notification = map_notification(notification);
5409 Dispatch::Notification(new_notification)
5410 }
5411 Dispatch::Response(result, router) => Dispatch::Response(result, router),
5412 }
5413 }
5414
5415 /// Convert the message in self to an untyped message.
5416 ///
5417 /// Note: Response variants don't have an untyped message representation.
5418 /// This returns an error for Response variants.
5419 pub fn to_untyped_message(&self) -> Result<UntypedMessage, crate::Error> {
5420 match self {
5421 Dispatch::Request(request, _) => request.to_untyped_message(),
5422 Dispatch::Notification(notification) => notification.to_untyped_message(),
5423 Dispatch::Response(_, _) => Err(crate::util::internal_error(
5424 "Response variant has no untyped message representation",
5425 )),
5426 }
5427 }
5428
5429 /// Convert self to an untyped message context.
5430 ///
5431 /// Note: Response variants cannot be converted. This returns an error for Response variants.
5432 pub fn into_untyped_dispatch(self) -> Result<Dispatch, crate::Error> {
5433 match self {
5434 Dispatch::Request(request, responder) => Ok(Dispatch::Request(
5435 request.to_untyped_message()?,
5436 responder.erase_to_json(),
5437 )),
5438 Dispatch::Notification(notification) => {
5439 Ok(Dispatch::Notification(notification.to_untyped_message()?))
5440 }
5441 Dispatch::Response(_, _) => Err(crate::util::internal_error(
5442 "cannot convert Response variant to untyped message context",
5443 )),
5444 }
5445 }
5446
5447 /// Returns the request ID if this is a request or response, None if notification.
5448 pub fn id(&self) -> Option<&RequestId> {
5449 match self {
5450 Dispatch::Request(_, cx) => Some(cx.id()),
5451 Dispatch::Notification(_) => None,
5452 Dispatch::Response(_, cx) => Some(cx.id()),
5453 }
5454 }
5455
5456 fn handler_error_target(&self) -> Option<HandlerErrorTarget> {
5457 match self {
5458 Dispatch::Request(_, responder) => {
5459 Some(HandlerErrorTarget::Request(responder.reply_target()))
5460 }
5461 Dispatch::Notification(_) => None,
5462 Dispatch::Response(_, router) => {
5463 Some(HandlerErrorTarget::Response(router.reply_target.clone()))
5464 }
5465 }
5466 }
5467
5468 /// Returns the method of the message.
5469 ///
5470 /// For requests and notifications, this is the method from the message payload.
5471 /// For responses, this is the method of the original request.
5472 pub fn method(&self) -> &str {
5473 match self {
5474 Dispatch::Request(msg, _) => msg.method(),
5475 Dispatch::Notification(msg) => msg.method(),
5476 Dispatch::Response(_, cx) => cx.method(),
5477 }
5478 }
5479}
5480
5481impl Dispatch {
5482 /// Attempts to parse `self` into a typed message context.
5483 ///
5484 /// # Returns
5485 ///
5486 /// * `Ok(Ok(typed))` if this dispatch matches the requested type for its variant
5487 /// * `Ok(Err(self))` if it does not match the requested type for its variant
5488 /// * `Err` if its method matches the requested type but parsing fails
5489 #[tracing::instrument(skip(self), fields(Request = ?std::any::type_name::<Req>(), Notif = ?std::any::type_name::<Notif>()), level = "trace", ret)]
5490 pub(crate) fn into_typed_dispatch<Req: JsonRpcRequest, Notif: JsonRpcNotification>(
5491 self,
5492 ) -> Result<Result<Dispatch<Req, Notif>, Dispatch>, crate::Error> {
5493 tracing::debug!(
5494 message = ?self,
5495 "into_typed_dispatch"
5496 );
5497 match self {
5498 Dispatch::Request(message, responder) => {
5499 if Req::matches_method(&message.method) {
5500 match Req::parse_message(&message.method, &message.params) {
5501 Ok(req) => {
5502 tracing::trace!(?req, "parsed ok");
5503 Ok(Ok(Dispatch::Request(req, responder.cast())))
5504 }
5505 Err(err) => {
5506 tracing::trace!(?err, "parse error");
5507 Err(err)
5508 }
5509 }
5510 } else {
5511 tracing::trace!("method doesn't match");
5512 Ok(Err(Dispatch::Request(message, responder)))
5513 }
5514 }
5515
5516 Dispatch::Notification(message) => {
5517 if Notif::matches_method(&message.method) {
5518 match Notif::parse_message(&message.method, &message.params) {
5519 Ok(notif) => {
5520 tracing::trace!(?notif, "parse ok");
5521 Ok(Ok(Dispatch::Notification(notif)))
5522 }
5523 Err(err) => {
5524 tracing::trace!(?err, "parse error");
5525 Err(err)
5526 }
5527 }
5528 } else {
5529 tracing::trace!("method doesn't match");
5530 Ok(Err(Dispatch::Notification(message)))
5531 }
5532 }
5533
5534 Dispatch::Response(result, cx) => {
5535 let method = cx.method();
5536 if Req::matches_method(method) {
5537 // Parse the response result
5538 let typed_result = match result {
5539 Ok(value) => {
5540 match <Req::Response as JsonRpcResponse>::from_value(method, value) {
5541 Ok(parsed) => {
5542 tracing::trace!(?parsed, "parse ok");
5543 Ok(parsed)
5544 }
5545 Err(err) => {
5546 tracing::trace!(?err, "parse error");
5547 return Err(err);
5548 }
5549 }
5550 }
5551 Err(err) => {
5552 tracing::trace!("error, passthrough");
5553 Err(err)
5554 }
5555 };
5556 Ok(Ok(Dispatch::Response(typed_result, cx.cast())))
5557 } else {
5558 tracing::trace!("method doesn't match");
5559 Ok(Err(Dispatch::Response(result, cx)))
5560 }
5561 }
5562 }
5563 }
5564
5565 /// True if this message has a field with the given name.
5566 ///
5567 /// Returns `false` for Response variants.
5568 #[must_use]
5569 pub fn has_field(&self, field_name: &str) -> bool {
5570 self.message()
5571 .and_then(|m| m.params().get(field_name))
5572 .is_some()
5573 }
5574
5575 /// Returns true if this message has a session-id field.
5576 ///
5577 /// Returns `false` for Response variants.
5578 pub(crate) fn has_session_id(&self) -> bool {
5579 self.has_field("sessionId")
5580 }
5581
5582 /// Extract the ACP session-id from this message (if any).
5583 ///
5584 /// Returns `Ok(None)` for Response variants.
5585 pub(crate) fn get_session_id(&self) -> Result<Option<SessionId>, crate::Error> {
5586 let Some(message) = self.message() else {
5587 return Ok(None);
5588 };
5589 let Some(value) = message.params().get("sessionId") else {
5590 return Ok(None);
5591 };
5592 let session_id = serde_json::from_value(value.clone())?;
5593 Ok(Some(session_id))
5594 }
5595
5596 /// Try to parse this as a notification of the given type.
5597 ///
5598 /// # Returns
5599 ///
5600 /// * `Ok(Ok(typed))` if this is a notification of the requested type
5601 /// * `Ok(Err(self))` if this is not a matching notification
5602 /// * `Err` if its method matches the requested type but parsing fails
5603 pub fn into_notification<N: JsonRpcNotification>(
5604 self,
5605 ) -> Result<Result<N, Dispatch>, crate::Error> {
5606 match self {
5607 Dispatch::Notification(msg) => {
5608 if !N::matches_method(&msg.method) {
5609 return Ok(Err(Dispatch::Notification(msg)));
5610 }
5611 match N::parse_message(&msg.method, &msg.params) {
5612 Ok(n) => Ok(Ok(n)),
5613 Err(err) => Err(err),
5614 }
5615 }
5616 Dispatch::Request(..) | Dispatch::Response(..) => Ok(Err(self)),
5617 }
5618 }
5619
5620 /// Try to parse this as a request of the given type.
5621 ///
5622 /// # Returns
5623 ///
5624 /// * `Ok(Ok(typed))` if this is a request of the requested type
5625 /// * `Ok(Err(self))` if this is not a matching request
5626 /// * `Err` if its method matches the requested type but parsing fails
5627 pub fn into_request<Req: JsonRpcRequest>(
5628 self,
5629 ) -> Result<Result<(Req, Responder<Req::Response>), Dispatch>, crate::Error> {
5630 match self {
5631 Dispatch::Request(msg, responder) => {
5632 if !Req::matches_method(&msg.method) {
5633 return Ok(Err(Dispatch::Request(msg, responder)));
5634 }
5635 match Req::parse_message(&msg.method, &msg.params) {
5636 Ok(req) => Ok(Ok((req, responder.cast()))),
5637 Err(err) => Err(err),
5638 }
5639 }
5640 Dispatch::Notification(..) | Dispatch::Response(..) => Ok(Err(self)),
5641 }
5642 }
5643}
5644
5645impl<M: JsonRpcRequest + JsonRpcNotification> Dispatch<M, M> {
5646 /// Returns the message payload for requests and notifications.
5647 ///
5648 /// Returns `None` for Response variants since they don't contain a message payload.
5649 pub fn message(&self) -> Option<&M> {
5650 match self {
5651 Dispatch::Request(msg, _) | Dispatch::Notification(msg) => Some(msg),
5652 Dispatch::Response(_, _) => None,
5653 }
5654 }
5655
5656 /// Map the request/notification message.
5657 ///
5658 /// Response variants pass through unchanged.
5659 pub(crate) fn try_map_message(
5660 self,
5661 map_message: impl FnOnce(M) -> Result<M, crate::Error>,
5662 ) -> Result<Dispatch<M, M>, crate::Error> {
5663 match self {
5664 Dispatch::Request(request, cx) => Ok(Dispatch::Request(map_message(request)?, cx)),
5665 Dispatch::Notification(notification) => {
5666 Ok(Dispatch::<M, M>::Notification(map_message(notification)?))
5667 }
5668 Dispatch::Response(result, cx) => Ok(Dispatch::Response(result, cx)),
5669 }
5670 }
5671}
5672
5673/// An incoming JSON message without any typing. Can be a request or a notification.
5674#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5675pub struct UntypedMessage {
5676 /// The JSON-RPC method name
5677 pub method: String,
5678 /// The JSON-RPC parameters as a raw JSON value
5679 pub params: serde_json::Value,
5680}
5681
5682impl UntypedMessage {
5683 /// Returns an untyped message with the given method and parameters.
5684 pub fn new(method: &str, params: impl Serialize) -> Result<Self, crate::Error> {
5685 let params = serde_json::to_value(params)?;
5686 Ok(Self {
5687 method: method.to_string(),
5688 params,
5689 })
5690 }
5691
5692 /// Returns the method name
5693 #[must_use]
5694 pub fn method(&self) -> &str {
5695 &self.method
5696 }
5697
5698 /// Returns the parameters as a JSON value
5699 #[must_use]
5700 pub fn params(&self) -> &serde_json::Value {
5701 &self.params
5702 }
5703
5704 /// Consumes this message and returns the method and params
5705 #[must_use]
5706 pub fn into_parts(self) -> (String, serde_json::Value) {
5707 (self.method, self.params)
5708 }
5709
5710 /// Convert `self` to a raw JSON-RPC message.
5711 pub(crate) fn into_raw_jsonrpc_message(
5712 self,
5713 id: Option<RequestId>,
5714 ) -> Result<RawJsonRpcMessage, crate::Error> {
5715 let Self { method, params } = self;
5716 match id {
5717 Some(id) => RawJsonRpcMessage::request(method, params, id),
5718 None => RawJsonRpcMessage::notification(method, params),
5719 }
5720 }
5721}
5722
5723impl JsonRpcMessage for UntypedMessage {
5724 fn matches_method(_method: &str) -> bool {
5725 // UntypedMessage matches any method - it's the untyped fallback
5726 true
5727 }
5728
5729 fn method(&self) -> &str {
5730 &self.method
5731 }
5732
5733 fn to_untyped_message(&self) -> Result<UntypedMessage, crate::Error> {
5734 Ok(self.clone())
5735 }
5736
5737 fn parse_message(method: &str, params: &impl Serialize) -> Result<Self, crate::Error> {
5738 UntypedMessage::new(method, params)
5739 }
5740}
5741
5742impl JsonRpcRequest for UntypedMessage {
5743 type Response = serde_json::Value;
5744}
5745
5746impl JsonRpcNotification for UntypedMessage {}
5747
5748/// Represents a pending response of type `R` from an outgoing request.
5749///
5750/// Returned by [`ConnectionTo::send_request`], this type provides explicit response-consumption
5751/// modes. The API is intentionally designed to make it difficult to accidentally wait for a
5752/// response inside the dispatch loop.
5753///
5754/// # Anti-Footgun Design
5755///
5756/// You cannot directly `.await` a `SentRequest`. Instead, you must choose how to handle
5757/// the response:
5758///
5759/// ## Option 1: Register an Ordered Callback (Safe in Handlers)
5760///
5761/// Calling [`on_receiving_result`](Self::on_receiving_result) registers the callback and returns
5762/// immediately. When ordered consumption is selected before the response is routed during its
5763/// original dispatch, the loop waits for the callback to complete before processing the next
5764/// message:
5765///
5766/// ```no_run
5767/// # use agent_client_protocol_test::*;
5768/// # async fn example(cx: agent_client_protocol::ConnectionTo<agent_client_protocol::UntypedRole>) -> Result<(), agent_client_protocol::Error> {
5769/// cx.send_request(MyRequest {})
5770/// .on_receiving_result(async |result| {
5771/// match result {
5772/// Ok(response) => {
5773/// // Handle successful response
5774/// Ok(())
5775/// }
5776/// Err(error) => {
5777/// // Handle error
5778/// Err(error)
5779/// }
5780/// }
5781/// })?;
5782/// # Ok(())
5783/// # }
5784/// ```
5785///
5786/// ## Option 2: Wait Outside the Dispatch Loop
5787///
5788/// Use [`block_task`](Self::block_task) only when the current task already runs outside the
5789/// dispatch loop—for example, in the foreground future passed to `connect_with` or in a task
5790/// created with [`ConnectionTo::spawn`]. Never await it in a handler:
5791///
5792/// ```no_run
5793/// # use agent_client_protocol_test::*;
5794/// # async fn example(cx: agent_client_protocol::ConnectionTo<agent_client_protocol::UntypedRole>) -> Result<(), agent_client_protocol::Error> {
5795/// // ✅ Safe: Spawned task runs concurrently
5796/// cx.spawn({
5797/// let cx = cx.clone();
5798/// async move {
5799/// let response = cx.send_request(MyRequest {})
5800/// .block_task()
5801/// .await?;
5802/// // Process response...
5803/// Ok(())
5804/// }
5805/// })?;
5806/// # Ok(())
5807/// # }
5808/// ```
5809///
5810/// ```no_run
5811/// # use agent_client_protocol_test::*;
5812/// # async fn example() -> Result<(), agent_client_protocol::Error> {
5813/// # let connection = mock_connection();
5814/// // ❌ NEVER do this in a handler - blocks the event loop!
5815/// connection.on_receive_request(async |req: MyRequest, responder, cx| {
5816/// let response = cx.send_request(MyRequest {})
5817/// .block_task() // This will deadlock!
5818/// .await?;
5819/// responder.respond(response)
5820/// }, agent_client_protocol::on_receive_request!())
5821/// # .connect_to(agent_client_protocol_test::MockTransport).await?;
5822/// # Ok(())
5823/// # }
5824/// ```
5825///
5826/// # Why This Design?
5827///
5828/// If you block the event loop while waiting for a response, the connection cannot process
5829/// the incoming response message, creating a deadlock. This API design prevents that footgun
5830/// by making blocking explicit and encouraging non-blocking patterns.
5831///
5832/// # Drop Behavior
5833///
5834/// By default, dropping a `SentRequest` before the SDK has received the
5835/// response sends a `$/cancel_request` notification asking the peer to cancel
5836/// the request, then discards the response when it arrives. Requests whose
5837/// eventual response should be ignored, but which should keep running on the
5838/// peer, should use [`detach`](Self::detach) instead.
5839///
5840/// # Incoming Transport EOF
5841///
5842/// If the incoming transport reaches clean EOF before the response arrives, every
5843/// consumption mode receives an error with the message `Incoming transport
5844/// closed` and data containing
5845/// `{"reason":"incoming_transport_closed","method":"..."}`. Requests made
5846/// after incoming EOF fail immediately with the same error. Use
5847/// [`is_incoming_transport_closed`] to identify it.
5848#[must_use = "dropping a SentRequest asks the peer to cancel the request and \
5849 discards the response; consume it with `block_task`, \
5850 `on_receiving_result`, `forward_response_to`, or `detach`"]
5851pub struct SentRequest<T> {
5852 id: RequestId,
5853 method: String,
5854 task_tx: TaskTx,
5855 response_rx: oneshot::Receiver<ResponsePayload>,
5856 to_result: Box<dyn FnOnce(serde_json::Value) -> Result<T, crate::Error> + Send>,
5857 cancellation: SentRequestCancellation,
5858 response_ordering: ResponseOrdering,
5859 /// Cancellation markers of other (incoming) requests whose cancellation
5860 /// should be forwarded to this request. See
5861 /// [`forward_cancellation_from`](Self::forward_cancellation_from).
5862 cancellation_sources: Vec<RequestCancellation>,
5863}
5864
5865/// A request that has not been published to its connection.
5866///
5867/// Created by [`ConnectionTo::prepare_request`] or
5868/// [`ConnectionTo::prepare_request_to`]. Preparation serializes the request but
5869/// does not register a pending reply or enqueue outgoing traffic. Dropping this
5870/// value sends neither the request nor a cancellation notification.
5871///
5872/// A consuming method publishes the request synchronously:
5873///
5874/// - [`on_receiving_result`](Self::on_receiving_result),
5875/// [`on_receiving_ok_result`](Self::on_receiving_ok_result), and
5876/// [`forward_response_to`](Self::forward_response_to) register ordered response
5877/// handling before publication. When a peer response is routed during its
5878/// original dispatch, later inbound messages wait for that handling to finish.
5879/// - [`block_task`](Self::block_task) publishes immediately and returns an
5880/// unordered response future. Publication does not wait for its first poll.
5881/// - [`detach`](Self::detach) publishes immediately and discards the response.
5882///
5883/// Ordered callbacks must do bounded work and must not await later inbound
5884/// traffic on the same connection. EOF failures and responses routed through a
5885/// retained [`ResponseRouter`] after their original dispatch have no ordering
5886/// barrier. See [`crate::concepts::ordering`].
5887///
5888/// # Errors
5889///
5890/// Preparation and publication failures are delivered to the selected response
5891/// consumer. Callback-style methods return an error if their task cannot be
5892/// registered; in that case the request is not published. [`detach`](Self::detach)
5893/// returns preparation or publication errors directly because it has no response
5894/// consumer. A callback returning an error terminates the connection.
5895#[must_use = "a prepared request is not sent until consumed with `block_task`, \
5896 `on_receiving_result`, `forward_response_to`, or `detach`"]
5897pub struct PreparedRequest<T> {
5898 sent: SentRequest<T>,
5899 publication: RequestPublication,
5900}
5901
5902struct RequestPublication {
5903 message: Result<OutgoingMessage, crate::Error>,
5904 pending_reply: PendingReply,
5905 message_tx: OutgoingMessageTx,
5906 pending_replies: PendingRepliesRegistrar,
5907 incoming_closed: IncomingClosed,
5908}
5909
5910impl RequestPublication {
5911 fn publish(self) -> Result<(), crate::Error> {
5912 let message = if self.incoming_closed.is_closing() {
5913 Err(incoming_transport_closed_error(&self.pending_reply.method))
5914 } else {
5915 self.message
5916 };
5917 let message = match message {
5918 Ok(message) => message,
5919 Err(error) => {
5920 self.pending_reply.fail(error.clone());
5921 return Err(error);
5922 }
5923 };
5924 let OutgoingMessage::Request { id, method, .. } = &message else {
5925 unreachable!();
5926 };
5927 let id = id.clone();
5928 let method = method.clone();
5929 self.pending_reply.cancellation_disarm.arm();
5930 // Register before enqueueing so incoming EOF can fail every observable
5931 // request before close callbacks begin. The outgoing actor checks that
5932 // the registration still exists before sending the request.
5933 self.pending_replies
5934 .subscribe(id.clone(), self.pending_reply, &self.incoming_closed)?;
5935 if self.message_tx.unbounded_send(message).is_err() {
5936 let error = if self.incoming_closed.is_closing() {
5937 incoming_transport_closed_error(&method)
5938 } else {
5939 crate::util::internal_error(format!("failed to send outgoing request `{method}`"))
5940 };
5941 if let Some(pending_reply) = self.pending_replies.remove(&id) {
5942 pending_reply.fail(error.clone());
5943 }
5944 return Err(error);
5945 }
5946 Ok(())
5947 }
5948}
5949
5950impl<T: Debug> Debug for PreparedRequest<T> {
5951 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5952 formatter
5953 .debug_struct("PreparedRequest")
5954 .field("request", &self.sent)
5955 .finish_non_exhaustive()
5956 }
5957}
5958
5959impl<T> PreparedRequest<T> {
5960 /// The ID reserved for this request, which has not been sent yet.
5961 #[must_use]
5962 pub fn id(&self) -> &RequestId {
5963 self.sent.id()
5964 }
5965
5966 /// The method of the prepared request.
5967 #[must_use]
5968 pub fn method(&self) -> &str {
5969 self.sent.method()
5970 }
5971
5972 /// Map a successful response without publishing the request.
5973 ///
5974 /// The mapper has the same contract as [`SentRequest::map`].
5975 pub fn map<U>(
5976 self,
5977 map_fn: impl FnOnce(T) -> Result<U, crate::Error> + 'static + Send,
5978 ) -> PreparedRequest<U>
5979 where
5980 T: 'static,
5981 {
5982 PreparedRequest {
5983 sent: self.sent.map(map_fn),
5984 publication: self.publication,
5985 }
5986 }
5987
5988 /// Register a cancellation source without publishing the request.
5989 ///
5990 /// After publication, cancellation is forwarded while awaiting the response,
5991 /// as described by [`SentRequest::forward_cancellation_from`].
5992 pub fn forward_cancellation_from(mut self, source: RequestCancellation) -> Self {
5993 self.sent = self.sent.forward_cancellation_from(source);
5994 self
5995 }
5996
5997 /// Publish now and return an unordered future for the response.
5998 ///
5999 /// The request is enqueued during this call, not when the future is first
6000 /// polled. Dropping that future asks the peer to cancel a still-outstanding
6001 /// request. Await it only outside the dispatch loop; awaiting it in an
6002 /// incoming handler deadlocks just like [`SentRequest::block_task`].
6003 ///
6004 /// # Errors
6005 ///
6006 /// The returned future delivers preparation, publication, and response errors.
6007 pub fn block_task(self) -> impl Future<Output = Result<T, crate::Error>> {
6008 self.into_sent_request(false).block_task()
6009 }
6010
6011 /// Publish now and discard the eventual response without cancelling.
6012 ///
6013 /// # Errors
6014 ///
6015 /// Returns immediate preparation or enqueue failures. Later local
6016 /// transformation errors and peer response errors are discarded along with
6017 /// successful responses. Transport failures still propagate through the
6018 /// connection future.
6019 pub fn detach(self) -> Result<(), crate::Error> {
6020 let result = self.publication.publish();
6021 self.sent.detach();
6022 result
6023 }
6024
6025 /// Register an ordered callback, then publish the request.
6026 ///
6027 /// Ordering is selected before publication, even when the connection runs
6028 /// concurrently. See [`PreparedRequest`] for barrier limits and deadlock risks.
6029 ///
6030 /// # Errors
6031 ///
6032 /// Returns an error if the callback task cannot be registered, without
6033 /// publishing the request. Preparation and publication errors are delivered
6034 /// to the callback. Returning an error from the callback ends the connection.
6035 #[track_caller]
6036 pub fn on_receiving_result<F>(
6037 self,
6038 task: impl FnOnce(Result<T, crate::Error>) -> F + 'static + Send,
6039 ) -> Result<(), crate::Error>
6040 where
6041 T: 'static,
6042 F: Future<Output = Result<(), crate::Error>> + 'static + Send,
6043 {
6044 self.consume_with(move |response| match response {
6045 Ok(result) => Either::Left(task(result)),
6046 Err(error) => Either::Right(future::ready(Err(error))),
6047 })
6048 }
6049
6050 /// Register an ordered success callback, then publish the request.
6051 ///
6052 /// Errors are forwarded to `responder`, as with
6053 /// [`SentRequest::on_receiving_ok_result`].
6054 ///
6055 /// # Errors
6056 ///
6057 /// Returns a task-registration error without publishing the request.
6058 /// Preparation, publication, and response errors are forwarded to `responder`.
6059 /// Returning an error from the callback ends the connection.
6060 #[track_caller]
6061 pub fn on_receiving_ok_result<F>(
6062 self,
6063 responder: Responder<T>,
6064 task: impl FnOnce(T, Responder<T>) -> F + 'static + Send,
6065 ) -> Result<(), crate::Error>
6066 where
6067 T: JsonRpcResponse,
6068 F: Future<Output = Result<(), crate::Error>> + 'static + Send,
6069 {
6070 self.on_receiving_result(async move |result| match result {
6071 Ok(value) => task(value, responder).await,
6072 Err(error) => responder.respond_with_error(error),
6073 })
6074 }
6075
6076 /// Register ordered response forwarding, then publish the request.
6077 ///
6078 /// Cancellation and response errors propagate as with
6079 /// [`SentRequest::forward_response_to`].
6080 ///
6081 /// # Errors
6082 ///
6083 /// Returns a task-registration error without publishing the request.
6084 /// Preparation, publication, and response errors are forwarded to `responder`.
6085 #[track_caller]
6086 pub fn forward_response_to(self, responder: Responder<T>) -> Result<(), crate::Error>
6087 where
6088 T: JsonRpcResponse,
6089 {
6090 self.forward_cancellation_from(responder.cancellation())
6091 .consume_with(async move |response| {
6092 responder.respond_with_result(response.unwrap_or_else(Err))
6093 })
6094 }
6095
6096 fn into_sent_request(self, ordered: bool) -> SentRequest<T> {
6097 if ordered {
6098 self.sent.response_ordering.mark_ordered();
6099 }
6100 // Publication errors also settle the response channel.
6101 drop(self.publication.publish());
6102 self.sent
6103 }
6104
6105 #[track_caller]
6106 fn consume_with<F>(
6107 self,
6108 handle: impl FnOnce(Result<Result<T, crate::Error>, crate::Error>) -> F + 'static + Send,
6109 ) -> Result<(), crate::Error>
6110 where
6111 T: 'static,
6112 F: Future<Output = Result<(), crate::Error>> + 'static + Send,
6113 {
6114 let published_tx = self.register_consumer(handle)?;
6115 drop(self.publication.publish());
6116 // Keep the cancellation guard here until publication completes. Even
6117 // destruction of the registered task cannot cancel before enqueueing.
6118 drop(published_tx.send(self.sent));
6119 Ok(())
6120 }
6121
6122 #[track_caller]
6123 fn register_consumer<F>(
6124 &self,
6125 handle: impl FnOnce(Result<Result<T, crate::Error>, crate::Error>) -> F + 'static + Send,
6126 ) -> Result<oneshot::Sender<SentRequest<T>>, crate::Error>
6127 where
6128 T: 'static,
6129 F: Future<Output = Result<(), crate::Error>> + 'static + Send,
6130 {
6131 self.sent.response_ordering.mark_ordered();
6132 let (published_tx, published_rx) = oneshot::channel::<SentRequest<T>>();
6133 Task::new(Location::caller(), async move {
6134 match published_rx.await {
6135 Ok(sent) => sent.handle_response(handle).await,
6136 // Publication was abandoned before the consumer took ownership.
6137 Err(_) => Ok(()),
6138 }
6139 })
6140 .spawn(&self.sent.task_tx)?;
6141 Ok(published_tx)
6142 }
6143}
6144
6145#[derive(Clone, Debug)]
6146pub(crate) struct SentRequestCancellationDisarm {
6147 armed: Arc<AtomicBool>,
6148}
6149
6150impl SentRequestCancellationDisarm {
6151 fn new() -> Self {
6152 Self {
6153 armed: Arc::new(AtomicBool::new(true)),
6154 }
6155 }
6156
6157 fn disarm(&self) {
6158 self.armed.store(false, Ordering::Release);
6159 }
6160
6161 fn arm(&self) {
6162 self.armed.store(true, Ordering::Release);
6163 }
6164}
6165
6166struct SentRequestCancellation {
6167 message_tx: OutgoingMessageTx,
6168 remote_style: crate::role::RemoteStyle,
6169 request_id: RequestId,
6170 disarm: SentRequestCancellationDisarm,
6171}
6172
6173impl SentRequestCancellation {
6174 fn new(
6175 message_tx: OutgoingMessageTx,
6176 remote_style: crate::role::RemoteStyle,
6177 request_id: RequestId,
6178 ) -> Self {
6179 Self {
6180 message_tx,
6181 remote_style,
6182 request_id,
6183 disarm: SentRequestCancellationDisarm::new(),
6184 }
6185 }
6186
6187 fn disarm(&self) {
6188 self.disarm.disarm();
6189 }
6190
6191 fn disarm_handle(&self) -> SentRequestCancellationDisarm {
6192 self.disarm.clone()
6193 }
6194
6195 fn send(&self) -> Result<(), crate::Error> {
6196 if !self.disarm.armed.swap(false, Ordering::AcqRel) {
6197 return Ok(());
6198 }
6199
6200 // Build the notification lazily: most requests are never cancelled,
6201 // so this avoids serializing a notification per outgoing request.
6202 let untyped = self.remote_style.transform_outgoing_message(
6203 crate::schema::v1::CancelRequestNotification::new(self.request_id.clone()),
6204 )?;
6205
6206 send_raw_message(&self.message_tx, OutgoingMessage::Notification { untyped })
6207 }
6208}
6209
6210impl Drop for SentRequestCancellation {
6211 fn drop(&mut self) {
6212 if let Err(error) = self.send() {
6213 tracing::debug!(?error, "failed to auto-cancel dropped request");
6214 }
6215 }
6216}
6217
6218impl Debug for SentRequestCancellation {
6219 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6220 f.debug_struct("SentRequestCancellation")
6221 .field("request_id", &self.request_id)
6222 .field("remote_style", &self.remote_style)
6223 .field("armed", &self.disarm.armed.load(Ordering::Acquire))
6224 .finish_non_exhaustive()
6225 }
6226}
6227
6228/// Await the response payload for an outgoing request, watching `sources` for
6229/// cancellation of the upstream requests it was registered with.
6230///
6231/// When any source reports cancellation, a `$/cancel_request` is forwarded to
6232/// the outgoing request (at most once, shared with [`SentRequest::cancel`] and
6233/// drop-time auto-cancellation), and the response is *still* awaited: the peer
6234/// always answers, with normal data or a cancellation error.
6235///
6236/// Watching is deliberately bounded by response arrival so that completed
6237/// requests do not leak waiters on markers that will never fire.
6238async fn await_response_forwarding_cancellation(
6239 response_rx: oneshot::Receiver<ResponsePayload>,
6240 cancellation: &SentRequestCancellation,
6241 sources: &[RequestCancellation],
6242) -> Result<ResponsePayload, oneshot::Canceled> {
6243 // Failing to forward the cancellation must not abort the wait: the
6244 // response (normal data or a cancellation error) may still arrive and
6245 // must still be processed.
6246 let forward_cancellation = || {
6247 if let Err(error) = cancellation.send() {
6248 tracing::debug!(
6249 ?error,
6250 "failed to forward cancellation to downstream request"
6251 );
6252 }
6253 };
6254
6255 let response = if sources.is_empty() {
6256 response_rx.await
6257 } else if sources.iter().any(RequestCancellation::is_cancelled) {
6258 forward_cancellation();
6259 response_rx.await
6260 } else {
6261 let cancelled = sources.iter().map(|source| source.state.signal_rx.clone());
6262 match future::select(future::select_all(cancelled), response_rx).await {
6263 Either::Left((_, response_rx)) => {
6264 forward_cancellation();
6265 response_rx.await
6266 }
6267 Either::Right((response, _)) => response,
6268 }
6269 };
6270
6271 cancellation.disarm();
6272 response
6273}
6274
6275impl<T: Debug> Debug for SentRequest<T> {
6276 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6277 let mut debug = f.debug_struct("SentRequest");
6278 debug
6279 .field("id", &self.id)
6280 .field("method", &self.method)
6281 .field("task_tx", &self.task_tx)
6282 .field("response_rx", &self.response_rx);
6283 debug
6284 .field("cancellation", &self.cancellation)
6285 .field("cancellation_sources", &self.cancellation_sources);
6286 debug.finish_non_exhaustive()
6287 }
6288}
6289
6290impl SentRequest<serde_json::Value> {
6291 fn new(
6292 id: RequestId,
6293 method: String,
6294 task_tx: mpsc::UnboundedSender<Task>,
6295 response_rx: oneshot::Receiver<ResponsePayload>,
6296 cancellation: SentRequestCancellation,
6297 response_ordering: ResponseOrdering,
6298 ) -> Self {
6299 Self {
6300 id,
6301 method,
6302 response_rx,
6303 task_tx,
6304 to_result: Box::new(Ok),
6305 cancellation,
6306 response_ordering,
6307 cancellation_sources: Vec::new(),
6308 }
6309 }
6310}
6311
6312impl<T> SentRequest<T> {
6313 /// Detach this request handle without waiting for its response.
6314 ///
6315 /// The response will be discarded when it arrives. This also disarms the
6316 /// drop-time automatic cancellation described in
6317 /// [Drop Behavior](Self#drop-behavior), so use it for requests whose
6318 /// eventual response should be ignored, but which should keep running on
6319 /// the peer. The peer is still expected to answer the JSON-RPC request
6320 /// eventually; use a notification instead when no response is expected at
6321 /// all.
6322 ///
6323 /// To ask the peer to stop the request, call `cancel` instead, or drop the
6324 /// handle while automatic cancellation is armed.
6325 pub fn detach(self) {
6326 self.cancellation.disarm();
6327 }
6328
6329 /// Send a `$/cancel_request` notification for this outgoing request.
6330 ///
6331 /// This uses the same peer and message wrapping that were used to send the
6332 /// original request, so it is the preferred way to cancel a [`SentRequest`]
6333 /// when the request handle is still available.
6334 ///
6335 /// At most one `$/cancel_request` is ever sent per request: the first
6336 /// `cancel` call sends it (and also prevents the drop-time automatic
6337 /// cancellation described in [Drop Behavior](Self#drop-behavior)), while
6338 /// later calls return `Ok(())` without sending anything. Likewise, once
6339 /// the SDK has routed the response to this handle, `cancel` becomes a
6340 /// no-op: there is nothing left to cancel.
6341 ///
6342 /// Errors are only reported by the call that attempts to send the
6343 /// notification.
6344 pub fn cancel(&self) -> Result<(), crate::Error> {
6345 self.cancellation.send()
6346 }
6347
6348 /// Forward cancellation of another request to this one.
6349 ///
6350 /// When the request that `source` belongs to is cancelled by its peer,
6351 /// a `$/cancel_request` for *this* request is sent to its peer, using the
6352 /// same wrapping as the original request. The response is still awaited
6353 /// and delivered as usual (normal data or a cancellation error), so this
6354 /// composes with [`block_task`](Self::block_task) and
6355 /// [`on_receiving_result`](Self::on_receiving_result).
6356 ///
6357 /// This is the building block for proxies that forward a request with
6358 /// custom logic instead of [`forward_response_to`](Self::forward_response_to)
6359 /// (which wires this up automatically from its responder). Without it,
6360 /// custom forwarding *absorbs* cancellation: the upstream marker is still
6361 /// set, but nothing is sent downstream.
6362 ///
6363 /// ```
6364 /// # use agent_client_protocol::{ConnectionTo, Error, Responder, UntypedRole};
6365 /// # use agent_client_protocol_test::{MyRequest, MyResponse};
6366 /// # async fn example(request: MyRequest, responder: Responder<MyResponse>, backend: ConnectionTo<UntypedRole>) -> Result<(), Error> {
6367 /// backend
6368 /// .send_request(request)
6369 /// .forward_cancellation_from(responder.cancellation())
6370 /// .on_receiving_result(async move |result| {
6371 /// // Custom result handling, e.g. bookkeeping or rewriting.
6372 /// responder.respond_with_result(result)
6373 /// })?;
6374 /// # Ok(())
6375 /// # }
6376 /// ```
6377 ///
6378 /// May be called multiple times; cancellation of any registered source
6379 /// triggers the forwarding (at most one `$/cancel_request` is ever sent
6380 /// per request). Sources are observed while the response is being
6381 /// awaited — that is, once the handle is consumed with
6382 /// [`block_task`](Self::block_task),
6383 /// [`on_receiving_result`](Self::on_receiving_result), or
6384 /// [`forward_response_to`](Self::forward_response_to); a source that was
6385 /// already cancelled by then is honored immediately.
6386 pub fn forward_cancellation_from(mut self, source: RequestCancellation) -> Self {
6387 self.cancellation_sources.push(source);
6388 self
6389 }
6390}
6391
6392impl<T> SentRequest<T> {
6393 /// The id of the outgoing request.
6394 #[must_use]
6395 pub fn id(&self) -> &RequestId {
6396 &self.id
6397 }
6398
6399 /// The method of the request this is in response to.
6400 #[must_use]
6401 pub fn method(&self) -> &str {
6402 &self.method
6403 }
6404
6405 /// Map a successful JSON-RPC response into an application type.
6406 ///
6407 /// The mapped type does not need to implement [`JsonRpcResponse`]. The
6408 /// mapper runs at most once and may consume captured state. JSON-RPC error
6409 /// responses bypass the mapper. The mapped type may carry a non-`'static`
6410 /// lifetime when it is consumed with [`block_task`](Self::block_task);
6411 /// callback-style consumption still requires a `'static` mapped type
6412 /// because its work is spawned onto the connection.
6413 pub fn map<U>(
6414 self,
6415 map_fn: impl FnOnce(T) -> Result<U, crate::Error> + 'static + Send,
6416 ) -> SentRequest<U>
6417 where
6418 T: 'static,
6419 {
6420 SentRequest {
6421 id: self.id,
6422 method: self.method,
6423 response_rx: self.response_rx,
6424 task_tx: self.task_tx,
6425 to_result: Box::new(move |value| map_fn((self.to_result)(value)?)),
6426 cancellation: self.cancellation,
6427 response_ordering: self.response_ordering,
6428 cancellation_sources: self.cancellation_sources,
6429 }
6430 }
6431
6432 /// Forward the response (success or error) to a request context when it arrives.
6433 ///
6434 /// This is a convenience method for proxying messages between connections. When the
6435 /// response arrives, it will be automatically sent to the provided request context,
6436 /// whether it's a successful response or an error.
6437 ///
6438 /// # Example: Proxying requests
6439 ///
6440 /// ```
6441 /// # use agent_client_protocol::UntypedRole;
6442 /// # use agent_client_protocol::{Builder, ConnectionTo};
6443 /// # use agent_client_protocol_test::*;
6444 /// # async fn example(cx: ConnectionTo<UntypedRole>) -> Result<(), agent_client_protocol::Error> {
6445 /// // Set up backend connection builder
6446 /// let backend = UntypedRole.builder()
6447 /// .on_receive_request(async |req: MyRequest, responder, cx| {
6448 /// responder.respond(MyResponse { status: "ok".into() })
6449 /// }, agent_client_protocol::on_receive_request!());
6450 ///
6451 /// // Spawn backend and get a context to send to it
6452 /// let backend_connection = cx.spawn_connection(backend, MockTransport)?;
6453 ///
6454 /// // Set up proxy that forwards requests to backend
6455 /// UntypedRole.builder()
6456 /// .on_receive_request({
6457 /// let backend_connection = backend_connection.clone();
6458 /// async move |req: MyRequest, responder, cx| {
6459 /// // Forward the request to backend and proxy the response back
6460 /// backend_connection.send_request(req)
6461 /// .forward_response_to(responder)?;
6462 /// Ok(())
6463 /// }
6464 /// }, agent_client_protocol::on_receive_request!());
6465 /// # Ok(())
6466 /// # }
6467 /// ```
6468 ///
6469 /// # Type Safety
6470 ///
6471 /// The request context's response type must match the request's response type,
6472 /// ensuring type-safe message forwarding.
6473 ///
6474 /// # When to Use
6475 ///
6476 /// Use this when:
6477 /// - You're implementing a proxy or gateway pattern
6478 /// - You want to forward responses without processing them
6479 /// - The response types match between the outgoing request and incoming request
6480 ///
6481 /// This is equivalent to calling `on_receiving_result` and manually forwarding
6482 /// the result, with two proxy-specific additions:
6483 ///
6484 /// - If the pending response cannot be delivered, the incoming request is
6485 /// answered with an internal error instead of being left unanswered.
6486 /// Known clean incoming EOF is delivered like any other response
6487 /// error; an unexpected response-channel loss is forwarded as an outer
6488 /// consumption error.
6489 /// - When the peer cancels the incoming request, the cancellation is
6490 /// forwarded to the outgoing request, and the downstream response
6491 /// (normal data or a cancellation error) is still forwarded back. This is
6492 /// equivalent to registering the responder's marker with
6493 /// `forward_cancellation_from`.
6494 #[track_caller]
6495 pub fn forward_response_to(self, responder: Responder<T>) -> Result<(), crate::Error>
6496 where
6497 T: JsonRpcResponse,
6498 {
6499 let this = self.forward_cancellation_from(responder.cancellation());
6500
6501 this.consume_with(async move |response| {
6502 // An unexpected response-channel loss (outer `Err`) is forwarded
6503 // as an error: the incoming request must not be left unanswered.
6504 responder.respond_with_result(response.unwrap_or_else(Err))
6505 })
6506 }
6507
6508 /// Spawn the response-consumption task shared by
6509 /// [`on_receiving_result`](Self::on_receiving_result) and
6510 /// [`forward_response_to`](Self::forward_response_to).
6511 ///
6512 /// The task awaits the response (forwarding cancellation from registered
6513 /// sources while waiting, converts the payload, and invokes `handle` with
6514 /// the typed result (`Ok(Result<T, _>)`). The dispatch loop's ack, if any,
6515 /// is sent after `handle` completes.
6516 ///
6517 /// Clean incoming EOF is delivered as `Ok(Err(error))`, just like
6518 /// a peer response error, so callback-style consumers still run. If the
6519 /// response channel disappears for another reason, `handle` receives an
6520 /// outer `Err` describing that unexpected loss; there is no ack then.
6521 #[track_caller]
6522 fn consume_with<F>(
6523 self,
6524 handle: impl FnOnce(Result<Result<T, crate::Error>, crate::Error>) -> F + 'static + Send,
6525 ) -> Result<(), crate::Error>
6526 where
6527 T: 'static,
6528 F: Future<Output = Result<(), crate::Error>> + 'static + Send,
6529 {
6530 self.response_ordering.mark_ordered();
6531 let task_tx = self.task_tx.clone();
6532 Task::new(Location::caller(), self.handle_response(handle)).spawn(&task_tx)
6533 }
6534
6535 fn handle_response<F>(
6536 self,
6537 handle: impl FnOnce(Result<Result<T, crate::Error>, crate::Error>) -> F + 'static + Send,
6538 ) -> impl Future<Output = Result<(), crate::Error>> + Send
6539 where
6540 T: 'static,
6541 F: Future<Output = Result<(), crate::Error>> + 'static + Send,
6542 {
6543 let method = self.method;
6544 let response_rx = self.response_rx;
6545 let to_result = self.to_result;
6546 let cancellation = self.cancellation;
6547 let cancellation_sources = self.cancellation_sources;
6548 async move {
6549 let response = await_response_forwarding_cancellation(
6550 response_rx,
6551 &cancellation,
6552 &cancellation_sources,
6553 )
6554 .await;
6555
6556 match response {
6557 Ok(ResponsePayload { result, ack_tx }) => {
6558 // Convert the result using to_result for Ok values
6559 let typed_result = match result {
6560 Ok(json_value) => to_result(json_value),
6561 Err(err) => Err(err),
6562 };
6563
6564 let outcome = handle(Ok(typed_result)).await;
6565
6566 // Ack AFTER the handler completes - this is the key
6567 // difference from block_task. The dispatch loop waits for
6568 // this ack.
6569 if let Some(tx) = ack_tx {
6570 let _ = tx.send(());
6571 }
6572
6573 outcome
6574 }
6575 Err(err) => {
6576 handle(Err(crate::util::internal_error(format!(
6577 "response to `{method}` never received: {err}"
6578 ))))
6579 .await
6580 }
6581 }
6582 }
6583 }
6584
6585 /// Block the current task until the response is received.
6586 ///
6587 /// **Warning:** This method blocks the current async task. It is safe only when that task
6588 /// already runs outside the dispatch loop, such as the foreground future passed to
6589 /// `connect_with` or a task created with [`ConnectionTo::spawn`]. Using it directly in a
6590 /// handler callback will deadlock the connection.
6591 ///
6592 /// # Safe Usage (outside the dispatch loop)
6593 ///
6594 /// ```no_run
6595 /// # use agent_client_protocol_test::*;
6596 /// # async fn example() -> Result<(), agent_client_protocol::Error> {
6597 /// # let connection = mock_connection();
6598 /// connection.on_receive_request(async |req: MyRequest, responder, cx| {
6599 /// // Spawn a task to handle the request
6600 /// cx.spawn({
6601 /// let connection = cx.clone();
6602 /// async move {
6603 /// // Safe: We're in a spawned task, not blocking the event loop
6604 /// let response = connection.send_request(OtherRequest {})
6605 /// .block_task()
6606 /// .await?;
6607 ///
6608 /// // Process the response...
6609 /// Ok(())
6610 /// }
6611 /// })?;
6612 ///
6613 /// // Respond immediately
6614 /// responder.respond(MyResponse { status: "ok".into() })
6615 /// }, agent_client_protocol::on_receive_request!())
6616 /// # .connect_to(agent_client_protocol_test::MockTransport).await?;
6617 /// # Ok(())
6618 /// # }
6619 /// ```
6620 ///
6621 /// # Unsafe Usage (in handlers - will deadlock!)
6622 ///
6623 /// ```no_run
6624 /// # use agent_client_protocol_test::*;
6625 /// # async fn example() -> Result<(), agent_client_protocol::Error> {
6626 /// # let connection = mock_connection();
6627 /// connection.on_receive_request(async |req: MyRequest, responder, cx| {
6628 /// // ❌ DEADLOCK: Handler blocks event loop, which can't process the response
6629 /// let response = cx.send_request(OtherRequest {})
6630 /// .block_task()
6631 /// .await?;
6632 ///
6633 /// responder.respond(MyResponse { status: response.value })
6634 /// }, agent_client_protocol::on_receive_request!())
6635 /// # .connect_to(agent_client_protocol_test::MockTransport).await?;
6636 /// # Ok(())
6637 /// # }
6638 /// ```
6639 ///
6640 /// # When to Use
6641 ///
6642 /// Use this method when:
6643 /// - Your current task already runs outside the dispatch loop
6644 /// - You need the response value to proceed with your logic
6645 /// - Linear control flow is more natural than callbacks
6646 ///
6647 /// For handler callbacks, use [`on_receiving_result`](Self::on_receiving_result) instead.
6648 pub async fn block_task(self) -> Result<T, crate::Error> {
6649 let response = await_response_forwarding_cancellation(
6650 self.response_rx,
6651 &self.cancellation,
6652 &self.cancellation_sources,
6653 )
6654 .await;
6655
6656 match response {
6657 Ok(ResponsePayload {
6658 result: Ok(json_value),
6659 ack_tx,
6660 }) => {
6661 // Blocking consumers ack before converting or returning the
6662 // value, so dispatch can continue while the caller processes it.
6663 if let Some(tx) = ack_tx {
6664 let _ = tx.send(());
6665 }
6666 match (self.to_result)(json_value) {
6667 Ok(value) => Ok(value),
6668 Err(err) => Err(err),
6669 }
6670 }
6671 Ok(ResponsePayload {
6672 result: Err(err),
6673 ack_tx,
6674 }) => {
6675 if let Some(tx) = ack_tx {
6676 let _ = tx.send(());
6677 }
6678 Err(err)
6679 }
6680 Err(err) => Err(crate::util::internal_error(format!(
6681 "response to `{}` never received: {}",
6682 self.method, err
6683 ))),
6684 }
6685 }
6686
6687 /// Block the current task and transform the typed result before releasing
6688 /// the ordered-response barrier.
6689 ///
6690 /// Framework lifecycle code uses this when success transfers local state
6691 /// to the returned value while an error must drop that state before later
6692 /// messages from the same transport frame are dispatched. The synchronous
6693 /// transform must not wait for additional connection traffic.
6694 pub(crate) async fn block_task_with_ordered_result<U>(
6695 self,
6696 transform: impl FnOnce(Result<T, crate::Error>) -> Result<U, crate::Error>,
6697 ) -> Result<U, crate::Error> {
6698 let response = await_response_forwarding_cancellation(
6699 self.response_rx,
6700 &self.cancellation,
6701 &self.cancellation_sources,
6702 )
6703 .await;
6704
6705 let (result, ack_tx) = match response {
6706 Ok(ResponsePayload { result, ack_tx }) => {
6707 let typed_result = match result {
6708 Ok(json_value) => (self.to_result)(json_value),
6709 Err(error) => Err(error),
6710 };
6711 (typed_result, ack_tx)
6712 }
6713 Err(error) => (
6714 Err(crate::util::internal_error(format!(
6715 "response to `{}` never received: {error}",
6716 self.method
6717 ))),
6718 None,
6719 ),
6720 };
6721
6722 let outcome = transform(result);
6723 if let Some(acknowledgment) = ack_tx {
6724 let _ = acknowledgment.send(());
6725 }
6726 outcome
6727 }
6728
6729 /// Schedule an async task to run when a successful response is received.
6730 ///
6731 /// This is a convenience wrapper around [`on_receiving_result`](Self::on_receiving_result)
6732 /// for the common pattern of forwarding errors to a request context while only processing
6733 /// successful responses.
6734 ///
6735 /// # Behavior
6736 ///
6737 /// - If the response is `Ok(value)`, your task receives the value and the request context
6738 /// - If the response is `Err(error)`, the error is automatically sent to `responder`
6739 /// and your task is not called
6740 ///
6741 /// # Example: Chaining requests
6742 ///
6743 /// ```no_run
6744 /// # use agent_client_protocol_test::*;
6745 /// # async fn example() -> Result<(), agent_client_protocol::Error> {
6746 /// # let connection = mock_connection();
6747 /// connection.on_receive_request(async |req: ValidateRequest, responder, cx| {
6748 /// // Send initial request
6749 /// cx.send_request(ValidateRequest { data: req.data.clone() })
6750 /// .on_receiving_ok_result(responder, async |validation, responder| {
6751 /// // Only runs if validation succeeded
6752 /// if validation.is_valid {
6753 /// // Respond to original request
6754 /// responder.respond(ValidateResponse { is_valid: true, error: None })
6755 /// } else {
6756 /// responder.respond_with_error(agent_client_protocol::util::internal_error("validation failed"))
6757 /// }
6758 /// })?;
6759 ///
6760 /// Ok(())
6761 /// }, agent_client_protocol::on_receive_request!())
6762 /// # .connect_to(agent_client_protocol_test::MockTransport).await?;
6763 /// # Ok(())
6764 /// # }
6765 /// ```
6766 ///
6767 /// # Ordering
6768 ///
6769 /// Like [`on_receiving_result`](Self::on_receiving_result), response handling holds the
6770 /// dispatch loop through callback completion when ordered consumption is selected before a
6771 /// peer response is routed during its original dispatch. Pending-request failures delivered
6772 /// without an incoming response and delayed routes do not carry that barrier. The callback
6773 /// must not await later inbound traffic on the same connection. See the
6774 /// [`ordering`](crate::concepts::ordering) module for details.
6775 ///
6776 /// # When to Use
6777 ///
6778 /// Use this when:
6779 /// - You need to respond to a request based on another request's result
6780 /// - You want errors to automatically propagate to the request context
6781 /// - You only care about the success case
6782 ///
6783 /// For more control over error handling, use [`on_receiving_result`](Self::on_receiving_result).
6784 #[track_caller]
6785 pub fn on_receiving_ok_result<F>(
6786 self,
6787 responder: Responder<T>,
6788 task: impl FnOnce(T, Responder<T>) -> F + 'static + Send,
6789 ) -> Result<(), crate::Error>
6790 where
6791 F: Future<Output = Result<(), crate::Error>> + 'static + Send,
6792 T: JsonRpcResponse,
6793 {
6794 self.on_receiving_result(async move |result| match result {
6795 Ok(value) => task(value, responder).await,
6796 Err(err) => responder.respond_with_error(err),
6797 })
6798 }
6799
6800 /// Register an async callback to run when the response is received.
6801 ///
6802 /// This is the recommended way to select response handling from inside a handler because
6803 /// registration returns immediately. The response-consumption task waits concurrently for
6804 /// the response; once the response is dispatched, the ordered callback may hold the dispatch
6805 /// loop until it completes.
6806 ///
6807 /// # Example: Handle response in callback
6808 ///
6809 /// ```no_run
6810 /// # use agent_client_protocol_test::*;
6811 /// # async fn example() -> Result<(), agent_client_protocol::Error> {
6812 /// # let connection = mock_connection();
6813 /// connection.on_receive_request(async |req: MyRequest, responder, cx| {
6814 /// // Send a request and schedule a callback for the response
6815 /// cx.send_request(QueryRequest { id: 22 })
6816 /// .on_receiving_result({
6817 /// let connection = cx.clone();
6818 /// async move |result| {
6819 /// match result {
6820 /// Ok(response) => {
6821 /// println!("Got response: {:?}", response);
6822 /// // Can send more messages here
6823 /// connection.send_notification(QueryComplete {})?;
6824 /// Ok(())
6825 /// }
6826 /// Err(error) => {
6827 /// eprintln!("Request failed: {}", error);
6828 /// Err(error)
6829 /// }
6830 /// }
6831 /// }
6832 /// })?;
6833 ///
6834 /// // Handler continues immediately after registering the callback
6835 /// responder.respond(MyResponse { status: "processing".into() })
6836 /// }, agent_client_protocol::on_receive_request!())
6837 /// # .connect_to(agent_client_protocol_test::MockTransport).await?;
6838 /// # Ok(())
6839 /// # }
6840 /// ```
6841 ///
6842 /// # Ordering
6843 ///
6844 /// When ordered consumption is selected before a peer response is routed during its original
6845 /// dispatch, the callback runs in a connection-managed task and the dispatch loop waits for
6846 /// it to complete before processing the next message.
6847 ///
6848 /// The barrier does not apply when the pending request is failed without an incoming response,
6849 /// such as on EOF. If the response was already routed, or an interceptor routes a retained
6850 /// [`ResponseRouter`] after its original dispatch, the callback still runs but cannot
6851 /// retroactively block messages that were already released.
6852 ///
6853 /// While the barrier is held, the callback must not await a later response, notification, or
6854 /// other inbound traffic on the same connection: that traffic cannot be dispatched until the
6855 /// callback completes. Spawn follow-up work with [`ConnectionTo::spawn`] and return, or use
6856 /// [`block_task`](Self::block_task) from a task already outside the dispatch loop.
6857 ///
6858 /// This differs from [`block_task`](Self::block_task), which does not select ordered
6859 /// consumption: dispatch remains free while the caller processes the delivered response.
6860 ///
6861 /// See the [`ordering`](crate::concepts::ordering) module for details on ordering guarantees
6862 /// and how to avoid deadlocks.
6863 ///
6864 /// # Error Handling
6865 ///
6866 /// If the scheduled task returns `Err`, the entire server will shut down. Make sure to handle
6867 /// errors appropriately within your task.
6868 ///
6869 /// # When to Use
6870 ///
6871 /// Use this method when:
6872 /// - You need to register response handling from a handler callback
6873 /// - You want a peer response callback to complete before later messages are dispatched
6874 /// - The callback performs bounded work that does not depend on later inbound traffic
6875 ///
6876 /// When already outside the dispatch loop and you do not need ordering guarantees, consider
6877 /// [`block_task`](Self::block_task).
6878 #[track_caller]
6879 pub fn on_receiving_result<F>(
6880 self,
6881 task: impl FnOnce(Result<T, crate::Error>) -> F + 'static + Send,
6882 ) -> Result<(), crate::Error>
6883 where
6884 T: 'static,
6885 F: Future<Output = Result<(), crate::Error>> + 'static + Send,
6886 {
6887 self.consume_with(move |response| match response {
6888 // Invoke the callback before constructing its future so the
6889 // response value does not need to be `Send` across an await.
6890 Ok(result) => Either::Left(task(result)),
6891 // A response that was never delivered fails the consuming
6892 // task instead of invoking the callback.
6893 Err(err) => Either::Right(future::ready(Err(err))),
6894 })
6895 }
6896}
6897
6898// ============================================================================
6899// IntoJrConnectionTransport Implementations
6900// ============================================================================
6901
6902/// A component that communicates over line streams.
6903///
6904/// `Lines` implements the [`ConnectTo`] trait for any pair of line-based streams
6905/// (a `Stream<Item = io::Result<String>>` for incoming and a `Sink<String>` for outgoing),
6906/// handling serialization of JSON-RPC messages to/from newline-delimited JSON.
6907/// An incoming line may contain one JSON-RPC message or a non-empty batch array. Batch
6908/// entries are dispatched individually in source order, and responses to the batch are
6909/// collected into one response-array line. SDK-initiated requests and notifications remain
6910/// individual messages.
6911///
6912/// This is a lower-level primitive than [`ByteStreams`] that enables interception and
6913/// transformation of individual lines before they are parsed or after they are serialized.
6914/// This is particularly useful for debugging, logging, or implementing custom line-based
6915/// protocols.
6916///
6917/// # Use Cases
6918///
6919/// - **Line-by-line logging**: Intercept and log each line before parsing
6920/// - **Custom protocols**: Transform lines before/after JSON-RPC processing
6921/// - **Debugging**: Inspect raw message strings
6922/// - **Line filtering**: Skip or modify specific messages
6923///
6924/// Most users should use [`ByteStreams`] instead, which provides a simpler interface
6925/// for byte-based I/O.
6926///
6927/// [`ConnectTo`]: crate::ConnectTo
6928#[derive(Debug)]
6929pub struct Lines<OutgoingSink, IncomingStream> {
6930 outgoing: OutgoingSink,
6931 incoming: IncomingStream,
6932}
6933
6934impl<OutgoingSink, IncomingStream> Lines<OutgoingSink, IncomingStream>
6935where
6936 OutgoingSink: futures::Sink<String, Error = std::io::Error> + Send + 'static,
6937 IncomingStream: futures::Stream<Item = std::io::Result<String>> + Send + 'static,
6938{
6939 /// Create a new line stream transport.
6940 pub fn new(outgoing: OutgoingSink, incoming: IncomingStream) -> Self {
6941 Self { outgoing, incoming }
6942 }
6943
6944 fn into_channel_transport(self) -> (Channel, crate::ConnectionDriver) {
6945 let Self { outgoing, incoming } = self;
6946 let (channel_for_caller, channel_for_lines) = Channel::duplex();
6947 let Channel { mut rx, tx } = channel_for_lines;
6948 let (finish_tx, finish_rx) = oneshot::channel();
6949 let finish = async move {
6950 // Losing a finish handle is not a shutdown request.
6951 if finish_rx.await.is_err() {
6952 future::pending::<()>().await;
6953 }
6954 }
6955 .boxed()
6956 .shared();
6957 let outgoing_frames = futures::stream::poll_fn({
6958 let mut finish = finish.clone();
6959 let mut finishing = false;
6960 move |cx| {
6961 if !finishing && std::pin::Pin::new(&mut finish).poll(cx).is_ready() {
6962 rx.close();
6963 finishing = true;
6964 }
6965 rx.poll_next_unpin(cx)
6966 }
6967 });
6968 let discard_incoming = Arc::new(AtomicBool::new(false));
6969 let incoming = incoming.filter_map({
6970 let discard_incoming = discard_incoming.clone();
6971 move |item| {
6972 let discard = discard_incoming.load(Ordering::Acquire);
6973 future::ready((!discard || item.is_err()).then_some(item))
6974 }
6975 });
6976 let outgoing = transport_actor::transport_outgoing_lines_actor(outgoing_frames, outgoing)
6977 .boxed()
6978 .shared();
6979 let serve_self = Box::pin({
6980 let outgoing = outgoing.clone();
6981 async move {
6982 futures::try_join!(
6983 outgoing,
6984 transport_actor::transport_incoming_lines_actor(incoming, tx),
6985 )?;
6986 Ok(())
6987 }
6988 });
6989 let server_future = crate::ConnectionDriver::with_finish(
6990 async move {
6991 match future::select(finish, serve_self).await {
6992 Either::Left(((), serve_self)) => {
6993 discard_incoming.store(true, Ordering::Release);
6994 // Keep reading while flushing, but do not require remote
6995 // read EOF. Poll incoming errors before clean sink drain.
6996 match future::select(serve_self, outgoing).await {
6997 Either::Left((result, _)) | Either::Right((result, _)) => result,
6998 }
6999 }
7000 Either::Right((result, _)) => result,
7001 }
7002 },
7003 move || {
7004 let _ = finish_tx.send(());
7005 },
7006 );
7007
7008 (channel_for_caller, server_future)
7009 }
7010}
7011
7012impl<OutgoingSink, IncomingStream, R: Role> ConnectTo<R> for Lines<OutgoingSink, IncomingStream>
7013where
7014 OutgoingSink: futures::Sink<String, Error = std::io::Error> + Send + 'static,
7015 IncomingStream: futures::Stream<Item = std::io::Result<String>> + Send + 'static,
7016{
7017 async fn connect_to(self, client: impl ConnectTo<R::Counterpart>) -> Result<(), crate::Error> {
7018 let (channel, mut serve_self) = self.into_channel_transport();
7019 let mut finish = serve_self
7020 .take_finish()
7021 .expect("built-in Lines transport supports explicit finishing");
7022 let client_future = Box::pin(ConnectTo::<R>::connect_to(channel, client));
7023
7024 match futures::future::select(client_future, serve_self).await {
7025 Either::Left((result, serve_self)) => {
7026 result?;
7027 // The local bridge has transferred all accepted client output.
7028 // Finish the physical sink without waiting for remote read EOF.
7029 finish.request();
7030 serve_self.await
7031 }
7032 Either::Right((result, _)) => result,
7033 }
7034 }
7035
7036 fn into_channel_and_future(self) -> (Channel, Option<crate::ConnectionDriver>) {
7037 let (channel, driver) = self.into_channel_transport();
7038 (channel, Some(driver))
7039 }
7040}
7041
7042/// A component that communicates over byte streams (stdin/stdout, sockets, pipes, etc.).
7043///
7044/// `ByteStreams` implements the [`ConnectTo`] trait for any pair of `AsyncRead` and `AsyncWrite`
7045/// streams, handling serialization of JSON-RPC messages to/from newline-delimited JSON.
7046/// This is the standard way to communicate with external processes or network connections.
7047///
7048/// # Use Cases
7049///
7050/// - **Stdio communication**: Connect to agents or proxies via stdin/stdout
7051/// - **Network sockets**: TCP, Unix domain sockets, or other stream-based protocols
7052/// - **Named pipes**: Cross-process communication on the same machine
7053/// - **File I/O**: Reading from and writing to file descriptors
7054///
7055/// # Example
7056///
7057/// Connecting to an agent via stdio:
7058///
7059/// ```no_run
7060/// use agent_client_protocol::UntypedRole;
7061/// # use agent_client_protocol::{ByteStreams};
7062/// use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};
7063///
7064/// # async fn example() -> Result<(), agent_client_protocol::Error> {
7065/// let component = ByteStreams::new(
7066/// tokio::io::stdout().compat_write(),
7067/// tokio::io::stdin().compat(),
7068/// );
7069///
7070/// // Use as a component in a connection
7071/// agent_client_protocol::UntypedRole.builder()
7072/// .name("my-client")
7073/// .connect_to(component)
7074/// .await?;
7075/// # Ok(())
7076/// # }
7077/// ```
7078///
7079/// [`ConnectTo`]: crate::ConnectTo
7080#[derive(Debug)]
7081pub struct ByteStreams<OB, IB> {
7082 outgoing: OB,
7083 incoming: IB,
7084}
7085
7086impl<OB, IB> ByteStreams<OB, IB>
7087where
7088 OB: AsyncWrite + Send + 'static,
7089 IB: AsyncRead + Send + 'static,
7090{
7091 /// Create a new byte stream transport.
7092 pub fn new(outgoing: OB, incoming: IB) -> Self {
7093 Self { outgoing, incoming }
7094 }
7095
7096 fn into_lines(
7097 self,
7098 ) -> Lines<
7099 impl futures::Sink<String, Error = std::io::Error> + Send + 'static,
7100 impl futures::Stream<Item = std::io::Result<String>> + Send + 'static,
7101 > {
7102 use futures::AsyncBufReadExt;
7103 use futures::io::BufReader;
7104 let Self { outgoing, incoming } = self;
7105
7106 let incoming_lines = Box::pin(BufReader::new(incoming).lines());
7107 let outgoing_lines = transport_actor::LineWriter::new(outgoing);
7108
7109 Lines::new(outgoing_lines, incoming_lines)
7110 }
7111}
7112
7113#[cfg(any(
7114 all(
7115 any(feature = "process", feature = "stdio"),
7116 not(target_family = "wasm")
7117 ),
7118 test
7119))]
7120pub(crate) async fn write_line<W>(writer: &mut W, line: String) -> std::io::Result<()>
7121where
7122 W: AsyncWrite + Unpin + ?Sized,
7123{
7124 use futures::AsyncWriteExt as _;
7125
7126 let mut bytes = line.into_bytes();
7127 bytes.push(b'\n');
7128 writer.write_all(&bytes).await?;
7129 writer.flush().await
7130}
7131
7132impl<OB, IB, R: Role> ConnectTo<R> for ByteStreams<OB, IB>
7133where
7134 OB: AsyncWrite + Send + 'static,
7135 IB: AsyncRead + Send + 'static,
7136{
7137 async fn connect_to(self, client: impl ConnectTo<R::Counterpart>) -> Result<(), crate::Error> {
7138 ConnectTo::<R>::connect_to(self.into_lines(), client).await
7139 }
7140
7141 fn into_channel_and_future(self) -> (Channel, Option<crate::ConnectionDriver>) {
7142 ConnectTo::<R>::into_channel_and_future(self.into_lines())
7143 }
7144}
7145
7146/// A channel endpoint representing one side of a bidirectional JSON-RPC transport.
7147///
7148/// A channel carries complete TransportFrame values, preserving batch boundaries
7149/// across in-process components and transport adapters. Malformed wire input is an
7150/// explicit frame; failures while driving a physical transport are returned by that
7151/// transport's future.
7152///
7153/// # Example
7154///
7155/// ```no_run
7156/// # use agent_client_protocol::UntypedRole;
7157/// # use agent_client_protocol::Channel;
7158/// # async fn example() -> Result<(), agent_client_protocol::Error> {
7159/// let (channel_a, _channel_b) = Channel::duplex();
7160///
7161/// UntypedRole.builder()
7162/// .name("connection-a")
7163/// .connect_to(channel_a)
7164/// .await?;
7165/// # Ok(())
7166/// # }
7167/// ```
7168#[derive(Debug)]
7169pub struct Channel {
7170 /// Receives frames from the counterpart.
7171 pub rx: mpsc::UnboundedReceiver<TransportFrame>,
7172 /// Sends frames to the counterpart.
7173 pub tx: mpsc::UnboundedSender<TransportFrame>,
7174}
7175
7176impl Channel {
7177 /// Create a pair of connected channel endpoints.
7178 ///
7179 /// Frames sent through either endpoint are received by the other endpoint.
7180 #[must_use]
7181 pub fn duplex() -> (Self, Self) {
7182 let (a_tx, b_rx) = mpsc::unbounded();
7183 let (b_tx, a_rx) = mpsc::unbounded();
7184
7185 (Self { rx: a_rx, tx: a_tx }, Self { rx: b_rx, tx: b_tx })
7186 }
7187
7188 /// Copy frames from `rx` to `tx` until the input closes.
7189 ///
7190 /// # Errors
7191 ///
7192 /// Returns an error if the receiving endpoint closes before the input.
7193 pub(crate) async fn copy(mut self) -> Result<(), crate::Error> {
7194 while let Some(frame) = self.rx.next().await {
7195 self.tx
7196 .unbounded_send(frame)
7197 .map_err(crate::util::internal_error)?;
7198 }
7199 Ok(())
7200 }
7201
7202 /// Copy output concurrently with its owning driver, then drain accepted frames.
7203 /// Passive endpoints instead retain the channel's independent half-close lifetime.
7204 pub(crate) async fn copy_with_driver(
7205 self,
7206 driver: Option<crate::ConnectionDriver>,
7207 ) -> Result<(), crate::Error> {
7208 self.copy_with_driver_until(driver, future::pending()).await
7209 }
7210
7211 /// After the destination's owned foreground finishes, keep driving source
7212 /// errors and sink work, but never deliver queued or new input to it.
7213 pub(crate) async fn copy_with_driver_until(
7214 mut self,
7215 mut driver: Option<crate::ConnectionDriver>,
7216 stop_delivery: impl Future<Output = ()>,
7217 ) -> Result<(), crate::Error> {
7218 let mut stop_delivery = pin!(stop_delivery);
7219 let mut delivering = true;
7220 let mut done = false;
7221 loop {
7222 let event = future::poll_fn(|cx| {
7223 if delivering && stop_delivery.as_mut().poll(cx).is_ready() {
7224 delivering = false;
7225 }
7226 // Driver errors remain authoritative even when stop or EOF is ready.
7227 if !done
7228 && let Some(driver) = driver.as_mut()
7229 && let std::task::Poll::Ready(result) = std::pin::Pin::new(driver).poll(cx)
7230 {
7231 return std::task::Poll::Ready(Either::Left(result));
7232 }
7233 if !delivering && driver.is_none() {
7234 return std::task::Poll::Ready(Either::Right(None));
7235 }
7236 self.rx.poll_next_unpin(cx).map(Either::Right)
7237 })
7238 .await;
7239 let frame = match event {
7240 Either::Left(result) => {
7241 result?;
7242 done = true;
7243 self.rx.close();
7244 continue;
7245 }
7246 Either::Right(frame) => frame,
7247 };
7248 let Some(frame) = frame else {
7249 break;
7250 };
7251 if delivering {
7252 self.tx
7253 .unbounded_send(frame)
7254 .map_err(crate::util::internal_error)?;
7255 }
7256 }
7257 // Propagate this half-close before waiting for a still-running driver.
7258 drop(self);
7259 if !done && let Some(driver) = driver {
7260 driver.await?;
7261 }
7262 Ok(())
7263 }
7264
7265 /// Bridge two endpoints while inspecting every valid message.
7266 ///
7267 /// Observers are invoked in source order, including for each valid member of
7268 /// a batch. The original frame is forwarded unchanged after inspection.
7269 ///
7270 /// # Errors
7271 ///
7272 /// Returns an observer error or an error if a destination closes before its
7273 /// source.
7274 pub async fn bridge_with_inspection(
7275 left: Self,
7276 right: Self,
7277 mut left_to_right: impl FnMut(&RawJsonRpcMessage) -> Result<(), crate::Error> + Send,
7278 mut right_to_left: impl FnMut(&RawJsonRpcMessage) -> Result<(), crate::Error> + Send,
7279 ) -> Result<(), crate::Error> {
7280 let Self {
7281 rx: mut left_rx,
7282 tx: left_tx,
7283 } = left;
7284 let Self {
7285 rx: mut right_rx,
7286 tx: right_tx,
7287 } = right;
7288
7289 let left_to_right = async move {
7290 while let Some(frame) = left_rx.next().await {
7291 frame.inspect_messages(&mut left_to_right)?;
7292 right_tx
7293 .unbounded_send(frame)
7294 .map_err(crate::util::internal_error)?;
7295 }
7296 Ok::<(), crate::Error>(())
7297 };
7298 let right_to_left = async move {
7299 while let Some(frame) = right_rx.next().await {
7300 frame.inspect_messages(&mut right_to_left)?;
7301 left_tx
7302 .unbounded_send(frame)
7303 .map_err(crate::util::internal_error)?;
7304 }
7305 Ok::<(), crate::Error>(())
7306 };
7307
7308 futures::try_join!(left_to_right, right_to_left)?;
7309 Ok(())
7310 }
7311}
7312
7313impl<R: Role> ConnectTo<R> for Channel {
7314 async fn connect_to(self, client: impl ConnectTo<R::Counterpart>) -> Result<(), crate::Error> {
7315 let (client_channel, client_future) = client.into_channel_and_future();
7316
7317 let passive = client_future.is_none();
7318 let outgoing = Box::pin(
7319 Channel {
7320 rx: client_channel.rx,
7321 tx: self.tx,
7322 }
7323 .copy_with_driver(client_future),
7324 );
7325 let incoming = Box::pin(
7326 Channel {
7327 rx: self.rx,
7328 tx: client_channel.tx,
7329 }
7330 .copy(),
7331 );
7332 if passive {
7333 futures::try_join!(outgoing, incoming)?;
7334 return Ok(());
7335 }
7336
7337 match future::select(outgoing, incoming).await {
7338 Either::Left((result, _)) => result,
7339 Either::Right((result, outgoing)) => {
7340 result?;
7341 outgoing.await
7342 }
7343 }
7344 }
7345
7346 fn into_channel_and_future(self) -> (Channel, Option<crate::ConnectionDriver>) {
7347 (self, None)
7348 }
7349}
7350
7351#[cfg(test)]
7352mod tests {
7353 use super::*;
7354
7355 #[test]
7356 fn protected_cleanup_keeps_scoped_runners_polled_on_every_shutdown_path() {
7357 #[derive(Clone, Copy, Debug)]
7358 enum Stop {
7359 ForegroundSuccess,
7360 ForegroundError,
7361 InputEof,
7362 TransportError,
7363 TaskError,
7364 RunnerError,
7365 SupervisorError,
7366 }
7367
7368 struct Dropped(Arc<AtomicBool>);
7369 impl Drop for Dropped {
7370 fn drop(&mut self) {
7371 self.0.store(true, Ordering::Release);
7372 }
7373 }
7374
7375 for stop in [
7376 Stop::ForegroundSuccess,
7377 Stop::ForegroundError,
7378 Stop::InputEof,
7379 Stop::TransportError,
7380 Stop::TaskError,
7381 Stop::RunnerError,
7382 Stop::SupervisorError,
7383 ] {
7384 let cleaned = Arc::new(AtomicBool::new(false));
7385 let disposable_dropped = Arc::new(AtomicBool::new(false));
7386 let close_finished = Arc::new(AtomicBool::new(false));
7387 let (cleanup_tx, cleanup_rx) = oneshot::channel::<()>();
7388 let (scoped_done_tx, scoped_done_rx) = completion_signal();
7389 let (stop_tx, stop_rx) = oneshot::channel::<()>();
7390 let stop_signal = stop_rx.map(|_| ()).boxed().shared();
7391 let (incoming_tx, incoming_rx) = mpsc::unbounded();
7392 let outgoing = futures::sink::unfold((), |(), _line: String| {
7393 future::ready(Ok::<_, std::io::Error>(()))
7394 });
7395 let builder = Client
7396 .builder()
7397 .with_spawned({
7398 let cleaned = cleaned.clone();
7399 async move |cx: ConnectionTo<Agent>| {
7400 cx.shutdown_requested().await;
7401 // This stands in for the actual scoped native operation:
7402 // its async cleanup only advances if this runner is polled.
7403 cleanup_rx.await.unwrap();
7404 cleaned.store(true, Ordering::Release);
7405 let _ = scoped_done_tx.send(());
7406 Ok(())
7407 }
7408 })
7409 .with_spawned({
7410 let stop_signal = stop_signal.clone();
7411 async move |_cx| {
7412 stop_signal.await;
7413 if matches!(stop, Stop::RunnerError) {
7414 Err(crate::Error::internal_error().data("runner failure"))
7415 } else {
7416 future::pending().await
7417 }
7418 }
7419 })
7420 .on_close({
7421 let close_finished = close_finished.clone();
7422 let scoped_done = scoped_done_rx.clone();
7423 async move |cx: ConnectionTo<Agent>| {
7424 // EOF cancellation must precede, not await, close callbacks.
7425 cx.shutdown_requested().await;
7426 assert!(!cx.is_incoming_closed());
7427 scoped_done.await;
7428 close_finished.store(true, Ordering::Release);
7429 Ok(())
7430 }
7431 });
7432 let (connection, driver) =
7433 builder.into_connection_and_future(Lines::new(outgoing, incoming_rx), false, {
7434 let stop_signal = stop_signal.clone();
7435 async move |cx| {
7436 if matches!(stop, Stop::InputEof) {
7437 cx.incoming_closed().await;
7438 return Ok(());
7439 }
7440 stop_signal.await;
7441 match stop {
7442 Stop::ForegroundSuccess | Stop::SupervisorError => Ok(()),
7443 Stop::ForegroundError => {
7444 Err(crate::Error::internal_error().data("foreground failure"))
7445 }
7446 _ => future::pending().await,
7447 }
7448 }
7449 });
7450 let disposable = Dropped(disposable_dropped.clone());
7451 connection
7452 .spawn(async move {
7453 let _disposable = disposable;
7454 future::pending().await
7455 })
7456 .unwrap();
7457 connection
7458 .spawn({
7459 let stop_signal = stop_signal.clone();
7460 async move {
7461 stop_signal.await;
7462 if matches!(stop, Stop::TaskError) {
7463 Err(crate::Error::internal_error().data("task failure"))
7464 } else {
7465 future::pending().await
7466 }
7467 }
7468 })
7469 .unwrap();
7470 connection
7471 .spawn_protected({
7472 let connection = connection.clone();
7473 async move {
7474 connection.shutdown_requested().await;
7475 scoped_done_rx.await;
7476 if matches!(stop, Stop::SupervisorError) {
7477 Err(crate::Error::internal_error().data("supervisor failure"))
7478 } else {
7479 Ok(())
7480 }
7481 }
7482 })
7483 .unwrap();
7484 let mut driver = Box::pin(driver);
7485 assert!(driver.as_mut().now_or_never().is_none(), "{stop:?}");
7486 assert!(connection.shutdown_requested().now_or_never().is_none());
7487 let _ = stop_tx.send(());
7488 let incoming_tx = match stop {
7489 Stop::InputEof => {
7490 drop(incoming_tx);
7491 None
7492 }
7493 Stop::TransportError => {
7494 incoming_tx
7495 .unbounded_send(Err(std::io::Error::other("transport failure")))
7496 .unwrap();
7497 Some(incoming_tx)
7498 }
7499 _ => Some(incoming_tx),
7500 };
7501 for _ in 0..10 {
7502 assert!(driver.as_mut().now_or_never().is_none(), "{stop:?}");
7503 if connection.shutdown_requested().now_or_never().is_some() {
7504 break;
7505 }
7506 }
7507 assert!(
7508 connection.shutdown_requested().now_or_never().is_some(),
7509 "{stop:?}"
7510 );
7511 assert!(!cleaned.load(Ordering::Acquire), "{stop:?}");
7512 assert!(!disposable_dropped.load(Ordering::Acquire), "{stop:?}");
7513 cleanup_tx.send(()).unwrap();
7514 // Task acknowledgments may wake an actor already polled in this turn.
7515 // Bound the probe so a broken scoped-runner join fails, not hangs.
7516 let mut result = None;
7517 for _ in 0..10 {
7518 result = driver.as_mut().now_or_never();
7519 if result.is_some() {
7520 break;
7521 }
7522 }
7523 let result =
7524 result.unwrap_or_else(|| panic!("driver did not finish owned cleanup: {stop:?}"));
7525 match stop {
7526 Stop::ForegroundSuccess | Stop::InputEof => result.unwrap(),
7527 _ => {
7528 let error = result.expect_err("shutdown must preserve the first error");
7529 let expected = match stop {
7530 Stop::ForegroundError => "foreground failure",
7531 Stop::TransportError => "transport failure",
7532 Stop::TaskError => "task failure",
7533 Stop::RunnerError => "runner failure",
7534 Stop::SupervisorError => "supervisor failure",
7535 _ => unreachable!(),
7536 };
7537 assert!(
7538 error.data.unwrap().to_string().contains(expected),
7539 "{stop:?}"
7540 );
7541 }
7542 }
7543 assert!(cleaned.load(Ordering::Acquire), "{stop:?}");
7544 assert!(disposable_dropped.load(Ordering::Acquire), "{stop:?}");
7545 assert_eq!(
7546 close_finished.load(Ordering::Acquire),
7547 matches!(stop, Stop::InputEof),
7548 "{stop:?}",
7549 );
7550 assert!(connection.spawn_protected(async { Ok(()) }).is_err());
7551 drop(incoming_tx);
7552 }
7553 }
7554
7555 #[test]
7556 fn protected_operation_acknowledgments_are_reaped_and_join_seals_registration() {
7557 let (connection, _message_rx, _pending_replies) = connection_for_response_hook_tests();
7558 // The helper drops its task receiver, so use a live receiver for this probe.
7559 let (task_tx, mut task_rx) = mpsc::unbounded();
7560 let connection = ConnectionTo {
7561 task_tx,
7562 ..connection
7563 };
7564 for _ in 0..100 {
7565 connection.spawn_protected(async { Ok(()) }).unwrap();
7566 assert_eq!(
7567 connection
7568 .protected_operations
7569 .lock()
7570 .unwrap()
7571 .pending
7572 .len(),
7573 1
7574 );
7575 let task = task_rx.next().now_or_never().unwrap().unwrap();
7576 futures::executor::block_on(task.run_for_test()).unwrap();
7577 }
7578 assert!(
7579 connection
7580 .wait_protected_operations()
7581 .now_or_never()
7582 .is_some()
7583 );
7584 assert!(
7585 connection
7586 .wait_protected_operations()
7587 .now_or_never()
7588 .is_some()
7589 );
7590 assert!(
7591 connection
7592 .protected_operations
7593 .lock()
7594 .unwrap()
7595 .pending
7596 .is_empty()
7597 );
7598 assert!(connection.spawn_protected(async { Ok(()) }).is_err());
7599 assert!(task_rx.next().now_or_never().is_none());
7600 }
7601
7602 #[test]
7603 fn dropping_unused_finish_signal_preserves_physical_half_closes() {
7604 let outgoing = futures::sink::unfold((), |(), _line: String| {
7605 future::ready(Ok::<_, std::io::Error>(()))
7606 });
7607 let (incoming_tx, incoming_rx) = mpsc::unbounded();
7608 let (Channel { mut rx, tx }, mut driver) =
7609 Lines::new(outgoing, incoming_rx).into_channel_transport();
7610
7611 drop(
7612 driver
7613 .take_finish()
7614 .expect("built-in Lines driver is finishable"),
7615 );
7616 drop(tx);
7617 assert!((&mut driver).now_or_never().is_none());
7618 incoming_tx
7619 .unbounded_send(Ok(
7620 r#"{"jsonrpc":"2.0","method":"test/after-output-eof"}"#.into()
7621 ))
7622 .unwrap();
7623 assert!((&mut driver).now_or_never().is_none());
7624 assert!(rx.next().now_or_never().unwrap().is_some());
7625
7626 drop(incoming_tx);
7627 futures::executor::block_on(driver).unwrap();
7628 assert!(rx.next().now_or_never().unwrap().is_none());
7629 }
7630
7631 #[test]
7632 fn explicit_physical_finish_does_not_hide_a_ready_read_error() {
7633 let outgoing = futures::sink::unfold((), |(), _line: String| {
7634 future::ready(Ok::<_, std::io::Error>(()))
7635 });
7636 let incoming = futures::stream::iter([Err(std::io::Error::other("finish read failed"))]);
7637 let (_channel, mut driver) = Lines::new(outgoing, incoming).into_channel_transport();
7638 assert!(driver.request_finish());
7639
7640 let error = futures::executor::block_on(driver).unwrap_err();
7641 assert_eq!(
7642 error
7643 .data
7644 .and_then(|value| value.as_str().map(str::to_owned)),
7645 Some("finish read failed".into())
7646 );
7647 }
7648
7649 #[cfg(feature = "unstable_protocol_v2")]
7650 fn connection_with_task_receiver() -> (
7651 ConnectionTo<crate::role::UntypedRole>,
7652 mpsc::UnboundedReceiver<Task>,
7653 ) {
7654 let (message_tx, _message_rx) = mpsc::unbounded();
7655 let (task_tx, task_rx) = mpsc::unbounded();
7656 let (dynamic_handler_tx, _dynamic_handler_rx) = mpsc::unbounded();
7657 let transport_completion: SharedTransportCompletion =
7658 future::ready(Ok::<(), crate::Error>(())).boxed().shared();
7659 let pending_replies = PendingReplies::default();
7660
7661 (
7662 ConnectionTo::new(
7663 crate::role::UntypedRole,
7664 message_tx,
7665 task_tx,
7666 dynamic_handler_tx,
7667 transport_completion,
7668 pending_replies.registrar(),
7669 ProtocolMode::disabled(),
7670 ),
7671 task_rx,
7672 )
7673 }
7674
7675 #[cfg(feature = "unstable_protocol_v2")]
7676 #[test]
7677 fn v2_builder_exposes_typed_context_to_user_callbacks() {
7678 fn assert_v2_context(_connection: &V2ConnectionTo<Agent>) {}
7679
7680 let _builder = Client
7681 .v2()
7682 .on_receive_request(
7683 async |_request: UntypedMessage, _responder, connection| {
7684 assert_v2_context(&connection);
7685 Ok(())
7686 },
7687 crate::on_receive_request!(),
7688 )
7689 .on_receive_notification(
7690 async |_notification: UntypedMessage, connection| {
7691 assert_v2_context(&connection);
7692 Ok(())
7693 },
7694 crate::on_receive_notification!(),
7695 )
7696 .on_receive_dispatch(
7697 async |_dispatch: Dispatch<UntypedMessage, UntypedMessage>, connection| {
7698 assert_v2_context(&connection);
7699 Ok(())
7700 },
7701 crate::on_receive_dispatch!(),
7702 )
7703 .on_receive_request_from(
7704 Agent,
7705 async |_request: UntypedMessage, _responder, connection| {
7706 assert_v2_context(&connection);
7707 Ok(())
7708 },
7709 crate::on_receive_request!(),
7710 )
7711 .on_receive_notification_from(
7712 Agent,
7713 async |_notification: UntypedMessage, connection| {
7714 assert_v2_context(&connection);
7715 Ok(())
7716 },
7717 crate::on_receive_notification!(),
7718 )
7719 .on_receive_dispatch_from(
7720 Agent,
7721 async |_dispatch: Dispatch<UntypedMessage, UntypedMessage>, connection| {
7722 assert_v2_context(&connection);
7723 Ok(())
7724 },
7725 crate::on_receive_dispatch!(),
7726 )
7727 .with_spawned(async |connection| {
7728 assert_v2_context(&connection);
7729 Ok(())
7730 })
7731 .on_close(async |connection| {
7732 assert_v2_context(&connection);
7733 Ok(())
7734 });
7735 }
7736
7737 #[cfg(feature = "unstable_protocol_v2")]
7738 #[test]
7739 fn proxy_builders_select_exact_proxy_protocol_guards() -> Result<(), crate::Error> {
7740 use crate::schema::ProtocolVersion;
7741
7742 for (mode, selected, unsupported) in [
7743 (
7744 Proxy.builder().protocol_mode,
7745 ProtocolVersion::V1,
7746 ProtocolVersion::V2,
7747 ),
7748 (
7749 Proxy.v2().protocol_mode,
7750 ProtocolVersion::V2,
7751 ProtocolVersion::V1,
7752 ),
7753 ] {
7754 assert_eq!(mode.api_protocol_version(), Some(selected));
7755
7756 let error = ProtocolCompat::new(mode)
7757 .incoming_message(UntypedMessage::new(
7758 "_proxy/initialize",
7759 serde_json::json!({ "protocolVersion": unsupported }),
7760 )?)
7761 .expect_err("a proxy builder must reject the other protocol version");
7762 let data = error
7763 .data
7764 .as_ref()
7765 .and_then(|data| data.as_str())
7766 .unwrap_or_default();
7767 assert!(
7768 data.contains(&format!("only supports ACP protocol version {selected}")),
7769 "{error:?}"
7770 );
7771 }
7772
7773 Ok(())
7774 }
7775
7776 #[cfg(feature = "unstable_protocol_v2")]
7777 #[test]
7778 fn v2_proxy_rejects_explicitly_prewrapped_initialize_request() {
7779 let (message_tx, message_rx) = mpsc::unbounded();
7780 let (task_tx, _task_rx) = mpsc::unbounded();
7781 let (dynamic_handler_tx, _dynamic_handler_rx) = mpsc::unbounded();
7782 let transport_completion: SharedTransportCompletion =
7783 future::ready(Ok::<(), crate::Error>(())).boxed().shared();
7784 let pending_replies = PendingReplies::default();
7785 let connection = ConnectionTo::new(
7786 crate::Conductor,
7787 message_tx,
7788 task_tx,
7789 dynamic_handler_tx,
7790 transport_completion,
7791 pending_replies.registrar(),
7792 ProtocolMode::v2_proxy(),
7793 );
7794
7795 let request = crate::schema::SuccessorMessage {
7796 message: UntypedMessage::new(
7797 "initialize",
7798 serde_json::json!({ "protocolVersion": crate::schema::ProtocolVersion::V1 }),
7799 )
7800 .expect("test initialize request should serialize"),
7801 meta: None,
7802 };
7803 let sent = connection.send_request_to(Agent, request);
7804
7805 let (transport_tx, mut transport_rx) = mpsc::unbounded();
7806 let mut actor = Box::pin(outgoing_actor::outgoing_protocol_actor(
7807 message_rx,
7808 pending_replies,
7809 transport_tx,
7810 ProtocolCompat::new(ProtocolMode::v2_proxy()),
7811 future::pending::<()>().boxed().shared(),
7812 ));
7813 assert!(
7814 actor.as_mut().now_or_never().is_none(),
7815 "the outgoing actor should continue after rejecting the request"
7816 );
7817 assert!(
7818 transport_rx.next().now_or_never().is_none(),
7819 "an explicitly prewrapped initialize must not reach the transport"
7820 );
7821
7822 let error = futures::executor::block_on(sent.block_task())
7823 .expect_err("connection routing must own successor wrapping");
7824 let data = error
7825 .data
7826 .as_ref()
7827 .and_then(|data| data.as_str())
7828 .unwrap_or_default();
7829 assert!(data.contains("logical `initialize`"), "{error:?}");
7830 assert!(data.contains("_proxy/successor"), "{error:?}");
7831 }
7832
7833 #[cfg(feature = "unstable_protocol_v2")]
7834 #[test]
7835 fn v2_proxy_builder_exposes_typed_context_to_user_callbacks() {
7836 fn assert_v2_context(_connection: &V2ConnectionTo<crate::Conductor>) {}
7837
7838 let _builder = Proxy
7839 .v2()
7840 .on_receive_request_from(
7841 Client,
7842 async |_request: UntypedMessage, _responder, connection| {
7843 assert_v2_context(&connection);
7844 Ok(())
7845 },
7846 crate::on_receive_request!(),
7847 )
7848 .on_receive_notification_from(
7849 Agent,
7850 async |_notification: UntypedMessage, connection| {
7851 assert_v2_context(&connection);
7852 Ok(())
7853 },
7854 crate::on_receive_notification!(),
7855 )
7856 .on_receive_dispatch_from(
7857 Client,
7858 async |_dispatch: Dispatch<UntypedMessage, UntypedMessage>, connection| {
7859 assert_v2_context(&connection);
7860 Ok(())
7861 },
7862 crate::on_receive_dispatch!(),
7863 )
7864 .with_spawned(async |connection| {
7865 assert_v2_context(&connection);
7866 Ok(())
7867 })
7868 .on_close(async |connection| {
7869 assert_v2_context(&connection);
7870 Ok(())
7871 });
7872 }
7873
7874 #[cfg(feature = "unstable_protocol_v2")]
7875 #[test]
7876 fn raw_connection_spawns_v2_builder_with_typed_child_callback() {
7877 let (parent, mut task_rx) = connection_with_task_receiver();
7878 let (transport, _peer) = Channel::duplex();
7879 let (callback_tx, callback_rx) = oneshot::channel();
7880
7881 let child: ConnectionTo<Agent> = parent
7882 .spawn_connection::<Client>(
7883 Client
7884 .v2()
7885 .with_spawned(async move |_connection: V2ConnectionTo<Agent>| {
7886 callback_tx.send(()).map_err(|()| {
7887 crate::util::internal_error("typed child callback receiver was dropped")
7888 })
7889 }),
7890 transport,
7891 )
7892 .expect("v2 child connection should be spawned");
7893
7894 let task = futures::FutureExt::now_or_never(futures::StreamExt::next(&mut task_rx))
7895 .expect("child connection task should already be queued")
7896 .expect("parent task queue should remain open");
7897 futures::executor::block_on(async {
7898 match future::select(Box::pin(task.run_for_test()), Box::pin(callback_rx)).await {
7899 Either::Right((Ok(()), child_task)) => drop(child_task),
7900 Either::Right((Err(error), _)) => {
7901 panic!("typed child callback sender was dropped: {error}")
7902 }
7903 Either::Left((result, _)) => {
7904 panic!("child connection stopped before its typed callback ran: {result:?}")
7905 }
7906 }
7907 });
7908
7909 drop(child);
7910 }
7911
7912 #[cfg(feature = "unstable_protocol_v2")]
7913 #[test]
7914 fn raw_connection_can_return_v2_context_for_spawned_builder() {
7915 let (parent, mut task_rx) = connection_with_task_receiver();
7916 let (transport, _peer) = Channel::duplex();
7917
7918 let child: V2ConnectionTo<Agent> = parent
7919 .spawn_connection_with_context(Client.v2(), transport)
7920 .expect("v2 child connection should be spawned");
7921
7922 let child_task = futures::FutureExt::now_or_never(futures::StreamExt::next(&mut task_rx))
7923 .expect("child connection task should already be queued")
7924 .expect("parent task queue should remain open");
7925
7926 drop((child, child_task));
7927 }
7928
7929 fn connection_with_dynamic_handler_receiver() -> (
7930 ConnectionTo<crate::role::UntypedRole>,
7931 mpsc::UnboundedReceiver<DynamicHandlerMessage<crate::role::UntypedRole>>,
7932 ) {
7933 let (message_tx, _message_rx) = mpsc::unbounded();
7934 let (task_tx, _task_rx) = mpsc::unbounded();
7935 let (dynamic_handler_tx, dynamic_handler_rx) = mpsc::unbounded();
7936 let transport_completion: SharedTransportCompletion =
7937 future::ready(Ok::<(), crate::Error>(())).boxed().shared();
7938 let pending_replies = PendingReplies::default();
7939
7940 (
7941 ConnectionTo::new(
7942 crate::role::UntypedRole,
7943 message_tx,
7944 task_tx,
7945 dynamic_handler_tx,
7946 transport_completion,
7947 pending_replies.registrar(),
7948 ProtocolMode::disabled(),
7949 ),
7950 dynamic_handler_rx,
7951 )
7952 }
7953
7954 struct ClaimingDynamicHandler;
7955
7956 impl HandleDispatchFrom<crate::role::UntypedRole> for ClaimingDynamicHandler {
7957 fn handle_dispatch_from(
7958 &mut self,
7959 _message: Dispatch,
7960 _connection: ConnectionTo<crate::role::UntypedRole>,
7961 ) -> impl Future<Output = Result<Handled<Dispatch>, crate::Error>> + Send {
7962 future::ready(Ok(Handled::Yes))
7963 }
7964
7965 fn describe_chain(&self) -> impl Debug {
7966 "ClaimingDynamicHandler"
7967 }
7968 }
7969
7970 fn connection_for_response_hook_tests() -> (
7971 ConnectionTo<crate::role::UntypedRole>,
7972 mpsc::UnboundedReceiver<OutgoingMessage>,
7973 PendingReplies,
7974 ) {
7975 let (message_tx, message_rx) = mpsc::unbounded();
7976 let (task_tx, _task_rx) = mpsc::unbounded();
7977 let (dynamic_handler_tx, _dynamic_handler_rx) = mpsc::unbounded();
7978 let transport_completion: SharedTransportCompletion =
7979 future::ready(Ok::<(), crate::Error>(())).boxed().shared();
7980 let pending_replies = PendingReplies::default();
7981
7982 (
7983 ConnectionTo::new(
7984 crate::role::UntypedRole,
7985 message_tx,
7986 task_tx,
7987 dynamic_handler_tx,
7988 transport_completion,
7989 pending_replies.registrar(),
7990 ProtocolMode::disabled(),
7991 ),
7992 message_rx,
7993 pending_replies,
7994 )
7995 }
7996
7997 #[cfg(feature = "unstable_protocol_v2")]
7998 fn route_test_response(
7999 request_id: RequestId,
8000 pending_replies: &PendingReplies,
8001 result: Result<serde_json::Value, crate::Error>,
8002 ) {
8003 let pending_reply = pending_replies
8004 .remove(&request_id)
8005 .expect("the request should have a pending reply");
8006 let (dispatch, _) =
8007 incoming_actor::dispatch_from_response(request_id, pending_reply, result);
8008 let Dispatch::Response(result, router) = dispatch else {
8009 panic!("expected a response dispatch");
8010 };
8011 router
8012 .route_with_result(result)
8013 .expect("response should route to the pending request");
8014 }
8015
8016 #[cfg(feature = "unstable_protocol_v2")]
8017 #[test]
8018 fn response_hook_runs_when_success_is_routed_before_consumption() {
8019 let (connection, _message_rx, pending_replies) = connection_for_response_hook_tests();
8020 let hook_ran = Arc::new(AtomicBool::new(false));
8021 let sent = connection.send_request_to_with_response_hook_after(
8022 crate::role::UntypedRole,
8023 UntypedMessage::new("hooked", serde_json::json!({}))
8024 .expect("test request should serialize"),
8025 future::ready(Ok(())),
8026 {
8027 let hook_ran = hook_ran.clone();
8028 move |response| {
8029 assert_eq!(response, &serde_json::json!({"ok": true}));
8030 hook_ran.store(true, Ordering::Release);
8031 Ok(())
8032 }
8033 },
8034 );
8035 let request_id = sent.id().clone();
8036
8037 route_test_response(
8038 request_id,
8039 &pending_replies,
8040 Ok(serde_json::json!({"ok": true})),
8041 );
8042
8043 assert!(hook_ran.load(Ordering::Acquire));
8044 assert_eq!(
8045 futures::executor::block_on(sent.block_task())
8046 .expect("routed response should remain consumable"),
8047 serde_json::json!({"ok": true})
8048 );
8049 }
8050
8051 #[cfg(feature = "unstable_protocol_v2")]
8052 #[test]
8053 fn response_hook_skips_errors_but_outlives_a_dropped_consumer() {
8054 let (connection, _message_rx, pending_replies) = connection_for_response_hook_tests();
8055 let peer_error_hook_ran = Arc::new(AtomicBool::new(false));
8056 let peer_error = connection.send_request_to_with_response_hook_after(
8057 crate::role::UntypedRole,
8058 UntypedMessage::new("peer-error", serde_json::json!({}))
8059 .expect("test request should serialize"),
8060 future::ready(Ok(())),
8061 {
8062 let hook_ran = peer_error_hook_ran.clone();
8063 move |_| {
8064 hook_ran.store(true, Ordering::Release);
8065 Ok(())
8066 }
8067 },
8068 );
8069 let peer_error_id = peer_error.id().clone();
8070 route_test_response(
8071 peer_error_id,
8072 &pending_replies,
8073 Err(crate::Error::invalid_request()),
8074 );
8075 assert!(
8076 futures::executor::block_on(peer_error.block_task()).is_err(),
8077 "the peer error should reach the consumer"
8078 );
8079 assert!(!peer_error_hook_ran.load(Ordering::Acquire));
8080
8081 let dropped_hook_ran = Arc::new(AtomicBool::new(false));
8082 let dropped = connection.send_request_to_with_response_hook_after(
8083 crate::role::UntypedRole,
8084 UntypedMessage::new("dropped", serde_json::json!({}))
8085 .expect("test request should serialize"),
8086 future::ready(Ok(())),
8087 {
8088 let hook_ran = dropped_hook_ran.clone();
8089 move |_| {
8090 hook_ran.store(true, Ordering::Release);
8091 Ok(())
8092 }
8093 },
8094 );
8095 let dropped_id = dropped.id().clone();
8096 drop(dropped);
8097 route_test_response(
8098 dropped_id,
8099 &pending_replies,
8100 Ok(serde_json::json!({"ok": true})),
8101 );
8102 assert!(dropped_hook_ran.load(Ordering::Acquire));
8103 }
8104
8105 #[cfg(feature = "unstable_protocol_v2")]
8106 #[test]
8107 fn response_hook_failure_replaces_the_success_result() {
8108 let (connection, _message_rx, pending_replies) = connection_for_response_hook_tests();
8109 let sent = connection.send_request_to_with_response_hook_after(
8110 crate::role::UntypedRole,
8111 UntypedMessage::new("hook-failure", serde_json::json!({}))
8112 .expect("test request should serialize"),
8113 future::ready(Ok(())),
8114 |_| Err(crate::Error::internal_error().data("response hook failed")),
8115 );
8116 let request_id = sent.id().clone();
8117 route_test_response(
8118 request_id,
8119 &pending_replies,
8120 Ok(serde_json::json!({"ok": true})),
8121 );
8122
8123 let error = futures::executor::block_on(sent.block_task())
8124 .expect_err("the hook failure should replace the successful response");
8125 assert_eq!(error.code, crate::ErrorCode::InternalError);
8126 assert_eq!(error.data, Some(serde_json::json!("response hook failed")));
8127 }
8128
8129 #[test]
8130 fn ordered_request_waits_for_readiness_before_publication() {
8131 let (connection, message_rx, pending_replies) = connection_for_response_hook_tests();
8132 let (ready_tx, ready_rx) = oneshot::channel();
8133 let sent = connection.send_ordered_request_to_after(
8134 crate::role::UntypedRole,
8135 UntypedMessage::new("after-ready", serde_json::json!({}))
8136 .expect("test request should serialize"),
8137 async move { ready_rx.await.map_err(crate::Error::into_internal_error) },
8138 );
8139
8140 let (transport_tx, mut transport_rx) = mpsc::unbounded();
8141 let mut actor = Box::pin(outgoing_actor::outgoing_protocol_actor(
8142 message_rx,
8143 pending_replies,
8144 transport_tx,
8145 ProtocolCompat::new(ProtocolMode::disabled()),
8146 future::pending::<()>().boxed().shared(),
8147 ));
8148
8149 assert!(
8150 actor.as_mut().now_or_never().is_none(),
8151 "the outgoing actor should wait for readiness"
8152 );
8153 assert!(
8154 transport_rx.next().now_or_never().is_none(),
8155 "the request must not be published before readiness"
8156 );
8157
8158 ready_tx
8159 .send(())
8160 .expect("the readiness receiver should remain active");
8161 assert!(
8162 actor.as_mut().now_or_never().is_none(),
8163 "the outgoing actor should continue serving after publication"
8164 );
8165 let frame = transport_rx
8166 .next()
8167 .now_or_never()
8168 .expect("the ready request should be published")
8169 .expect("the transport queue should remain open");
8170 assert!(matches!(
8171 frame,
8172 TransportFrame::Single(RawJsonRpcMessage::Request(_))
8173 ));
8174
8175 drop(sent);
8176 }
8177
8178 #[test]
8179 fn foreground_finish_settles_unready_requests_and_preserves_ready_output_fifo() {
8180 let (connection, message_rx, pending_replies) = connection_for_response_hook_tests();
8181 let unready = connection.send_ordered_request_to_after(
8182 crate::role::UntypedRole,
8183 UntypedMessage::new("unready", serde_json::json!({})).unwrap(),
8184 future::pending(),
8185 );
8186 let unready_id = unready.id().clone();
8187 send_raw_message(
8188 &connection.message_tx,
8189 OutgoingMessage::Notification {
8190 untyped: UntypedMessage::new("first", serde_json::json!({})).unwrap(),
8191 },
8192 )
8193 .unwrap();
8194 let ready = connection.send_ordered_request_to_after(
8195 crate::role::UntypedRole,
8196 UntypedMessage::new("ready", serde_json::json!({})).unwrap(),
8197 future::ready(Ok(())),
8198 );
8199 let unready_after = connection.send_ordered_request_to_after(
8200 crate::role::UntypedRole,
8201 UntypedMessage::new("unready-after", serde_json::json!({})).unwrap(),
8202 future::pending(),
8203 );
8204 let unready_after_id = unready_after.id().clone();
8205 send_raw_message(
8206 &connection.message_tx,
8207 OutgoingMessage::Notification {
8208 untyped: UntypedMessage::new("last", serde_json::json!({})).unwrap(),
8209 },
8210 )
8211 .unwrap();
8212 let (done_tx, done_rx) = oneshot::channel();
8213 send_raw_message(
8214 &connection.message_tx,
8215 OutgoingMessage::CloseAfterDraining { done: done_tx },
8216 )
8217 .unwrap();
8218 let (transport_tx, transport_rx) = mpsc::unbounded();
8219 futures::executor::block_on(outgoing_actor::outgoing_protocol_actor(
8220 message_rx,
8221 pending_replies.clone(),
8222 transport_tx,
8223 ProtocolCompat::new(ProtocolMode::disabled()),
8224 future::ready(()).boxed().shared(),
8225 ))
8226 .unwrap();
8227 futures::executor::block_on(done_rx).unwrap();
8228 let error = futures::executor::block_on(unready.block_task())
8229 .expect_err("an unresolved gate must explicitly fail its consumer");
8230 assert!(
8231 error
8232 .data
8233 .unwrap()
8234 .to_string()
8235 .contains("foreground completed before outgoing request readiness")
8236 );
8237 assert!(!pending_replies.contains(&unready_id));
8238 let error = futures::executor::block_on(unready_after.block_task())
8239 .expect_err("each unresolved gate must fail without repolling a consumed signal");
8240 assert!(
8241 error
8242 .data
8243 .unwrap()
8244 .to_string()
8245 .contains("foreground completed before outgoing request readiness")
8246 );
8247 assert!(!pending_replies.contains(&unready_after_id));
8248 assert!(pending_replies.contains(ready.id()));
8249 let frames = futures::executor::block_on(transport_rx.collect::<Vec<_>>());
8250 let methods = frames
8251 .into_iter()
8252 .map(|frame| match frame {
8253 TransportFrame::Single(RawJsonRpcMessage::Notification(message)) => {
8254 message.method.to_string()
8255 }
8256 TransportFrame::Single(RawJsonRpcMessage::Request(message)) => {
8257 message.method.to_string()
8258 }
8259 _ => panic!("expected ready request/notification output"),
8260 })
8261 .collect::<Vec<_>>();
8262 assert_eq!(methods, ["first", "ready", "last"]);
8263 }
8264
8265 #[test]
8266 fn ordered_blocking_transform_precedes_response_acknowledgment() {
8267 let (connection, _message_rx, pending_replies) = connection_for_response_hook_tests();
8268 let sent = connection.send_ordered_request_to(
8269 crate::role::UntypedRole,
8270 UntypedMessage::new("ordered-transform", serde_json::json!({}))
8271 .expect("test request should serialize"),
8272 );
8273 let request_id = sent.id().clone();
8274 let pending_reply = pending_replies
8275 .remove(&request_id)
8276 .expect("the request should have a pending reply");
8277 let (dispatch, response_dispatch) = incoming_actor::dispatch_from_response(
8278 request_id,
8279 pending_reply,
8280 Err(crate::Error::invalid_params()),
8281 );
8282 let Dispatch::Response(result, router) = dispatch else {
8283 panic!("expected a response dispatch");
8284 };
8285 router
8286 .route_with_result(result)
8287 .expect("response should route to the pending request");
8288 let acknowledgment = response_dispatch
8289 .complete()
8290 .expect("an ordered response should wait for acknowledgment");
8291 let acknowledgment = Arc::new(Mutex::new(Some(acknowledgment)));
8292 let acknowledgment_probe = acknowledgment.clone();
8293
8294 let error =
8295 futures::executor::block_on(sent.block_task_with_ordered_result(move |result| {
8296 assert_eq!(
8297 acknowledgment_probe
8298 .lock()
8299 .expect("acknowledgment mutex poisoned")
8300 .as_mut()
8301 .expect("acknowledgment receiver should remain available")
8302 .try_recv()
8303 .expect("acknowledgment sender should remain open"),
8304 None,
8305 "the ordered response was acknowledged before its transform"
8306 );
8307 result
8308 }))
8309 .expect_err("the peer error should survive the ordered transform");
8310 assert_eq!(error.code, crate::ErrorCode::InvalidParams);
8311
8312 let acknowledgment = acknowledgment
8313 .lock()
8314 .expect("acknowledgment mutex poisoned")
8315 .take()
8316 .expect("acknowledgment receiver should remain available");
8317 futures::executor::block_on(acknowledgment)
8318 .expect("the transform should release the ordered response");
8319 }
8320
8321 #[cfg(feature = "unstable_protocol_v2")]
8322 #[test]
8323 fn outgoing_request_readiness_failure_rejects_without_publication() {
8324 let (connection, message_rx, pending_replies) = connection_for_response_hook_tests();
8325 let hook_ran = Arc::new(AtomicBool::new(false));
8326 let sent = connection.send_request_to_with_response_hook_after(
8327 crate::role::UntypedRole,
8328 UntypedMessage::new("never-published", serde_json::json!({}))
8329 .expect("test request should serialize"),
8330 future::ready(Err(crate::Error::internal_error().data("readiness failed"))),
8331 {
8332 let hook_ran = hook_ran.clone();
8333 move |_| {
8334 hook_ran.store(true, Ordering::Release);
8335 Ok(())
8336 }
8337 },
8338 );
8339
8340 let (transport_tx, mut transport_rx) = mpsc::unbounded();
8341 let mut actor = Box::pin(outgoing_actor::outgoing_protocol_actor(
8342 message_rx,
8343 pending_replies,
8344 transport_tx,
8345 ProtocolCompat::new(ProtocolMode::disabled()),
8346 future::pending::<()>().boxed().shared(),
8347 ));
8348
8349 assert!(
8350 actor.as_mut().now_or_never().is_none(),
8351 "the outgoing actor should continue serving after rejecting the request"
8352 );
8353 assert!(
8354 transport_rx.next().now_or_never().is_none(),
8355 "a request whose readiness failed must not be published"
8356 );
8357 let error = futures::executor::block_on(sent.block_task())
8358 .expect_err("the readiness error should reach the request consumer");
8359 assert_eq!(error.code, crate::ErrorCode::InternalError);
8360 assert_eq!(error.data, Some(serde_json::json!("readiness failed")));
8361 assert!(!hook_ran.load(Ordering::Acquire));
8362 }
8363
8364 #[test]
8365 fn ordered_request_is_marked_before_entering_outgoing_queue() {
8366 let (message_tx, mut message_rx) = mpsc::unbounded();
8367 let (task_tx, mut task_rx) = mpsc::unbounded();
8368 let (dynamic_handler_tx, _dynamic_handler_rx) = mpsc::unbounded();
8369 let transport_completion: SharedTransportCompletion =
8370 future::ready(Ok::<(), crate::Error>(())).boxed().shared();
8371 let pending_replies = PendingReplies::default();
8372 let connection = ConnectionTo::new(
8373 crate::role::UntypedRole,
8374 message_tx,
8375 task_tx,
8376 dynamic_handler_tx,
8377 transport_completion,
8378 pending_replies.registrar(),
8379 ProtocolMode::disabled(),
8380 );
8381
8382 let sent = connection.send_ordered_request_to(
8383 crate::role::UntypedRole,
8384 UntypedMessage::new("ordered", serde_json::json!({}))
8385 .expect("test request should serialize"),
8386 );
8387 let request_id = sent.id().clone();
8388 let message = futures::FutureExt::now_or_never(futures::StreamExt::next(&mut message_rx))
8389 .expect("outgoing request should already be queued")
8390 .expect("outgoing request queue should remain open");
8391 let OutgoingMessage::Request { id, .. } = message else {
8392 panic!("expected an outgoing request");
8393 };
8394 assert_eq!(id, request_id);
8395
8396 let pending_reply = pending_replies
8397 .remove(&request_id)
8398 .expect("the request should have a pending reply");
8399 assert!(
8400 pending_reply.ordering.is_ordered(),
8401 "the response ordering barrier must be installed before publication"
8402 );
8403
8404 // Route the response before the callback is registered. The pre-set
8405 // ordering marker must hold dispatch until the callback task is
8406 // subsequently installed and completes.
8407 let (dispatch, response_dispatch) = incoming_actor::dispatch_from_response(
8408 request_id,
8409 pending_reply,
8410 Ok(serde_json::json!({"ok": true})),
8411 );
8412 let Dispatch::Response(result, router) = dispatch else {
8413 panic!("expected a response dispatch");
8414 };
8415 router
8416 .route_with_result(result)
8417 .expect("response should route to the pending request");
8418 let acknowledgment = response_dispatch
8419 .complete()
8420 .expect("an ordered response should require acknowledgment");
8421
8422 let callback_ran = Arc::new(AtomicBool::new(false));
8423 sent.on_receiving_result({
8424 let callback_ran = callback_ran.clone();
8425 async move |result| {
8426 assert_eq!(result?, serde_json::json!({"ok": true}));
8427 callback_ran.store(true, Ordering::Release);
8428 Ok(())
8429 }
8430 })
8431 .expect("ordered callback should be scheduled");
8432
8433 let task = futures::FutureExt::now_or_never(futures::StreamExt::next(&mut task_rx))
8434 .expect("callback task should already be queued")
8435 .expect("callback task queue should remain open");
8436 futures::executor::block_on(task.run_for_test()).expect("callback task should succeed");
8437 futures::executor::block_on(acknowledgment)
8438 .expect("callback completion should acknowledge dispatch");
8439 assert!(callback_ran.load(Ordering::Acquire));
8440 }
8441
8442 fn next_dynamic_handler_message<Counterpart: Role>(
8443 receiver: &mut mpsc::UnboundedReceiver<DynamicHandlerMessage<Counterpart>>,
8444 ) -> Option<DynamicHandlerMessage<Counterpart>> {
8445 futures::FutureExt::now_or_never(futures::StreamExt::next(receiver))
8446 .expect("dynamic-handler receiver should be ready")
8447 }
8448
8449 #[cfg(feature = "unstable_protocol_v2")]
8450 #[test]
8451 fn v2_dynamic_handler_guard_registers_and_removes_handler() {
8452 let (message_tx, _message_rx) = mpsc::unbounded();
8453 let (task_tx, _task_rx) = mpsc::unbounded();
8454 let (dynamic_handler_tx, mut dynamic_handler_rx) = mpsc::unbounded();
8455 let transport_completion: SharedTransportCompletion =
8456 future::ready(Ok::<(), crate::Error>(())).boxed().shared();
8457 let pending_replies = PendingReplies::default();
8458 let connection = V2ConnectionTo {
8459 inner: ConnectionTo::new(
8460 Agent,
8461 message_tx,
8462 task_tx,
8463 dynamic_handler_tx,
8464 transport_completion,
8465 pending_replies.registrar(),
8466 ProtocolMode::v2_client(),
8467 ),
8468 };
8469
8470 let guard = connection
8471 .add_dynamic_handler(NullHandler)
8472 .expect("v2 dynamic handler should register");
8473 let added_uuid = match next_dynamic_handler_message(&mut dynamic_handler_rx) {
8474 Some(DynamicHandlerMessage::AddDynamicHandler(uuid, _)) => uuid,
8475 other => panic!("expected v2 handler registration, got {other:?}"),
8476 };
8477
8478 drop(guard);
8479
8480 match next_dynamic_handler_message(&mut dynamic_handler_rx) {
8481 Some(DynamicHandlerMessage::RemoveDynamicHandler(uuid)) => {
8482 assert_eq!(uuid, added_uuid);
8483 }
8484 other => panic!("expected v2 handler removal, got {other:?}"),
8485 }
8486 }
8487
8488 #[test]
8489 fn dropping_dynamic_handler_guard_unregisters_handler() {
8490 let (connection, mut receiver) = connection_with_dynamic_handler_receiver();
8491 let guard = connection.add_dynamic_handler(NullHandler).unwrap();
8492
8493 let added_uuid = match next_dynamic_handler_message(&mut receiver) {
8494 Some(DynamicHandlerMessage::AddDynamicHandler(uuid, _)) => uuid,
8495 other => panic!("expected handler registration, got {other:?}"),
8496 };
8497
8498 drop(guard);
8499
8500 match next_dynamic_handler_message(&mut receiver) {
8501 Some(DynamicHandlerMessage::RemoveDynamicHandler(uuid)) => {
8502 assert_eq!(uuid, added_uuid);
8503 }
8504 other => panic!("expected handler removal, got {other:?}"),
8505 }
8506 }
8507
8508 #[test]
8509 fn dropping_dynamic_handler_guard_deactivates_queued_handler_immediately() {
8510 let (connection, mut receiver) = connection_with_dynamic_handler_receiver();
8511 let guard = connection
8512 .add_dynamic_handler(ClaimingDynamicHandler)
8513 .expect("dynamic handler should register");
8514 let mut handler = match next_dynamic_handler_message(&mut receiver) {
8515 Some(DynamicHandlerMessage::AddDynamicHandler(_, handler)) => handler,
8516 other => panic!("expected handler registration, got {other:?}"),
8517 };
8518
8519 drop(guard);
8520
8521 let message = Dispatch::Notification(
8522 UntypedMessage::new("stale", serde_json::json!({}))
8523 .expect("test notification should serialize"),
8524 );
8525 let handled =
8526 futures::executor::block_on(handler.dyn_handle_dispatch_from(message, connection))
8527 .expect("inactive handler should decline cleanly");
8528 assert!(matches!(handled, Handled::No { retry: false, .. }));
8529 }
8530
8531 #[test]
8532 fn dynamic_handler_barrier_acknowledges_prior_messages() {
8533 let (connection, mut receiver) = connection_with_dynamic_handler_receiver();
8534 let _guard = connection.add_dynamic_handler(NullHandler).unwrap();
8535 let mut barrier = Box::pin(connection.dynamic_handler_barrier());
8536
8537 assert!(matches!(
8538 next_dynamic_handler_message(&mut receiver),
8539 Some(DynamicHandlerMessage::AddDynamicHandler(_, _))
8540 ));
8541 assert!(
8542 barrier.as_mut().now_or_never().is_none(),
8543 "the barrier must wait for the incoming actor"
8544 );
8545
8546 let acknowledgment = match next_dynamic_handler_message(&mut receiver) {
8547 Some(DynamicHandlerMessage::AcknowledgedBarrier(acknowledgment)) => acknowledgment,
8548 other => panic!("expected acknowledged barrier, got {other:?}"),
8549 };
8550 acknowledgment
8551 .send(())
8552 .expect("the barrier receiver should remain active");
8553 futures::executor::block_on(barrier)
8554 .expect("the acknowledged dynamic-handler barrier should complete");
8555 }
8556
8557 #[test]
8558 fn detaching_dynamic_handler_guard_does_not_leak_connection() {
8559 let (connection, mut receiver) = connection_with_dynamic_handler_receiver();
8560 let guard = connection.add_dynamic_handler(NullHandler).unwrap();
8561
8562 assert!(matches!(
8563 next_dynamic_handler_message(&mut receiver),
8564 Some(DynamicHandlerMessage::AddDynamicHandler(_, _))
8565 ));
8566
8567 drop(connection);
8568 guard.detach();
8569
8570 assert!(
8571 next_dynamic_handler_message(&mut receiver).is_none(),
8572 "detach should retain the handler without retaining a connection sender"
8573 );
8574 }
8575
8576 #[tokio::test]
8577 async fn write_line_flushes_buffered_writers() {
8578 let mut writer =
8579 futures::io::BufWriter::with_capacity(4096, futures::io::Cursor::new(Vec::new()));
8580
8581 write_line(&mut writer, "message".into()).await.unwrap();
8582
8583 assert_eq!(writer.into_inner().into_inner(), b"message\n");
8584 }
8585
8586 #[test]
8587 fn peel_successor_envelopes_returns_plain_messages_unchanged() {
8588 let params = serde_json::json!({ "key": "value" });
8589 let (method, peeled) = peel_successor_envelopes("session/update", ¶ms);
8590 assert_eq!(method, "session/update");
8591 assert_eq!(peeled, ¶ms);
8592 }
8593
8594 #[test]
8595 fn peel_successor_envelopes_unwraps_nested_envelopes() {
8596 let params = serde_json::json!({
8597 "method": "_proxy/successor",
8598 "params": {
8599 "method": "$/cancel_request",
8600 "params": { "requestId": "req-1" }
8601 }
8602 });
8603 let (method, peeled) = peel_successor_envelopes("_proxy/successor", ¶ms);
8604 assert_eq!(method, "$/cancel_request");
8605 assert_eq!(peeled, &serde_json::json!({ "requestId": "req-1" }));
8606 }
8607
8608 #[test]
8609 fn peel_successor_envelopes_leaves_malformed_envelopes_intact() {
8610 // No string `method` field: the envelope cannot be peeled, so the
8611 // message is returned as-is for the handler chain to deal with.
8612 let params = serde_json::json!({ "unexpected": true });
8613 let (method, peeled) = peel_successor_envelopes("_proxy/successor", ¶ms);
8614 assert_eq!(method, "_proxy/successor");
8615 assert_eq!(peeled, ¶ms);
8616 }
8617
8618 mod cancel_request {
8619 use super::super::*;
8620
8621 fn notification(method: &str, params: serde_json::Value) -> UntypedMessage {
8622 UntypedMessage::new(method, params).expect("well-formed JSON")
8623 }
8624
8625 #[test]
8626 fn cancellation_request_id_is_extracted_from_wrapped_notifications() {
8627 let message = notification(
8628 "_proxy/successor",
8629 serde_json::json!({
8630 "method": "$/cancel_request",
8631 "params": { "requestId": "req-1" }
8632 }),
8633 );
8634 let request_id = cancellation_request_id_from_message(&message)
8635 .expect("wrapped cancel should parse");
8636 assert_eq!(request_id, Some(RequestId::Str("req-1".into())));
8637 }
8638
8639 #[test]
8640 fn malformed_successor_envelope_is_not_treated_as_cancellation() {
8641 // The envelope cannot be peeled; the message must flow on to the
8642 // handler chain instead of erroring the dispatch.
8643 let message = notification("_proxy/successor", serde_json::json!({ "bogus": true }));
8644 let request_id = cancellation_request_id_from_message(&message)
8645 .expect("malformed envelope should be left to the handler chain");
8646 assert_eq!(request_id, None);
8647 }
8648
8649 #[test]
8650 fn cancel_request_notifications_are_detected_even_when_wrapped() {
8651 let plain = notification("$/cancel_request", serde_json::json!({ "requestId": 1 }));
8652 assert!(is_cancel_request_notification(&plain));
8653
8654 let wrapped = notification(
8655 "_proxy/successor",
8656 serde_json::json!({
8657 "method": "$/cancel_request",
8658 "params": { "requestId": 1 }
8659 }),
8660 );
8661 assert!(is_cancel_request_notification(&wrapped));
8662
8663 let other_wrapped = notification(
8664 "_proxy/successor",
8665 serde_json::json!({
8666 "method": "session/update",
8667 "params": {}
8668 }),
8669 );
8670 assert!(!is_cancel_request_notification(&other_wrapped));
8671
8672 let malformed_envelope =
8673 notification("_proxy/successor", serde_json::json!({ "bogus": true }));
8674 assert!(!is_cancel_request_notification(&malformed_envelope));
8675 }
8676
8677 #[test]
8678 fn malformed_cancel_request_params_error() {
8679 let message = notification(
8680 "$/cancel_request",
8681 serde_json::json!({ "requestId": { "not": "an id" } }),
8682 );
8683 cancellation_request_id_from_message(&message)
8684 .expect_err("malformed cancel params should error");
8685 }
8686
8687 #[test]
8688 fn registry_marks_and_removes_requests() {
8689 let registry = RequestCancellationRegistry::new();
8690 let id = RequestId::Str("req-1".into());
8691
8692 let responder_cancellation = registry.register(&id);
8693 let marker = responder_cancellation.cancellation();
8694 assert!(!marker.is_cancelled());
8695
8696 assert!(registry.cancel(&id));
8697 assert!(marker.is_cancelled());
8698 assert!(responder_cancellation.cancellation().is_cancelled());
8699
8700 drop(responder_cancellation);
8701 assert!(!registry.cancel(&id), "slot should be removed on drop");
8702 }
8703
8704 #[test]
8705 fn reused_request_id_does_not_cross_wire_cancellation_state() {
8706 let registry = RequestCancellationRegistry::new();
8707 let id = RequestId::Str("dup".into());
8708
8709 // A protocol-violating peer reuses an in-flight request ID.
8710 let first = registry.register(&id);
8711 let first_marker = first.cancellation();
8712 let second = registry.register(&id);
8713 let second_marker = second.cancellation();
8714
8715 // A cancellation targets whichever request currently owns the ID.
8716 assert!(registry.cancel(&id));
8717 assert!(second_marker.is_cancelled());
8718 assert!(
8719 !first_marker.is_cancelled(),
8720 "the stale request must not observe the newer request's cancellation"
8721 );
8722
8723 // The stale responder must hand out detached markers, not the
8724 // newer request's marker.
8725 assert!(!first.cancellation().is_cancelled());
8726
8727 // Dropping the stale responder must not remove the newer
8728 // request's slot.
8729 drop(first);
8730 assert!(registry.cancel(&id), "newer slot should still be present");
8731
8732 drop(second);
8733 assert!(!registry.cancel(&id), "slot should be removed on drop");
8734 }
8735 }
8736}