rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What runs a loaded session's turns: Rho's own loop, or an agent on an
//! external runtime over ACP (see [`super::acp_turns`]).
//!
//! The engine sits in the session's turn slot, which turns, settings
//! changes and the daemon's stop take one at a time either way. A client's
//! request that only Rho's loop can serve, such as a model change, the
//! tree, or an MCP reload, reaches it through [`Engine::rho_for`], which
//! names the capability the request needs and refuses it, typed, for a
//! session an agent runs (see `capabilities.rs`).

use std::path::Path;

use rho_protocol::{RpcError, SessionCapability};
use rho_sdk::SessionId;

use super::{acp_turns::AcpTurns, capabilities};
use crate::app::daemon_host::jobs::JobAgent;
use crate::{
    agent::AgentRuntime,
    app::{background_delivery::CountedWork, session_runtime::SessionRuntime},
    session::SessionPersistence,
};

pub(super) enum Engine {
    Rho(Box<SessionRuntime>),
    Acp(Box<AcpTurns>),
}

impl Engine {
    pub(super) fn session_id(&self) -> &SessionId {
        match self {
            Self::Rho(runtime) => runtime.session_id(),
            Self::Acp(turns) => turns.session_id(),
        }
    }

    pub(super) fn workspace_root(&self) -> &Path {
        match self {
            Self::Rho(runtime) => runtime.workspace_root(),
            Self::Acp(turns) => turns.workspace(),
        }
    }

    pub(super) fn persistence(&self) -> SessionPersistence {
        match self {
            Self::Rho(runtime) => runtime.persistence(),
            // An agent's own session outlives each turn's process, so its
            // daemon session is always saved.
            Self::Acp(_) => SessionPersistence::Saved,
        }
    }

    /// Whether the session has a file: always, for one an agent runs.
    pub(super) fn is_saved(&self) -> bool {
        match self {
            Self::Rho(runtime) => runtime.is_saved(),
            Self::Acp(_) => true,
        }
    }

    /// Whether work the session's turns started runs on, or holds a result
    /// or a child's notice no turn took, as `counted` says, which a teardown
    /// would stop or drop. An agent's own process ends with its turn, but
    /// what the tools rho serves it started runs on; a result counts only
    /// where the daemon wakes the session to deliver it (see
    /// `acp_background::has_work`).
    pub(super) fn has_background_work(&self, counted: CountedWork) -> bool {
        match self {
            Self::Rho(runtime) => runtime.has_background_work(counted),
            Self::Acp(turns) => turns.served().is_some_and(|served| {
                super::acp_background::has_work(turns.session_id().as_str(), served, counted)
            }),
        }
    }

    /// The agent the session runs as now, and what runs it, as its job
    /// records them.
    pub(super) fn job_agent(&self) -> JobAgent {
        let id = match self {
            Self::Rho(runtime) => runtime.agent_id().as_str().to_owned(),
            Self::Acp(turns) => turns.agent_id().to_owned(),
        };
        JobAgent {
            id,
            engine: self.runtime().into(),
        }
    }

    /// What runs the session: Rho's loop, or the agent's runtime.
    pub(super) fn runtime(&self) -> AgentRuntime {
        match self {
            Self::Rho(_) => AgentRuntime::Rho,
            Self::Acp(turns) => turns.runtime(),
        }
    }

    /// Rho's loop, to serve a client's request for `capability`, which only
    /// it does; for a session an agent runs, the typed refusal.
    pub(super) fn rho_for(
        &self,
        capability: SessionCapability,
    ) -> Result<&SessionRuntime, RpcError> {
        match self {
            Self::Rho(runtime) => Ok(runtime),
            Self::Acp(turns) => Err(capabilities::rho_only(turns.runtime(), capability)),
        }
    }

    /// [`Self::rho_for`], to change it.
    pub(super) fn rho_for_mut(
        &mut self,
        capability: SessionCapability,
    ) -> Result<&mut SessionRuntime, RpcError> {
        match self {
            Self::Rho(runtime) => Ok(runtime),
            Self::Acp(turns) => Err(capabilities::rho_only(turns.runtime(), capability)),
        }
    }

    /// Rho's loop, when it runs the session, for what the daemon does with
    /// it on its own, such as warming its cache. A client's request goes
    /// through [`Self::rho_for`] instead, which refuses it typed.
    pub(super) fn as_rho(&self) -> Option<&SessionRuntime> {
        match self {
            Self::Rho(runtime) => Some(runtime),
            Self::Acp(_) => None,
        }
    }

    /// [`Self::as_rho`], to change it.
    pub(super) fn as_rho_mut(&mut self) -> Option<&mut SessionRuntime> {
        match self {
            Self::Rho(runtime) => Some(runtime),
            Self::Acp(_) => None,
        }
    }

    /// Tears the engine down: Rho's loop stops its tools; an agent has no
    /// process between turns, but the tools rho served it may have started
    /// work, which stops.
    pub(super) async fn shutdown(self) {
        match self {
            Self::Rho(runtime) => runtime.shutdown().await,
            Self::Acp(turns) => turns.shutdown().await,
        }
    }

    /// Tears down a new session whose open was refused, leaving nothing
    /// saved: Rho's loop saves nothing before its first prompt, but an
    /// agent's session wrote its file as it opened.
    pub(super) async fn discard_new(self) {
        match self {
            Self::Rho(runtime) => runtime.shutdown().await,
            Self::Acp(turns) => turns.discard(),
        }
    }
}