rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The turns the daemon starts on its own in a session an agent of another
//! runtime runs, where the session's client takes them (`idle_wake: turn`):
//! the delivery of background results that landed while the session sat
//! idle (see `idle_wake`), and the turn that continues one a daemon restart
//! interrupted (see `restart_resume`).
//!
//! Each is one run of the agent, as a person's turn is, and shows clients
//! what a Rho session's daemon turn shows them: no user message for its
//! prompt, which is the daemon's, but the notice the turn names, and, for a
//! delivery, a card for each result as the run delivers it. The run's
//! journal records the notice in the prompt's place (see
//! [`PromptRecord::Hidden`]), so a reopen replays what clients saw live.
//! The session's file saves the turn's prompt as its user message once the
//! run sent it, as a Rho session saves a daemon turn's input: for a
//! delivery, the results as the agent reads them. A run that never sent it
//! saves nothing, and puts the results back for the next turn.
//!
//! A delivery's prompt carries the results as a content block of their own,
//! as a person's prompt carries what waits (see `acp_background`), followed
//! by [`DELIVERY_PROMPT`]: an agent's turn starts from text of its own.
//!
//! The turn that continues an interrupted one sends the restart's note,
//! unless the interrupted turn was a person's whose run never sent its
//! prompt (see `unsent`): the agent never heard of that turn, so it gets the
//! person's prompt instead, which the file and clients have already.

use rho_protocol as wire;
use rho_sdk::{model::Message, CancellationToken};

use super::{
    super::turn::{DaemonTurn, DaemonView, Ran},
    text_of, AcpTurns, PromptSave, ServedTools, TurnPrompt, UnsentPrompt,
};
use crate::{
    app::session_runtime::{Turn, TurnStart},
    run_artifacts::PromptRecord,
    session_environment::Ambient,
};

/// What the agent reads after the results a delivery turn's prompt carries:
/// what to do with them, which no person asked.
const DELIVERY_PROMPT: &str = "(rho: the background results above landed while you were idle. Continue the work they belong to, or end your turn if they need nothing from you.)";

impl AcpTurns {
    /// Runs `turn`, which the daemon started on its own and `view` shows,
    /// as one run of the agent, in `environment`, until it ends or `cancel`
    /// stops it. A delivery that finds no result waiting runs nothing and
    /// ends cancelled: a guard, since the watcher starts one only for a
    /// result that waits, under the turn lock no other turn takes it
    /// through, unless its rendering yields nothing for the prompt.
    pub(in crate::app::daemon_host::sessions) async fn run_daemon_turn(
        &mut self,
        view: &mut DaemonView,
        DaemonTurn { start, notice }: DaemonTurn,
        environment: Ambient,
        cancel: &CancellationToken,
    ) -> Result<Ran, wire::TurnError> {
        // Read before anything is reserved. A daemon turn calls no tools
        // ahead of its prompt.
        let prompt = match &start {
            TurnStart::Prompt(Turn { display, model, .. }) => {
                Some((text_of(display)?, text_of(model)?))
            }
            TurnStart::Continue => None,
        };
        if cancel.is_cancelled() {
            return Ok(Ran::Cancelled);
        }
        let record = PromptRecord::Hidden { notice };
        let background = self.tools.as_mut().map(ServedTools::deliver_into_turn);
        let asked = match prompt {
            // Only the turn that continues an interrupted one has a prompt.
            // What waits rides ahead of it, as ahead of a person's prompt.
            Some((shown, sent)) => match UnsentPrompt::in_folder(&self.artifacts).read() {
                Some(unsent) => TurnPrompt {
                    save: PromptSave::Saved,
                    sent: unsent,
                    record,
                },
                None => TurnPrompt {
                    save: PromptSave::OnceSent(Message::user_text(&shown)),
                    sent,
                    record,
                },
            },
            None => {
                let results = background
                    .as_ref()
                    .and_then(|(_, mailbox)| mailbox.prompt_results())
                    .map(|results| results.framed());
                let Some(results) = results else {
                    if let (Some(tools), Some(background)) = (&mut self.tools, background) {
                        tools.put_back(background);
                    }
                    return Ok(Ran::Cancelled);
                };
                TurnPrompt {
                    save: PromptSave::OnceSent(Message::user_text(&results)),
                    sent: DELIVERY_PROMPT.to_owned(),
                    record,
                }
            }
        };
        self.run_asked(view, asked, background, environment, cancel)
            .await
    }
}