rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use std::path::PathBuf;

use pretty_assertions::assert_eq;
use rho_protocol::{
    DeferredItem, DeferredKind, SessionEvent, ToolCallId, ToolCallUpdate, ToolContent,
    ToolOutputDeferral, ToolOutputRef, ToolStatus,
};

use super::*;
use crate::app::session_events::status_update;

fn ref_of(item: &ToolContent) -> ToolOutputRef {
    item_ref(item).unwrap()
}

fn text(bytes: usize) -> ToolContent {
    ToolContent::Text {
        text: "x".repeat(bytes),
    }
}

fn image() -> ToolContent {
    ToolContent::Image {
        mime_type: "image/png".into(),
        data: "A".repeat(4096),
    }
}

fn diff() -> ToolContent {
    ToolContent::Diff {
        path: PathBuf::from("/w/a.txt"),
        old_text: Some("old\n".repeat(300)),
        new_text: "new\n".repeat(300),
        deleted: false,
    }
}

fn terminal() -> ToolContent {
    ToolContent::Terminal {
        process: "p-1".into(),
    }
}

fn call(status: ToolStatus, content: Vec<ToolContent>) -> SessionEvent {
    SessionEvent::tool_call(ToolCallUpdate {
        content: Some(content),
        ..status_update(ToolCallId::new("c"), Some(status))
    })
}

/// `item`'s placeholder, as a deferral sends it.
fn placeholder_of(item: &ToolContent, kind: DeferredKind) -> ToolContent {
    let bytes = encoded_len(item).unwrap() as u64;
    ToolContent::Deferred(DeferredItem::new(kind, bytes, ref_of(item)))
}

fn content(event: &SessionEvent) -> Vec<ToolContent> {
    match event {
        SessionEvent::ToolCall(update) => update.content.clone().unwrap(),
        other => panic!("not a call: {other:?}"),
    }
}

// Covers: a deferral leaves out exactly a finished call's text, image and
// diff items of at least its size, each in place, and keeps the rest whole:
// smaller items, an item its placeholder would not shrink, the terminal a
// client draws, and any call still running, whose content may change.
// Owner: session events tool output deferral.
#[test]
fn a_deferral_leaves_out_a_finished_calls_large_output_items() {
    let at_1k = ToolOutputDeferral::new(1024);
    let short = text(10);
    let tiny = text(40);
    let items = vec![text(2000), short.clone(), image(), diff(), terminal()];
    let cases = [
        (
            "finished",
            call(ToolStatus::Completed, items.clone()),
            at_1k,
            Some(vec![
                placeholder_of(&items[0], DeferredKind::Text),
                short,
                placeholder_of(&items[2], DeferredKind::Image),
                placeholder_of(&items[3], DeferredKind::Diff),
                terminal(),
            ]),
        ),
        ("running", call(ToolStatus::InProgress, items), at_1k, None),
        (
            "no smaller",
            call(ToolStatus::Failed, vec![tiny]),
            ToolOutputDeferral::new(0),
            None,
        ),
    ];
    for (name, event, deferral, expected) in cases {
        let thinned = defer(&event, deferral);
        assert_eq!(thinned.as_ref().map(content), expected, "{name}");
    }
}

// Covers: what capture records of an event's deferrable items adds up to
// exactly the bytes deferral takes off the event, for any size a client
// asks, so a window counts a deferred replay as it is sent.
// Owner: session events tool output deferral.
#[test]
fn deferrable_sizes_add_up_to_what_a_deferral_takes_off() {
    let event = call(
        ToolStatus::Completed,
        vec![text(2000), text(500), image(), diff(), terminal()],
    );
    let mut index = DeferrableIndex::default();
    index.record(0, &event);
    let whole = encoded_len(&event).unwrap() as u64;
    for min_bytes in [0, 600, 2100, 100_000] {
        let deferral = ToolOutputDeferral::new(min_bytes);
        let sent =
            defer(&event, deferral).map_or(whole, |thinned| encoded_len(&thinned).unwrap() as u64);
        let left_out = index.left_out(Some(deferral)).between(0, 1);
        assert_eq!(whole - left_out, sent, "min_bytes {min_bytes}");
    }
}

// Covers: a read asks for refs in any order and any number of times; each
// is found by its item's hash, in whichever event carries it, live or
// listed with the refs its buffer kept, and answered each time asked; the
// ones no event carried, and any no item could have, are named gone once
// each, in the order first asked.
// Owner: session events tool output lookup.
#[test]
fn wanted_refs_are_found_by_their_items_and_the_rest_named_gone() {
    let (first, second) = (text(2000), image());
    let missing = ref_of(&text(3000));
    let malformed = ToolOutputRef::new("t1.not-a-hash");
    let asked = vec![
        ref_of(&second),
        missing.clone(),
        malformed.clone(),
        ref_of(&second),
        ref_of(&first),
        missing.clone(),
    ];
    let mut wanted = Wanted::new(asked);
    wanted.find_in(&call(ToolStatus::Completed, vec![first.clone()]));
    wanted.find_in(&call(ToolStatus::InProgress, vec![text(3000)]));
    let listed = call(ToolStatus::Completed, vec![terminal(), second.clone()]);
    wanted.find_listed(&listed, &Deferrables::of(&listed));
    let answers: Vec<_> = wanted
        .answers()
        .map(|(item, content)| (item.clone(), content.cloned()))
        .collect();

    assert_eq!(
        (answers, wanted.gone()),
        (
            vec![
                (ref_of(&second), Some(second.clone())),
                (missing.clone(), None),
                (malformed.clone(), None),
                (ref_of(&second), Some(second)),
                (ref_of(&first), Some(first)),
                (missing.clone(), None),
            ],
            vec![missing, malformed],
        )
    );
}

// Covers: refs that no item could have, as a call naming junk sends, leave
// nothing to search for, so the read answers them gone without rendering
// the transcript.
// Owner: session events tool output lookup.
#[test]
fn refs_no_item_could_have_need_no_search() {
    let wanted = Wanted::new(vec![
        ToolOutputRef::new("x"),
        ToolOutputRef::new("t1.0123456789abcdef0123456789ABCDEF"),
        ToolOutputRef::new("t1.0123456789abcdef0123456789abcde"),
    ]);
    assert_eq!((wanted.complete(), wanted.gone().len()), (true, 3));
}