Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
increased threshold for loop detection
Saurav Pandacec4aea3bc05
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 73d0a4d4a..170cc30e7 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -199,7 +199,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):enable_planning: bool = True,planning_replan_on_stall: int = 3,planning_exploration_limit: int = 5,- loop_detection_window: int = 10,+ loop_detection_window: int = 20,loop_detection_enabled: bool = True,llm_screenshot_size: tuple[int, int] | None = None,_url_shortening_limit: int = 25,@@ -1367,9 +1367,14 @@ class Agent(Generic[Context, AgentStructuredOutput]):returnif self.state.last_model_output is None:return+ # Actions to exclude: wait always hashes identically (instant false positive),+ # done is terminal, go_back is navigation recovery+ _LOOP_EXEMPT_ACTIONS = {'wait', 'done', 'go_back'}for action in self.state.last_model_output.action:action_data = action.model_dump(exclude_unset=True)action_name = next(iter(action_data.keys()), 'unknown')+ if action_name in _LOOP_EXEMPT_ACTIONS:+ continueparams = action_data.get(action_name, {})if not isinstance(params, dict):params = {}diff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex 4b3967f60..ece2ce6e9 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -61,7 +61,7 @@ class AgentSettings(BaseModel):final_response_after_failure: bool = True # If True, attempt one final recovery call after max_failures# Loop detection settings- loop_detection_window: int = 10 # Rolling window size for action similarity tracking+ loop_detection_window: int = 20 # Rolling window size for action similarity trackingloop_detection_enabled: bool = True # Whether to enable loop detection nudges@@ -95,15 +95,15 @@ def _normalize_action_for_hash(action_name: str, params: dict[str, Any]) -> str:engine = params.get('engine', 'google')return f'search|{engine}|{"|".join(tokens)}'- if action_name in ('click_element', 'input_text'):+ if action_name in ('click', 'input'):# For element-interaction actions, we only use the index (element identity).# Two clicks on the same element index are the same action.index = params.get('index')- if action_name == 'input_text':+ if action_name == 'input':text = str(params.get('text', ''))# Normalize input text: lowercase, strip whitespace- return f'input_text|{index}|{text.strip().lower()}'- return f'click_element|{index}'+ return f'input|{index}|{text.strip().lower()}'+ return f'click|{index}'if action_name == 'navigate':url = str(params.get('url', ''))@@ -113,7 +113,8 @@ def _normalize_action_for_hash(action_name: str, params: dict[str, Any]) -> str:if action_name == 'scroll':direction = 'down' if params.get('down', True) else 'up'- return f'scroll|{direction}'+ index = params.get('index')+ return f'scroll|{direction}|{index}'# Default: hash by action name + sorted params (excluding None values)filtered = {k: v for k, v in sorted(params.items()) if v is not None}@@ -136,7 +137,7 @@ class ActionLoopDetector(BaseModel):model_config = ConfigDict(arbitrary_types_allowed=True)# Rolling window of recent action hashes- window_size: int = 10+ window_size: int = 20recent_action_hashes: list[str] = Field(default_factory=list)# Page fingerprint tracking for stagnation detection@@ -184,30 +185,30 @@ class ActionLoopDetector(BaseModel):"""Return an escalating nudge message based on repetition severity, or None if no loop detected."""messages: list[str] = []- # Action repetition nudges (escalating at 3, 5, 8)- if self.max_repetition_count >= 8:+ # Action repetition nudges (escalating at 5, 8, 12)+ if self.max_repetition_count >= 12:messages.append(f'CRITICAL LOOP DETECTED: You have repeated a similar action {self.max_repetition_count} times 'f'in the last {len(self.recent_action_hashes)} actions. '- 'Your remaining action budget is low. Extract what you can and report results using the done action. '+ 'Try a fundamentally different approach, or if the task is partially complete, report your progress. ''Continuing to repeat the same action will not yield different results.')- elif self.max_repetition_count >= 5:+ elif self.max_repetition_count >= 8:messages.append(f'STRONG LOOP WARNING: You have repeated a similar action {self.max_repetition_count} times 'f'in the last {len(self.recent_action_hashes)} actions. ''Strongly consider abandoning this approach. Try a fundamentally different strategy ''or report that the task cannot be completed with the current approach.')- elif self.max_repetition_count >= 3:+ elif self.max_repetition_count >= 5:messages.append(f'LOOP DETECTED: You have repeated a similar action {self.max_repetition_count} times 'f'in the last {len(self.recent_action_hashes)} actions without meaningful progress. '- 'Try a fundamentally different approach or report that the task cannot be completed on this site.'+ 'Consider trying a different approach.')# Page stagnation nudge- if self.consecutive_stagnant_pages >= 3:+ if self.consecutive_stagnant_pages >= 5:messages.append(f'PAGE STAGNATION: The page content has not changed across {self.consecutive_stagnant_pages} ''consecutive actions. Your actions are not affecting the page state. 'diff --git a/tests/ci/test_action_loop_detection.py b/tests/ci/test_action_loop_detection.pyindex f9a0fe3f3..fe07d2c02 100644--- a/tests/ci/test_action_loop_detection.py+++ b/tests/ci/test_action_loop_detection.py@@ -54,31 +54,31 @@ def test_search_different_queries_produce_different_hashes():assert h1 != h2-def test_click_element_same_index_same_hash():+def test_click_same_index_same_hash():"""Clicking the same element index produces the same hash."""- h1 = compute_action_hash('click_element', {'index': 5})- h2 = compute_action_hash('click_element', {'index': 5})+ h1 = compute_action_hash('click', {'index': 5})+ h2 = compute_action_hash('click', {'index': 5})assert h1 == h2-def test_click_element_different_index_different_hash():+def test_click_different_index_different_hash():"""Clicking different element indices produces different hashes."""- h1 = compute_action_hash('click_element', {'index': 5})- h2 = compute_action_hash('click_element', {'index': 12})+ h1 = compute_action_hash('click', {'index': 5})+ h2 = compute_action_hash('click', {'index': 12})assert h1 != h2-def test_input_text_same_element_same_text():+def test_input_same_element_same_text():"""Same element + same text = same hash."""- h1 = compute_action_hash('input_text', {'index': 3, 'text': 'hello world', 'clear': True})- h2 = compute_action_hash('input_text', {'index': 3, 'text': 'hello world', 'clear': False})+ h1 = compute_action_hash('input', {'index': 3, 'text': 'hello world', 'clear': True})+ h2 = compute_action_hash('input', {'index': 3, 'text': 'hello world', 'clear': False})assert h1 == h2 # clear flag doesn't affect hash-def test_input_text_different_text_different_hash():+def test_input_different_text_different_hash():"""Same element but different text = different hash."""- h1 = compute_action_hash('input_text', {'index': 3, 'text': 'hello'})- h2 = compute_action_hash('input_text', {'index': 3, 'text': 'goodbye'})+ h1 = compute_action_hash('input', {'index': 3, 'text': 'hello'})+ h2 = compute_action_hash('input', {'index': 3, 'text': 'goodbye'})assert h1 != h2@@ -105,15 +105,29 @@ def test_navigate_different_domain_different_hash():def test_scroll_direction_matters():"""Scroll up and scroll down are different actions."""- h1 = compute_action_hash('scroll', {'down': True})- h2 = compute_action_hash('scroll', {'down': False})+ h1 = compute_action_hash('scroll', {'down': True, 'index': None})+ h2 = compute_action_hash('scroll', {'down': False, 'index': None})assert h1 != h2+def test_scroll_different_elements_different_hash():+ """Scrolling different elements produces different hashes."""+ h1 = compute_action_hash('scroll', {'down': True, 'index': 5})+ h2 = compute_action_hash('scroll', {'down': True, 'index': 10})+ assert h1 != h2+++def test_scroll_same_element_same_hash():+ """Scrolling the same element in the same direction produces the same hash."""+ h1 = compute_action_hash('scroll', {'down': True, 'index': 5})+ h2 = compute_action_hash('scroll', {'down': True, 'index': 5})+ assert h1 == h2++def test_different_action_types_different_hashes():"""Different action types always produce different hashes."""- h1 = compute_action_hash('click_element', {'index': 5})- h2 = compute_action_hash('scroll', {'down': True})+ h1 = compute_action_hash('click', {'index': 5})+ h2 = compute_action_hash('scroll', {'down': True, 'index': None})h3 = compute_action_hash('search', {'query': 'test'})assert len({h1, h2, h3}) == 3@@ -123,76 +137,108 @@ def test_different_action_types_different_hashes():def test_detector_no_nudge_for_diverse_actions():"""No nudge when actions are all different."""- detector = ActionLoopDetector(window_size=10)- detector.record_action('click_element', {'index': 1})- detector.record_action('scroll', {'down': True})- detector.record_action('click_element', {'index': 2})+ detector = ActionLoopDetector(window_size=20)+ detector.record_action('click', {'index': 1})+ detector.record_action('scroll', {'down': True, 'index': None})+ detector.record_action('click', {'index': 2})detector.record_action('search', {'query': 'something'})detector.record_action('navigate', {'url': 'https://example.com'})assert detector.get_nudge_message() is None-def test_detector_nudge_at_3_repeats():- """Nudge triggers at 3 repetitions of the same action."""- detector = ActionLoopDetector(window_size=10)- for _ in range(3):+def test_detector_nudge_at_5_repeats():+ """Nudge triggers at 5 repetitions of the same action."""+ detector = ActionLoopDetector(window_size=20)+ for _ in range(5):detector.record_action('search', {'query': 'site:hinative.com answers votes'})msg = detector.get_nudge_message()assert msg is not Noneassert 'LOOP DETECTED' in msg- assert '3 times' in msg+ assert '5 times' in msg-def test_detector_nudge_escalates_at_5_repeats():- """Stronger nudge at 5 repetitions."""- detector = ActionLoopDetector(window_size=10)- for _ in range(5):+def test_detector_no_nudge_at_4_repeats():+ """No nudge at only 4 repetitions (below threshold)."""+ detector = ActionLoopDetector(window_size=20)+ for _ in range(4):+ detector.record_action('search', {'query': 'site:hinative.com answers votes'})+ assert detector.get_nudge_message() is None+++def test_detector_nudge_escalates_at_8_repeats():+ """Stronger nudge at 8 repetitions."""+ detector = ActionLoopDetector(window_size=20)+ for _ in range(8):detector.record_action('search', {'query': 'site:hinative.com answers votes'})msg = detector.get_nudge_message()assert msg is not Noneassert 'STRONG LOOP WARNING' in msg- assert '5 times' in msg+ assert '8 times' in msg-def test_detector_nudge_escalates_at_8_repeats():- """Critical nudge at 8 repetitions."""- detector = ActionLoopDetector(window_size=10)- for _ in range(8):+def test_detector_nudge_escalates_at_12_repeats():+ """Critical nudge at 12 repetitions."""+ detector = ActionLoopDetector(window_size=20)+ for _ in range(12):detector.record_action('search', {'query': 'site:hinative.com answers votes'})msg = detector.get_nudge_message()assert msg is not Noneassert 'CRITICAL LOOP DETECTED' in msg- assert '8 times' in msg+ assert '12 times' in msg+++def test_detector_critical_message_no_done_directive():+ """Critical nudge should NOT tell the agent to call done — just suggest a different approach."""+ detector = ActionLoopDetector(window_size=20)+ for _ in range(12):+ detector.record_action('search', {'query': 'site:hinative.com answers votes'})+ msg = detector.get_nudge_message()+ assert msg is not None+ assert 'done action' not in msg+ assert 'report your progress' in msg+++def test_detector_first_nudge_no_cannot_complete():+ """First nudge should NOT say task 'cannot be completed on this site'."""+ detector = ActionLoopDetector(window_size=20)+ for _ in range(5):+ detector.record_action('search', {'query': 'site:hinative.com answers votes'})+ msg = detector.get_nudge_message()+ assert msg is not None+ assert 'cannot be completed' not in msg+ assert 'Consider trying a different approach' in msgdef test_detector_window_slides():"""Old actions fall out of the window."""- detector = ActionLoopDetector(window_size=5)+ detector = ActionLoopDetector(window_size=10)# Fill window with repeated actions- for _ in range(3):- detector.record_action('click_element', {'index': 7})- assert detector.max_repetition_count == 3+ for _ in range(5):+ detector.record_action('click', {'index': 7})+ assert detector.max_repetition_count == 5# Push them out with diverse actions- for i in range(5):- detector.record_action('click_element', {'index': 100 + i})- # The 3 old repeated actions should have been pushed out- assert detector.max_repetition_count < 3+ for i in range(10):+ detector.record_action('click', {'index': 100 + i})+ # The 5 old repeated actions should have been pushed out+ assert detector.max_repetition_count < 5assert detector.get_nudge_message() is Nonedef test_detector_search_variations_detected_as_same():"""Minor variations of the same search (the hinative pattern) are detected as repetition."""- detector = ActionLoopDetector(window_size=10)+ detector = ActionLoopDetector(window_size=20)# These are the kind of variations the agent producesqueries = ['site:hinative.com answers votes questions','site:hinative.com questions answers votes','site:hinative.com votes answers questions',+ 'site:hinative.com questions votes answers',+ 'site:hinative.com answers questions votes',]for q in queries:detector.record_action('search', {'query': q})- assert detector.max_repetition_count == 3+ assert detector.max_repetition_count == 5assert detector.get_nudge_message() is not None@@ -201,7 +247,7 @@ def test_detector_search_variations_detected_as_same():def test_page_stagnation_no_nudge_when_pages_change():"""No stagnation nudge when page content changes each step."""- detector = ActionLoopDetector(window_size=10)+ detector = ActionLoopDetector(window_size=20)detector.record_page_state('https://example.com', 'page content 1', 50)detector.record_page_state('https://example.com', 'page content 2', 55)detector.record_page_state('https://example.com', 'page content 3', 60)@@ -209,23 +255,31 @@ def test_page_stagnation_no_nudge_when_pages_change():assert detector.get_nudge_message() is None-def test_page_stagnation_nudge_at_3_identical_pages():- """Stagnation nudge fires after 3 consecutive identical page states."""- detector = ActionLoopDetector(window_size=10)+def test_page_stagnation_nudge_at_5_identical_pages():+ """Stagnation nudge fires after 5 consecutive identical page states."""+ detector = ActionLoopDetector(window_size=20)# First recording establishes baseline (doesn't count as stagnant)- detector.record_page_state('https://example.com', 'same content', 50)- detector.record_page_state('https://example.com', 'same content', 50)- detector.record_page_state('https://example.com', 'same content', 50)- detector.record_page_state('https://example.com', 'same content', 50)- assert detector.consecutive_stagnant_pages >= 3+ for _ in range(6):+ detector.record_page_state('https://example.com', 'same content', 50)+ assert detector.consecutive_stagnant_pages >= 5msg = detector.get_nudge_message()assert msg is not Noneassert 'PAGE STAGNATION' in msg+def test_page_stagnation_no_nudge_at_4_identical_pages():+ """No stagnation nudge at only 4 consecutive identical pages (below threshold)."""+ detector = ActionLoopDetector(window_size=20)+ # First recording establishes baseline, then 4 stagnant = 5 total recordings+ for _ in range(5):+ detector.record_page_state('https://example.com', 'same content', 50)+ assert detector.consecutive_stagnant_pages == 4+ assert detector.get_nudge_message() is None++def test_page_stagnation_resets_on_change():"""Stagnation counter resets when page content changes."""- detector = ActionLoopDetector(window_size=10)+ detector = ActionLoopDetector(window_size=20)detector.record_page_state('https://example.com', 'same content', 50)detector.record_page_state('https://example.com', 'same content', 50)detector.record_page_state('https://example.com', 'same content', 50)@@ -237,13 +291,13 @@ def test_page_stagnation_resets_on_change():def test_combined_loop_and_stagnation():"""Both action loop and page stagnation messages appear together."""- detector = ActionLoopDetector(window_size=10)- # Create action repetition- for _ in range(5):- detector.record_action('click_element', {'index': 7})- # Create page stagnation+ detector = ActionLoopDetector(window_size=20)+ # Create action repetition (8 for STRONG LOOP WARNING)+ for _ in range(8):+ detector.record_action('click', {'index': 7})+ # Create page stagnation (need 5 consecutive stagnant)detector.record_page_state('https://example.com', 'same', 50)- for _ in range(3):+ for _ in range(5):detector.record_page_state('https://example.com', 'same', 50)msg = detector.get_nudge_message()assert msg is not None@@ -290,8 +344,8 @@ async def test_loop_nudge_injected_into_context():llm = create_mock_llm()agent = Agent(task='Test task', llm=llm)- # Simulate 3 repeated actions- for _ in range(3):+ # Simulate 5 repeated actions (new threshold)+ for _ in range(5):agent.state.loop_detector.record_action('search', {'query': 'site:example.com answers'})agent._inject_loop_detection_nudge()@@ -310,8 +364,8 @@ async def test_no_loop_nudge_when_disabled():loop_detection_enabled=False,)- # Simulate 5 repeated actions- for _ in range(5):+ # Simulate 8 repeated actions+ for _ in range(8):agent.state.loop_detector.record_action('search', {'query': 'site:example.com answers'})agent._inject_loop_detection_nudge()@@ -325,9 +379,9 @@ async def test_no_loop_nudge_for_diverse_actions():llm = create_mock_llm()agent = Agent(task='Test task', llm=llm)- agent.state.loop_detector.record_action('click_element', {'index': 1})- agent.state.loop_detector.record_action('scroll', {'down': True})- agent.state.loop_detector.record_action('click_element', {'index': 2})+ agent.state.loop_detector.record_action('click', {'index': 1})+ agent.state.loop_detector.record_action('scroll', {'down': True, 'index': None})+ agent.state.loop_detector.record_action('click', {'index': 2})agent._inject_loop_detection_nudge()@@ -338,13 +392,13 @@ async def test_no_loop_nudge_for_diverse_actions():async def test_loop_detector_initialized_from_settings():"""Loop detector window size is set from agent settings."""llm = create_mock_llm()- agent = Agent(task='Test task', llm=llm, loop_detection_window=20)- assert agent.state.loop_detector.window_size == 20+ agent = Agent(task='Test task', llm=llm, loop_detection_window=30)+ assert agent.state.loop_detector.window_size == 30-async def test_loop_detector_default_enabled():- """Loop detection is enabled by default."""+async def test_loop_detector_default_window_size():+ """Loop detection default window size is 20."""llm = create_mock_llm()agent = Agent(task='Test task', llm=llm)assert agent.settings.loop_detection_enabled is True- assert agent.state.loop_detector.window_size == 10+ assert agent.state.loop_detector.window_size == 20