Skip to content

fix: add loop detection to prevent agent from repeating identical actions#392

Open
octo-patch wants to merge 1 commit intoalibaba:mainfrom
octo-patch:fix/issue-112-loop-detection
Open

fix: add loop detection to prevent agent from repeating identical actions#392
octo-patch wants to merge 1 commit intoalibaba:mainfrom
octo-patch:fix/issue-112-loop-detection

Conversation

@octo-patch
Copy link
Copy Markdown
Contributor

Fixes #112

Problem

The agent can get stuck in an infinite loop, repeatedly calling the same action with the same input (e.g., clicking the same button or entering the same text 4+ times in a row) without making progress. There was already a @todo loop detection comment in #handleObservations acknowledging this.

Solution

Added loop detection logic in PageAgentCore.#handleObservations(). After each step, the agent checks the last 4 consecutive history entries. If all 4 share the identical action name and input, a system observation is pushed into the agent's history:

⚠️ Loop detected: "click_element_by_index" has been called 4 times in a row with identical input. This approach is not working. Try a completely different strategy to achieve your goal.

This warning is included in the next LLM invocation as part of <agent_history>, prompting the model to reconsider its approach.

Testing

  • Loop is triggered only after 4 consecutive identical actions (not 2–3, which may be intentional retries).
  • Detection uses JSON.stringify(action.input) for deep equality on the action payload.
  • Works for all tool types (click_element_by_index, input_text, scroll_down, etc.).
  • Non-looping repeated scrolls (e.g., scroll 3 times to read content) are not falsely flagged.

…ions (fixes alibaba#112)

When an agent gets stuck repeating the same action (e.g., clicking the
same button 4 times in a row), it now receives a system observation
warning that its current approach is not working and should try a
different strategy.

The detection checks the last 4 consecutive steps. If they all share
the same action name and identical input, a loop is flagged and an
observation is pushed into the agent history.
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.

Loop detection (same action has been called over and over again)

1 participant