use super::*;
fn multi_echo_response(calls: &[(&str, &str)]) -> Value {
let tool_calls: Vec<Value> = calls
.iter()
.map(|(id, raw_arguments)| {
json!({
"id": id,
"type": "function",
"function": { "name": "echo", "arguments": raw_arguments }
})
})
.collect();
json!({
"id": "gen-multi",
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": tool_calls
},
"finish_reason": "tool_calls"
}],
"usage": { "prompt_tokens": 15, "completion_tokens": 12, "total_tokens": 27 }
})
}
#[tokio::test]
async fn batched_multi_tool_calls_all_dispatch_in_one_turn() {
let llm = Arc::new(ScriptedLlm::from_json(&[
multi_echo_response(&[
("call_a", r#"{"text":"alpha"}"#),
("call_b", r#"{"text":"beta"}"#),
("call_c", r#"{"text":"gamma"}"#),
]),
stop_response("all three echoed"),
]));
let registry = registry_with_echo(false);
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default());
let mut transcript = Transcript::seed("system", "echo three things at once");
let out = agent
.run_with_transcript(&mut transcript, "echo three things at once")
.await
.expect("batched turn should complete");
assert_eq!(out.content, "all three echoed");
assert_eq!(
llm.calls(),
2,
"three tool calls must be answered within a SINGLE turn, then one stop"
);
let tool_msgs: Vec<crate::llm::ChatMessage> = transcript
.messages()
.into_iter()
.filter(|m| m.role == "tool")
.collect();
assert_eq!(
tool_msgs.len(),
3,
"every call in the batch must produce exactly one tool result"
);
let seen: Vec<(Option<&str>, Option<&str>)> = tool_msgs
.iter()
.map(|m| (m.tool_call_id.as_deref(), m.content.as_deref()))
.collect();
assert_eq!(
seen,
vec![
(Some("call_a"), Some("echo: alpha")),
(Some("call_b"), Some("echo: beta")),
(Some("call_c"), Some("echo: gamma")),
],
"each tool result must correlate to its call_id and preserve emission order"
);
}
#[tokio::test]
async fn batched_partial_failure_still_answers_siblings_and_continues() {
let llm = Arc::new(ScriptedLlm::from_json(&[
multi_echo_response(&[
("call_1", r#"{"text":"first"}"#),
("call_2", "{not valid json"),
("call_3", r#"{"text":"third"}"#),
]),
stop_response("continued past the partial failure"),
]));
let registry = registry_with_echo(false);
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default());
let mut transcript = Transcript::seed("system", "batch with one bad call");
let out = agent
.run_with_transcript(&mut transcript, "batch with one bad call")
.await
.expect("a single failed call must not abort the turn");
assert_eq!(out.content, "continued past the partial failure");
assert_eq!(
llm.calls(),
2,
"the loop must continue past the partial failure, not abort the turn"
);
let tool_msgs: Vec<crate::llm::ChatMessage> = transcript
.messages()
.into_iter()
.filter(|m| m.role == "tool")
.collect();
assert_eq!(
tool_msgs.len(),
3,
"all three calls — including the failed one — must produce a tool result"
);
assert_eq!(tool_msgs[0].tool_call_id.as_deref(), Some("call_1"));
assert_eq!(tool_msgs[0].content.as_deref(), Some("echo: first"));
assert_eq!(tool_msgs[2].tool_call_id.as_deref(), Some("call_3"));
assert_eq!(tool_msgs[2].content.as_deref(), Some("echo: third"));
assert_eq!(tool_msgs[1].tool_call_id.as_deref(), Some("call_2"));
let failed = tool_msgs[1].content.as_deref().unwrap_or("");
assert!(
!failed.is_empty() && failed != "echo: ",
"the failed call's result must carry a real error message, got {failed:?}"
);
assert!(
!failed.starts_with("echo: "),
"the failed call must NOT be reported as a successful echo, got {failed:?}"
);
}