#11417: fix: add defense against consecutive tool misuse loops
agents
Cluster:
Error Handling in Agent Tools
Adds toolMisuseCount tracking with [DEFENSE] warning after 3+ consecutive failures. Fixes #11228.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds a small defense mechanism in the embedded Pi session subscription pipeline to detect repeated misuse of the `read` tool (calls without a `path`). It introduces new state (`toolMisuseCount`, `lastToolMisuseWarned`) initialized in `subscribeEmbeddedPiSession`, and updates `handleToolExecutionStart` to increment/reset the counter and emit an additional `[DEFENSE]` warning after 3 consecutive failures.
<h3>Confidence Score: 5/5</h3>
- This PR appears safe to merge with minimal risk.
- Changes are localized to tool execution start handling and state initialization, and do not affect core tool execution or message emission flows beyond adding warnings and updating new state fields. No behavioral regressions were found in the reviewed paths.
- No files require special attention
<!-- greptile_other_comments_section -->
<sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#9759: feat(core): Adaptive Loop Prevention via Tool Output Injection
by justin55afdfdsf5ds45f4ds5f45ds4 · 2026-02-05
78.2%
#8312: fix: add logging and markers for tool result repair
by ekson73 · 2026-02-03
76.0%
#22516: fix: add resilient tool registration with per-tool error isolation
by white-rm · 2026-02-21
74.9%
#9893: Tools: downgrade tool execution failure logs
by ai-fanatic · 2026-02-05
74.6%
#21195: fix: suppress orphaned tool_use/tool_result errors after session co...
by ruslansychov-git · 2026-02-19
74.4%
#14136: feat: add agent collapse safeguards and fix TUI display on abort
by liangweigain-create · 2026-02-11
73.9%
#3362: fix: auto-repair and retry on orphan tool_result errors
by samhotchkiss · 2026-01-28
73.7%
#14328: fix: strip incomplete tool_use blocks from errored/aborted messages...
by Kropiunig · 2026-02-12
73.5%
#19632: fix: suppressToolErrors now suppresses exec tool failure notifications
by Gitjay11 · 2026-02-18
73.5%
#9511: feat: add tool error fallback toggle
by bolismauro · 2026-02-05
73.5%