use crate::types::{ToolCall, ToolResult};
use regex::Regex;
use serde_json::Value;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::OnceLock;
pub const REPEATED_FAILURES: u32 = 3;
pub const REPEATED_READS: u32 = 3;
pub const READ_TOOLS: &[&str] = &["read_file", "list_dir", "search_files", "find_files"];
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StallKind {
RepeatedCall,
RepeatedFailure,
RepeatedReads,
}
impl StallKind {
pub fn as_str(self) -> &'static str {
match self {
Self::RepeatedCall => "repeated_call",
Self::RepeatedFailure => "repeated_failure",
Self::RepeatedReads => "repeated_reads",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stall {
pub kind: StallKind,
pub tool: Option<String>,
pub count: u32,
}
impl Stall {
pub fn note(&self) -> String {
let tool = self.tool.as_deref().unwrap_or("the tool");
match self.kind {
StallKind::RepeatedCall => format!(
"[stall] That call to `{tool}` is the same as an earlier one and failed the \
same way, with nothing changed in between, so running it again will not help. \
Read the error again, check the assumption behind the call, and try something \
different."
),
StallKind::RepeatedFailure => format!(
"[stall] `{tool}` has now failed the same way {} times, though things changed in \
between, so the fixes so far have not reached the cause. Stop adjusting the \
same thing: re-read the error and the code it points at, work out what really \
causes it, and change your approach.",
self.count
),
StallKind::RepeatedReads => format!(
"[stall] You have read the same thing again {} times with nothing changed in \
between, so reading it again will not tell you more. If you know enough to \
answer or to make the change, do it now; otherwise decide exactly what you are \
still missing and look only for that.",
self.count
),
}
}
pub fn notice(&self) -> String {
let what = match self.kind {
StallKind::RepeatedCall => format!(
"`{}` repeated a failed call",
self.tool.as_deref().unwrap_or_default()
),
StallKind::RepeatedFailure => format!(
"`{}` failed the same way {} times",
self.tool.as_deref().unwrap_or_default(),
self.count
),
StallKind::RepeatedReads => format!("{} repeated reads", self.count),
};
format!("\n[stall] {what}; reminded the model\n")
}
}
pub fn signature(observation: &str) -> u64 {
static VOLATILE: OnceLock<Regex> = OnceLock::new();
let volatile = VOLATILE.get_or_init(|| {
Regex::new(r"(?i)\b\d+(?:\.\d+)?\s?(?:s|ms|sec|secs|seconds)\b|0x[0-9a-f]+")
.expect("valid regex")
});
let masked = volatile.replace_all(observation.trim(), "#");
let mut hasher = DefaultHasher::new();
masked.hash(&mut hasher);
hasher.finish()
}
fn arguments_key(call: &ToolCall) -> String {
fn sorted(value: &Value) -> Value {
match value {
Value::Object(map) => {
let mut keys: Vec<&String> = map.keys().collect();
keys.sort();
Value::Object(
keys.into_iter()
.map(|key| (key.clone(), sorted(&map[key])))
.collect(),
)
}
Value::Array(items) => Value::Array(items.iter().map(sorted).collect()),
other => other.clone(),
}
}
serde_json::to_string(&sorted(&Value::Object(call.arguments.clone()))).unwrap_or_default()
}
#[derive(Debug, Default)]
pub struct StallWatch {
enabled: bool,
changes: u32,
failed_calls: HashMap<(String, String), (u64, u32)>,
failures: HashMap<(String, u64), u32>,
read_calls: HashMap<(String, String), u32>,
repeated_reads: u32,
nudged: Vec<StallKind>,
pending: Option<Stall>,
}
impl StallWatch {
pub fn new(enabled: bool) -> Self {
Self {
enabled,
..Self::default()
}
}
pub fn saw(&mut self, call: &ToolCall, result: &ToolResult, observation: &str) {
if !self.enabled {
return;
}
let name = call.name.as_str();
if READ_TOOLS.contains(&name) && result.ok {
let key = (name.to_owned(), arguments_key(call));
if self.read_calls.insert(key, self.changes) == Some(self.changes) {
self.repeated_reads += 1;
if self.repeated_reads >= REPEATED_READS {
self.raise(StallKind::RepeatedReads, Some(name), self.repeated_reads);
}
}
return;
}
if result.ok {
if crate::verify::CHANGING_TOOLS.contains(&name) {
self.changes += 1;
self.repeated_reads = 0;
}
return;
}
let sig = signature(observation);
let key = (name.to_owned(), arguments_key(call));
let previous = self.failed_calls.insert(key, (sig, self.changes));
if previous == Some((sig, self.changes)) {
self.raise(StallKind::RepeatedCall, Some(name), 2);
}
let count = self.failures.entry((name.to_owned(), sig)).or_default();
*count += 1;
let count = *count;
if count >= REPEATED_FAILURES {
self.raise(StallKind::RepeatedFailure, Some(name), count);
}
}
fn raise(&mut self, kind: StallKind, tool: Option<&str>, count: u32) {
if self.nudged.contains(&kind) || self.pending.is_some() {
return;
}
self.nudged.push(kind);
self.pending = Some(Stall {
kind,
tool: tool.map(str::to_owned),
count,
});
}
pub fn take(&mut self) -> Option<Stall> {
self.pending.take()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn call(name: &str, arguments: Value) -> ToolCall {
ToolCall {
id: "c".into(),
name: name.into(),
arguments: arguments.as_object().unwrap().clone(),
}
}
fn result(ok: bool) -> ToolResult {
ToolResult {
call_id: "c".into(),
name: "t".into(),
ok,
content: String::new(),
error: None,
metadata: Default::default(),
}
}
fn tests_run() -> ToolCall {
call("run_command", json!({"command": "python -m unittest"}))
}
#[test]
fn the_same_failed_call_with_nothing_changed_is_a_repeated_call() {
let mut watch = StallWatch::new(true);
watch.saw(&tests_run(), &result(false), "FAILED in 0.003s");
assert_eq!(watch.take(), None);
watch.saw(&tests_run(), &result(false), "FAILED in 0.010s");
let stall = watch.take().unwrap();
assert_eq!(stall.kind, StallKind::RepeatedCall);
assert_eq!(stall.tool.as_deref(), Some("run_command"));
assert!(stall
.note()
.starts_with("[stall] That call to `run_command`"));
watch.saw(&tests_run(), &result(false), "FAILED in 0.010s");
assert_eq!(
watch.take().map(|s| s.kind),
Some(StallKind::RepeatedFailure)
);
watch.saw(&tests_run(), &result(false), "FAILED in 0.010s");
assert_eq!(watch.take(), None);
}
#[test]
fn fixes_that_leave_the_failure_unchanged_are_a_repeated_failure() {
let mut watch = StallWatch::new(true);
let edit = call("edit_file", json!({"path": "money.py"}));
for _ in 0..2 {
watch.saw(&tests_run(), &result(false), "AssertionError: 2.67 != 2.68");
watch.saw(&edit, &result(true), "ok");
assert_eq!(watch.take(), None);
}
watch.saw(&tests_run(), &result(false), "AssertionError: 2.67 != 2.68");
let stall = watch.take().unwrap();
assert_eq!(stall.kind, StallKind::RepeatedFailure);
assert_eq!(stall.count, 3);
assert_eq!(
stall.notice(),
"\n[stall] `run_command` failed the same way 3 times; reminded the model\n"
);
}
#[test]
fn a_failure_that_changes_is_progress() {
let mut watch = StallWatch::new(true);
let edit = call("edit_file", json!({"path": "money.py"}));
for failures in 3..6 {
watch.saw(
&tests_run(),
&result(false),
&format!("FAILED (failures={failures})"),
);
watch.saw(&edit, &result(true), "ok");
}
assert_eq!(watch.take(), None);
watch.saw(&tests_run(), &result(false), "same");
watch.saw(&edit, &result(true), "ok");
watch.saw(&tests_run(), &result(false), "same");
assert_eq!(watch.take(), None);
}
#[test]
fn new_reads_never_count_however_many() {
let mut watch = StallWatch::new(true);
for index in 0..40 {
let read = call("read_file", json!({"path": format!("shop/m{index}.py")}));
watch.saw(&read, &result(true), "text");
}
assert_eq!(watch.take(), None);
}
#[test]
fn reading_the_same_thing_again_with_nothing_changed_is_repeated_reads() {
let mut watch = StallWatch::new(true);
let read = call("read_file", json!({"path": "a.py"}));
let search = call("search_files", json!({"pattern": "x", "path": "."}));
watch.saw(&read, &result(true), "text");
watch.saw(&search, &result(true), "text");
watch.saw(
&call("edit_file", json!({"path": "a.py"})),
&result(true),
"ok",
);
watch.saw(&read, &result(true), "text");
watch.saw(&search, &result(true), "text");
watch.saw(
&call("read_file", json!({"path": "b.py"})),
&result(true),
"text",
);
watch.saw(&read, &result(true), "text");
watch.saw(&search, &result(true), "text");
assert_eq!(watch.take(), None);
let reordered = call("search_files", json!({"path": ".", "pattern": "x"}));
watch.saw(&reordered, &result(true), "text");
let stall = watch.take().unwrap();
assert_eq!(stall.kind, StallKind::RepeatedReads);
assert_eq!(stall.count, REPEATED_READS);
assert_eq!(stall.tool.as_deref(), Some("search_files"));
assert_eq!(
stall.notice(),
"\n[stall] 3 repeated reads; reminded the model\n"
);
assert!(stall
.note()
.starts_with("[stall] You have read the same thing again 3 times"));
watch.saw(&read, &result(true), "text");
assert_eq!(watch.take(), None);
}
#[test]
fn the_same_call_in_another_key_order_is_still_a_repeat() {
let mut watch = StallWatch::new(true);
let first = call(
"edit_file",
json!({"new_string": "x", "old_string": "x", "path": "t.py"}),
);
let second = call(
"edit_file",
json!({"path": "t.py", "old_string": "x", "new_string": "x"}),
);
let error = "old_string and new_string are identical; nothing to do.";
watch.saw(&first, &result(false), error);
watch.saw(&second, &result(false), error);
assert_eq!(watch.take().map(|s| s.kind), Some(StallKind::RepeatedCall));
}
#[test]
fn a_watch_that_is_off_sees_nothing() {
let mut watch = StallWatch::new(false);
for _ in 0..5 {
watch.saw(&tests_run(), &result(false), "same");
}
assert_eq!(watch.take(), None);
}
#[test]
fn signatures_ignore_timings_and_addresses_only() {
assert_eq!(
signature("Ran 3 tests in 0.003s\nFAILED"),
signature("Ran 3 tests in 1.250s\nFAILED")
);
assert_eq!(signature("<obj at 0x7f3a>"), signature("<obj at 0x91bc>"));
assert_ne!(signature("failures=2"), signature("failures=1"));
}
}