rho-coding-agent 3.6.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The `sessions` tool's control actions (#1714): `list`, `new`, `prompt`,
//! `steer`, `cancel`, `wait`, and `read` of a session the daemon hosts, and
//! `answer` to a question a session this one started asks. A thin adapter
//! over the session-control core (`app::session_control`), which `rho mcp
//! serve` adapts too, so both answer the same call the same way under one
//! policy; only this tool offers `answer`, since only it knows the calling
//! session.
//!
//! The caller is this session's agent: its own holder, at the depth and
//! within the scope its environment carries (`crate::caller_context`), and
//! a session it starts records this one as its parent. The core checks a
//! call before anyone is asked to approve it; acting calls then ask for the
//! session-control capability naming the action, the session, its workspace,
//! and the text sent. Watching calls ask for the read of the session store
//! that searching history asks for, since they show other sessions'
//! transcripts. The core reaches only a daemon already running: a tool call
//! never starts one.

use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
    sync::OnceLock,
};

use rho_protocol::SessionId;
use rho_sdk::{
    tool::{
        OperationKind, PreparedToolInvocation, ToolError, ToolErrorKind, ToolMetadata, ToolOutput,
    },
    CapabilityRequest, CapabilitySource, PathScope, SessionControlAction, SessionControlTarget,
};
use serde_json::{json, Map, Value};

use crate::{
    app::{
        agent_concurrency::AgentConcurrency,
        peer_sessions::PeerInbox,
        session_control::{
            Action, Caller, ControlError, Limits, MaxSessions, Reach, SessionControl, Target,
        },
    },
    caller_context::{Access, CallerContext},
    session_environment::SessionEnvironment,
};

/// A call of a core action: the action and its arguments, `action` removed.
pub(super) struct Call {
    action: Action,
    arguments: Map<String, Value>,
}

impl Call {
    /// The core call `arguments` make, if their action is the core's: every
    /// action but `search` and `recall`, and `read` without an `anchor`,
    /// which reads saved history instead.
    pub(super) fn parse(arguments: &Value) -> Option<Self> {
        let mut arguments = arguments.as_object()?.clone();
        let action = Action::from_name(arguments.get("action")?.as_str()?)?;
        if action == Action::Read && arguments.contains_key("anchor") {
            return None;
        }
        arguments.remove("action");
        Some(Self { action, arguments })
    }

    /// What the call shows before it runs, from its arguments alone.
    pub(super) fn start_metadata(&self) -> ToolMetadata {
        let named = |key| self.arguments.get(key).and_then(Value::as_str);
        metadata(
            self.action,
            named("session"),
            named("workspace").map(Path::new),
        )
    }
}

/// The operation acting calls report, beside reads.
const CONTROL_OPERATION: &str = "session control";

/// The core every session's tool in this process calls through, as one
/// `rho mcp serve` process has one: its caps count each holder, here each
/// calling session, across calls. The daemon, where sessions run, and an
/// in-process rho each run one async runtime, which the core's daemon
/// connection lives on.
static CORE: OnceLock<SessionControl> = OnceLock::new();

/// Makes the core cap each session's running sessions at the daemon's
/// shared agent budget, which its settings resize as each config is
/// published, so a change to `behavior.agent_concurrency` applies to the
/// next `new` or `prompt`. The daemon calls it as it starts, before any
/// session's tool can make the core.
pub(crate) fn share_agent_budget(budget: AgentConcurrency) {
    // One daemon per process; a second one (in tests) finds the core made.
    let _ = CORE.set(capped_at(MaxSessions::AgentConcurrency(budget)));
}

/// The core, which a process other than the daemon makes on first use,
/// capped at the calling session's `agent_concurrency`.
fn core(agent_concurrency: usize) -> &'static SessionControl {
    CORE.get_or_init(|| {
        let limit = u32::try_from(agent_concurrency).unwrap_or(u32::MAX);
        capped_at(MaxSessions::Fixed(limit))
    })
}

fn capped_at(max_sessions: MaxSessions) -> SessionControl {
    let limits = Limits {
        max_sessions,
        ..Limits::default()
    };
    SessionControl::new(limits, Reach::Running)
}

