Skip to content

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

Support Rust agent follow-up sessions

Magnus Müller61171d1ad6b1
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 629a8dc4c..c38f25b00 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -311,6 +311,7 @@ class Agent(Generic[AgentStructuredOutput]):
async def follow_up(self, task: str) -> AgentHistoryList[AgentStructuredOutput]:
if not self.session_id:
raise RustAgentError('No active Rust session. Call run() before follow_up().')
+ started = time.time()
binary = find_browser_use_terminal_binary()
proc = await asyncio.create_subprocess_exec(
binary,
@@ -325,7 +326,28 @@ class Agent(Generic[AgentStructuredOutput]):
stdout, stderr = await proc.communicate()
if proc.returncode:
raise RustAgentError(stderr.decode(errors='replace') or stdout.decode(errors='replace'))
- return await self.run(max_steps=self.kwargs.get('max_steps', 100))
+ proc = await asyncio.create_subprocess_exec(
+ *self._run_existing_argv(self.kwargs.get('max_steps', 100)),
+ stdout=asyncio.subprocess.PIPE,
+ stderr=asyncio.subprocess.PIPE,
+ env=self._run_env(),
+ )
+ stdout, stderr = await proc.communicate()
+ finished = time.time()
+ process_error = None
+ if proc.returncode:
+ process_error = stderr.decode(errors='replace').strip() or f'browser-use-terminal exited with code {proc.returncode}'
+ self.last_events = await self._load_events()
+ self.result = _history_from_events(
+ self.last_events,
+ model=self.model,
+ started=started,
+ finished=finished,
+ output_model_schema=self.output_model_schema,
+ process_error=process_error,
+ )
+ self.history = self.result
+ return self.history
follow_up_task = follow_up
@@ -348,6 +370,23 @@ class Agent(Generic[AgentStructuredOutput]):
self.model,
]
+ def _run_existing_argv(self, max_steps: int) -> list[str]:
+ if not self.session_id:
+ raise RustAgentError('No active Rust session. Call run() before rerunning an existing session.')
+ binary = find_browser_use_terminal_binary()
+ return [
+ binary,
+ *self._state_dir_args(),
+ '-c',
+ f'max_turns={int(max_steps)}',
+ '-c',
+ f'browser_mode="{self._browser_mode()}"',
+ 'run-codex-session',
+ self.session_id,
+ '--model',
+ self.model,
+ ]
+
def _state_dir_args(self) -> list[str]:
state_dir = os.environ.get('BROWSER_USE_RUST_STATE_DIR')
return ['--state-dir', state_dir] if state_dir else []
@@ -400,4 +439,3 @@ class Agent(Generic[AgentStructuredOutput]):
if isinstance(parsed, dict):
events.append(parsed)
return events
-
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 6744e0152..ef29caf7a 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -95,6 +95,20 @@ def test_rust_agent_translates_browser_use_args_to_terminal(monkeypatch):
assert env['BU_CDP_URL'] == 'wss://browser.example/devtools/browser/1'
+def test_rust_agent_translates_followup_to_existing_terminal_session(monkeypatch):
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ agent = Agent(task='start', llm=type('LLM', (), {'model': 'gpt-test'})())
+ agent.session_id = '12345678-1234-1234-1234-123456789abc'
+
+ argv = agent._run_existing_argv(max_steps=9)
+
+ assert argv[0] == '/tmp/browser-use-terminal'
+ assert argv[-4:] == ['run-codex-session', agent.session_id, '--model', 'gpt-test']
+ assert 'max_turns=9' in argv
+
+
def test_rust_history_marks_process_failure_not_done():
from browser_use.rust.service import _history_from_events
@@ -111,4 +125,3 @@ def test_rust_history_marks_process_failure_not_done():
assert history.is_done() is False
assert history.is_successful() is None
assert history.errors() == ['terminal failed']
-