use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use a2a_protocol_types::events::StreamResponse;
use a2a_protocol_types::params::SendMessageConfiguration;
use a2a_protocol_types::responses::TaskListResponse;
use a2a_protocol_types::task::{Task, TaskState};
use a2a_protocol_types::{AgentCard, Message, TaskPushNotificationConfig};
use futures::StreamExt;
use futures::stream::BoxStream;
use tokio::sync::RwLock;
use tokio_util::sync::CancellationToken;
use super::card::CachedAgentCard;
use super::convert::internal_task_to_wire;
use super::error::A2aError;
use super::executor::V1Executor;
use super::processor::OutputMode;
use super::push::PushNotificationSender;
use super::run::{RunContext, adk_task_metadata};
use super::task_store::{ListTasksParams, TaskStore, TaskStoreEntry};
use crate::auth_bridge::RequestContext;
fn status_time(entry: &TaskStoreEntry) -> chrono::DateTime<chrono::Utc> {
entry
.status
.timestamp
.as_deref()
.and_then(|timestamp| chrono::DateTime::parse_from_rfc3339(timestamp).ok())
.map_or(entry.updated_at, |time| time.with_timezone(&chrono::Utc))
}
#[derive(serde::Serialize, serde::Deserialize)]
struct SendParams {
message: Message,
#[serde(default, skip_serializing_if = "Option::is_none")]
configuration: Option<SendMessageConfiguration>,
}
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct TaskQuery {
id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
history_length: Option<u32>,
}
#[derive(serde::Serialize, serde::Deserialize)]
struct TaskIdOnly {
id: String,
}
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct PushConfigCreate {
task_id: String,
config: TaskPushNotificationConfig,
}
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct PushConfigRef {
task_id: String,
id: String,
}
fn to_json<T: serde::Serialize>(value: &T) -> Result<serde_json::Value, A2aError> {
serde_json::to_value(value)
.map_err(|e| A2aError::Internal { message: format!("interceptor JSON: {e}") })
}
fn from_json<T: serde::de::DeserializeOwned>(value: serde_json::Value) -> Result<T, A2aError> {
serde_json::from_value(value)
.map_err(|e| A2aError::InvalidParams { message: format!("interceptor JSON: {e}") })
}
struct PreparedTask {
task_id: String,
context_id: String,
runs: bool,
}
fn pending_call_ids(entry: &TaskStoreEntry) -> Vec<String> {
let Some(message) = &entry.status.message else { return Vec::new() };
message
.parts
.iter()
.filter(|part| {
part.metadata
.as_ref()
.and_then(|metadata| metadata.get(super::convert::ADK_TYPE_METADATA_KEY))
.and_then(serde_json::Value::as_str)
== Some("function_call")
})
.filter_map(|part| match &part.content {
a2a_protocol_types::PartContent::Data(data) => {
data.get("id").and_then(serde_json::Value::as_str)
}
_ => None,
})
.map(str::to_owned)
.collect()
}
fn answered_calls(msg: &Message, pending: &[String]) -> Vec<String> {
msg.parts
.iter()
.filter_map(|part| super::convert::wire_part_to_adk(part).ok())
.filter_map(|part| match part {
adk_core::Part::FunctionResponse { id: Some(id), .. } if pending.contains(&id) => {
Some(id)
}
_ => None,
})
.collect()
}
fn validate_push_url(url: &str) -> Result<(), A2aError> {
let valid = reqwest::Url::parse(url).is_ok_and(|parsed| {
matches!(parsed.scheme(), "http" | "https")
&& parsed.host_str().is_some_and(|host| !host.is_empty())
});
if valid {
Ok(())
} else {
Err(A2aError::InvalidParams {
message: format!("push notification URL {url:?} is not an absolute http(s) URL"),
})
}
}
fn validate_id(id: &str, field_name: &str) -> Result<(), A2aError> {
let trimmed = id.trim();
if trimmed.is_empty() {
return Err(A2aError::InvalidParams {
message: format!("{field_name} must not be empty or whitespace-only"),
});
}
if id.len() > 256 {
return Err(A2aError::InvalidParams {
message: format!("{field_name} exceeds 256 character limit ({} chars)", id.len()),
});
}
Ok(())
}
fn validate_message(msg: &Message) -> Result<(), A2aError> {
if msg.parts.is_empty() {
return Err(A2aError::InvalidParams {
message: "message must contain at least one part".to_string(),
});
}
validate_id(&msg.id.0, "messageId")?;
if let Some(ref metadata) = msg.metadata {
let size = serde_json::to_vec(metadata).map(|v| v.len()).unwrap_or(0);
if size > 65_536 {
return Err(A2aError::InvalidParams {
message: format!("metadata exceeds 64 KB limit ({size} bytes)"),
});
}
}
Ok(())
}
type IdempotencyKey = (Option<String>, String);
pub type ExtendedCardProducer = Arc<
dyn Fn(
Option<RequestContext>,
) -> futures::future::BoxFuture<'static, Result<AgentCard, A2aError>>
+ Send
+ Sync,
>;
pub struct RequestHandler {
executor: Arc<V1Executor>,
task_store: Arc<dyn TaskStore>,
agent_card: Arc<RwLock<CachedAgentCard>>,
runner_config: Option<Arc<adk_runner::RunnerConfig>>,
idempotency_map: RwLock<HashMap<IdempotencyKey, String>>,
running: Arc<RunningTasks>,
output_mode: OutputMode,
extended_card: Option<ExtendedCardProducer>,
hooks: super::hooks::ExecutorHooks,
inactivity_timeout: Option<std::time::Duration>,
limiter: Option<Arc<super::run::RunLimiter>>,
interceptors: Vec<Arc<dyn super::interceptor::CallInterceptor>>,
}
impl RequestHandler {
pub fn new(
executor: Arc<V1Executor>,
task_store: Arc<dyn TaskStore>,
push_sender: Arc<dyn PushNotificationSender>,
agent_card: Arc<RwLock<CachedAgentCard>>,
) -> Self {
executor.set_push_sender(push_sender);
Self {
executor,
task_store,
agent_card,
runner_config: None,
idempotency_map: RwLock::new(HashMap::new()),
running: Arc::default(),
output_mode: OutputMode::default(),
extended_card: None,
hooks: super::hooks::ExecutorHooks::default(),
inactivity_timeout: None,
limiter: None,
interceptors: Vec::new(),
}
}
pub fn with_runner(
executor: Arc<V1Executor>,
task_store: Arc<dyn TaskStore>,
push_sender: Arc<dyn PushNotificationSender>,
agent_card: Arc<RwLock<CachedAgentCard>>,
runner_config: Arc<adk_runner::RunnerConfig>,
) -> Self {
executor.set_push_sender(push_sender);
Self {
executor,
task_store,
agent_card,
runner_config: Some(runner_config),
idempotency_map: RwLock::new(HashMap::new()),
running: Arc::default(),
output_mode: OutputMode::default(),
extended_card: None,
hooks: super::hooks::ExecutorHooks::default(),
inactivity_timeout: None,
limiter: None,
interceptors: Vec::new(),
}
}
#[must_use]
pub fn with_interceptor(
mut self,
interceptor: Arc<dyn super::interceptor::CallInterceptor>,
) -> Self {
self.interceptors.push(interceptor);
self
}
#[must_use]
pub fn with_inactivity_timeout(mut self, timeout: std::time::Duration) -> Self {
self.inactivity_timeout = Some(timeout);
self
}
#[must_use]
pub fn with_concurrency_limits(mut self, limits: super::run::ConcurrencyLimits) -> Self {
self.limiter = Some(super::run::RunLimiter::new(limits));
self
}
#[must_use]
pub fn with_hooks(mut self, hooks: super::hooks::ExecutorHooks) -> Self {
self.hooks = hooks;
self
}
#[must_use]
pub fn with_extended_card(mut self, producer: ExtendedCardProducer) -> Self {
self.extended_card = Some(producer);
self
}
#[must_use]
pub fn with_output_mode(mut self, mode: OutputMode) -> Self {
self.output_mode = mode;
self
}
pub fn for_caller(&self, caller: Option<RequestContext>) -> CallerScope<'_> {
CallerScope { handler: self, caller, extensions: Vec::new() }
}
pub async fn require_extensions(
&self,
requested: &super::extensions::RequestedExtensions,
) -> Result<(), A2aError> {
let card = self.agent_card.read().await;
let missing = card.card.capabilities.extensions.iter().flatten().find(|extension| {
extension.required == Some(true) && !requested.contains(&extension.uri)
});
match missing {
Some(extension) => {
Err(A2aError::ExtensionSupportRequired { uri: extension.uri.clone() })
}
None => Ok(()),
}
}
pub async fn message_send(&self, msg: Message) -> Result<Task, A2aError> {
self.for_caller(None).message_send(msg).await
}
pub async fn message_send_with(
&self,
msg: Message,
configuration: Option<&SendMessageConfiguration>,
) -> Result<Task, A2aError> {
self.for_caller(None).message_send_with(msg, configuration).await
}
pub async fn message_stream(
&self,
msg: Message,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError> {
self.for_caller(None).message_stream(msg).await
}
pub async fn message_stream_with(
&self,
msg: Message,
configuration: Option<&SendMessageConfiguration>,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError> {
self.for_caller(None).message_stream_with(msg, configuration).await
}
pub async fn tasks_get(
&self,
task_id: &str,
history_len: Option<u32>,
) -> Result<Task, A2aError> {
self.for_caller(None).tasks_get(task_id, history_len).await
}
pub async fn tasks_cancel(&self, task_id: &str) -> Result<Task, A2aError> {
self.for_caller(None).tasks_cancel(task_id).await
}
pub async fn tasks_list(&self, params: ListTasksParams) -> Result<TaskListResponse, A2aError> {
self.for_caller(None).tasks_list(params).await
}
pub async fn tasks_subscribe(
&self,
task_id: &str,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError> {
self.for_caller(None).tasks_subscribe(task_id).await
}
pub async fn push_config_create(
&self,
task_id: &str,
config: TaskPushNotificationConfig,
) -> Result<TaskPushNotificationConfig, A2aError> {
self.for_caller(None).push_config_create(task_id, config).await
}
pub async fn push_config_get(
&self,
task_id: &str,
config_id: &str,
) -> Result<TaskPushNotificationConfig, A2aError> {
self.for_caller(None).push_config_get(task_id, config_id).await
}
pub async fn push_config_list(
&self,
task_id: &str,
) -> Result<Vec<TaskPushNotificationConfig>, A2aError> {
self.for_caller(None).push_config_list(task_id).await
}
pub async fn push_config_delete(&self, task_id: &str, config_id: &str) -> Result<(), A2aError> {
self.for_caller(None).push_config_delete(task_id, config_id).await
}
pub async fn agent_card_extended(&self) -> Result<AgentCard, A2aError> {
self.agent_card_extended_for(None).await
}
pub async fn agent_card_extended_for(
&self,
caller: Option<RequestContext>,
) -> Result<AgentCard, A2aError> {
if self.agent_card.read().await.card.capabilities.extended_agent_card == Some(false) {
return Err(A2aError::UnsupportedOperation {
method: "GetExtendedAgentCard: the agent card declares no extended card"
.to_string(),
});
}
let producer =
self.extended_card.as_ref().ok_or(A2aError::ExtendedAgentCardNotConfigured)?;
producer(caller).await
}
async fn require(&self, operation: Capability) -> Result<(), A2aError> {
let capabilities = self.agent_card.read().await.card.capabilities.clone();
match operation {
Capability::Streaming(method) if capabilities.streaming == Some(false) => {
Err(A2aError::UnsupportedOperation {
method: format!("{method}: the agent card does not declare streaming"),
})
}
Capability::PushNotifications if capabilities.push_notifications == Some(false) => {
Err(A2aError::PushNotificationNotSupported)
}
_ => Ok(()),
}
}
pub fn executor(&self) -> &Arc<V1Executor> {
&self.executor
}
pub fn task_store(&self) -> &Arc<dyn TaskStore> {
&self.task_store
}
}
pub struct CallerScope<'a> {
handler: &'a RequestHandler,
caller: Option<RequestContext>,
extensions: Vec<String>,
}
impl CallerScope<'_> {
async fn intercepted<P, R, F, Fut>(
&self,
method: &'static str,
params: P,
call: F,
) -> Result<R, A2aError>
where
P: serde::Serialize + serde::de::DeserializeOwned,
R: serde::Serialize + serde::de::DeserializeOwned,
F: FnOnce(P) -> Fut,
Fut: std::future::Future<Output = Result<R, A2aError>>,
{
let interceptors = &self.handler.interceptors;
if interceptors.is_empty() {
return call(params).await;
}
let context = self.call_context(method);
let mut value = to_json(¶ms)?;
for interceptor in interceptors {
if let Some(answer) = interceptor.before(&context, &mut value).await? {
return from_json(answer);
}
}
let result = call(from_json(value)?).await?;
let mut value = to_json(&result)?;
for interceptor in interceptors.iter().rev() {
interceptor.after(&context, &mut value).await?;
}
from_json(value)
}
async fn intercepted_stream<P, F, Fut>(
&self,
method: &'static str,
params: P,
call: F,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError>
where
P: serde::Serialize + serde::de::DeserializeOwned,
F: FnOnce(P) -> Fut,
Fut: std::future::Future<
Output = Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError>,
>,
{
let interceptors = self.handler.interceptors.clone();
if interceptors.is_empty() {
return call(params).await;
}
let context = Arc::new(self.call_context(method));
let mut value = to_json(¶ms)?;
for interceptor in &interceptors {
if let Some(answer) = interceptor.before(&context, &mut value).await? {
let event: StreamResponse = from_json(answer)?;
return Ok(futures::stream::once(async move { Ok(event) }).boxed());
}
}
let stream = call(from_json(value)?).await?;
Ok(stream
.then(move |item| {
let interceptors = interceptors.clone();
let context = context.clone();
async move {
let mut value = to_json(&item?)?;
for interceptor in interceptors.iter().rev() {
interceptor.after(&context, &mut value).await?;
}
from_json(value)
}
})
.boxed())
}
fn call_context(&self, method: &'static str) -> super::interceptor::CallContext {
super::interceptor::CallContext {
method,
caller: self.caller.clone(),
extensions: self.extensions.clone(),
}
}
pub fn caller(&self) -> Option<&RequestContext> {
self.caller.as_ref()
}
#[must_use]
pub fn with_extensions(mut self, requested: super::extensions::RequestedExtensions) -> Self {
self.extensions = requested.0;
self
}
pub fn owner(&self) -> Option<&str> {
self.caller.as_ref().map(|context| context.user_id.as_str())
}
async fn owned_task(&self, task_id: &str) -> Result<TaskStoreEntry, A2aError> {
let entry = self.handler.task_store.get_task(task_id).await?;
if entry.owner() == self.owner() {
Ok(entry)
} else {
Err(A2aError::TaskNotFound { task_id: task_id.to_string() })
}
}
fn idempotency_key(&self, message_id: &str) -> IdempotencyKey {
(self.owner().map(str::to_string), message_id.to_string())
}
pub async fn message_send(&self, msg: Message) -> Result<Task, A2aError> {
self.message_send_with(msg, None).await
}
pub async fn message_send_with(
&self,
msg: Message,
configuration: Option<&SendMessageConfiguration>,
) -> Result<Task, A2aError> {
self.intercepted(
"SendMessage",
SendParams { message: msg, configuration: configuration.cloned() },
|p: SendParams| async move {
self.message_send_with_direct(p.message, p.configuration.as_ref()).await
},
)
.await
}
async fn message_send_with_direct(
&self,
msg: Message,
configuration: Option<&SendMessageConfiguration>,
) -> Result<Task, A2aError> {
validate_message(&msg)?;
let handler = self.handler;
if configuration.is_some_and(|c| c.task_push_notification_config.is_some()) {
handler.require(Capability::PushNotifications).await?;
}
let idempotency_key = self.idempotency_key(&msg.id.0);
if let Some(existing_task_id) = self.idempotent_task(&idempotency_key).await? {
return self.tasks_get_direct(&existing_task_id, None).await;
}
let permit = self.acquire_run()?;
let prepared = self.prepare_task(&msg).await?;
let task_id = prepared.task_id.clone();
if let Some(config) = configuration.and_then(|c| c.task_push_notification_config.clone()) {
self.push_config_create_direct(&task_id, config).await?;
}
handler.idempotency_map.write().await.insert(idempotency_key, task_id.clone());
if prepared.runs {
let return_immediately =
configuration.and_then(|c| c.return_immediately).unwrap_or(false);
let submitted = handler.task_store.get_task(&task_id).await?;
let run = self.start_run(&prepared, msg, permit).await?;
if return_immediately {
return internal_task_to_wire(&submitted);
}
if let Err(error) = run.await {
tracing::warn!(%task_id, %error, "A2A run did not finish");
}
}
let entry = handler.task_store.get_task(&task_id).await?;
internal_task_to_wire(&entry)
}
async fn idempotent_task(&self, key: &IdempotencyKey) -> Result<Option<String>, A2aError> {
let handler = self.handler;
let Some(existing) = handler.idempotency_map.read().await.get(key).cloned() else {
return Ok(None);
};
match self.owned_task(&existing).await {
Ok(_) => Ok(Some(existing)),
Err(A2aError::TaskNotFound { .. }) => {
handler.idempotency_map.write().await.remove(key);
Ok(None)
}
Err(error) => Err(error),
}
}
fn acquire_run(&self) -> Result<Option<super::run::RunPermit>, A2aError> {
let Some(limiter) = &self.handler.limiter else { return Ok(None) };
let scope = self.caller.as_ref().map_or("anonymous", |caller| caller.user_id.as_str());
limiter.acquire(scope).map(Some)
}
async fn start_run(
&self,
prepared: &PreparedTask,
msg: Message,
permit: Option<super::run::RunPermit>,
) -> Result<tokio::task::JoinHandle<()>, A2aError> {
let handler = self.handler;
let (task_id, context_id) = (prepared.task_id.clone(), prepared.context_id.clone());
let Some(runner_config) = handler.runner_config.clone() else {
let executor = handler.executor.clone();
executor.transition_state(&task_id, &context_id, TaskState::Working, None).await?;
executor.transition_state(&task_id, &context_id, TaskState::Completed, None).await?;
return Ok(tokio::spawn(async {}));
};
let guard = handler.running.start(&task_id, runner_config.cancellation_token.as_ref());
let cancellation = guard.token().clone();
let run = RunContext {
executor: handler.executor.clone(),
runner_config,
caller: self.caller.clone(),
output_mode: handler.output_mode,
hooks: handler.hooks.clone(),
extensions: self.extensions.clone(),
inactivity_timeout: handler.inactivity_timeout,
};
Ok(super::run::spawn(run, task_id, context_id, msg, cancellation, (guard, permit)))
}
async fn task_to_continue(&self, msg: &Message) -> Result<Option<TaskStoreEntry>, A2aError> {
if let Some(task_id) = &msg.task_id {
validate_id(&task_id.0, "taskId")?;
let entry = self.owned_task(&task_id.0).await?;
if let Some(context_id) = &msg.context_id
&& context_id.0 != entry.context_id
{
return Err(A2aError::InvalidParams {
message: format!(
"contextId {} is not the context of task {}",
context_id.0, entry.id
),
});
}
return match entry.status.state {
TaskState::InputRequired | TaskState::AuthRequired => Ok(Some(entry)),
state if is_terminal_state(state) => Err(A2aError::UnsupportedOperation {
method: format!("a message to task {} in terminal state {state:?}", entry.id),
}),
state => Err(A2aError::UnsupportedOperation {
method: format!("a message to task {} while it is {state:?}", entry.id),
}),
};
}
if let Some(ctx_id) = &msg.context_id
&& let Some(existing) = self.handler.task_store.find_task_by_context(&ctx_id.0).await?
&& existing.status.state == TaskState::InputRequired
&& existing.owner() == self.owner()
&& !answered_calls(msg, &pending_call_ids(&existing)).is_empty()
{
return Ok(Some(existing));
}
Ok(None)
}
async fn prepare_task(&self, msg: &Message) -> Result<PreparedTask, A2aError> {
let handler = self.handler;
if let Some(existing) = self.task_to_continue(msg).await? {
let pending = pending_call_ids(&existing);
let answered = answered_calls(msg, &pending);
if existing.status.state == TaskState::InputRequired
&& let Some(missing) = pending.iter().find(|id| !answered.contains(*id))
{
handler.executor.ask_again(&existing.id, msg.clone(), missing).await?;
return Ok(PreparedTask {
task_id: existing.id,
context_id: existing.context_id,
runs: false,
});
}
let claimed = handler.executor.claim_waiting_task(&existing.id, msg.clone()).await?;
return Ok(PreparedTask {
task_id: claimed.id,
context_id: claimed.context_id,
runs: true,
});
}
let task_id = uuid::Uuid::new_v4().to_string();
let context_id = msg
.context_id
.as_ref()
.map(|c| c.0.clone())
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
let metadata =
handler.runner_config.as_ref().map(|_| adk_task_metadata()).unwrap_or_default();
handler
.executor
.create_task_with_metadata(&task_id, &context_id, self.owner(), metadata)
.await?;
handler.executor.add_history_message(&task_id, msg.clone()).await?;
Ok(PreparedTask { task_id, context_id, runs: true })
}
pub async fn message_stream(
&self,
msg: Message,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError> {
self.message_stream_with(msg, None).await
}
pub async fn message_stream_with(
&self,
msg: Message,
configuration: Option<&SendMessageConfiguration>,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError> {
self.intercepted_stream(
"SendStreamingMessage",
SendParams { message: msg, configuration: configuration.cloned() },
|p: SendParams| async move {
self.message_stream_with_direct(p.message, p.configuration.as_ref()).await
},
)
.await
}
async fn message_stream_with_direct(
&self,
msg: Message,
configuration: Option<&SendMessageConfiguration>,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError> {
self.handler.require(Capability::Streaming("SendStreamingMessage")).await?;
validate_message(&msg)?;
let handler = self.handler;
if configuration.is_some_and(|c| c.task_push_notification_config.is_some()) {
handler.require(Capability::PushNotifications).await?;
}
let idempotency_key = self.idempotency_key(&msg.id.0);
if let Some(existing_task_id) = self.idempotent_task(&idempotency_key).await? {
let task = self.tasks_get_direct(&existing_task_id, None).await?;
return Ok(futures::stream::once(async move { Ok(StreamResponse::Task(task)) }).boxed());
}
let permit = self.acquire_run()?;
let prepared = self.prepare_task(&msg).await?;
let task_id = prepared.task_id.clone();
if let Some(config) = configuration.and_then(|c| c.task_push_notification_config.clone()) {
self.push_config_create_direct(&task_id, config).await?;
}
handler.idempotency_map.write().await.insert(idempotency_key, task_id.clone());
let mut updates = handler.executor.subscribe(&task_id);
let first = internal_task_to_wire(&handler.task_store.get_task(&task_id).await?)?;
let settled = ends_subscription(first.status.state);
if prepared.runs {
drop(self.start_run(&prepared, msg, permit).await?);
}
let stream = async_stream::stream! {
yield Ok(StreamResponse::Task(first));
if settled && !prepared.runs {
return;
}
while let Some(update) = updates.recv().await {
let ends = matches!(
&update,
StreamResponse::StatusUpdate(event) if ends_subscription(event.status.state)
);
yield Ok(update);
if ends {
return;
}
}
};
Ok(stream.boxed())
}
pub async fn tasks_get(
&self,
task_id: &str,
history_len: Option<u32>,
) -> Result<Task, A2aError> {
self.intercepted(
"GetTask",
TaskQuery { id: task_id.to_string(), history_length: history_len },
|p: TaskQuery| async move { self.tasks_get_direct(&p.id, p.history_length).await },
)
.await
}
async fn tasks_get_direct(
&self,
task_id: &str,
history_len: Option<u32>,
) -> Result<Task, A2aError> {
validate_id(task_id, "taskId")?;
let mut entry = self.owned_task(task_id).await?;
if let Some(len) = history_len {
let len = len as usize;
if entry.history.len() > len {
let start = entry.history.len() - len;
entry.history = entry.history[start..].to_vec();
}
}
internal_task_to_wire(&entry)
}
pub async fn tasks_cancel(&self, task_id: &str) -> Result<Task, A2aError> {
self.intercepted(
"CancelTask",
TaskIdOnly { id: task_id.to_string() },
|p: TaskIdOnly| async move { self.tasks_cancel_direct(&p.id).await },
)
.await
}
async fn tasks_cancel_direct(&self, task_id: &str) -> Result<Task, A2aError> {
validate_id(task_id, "taskId")?;
let entry = self.owned_task(task_id).await?;
let not_cancelable = |state: TaskState| A2aError::TaskNotCancelable {
task_id: task_id.to_string(),
current_state: format!("{state:?}"),
};
if is_terminal_state(entry.status.state) {
return Err(not_cancelable(entry.status.state));
}
self.handler.running.cancel(task_id);
match self
.handler
.executor
.update_status(
task_id,
&entry.context_id,
TaskState::Canceled,
Vec::new(),
Some(serde_json::Map::new()),
)
.await
{
Ok(_) => {}
Err(A2aError::InvalidStateTransition { .. }) => {
let current = self.handler.task_store.get_task(task_id).await?;
if current.status.state != TaskState::Canceled {
return Err(not_cancelable(current.status.state));
}
}
Err(error) => return Err(error),
}
if entry.status.state == TaskState::InputRequired
&& let Some(runner_config) = &self.handler.runner_config
{
super::run::cancel_waiting_remote_tasks(runner_config, self.caller.as_ref(), &entry)
.await;
}
let updated = self.handler.task_store.get_task(task_id).await?;
internal_task_to_wire(&updated)
}
pub async fn tasks_list(&self, params: ListTasksParams) -> Result<TaskListResponse, A2aError> {
self.intercepted("ListTasks", params, |p: ListTasksParams| async move {
self.tasks_list_direct(p).await
})
.await
}
async fn tasks_list_direct(
&self,
params: ListTasksParams,
) -> Result<TaskListResponse, A2aError> {
let page_size = match params.page_size {
None | Some(0) => DEFAULT_PAGE_SIZE,
Some(size @ 1..=MAX_PAGE_SIZE) => size,
Some(size) => {
return Err(A2aError::InvalidParams {
message: format!("pageSize must be between 1 and {MAX_PAGE_SIZE}, got {size}"),
});
}
};
let updated_after = params
.status_timestamp_after
.as_deref()
.map(|timestamp| {
chrono::DateTime::parse_from_rfc3339(timestamp)
.map(|time| time.with_timezone(&chrono::Utc))
.map_err(|e| A2aError::InvalidParams {
message: format!("statusTimestampAfter {timestamp:?}: {e}"),
})
})
.transpose()?;
let after = params
.page_token
.as_deref()
.filter(|t| !t.is_empty())
.map(PageCursor::decode)
.transpose()?;
let history_length = params.history_length;
let include_artifacts = params.include_artifacts.unwrap_or(false);
let unpaged = ListTasksParams { page_size: None, page_token: None, ..params };
let mut entries: Vec<TaskStoreEntry> = self
.handler
.task_store
.list_tasks(unpaged)
.await?
.into_iter()
.filter(|entry| entry.owner() == self.owner())
.filter(|entry| updated_after.is_none_or(|after| status_time(entry) >= after))
.collect();
entries.sort_by(|a, b| b.updated_at.cmp(&a.updated_at).then_with(|| a.id.cmp(&b.id)));
let total_size = entries.len();
let start = after.map_or(0, |cursor| {
entries.iter().position(|entry| cursor.precedes(entry)).unwrap_or(entries.len())
});
let end = (start + page_size as usize).min(entries.len());
let next_page_token = if end < entries.len() {
PageCursor::after(&entries[end - 1]).encode()
} else {
String::new()
};
let tasks = entries[start..end]
.iter()
.cloned()
.map(|mut entry| {
if let Some(len) = history_length {
let keep = entry.history.len().saturating_sub(len as usize);
entry.history.drain(..keep);
}
if !include_artifacts {
entry.artifacts.clear();
}
internal_task_to_wire(&entry)
})
.collect::<Result<Vec<_>, _>>()?;
Ok(TaskListResponse {
tasks,
next_page_token,
page_size,
total_size: u32::try_from(total_size).unwrap_or(u32::MAX),
})
}
pub async fn tasks_subscribe(
&self,
task_id: &str,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError> {
self.intercepted_stream(
"SubscribeToTask",
TaskIdOnly { id: task_id.to_string() },
|p: TaskIdOnly| async move { self.tasks_subscribe_direct(&p.id).await },
)
.await
}
async fn tasks_subscribe_direct(
&self,
task_id: &str,
) -> Result<BoxStream<'static, Result<StreamResponse, A2aError>>, A2aError> {
self.handler.require(Capability::Streaming("SubscribeToTask")).await?;
let entry = self.owned_task(task_id).await?;
if is_terminal_state(entry.status.state) {
return Err(A2aError::UnsupportedOperation {
method: format!(
"subscribing to task {task_id} in terminal state {:?}",
entry.status.state
),
});
}
let mut updates = self.handler.executor.subscribe(task_id);
let snapshot = self.owned_task(task_id).await?;
let task = internal_task_to_wire(&snapshot)?;
let settled =
ends_subscription(snapshot.status.state) || !self.handler.running.contains(task_id);
let stream = async_stream::stream! {
yield Ok(StreamResponse::Task(task));
if settled {
return;
}
while let Some(update) = updates.recv().await {
let ends = matches!(
&update,
StreamResponse::StatusUpdate(event) if ends_subscription(event.status.state)
);
yield Ok(update);
if ends {
return;
}
}
};
Ok(stream.boxed())
}
pub async fn push_config_create(
&self,
task_id: &str,
config: TaskPushNotificationConfig,
) -> Result<TaskPushNotificationConfig, A2aError> {
self.intercepted(
"CreateTaskPushNotificationConfig",
PushConfigCreate { task_id: task_id.to_string(), config },
|p: PushConfigCreate| async move {
self.push_config_create_direct(&p.task_id, p.config).await
},
)
.await
}
async fn push_config_create_direct(
&self,
task_id: &str,
mut config: TaskPushNotificationConfig,
) -> Result<TaskPushNotificationConfig, A2aError> {
self.handler.require(Capability::PushNotifications).await?;
validate_push_url(&config.url)?;
self.owned_task(task_id).await?;
if config.id.as_deref().is_none_or(str::is_empty) {
config.id = Some(uuid::Uuid::new_v4().to_string());
}
config.task_id = Some(task_id.to_string());
self.handler.executor.save_push_config(task_id, config).await
}
pub async fn push_config_get(
&self,
task_id: &str,
config_id: &str,
) -> Result<TaskPushNotificationConfig, A2aError> {
self.intercepted(
"GetTaskPushNotificationConfig",
PushConfigRef { task_id: task_id.to_string(), id: config_id.to_string() },
|p: PushConfigRef| async move { self.push_config_get_direct(&p.task_id, &p.id).await },
)
.await
}
async fn push_config_get_direct(
&self,
task_id: &str,
config_id: &str,
) -> Result<TaskPushNotificationConfig, A2aError> {
self.handler.require(Capability::PushNotifications).await?;
let entry = self.owned_task(task_id).await?;
entry.push_configs.iter().find(|c| c.id.as_deref() == Some(config_id)).cloned().ok_or_else(
|| A2aError::TaskNotFound {
task_id: format!("push config {config_id} on task {task_id}"),
},
)
}
pub async fn push_config_list(
&self,
task_id: &str,
) -> Result<Vec<TaskPushNotificationConfig>, A2aError> {
self.intercepted(
"ListTaskPushNotificationConfigs",
TaskIdOnly { id: task_id.to_string() },
|p: TaskIdOnly| async move { self.push_config_list_direct(&p.id).await },
)
.await
}
async fn push_config_list_direct(
&self,
task_id: &str,
) -> Result<Vec<TaskPushNotificationConfig>, A2aError> {
self.handler.require(Capability::PushNotifications).await?;
let entry = self.owned_task(task_id).await?;
Ok(entry.push_configs)
}
pub async fn push_config_delete(&self, task_id: &str, config_id: &str) -> Result<(), A2aError> {
self.intercepted("DeleteTaskPushNotificationConfig", PushConfigRef { task_id: task_id.to_string(), id: config_id.to_string() }, |p: PushConfigRef| async move { self.push_config_delete_direct(&p.task_id, &p.id).await }).await
}
async fn push_config_delete_direct(
&self,
task_id: &str,
config_id: &str,
) -> Result<(), A2aError> {
self.handler.require(Capability::PushNotifications).await?;
self.owned_task(task_id).await?;
self.handler.executor.delete_push_config(task_id, config_id).await
}
}
enum Capability {
Streaming(&'static str),
PushNotifications,
}
const DEFAULT_PAGE_SIZE: u32 = 50;
const MAX_PAGE_SIZE: u32 = 100;
struct PageCursor {
updated_at: chrono::DateTime<chrono::Utc>,
id: String,
}
impl PageCursor {
fn after(entry: &TaskStoreEntry) -> Self {
Self { updated_at: entry.updated_at, id: entry.id.clone() }
}
fn precedes(&self, entry: &TaskStoreEntry) -> bool {
entry.updated_at < self.updated_at
|| (entry.updated_at == self.updated_at && entry.id > self.id)
}
fn encode(&self) -> String {
use base64::Engine as _;
let raw = format!("{}|{}", self.updated_at.to_rfc3339(), self.id);
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(raw)
}
fn decode(token: &str) -> Result<Self, A2aError> {
use base64::Engine as _;
let invalid =
|| A2aError::InvalidParams { message: format!("invalid pageToken {token:?}") };
let raw = base64::engine::general_purpose::URL_SAFE_NO_PAD
.decode(token)
.ok()
.and_then(|bytes| String::from_utf8(bytes).ok())
.ok_or_else(invalid)?;
let (time, id) = raw.split_once('|').ok_or_else(invalid)?;
let updated_at = chrono::DateTime::parse_from_rfc3339(time)
.map_err(|_| invalid())?
.with_timezone(&chrono::Utc);
Ok(Self { updated_at, id: id.to_string() })
}
}
#[derive(Default)]
struct RunningTasks {
next_run: AtomicU64,
runs: Mutex<HashMap<String, (u64, CancellationToken)>>,
}
impl RunningTasks {
fn start(self: &Arc<Self>, task_id: &str, parent: Option<&CancellationToken>) -> RunGuard {
let token = parent.map_or_else(CancellationToken::new, CancellationToken::child_token);
let run = self.next_run.fetch_add(1, Ordering::Relaxed);
self.runs().insert(task_id.to_string(), (run, token.clone()));
RunGuard { running: Arc::clone(self), task_id: task_id.to_string(), run, token }
}
fn contains(&self, task_id: &str) -> bool {
self.runs().contains_key(task_id)
}
fn cancel(&self, task_id: &str) {
if let Some((_, token)) = self.runs().get(task_id) {
token.cancel();
}
}
fn runs(&self) -> MutexGuard<'_, HashMap<String, (u64, CancellationToken)>> {
self.runs.lock().unwrap_or_else(PoisonError::into_inner)
}
}
struct RunGuard {
running: Arc<RunningTasks>,
task_id: String,
run: u64,
token: CancellationToken,
}
impl RunGuard {
fn token(&self) -> &CancellationToken {
&self.token
}
}
impl Drop for RunGuard {
fn drop(&mut self) {
let mut runs = self.running.runs();
if runs.get(&self.task_id).is_some_and(|(run, _)| *run == self.run) {
runs.remove(&self.task_id);
}
}
}
fn ends_subscription(state: TaskState) -> bool {
is_terminal_state(state) || matches!(state, TaskState::InputRequired | TaskState::AuthRequired)
}
fn is_terminal_state(state: TaskState) -> bool {
matches!(
state,
TaskState::Completed | TaskState::Failed | TaskState::Canceled | TaskState::Rejected
)
}
#[cfg(test)]
mod tests {
use super::super::push::NoOpPushNotificationSender;
use super::super::task_store::InMemoryTaskStore;
use super::*;
use a2a_protocol_types::artifact::Artifact;
use a2a_protocol_types::events::{TaskArtifactUpdateEvent, TaskStatusUpdateEvent};
use a2a_protocol_types::task::{ContextId, TaskId};
use a2a_protocol_types::{
AgentCapabilities, AgentCard, AgentInterface, AgentSkill, MessageId, MessageRole, Part,
TaskPushNotificationConfig,
};
fn part_text(part: &a2a_protocol_types::Part) -> Option<&str> {
match &part.content {
a2a_protocol_types::PartContent::Text(text) => Some(text.as_str()),
_ => None,
}
}
struct ChunkingAgent {
chunks: Vec<String>,
}
struct SnapshotAgent {
prefix: String,
snapshot: String,
}
#[async_trait::async_trait]
impl adk_core::Agent for SnapshotAgent {
fn name(&self) -> &str {
"snapshot_agent"
}
fn description(&self) -> &str {
"emits an authoritative response after text deltas"
}
fn sub_agents(&self) -> &[Arc<dyn adk_core::Agent>] {
&[]
}
async fn run(
&self,
_ctx: Arc<dyn adk_core::InvocationContext>,
) -> adk_core::Result<adk_core::EventStream> {
let mut first = adk_core::Event::new("invocation");
first.set_content(adk_core::Content::new("model").with_text(&self.prefix));
let mut delta = adk_core::Event::new("invocation");
delta.llm_response.partial = true;
delta.set_content(adk_core::Content::new("model").with_text("Hello"));
let mut terminal = delta.clone();
terminal.llm_response.partial = false;
terminal.llm_response.provider_metadata =
Some(serde_json::json!({"content_complete": true}));
terminal.set_content(adk_core::Content::new("model").with_text(&self.snapshot));
Ok(Box::pin(futures::stream::iter(vec![Ok(first), Ok(delta), Ok(terminal)])))
}
}
#[async_trait::async_trait]
impl adk_core::Agent for ChunkingAgent {
fn name(&self) -> &str {
"chunking_agent"
}
fn description(&self) -> &str {
"emits partial text chunks"
}
fn sub_agents(&self) -> &[Arc<dyn adk_core::Agent>] {
&[]
}
async fn run(
&self,
_ctx: Arc<dyn adk_core::InvocationContext>,
) -> adk_core::Result<adk_core::EventStream> {
let chunks = self.chunks.clone();
let last = chunks.len().saturating_sub(1);
let events: Vec<adk_core::Result<adk_core::Event>> = chunks
.into_iter()
.enumerate()
.map(|(i, text)| {
let mut event = adk_core::Event::new("chunking_agent");
event.llm_response.content =
Some(adk_core::Content::new("model").with_text(&text));
event.llm_response.partial = i < last;
Ok(event)
})
.collect();
Ok(Box::pin(futures::stream::iter(events)))
}
}
struct FailingAgent;
#[async_trait::async_trait]
impl adk_core::Agent for FailingAgent {
fn name(&self) -> &str {
"failing_agent"
}
fn description(&self) -> &str {
"fails immediately"
}
fn sub_agents(&self) -> &[Arc<dyn adk_core::Agent>] {
&[]
}
async fn run(
&self,
_ctx: Arc<dyn adk_core::InvocationContext>,
) -> adk_core::Result<adk_core::EventStream> {
Ok(Box::pin(futures::stream::iter(vec![Err(adk_core::AdkError::model(
"upstream exploded",
))])))
}
}
fn handler_with_agent(agent: Arc<dyn adk_core::Agent>) -> (RequestHandler, Arc<dyn TaskStore>) {
let store: Arc<dyn TaskStore> = Arc::new(InMemoryTaskStore::new());
let executor = Arc::new(V1Executor::new(store.clone()));
let cached = Arc::new(RwLock::new(CachedAgentCard::new(make_test_agent_card())));
let runner_config = Arc::new(
adk_runner::Runner::builder()
.app_name("a2a-stream-test")
.agent(agent)
.session_service(Arc::new(adk_session::InMemorySessionService::new()))
.build_config(),
);
let handler = RequestHandler::with_runner(
executor,
store.clone(),
Arc::new(NoOpPushNotificationSender),
cached,
runner_config,
);
(handler, store)
}
fn is_partial(update: &TaskArtifactUpdateEvent) -> bool {
update.metadata.as_ref().and_then(|meta| meta.get("adk_partial"))
== Some(&serde_json::json!(true))
}
fn answer_text(updates: &[TaskArtifactUpdateEvent]) -> String {
let mut text = String::new();
for update in updates.iter().filter(|update| !is_partial(update)) {
if update.append != Some(true) {
text.clear();
}
text.extend(update.artifact.parts.iter().filter_map(part_text));
}
text
}
fn stored_answer(entry: &TaskStoreEntry) -> String {
entry
.artifacts
.iter()
.filter(|artifact| {
artifact.metadata.as_ref().and_then(|meta| meta.get("adk_partial")).is_none()
})
.flat_map(|artifact| artifact.parts.iter().filter_map(part_text))
.collect()
}
async fn stream_updates(
handler: &RequestHandler,
) -> (String, Vec<TaskArtifactUpdateEvent>, Vec<TaskState>) {
let mut stream = handler.message_stream(make_test_message()).await.expect("stream starts");
let (mut task_id, mut artifacts, mut states) = (String::new(), Vec::new(), Vec::new());
while let Some(item) = stream.next().await {
match item.expect("no stream error") {
StreamResponse::Task(task) => task_id = task.id.0,
StreamResponse::ArtifactUpdate(e) => artifacts.push(e),
StreamResponse::StatusUpdate(e) => states.push(e.status.state),
_ => {}
}
}
(task_id, artifacts, states)
}
#[tokio::test]
async fn streamed_chunks_fill_a_temporary_artifact_and_the_answer_has_its_own() {
let (handler, store) = handler_with_agent(Arc::new(ChunkingAgent {
chunks: vec!["Hel".into(), "lo ".into(), "world".into()],
}));
let (task_id, artifacts, states) = stream_updates(&handler).await;
assert_eq!(artifacts.len(), 4, "two chunks, the answer, the reset: {artifacts:?}");
assert!(is_partial(&artifacts[0]) && is_partial(&artifacts[1]));
assert_eq!(artifacts[0].artifact.id, artifacts[1].artifact.id);
assert_eq!(artifacts[1].append, Some(false), "a chunk replaces the temporary artifact");
assert!(!is_partial(&artifacts[2]));
assert_ne!(artifacts[2].artifact.id, artifacts[0].artifact.id);
assert_eq!(
answer_text(&artifacts),
"Hello world",
"the final event carries the whole answer"
);
let reset = &artifacts[3];
assert!(is_partial(reset) && reset.last_chunk == Some(true));
assert_eq!(reset.artifact.id, artifacts[0].artifact.id);
assert_eq!(states, vec![TaskState::Working, TaskState::Completed]);
let entry = store.get_task(&task_id).await.unwrap();
assert_eq!(stored_answer(&entry), "Hello world", "tasks/get sees the answer");
}
#[tokio::test]
async fn an_authoritative_final_event_is_the_answer() {
for prefix in ["", "Earlier. "] {
for snapshot in ["Hello world", "Corrected", ""] {
let (handler, store) = handler_with_agent(Arc::new(SnapshotAgent {
prefix: prefix.into(),
snapshot: snapshot.into(),
}));
let (task_id, artifacts, _) = stream_updates(&handler).await;
let expected = format!("{prefix}{snapshot}");
assert_eq!(answer_text(&artifacts), expected, "{prefix:?} {snapshot:?}");
let entry = store.get_task(&task_id).await.unwrap();
assert_eq!(stored_answer(&entry), expected, "{prefix:?} {snapshot:?}");
let (handler, _) = handler_with_agent(Arc::new(SnapshotAgent {
prefix: prefix.into(),
snapshot: snapshot.into(),
}));
let task = handler.message_send(make_test_message()).await.unwrap();
let entry = store_entry_of(&task);
assert_eq!(stored_answer(&entry), expected, "buffered {prefix:?} {snapshot:?}");
}
}
}
fn store_entry_of(task: &Task) -> TaskStoreEntry {
TaskStoreEntry {
id: task.id.0.clone(),
context_id: task.context_id.0.clone(),
status: task.status.clone(),
artifacts: task.artifacts.clone().unwrap_or_default(),
history: Vec::new(),
metadata: HashMap::new(),
push_configs: Vec::new(),
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
}
}
fn status_text(status: &a2a_protocol_types::TaskStatus) -> String {
status.message.iter().flat_map(|m| m.parts.iter().filter_map(part_text)).collect()
}
#[tokio::test]
async fn stream_failure_ends_on_a_failed_status_that_carries_the_error() {
let (handler, store) = handler_with_agent(Arc::new(FailingAgent));
let stream = handler.message_stream(make_test_message()).await.expect("stream starts");
let items: Vec<_> = stream.collect().await;
assert!(
items.iter().all(Result::is_ok),
"the error rides on the terminal status, not on a separate error item: {items:?}"
);
let states: Vec<_> = items
.iter()
.filter_map(|item| match item {
Ok(StreamResponse::StatusUpdate(e)) => Some(e.status.state),
_ => None,
})
.collect();
assert!(
!states.contains(&TaskState::Completed),
"a failed run must never report Completed: {states:?}"
);
let Some(Ok(StreamResponse::StatusUpdate(last))) = items.last() else {
panic!("the stream must end on a status update: {items:?}");
};
assert_eq!(last.status.state, TaskState::Failed);
let text = status_text(&last.status);
assert!(text.contains("upstream exploded"), "the status message names the error: {text}");
let stored = store.get_task(&last.task_id.0).await.unwrap();
assert_eq!(status_text(&stored.status), text);
let (handler, _) = handler_with_agent(Arc::new(FailingAgent));
let sent = handler.message_send(make_test_message()).await.unwrap();
assert_eq!(sent.status.state, TaskState::Failed);
assert_eq!(status_text(&sent.status), text);
}
struct ApprovalAgent;
#[async_trait::async_trait]
impl adk_core::Agent for ApprovalAgent {
fn name(&self) -> &str {
"approval_agent"
}
fn description(&self) -> &str {
"waits for an approval"
}
fn sub_agents(&self) -> &[Arc<dyn adk_core::Agent>] {
&[]
}
async fn run(
&self,
ctx: Arc<dyn adk_core::InvocationContext>,
) -> adk_core::Result<adk_core::EventStream> {
let approved = ctx.user_content().parts.iter().any(|part| {
matches!(
part,
adk_core::Part::FunctionResponse { function_response, id: Some(id), .. }
if id == "call-approve"
&& function_response.response["status"] == "approved"
)
});
if approved {
let mut done = adk_core::Event::new("inv");
done.set_content(adk_core::Content::new("model").with_text("Approved."));
return Ok(Box::pin(futures::stream::iter(vec![Ok(done)])));
}
let mut call = adk_core::Event::new("inv");
call.set_content(adk_core::Content {
role: "model".to_string(),
parts: vec![adk_core::Part::FunctionCall {
name: "request_approval".to_string(),
args: serde_json::json!({"amount": 500}),
id: Some("call-approve".to_string()),
thought_signature: None,
}],
});
call.long_running_tool_ids = vec!["call-approve".to_string()];
let mut interim = adk_core::Event::new("inv");
interim.set_content(adk_core::Content {
role: "user".to_string(),
parts: vec![adk_core::Part::FunctionResponse {
function_response: adk_core::FunctionResponseData::new(
"request_approval",
serde_json::json!({"status": "pending"}),
),
id: Some("call-approve".to_string()),
annotations: None,
}],
});
let mut text = adk_core::Event::new("inv");
text.set_content(adk_core::Content::new("model").with_text("Approval pending."));
Ok(Box::pin(futures::stream::iter(vec![Ok(call), Ok(interim), Ok(text)])))
}
}
fn approval_for(task_id: &TaskId, context_id: Option<&ContextId>) -> Message {
let part: Part = serde_json::from_value(serde_json::json!({
"data": {"id": "call-approve", "name": "request_approval", "response": {"status": "approved"}},
"metadata": {"adk_type": "function_response"}
}))
.unwrap();
Message {
id: MessageId::new("msg-2"),
role: MessageRole::User,
parts: vec![part],
task_id: Some(task_id.clone()),
context_id: context_id.cloned(),
reference_task_ids: None,
extensions: None,
metadata: None,
}
}
fn artifact_texts(task: &Task) -> Vec<String> {
task.artifacts
.iter()
.flatten()
.flat_map(|artifact| artifact.parts.iter().filter_map(part_text).map(str::to_string))
.collect()
}
#[tokio::test]
async fn a_waiting_task_names_the_pending_call_and_the_answer_continues_it() {
let (handler, _) = handler_with_agent(Arc::new(ApprovalAgent));
let waiting = handler.message_send(make_test_message()).await.unwrap();
assert_eq!(waiting.status.state, TaskState::InputRequired);
let parts: Vec<_> = waiting
.status
.message
.as_ref()
.expect("the status names the pending call")
.parts
.iter()
.map(|part| serde_json::to_value(part).unwrap())
.collect();
assert_eq!(
parts,
vec![
serde_json::json!({
"data": {"id": "call-approve", "name": "request_approval", "args": {"amount": 500}},
"metadata": {"adk_type": "function_call", "adk_is_long_running": true}
}),
serde_json::json!({
"data": {"id": "call-approve", "name": "request_approval", "response": {"status": "pending"}},
"metadata": {"adk_type": "function_response"}
}),
]
);
let answer = approval_for(&waiting.id, Some(&waiting.context_id));
let done = handler.message_send(answer).await.unwrap();
assert_eq!(done.id, waiting.id, "the answer continues the same task");
assert_eq!(done.status.state, TaskState::Completed);
assert!(artifact_texts(&done).contains(&"Approved.".to_string()), "{done:?}");
}
#[tokio::test]
async fn a_stream_names_the_pending_call_and_a_task_id_alone_continues_the_task() {
let (handler, _) = handler_with_agent(Arc::new(ApprovalAgent));
let items: Vec<_> =
handler.message_stream(make_test_message()).await.unwrap().collect().await;
let Some(Ok(StreamResponse::StatusUpdate(waiting))) = items.last() else {
panic!("the stream ends on a status: {items:?}");
};
assert_eq!(waiting.status.state, TaskState::InputRequired);
assert_eq!(waiting.status.message.as_ref().map(|m| m.parts.len()), Some(2));
let answer = approval_for(&waiting.task_id, None);
let items: Vec<_> = handler.message_stream(answer).await.unwrap().collect().await;
let Some(Ok(StreamResponse::Task(first))) = items.first() else {
panic!("the stream starts with the task: {items:?}");
};
assert_eq!(first.id, waiting.task_id, "the answer continues the same task");
let Some(Ok(StreamResponse::StatusUpdate(last))) = items.last() else {
panic!("the stream ends on a status: {items:?}");
};
assert_eq!(last.status.state, TaskState::Completed);
}
#[tokio::test]
async fn a_message_naming_a_task_it_cannot_continue_is_refused() {
let (handler, _) = handler_with_agent(Arc::new(ApprovalAgent));
let mut unknown = make_test_message();
unknown.task_id = Some(TaskId::new("no-such-task"));
assert!(matches!(handler.message_send(unknown).await, Err(A2aError::TaskNotFound { .. })));
let waiting = handler.message_send(make_test_message()).await.unwrap();
let elsewhere = approval_for(&waiting.id, Some(&ContextId::new("another-context")));
assert!(matches!(
handler.message_send(elsewhere).await,
Err(A2aError::InvalidParams { .. })
));
let done = handler.message_send(approval_for(&waiting.id, None)).await.unwrap();
assert_eq!(done.status.state, TaskState::Completed);
let mut after_the_end = approval_for(&waiting.id, None);
after_the_end.id = MessageId::new("msg-3");
assert!(matches!(
handler.message_send(after_the_end).await,
Err(A2aError::UnsupportedOperation { .. })
));
}
struct SlowAgent {
delay: std::time::Duration,
answered: Arc<std::sync::atomic::AtomicBool>,
}
#[async_trait::async_trait]
impl adk_core::Agent for SlowAgent {
fn name(&self) -> &str {
"slow_agent"
}
fn description(&self) -> &str {
"answers late"
}
fn sub_agents(&self) -> &[Arc<dyn adk_core::Agent>] {
&[]
}
async fn run(
&self,
_ctx: Arc<dyn adk_core::InvocationContext>,
) -> adk_core::Result<adk_core::EventStream> {
let delay = self.delay;
let answered = self.answered.clone();
Ok(Box::pin(async_stream::stream! {
tokio::time::sleep(delay).await;
answered.store(true, Ordering::SeqCst);
let mut event = adk_core::Event::new("inv");
event.set_content(adk_core::Content::new("model").with_text("Too late."));
yield Ok(event);
}))
}
}
fn slow_handler(
delay_ms: u64,
) -> (RequestHandler, Arc<dyn TaskStore>, Arc<std::sync::atomic::AtomicBool>) {
let answered = Arc::new(std::sync::atomic::AtomicBool::new(false));
let (handler, store) = handler_with_agent(Arc::new(SlowAgent {
delay: std::time::Duration::from_millis(delay_ms),
answered: answered.clone(),
}));
(handler, store, answered)
}
fn return_immediately() -> SendMessageConfiguration {
serde_json::from_value(serde_json::json!({"returnImmediately": true})).unwrap()
}
#[tokio::test]
async fn return_immediately_answers_while_the_run_goes_on_in_the_background() {
let (handler, store, _) = slow_handler(50);
let started = handler
.message_send_with(make_test_message(), Some(&return_immediately()))
.await
.unwrap();
assert_eq!(
started.status.state,
TaskState::Submitted,
"the send returns the submitted task without waiting for the run"
);
let mut state = TaskState::Submitted;
for _ in 0..100 {
state = store.get_task(&started.id.0).await.unwrap().status.state;
if is_terminal_state(state) {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
assert_eq!(state, TaskState::Completed);
let done = handler.tasks_get(&started.id.0, None).await.unwrap();
assert_eq!(artifact_texts(&done), vec!["Too late.".to_string()]);
}
#[tokio::test]
async fn cancel_stops_the_run_and_a_second_cancel_is_refused() {
let (handler, store, answered) = slow_handler(300);
let started = handler
.message_send_with(make_test_message(), Some(&return_immediately()))
.await
.unwrap();
let canceled = handler.tasks_cancel(&started.id.0).await.unwrap();
assert_eq!(canceled.status.state, TaskState::Canceled);
assert!(matches!(
handler.tasks_cancel(&started.id.0).await,
Err(A2aError::TaskNotCancelable { .. })
));
tokio::time::sleep(std::time::Duration::from_millis(600)).await;
assert!(
!answered.load(Ordering::SeqCst),
"cancelling stops the agent, not only the task's state"
);
let stored = store.get_task(&started.id.0).await.unwrap();
assert_eq!(stored.status.state, TaskState::Canceled);
assert!(stored.artifacts.is_empty(), "a cancelled run records no output");
}
#[tokio::test]
async fn cancelling_a_streamed_task_ends_the_stream_on_canceled() {
let (handler, _, answered) = slow_handler(5_000);
let mut stream = handler.message_stream(make_test_message()).await.unwrap();
let Some(Ok(StreamResponse::Task(task))) = stream.next().await else {
panic!("the stream starts with the task");
};
let Some(Ok(StreamResponse::StatusUpdate(working))) = stream.next().await else {
panic!("then reports it working");
};
assert_eq!(working.status.state, TaskState::Working);
let canceled = handler.tasks_cancel(&task.id.0).await.unwrap();
assert_eq!(canceled.status.state, TaskState::Canceled);
let rest: Vec<_> =
tokio::time::timeout(std::time::Duration::from_secs(2), stream.collect::<Vec<_>>())
.await
.expect("a cancelled stream ends");
let Some(Ok(StreamResponse::StatusUpdate(last))) = rest.last() else {
panic!("the stream ends on a status: {rest:?}");
};
assert_eq!(last.status.state, TaskState::Canceled);
assert!(!answered.load(Ordering::SeqCst));
}
#[derive(Default)]
struct RecordingSender(std::sync::Mutex<Vec<(String, String)>>);
#[async_trait::async_trait]
impl super::super::push::PushNotificationSender for RecordingSender {
async fn send_status_update(
&self,
url: &str,
event: &TaskStatusUpdateEvent,
_config: &TaskPushNotificationConfig,
) -> Result<(), A2aError> {
self.0.lock().unwrap().push((url.to_string(), format!("{:?}", event.status.state)));
Ok(())
}
async fn send_artifact_update(
&self,
url: &str,
_event: &TaskArtifactUpdateEvent,
_config: &TaskPushNotificationConfig,
) -> Result<(), A2aError> {
self.0.lock().unwrap().push((url.to_string(), "artifact".to_string()));
Ok(())
}
}
#[tokio::test]
async fn a_push_config_sent_with_the_message_gets_every_update() {
let sender = Arc::new(RecordingSender::default());
let store: Arc<dyn TaskStore> = Arc::new(InMemoryTaskStore::new());
let mut card = make_test_agent_card();
card.capabilities = AgentCapabilities::none().with_push_notifications(true);
let handler = RequestHandler::with_runner(
Arc::new(V1Executor::new(store.clone())),
store,
sender.clone(),
Arc::new(RwLock::new(CachedAgentCard::new(card))),
Arc::new(
adk_runner::Runner::builder()
.app_name("push")
.agent(Arc::new(ChunkingAgent { chunks: vec!["done".into()] }))
.session_service(Arc::new(adk_session::InMemorySessionService::new()))
.build_config(),
),
);
let configuration: SendMessageConfiguration = serde_json::from_value(serde_json::json!({
"taskPushNotificationConfig": {"url": "https://hooks.example.com/a2a"}
}))
.unwrap();
let task =
handler.message_send_with(make_test_message(), Some(&configuration)).await.unwrap();
assert_eq!(handler.push_config_list(&task.id.0).await.unwrap().len(), 1);
let mut delivered = Vec::new();
for _ in 0..100 {
delivered = sender.0.lock().unwrap().clone();
if delivered.iter().any(|(_, what)| what == "Completed") {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
let what: Vec<&str> = delivered.iter().map(|(_, what)| what.as_str()).collect();
assert_eq!(what, vec!["Working", "artifact", "Completed"]);
assert!(delivered.iter().all(|(url, _)| url == "https://hooks.example.com/a2a"));
}
struct ImageAgent;
#[async_trait::async_trait]
impl adk_core::Agent for ImageAgent {
fn name(&self) -> &str {
"image_agent"
}
fn description(&self) -> &str {
"draws"
}
fn sub_agents(&self) -> &[Arc<dyn adk_core::Agent>] {
&[]
}
async fn run(
&self,
_ctx: Arc<dyn adk_core::InvocationContext>,
) -> adk_core::Result<adk_core::EventStream> {
let mut event = adk_core::Event::new("inv");
event.set_content(
adk_core::Content::new("model")
.with_text("A square.")
.with_inline_data("image/png", vec![0x89, b'P', b'N', b'G']),
);
Ok(Box::pin(futures::stream::iter(vec![Ok(event)])))
}
}
fn image_parts(artifacts: &[Artifact]) -> Vec<&a2a_protocol_types::Part> {
artifacts
.iter()
.flat_map(|artifact| &artifact.parts)
.filter(|part| part.media_type.as_deref() == Some("image/png"))
.collect()
}
#[tokio::test]
async fn files_and_inline_data_reach_the_client_beside_the_text() {
let (handler, store) = handler_with_agent(Arc::new(ImageAgent));
let sent = handler.message_send(make_test_message()).await.unwrap();
let artifacts = sent.artifacts.clone().unwrap_or_default();
assert_eq!(artifact_texts(&sent), vec!["A square.".to_string()]);
assert_eq!(image_parts(&artifacts).len(), 1, "{artifacts:?}");
let (handler, _) = handler_with_agent(Arc::new(ImageAgent));
let updates: Vec<_> = handler
.message_stream(make_test_message())
.await
.unwrap()
.map(|item| item.unwrap())
.collect()
.await;
let streamed: Vec<Artifact> = updates
.iter()
.filter_map(|update| match update {
StreamResponse::ArtifactUpdate(event) => Some(event.artifact.clone()),
_ => None,
})
.collect();
assert_eq!(image_parts(&streamed).len(), 1, "the stream carries the image too");
let stored = store.get_task(&sent.id.0).await.unwrap();
assert_eq!(image_parts(&stored.artifacts).len(), 1);
}
#[tokio::test]
async fn tasks_and_their_updates_name_the_adk_app_user_and_session() {
use super::super::convert::{ADK_ERROR_MESSAGE_METADATA_KEY, ADK_EXTENSION_URI};
let (handler, store) =
handler_with_agent(Arc::new(ChunkingAgent { chunks: vec!["hi".into()] }));
let mut stream = handler.message_stream(make_test_message()).await.unwrap();
let Some(Ok(StreamResponse::Task(task))) = stream.next().await else {
panic!("the stream starts with the task");
};
let context = task.context_id.0.clone();
let metadata = task.metadata.clone().expect("the task carries the ADK marker");
assert_eq!(metadata, serde_json::json!({ ADK_EXTENSION_URI: true }));
let task_id = task.id.0.clone();
for update in stream.collect::<Vec<_>>().await {
let metadata = match update.unwrap() {
StreamResponse::StatusUpdate(event) => event.metadata,
StreamResponse::ArtifactUpdate(event) => event.metadata,
_ => continue,
};
assert_eq!(
metadata.expect("every update carries it")["adk_session_id"],
context.as_str()
);
}
let stored = store.get_task(&task_id).await.unwrap().metadata;
assert_eq!(stored["adk_app_name"], "a2a-stream-test");
assert_eq!(stored["adk_session_id"], context.as_str());
assert_eq!(stored["adk_user_id"], format!("A2A_USER_{context}"));
assert_eq!(stored[ADK_EXTENSION_URI], true);
let (handler, _) = handler_with_agent(Arc::new(FailingAgent));
let failed = handler.message_send(make_test_message()).await.unwrap();
let error_part = &failed.status.message.expect("the error").parts[0];
assert_eq!(error_part.metadata.as_ref().unwrap()[ADK_ERROR_MESSAGE_METADATA_KEY], true);
}
#[tokio::test]
async fn operations_the_card_says_are_off_are_refused() {
let store = Arc::new(InMemoryTaskStore::new());
let mut card = make_test_agent_card();
card.capabilities = AgentCapabilities::none()
.with_streaming(false)
.with_push_notifications(false)
.with_extended_agent_card(false);
let handler = RequestHandler::new(
Arc::new(V1Executor::new(store.clone())),
store,
Arc::new(NoOpPushNotificationSender),
Arc::new(RwLock::new(CachedAgentCard::new(card))),
);
let task = handler.message_send(make_test_message()).await.unwrap();
assert!(matches!(
handler.message_stream(make_test_message()).await,
Err(A2aError::UnsupportedOperation { .. })
));
assert!(matches!(
handler.tasks_subscribe(&task.id.0).await,
Err(A2aError::UnsupportedOperation { .. })
));
assert!(matches!(
handler.push_config_list(&task.id.0).await,
Err(A2aError::PushNotificationNotSupported)
));
let with_push: SendMessageConfiguration = serde_json::from_value(serde_json::json!({
"taskPushNotificationConfig": {"url": "https://hooks.example.com/a2a"}
}))
.unwrap();
let mut msg = make_test_message();
msg.id = MessageId::new("msg-push");
assert!(matches!(
handler.message_send_with(msg, Some(&with_push)).await,
Err(A2aError::PushNotificationNotSupported)
));
assert!(matches!(
handler.agent_card_extended().await,
Err(A2aError::UnsupportedOperation { .. })
));
}
#[tokio::test]
async fn list_tasks_pages_through_every_task_once_newest_first() {
let handler = make_handler();
for i in 0..5 {
let mut msg = make_test_message();
msg.id = MessageId::new(format!("msg-{i}"));
handler.message_send(msg).await.unwrap();
}
let mut seen = Vec::new();
let mut token = None;
loop {
let page = handler
.tasks_list(ListTasksParams {
page_size: Some(2),
page_token: token.clone(),
..Default::default()
})
.await
.unwrap();
assert_eq!(page.total_size, 5);
assert_eq!(page.page_size, 2);
assert!(page.tasks.len() <= 2);
assert!(
page.tasks.iter().all(|task| task.artifacts.is_none()),
"no artifacts unless asked"
);
seen.extend(page.tasks.into_iter().map(|task| task.id.0));
if page.next_page_token.is_empty() {
break;
}
token = Some(page.next_page_token);
}
let mut unique = seen.clone();
unique.sort();
unique.dedup();
assert_eq!((seen.len(), unique.len()), (5, 5), "every task once: {seen:?}");
let default_page = handler
.tasks_list(ListTasksParams { page_size: Some(0), ..Default::default() })
.await
.unwrap();
assert_eq!(default_page.page_size, DEFAULT_PAGE_SIZE);
for bad in [
ListTasksParams { page_size: Some(101), ..Default::default() },
ListTasksParams { page_token: Some("not a token".into()), ..Default::default() },
ListTasksParams {
status_timestamp_after: Some("yesterday".into()),
..Default::default()
},
] {
assert!(matches!(handler.tasks_list(bad).await, Err(A2aError::InvalidParams { .. })));
}
let later = (chrono::Utc::now() + chrono::Duration::minutes(1)).to_rfc3339();
let none = handler
.tasks_list(ListTasksParams {
status_timestamp_after: Some(later),
..Default::default()
})
.await
.unwrap();
assert_eq!(none.total_size, 0);
}
async fn collect_subscription(
stream: BoxStream<'static, Result<StreamResponse, A2aError>>,
) -> Vec<StreamResponse> {
tokio::time::timeout(std::time::Duration::from_secs(2), stream.collect::<Vec<_>>())
.await
.expect("the subscription ends")
.into_iter()
.map(|item| item.expect("no stream error"))
.collect()
}
fn last_state(updates: &[StreamResponse]) -> Option<TaskState> {
updates.iter().rev().find_map(|update| match update {
StreamResponse::StatusUpdate(event) => Some(event.status.state),
_ => None,
})
}
fn streamed_text(updates: &[StreamResponse]) -> String {
updates
.iter()
.filter_map(|update| match update {
StreamResponse::ArtifactUpdate(event) => Some(&event.artifact),
_ => None,
})
.flat_map(|artifact| artifact.parts.iter().filter_map(part_text))
.collect()
}
#[tokio::test]
async fn a_subscriber_follows_a_background_run_to_its_end() {
let (handler, _, _) = slow_handler(200);
let started = handler
.message_send_with(make_test_message(), Some(&return_immediately()))
.await
.unwrap();
let updates =
collect_subscription(handler.tasks_subscribe(&started.id.0).await.unwrap()).await;
let Some(StreamResponse::Task(first)) = updates.first() else {
panic!("the subscription starts with the task: {updates:?}");
};
assert!(
matches!(first.status.state, TaskState::Submitted | TaskState::Working),
"the snapshot is the task as it stands: {:?}",
first.status.state
);
assert_eq!(streamed_text(&updates), "Too late.", "the run's output reaches the subscriber");
assert_eq!(last_state(&updates), Some(TaskState::Completed));
}
#[tokio::test]
async fn a_subscriber_follows_a_task_another_client_streams() {
let (handler, _, _) = slow_handler(200);
let mut stream = handler.message_stream(make_test_message()).await.unwrap();
let Some(Ok(StreamResponse::Task(task))) = stream.next().await else {
panic!("the stream starts with the task");
};
let streaming = tokio::spawn(stream.collect::<Vec<_>>());
let updates =
collect_subscription(handler.tasks_subscribe(&task.id.0).await.unwrap()).await;
assert_eq!(streamed_text(&updates), "Too late.");
assert_eq!(last_state(&updates), Some(TaskState::Completed));
streaming.await.unwrap();
}
#[tokio::test]
async fn a_task_waiting_on_its_caller_is_a_snapshot_and_a_finished_one_is_refused() {
let (handler, _) = handler_with_agent(Arc::new(ApprovalAgent));
let waiting = handler.message_send(make_test_message()).await.unwrap();
let updates =
collect_subscription(handler.tasks_subscribe(&waiting.id.0).await.unwrap()).await;
let [StreamResponse::Task(snapshot)] = updates.as_slice() else {
panic!("the subscription is the snapshot alone: {updates:?}");
};
assert_eq!(snapshot.status.state, TaskState::InputRequired);
handler.message_send(approval_for(&waiting.id, None)).await.unwrap();
assert!(matches!(
handler.tasks_subscribe(&waiting.id.0).await,
Err(A2aError::UnsupportedOperation { .. })
));
}
#[tokio::test]
async fn a_run_outlives_the_stream_that_started_it() {
let (handler, store, answered) = slow_handler(100);
let mut stream = handler.message_stream(make_test_message()).await.unwrap();
let Some(Ok(StreamResponse::Task(task))) = stream.next().await else {
panic!("the stream starts with the task");
};
drop(stream);
let mut state = TaskState::Submitted;
for _ in 0..100 {
state = store.get_task(&task.id.0).await.unwrap().status.state;
if is_terminal_state(state) {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
assert_eq!(state, TaskState::Completed, "the client left; the run did not");
assert!(answered.load(std::sync::atomic::Ordering::SeqCst));
}
struct PanickingAgent;
#[async_trait::async_trait]
impl adk_core::Agent for PanickingAgent {
fn name(&self) -> &str {
"panicking_agent"
}
fn description(&self) -> &str {
"panics"
}
fn sub_agents(&self) -> &[Arc<dyn adk_core::Agent>] {
&[]
}
async fn run(
&self,
_ctx: Arc<dyn adk_core::InvocationContext>,
) -> adk_core::Result<adk_core::EventStream> {
Ok(Box::pin(async_stream::stream! {
if true {
panic!("boom");
}
yield Ok(adk_core::Event::new("never"));
}))
}
}
#[tokio::test]
async fn a_panicking_agent_fails_its_task() {
let (handler, _) = handler_with_agent(Arc::new(PanickingAgent));
let task = handler.message_send(make_test_message()).await.unwrap();
assert_eq!(task.status.state, TaskState::Failed);
let text = status_text(&task.status);
assert!(text.contains("agent run panicked: boom"), "{text}");
}
#[tokio::test]
async fn the_waiting_message_moves_into_history_when_the_task_goes_on() {
let (handler, _) = handler_with_agent(Arc::new(ApprovalAgent));
let waiting = handler.message_send(make_test_message()).await.unwrap();
assert_eq!(waiting.status.state, TaskState::InputRequired);
let done = handler.message_send(approval_for(&waiting.id, None)).await.unwrap();
let roles: Vec<MessageRole> =
done.history.unwrap_or_default().iter().map(|message| message.role).collect();
assert_eq!(
roles,
vec![MessageRole::User, MessageRole::Agent, MessageRole::User],
"the question, the agent's waiting message, the answer"
);
}
#[tokio::test]
async fn push_configs_are_validated_and_replaced_by_id() {
let handler = make_handler();
let task = handler.message_send(make_test_message()).await.unwrap();
let config = |value: serde_json::Value| -> TaskPushNotificationConfig {
serde_json::from_value(value).unwrap()
};
for url in ["not a url", "ftp://hooks.example.com/a2a", "/relative"] {
let err = handler
.push_config_create(&task.id.0, config(serde_json::json!({"url": url})))
.await
.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }), "{url}: {err}");
}
handler
.push_config_create(
&task.id.0,
config(serde_json::json!({"id": "c1", "url": "https://a.example/1"})),
)
.await
.unwrap();
handler
.push_config_create(
&task.id.0,
config(serde_json::json!({"id": "c1", "url": "https://a.example/2"})),
)
.await
.unwrap();
let configs = handler.push_config_list(&task.id.0).await.unwrap();
assert_eq!(configs.len(), 1, "a config with the same ID replaces the old one");
assert_eq!(configs[0].url, "https://a.example/2");
}
struct EchoAgent;
#[async_trait::async_trait]
impl adk_core::Agent for EchoAgent {
fn name(&self) -> &str {
"echo_agent"
}
fn description(&self) -> &str {
"repeats"
}
fn sub_agents(&self) -> &[Arc<dyn adk_core::Agent>] {
&[]
}
async fn run(
&self,
ctx: Arc<dyn adk_core::InvocationContext>,
) -> adk_core::Result<adk_core::EventStream> {
let text: String =
ctx.user_content().parts.iter().filter_map(adk_core::Part::text).collect();
let mut event = adk_core::Event::new("echo");
event.llm_response.content =
Some(adk_core::Content::new("model").with_text(format!("echo: {text}")));
Ok(Box::pin(futures::stream::iter(vec![Ok(event)])))
}
}
#[tokio::test]
async fn executor_hooks_shape_a_run_as_adk_go_callbacks_do() {
use super::super::hooks::ExecutorHooks;
let cleaned = Arc::new(Mutex::new(Vec::new()));
let cleanup_log = cleaned.clone();
let hooks = ExecutorHooks::default()
.before_execute(Arc::new(|run| {
Box::pin(async move {
let text: String =
run.message.parts.iter().filter_map(|p| p.text_content()).collect();
if text.contains("forbidden") {
Err(A2aError::InvalidParams { message: "not allowed".to_string() })
} else {
Ok(())
}
})
}))
.from_wire_part(Arc::new(|part| {
Ok(part
.text_content()
.map(|text| adk_core::Part::Text { text: text.to_uppercase() }))
}))
.after_event(Arc::new(|run, _event, update| {
update
.metadata
.get_or_insert_with(Default::default)
.insert("tenant".into(), run.user_id.clone().into());
Ok(())
}))
.after_execute(Arc::new(|_run, status| {
status.metadata.insert("audited".into(), true.into());
Ok(())
}))
.on_cleanup(Arc::new(move |run, event| {
cleanup_log.lock().unwrap().push((run.task_id.clone(), event.status.state));
}));
let (handler, _) = handler_with_agent(Arc::new(EchoAgent));
let handler = handler.with_hooks(hooks);
let mut msg = make_test_message();
msg.parts = vec![Part::text("hello")];
let mut stream = handler.message_stream(msg).await.unwrap();
let mut updates = Vec::new();
while let Some(update) = stream.next().await {
updates.push(update.unwrap());
}
let artifact = updates
.iter()
.find_map(|u| match u {
StreamResponse::ArtifactUpdate(e) => Some(e),
_ => None,
})
.expect("an artifact update");
assert_eq!(
artifact.artifact.parts[0].text_content(),
Some("echo: HELLO"),
"inbound converter"
);
assert_eq!(
artifact.metadata.as_ref().unwrap()["tenant"],
"A2A_USER_".to_string() + artifact.context_id.0.as_str()
);
let StreamResponse::StatusUpdate(last) = updates.last().unwrap() else {
panic!("ends on a status")
};
assert_eq!(last.status.state, TaskState::Completed);
assert_eq!(last.metadata.as_ref().unwrap()["audited"], true);
assert_eq!(cleaned.lock().unwrap().len(), 1);
let mut msg = make_test_message();
msg.id = MessageId::new("msg-forbidden");
msg.parts = vec![Part::text("forbidden")];
let task = handler.message_send(msg).await.unwrap();
assert_eq!(task.status.state, TaskState::Failed);
assert!(
status_text(&task.status).starts_with("before execute: "),
"{}",
status_text(&task.status)
);
assert!(task.artifacts.unwrap_or_default().is_empty());
}
#[tokio::test]
async fn an_outbound_part_converter_replaces_the_default_conversion() {
use super::super::hooks::ExecutorHooks;
let hooks = ExecutorHooks::default().to_wire_part(Arc::new(|_event, part| {
Ok(part.text().map(|text| Part::text(format!("[{text}]"))))
}));
let (handler, _) = handler_with_agent(Arc::new(EchoAgent));
let task = handler.with_hooks(hooks).message_send(make_test_message()).await.unwrap();
let texts = artifact_texts(&task);
assert_eq!(texts.len(), 1);
assert!(texts[0].starts_with("[echo: "), "{texts:?}");
}
#[tokio::test]
async fn a_required_extension_must_be_asked_for_and_reaches_the_run() {
use super::super::extensions::RequestedExtensions;
use super::super::hooks::ExecutorHooks;
let required = "https://example.com/ext/billing";
let (handler, _) = handler_with_agent(Arc::new(EchoAgent));
{
let mut card = handler.agent_card.write().await;
let mut updated = card.card.clone();
updated.capabilities.extensions =
Some(vec![a2a_protocol_types::extensions::AgentExtension {
uri: required.to_string(),
description: None,
required: Some(true),
params: None,
}]);
card.update(updated);
}
let seen = Arc::new(Mutex::new(Vec::new()));
let record = seen.clone();
let handler =
handler.with_hooks(ExecutorHooks::default().on_cleanup(Arc::new(move |run, _| {
record.lock().unwrap().extend(run.extensions.clone());
})));
let err = handler.require_extensions(&RequestedExtensions::default()).await.unwrap_err();
assert!(
matches!(err, A2aError::ExtensionSupportRequired { ref uri } if uri == required),
"{err}"
);
let asked = RequestedExtensions(vec![required.to_string()]);
handler.require_extensions(&asked).await.unwrap();
handler
.for_caller(None)
.with_extensions(asked)
.message_send(make_test_message())
.await
.unwrap();
assert_eq!(*seen.lock().unwrap(), vec![required.to_string()]);
}
#[tokio::test]
async fn an_agent_that_goes_quiet_fails_its_task() {
let (handler, _, _) = slow_handler(2_000);
let handler = handler.with_inactivity_timeout(std::time::Duration::from_millis(100));
let task = handler.message_send(make_test_message()).await.unwrap();
assert_eq!(task.status.state, TaskState::Failed);
assert!(
status_text(&task.status).contains("agent inactivity timeout"),
"{}",
status_text(&task.status)
);
}
#[tokio::test]
async fn runs_over_the_concurrency_limit_are_refused() {
let (handler, store, _) = slow_handler(300);
let handler = handler.with_concurrency_limits(super::super::run::ConcurrencyLimits {
max_runs: 1,
max_runs_for: None,
});
let first = handler
.message_send_with(make_test_message(), Some(&return_immediately()))
.await
.unwrap();
let mut second = make_test_message();
second.id = MessageId::new("msg-second");
let refused = handler.message_send(second).await.unwrap_err();
assert!(refused.to_string().contains("max concurrency limit reached"), "{refused}");
for _ in 0..100 {
if is_terminal_state(store.get_task(&first.id.0).await.unwrap().status.state) {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
let mut third = make_test_message();
third.id = MessageId::new("msg-third");
assert_eq!(handler.message_send(third).await.unwrap().status.state, TaskState::Completed);
}
struct TestInterceptor {
stream_events: Arc<std::sync::atomic::AtomicUsize>,
}
#[async_trait::async_trait]
impl super::super::interceptor::CallInterceptor for TestInterceptor {
async fn before(
&self,
call: &super::super::interceptor::CallContext,
params: &mut serde_json::Value,
) -> Result<Option<serde_json::Value>, A2aError> {
match call.method {
"SendMessage" | "SendStreamingMessage" => {
for part in params["message"]["parts"].as_array_mut().into_iter().flatten() {
if let Some(text) = part["text"].as_str() {
part["text"] = text.to_uppercase().into();
}
}
Ok(None)
}
"CancelTask" => {
Err(A2aError::UnsupportedOperation { method: "cancel is off".to_string() })
}
"GetTask" if params["id"] == "canned" => Ok(Some(serde_json::json!({
"id": "canned",
"contextId": "ctx",
"status": {"state": "TASK_STATE_COMPLETED"},
}))),
_ => Ok(None),
}
}
async fn after(
&self,
call: &super::super::interceptor::CallContext,
result: &mut serde_json::Value,
) -> Result<(), A2aError> {
if call.method == "SendStreamingMessage" {
self.stream_events.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}
if call.method == "SendMessage" {
result["metadata"]["intercepted"] = true.into();
}
Ok(())
}
}
#[tokio::test]
async fn call_interceptors_run_around_every_operation() {
let stream_events = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let (handler, _) = handler_with_agent(Arc::new(EchoAgent));
let handler = handler
.with_interceptor(Arc::new(TestInterceptor { stream_events: stream_events.clone() }));
let mut msg = make_test_message();
msg.parts = vec![Part::text("quiet")];
let task = handler.message_send(msg).await.unwrap();
assert_eq!(artifact_texts(&task), vec!["echo: QUIET".to_string()], "params rewritten");
assert_eq!(task.metadata.unwrap()["intercepted"], true, "result rewritten");
assert!(matches!(
handler.tasks_cancel(&task.id.0).await,
Err(A2aError::UnsupportedOperation { .. })
));
assert_eq!(
handler.tasks_get("canned", None).await.unwrap().id.0,
"canned",
"answered early"
);
let mut msg = make_test_message();
msg.id = MessageId::new("msg-stream");
let events = handler.message_stream(msg).await.unwrap().collect::<Vec<_>>().await;
assert_eq!(
stream_events.load(std::sync::atomic::Ordering::SeqCst),
events.len(),
"after sees each event"
);
}
fn make_handler() -> RequestHandler {
let store = Arc::new(InMemoryTaskStore::new());
let executor = Arc::new(V1Executor::new(store.clone()));
let push_sender = Arc::new(NoOpPushNotificationSender);
let card = make_test_agent_card();
let cached = Arc::new(RwLock::new(CachedAgentCard::new(card)));
RequestHandler::new(executor, store, push_sender, cached)
}
fn make_test_agent_card() -> AgentCard {
AgentCard {
name: "test-agent".to_string(),
url: Some("http://localhost:8080".to_string()),
description: "A test agent".to_string(),
version: "1.0.0".to_string(),
supported_interfaces: vec![AgentInterface {
url: "http://localhost:8080/a2a".to_string(),
protocol_binding: "JSONRPC".to_string(),
protocol_version: "1.0".to_string(),
tenant: None,
}],
default_input_modes: vec!["text/plain".to_string()],
default_output_modes: vec!["text/plain".to_string()],
skills: vec![AgentSkill {
id: "echo".to_string(),
name: "Echo".to_string(),
description: "Echoes input".to_string(),
tags: vec![],
examples: None,
input_modes: None,
output_modes: None,
security_requirements: None,
}],
capabilities: AgentCapabilities::none(),
provider: None,
icon_url: None,
documentation_url: None,
security_schemes: None,
security_requirements: None,
signatures: None,
}
}
fn make_test_message() -> Message {
Message {
id: MessageId::new("msg-1"),
role: MessageRole::User,
parts: vec![Part::text("hello")],
task_id: None,
context_id: None,
reference_task_ids: None,
extensions: None,
metadata: None,
}
}
struct FlakySessions {
inner: adk_session::InMemorySessionService,
fail_next_get: std::sync::atomic::AtomicBool,
}
#[async_trait::async_trait]
impl adk_session::SessionService for FlakySessions {
async fn create(
&self,
req: adk_session::CreateRequest,
) -> adk_core::Result<Box<dyn adk_session::Session>> {
self.inner.create(req).await
}
async fn get(
&self,
req: adk_session::GetRequest,
) -> adk_core::Result<Box<dyn adk_session::Session>> {
if self.fail_next_get.swap(false, std::sync::atomic::Ordering::SeqCst) {
return Err(adk_core::AdkError::unavailable(
adk_core::ErrorComponent::Session,
"session.backend.timeout",
"the session backend timed out",
));
}
self.inner.get(req).await
}
async fn list(
&self,
req: adk_session::ListRequest,
) -> adk_core::Result<Vec<Box<dyn adk_session::Session>>> {
self.inner.list(req).await
}
async fn delete(&self, req: adk_session::DeleteRequest) -> adk_core::Result<()> {
self.inner.delete(req).await
}
async fn append_event(
&self,
session_id: &str,
event: adk_core::Event,
) -> adk_core::Result<()> {
self.inner.append_event(session_id, event).await
}
async fn append_event_for_identity(
&self,
req: adk_session::AppendEventRequest,
) -> adk_core::Result<()> {
self.inner.append_event_for_identity(req).await
}
}
#[tokio::test]
async fn message_send_keeps_the_session_when_its_lookup_fails() {
use adk_session::SessionService;
let sessions = Arc::new(FlakySessions {
inner: adk_session::InMemorySessionService::new(),
fail_next_get: std::sync::atomic::AtomicBool::new(false),
});
let identity = adk_core::AdkIdentity::new(
"a2a-stream-test".try_into().unwrap(),
"a2a-ctx-kept".try_into().unwrap(),
"ctx-kept".try_into().unwrap(),
);
sessions
.create(adk_session::CreateRequest {
app_name: "a2a-stream-test".to_string(),
user_id: "a2a-ctx-kept".to_string(),
session_id: Some("ctx-kept".to_string()),
state: std::collections::HashMap::from([(
"plan".to_string(),
serde_json::json!("gold"),
)]),
})
.await
.unwrap();
sessions
.append_event_for_identity(adk_session::AppendEventRequest {
identity: identity.clone(),
event: adk_core::Event::new("earlier"),
})
.await
.unwrap();
let store: Arc<dyn TaskStore> = Arc::new(InMemoryTaskStore::new());
let handler = RequestHandler::with_runner(
Arc::new(V1Executor::new(store.clone())),
store,
Arc::new(NoOpPushNotificationSender),
Arc::new(RwLock::new(CachedAgentCard::new(make_test_agent_card()))),
Arc::new(
adk_runner::Runner::builder()
.app_name("a2a-stream-test")
.agent(Arc::new(ChunkingAgent { chunks: vec!["answer".into()] }))
.session_service(sessions.clone())
.build_config(),
),
);
let mut message = make_test_message();
message.context_id = Some(a2a_protocol_types::ContextId::new("ctx-kept"));
sessions.fail_next_get.store(true, std::sync::atomic::Ordering::SeqCst);
if let Ok(task) = handler.message_send(message).await {
assert_ne!(task.status.state, TaskState::Completed);
}
let stored = sessions.inner.get_for_identity(&identity).await.unwrap();
assert_eq!(
(stored.events().len(), stored.state().get("plan")),
(1, Some(serde_json::json!("gold")))
);
}
#[tokio::test]
async fn message_send_creates_and_completes_task() {
let handler = make_handler();
let msg = make_test_message();
let task = handler.message_send(msg).await.unwrap();
assert_eq!(task.status.state, TaskState::Completed);
assert!(!task.id.0.is_empty());
assert!(!task.context_id.0.is_empty());
assert!(task.history.is_some());
assert_eq!(task.history.unwrap().len(), 1);
}
#[tokio::test]
async fn message_send_uses_provided_context_id() {
let handler = make_handler();
let mut msg = make_test_message();
msg.context_id = Some(a2a_protocol_types::ContextId::new("my-ctx"));
let task = handler.message_send(msg).await.unwrap();
assert_eq!(task.context_id.0, "my-ctx");
}
#[tokio::test]
async fn tasks_get_returns_task() {
let handler = make_handler();
let msg = make_test_message();
let task = handler.message_send(msg).await.unwrap();
let retrieved = handler.tasks_get(&task.id.0, None).await.unwrap();
assert_eq!(retrieved.id, task.id);
assert_eq!(retrieved.status.state, TaskState::Completed);
}
#[tokio::test]
async fn tasks_get_truncates_history() {
let handler = make_handler();
let msg = make_test_message();
let task = handler.message_send(msg).await.unwrap();
let msg2 = Message {
id: MessageId::new("msg-2"),
role: MessageRole::Agent,
parts: vec![Part::text("response")],
task_id: None,
context_id: None,
reference_task_ids: None,
extensions: None,
metadata: None,
};
handler.task_store.add_history_message(&task.id.0, msg2).await.unwrap();
let retrieved = handler.tasks_get(&task.id.0, Some(1)).await.unwrap();
assert_eq!(retrieved.history.unwrap().len(), 1);
}
#[tokio::test]
async fn tasks_get_not_found() {
let handler = make_handler();
let err = handler.tasks_get("nonexistent", None).await.unwrap_err();
assert!(err.to_string().contains("nonexistent"));
}
#[tokio::test]
async fn tasks_cancel_cancels_working_task() {
let handler = make_handler();
let task_id = "cancel-test";
let ctx_id = "ctx-cancel";
handler.executor.create_task(task_id, ctx_id).await.unwrap();
handler.executor.transition_state(task_id, ctx_id, TaskState::Working, None).await.unwrap();
let task = handler.tasks_cancel(task_id).await.unwrap();
assert_eq!(task.status.state, TaskState::Canceled);
}
#[tokio::test]
async fn tasks_cancel_rejects_terminal_task() {
let handler = make_handler();
let msg = make_test_message();
let task = handler.message_send(msg).await.unwrap();
let err = handler.tasks_cancel(&task.id.0).await.unwrap_err();
assert!(matches!(err, A2aError::TaskNotCancelable { .. }));
}
#[tokio::test]
async fn tasks_cancel_not_found() {
let handler = make_handler();
let err = handler.tasks_cancel("nonexistent").await.unwrap_err();
assert!(err.to_string().contains("nonexistent"));
}
#[tokio::test]
async fn tasks_list_returns_all_tasks() {
let handler = make_handler();
handler.message_send(make_test_message()).await.unwrap();
let mut msg2 = make_test_message();
msg2.id = MessageId::new("msg-list-2");
handler.message_send(msg2).await.unwrap();
let tasks = handler.tasks_list(ListTasksParams::default()).await.unwrap();
assert_eq!(tasks.tasks.len(), 2);
}
#[tokio::test]
async fn tasks_list_filters_by_context_id() {
let handler = make_handler();
let mut msg1 = make_test_message();
msg1.context_id = Some(a2a_protocol_types::ContextId::new("ctx-a"));
handler.message_send(msg1).await.unwrap();
let mut msg2 = make_test_message();
msg2.id = MessageId::new("msg-ctx-b");
msg2.context_id = Some(a2a_protocol_types::ContextId::new("ctx-b"));
handler.message_send(msg2).await.unwrap();
let tasks = handler
.tasks_list(ListTasksParams {
context_id: Some("ctx-a".to_string()),
..Default::default()
})
.await
.unwrap();
assert_eq!(tasks.tasks.len(), 1);
assert_eq!(tasks.tasks[0].context_id.0, "ctx-a");
}
#[tokio::test]
async fn tasks_list_with_page_size() {
let handler = make_handler();
handler.message_send(make_test_message()).await.unwrap();
let mut msg2 = make_test_message();
msg2.id = MessageId::new("msg-page-2");
handler.message_send(msg2).await.unwrap();
let mut msg3 = make_test_message();
msg3.id = MessageId::new("msg-page-3");
handler.message_send(msg3).await.unwrap();
let tasks = handler
.tasks_list(ListTasksParams { page_size: Some(2), ..Default::default() })
.await
.unwrap();
assert_eq!(tasks.tasks.len(), 2);
}
#[tokio::test]
async fn tasks_list_empty() {
let handler = make_handler();
let tasks = handler.tasks_list(ListTasksParams::default()).await.unwrap();
assert!(tasks.tasks.is_empty());
}
#[tokio::test]
async fn push_config_lifecycle() {
let handler = make_handler();
let msg = make_test_message();
let task = handler.message_send(msg).await.unwrap();
let task_id = &task.id.0;
let config = TaskPushNotificationConfig::new(task_id, "https://example.com/webhook");
let created = handler.push_config_create(task_id, config).await.unwrap();
assert!(created.id.is_some());
assert_eq!(created.url, "https://example.com/webhook");
let config_id = created.id.clone().unwrap();
let retrieved = handler.push_config_get(task_id, &config_id).await.unwrap();
assert_eq!(retrieved.url, "https://example.com/webhook");
let configs = handler.push_config_list(task_id).await.unwrap();
assert_eq!(configs.len(), 1);
handler.push_config_delete(task_id, &config_id).await.unwrap();
let configs = handler.push_config_list(task_id).await.unwrap();
assert!(configs.is_empty());
}
#[tokio::test]
async fn push_config_get_not_found() {
let handler = make_handler();
let msg = make_test_message();
let task = handler.message_send(msg).await.unwrap();
let err = handler.push_config_get(&task.id.0, "nonexistent").await.unwrap_err();
assert!(err.to_string().contains("nonexistent"));
}
#[tokio::test]
async fn deleting_a_push_config_the_task_lacks_succeeds() {
let handler = make_handler();
let msg = make_test_message();
let task = handler.message_send(msg).await.unwrap();
handler.push_config_delete(&task.id.0, "nonexistent").await.unwrap();
let err = handler.push_config_delete("no-such-task", "nonexistent").await.unwrap_err();
assert!(matches!(err, A2aError::TaskNotFound { .. }), "{err}");
}
#[tokio::test]
async fn push_config_create_task_not_found() {
let handler = make_handler();
let config = TaskPushNotificationConfig::new("nonexistent", "https://example.com/hook");
let err = handler.push_config_create("nonexistent", config).await.unwrap_err();
assert!(err.to_string().contains("nonexistent"));
}
#[tokio::test]
async fn the_extended_card_is_built_for_each_caller() {
assert!(matches!(
make_handler().agent_card_extended().await,
Err(A2aError::ExtendedAgentCardNotConfigured)
));
let producer: ExtendedCardProducer = Arc::new(|caller: Option<RequestContext>| {
Box::pin(async move {
let mut card = make_test_agent_card();
card.description = match caller {
Some(caller) => format!("for {}", caller.user_id),
None => "for anyone".to_string(),
};
Ok(card)
})
});
let handler = make_handler().with_extended_card(producer);
assert_eq!(handler.agent_card_extended().await.unwrap().description, "for anyone");
let alice = RequestContext {
user_id: "alice".to_string(),
scopes: Vec::new(),
metadata: HashMap::new(),
};
let card = handler.agent_card_extended_for(Some(alice)).await.unwrap();
assert_eq!(card.description, "for alice");
}
#[tokio::test]
async fn message_stream_yields_events() {
use a2a_protocol_types::events::StreamResponse;
let handler = make_handler();
let mut msg = make_test_message();
msg.id = MessageId::new("msg-stream-test");
let mut stream = handler.message_stream(msg).await.unwrap();
let first = stream.next().await.unwrap().unwrap();
assert!(matches!(first, StreamResponse::Task(_)), "first event should be Task");
let event1 = stream.next().await.unwrap().unwrap();
assert!(matches!(
event1,
StreamResponse::StatusUpdate(ref e) if e.status.state == TaskState::Working
));
let event2 = stream.next().await.unwrap().unwrap();
assert!(matches!(
event2,
StreamResponse::StatusUpdate(ref e) if e.status.state == TaskState::Completed
));
assert!(stream.next().await.is_none());
}
#[tokio::test]
async fn message_send_rejects_empty_parts() {
let handler = make_handler();
let mut msg = make_test_message();
msg.parts = vec![];
let err = handler.message_send(msg).await.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }));
assert!(err.to_string().contains("at least one part"));
}
#[tokio::test]
async fn message_send_rejects_empty_message_id() {
let handler = make_handler();
let mut msg = make_test_message();
msg.id = MessageId::new("");
let err = handler.message_send(msg).await.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }));
assert!(err.to_string().contains("messageId"));
}
#[tokio::test]
async fn message_send_rejects_whitespace_message_id() {
let handler = make_handler();
let mut msg = make_test_message();
msg.id = MessageId::new(" ");
let err = handler.message_send(msg).await.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }));
}
#[tokio::test]
async fn message_send_rejects_long_message_id() {
let handler = make_handler();
let mut msg = make_test_message();
msg.id = MessageId::new("x".repeat(257));
let err = handler.message_send(msg).await.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }));
assert!(err.to_string().contains("256"));
}
#[tokio::test]
async fn tasks_get_rejects_empty_task_id() {
let handler = make_handler();
let err = handler.tasks_get("", None).await.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }));
assert!(err.to_string().contains("taskId"));
}
#[tokio::test]
async fn tasks_get_rejects_long_task_id() {
let handler = make_handler();
let long_id = "x".repeat(257);
let err = handler.tasks_get(&long_id, None).await.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }));
}
#[tokio::test]
async fn tasks_cancel_rejects_empty_task_id() {
let handler = make_handler();
let err = handler.tasks_cancel("").await.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }));
}
#[tokio::test]
async fn message_send_rejects_oversized_metadata() {
let handler = make_handler();
let mut msg = make_test_message();
let big_value = "x".repeat(70_000);
msg.metadata = Some(serde_json::json!({"big": big_value}));
let err = handler.message_send(msg).await.unwrap_err();
assert!(matches!(err, A2aError::InvalidParams { .. }));
assert!(err.to_string().contains("64 KB"));
}
#[tokio::test]
async fn message_send_idempotent_same_message_id() {
let handler = make_handler();
let msg1 = make_test_message();
let msg2 = make_test_message();
let task1 = handler.message_send(msg1).await.unwrap();
let task2 = handler.message_send(msg2).await.unwrap();
assert_eq!(task1.id, task2.id, "same messageId should return same task");
}
#[tokio::test]
async fn message_send_different_message_id_creates_new_task() {
let handler = make_handler();
let msg1 = make_test_message();
let mut msg2 = make_test_message();
msg2.id = MessageId::new("msg-2");
let task1 = handler.message_send(msg1).await.unwrap();
let task2 = handler.message_send(msg2).await.unwrap();
assert_ne!(task1.id, task2.id, "different messageId should create different tasks");
}
#[tokio::test]
async fn message_stream_idempotent_returns_existing() {
use a2a_protocol_types::events::StreamResponse;
let handler = make_handler();
let msg1 = make_test_message();
let task = handler.message_send(msg1).await.unwrap();
let msg2 = make_test_message(); let mut stream = handler.message_stream(msg2).await.unwrap();
let first = stream.next().await.unwrap().unwrap();
match first {
StreamResponse::Task(t) => assert_eq!(t.id.0, task.id.0),
other => panic!("expected Task variant, got {other:?}"),
}
}
#[tokio::test]
async fn a_context_message_continues_the_waiting_task_only_when_it_answers_its_call() {
let handler = make_handler();
let task_id = "resume-test";
let ctx_id = "ctx-resume";
handler.executor.create_task(task_id, ctx_id).await.unwrap();
handler.executor.transition_state(task_id, ctx_id, TaskState::Working, None).await.unwrap();
let call = super::super::convert::tagged_function_call_part(
"approve",
&serde_json::json!({}),
Some("c1"),
true,
);
handler
.executor
.transition_state_with_parts(task_id, ctx_id, TaskState::InputRequired, vec![call])
.await
.unwrap();
let mut follow_up = make_test_message();
follow_up.id = MessageId::new("msg-follow-up");
follow_up.context_id = Some(a2a_protocol_types::ContextId::new(ctx_id));
let new_task = handler.message_send(follow_up).await.unwrap();
assert_ne!(new_task.id.0, task_id);
assert_eq!(new_task.context_id.0, ctx_id);
let waiting = handler.tasks_get(task_id, None).await.unwrap();
assert_eq!(waiting.status.state, TaskState::InputRequired);
let mut answer = make_test_message();
answer.id = MessageId::new("msg-answer");
answer.context_id = Some(a2a_protocol_types::ContextId::new(ctx_id));
answer.parts = vec![super::super::convert::tagged_function_response_part(
"approve",
&serde_json::json!({"approved": true}),
Some("c1"),
)];
let resumed = handler.message_send(answer).await.unwrap();
assert_eq!(resumed.id.0, task_id);
assert_eq!(resumed.status.state, TaskState::Completed);
}
#[tokio::test]
async fn an_answer_that_misses_a_pending_call_is_asked_again_without_running() {
let handler = make_handler();
let (task_id, ctx_id) = ("ask-again", "ctx-ask-again");
handler.executor.create_task(task_id, ctx_id).await.unwrap();
handler.executor.transition_state(task_id, ctx_id, TaskState::Working, None).await.unwrap();
let calls = vec![
super::super::convert::tagged_function_call_part(
"approve",
&serde_json::json!({}),
Some("c1"),
true,
),
super::super::convert::tagged_function_call_part(
"approve",
&serde_json::json!({}),
Some("c2"),
true,
),
];
handler
.executor
.transition_state_with_parts(task_id, ctx_id, TaskState::InputRequired, calls)
.await
.unwrap();
let mut partial_answer = make_test_message();
partial_answer.id = MessageId::new("msg-partial-answer");
partial_answer.task_id = Some(TaskId::new(task_id));
partial_answer.parts = vec![super::super::convert::tagged_function_response_part(
"approve",
&serde_json::json!({"approved": true}),
Some("c1"),
)];
let task = handler.message_send(partial_answer).await.unwrap();
assert_eq!(task.status.state, TaskState::InputRequired);
let parts = task.status.message.expect("the task asks again").parts;
assert_eq!(parts.len(), 3, "both pending calls, then the notice");
assert_eq!(parts[2].text_content(), Some(r#"no input provided for function call ID "c2""#));
assert_eq!(parts[2].metadata.as_ref().unwrap()["validation_error"], true);
let history = task.history.expect("history");
assert_eq!(history.last().unwrap().id.0, "msg-partial-answer");
}
#[tokio::test]
async fn message_send_creates_new_task_for_terminal_context() {
let handler = make_handler();
let mut msg1 = make_test_message();
msg1.id = MessageId::new("msg-terminal-1");
msg1.context_id = Some(a2a_protocol_types::ContextId::new("ctx-terminal"));
let task1 = handler.message_send(msg1).await.unwrap();
assert_eq!(task1.status.state, TaskState::Completed);
let mut msg2 = make_test_message();
msg2.id = MessageId::new("msg-terminal-2");
msg2.context_id = Some(a2a_protocol_types::ContextId::new("ctx-terminal"));
let task2 = handler.message_send(msg2).await.unwrap();
assert_ne!(task1.id, task2.id, "terminal context should create new task");
}
#[tokio::test]
async fn message_send_creates_new_task_without_context_id() {
let handler = make_handler();
let mut msg = make_test_message();
msg.id = MessageId::new("msg-no-ctx");
msg.context_id = None;
let task = handler.message_send(msg).await.unwrap();
assert_eq!(task.status.state, TaskState::Completed);
}
fn caller(user_id: &str) -> Option<RequestContext> {
Some(RequestContext {
user_id: user_id.to_string(),
scopes: vec![],
metadata: Default::default(),
})
}
fn message_in(message_id: &str, context_id: &str) -> Message {
let mut msg = make_test_message();
msg.id = MessageId::new(message_id);
msg.context_id = Some(a2a_protocol_types::ContextId::new(context_id));
msg
}
fn is_not_found<T>(result: Result<T, A2aError>) -> bool {
matches!(result, Err(A2aError::TaskNotFound { .. }))
}
#[tokio::test]
async fn another_callers_task_is_reported_as_missing() {
let handler = make_handler();
handler.executor.create_task_for("bob-task", "bob-ctx", Some("bob")).await.unwrap();
handler
.executor
.transition_state("bob-task", "bob-ctx", TaskState::Working, None)
.await
.unwrap();
let hook = TaskPushNotificationConfig::new("bob-task", "https://example.com/hook");
for scope in [handler.for_caller(caller("alice")), handler.for_caller(None)] {
assert!(is_not_found(scope.tasks_get("bob-task", None).await));
assert!(is_not_found(scope.tasks_cancel("bob-task").await));
assert!(is_not_found(scope.tasks_subscribe("bob-task").await));
assert!(is_not_found(scope.push_config_create("bob-task", hook.clone()).await));
assert!(is_not_found(scope.push_config_list("bob-task").await));
assert!(scope.tasks_list(ListTasksParams::default()).await.unwrap().tasks.is_empty());
}
let bob = handler.for_caller(caller("bob"));
assert_eq!(bob.tasks_get("bob-task", None).await.unwrap().status.state, TaskState::Working);
assert_eq!(bob.tasks_list(ListTasksParams::default()).await.unwrap().tasks.len(), 1);
assert_eq!(bob.tasks_cancel("bob-task").await.unwrap().status.state, TaskState::Canceled);
}
#[tokio::test]
async fn a_reused_message_id_does_not_return_another_callers_task() {
let handler = make_handler();
let bobs = handler
.for_caller(caller("bob"))
.message_send(message_in("shared-msg", "bob-ctx"))
.await
.unwrap();
let alices = handler
.for_caller(caller("alice"))
.message_send(message_in("shared-msg", "alice-ctx"))
.await
.unwrap();
assert_ne!(alices.id, bobs.id);
assert_eq!(alices.context_id.0, "alice-ctx");
let bob_retry = handler
.for_caller(caller("bob"))
.message_send(message_in("shared-msg", "bob-ctx"))
.await
.unwrap();
assert_eq!(bob_retry.id, bobs.id, "bob's own retry still deduplicates");
}
#[tokio::test]
async fn another_callers_input_required_task_is_not_resumed() {
let handler = make_handler();
handler.executor.create_task_for("bob-task", "shared-ctx", Some("bob")).await.unwrap();
for state in [TaskState::Working, TaskState::InputRequired] {
handler.executor.transition_state("bob-task", "shared-ctx", state, None).await.unwrap();
}
let alices = handler
.for_caller(caller("alice"))
.message_send(message_in("alice-msg", "shared-ctx"))
.await
.unwrap();
assert_ne!(alices.id.0, "bob-task");
let bobs = handler.for_caller(caller("bob")).tasks_get("bob-task", None).await.unwrap();
assert_eq!(bobs.status.state, TaskState::InputRequired);
assert_eq!(bobs.history, None, "alice's message must not join bob's task");
}
#[tokio::test]
async fn the_session_belongs_to_the_authenticated_caller() {
let sessions = Arc::new(adk_session::InMemorySessionService::new());
let store: Arc<dyn TaskStore> = Arc::new(InMemoryTaskStore::new());
let runner_config = Arc::new(
adk_runner::Runner::builder()
.app_name("a2a-identity")
.agent(Arc::new(ChunkingAgent { chunks: vec!["ok".into()] }))
.session_service(sessions.clone())
.build_config(),
);
let handler = RequestHandler::with_runner(
Arc::new(V1Executor::new(store.clone())),
store,
Arc::new(NoOpPushNotificationSender),
Arc::new(RwLock::new(CachedAgentCard::new(make_test_agent_card()))),
runner_config,
);
handler.for_caller(caller("alice")).message_send(message_in("m1", "ctx-1")).await.unwrap();
use adk_session::SessionService;
let session_for = |user_id: &str| {
sessions.get(adk_session::GetRequest {
app_name: "a2a-identity".to_string(),
user_id: user_id.to_string(),
session_id: "ctx-1".to_string(),
num_recent_events: None,
after: None,
})
};
assert!(session_for("alice").await.is_ok());
assert!(
session_for("a2a-ctx-1").await.is_err(),
"the client-chosen context ID must not name the session user"
);
}
#[tokio::test]
async fn the_owner_is_not_sent_on_the_wire() {
let handler = make_handler();
let alice = handler.for_caller(caller("alice"));
let task = alice.message_send(message_in("m-send", "ctx")).await.unwrap();
assert_eq!(task.metadata, None);
let entry = handler.task_store.get_task(&task.id.0).await.unwrap();
assert_eq!(entry.owner(), Some("alice"));
let events: Vec<_> =
alice.message_stream(message_in("m-stream", "ctx")).await.unwrap().collect().await;
for event in events {
match event.unwrap() {
StreamResponse::Task(task) => assert_eq!(task.metadata, None),
StreamResponse::StatusUpdate(update) => assert_eq!(update.metadata, None),
StreamResponse::ArtifactUpdate(update) => assert_eq!(update.metadata, None),
StreamResponse::Message(message) => assert_eq!(message.metadata, None),
_ => {}
}
}
}
}