rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The instructions rho gives an agent that runs a daemon session on
//! another runtime (Claude Code, Antigravity): its session definition's
//! own text, then how to write for rho's clients, then how to use the
//! tools rho serves it, if any, then the AGENTS.md files the agent does not
//! read itself, then the skills it does not find itself.
//!
//! The agent keeps its own system prompt and tools, so rho's base prompt,
//! per-model prompt files and the guidance for tools rho does not serve it
//! stay out.

use std::path::Path;

use super::{
    agent_instruction_paths, project_instructions::AGENTS_MD_HEADER, push_context_file,
    push_delegation_guidance, read_existing_files, skill_listing, RENDERING_GUIDANCE,
};
use crate::{
    acp_runtime::agent_tools::McpToolNaming,
    skills::{Skill, SkillSource},
    tools::sdk_registry::IdleDelivery,
};

/// Whether the agent's runtime reads the project's AGENTS.md files itself.
/// The user's `~/.rho/AGENTS.md` is rho's, which no runtime reads.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ProjectInstructionFiles {
    /// The agent reads them: rho leaves them out.
    ReadByAgent,
    /// rho puts them in its instructions.
    FromRho,
}

/// Which skills the agent's runtime finds itself, which rho then leaves
/// out of the skills it lists.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SkillFiles {
    /// It reads the `.agents/skills` folders of the user's home and the
    /// project (agy): rho lists only its built-in skills and its plugins',
    /// which its served `skill` tool loads.
    LooseReadByAgent,
    /// It reads none of rho's (Claude Code): rho lists every skill it
    /// finds.
    FromRho,
}

/// The tools of rho's a run serves the agent
/// ([`crate::acp_runtime::agent_tools`]), for the instructions on using
/// them.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ServedToolGuide {
    /// The tools, by rho's names for them.
    pub(crate) tools: Vec<String>,
    /// The skills rho finds for the session, which the `skill` tool loads.
    pub(crate) skills: Vec<Skill>,
    /// How the results of the work they start reach the agent once its turn
    /// ended: whether the daemon wakes the session for them.
    pub(crate) idle: IdleDelivery,
    /// How the agent names them.
    pub(crate) naming: McpToolNaming,
}

impl ServedToolGuide {
    fn serves(&self, name: &str) -> bool {
        self.tools.iter().any(|tool| tool == name)
    }
}

/// How the agent reaches rho's tools, named as `naming` says, and how
/// their calls run; the instructions follow it with how the results of the
/// background work they start reach it ([`IdleDelivery::clause`]).
fn served_tools(naming: McpToolNaming) -> String {
    format!(
        "
# rho's tools

rho serves you tools of its own through the MCP server `rho`, which lists them as `{}`, such as `{}`. Each call runs in this rho session under its permission mode: an approval a call needs, or a question a tool asks, goes to the person through rho, and the call waits for the answer.

Work you start with them in the background, such as a `process` task, an `agent` run or a `workflow` run, outlives your turn, and its result comes as a message saying it is rho's background results. ",
        naming.name_of("<name>"),
        naming.name_of("questionnaire"),
    )
}

/// What `ui` does for an agent that runs a daemon session on another
/// runtime: a Rho session's guidance (`tool_sources_context`), without
/// `tool_search`, which the agent does not have, and with how the person's
/// actions reach it: through a call that waits, or, for one taken after the
/// call returned, as a background result. The Express notation follows
/// ([`crate::tools::ui::express_notation`]): the tool's own description
/// leaves it out, since Claude Code cuts an MCP tool's description past
/// 4,096 characters, and nothing else would load it when the agent needs it.
fn ui_tool(naming: McpToolNaming) -> String {
    format!(
        "
# User interface

`{}` shows the person a form, a choice or a result card in rho's transcript. With `wait: true` it returns what they do on it. Use `wait: true` whenever you need their answer now: an action taken after a call returned reaches you later, as a background result naming the surface. Write the call's `express` block in A2UI Express:

",
        naming.name_of("ui"),
    )
}

/// How a child's result reaches an agent that runs a daemon session on
/// another runtime: as a background result, as [`served_tools`] says.
const SESSION_COMPLETIONS: &str = "A child's result comes as one of rho's background results. When you need a child's result before you answer, keep working on what does not depend on it, so the result can arrive during this turn, or check on it with `agents` (action `status`), which hands over the result once the run finished. Do not declare the task complete while required children are still running. Subagents share the workspace, so avoid overlapping edits.";

/// The instructions for an agent whose session `definition` text describes,
/// working in `cwd`, with `home`'s `~/.rho/AGENTS.md`, served `served` of
/// rho's tools, if any, on a runtime that reads `project` and `skills` as
/// they say.
pub(crate) fn agent_session_instructions(
    definition: &str,
    cwd: &Path,
    home: Option<&Path>,
    project: ProjectInstructionFiles,
    served: Option<&ServedToolGuide>,
    skills: SkillFiles,
) -> String {
    let mut text = definition.trim_end().to_owned();
    text.push('\n');
    text.push_str(RENDERING_GUIDANCE);
    if let Some(served) = served {
        text.push_str(&served_tools(served.naming));
        text.push_str(served.idle.clause());
        text.push('\n');
        if served.serves("ui") {
            text.push_str(&ui_tool(served.naming));
            text.push_str(crate::tools::ui::express_notation().trim_end());
            text.push('\n');
        }
        if served.serves("agent") {
            push_delegation_guidance(&mut text, SESSION_COMPLETIONS);
        }
    }
    let paths = match project {
        ProjectInstructionFiles::FromRho => agent_instruction_paths(cwd, &[], home),
        ProjectInstructionFiles::ReadByAgent => {
            home.map(crate::paths::user_agents_md).into_iter().collect()
        }
    };
    let files = read_existing_files(paths);
    if !files.is_empty() {
        text.push_str(AGENTS_MD_HEADER);
        for (path, contents) in &files {
            push_context_file(&mut text, "agents_instructions", path, contents);
        }
    }
    let listed: Vec<Skill> = served
        .filter(|served| served.serves("skill"))
        .map(|served| listed_skills(&served.skills, skills))
        .unwrap_or_default();
    if !listed.is_empty() {
        text.push_str(&skill_listing(&listed));
    }
    text
}

/// Those of `skills` the agent may load that rho lists for a runtime that
/// reads `files` as it says.
fn listed_skills(skills: &[Skill], files: SkillFiles) -> Vec<Skill> {
    skills
        .iter()
        .filter(|skill| !skill.disable_model_invocation)
        .filter(|skill| match (files, &skill.source) {
            (SkillFiles::FromRho, _) => true,
            (SkillFiles::LooseReadByAgent, SkillSource::BuiltIn) => true,
            (SkillFiles::LooseReadByAgent, SkillSource::Filesystem { owner, .. }) => {
                owner.is_some()
            }
        })
        .cloned()
        .collect()
}

#[cfg(test)]
#[path = "agent_session_tests.rs"]
mod tests;