rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Fail-closed selection of one-shot ACP permission options.

use agent_client_protocol::v1::schema::{
    PermissionOption, PermissionOptionKind, RequestPermissionOutcome, SelectedPermissionOutcome,
};

use crate::permission::PermissionMode;

/// What an agent policy decided for one `session/request_permission`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum PermissionDecision {
    /// Select the offered `allow_once` option; never `allow_always` (it persists in the agent's config).
    AllowOnce,
    /// Select `reject_once`, else answer `cancelled`; never `reject_always`.
    Reject,
    /// Put the request to the person who attends the run, as `Asking`
    /// says, and answer as they do; reject it when nobody attends.
    AskPerson(Asking),
}

/// What an ask put to a person is about.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Asking {
    /// A tool call.
    Tool,
    /// Leaving plan mode: the agent's `allow_once` approves its plan and
    /// moves it to `to`, which [`super::AcpAgentPolicy::leave_plan`] then
    /// fences.
    PlanExit { to: PermissionMode },
}

pub(crate) fn choose_option(
    decision: PermissionDecision,
    options: &[PermissionOption],
) -> RequestPermissionOutcome {
    let allowed = match decision {
        PermissionDecision::AllowOnce => options
            .iter()
            .find(|option| option.kind == PermissionOptionKind::AllowOnce),
        // Only a person's answer settles an ask; unsettled, it fails closed.
        PermissionDecision::Reject | PermissionDecision::AskPerson(_) => None,
    };
    allowed
        .or_else(|| {
            options
                .iter()
                .find(|option| option.kind == PermissionOptionKind::RejectOnce)
        })
        .map_or(RequestPermissionOutcome::Cancelled, |option| {
            RequestPermissionOutcome::Selected(SelectedPermissionOutcome::new(
                option.option_id.clone(),
            ))
        })
}

/// Whether selection actually approved execution, including the missing-allow
/// fallback. Ids themselves carry no policy meaning.
pub(super) fn is_allowed(outcome: &RequestPermissionOutcome, options: &[PermissionOption]) -> bool {
    match outcome {
        RequestPermissionOutcome::Selected(selected) => options.iter().any(|option| {
            option.option_id == selected.option_id && option.kind == PermissionOptionKind::AllowOnce
        }),
        RequestPermissionOutcome::Cancelled => false,
        _ => false,
    }
}

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