Skip to content

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

Merge remote-tracking branch 'origin/HEAD' into fix/issue-1716-event-driven-pause-resume

Magnus Müller0e44eb1d3d49
diff --cc browser_use/agent/service.py
index 23ef82d84,e2d3b1b52..53d9ced60
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@@ -474,11 -434,10 +434,11 @@@ class Agent(Generic[Context, AgentStruc
self.has_downloads_path = self.browser_session.browser_profile.downloads_path is not None
if self.has_downloads_path:
self._last_known_downloads: list[str] = []
- self.logger.info('📁 Initialized download tracking for agent')
+ self.logger.debug('📁 Initialized download tracking for agent')
- self._external_pause_event = asyncio.Event()
- self._external_pause_event.set()
+ # Event-based pause control (kept out of AgentState for serialization)
+ self._pause_event = asyncio.Event()
+ self._pause_event.set()
@property
def logger(self) -> logging.Logger:
@@@ -1244,14 -1447,16 +1452,16 @@@
self.logger.debug(f'🔄 Starting main execution loop with max {max_steps} steps...')
for step in range(max_steps):
- # Replace the polling with clean pause-wait
+ # Use the consolidated pause state management
if self.state.paused:
self.logger.debug(f'⏸️ Step {step}: Agent paused, waiting to resume...')
- await self.wait_until_resumed()
+ await self._pause_event.wait()
signal_handler.reset()
- # Check if we should stop due to too many failures
- if self.state.consecutive_failures >= self.settings.max_failures:
+ # Check if we should stop due to too many failures, if final_response_after_failure is True, we try one last time
+ if (self.state.consecutive_failures) >= self.settings.max_failures + int(
+ self.settings.final_response_after_failure
+ ):
self.logger.error(f'❌ Stopping due to {self.settings.max_failures} consecutive failures')
agent_run_error = f'Stopped due to {self.settings.max_failures} consecutive failures'
break
diff --cc browser_use/agent/views.py
index 933b0aed7,8de986114..89d89cb2d
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@@ -78,10 -61,10 +63,12 @@@ class AgentState(BaseModel)
last_result: list[ActionResult] | None = None
last_plan: str | None = None
last_model_output: AgentOutput | None = None
+
+ # Pause/resume state (kept serialisable for checkpointing)
paused: bool = False
stopped: bool = False
+ session_initialized: bool = False # Track if session events have been dispatched
+ follow_up_task: bool = False # Track if the agent is a follow-up task
message_manager_state: MessageManagerState = Field(default_factory=MessageManagerState)
file_system_state: FileSystemState | None = None