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_protocol::{
    ErrorCode, SessionEvent, ToolCallId, ToolCallUpdate, ToolContent, ToolOutputGone,
    ToolOutputRef, ToolStatus,
};

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

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

/// A text item of about `bytes` as JSON.
fn text(bytes: usize) -> ToolContent {
    ToolContent::Text {
        text: "x".repeat(bytes),
    }
}

/// A failed read: its error code and the refs it names gone.
type Refused = (Option<ErrorCode>, Option<ToolOutputGone>);

/// What `asked` reads when the session shows `shown`: each item's ref and
/// its size as JSON.
fn answered(
    asked: &[ToolOutputRef],
    shown: Vec<ToolContent>,
) -> Result<Vec<(ToolOutputRef, usize)>, Refused> {
    let mut wanted = Wanted::new(asked.to_vec());
    wanted.find_in(&SessionEvent::tool_call(ToolCallUpdate {
        id: ToolCallId::new("c"),
        title: None,
        kind: None,
        status: Some(ToolStatus::Completed),
        raw_input: None,
        content: Some(shown),
        locations: None,
        display: None,
    }));
    match answer(&wanted) {
        Ok(answer) => Ok(answer
            .items
            .into_iter()
            .map(|item| (item.item, encoded_len(&item.content).unwrap()))
            .collect()),
        Err(error) => Err((
            error.error_code(),
            error.data.map(|data| serde_json::from_value(data).unwrap()),
        )),
    }
}

// Covers: an answer holds the items in the order asked, a ref asked twice
// twice, and stops before the item that would take it past one message,
// so the client asks again for the rest; an item too large alone fails the
// call as an internal error, and any ref not found fails it as
// `ToolOutputGone` naming each, before any size is weighed.
// Owner: daemon tool output answers.
#[test]
fn an_answer_takes_the_items_asked_in_order_that_fit_one_message() {
    let half = ANSWER_LIMIT / 2;
    let (small, large, huge) = (text(100), text(half), text(ANSWER_LIMIT));
    let (s, l, h) = (ref_of(&small), ref_of(&large), ref_of(&huge));
    let gone = ref_of(&text(7));
    let shown = || vec![small.clone(), large.clone(), huge.clone()];
    let sized = |item: &ToolContent| encoded_len(item).unwrap();
    let cases = [
        (
            "in order, twice",
            vec![l.clone(), s.clone(), l.clone(), s.clone()],
            Ok(vec![(l.clone(), sized(&large)), (s.clone(), sized(&small))]),
        ),
        (
            "twice in a row",
            vec![s.clone(), s.clone(), l.clone()],
            Ok(vec![
                (s.clone(), sized(&small)),
                (s.clone(), sized(&small)),
                (l.clone(), sized(&large)),
            ]),
        ),
        (
            "too large alone",
            vec![h.clone(), s.clone()],
            Err((Some(ErrorCode::InternalError), None)),
        ),
        (
            "gone first",
            vec![h.clone(), gone.clone()],
            Err((
                Some(ErrorCode::ToolOutputGone),
                Some(ToolOutputGone::new(vec![gone.clone()])),
            )),
        ),
    ];
    for (name, asked, expected) in cases {
        assert_eq!(answered(&asked, shown()), expected, "{name}");
    }
}