rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The instructions rho gives the agent that runs a daemon session on
//! another runtime, each turn: its session definition's text, how to write
//! for rho's clients, how to use the tools of rho's the turn serves it, and
//! the AGENTS.md files and skills it does not find itself
//! ([`crate::prompt::agent_session`]).
//!
//! A runtime that takes a resumed session's system prompt anew (agy) gets
//! them as each run's system prompt. One that keeps the system prompt the
//! session started with (Claude Code) gets them as its first run's, and the
//! session's folder keeps what it got ([`INSTRUCTIONS_FILE`]): a later turn
//! whose instructions differ, because an AGENTS.md file or rho changed,
//! sends them ahead of its prompt, which clients show as the person wrote
//! it.

use std::path::Path;

use crate::{
    app::agent_binding::{ExternalLaunch, ResumedInstructions, SessionInstructions},
    prompt::agent_session::ServedToolGuide,
};

/// The instructions the agent's session last got, kept in the session's
/// folder for a runtime that keeps the first ones.
const INSTRUCTIONS_FILE: &str = "instructions.md";

/// Gives `launch`, whose overrides are set, the session's instructions, for
/// the session whose folder is `artifacts`, with how to use `served`, the
/// tools of rho's the launch serves, if any. Returns what to keep once the
/// turn shows the agent got them.
#[must_use = "the instructions are kept only once the turn completes"]
pub(super) fn instruct(
    launch: &mut ExternalLaunch,
    artifacts: &Path,
    served: Option<&ServedToolGuide>,
) -> Option<Unkept> {
    let home = crate::paths::home_dir();
    let SessionInstructions {
        text: instructions,
        resumed,
    } = launch.give_session_instructions(home.as_deref(), served);
    let path = artifacts.join(INSTRUCTIONS_FILE);
    match (resumed, launch.resumes()) {
        (ResumedInstructions::Refreshed, _) => return None,
        (ResumedInstructions::KeptFromStart, false) => {}
        (ResumedInstructions::KeptFromStart, true) => {
            // A session that predates the file never got them.
            if std::fs::read_to_string(&path).is_ok_and(|kept| kept == instructions) {
                return None;
            }
            launch.overrides_mut().preamble = Some(updated(&instructions));
        }
    }
    Some(Unkept { path, instructions })
}

/// Instructions a run sent that the session's folder does not keep yet.
pub(super) struct Unkept {
    path: std::path::PathBuf,
    instructions: String,
}

impl Unkept {
    /// Keeps them, once the turn completed: a turn that failed sends them
    /// again next time, which costs only tokens.
    pub(super) fn keep(self) {
        let Self { path, instructions } = self;
        if let Err(error) =
            crate::config_writer::write_bytes_atomically(&path, instructions.as_bytes())
        {
            tracing::warn!(
                path = %path.display(),
                %error,
                "could not keep the instructions the agent's session got"
            );
        }
    }
}

/// Tells the agent `instructions` replace the ones its session started
/// with, ahead of the turn's prompt, which clients show without it.
fn updated(instructions: &str) -> String {
    format!(
        "rho updated your instructions for this session. They replace the ones it started with:\n\n<rho_instructions>\n{}\n</rho_instructions>",
        instructions.trim_end()
    )
}