/// Checks `call` from `session`, whose children see `environment`, and
/// prepares it. Acting calls ask for approval of what the check found;
/// watching calls read other sessions' transcripts, so they ask for the
/// read of the session store `root` that searching history asks for. A
/// session with an inbox, `peers`, is told of the turns its `new` and
/// `prompt` start (see `app::peer_sessions`).
pub(super) async fn prepare<'a>(
    call: Call,
    session: &str,
    agent_concurrency: usize,
    peers: Option<PeerInbox>,
    environment: &SessionEnvironment,
    root: &Result<PathBuf, String>,
) -> Result<PreparedToolInvocation<'a>, ToolError> {
    let ambient = environment.current();
    let context = CallerContext::from_env(|name| ambient.read_os(name))
        .map_err(|error| ToolError::new(ToolErrorKind::Execution, format!("{error:#}")))?;
    let told = peers.is_some() && matches!(call.action, Action::New | Action::Prompt);
    let caller = Caller {
        told: peers,
        ..Caller::session(SessionId::new(session), &context, ambient.to_client())
    };
    let Call { action, arguments } = call;
    let core = core(agent_concurrency);
    let checked = core
        .check(&caller, action, arguments)
        .await
        .map_err(tool_error)?;
    let source = CapabilitySource::built_in_tool(crate::session::search::TOOL_NAME);
    let (capability, metadata) = match checked.target() {
        None => {
            let root = root
                .clone()
                .map_err(|message| ToolError::new(ToolErrorKind::Execution, message))?;
            (
                CapabilityRequest::read_path(root, PathScope::UnrestrictedFilesystem, source),
                metadata(action, None, None),
            )
        }
        Some(target) => (
            CapabilityRequest::session_control(capability_target(target)?, source),
            metadata(
                action,
                target.session.as_ref().map(SessionId::as_str),
                Some(&target.workspace),
            ),
        ),
    };
    Ok(PreparedToolInvocation::resource_aware(
        [],
        [capability],
        metadata,
        move |context| {
            Box::pin(async move {
                let run = core.run(&caller, checked);
                let value = match action.needs() {
                    // A wait or read stops with the tool. An action runs to
                    // its end, so a session it opened is never left behind
                    // unreported, or uncounted against the caller's cap.
                    Access::Read => tokio::select! {
                        ran = run => ran,
                        () = context.cancellation().cancelled() => {
                            return Err(ToolError::new(ToolErrorKind::Cancelled, "cancelled"));
                        }
                    },
                    Access::Write => run.await,
                }
                .map_err(tool_error)?;
                let mut text = serde_json::to_string_pretty(&value)
                    .map_err(|error| ToolError::new(ToolErrorKind::Execution, error.to_string()))?;
                if told {
                    text.push_str("\nrho tells you when this turn ends or asks a question.");
                }
                Ok(ToolOutput::text(text))
            })
        },
    ))
}

