Skip to content

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

Finalize Rust follow-ups on interrupts

Magnus Müller483462cc87f7
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index a97477d95..6027f0f30 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -891,10 +891,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- The log includes the resolved max-step budget, preserving the observable run-start debug metadata expected by Browser Use callers.
- Proof: `test_rust_agent_run_logs_main_execution_start`.
+176. Rust Agent follow-up interrupt-finalization parity
+ - Direct terminal-backed `Agent.follow_up()` now handles `KeyboardInterrupt` like Browser Use run execution, returning current history after finalizing lifecycle state.
+ - Interrupted follow-ups log usage summaries, record `KeyboardInterrupt` telemetry, dispatch the final task update, unregister signal handlers, and close browser resources before returning.
+ - Proof: `test_rust_agent_follow_up_finalizes_after_keyboard_interrupt`.
+
## Current Verification
- `python3 -m py_compile browser_use/agent/service.py browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`
-- `uv run pytest -q tests/ci/test_rust_agent.py` (147 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (148 tests)
- `cargo build -q -p browser-use-cli` on terminal branch `magnus/browser-use-rust-main-integration`
- `cargo test -q -p browser-use-cli run_codex_session_command_accepts_task_id_and_model -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 5b71e641b..5e61a9936 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -3036,6 +3036,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self._register_run_signal_handler(resolved_max_steps)
try:
return await self._follow_up_terminal(task, max_steps=max_steps, resolved_max_steps=resolved_max_steps)
+ except KeyboardInterrupt:
+ self.logger.debug('Got KeyboardInterrupt during execution, returning current history')
+ await self._finalize_exceptional_run(max_steps=resolved_max_steps, agent_run_error='KeyboardInterrupt')
+ return self.history
except Exception as exc:
self.logger.error(f'Agent follow-up failed with exception: {exc}', exc_info=True)
await self._finalize_exceptional_run(max_steps=resolved_max_steps, agent_run_error=str(exc))
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index c494bdb9e..a99852f7f 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3461,6 +3461,98 @@ async def test_rust_agent_follow_up_finalizes_after_terminal_exception(monkeypat
assert logger.errors[0] == 'Agent follow-up failed with exception: followup exploded'
+async def test_rust_agent_follow_up_finalizes_after_keyboard_interrupt(monkeypatch):
+ import browser_use.rust.service as rust_service
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ events = []
+ captured_events = []
+
+ class FakeSignalHandler:
+ def __init__(self, **kwargs):
+ events.append('init_signal')
+
+ def register(self):
+ events.append('register_signal')
+
+ def unregister(self):
+ events.append('unregister_signal')
+
+ class RecordingLogger:
+ def __init__(self):
+ self.infos = []
+ self.debugs = []
+ self.errors = []
+ self.warnings = []
+
+ def info(self, message, *args, **kwargs):
+ self.infos.append(message)
+
+ def debug(self, message, *args, **kwargs):
+ self.debugs.append(message)
+
+ def error(self, message, *args, **kwargs):
+ self.errors.append(message)
+
+ def warning(self, message, *args, **kwargs):
+ self.warnings.append(message)
+
+ class BrowserProfile:
+ keep_alive = False
+ viewport = {'width': 1280, 'height': 720}
+ user_agent = None
+ headless = True
+ allowed_domains = []
+
+ class BrowserSession:
+ id = 'browser-session-1234'
+ browser_profile = BrowserProfile()
+ cdp_url = None
+
+ def __init__(self):
+ self.kill_calls = 0
+
+ async def kill(self):
+ events.append('kill_browser')
+ self.kill_calls += 1
+
+ class TokenCostService:
+ async def log_usage_summary(self):
+ events.append('usage_summary')
+
+ class Telemetry:
+ def capture(self, event):
+ captured_events.append(event)
+
+ session = BrowserSession()
+ logger = RecordingLogger()
+ monkeypatch.setattr(rust_service, 'SignalHandler', FakeSignalHandler)
+ monkeypatch.setattr(Agent, 'logger', property(lambda self: logger))
+ agent = Agent(task='Interrupt terminal follow-up.', llm=type('LLM', (), {'model': 'gpt-test'})(), browser_session=session)
+ agent.terminal_session_id = '12345678-1234-1234-1234-123456789abc'
+ agent.token_cost_service = TokenCostService()
+ agent.telemetry = Telemetry()
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ events.append('run_process')
+ raise KeyboardInterrupt
+
+ agent._run_process = fake_run_process
+
+ history = await agent.follow_up('Continue the task.', max_steps=5)
+
+ assert history is agent.history
+ assert events == ['init_signal', 'register_signal', 'run_process', 'usage_summary', 'unregister_signal', 'kill_browser']
+ assert session.kill_calls == 1
+ assert getattr(agent, '_run_signal_handler') is None
+ assert len(captured_events) == 1
+ assert captured_events[0].max_steps == 5
+ assert captured_events[0].error_message == 'KeyboardInterrupt'
+ assert 'Got KeyboardInterrupt during execution, returning current history' in logger.debugs
+ assert logger.errors == []
+
+
async def test_rust_agent_run_logs_token_usage_summary(monkeypatch):
from browser_use.rust import Agent