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::{SubagentRef, ToolContent};
use rho_sdk::{
    tool::{ToolMetadata, ToolOutput},
    ToolCallId, ToolCompletion,
};
use serde_json::json;
use tokio::sync::mpsc;

use super::{CallCard, CardScope};
use crate::{
    acp_runtime::agent_tools::CallCards,
    app::{
        session_events::LinkSources, subagent_manager::SubagentManager, subagent_pool::SubagentPool,
    },
    cli_runtime::agent_event::{AgentEvent, ServedCard},
    session::SessionPersistence,
    subagent::{RunState, RunStatus},
};

// Covers: a served `agent` call's finished card links the subagent the call
// started, by the call's id, showing as the run did when the call finished,
// as a Rho session's card does; a failed one links none, since it started
// no run of its own, even when a run names its id.
// Kills: `CallCard::finished` without the linked content (`content.extend(
// linked)` dropped), and the link resolved as though every call succeeded
// (`finished_status(result)` → `ToolStatus::Completed`).
// Owner: daemon sessions on an external runtime (served calls' cards).
#[tokio::test]
async fn a_finished_served_agent_call_links_its_subagent() {
    let root = tempfile::tempdir().unwrap();
    let subagents = SubagentManager::new(
        crate::config::Config::default(),
        root.path().join("config.toml"),
        root.path().to_path_buf(),
        SubagentPool::Own,
    );
    subagents.bind_parent_session("session", /*storage*/ None, SessionPersistence::Saved);
    subagents.insert_completed_status_for_test(
        "run",
        "session",
        RunStatus {
            state: RunState::Ok,
            title: Some("Fix the parser".into()),
            ..Default::default()
        },
    );
    let scope = CardScope {
        cwd: root.path().to_path_buf(),
        links: LinkSources::new(/*processes*/ None, Some(subagents.clone())),
    };
    // What the card of served call `call` shows besides itself once it
    // ended with `output`, the run having been started under its id.
    let finished = |call: &str, output: ToolOutput| {
        subagents.record_starter("run", call);
        let (events, mut drawn) = mpsc::unbounded_channel();
        let card = CallCard::start(
            CallCards::for_test(call, events),
            scope.clone(),
            ToolCallId::from_string(call).unwrap(),
            "agent",
            &json!({"agent_id": "default", "prompt": "Fix the parser."}),
            ToolMetadata::new(),
        );
        card.finished(&ToolCompletion::Completed(output));
        let mut last = None;
        while let Ok(event) = drawn.try_recv() {
            last = Some(event);
        }
        match last {
            Some(AgentEvent::Served {
                card: ServedCard::Finished { content, .. },
                ..
            }) => content,
            other => panic!("expected the call's end last, saw {other:?}"),
        }
    };
    let text = |text: &str| ToolContent::Text { text: text.into() };

    assert_eq!(
        [
            finished("toolu_ok", ToolOutput::text("started run")),
            finished("toolu_failed", ToolOutput::text("no run").failed()),
        ],
        [
            vec![
                text("started run"),
                ToolContent::Subagent(SubagentRef::new("run", None).titled("Fix the parser")),
            ],
            vec![text("no run")],
        ]
    );
}