rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Questions answered by an agent rather than a person.
//!
//! `rho/session/answer_question`: the session that started the asking one
//! through session control answers (#1714). An agent's connection may call
//! it, so the daemon checks the answering session against its own job
//! records before it answers (`JobRegistry::may_answer`, the one copy of
//! the rule). The answering session is the one the caller declares, as a
//! new session's parent is: the daemon cannot tell who sends a request.
//!
//! `rho/session/answer_question_by_agent`: an agent outside any session
//! answers, as an MCP client of `rho mcp serve` does. Like a prompt or a
//! steer, it is the caller's to hold to who may drive the session: the
//! daemon checks only the request.
//!
//! Either way the request must be a question anyone but the user may
//! answer (see `inputs`): approvals, plan approvals, and MCP servers'
//! elicitations wait for a person whoever asks.

use rho_protocol::{
    RpcError, SessionAnswerQuestionByAgentParams, SessionAnswerQuestionByAgentResponse,
    SessionAnswerQuestionParams, SessionAnswerQuestionResponse,
};

use super::{
    inputs::{self, AgentAnswerer},
    reopen, Sessions,
};
use crate::app::daemon_host::jobs::AnswerRefused;

impl Sessions {
    /// Hands `by`'s answer to one of a session's open questions, when `by`
    /// started the session and may act on sessions. The first answer wins,
    /// whoever sends it: a later one finds the request gone.
    pub(in crate::app::daemon_host) async fn answer_question(
        &self,
        params: SessionAnswerQuestionParams,
    ) -> Result<SessionAnswerQuestionResponse, RpcError> {
        let SessionAnswerQuestionParams {
            session,
            request,
            answer,
            by,
        } = params;
        let Some(asking) = self.loaded_session(&session).await? else {
            return Err(inputs::not_open(request));
        };
        let answering = reopen::parse_session(&by)?;
        self.jobs
            .may_answer(&asking.job, &answering)
            .map_err(|refused| {
                inputs::invalid(match refused {
                    AnswerRefused::NotStarter => format!(
                        "session {by} did not start session {session}; only the session that started it may answer its questions"
                    ),
                    AnswerRefused::ReadOnly => format!(
                        "session {by} may not act on sessions; answering a question needs write access"
                    ),
                    AnswerRefused::NoGrant => format!(
                        "session {by} runs under no grant the daemon knows, since the session that ran it is gone; answering a question needs write access"
                    ),
                })
            })?;
        asking.change_inputs(|inputs| {
            inputs.answer_question(request, answer, AgentAnswerer::StartingSession)
        })?;
        Ok(SessionAnswerQuestionResponse {})
    }

    /// Hands an outside agent's answer to one of a session's open
    /// questions. The first answer wins, whoever sends it: a later one
    /// finds the request gone.
    pub(in crate::app::daemon_host) async fn answer_question_by_agent(
        &self,
        params: SessionAnswerQuestionByAgentParams,
    ) -> Result<SessionAnswerQuestionByAgentResponse, RpcError> {
        let SessionAnswerQuestionByAgentParams {
            session,
            request,
            answer,
            ..
        } = params;
        let Some(asking) = self.loaded_session(&session).await? else {
            return Err(inputs::not_open(request));
        };
        asking.change_inputs(|inputs| {
            inputs.answer_question(request, answer, AgentAnswerer::Outside)
        })?;
        Ok(SessionAnswerQuestionByAgentResponse::default())
    }
}