use std::collections::HashMap;
use std::sync::Mutex;
use async_trait::async_trait;
use crate::engine::middleware::{Middleware, PostLlmCtx, UserMessageCtx};
use crate::types::{AgentResult, SessionId};
pub const DEFAULT_NUDGE_AFTER: usize = 5;
pub const DEFAULT_BLOCK_AFTER: usize = 10;
#[derive(Clone, Debug)]
pub struct RepeatToolLimitConfig {
pub nudge_after: usize,
pub block_after: usize,
pub nudge_message: String,
pub block_message: String,
}
impl Default for RepeatToolLimitConfig {
fn default() -> Self {
Self {
nudge_after: DEFAULT_NUDGE_AFTER,
block_after: DEFAULT_BLOCK_AFTER,
nudge_message: "You have issued the exact same tool call with the same arguments \
repeatedly. Identical repetition cannot change the outcome: if the call keeps \
failing, fix the payload or take a different action; if the work is done, \
write your response and end the turn."
.to_string(),
block_message: "You have issued this identical tool call far too many times. The \
pending calls were discarded. Based on everything you already know, write your \
response and end the turn. Do not call this tool again."
.to_string(),
}
}
}
pub struct RepeatToolLimitMiddleware {
config: RepeatToolLimitConfig,
counts: Mutex<HashMap<SessionId, HashMap<String, usize>>>,
}
impl RepeatToolLimitMiddleware {
pub fn new(config: RepeatToolLimitConfig) -> Self {
Self {
config,
counts: Mutex::new(HashMap::new()),
}
}
fn bump(&self, session_id: &SessionId, fingerprint: &str) -> usize {
let mut counts = self.counts.lock().unwrap();
let session_counts = counts.entry(session_id.clone()).or_default();
let entry = session_counts.entry(fingerprint.to_string()).or_insert(0);
*entry += 1;
*entry
}
}
#[async_trait]
impl Middleware for RepeatToolLimitMiddleware {
async fn on_user_message(&self, ctx: &mut UserMessageCtx) -> AgentResult<()> {
self.counts.lock().unwrap().remove(&ctx.session_id);
Ok(())
}
async fn on_post_llm(&self, ctx: &mut PostLlmCtx) -> AgentResult<()> {
if ctx.actions().is_empty() {
return Ok(());
}
let mut max_count = 0usize;
let mut offending_tool = String::new();
for action in ctx.actions() {
let count = self.bump(&ctx.session_id, &action.identity);
if count > max_count {
max_count = count;
offending_tool = action.name.clone();
}
}
if max_count >= self.config.block_after {
tracing::warn!(
session_id = ctx.session_id.id,
tool = %offending_tool,
count = max_count,
pending_calls = ctx.actions().len(),
"RepeatToolLimit: blocking tool calls — identical-call limit reached"
);
ctx.demote_to_text();
ctx.follow_up_message = Some(self.config.block_message.clone());
} else if self.config.nudge_after > 0 && max_count == self.config.nudge_after {
tracing::info!(
session_id = ctx.session_id.id,
tool = %offending_tool,
count = max_count,
"RepeatToolLimit: nudging — identical-call threshold reached"
);
ctx.follow_up_message = Some(self.config.nudge_message.clone());
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::executor::ParseOutcome;
use crate::types::{Action, ActionFormat, FinishReason};
fn mw() -> RepeatToolLimitMiddleware {
RepeatToolLimitMiddleware::new(RepeatToolLimitConfig::default())
}
fn ctx(session: u64, calls: Vec<(&str, &str)>) -> PostLlmCtx {
let actions = calls
.into_iter()
.enumerate()
.map(|(i, (name, args))| Action {
id: format!("call_{i}"),
name: name.to_string(),
args: serde_json::Value::String(args.to_string()),
identity: Action::tool_call_identity(name, args),
format: ActionFormat::ToolCall,
})
.collect::<Vec<Action>>();
let parse = if actions.is_empty() {
ParseOutcome::Empty
} else {
ParseOutcome::Actions(actions)
};
PostLlmCtx {
session_id: SessionId::new(session),
full_text: String::new(),
parse,
available_tools: vec![],
turn_count: 1,
total_actions: 0,
nudge_count: 0,
turn_actions: 0,
skip_push: false,
follow_up_message: None,
finish_reason: FinishReason::ToolUse,
}
}
async fn call(mw: &RepeatToolLimitMiddleware, session: u64, calls: Vec<(&str, &str)>) {
let mut c = ctx(session, calls);
mw.on_post_llm(&mut c).await.unwrap();
}
#[tokio::test]
async fn interleaved_identical_calls_still_count() {
let mw = mw();
for _ in 0..4 {
call(
&mw,
1,
vec![("list_agents", "{}"), ("read_file", r#"{"path":"a.rs"}"#)],
)
.await;
}
let mut c = ctx(
1,
vec![("list_agents", "{}"), ("read_file", r#"{"path":"b.rs"}"#)],
);
mw.on_post_llm(&mut c).await.unwrap();
assert!(c.follow_up_message.is_some(), "5th identical call nudges");
assert!(
!c.actions().is_empty() && c.has_action_intent(),
"nudge is soft — calls still execute"
);
}
#[tokio::test]
async fn nudge_fires_only_once_per_fingerprint() {
let mw = mw();
let mut fired = 0;
for _ in 0..8 {
let mut c = ctx(1, vec![("list_agents", "{}")]);
mw.on_post_llm(&mut c).await.unwrap();
if c.follow_up_message.is_some() {
fired += 1;
}
}
assert_eq!(fired, 1, "nudge must fire exactly once, got {fired}");
}
#[tokio::test]
async fn blocks_at_threshold_and_discards_pending_calls() {
let mw = mw();
for _ in 0..9 {
call(&mw, 1, vec![("list_agents", "{}")]).await;
}
let mut c = ctx(
1,
vec![("list_agents", "{}"), ("read_file", r#"{"path":"x"}"#)],
);
mw.on_post_llm(&mut c).await.unwrap();
assert!(c.actions().is_empty(), "pending calls discarded");
assert!(!c.has_action_intent(), "response demoted to text-only");
assert!(c.follow_up_message.is_some(), "block carries a follow-up");
}
#[tokio::test]
async fn different_args_are_different_fingerprints() {
let mw = mw();
for i in 0..20 {
call(
&mw,
1,
vec![("read_file", &format!(r#"{{"path":"f{i}.rs"}}"#))],
)
.await;
let mut probe = ctx(
1,
vec![("read_file", &format!(r#"{{"path":"probe{i}.rs"}}"#))],
);
mw.on_post_llm(&mut probe).await.unwrap();
assert!(
probe.follow_up_message.is_none() && probe.has_action_intent(),
"legitimate distinct calls must never be limited"
);
}
}
#[tokio::test]
async fn same_args_reordered_keys_share_fingerprint() {
let mw = mw();
for _ in 0..4 {
call(&mw, 1, vec![("wait_agent", r#"{"name":"a","timeout":5}"#)]).await;
}
let mut c = ctx(1, vec![("wait_agent", r#"{"timeout":5,"name":"a"}"#)]);
mw.on_post_llm(&mut c).await.unwrap();
assert!(c.follow_up_message.is_some(), "canonical args must unify");
}
#[tokio::test]
async fn counting_key_is_the_parse_time_identity() {
fn action_with(id: &str, name: &str, args: &str, identity: &str) -> Action {
Action {
id: id.to_string(),
name: name.to_string(),
args: serde_json::Value::String(args.to_string()),
identity: identity.to_string(),
format: ActionFormat::ToolCall,
}
}
fn post(actions: Vec<Action>) -> PostLlmCtx {
let parse = if actions.is_empty() {
ParseOutcome::Empty
} else {
ParseOutcome::Actions(actions)
};
PostLlmCtx {
session_id: SessionId::new(1),
full_text: String::new(),
parse,
available_tools: vec![],
turn_count: 1,
total_actions: 0,
nudge_count: 0,
turn_actions: 0,
skip_push: false,
follow_up_message: None,
finish_reason: FinishReason::ToolUse,
}
}
let mw1 = mw();
for i in 0..4 {
let mut c = post(vec![action_with(&format!("c{i}"), "a", "{}", "shared")]);
mw1.on_post_llm(&mut c).await.unwrap();
assert!(c.follow_up_message.is_none(), "below threshold");
}
let mut c = post(vec![action_with(
"c4",
"totally_different",
r#"{"x":1}"#,
"shared",
)]);
mw1.on_post_llm(&mut c).await.unwrap();
assert!(
c.follow_up_message.is_some(),
"same identity counts together even across different names"
);
let mw2 = mw();
for i in 0..4 {
let mut c = post(vec![action_with(
&format!("d{i}"),
"b",
"{}",
&format!("distinct-{i}"),
)]);
mw2.on_post_llm(&mut c).await.unwrap();
assert!(
c.follow_up_message.is_none(),
"distinct identities do not stack"
);
}
let mut c = post(vec![action_with("d4", "b", "{}", "distinct-again")]);
mw2.on_post_llm(&mut c).await.unwrap();
assert!(
c.follow_up_message.is_none(),
"identity is the key — name+args alone do not accumulate"
);
}
#[tokio::test]
async fn on_user_message_resets_counts() {
let mw = mw();
for _ in 0..4 {
call(&mw, 1, vec![("list_agents", "{}")]).await;
}
mw.on_user_message(&mut UserMessageCtx {
session_id: SessionId::new(1),
user_input: "next run".to_string(),
})
.await
.unwrap();
for _ in 0..4 {
let mut c = ctx(1, vec![("list_agents", "{}")]);
mw.on_post_llm(&mut c).await.unwrap();
assert!(c.follow_up_message.is_none(), "counts must reset per run");
}
}
#[tokio::test]
async fn sessions_are_isolated() {
let mw = mw();
for _ in 0..4 {
call(&mw, 1, vec![("list_agents", "{}")]).await;
}
let mut other = ctx(2, vec![("list_agents", "{}")]);
mw.on_post_llm(&mut other).await.unwrap();
assert!(
other.follow_up_message.is_none(),
"session 2's first call must not see session 1's count"
);
}
}