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
Resolve merge conflicts: integrate login cookie evaluation with new browser setup and evaluation architecture
Alezander9002ef3051525
diff --cc eval/service.pyindex 500d2a550,84b800360..869a4773f--- a/eval/service.py+++ b/eval/service.py@@@ -1051,41 -1500,17 +1500,17 @@@ async def run_stage(stage: Stage, stagereturn await stage_func()- async def load_existing_result(task_folder: Path) -> dict:- """Load existing result if available"""- result_file = task_folder / 'result.json'- if not result_file.exists():- raise FileNotFoundError('No existing result found')-- async with await anyio.open_file(result_file) as f:- existing_result = json.loads(await f.read())-- # Check if evaluation is also present- existing_eval = existing_result.get('Online_Mind2Web_evaluation')- if existing_eval:- existing_result['has_evaluation'] = True- existing_result['evaluation_data'] = existing_eval- else:- existing_result['has_evaluation'] = False-- return existing_result--- async def setup_browser_session(task: Task, headless: bool) -> BrowserSession:+ async def setup_browser_session(task: Task, headless: bool, highlight_elements: bool = True) -> BrowserSession:"""Setup browser session for the task"""- logger.debug(f'Browser setup: Creating unique user data directory for task {task.task_id}')- # Create unique user data directory- base_user_data_dir = Path(BrowserProfile().user_data_dir).parent- unique_user_data_dir = base_user_data_dir / f'task_{task.task_id}'- unique_user_data_dir.mkdir(parents=True, exist_ok=True)-logger.debug(f'Browser setup: Initializing BrowserSession for task {task.task_id}')-- # For login tasks, configure cookie saving to ensure save_storage_state() gets called- browser_session_kwargs = {- 'user_data_dir': str(unique_user_data_dir),++ # Use incognito mode (user_data_dir=None) for evaluations to avoid state pollution- profile = BrowserProfile(- user_data_dir=None, # Incognito mode - no persistent state- headless=headless,- chromium_sandbox=False, # running in docker- highlight_elements=highlight_elements, # Control element highlighting (passed to profile)- keep_alive=True,++ profile_kwargs = {++ 'user_data_dir': None, # Incognito mode - no persistent state+ 'headless': headless,+ 'chromium_sandbox': False, # running in docker++ 'highlight_elements': highlight_elements, # Control element highlighting (passed to profile)++ 'keep_alive': True,# higher timeouts = higher success rates on long tail of slow sites or if on a slow CI server# timeout=60_000,# default_timeout=60_000,@@@ -1094,21 -1519,9 +1519,21 @@@# maximum_wait_page_load_time=60.0,# wait_between_actions=0.5,# ignore_https_errors=True, # some eval tasks have http:// or broken https sites in them- )+ }-+++ if hasattr(task, 'login_cookie') and task.login_cookie:- # For login tasks, we need to ensure cookies get saved to JSON format for evaluation- # Create the task folder if it doesn't exist++ # For login tasks, configure storage_state to save cookies to JSON file++ # This works even in incognito mode (user_data_dir=None)+ task_folder = Path(f'saved_trajectories/{task.task_id}')+ task_folder.mkdir(parents=True, exist_ok=True)-- # Configure storage_state to save cookies to JSON file+++ storage_state_path = task_folder / 'storage_state.json'- browser_session_kwargs['storage_state'] = str(storage_state_path)-- logger.debug(f"Login task {task.task_id}: Configured to save cookies to {storage_state_path}")-- browser_session = BrowserSession(**browser_session_kwargs)++ profile_kwargs['storage_state'] = str(storage_state_path)++++ logger.debug(f'Login task {task.task_id}: Configured to save cookies to {storage_state_path}')+++ profile = BrowserProfile(**profile_kwargs)+ browser_session = BrowserSession(browser_profile=profile)# Start browser sessionlogger.debug(f'Browser setup: Starting browser session for task {task.task_id}')@@@ -1162,133 -1576,17 +1588,137 @@@ async def run_agent_with_browserplanner_interval=planner_interval,source='eval_platform',)-+ # get last messageawait agent.run(max_steps=max_steps)- return agent.state.history+ last_input_messages = agent.message_manager.last_input_messages+ last_message = last_input_messages[-1].text+ return agent.state.history, last_message- @observe(name='evaluate_task_result', span_type='EVALUATOR')- async def evaluate_task_result(eval_model: BaseChatModel, task_folder: Path, task: Task | None = None) -> dict:+ @observe(name='evaluate_task_result', span_type='EVALUATOR') # type: ignore[arg-type]-async def evaluate_task_result(eval_model: BaseChatModel, task_folder: Path, use_mind2web: bool = False) -> dict:++async def evaluate_task_result(++ eval_model: BaseChatModel, task_folder: Path, task: Task | None = None, use_mind2web: bool = False++) -> dict:"""Evaluate the task result"""- return await judge_task_result(eval_model, task_folder, score_threshold=3, use_mind2web=use_mind2web)+ # Check if this is a login task that should use cookie-based evaluation+ if task and hasattr(task, 'login_cookie') and task.login_cookie:- logger.info(f"Using cookie-based evaluation for login task {task.task_id}")++ logger.info(f'Using cookie-based evaluation for login task {task.task_id}')+ return await evaluate_task_with_login_cookie(task.login_cookie, task_folder)+ else:- return await judge_task_result(eval_model, task_folder, score_threshold=3)++ return await judge_task_result(eval_model, task_folder, score_threshold=3, use_mind2web=use_mind2web)+++async def evaluate_task_with_login_cookie(login_cookie: str, task_folder: Path) -> dict:+ """+ Evaluate a login task by checking if the login_cookie is present in browser cookies.-+++ Args:+ login_cookie: String identifier that should appear in cookies if login was successful+ task_folder: Path to the task result folder containing saved cookies-+++ Returns:+ Dictionary containing evaluation results similar to Online_Mind2Web_eval format+ """+ # Look for cookies in saved_trajectories (saved by browser-use during shutdown)+ cookies_file = task_folder / 'cookies.json'+ storage_state_file = task_folder / 'storage_state.json'-+++ cookies_data = None+ cookies_source = None-+++ # Try to load cookies from storage_state.json first (newer format)+ if storage_state_file.exists():+ try:+ async with await anyio.open_file(storage_state_file) as f:+ storage_state = json.loads(await f.read())+ cookies_data = storage_state.get('cookies', [])+ cookies_source = 'storage_state.json'+ except Exception as e:- logger.warning(f"Failed to load storage_state.json: {e}")-++ logger.warning(f'Failed to load storage_state.json: {e}')+++ # Fallback to cookies.json (older format)+ if not cookies_data and cookies_file.exists():+ try:+ async with await anyio.open_file(cookies_file) as f:+ cookies_data = json.loads(await f.read())+ cookies_source = 'cookies.json'+ except Exception as e:- logger.warning(f"Failed to load cookies.json: {e}")-++ logger.warning(f'Failed to load cookies.json: {e}')+++ if not cookies_data:+ return {+ 'task_id': task_folder.name,+ 'judgement': 'Automatic judgement: No cookies saved for evaluation',+ 'success': False,+ 'error': 'No cookies file found for login task evaluation',+ 'score': 0.0,+ }-- logger.debug(f"Found {len(cookies_data)} cookies from {cookies_source}")-++++ logger.debug(f'Found {len(cookies_data)} cookies from {cookies_source}')+++ # Check if this is an exact match requirement- if login_cookie.startswith("EXACTMATCH "):++ if login_cookie.startswith('EXACTMATCH '):+ # Extract the actual cookie name after "EXACTMATCH "+ exact_cookie_name = login_cookie[11:] # Remove "EXACTMATCH " prefix+ is_exact_match = True+ search_target = exact_cookie_name+ logger.debug(f"Using exact match for cookie name: '{exact_cookie_name}'")+ else:+ # Use substring matching (original behavior)+ is_exact_match = False+ search_target = login_cookie+ logger.debug(f"Using substring matching for: '{login_cookie}'")-+++ # Check if login_cookie is present in cookies+ login_cookie_found = False+ matching_cookie_info = None-+++ for cookie in cookies_data:+ cookie_name = cookie.get('name', '')+ cookie_value = cookie.get('value', '')-+++ if is_exact_match:+ # Exact match: check if cookie name exactly matches the target+ if cookie_name == search_target:+ login_cookie_found = True+ matching_cookie_info = f"exact name match='{cookie_name}'"- logger.debug(f"Login cookie found with exact match: {matching_cookie_info}")++ logger.debug(f'Login cookie found with exact match: {matching_cookie_info}')+ break+ else:+ # Substring match: check if target appears in cookie name or value+ if search_target in cookie_name or search_target in cookie_value:+ login_cookie_found = True+ matching_cookie_info = f"substring match in name='{cookie_name}'"- logger.debug(f"Login cookie found with substring match: {matching_cookie_info}")++ logger.debug(f'Login cookie found with substring match: {matching_cookie_info}')+ break-+++ # Prepare evaluation result+ if login_cookie_found:+ if is_exact_match:+ judgement = f"Automatic judgement: Login cookie '{search_target}' was found as exact match in browser cookies"+ else:+ judgement = f"Automatic judgement: Login cookie '{search_target}' was found in browser cookies"+ success = True+ score = 1.0+ error = None+ else:+ if is_exact_match:+ judgement = f"Automatic judgement: Login cookie '{search_target}' was NOT found as exact match in browser cookies"+ else:+ judgement = f"Automatic judgement: Login cookie '{search_target}' was NOT found in browser cookies"+ success = False+ score = 0.0+ error = None-+++ logger.info(f"Cookie evaluation result: success={success} for login_cookie='{login_cookie}'")-+++ return {+ 'task_id': task_folder.name,+ 'judgement': judgement,+ 'success': success,+ 'error': error,+ 'score': score,+ }def save_result_to_server(convex_url: str, secret_key: str, payload: dict) -> bool:@@@ -1490,7 -1789,7 +1921,9 @@@ async def run_task_with_semaphoretry:logger.info(f'Task {task.task_id}: Evaluation starting.')evaluation = await run_stage(- Stage.EVALUATE, lambda: evaluate_task_result(eval_model, task_folder, task), timeout=300- Stage.EVALUATE, lambda: evaluate_task_result(eval_model, task_folder, use_mind2web_judge), timeout=300++ Stage.EVALUATE,++ lambda: evaluate_task_result(eval_model, task_folder, task, use_mind2web_judge),++ timeout=300,)task_result.stage_completed(Stage.EVALUATE, evaluation)logger.info(f'Task {task.task_id}: Evaluation completed.')