rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The definition a saved session reopens as: its agent's current
//! definition, whose tools, model and runtime it runs with, and the
//! instructions the session was created with, which its conversation was
//! had under. The session's header keeps them (`SavedAgent::prompt`); a
//! session saved before it did reads them back from its saved system
//! prompt.

use crate::{
    agent::{AgentDefinition, PromptPolicy},
    prompt::agent_instructions,
    session::SavedAgent,
};

use super::subagent_messaging::CHILD_COMMUNICATION_CONTRACT;

/// Whether a saved agent's instructions are the session's, which its saved
/// system prompt renders.
pub(crate) enum Instructions<'a> {
    /// They are; this reads the session's saved system prompt.
    Rendered(&'a dyn Fn() -> anyhow::Result<Option<String>>),
    /// They are not: a session `/agent:` rebound runs its child's, and its
    /// root only caps the child's tools.
    NotRendered,
}

/// The definition a session saved as `saved` reopens as, `current` being
/// that agent's definition now. Refused when `current` is another agent
/// (#1661). A session whose header kept no instructions runs `current`'s
/// when it was saved under `current`, else those its saved prompt holds,
/// else, with a warning, `current`'s.
pub(crate) fn reopened(
    saved: &SavedAgent,
    current: &AgentDefinition,
    instructions: Instructions<'_>,
) -> anyhow::Result<AgentDefinition> {
    saved.validate(current)?;
    let prompt = match (&saved.prompt, instructions) {
        (Some(prompt), _) => prompt.clone(),
        (None, Instructions::NotRendered) => current.prompt.clone(),
        (None, Instructions::Rendered(_))
            if current.accepts_stored_fingerprint(&saved.fingerprint) =>
        {
            current.prompt.clone()
        }
        (None, Instructions::Rendered(read)) => {
            let system = read()?;
            match system
                .as_deref()
                .and_then(|system| agent_instructions::of(system, &[CHILD_COMMUNICATION_CONTRACT]))
            {
                Some(text) => PromptPolicy::Extend(text.to_owned()),
                None => {
                    tracing::warn!(
                        agent = %saved.id,
                        "a session saved before its agent's instructions were kept, whose prompt holds none, runs the agent's current ones"
                    );
                    current.prompt.clone()
                }
            }
        }
    };
    Ok(AgentDefinition {
        prompt,
        ..current.clone()
    })
}

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