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},
};
#[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", 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( None, Some(subagents.clone())),
};
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")],
]
);
}