use super::*;
#[tokio::test]
async fn truncation_circuit_breaker_redirects_then_hard_stops() {
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "t1",
"function": { "name": "spawn_agent", "arguments": "{\"task_name\":\"x\",\"message\":\"tru" }
}]
}
})),
StreamChunk::Stop { finish_reason: Some("tool_calls".to_string()) },
],
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "t2",
"function": { "name": "spawn_agent", "arguments": "{\"task_name\":\"x\",\"message\":\"tru" }
}]
}
})),
StreamChunk::Stop { finish_reason: Some("tool_calls".to_string()) },
],
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "t3",
"function": { "name": "spawn_agent", "arguments": "{\"task_name\":\"x\",\"message\":\"tru" }
}]
}
})),
StreamChunk::Stop { finish_reason: Some("tool_calls".to_string()) },
],
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "t4",
"function": { "name": "spawn_agent", "arguments": "{\"task_name\":\"x\",\"message\":\"tru" }
}]
}
})),
StreamChunk::Stop { finish_reason: Some("tool_calls".to_string()) },
],
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "t5",
"function": { "name": "spawn_agent", "arguments": "{\"task_name\":\"x\",\"message\":\"tru" }
}]
}
})),
StreamChunk::Stop { finish_reason: Some("tool_calls".to_string()) },
],
])))
.system_prompt("test")
.register_tool(SpawnLikeTool)
.build()
.expect("build");
let sid = runtime.create_session().await;
let events = Arc::new(Mutex::new(Vec::new()));
let ec = events.clone();
let result = runtime
.run_turn(sid.clone(), "do something", move |e| {
ec.lock().unwrap().push(e);
Ok(())
})
.await;
assert!(
result.is_ok(),
"run_turn should not error: {:?}",
result.err()
);
let outcome = result.unwrap();
match &outcome {
RunOutcome::Failed { error } => {
assert!(
error.contains("repeatedly truncated"),
"error should mention repeated truncation: {}",
error,
);
}
other => panic!("expected RunOutcome::Failed at hard limit, got {:?}", other),
}
let session = runtime.session(&sid).await.expect("session");
assert_eq!(session.run_state.truncation_strikes, 5);
}
#[tokio::test]
async fn truncation_strikes_reset_on_successful_tool_call() {
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "bad1",
"function": { "name": "spawn_agent", "arguments": "{\"task_name\":\"x\",\"message\":\"tru" }
}]
}
})),
StreamChunk::Stop { finish_reason: Some("tool_calls".to_string()) },
],
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "good1",
"function": { "name": "spawn_agent", "arguments": "{\"task_name\":\"y\",\"message\":\"hello\"}" }
}]
}
})),
StreamChunk::Stop { finish_reason: Some("tool_calls".to_string()) },
],
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "bad2",
"function": { "name": "spawn_agent", "arguments": "{\"task_name\":\"z\",\"message\":\"tru" }
}]
}
})),
StreamChunk::Stop { finish_reason: Some("tool_calls".to_string()) },
],
vec![
StreamChunk::Text("Done.".to_string()),
StreamChunk::Stop { finish_reason: Some("stop".to_string()) },
],
])))
.system_prompt("test")
.register_tool(SpawnLikeTool)
.build()
.expect("build");
let sid = runtime.create_session().await;
let events = Arc::new(Mutex::new(Vec::new()));
let ec = events.clone();
let result = runtime
.run_turn(sid.clone(), "do something", move |e| {
ec.lock().unwrap().push(e);
Ok(())
})
.await;
assert!(
result.is_ok(),
"run_turn should not error: {:?}",
result.err()
);
let outcome = result.unwrap();
assert!(
matches!(outcome, RunOutcome::Completed),
"expected Completed after reset, got {:?}",
outcome,
);
let session = runtime.session(&sid).await.expect("session");
assert_eq!(session.run_state.truncation_strikes, 1);
}
#[tokio::test]
async fn empty_args_counted_as_truncation_strike() {
let runtime = AgentBuilder::new(Arc::new(ScriptedProvider::new(vec![
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "e1",
"function": { "name": "spawn_agent", "arguments": "" }
}]
}
})),
StreamChunk::Stop {
finish_reason: Some("tool_calls".to_string()),
},
],
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "e2",
"function": { "name": "spawn_agent", "arguments": "" }
}]
}
})),
StreamChunk::Stop {
finish_reason: Some("tool_calls".to_string()),
},
],
vec![
StreamChunk::ToolCall(serde_json::json!({
"delta": {
"tool_calls": [{
"id": "e3",
"function": { "name": "spawn_agent", "arguments": "" }
}]
}
})),
StreamChunk::Stop {
finish_reason: Some("tool_calls".to_string()),
},
],
vec![
StreamChunk::Text("Done.".to_string()),
StreamChunk::Stop {
finish_reason: Some("stop".to_string()),
},
],
])))
.system_prompt("test")
.register_tool(SpawnLikeTool)
.build()
.expect("build");
let sid = runtime.create_session().await;
let events = Arc::new(Mutex::new(Vec::new()));
let ec = events.clone();
let result = runtime
.run_turn(sid.clone(), "do something", move |e| {
ec.lock().unwrap().push(e);
Ok(())
})
.await;
assert!(
result.is_ok(),
"run_turn should not error: {:?}",
result.err()
);
let outcome = result.unwrap();
assert!(
matches!(outcome, RunOutcome::Completed),
"expected Completed after redirect, got {:?}",
outcome,
);
let session = runtime.session(&sid).await.expect("session");
assert_eq!(session.run_state.truncation_strikes, 3);
}