use crate::llm::{ChatMessage, ToolCall};
use crate::tools::BASH_TOOL_NAME;
use crate::verify_gate::{bash_command_from_call, is_test_command};
pub const REDUNDANT_RERUN_MESSAGE: &str = "skipped: this exact test command already passed \
earlier in this session and no code has changed since (no write_file/edit and no other shell \
command ran between that passing run and this one). Re-running the identical command would \
produce the same result and cost a turn. The prior clean pass still stands — proceed to finish \
the task, or run a DIFFERENT (e.g. broader) command if you need to verify something the passing \
run did not cover.";
const READ_ONLY_TOOLS: &[&str] = &[
"read_file",
"grep",
"glob",
"list_dir",
"search_code",
"recall_session",
];
fn test_result_passed(result_content: &str) -> bool {
result_content.trim_end().ends_with("exit_code: 0")
}
fn call_result_passed(messages: &[ChatMessage], call_id: &str) -> bool {
messages
.iter()
.find(|m| m.role == "tool" && m.tool_call_id.as_deref() == Some(call_id))
.and_then(|m| m.content.as_deref())
.map(test_result_passed)
.unwrap_or(false)
}
fn fold_call(baseline: &mut Option<String>, call: &ToolCall, messages: &[ChatMessage]) {
if let Some(command) = bash_command_from_call(call) {
*baseline = (is_test_command(&command) && call_result_passed(messages, &call.id))
.then_some(command);
return;
}
if READ_ONLY_TOOLS.contains(&call.function.name.as_str()) {
return;
}
*baseline = None;
}
fn same_test_command(baseline: &str, pending: &str) -> bool {
baseline.trim() == pending.trim()
}
pub fn is_redundant_test_rerun(messages: &[ChatMessage], current_call_id: &str) -> bool {
let mut baseline: Option<String> = None;
for message in messages {
if message.role != "assistant" {
continue;
}
let Some(calls) = message.tool_calls.as_ref() else {
continue;
};
for call in calls {
if call.id == current_call_id {
return match (baseline.as_deref(), bash_command_from_call(call)) {
(Some(passed), Some(pending)) => same_test_command(passed, &pending),
_ => false,
};
}
fold_call(&mut baseline, call, messages);
}
}
false
}
pub fn is_test_bash_call(call: &ToolCall) -> bool {
call.function.name == BASH_TOOL_NAME
&& bash_command_from_call(call).is_some_and(|c| is_test_command(&c))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::llm::FunctionCall;
fn bash_call(id: &str, command: &str) -> ToolCall {
ToolCall {
id: id.into(),
kind: "function".into(),
function: FunctionCall {
name: BASH_TOOL_NAME.into(),
arguments: serde_json::json!({ "command": command }).to_string(),
},
}
}
fn tool_call(id: &str, name: &str) -> ToolCall {
ToolCall {
id: id.into(),
kind: "function".into(),
function: FunctionCall {
name: name.into(),
arguments: "{}".into(),
},
}
}
fn assistant(calls: Vec<ToolCall>) -> ChatMessage {
ChatMessage {
role: "assistant".into(),
content: None,
images: vec![],
tool_calls: Some(calls),
tool_call_id: None,
name: None,
cache_control: None,
}
}
fn bash_result(call_id: &str, code: i32) -> ChatMessage {
ChatMessage::tool_result(
call_id,
BASH_TOOL_NAME,
format!("stdout:\nok\nexit_code: {code}"),
)
}
#[test]
fn exit_zero_reads_as_pass() {
assert!(test_result_passed("stdout:\n5 passed\nexit_code: 0"));
assert!(test_result_passed("exit_code: 0\n"));
}
#[test]
fn nonzero_and_missing_exit_read_as_not_pass() {
assert!(!test_result_passed("stderr:\nFAILED\nexit_code: 1"));
assert!(!test_result_passed("exit_code: 10"));
assert!(!test_result_passed(REDUNDANT_RERUN_MESSAGE));
assert!(!test_result_passed(""));
}
#[test]
fn passing_prior_run_makes_rerun_redundant() {
let messages = vec![
assistant(vec![bash_call("t1", "cargo test -p trusty-code")]),
bash_result("t1", 0),
assistant(vec![bash_call("t2", "cargo test -p trusty-code")]),
];
assert!(is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn different_test_command_after_pass_is_not_redundant() {
let messages = vec![
assistant(vec![bash_call("t1", "cargo test -p a")]),
bash_result("t1", 0),
assistant(vec![bash_call("t2", "cargo test -p b")]),
];
assert!(!is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn compile_only_pass_does_not_suppress_real_run() {
let messages = vec![
assistant(vec![bash_call("t1", "cargo test --no-run")]),
bash_result("t1", 0),
assistant(vec![bash_call("t2", "cargo test")]),
];
assert!(!is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn outer_whitespace_does_not_defeat_identity() {
let messages = vec![
assistant(vec![bash_call("t1", "cargo test")]),
bash_result("t1", 0),
assistant(vec![bash_call("t2", " cargo test ")]),
];
assert!(is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn failing_prior_run_is_not_redundant() {
let messages = vec![
assistant(vec![bash_call("t1", "cargo test")]),
bash_result("t1", 1),
assistant(vec![bash_call("t2", "cargo test")]),
];
assert!(!is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn edit_between_runs_defeats_suppression() {
let messages = vec![
assistant(vec![bash_call("t1", "cargo test")]),
bash_result("t1", 0),
assistant(vec![tool_call("e1", "edit")]),
ChatMessage::tool_result("e1", "edit", "edited"),
assistant(vec![bash_call("t2", "cargo test")]),
];
assert!(!is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn read_only_activity_keeps_run_redundant() {
let messages = vec![
assistant(vec![bash_call("t1", "pytest tests/ -v")]),
bash_result("t1", 0),
assistant(vec![tool_call("r1", "read_file"), tool_call("g1", "grep")]),
ChatMessage::tool_result("r1", "read_file", "..."),
ChatMessage::tool_result("g1", "grep", "..."),
assistant(vec![bash_call("t2", "pytest tests/ -v")]),
];
assert!(is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn non_test_bash_between_runs_defeats_suppression() {
let messages = vec![
assistant(vec![bash_call("t1", "cargo test")]),
bash_result("t1", 0),
assistant(vec![bash_call("b1", "git checkout .")]),
bash_result("b1", 0),
assistant(vec![bash_call("t2", "cargo test")]),
];
assert!(!is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn no_prior_run_is_not_redundant() {
let messages = vec![assistant(vec![bash_call("t1", "cargo test")])];
assert!(!is_redundant_test_rerun(&messages, "t1"));
}
#[test]
fn same_turn_edit_before_rerun_defeats_suppression() {
let messages = vec![
assistant(vec![bash_call("t1", "cargo test")]),
bash_result("t1", 0),
assistant(vec![tool_call("e1", "edit"), bash_call("t2", "cargo test")]),
];
assert!(!is_redundant_test_rerun(&messages, "t2"));
}
#[test]
fn is_test_bash_call_true_only_for_test_bash() {
assert!(is_test_bash_call(&bash_call("a", "cargo test")));
assert!(!is_test_bash_call(&bash_call("b", "git status")));
assert!(!is_test_bash_call(&tool_call("c", "write_file")));
}
#[test]
fn suppression_message_is_informative() {
assert!(REDUNDANT_RERUN_MESSAGE.contains("already passed"));
assert!(REDUNDANT_RERUN_MESSAGE.contains("proceed to finish"));
}
}