#9759: feat(core): Adaptive Loop Prevention via Tool Output Injection
agents
stale
Cluster:
Error Handling in Agent Tools
Implements lightweight loop detection in the agent execution pipeline:
- Tracks tool execution history (toolName + args hash) in session state
- Detects repeated failures (2x threshold within 5-minute window)
- Blocks loop-prone actions and injects synthetic error result
- LLM receives SYSTEM INTERVENTION message via tool output
- Forces strategy change without architectural rebuild
Technical details:
- History buffer: 10 entries, 5-minute sliding window
- Failure threshold: 2 identical failures trigger block
- Intervention vector: Tool result (not system prompt)
- State management: Per-session, cleared on compaction retry
Loop detection logic in handleToolExecutionStart prevents execution and returns synthetic error. History tracking in handleToolExecutionEnd records success/failure for future detection.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds per-session loop prevention to the embedded agent tool pipeline by hashing tool arguments, tracking recent tool executions (success/failure + timestamps), and blocking repeated failing tool calls. When a loop is detected in `handleToolExecutionStart`, it emits synthetic tool start/result events and injects a “SYSTEM INTERVENTION” error payload, while `handleToolExecutionEnd` records outcomes to drive future detections. State is stored in `EmbeddedPiSubscribeState` and reset during compaction retry resets.
<h3>Confidence Score: 2/5</h3>
- This PR is not safe to merge as-is due to a crash-prone hashing implementation and questionable delivery of the intervention to the model.
- The new loop detection relies on `hashToolArgs()` which will throw on null/undefined/non-object args and can also produce collisions due to shallow serialization, leading to incorrect blocking. Additionally, the intervention message appears to be routed primarily through verbose tool output rather than guaranteed model-visible tool result text, undermining the stated goal. These issues affect correctness and runtime stability of the agent pipeline.
- src/agents/pi-embedded-subscribe.handlers.tools.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</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
#11417: fix: add defense against consecutive tool misuse loops
by seojoonkim · 2026-02-07
78.2%
#3362: fix: auto-repair and retry on orphan tool_result errors
by samhotchkiss · 2026-01-28
76.0%
#14136: feat: add agent collapse safeguards and fix TUI display on abort
by liangweigain-create · 2026-02-11
74.8%
#14328: fix: strip incomplete tool_use blocks from errored/aborted messages...
by Kropiunig · 2026-02-12
74.4%
#3622: fix(agents): drop orphan tool results
by mickobizzle · 2026-01-28
73.9%
#11825: fix: keep tool_use/tool_result pairs together during session compac...
by C31gordon · 2026-02-08
73.2%
#14024: feat(agents): add structured tool reflection for error recovery
by career091101 · 2026-02-11
73.2%
#21195: fix: suppress orphaned tool_use/tool_result errors after session co...
by ruslansychov-git · 2026-02-19
73.1%
#4922: fix(agents): ensure parallel tool results have correct parentId
by jduartedj · 2026-01-30
73.0%
#22516: fix: add resilient tool registration with per-tool error isolation
by white-rm · 2026-02-21
72.8%