Skip to main content

Module loop_guard

Module loop_guard 

Source
Expand description

Loop detection — catches runaway repeated tool calls. Loop detection for the inference loop.

Modeled after Gemini CLI’s approach: simple consecutive-identical-call detection + feedback injection instead of hard stops. No windowed fingerprinting, no name saturation heuristics, no tool-only suppression.

§Design philosophy

Claude Code and Codex have zero loop detection — they trust the model. Gemini CLI has the only thoughtful approach: detect consecutive identical tool calls (same name + args), then inject a “take a step back” feedback message to nudge the model out of the loop. Hard-stop only on the 2nd detection (model ignored the feedback).

§What we DON’T do (and why)

  • No windowed fingerprint tracking — nobody else does this.
  • No tool-name saturation — editing 12 files in a refactoring is normal.
  • No tool-only response suppression — efficient models work silently.
  • No per-turn tool call cap — frontier models emit 30+ parallel calls.
  • No deduplication — if a model emits 66 identical calls, the user should see that and switch models, not have us silently paper over it.

§What we DO

  1. Consecutive identical calls — same (tool, args) called CONSECUTIVE_REPEAT_THRESHOLD times in a row → inject feedback.
  2. Hard iteration cap — absolute ceiling on loop iterations. User can extend interactively.

Structs§

LoopDetector
Tracks consecutive identical tool calls.

Enums§

LoopAction
What to do when a loop is detected.
LoopContinuation
Options for continuing after hitting the hard cap.

Constants§

MAX_ITERATIONS_DEFAULT
Default hard cap for the main inference loop.
MAX_SUB_AGENT_ITERATIONS
Hard cap for sub-agent loops.