use std::any::Any;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use gadk_agent::{Agent, AgentError, InvocationContext, Tool};
use gadk_model::LlmRequest;
use tokio::sync::Barrier;
use super::agent_workflowagents_sequentialagent_agent::{
FakeLlm, new_custom_agent, new_sequential_agent,
};
use crate::llm::flow::Flow;
use crate::llm::processors::instructions::instructions_request_processor;
use crate::llm::state::reveal;
use crate::llm::{InstructionProvider, LlmAgentConfig, agent as llm_agent};
const COMPLETION_INSTRUCTION_MARKER: &str = "call the task_completed function to exit";
fn ctx_for(agent: Arc<dyn Agent>, invocation_id: &str) -> InvocationContext {
InvocationContext::builder()
.agent(agent)
.invocation_id(invocation_id)
.build_invocation()
}
fn completion_counts(child: &Arc<dyn Agent>) -> (usize, usize) {
let state = reveal(&**child).expect("an LLM agent");
let tools = state
.tools
.read()
.iter()
.filter(|t| t.name() == "task_completed")
.count();
let instructions = state
.instruction()
.matches(COMPLETION_INSTRUCTION_MARKER)
.count();
(tools, instructions)
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_sequential_agent_run_live_concurrent_injection_is_visible_to_every_caller() {
for (name, parents_n) in [
("same_parent", 1usize),
("shared_children_between_parents", 2),
] {
for _round in 0..5 {
let children: Vec<Arc<dyn Agent>> = vec![
new_custom_agent(1),
new_custom_agent(2),
new_custom_agent(3),
];
let parents: Vec<Arc<dyn Agent>> = (0..parents_n)
.map(|i| new_sequential_agent(children.clone(), &format!("sequential_{i}")))
.collect();
let start = Arc::new(Barrier::new(20));
let mut handles = Vec::new();
for caller in 0..20usize {
let start = start.clone();
let seq = parents[caller % parents.len()].clone();
let children = children.clone();
let name = name.to_string();
handles.push(tokio::spawn(async move {
start.wait().await;
let live = seq.as_live().expect("RunLive");
if let Err(e) = live.run_live(ctx_for(seq.clone(), "concurrent")).await {
panic!("{name}: RunLive() error = {e}");
}
for (i, child) in children.iter().enumerate() {
let (tools, instructions) = completion_counts(child);
assert_eq!(
tools, 1,
"{name}: child {i} has {tools} completion tools, want 1"
);
assert_eq!(
instructions, 1,
"{name}: child {i} has {instructions} completion instructions, want 1"
);
}
}));
}
for h in handles {
h.await.unwrap();
}
}
}
}
struct NamedCompletionTool;
impl Tool for NamedCompletionTool {
fn name(&self) -> &str {
"task_completed"
}
fn description(&self) -> &str {
"caller-provided completion tool"
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[tokio::test]
async fn test_sequential_agent_run_live_preserves_existing_completion_tool() {
let existing: Arc<dyn Tool> = Arc::new(NamedCompletionTool);
let child = llm_agent::new(LlmAgentConfig {
name: "child".into(),
model: Some(Arc::new(FakeLlm {
id: 0,
calls: AtomicUsize::new(0),
})),
tools: vec![existing.clone()],
instruction: "original instruction".into(),
..Default::default()
})
.unwrap();
let seq = new_sequential_agent(vec![child.clone()], "sequential");
let live = seq.as_live().expect("RunLive");
for _ in 0..2 {
if let Err(e) = live.run_live(ctx_for(seq.clone(), "existing")).await {
panic!("RunLive() error = {e}");
}
}
let state = reveal(&*child).unwrap();
let tools = state.tools.read().clone();
assert!(
tools.len() == 1 && Arc::ptr_eq(&tools[0], &existing),
"RunLive changed the caller's completion tool"
);
assert_eq!(
state.instruction(),
"original instruction",
"RunLive changed the instruction for a caller-provided completion tool"
);
}
async fn system_instruction(agent: Arc<dyn Agent>) -> Result<String, AgentError> {
let ctx = ctx_for(agent, "provider");
let mut req = LlmRequest::default();
instructions_request_processor(&ctx, &mut req, &Flow::default()).await?;
Ok(req
.config
.and_then(|c| c.system_instruction)
.map(|si| si.parts.iter().map(|p| p.text.as_str()).collect::<String>())
.unwrap_or_default())
}
#[tokio::test]
async fn test_sequential_agent_run_live_appends_completion_instruction_to_provider() {
for (name, result, err, want) in [
(
"appends_to_provider_result",
"dynamic instruction",
None,
"dynamic instruction",
),
(
"passes_provider_error_through",
"partial",
Some("provider failed"),
"",
),
] {
let provider: InstructionProvider = Arc::new(move |_ctx| {
Box::pin(async move {
match err {
Some(e) => Err(AgentError::other(e)),
None => Ok(result.to_string()),
}
})
});
let child = llm_agent::new(LlmAgentConfig {
name: "child".into(),
model: Some(Arc::new(FakeLlm {
id: 0,
calls: AtomicUsize::new(0),
})),
instruction_provider: Some(provider),
..Default::default()
})
.unwrap();
let seq = new_sequential_agent(vec![child.clone()], "sequential");
let live = seq.as_live().expect("RunLive");
for _ in 0..2 {
if let Err(e) = live.run_live(ctx_for(seq.clone(), "provider")).await {
panic!("{name}: RunLive() error = {e}");
}
}
let got = system_instruction(child).await;
if let Some(want_err) = err {
let e = got.expect_err("want the provider's error");
assert!(
e.to_string().contains(want_err),
"{name}: InstructionProvider() error = {e}, want {want_err}"
);
continue;
}
let got = got.unwrap_or_else(|e| panic!("{name}: InstructionProvider() error = {e}"));
assert!(
got.starts_with(want),
"{name}: InstructionProvider() = {got:?}, want it to start with {want:?}"
);
let count = got.matches(COMPLETION_INSTRUCTION_MARKER).count();
assert_eq!(
count, 1,
"{name}: InstructionProvider() has {count} completion instructions, want 1: {got:?}"
);
}
}