use super::*;
fn named_tool_call_response(call_id: &str, tool: &str) -> serde_json::Value {
serde_json::json!({
"id": "gen-tool",
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": call_id,
"type": "function",
"function": { "name": tool, "arguments": "{}" }
}]
},
"finish_reason": "tool_calls"
}],
"usage": { "prompt_tokens": 6, "completion_tokens": 4, "total_tokens": 10 }
})
}
fn tool_result_matches(
requests: &[Vec<crate::llm::ChatMessage>],
call_id: &str,
pred: impl Fn(&str) -> bool,
) -> bool {
requests.iter().flatten().any(|m| {
m.role == "tool"
&& m.tool_call_id.as_deref() == Some(call_id)
&& m.content.as_deref().is_some_and(&pred)
})
}
#[tokio::test]
async fn redundant_test_rerun_is_suppressed() {
let llm = Arc::new(ScriptedLlm::from_json(&[
bash_call_response("b1", "echo cargo test"),
bash_call_response("b2", "echo cargo test"),
finish_task_call_response("f1", r#"{"status": "completed", "summary": "done"}"#),
]));
let registry = registry_with_finish_task_and_bash();
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default())
.with_redundant_run_suppression();
let out = agent
.run("system prompt", "run cargo test, then finish")
.await
.expect("loop terminates on finish_task");
assert_eq!(llm.calls(), 3, "both bash turns plus the finish turn run");
assert_eq!(out.summary.as_deref(), Some("done"));
let requests = llm.requests();
assert!(
tool_result_matches(&requests, "b1", |c| c.contains("exit_code: 0")),
"the first test run must actually execute"
);
assert!(
tool_result_matches(&requests, "b2", |c| c
== crate::redundant_run::REDUNDANT_RERUN_MESSAGE),
"the redundant second run must be short-circuited with the sentinel"
);
assert!(
!tool_result_matches(&requests, "b2", |c| c.contains("exit_code:")),
"the suppressed run must NOT carry a real exit_code line"
);
}
#[tokio::test]
async fn test_rerun_after_edit_is_not_suppressed() {
let llm = Arc::new(ScriptedLlm::from_json(&[
bash_call_response("b1", "echo cargo test"),
named_tool_call_response("w1", "write_file"),
bash_call_response("b2", "echo cargo test"),
finish_task_call_response("f1", r#"{"status": "completed", "summary": "done"}"#),
]));
let registry = registry_with_finish_task_and_bash();
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default())
.with_redundant_run_suppression();
let out = agent
.run("system prompt", "run cargo test, then finish")
.await
.expect("loop terminates on finish_task");
assert_eq!(
llm.calls(),
4,
"both bash turns, the write turn, and finish run"
);
assert_eq!(out.summary.as_deref(), Some("done"));
let requests = llm.requests();
assert!(
tool_result_matches(&requests, "b2", |c| c.contains("exit_code: 0")),
"a test run after a write_file must actually execute"
);
assert!(
!tool_result_matches(&requests, "b2", |c| c
== crate::redundant_run::REDUNDANT_RERUN_MESSAGE),
"a post-change run must NOT be suppressed"
);
}
#[tokio::test]
async fn differently_scoped_rerun_is_not_suppressed() {
let llm = Arc::new(ScriptedLlm::from_json(&[
bash_call_response("b1", "echo cargo test"),
bash_call_response("b2", "echo cargo test --workspace"),
finish_task_call_response("f1", r#"{"status": "completed", "summary": "done"}"#),
]));
let registry = registry_with_finish_task_and_bash();
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default())
.with_redundant_run_suppression();
let out = agent
.run("system prompt", "run the tests, then finish")
.await
.expect("loop terminates on finish_task");
assert_eq!(llm.calls(), 3, "both bash turns plus the finish turn run");
assert_eq!(out.summary.as_deref(), Some("done"));
let requests = llm.requests();
assert!(
tool_result_matches(&requests, "b2", |c| c.contains("exit_code: 0")),
"a differently-scoped run must actually execute"
);
assert!(
!tool_result_matches(&requests, "b2", |c| c
== crate::redundant_run::REDUNDANT_RERUN_MESSAGE),
"a differently-scoped run must NOT be suppressed"
);
}
#[tokio::test]
async fn rerun_not_suppressed_when_flag_absent() {
let llm = Arc::new(ScriptedLlm::from_json(&[
bash_call_response("b1", "echo cargo test"),
bash_call_response("b2", "echo cargo test"),
finish_task_call_response("f1", r#"{"status": "completed", "summary": "done"}"#),
]));
let registry = registry_with_finish_task_and_bash();
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default());
agent
.run("system prompt", "run cargo test, then finish")
.await
.expect("loop terminates on finish_task");
let requests = llm.requests();
assert!(
tool_result_matches(&requests, "b2", |c| c.contains("exit_code: 0")),
"without the opt-in flag the second run must execute for real"
);
assert!(
!tool_result_matches(&requests, "b2", |c| c
== crate::redundant_run::REDUNDANT_RERUN_MESSAGE),
"the sentinel must never appear when suppression is disabled"
);
}