/// Adds the core's actions to the history `schema`, with each action's
/// arguments as the core's own schema has them, so the tool and `rho mcp
/// serve` take the same arguments. An argument several actions take names
/// each action's use in its description, as [`describe`] words it.
pub(super) fn extend_schema(schema: &mut Value) {
    // Each argument's descriptions, by the actions that give them, in
    // action order.
    let mut uses: BTreeMap<String, Vec<(&'static str, String)>> = BTreeMap::new();
    for action in Action::ALL {
        let mut own = Value::Object(action.input_schema());
        let defs = own.get("$defs").cloned().unwrap_or(Value::Null);
        inline_refs(&mut own, &defs);
        let name = action.name();
        let names = &mut schema["properties"]["action"]["enum"];
        if let Some(names) = names
            .as_array_mut()
            .filter(|names| !names.contains(&json!(name)))
        {
            names.push(json!(name));
        }
        let Some(arguments) = own.get_mut("properties").and_then(Value::as_object_mut) else {
            continue;
        };
        for (key, mut argument) in std::mem::take(arguments) {
            // Taken in place, so the merged description keeps its slot.
            if let Some(Value::String(said)) = argument.get_mut("description") {
                let said = std::mem::take(said);
                uses.entry(key.clone()).or_default().push((name, said));
            }
            let properties = &mut schema["properties"];
            if properties.get(&key).is_none() {
                properties[&key] = argument;
            }
        }
    }
    for (key, pairs) in uses {
        let known = &mut schema["properties"][&key];
        known["description"] = json!(describe(known["description"].as_str(), &pairs));
    }
}

/// An argument's merged description: `before`, the history's own, then one
/// `For a, b: text` clause per distinct text in `uses` (action, text) pairs,
/// naming every action that gives that text, in first-use order.
fn describe(before: Option<&str>, uses: &[(&str, String)]) -> String {
    let mut clauses: Vec<(Vec<&str>, &str)> = Vec::new();
    for (action, text) in uses {
        match clauses.iter_mut().find(|(_, said)| said == text) {
            Some((actions, _)) => actions.push(action),
            None => clauses.push((vec![action], text)),
        }
    }
    clauses
        .into_iter()
        .map(|(actions, text)| format!("For {}: {text}", actions.join(", ")))
        .fold(
            before.unwrap_or_default().to_owned(),
            |so_far, clause| match so_far.as_str() {
                "" => clause,
                so_far if so_far.ends_with('.') => format!("{so_far} {clause}"),
                so_far => format!("{so_far}. {clause}"),
            },
        )
}

/// Replaces each `{"$ref": "#/$defs/Name"}` in `value` with `Name` from
/// `defs`, so an argument's schema stands alone in the merged schema.
fn inline_refs(value: &mut Value, defs: &Value) {
    if let Some(name) = value
        .get("$ref")
        .and_then(Value::as_str)
        .and_then(|reference| reference.strip_prefix("#/$defs/"))
    {
        let mut resolved = defs[name].clone();
        inline_refs(&mut resolved, defs);
        if let (Some(value), Value::Object(resolved)) = (value.as_object_mut(), resolved) {
            value.remove("$ref");
            for (key, field) in resolved {
                value.entry(key).or_insert(field);
            }
        }
        return;
    }
    match value {
        Value::Object(fields) => {
            fields.remove("$defs");
            fields
                .values_mut()
                .for_each(|field| inline_refs(field, defs));
        }
        Value::Array(items) => items.iter_mut().for_each(|item| inline_refs(item, defs)),
        _ => {}
    }
}

/// The capability target an approval names for `target`.
fn capability_target(target: &Target) -> Result<SessionControlTarget, ToolError> {
    let Some(session) = &target.session else {
        return Ok(SessionControlTarget::start(
            &target.workspace,
            target.text.clone().unwrap_or_default(),
        ));
    };
    let action = match target.action {
        Action::Prompt => SessionControlAction::Prompt,
        Action::Steer => SessionControlAction::Steer,
        Action::Cancel => SessionControlAction::Cancel,
        Action::Answer => SessionControlAction::Answer,
        Action::New | Action::List | Action::Read | Action::Wait => {
            unreachable!("a target with a session is a prompt, steer, cancel, or answer")
        }
    };
    let session = rho_sdk::SessionId::from_string(session.as_str())
        .map_err(|_| ToolError::new(ToolErrorKind::InvalidArguments, "the session id is empty"))?;
    let on = SessionControlTarget::on(action, session, &target.workspace);
    Ok(match &target.text {
        Some(text) => on.with_text(text),
        None => on,
    })
}

/// What a call of `action` shows: a read for one that watches; for one
/// that acts, the session it acts on and that session's workspace, when
/// known.
fn metadata(action: Action, session: Option<&str>, workspace: Option<&Path>) -> ToolMetadata {
    if action.needs() == Access::Read {
        return ToolMetadata::new().operation(OperationKind::Read);
    }
    let mut metadata = ToolMetadata::new()
        .operation(OperationKind::Other(CONTROL_OPERATION.into()))
        .command_summary(match session {
            Some(session) => format!("{} session {session}", action.name()),
            None => format!("{} session", action.name()),
        });
    if let Some(workspace) = workspace {
        metadata = metadata.affected_path(workspace);
    }
    metadata
}

/// The tool's failure for `error`, whose text is what `rho mcp serve`
/// returns for it too.
fn tool_error(error: ControlError) -> ToolError {
    let kind = match error {
        ControlError::Invalid(_) => ToolErrorKind::InvalidArguments,
        ControlError::Refused(_)
        | ControlError::UnknownSession(_)
        | ControlError::NotSteerable(_)
        | ControlError::Daemon(_) => ToolErrorKind::Execution,
    };
    ToolError::new(kind, error.to_string())
}

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