← Back to PRs

#9759: feat(core): Adaptive Loop Prevention via Tool Output Injection

by justin55afdfdsf5ds45f4ds5f45ds4 open 2026-02-05 16:56 View on GitHub →
agents stale
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