rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;
use rho_sdk::model::{Message, ModelIdentity};

use std::path::PathBuf;

use super::{PromptRow, RunArtifacts, SessionEngine};
use crate::session::{agent_identity, Session, SessionOrigin, SessionProvenance, SessionTarget};
use crate::session::{persistence::active_display_from_tree, tree::SessionTree};

// Covers: an external run's session file names its engine in its header,
// and holds the run's prompt from its start, and its final reply once it
// ended, as a Rho conversation, which every reader of session files,
// listings, search and `sessions read`, reads as it reads any session's.
// Owner: session persistence (engine).
#[test]
fn an_engine_sessions_file_keeps_its_engine_and_its_one_turn() {
    let sessions = tempfile::tempdir().unwrap();
    let workspace = tempfile::tempdir().unwrap();
    let engine = SessionEngine::Acp {
        artifacts: RunArtifacts::try_from(PathBuf::from("subagents/abc123")).unwrap(),
        permission_mode: rho_protocol::PermissionMode::Bypass,
    };
    let provenance = SessionProvenance {
        origin: SessionOrigin::Subagent,
        parent: Some(SessionTarget::new("parent", workspace.path())),
        started_by: None,
    };
    let id = rho_sdk::SessionId::new().to_string();
    let session = Session::create_on_engine_in_root(
        sessions.path(),
        workspace.path(),
        &id,
        ("worker", "fingerprint"),
        provenance,
        &engine,
    )
    .unwrap();
    let model = || ModelIdentity::new("antigravity", "antigravity", "default");
    session
        .save_engine_prompt("Run the tests.", model())
        .unwrap();
    let (_, started) = Session::open_by_id_in_root(sessions.path(), workspace.path(), &id).unwrap();
    session
        .save_engine_reply(
            "Run the tests.",
            "All 12 passed.",
            model(),
            PromptRow::Saved,
        )
        .unwrap();
    let path = session.path().to_path_buf();
    drop(session);

    let (_, ended) = Session::open_by_id_in_root(sessions.path(), workspace.path(), &id).unwrap();
    assert_eq!(
        (agent_identity::read_engine(&path).unwrap(), started, ended),
        (
            Some(engine),
            vec![Message::user_text("Run the tests.")],
            vec![
                Message::user_text("Run the tests."),
                Message::assistant_text("All 12 passed."),
            ]
        )
    );
}

// Covers: an external run's session shows its prompt above its reply
// whether or not the prompt's own save succeeded as the run started: a reply
// saved after a failed prompt save adds the prompt's row, and one saved
// after a good one does not repeat it.
// Owner: session persistence (engine).
#[test]
fn an_engine_sessions_reply_shows_under_its_prompt() {
    let shown = |prompt_row: PromptRow| {
        let sessions = tempfile::tempdir().unwrap();
        let workspace = tempfile::tempdir().unwrap();
        let engine = SessionEngine::Acp {
            artifacts: RunArtifacts::try_from(PathBuf::from("subagents/abc123")).unwrap(),
            permission_mode: rho_protocol::PermissionMode::Bypass,
        };
        let session = Session::create_on_engine_in_root(
            sessions.path(),
            workspace.path(),
            &rho_sdk::SessionId::new().to_string(),
            ("worker", "fingerprint"),
            SessionProvenance {
                origin: SessionOrigin::Subagent,
                parent: Some(SessionTarget::new("parent", workspace.path())),
                started_by: None,
            },
            &engine,
        )
        .unwrap();
        let model = || ModelIdentity::new("antigravity", "antigravity", "default");
        if prompt_row == PromptRow::Saved {
            session
                .save_engine_prompt("Run the tests.", model())
                .unwrap();
        }
        session
            .save_engine_reply("Run the tests.", "All 12 passed.", model(), prompt_row)
            .unwrap();
        active_display_from_tree(&SessionTree::load(session.path()).unwrap())
            .unwrap()
            .into_iter()
            .map(|row| row.message)
            .collect::<Vec<_>>()
    };
    let both = vec![
        Message::user_text("Run the tests."),
        Message::assistant_text("All 12 passed."),
    ];

    assert_eq!(
        [shown(PromptRow::Saved), shown(PromptRow::Unsaved)],
        [both.clone(), both]
    );
}

// Covers: an engine a later Rho records reads as unknown, so the header,
// and with it the session, still reads.
// Owner: session persistence (engine).
#[test]
fn a_later_engine_reads_as_unknown() {
    assert_eq!(
        serde_json::from_str::<SessionEngine>(r#"{"kind":"wasm","module":"x"}"#).unwrap(),
        SessionEngine::Unknown
    );
}

// Covers: a header's run artifacts name a directory under the rho home
// alone, so a session file cannot have the daemon replay files elsewhere.
// Owner: session persistence (engine).
#[test]
fn run_artifacts_stay_under_the_rho_home() {
    let read = |path: &str| {
        serde_json::from_value::<RunArtifacts>(serde_json::json!(path))
            .map(|artifacts| artifacts.directory(std::path::Path::new("/home/rho")))
            .is_ok()
    };
    assert_eq!(
        [
            read("sessions/ws/s1/subagents/abc123"),
            read("workflows/runs/r1/nodes/n/attempts/1/agent"),
            read("../elsewhere"),
            read("/etc"),
            read("subagents/../../etc"),
            read(""),
        ],
        [true, true, false, false, false, false]
    );
}