use super::*;
#[tokio::test]
async fn finish_gate_trips_and_recovers() {
let llm = Arc::new(ScriptedLlm::from_json(&[
finish_task_call_response(
"call-premature",
r#"{"status": "completed", "summary": "done (premature)"}"#,
),
bash_call_response("call-bash", "pytest tests/ -v"),
finish_task_call_response(
"call-verified",
r#"{"status": "completed", "summary": "done (verified)"}"#,
),
]));
let registry = registry_with_finish_task_and_bash();
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default())
.with_finish_gate(crate::verify_gate::default_finish_gate());
let out = agent
.run(
"system prompt",
"implement the parser; run `pytest tests/ -v` before finishing",
)
.await
.expect("loop should recover and terminate on the verified finish call");
assert_eq!(
llm.calls(),
3,
"turn 1's premature finish must be rejected, turn 2 runs the named \
tests, turn 3's finish must succeed"
);
let turn2_request = &llm.requests()[1];
let nudge_present = turn2_request.iter().any(|m| {
m.role == "tool"
&& m.content.as_deref().is_some_and(|c| {
c.contains("finish_task rejected") && c.contains("Run the named test suite")
})
});
assert!(
nudge_present,
"turn 2's request must carry the injected block→retry nudge as a tool \
result; messages: {turn2_request:?}"
);
assert_ne!(out.summary.as_deref(), Some("done (premature)"));
assert_eq!(out.summary.as_deref(), Some("done (verified)"));
assert_eq!(out.content, "Task completed: done (verified)");
}
#[tokio::test]
async fn finish_gate_trip_logs_warn() {
crate::test_support::begin_capture();
let llm = Arc::new(ScriptedLlm::from_json(&[
finish_task_call_response(
"call-premature",
r#"{"status": "completed", "summary": "done (premature)"}"#,
),
bash_call_response("call-bash", "pytest tests/ -v"),
finish_task_call_response(
"call-verified",
r#"{"status": "completed", "summary": "done (verified)"}"#,
),
]));
let registry = registry_with_finish_task_and_bash();
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default())
.with_finish_gate(crate::verify_gate::default_finish_gate());
agent
.run(
"system prompt",
"implement the parser; run `pytest tests/ -v` before finishing",
)
.await
.expect("loop should recover and terminate on the verified finish call");
let messages = crate::test_support::captured_at_least(tracing::Level::WARN);
assert!(
messages
.iter()
.any(|m| m.contains("gate intercepted finish_task")),
"expected a warn-level gate-intercept log, got: {messages:?}"
);
}
#[tokio::test]
async fn finish_gate_inert_without_named_test_command() {
let llm = Arc::new(ScriptedLlm::from_json(&[finish_task_call_response(
"call-finish",
r#"{"status": "completed", "summary": "implemented the widget"}"#,
)]));
let registry = registry_with_finish_task_and_bash();
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default())
.with_finish_gate(crate::verify_gate::default_finish_gate());
let out = agent
.run("system prompt", "implement a widget")
.await
.expect("gate must stay inert when no test command is named");
assert_eq!(llm.calls(), 1, "gate must not force an extra turn");
assert_eq!(out.summary.as_deref(), Some("implemented the widget"));
}
#[tokio::test]
async fn finish_gate_passes_first_attempt_when_named_tests_already_ran() {
let llm = Arc::new(ScriptedLlm::from_json(&[
bash_call_response("call-bash", "pytest tests/ -v"),
finish_task_call_response(
"call-finish",
r#"{"status": "completed", "summary": "done (verified first try)"}"#,
),
]));
let registry = registry_with_finish_task_and_bash();
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default())
.with_finish_gate(crate::verify_gate::default_finish_gate());
let out = agent
.run(
"system prompt",
"implement the parser; run `pytest tests/ -v` before finishing",
)
.await
.expect("finish must be accepted on its first attempt once tests have run");
assert_eq!(
llm.calls(),
2,
"tests ran on turn 1, so turn 2's first finish must succeed with no extra turn"
);
assert_eq!(out.summary.as_deref(), Some("done (verified first try)"));
let any_nudge = llm.requests().iter().flatten().any(|m| {
m.role == "tool"
&& m.content
.as_deref()
.is_some_and(|c| c.contains("finish_task rejected"))
});
assert!(
!any_nudge,
"the gate must not inject a nudge when the named tests already ran"
);
}