rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Which announced call an MCP call over the bridge draws its card under.

use agent_client_protocol::v1::schema::{
    SessionUpdate, ToolCall, ToolCallStatus, ToolCallUpdate, ToolCallUpdateFields,
};
use pretty_assertions::assert_eq;
use serde_json::{json, Map, Value};

use super::Announced;

fn arguments(value: Value) -> Map<String, Value> {
    let Value::Object(arguments) = value else {
        unreachable!("arguments are an object");
    };
    arguments
}

// Covers: what the agent's updates leave to claim: its `tool_call` of a
// served tool, announced pending or running, is kept, and its update that
// completes or fails a call withdraws it, while one announced already ended
// and an update that ends nothing change nothing; an MCP call claims the
// oldest unclaimed announcement of its tool whose `rawInput` carries its
// arguments, agy's own keys besides; with none such, the oldest of its
// tool's; never another tool's. Time is paused, so a claim that finds none
// gives up at once. Kills: the end arm dropped (a call the agent ended is
// claimed, so the next call draws under a closed card), any update read as
// an end, the announce arm's status check dropped (a call announced as it
// ended, which no MCP call follows, is claimed), the arguments ignored
// (same-name calls pair oldest-first, the wrong card), the oldest-first
// fallback dropped (a call whose arguments agy reworded draws a card of its
// own), the tool not checked.
// Owner: stdio bridge (announcement pairing).
#[tokio::test(start_paused = true)]
async fn an_mcp_call_claims_the_announcement_of_its_arguments() {
    type Heard = (SessionUpdate, Option<&'static str>);
    type Claim = (&'static str, Value);
    // Its name, the updates the agent sent, the claims made in order, and
    // the announcement each claim takes.
    type Case = (
        &'static str,
        Vec<Heard>,
        Vec<Claim>,
        Vec<Option<&'static str>>,
    );
    let ask = |color: &str| Some(json!({"color": color, "toolSummary": "Ask"}));
    let announced = |tool: &'static str, id: &'static str, status, input: Option<Value>| {
        let call = ToolCall::new(id, format!("mcp_rho_{tool}"))
            .status(status)
            .raw_input(input);
        (SessionUpdate::ToolCall(call), Some(tool))
    };
    let running = |tool, id, input| announced(tool, id, ToolCallStatus::InProgress, input);
    let updated = |id: &'static str, status| {
        let update = ToolCallUpdate::new(id, ToolCallUpdateFields::new().status(status));
        (SessionUpdate::ToolCallUpdate(update), None::<&'static str>)
    };
    let red = || json!({"color": "red"});
    let cases: [Case; 9] = [
        (
            "arguments pick among the tool's calls",
            vec![
                running("ask", "a", ask("red")),
                running("ask", "b", ask("blue")),
            ],
            vec![("ask", json!({"color": "blue"})), ("ask", red())],
            vec![Some("b"), Some("a")],
        ),
        (
            "none carries them: oldest first",
            vec![running("ask", "a", None), running("ask", "b", ask("blue"))],
            vec![
                ("ask", json!({"color": "green"})),
                ("ask", json!({"color": "green"})),
            ],
            vec![Some("a"), Some("b")],
        ),
        (
            "no arguments: oldest first",
            vec![
                running("ask", "a", ask("red")),
                running("ask", "b", Some(json!({}))),
            ],
            vec![("ask", json!({}))],
            vec![Some("a")],
        ),
        (
            "another tool's call",
            vec![running("other", "a", ask("red"))],
            vec![("ask", red())],
            vec![None],
        ),
        (
            "announced pending",
            vec![announced("ask", "a", ToolCallStatus::Pending, ask("red"))],
            vec![("ask", red())],
            vec![Some("a")],
        ),
        (
            "announced as it ended",
            vec![announced("ask", "a", ToolCallStatus::Completed, ask("red"))],
            vec![("ask", red())],
            vec![None],
        ),
        (
            "a call the agent completed",
            vec![
                running("ask", "a", ask("red")),
                running("ask", "b", ask("red")),
                updated("a", ToolCallStatus::Completed),
            ],
            vec![("ask", red()), ("ask", red())],
            vec![Some("b"), None],
        ),
        (
            "a call the agent failed",
            vec![
                running("ask", "a", ask("red")),
                updated("a", ToolCallStatus::Failed),
            ],
            vec![("ask", red())],
            vec![None],
        ),
        (
            "an update that ends nothing",
            vec![
                running("ask", "a", ask("red")),
                updated("a", ToolCallStatus::InProgress),
            ],
            vec![("ask", red())],
            vec![Some("a")],
        ),
    ];
    for (name, heard, claims, expected) in cases {
        let calls = Announced::default();
        for (update, tool) in &heard {
            calls.hear(update, *tool);
        }
        let mut claimed = Vec::new();
        for (tool, call) in claims {
            let id = calls.claim(tool, &arguments(call)).await;
            claimed.push(id.map(|id| id.to_string()));
        }

        let expected: Vec<Option<String>> = expected
            .into_iter()
            .map(|id| id.map(str::to_owned))
            .collect();
        assert_eq!(claimed, expected, "{name}");
    }
}