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
added milder nudge for action loop
Saurav Pandabb5914f251ae
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex ece2ce6e9..24ef9d0a7 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -182,37 +182,38 @@ class ActionLoopDetector(BaseModel):self.max_repetition_count = counts[self.most_repeated_hash]def get_nudge_message(self) -> str | None:- """Return an escalating nudge message based on repetition severity, or None if no loop detected."""+ """Return an escalating awareness nudge based on repetition severity, or None if no loop detected."""messages: list[str] = []# 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'Heads up: you have repeated a similar action {self.max_repetition_count} times 'f'in the last {len(self.recent_action_hashes)} actions. '- '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.'+ 'If you are making progress with each repetition, keep going. '+ 'If not, a different approach might get you there faster.')elif self.max_repetition_count >= 8:messages.append(- f'STRONG LOOP WARNING: You have repeated a similar action {self.max_repetition_count} times '+ f'Heads up: 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.'+ 'Are you still making progress with each attempt? '+ 'If so, carry on. Otherwise, it might be worth trying a different approach.')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. '- 'Consider trying a different approach.'+ f'Heads up: you have repeated a similar action {self.max_repetition_count} times '+ f'in the last {len(self.recent_action_hashes)} actions. '+ 'If this is intentional and making progress, carry on. '+ 'If not, it might be worth reconsidering your approach.')# Page stagnation nudgeif 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. '- 'Try a different element, navigate to a different page, or conclude the task.'+ f'The page content has not changed across {self.consecutive_stagnant_pages} consecutive actions. '+ 'Your actions might not be having the intended effect. '+ 'It could be worth trying a different element or approach.')if messages:diff --git a/tests/ci/test_action_loop_detection.py b/tests/ci/test_action_loop_detection.pyindex fe07d2c02..47481fd6f 100644--- a/tests/ci/test_action_loop_detection.py+++ b/tests/ci/test_action_loop_detection.py@@ -153,7 +153,7 @@ def test_detector_nudge_at_5_repeats():detector.record_action('search', {'query': 'site:hinative.com answers votes'})msg = detector.get_nudge_message()assert msg is not None- assert 'LOOP DETECTED' in msg+ assert 'repeated a similar action' in msgassert '5 times' in msg@@ -172,41 +172,41 @@ def test_detector_nudge_escalates_at_8_repeats():detector.record_action('search', {'query': 'site:hinative.com answers votes'})msg = detector.get_nudge_message()assert msg is not None- assert 'STRONG LOOP WARNING' in msg+ assert 'still making progress' in msgassert '8 times' in msgdef test_detector_nudge_escalates_at_12_repeats():- """Critical nudge at 12 repetitions."""+ """Most urgent 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 None- assert 'CRITICAL LOOP DETECTED' in msg+ assert 'making progress with each repetition' in msgassert '12 times' in msgdef test_detector_critical_message_no_done_directive():- """Critical nudge should NOT tell the agent to call done — just suggest a different approach."""+ """Critical nudge should NOT tell the agent to call done — just a gentle heads up."""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 'done action' not in msg- assert 'report your progress' in msg+ assert 'different approach' in msgdef test_detector_first_nudge_no_cannot_complete():- """First nudge should NOT say task 'cannot be completed on this site'."""+ """First nudge should NOT say task 'cannot be completed' — just raise awareness."""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 'cannot be completed' not in msg- assert 'Consider trying a different approach' in msg+ assert 'reconsidering your approach' in msgdef test_detector_window_slides():@@ -264,7 +264,7 @@ def test_page_stagnation_nudge_at_5_identical_pages():assert detector.consecutive_stagnant_pages >= 5msg = detector.get_nudge_message()assert msg is not None- assert 'PAGE STAGNATION' in msg+ assert 'page content has not changed' in msgdef test_page_stagnation_no_nudge_at_4_identical_pages():@@ -301,8 +301,8 @@ def test_combined_loop_and_stagnation():detector.record_page_state('https://example.com', 'same', 50)msg = detector.get_nudge_message()assert msg is not None- assert 'STRONG LOOP WARNING' in msg- assert 'PAGE STAGNATION' in msg+ assert 'still making progress' in msg+ assert 'page content has not changed' in msg# ─── PageFingerprint tests ───────────────────────────────────────────────────@@ -352,7 +352,7 @@ async def test_loop_nudge_injected_into_context():messages = _get_context_messages(agent)assert len(messages) == 1- assert 'LOOP DETECTED' in messages[0]+ assert 'repeated a similar action' in messages[0]async def test_no_loop_nudge_when_disabled():