rho-coding-agent 3.7.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;
use rho_protocol::{
    AnswerStop, CardTone, MessageId, SessionEvent, ToolCallId, ToolCallUpdate, TurnId, TurnOutcome,
};

use super::{MissingReply, StreamedReply};

fn chunk(message: &str, text: &str) -> SessionEvent {
    SessionEvent::AgentMessageChunk {
        message: MessageId::new(message),
        text: text.into(),
    }
}

fn thought(message: &str) -> SessionEvent {
    SessionEvent::ThoughtChunk {
        message: MessageId::new(message),
        text: "thinking".into(),
    }
}

fn tool_call() -> SessionEvent {
    SessionEvent::ToolCall(Box::new(ToolCallUpdate {
        id: ToolCallId::new("call-1"),
        title: None,
        kind: None,
        status: None,
        raw_input: None,
        content: None,
        locations: None,
        display: None,
    }))
}

fn card() -> SessionEvent {
    SessionEvent::Card {
        title: "result".into(),
        markdown: String::new(),
        plain: String::new(),
        tone: CardTone::Neutral,
        display: None,
    }
}

fn user() -> SessionEvent {
    SessionEvent::UserMessage {
        message: MessageId::new("u1"),
        content: Vec::new(),
        steer: None,
    }
}

fn ended(outcome: TurnOutcome) -> SessionEvent {
    SessionEvent::TurnEnded {
        turn: TurnId::new("t1"),
        outcome,
    }
}

fn answered(text: &str) -> SessionEvent {
    ended(TurnOutcome::Answered {
        text: text.into(),
        stop: AnswerStop::EndTurn,
    })
}

fn whole(text: &str) -> Option<MissingReply> {
    Some(MissingReply {
        text: text.into(),
        continues: None,
    })
}

fn rest(message: &str, text: &str) -> Option<MissingReply> {
    Some(MissingReply {
        text: text.into(),
        continues: Some(MessageId::new(message)),
    })
}

// Covers: a turn's end finds the answer text its stream did not deliver:
// all of an answer nothing streamed, or a later step's after a tool call or
// a result; the rest of a reply whose last chunks went missing; nothing for
// a reply streamed whole, one that differs (a retry the stream showed), an
// empty answer, an outcome with no answer, or a host command's answer,
// which a replay shows only as what the command did (a compaction). A user
// message, a restarted response or a thought ends what streamed before it.
// Owner: shared turn-answer reconciliation (TUI and ACP)
#[test]
fn a_turns_end_finds_the_answer_text_its_stream_missed() {
    let cases = [
        (
            "nothing streamed",
            vec![],
            answered("Done."),
            whole("Done."),
        ),
        (
            "the last chunks went missing",
            vec![chunk("m1", "Good ")],
            answered("Good morning."),
            rest("m1", "morning."),
        ),
        (
            "streamed whole across chunks",
            vec![chunk("m1", "Good "), chunk("m1", "morning.")],
            answered("Good morning."),
            None,
        ),
        (
            "a later step's answer after a tool call",
            vec![chunk("m1", "Let me read it."), tool_call()],
            answered("It says hello."),
            whole("It says hello."),
        ),
        (
            "a later step's answer after a result card",
            vec![chunk("m1", "Waiting."), card()],
            answered("It finished."),
            whole("It finished."),
        ),
        (
            "the step's own text again after a tool call",
            vec![chunk("m1", "Done."), tool_call()],
            answered("Done."),
            None,
        ),
        (
            "a reply the answer does not begin with",
            vec![chunk("m1", "Hello")],
            answered("Goodbye"),
            None,
        ),
        (
            "an empty answer: the last step only called tools",
            vec![chunk("m1", "Let me read it."), tool_call()],
            answered(""),
            None,
        ),
        (
            "a step limit's unstreamed last reply",
            vec![],
            ended(TurnOutcome::MaxSteps {
                text: "Partial.".into(),
            }),
            whole("Partial."),
        ),
        (
            "a host command's answer after a turn that only called tools",
            vec![user(), tool_call()],
            ended(TurnOutcome::Answered {
                text: "context compacted".into(),
                stop: AnswerStop::Command,
            }),
            None,
        ),
        (
            "an outcome with no answer",
            vec![chunk("m1", "Hel")],
            ended(TurnOutcome::Cancelled),
            None,
        ),
        (
            "the previous turn's reply is over at the next user message",
            vec![chunk("m1", "Done."), user()],
            answered("Done."),
            whole("Done."),
        ),
        (
            "a restarted response starts over",
            vec![chunk("m1", "Hel"), SessionEvent::ResponseDiscarded],
            answered("Hello."),
            whole("Hello."),
        ),
        (
            "a chunk after a thought starts a new reply, though its id repeats",
            vec![chunk("m1", "Hel"), thought("t1"), chunk("m1", "Hello.")],
            answered("Hello."),
            None,
        ),
    ];
    for (name, before, end, expected) in cases {
        let mut reply = StreamedReply::default();
        for event in &before {
            assert_eq!(reply.observe(event), None, "{name}: {event:?}");
        }
        assert_eq!(reply.observe(&end), expected, "{name}");
    }
}