//! Subagent spawning — launch a subagent on a background OS thread. //! //! Flow: creates a new tokio runtime on a dedicated OS thread, then //! `block_on` the engine's `run_agent` future. Returns a //! `JoinHandle>` the caller can `.join()`. use std::thread; use anyhow::Result; use tracing::{info, instrument}; use crate::subagent::context::SubagentContext; use crate::subagent::division::AccessTier; use crate::subagent::engine::run_agent; use crate::tools::ToolCtx; /// Spawn a subagent on a background OS thread. /// /// The subagent runs inside its own tokio runtime so it can make async calls /// without blocking the calling thread's runtime. /// /// Flow: `thread::spawn` → create `tokio::runtime::Runtime` → /// `runtime.block_on(run_agent(...))` → return. /// /// Returns a `JoinHandle` the caller can `join()` to await the result. #[instrument(skip(ctx, tool_ctx))] pub fn spawn_subagent( ctx: SubagentContext, directive: String, access: AccessTier, tool_ctx: ToolCtx, ) -> thread::JoinHandle> { info!("Spawning subagent: {directive}"); thread::spawn(move || { let rt = tokio::runtime::Runtime::new()?; rt.block_on(run_agent(ctx, &directive, access, tool_ctx)) }) }