use crate::core::agent::session::AgentSessionState;
use crate::llm::provider::MessageRole;
pub fn check_completion_indicators(response_text: &str) -> bool {
const COMPLETION_SENTENCES: &[&str] = &[
"the task is complete",
"task is complete",
"task has been completed",
"i have successfully completed the task",
"work is finished",
"operation successful",
"i am done",
"no more actions needed",
"successfully accomplished",
"task is now complete",
"everything is finished",
"i've finished the task",
"all requested changes have been applied",
"i have finished all the work",
];
const SOFT_INDICATORS: &[&str] = &[
"task completed",
"task done",
"all done",
"finished.",
"complete.",
"done.",
];
let response_lower = response_text.to_lowercase();
if COMPLETION_SENTENCES
.iter()
.any(|&s| response_lower.contains(s))
{
return true;
}
let trimmed = response_lower.trim();
for &indicator in SOFT_INDICATORS {
if trimmed.ends_with(indicator) || trimmed == indicator {
let sentences: Vec<_> = trimmed.split(['.', '!', '?']).collect();
if let Some(last_sentence) = sentences.last() {
let ls = last_sentence.trim();
if ls.contains(indicator)
&& !ls.contains("will")
&& !ls.contains("going to")
&& !ls.contains("about to")
&& !ls.contains("once")
&& !ls.contains("after")
{
return true;
}
}
}
}
false
}
pub fn check_for_response_loop(response_text: &str, session_state: &mut AgentSessionState) -> bool {
if response_text.len() < 10 {
return false;
}
let normalized_current = response_text
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
let repeated = session_state
.messages
.iter()
.rev()
.filter(|m| m.role == MessageRole::Assistant)
.skip(1)
.take(2)
.any(|m| {
let normalized_prev = m
.content
.as_text()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
normalized_prev == normalized_current
});
if repeated {
let warning =
"Repetitive assistant response detected. Breaking potential loop.".to_string();
session_state.warnings.push(warning);
session_state.consecutive_idle_turns =
session_state.consecutive_idle_turns.saturating_add(1);
return true;
}
false
}
#[cfg(test)]
mod tests {
use super::*;
use crate::llm::provider::Message;
#[test]
fn test_completion_indicators() {
assert!(check_completion_indicators("The task is complete"));
assert!(check_completion_indicators("Revision 1: task is complete."));
assert!(check_completion_indicators(
"I have successfully completed the task."
));
assert!(check_completion_indicators("Task done"));
assert!(check_completion_indicators("All done"));
assert!(!check_completion_indicators(
"I will have the task done soon"
));
assert!(!check_completion_indicators("Is the task done?"));
assert!(!check_completion_indicators("random text"));
}
#[test]
fn response_loop_ignores_current_assistant_message() {
let repeated_response = "The task is complete";
let mut state = AgentSessionState::new("session".to_string(), 8, 4, 128_000);
state
.messages
.push(Message::assistant(repeated_response.to_string()));
assert!(!check_for_response_loop(repeated_response, &mut state));
}
#[test]
fn response_loop_still_detects_prior_duplicate_assistant_message() {
let repeated_response = "The task is complete";
let mut state = AgentSessionState::new("session".to_string(), 8, 4, 128_000);
state
.messages
.push(Message::assistant(repeated_response.to_string()));
state
.messages
.push(Message::assistant(repeated_response.to_string()));
assert!(check_for_response_loop(repeated_response, &mut state));
}
}