use super::*;
#[tokio::test]
async fn tool_use_with_no_tool_calls_returns_failed() {
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![vec![
StreamChunk::ToolCall(serde_json::json!({ "no_delta": true })),
StreamChunk::Stop {
finish_reason: Some("tool_use".to_string()),
},
]])))
.system_prompt("test")
.build()
.expect("build runtime");
let sid = runtime.create_session().await;
let result = runtime.run_turn(sid, "do something", |_| Ok(())).await;
assert!(
matches!(result, Ok(RunOutcome::Failed { .. })),
"tool_use with no tool calls should return Failed: {:?}",
result
);
}
#[tokio::test]
async fn truncated_text_only_returns_failed() {
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![vec![
StreamChunk::Text("Here is part of my answer before being cut".to_string()),
StreamChunk::Stop {
finish_reason: Some("max_tokens".to_string()),
},
]])))
.system_prompt("test")
.build()
.expect("build runtime");
let sid = runtime.create_session().await;
let result = runtime.run_turn(sid, "do something", |_| Ok(())).await;
assert!(
matches!(result, Ok(RunOutcome::Failed { .. })),
"truncated text-only should return Failed: {:?}",
result
);
}
#[tokio::test]
async fn openai_length_text_only_returns_failed() {
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![vec![
StreamChunk::Text("Partial response before length limit".to_string()),
StreamChunk::Stop {
finish_reason: Some("length".to_string()),
},
]])))
.system_prompt("test")
.build()
.expect("build runtime");
let sid = runtime.create_session().await;
let result = runtime.run_turn(sid, "do something", |_| Ok(())).await;
assert!(
matches!(result, Ok(RunOutcome::Failed { .. })),
"OpenAI length with text-only should return Failed: {:?}",
result
);
}
#[tokio::test]
async fn tool_call_chunk_with_no_parsed_calls_and_stop_finish_fails() {
let guard = Arc::new(RecordingGuard::new(GuardDecision::Fail {
error: "incomplete tool call".to_string(),
}));
let guard_clone = guard.clone();
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![vec![
StreamChunk::ToolCall(serde_json::json!({ "no_delta": true })),
StreamChunk::Stop {
finish_reason: Some("stop".to_string()),
},
]])))
.system_prompt("test")
.register_tool(EchoTool)
.guard_dyn(guard_clone)
.build()
.expect("build runtime");
let sid = runtime.create_session().await;
let result = runtime
.run_turn(sid, "帮我解读一下这个工程", |_| Ok(()))
.await;
assert!(
matches!(result, Ok(RunOutcome::Failed { .. })),
"incomplete tool call should fail, not silently complete. Got: {:?}",
result
);
}
use crate::engine::react_loop_guard::{GuardCtx, GuardDecision, ReactLoopGuard};
struct RecordingGuard {
calls: Mutex<Vec<(bool, String)>>,
decision: GuardDecision,
}
impl RecordingGuard {
fn new(decision: GuardDecision) -> Self {
Self {
calls: Mutex::new(Vec::new()),
decision,
}
}
}
#[async_trait]
impl ReactLoopGuard for RecordingGuard {
async fn on_turn(&self, ctx: &GuardCtx) -> GuardDecision {
if ctx.is_text_only {
self.calls
.lock()
.unwrap()
.push((ctx.run_has_actions, ctx.model_response.clone()));
}
self.decision.clone()
}
}
#[tokio::test]
async fn branch4_guard_called_with_run_has_actions() {
let guard = Arc::new(RecordingGuard::new(GuardDecision::Complete));
let guard_clone = guard.clone();
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "call_1",
"function": {
"name": "echo",
"arguments": "{\"text\":\"hello\"}"
}
}]
}
})),
StreamChunk::Stop {
finish_reason: Some("tool_calls".to_string()),
},
],
vec![
StreamChunk::Text("The answer is 42.".to_string()),
StreamChunk::Stop {
finish_reason: Some("stop".to_string()),
},
],
])))
.system_prompt("test")
.register_tool(EchoTool)
.guard_dyn(guard_clone)
.build()
.expect("build runtime");
let sid = runtime.create_session().await;
let result = runtime.run_turn(sid, "echo hello", |_| Ok(())).await;
assert!(
matches!(result, Ok(RunOutcome::Completed)),
"run_turn should complete: {:?}",
result
);
let calls = guard.calls.lock().unwrap();
assert_eq!(calls.len(), 1, "on_text_only should be called exactly once");
let (run_has_tools, response) = &calls[0];
assert!(
*run_has_tools,
"run_has_actions must be true after tool use"
);
assert_eq!(response, "The answer is 42.");
}
#[tokio::test]
async fn branch4_guard_continue_nudges_and_retries() {
let guard = Arc::new(RecordingGuard::new(GuardDecision::Continue {
nudge: Some("please continue".to_string()),
}));
let guard_clone = guard.clone();
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "call_1",
"function": {
"name": "echo",
"arguments": "{\"text\":\"hello\"}"
}
}]
}
})),
StreamChunk::Stop {
finish_reason: Some("tool_calls".to_string()),
},
],
vec![
StreamChunk::Text("maybe done".to_string()),
StreamChunk::Stop {
finish_reason: Some("stop".to_string()),
},
],
vec![
StreamChunk::Text("here is the complete answer".to_string()),
StreamChunk::Stop {
finish_reason: Some("stop".to_string()),
},
],
])))
.system_prompt("test")
.register_tool(EchoTool)
.guard_dyn(guard_clone)
.build()
.expect("build runtime");
let sid = runtime.create_session().await;
let (_events, _outcome) = runtime
.run_turn_collect(sid.clone(), "echo hello")
.await
.expect("run_turn_collect");
let (call_count, all_have_tools) = {
let calls = guard.calls.lock().unwrap();
(calls.len(), calls.iter().all(|(has_tools, _)| *has_tools))
};
assert!(
call_count >= 2,
"guard should be called at least twice (initial + after nudge), got: {}",
call_count
);
let session = runtime.session(&sid).await.expect("session exists");
let has_nudge = session.chat_messages().iter().any(
|m| matches!(m, ChatMessage::User { content, .. } if content.contains("please continue")),
);
assert!(has_nudge, "session should contain the nudge message");
assert!(
all_have_tools,
"all on_text_only calls should have run_has_actions = true"
);
}
struct EmptyResponseStrikeGuard {
max_strikes: usize,
calls: AtomicUsize,
}
impl EmptyResponseStrikeGuard {
fn new(max_strikes: usize) -> Self {
Self {
max_strikes,
calls: AtomicUsize::new(0),
}
}
}
#[async_trait]
impl ReactLoopGuard for EmptyResponseStrikeGuard {
async fn on_turn(&self, ctx: &GuardCtx) -> GuardDecision {
if ctx.is_empty_response {
let count = self.calls.fetch_add(1, Ordering::SeqCst) + 1;
if count >= self.max_strikes {
GuardDecision::Fail {
error: format!("exceeded max retries ({})", self.max_strikes),
}
} else {
GuardDecision::Continue {
nudge: Some("please retry".to_string()),
}
}
} else {
GuardDecision::Complete
}
}
}
#[tokio::test]
async fn incomplete_tool_calls_fail_after_max_strikes() {
let max_strikes = 3;
let guard = Arc::new(EmptyResponseStrikeGuard::new(max_strikes));
let guard_clone = guard.clone();
let script: Vec<Vec<StreamChunk>> = (0..max_strikes + 1)
.map(|_| {
vec![
StreamChunk::ToolCall(serde_json::json!({ "no_delta": true })),
StreamChunk::Stop {
finish_reason: Some("stop".to_string()),
},
]
})
.collect();
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(script)))
.system_prompt("test")
.register_tool(EchoTool)
.guard_dyn(guard_clone)
.build()
.expect("build runtime");
let sid = runtime.create_session().await;
let result = runtime
.run_turn(sid, "帮我解读一下这个工程", |_| Ok(()))
.await;
assert!(
matches!(result, Ok(RunOutcome::Failed { .. })),
"incomplete tool calls should fail after {} strikes, got: {:?}",
max_strikes,
result
);
let calls = guard.calls.load(Ordering::SeqCst);
assert_eq!(
calls, max_strikes,
"guard should be called {} times, got: {}",
max_strikes, calls
);
}