use super::*;
impl MultiAgentRuntime {
pub async fn spawn_child(
&self,
name: &str,
system_prompt: String,
depth: i32,
full_permission: bool,
parent_messages: Vec<agent_base::ChatMessage>,
) -> Result<String, String> {
let config = ChildConfig {
system_prompt: Some(system_prompt),
full_permission: Some(full_permission),
..Default::default()
};
let prepared = self
.spawn_inner(name, depth, &config, parent_messages)
.await
.map_err(|e| match e {
AgentError::ConfigError(s) => s,
other => other.to_string(),
})?;
let path = prepared.path.clone();
self.spawn_ready(prepared);
Ok(path.to_string())
}
#[allow(clippy::too_many_arguments)]
async fn spawn_inner(
&self,
name: &str,
depth: i32,
config: &ChildConfig,
parent_messages: Vec<agent_base::ChatMessage>,
) -> Result<PreparedChild, AgentError> {
let path = AgentPath::root().join(name);
let ticket = self
.control
.budget()
.try_reserve_spawn()
.map_err(|e| AgentError::ConfigError(e.to_string()))?;
{
let mut registry = self.registry.lock().unwrap();
registry
.can_spawn(depth)
.map_err(|e| AgentError::ConfigError(e.to_string()))?;
registry
.register(&path, depth)
.map_err(|e| AgentError::ConfigError(e.to_string()))?;
}
let slot = match self.limiter.try_acquire() {
Ok(slot) => slot,
Err(e) => {
self.registry.lock().unwrap().close(&path);
return Err(AgentError::ConfigError(e.to_string()));
}
};
let child_mailbox = match self.mailbox.register(&path) {
Some(mb) => mb,
None => {
self.registry.lock().unwrap().close(&path);
return Err(AgentError::ConfigError(
"mailbox already exists".to_string(),
));
}
};
let (child_runtime, spawned_tools) = self
.build_child_runtime_with_config(config, self.spawn_permission(config.full_permission))
.await
.map_err(|e| {
self.registry.lock().unwrap().close(&path);
self.mailbox.unregister(&path);
match e {
AgentError::ToolNotFound { .. } => e,
other => {
AgentError::ConfigError(format!("failed to build child runtime: {other}"))
}
}
})?;
let session_id = child_runtime.create_session().await;
self.prefill_child_session(&child_runtime, &session_id, &parent_messages)
.await
.map_err(|e| {
self.registry.lock().unwrap().close(&path);
self.mailbox.unregister(&path);
AgentError::ConfigError(format!("failed to prefill child session: {e}"))
})?;
let child_cancel = self.root_cancel.child_token();
{
let mut cancels = self.child_cancels.lock().unwrap();
cancels.insert(path.clone(), child_cancel.clone());
}
Ok(PreparedChild {
path,
child_mailbox,
child_runtime,
session_id,
child_cancel,
slot,
ticket,
spawned_tools,
})
}
fn spawn_ready(&self, prepared: PreparedChild) {
let PreparedChild {
path,
child_mailbox,
child_runtime,
session_id,
child_cancel,
slot,
ticket,
spawned_tools: _spawned_tools,
} = prepared;
let task_timeout = self.task_timeout;
let agent_path = path.clone();
let mailbox_for_task = self.mailbox.clone();
let registry_for_task = self.registry.clone();
let event_tx = self.event_tx.lock().unwrap().clone();
let cleanup = ChildCleanup {
_slot: slot,
mailbox: self.mailbox.clone(),
registry: self.registry.clone(),
child_cancels: self.child_cancels.clone(),
path: agent_path.clone(),
};
self.join_set.lock().unwrap().spawn(async move {
let _cleanup = cleanup;
run_child_loop(
child_mailbox,
child_runtime,
session_id,
agent_path,
mailbox_for_task,
registry_for_task,
event_tx,
child_cancel,
task_timeout,
)
.await;
});
ticket.commit();
}
#[allow(clippy::too_many_arguments)] pub async fn spawn_child_with_history(
&self,
name: &str,
system_prompt: String,
full_permission: bool,
fork_history: Option<String>,
model: Option<String>,
parent_session_id: &SessionId,
) -> Result<String, String> {
let parent_messages = self
.resolve_fork_history(fork_history, parent_session_id)
.await;
let config = ChildConfig {
system_prompt: Some(system_prompt),
full_permission: Some(full_permission),
model,
..Default::default()
};
self.spawn_with_config_forked(name.to_string(), config, parent_messages)
.await
.map_err(|e| match e {
AgentError::ConfigError(s) => s,
other => other.to_string(),
})
.map(|spawned| spawned.path.to_string())
}
pub fn child(self: &Arc<Self>) -> ChildBuilder {
ChildBuilder::new(Arc::clone(self))
}
#[cfg(test)]
pub(crate) async fn spawn_with_config(
&self,
name: String,
config: ChildConfig,
) -> Result<SpawnedChild, AgentError> {
self.spawn_with_config_forked(name, config, Vec::new())
.await
}
pub(crate) async fn spawn_with_config_forked(
&self,
name: String,
config: ChildConfig,
parent_messages: Vec<agent_base::ChatMessage>,
) -> Result<SpawnedChild, AgentError> {
if config.system_prompt.as_deref().unwrap_or("").is_empty() {
return Err(AgentError::ConfigError(
"ChildConfig.system_prompt is required (set it directly or use a preset)".into(),
));
}
let prepared = self.spawn_inner(&name, 1, &config, parent_messages).await?;
let path = prepared.path.clone();
let spawned_tools = prepared.spawned_tools.clone();
self.spawn_ready(prepared);
Ok(SpawnedChild {
path,
spawned_tools,
})
}
}
struct PreparedChild {
path: AgentPath,
child_mailbox: ChildMailbox,
child_runtime: AgentRuntime,
session_id: SessionId,
child_cancel: CancellationToken,
slot: ExecutionSlot,
ticket: SpawnTicket,
spawned_tools: BTreeSet<String>,
}
pub(crate) struct SpawnedChild {
path: AgentPath,
spawned_tools: BTreeSet<String>,
}
impl SpawnedChild {
pub(crate) fn agent_path(&self) -> &AgentPath {
&self.path
}
pub(crate) fn spawned_tools(&self) -> &BTreeSet<String> {
&self.spawned_tools
}
}
struct ChildCleanup {
_slot: ExecutionSlot,
mailbox: Arc<MailboxHub>,
registry: Arc<Mutex<AgentRegistry>>,
child_cancels: Arc<Mutex<HashMap<AgentPath, CancellationToken>>>,
path: AgentPath,
}
impl Drop for ChildCleanup {
fn drop(&mut self) {
self.mailbox.post_result(MailboxResult {
agent_path: self.path.clone(),
status: MailboxStatus::Closed,
result: None,
denied_tools: vec![],
});
self.registry.lock().unwrap().close(&self.path);
self.mailbox.unregister(&self.path);
self.child_cancels.lock().unwrap().remove(&self.path);
}
}
async fn execute_child_task(
child_runtime: &AgentRuntime,
session_id: &SessionId,
task: &crate::multi_agent::mailbox::MailboxTask,
task_timeout: Option<Duration>,
) -> (MailboxStatus, Option<String>, Vec<String>) {
let input = outcome::build_child_input(task);
let run = child_runtime.run_turn_collect(session_id.clone(), &input);
let result = if let Some(dur) = task_timeout {
match tokio::time::timeout(dur, run).await {
Ok(r) => r,
Err(_elapsed) => {
child_runtime.cancel();
return (
MailboxStatus::Error,
Some(format!("task timed out after {dur:?}")),
vec![],
);
}
}
} else {
run.await
};
match result {
Ok((events, outcome)) => (
MailboxStatus::Ok,
Some(outcome::build_child_result(&outcome, &events)),
outcome::collect_denied_tools(&events),
),
Err(e) => (MailboxStatus::Error, Some(e.to_string()), vec![]),
}
}
#[allow(clippy::too_many_arguments)] async fn run_child_loop(
child_mailbox: ChildMailbox,
child_runtime: AgentRuntime,
session_id: SessionId,
agent_path: AgentPath,
mailbox: Arc<MailboxHub>,
registry: Arc<Mutex<AgentRegistry>>,
event_tx: Option<tokio::sync::mpsc::UnboundedSender<RuntimeEvent>>,
child_cancel: CancellationToken,
task_timeout: Option<Duration>,
) {
let mut task_rx = child_mailbox.task_rx;
if let Some(tx) = event_tx {
let mut child_events = child_runtime.subscribe_runtime_events();
let bridge_path = agent_path.to_string();
let bridge_cancel = child_cancel.clone();
let bridge_registry = registry.clone();
let bridge_agent_path = agent_path.clone();
tokio::spawn(async move {
loop {
tokio::select! {
_ = bridge_cancel.cancelled() => break,
event = child_events.recv() => {
match event {
Ok(event) => {
if matches!(event, RuntimeEvent::ToolCallStarted { .. }) {
bridge_registry
.lock()
.unwrap()
.record_tool_call(&bridge_agent_path);
}
if matches!(event,
RuntimeEvent::RunFinished { .. }
| RuntimeEvent::RunCancelled { .. }
| RuntimeEvent::AwaitingApproval { .. }) {
continue;
}
let _ = tx.send(event.with_agent_id(bridge_path.as_str()));
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
tracing::warn!(
subagent = %bridge_path,
lagged = n,
"child event bridge lagged"
);
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
}
}
}
}
});
}
loop {
tokio::select! {
_ = child_cancel.cancelled() => {
break;
}
task = task_rx.recv() => {
match task {
Some(task) => {
registry.lock().unwrap().note_dequeued(&agent_path);
let (status, result_text, denied_tools) =
execute_child_task(
&child_runtime,
&session_id,
&task,
task_timeout,
)
.await;
registry.lock().unwrap().note_posted(&agent_path);
mailbox.post_result(MailboxResult {
agent_path: agent_path.clone(),
status,
result: result_text,
denied_tools,
});
}
None => break, }
}
}
}
}