rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What a person chose for the sessions a session an agent runs starts: a
//! copy of the choice that applied to the session as it opened, as a
//! session of rho's keeps one (see `session_runtime/started_sessions.rs`),
//! so the sessions the agent starts through the `sessions` tool rho serves
//! it open with it in turn. No change sets it afterwards: such a session
//! takes no started-session change.
//!
//! The session's folder keeps it in [`STARTED_SESSIONS_FILE`], as the
//! record a session of rho's keeps in its snapshot metadata. A file that
//! does not read refuses every session started under it for as long as the
//! session lasts: unlike a session of rho's, it takes no change that would
//! replace it.

use std::path::Path;

use crate::{
    app::session_runtime::{StartedSessionDefaults, StartedSessions},
    session::{Session as StoredSession, SessionEngine},
    subagent,
};

/// The file in a session's folder that keeps its [`StartedSessions`].
const STARTED_SESSIONS_FILE: &str = "started_sessions.json";

/// What the session saved under `id` in workspace `cwd` chose for the
/// sessions it starts, read without opening it: from its folder for a
/// session an agent runs, else from its file's metadata, as a session of
/// rho's keeps it. An engine this rho does not know refuses every session
/// started under it, as it may have chosen a stricter mode. Blocking.
pub(in crate::app::daemon_host::sessions) fn saved_by_id(
    cwd: &Path,
    id: &str,
) -> anyhow::Result<StartedSessions> {
    match StoredSession::engine_by_id(cwd, id)? {
        Some(SessionEngine::AcpSession { artifacts, .. }) => {
            Ok(read(&artifacts.directory(&crate::paths::rho_dir()?)))
        }
        None | Some(SessionEngine::Acp { .. }) => StartedSessions::saved_by_id(cwd, id),
        Some(SessionEngine::Unknown) => Ok(StartedSessions::Unreadable(None)),
    }
}

/// What the session folder `artifacts` keeps: nothing chosen without the
/// file, and [`StartedSessions::Unreadable`] for one that does not read.
pub(in crate::app::daemon_host::sessions) fn read(artifacts: &Path) -> StartedSessions {
    let path = artifacts.join(STARTED_SESSIONS_FILE);
    match std::fs::read_to_string(&path) {
        Ok(record) => StartedSessions::read(Some(record)),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => StartedSessions::default(),
        Err(error) => {
            tracing::warn!(
                path = %path.display(),
                %error,
                "could not read what the sessions this one starts open with; none will start under it"
            );
            StartedSessions::Unreadable(None)
        }
    }
}

/// Saves `chosen` in the session folder `artifacts`; nothing chosen
/// writes nothing.
pub(super) fn write(artifacts: &Path, chosen: &StartedSessionDefaults) -> anyhow::Result<()> {
    if *chosen == StartedSessionDefaults::default() {
        return Ok(());
    }
    subagent::ensure_private_directory(artifacts)?;
    let path = artifacts.join(STARTED_SESSIONS_FILE);
    crate::config_writer::write_bytes_atomically(&path, &serde_json::to_vec(chosen)?)
        .map_err(|error| anyhow::anyhow!("could not save {}: {error}", path.display()))
}

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