rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;
use rho_protocol as wire;

use super::replay;
use crate::{
    app::daemon_host::jobs::{JobState, TurnEnd},
    run_artifacts::AttachmentEvent,
    subagent::{RunState, RunStatus},
};
use rho_protocol::display::{ToolCard, ToolCardStatus, ToolFamily, ToolHeader};

// Covers: a replayed external run ends as its job did while the daemon
// knows the job, else as its status says, and as interrupted when its
// status never ended, as when the daemon that ran it died mid-run.
// Owner: daemon session observe (engine replay).
#[test]
fn a_replayed_run_ends_as_its_job_or_status_did() {
    let ended = |state: RunState, job: Option<TurnEnd>| {
        let status = RunStatus {
            state,
            ..RunStatus::default()
        };
        replay(&status, &[], wire::PermissionMode::Bypass, job)
            .attach(crate::app::daemon_host::sessions::feed::ResumeFrom::default())
            .job
    };
    let shown = |end: TurnEnd| wire::JobState::from(&JobState::Ended(end));

    assert_eq!(
        [
            ended(RunState::Ok, /*job*/ None),
            ended(RunState::Stopped, /*job*/ None),
            ended(RunState::Running, /*job*/ None),
            ended(RunState::Stopped, Some(TurnEnd::Interrupted)),
        ],
        [
            shown(TurnEnd::Completed),
            shown(TurnEnd::Cancelled),
            shown(TurnEnd::Interrupted),
            shown(TurnEnd::Interrupted),
        ]
    );
}

// Covers: a replayed run whose journal ends mid-call, as when the daemon
// running it died, shows the call interrupted, while one whose journal
// records its end shows the call as that end closed it, with no update
// after.
// Owner: daemon session observe (engine replay).
#[test]
fn a_replay_closes_the_calls_a_cut_off_journal_left_running() {
    let calls = |end: Option<AttachmentEvent>| {
        let status = RunStatus {
            state: RunState::Running,
            ..RunStatus::default()
        };
        let card = ToolCard::new(
            ToolCardStatus::Running,
            ToolFamily::FileCommand,
            ToolHeader::shell("$", Some("cargo test".into())),
        );
        let events: Vec<_> = [
            AttachmentEvent::Prompt("Run the tests.".into()),
            AttachmentEvent::ToolStarted {
                key: Some("call-1".into()),
                card,
            },
        ]
        .into_iter()
        .chain(end)
        .collect();
        replay(
            &status,
            &events,
            wire::PermissionMode::Bypass,
            /*ended*/ None,
        )
        .buffered_for_tests()
        .into_iter()
        .filter_map(|event| match event {
            wire::SessionEvent::ToolCall(update) => update.status,
            _ => None,
        })
        .collect::<Vec<_>>()
    };

    assert_eq!(
        [calls(/*end*/ None), calls(Some(AttachmentEvent::Completed))],
        [
            vec![wire::ToolStatus::InProgress, wire::ToolStatus::Cancelled],
            vec![wire::ToolStatus::InProgress, wire::ToolStatus::Completed],
        ]
    );
}

// Covers: a replayed journal shows what a served call showed besides its
// card as it went out live, but for the terminal of the process a served
// `process` start ran, which ended with the load that ran it: a Rho
// session's replay shows a process's terminal only while it runs.
// Kills: `publish_journal` without `without_terminals` (a client follows a
// terminal whose process is gone).
// Owner: daemon session observe (engine replay).
#[test]
fn a_replayed_journal_shows_no_terminal_of_a_served_process() {
    let status = RunStatus {
        state: RunState::Ok,
        ..RunStatus::default()
    };
    let card = ToolCard::new(
        ToolCardStatus::Running,
        ToolFamily::FileCommand,
        ToolHeader::shell("$", Some("cargo test".into())),
    );
    let started = wire::ToolContent::Text {
        text: "process_id: p1".into(),
    };
    let events = [
        AttachmentEvent::ToolStarted {
            key: Some("call-1".into()),
            card,
        },
        AttachmentEvent::ToolContent {
            key: "call-1".into(),
            content: vec![
                started.clone(),
                wire::ToolContent::Terminal {
                    process: "p1".into(),
                },
            ],
        },
    ];
    let shown: Vec<_> = replay(
        &status,
        &events,
        wire::PermissionMode::Bypass,
        /*ended*/ None,
    )
    .buffered_for_tests()
    .into_iter()
    .filter_map(|event| match event {
        wire::SessionEvent::ToolCall(update) => update.content.clone(),
        _ => None,
    })
    .collect();

    assert_eq!(shown, vec![vec![started]]);
}

// Covers: a replayed journal shows a person's steer as history, a user
// message with no steer id, which marks only a steer that just applied.
// Owner: daemon session observe (engine replay).
#[test]
fn a_replayed_journal_shows_a_steer_as_a_plain_user_message() {
    let content = vec![wire::ContentBlock::Text {
        text: "go left".into(),
    }];
    let events = [AttachmentEvent::Steered {
        steer: wire::SteerId::new("acp-steer-1"),
        content: content.clone(),
    }];
    let shown: Vec<_> = replay(
        &RunStatus::default(),
        &events,
        wire::PermissionMode::Bypass,
        /*ended*/ None,
    )
    .buffered_for_tests()
    .into_iter()
    .filter_map(|event| match event {
        wire::SessionEvent::UserMessage { content, steer, .. } => Some((content.clone(), steer)),
        _ => None,
    })
    .collect();

    assert_eq!(shown, vec![(content, None)]);
}