1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//! Execution guard gates for `execute_tool_ref_internal`.
//!
//! Fast-reuse and identical-call loop detection run before policy checks so
//! repeated read-only calls short-circuit without another round-trip.
use std::time::Duration;
use serde_json::Value;
use super::*;
use crate::tools::registry::execution_history::HarnessContextSnapshot;
/// When a read-only tool call has been repeated this many times, stop returning
/// cached results and return a hard error instead. Must be greater than
/// MIN_READONLY_IDENTICAL_LIMIT (currently 2).
const LOOP_HARD_BLOCK_REPEAT_COUNT: usize = 5;
/// Failure recorder shared by execution guards (see `record_failure` in
/// `execute_tool_ref_internal`).
pub(super) type FailureRecorder<'a> = &'a (
dyn Fn(String, bool, Option<String>, Value, String, Option<String>, Option<u64>, Option<u64>, Option<u64>, bool)
+ Send
+ Sync
);
/// Mutable loop-gate state threaded through reuse/loop detection.
pub(super) struct LoopGate<'a> {
pub skip_loop_detection: bool,
pub reusable_result: bool,
pub is_verification_command: bool,
pub fresh_patch_read: bool,
pub tool_name: &'a str,
pub display_name: &'a str,
pub tool_name_owned: &'a str,
pub args: &'a Value,
pub requested_name: &'a str,
pub args_for_recording: &'a Value,
pub context_snapshot: &'a HarnessContextSnapshot,
pub timeout_category_label: &'a Option<String>,
pub base_timeout_ms: Option<u64>,
pub adaptive_timeout_ms: Option<u64>,
pub record_failure: FailureRecorder<'a>,
}
/// How tool execution proceeds after the loop gate.
pub(super) enum LoopGateOutcome {
EarlyReturn(Value),
Proceed {
repeat_recovery_generation: Option<u64>,
loop_repeat_count: usize,
loop_limit: usize,
},
}
impl ToolRegistry {
/// Fast-reuse recent read-only results and block repeated identical calls.
pub(super) async fn check_loop_gate(&self, gate: LoopGate<'_>) -> Result<LoopGateOutcome> {
let LoopGate {
skip_loop_detection,
reusable_result,
is_verification_command,
fresh_patch_read,
tool_name,
display_name,
tool_name_owned,
args,
requested_name,
args_for_recording,
context_snapshot,
timeout_category_label,
base_timeout_ms,
adaptive_timeout_ms,
record_failure,
} = gate;
if skip_loop_detection {
trace!(
tool = %tool_name,
"Skipping identical-call loop detection for stateful exec continuation"
);
}
// FAST REUSE: Read-only inspection calls are often repeated verbatim within a
// single turn (e.g. `diff a b | wc -l`, `find ... | grep`). Reuse the most recent
// successful result immediately instead of paying for another round-trip and
// another spool file. This is gated by a short TTL and the read-only classification
// so mutating calls never take this path.
//
// Verification commands (`cargo check`, `cargo nextest run`, `cargo fmt
// --check`, and pure `&&` chains thereof) are read-only by intent but
// must always re-execute: reusing a stale success would clear the
// anti-blind-editing gate without verifying the current worktree, and
// reusing a stale failure would keep the gate pending after a fix.
// (`is_verification_command` is bound once at preview-budget
// resolution above; the stripped output-metadata field does not
// affect shell classification.)
if reusable_result && !is_verification_command && !skip_loop_detection && !fresh_patch_read {
let fast_reuse_max_age = Duration::from_secs(60);
let fast_reused = self
.execution_history
.find_recent_spooled_result(tool_name, args, fast_reuse_max_age)
.or_else(|| {
self.execution_history
.find_recent_successful_result(tool_name, args, fast_reuse_max_age)
});
if let Some(mut reused_value) = fast_reused {
if let Some(obj) = reused_value.as_object_mut() {
obj.insert("reused_recent_result".into(), json!(true));
obj.insert("tool".into(), json!(display_name));
let reused_spooled = obj.get("spool_path").and_then(|v| v.as_str()).is_some();
let note = if reused_spooled {
"Reusing a recent spooled output for this identical read-only call. Continue from the spool file instead of re-running the tool."
} else {
"Reusing a recent successful output for this identical read-only call."
};
obj.insert("reused_result_note".into(), json!(note));
}
// Record a synthetic "reused" entry so subsequent
// `detect_loop` / `find_recent_*` queries see this call in the
// history. Previously the fast-reuse path returned the
// cached payload without recording, which meant `detect_loop`
// could not account for the reused call — leading to
// undercounting on the very next turn and silent cache
// poisoning.
//
// We pass `is_mcp_tool = false` and `mcp_provider = None`
// because the fast-reuse gate has already filtered for
// read-only calls and we don't have those locals in scope at
// this point in the function. The cached record itself
// (added when the original call ran) carries the true MCP
// metadata; the synthetic record exists only to make
// `detect_loop` see this call in the rolling window.
self.execution_history.add_record(ToolExecutionRecord::success(
tool_name.to_owned(),
requested_name.to_owned(),
false,
None,
args_for_recording.clone(),
reused_value.clone(),
context_snapshot.clone(),
timeout_category_label.clone(),
base_timeout_ms,
adaptive_timeout_ms,
None,
false,
));
trace!(
tool = %tool_name,
"Fast-reusing recent successful read-only result"
);
self.observe_source_output(tool_name, args, &reused_value).await;
return Ok(LoopGateOutcome::EarlyReturn(reused_value));
}
}
// LOOP DETECTION: Check if we're calling the same tool repeatedly with identical params
let loop_limit = if skip_loop_detection {
0
} else {
self.execution_history.loop_limit_for(tool_name, args)
};
let loop_result = if skip_loop_detection {
crate::tools::registry::execution_history::LoopDetectionResult {
detected: false,
repeat_count: 0,
tool_name: tool_name.to_owned(),
}
} else {
self.execution_history.detect_loop(tool_name, args)
};
let repeat_recovery_generation =
if loop_limit > 0 && loop_result.detected && loop_result.repeat_count >= loop_limit {
self.completion_verifier_repeat_recovery_generation(tool_name, args)
} else {
None
};
if loop_result.detected && loop_result.repeat_count > 1 {
let delay_ms = (LOOP_THROTTLE_REGISTRY_BASE_MS * loop_result.repeat_count as u64).min(LOOP_THROTTLE_MAX_MS);
if delay_ms > 0 {
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
}
}
if loop_limit > 0 && loop_result.detected {
warn!(
tool = %tool_name,
repeats = loop_result.repeat_count,
"Loop detected: agent calling same tool with identical parameters {} times",
loop_result.repeat_count
);
if loop_result.repeat_count >= loop_limit && repeat_recovery_generation.is_none() {
// Hard block: when the model has been repeating the same call far
// beyond the limit, stop returning cached results and return an
// error. Returning a cached result at high repeat counts causes
// the model to see "success" and keep retrying.
let hard_block = loop_result.repeat_count >= LOOP_HARD_BLOCK_REPEAT_COUNT;
if reusable_result && !is_verification_command && !hard_block && !fresh_patch_read {
let reuse_max_age = Duration::from_secs(120);
let reused = self
.execution_history
.find_recent_spooled_result(tool_name, args, reuse_max_age)
.or_else(|| {
self.execution_history
.find_recent_successful_result(tool_name, args, reuse_max_age)
});
if let Some(mut reused_value) = reused {
if let Some(obj) = reused_value.as_object_mut() {
obj.insert("reused_recent_result".into(), json!(true));
obj.insert("loop_detected".into(), json!(true));
obj.insert("repeat_count".into(), json!(loop_result.repeat_count));
obj.insert("limit".into(), json!(loop_limit));
obj.insert("tool".into(), json!(display_name));
let reused_spooled = obj.get("spool_path").and_then(|v| v.as_str()).is_some();
let note = if reused_spooled {
"Loop detected: this identical read-only call has been repeated, so the earlier result was reused. The full output is in the spool file and the conversation history; further repeats return an error instead."
} else {
"Loop detected: this identical read-only call has been repeated with no new information, so the earlier result was reused. It is already in the conversation history; further repeats return an error instead."
};
obj.insert("loop_detected_note".into(), json!(note));
}
self.observe_source_output(tool_name, args, &reused_value).await;
return Ok(LoopGateOutcome::EarlyReturn(reused_value));
}
}
let delay_ms =
(LOOP_THROTTLE_REGISTRY_BASE_MS * loop_result.repeat_count as u64).min(LOOP_THROTTLE_MAX_MS);
if delay_ms > 0 {
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
}
let payload =
loop_detection_block_payload(tool_name_owned, display_name, loop_result.repeat_count, loop_limit);
record_failure(
tool_name_owned.to_owned(),
false,
None,
args_for_recording.clone(),
"Tool call blocked due to repeated identical invocations".to_string(),
timeout_category_label.clone(),
base_timeout_ms,
adaptive_timeout_ms,
None,
false,
);
return Ok(LoopGateOutcome::EarlyReturn(payload));
}
}
Ok(LoopGateOutcome::Proceed {
repeat_recovery_generation,
loop_repeat_count: loop_result.repeat_count,
loop_limit,
})
}
/// Enter the tool reentrancy guard, rejecting recursive invocation.
pub(super) fn check_tool_reentrancy(
&self,
tool_name: &str,
display_name: &str,
prevalidated: bool,
args: &Value,
args_for_recording: &Value,
record_failure: FailureRecorder<'_>,
) -> Result<ToolReentrancyGuard> {
let allow_parallel_sibling = prevalidated && tool_intent::is_parallel_safe_call(tool_name, args);
match ToolReentrancyGuard::enter(tool_name, allow_parallel_sibling) {
Ok(guard) => Ok(guard),
Err(violation) => {
let reentry_count = violation.tool_reentry_count + 1;
let error_message = format!(
"Reentrancy guard: tool '{}' is already running in this call stack, so this recursive call was blocked. \
Repeating the same call is blocked the same way; change the control flow or use a different tool.\n\
Current stack depth: {}. Re-entry count for this tool in the current task: {}.\n\
Stack trace: {}",
display_name, violation.stack_depth, reentry_count, violation.stack_trace
);
let error = ToolExecutionError::new(
tool_name.to_owned(),
ToolErrorType::PolicyViolation,
error_message.clone(),
);
let mut payload = error.to_json_value();
if let Some(obj) = payload.as_object_mut() {
obj.insert("reentrant_call_blocked".into(), json!(true));
obj.insert("stack_depth".into(), json!(violation.stack_depth));
obj.insert("reentry_count".into(), json!(reentry_count));
obj.insert("tool".into(), json!(display_name));
obj.insert("stack_trace".into(), json!(violation.stack_trace));
}
record_failure(
tool_name.to_owned(),
false,
None,
args_for_recording.clone(),
error_message.clone(),
None,
None,
None,
None,
false,
);
Err(anyhow!(error_message).context("tool reentrancy blocked"))
}
}
}
/// Validate the resolved call and enforce planning-workflowmutating-tool rules.
pub(super) fn validate_preflight_and_planning(
&self,
tool_name: &str,
display_name: &str,
args: &Value,
prevalidated: bool,
args_for_recording: &Value,
record_failure: FailureRecorder<'_>,
) -> Result<bool> {
// Classify the tool intent once: the prevalidated fast path skips full
// preflight and classifies here; the full-preflight path reuses the
// intent the kernel already computed on the validated (executed) args,
// so planning enforcement below cannot disagree with
// `readonly_classification`.
let (intent, readonly_classification) = if prevalidated {
#[cfg(debug_assertions)]
{
if let Err(err) = execution_kernel::preflight_validate_resolved_call(self, tool_name, args)
&& !agent_execution::is_planning_active_denial(&err.to_string())
{
debug_assert!(false, "prevalidated execution received invalid call for '{tool_name}': {err}");
}
}
let intent = tool_intent::classify_tool_intent(tool_name, args);
(intent, !intent.mutating)
} else {
match execution_kernel::preflight_validate_resolved_call(self, tool_name, args) {
Ok(outcome) => (outcome.intent, outcome.readonly_classification),
Err(err) => {
let err_msg = err.to_string();
record_failure(
tool_name.to_owned(),
false,
None,
args_for_recording.clone(),
err_msg,
None,
None,
None,
None,
false,
);
return Err(err);
}
}
};
if readonly_classification {
trace!(tool = %tool_name, "Validation classified tool as read-only");
}
// Defense-in-depth: prevalidated fast path skips full preflight, but planning workflow
// mutating-tool enforcement remains a hard safety invariant. Reuse the already-computed
// `intent` so we don't reclassify on the hot path.
if self.is_planning_active() && !self.is_planning_active_allowed_with_intent(tool_name, args, &intent) {
let error_msg = agent_execution::planning_workflow_denial_message(display_name);
record_failure(
tool_name.to_owned(),
false,
None,
args_for_recording.clone(),
error_msg.clone(),
None,
None,
None,
None,
false,
);
return Err(anyhow!(error_msg).context(agent_execution::PLANNING_DENIED_CONTEXT));
}
Ok(readonly_classification)
}
}