rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What a loaded session's engine can do, as one table: the capabilities
//! clients read in [`wire::SessionEvent::Engine`] and the refusals the
//! daemon answers a request for one with
//! ([`wire::ErrorCode::EngineUnsupported`]) both come from
//! [`unavailable`], so what a session advertises and what it refuses
//! cannot disagree.
//!
//! Rho's own loop does everything but switch runtime, which no session
//! does. An agent of another runtime keeps its own conversation, tools and
//! context, so rho can do none of what needs them (see `acp_turns.rs`); it
//! takes image attachments, unless its adapter said it takes no images,
//! steers once its adapter answered ACP v2, and a model of its own runtime
//! and a permission mode from its next turn on.

use rho_protocol::{self as wire, ErrorCode, RpcError, SessionCapability};

use crate::{
    acp_runtime::{adapters::AdapterFacts, policy::AcpProtocol},
    agent::AgentRuntime,
};

/// What runs a session: its runtime, and for an ACP runtime what its
/// adapter said, once this daemon ran it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct SessionEngine {
    pub(super) runtime: AgentRuntime,
    pub(super) adapter: Option<AdapterFacts>,
}

impl From<AgentRuntime> for SessionEngine {
    fn from(runtime: AgentRuntime) -> Self {
        Self {
            runtime,
            adapter: None,
        }
    }
}

/// Every capability the daemon knows, in the order clients get them.
const ALL: [SessionCapability; 16] = [
    SessionCapability::Rewind,
    SessionCapability::Fork,
    SessionCapability::SideChat,
    SessionCapability::Compact,
    SessionCapability::SwitchModel,
    SessionCapability::ModelSettings,
    SessionCapability::PermissionMode,
    SessionCapability::AddDirectory,
    SessionCapability::AddContext,
    SessionCapability::Delegation,
    SessionCapability::McpReload,
    SessionCapability::Hooks,
    SessionCapability::ContextReports,
    SessionCapability::Attachments,
    SessionCapability::Steer,
    SessionCapability::SwitchRuntime,
];

/// Why a session `engine` runs cannot do `capability`, for a person;
/// `None` when it can. One row per runtime and capability, so a runtime
/// that comes to do one changes its row alone.
pub(super) fn unavailable(engine: SessionEngine, capability: SessionCapability) -> Option<String> {
    use AgentRuntime::{Antigravity, ClaudeCli, Rho};
    use SessionCapability as Can;
    let SessionEngine { runtime, adapter } = engine;
    let agent = runtime.display_name();
    let reason = match (runtime, capability) {
        // No session switches runtime.
        (_, Can::SwitchRuntime) => "a session's runtime is fixed once it opens".to_owned(),
        (Rho, _) => return None,
        (ClaudeCli | Antigravity, Can::Rewind) => {
            format!("{agent} keeps this conversation, so rho can't rewind it or show its tree")
        }
        (ClaudeCli | Antigravity, Can::Fork) => {
            format!("{agent} keeps this conversation, so rho can't fork it from an earlier message")
        }
        (ClaudeCli | Antigravity, Can::SideChat) => {
            format!("{agent} keeps this conversation, so rho can't open a side chat on it")
        }
        (ClaudeCli | Antigravity, Can::Compact) => format!("{agent} compacts its own context"),
        (ClaudeCli | Antigravity, Can::Attachments)
            if adapter.is_some_and(|adapter| !adapter.images) =>
        {
            format!("{agent}'s ACP adapter takes no images: its initialize does not advertise them")
        }
        // SwitchModel and PermissionMode apply from the next turn, whose
        // process starts on them; Attachments travel as ACP image content blocks.
        (ClaudeCli | Antigravity, Can::SwitchModel | Can::PermissionMode | Can::Attachments) => {
            return None
        }
        (ClaudeCli | Antigravity, Can::ModelSettings) => {
            format!("{agent} sets its model's reasoning itself")
        }
        (ClaudeCli | Antigravity, Can::AddDirectory) => {
            format!("{agent} reaches only the directories it opened with")
        }
        (ClaudeCli | Antigravity, Can::AddContext) => {
            format!("{agent} reads only what its prompts say")
        }
        (ClaudeCli | Antigravity, Can::Delegation) => {
            format!("{agent} can't start rho's subagents or sessions")
        }
        (ClaudeCli | Antigravity, Can::McpReload) => format!("{agent} runs its own MCP servers"),
        (ClaudeCli | Antigravity, Can::Hooks) => format!("rho's hooks don't run inside {agent}"),
        (ClaudeCli | Antigravity, Can::ContextReports) => {
            format!("{agent} keeps its own context, which rho can't report on")
        }
        // A steer goes into the turn as another prompt, which ACP v2 takes.
        (ClaudeCli | Antigravity, Can::Steer)
            if adapter.is_some_and(|adapter| adapter.protocol == AcpProtocol::V2) =>
        {
            return None
        }
        (ClaudeCli | Antigravity, Can::Steer) => format!(
            "{agent} takes a message mid-turn only over ACP v2, which its adapter has not been seen to speak"
        ),
        // `Unknown`, which this daemon never asks about.
        (ClaudeCli | Antigravity, _) => format!("{agent} can't do that"),
    };
    Some(reason)
}

/// What a session `engine` runs tells its clients it can do: every
/// capability, each it cannot with why.
pub(super) fn engine_event(engine: SessionEngine) -> wire::SessionEvent {
    wire::SessionEvent::Engine {
        engine: engine.runtime.into(),
        capabilities: ALL
            .into_iter()
            .map(|capability| {
                wire::SessionCapabilityStatus::new(capability, unavailable(engine, capability))
            })
            .collect(),
    }
}

/// `Ok` when a session `engine` runs can do `capability`, else the typed
/// refusal, which names the capability and says why.
pub(super) fn require(
    engine: SessionEngine,
    capability: SessionCapability,
) -> Result<(), RpcError> {
    match unavailable(engine, capability) {
        None => Ok(()),
        Some(reason) => Err(refusal(engine.runtime, capability, reason)),
    }
}

/// The refusal of `capability` for a session `engine` runs, which only
/// Rho's own loop serves: the table's reason, which it must have.
pub(super) fn rho_only(engine: AgentRuntime, capability: SessionCapability) -> RpcError {
    let reason = unavailable(engine.into(), capability);
    debug_assert!(
        reason.is_some(),
        "{capability:?} is advertised for a {} session, yet only rho's loop serves it",
        engine.as_str()
    );
    let reason = reason.unwrap_or_else(|| format!("{} can't do that", engine.display_name()));
    refusal(engine, capability, reason)
}

fn refusal(engine: AgentRuntime, capability: SessionCapability, reason: String) -> RpcError {
    let data = serde_json::to_value(wire::EngineUnsupported::new(capability, engine.into()))
        .expect("an engine refusal serializes");
    RpcError::new(ErrorCode::EngineUnsupported, reason).with_data(data)
}