//! Delegated agent tools backed by native SDK runtimes or the session's
//! host.
mod host_orchestrator;
mod host_target;
mod host_tools;
pub(crate) mod message_receipt;
// Only the daemon's T3 wake watchers use the contract outside the
// host-backed `agent` tools.
pub(crate) use host_orchestrator::HostContract;
pub(crate) use host_tools::install_host_delegation;
use std::{
future::Future,
path::{Path, PathBuf},
pin::Pin,
sync::Arc,
};
use serde::Deserialize;
use serde_json::{json, Value};
use {
crate::agent::{AdvertisedAgents, AgentCatalog},
crate::app::{agent_executor::LaunchWorkspace, subagent_manager::ValidatedMessage},
crate::workspace::ProjectTrusts,
rho_sdk::tool::{
OperationKind, PreparedToolInvocation, Tool, ToolError, ToolErrorKind, ToolInvocation,
ToolMetadata, ToolOutput, ToolPreparationContext, ToolPrepareFuture, ToolResource,
ToolResourceAccess, ToolSecurity,
},
};
use super::sdk_registry::ToolCaller;
use super::agent_output::{
format_background_start, format_list_entry, format_snapshot, AgentRunView, SnapshotFormat,
StartedWorktree,
};
const SUBAGENT_MANAGER: &str = "subagents";
const AGENT_TOOL: &str = "agent";
const AGENTS_TOOL: &str = "agents";
/// Whether a call to tool `name` starts a subagent.
pub(crate) fn starts_subagent(name: &str) -> bool {
name == AGENT_TOOL
}
pub use crate::app::subagent_manager::{SubagentManager, SubagentNotification, SubagentSnapshot};
pub(crate) use super::agent_output::notification_prompt;
pub struct AgentTool {
manager: SubagentManager,
max_output_bytes: usize,
/// Directory definitions are rediscovered from at each launch.
cwd: PathBuf,
/// The session's project trust, which each rediscovery reads under.
project_trust: ProjectTrusts,
/// Fixed at construction so the spec never rewrites what the caller was
/// told; later corrections are appended to the conversation instead.
advertised: AdvertisedAgents,
mutation_observer: Arc<dyn rho_tools::WorkspaceMutationObserver>,
/// Whose loop calls it, which decides how it says results arrive.
caller: ToolCaller,
}
impl AgentTool {
pub(super) fn new(
manager: SubagentManager,
cwd: &Path,
catalog: Option<Arc<AgentCatalog>>,
project_trust: ProjectTrusts,
) -> Self {
let catalog = catalog.unwrap_or_else(|| {
Arc::new(
AgentCatalog::discover(cwd, project_trust)
.expect("agent catalog was validated at startup"),
)
});
Self {
manager,
cwd: cwd.to_path_buf(),
project_trust,
advertised: AdvertisedAgents::from_catalog(&catalog),
mutation_observer: Arc::new(()),
max_output_bytes: rho_tools::DEFAULT_MAX_OUTPUT_BYTES,
caller: ToolCaller::RhoLoop,
}
}
fn with_max_output_bytes(mut self, max_output_bytes: usize) -> Self {
self.max_output_bytes = max_output_bytes;
self
}
/// Described for `caller`'s loop.
fn with_caller(mut self, caller: ToolCaller) -> Self {
self.caller = caller;
self
}
fn with_mutation_observer(
mut self,
mutation_observer: Arc<dyn rho_tools::WorkspaceMutationObserver>,
) -> Self {
self.mutation_observer = mutation_observer;
self
}
async fn execute(
&self,
call: &rho_sdk::ToolCallId,
args: AgentArgs,
context: &rho_sdk::tool::AuthorizedToolContext,
) -> Result<ToolOutput, ToolError> {
// Resolve from disk, not the startup snapshot: the user may have
// deleted or edited the definition (for example in `/agents`) since
// the session began. The spec's agent list stays stable on purpose.
let catalog = AgentCatalog::discover(&self.cwd, self.project_trust).map_err(|error| {
ToolError::new(
ToolErrorKind::Execution,
format!("could not reload agent definitions: {error}"),
)
})?;
let definition = catalog
.find(&args.agent_id)
.map_err(|error| ToolError::new(ToolErrorKind::InvalidArguments, error.to_string()))?
.definition
.clone();
let definition_id = definition.id.to_string();
self.mutation_observer
.mark_untracked_effect(rho_tools::UntrackedWorkspaceEffect::MutatingTool, "agent");
let cwd = context
.workspace_root()
.map(Path::to_path_buf)
.or_else(|| std::env::current_dir().ok())
.unwrap_or_default();
let start_error = |error: anyhow::Error| {
ToolError::new(
ToolErrorKind::Execution,
format!("failed to start delegated agent: {error}"),
)
};
// Bind first, so a bad model or missing credential fails before a
// worktree exists.
let binding = self
.manager
.bind(&definition, args.model.as_deref())
.map_err(start_error)?;
let (workspace, worktree) = match args.worktree {
false => (LaunchWorkspace::Executor, None),
true => {
// Nothing is registered yet; a cancelled add discards what
// git makes once it finishes.
let added = tokio::select! {
biased;
added = self.manager.add_worktree().added() => added.map_err(start_error)?,
() = context.cancellation().cancelled() => return Err(ToolError::cancelled()),
};
let workspace = LaunchWorkspace::Worktree {
worktree: added.worktree.clone(),
cwd: added.cwd,
};
(workspace, Some((added.worktree, added.started)))
}
};
let spawn = self.manager.spawn(binding, &args.prompt, &cwd, workspace);
tokio::pin!(spawn);
let run_id = tokio::select! {
result = &mut spawn => result.map_err(start_error)?,
() = context.cancellation().cancelled() => {
// Finish registration so the manager retains ownership and
// host shutdown can cancel the delegated task.
if let Err(error) = spawn.await {
tracing::warn!(%error, "a cancelled agent call failed to start its run");
}
return Err(ToolError::cancelled());
}
};
// Before the call finishes, so its result names the run.
self.manager.record_starter(&run_id, call.as_str());
// Registration is the start receipt; instant failures still reach
// the parent through automatic completion delivery.
let snapshot = self
.manager
.status(&run_id)
.expect("newly registered delegated run");
// A subagent's worktree is one no daemon session owns.
let worktree = worktree.as_ref().map(|(worktree, from)| StartedWorktree {
worktree,
from,
removal: crate::worktree::Removal::Unowned,
});
rho_tools::Rendered::new(
format_background_start(&run_id, &definition_id, worktree),
AgentRunView::started_with_identity(&snapshot),
)
.limit_data(self.max_output_bytes)?
.into_tool_output(agent_metadata())
}
}
#[derive(Deserialize)]
struct AgentArgs {
agent_id: String,
prompt: String,
model: Option<String>,
#[serde(default)]
worktree: bool,
}
/// Whether a backend offers the `worktree` argument: only one that runs the
/// child itself can add a worktree for it.
#[derive(Clone, Copy)]
enum WorktreeArgument {
Offered,
Absent,
}
impl Tool for AgentTool {
fn spec(&self) -> rho_sdk::model::ToolSpec {
// How the runs behave holds for every caller: the backend is rho's.
let runs = " Nobody is asked to approve a run's tool calls: one that needs approval is denied, unless in Auto the classifier allows it. Runs are hidden from the session list, deleted with this session, and cannot start agents.";
let background_guidance = match self.caller {
ToolCaller::RhoLoop => format!(" Every call starts a background run and returns its id immediately. Completions arrive automatically at safe provider boundaries and wake an idle parent. Continue independent work, or end your turn to wait for required results. Never sleep or poll for results, and do not declare the task complete before required agents finish.{runs}"),
ToolCaller::ExternalAgent(idle) => format!(" Every call starts a background run and returns its id immediately. {} Continue independent work so a required result can land during this turn, or check on the run with `agents`. Never sleep, and do not declare the task complete before required agents finish.{runs}", idle.clause()),
};
agent_spec(
&self.advertised,
&background_guidance,
WorktreeArgument::Offered,
)
}
fn output_schema(&self) -> Option<serde_json::Value> {
Some(rho_tools::output_schema::<AgentRunView>())
}
fn security(&self) -> ToolSecurity {
ToolSecurity::built_in([])
}
fn prepare<'a>(
&'a self,
invocation: ToolInvocation,
_context: ToolPreparationContext,
) -> ToolPrepareFuture<'a> {
let call = invocation.id().clone();
let args = parse_agent_args(invocation.into_arguments(), WorktreeArgument::Offered);
Box::pin(async move {
let args = args?;
// Registry ops are mutex-protected and short. Shared access lets
// several launches in one batch overlap.
Ok(PreparedToolInvocation::resource_aware(
[ToolResourceAccess::shared(ToolResource::manager_state(
SUBAGENT_MANAGER,
))],
[],
agent_metadata(),
move |context| Box::pin(async move { self.execute(&call, args, &context).await }),
))
})
}
}
pub struct AgentsTool {
manager: SubagentManager,
max_output_bytes: usize,
/// Whose loop calls it, which decides how it says results arrive.
caller: ToolCaller,
}
impl AgentsTool {
pub fn new(manager: SubagentManager) -> Self {
Self {
manager,
max_output_bytes: rho_tools::DEFAULT_MAX_OUTPUT_BYTES,
caller: ToolCaller::RhoLoop,
}
}
pub(super) fn with_max_output_bytes(mut self, max_output_bytes: usize) -> Self {
self.max_output_bytes = max_output_bytes;
self
}
/// Described for `caller`'s loop.
fn with_caller(mut self, caller: ToolCaller) -> Self {
self.caller = caller;
self
}
async fn execute(&self, args: AgentsArgs) -> Result<ToolOutput, ToolError> {
let (content, data) = match args.action.as_str() {
"list" => {
let agents = self.manager.list();
let content = if agents.is_empty() {
"no delegated agents".to_string()
} else {
agents
.iter()
.map(format_list_entry)
.collect::<Vec<_>>()
.join("\n")
};
(
content,
AgentsOutput::List {
runs: agents.iter().map(AgentRunView::from).collect(),
},
)
}
"status" => {
let id = required_id(&args)?;
let snapshot = self.manager.observe(id).ok_or_else(|| {
ToolError::new(
ToolErrorKind::InvalidArguments,
format!("unknown delegated run '{id}'"),
)
})?;
// A finished run hands over its full result here and counts
// as delivered; a running run reports progress only.
let format = if snapshot.done {
SnapshotFormat::Completion
} else {
SnapshotFormat::Status
};
(
format_snapshot(&snapshot, format),
AgentsOutput::Status {
run: AgentRunView::from(&snapshot),
},
)
}
"stop" => {
let id = required_id(&args)?;
let snapshot =
self.manager.stop(id).await.map_err(|error| {
ToolError::new(ToolErrorKind::Execution, error.to_string())
})?;
(
format_snapshot(&snapshot, SnapshotFormat::Completion),
AgentsOutput::Stop {
run: AgentRunView::from(&snapshot),
},
)
}
"message" => {
let id = required_id(&args)?;
let text = args.message.as_deref().ok_or_else(|| {
ToolError::new(
ToolErrorKind::InvalidArguments,
"message action requires message text",
)
})?;
// Parse at this argument boundary so an empty or over-budget
// body reads as invalid arguments, not an execution failure.
let message = ValidatedMessage::parse(text).map_err(|error| {
ToolError::new(ToolErrorKind::InvalidArguments, error.to_string())
})?;
self.manager
.message(id, &message)
.await
.map_err(|error| ToolError::new(ToolErrorKind::Execution, error.to_string()))?;
let content = match self.manager.task_identity(id) {
Some(identity) => message_receipt::MessageReceipt {
run_id: identity.run_id,
agent_id: identity.agent_id,
task: identity.task,
}
.content(),
None => format!("queued parent message for delegated run '{id}'"),
};
(content, AgentsOutput::Message { id: id.to_owned() })
}
other => {
return Err(ToolError::new(
ToolErrorKind::InvalidArguments,
format!("unknown action '{other}': expected list, status, stop, or message"),
))
}
};
rho_tools::Rendered::new(content, data)
.limit_data(self.max_output_bytes)?
.into_tool_output(agents_metadata())
}
}
#[derive(serde::Serialize, schemars::JsonSchema)]
#[serde(tag = "action", rename_all = "snake_case")]
enum AgentsOutput {
List { runs: Vec<AgentRunView> },
Status { run: AgentRunView },
Stop { run: AgentRunView },
Message { id: String },
}
#[derive(Deserialize)]
struct AgentsArgs {
action: String,
#[serde(default)]
id: Option<String>,
#[serde(default)]
message: Option<String>,
}
impl Tool for AgentsTool {
fn spec(&self) -> rho_sdk::model::ToolSpec {
let delivery = match self.caller {
ToolCaller::RhoLoop => "Completions and child notices are delivered automatically at safe provider boundaries during work and before final completion. Completions and requests for parent action can wake an idle parent; informational child notices cannot. Waiting for a result means ending your turn, not polling status.".to_owned(),
ToolCaller::ExternalAgent(idle) => format!("Completions and requests for parent action are delivered as results. {} Informational child notices wait for one of those, or for your next turn.", idle.clause()),
};
rho_sdk::model::ToolSpec {
name: AGENTS_TOOL.into(),
description: format!("Check on, stop, or message a delegated background run. {delivery} While a run is in progress, status reports progress only and never partial output - do not act on a run's result before it finishes. Once a run has finished, status or stop returns its final result with pending notices as earlier context and counts as delivery, so it will not be redelivered automatically. Use action=message to steer a running child with plain text: Rho-runtime children apply it at the next provider turn; claude-cli and antigravity children receive it as their next queued user turn. Once a Rho-runtime child has finished and you have its result, action=message follows it up instead: the child continues its own session, with everything it learned, taking the message as its next turn, and its new result is delivered like the first. Prefer this to starting a new agent when the work builds on what the child already did."),
input_schema: json!({
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["list", "status", "stop", "message"],
"description": "Operation to perform"
},
"id": {
"type": "string",
"description": "Delegated run ID (required for status, stop, and message)"
},
"message": {
"type": "string",
"description": "Plain-text parent message (required for message). Rho children apply it at the next provider turn; Claude-cli and Antigravity children queue it as their next user turn. A finished Rho child takes it as a follow-up turn."
}
},
"required": ["action"],
"additionalProperties": false
}),
}
}
fn output_schema(&self) -> Option<serde_json::Value> {
Some(rho_tools::output_schema::<AgentsOutput>())
}
fn security(&self) -> ToolSecurity {
ToolSecurity::built_in([])
}
fn prepare<'a>(
&'a self,
invocation: ToolInvocation,
_context: ToolPreparationContext,
) -> ToolPrepareFuture<'a> {
let args = parse_agents_args(invocation.into_arguments());
Box::pin(async move {
let args = args?;
// list/status/stop all touch the mutex-backed registry briefly.
// Shared access keeps lifecycle ops from serializing launches.
Ok(PreparedToolInvocation::resource_aware(
[ToolResourceAccess::shared(ToolResource::manager_state(
SUBAGENT_MANAGER,
))],
[],
agents_metadata(),
move |_context| Box::pin(async move { self.execute(args).await }),
))
})
}
}
/// Shared launch contract; each backend supplies its completion guidance.
fn agent_spec(
advertised: &AdvertisedAgents,
background_guidance: &str,
worktree: WorktreeArgument,
) -> rho_sdk::model::ToolSpec {
let names: Vec<&str> = advertised.iter().map(|(name, _)| name).collect();
// Deliberately model-free: policy is reloaded at launch, while the spec
// preserves the catalog the caller was originally told about.
let summaries = advertised
.iter()
.map(|(name, description)| format!("{name}: {description}"))
.collect::<Vec<_>>()
.join("\n");
let parallel_guidance =
" Independent agent calls in the same batch run together - issue them in one turn for parallel work.";
let session_pointer = " For work that should outlive this turn, run in another workspace, or be handed to a person, start a session with the sessions tool instead when you have it.";
let mut input_schema = json!({
"type": "object",
"properties": {
"agent_id": {
"type": "string",
"enum": names,
"description": "Agent ID"
},
"prompt": {
"type": "string",
"description": "Self-contained task and all context the agent needs"
},
"model": {
"type": "string",
"description": "Optional per-call Rho model reference (provider/model, catalog model ID, or @alias). Replaces the definition's model/provider/auth pins for this call only; omitted preserves its policy. Unsupported for external runtimes. Unavailable models or credentials fail without fallback."
}
},
"required": ["agent_id", "prompt"],
"additionalProperties": false
});
match worktree {
WorktreeArgument::Offered => {
input_schema["properties"]["worktree"] = json!({
"type": "boolean",
"description": "Run the agent in a new git worktree on a new branch, so its edits cannot collide with yours. The branch starts from the repository's default branch (or HEAD, as configured), without your uncommitted changes. The receipt names the worktree and branch; ask the agent to commit, then merge from that branch. Defaults to false."
});
}
WorktreeArgument::Absent => {}
}
rho_sdk::model::ToolSpec {
name: AGENT_TOOL.into(),
description: format!(
"Delegate a substantial, self-contained task to a fresh agent.{parallel_guidance}{background_guidance}{session_pointer}\n\nAgents:\n{summaries}"
),
input_schema,
}
}
/// Parses an `agent` call for a backend that offers `worktree` as its spec
/// says.
fn parse_agent_args(arguments: Value, worktree: WorktreeArgument) -> Result<AgentArgs, ToolError> {
let args: AgentArgs = serde_json::from_value(arguments)
.map_err(|error| ToolError::new(ToolErrorKind::InvalidArguments, error.to_string()))?;
match (worktree, args.worktree) {
(WorktreeArgument::Absent, true) => {
return Err(ToolError::new(
ToolErrorKind::InvalidArguments,
"worktree is not available here: the host decides where its agents work",
))
}
(WorktreeArgument::Offered, _) | (WorktreeArgument::Absent, false) => {}
}
if args
.model
.as_deref()
.is_some_and(|model| model.is_empty() || model.contains(char::is_whitespace))
{
return Err(ToolError::new(
ToolErrorKind::InvalidArguments,
"model override must be a non-empty model reference without whitespace",
));
}
Ok(args)
}
fn parse_agents_args(arguments: Value) -> Result<AgentsArgs, ToolError> {
serde_json::from_value(arguments)
.map_err(|error| ToolError::new(ToolErrorKind::InvalidArguments, error.to_string()))
}
fn agent_metadata() -> ToolMetadata {
ToolMetadata::new().operation(OperationKind::Other(AGENT_TOOL.into()))
}
fn agents_metadata() -> ToolMetadata {
ToolMetadata::new().operation(OperationKind::Other(AGENTS_TOOL.into()))
}
fn required_id(args: &AgentsArgs) -> Result<&str, ToolError> {
args.id
.as_deref()
.filter(|id| !id.is_empty())
.ok_or_else(|| {
ToolError::new(
ToolErrorKind::InvalidArguments,
"this action requires a delegated run id",
)
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum DelegationToolSelection {
Launch,
Manage,
LaunchAndManage,
}
impl DelegationToolSelection {
pub(super) fn from_capabilities(
capabilities: &crate::agent::AgentCapabilities,
) -> Option<Self> {
use crate::agent::ToolCapability;
match (
capabilities.contains(&ToolCapability::Agent),
capabilities.contains(&ToolCapability::Agents),
) {
(true, true) => Some(Self::LaunchAndManage),
(true, false) => Some(Self::Launch),
(false, true) => Some(Self::Manage),
(false, false) => None,
}
}
fn launches(self) -> bool {
matches!(self, Self::Launch | Self::LaunchAndManage)
}
fn manages(self) -> bool {
matches!(self, Self::Manage | Self::LaunchAndManage)
}
}
pub(super) struct DelegationBundleOptions {
pub cwd: PathBuf,
pub tools: DelegationToolSelection,
pub config_path: PathBuf,
/// Catalog already discovered for `cwd`; rediscovered when absent.
pub catalog: Option<Arc<AgentCatalog>>,
/// The parent's environment, which delegated Rho agents share.
pub environment: crate::session_environment::SessionEnvironment,
/// Where the agents take capacity, and who else hears of them.
pub pool: crate::app::subagent_pool::SubagentPool,
/// Whose loop calls the tools, which decides how they say results
/// arrive.
pub caller: ToolCaller,
}
pub(super) struct SdkDelegationBundle {
tools: Vec<Arc<dyn rho_sdk::tool::Tool>>,
manager: SubagentManager,
advertised: Option<AdvertisedAgents>,
}
impl SdkDelegationBundle {
pub(super) fn manager_handle(&self) -> SubagentManager {
self.manager.clone()
}
/// The catalog the `agent` tool spec advertises; `None` without that tool.
pub(super) fn take_advertised_agents(&mut self) -> Option<AdvertisedAgents> {
self.advertised.take()
}
}
impl super::sdk_registry::ToolBundle for SdkDelegationBundle {
fn tools(&self) -> &[Arc<dyn rho_sdk::tool::Tool>] {
&self.tools
}
fn shutdown(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
Box::pin(self.manager.shutdown())
}
}
pub(super) fn sdk_bundle(
config: &crate::config::Config,
options: DelegationBundleOptions,
mutation_observer: Arc<dyn rho_tools::WorkspaceMutationObserver>,
) -> SdkDelegationBundle {
let manager = SubagentManager::new(
config.clone(),
options.config_path,
options.cwd.clone(),
options.pool,
)
.with_environment(options.environment.clone());
let mut tools = Vec::<Arc<dyn rho_sdk::tool::Tool>>::new();
let mut advertised = None;
if options.tools.launches() {
let tool = AgentTool::new(
manager.clone(),
&options.cwd,
options.catalog,
options.environment.project_trust(),
)
.with_max_output_bytes(config.max_output_bytes)
.with_mutation_observer(mutation_observer)
.with_caller(options.caller);
advertised = Some(tool.advertised.clone());
tools.push(Arc::new(tool));
}
if options.tools.manages() {
tools.push(Arc::new(
AgentsTool::new(manager.clone())
.with_max_output_bytes(config.max_output_bytes)
.with_caller(options.caller),
));
}
SdkDelegationBundle {
tools,
manager,
advertised,
}
}
#[cfg(test)]
#[path = "agent_tests.rs"]
mod tests;