diff --git a/a2a-server/src/handler.rs b/a2a-server/src/handler.rs index 76e0e038..7ca87e50 100644 --- a/a2a-server/src/handler.rs +++ b/a2a-server/src/handler.rs @@ -695,9 +695,6 @@ impl DefaultRequestHandler { _ => None, }; match running { - Some(_) if mode != StartMode::Bidi => Err(Rejection::Failed( - A2AError::invalid_request("task execution is already in progress"), - )), Some(active) => self.inject(&message, &task_id, &active, writer).await, None => self.start_delivery(message, mode, Some(writer)).await, } diff --git a/a2a-server/tests/live_message.rs b/a2a-server/tests/live_message.rs index 1112e5c1..5200fed7 100644 --- a/a2a-server/tests/live_message.rs +++ b/a2a-server/tests/live_message.rs @@ -550,12 +550,12 @@ async fn routed_http_messages_authorize_once_with_their_own_credentials() { .await; assert!(matches!(result, Err(error) if error.message == "caller denied")); assert_eq!(calls.load(Ordering::SeqCst), 3); - let LiveResponse::Ack(task) = handler - .send_live_request(&alice, send("finish", Some(&task_id))) - .await - .unwrap() + let StreamRequestPayload::SendMessage(request) = send("finish", Some(&task_id)).payload else { + unreachable!() + }; + let SendMessageResponse::Task(task) = handler.send_message(&alice, request).await.unwrap() else { - panic!("expected an acknowledgement"); + panic!("expected a task") }; assert_eq!(calls.load(Ordering::SeqCst), 4); assert!( @@ -1831,3 +1831,363 @@ async fn a_panic_while_handling_a_call_never_loses_or_hangs_the_calls_queued_beh } } } + +// --------------------------------------------------------------------------- +// SendMessage and SendStreamingMessage to a task that is working +// --------------------------------------------------------------------------- + +fn message_request(text: &str, task_id: &str) -> SendMessageRequest { + match send(text, Some(task_id)).payload { + StreamRequestPayload::SendMessage(request) => request, + _ => unreachable!(), + } +} + +/// Starts a task that waits for input, holding the stream that started it. +async fn working_task(handler: &DefaultRequestHandler, p: &ServiceParams) -> (String, Downlink) { + let mut stream = match send("start", None).payload { + StreamRequestPayload::SendMessage(request) => { + handler.send_streaming_message(p, request).await.unwrap() + } + _ => unreachable!(), + }; + let task_id = task_of(next(&mut stream).await).id; + assert_eq!(state_text(&next(&mut stream).await).1, "working"); + (task_id, stream) +} + +#[tokio::test] +async fn a_streaming_message_to_a_working_task_reaches_the_agent() { + let (handler, _) = handler(true); + let p = params("1.1"); + let (task_id, mut first) = working_task(&handler, &p).await; + + let mut second = handler + .send_streaming_message(&p, message_request("hello", &task_id)) + .await + .unwrap(); + // The opening task already holds the message: it is not announced again. + let opening = task_of(next(&mut second).await); + assert_eq!(opening.id, task_id); + assert_eq!(applied(&opening, "hello"), 1); + assert_eq!(state_text(&next(&mut second).await).1, "got: hello"); + // The stream that started the turn hears it too. + assert!(matches!( + next(&mut first).await, + StreamResponse::MessageUpdate(_) + )); +} + +#[tokio::test] +async fn a_blocking_message_to_a_working_task_waits_for_the_turn_it_joined() { + let (handler, _) = handler(true); + let p = params("1.1"); + let (task_id, _first) = working_task(&handler, &p).await; + + let call = { + let handler = handler.clone(); + let p = p.clone(); + let request = message_request("finish", &task_id); + tokio::spawn(async move { handler.send_message(&p, request).await }) + }; + let response = tokio::time::timeout(Duration::from_secs(5), call) + .await + .expect("the call returns when the turn ends") + .unwrap() + .unwrap(); + let SendMessageResponse::Task(task) = response else { + panic!("a task is returned"); + }; + assert_eq!(task.status.state, TaskState::Completed); + assert_eq!(applied(&task, "finish"), 1); +} + +#[tokio::test] +async fn a_message_to_a_working_task_can_return_at_once() { + let (handler, _) = handler(true); + let p = params("1.1"); + let (task_id, _first) = working_task(&handler, &p).await; + + let mut request = message_request("hello", &task_id); + request.configuration = Some(SendMessageConfiguration { + accepted_output_modes: None, + task_push_notification_config: None, + history_length: None, + timeline_length: None, + return_immediately: Some(true), + if_generation_match: None, + }); + let response = tokio::time::timeout(Duration::from_secs(5), handler.send_message(&p, request)) + .await + .expect("returns at once") + .unwrap(); + let SendMessageResponse::Task(task) = response else { + panic!("a task is returned"); + }; + assert_eq!(task.status.state, TaskState::Working); + assert_eq!(applied(&task, "hello"), 1); +} + +#[tokio::test] +async fn a_call_that_returns_at_once_does_not_wait_for_an_agent_that_ignores_input() { + let (handler, _) = handler(true); + let p = params("1.1"); + let mut stream = match send("lazy", None).payload { + StreamRequestPayload::SendMessage(request) => { + handler.send_streaming_message(&p, request).await.unwrap() + } + _ => unreachable!(), + }; + let task_id = task_of(next(&mut stream).await).id; + assert_eq!(state_text(&next(&mut stream).await).1, "lazy"); + + let mut request = message_request("hello", &task_id); + request.configuration = Some(SendMessageConfiguration { + accepted_output_modes: None, + task_push_notification_config: None, + history_length: None, + timeline_length: None, + return_immediately: Some(true), + if_generation_match: None, + }); + let response = tokio::time::timeout(Duration::from_secs(2), handler.send_message(&p, request)) + .await + .expect("returns at once") + .unwrap(); + let SendMessageResponse::Task(task) = response else { + panic!("a task is returned"); + }; + assert_eq!(applied(&task, "hello"), 1); +} + +#[tokio::test] +async fn a_blocking_call_returns_when_the_turn_it_joined_asks_for_input() { + let (handler, _) = handler(true); + let p = params("1.1"); + let mut stream = match send("chat", None).payload { + StreamRequestPayload::SendMessage(request) => { + handler.send_streaming_message(&p, request).await.unwrap() + } + _ => unreachable!(), + }; + let task_id = task_of(next(&mut stream).await).id; + assert_eq!(state_text(&next(&mut stream).await).1, "working"); + + let response = tokio::time::timeout( + Duration::from_secs(2), + handler.send_message(&p, message_request("hello", &task_id)), + ) + .await + .expect("returns at the interrupted state, though the turn goes on") + .unwrap(); + let SendMessageResponse::Task(task) = response else { + panic!("a task is returned"); + }; + assert_eq!(task.status.state, TaskState::InputRequired); + assert_eq!(applied(&task, "hello"), 1); +} + +/// A webhook that takes the connection and never answers. +async fn silent_webhook() -> String { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}/hook", listener.local_addr().unwrap()); + tokio::spawn(async move { + let mut held = Vec::new(); + loop { + let (socket, _) = listener.accept().await.unwrap(); + held.push(socket); + } + }); + url +} + +#[tokio::test] +async fn a_receiver_that_never_answers_does_not_hold_up_the_messages_of_a_working_task() { + let url = silent_webhook().await; + let handler = Arc::new( + DefaultRequestHandler::new( + LiveAgent { + takes_input: true, + release: Arc::new(Notify::new()), + }, + InMemoryTaskStore::new(), + ) + .with_capabilities(AgentCapabilities { + streaming: Some(true), + push_notifications: Some(true), + ..AgentCapabilities::default() + }) + .with_push_notifications( + a2a_server::InMemoryPushConfigStore::new(), + a2a_server::HttpPushSender::new(Some(a2a_server::HttpPushSenderConfig { + validate_urls: false, + ..Default::default() + })), + ), + ); + let p = params("1.1"); + let (task_id, _first) = working_task(&handler, &p).await; + + let mut request = message_request("one", &task_id); + request.configuration = Some(SendMessageConfiguration { + accepted_output_modes: None, + task_push_notification_config: Some(TaskPushNotificationConfig { + url, + id: Some("silent".into()), + task_id: String::new(), + token: None, + authentication: None, + tenant: None, + protocol_version: None, + }), + history_length: None, + timeline_length: None, + return_immediately: Some(true), + if_generation_match: None, + }); + let first = tokio::time::timeout(Duration::from_secs(2), handler.send_message(&p, request)) + .await + .expect("the call does not wait for the webhook"); + first.unwrap(); + let mut second = tokio::time::timeout( + Duration::from_secs(2), + handler.send_streaming_message(&p, message_request("two", &task_id)), + ) + .await + .expect("nor does the next one") + .unwrap(); + // The call that took the message answered at once, with the task holding it. + assert_eq!(applied(&task_of(next(&mut second).await), "two"), 1); +} + +/// A message that reaches a task as an executor fails is never refused for a +/// closed input queue: the queue is closed together with the execution being +/// deregistered, under the writer a delivery decides under. +#[tokio::test(flavor = "multi_thread", worker_threads = 8)] +async fn messages_racing_an_executor_that_fails_are_never_refused_for_a_closed_queue() { + for round in 0..60 { + let (handler, release) = handler(true); + let p = params("1.1"); + let mut stream = match send("lazy-err", None).payload { + StreamRequestPayload::SendMessage(request) => { + handler.send_streaming_message(&p, request).await.unwrap() + } + _ => unreachable!(), + }; + let task_id = task_of(next(&mut stream).await).id; + assert_eq!(state_text(&next(&mut stream).await).1, "lazy"); + + let stop = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let senders: Vec<_> = (0..4) + .map(|i| { + let (handler, p, task_id, stop) = + (handler.clone(), p.clone(), task_id.clone(), stop.clone()); + tokio::spawn(async move { + let mut n = 0; + let mut refused = Vec::new(); + while !stop.load(std::sync::atomic::Ordering::SeqCst) { + n += 1; + let mut request = message_request(&format!("s{i}-{n}"), &task_id); + request.configuration = Some(SendMessageConfiguration { + accepted_output_modes: None, + task_push_notification_config: None, + history_length: None, + timeline_length: None, + return_immediately: Some(true), + if_generation_match: None, + }); + if let Err(error) = handler.send_message(&p, request).await { + refused.push(error.message); + } + tokio::task::yield_now().await; + } + refused + }) + }) + .collect(); + tokio::task::yield_now().await; + // The executor fails while the senders are at it. + release.notify_one(); + tokio::time::timeout(Duration::from_secs(5), async { + while stream.next().await.is_some() {} + }) + .await + .unwrap_or_else(|_| panic!("round {round}: the failed execution's stream did not end")); + stop.store(true, std::sync::atomic::Ordering::SeqCst); + for sender in senders { + let refused = tokio::time::timeout(Duration::from_secs(5), sender) + .await + .unwrap_or_else(|_| panic!("round {round}: a sender remained blocked")) + .unwrap(); + for message in refused { + assert!( + !message.contains("no longer taking input") && !message.contains("closed"), + "round {round}: refused for a closed queue: {message}" + ); + } + } + } +} + +/// A client that answers INPUT_REQUIRED the moment it sees it reaches a turn +/// that has asked for input but not yet returned. Its answer is not swallowed: +/// the turn that follows handles it. +#[tokio::test(flavor = "multi_thread", worker_threads = 8)] +async fn an_answer_sent_as_the_turn_asks_for_input_is_handled_by_a_turn() { + for round in 0..30 { + let (handler, _) = handler(true); + let p = params("1.1"); + let mut stream = match send("ask-slow", None).payload { + StreamRequestPayload::SendMessage(request) => { + handler.send_streaming_message(&p, request).await.unwrap() + } + _ => unreachable!(), + }; + let task_id = task_of(next(&mut stream).await).id; + loop { + if let StreamResponse::StatusUpdate(update) = next(&mut stream).await { + if update.status.state == TaskState::InputRequired { + break; + } + } + } + // "quick" completes the task, which only a turn that ran can do. + let reply = tokio::time::timeout( + Duration::from_secs(5), + handler.send_message(&p, message_request("quick", &task_id)), + ) + .await + .expect("no hang") + .expect("accepted"); + let SendMessageResponse::Task(task) = reply else { + panic!("a task is returned"); + }; + let completed = tokio::time::timeout(Duration::from_secs(5), async { + loop { + let stored = handler + .get_task( + &p, + GetTaskRequest { + id: task_id.clone(), + history_length: None, + tenant: None, + timeline_length: None, + current_generation: None, + }, + ) + .await + .unwrap(); + if stored.status.state == TaskState::Completed { + return; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + }) + .await; + assert!( + completed.is_ok(), + "round {round}: the answer was recorded ({:?}) but no turn handled it", + task.status.state + ); + } +} diff --git a/a2a-server/tests/multi_client_live.rs b/a2a-server/tests/multi_client_live.rs index 51d59bf2..8a982cfb 100644 --- a/a2a-server/tests/multi_client_live.rs +++ b/a2a-server/tests/multi_client_live.rs @@ -258,8 +258,7 @@ async fn caller_context_survives_unread_handoff_and_remaining_queued_inputs() { #[tokio::test] async fn unary_streaming_and_live_inputs_carry_their_own_trusted_context() { - { - let mode = "live"; + for mode in ["unary", "streaming", "live"] { let (handler, mut receiver, _) = handler(); let (id, _owner) = start(&handler, ¶ms("alice", Some("a")), "start").await; let initial_execution = caller(seen(&mut receiver).await, "alice", "start", true); @@ -422,8 +421,7 @@ async fn unread_input_rejection_is_private_to_the_exchange_that_accepted_it() { #[tokio::test] async fn native_live_rejections_stay_private_and_unary_rejections_are_not_broadcast() { - { - let native_input = true; + for native_input in [true, false] { let (handler, _receiver, release) = handler(); let a = params("alice", Some("a")); let (id, mut owner) = start(&handler, &a, "hold-finish").await; diff --git a/a2a-server/tests/owned_inputs.rs b/a2a-server/tests/owned_inputs.rs new file mode 100644 index 00000000..d2560fd2 --- /dev/null +++ b/a2a-server/tests/owned_inputs.rs @@ -0,0 +1,263 @@ +// Copyright AGNTCY Contributors (https://github.com/agntcy) +// Copyright A2A Contributors (https://github.com/a2aproject) +// SPDX-License-Identifier: Apache-2.0 +//! Main-compatible executor signatures and owned inboxes in Tokio workers. + +use a2a::*; +use a2a_server::{ + AgentExecutor, ClientInput, DefaultRequestHandler, ExecutorContext, InMemoryTaskStore, + LiveResponse, RequestHandler, ServiceParams, +}; +use futures::{StreamExt, stream::BoxStream}; +use std::{sync::Arc, time::Duration}; +use tokio::sync::{Notify, mpsc}; +use tokio_stream::wrappers::ReceiverStream; + +fn status(task: &str, context: &str, state: TaskState, text: &str) -> StreamResponse { + StreamResponse::StatusUpdate(TaskStatusUpdateEvent { + task_id: task.into(), + context_id: context.into(), + status: TaskStatus { + state, + message: Some(Message::new(Role::Agent, vec![Part::text(text)])), + timestamp: None, + }, + metadata: None, + generation: 0, + }) +} + +struct WorkerExecutor { + end_turn: Arc, + read_old_inbox: Arc, + old_result: mpsc::UnboundedSender>, + processed: mpsc::UnboundedSender, +} + +impl AgentExecutor for WorkerExecutor { + // These are the exact method signatures from main, with no named lifetimes. + fn execute( + &self, + ctx: ExecutorContext, + ) -> BoxStream<'static, Result> { + let (task, context) = (ctx.task_id, ctx.context_id); + let text = ctx + .message + .as_ref() + .and_then(Message::text) + .unwrap_or("") + .to_string(); + let mut inputs = ctx.inputs; + let processed = self.processed.clone(); + if text == "hold" { + let read = self.read_old_inbox.clone(); + let result = self.old_result.clone(); + // Deliberately retains ownership beyond the output stream's EOF. + tokio::spawn(async move { + read.notified().await; + result.send(inputs.recv().await).unwrap(); + }); + let end = self.end_turn.clone(); + return Box::pin(async_stream::stream! { + yield Ok(status(&task, &context, TaskState::Working, "waiting")); + end.notified().await; + }); + } + if text == "finish" { + processed.send(text).unwrap(); + return Box::pin(futures::stream::once(async move { + Ok(status(&task, &context, TaskState::Completed, "done")) + })); + } + let (output, events) = mpsc::channel(8); + // Input processing and event production live in an independent task. + tokio::spawn(async move { + if output + .send(Ok(status(&task, &context, TaskState::Working, "ready"))) + .await + .is_err() + { + return; + } + while let Some(input) = inputs.recv().await { + let text = input.message.text().unwrap_or("").to_string(); + let finished = text == "finish"; + processed.send(text.clone()).unwrap(); + let event = status( + &task, + &context, + if finished { + TaskState::Completed + } else { + TaskState::Working + }, + &text, + ); + if output.send(Ok(event)).await.is_err() || finished { + break; + } + } + }); + ReceiverStream::new(events).boxed() + } + + fn cancel(&self, ctx: ExecutorContext) -> BoxStream<'static, Result> { + Box::pin(futures::stream::once(async move { + Ok(status( + &ctx.task_id, + &ctx.context_id, + TaskState::Canceled, + "canceled", + )) + })) + } +} + +fn send(text: &str, task: Option<&str>) -> StreamRequest { + let mut message = Message::new(Role::User, vec![Part::text(text)]); + message.task_id = task.map(Into::into); + StreamRequest { + tenant: None, + payload: StreamRequestPayload::SendMessage(SendMessageRequest { + message: Some(message), + tenant: None, + configuration: None, + metadata: None, + artifact_updates: vec![], + }), + } +} +fn params() -> ServiceParams { + [(SVC_PARAM_VERSION.into(), vec!["1.1".into()])].into() +} +async fn next(stream: &mut BoxStream<'static, Result>) -> StreamResponse { + tokio::time::timeout(Duration::from_secs(5), stream.next()) + .await + .unwrap() + .unwrap() + .unwrap() +} +async fn received(receiver: &mut mpsc::UnboundedReceiver) -> T { + tokio::time::timeout(Duration::from_secs(5), receiver.recv()) + .await + .unwrap() + .unwrap() +} + +#[tokio::test] +async fn detached_receiver_is_revoked_without_waiting_for_its_worker_to_finish() { + let end_turn = Arc::new(Notify::new()); + let read_old_inbox = Arc::new(Notify::new()); + let (old_result, mut old_results) = mpsc::unbounded_channel(); + let (processed, mut messages) = mpsc::unbounded_channel(); + let handler = DefaultRequestHandler::new( + WorkerExecutor { + end_turn: end_turn.clone(), + read_old_inbox: read_old_inbox.clone(), + old_result, + processed, + }, + InMemoryTaskStore::new(), + ) + .with_capabilities(AgentCapabilities { + streaming: Some(true), + ..Default::default() + }); + let p = params(); + let LiveResponse::Stream(mut stream) = handler + .send_live_request(&p, send("hold", None)) + .await + .unwrap() + else { + panic!("stream") + }; + let StreamResponse::Task(task) = next(&mut stream).await else { + panic!("task") + }; + next(&mut stream).await; // Working. + assert!(matches!( + handler + .send_live_request(&p, send("finish", Some(&task.id))) + .await + .unwrap(), + LiveResponse::Ack(_) + )); + assert!(matches!( + next(&mut stream).await, + StreamResponse::MessageUpdate(_) + )); + end_turn.notify_one(); + // The next invocation must complete while the old worker still owns its receiver. + assert_eq!(received(&mut messages).await, "finish"); + loop { + let event = next(&mut stream).await; + if match event { + StreamResponse::StatusUpdate(update) => update.status.state == TaskState::Completed, + StreamResponse::Task(task) => task.status.state == TaskState::Completed, + _ => false, + } { + break; + } + } + read_old_inbox.notify_one(); + assert!( + received(&mut old_results).await.is_none(), + "the old worker cannot steal handed-off input" + ); + assert!(messages.try_recv().is_err(), "the input is processed once"); +} + +#[tokio::test] +async fn independent_worker_receives_inputs_and_sends_events_with_main_signatures() { + let (old_result, _old_results) = mpsc::unbounded_channel(); + let (processed, mut messages) = mpsc::unbounded_channel(); + let handler = DefaultRequestHandler::new( + WorkerExecutor { + end_turn: Arc::new(Notify::new()), + read_old_inbox: Arc::new(Notify::new()), + old_result, + processed, + }, + InMemoryTaskStore::new(), + ) + .with_capabilities(AgentCapabilities { + streaming: Some(true), + ..Default::default() + }); + let p = params(); + let LiveResponse::Stream(mut stream) = handler + .send_live_request(&p, send("start", None)) + .await + .unwrap() + else { + panic!("stream") + }; + let StreamResponse::Task(task) = next(&mut stream).await else { + panic!("task") + }; + next(&mut stream).await; + for text in ["hello", "finish"] { + assert!(matches!( + handler + .send_live_request(&p, send(text, Some(&task.id))) + .await + .unwrap(), + LiveResponse::Ack(_) + )); + assert_eq!(received(&mut messages).await, text); + assert!(matches!( + next(&mut stream).await, + StreamResponse::MessageUpdate(_) + )); + let StreamResponse::StatusUpdate(update) = next(&mut stream).await else { + panic!("worker output") + }; + assert_eq!(update.status.message.unwrap().text(), Some(text)); + } + assert!( + tokio::time::timeout(Duration::from_secs(5), stream.next()) + .await + .unwrap() + .is_none() + ); +}