rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Who decides the agent's tool calls, for both protocols: the tools' owner,
//! as each call runs, for a tool the run serves; the policy for the rest.
//!
//! A permission ask about a served tool is allowed once, and the policy
//! decides any other ([`Deciders::decide`], put to a person by
//! [`super::asks::permission`] when the policy says so). Each update the
//! agent sends is shown through [`Deciders::show`], in v1 form: the policy
//! reads its status off it, the run's tools hear it, and a call the run
//! rejected stays failed.

use super::{
    agent_tools::Served,
    driver::render,
    permission::PermissionDecision,
    policy::{AcpAgentPolicy, AgentToolCallRef},
};
use crate::cli_runtime::{
    agent_event::{render::EventRenderer, AgentEvent},
    status_sink::StatusSink,
};
use agent_client_protocol::v1::schema::{
    RequestPermissionRequest, SessionUpdate, ToolCallContent, ToolCallId, ToolCallStatus,
};
use std::{collections::HashSet, sync::Mutex};

pub(super) struct Deciders<'p, P> {
    policy: &'p Mutex<P>,
    served: Option<&'p Served>,
}

impl<'p, P: AcpAgentPolicy> Deciders<'p, P> {
    pub(super) fn new(policy: &'p Mutex<P>, served: Option<&'p Served>) -> Self {
        Self { policy, served }
    }

    pub(super) fn policy(&self) -> &'p Mutex<P> {
        self.policy
    }

    /// The tool the run serves that the agent's tool call `call` runs, if
    /// it runs one.
    fn served_tool<'c>(&self, call: AgentToolCallRef<'c>) -> Option<&'c str> {
        // The tools are asked outside the policy's lock.
        let named = self.policy.lock().expect("ACP policy").agent_tool(call);
        named.filter(|name| self.served.is_some_and(|served| served.serves(name)))
    }

    pub(super) fn decide(&self, request: &RequestPermissionRequest) -> PermissionDecision {
        if self.served_tool((&request.tool_call).into()).is_some() {
            return PermissionDecision::AllowOnce;
        }
        self.policy
            .lock()
            .expect("ACP policy")
            .decide_permission(request)
    }

    /// Renders `update` with the status the policy reads off it, such as a
    /// rate limit in a usage update's `_meta`. The run's tools hear it
    /// first (see [`Served::heard`]); a call of one of them draws its own
    /// card, so the agent's is held back. A call in `rejected` stays failed.
    pub(super) fn show(
        &self,
        mut update: SessionUpdate,
        rejected: &HashSet<ToolCallId>,
        renderer: &mut EventRenderer,
        sink: &mut StatusSink,
    ) {
        let status = self
            .policy
            .lock()
            .expect("ACP policy")
            .status_effects(&update);
        for effect in status {
            sink.apply_effect(effect);
        }
        mark_rejected(&mut update, rejected);
        let tool = match &update {
            SessionUpdate::ToolCall(call) => self.served_tool(call.into()),
            _ => None,
        };
        if let Some(served) = self.served {
            served.heard(&update, tool);
        }
        let event = match update {
            SessionUpdate::ToolCall(call) if tool.is_some() => {
                AgentEvent::ServedToolCall(Box::new(call))
            }
            update => AgentEvent::from(update),
        };
        render(renderer, sink, event);
    }
}

/// A tool call the run rejected stays failed: a later snapshot that reports
/// it completed must not turn its card successful.
pub(super) fn mark_rejected(update: &mut SessionUpdate, rejected: &HashSet<ToolCallId>) {
    if let SessionUpdate::ToolCallUpdate(call) = update {
        if call.fields.status == Some(ToolCallStatus::Completed)
            && rejected.contains(&call.tool_call_id)
        {
            call.fields.status = Some(ToolCallStatus::Failed);
            call.fields.content = Some(vec![ToolCallContent::from(
                "rejected by Rho permission policy",
            )]);
        }
    }
}