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
feat: added step_interval calculation for history
Saurav Panda8f8713b00168
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 010e2da43..c5f3fb724 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -899,10 +899,18 @@ class Agent(Generic[Context, AgentStructuredOutput]):returnif browser_state_summary:+ step_interval = None+ if len(self.history.history) > 0:+ last_history_item = self.history.history[-1]++ if last_history_item.metadata:+ previous_end_time = last_history_item.metadata.step_end_time+ step_interval = max(0, self.step_start_time - previous_end_time)metadata = StepMetadata(step_number=self.state.n_steps,step_start_time=self.step_start_time,step_end_time=step_end_time,+ step_interval=step_interval,)# Use _make_history_item like main branch@@ -2197,10 +2205,15 @@ class Agent(Generic[Context, AgentStructuredOutput]):results.append(ActionResult(error='No action to replay'))continue+ if history_item.metadata and history_item.metadata.step_interval is not None:+ step_delay = history_item.metadata.step_interval+ else:+ step_delay = delay_between_actions+retry_count = 0while retry_count < max_retries:try:- result = await self._execute_history_step(history_item, delay_between_actions)+ result = await self._execute_history_step(history_item, step_delay)results.extend(result)break@@ -2246,11 +2259,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):action=self.initial_actions,)- metadata = StepMetadata(- step_number=0,- step_start_time=time.time(),- step_end_time=time.time(),- )+ metadata = StepMetadata(step_number=0, step_start_time=time.time(), step_end_time=time.time(), step_interval=None)# Create minimal browser state history for initial actionsstate_history = BrowserStateHistory(diff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex 303fcd4f1..47009abb8 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -159,6 +159,7 @@ class StepMetadata(BaseModel):step_start_time: floatstep_end_time: floatstep_number: int+ step_interval: float | None = None@propertydef duration_seconds(self) -> float:diff --git a/examples/features/rerun_history.py b/examples/features/rerun_history.pyindex 3696d6fcd..57a7d74ae 100644--- a/examples/features/rerun_history.py+++ b/examples/features/rerun_history.py@@ -19,14 +19,14 @@ import asynciofrom pathlib import Pathfrom browser_use import Agent-from browser_use.llm.openai.chat import ChatOpenAI+from browser_use.llm import ChatBrowserUseasync def main():# Example task to demonstrate history saving and rerunninghistory_file = Path('agent_history.json')task = 'Go to https://browser-use.github.io/stress-tests/challenges/ember-form.html and fill the form with example data.'- llm = ChatOpenAI(model='gpt-4.1-mini')+ llm = ChatBrowserUse()agent = Agent(task=task, llm=llm, max_actions_per_step=1)await agent.run(max_steps=5)diff --git a/tests/ci/test_history_wait_time.py b/tests/ci/test_history_wait_time.pynew file mode 100644index 000000000..a718f0844--- /dev/null+++ b/tests/ci/test_history_wait_time.py@@ -0,0 +1,118 @@+from browser_use.agent.views import StepMetadata+++def test_step_metadata_has_step_interval_field():+ """Test that StepMetadata includes step_interval field"""+ metadata = StepMetadata(step_number=1, step_start_time=10.0, step_end_time=12.5, step_interval=2.5)++ assert hasattr(metadata, 'step_interval')+ assert metadata.step_interval == 2.5+++def test_step_metadata_step_interval_optional():+ """Test that step_interval is optional (None for first step)"""+ # Explicitly None+ metadata_none = StepMetadata(step_number=0, step_start_time=0.0, step_end_time=1.0, step_interval=None)+ assert metadata_none.step_interval is None++ # Omitted (defaults to None)+ metadata_default = StepMetadata(step_number=0, step_start_time=0.0, step_end_time=1.0)+ assert metadata_default.step_interval is None+++def test_step_interval_calculation():+ """Test step_interval calculation logic"""+ # Step 1 ends at 100.5+ step1_end = 100.5++ # Step 2 starts at 103.2+ step2_start = 103.2++ # Wait time should be 2.7 seconds+ expected_wait = 2.7+ calculated_wait = step2_start - step1_end++ assert abs(calculated_wait - expected_wait) < 0.001 # Float comparison+++def test_step_metadata_serialization_with_step_interval():+ """Test that step_interval is included in metadata serialization"""+ # With step_interval+ metadata_with_wait = StepMetadata(step_number=1, step_start_time=10.0, step_end_time=12.5, step_interval=2.5)++ data = metadata_with_wait.model_dump()+ assert 'step_interval' in data+ assert data['step_interval'] == 2.5++ # Without step_interval (None)+ metadata_without_wait = StepMetadata(step_number=0, step_start_time=0.0, step_end_time=1.0, step_interval=None)++ data = metadata_without_wait.model_dump()+ assert 'step_interval' in data+ assert data['step_interval'] is None+++def test_step_metadata_deserialization_with_step_interval():+ """Test that step_interval can be loaded from dict"""+ # Load with step_interval+ data_with_wait = {'step_number': 1, 'step_start_time': 10.0, 'step_end_time': 12.5, 'step_interval': 2.5}++ metadata = StepMetadata.model_validate(data_with_wait)+ assert metadata.step_interval == 2.5++ # Load without step_interval (old format)+ data_without_wait = {+ 'step_number': 0,+ 'step_start_time': 0.0,+ 'step_end_time': 1.0,+ # step_interval is missing+ }++ metadata = StepMetadata.model_validate(data_without_wait)+ assert metadata.step_interval is None # Defaults to None+++def test_step_interval_backwards_compatibility():+ """Test that old metadata without step_interval still works"""+ # Simulate old format from JSON+ old_metadata_dict = {+ 'step_number': 0,+ 'step_start_time': 1000.0,+ 'step_end_time': 1002.5,+ # step_interval field doesn't exist (old format)+ }++ # Should load successfully with step_interval defaulting to None+ metadata = StepMetadata.model_validate(old_metadata_dict)++ assert metadata.step_number == 0+ assert metadata.step_start_time == 1000.0+ assert metadata.step_end_time == 1002.5+ assert metadata.step_interval is None # Default value+++def test_duration_seconds_property_still_works():+ """Test that existing duration_seconds property still works"""+ metadata = StepMetadata(step_number=1, step_start_time=10.0, step_end_time=13.5, step_interval=2.0)++ # duration_seconds should be 3.5 (13.5 - 10.0)+ assert metadata.duration_seconds == 3.5++ # step_interval is separate from duration+ assert metadata.step_interval == 2.0+++def test_step_metadata_json_round_trip():+ """Test that step_interval survives JSON serialization round-trip"""+ metadata = StepMetadata(step_number=1, step_start_time=100.0, step_end_time=102.5, step_interval=1.5)++ # Serialize to JSON+ json_str = metadata.model_dump_json()++ # Deserialize from JSON+ loaded = StepMetadata.model_validate_json(json_str)++ assert loaded.step_interval == 1.5+ assert loaded.step_number == 1+ assert loaded.step_start_time == 100.0+ assert loaded.step_end_time == 102.5