feat: Add anti-loop / duplicate tool-call guard#82
feat: Add anti-loop / duplicate tool-call guard#82devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Four-layer protection against models (Kimi K2, etc.) repeating the same tool call in a tight loop: 1. Dedup guard — tracks last 5 calls (name + normalized args); blocks duplicates with a synthetic error. 2. Repeat counter — hard-caps identical calls at 3; aborts the turn on the 4th attempt. 3. Result diff check — if a tool returned the same payload and the model tries again, short-circuits with previous result summary. 4. Prompt injection — when any layer trips, injects a strong system reminder into the next buildSystemPrompt telling the model to stop. ToolCallGuard is a thread-safe singleton wired into SkillDispatcher.dispatch (the single chokepoint for MessagingVC, SubAgentRuntime, and BackgroundScheduler). State resets on each new user message / scheduled job. All thresholds are configurable via ToolCallGuard.Config (historyWindow, maxRepeats, per-layer enable flags). Co-Authored-By: bot_apk <apk@cognition.ai>
Original prompt from API User
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Summary
Prevents models (Kimi K2, etc.) from burning tokens/API quota by repeating the same tool call in a tight loop. Adds a new
ToolCallGuardsingleton with 4 layers of protection, wired into the single dispatch chokepoint (SkillDispatcher.dispatch) so it works for all execution paths (chat, sub-agents, background scheduler).How it works
ToolCallGuard.evaluate(call:)runs before every skill dispatch. On.allow, the call proceeds and its result is recorded viaguardedCompletion. On.block, a synthetic error is returned to the model immediately.Layer 1 — Dedup guard: Tracks the last 5 calls by
(name, normalizedArgs)signature. Second call with same signature → blocked with"You already called this tool with these arguments. Stop looping.".Layer 2 — Repeat counter: Hard-caps identical
(name, args)atmaxRepeats(default 3). On the 4th attempt → hard abort with"LOOP ABORTED".Layer 3 — Result diff check: If a tool returned results and the model tries the same call again → blocked with a reminder including a 200-char prefix of the previous result.
Layer 4 — Prompt injection: When any layer trips, sets
shouldInjectLoopReminder = true.AgentHarness.buildSystemPromptreads this flag and appends a strong system-level"⚠️ LOOP DETECTED — STOP REPEATING"reminder, then clears the flag.Integration points
Configuration
All thresholds are configurable via
ToolCallGuard.Config:Link to Devin session: https://app.devin.ai/sessions/1964159a3f5c4017b31db864e7f176b1