Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fixed agent loop detection

Saurav Pandab96b9436fc96
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index c0ba0e779..73d0a4d4a 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1089,8 +1089,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):
await self._inject_budget_warning(step_info)
self._inject_replan_nudge()
self._inject_exploration_nudge()
- self._inject_loop_detection_nudge()
self._update_loop_detector_page_state(browser_state_summary)
+ self._inject_loop_detection_nudge()
await self._force_done_after_last_step(step_info)
await self._force_done_after_failure()
return browser_state_summary
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index 69fe59379..4b3967f60 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -107,14 +107,9 @@ def _normalize_action_for_hash(action_name: str, params: dict[str, Any]) -> str:
if action_name == 'navigate':
url = str(params.get('url', ''))
- # Hash by domain only — navigating to same domain with different paths is still exploration
- try:
- from urllib.parse import urlparse
-
- domain = urlparse(url).netloc
- return f'navigate|{domain}'
- except Exception:
- return f'navigate|{url}'
+ # Hash by full URL — navigating to different paths is genuine exploration,
+ # only repeated navigation to the exact same URL is a loop signal.
+ return f'navigate|{url}'
if action_name == 'scroll':
direction = 'down' if params.get('down', True) else 'up'
diff --git a/tests/ci/test_action_loop_detection.py b/tests/ci/test_action_loop_detection.py
index 4b21f6b9d..f9a0fe3f3 100644
--- a/tests/ci/test_action_loop_detection.py
+++ b/tests/ci/test_action_loop_detection.py
@@ -82,13 +82,20 @@ def test_input_text_different_text_different_hash():
assert h1 != h2
-def test_navigate_same_domain_same_hash():
- """Navigate to same domain with different paths produces the same hash."""
+def test_navigate_same_url_same_hash():
+ """Navigating to the exact same URL produces the same hash."""
h1 = compute_action_hash('navigate', {'url': 'https://example.com/page1'})
- h2 = compute_action_hash('navigate', {'url': 'https://example.com/page2'})
+ h2 = compute_action_hash('navigate', {'url': 'https://example.com/page1'})
assert h1 == h2
+def test_navigate_different_paths_different_hash():
+ """Navigating to different paths on the same domain produces different hashes — this is genuine exploration."""
+ h1 = compute_action_hash('navigate', {'url': 'https://example.com/page1'})
+ h2 = compute_action_hash('navigate', {'url': 'https://example.com/page2'})
+ assert h1 != h2
+
+
def test_navigate_different_domain_different_hash():
"""Navigate to different domains produces different hashes."""
h1 = compute_action_hash('navigate', {'url': 'https://example.com/page1'})