use super::child::{
error_info, publish_terminal_activity_snapshot, spawn_reserved_child,
terminal_activity_for_agent,
};
use super::events::SubagentEventSink;
use super::manager::{
SubagentManagerState, enqueue_completion_notification_if_needed, initial_summary,
};
use super::{
ChildRuntimeFactory, ParentPrimaryModel, SubagentActivityHub, SubagentStatusLabel,
SubagentStatusView, SubagentTaskSpec,
};
use crate::{FileSessionStore, RuntimeError, TaskAnchor};
use merry_core::{ErrorInfo, SubagentId, SubagentTaskId};
use merry_llm::GenerationConfig;
use std::{
collections::VecDeque,
sync::{
Arc, Mutex as StdMutex,
atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
},
};
use tokio::sync::{Mutex, Notify};
use tokio_util::sync::CancellationToken;
#[derive(Debug)]
pub(super) struct ReservedChildStart {
pub(super) agent_id: SubagentId,
pub(super) task_id: SubagentTaskId,
pub(super) task: SubagentTaskSpec,
pub(super) task_anchor: TaskAnchor,
pub(super) cancellation_token: CancellationToken,
}
#[derive(Clone)]
pub(super) struct ChildScheduler {
pub(super) parent_session_id: merry_core::SessionId,
pub(super) factory: Arc<dyn ChildRuntimeFactory>,
pub(super) state: Arc<Mutex<SubagentManagerState>>,
pub(super) notify: Arc<Notify>,
pub(super) completion_notifications: Arc<Mutex<VecDeque<SubagentStatusView>>>,
pub(super) completion_notify: Arc<Notify>,
pub(super) enabled: Arc<AtomicBool>,
pub(super) max_threads: Arc<AtomicUsize>,
pub(super) default_timeout_secs: Arc<AtomicU64>,
pub(super) max_total_tokens: Arc<AtomicU64>,
pub(super) depth: u8,
pub(super) activity_hub: Arc<StdMutex<Option<Arc<SubagentActivityHub>>>>,
pub(super) event_sink: Arc<StdMutex<Option<SubagentEventSink>>>,
pub(super) child_session_store: Arc<StdMutex<Option<FileSessionStore>>>,
pub(super) live_parent_model: Arc<StdMutex<Option<ParentPrimaryModel>>>,
pub(super) live_parent_generation: Arc<StdMutex<GenerationConfig>>,
}
impl ChildScheduler {
pub(super) fn attached_event_sink(&self) -> Option<SubagentEventSink> {
self.event_sink
.lock()
.expect("subagent event sink mutex is not poisoned")
.clone()
}
pub(super) fn attached_child_session_store(&self) -> Option<FileSessionStore> {
self.child_session_store
.lock()
.expect("subagent child session store mutex is not poisoned")
.clone()
}
pub(super) fn live_parent_model_snapshot(&self) -> Option<ParentPrimaryModel> {
self.live_parent_model
.lock()
.expect("subagent live parent model mutex is not poisoned")
.clone()
}
pub(super) fn live_parent_generation_snapshot(&self) -> GenerationConfig {
self.live_parent_generation
.lock()
.expect("subagent live parent generation mutex is not poisoned")
.clone()
}
pub(super) fn effective_max_threads(&self) -> usize {
if self.enabled.load(Ordering::Acquire) {
self.max_threads.load(Ordering::Acquire)
} else {
0
}
}
pub(super) fn default_timeout_secs(&self) -> Option<u64> {
match self.default_timeout_secs.load(Ordering::Acquire) {
0 => None,
secs => Some(secs),
}
}
pub(super) fn max_total_tokens(&self) -> Option<u64> {
match self.max_total_tokens.load(Ordering::Acquire) {
0 => None,
limit => Some(limit),
}
}
pub(super) fn attached_activity_hub(&self) -> Option<Arc<SubagentActivityHub>> {
self.activity_hub
.lock()
.expect("subagent activity hub mutex is not poisoned")
.clone()
}
pub(super) async fn enqueue_completion_notification(&self, status: SubagentStatusView) {
enqueue_completion_notification_if_needed(
&self.state,
&self.completion_notifications,
&self.completion_notify,
status,
)
.await;
}
}
pub(super) fn reserve_queued_starts_locked(
state: &mut SubagentManagerState,
max_threads: usize,
) -> Vec<ReservedChildStart> {
let mut global_available = max_threads.saturating_sub(running_child_count(state));
if global_available == 0 {
return Vec::new();
}
let queued_ids = state
.agents
.values()
.filter(|agent| agent.status == SubagentStatusLabel::Queued)
.map(|agent| agent.agent_id.clone())
.collect::<Vec<_>>();
let mut starts = Vec::new();
for agent_id in queued_ids {
if global_available == 0 {
break;
}
let Some(agent) = state.agents.get(&agent_id) else {
continue;
};
let batch_id = agent.batch_id;
let batch_max_concurrency = state
.batches
.get(&batch_id)
.map(|batch| batch.max_concurrency)
.unwrap_or(0);
if batch_running_child_count(state, batch_id) >= batch_max_concurrency {
continue;
}
let Some(agent) = state.agents.get_mut(&agent_id) else {
continue;
};
agent.status = SubagentStatusLabel::Running;
agent.summary = initial_summary(SubagentStatusLabel::Running);
starts.push(ReservedChildStart {
agent_id: agent.agent_id.clone(),
task_id: agent.task_id.clone(),
task: agent.task.clone(),
task_anchor: agent.task_anchor.clone(),
cancellation_token: agent.cancellation_token.clone(),
});
global_available -= 1;
}
starts
}
pub(super) fn running_child_count(state: &SubagentManagerState) -> usize {
state
.agents
.values()
.filter(|agent| agent.status == SubagentStatusLabel::Running)
.count()
}
pub(super) fn batch_running_child_count(state: &SubagentManagerState, batch_id: u64) -> usize {
state
.agents
.values()
.filter(|agent| agent.batch_id == batch_id && agent.status == SubagentStatusLabel::Running)
.count()
}
pub(super) async fn start_reserved_children_iteratively(
scheduler: ChildScheduler,
starts: Vec<ReservedChildStart>,
) {
let mut pending = VecDeque::from(starts);
while let Some(start) = pending.pop_front() {
match spawn_reserved_child(scheduler.clone(), &start).await {
Ok(ReservedChildStartOutcome::Started) => {}
Ok(ReservedChildStartOutcome::Cancelled) => {
finish_reserved_child_start(
&scheduler,
&start,
&mut pending,
SubagentStatusLabel::Cancelled,
"child cancelled before runtime start",
error_info(
"subagent_cancelled",
"child cancellation requested before runtime start",
),
)
.await;
}
Err(error) => {
let (summary, diagnostics) = error.failure_details();
finish_reserved_child_start(
&scheduler,
&start,
&mut pending,
SubagentStatusLabel::Failed,
summary,
diagnostics,
)
.await;
}
}
}
}
pub(super) enum ReservedChildStartOutcome {
Started,
Cancelled,
}
pub(super) enum ReservedChildStartError {
Factory(RuntimeError),
}
impl ReservedChildStartError {
fn failure_details(&self) -> (&'static str, ErrorInfo) {
match self {
Self::Factory(error) => (
"child runtime start failed",
error_info("subagent_start_error", error.to_string()),
),
}
}
}
async fn finish_reserved_child_start(
scheduler: &ChildScheduler,
start: &ReservedChildStart,
pending: &mut VecDeque<ReservedChildStart>,
status: SubagentStatusLabel,
summary: &'static str,
diagnostics: ErrorInfo,
) {
let mut state_guard = scheduler.state.lock().await;
let mut terminal_activity = None;
let mut terminal_notification = None;
let mut terminal_task = None;
if let Some(agent) = state_guard.agents.get_mut(&start.agent_id)
&& !agent.status.is_terminal()
{
agent.status = status;
agent.summary = summary.to_owned();
agent.diagnostics = Some(diagnostics);
terminal_activity = terminal_activity_for_agent(agent);
terminal_task = Some(agent.task.task().to_owned());
terminal_notification = Some(agent.status_view());
}
pending.extend(reserve_queued_starts_locked(
&mut state_guard,
scheduler.effective_max_threads(),
));
drop(state_guard);
if let Some((task_id, phase, summary)) = terminal_activity {
publish_terminal_activity_snapshot(
scheduler.attached_activity_hub(),
start.agent_id.clone(),
task_id,
phase,
&summary,
);
}
if let Some(status) = terminal_notification {
let sink = scheduler.attached_event_sink();
super::events::emit_terminal_event_and_persist(
sink.as_ref(),
terminal_task.unwrap_or_else(|| start.task.task().to_owned()),
&status,
)
.await;
scheduler.enqueue_completion_notification(status).await;
}
scheduler.notify.notify_waiters();
}