use super::{AgentLoopError, AgentLoopResult, agent_loop_stream_error};
use crate::{
HookDecision, HookInvocation, HookInvocationId, RuntimeError, ToolExecutionOutcome,
bridge::{BridgeToolResultCommand, BridgeToolResultPayload},
hooks::bridge::HookDecisionCommand,
};
use futures_util::StreamExt;
use merry_core::{PendingToolCallBatch, RuntimeEvent, SessionId, ToolCallBatchId, ToolCallId};
use std::sync::{
Arc,
atomic::{AtomicU64, Ordering},
};
use tokio::sync::{mpsc, oneshot};
use tokio_stream::wrappers::ReceiverStream;
pub struct AgentRun {
session_id: SessionId,
events: ReceiverStream<AgentRunMessage>,
loop_token: tokio_util::sync::CancellationToken,
producer_handle: Option<tokio::task::JoinHandle<()>>,
result_receiver: Option<oneshot::Receiver<Result<AgentLoopResult, AgentLoopError>>>,
bridge_sender: mpsc::Sender<BridgeToolResultCommand>,
pending_tool_invocations: Option<PendingToolCallBatch>,
bridge_resolution_epoch: Arc<AtomicU64>,
observed_bridge_resolution_epoch: u64,
hook_sender: mpsc::Sender<HookDecisionCommand>,
pending_hook_invocation: Option<HookInvocation>,
hook_resolution_epoch: Arc<AtomicU64>,
observed_hook_resolution_epoch: u64,
}
impl AgentRun {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
session_id: SessionId,
events: ReceiverStream<AgentRunMessage>,
loop_token: tokio_util::sync::CancellationToken,
producer_handle: tokio::task::JoinHandle<()>,
result_receiver: oneshot::Receiver<Result<AgentLoopResult, AgentLoopError>>,
bridge_sender: mpsc::Sender<BridgeToolResultCommand>,
bridge_resolution_epoch: Arc<AtomicU64>,
hook_sender: mpsc::Sender<HookDecisionCommand>,
hook_resolution_epoch: Arc<AtomicU64>,
) -> Self {
let observed_bridge_resolution_epoch = bridge_resolution_epoch.load(Ordering::Acquire);
let observed_hook_resolution_epoch = hook_resolution_epoch.load(Ordering::Acquire);
Self {
session_id,
events,
loop_token,
producer_handle: Some(producer_handle),
result_receiver: Some(result_receiver),
bridge_sender,
pending_tool_invocations: None,
bridge_resolution_epoch,
observed_bridge_resolution_epoch,
hook_sender,
pending_hook_invocation: None,
hook_resolution_epoch,
observed_hook_resolution_epoch,
}
}
fn synchronize_bridge_resolution(&mut self) {
let epoch = self.bridge_resolution_epoch.load(Ordering::Acquire);
if epoch != self.observed_bridge_resolution_epoch {
self.pending_tool_invocations = None;
self.observed_bridge_resolution_epoch = epoch;
}
let hook_epoch = self.hook_resolution_epoch.load(Ordering::Acquire);
if hook_epoch != self.observed_hook_resolution_epoch {
self.pending_hook_invocation = None;
self.observed_hook_resolution_epoch = hook_epoch;
}
}
pub async fn submit_bridge_tool_outcomes(
&mut self,
batch_id: &ToolCallBatchId,
outcomes: Vec<(ToolCallId, ToolExecutionOutcome)>,
) -> Result<(), RuntimeError> {
let Some(expected_batch) = self.pending_tool_invocations.as_ref() else {
return Err(RuntimeError::NoPendingAgentRunToolInvocations {
session_id: self.session_id.clone(),
});
};
if expected_batch.id() != batch_id {
return Err(RuntimeError::BridgeToolResultBatchIdMismatch {
session_id: self.session_id.clone(),
expected_batch_id: expected_batch.id().clone(),
received_batch_id: batch_id.clone(),
});
}
if outcomes.is_empty() {
return Err(RuntimeError::BridgeToolResultBatchEmpty {
session_id: self.session_id.clone(),
});
}
let result = self
.submit_bridge_command(BridgeToolResultPayload::Outcomes {
batch_id: batch_id.clone(),
outcomes,
})
.await;
match &result {
Ok(()) => self.pending_tool_invocations = None,
Err(error) if error.is_retryable_bridge_tool_result() => {}
Err(_) => self.pending_tool_invocations = None,
}
result
}
async fn submit_bridge_command(
&self,
payload: BridgeToolResultPayload,
) -> Result<(), RuntimeError> {
let (ack_sender, ack_receiver) = oneshot::channel();
let command = BridgeToolResultCommand {
payload,
ack_sender,
};
self.bridge_sender
.send(command)
.await
.map_err(|_| RuntimeError::AgentRunClosed {
session_id: self.session_id.clone(),
message: "agent run closed before accepting the bridge tool result",
})?;
ack_receiver
.await
.map_err(|_| RuntimeError::AgentRunClosed {
session_id: self.session_id.clone(),
message: "agent run closed before acknowledging the bridge tool result",
})?
}
pub async fn submit_hook_decision(
&mut self,
invocation_id: &HookInvocationId,
decision: HookDecision,
) -> Result<(), RuntimeError> {
self.submit_hook_outcome(invocation_id, Ok(decision)).await
}
pub async fn fail_hook_invocation(
&mut self,
invocation_id: &HookInvocationId,
message: String,
) -> Result<(), RuntimeError> {
self.submit_hook_outcome(invocation_id, Err(message)).await
}
async fn submit_hook_outcome(
&mut self,
invocation_id: &HookInvocationId,
outcome: Result<HookDecision, String>,
) -> Result<(), RuntimeError> {
self.synchronize_bridge_resolution();
let Some(expected) = self.pending_hook_invocation.as_ref() else {
return Err(RuntimeError::NoPendingAgentRunHookInvocation {
session_id: self.session_id.clone(),
});
};
if expected.id() != invocation_id {
return Err(RuntimeError::HookInvocationIdMismatch {
session_id: self.session_id.clone(),
expected_invocation_id: expected.id().clone(),
received_invocation_id: invocation_id.clone(),
});
}
let (ack_sender, ack_receiver) = oneshot::channel();
self.hook_sender
.send(HookDecisionCommand {
invocation_id: invocation_id.clone(),
outcome,
ack_sender,
})
.await
.map_err(|_| RuntimeError::AgentRunClosed {
session_id: self.session_id.clone(),
message: "agent run closed before accepting the hook decision",
})?;
let result = ack_receiver
.await
.map_err(|_| RuntimeError::AgentRunClosed {
session_id: self.session_id.clone(),
message: "agent run closed before acknowledging the hook decision",
})?;
if result.is_ok() {
self.pending_hook_invocation = None;
}
result
}
pub async fn result(&mut self) -> Result<AgentLoopResult, AgentLoopError> {
loop {
match self.next_message().await {
Ok(Some(AgentRunMessage::Event(_))) => {}
Ok(Some(AgentRunMessage::ToolInvocations { batch: _ })) => {
self.request_cancel();
self.pending_tool_invocations = None;
return Err(agent_loop_stream_error(
&self.session_id,
Vec::new(),
"agent run result requested before the host tool batch was resolved",
));
}
Ok(Some(AgentRunMessage::HookInvocation { .. })) => {
self.request_cancel();
self.pending_hook_invocation = None;
return Err(agent_loop_stream_error(
&self.session_id,
Vec::new(),
"agent run result requested before the hook invocation was resolved",
));
}
Ok(None) => break,
Err(source) => {
self.request_cancel();
self.pending_tool_invocations = None;
self.pending_hook_invocation = None;
return Err(AgentLoopError::new(Vec::new(), source));
}
}
}
let Some(result_receiver) = self.result_receiver.take() else {
return Err(agent_loop_stream_error(
&self.session_id,
Vec::new(),
"agent loop stream result was already consumed",
));
};
match result_receiver.await {
Ok(result) => result,
Err(_) => Err(agent_loop_stream_error(
&self.session_id,
Vec::new(),
"agent loop stream producer stopped before returning a result",
)),
}
}
pub async fn cancel_and_wait(&mut self) {
self.loop_token.cancel();
self.pending_tool_invocations = None;
self.pending_hook_invocation = None;
while self.events.next().await.is_some() {}
if let Some(handle) = self.producer_handle.take() {
let _ = handle.await;
}
}
pub async fn next_message(&mut self) -> Result<Option<AgentRunMessage>, RuntimeError> {
self.synchronize_bridge_resolution();
if let Some(batch) = self.pending_tool_invocations.as_ref() {
return Err(RuntimeError::AgentRunToolInvocationsPending {
session_id: self.session_id.clone(),
batch_id: batch.id().clone(),
});
}
if let Some(invocation) = self.pending_hook_invocation.as_ref() {
return Err(RuntimeError::AgentRunHookInvocationPending {
session_id: self.session_id.clone(),
invocation_id: invocation.id().clone(),
});
}
let Some(message) = self.events.next().await else {
return Ok(None);
};
match &message {
AgentRunMessage::ToolInvocations { batch } => {
if batch.calls().is_empty() {
return Err(RuntimeError::BridgeToolResultBatchEmpty {
session_id: self.session_id.clone(),
});
}
self.pending_tool_invocations = Some(batch.clone());
}
AgentRunMessage::HookInvocation { invocation } => {
self.pending_hook_invocation = Some(invocation.clone());
}
AgentRunMessage::Event(_) => {}
}
Ok(Some(message))
}
pub async fn next(&mut self) -> Result<Option<AgentRunMessage>, RuntimeError> {
self.next_message().await
}
pub fn request_cancel(&mut self) {
self.loop_token.cancel();
self.pending_tool_invocations = None;
self.pending_hook_invocation = None;
}
}
impl Drop for AgentRun {
fn drop(&mut self) {
self.loop_token.cancel();
if let Some(handle) = self.producer_handle.take() {
handle.abort();
}
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AgentRunMessage {
Event(RuntimeEvent),
ToolInvocations {
batch: PendingToolCallBatch,
},
HookInvocation {
invocation: HookInvocation,
},
}
impl AgentRunMessage {
#[must_use]
pub fn as_event(&self) -> Option<&RuntimeEvent> {
match self {
Self::Event(event) => Some(event),
Self::ToolInvocations { .. } | Self::HookInvocation { .. } => None,
}
}
#[must_use]
pub fn as_tool_invocations(&self) -> Option<&PendingToolCallBatch> {
match self {
Self::ToolInvocations { batch } => Some(batch),
Self::Event(_) | Self::HookInvocation { .. } => None,
}
}
#[must_use]
pub fn as_hook_invocation(&self) -> Option<&HookInvocation> {
match self {
Self::HookInvocation { invocation } => Some(invocation),
Self::Event(_) | Self::ToolInvocations { .. } => None,
}
}
}