Skip to content

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

fix(cli): make poll_until_complete async and show task_id immediately with --wait

ShawnPanac5dcb52cc90d
diff --git a/browser_use/skill_cli/commands/agent.py b/browser_use/skill_cli/commands/agent.py
index e5bd72c3a..e89e345f0 100644
--- a/browser_use/skill_cli/commands/agent.py
+++ b/browser_use/skill_cli/commands/agent.py
@@ -1,6 +1,5 @@
"""Agent task command handler."""
-import asyncio
import logging
import os
from typing import Any
@@ -152,11 +151,9 @@ async def _handle_cloud_task(params: dict[str, Any]) -> Any:
'message': 'Task started. Use "browser-use task status <task_id>" to check progress.',
}
- # Poll until complete (run in thread to avoid blocking event loop)
+ # Poll until complete
logger.info('Waiting for task completion...')
- result = await asyncio.to_thread(
- cloud_task.poll_until_complete, task_id, stream=params.get('stream', False)
- )
+ result = await cloud_task.poll_until_complete(task_id, stream=params.get('stream', False))
return {
'success': True,
diff --git a/browser_use/skill_cli/commands/cloud_task.py b/browser_use/skill_cli/commands/cloud_task.py
index b001ff1a1..38507b80f 100644
--- a/browser_use/skill_cli/commands/cloud_task.py
+++ b/browser_use/skill_cli/commands/cloud_task.py
@@ -5,7 +5,6 @@ for creating and managing cloud-based agent tasks and sessions.
"""
import logging
-import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any
@@ -182,12 +181,14 @@ def get_task_logs(task_id: str) -> TaskLogFileResponse:
# ============ Polling ============
-def poll_until_complete(
+async def poll_until_complete(
task_id: str,
stream: bool = False,
poll_interval: float = 1.0,
) -> TaskView:
"""Poll task status until finished."""
+ import asyncio
+
client = get_sdk_client()
last_status = None
@@ -202,4 +203,4 @@ def poll_until_complete(
if current_status in ('finished', 'stopped', 'failed'):
return task
- time.sleep(poll_interval)
+ await asyncio.sleep(poll_interval)
diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.py
index 781f03827..2b2568070 100755
--- a/browser_use/skill_cli/main.py
+++ b/browser_use/skill_cli/main.py
@@ -848,6 +848,54 @@ def handle_server_command(args: argparse.Namespace) -> int:
return 0
+def _handle_remote_run_with_wait(args: argparse.Namespace) -> int:
+ """Handle remote run with --wait directly (prints task info immediately, then waits)."""
+ import asyncio
+
+ from browser_use.skill_cli.commands import cloud_task
+
+ if not args.task:
+ print('Error: No task provided', file=sys.stderr)
+ return 1
+
+ try:
+ # Create task
+ task_response = cloud_task.create_task(
+ task=args.task,
+ llm=args.llm,
+ max_steps=args.max_steps,
+ flash_mode=args.flash,
+ thinking=args.thinking,
+ start_url=args.start_url,
+ )
+
+ # Print initial info immediately
+ print(f'mode: {args.browser}')
+ print(f'task_id: {task_response.id}')
+ print(f'session_id: {task_response.session_id}')
+ print('waiting...', end='', flush=True)
+
+ # Wait for completion
+ try:
+ result = asyncio.run(cloud_task.poll_until_complete(task_response.id))
+ except KeyboardInterrupt:
+ print(f'\nInterrupted. Task {task_response.id} continues remotely.')
+ return 0
+
+ # Print final result
+ print(' done.')
+ print(f'status: {result.status}')
+ print(f'output: {result.output}')
+ if result.cost:
+ print(f'cost: {result.cost}')
+
+ return 0
+
+ except Exception as e:
+ print(f'Error: {e}', file=sys.stderr)
+ return 1
+
+
def main() -> int:
"""Main entry point."""
parser = build_parser()
@@ -1080,6 +1128,10 @@ def main() -> int:
)
return 1
+ # Handle remote run with --wait directly (prints task_id immediately, then waits)
+ if args.browser == 'remote' and args.command == 'run' and getattr(args, 'wait', False):
+ return _handle_remote_run_with_wait(args)
+
# Ensure server is running
ensure_server(args.session, args.browser, args.headed, args.profile, args.api_key)