use std::sync::OnceLock;
use regex::Regex;
fn tool_result_error_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"(?i)└\s*(?:error|exception):").expect("valid regex"))
}
pub fn strip_tool_result_errors(screen: &str) -> String {
screen
.lines()
.filter(|line| !tool_result_error_re().is_match(line))
.collect::<Vec<_>>()
.join("\n")
}
pub const DEFAULT_LINE_VERBS: [&str; 5] =
["traceback", "exception", "error:", "failed to", "panic"];
pub const CORE_FAILURE_MARKERS: [&str; 4] = ["traceback", "exception", "error:", "failed to"];
#[derive(Debug, Clone)]
pub struct FailureDetector {
line_verbs: &'static [&'static str],
words: &'static [&'static str],
anywhere: &'static [&'static str],
extra: &'static [&'static str],
ignore_tool_result_errors: bool,
}
impl FailureDetector {
pub const fn new() -> Self {
FailureDetector {
line_verbs: &DEFAULT_LINE_VERBS,
words: &[],
anywhere: &[],
extra: &[],
ignore_tool_result_errors: false,
}
}
pub const fn markers_only() -> Self {
FailureDetector {
line_verbs: &[],
words: &[],
anywhere: &CORE_FAILURE_MARKERS,
extra: &[],
ignore_tool_result_errors: false,
}
}
pub const fn line_verbs(mut self, verbs: &'static [&'static str]) -> Self {
self.line_verbs = verbs;
self
}
pub const fn words(mut self, words: &'static [&'static str]) -> Self {
self.words = words;
self
}
pub const fn anywhere(mut self, markers: &'static [&'static str]) -> Self {
self.anywhere = markers;
self
}
pub const fn extra(mut self, markers: &'static [&'static str]) -> Self {
self.extra = markers;
self
}
pub const fn ignoring_tool_result_errors(mut self) -> Self {
self.ignore_tool_result_errors = true;
self
}
pub fn detects(&self, screen: &str) -> bool {
let owned;
let subject = if self.ignore_tool_result_errors {
owned = strip_tool_result_errors(screen);
owned.as_str()
} else {
screen
};
let lowered = subject.to_lowercase();
if self
.anywhere
.iter()
.chain(self.extra)
.any(|m| lowered.contains(&m.to_lowercase()))
{
return true;
}
if self
.words
.iter()
.any(|w| contains_word(&lowered, &w.to_lowercase()))
{
return true;
}
lowered.lines().any(|line| {
let trimmed = line.trim_start();
self.line_verbs
.iter()
.any(|verb| starts_with_word(trimmed, &verb.to_lowercase()))
})
}
}
impl Default for FailureDetector {
fn default() -> Self {
FailureDetector::new()
}
}
fn starts_with_word(line: &str, verb: &str) -> bool {
let Some(rest) = line.strip_prefix(verb) else {
return false;
};
if !verb.ends_with(|c: char| c.is_alphanumeric()) {
return true;
}
!rest.starts_with(|c: char| c.is_alphanumeric() || c == '_')
}
fn contains_word(haystack: &str, word: &str) -> bool {
let is_word = |c: char| c.is_alphanumeric() || c == '_';
haystack.match_indices(word).any(|(i, _)| {
let before_ok = haystack[..i]
.chars()
.next_back()
.is_none_or(|c| !is_word(c));
let after_ok = haystack[i + word.len()..]
.chars()
.next()
.is_none_or(|c| !is_word(c));
before_ok && after_ok
})
}
pub fn count_markers(haystack: &str, markers: &[&str]) -> usize {
let lowered = haystack.to_lowercase();
markers
.iter()
.filter(|m| lowered.contains(&m.to_lowercase()))
.count()
}
pub fn all_markers(haystack: &str, markers: &[&str]) -> bool {
let lowered = haystack.to_lowercase();
markers.iter().all(|m| lowered.contains(&m.to_lowercase()))
}
pub fn any_marker(haystack: &str, markers: &[&str]) -> bool {
let lowered = haystack.to_lowercase();
markers.iter().any(|m| lowered.contains(&m.to_lowercase()))
}
pub fn marker_on_unrelated_line(screen: &str, marker: &str, excluding: &[&str]) -> bool {
screen
.lines()
.any(|line| line.contains(marker) && !excluding.iter().any(|e| line.contains(e)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn line_anchored_verbs_ignore_prose() {
let d = FailureDetector::new();
assert!(d.detects("Traceback (most recent call last):"));
assert!(d.detects(" error: something broke"));
assert!(!d.detects("I will explain what an exception is in Python."));
assert!(!d.detects("no errors were found"));
}
#[test]
fn line_verbs_respect_word_boundaries() {
let d = FailureDetector::new();
assert!(d.detects("panic: at the disco"));
assert!(d.detects("panic"));
assert!(!d.detects("panicking is unhelpful"));
assert!(!d.detects("tracebacks explained"));
assert!(d.detects("error: boom"));
}
#[test]
fn word_matching_sits_between_anchored_and_substring() {
const W: [&str; 1] = ["panic"];
let d = FailureDetector::markers_only().anywhere(&[]).words(&W);
assert!(
d.detects("the process hit a panic and died"),
"not line-anchored"
);
assert!(
!d.detects("panicking is unhelpful"),
"but still word-bounded"
);
}
#[test]
fn substring_markers_match_anywhere() {
const M: [&str; 1] = ["no results found"];
let d = FailureDetector::markers_only().anywhere(&M);
assert!(d.detects("Search returned: No Results Found."));
assert!(
!d.detects("a panic here"),
"markers_only drops the line-anchored verb set"
);
}
#[test]
fn the_two_strategies_compose() {
const M: [&str; 1] = ["unknown command"];
let d = FailureDetector::new().anywhere(&M);
assert!(d.detects("Unknown command: /nope"));
assert!(d.detects("panic: at the disco"));
assert!(!d.detects("all good"));
}
#[test]
fn tool_result_errors_can_be_excluded() {
const M: [&str; 1] = ["error:"];
let screen = "SFT Submission Status\nmemory_write └ error: disabled";
assert!(FailureDetector::markers_only().anywhere(&M).detects(screen));
assert!(
!FailureDetector::markers_only()
.anywhere(&M)
.ignoring_tool_result_errors()
.detects(screen),
"another tool failing is not this recipe's concern"
);
}
#[test]
fn anchoring_already_excludes_tool_result_errors() {
let screen = "SFT Submission Status\nmemory_write └ error: disabled";
assert!(!FailureDetector::new().detects(screen));
}
#[test]
fn a_custom_verb_set_replaces_the_default() {
const V: [&str; 1] = ["permission denied"];
let d = FailureDetector::new().line_verbs(&V);
assert!(d.detects("Permission denied: /etc/shadow"));
assert!(!d.detects("Traceback"), "default verbs were replaced");
}
#[test]
fn the_core_set_is_the_default_for_markers_only() {
let d = FailureDetector::markers_only();
assert!(d.detects("the tool reported error: boom"));
assert!(d.detects("Traceback (most recent call last)"));
assert!(!d.detects("all good"));
}
#[test]
fn extra_markers_add_to_the_core_rather_than_replacing_it() {
const E: [&str; 1] = ["no results found"];
let d = FailureDetector::markers_only().extra(&E);
assert!(d.detects("No results found"), "the extra marker");
assert!(d.detects("Traceback"), "and still the core set");
}
#[test]
fn markers_are_matched_case_insensitively_on_both_sides() {
assert!(any_marker("no results found", &["No Results Found"]));
assert!(any_marker("NO RESULTS FOUND", &["no results found"]));
assert_eq!(count_markers("Alpha Beta", &["ALPHA", "beta"]), 2);
assert!(all_markers("alpha beta", &["Alpha", "BETA"]));
const M: [&str; 1] = ["No Results Found"];
assert!(FailureDetector::markers_only()
.anywhere(&M)
.detects("search: no results found"));
}
#[test]
fn unrelated_line_matching_is_case_sensitive_on_purpose() {
let screen = "> Run echo hello_marker\nHELLO_MARKER\n";
assert!(!marker_on_unrelated_line(
screen,
"hello_marker",
&["Run echo"]
));
let exact = "> Run echo hello_marker\nhello_marker\n";
assert!(marker_on_unrelated_line(
exact,
"hello_marker",
&["Run echo"]
));
}
#[test]
fn marker_counting() {
assert_eq!(count_markers("Alpha Beta", &["alpha", "beta", "gamma"]), 2);
assert!(all_markers("Alpha Beta", &["alpha", "beta"]));
assert!(!all_markers("Alpha", &["alpha", "beta"]));
assert!(any_marker("Alpha", &["alpha", "beta"]));
assert!(!any_marker("Delta", &["alpha", "beta"]));
}
#[test]
fn prompt_echo_is_excluded_from_output_checks() {
let echo_only = "> Run echo hello_marker\n";
assert!(!marker_on_unrelated_line(
echo_only,
"hello_marker",
&["Run echo"]
));
let with_output = "> Run echo hello_marker\nhello_marker\n";
assert!(marker_on_unrelated_line(
with_output,
"hello_marker",
&["Run echo"]
));
}
}