Skip to content

fix: Add duplicate response loop breaker to prevent infinite loops#1265

Open
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
gdeyoung:fix/duplicate-loop-breaker
Open

fix: Add duplicate response loop breaker to prevent infinite loops#1265
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
gdeyoung:fix/duplicate-loop-breaker

Conversation

@gdeyoung
Copy link

Summary

This PR adds a duplicate response loop breaker to prevent the agent from getting stuck in infinite loops when receiving "You have sent the same message again" errors from external APIs.

Problem

The agent can get stuck in an infinite loop when:

  1. External LLM API (e.g., ZAI/GLM-5, Ollama) rejects a message as a duplicate
  2. Agent receives "You have sent the same message again" error
  3. Agent retries with the exact same message
  4. Loop continues indefinitely

Solution

  • Add duplicate_retries counter to track consecutive duplicate responses
  • Break loop after 3 consecutive identical responses with HandledException
  • Log clear error message when loop is broken
  • Reset duplicate_retries on successful iteration

Changes

  • agent.py: Added duplicate_retries counter and loop breaker logic (13 lines added)

Testing

Tested by triggering duplicate response scenarios - agent now breaks out after 3 attempts with clear error message.

Related Issues

Fixes #1056
Fixes #1000
Related to #1187, #1011

Notes

This is a core code fix that cannot be solved through agent behavior changes alone, as the LLM is not in a coherent state during a loop to recognize and break the pattern.

- Add duplicate_retries counter to track consecutive duplicate responses
- Break loop after 3 consecutive identical responses with HandledException
- Log error message when loop is broken
- Reset duplicate_retries on successful iteration

Fixes agent0ai#1056, agent0ai#1000 - Prevents agent from getting stuck in infinite loop
when receiving 'You have sent the same message again' from external APIs
gdeyoung pushed a commit to gdeyoung/agent-zero that referenced this pull request Mar 15, 2026
- Add duplicate_retries counter to track consecutive duplicate responses
- Pass retry_count to fw.msg_repeat.md for context
- Enhanced warning message with specific guidance on breaking loops
- Provides 4 concrete alternatives when stuck in a loop
- Reset duplicate_retries on successful iteration

This addresses the ROOT CAUSE by giving the LLM:
1. Context (retry count) so it knows it's in a loop
2. Specific alternatives instead of generic 'do something else'
3. Self-correction capability before circuit breaker kicks in

Works in conjunction with PR agent0ai#1265 (circuit breaker) for defense-in-depth.

Related to agent0ai#1056, agent0ai#1000, agent0ai#1187, agent0ai#1011
@gdeyoung
Copy link
Author

🚨 Report: Increased Looping After Today's Update

After today's platform update, I'm experiencing significantly MORE looping issues where patterns get stuck, and it's happening EARLIER in chats.

Evidence: Chat Session Restarts Today (March 25)

Time Session Notes
05:03 Session 1 -
08:01 Session 2 -
14:28 Session 3 -
14:32 Session 4 ⚠️ 4 min gap - restart
14:49 Session 5 ⚠️ 17 min gap - restart
15:49 Session 6 -
16:08 Session 7 -
16:39 Session 8 -
16:42 Session 9 ⚠️ 3 min gap - restart
18:05 Current -

Key Finding: 3 restarts within 21 minutes (14:28 → 14:49) indicates severe looping/hang issues!

Additional Warning on Startup

Seeing this in Docker logs on startup:
/opt/venv-a0/lib/python3.12/site-packages/requests/init.py:113: RequestsDependencyWarning:
urllib3 (2.6.3) or chardet (7.3.0)/charset_normalizer (3.4.6) doesn't match a supported version!
\

Manifestation

The loops manifest as:

  • Agent getting stuck repeating similar actions
  • Patterns that don't break naturally
  • Requiring manual restart to recover

Request

This reinforces the NEED for the circuit breaker fix in this PR. Please prioritize review - this is actively impacting production use.


Related: #1266 (Enhanced duplicate response guidance)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

You have sent the same message again. You have to do something else! You have sent the same message again. You have to do something else!

1 participant