rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use super::*;
use pretty_assertions::assert_eq;

fn state(running: bool, listed: &[&'static str]) -> v2::StateUpdate {
    work_state(
        running,
        &listed
            .iter()
            .map(|id| Work::Command(id))
            .collect::<Vec<_>>(),
    )
}

/// A piece of listed work, by kind.
#[derive(Clone, Copy)]
enum Work {
    Command(&'static str),
    /// A tool call listed as work, such as a Monitor.
    ToolCall(&'static str),
    Subagent(&'static str),
}

fn work_state(running: bool, listed: &[Work]) -> v2::StateUpdate {
    let background = listed
        .iter()
        .map(|work| match *work {
            Work::Command(id) => v2::BackgroundWork::Terminal(v2::BackgroundTerminal::new(id)),
            Work::ToolCall(id) => v2::BackgroundWork::ToolCall(v2::BackgroundToolCall::new(id)),
            Work::Subagent(id) => v2::BackgroundWork::Subagent(v2::BackgroundSubagent::new(id)),
        })
        .collect::<Vec<_>>();
    if running {
        v2::StateUpdate::Running(v2::RunningStateUpdate::new().background(background))
    } else {
        v2::StateUpdate::Idle(v2::IdleStateUpdate::new().background(background))
    }
}

/// What the run hears of the root session.
enum Heard {
    State(v2::StateUpdate),
    /// A tool call at a status.
    Call(&'static str, v2::ToolCallStatus),
}

// Covers: after the prompted turn, the run keeps the agent while work is
// listed, up to the budget from the turn's end; once an item ended, it waits
// for the turn that reports it, which settles it; a call the running turn
// opened that finishes after a command ended settles that command, while
// other ended work stays owed and a call first heard finished, or one that
// finishes between turns, is no step; and a run
// with no background work ends at its idle. Sequences as claude-agent-acp
// 0.114.0 sent them.
// Owner: v2 background-work keeping (pure).
#[test]
fn the_run_keeps_the_agent_while_background_work_may_report() {
    use v2::ToolCallStatus::{Completed, Pending};
    use Work::{Command, Subagent, ToolCall};
    let start = Instant::now();
    let budget = start + BACKGROUND_BUDGET;
    let follow_up = start + FOLLOW_UP_WAIT;
    let s = |running, listed: &[Work]| Heard::State(work_state(running, listed));
    let opened = |id| Heard::Call(id, Pending);
    let finished = |id| Heard::Call(id, Completed);
    // (case, what the run heard before the idle it decides at, its decision)
    let cases = [
        ("no background work", vec![s(true, &[])], None),
        (
            "a command still runs",
            vec![s(true, &[Command("t1")]), s(false, &[Command("t1")])],
            Some(budget),
        ),
        (
            "the command ended: its report is owed",
            vec![s(true, &[Command("t1")]), s(false, &[])],
            Some(follow_up),
        ),
        (
            "a command ended in the turn's last step: its report is owed too",
            vec![s(true, &[Command("t1")]), s(true, &[]), s(false, &[])],
            Some(follow_up),
        ),
        (
            "a call that finished after the command ended took it in",
            vec![
                s(true, &[Command("t1")]),
                opened("t2"),
                s(true, &[]),
                finished("t2"),
                s(false, &[]),
            ],
            None,
        ),
        (
            "a call first heard finished opened no step",
            vec![
                s(true, &[Command("t1")]),
                s(true, &[]),
                finished("t2"),
                s(false, &[]),
            ],
            Some(follow_up),
        ),
        (
            "ended work but a command stays owed past a call",
            vec![
                s(true, &[Subagent("a1"), ToolCall("m1")]),
                opened("t2"),
                s(true, &[]),
                finished("t2"),
                s(false, &[]),
            ],
            Some(follow_up),
        ),
        (
            "a call that finishes between turns is no step",
            vec![
                s(true, &[Command("t1")]),
                opened("t2"),
                s(false, &[Command("t1")]),
                s(false, &[]),
                finished("t2"),
            ],
            Some(follow_up),
        ),
        (
            "the turn that reports it settled it",
            vec![
                s(true, &[Command("t1")]),
                s(false, &[]),
                s(true, &[]),
                s(false, &[]),
            ],
            None,
        ),
    ];
    for (case, heard, expected) in cases {
        let mut background = Background::default();
        for heard in heard {
            match heard {
                Heard::State(state) => background.observe(&state),
                Heard::Call(id, status) => {
                    background.tool_call(&v2::ToolCallUpdate::new(id).status(status));
                }
            }
        }
        assert_eq!(background.idle(start), expected, "{case}");
    }
}

// Covers: the budget counts from the prompted turn's end, not from each
// later idle, and names what still ran when it trips: a turn the agent
// started, and each listed item; a missed follow-up is not a budget trip.
// Owner: v2 background-work keeping (pure).
#[test]
fn the_budget_names_what_still_ran() {
    let start = Instant::now();
    let mut background = Background::default();
    background.observe(&state(false, &["toolu_1"]));
    background.idle(start);
    background.observe(&state(true, &["toolu_1"]));
    let later = start + Duration::from_secs(60);
    background.observe(&state(false, &["toolu_1"]));
    assert_eq!(background.idle(later), Some(start + BACKGROUND_BUDGET));
    background.observe(&state(true, &["toolu_1"]));
    assert_eq!(
        background.expired(start + BACKGROUND_BUDGET),
        Some("acp: background work budget of 10 min after the prompted turn is spent; stopping the agent with still running: a turn the agent started on its own, command toolu_1".to_owned())
    );

    let mut owed = Background::default();
    owed.observe(&state(true, &["toolu_1"]));
    owed.observe(&state(false, &[]));
    let deadline = owed.idle(start).unwrap();
    assert_eq!(owed.expired(deadline), None);
}