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
remove eval from main repo
Alezander9a6cade3f1791
diff --git a/eval/browsers.py b/eval/browsers.pydeleted file mode 100644index 7766b4378..000000000--- a/eval/browsers.py+++ /dev/null@@ -1,288 +0,0 @@-# pyright: reportMissingImports=false-# We do this because we need to install the other browser packages but dont want them in our main package dependencies--import asyncio-import json-import logging-import os-from pathlib import Path--import requests--from browser_use import BrowserProfile, BrowserSession-from eval.task_types import Task--logger = logging.getLogger(__name__)--# Check for Anchor Browser API key-ANCHOR_BROWSER_API_KEY = os.getenv('ANCHOR_BROWSER_API_KEY')-if ANCHOR_BROWSER_API_KEY:- logger.info('ANCHOR_BROWSER_API_KEY is set. Tasks can use Anchor Browser.')-else:- logger.warning('ANCHOR_BROWSER_API_KEY is not set. Anchor Browser will not be available.')--# Check for Brightdata CDP URL-BRIGHTDATA_CDP_URL = os.getenv('BRIGHTDATA_CDP_URL')-if BRIGHTDATA_CDP_URL:- logger.info('BRIGHTDATA_CDP_URL is set. Tasks can use Brightdata browser.')-else:- logger.warning('BRIGHTDATA_CDP_URL is not set. Brightdata browser will not be available.')--# Check for Browserbase API key-BROWSERBASE_API_KEY = os.getenv('BROWSERBASE_API_KEY')-BROWSERBASE_PROJECT_ID = os.getenv('BROWSERBASE_PROJECT_ID')-if BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID:- logger.info('BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID are set. Tasks can use Browserbase.')-else:- logger.warning('BROWSERBASE_API_KEY or BROWSERBASE_PROJECT_ID are not set. Browserbase will not be available.')--# Check for Hyperbrowser API key-HYPERBROWSER_API_KEY = os.getenv('HYPERBROWSER_API_KEY')-if HYPERBROWSER_API_KEY:- logger.info('HYPERBROWSER_API_KEY is set. Tasks can use Hyperbrowser.')-else:- logger.warning('HYPERBROWSER_API_KEY is not set. Hyperbrowser will not be available.')---def create_anchor_browser_session(headless: bool = False) -> str:- """Create an Anchor Browser session and return CDP URL"""- if not ANCHOR_BROWSER_API_KEY:- raise ValueError('ANCHOR_BROWSER_API_KEY must be set')-- browser_configuration = {- 'session': {'proxy': {'type': 'anchor_mobile', 'active': True, 'country_code': 'us'}},- 'browser': {- 'adblock': {'active': True},- 'captcha_solver': {'active': True},- 'headless': {'active': headless},- 'extra_stealth': {'active': True},- },- }-- try:- response = requests.post(- 'https://api.anchorbrowser.io/v1/sessions',- headers={- 'anchor-api-key': ANCHOR_BROWSER_API_KEY,- 'Content-Type': 'application/json',- },- json=browser_configuration,- timeout=10,- )- response.raise_for_status()- session_data = response.json()['data']- session_id = session_data['id']-- return f'wss://connect.anchorbrowser.io?apiKey={ANCHOR_BROWSER_API_KEY}&sessionId={session_id}'-- except requests.RequestException as e:- logger.error(f'Failed to create Anchor Browser session: {type(e).__name__}: {e}')- raise- except KeyError as e:- logger.error(f'Unexpected response format from Anchor Browser API: {e}')- raise---def create_browserbase_session() -> str:- """Create a Browserbase session and return CDP URL"""- if not BROWSERBASE_API_KEY or not BROWSERBASE_PROJECT_ID:- raise ValueError('BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID must be set')-- try:- from browserbase import Browserbase- except ImportError:- raise ImportError(- 'browserbase package is required for Browserbase functionality. Install it with: pip install browserbase'- )-- try:- bb = Browserbase(api_key=BROWSERBASE_API_KEY)- session = bb.sessions.create(- project_id=BROWSERBASE_PROJECT_ID,- proxies=True,- )-- return session.connect_url-- except Exception as e:- logger.error(f'Failed to create Browserbase session: {type(e).__name__}: {e}')- raise---async def create_hyperbrowser_session() -> str:- """Create a Hyperbrowser session and return WebSocket endpoint"""- if not HYPERBROWSER_API_KEY:- raise ValueError('HYPERBROWSER_API_KEY must be set')-- try:- from hyperbrowser import AsyncHyperbrowser- from hyperbrowser.models import CreateSessionParams- except ImportError:- raise ImportError(- 'hyperbrowser package is required for Hyperbrowser functionality. Install it with: pip install hyperbrowser'- )-- try:- client = AsyncHyperbrowser(api_key=HYPERBROWSER_API_KEY)-- session = await client.sessions.create(- params=CreateSessionParams(- use_stealth=True,- )- )-- await client.close()-- return session.ws_endpoint or ''-- except Exception as e:- logger.error(f'Failed to create Hyperbrowser session: {type(e).__name__}: {e}')- raise---async def setup_browser_session(- task: Task,- headless: bool,- highlight_elements: bool = True,- browser: str = 'local',- default_navigation_timeout: int | None = None,- default_timeout: int | None = None,- minimum_wait_page_load_time: float | None = None,- wait_for_network_idle_page_load_time: float | None = None,- maximum_wait_page_load_time: float | None = None,- wait_between_actions: float | None = None,- stealth: bool = False,-) -> BrowserSession:- """Setup browser session for the task"""-- # Validate browser option- valid_browsers = ['local', 'anchor-browser', 'brightdata', 'browserbase', 'hyperbrowser', 'browser-use']- if browser not in valid_browsers:- logger.warning(f'Browser setup: Invalid browser option "{browser}". Falling back to local browser.')- browser = 'local'-- cdp_url = None-- if browser == 'anchor-browser':- if ANCHOR_BROWSER_API_KEY:- try:- logger.debug(f'Browser setup: Creating Anchor Browser session for task {task.task_id}')- cdp_url = await asyncio.to_thread(create_anchor_browser_session, headless)- except Exception as e:- logger.error(- f'Browser setup: Failed to create Anchor Browser session for task {task.task_id}: {type(e).__name__}: {e}'- )- logger.info(f'Browser setup: Falling back to local browser for task {task.task_id}')- cdp_url = None- else:- logger.warning(- f'Browser setup: Anchor Browser requested but ANCHOR_BROWSER_API_KEY not set. Using local browser for task {task.task_id}'- )- elif browser == 'brightdata':- if BRIGHTDATA_CDP_URL:- logger.debug(f'Browser setup: Using Brightdata CDP URL for task {task.task_id}')- cdp_url = BRIGHTDATA_CDP_URL- else:- logger.warning(- f'Browser setup: Brightdata requested but BRIGHTDATA_CDP_URL not set. Using local browser for task {task.task_id}'- )- elif browser == 'browserbase':- if BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID:- try:- logger.debug(f'Browser setup: Creating Browserbase session for task {task.task_id}')- cdp_url = await asyncio.to_thread(create_browserbase_session)- except Exception as e:- logger.error(- f'Browser setup: Failed to create Browserbase session for task {task.task_id}: {type(e).__name__}: {e}'- )- logger.info(f'Browser setup: Falling back to local browser for task {task.task_id}')- cdp_url = None- else:- logger.warning(- f'Browser setup: Browserbase requested but BROWSERBASE_API_KEY or BROWSERBASE_PROJECT_ID not set. Using local browser for task {task.task_id}'- )- elif browser == 'hyperbrowser':- if HYPERBROWSER_API_KEY:- try:- logger.debug(f'Browser setup: Creating Hyperbrowser session for task {task.task_id}')- cdp_url = await create_hyperbrowser_session()- except Exception as e:- logger.error(- f'Browser setup: Failed to create Hyperbrowser session for task {task.task_id}: {type(e).__name__}: {e}'- )- logger.info(f'Browser setup: Falling back to local browser for task {task.task_id}')- cdp_url = None- else:- logger.warning(- f'Browser setup: Hyperbrowser requested but HYPERBROWSER_API_KEY not set. Using local browser for task {task.task_id}'- )- elif browser == 'browser-use':- logger.warning(f'Browser setup: Browser-use not implemented yet. Falling back to local browser for task {task.task_id}')-- 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,- 'stealth': stealth,- # higher timeouts = higher success rates on long tail of slow sites or if on a slow CI server- # ignore_https_errors=True, # some eval tasks have http:// or broken https sites in them- }-- # Add timeout parameters if provided- if default_navigation_timeout is not None:- profile_kwargs['default_navigation_timeout'] = default_navigation_timeout- if default_timeout is not None:- profile_kwargs['default_timeout'] = default_timeout- if minimum_wait_page_load_time is not None:- profile_kwargs['minimum_wait_page_load_time'] = minimum_wait_page_load_time- if wait_for_network_idle_page_load_time is not None:- profile_kwargs['wait_for_network_idle_page_load_time'] = wait_for_network_idle_page_load_time- if maximum_wait_page_load_time is not None:- profile_kwargs['maximum_wait_page_load_time'] = maximum_wait_page_load_time- if wait_between_actions is not None:- profile_kwargs['wait_between_actions'] = wait_between_actions-- if hasattr(task, 'login_cookie') and task.login_cookie:- # For login tasks, configure storage_state to save cookies to JSON file- # Don't set user_data_dir=None for login tasks to avoid conflict- task_folder = Path(f'saved_trajectories/{task.task_id}')- task_folder.mkdir(parents=True, exist_ok=True)-- storage_state_path = task_folder / 'storage_state.json'- # Create empty storage state file if it doesn't exist to avoid FileNotFoundError- if not storage_state_path.exists():- storage_state_path.write_text(json.dumps({'cookies': [], 'origins': []}))-- profile_kwargs['storage_state'] = str(storage_state_path)- # Remove user_data_dir=None for login tasks to avoid conflict with storage_state- profile_kwargs.pop('user_data_dir', None)-- downloads_dir_path = task_folder / 'downloads'- downloads_dir_path.mkdir(parents=True, exist_ok=True)- profile_kwargs['downloads_path'] = str(downloads_dir_path)-- logger.debug(f'Login task {task.task_id}: Configured to save cookies to {storage_state_path}')-- profile = BrowserProfile(**profile_kwargs)-- if cdp_url:- logger.debug(f'Browser setup: Using CDP Browser for task {task.task_id}')- browser_session = BrowserSession(browser_profile=profile, cdp_url=cdp_url)- else:- # Use local browser- logger.debug(f'Browser setup: Initializing BrowserSession for task {task.task_id}')- browser_session = BrowserSession(browser_profile=profile)-- # Start browser session- await browser_session.start()- logger.debug(f'Browser setup: Browser session started for task {task.task_id}')-- # Navigate to task starting url if provided- # if task.website:- # logger.debug(f'Browser setup: Navigating to {task.website} for task {task.task_id}')- # await browser_session.navigate(task.website)-- logger.debug(f'Browser setup: Setup completed for task {task.task_id}')- return browser_sessiondiff --git a/eval/comprehensive_judge.py b/eval/comprehensive_judge.pydeleted file mode 100644index f4122cb01..000000000--- a/eval/comprehensive_judge.py+++ /dev/null@@ -1,722 +0,0 @@-"""-@file purpose: Comprehensive judge system for evaluating browser-use agent runs with detailed structured feedback.-"""--import asyncio-import base64-import io-import json-import logging-from enum import Enum-from pathlib import Path-from typing import Any--from PIL import Image-from pydantic import BaseModel--from browser_use.llm.base import BaseChatModel-from browser_use.llm.messages import (- BaseMessage,- ContentPartImageParam,- ContentPartTextParam,- ImageURL,- SystemMessage,- UserMessage,-)-from browser_use.observability import observe_debug--logger = logging.getLogger(__name__)---class ErrorCategory(Enum):- # Access & Authentication- CAPTCHA = 'captcha'- CLOUDFLARE_BLOCKED = 'cloudflare_blocked'- NEED_2FA = 'need_2fa'- INVALID_CREDENTIALS = 'invalid_credentials'- LOGIN_FAILED = 'login_failed'-- # LLM- RATE_LIMITED = 'rate_limited'- LLM_CALL_ERROR = 'llm_call_error'-- # Planning / context- INFINITE_LOOP = 'infinite_loop'- WRONG_OUTPUT_FORMAT = 'wrong_output_format'-- # Browser- WAIT_TOO_SHORT = 'wait_too_short'- BROWSER_CRASHES = 'browser_crashes'- ELEMENT_INTERACTION_ERROR = 'element_interaction_error'- IFRAME_ISSUES = 'iframe_issues'-- # Tools- TOOL_FAILED = 'tool_failed'-- # Task- PARTIAL_OUTPUT = 'partial_output'- IMPOSSIBLE_TASK = 'impossible_task'-- # File System- FILE_SYSTEM_MISUSE = 'file_system_misuse' # Not saving results or tracking progress---class TaskCategory(Enum):- EXTRACTION = 'extraction'- INTERACTION = 'interaction'- LOGIN = 'login'- RESEARCH = 'research'- SHOPPING = 'shopping'- BOOKING = 'booking'- COMPARISON = 'comparison'- QA_TESTING = 'qa_testing'- FORM_FILLING = 'form_filling'- NAVIGATION = 'navigation'- SEARCH = 'search'- FILTERING = 'filtering'- CONTENT_CREATION = 'content_creation'- FILE_OPERATIONS = 'file_operations'- MULTI_STEP_WORKFLOW = 'multi_step_workflow'---class JudgeResult(BaseModel):- # Basic Information- task_summary: str # 1 sentence summary-- # Analysis- reasoning: str # What went well/not well analysis- error_categories: list[ErrorCategory] # Core error categories identified-- final_score: int # Overall score (0-100) - percentage of task completion-- # Developer Feedback- improvement_tips: list[str] # Concrete improvement suggestions---def encode_image(image_path: str) -> str:- """Convert image file to base64 string."""- try:- with Image.open(image_path) as image:- if image.mode == 'RGBA':- image = image.convert('RGB')- buffered = io.BytesIO()- image.save(buffered, format='JPEG')- return base64.b64encode(buffered.getvalue()).decode('utf-8')- except Exception as e:- logger.error(f'Failed to encode image {image_path}: {e}')- return ''---def truncate_text(text: str, max_length: int, from_beginning: bool = False) -> str:- """Truncate text to maximum length with eval system indicator."""- if len(text) <= max_length:- return text- if from_beginning:- return '...[cut for eval]' + text[-max_length + 23 :]- else:- return text[: max_length - 23] + '...[cut for eval]...'---@observe_debug()-def prepare_agent_steps(complete_history: list[dict]) -> list[str]:- """Extract and format agent steps, limiting each to 2000 characters.-- Excludes the last step if it contains a 'done' action, since that content- is already included in the final_result.- """- # Check if last step contains a 'done' action- # history_to_process = complete_history.copy()- # if complete_history:- # last_step = complete_history[-1]- # if last_step.get('result'):- # for result in last_step['result']:- # if isinstance(result, dict) and result.get('is_done'):- # # Exclude the last step since it's a 'done' action- # history_to_process = complete_history[:-1]- # break- history_to_process = complete_history-- steps = []- for i, step in enumerate(history_to_process):- step_text = f'Step {i + 1}:\n'-- # Add model output if available- if step.get('model_output'):- model_output = step['model_output']- if isinstance(model_output, dict):- # Format the model output nicely- if 'action' in model_output:- action_json = json.dumps(model_output['action'], indent=1)- if len(action_json) > 500:- step_text += f'Actions: {action_json[:500]}...[cut for eval system]\n'- else:- step_text += f'Actions: {action_json}\n'- # if 'current_state' in model_output:- # step_text += f'State: {model_output["current_state"]}\n'-- # Add results if available- if step.get('result'):- for j, result in enumerate(step['result']):- if isinstance(result, dict):- if result.get('extracted_content'):- content = str(result['extracted_content'])- if len(content) > 500:- step_text += f'Result {j + 1}: {content[:500]}...[cut for eval system]\n'- else:- step_text += f'Result {j + 1}: {content}\n'- if result.get('error'):- error = str(result['error'])- if len(error) > 500:- step_text += f'Error {j + 1}: {error[:500]}...[cut for eval system]\n'- else:- step_text += f'Error {j + 1}: {error}\n'-- steps.append(step_text)-- # iterate reversed over steps until you reach 15000 char and return the last part of the steps- total_length = 0- last_part: list[str] = []- for step_text in reversed(steps):- total_length += len(step_text)- if total_length > 15000:- break- last_part.append(step_text)- return last_part[::-1]---@observe_debug()-def are_images_identical(img_path1: str, img_path2: str) -> bool:- """Check if two images are identical by comparing their content."""- try:- with Image.open(img_path1) as img1, Image.open(img_path2) as img2:- # Convert to same format for comparison- if img1.mode != img2.mode:- img1 = img1.convert('RGB')- img2 = img2.convert('RGB')-- # Compare sizes first (quick check)- if img1.size != img2.size:- return False-- # Compare pixel data- return list(img1.getdata()) == list(img2.getdata())- except Exception as e:- logger.warning(f'Failed to compare images {img_path1} and {img_path2}: {e}')- return False---@observe_debug()-def filter_images(screenshot_paths: list[str], max_images: int) -> list[str]:- """- Filter screenshot paths to:- 1. Never include the first image (always white)- 2. Remove consecutive duplicate images- 3. Return up to max_images from the end- """- if not screenshot_paths:- return []-- # Skip the first image (always white)- filtered_paths = screenshot_paths[1:] if len(screenshot_paths) > 1 else []-- if not filtered_paths:- return []-- # Remove consecutive duplicates- deduplicated_paths = [filtered_paths[0]] # Always include the first non-skipped image-- for i in range(1, len(filtered_paths)):- current_path = filtered_paths[i]- previous_path = filtered_paths[i - 1]-- # Only add if not identical to previous image- if not are_images_identical(current_path, previous_path):- deduplicated_paths.append(current_path)-- # Return last max_images images- return deduplicated_paths[-max_images:] if len(deduplicated_paths) > max_images else deduplicated_paths---@observe_debug()-async def comprehensive_judge(- task: str,- complete_history: list[dict],- final_result: str,- last_message: str,- screenshot_paths: list[str],- model: BaseChatModel,- max_images: int = 10,-) -> JudgeResult:- """- Comprehensive judge that evaluates browser-use agent runs with detailed structured feedback.-- Args:- task: The original task description- complete_history: Full execution history with steps and results- final_result: The final result returned to the user- last_message: The agent's final message/output before completion- screenshot_paths: List of screenshot file paths from execution- model: The LLM model to use for evaluation- max_images: Maximum number of images to include in evaluation- """-- # Prepare inputs with length limits- task_truncated = truncate_text(task, 40000)- final_result_truncated = truncate_text(final_result or 'No final result', 20000)- last_message_truncated = truncate_text(last_message or 'No last message', 40000, from_beginning=True)- agent_steps = prepare_agent_steps(complete_history)-- # Select and filter images- selected_images = filter_images(screenshot_paths, max_images)-- # Encode images- encoded_images: list[ContentPartImageParam] = []- for img_path in selected_images:- if Path(img_path).exists():- encoded_img = encode_image(img_path)- if encoded_img:- encoded_images.append(ContentPartImageParam(image_url=ImageURL(url=f'data:image/jpeg;base64,{encoded_img}')))-- # Build error categories dynamically from enum- error_categories_text = ', '.join([category.value for category in ErrorCategory])-- # Construct the evaluation prompt- system_prompt = f"""You are an expert judge evaluating browser-use agent performance.--Here is context about the agent you have to evaluate:--<browser_use_agent_context>-**AGENT ARCHITECTURE UNDERSTANDING:**-The browser-use agent operates in iterative loops receiving structured input:--**AGENT INPUT (what agent sees each step):**-1. AGENT HISTORY: Chronological event stream with previous actions and results-2. AGENT STATE: User request, file system state, todo.md contents, step info-3. BROWSER STATE: Current URL, tabs, and interactive elements in indexed format (this represents the css selector of the element), and text of the current viewport-4. BROWSER VISION: Screenshot with bounding boxes around interactive elements-5. READ STATE: Temporary data from extract_structured_data or read_file actions--**CRITICAL: BROWSER STATE CONTAINS READABLE TEXT**-- The DOM is converted to text with indexed interactive elements: [index]<type>text content</type>-- Agent sees the browser_state of the current viewport at every step without needing extract_structured_data-- extract_structured_data gets the markdown of the entire page and not just the visible part, it then parses it to structured data based on a query and saves it to a markdown file and shows it into the read state-- Instead of extract_structured_data the agent can also scroll to get more information in the browser_state-- The browser_state is the ground truth, but can be improved if information is missing-- The agent can also read information directly from the input screenshot--**AGENT OUTPUT FORMAT (always JSON):**-- thinking: Structured reasoning following specific patterns-- evaluation_previous_goal: Assessment of last action success/failure-- memory: Progress tracking (1-3 sentences)-- next_goal: Clear statement of immediate objectives-- action: List of actions to execute sequentially--**EXPECTED AGENT BEHAVIORS:**-- Follows task output format requirements precisely (direct output vs file writing)-- Uses todo.md for long tasks above 20 steps-- Saves findings to results.md when the task is long multiple things need to be extracted on different pages-- Dont use file system for short tasks except required by the task-- Calls done action only when task complete or impossible to continue - not too early-- If the agent needs to repeat the same sub task multiple times & has a good trajectory, but hits the max step limit the score should be medium-- Analyse the screenshots. Each interactive element should have exactly one color bounding box. If the bounding boxes look off mention that.--</browser_use_agent_context>--**EVALUATION FRAMEWORK:**-**PRIMARY EVALUATION CRITERIA (in order of importance):**-1. **Task Satisfaction (Most Important)**: Did the agent accomplish what the user asked for? Focus on user intent and final outcome.-2. **Output Quality**: Is the final result in the correct format and complete? Does it match exactly what was requested?-3. **Tool Effectiveness**: Did the browser interactions work as expected? Were tools used appropriately? How many % of the tools failed?-4. **Agent Reasoning**: Quality of decision-making, planning, and problem-solving throughout the trajectory.-5. **Browser Handling**: Navigation stability, error recovery, and technical execution. If the browser crashes, does not load or a captcha blocks the task, the score must be very low.--**SCORING GUIDELINES (final_score represents % of task completion):**-- 90-100: Excellent - Task completed as requested, human-like execution-- 80-89: Very Good - Task completed with minor issues, but meets user fully requirements-- 70-79: Good - Task completed with minor issues, core requirements satisfied-- 60-69: Partial - Some parts of task completed, but significant portions incomplete or incorrect-- 40-59: Poor - Major issues, only minor parts of task completed successfully-- 1-39: Failed - Task not completed, significant problems throughout execution-- 0: Complete failure - No meaningful progress toward task completion or completely blocked by a captcha or login--**Examples of task completion scoring:**-- If task asks for 10 items and agent finds 4 items correctly: 40-- If task completed to full user requirements but with some errors to improve in the trajectory: 85-- If task impossible due to captcha/login requirements: 0-- If we get blocked by Cloudflare challenge the final score must be 0-- If the trajectory is ideal and the output is perfect: 100---**FAILURE CONDITIONS (automatically score very low):**-- Task not completed when it should be completable-- Blocked by captcha or missing authentication-- Output format completely wrong or missing-- Infinite loops or severe technical failures-- Critical user requirements ignored-- Page not loaded-- Browser crashed-- Agent could not interact with required UI elements--**ERROR CATEGORIES TO IDENTIFY:**-{error_categories_text}--- Notes for the error categories:-- Use the main error - e.g. if we cant login and thats why we dont have an output we should use the login_failed error category-- The error category list is sequential - so check if an error before is matching better and use that instead-- captcha includes traditional captchas and any other anti-bot protection systems that block task completion-- cloudflare_blocked means the agent was blocked by Cloudflare challenge or notice-- need_2fa means the agent needs to complete a 2FA step to login and was unable to do so-- invalid_credentials means the agent used the provided credentials to login but they were invalid- partial_output means we collected some part of the output but some is missing-- tool_failed means a tool like scrolling or file interaction failed or can be improved because functionality which would be helpful was missing - mention that in the improvement tips-- infinite_loop means the agent is stuck in a loop and not making progress-- wrong_output_format means the output is not in the requested format-- element_interaction_error means that our extraction of the DOM is not correct. E.g. we missed to detect a crucial button and the agent does not see it with a [index]. This can be verified if you look how we highlight elements in the screenshot.-- iframe_issues means we dont parse elements in the iframe correctly. E.g. we missed to detect a crucial button and the agent does not see it with a [index].-- impossible_task means the task is impossible to complete because the said is down or information is missing-- file_system_misuse means using read_file/write_file for short tasks when direct output would be appropriate. Important NOTE: extract_structured_data alone is not a file system misuse. This is allowed even for short tasks.---**Improvement Tips (Actionable Developer Guidance):**-Format: "Error Category: Specific improvement suggestion"-Examples:-- "Login error on sheets.google.com: Build a dedicated Google Sheets login function"-- "Element not found: Improve the DOM extraction layer to correctly include buttons in the navigation bar of the website check24.de"-- "Load timeout: Implement better wait strategies for dynamic content to wait until the page is fully loaded"-- "File system misuse: The agent used the read and write file tools for short tasks even it could have outputted the information directly. Adapt the system prompt to not use the file system for short tasks."-- "Captcha: The agent was blocked by a press and hold captcha and was unable to complete the task."--**IMPORTANT EVALUATION NOTES:**-- **DO NOT evaluate for hallucination** - Agent has access to browser_state with the DOM and the screenshot at every step, so trust all factual claims. When ever the agent states clear output information trust it and do not include that in your evaluation. The agent is not hallucinating. It know that information.-- **Penalize poor planning** - The agent should not use the file system for short tasks.-- **Penalize poor tool usage** - Wrong tools, inefficient approaches, ignoring available information--**RESPONSE FORMAT:**-Respond with EXACTLY this JSON structure (no additional text before or after):--{{- "task_summary": "One sentence summary of what the task. This only includes the input task and nothing from the agent.",- "reasoning": "Detailed analysis covering: what went well, what didn't work, trajectory quality assessment, tool usage evaluation, output quality review, and overall user satisfaction prediction",- "error_categories": ["error1", "error2"],- "final_score": 75,- "improvement_tips": [- "Button not clickable: Improve the DOM extraction layer to correctly include buttons in the navigation bar of the website check24.de"- ]-}}"""-- user_prompt = f"""**TASK:**-<task>-{task_truncated}-</task>--**AGENT TRAJECTORY:**-<agent_trajectory>-{chr(10).join(agent_steps)}-</agent_trajectory>--**AGENT'S LAST INPUT MESSAGE:**-<agent_last_input_message>-{last_message_truncated}-</agent_last_input_message>--**FINAL RESULT:**-<agent_final_result>-{final_result_truncated}-</agent_final_result>--**TOTAL STEPS:** {len(complete_history)}-**SCREENSHOTS PROVIDED:** {len(selected_images)}--Evaluate this agent execution given the criteria and respond with the exact JSON structure requested."""-- # Build messages- content_parts: list[ContentPartTextParam | ContentPartImageParam] = [ContentPartTextParam(text=user_prompt)]- content_parts.extend(encoded_images)-- messages: list[BaseMessage] = [- SystemMessage(content=system_prompt),- UserMessage(content=content_parts),- ]-- # Get structured response- try:- response = await model.ainvoke(messages, output_format=JudgeResult)- logger.info(f'Judge response: {response}')- return response.completion-- except Exception as e:- logger.error(f'Judge evaluation failed: {e}')- return create_fallback_result(task, str(e))---@observe_debug()-def parse_judge_response(result_dict: dict, task: str) -> JudgeResult:- """Parse the LLM response into a structured JudgeResult."""- try:- # Parse error categories- error_categories = []- if 'error_categories' in result_dict:- for err in result_dict['error_categories']:- try:- error_categories.append(ErrorCategory(err))- except ValueError:- logger.warning(f'Unknown error category: {err}')-- final_score = result_dict.get('final_score', 0)-- return JudgeResult(- task_summary=result_dict.get('task_summary', 'Task analysis unavailable'),- reasoning=result_dict.get('reasoning', 'Analysis unavailable'),- error_categories=error_categories,- final_score=final_score,- improvement_tips=result_dict.get('improvement_tips', []),- )-- except Exception as e:- logger.error(f'Failed to parse judge response: {e}')- return create_fallback_result(task, 'Failed to parse structured response')---@observe_debug()-def create_fallback_result(task: str, error_msg: str) -> JudgeResult:- """Create a fallback result when evaluation fails."""- return JudgeResult(- task_summary=f'Failed to analyze task: {task[:100]}...',- reasoning=f'Evaluation failed: {error_msg}',- error_categories=[ErrorCategory.IMPOSSIBLE_TASK],- final_score=0,- improvement_tips=['Fix evaluation system'],- )---@observe_debug()-async def judge_with_retry(- task: str,- complete_history: list[dict],- final_result: str,- last_message: str,- screenshot_paths: list[str],- model: BaseChatModel,- max_retries: int = 3,- max_images: int = 10,-) -> JudgeResult:- """- Judge with retry logic for robustness.-- Args:- task: The original task description- complete_history: Full execution history with steps and results- final_result: The final result returned to the user- last_message: The agent's final message/output before completion- screenshot_paths: List of screenshot file paths from execution- model: The LLM model to use for evaluation- max_retries: Maximum number of retry attempts- max_images: Maximum number of images to include in evaluation- """- for attempt in range(max_retries):- try:- return await comprehensive_judge(- task,- complete_history,- final_result,- last_message,- screenshot_paths,- model,- max_images,- )- except Exception as e:- if attempt == max_retries - 1:- logger.error(f'Judge failed after {max_retries} attempts: {e}')- return create_fallback_result(task, str(e))- logger.warning(f'Judge attempt {attempt + 1} failed, retrying: {e}')- await asyncio.sleep(2**attempt)-- # Fallback return (should never reach here given the logic above, but ensures type safety)- return create_fallback_result(task, 'Max retries exceeded without proper error handling')---async def judge_with_repeat_and_average(- task: str,- complete_history: list[dict],- final_result: str,- last_message: str,- screenshot_paths: list[str],- model: BaseChatModel,- judge_repeat_count: int = 1,- max_retries: int = 3,- max_images: int = 10,-) -> JudgeResult:- """- Run the judge multiple times and average the results.-- Args:- task: The original task description- complete_history: Full execution history with steps and results- final_result: The final result returned to the user- last_message: The agent's final message/output before completion- screenshot_paths: List of screenshot file paths from execution- model: The LLM model to use for evaluation- judge_repeat_count: Number of times to repeat the judge evaluation (averages over multiple judgments)- max_retries: Maximum number of retry attempts per judge run- max_images: Maximum number of images to include in evaluation-- Returns:- JudgeResult with averaged scores and merged feedback- """- if judge_repeat_count <= 1:- # Single evaluation - use existing logic- return await judge_with_retry(- task, complete_history, final_result, last_message, screenshot_paths, model, max_retries, max_images- )-- logger.info(f'Running {judge_repeat_count} judge evaluations in parallel for averaging')-- # Create tasks for parallel execution- judge_tasks = []- for i in range(judge_repeat_count):- task_coro = judge_with_retry(- task, complete_history, final_result, last_message, screenshot_paths, model, max_retries, max_images- )- judge_tasks.append(task_coro)-- # Run all judge evaluations in parallel- logger.info(f'Starting {len(judge_tasks)} parallel judge evaluations...')- results = await asyncio.gather(*judge_tasks, return_exceptions=True)-- # Process results and filter out exceptions- evaluations: list[JudgeResult] = []- for i, result in enumerate(results):- if isinstance(result, Exception):- logger.warning(f'Judge evaluation {i + 1} failed: {result}')- elif isinstance(result, JudgeResult):- evaluations.append(result)- logger.info(f'Judge evaluation {i + 1} completed successfully with score: {result.final_score}')- else:- logger.warning(f'Judge evaluation {i + 1} returned unexpected type: {type(result)}')-- if not evaluations or len(evaluations) == 0:- return create_fallback_result(task, 'All judge evaluations failed')-- logger.info(f'Averaging {len(evaluations)} successful evaluations')-- # Calculate averaged score- avg_score = sum(eval.final_score for eval in evaluations) / len(evaluations)-- # Merge error categories (keep unique)- all_error_categories = []- for eval in evaluations:- all_error_categories.extend(eval.error_categories)- unique_error_categories = list(set(all_error_categories)) # Remove duplicates-- # Merge improvement tips (keep unique)- all_improvement_tips = []- for eval in evaluations:- all_improvement_tips.extend(eval.improvement_tips)- unique_improvement_tips = list(set(all_improvement_tips)) # Remove duplicates-- # concat reasoning with 1. and 2....- reasoning = ''- for j, eval in enumerate(evaluations):- reasoning += f'JUDGE {j + 1} SCORE: {eval.final_score}\n{eval.reasoning}\n'-- max_diff = (- max(evaluations, key=lambda x: x.final_score).final_score - min(evaluations, key=lambda x: x.final_score).final_score- )- reasoning += f'MAX DIFF: {max_diff}\n'- # Create averaged result- return JudgeResult(- task_summary=evaluations[0].task_summary,- reasoning=reasoning,- error_categories=unique_error_categories,- final_score=int(avg_score),- improvement_tips=unique_improvement_tips,- )---def _read_result_file(result_file: Path) -> dict[str, Any]:- """Helper function to read result file synchronously."""- with open(result_file) as f:- return json.load(f)---def _write_result_file(result_file: Path, result_data: dict[str, Any]) -> None:- """Helper function to write result file synchronously."""- with open(result_file, 'w') as f:- f.write(json.dumps(result_data, indent=2, default=str))---# Integration helper function-@observe_debug()-async def evaluate_task_with_comprehensive_judge(- task_folder: Path, model: BaseChatModel, max_images: int = 10, judge_repeat_count: int = 1-) -> dict[str, Any]:- """- Evaluate a task result using the comprehensive judge system.-- Args:- task_folder: Path to the task result folder- model: The LLM model to use for evaluation- max_images: Maximum number of images to include in evaluation- judge_repeat_count: Number of times to repeat the judge evaluation (averages over multiple judgments)-- Returns:- Dictionary with both the old format for compatibility and the new comprehensive analysis.- """- result_file = task_folder / 'result.json'- if not result_file.exists():- return {- 'task_id': task_folder.name,- 'comprehensive_judge': None,- 'error': 'No result.json found',- }-- try:- # Load existing result using async wrapper- result_data = await asyncio.to_thread(_read_result_file, result_file)-- # Check if comprehensive judge result already exists- if result_data.get('comprehensive_judge_evaluation'):- return {- 'task_id': task_folder.name,- 'comprehensive_judge': result_data['comprehensive_judge_evaluation'],- 'error': None,- }-- # Extract data for evaluation- task = result_data.get('task', 'Unknown task')- complete_history = result_data.get('complete_history', [])- final_result = result_data.get('final_result_response', '')- last_message = result_data.get('last_message', '')- screenshot_paths = result_data.get('screenshot_paths', [])-- # Run comprehensive evaluation with repeat and averaging- judge_result = await judge_with_repeat_and_average(- task=task,- complete_history=complete_history,- final_result=final_result,- last_message=last_message,- screenshot_paths=screenshot_paths,- model=model,- judge_repeat_count=judge_repeat_count,- max_images=max_images,- )-- # Convert to dict for storage- judge_dict = judge_result.model_dump()-- # Save back to result file using async wrapper- result_data['comprehensive_judge_evaluation'] = judge_dict- await asyncio.to_thread(_write_result_file, result_file, result_data)-- return {- 'task_id': task_folder.name,- 'comprehensive_judge': judge_dict,- 'error': None,- }-- except Exception as e:- logger.error(f'Comprehensive judge evaluation failed for {task_folder.name}: {e}')- return {- 'task_id': task_folder.name,- 'comprehensive_judge': None,- 'error': str(e),- }diff --git a/eval/cookie_judge.py b/eval/cookie_judge.pydeleted file mode 100644index dc0a1724e..000000000--- a/eval/cookie_judge.py+++ /dev/null@@ -1,253 +0,0 @@-import json-import logging-import time-from pathlib import Path--import anyio--logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s: %(message)s')-logger = logging.getLogger(__name__)--# Global tracking for login cookie monitoring-_login_cookie_tracker = {}---async def check_login_cookie_at_step(browser_session, task_id: str, login_cookie: str, step: int) -> bool:- global _login_cookie_tracker-- try:- # Get current cookies from browser- current_cookies = await browser_session.get_cookies()-- if not current_cookies:- logger.debug(f'Task {task_id} Step {step}: No cookies found')- return False-- # Check if this is an exact match requirement- if login_cookie.startswith('EXACTMATCH '):- exact_cookie_name = login_cookie[11:] # Remove "EXACTMATCH " prefix- is_exact_match = True- search_target = exact_cookie_name- else:- is_exact_match = False- search_target = login_cookie-- # Check if login_cookie is present- for cookie in current_cookies:- cookie_name = cookie.get('name', '')- cookie_value = cookie.get('value', '')-- if is_exact_match:- if cookie_name == search_target:- logger.info(f'β Task {task_id} Step {step}: Login cookie "{search_target}" found (exact match)')- # Track that we found the cookie- _login_cookie_tracker[task_id] = {- 'found': True,- 'step': step,- 'cookie_name': cookie_name,- 'match_type': 'exact',- }- return True- else:- if search_target in cookie_name or search_target in cookie_value:- logger.info(f'β Task {task_id} Step {step}: Login cookie "{search_target}" found (substring match)')- # Track that we found the cookie- _login_cookie_tracker[task_id] = {- 'found': True,- 'step': step,- 'cookie_name': cookie_name,- 'match_type': 'substring',- }- return True-- # Enhanced logging when cookie not found- logger.debug(f'Task {task_id} Step {step}: Login cookie "{search_target}" not found in {len(current_cookies)} cookies')- logger.debug(- f'Task {task_id} Step {step}: Available cookies: {[cookie.get("name", "unnamed") for cookie in current_cookies]}'- )-- return False-- except Exception as e:- logger.warning(f'Task {task_id} Step {step}: Error checking login cookie: {type(e).__name__}: {e}')- return False---async def save_login_cookie_tracking(task_folder: Path, task_id: str) -> None:- """- Save the login cookie tracking information to a file.-- Args:- task_folder: Directory to save the tracking file- task_id: The task ID- """- global _login_cookie_tracker-- try:- tracking_file = task_folder / 'login_cookie_tracking.json'- tracking_data = _login_cookie_tracker.get(task_id, {'found': False})-- # Add timestamp- tracking_data['timestamp'] = time.time()-- # Save to file- async with await anyio.open_file(tracking_file, 'w') as f:- await f.write(json.dumps(tracking_data, indent=2))-- logger.info(f'π Saved login cookie tracking for task {task_id}: {tracking_data}')-- # Clean up tracking data to avoid memory leaks- _login_cookie_tracker.pop(task_id, None)-- except Exception as e:- logger.warning(f'β Failed to save login cookie tracking for task {task_id}: {type(e).__name__}: {e}')---async def evaluate_task_with_login_cookie(login_cookie: str, task_folder: Path) -> dict:- task_id = task_folder.name-- # First, check if we have step-by-step tracking data- tracking_file = task_folder / 'login_cookie_tracking.json'- if tracking_file.exists():- try:- async with await anyio.open_file(tracking_file) as f:- tracking_data = json.loads(await f.read())-- if tracking_data.get('found', False):- # Cookie was found during execution!- step_found = tracking_data.get('step', 'unknown')- match_type = tracking_data.get('match_type', 'unknown')- cookie_name = tracking_data.get('cookie_name', 'unknown')-- success = True- score = 1.0- judgement = f"Automatic judgement: Login cookie '{login_cookie}' was found during step {step_found} ({match_type} match on '{cookie_name}')"- error = None-- logger.info(- f"β Cookie evaluation result from step tracking: success={success} for login_cookie='{login_cookie}'"- )-- return {- 'task_id': task_id,- 'judgement': judgement,- 'success': success,- 'error': error,- 'score': score,- 'tracking_data': tracking_data,- }- except Exception as e:- logger.warning(f'Failed to load login cookie tracking: {e}')-- # Fallback to end-state cookie checking (original behavior)- logger.info(f'π No step-by-step tracking found for task {task_id}, falling back to end-state cookie checking')-- # 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}')-- # 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}')-- if not cookies_data:- return {- 'task_id': task_id,- 'judgement': 'Automatic judgement: No cookies saved for evaluation and no step-by-step tracking',- 'success': False,- 'error': 'No cookies file found for login task evaluation and no step-by-step tracking',- 'score': 0.0,- }-- logger.debug(f'Found {len(cookies_data)} cookies from {cookies_source}')-- # Check if this is an exact match requirement- 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}')- 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}')- break-- # Enhanced logging when cookie not found in end-state- if not login_cookie_found:- logger.debug(f'Task {task_id}: Cookie "{search_target}" not found in end-state cookies')- logger.debug(f'Task {task_id}: Available end-state cookies: {[cookie.get("name", "unnamed") for cookie in cookies_data]}')-- # 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 end-state browser cookies"- )- else:- judgement = f"Automatic judgement: Login cookie '{search_target}' was found in end-state 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 end-state browser cookies"- )- else:- judgement = f"Automatic judgement: Login cookie '{search_target}' was NOT found in end-state browser cookies"- success = False- score = 0.0- error = None-- logger.info(f"Cookie evaluation result from end-state: success={success} for login_cookie='{login_cookie}'")-- return {- 'task_id': task_id,- 'judgement': judgement,- 'success': success,- 'error': error,- 'score': score,- }diff --git a/eval/models.py b/eval/models.pydeleted file mode 100644index 39e7e9edd..000000000--- a/eval/models.py+++ /dev/null@@ -1,292 +0,0 @@-import logging-import os--from browser_use.llm.anthropic.chat import ChatAnthropic-from browser_use.llm.google.chat import ChatGoogle-from browser_use.llm.groq.chat import ChatGroq-from browser_use.llm.openai.chat import ChatOpenAI--logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s: %(message)s')-logger = logging.getLogger(__name__)--SUPPORTED_MODELS = {- # Anthropic- 'claude-3.5-sonnet': {- 'provider': 'anthropic',- 'model_name': 'claude-3-5-sonnet-20240620',- 'api_key_env': 'ANTHROPIC_API_KEY',- },- 'claude-3.5-sonnet-exp': {- 'provider': 'anthropic',- 'model_name': 'claude-3-5-sonnet-20241022',- 'api_key_env': 'ANTHROPIC_API_KEY',- },- 'claude-3.5-haiku-latest': {- 'provider': 'anthropic',- 'model_name': 'claude-3-5-haiku-latest',- 'api_key_env': 'ANTHROPIC_API_KEY',- },- 'claude-3.7-sonnet-exp': {- 'provider': 'anthropic',- 'model_name': 'claude-3-7-sonnet-20250219',- 'api_key_env': 'ANTHROPIC_API_KEY',- },- 'claude-sonnet-4': {- 'provider': 'anthropic',- 'model_name': 'claude-sonnet-4-20250514',- 'api_key_env': 'ANTHROPIC_API_KEY',- },- 'claude-opus-4': {- 'provider': 'anthropic',- 'model_name': 'claude-opus-4-20250514',- 'api_key_env': 'ANTHROPIC_API_KEY',- },- # Deepseek (via OpenAI Compatible API)- 'deepseek-reasoner': {- 'provider': 'openai_compatible',- 'model_name': 'deepseek-reasoner',- 'base_url': 'https://api.deepseek.com/v1',- 'api_key_env': 'DEEPSEEK_API_KEY',- },- 'deepseek-chat': {- 'provider': 'openai_compatible',- 'model_name': 'deepseek-chat',- 'base_url': 'https://api.deepseek.com/v1',- 'api_key_env': 'DEEPSEEK_API_KEY',- },- 'gemini-1.5-flash': {'provider': 'google', 'model_name': 'gemini-1.5-flash-latest', 'api_key_env': 'GEMINI_API_KEY'},- 'gemini-2.0-flash-lite': {'provider': 'google', 'model_name': 'gemini-2.0-flash-lite', 'api_key_env': 'GEMINI_API_KEY'},- 'gemini-2.0-flash': {- 'provider': 'google',- 'model_name': 'gemini-2.0-flash',- 'api_key_env': 'GEMINI_API_KEY',- 'thinking_budget': 0,- },- 'gemini-2.5-pro': {'provider': 'google', 'model_name': 'gemini-2.5-pro', 'api_key_env': 'GEMINI_API_KEY'},- 'gemini-2.5-flash': {- 'provider': 'google',- 'model_name': 'gemini-2.5-flash',- 'api_key_env': 'GEMINI_API_KEY',- 'thinking_budget': 0,- },- 'gemini-2.5-flash-lite-preview': {- 'provider': 'google',- 'model_name': 'gemini-2.5-flash-lite-preview-06-17',- 'api_key_env': 'GEMINI_API_KEY',- 'thinking_budget': 0,- },- 'gemini-2.5-pro-preview-05-06': {- 'provider': 'google',- 'model_name': 'gemini-2.5-pro-preview-05-06',- 'api_key_env': 'GEMINI_API_KEY',- },- 'gemini-2.5-flash-preview': {- 'provider': 'google',- 'model_name': 'gemini-2.5-flash-preview-04-17',- 'api_key_env': 'GEMINI_API_KEY',- },- # OpenAI- 'gpt-4.1': {'provider': 'openai', 'model_name': 'gpt-4.1-2025-04-14', 'api_key_env': 'OPENAI_API_KEY'},- 'gpt-4.1-mini': {'provider': 'openai', 'model_name': 'gpt-4.1-mini-2025-04-14', 'api_key_env': 'OPENAI_API_KEY'},- 'gpt-o3': {'provider': 'openai', 'model_name': 'o3-2025-04-16', 'api_key_env': 'OPENAI_API_KEY'},- 'gpt-4.1-nano': {'provider': 'openai', 'model_name': 'gpt-4.1-nano-2025-04-14', 'api_key_env': 'OPENAI_API_KEY'},- 'gpt-4o': {'provider': 'openai', 'model_name': 'gpt-4o', 'api_key_env': 'OPENAI_API_KEY'},- 'gpt-4o-mini': {'provider': 'openai', 'model_name': 'gpt-4o-mini', 'api_key_env': 'OPENAI_API_KEY'},- 'gpt-o4-mini': {'provider': 'openai', 'model_name': 'o4-mini', 'api_key_env': 'OPENAI_API_KEY'},- # OpenRouter (wildcard pattern)- 'openrouter-*': {- 'provider': 'openai_compatible',- 'model_name_pattern': '*', # Use the wildcard part as model name- 'base_url': 'https://openrouter.ai/api/v1',- 'api_key_env': 'OPENROUTER_API_KEY',- },- # X.ai (via OpenAI Compatible API)- 'grok-2': {- 'provider': 'openai_compatible',- 'model_name': 'grok-2-1212',- 'base_url': 'https://api.x.ai/v1',- 'api_key_env': 'XAI_API_KEY',- },- 'grok-3': {- 'provider': 'openai_compatible',- 'model_name': 'grok-3-beta',- 'base_url': 'https://api.x.ai/v1',- 'api_key_env': 'XAI_API_KEY',- },- 'grok-4': {- 'provider': 'openai_compatible',- 'model_name': 'grok-4-0709',- 'base_url': 'https://api.x.ai/v1',- 'api_key_env': 'XAI_API_KEY',- },- # Groq- 'gemma2-9b-it': {- 'provider': 'groq',- 'model_name': 'gemma2-9b-it',- 'api_key_env': 'GROQ_API_KEY',- 'service_tier': 'auto',- },- 'llama-3.3-70b-versatile': {- 'provider': 'groq',- 'model_name': 'llama-3.3-70b-versatile',- 'api_key_env': 'GROQ_API_KEY',- 'service_tier': 'auto',- },- 'llama-3.1-8b-instant': {- 'provider': 'groq',- 'model_name': 'llama-3.1-8b-instant',- 'api_key_env': 'GROQ_API_KEY',- 'service_tier': 'auto',- },- 'llama3-70b-8192': {- 'provider': 'groq',- 'model_name': 'llama3-70b-8192',- 'api_key_env': 'GROQ_API_KEY',- 'service_tier': 'auto',- },- 'llama3-8b-8192': {- 'provider': 'groq',- 'model_name': 'llama3-8b-8192',- 'api_key_env': 'GROQ_API_KEY',- 'service_tier': 'auto',- },- # Groq Preview- 'llama-4-maverick': {- 'provider': 'groq',- 'model_name': 'meta-llama/llama-4-maverick-17b-128e-instruct',- 'api_key_env': 'GROQ_API_KEY',- 'service_tier': 'auto',- },- 'llama-4-scout': {- 'provider': 'groq',- 'model_name': 'meta-llama/llama-4-scout-17b-16e-instruct',- 'api_key_env': 'GROQ_API_KEY',- 'service_tier': 'auto',- },- # SambaNova- 'deepseek-r1-sambanova': {- 'provider': 'openai_compatible',- 'model_name': 'DeepSeek-R1',- 'base_url': 'https://api.sambanova.ai/v1',- 'api_key_env': 'SAMBANOVA_API_KEY',- },- 'llama-4-maverick-sambanova': {- 'provider': 'openai_compatible',- 'model_name': 'Llama-4-Maverick-17B-128E-Instruct',- 'base_url': 'https://api.sambanova.ai/v1',- 'api_key_env': 'SAMBANOVA_API_KEY',- },- 'qwen2.5-vl-72b-instruct': {- 'provider': 'openai_compatible',- 'model_name': 'Qwen/Qwen2.5-VL-72B-Instruct',- 'base_url': 'https://api.together.xyz/v1',- 'api_key_env': 'TOGETHER_API_KEY',- },-}---def get_model_config(model_name: str):- """Find the model configuration, supporting both exact matches and wildcard patterns."""- # First try exact match- if model_name in SUPPORTED_MODELS:- config = SUPPORTED_MODELS[model_name].copy()- return config, None-- # Then try wildcard patterns - look for patterns containing *- for pattern, config in SUPPORTED_MODELS.items():- if '*' in pattern:- # Simple wildcard matching- # Split pattern on * and check if model_name matches the structure- parts = pattern.split('*')- if len(parts) == 2: # Simple case: prefix-*- prefix, suffix = parts- if model_name.startswith(prefix) and model_name.endswith(suffix):- # Extract the wildcard part- wildcard_part = (- model_name[len(prefix) : len(model_name) - len(suffix)] if suffix else model_name[len(prefix) :]- )-- config_copy = config.copy()- # Handle model_name_pattern substitution- if 'model_name_pattern' in config_copy:- # Replace * with the wildcard part- model_name_resolved = config_copy['model_name_pattern'].replace('*', wildcard_part)- config_copy['model_name'] = model_name_resolved- del config_copy['model_name_pattern']- return config_copy, wildcard_part-- return None, None---def get_llm(model_name: str):- """Instantiates the correct ChatModel based on the model name."""- config, regex_match = get_model_config(model_name)-- if config is None:- supported_models = []- wildcard_patterns = []- for k in SUPPORTED_MODELS.keys():- if '*' in k:- wildcard_patterns.append(k)- else:- supported_models.append(k)- raise ValueError(- f'Unsupported model: {model_name}. '- f'Supported models are: {supported_models}. '- f'Supported wildcard patterns: {wildcard_patterns}'- )-- provider = config['provider']- api_key_env = config.get('api_key_env')- api_key = os.getenv(api_key_env) if api_key_env else None-- if not api_key and api_key_env:- logger.warning(- f'API key environment variable {api_key_env} not found or empty for model {model_name}. Trying without API key if possible.'- )- api_key = None-- match provider:- case 'openai':- kwargs = {'model': config['model_name'], 'temperature': 0.0}- # Must set temperatue=1 if model is gpt-o4-mini- if model_name in ['gpt-o4-mini', 'gpt-o3']:- kwargs['temperature'] = 1- if api_key:- kwargs['api_key'] = api_key- return ChatOpenAI(**kwargs)- case 'anthropic':- kwargs = {- 'model': config['model_name'],- 'temperature': 0.0,- 'timeout': 100,- }- if api_key:- kwargs['api_key'] = api_key- return ChatAnthropic(**kwargs)- case 'google':- kwargs = {'model': config['model_name'], 'temperature': 0.0, 'thinking_budget': config.get('thinking_budget', None)}- if api_key:- kwargs['api_key'] = api_key- return ChatGoogle(**kwargs)- case 'groq':- kwargs = {- 'model': config['model_name'],- 'temperature': 0.0,- 'service_tier': config.get('service_tier', 'auto'),- }- if api_key:- kwargs['api_key'] = api_key- return ChatGroq(**kwargs)- case 'openai_compatible':- kwargs = {'model': config['model_name'], 'base_url': config['base_url'], 'temperature': 0.0}- if api_key:- kwargs['api_key'] = api_key- elif config.get('base_url'):- logger.warning(- f'API key for {model_name} at {config["base_url"]} is missing, but base_url is specified. Authentication may fail.'- )- return ChatOpenAI(**kwargs)- case _:- raise ValueError(f'Unknown provider: {provider}')diff --git a/eval/resource_monitoring.py b/eval/resource_monitoring.pydeleted file mode 100644index 15d260b24..000000000--- a/eval/resource_monitoring.py+++ /dev/null@@ -1,221 +0,0 @@-import asyncio-import gc-import logging-import signal-import sys-import threading-import time-from typing import Any--import psutil--from browser_use.observability import observe_debug--# Module logger-logger = logging.getLogger(__name__)--# Global variables for resource monitoring-_resource_monitor_task = None-_resource_monitor_stop_event = None-_graceful_shutdown_initiated = False---@observe_debug()-def get_system_resources() -> dict[str, Any]:- """Get current system resource usage"""- try:- # Memory usage- memory = psutil.virtual_memory()- memory_percent = memory.percent- memory_available_gb = memory.available / (1024**3)-- # CPU usage- cpu_percent = psutil.cpu_percent(interval=None)-- # Load average (Unix only)- try:- load_avg = psutil.getloadavg()- load_1min = load_avg[0]- except (AttributeError, OSError):- load_1min = 0.0-- # Process count- process_count = len(psutil.pids())-- # Chrome/Browser processes- chrome_processes = []- python_processes = []- for proc in psutil.process_iter(['pid', 'name', 'memory_percent', 'cpu_percent']):- try:- name = proc.info['name'].lower()- if 'chrome' in name or 'chromium' in name:- chrome_processes.append(proc.info)- elif 'python' in name:- python_processes.append(proc.info)- except (psutil.NoSuchProcess, psutil.AccessDenied):- continue-- return {- 'memory_percent': memory_percent,- 'memory_available_gb': memory_available_gb,- 'cpu_percent': cpu_percent,- 'load_1min': load_1min,- 'process_count': process_count,- 'chrome_process_count': len(chrome_processes),- 'python_process_count': len(python_processes),- 'chrome_processes': chrome_processes[:5], # Top 5 chrome processes- 'python_processes': python_processes[:5], # Top 5 python processes- }- except Exception as e:- logger.warning(f'Failed to get system resources: {type(e).__name__}: {e}')- return {- 'memory_percent': 0,- 'memory_available_gb': 0,- 'cpu_percent': 0,- 'load_1min': 0,- 'process_count': 0,- 'chrome_process_count': 0,- 'python_process_count': 0,- 'chrome_processes': [],- 'python_processes': [],- }---@observe_debug()-def log_system_resources(context: str = ''):- """Log current system resource usage"""- resources = get_system_resources()- logger.info(f'=== SYSTEM RESOURCES {context} ===')- logger.info(f'Memory: {resources["memory_percent"]:.1f}% used, {resources["memory_available_gb"]:.2f}GB available')- logger.info(f'CPU: {resources["cpu_percent"]:.1f}%, Load: {resources["load_1min"]:.2f}')- logger.info(- f'Processes: {resources["process_count"]} total, {resources["chrome_process_count"]} Chrome, {resources["python_process_count"]} Python'- )-- if resources['chrome_processes']:- logger.info('Top Chrome processes:')- for proc in resources['chrome_processes']:- logger.info(- f' PID {proc["pid"]}: {proc["name"]} - CPU: {proc["cpu_percent"]:.1f}%, Memory: {proc["memory_percent"]:.1f}%'- )-- logger.info('=' * (20 + len(context)))---async def start_resource_monitoring(interval: int = 30):- """Start background resource monitoring"""- global _resource_monitor_task, _resource_monitor_stop_event-- if _resource_monitor_task is not None:- logger.warning('Resource monitoring is already running')- return-- _resource_monitor_stop_event = asyncio.Event()-- async def monitor_loop():- """Background monitoring loop"""- logger.info(f'Starting resource monitoring (interval: {interval}s)')- try:- while _resource_monitor_stop_event is not None and not _resource_monitor_stop_event.is_set():- try:- log_system_resources('MONITOR')-- # Check for concerning resource levels- resources = get_system_resources()- if resources['memory_percent'] > 85:- logger.warning(f'β οΈ HIGH MEMORY USAGE: {resources["memory_percent"]:.1f}%')- if resources['cpu_percent'] > 90:- logger.warning(f'β οΈ HIGH CPU USAGE: {resources["cpu_percent"]:.1f}%')- if resources['chrome_process_count'] > 20:- logger.warning(f'β οΈ HIGH CHROME PROCESS COUNT: {resources["chrome_process_count"]}')-- # Force garbage collection periodically- if resources['memory_percent'] > 70:- logger.info('Running garbage collection due to high memory usage')- gc.collect()-- except Exception as e:- logger.error(f'Error in resource monitoring: {type(e).__name__}: {e}')-- try:- if _resource_monitor_stop_event is not None:- await asyncio.wait_for(_resource_monitor_stop_event.wait(), timeout=interval)- else:- await asyncio.sleep(interval)- break # Event was set, exit loop- except TimeoutError:- continue # Timeout reached, continue monitoring- except Exception as e:- logger.error(f'Resource monitoring loop crashed: {type(e).__name__}: {e}')- finally:- logger.info('Resource monitoring stopped')-- _resource_monitor_task = asyncio.create_task(monitor_loop())---async def stop_resource_monitoring():- """Stop background resource monitoring"""- global _resource_monitor_task, _resource_monitor_stop_event-- if _resource_monitor_stop_event is not None:- _resource_monitor_stop_event.set()-- if _resource_monitor_task is not None:- try:- await asyncio.wait_for(_resource_monitor_task, timeout=5.0)- except TimeoutError:- logger.warning('Resource monitoring task did not stop gracefully')- _resource_monitor_task.cancel()- try:- await _resource_monitor_task- except asyncio.CancelledError:- pass-- _resource_monitor_task = None- _resource_monitor_stop_event = None---def setup_signal_handlers():- """Setup signal handlers for graceful shutdown"""- global _graceful_shutdown_initiated-- def signal_handler(signum, frame):- global _graceful_shutdown_initiated- if _graceful_shutdown_initiated:- logger.critical('π₯ FORCE EXIT: Second signal received, terminating immediately')- sys.exit(1)-- _graceful_shutdown_initiated = True- logger.warning(f'β οΈ GRACEFUL SHUTDOWN: Received signal {signum}, initiating graceful shutdown...')- log_system_resources('SHUTDOWN')-- # Try to stop resource monitoring- try:- loop = asyncio.get_event_loop()- if loop.is_running():- loop.create_task(stop_resource_monitoring())- except Exception as e:- logger.error(f'Failed to stop resource monitoring during shutdown: {e}')-- # Give some time for cleanup, then force exit- def force_exit():- time.sleep(10)- if _graceful_shutdown_initiated:- logger.critical('π₯ FORCE EXIT: Graceful shutdown timeout, terminating')- sys.exit(1)-- threading.Thread(target=force_exit, daemon=True).start()-- # Register signal handlers- signal.signal(signal.SIGINT, signal_handler)- signal.signal(signal.SIGTERM, signal_handler)---def is_shutdown_initiated() -> bool:- """Check if graceful shutdown has been initiated"""- return _graceful_shutdown_initiated---def is_monitoring_active() -> bool:- """Check if resource monitoring is currently active"""- return _resource_monitor_task is not None and not _resource_monitor_task.done()diff --git a/eval/server.py b/eval/server.pydeleted file mode 100644index 86fa82b65..000000000--- a/eval/server.py+++ /dev/null@@ -1,349 +0,0 @@-import json-import logging-import os-import time--import requests--logger = logging.getLogger(__name__)---def fetch_tasks_from_server(convex_url: str, secret_key: str, test_case_name: str):- """Fetches the specified test case file from the Convex HTTP endpoint."""-- if not convex_url:- logger.error('Error: EVALUATION_TOOL_URL environment variable not set.')- return None-- if not secret_key:- logger.error('Error: EVALUATION_TOOL_SECRET_KEY environment variable not set.')- return None-- endpoint_url = f'{convex_url}/api/getTestCase'- headers = {- 'Authorization': f'Bearer {secret_key}',- 'Content-Type': 'application/json',- }- payload = {'name': test_case_name}-- logger.info(f"Fetching test case '{test_case_name}' from {endpoint_url}...")-- try:- response = requests.post(endpoint_url, headers=headers, json=payload, timeout=10)-- logger.info(f'Fetch Status Code: {response.status_code}')-- if response.status_code == 200:- try:- data = response.json()- logger.info(f"Successfully fetched test case data for '{test_case_name}'.")- # Assuming the data is the list of tasks- if isinstance(data, list):- return data- else:- logger.error(f'Error: Fetched data is not a list. Type: {type(data)}')- logger.error(f'Raw response: {response.text}')- return None-- except json.JSONDecodeError:- logger.error('Error: Failed to decode JSON response.')- logger.error(f'Raw response text: {response.text}')- return None- else:- logger.error(f"Error: Failed to fetch test case '{test_case_name}'. Status: {response.status_code}")- logger.error(f'Response: {response.text}')- return None-- except requests.exceptions.RequestException as e:- logger.error(f'Error during request to fetch test case: {type(e).__name__}: {e}')- return None---def fetch_auth_distribution_from_server(convex_url: str, secret_key: str):- """Fetches an available auth distribution from the Convex HTTP endpoint."""-- if not convex_url:- logger.error('Error: EVALUATION_TOOL_URL environment variable not set.')- return None-- if not secret_key:- logger.error('Error: EVALUATION_TOOL_SECRET_KEY environment variable not set.')- return None-- endpoint_url = f'{convex_url}/api/getAuthDistribution'- headers = {- 'Authorization': f'Bearer {secret_key}',- 'Content-Type': 'application/json',- }-- logger.info(f'Fetching auth distribution from {endpoint_url}...')-- try:- response = requests.post(endpoint_url, headers=headers, json={}, timeout=10)-- logger.info(f'Fetch Auth Distribution Status Code: {response.status_code}')-- if response.status_code == 200:- try:- data = response.json()- logger.info('Successfully fetched auth distribution data.')- # Verify the response has the expected structure- if isinstance(data, dict) and 'id' in data and 'loginInfo' in data:- return data- else:- logger.error(- f'Error: Fetched auth distribution data has unexpected structure. Keys: {list(data.keys()) if isinstance(data, dict) else "Not a dict"}'- )- logger.error(f'Raw response: {response.text}')- return None-- except json.JSONDecodeError:- logger.error('Error: Failed to decode JSON response for auth distribution.')- logger.error(f'Raw response text: {response.text}')- return None- elif response.status_code == 404:- logger.warning('No available auth distribution found on server.')- return None- else:- logger.error(f'Error: Failed to fetch auth distribution. Status: {response.status_code}')- logger.error(f'Response: {response.text}')- return None-- except requests.exceptions.RequestException as e:- logger.error(f'Error during request to fetch auth distribution: {type(e).__name__}: {e}')- return None---def format_auth_info_for_agent(auth_distribution: dict, auth_keys: list[str]) -> str:- """- Formats auth information from auth distribution for the agent task description.-- Args:- auth_distribution: Dict with 'loginInfo' key containing auth data- auth_keys: List of auth keys to extract (e.g., ['google', 'facebook'])-- Returns:- Formatted string with login credentials or empty string if no matching keys- """- if not auth_distribution or not auth_keys:- return ''-- login_info = auth_distribution.get('loginInfo', {})- if not login_info:- logger.warning('Auth distribution has no loginInfo')- return ''-- # Extract relevant auth information based on auth_keys- relevant_auths = []- for auth_key in auth_keys:- if auth_key in login_info:- auth_data = login_info[auth_key]- if isinstance(auth_data, dict):- # Format the auth data for this key- auth_details = []- for key, value in auth_data.items():- auth_details.append(f'{key}: {value}')-- if auth_details:- relevant_auths.append(f'{auth_key} with {", ".join(auth_details)}')- else:- logger.warning(f"Auth data for key '{auth_key}' is not a dictionary: {type(auth_data)}")- else:- logger.warning(f"Auth key '{auth_key}' not found in available login info. Available keys: {list(login_info.keys())}")-- if relevant_auths:- auth_text = f"\n\nOnly log into the account if it's required to complete the task. Do not log in otherwise.\n The following login credentials can be used to complete this task: {'; '.join(relevant_auths)}\nIf you need a verification code or OTP that is sent to your email, you can use the get_recent_emails action to get your latest emails."- logger.info(f'Formatted auth info: {auth_text}')- return auth_text- else:- logger.warning(f'No matching auth keys found. Requested: {auth_keys}, Available: {list(login_info.keys())}')- return ''---def start_new_run(convex_url: str, secret_key: str, run_details: dict, existing_run_id: str | None = None):- """Sends a request to start a new evaluation run and returns the run ID."""- if not convex_url or not secret_key:- logger.error('Error: Convex URL or Secret Key not provided for starting run.')- return None-- endpoint_url = f'{convex_url}/api/startRun'- headers = {- 'Authorization': f'Bearer {secret_key}',- 'Content-Type': 'application/json',- }-- # Add existing_run_id to the payload if provided- payload = run_details.copy()- if existing_run_id:- payload['runId'] = existing_run_id-- logger.info(f'Sending request to start run at {endpoint_url}...')- # Avoid logging secret key in run_details if it were ever passed- loggable_details = {k: v for k, v in payload.items() if k != 'secret_key'}- logger.info(f'Run details: {json.dumps(loggable_details, indent=2)}')-- try:- response = requests.post(endpoint_url, headers=headers, json=payload, timeout=10)- logger.info(f'Start Run Status Code: {response.status_code}')-- if response.status_code == 200:- try:- data = response.json()- run_id = data.get('runId')- if run_id:- logger.info(f'Successfully started run. Run ID: {run_id}')- return run_id- else:- logger.error("Error: 'runId' not found in successful startRun response.")- logger.error(f'Raw response: {response.text}')- return None- except json.JSONDecodeError:- logger.error('Error: Failed to decode startRun JSON response.')- logger.error(f'Raw response text: {response.text}')- return None- else:- logger.error('Error: Failed to start run.')- logger.error(f'Response: {response.text}')- return None-- except requests.exceptions.RequestException as e:- logger.error(f'Error during startRun request: {type(e).__name__}: {e}')- return None---def save_task_result_to_server(convex_url: str, secret_key: str, result_details: dict):- """Sends a request to save a single task result to the Convex backend."""-- if not convex_url:- logger.error('Error: EVALUATION_TOOL_URL environment variable not set for saving task result.')- return False-- if not secret_key:- logger.error('Error: EVALUATION_TOOL_SECRET_KEY environment variable not set for saving task result.')- return False-- # Ensure runId is present in the details being sent- if 'runId' not in result_details or not result_details['runId']:- logger.error("Error: 'runId' is missing or empty in result_details for saveTaskResult.")- return False-- endpoint_url = f'{convex_url}/api/saveTaskResult'- headers = {- 'Authorization': f'Bearer {secret_key}',- 'Content-Type': 'application/json',- }-- logger.info(f'Sending request to save task result at {endpoint_url}...')- logger.debug(f'Result details payload: {json.dumps(result_details, indent=2)}') # Log details at debug level-- try:- response = requests.post(endpoint_url, headers=headers, json=result_details, timeout=10)-- logger.info(f'Save Task Result Status Code: {response.status_code}')-- if response.status_code == 200:- try:- data = response.json()- logger.info(f'Successfully saved task result: {data.get("message")}')- logger.info(f'Result ID: {data.get("resultId")}')- return True- except json.JSONDecodeError:- logger.error('Error: Failed to decode saveTaskResult JSON response.')- logger.error(f'Raw response text: {response.text}')- return False- else:- logger.error('Error: Failed to save task result.')- logger.error(f'Response: {response.text}')- return False-- except requests.exceptions.RequestException as e:- logger.error(f'Error during saveTaskResult request: {type(e).__name__}: {e}')- return False---def save_runner_progress_to_server(convex_url: str, secret_key: str, progress_details: dict):- """Sends a request to save runner progress to the Convex backend."""-- if not convex_url:- logger.debug('No EVALUATION_TOOL_URL environment variable set for saving runner progress.')- return False-- if not secret_key:- logger.debug('No EVALUATION_TOOL_SECRET_KEY environment variable set for saving runner progress.')- return False-- endpoint_url = f'{convex_url}/api/saveRunnerProgress'- headers = {- 'Authorization': f'Bearer {secret_key}',- 'Content-Type': 'application/json',- }-- try:- response = requests.post(endpoint_url, headers=headers, json=progress_details, timeout=10)-- if response.status_code == 200:- logger.debug(f'Successfully saved runner progress for {progress_details.get("runnerId")}')- return True- else:- logger.warning(f'Failed to save runner progress. Status: {response.status_code}')- return False-- except requests.exceptions.RequestException as e:- logger.warning(f'Error during saveRunnerProgress request: {type(e).__name__}: {e}')- return False---def generate_runner_id(task_id: str, github_run_id: str | None = None) -> str:- """Generate a unique runner ID for progress tracking that matches GitHub Actions pattern"""- if github_run_id:- # Use batch-level runner ID consistent with GitHub Actions- # GitHub Actions uses: github_run_{GITHUB_RUN_ID}_batch_{START_INDEX}- # Get start index from environment variable set by GitHub Actions- start_index = os.getenv('EVAL_START_INDEX', '0')- return f'github_run_{github_run_id}_batch_{start_index}'- else:- # Fallback for local runs- return f'local_run_{int(time.time())}'---def send_progress_update(- convex_url: str,- secret_key: str,- run_id: str,- task_id: str,- current_stage: str,- status: str = 'active',- github_workflow_url: str | None = None,- assigned_task_range: str | None = None,- error_message: str | None = None,-) -> bool:- """Send a progress update for the current runner and task"""- try:- # Generate runner ID- github_run_id = os.getenv('GITHUB_RUN_ID')- runner_id = generate_runner_id(task_id, github_run_id)-- # Extract workflow run ID from URL if available- github_workflow_run_id = None- if github_workflow_url and 'actions/runs/' in github_workflow_url:- try:- github_workflow_run_id = github_workflow_url.split('actions/runs/')[1].split('/')[0]- except (IndexError, AttributeError):- pass-- progress_details = {- 'runId': run_id,- 'runnerId': runner_id,- 'taskId': task_id,- 'currentStage': current_stage,- 'status': status,- 'githubWorkflowUrl': github_workflow_url,- 'githubWorkflowRunId': github_workflow_run_id,- 'assignedTaskRange': assigned_task_range,- 'errorMessage': error_message,- }-- return save_runner_progress_to_server(convex_url, secret_key, progress_details)- except Exception as e:- logger.warning(f'Failed to send progress update: {type(e).__name__}: {e}')- return Falsediff --git a/eval/service.py b/eval/service.pydeleted file mode 100644index 9eebd4aa8..000000000--- a/eval/service.py+++ /dev/null@@ -1,1958 +0,0 @@-# ================================================-# Imports-# ================================================--import argparse-import asyncio-import base64-import http.client-import json-import logging-import os-import time-from pathlib import Path-from uuid import UUID--import anyio-from dotenv import load_dotenv-from lmnr import AsyncLaminarClient, Instruments, Laminar-from pydantic import BaseModel--from browser_use import ActionResult, Agent, BrowserSession, Controller-from browser_use.agent.views import AgentHistoryList-from browser_use.llm.base import BaseChatModel-from browser_use.observability import observe, observe_debug--MAX_IMAGE = 5-from eval.browsers import (- ANCHOR_BROWSER_API_KEY,- BRIGHTDATA_CDP_URL,- BROWSERBASE_API_KEY,- BROWSERBASE_PROJECT_ID,- HYPERBROWSER_API_KEY,- setup_browser_session,-)-from eval.comprehensive_judge import evaluate_task_with_comprehensive_judge-from eval.cookie_judge import check_login_cookie_at_step, evaluate_task_with_login_cookie, save_login_cookie_tracking-from eval.models import SUPPORTED_MODELS, get_llm-from eval.resource_monitoring import (- get_system_resources,- log_system_resources,- setup_signal_handlers,- start_resource_monitoring,- stop_resource_monitoring,-)-from eval.server import (- fetch_auth_distribution_from_server,- fetch_tasks_from_server,- format_auth_info_for_agent,- save_task_result_to_server,- send_progress_update,- start_new_run,-)-from eval.task_types import Stage, StageError, Task, TaskResult-from eval.utils import get_git_info-from eval.web_judge import Online_Mind2Web_eval_with_retry--# ================================================-# Setup Logging-# ================================================--logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s: %(message)s')-logger = logging.getLogger(__name__)--# ================================================-# Environment variables-# ================================================--# Load dotenv-load_dotenv()--# Check for SERPER API key-SERPER_API_KEY = os.getenv('SERPER_API_KEY')-if not SERPER_API_KEY:- logger.warning('SERPER_API_KEY is not set. Search functionality will not be available.')---# ================================================-# Tracking and Observations-# ================================================--Laminar.initialize(disabled_instruments={Instruments.BROWSER_USE}, disable_batch=True)-laminar_client = AsyncLaminarClient()--# Resource monitoring functions moved to resource_monitoring.py module---# ================================================-# Custom Controllers-# ================================================---def create_controller_with_serp_search(output_model: type[BaseModel] | None = None):- """Create a controller with SERP search instead of Google search"""- controller = Controller(exclude_actions=['search_google'], output_model=output_model)-- @controller.registry.action('Search the web for a specific query')- async def search_web(query: str):- """Search the web using Serper API"""- if not SERPER_API_KEY:- return ActionResult(extracted_content='Search unavailable: SERPER_API_KEY not configured', include_in_memory=True)-- try:- # Make request to Serper API- conn = http.client.HTTPSConnection('google.serper.dev')- payload = json.dumps({'q': query})- headers = {'X-API-KEY': SERPER_API_KEY, 'Content-Type': 'application/json'}- conn.request('POST', '/search', payload, headers)- res = conn.getresponse()- data = res.read()- serp_data = json.loads(data.decode('utf-8'))-- # Exclude searchParameters and credits to reduce noise- serp_data = {k: v for k, v in serp_data.items() if k not in ['searchParameters', 'credits']}-- # Log the search data for debugging- logger.debug(f"SERP search for '{query}': {json.dumps(serp_data, indent=2)}")-- # Convert to string for the agent- serp_data_str = json.dumps(serp_data)-- return ActionResult(- extracted_content=serp_data_str, include_in_memory=False, include_extracted_content_only_once=True- )-- except Exception as e:- logger.error(f'Error in SERP search: {type(e).__name__}: {e}')- return ActionResult(error=f'Search error: {str(e)}')-- return controller---def create_controller(- use_serp: bool = False,- output_model: type[BaseModel] | None = None,- gmail_tokens_dict: dict[str, str] | None = None,- task: 'Task | None' = None,-):- """Create a controller, optionally with SERP search and Gmail 2FA support"""- if use_serp:- controller = create_controller_with_serp_search(output_model=output_model)- else:- controller = Controller(output_model=output_model)-- # Add Gmail 2FA support if tokens dict is available and task has login_type OTP- if gmail_tokens_dict and task and hasattr(task, 'login_type') and task.login_type == 'OTP':- try:- # Extract username from task - check multiple possible sources- username = None-- # Check if task has email field directly- if hasattr(task, 'username') and getattr(task, 'username', None):- username = getattr(task, 'username')- # Check if email is in task description or other fields- elif hasattr(task, 'confirmed_task') and '@' in task.confirmed_task:- # Extract email from task description using regex- import re-- email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'- matches = re.findall(email_pattern, task.confirmed_task)- if matches:- username = matches[0]-- if username:- # Extract user ID (part before @)- user_id = username.split('@')[0]-- # Look up access token in the dictionary- access_token = gmail_tokens_dict.get(user_id)-- if access_token:- from browser_use.integrations.gmail import register_gmail_actions-- # Register Gmail actions using the access token- controller = register_gmail_actions(controller, access_token=access_token)- logger.info(f'Gmail 2FA integration registered successfully for user {user_id} (OTP task)')- else:- logger.info(f'No Gmail 2FA token found for user {user_id}, running without Gmail integration')- else:- logger.info('No email found in OTP task, running without Gmail integration')-- except Exception as e:- logger.error(f'Failed to setup Gmail integration: {e}')- else:- if gmail_tokens_dict and task:- if not hasattr(task, 'login_type') or task.login_type != 'OTP':- logger.info(f'Task login_type is "{getattr(task, "login_type", "None")}", not OTP - skipping Gmail integration')- else:- logger.info('Gmail 2FA tokens provided but no task or task missing login_type')- else:- logger.info('No Gmail 2FA tokens provided or no task, running without Gmail integration')-- return controller---# ================================================-# Formatting results-# ================================================---def clean_action_dict(action_dict: dict) -> dict:- return {k: clean_action_dict(v) if isinstance(v, dict) else v for k, v in action_dict.items() if v is not None}---async def reformat_agent_history(- agent_history: AgentHistoryList,- task_id: str,- run_id: str,- task: str,- last_message: str,- base_path: str = 'saved_trajectories',- include_result: bool = False,- agent_execution_time: float | None = None,-) -> dict:- # Update directory name- task_dir = Path(base_path) / task_id- trajectory_with_highlights_dir = task_dir / 'trajectory_with_highlights'-- # Create directories- task_dir.mkdir(parents=True, exist_ok=True)- trajectory_with_highlights_dir.mkdir(parents=True, exist_ok=True)-- # Collect screenshot paths and action history- screenshot_paths = []- action_history = []- final_result = None- self_report_completed = False- self_report_success = None- complete_history = []- total_tokens_used = 0 # Initialize token counter-- # Process history items- for step_num, history_item in enumerate(agent_history.history):- # Save screenshot- if history_item.state and history_item.state.screenshot:- screenshot_path = trajectory_with_highlights_dir / f'step_{step_num}.png'- screenshot_paths.append(str(screenshot_path))- # Save the actual screenshot- screenshot_data = base64.b64decode(history_item.state.screenshot)- async with await anyio.open_file(screenshot_path, 'wb') as f:- await f.write(screenshot_data)-- # Get action result content- if history_item.result:- for result in history_item.result:- # We don't want to include the final result in the action history as per the evaluation criteria- if result.extracted_content and result.extracted_content != 'None' and not result.is_done:- action_history.append(result.extracted_content)- # Check if this is the final result- if result.is_done:- final_result = result.extracted_content- self_report_completed = True- self_report_success = result.success-- # Build complete history entry with cleaned model output- model_output = None- if history_item.model_output:- model_output = history_item.model_output.model_dump()- if 'action' in model_output:- # Clean each action in the action list- model_output['action'] = [clean_action_dict(action) for action in model_output['action']]-- step_metadata = history_item.metadata.model_dump() if history_item.metadata else {}- step_info = {- 'step_number': step_num,- 'model_output': model_output,- 'result': [r.model_dump() for r in history_item.result] if history_item.result else None,- 'state': {- 'url': history_item.state.url if history_item.state else None,- 'title': history_item.state.title if history_item.state else None,- },- 'metadata': step_metadata, # Use dumped metadata- }- complete_history.append(step_info)-- # Sum up tokens from metadata- if step_metadata and 'input_tokens' in step_metadata:- try:- total_tokens_used += int(step_metadata['input_tokens'])- except (ValueError, TypeError):- logger.warning(- f"Task {task_id}, Step {step_num}: Could not parse input_tokens '{step_metadata['input_tokens']}' as integer."- )-- # Calculate task duration from metadata (step-based timing)- step_based_duration = None- if complete_history and len(complete_history) > 0:- first_step = complete_history[0].get('metadata', {})- last_step = complete_history[-1].get('metadata', {})- if first_step and last_step:- start_time = first_step.get('step_start_time')- end_time = last_step.get('step_end_time')- if start_time and end_time:- # Ensure timestamps are floats before subtracting- try:- start_time_float = float(start_time)- end_time_float = float(end_time)- step_based_duration = end_time_float - start_time_float- except (ValueError, TypeError) as e:- logger.warning(f'Could not calculate step-based duration due to invalid timestamp format: {e}')-- # Use agent execution time if provided (wall-clock timing around run_agent), otherwise fall back to step-based- task_duration = agent_execution_time if agent_execution_time is not None else step_based_duration-- # Conditionally include the final result in action history- if include_result and final_result and final_result.strip():- action_history = action_history + [final_result]-- # Extract usage data from agent history- usage_data = None- logger.info(f'Agent history usage object: {agent_history.usage}')- logger.info(f'Agent history usage type: {type(agent_history.usage)}')- if hasattr(agent_history, 'usage') and agent_history.usage:- logger.info(f'Agent history usage model_dump: {agent_history.usage.model_dump()}')- usage_data = agent_history.usage.model_dump()- else:- logger.warning('Agent history has no usage data or usage is empty/None')-- # Create results structure with new fields- results = {- 'task_id': task_id,- 'run_id': run_id,- 'task': task,- 'action_history': action_history,- 'screenshot_paths': screenshot_paths,- 'final_result_response': final_result,- 'last_message': last_message,- 'self_report_completed': self_report_completed,- 'self_report_success': self_report_success,- 'complete_history': complete_history,- 'task_duration': task_duration,- 'steps': len(complete_history),- 'tokensUsed': total_tokens_used, # Add total tokens used- 'usage': usage_data, # Add usage data- }-- # Save results file- results_path = task_dir / 'result.json'- async with await anyio.open_file(results_path, 'w') as f:- # Use a custom JSON encoder to handle potential non-serializable types like Path- await f.write(json.dumps(results, indent=2, default=str))-- return results---# ================================================-# Judge task result-# ================================================---@observe_debug()-async def judge_task_result(- model, task_folder: Path, score_threshold: float = 3, use_mind2web: bool = False, judge_repeat_count: int = 1-) -> dict:- """- Judge a single task result using the comprehensive judge system by default,- with optional fallback to the original Online_Mind2Web evaluation.-- Args:- model: The model to use for evaluation- task_folder: Path to the task result folder- score_threshold: Score threshold for image filtering (used only for Mind2Web)- use_mind2web: If True, use the original Online_Mind2Web evaluation instead- judge_repeat_count: Number of times to repeat the judge evaluation (averages over multiple judgments)-- Returns:- Dictionary containing judgment results- """- result_file = task_folder / 'result.json'- if not result_file.exists():- return {- 'task_id': task_folder.name,- 'judgement': 'No result.json found',- 'success': False,- 'error': 'No result.json found',- 'score': 0.0,- }-- try:- async with await anyio.open_file(result_file) as f:- result = json.loads(await f.read())-- # Check if we should use the original Mind2Web evaluation- if use_mind2web:- logger.info(f'Task {task_folder.name}: Using original Online_Mind2Web evaluation')-- # If a Online_Mind2Web_evaluation is already saved, we can skip the eval- if result.get('Online_Mind2Web_evaluation'):- return result.get('Online_Mind2Web_evaluation')-- # Get the screenshot paths, task description, and action history- screenshot_paths = result.get('screenshot_paths', [])- task_description = result.get('task')- action_history = result.get('action_history', [])-- # Use the retry wrapper for evaluation- try:- # Await the async function directly instead of using asyncio.run()- eval_result = await Online_Mind2Web_eval_with_retry(- task_description, action_history, screenshot_paths, model, score_threshold- )-- if eval_result is None:- raise Exception('Evaluation failed after all retries')-- messages, text, system_msg, record, key_points = eval_result-- # Final steps to get judgement - use async invoke directly- judgement_response = await model.ainvoke(messages)- judgement = judgement_response.completion-- if 'success' in judgement.lower().split('status:')[1]: # This is the official criteria for success- evaluation = {- 'task_id': task_folder.name,- 'judgement': judgement,- 'success': True,- 'error': None,- 'score': 1.0,- }- else: # This is the official criteria for failure- evaluation = {- 'task_id': task_folder.name,- 'judgement': judgement,- 'success': False,- 'error': None,- 'score': 0.0,- }-- # Save the Online_Mind2Web_evaluation into the result.json file- result['Online_Mind2Web_evaluation'] = evaluation- async with await anyio.open_file(result_file, 'w') as f:- await f.write(json.dumps(result, indent=2))-- return evaluation-- except Exception as err:- return {- 'task_id': task_folder.name,- 'judgement': f'Mind2Web evaluation failed: {type(err).__name__}: {err}',- 'success': False,- 'error': f'{type(err).__name__}: {err}',- 'score': 0.0,- }-- else:- # Use the new comprehensive judge system (default)- logger.info(f'Task {task_folder.name}: Using comprehensive judge evaluation with {judge_repeat_count} repetition(s)')-- # Check if comprehensive judge result already exists- if result.get('comprehensive_judge_evaluation'):- existing_eval = result['comprehensive_judge_evaluation']- return {- 'task_id': task_folder.name,- 'judgement': existing_eval.get('reasoning', 'Comprehensive evaluation completed'),- 'success': existing_eval.get('passed', False),- 'error': None,- 'score': existing_eval.get('final_score', 0) / 100.0, # Convert to 0-1 scale- 'comprehensive_evaluation': existing_eval,- }-- try:- # Run comprehensive judge evaluation (with repeat and averaging handled in comprehensive_judge.py)- comprehensive_result = await asyncio.wait_for(- evaluate_task_with_comprehensive_judge(- task_folder=task_folder, model=model, max_images=10, judge_repeat_count=judge_repeat_count- ),- timeout=180 * judge_repeat_count, # Increase timeout based on repeat count- )-- if comprehensive_result.get('error'):- return {- 'task_id': task_folder.name,- 'judgement': f'Comprehensive evaluation failed: {comprehensive_result["error"]}',- 'success': False,- 'error': comprehensive_result['error'],- 'score': 0.0,- }-- comp_eval = comprehensive_result.get('comprehensive_judge')- if comp_eval:- return {- 'task_id': task_folder.name,- 'judgement': comp_eval.get('reasoning', 'Comprehensive evaluation completed'),- 'success': comp_eval.get('passed', False),- 'error': None,- 'score': comp_eval.get('final_score', 0) / 100.0, # Convert to 0-1 scale- 'comprehensive_evaluation': comp_eval,- }- else:- return {- 'task_id': task_folder.name,- 'judgement': 'Comprehensive judge failed to return results',- 'success': False,- 'error': 'Comprehensive judge failed to return results',- 'score': 0.0,- }-- except Exception as err:- logger.error(f'Comprehensive judge evaluation failed for {task_folder.name}: {err}')- return {- 'task_id': task_folder.name,- 'judgement': f'Comprehensive judge error: {type(err).__name__}: {err}',- 'success': False,- 'error': f'Comprehensive judge error: {type(err).__name__}: {err}',- 'score': 0.0,- }-- except Exception as err:- return {- 'task_id': task_folder.name,- 'judgement': f'Evaluation failed: {type(err).__name__}: {err}',- 'success': False,- 'error': f'{type(err).__name__}: {err}',- 'score': 0.0,- }---# ================================================-# Main Evaluation Functions-# ================================================---@observe(name='executor', span_type='EXECUTOR') # type: ignore[arg-type]-async def run_agent_with_browser(- browser_session: BrowserSession,- task: Task,- llm: BaseChatModel,- max_steps: int,- use_vision: bool,- use_serp: bool = False,- enable_memory: bool = False,- memory_interval: int = 10,- max_actions_per_step: int = 10,- validate_output: bool = False,- planner_llm: BaseChatModel | None = None,- planner_interval: int = 1,- use_thinking: bool = True,- gmail_tokens_dict: dict[str, str] | None = None,- images_per_step: int = 1,-) -> tuple[AgentHistoryList, str]:- """Run agent with the browser session"""- # Create controller, optionally with SERP search, structured output, and Gmail 2FA support- controller = create_controller(- use_serp=use_serp, output_model=task.output_model, gmail_tokens_dict=gmail_tokens_dict, task=task- )-- # Check for deprecated memory parameters- if enable_memory:- raise ValueError(- 'Memory support has been removed as of version 0.3.2. '- 'The agent context for memory is significantly improved and no longer requires the old memory system. '- "Please remove the 'enable_memory' parameter."- )-- # Set up login cookie monitoring if this is a login task- is_login_task = hasattr(task, 'login_cookie') and task.login_cookie- new_step_callback = None-- if is_login_task:- logger.info(f'π Setting up login cookie monitoring for task {task.task_id}')-- async def login_cookie_step_callback(browser_state_summary, agent_output, step_number):- """Callback to check login cookie after each step"""- try:- if task.login_cookie is not None:- await check_login_cookie_at_step(- browser_session=browser_session, task_id=task.task_id, login_cookie=task.login_cookie, step=step_number- )- else:- logger.warning(f'β Task {task.task_id} Step {step_number}: login_cookie is None, skipping check')- except Exception as e:- logger.warning(f'β Error checking login cookie at step {step_number}: {type(e).__name__}: {e}')-- new_step_callback = login_cookie_step_callback-- agent = Agent(- task=task.confirmed_task,- llm=llm,- controller=controller,- browser_session=browser_session,- use_vision=use_vision,- max_actions_per_step=max_actions_per_step,- validate_output=validate_output,- planner_llm=planner_llm,- planner_interval=planner_interval,- use_thinking=use_thinking,- images_per_step=images_per_step,- source='eval_platform',- calculate_cost=True,- register_new_step_callback=new_step_callback,- )-- # get last message- await agent.run(max_steps=max_steps)- last_input_messages = agent.message_manager.last_input_messages- last_message = last_input_messages[-1].text-- # Save login cookie tracking if this was a login task- if is_login_task:- # Save tracking data to the task folder (will be created later in the pipeline)- # For now, we'll save it when the task folder is available- pass-- return agent.state.history, last_message---@observe(name='evaluate_task_result', span_type='EVALUATOR') # type: ignore[arg-type]-async def evaluate_task_result(- eval_model: BaseChatModel,- task_folder: Path,- task: Task | None = None,- use_mind2web: bool = False,- judge_repeat_count: int = 1,-) -> dict:- """Evaluate the task result"""- # Check if this is a login task that should use both cookie-based and judge evaluation- if task and hasattr(task, 'login_cookie') and task.login_cookie:- logger.info(f'Using combined cookie-based and judge evaluation for login task {task.task_id}')-- # First run the judge evaluation to get comprehensive feedback- judge_result = await judge_task_result(- eval_model, task_folder, score_threshold=3, use_mind2web=use_mind2web, judge_repeat_count=judge_repeat_count- )-- # Then run the cookie-based evaluation to get the actual score- cookie_result = await evaluate_task_with_login_cookie(task.login_cookie, task_folder)-- # Use the score from cookie_result to overwrite judge_result- judge_result['score'] = cookie_result['score']- judge_result['success'] = cookie_result['success']- judge_result['error'] = cookie_result['error']-- # IMPORTANT: Also populate the WebJudge fields so Section 4 appears in the UI- judge_result['judgement'] = cookie_result['judgement']-- # Also overwrite comprehensive judge evaluation if it exists- if 'comprehensive_evaluation' in judge_result and judge_result['comprehensive_evaluation']:- judge_result['comprehensive_evaluation']['passed'] = cookie_result['success']- # Convert score from 0-1 scale to 0-100 scale for comprehensive judge- judge_result['comprehensive_evaluation']['final_score'] = int(cookie_result['score'] * 100)-- return judge_result- else:- return await judge_task_result(- eval_model, task_folder, score_threshold=3, use_mind2web=use_mind2web, judge_repeat_count=judge_repeat_count- )---@observe_debug()-async def cleanup_browser_safe(browser_session: BrowserSession):- """Safe browser cleanup with timeout"""- try:- logger.debug('Browser cleanup: Starting close operation for session')- await asyncio.wait_for(browser_session.kill(), timeout=30)- logger.debug('Browser cleanup: Close operation completed successfully')- except TimeoutError:- logger.warning('Browser cleanup: Timed out after 30 seconds')- except Exception as e:- logger.warning(f'Browser cleanup: Failed with error: {type(e).__name__}: {e}')---# ================================================-# Stage runner and related functions-# ================================================---def save_result_to_server(convex_url: str, secret_key: str, payload: dict) -> bool:- """Save result to server (sync function for use with asyncio.to_thread)"""- return save_task_result_to_server(convex_url, secret_key, payload)---async def run_stage(stage: Stage, stage_func, timeout: int | None = None):- """Generic stage runner with timeout"""- if timeout:- return await asyncio.wait_for(stage_func(), timeout)- return await stage_func()---def determine_current_stage(completed_stages: set) -> Stage:- """Determine current stage based on completed stages"""- if Stage.SAVE_SERVER in completed_stages:- return Stage.SAVE_SERVER- elif Stage.EVALUATE in completed_stages:- return Stage.EVALUATE- elif Stage.FORMAT_HISTORY in completed_stages:- return Stage.FORMAT_HISTORY- elif Stage.RUN_AGENT in completed_stages:- return Stage.RUN_AGENT- elif Stage.SETUP_BROWSER in completed_stages:- return Stage.SETUP_BROWSER- else:- return Stage.SETUP_BROWSER # Default starting stage---@observe(name='evaluation', span_type='EVALUATION') # type: ignore[arg-type]-async def run_task_with_semaphore(- task: Task,- run_id: str,- lmnr_run_id: str | None,- laminar_eval_link: str | None,- convex_url: str,- secret_key: str,- eval_model: BaseChatModel,- llm: BaseChatModel,- max_steps_per_task: int,- headless: bool,- use_vision: bool,- semaphore_runs: asyncio.Semaphore, # Pass semaphore as argument- auth_distribution: dict | None = None, # Pre-fetched auth distribution- github_workflow_url: str | None = None,- use_serp: bool = False,- browser: str = 'local',- enable_memory: bool = False,- memory_interval: int = 10,- max_actions_per_step: int = 10,- validate_output: bool = False,- planner_llm: BaseChatModel | None = None,- planner_interval: int = 1,- include_result: bool = False,- highlight_elements: bool = True,- use_mind2web_judge: bool = False,- use_thinking: bool = True,- gmail_tokens_dict: dict[str, str] | None = None,- judge_repeat_count: int = 1,- images_per_step: int = 1,- default_navigation_timeout: int | None = None,- default_timeout: int | None = None,- minimum_wait_page_load_time: float | None = None,- wait_for_network_idle_page_load_time: float | None = None,- maximum_wait_page_load_time: float | None = None,- wait_between_actions: float | None = None,- stealth: bool = False,-) -> dict:- """Clean pipeline approach for running tasks"""- task_start_time = time.time()- logger.info(f'π Task {task.task_id}: Starting execution pipeline')- logger.info(f'π Task {task.task_id}: Waiting to acquire semaphore (current available: ~{semaphore_runs._value})')- log_system_resources(f'TASK_START_{task.task_id}')-- semaphore_acquired_time = None- async with semaphore_runs:- semaphore_acquired_time = time.time()- wait_time = semaphore_acquired_time - task_start_time- logger.info(- f'β Task {task.task_id}: Semaphore acquired after {wait_time:.2f}s (remaining slots: ~{semaphore_runs._value})'- )- log_system_resources(f'SEMAPHORE_ACQUIRED_{task.task_id}')-- task_result = None- browser_session = None- laminar_task_link = None- datapoint_id = None- agent_execution_time = None # Track agent execution time separately-- try:- if lmnr_run_id:- try:- datapoint_id = await laminar_client.evals.create_datapoint(- eval_id=UUID(lmnr_run_id),- data={- 'task_id': task.task_id,- 'confirmed_task': task.confirmed_task,- 'website': task.website,- 'reference_length': task.reference_length,- 'level': task.level,- 'cluster_id': task.cluster_id,- 'category': task.category,- },- metadata={- 'use_vision': str(use_vision),- 'use_serp': str(use_serp),- 'enable_memory': str(enable_memory),- 'memory_interval': str(memory_interval),- 'max_actions_per_step': str(max_actions_per_step),- 'validate_output': str(validate_output),- 'planner_model': str(planner_llm),- 'planner_interval': str(planner_interval),- 'include_result': str(include_result),- },- trace_id=Laminar.get_trace_id(),- )- # Only create task-specific link if we have the evaluation link- if laminar_eval_link:- laminar_task_link = f'{laminar_eval_link}?traceId={Laminar.get_trace_id()}&datapointId={datapoint_id}'- logger.info(f'Task {task.task_id}: Laminar link: {laminar_task_link}')- else:- logger.debug(f'Task {task.task_id}: No Laminar evaluation link available, task link not created')- except Exception as e:- logger.warning(f'Task {task.task_id}: Failed to create Laminar datapoint: {type(e).__name__}: {e}')- else:- logger.debug(f'Task {task.task_id}: No Laminar run ID available, skipping datapoint creation')-- # Initialize task result and basic setup- task_result = TaskResult(- task.task_id, run_id, task.confirmed_task, task, max_steps_per_task, laminar_task_link, github_workflow_url- )-- task_folder = Path(f'saved_trajectories/{task.task_id}')-- logger.info(f'Task {task.task_id}: Starting execution pipeline.')-- # Send initial progress update to show task is starting- send_progress_update(convex_url, secret_key, run_id, task.task_id, 'starting', 'active', github_workflow_url)-- try:- agent_history = None # Initialize to track agent execution-- # Stage 1: Setup browser- try:- logger.info(f'Task {task.task_id}: Browser setup starting.')- # Send progress update for starting browser setup- send_progress_update(- convex_url, secret_key, run_id, task.task_id, 'setup_browser', 'active', github_workflow_url- )-- browser_session = await run_stage(- Stage.SETUP_BROWSER,- lambda: setup_browser_session(- task,- headless,- highlight_elements,- browser,- default_navigation_timeout,- default_timeout,- minimum_wait_page_load_time,- wait_for_network_idle_page_load_time,- maximum_wait_page_load_time,- wait_between_actions,- stealth,- ),- timeout=120,- )- task_result.stage_completed(Stage.SETUP_BROWSER)- logger.info(f'Task {task.task_id}: Browser session started successfully.')-- # Send progress update for completed browser setup- send_progress_update(- convex_url, secret_key, run_id, task.task_id, 'browser_ready', 'active', github_workflow_url- )- except Exception as e:- error = StageError(Stage.SETUP_BROWSER, 'exception', str(e))- task_result.stage_failed(Stage.SETUP_BROWSER, error)- logger.error(f'Task {task.task_id}: Browser setup failed: {str(e)}')- # Send error progress update- send_progress_update(- convex_url, secret_key, run_id, task.task_id, 'setup_browser', 'failed', github_workflow_url, None, str(e)- )- # Continue to server save instead of early return-- # Stage 2: Run agent- if browser_session: # Only run agent if browser setup succeeded- try:- logger.info(f'Task {task.task_id}: Agent run starting.')- # Send progress update for starting agent run- send_progress_update(- convex_url, secret_key, run_id, task.task_id, 'run_agent', 'active', github_workflow_url- )-- # Handle auth information if task requires it- task_with_auth = task- if hasattr(task, 'auth_keys') and task.auth_keys:- # Validate auth_keys is a list- if isinstance(task.auth_keys, list) and len(task.auth_keys) > 0:- if auth_distribution:- logger.info(- f'Task {task.task_id}: Using pre-fetched auth distribution for auth_keys: {task.auth_keys}'- )- auth_info_text = format_auth_info_for_agent(auth_distribution, task.auth_keys)- if auth_info_text:- # Create a modified task with auth info appended- class TaskWithAuth(Task):- def __init__(self, original_task: Task, auth_text: str):- # Copy all attributes from original task- for attr_name in dir(original_task):- if not attr_name.startswith('__'):- setattr(self, attr_name, getattr(original_task, attr_name))- # Modify the confirmed_task to include auth info- self.confirmed_task = original_task.confirmed_task + auth_text-- task_with_auth = TaskWithAuth(task, auth_info_text)- logger.info(f'Task {task.task_id}: Auth info added to task description')- else:- logger.warning(- f'Task {task.task_id}: No matching auth info found for keys: {task.auth_keys}'- )- else:- logger.warning(f'Task {task.task_id}: Auth keys specified but no auth distribution available')- else:- logger.warning(f'Task {task.task_id}: auth_keys is not a valid list: {task.auth_keys}')-- # Start timing for agent execution only- agent_start_time = time.time()-- agent_history, last_message = await run_stage(- Stage.RUN_AGENT,- lambda: run_agent_with_browser(- browser_session,- task_with_auth,- llm,- max_steps_per_task,- use_vision,- use_serp,- enable_memory,- memory_interval,- max_actions_per_step,- validate_output,- planner_llm,- planner_interval,- use_thinking,- gmail_tokens_dict,- images_per_step,- ),- timeout=1000,- )-- # End timing for agent execution only- agent_end_time = time.time()- agent_execution_time = agent_end_time - agent_start_time-- task_result.stage_completed(Stage.RUN_AGENT)- logger.info(f'Task {task.task_id}: Agent run completed in {agent_execution_time:.2f}s.')-- # Save login cookie tracking data if this was a login task- if hasattr(task, 'login_cookie') and task.login_cookie:- try:- await save_login_cookie_tracking(task_folder, task.task_id)- except Exception as e:- logger.warning(- f'Failed to save login cookie tracking for task {task.task_id}: {type(e).__name__}: {e}'- )-- # Send progress update for completed agent run- send_progress_update(- convex_url, secret_key, run_id, task.task_id, 'agent_completed', 'active', github_workflow_url- )- except Exception as e:- error = StageError(Stage.RUN_AGENT, 'exception', str(e))- task_result.stage_failed(Stage.RUN_AGENT, error)- logger.error(f'Task {task.task_id}: Agent run failed: {str(e) + " " + str(e.__traceback__)}')- # Send error progress update- send_progress_update(- convex_url, secret_key, run_id, task.task_id, 'run_agent', 'failed', github_workflow_url, None, str(e)- )-- # Continue to server save instead of early return-- # Stage 3: Format history (MOVED OUTSIDE browser_session block)- if agent_history is not None: # Only format if agent ran successfully- try:- logger.info(f'Task {task.task_id}: History formatting starting.')- formatted_data = await run_stage(- Stage.FORMAT_HISTORY,- lambda: reformat_agent_history(- agent_history,- task.task_id,- run_id,- task.confirmed_task,- last_message,- include_result=include_result,- agent_execution_time=agent_execution_time, # Pass agent execution time- ),- )- task_result.stage_completed(Stage.FORMAT_HISTORY, formatted_data)- logger.info(f'Task {task.task_id}: Agent history formatted.')- except Exception as e:- error = StageError(Stage.FORMAT_HISTORY, 'exception', str(e))- task_result.stage_failed(Stage.FORMAT_HISTORY, error)- logger.error(f'Task {task.task_id}: History formatting failed: {str(e)}')- # Continue to server save instead of early return-- # Stage 4: Evaluate (MOVED OUTSIDE browser_session block)- if task_result.has_execution_data() and Stage.EVALUATE not in task_result.completed_stages:- try:- logger.info(f'Task {task.task_id}: Evaluation starting.')- evaluation = await run_stage(- Stage.EVALUATE,- lambda: evaluate_task_result(eval_model, task_folder, task, use_mind2web_judge, judge_repeat_count),- timeout=300 * judge_repeat_count, # Increase timeout based on repeat count- )- task_result.stage_completed(Stage.EVALUATE, evaluation)- logger.info(f'Task {task.task_id}: Evaluation completed.')-- if lmnr_run_id and datapoint_id:- await laminar_client.evals.update_datapoint(- eval_id=UUID(lmnr_run_id),- datapoint_id=datapoint_id,- scores={- 'accuracy': evaluation['score'],- },- )- except Exception as e:- error = StageError(Stage.EVALUATE, 'exception', str(e))- task_result.stage_failed(Stage.EVALUATE, error)- logger.error(f'Task {task.task_id}: Evaluation failed: {str(e)}')-- # Stage 5: Save to server (MOVED OUTSIDE browser_session block - ALWAYS attempt)- try:- logger.info(f'Task {task.task_id}: Saving result to server.')- # Only save to server if URLs are provided (skip for single task mode)- if convex_url and secret_key:- await run_stage(- Stage.SAVE_SERVER,- lambda: asyncio.to_thread(- save_result_to_server,- convex_url,- secret_key,- task_result.server_payload if task_result else {},- ),- timeout=60,- )- task_result.stage_completed(Stage.SAVE_SERVER)- logger.info(f'Task {task.task_id}: Successfully saved result to server.')- else:- # Single task mode - skip server save but mark as completed- logger.info(f'Task {task.task_id}: Skipping server save (single task mode)')- task_result.stage_completed(Stage.SAVE_SERVER)- except Exception as e:- error = StageError(Stage.SAVE_SERVER, 'exception', str(e))- task_result.stage_failed(Stage.SAVE_SERVER, error)- task_result.mark_server_save_failed(str(e))- logger.error(f'Task {task.task_id}: Server save failed: {str(e)}')-- except TimeoutError:- current_stage = determine_current_stage(task_result.completed_stages)- error = StageError(current_stage, 'timeout', 'Operation timed out')- task_result.stage_failed(current_stage, error)- logger.error(f'Task {task.task_id}: {current_stage.value} timed out')-- # Attempt to save result even if timeout occurred- try:- logger.info(f'Task {task.task_id}: Attempting server save after timeout.')- await run_stage(- Stage.SAVE_SERVER,- lambda: asyncio.to_thread(- save_result_to_server, convex_url, secret_key, task_result.server_payload if task_result else {}- ),- timeout=30, # Shorter timeout for emergency save- )- task_result.stage_completed(Stage.SAVE_SERVER)- except Exception as save_e:- task_result.mark_server_save_failed(str(save_e))- logger.error(f'Task {task.task_id}: Emergency server save after timeout failed: {str(save_e)}')-- except asyncio.CancelledError:- task_result.mark_cancelled()- logger.warning(f'Task {task.task_id}: Task was cancelled')-- # Attempt to save result even if cancelled- try:- logger.info(f'Task {task.task_id}: Attempting server save after cancellation.')- await run_stage(- Stage.SAVE_SERVER,- lambda: asyncio.to_thread(- save_result_to_server, convex_url, secret_key, task_result.server_payload if task_result else {}- ),- timeout=30, # Shorter timeout for emergency save- )- task_result.stage_completed(Stage.SAVE_SERVER)- except Exception as save_e:- task_result.mark_server_save_failed(str(save_e))- logger.error(f'Task {task.task_id}: Emergency server save after cancellation failed: {str(save_e)}')-- except Exception as e:- task_result.mark_critical_error(str(e))- logger.critical(f'Task {task.task_id}: Critical error: {str(e)}', exc_info=True)-- # Attempt to save result even if critical error occurred- try:- logger.info(f'Task {task.task_id}: Attempting server save after critical error.')- await run_stage(- Stage.SAVE_SERVER,- lambda: asyncio.to_thread(- save_result_to_server, convex_url, secret_key, task_result.server_payload if task_result else {}- ),- timeout=30, # Shorter timeout for emergency save- )- task_result.stage_completed(Stage.SAVE_SERVER)- except Exception as save_e:- task_result.mark_server_save_failed(str(save_e))- logger.error(f'Task {task.task_id}: Emergency server save after critical error failed: {str(save_e)}')-- except Exception as init_error:- # Handle catastrophic initialization errors- logger.critical(f'Task {task.task_id}: Catastrophic initialization error: {str(init_error)}', exc_info=True)- if task_result is None:- # Create minimal task result for server reporting- try:- task_result = TaskResult(- task.task_id,- run_id,- task.confirmed_task,- task,- max_steps_per_task,- laminar_task_link,- github_workflow_url,- )- task_result.mark_critical_error(f'Initialization failed: {str(init_error)}')- except Exception as result_error:- logger.critical(f'Task {task.task_id}: Cannot create TaskResult: {str(result_error)}')- # Return minimal error status as last resort- return {- 'task_id': task.task_id,- 'success': False,- 'error': f'Catastrophic initialization failure: {str(init_error)}',- }-- # Try emergency server save- try:- logger.info(f'Task {task.task_id}: Attempting emergency server save after initialization error.')- await asyncio.to_thread(- save_result_to_server, convex_url, secret_key, task_result.server_payload if task_result else {}- )- except Exception as save_e:- logger.error(f'Task {task.task_id}: Emergency server save after initialization error failed: {str(save_e)}')-- finally:- # Always cleanup browser if it was created- if browser_session:- logger.info(f'Task {task.task_id}: Starting browser cleanup')- await cleanup_browser_safe(browser_session)- logger.info(f'Task {task.task_id}: Browser cleanup completed')- else:- logger.info(f'Task {task.task_id}: No browser to cleanup')-- task_end_time = time.time()- total_task_time = task_end_time - task_start_time- semaphore_hold_time = task_end_time - (semaphore_acquired_time or task_start_time)-- # Log both pipeline time and agent execution time- if agent_execution_time is not None:- logger.info(- f'π Task {task.task_id}: Agent executed in {agent_execution_time:.2f}s (total pipeline: {total_task_time:.2f}s, semaphore held: {semaphore_hold_time:.2f}s)'- )- else:- logger.info(- f'π Task {task.task_id}: Pipeline completed in {total_task_time:.2f}s (agent did not run, semaphore held: {semaphore_hold_time:.2f}s)'- )-- logger.info(f'π Task {task.task_id}: About to release semaphore (remaining slots will be: ~{semaphore_runs._value + 1})')- log_system_resources(f'TASK_END_{task.task_id}')-- final_result = (- task_result.get_local_status()- if task_result- else {'task_id': task.task_id, 'success': False, 'error': 'Task result not available'}- )-- logger.info(- f'π― Task {task.task_id}: Final status - Success: {final_result.get("success", False)}, Error: {final_result.get("error", "None")}'- )- return final_result---@observe_debug()-async def run_multiple_tasks(- tasks: list[Task],- llm: BaseChatModel,- run_id: str,- lmnr_run_id: str | None,- laminar_eval_link: str | None,- convex_url: str,- secret_key: str,- eval_model: BaseChatModel,- auth_distribution: dict | None = None,- github_workflow_url: str | None = None,- max_parallel_runs: int = 3,- max_steps_per_task: int = 25,- start_index: int = 0,- end_index: int | None = None,- headless: bool = False,- use_vision: bool = True,- use_serp: bool = False,- browser: str = 'local',- enable_memory: bool = False,- memory_interval: int = 10,- max_actions_per_step: int = 10,- validate_output: bool = False,- planner_llm: BaseChatModel | None = None,- planner_interval: int = 1,- include_result: bool = False,- highlight_elements: bool = True,- use_mind2web_judge: bool = False,- use_thinking: bool = True,- gmail_tokens_dict: dict[str, str] | None = None,- judge_repeat_count: int = 1,- images_per_step: int = 1,- default_navigation_timeout: int | None = None,- default_timeout: int | None = None,- minimum_wait_page_load_time: float | None = None,- wait_for_network_idle_page_load_time: float | None = None,- maximum_wait_page_load_time: float | None = None,- wait_between_actions: float | None = None,- stealth: bool = False,-) -> dict:- """- Run multiple tasks in parallel and evaluate results.- """- batch_start_time = time.time()- logger.info(f'π BATCH START: Creating semaphore with max_parallel_runs={max_parallel_runs}')- log_system_resources('BATCH_START')-- semaphore_runs = asyncio.Semaphore(max_parallel_runs)- tasks_to_run = tasks[start_index:end_index] if end_index else tasks[start_index:]-- logger.info(f'π Starting {len(tasks_to_run)} tasks with parallel limit of {max_parallel_runs}')- logger.info(f'π Task range: {start_index} to {end_index or len(tasks)} (total tasks available: {len(tasks)})')-- # Start resource monitoring- await start_resource_monitoring(interval=30)-- # Setup signal handlers for graceful shutdown- setup_signal_handlers()-- # Create a heartbeat task for long-running operations- heartbeat_task = None- heartbeat_stop_event = asyncio.Event()-- @observe_debug()- async def heartbeat_logger():- """Log periodic heartbeat to show the process is alive"""- heartbeat_count = 0- while not heartbeat_stop_event.is_set():- try:- await asyncio.wait_for(heartbeat_stop_event.wait(), timeout=60.0) # 1-minute heartbeat- break # Event was set, exit- except TimeoutError:- heartbeat_count += 1- elapsed = time.time() - batch_start_time- logger.info(f'π HEARTBEAT {heartbeat_count}: Batch still running after {elapsed:.1f}s')- log_system_resources('HEARTBEAT')-- # Check for potential issues- resources = get_system_resources()- if resources['memory_percent'] > 90:- logger.critical(f'π¨ CRITICAL: Memory usage at {resources["memory_percent"]:.1f}% - potential OOM risk!')- if resources['chrome_process_count'] > 50:- logger.warning(f'β οΈ HIGH BROWSER PROCESS COUNT: {resources["chrome_process_count"]} Chrome processes')-- try:- # Start heartbeat logging- heartbeat_task = asyncio.create_task(heartbeat_logger())- logger.info('π Heartbeat monitoring started')-- # Run all tasks in parallel with additional parameters- logger.info(f'π Launching {len(tasks_to_run)} parallel task executions...')-- task_results = await asyncio.gather(- *(- run_task_with_semaphore(- task=task,- run_id=run_id,- lmnr_run_id=lmnr_run_id,- laminar_eval_link=laminar_eval_link,- convex_url=convex_url,- secret_key=secret_key,- eval_model=eval_model,- llm=llm, # Pass the agent LLM- max_steps_per_task=max_steps_per_task,- headless=headless,- use_vision=use_vision,- semaphore_runs=semaphore_runs, # Pass the semaphore- auth_distribution=auth_distribution, # Pass the pre-fetched auth distribution- github_workflow_url=github_workflow_url,- use_serp=use_serp,- browser=browser,- enable_memory=enable_memory,- memory_interval=memory_interval,- max_actions_per_step=max_actions_per_step,- validate_output=validate_output,- planner_llm=planner_llm,- planner_interval=planner_interval,- include_result=include_result,- highlight_elements=highlight_elements,- use_mind2web_judge=use_mind2web_judge,- use_thinking=use_thinking,- gmail_tokens_dict=gmail_tokens_dict,- judge_repeat_count=judge_repeat_count,- images_per_step=images_per_step,- default_navigation_timeout=default_navigation_timeout,- default_timeout=default_timeout,- minimum_wait_page_load_time=minimum_wait_page_load_time,- wait_for_network_idle_page_load_time=wait_for_network_idle_page_load_time,- maximum_wait_page_load_time=maximum_wait_page_load_time,- wait_between_actions=wait_between_actions,- stealth=stealth,- )- for task in tasks_to_run- ),- return_exceptions=True, # Prevent task cancellation cascade- )-- logger.info(f'β All {len(tasks_to_run)} parallel task executions completed')-- except Exception as e:- logger.critical(f'π¨ CRITICAL ERROR in batch execution: {type(e).__name__}: {e}', exc_info=True)- log_system_resources('BATCH_ERROR')- # Create error results for all tasks- task_results = [- {'task_id': task.task_id, 'success': False, 'error': f'Batch execution failed: {str(e)}'} for task in tasks_to_run- ]-- finally:- # Cleanup: Stop heartbeat and resource monitoring- batch_end_time = time.time()- total_batch_time = batch_end_time - batch_start_time- logger.info(f'π BATCH END: Total execution time {total_batch_time:.2f}s')-- if heartbeat_task and not heartbeat_task.done():- heartbeat_stop_event.set()- try:- await asyncio.wait_for(heartbeat_task, timeout=5.0)- except TimeoutError:- logger.warning('Heartbeat task did not stop gracefully')- heartbeat_task.cancel()-- await stop_resource_monitoring()-- log_system_resources('BATCH_CLEANUP')-- # Process task results and handle any exceptions returned by gather- processed_results = []- successful_tasks = 0- failed_tasks = 0-- for i, result in enumerate(task_results):- if isinstance(result, Exception):- logger.error(f'β Task {i} failed with exception: {type(result).__name__}: {result}')- task_id = tasks_to_run[i].task_id if i < len(tasks_to_run) else f'unknown_task_{i}'- processed_results.append({'task_id': task_id, 'success': False, 'error': str(result)})- failed_tasks += 1- else:- processed_results.append(result)- if isinstance(result, dict) and result.get('success', False):- successful_tasks += 1- else:- failed_tasks += 1-- logger.info(f'π FINAL RESULTS: {len(tasks_to_run)} tasks completed. Success: {successful_tasks}, Failed: {failed_tasks}')- logger.info(f'π Success rate: {successful_tasks / len(tasks_to_run) * 100:.1f}%')-- logger.info('π All tasks completed.')-- return {'task_results': processed_results}---# ================================================-# Main Evaluation Pipeline-# ================================================---@observe_debug()-async def run_evaluation_pipeline(- tasks: list[Task],- llm: BaseChatModel,- run_id: str,- test_case: str,- user_message: str,- convex_url: str,- secret_key: str,- eval_model: BaseChatModel,- auth_distribution: dict | None = None,- github_workflow_url: str | None = None,- max_parallel_runs: int = 3,- max_steps_per_task: int = 25,- start_index: int = 0,- end_index: int | None = None,- headless: bool = False,- use_vision: bool = True,- use_serp: bool = False,- browser: str = 'local',- enable_memory: bool = False,- memory_interval: int = 10,- max_actions_per_step: int = 10,- validate_output: bool = False,- planner_llm: BaseChatModel | None = None,- planner_interval: int = 1,- include_result: bool = False,- laminar_eval_id: str | None = None,- highlight_elements: bool = True,- use_mind2web_judge: bool = False,- use_thinking: bool = True,- gmail_tokens_dict: dict[str, str] | None = None,- judge_repeat_count: int = 1,- images_per_step: int = 1,- default_navigation_timeout: int | None = None,- default_timeout: int | None = None,- minimum_wait_page_load_time: float | None = None,- wait_for_network_idle_page_load_time: float | None = None,- maximum_wait_page_load_time: float | None = None,- wait_between_actions: float | None = None,- stealth: bool = False,-) -> dict:- """- Complete evaluation pipeline that handles Laminar setup and task execution in the same event loop- """- # --- Use provided Laminar Evaluation ID or skip tracking ---- lmnr_run_id = None- laminar_eval_link = None-- if laminar_eval_id:- # Use existing evaluation ID provided from frontend- lmnr_run_id = laminar_eval_id- project_id = 'f07da4a9-b7de-488a-91e3-e17c5f6d676a'- laminar_eval_link = f'https://www.lmnr.ai/project/{project_id}/evaluations/{lmnr_run_id}'- logger.info(f'π Using provided Laminar evaluation ID: {lmnr_run_id}')- logger.info(f'π Laminar evaluation link: {laminar_eval_link}')- else:- # No Laminar evaluation ID provided, skip tracking- logger.info('π No Laminar evaluation ID provided, skipping Laminar tracking')- # --------------------------- # Update run data with Laminar link- # run_data_update = {'laminarEvalLink': laminar_eval_link}- # TODO: Update the run data on the server with the Laminar link if needed-- # Run the tasks- return await run_multiple_tasks(- tasks=tasks,- llm=llm,- run_id=run_id,- lmnr_run_id=lmnr_run_id,- laminar_eval_link=laminar_eval_link,- convex_url=convex_url,- secret_key=secret_key,- eval_model=eval_model,- auth_distribution=auth_distribution,- github_workflow_url=github_workflow_url,- max_parallel_runs=max_parallel_runs,- max_steps_per_task=max_steps_per_task,- start_index=start_index,- end_index=end_index,- headless=headless,- use_vision=use_vision,- use_serp=use_serp,- browser=browser,- enable_memory=enable_memory,- memory_interval=memory_interval,- max_actions_per_step=max_actions_per_step,- validate_output=validate_output,- planner_llm=planner_llm,- planner_interval=planner_interval,- include_result=include_result,- highlight_elements=highlight_elements,- use_mind2web_judge=use_mind2web_judge,- use_thinking=use_thinking,- gmail_tokens_dict=gmail_tokens_dict,- judge_repeat_count=judge_repeat_count,- images_per_step=images_per_step,- default_navigation_timeout=default_navigation_timeout,- default_timeout=default_timeout,- minimum_wait_page_load_time=minimum_wait_page_load_time,- wait_for_network_idle_page_load_time=wait_for_network_idle_page_load_time,- maximum_wait_page_load_time=maximum_wait_page_load_time,- wait_between_actions=wait_between_actions,- stealth=stealth,- )---# ================================================-# Main Evaluation Pipeline Execution-# ================================================---if __name__ == '__main__':- parser = argparse.ArgumentParser(description='Run and evaluate browser automation tasks')- parser.add_argument('--parallel-runs', type=int, default=3, help='Number of parallel tasks to run')- parser.add_argument('--max-steps', type=int, default=25, help='Maximum steps per task')- parser.add_argument('--start', type=int, default=0, help='Start index')- parser.add_argument('--end', type=int, default=None, help='End index (exclusive)')- parser.add_argument('--headless', action='store_true', help='Run in headless mode')-- parser.add_argument(- '--model', type=str, default='gpt-4o', choices=list(SUPPORTED_MODELS.keys()), help='Model to use for the agent'- )- parser.add_argument(- '--eval-model', type=str, default='gpt-4.1', choices=list(SUPPORTED_MODELS.keys()), help='Model to use for evaluation'- )- parser.add_argument('--no-vision', action='store_true', help='Disable vision capabilities in the agent')-- parser.add_argument('--user-message', type=str, default='', help='User message to include in the run')- parser.add_argument('--eval-group', type=str, default='', help='Evaluation group to include in the run')- parser.add_argument('--developer-id', type=str, default=None, help='Name of the developer starting the run')- parser.add_argument('--use-serp', action='store_true', help='Use SERP search instead of Google search')- parser.add_argument(- '--browser',- type=str,- default='local',- help='Browser to use: local, anchor-browser, brightdata, browserbase, hyperbrowser, browser-use (default: local)',- )- parser.add_argument('--enable-memory', action='store_true', help='Enable mem0 memory system for agents')- parser.add_argument('--memory-interval', type=int, default=10, help='Memory creation interval (default: 10 steps)')- parser.add_argument('--max-actions-per-step', type=int, default=10, help='Maximum number of actions per step (default: 10)')- parser.add_argument('--validate-output', action='store_true', help='Enable output validation using LLM')- parser.add_argument(- '--planner-model',- type=str,- default=None,- choices=list(SUPPORTED_MODELS.keys()),- help='Model to use for planning (separate from main agent model)',- )- parser.add_argument('--planner-interval', type=int, default=1, help='Run planner every N steps (default: 1)')- parser.add_argument(- '--judge-repeat-count',- type=int,- default=1,- help='Number of times to repeat the judge evaluation for each task (averages over multiple judgments)',- )- parser.add_argument(- '--images-per-step',- type=int,- default=1,- help='Number of screenshots to include per step (1=current only, 2=current+previous, etc.)',- )- parser.add_argument(- '--test-case', type=str, default='OnlineMind2Web', help='Name of the test case to fetch (default: OnlineMind2Web)'- )- parser.add_argument(- '--run-id',- type=str,- default=None,- help='Existing run ID to continue adding results to (if not provided, a new run will be started)',- )- parser.add_argument(- '--include-result',- action='store_true',- help='Include result flag (functionality to be implemented)',- )- parser.add_argument(- '--no-highlight-elements',- action='store_false',- dest='highlight_elements',- default=True,- help='Disable highlighting of interactive elements on the page (highlighting is enabled by default)',- )- parser.add_argument(- '--laminar-eval-id',- type=str,- default=None,- help='Existing Laminar evaluation ID to use (if not provided, a new evaluation will be created)',- )- parser.add_argument('--use-mind2web-judge', action='store_true', help='Use original judge')- parser.add_argument('--no-thinking', action='store_true', help='Disable thinking in agent system prompt')- parser.add_argument('--github-workflow-url', type=str, default=None, help='GitHub workflow URL for tracking')-- # Browser timeout and stealth configuration arguments- parser.add_argument('--default-navigation-timeout', type=int, default=None, help='Default navigation timeout in milliseconds')- parser.add_argument('--default-timeout', type=int, default=None, help='Default timeout in milliseconds')- parser.add_argument(- '--minimum-wait-page-load-time', type=float, default=None, help='Minimum wait time for page load in seconds'- )- parser.add_argument(- '--wait-for-network-idle-page-load-time', type=float, default=None, help='Wait time for network idle page load in seconds'- )- parser.add_argument(- '--maximum-wait-page-load-time', type=float, default=None, help='Maximum wait time for page load in seconds'- )- parser.add_argument('--wait-between-actions', type=float, default=None, help='Wait time between actions in seconds')- parser.add_argument('--stealth', action='store_true', help='Enable stealth mode for browser')-- # Gmail 2FA support arguments- parser.add_argument(- '--gmail-2fa-tokens',- type=str,- default=None,- help='JSON dictionary of user IDs to access tokens for Gmail 2FA (e.g., \'{"user123": "token1", "user456": "token2"}\')',- )-- # Single task mode arguments- parser.add_argument('--task-text', type=str, default=None, help='Task description for single task mode')- parser.add_argument('--task-website', type=str, default=None, help='Task website for single task mode')- # Keep task-id for backward compatibility but make it optional- parser.add_argument('--task-id', type=str, default=None, help='Optional task ID (auto-generated if not provided)')-- args = parser.parse_args()-- # Set up logging - Make sure logger is configured before use in fetch function- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')- logger = logging.getLogger(__name__) # Define logger for the module-- logger.info('Running tasks...')-- # Parse Gmail 2FA tokens - handle GitHub Actions raw object format- gmail_tokens_dict = None- if args.gmail_2fa_tokens:- raw_tokens = args.gmail_2fa_tokens- logger.info(f'π§ Raw Gmail 2FA tokens received: "{raw_tokens}"')-- # Check if GitHub Actions passed us something like "[object Object]" or similar- if raw_tokens in ['[object Object]', 'null', '', '{}']:- logger.info('π§ GitHub Actions passed placeholder value, no Gmail tokens available')- gmail_tokens_dict = None- else:- try:- # First try parsing as valid JSON (in case it's already proper JSON)- gmail_tokens_dict = json.loads(raw_tokens)- logger.info(f'π§ Successfully parsed as JSON - Gmail 2FA tokens count: {len(gmail_tokens_dict)}')- logger.info(f'π§ Gmail 2FA users: {list(gmail_tokens_dict.keys())}')- except json.JSONDecodeError:- # If JSON parsing fails, try to parse GitHub Actions malformed toJSON format- try:- logger.info('π§ JSON parsing failed, attempting to parse GitHub Actions malformed format...')-- # Handle GitHub Actions toJSON format: { key: value, key2: value2 }- if raw_tokens.strip() and raw_tokens.strip() not in ['null', '{}']:- # Remove outer braces and parse line by line- content = raw_tokens.strip().strip('{}').strip()-- if content:- tokens = {}- lines = [line.strip() for line in content.split('\n') if line.strip()]-- for line in lines:- # Remove trailing comma if present- line = line.rstrip(',')-- if ':' in line:- # Split on first colon only- key, value = line.split(':', 1)- key = key.strip()- value = value.strip()-- # Store the key-value pair- tokens[key] = value-- if tokens:- gmail_tokens_dict = tokens- logger.info('π§ Successfully parsed malformed GitHub Actions format')- logger.info(f'π§ Gmail 2FA tokens count: {len(gmail_tokens_dict)}')- logger.info(f'π§ Gmail 2FA users: {list(gmail_tokens_dict.keys())}')- else:- logger.warning('π§ No tokens found in malformed format')- gmail_tokens_dict = None- else:- logger.warning('π§ Empty content in malformed format')- gmail_tokens_dict = None- else:- logger.info('π§ Raw tokens empty or null')- gmail_tokens_dict = None- except Exception as e:- logger.error(f'π§ Failed to parse malformed GitHub Actions format: {type(e).__name__}: {e}')- gmail_tokens_dict = None- else:- logger.info('π§ Gmail 2FA tokens: None or empty')- # Run tasks and evaluate- load_dotenv()-- # --- Load Environment Variables (Always) ---- CONVEX_URL = os.getenv('EVALUATION_TOOL_URL') or ''- SECRET_KEY = os.getenv('EVALUATION_TOOL_SECRET_KEY') or ''-- # --- Load Tasks (Either Single Task or from Server) ---- tasks = []- task_id = None # Initialize for proper scoping- auth_distribution = None # Initialize auth distribution-- # Check if this is single task mode- if args.task_text:- # Generate task ID if not provided- task_id = args.task_id or f'single_task_{int(time.time())}_{hash(args.task_text) % 10000}'- logger.info(f'Single task mode: Running task {task_id}')-- # Create a single task- single_task = Task(- task_id=task_id,- confirmed_task=args.task_text,- website=args.task_website, # Optional website- )- tasks = [single_task]- logger.info(f'Single task mode: Created task {task_id}')-- else:- # Original multi-task mode - fetch from server- if not CONVEX_URL or not SECRET_KEY:- logger.error('Error: EVALUATION_TOOL_URL or EVALUATION_TOOL_SECRET_KEY environment variables not set.')- exit(1) # Exit if config is missing-- logger.info(f"Attempting to fetch task list '{args.test_case}' from server...")- fetched_task_data = fetch_tasks_from_server(CONVEX_URL, SECRET_KEY, args.test_case)-- if fetched_task_data is None:- logger.error('Failed to fetch tasks from the server. Exiting.')- exit(1) # Exit if fetch fails-- try:- tasks = [Task(**task_data) for task_data in fetched_task_data]- logger.info(f'Successfully loaded {len(tasks)} tasks from the server.')- except (TypeError, ValueError) as e:- logger.error(- f'Error creating Task objects from fetched data. Ensure the data structure includes required fields (task_id, confirmed_task). Known optional fields: website, reference_length, level, cluster_id, login_cookie, login_type, category, auth_keys. Any additional fields will be accepted dynamically. Error: {type(e).__name__}: {e}'- )- logger.error(f'First item in fetched data: {fetched_task_data[0] if fetched_task_data else "None"}')- exit(1)-- # --- Fetch Auth Distribution Once (if any tasks need auth) ---- tasks_with_auth = [- task- for task in tasks- if hasattr(task, 'auth_keys') and task.auth_keys and isinstance(task.auth_keys, list) and len(task.auth_keys) > 0- ]- if tasks_with_auth and CONVEX_URL and SECRET_KEY:- logger.info(f'Found {len(tasks_with_auth)} tasks requiring auth. Fetching auth distribution...')- auth_distribution = fetch_auth_distribution_from_server(CONVEX_URL, SECRET_KEY)- if auth_distribution:- logger.info(- f'Successfully fetched auth distribution with login info for: {list(auth_distribution.get("loginInfo", {}).keys())}'- )- else:- logger.warning('Failed to fetch auth distribution. Tasks requiring auth may fail.')- elif tasks_with_auth:- logger.warning(f'Found {len(tasks_with_auth)} tasks requiring auth but no server config available')- # ------------------------------- # --- Start Run on Server (with optional existing Run ID) ---- if args.run_id:- logger.info(f'Initializing existing run ID: {args.run_id} with git info...')- else:- logger.info('Attempting to start a new run on the server...')-- # Get git info- git_info = get_git_info()-- # Collect additional data from args to store with the run- additional_run_data = {- 'max_steps': args.max_steps,- 'parallel_runs': args.parallel_runs,- 'start_index': args.start,- 'end_index': args.end,- 'headless': args.headless,- 'use_vision': not args.no_vision,- 'task_source': args.test_case,- 'llm_judge': args.eval_model,- 'use_serp': args.use_serp,- 'enable_memory': args.enable_memory,- 'memory_interval': args.memory_interval,- 'max_actions_per_step': args.max_actions_per_step,- 'validate_output': args.validate_output,- 'planner_model': args.planner_model,- 'planner_interval': args.planner_interval,- 'include_result': args.include_result,- 'judge_repeat_count': args.judge_repeat_count,- 'images_per_step': args.images_per_step,- 'default_navigation_timeout': args.default_navigation_timeout,- 'default_timeout': args.default_timeout,- 'minimum_wait_page_load_time': args.minimum_wait_page_load_time,- 'wait_for_network_idle_page_load_time': args.wait_for_network_idle_page_load_time,- 'maximum_wait_page_load_time': args.maximum_wait_page_load_time,- 'wait_between_actions': args.wait_between_actions,- 'stealth': args.stealth,- }-- run_data = {- 'model': args.model,- 'gitBranch': git_info['branch'],- 'gitCommitHash': git_info['hash'],- 'gitCommitTimestamp': git_info['timestamp'],- 'gitRepo': git_info['repo'],- 'userMessage': args.user_message,- 'evalGroup': args.eval_group,- 'developerId': args.developer_id,- 'totalTasks': 1 if args.task_text else (len(tasks) - args.start if args.end is None else args.end - args.start),- 'testCaseName': args.test_case,- 'additionalData': additional_run_data,- 'laminarEvalLink': None, # Will be updated after evaluation creation- }-- # For single task mode, use provided run ID if available, otherwise skip server run creation- if args.task_text:- # Single task mode - use provided run_id (from GitHub Actions) or generate local one- if args.run_id:- run_id = args.run_id- logger.info(f'Single task mode: Using provided run ID {run_id}')- else:- # Fallback for local single task runs without server- safe_task_id = task_id or 'unknown'- run_id = f'local_single_task_{safe_task_id}_{int(time.time())}'- logger.info(f'Single task mode: Using local run ID {run_id}')- else:- # Multi-task mode - use server- run_id = start_new_run(CONVEX_URL, SECRET_KEY, run_data, existing_run_id=args.run_id)-- if not run_id:- logger.error('Failed to start/initialize run on the server. Exiting.')- exit(1)-- logger.info(f'Successfully obtained run ID: {run_id}. Proceeding with tasks...')-- # Log search mode being used- if args.use_serp:- if SERPER_API_KEY:- logger.info('π Using SERP search (Serper API) instead of Google search')- else:- logger.warning('β οΈ --use-serp flag provided but SERPER_API_KEY not set. Search will fail!')- else:- logger.info('π Using default Google search')-- # Log browser mode being used- if args.browser == 'anchor-browser':- if ANCHOR_BROWSER_API_KEY:- logger.info('π Using Anchor Browser (remote browser service)')- else:- logger.warning('β οΈ --browser anchor-browser provided but ANCHOR_BROWSER_API_KEY not set. Will use local browser!')- elif args.browser == 'brightdata':- if BRIGHTDATA_CDP_URL:- logger.info('π Using Brightdata browser (remote browser service)')- else:- logger.warning('β οΈ --browser brightdata provided but BRIGHTDATA_CDP_URL not set. Will use local browser!')- elif args.browser == 'browserbase':- if BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID:- logger.info('π Using Browserbase (remote browser service)')- else:- logger.warning(- 'β οΈ --browser browserbase provided but BROWSERBASE_API_KEY or BROWSERBASE_PROJECT_ID not set. Will use local browser!'- )- elif args.browser == 'hyperbrowser':- if HYPERBROWSER_API_KEY:- logger.info('π Using Hyperbrowser (remote browser service)')- else:- logger.warning('β οΈ --browser hyperbrowser provided but HYPERBROWSER_API_KEY not set. Will use local browser!')- elif args.browser == 'browser-use':- logger.warning('π Browser-use not implemented yet. Will use local browser!')- else:- logger.info('π Using local browser')-- # Log memory configuration- if args.enable_memory:- logger.info(f'π§ Memory enabled: mem0 system with interval={args.memory_interval} steps')- else:- logger.info('π§ Memory disabled')-- # Log other agent configuration- logger.info(f'π― Max actions per step: {args.max_actions_per_step}')-- if args.validate_output:- logger.info('β Output validation enabled')- else:- logger.info('β Output validation disabled')-- if args.planner_model:- logger.info(f'πΊοΈ Planner enabled: {args.planner_model} (interval={args.planner_interval} steps)')- else:- logger.info('πΊοΈ Planner disabled')- # --------------------------- # --- Get LLMs ---- logger.info(f'Instantiating agent LLM: {args.model}')- try:- # Get the selected LLM for the agent- llm = get_llm(args.model)- logger.info('Agent LLM instantiated successfully.')- except Exception as e:- logger.error(f'Failed to instantiate agent LLM ({args.model}): {type(e).__name__}: {e}', exc_info=True)- exit(1)-- logger.info(f'Instantiating evaluation LLM: {args.eval_model}')- try:- eval_model = get_llm(args.eval_model)- logger.info(f'Evaluation LLM ({args.eval_model}) instantiated successfully.')- except Exception as e:- logger.error(- f'Failed to instantiate evaluation LLM ({args.eval_model}): {type(e).__name__}: {e}. Make sure required API keys are set.',- exc_info=True,- )- exit(1)-- # Get planner LLM if specified- planner_llm = None- if args.planner_model:- logger.info(f'Instantiating planner LLM: {args.planner_model}')- try:- planner_llm = get_llm(args.planner_model)- logger.info(f'Planner LLM ({args.planner_model}) instantiated successfully.')- except Exception as e:- logger.error(- f'Failed to instantiate planner LLM ({args.planner_model}): {type(e).__name__}: {e}. Make sure required API keys are set.',- exc_info=True,- )- exit(1)- # ------------------- # Log initial system state- logger.info('π§ EVALUATION STARTUP')- log_system_resources('STARTUP')-- # For single task mode, set appropriate start/end indices and parallel runs- if args.task_text:- # Single task mode - force single execution but SAVE results to server- start_index = 0- end_index = 1- parallel_runs = 1- # Use server URLs for single task mode too so results are saved and visible- convex_url = CONVEX_URL if CONVEX_URL else ''- secret_key = SECRET_KEY if SECRET_KEY else ''- logger.info('Single task mode: Running single task with parallel_runs=1')- else:- # Multi-task mode - use provided arguments- start_index = args.start- end_index = args.end- parallel_runs = args.parallel_runs- convex_url = CONVEX_URL- secret_key = SECRET_KEY-- try:- results = asyncio.run(- run_evaluation_pipeline(- tasks=tasks,- llm=llm,- run_id=run_id,- test_case=args.test_case,- user_message=args.user_message,- convex_url=convex_url,- secret_key=secret_key,- eval_model=eval_model,- auth_distribution=auth_distribution,- github_workflow_url=args.github_workflow_url,- max_parallel_runs=parallel_runs,- max_steps_per_task=args.max_steps,- start_index=start_index,- end_index=end_index,- headless=args.headless,- use_vision=not args.no_vision,- use_serp=args.use_serp,- browser=args.browser,- enable_memory=args.enable_memory,- memory_interval=args.memory_interval,- max_actions_per_step=args.max_actions_per_step,- validate_output=args.validate_output,- planner_llm=planner_llm,- planner_interval=args.planner_interval,- include_result=args.include_result,- laminar_eval_id=args.laminar_eval_id,- highlight_elements=args.highlight_elements,- use_mind2web_judge=args.use_mind2web_judge,- use_thinking=not args.no_thinking,- gmail_tokens_dict=gmail_tokens_dict,- judge_repeat_count=args.judge_repeat_count,- images_per_step=args.images_per_step,- default_navigation_timeout=args.default_navigation_timeout,- default_timeout=args.default_timeout,- minimum_wait_page_load_time=args.minimum_wait_page_load_time,- wait_for_network_idle_page_load_time=args.wait_for_network_idle_page_load_time,- maximum_wait_page_load_time=args.maximum_wait_page_load_time,- wait_between_actions=args.wait_between_actions,- stealth=args.stealth,- )- )-- logger.info('β EVALUATION COMPLETED SUCCESSFULLY')- log_system_resources('SUCCESS_COMPLETION')-- except KeyboardInterrupt:- logger.warning('β οΈ EVALUATION INTERRUPTED by user (Ctrl+C)')- log_system_resources('INTERRUPTED')- raise- except Exception as e:- logger.critical(f'π¨ EVALUATION FAILED: {type(e).__name__}: {e}', exc_info=True)- log_system_resources('FAILED_COMPLETION')- raise-- logger.info('β All tasks completed successfully.')diff --git a/eval/task_types.py b/eval/task_types.pydeleted file mode 100644index f8944ebc1..000000000--- a/eval/task_types.py+++ /dev/null@@ -1,228 +0,0 @@-import json-import logging-from dataclasses import dataclass, field-from enum import Enum-from typing import Any--from eval.utils import create_pydantic_model_from_schema, make_json_serializable--logger = logging.getLogger(__name__)---class Stage(Enum):- SETUP_BROWSER = 'setup_browser'- RUN_AGENT = 'run_agent'- FORMAT_HISTORY = 'format_history'- EVALUATE = 'evaluate'- SAVE_SERVER = 'save_server'---@dataclass-class StageError:- stage: Stage- error_type: str- message: str---@dataclass-class TaskResult:- task_id: str- run_id: str- confirmed_task: str- task: Any- max_steps: int- laminar_link: str | None = None- github_workflow_url: str | None = None- completed_stages: set[Stage] = field(default_factory=set)- stage_data: dict[Stage, Any] = field(default_factory=dict)- errors: list = field(default_factory=list)- cancelled: bool = False- critical_error: str | None = None- server_save_failed: bool = False-- def stage_completed(self, stage: Stage, data: Any = None):- self.completed_stages.add(stage)- if data is not None:- self.stage_data[stage] = data-- def stage_failed(self, stage: Stage, error: StageError):- self.errors.append(error)-- def mark_cancelled(self):- self.cancelled = True-- def mark_critical_error(self, error: str):- self.critical_error = error-- def mark_server_save_failed(self, error: str):- self.server_save_failed = True- self.errors.append(StageError(Stage.SAVE_SERVER, 'server_save', error))-- def has_execution_data(self) -> bool:- return Stage.RUN_AGENT in self.completed_stages or Stage.FORMAT_HISTORY in self.completed_stages-- @property- def server_payload(self) -> dict[str, Any]:- """Generate payload for server submission"""- payload = {- 'taskId': self.task_id,- 'runId': self.run_id,- 'task': self.confirmed_task,- 'completed_stages': [stage.value for stage in self.completed_stages],- 'has_errors': len(self.errors) > 0,- 'cancelled': self.cancelled,- 'critical_error': self.critical_error,- 'server_save_failed': self.server_save_failed,- 'laminarTaskLink': self.laminar_link,- 'githubWorkflowUrl': self.github_workflow_url,- }-- # Add task execution data if available- if Stage.FORMAT_HISTORY in self.completed_stages:- format_data = self.stage_data.get(Stage.FORMAT_HISTORY, {})- logger.info(f'format_data: {format_data}')- # log token usage- logger.info(f'tokensUsed: {format_data.get("tokensUsed")}')- logger.info(f'usage: {format_data.get("usage")}')-- # Handle usage data - convert to JSON string if it's a dict- usage_data = format_data.get('usage')- if usage_data and isinstance(usage_data, dict):- usage_data = json.dumps(usage_data)-- payload.update(- {- 'actionHistory': format_data.get('action_history', []),- 'finalResultResponse': format_data.get('final_result_response', ''),- 'selfReportCompleted': format_data.get('self_report_completed', False),- 'selfReportSuccess': format_data.get('self_report_success', False),- 'taskDuration': format_data.get('task_duration'),- 'steps': format_data.get('steps'),- 'maxSteps': self.max_steps,- 'tokensUsed': format_data.get('tokensUsed'),- 'usage': usage_data, # Add usage data (JSON string if dict)- 'completeHistory': format_data.get('complete_history', []), # Add complete step history- }- )-- # Add evaluation data if available- if Stage.EVALUATE in self.completed_stages:- eval_data = self.stage_data.get(Stage.EVALUATE, {})-- # Handle comprehensive judge evaluation- comp_eval = eval_data.get('comprehensive_evaluation') or eval_data.get('comprehensive_judge')- if comp_eval:- # Convert enum lists to string lists for database storage- task_categories = comp_eval.get('task_categories', [])- if task_categories and hasattr(task_categories[0], 'value'):- task_categories = [cat.value for cat in task_categories]-- error_categories = comp_eval.get('error_categories', [])- if error_categories and hasattr(error_categories[0], 'value'):- error_categories = [err.value for err in error_categories]-- payload.update(- {- 'comprehensiveJudgeEvaluationSummary': comp_eval.get('task_summary'),- 'comprehensiveJudgeEvaluationReasoning': comp_eval.get('reasoning'),- 'comprehensiveJudgeEvaluationPassed': comp_eval.get('passed'),- 'comprehensiveJudgeEvaluationScore': comp_eval.get('final_score'),- 'comprehensiveJudgeEvaluationCategories': task_categories,- 'comprehensiveJudgeEvaluationErrors': error_categories,- 'comprehensiveJudgeEvaluationTips': comp_eval.get('improvement_tips', []),- 'comprehensiveJudgeEvaluationCriticalIssues': comp_eval.get('critical_issues', []),- 'comprehensiveJudgeEvaluationScores': comp_eval.get('scores'),- 'comprehensiveJudgeEvaluationFull': comp_eval, # Include full comprehensive eval data- }- )-- # Handle legacy Mind2Web evaluation (for compatibility)- payload.update(- {- 'onlineMind2WebEvaluationJudgement': eval_data.get('judgement') or 'No evaluation available',- 'onlineMind2WebEvaluationError': eval_data.get('error'),- 'onlineMind2WebEvaluationSuccess': eval_data.get('success', False),- 'onlineMind2WebEvaluationScore': eval_data.get('score', 0.0),- }- )-- # Ensure all data in payload is JSON serializable- serialized_payload = make_json_serializable(payload)- # Type assertion since we know payload is a dict and make_json_serializable preserves dict structure- assert isinstance(serialized_payload, dict), 'Payload serialization should preserve dict structure'- return serialized_payload-- def get_local_status(self) -> dict[str, Any]:- """Get local status summary"""- success = (- Stage.EVALUATE in self.completed_stages- and not self.cancelled- and self.critical_error is None- and len([e for e in self.errors if e.error_type == 'exception']) == 0- )- return {- 'task_id': self.task_id,- 'success': success,- 'error': self.critical_error or (self.errors[0].message if self.errors else None),- 'completed_stages': [stage.value for stage in self.completed_stages],- }---class Task:- def __init__(self, task_id, confirmed_task, **kwargs):- # Validate required fields- if not task_id:- raise ValueError('task_id is required and cannot be empty')- if not confirmed_task:- raise ValueError('confirmed_task is required and cannot be empty')-- # Set required fields- self.task_id = task_id- self.confirmed_task = confirmed_task-- # Set optional fields dynamically- # Known optional fields with defaults- self.website = kwargs.get('website', None)- self.reference_length = kwargs.get('reference_length', None)- self.level = kwargs.get('level', None)- self.cluster_id = kwargs.get('cluster_id', None)- self.login_cookie = kwargs.get('login_cookie', None)- self.login_type = kwargs.get('login_type', None)- self.category = kwargs.get('category', None)- self.output_schema = kwargs.get('output_schema', None) # Add structured output schema support- self.auth_keys = kwargs.get('auth_keys', None) # List of auth keys to fetch from auth distribution- if self.output_schema:- # Convert JSON schema to Pydantic model class- self.output_model = create_pydantic_model_from_schema(self.output_schema, f'Task_{self.task_id}_Output')- else:- self.output_model = None-- # Store any additional optional fields- known_fields = {- 'website',- 'reference_length',- 'level',- 'cluster_id',- 'login_cookie',- 'login_type',- 'category',- 'output_schema',- 'auth_keys',- }- self.additional_fields = {k: v for k, v in kwargs.items() if k not in known_fields}-- # Make all additional fields accessible as attributes- for key, value in self.additional_fields.items():- setattr(self, key, value)-- def __str__(self):- # Include main fields and indicate if there are additional fields- base_str = f'Task(task_id={self.task_id}, confirmed_task={self.confirmed_task}, website={self.website}, reference_length={self.reference_length}, level={self.level}, cluster_id={self.cluster_id}, login_cookie={self.login_cookie}, login_type={self.login_type}, category={self.category}, output_schema={self.output_schema}, auth_keys={self.auth_keys}'- if self.additional_fields:- additional_str = ', '.join(f'{k}={v}' for k, v in self.additional_fields.items())- base_str += f', {additional_str}'- base_str += ')'- return base_str-- def __repr__(self):- return self.__str__()diff --git a/eval/tests/test_structured_output.py b/eval/tests/test_structured_output.pydeleted file mode 100644index adc6f5d74..000000000--- a/eval/tests/test_structured_output.py+++ /dev/null@@ -1,635 +0,0 @@-import json-import traceback--from pydantic import BaseModel--from eval.utils import create_pydantic_model_from_schema---class OutputModel(BaseModel):- """Test output model"""-- city: str- country: str---# async def test_optimized_schema():-# """Test the optimized schema generation and save to file."""--# # Create controller and get all registered actions-# controller = Controller()-# ActionModel = controller.registry.create_action_model()--# # Create the agent output model with custom actions-# agent_output_model = AgentOutput.type_with_custom_actions(ActionModel)--# # # Get original schema for comparison-# # original_schema = agent_output_model.model_json_schema()--# # # Create the optimized schema-# # optimized_schema = SchemaOptimizer.create_optimized_json_schema(agent_output_model)--# scgena = create_pydantic_model_from_schema(OutputModel.model_json_schema(), 'OutputModel')--# agent = Agent(-# task='What is the capital of France? Do not use the internet, just output the done function.',-# llm=ChatOpenAI(model='gpt-4.1-mini'),-# controller=controller,-# output_model_schema=scgena,-# )--# history = await agent.run()--# if history.structured_output:-# # print(history.structured_output.city, history.structured_output.country)-# print(OutputModel.model_validate_json(history.final_result() or '{}'))-# else:-# print('No structured output')-# print(history.final_result())---def test_basic_types():- """Test basic JSON schema types"""- print('=== Testing Basic Types ===')-- schema = {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'age': {'type': 'integer'},- 'height': {'type': 'number'},- 'is_active': {'type': 'boolean'},- 'tags': {'type': 'array', 'items': {'type': 'string'}},- 'metadata': {'type': 'object'},- },- 'required': ['name', 'age'],- }-- try:- Model = create_pydantic_model_from_schema(schema, 'BasicTypesModel')- print(f'β Created model: {Model}')-- # Test valid data- instance = Model(name='John', age=30, height=5.9, is_active=True, tags=['dev', 'python'], metadata={'role': 'developer'})- print(f'β Valid instance: {instance}')-- # Test minimal required data- minimal = Model(name='Jane', age=25)- print(f'β Minimal instance: {minimal}')-- print('β Basic types test passed\n')- return True- except Exception as e:- print(f'β Basic types test failed: {e}')- traceback.print_exc()- return False---def test_nested_objects():- """Test deeply nested object structures"""- print('=== Testing Nested Objects ===')-- schema = {- 'type': 'object',- 'properties': {- 'user': {- 'type': 'object',- 'properties': {- 'profile': {- 'type': 'object',- 'properties': {- 'personal': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'age': {'type': 'integer'},- 'contacts': {- 'type': 'array',- 'items': {- 'type': 'object',- 'properties': {'type': {'type': 'string'}, 'value': {'type': 'string'}},- 'required': ['type', 'value'],- },- },- },- 'required': ['name'],- },- 'settings': {- 'type': 'object',- 'properties': {'theme': {'type': 'string'}, 'notifications': {'type': 'boolean'}},- },- },- 'required': ['personal'],- }- },- 'required': ['profile'],- }- },- 'required': ['user'],- }-- try:- Model = create_pydantic_model_from_schema(schema, 'NestedModel')- print(f'β Created nested model: {Model}')-- # Test complex nested data- data = {- 'user': {- 'profile': {- 'personal': {- 'name': 'Alice',- 'age': 28,- 'contacts': [{'type': 'email', 'value': '[email protected]'}, {'type': 'phone', 'value': '+1234567890'}],- },- 'settings': {'theme': 'dark', 'notifications': True},- }- }- }-- instance = Model(**data)- print(f'β Complex nested instance: {instance}')-- print('β Nested objects test passed\n')- return True- except Exception as e:- print(f'β Nested objects test failed: {e}')- traceback.print_exc()- return False---def test_union_types():- """Test union types and nullable fields"""- print('=== Testing Union Types ===')-- schema = {- 'type': 'object',- 'properties': {- 'mixed_value': {'anyOf': [{'type': 'string'}, {'type': 'integer'}, {'type': 'boolean'}]},- 'nullable_string': {'type': ['string', 'null']},- 'string_or_number': {'oneOf': [{'type': 'string'}, {'type': 'number'}]},- 'complex_union': {- 'anyOf': [- {- 'type': 'object',- 'properties': {'type': {'const': 'user'}, 'name': {'type': 'string'}},- 'required': ['type', 'name'],- },- {- 'type': 'object',- 'properties': {'type': {'const': 'admin'}, 'permissions': {'type': 'array', 'items': {'type': 'string'}}},- 'required': ['type', 'permissions'],- },- ]- },- },- }-- try:- Model = create_pydantic_model_from_schema(schema, 'UnionModel')- print(f'β Created union model: {Model}')-- # Test different union values- instance1 = Model(mixed_value='hello', nullable_string=None, string_or_number='123')- print(f'β Union instance 1: {instance1}')-- instance2 = Model(mixed_value=42, nullable_string='world', string_or_number=3.14)- print(f'β Union instance 2: {instance2}')-- print('β Union types test passed\n')- return True- except Exception as e:- print(f'β Union types test failed: {e}')- traceback.print_exc()- return False---def test_array_variations():- """Test various array configurations"""- print('=== Testing Array Variations ===')-- schema = {- 'type': 'object',- 'properties': {- 'simple_array': {'type': 'array', 'items': {'type': 'string'}},- 'mixed_array': {- 'type': 'array',- 'items': {- 'anyOf': [- {'type': 'string'},- {'type': 'integer'},- {'type': 'object', 'properties': {'key': {'type': 'string'}}},- ]- },- },- 'nested_arrays': {'type': 'array', 'items': {'type': 'array', 'items': {'type': 'integer'}}},- 'array_of_objects': {- 'type': 'array',- 'items': {- 'type': 'object',- 'properties': {- 'id': {'type': 'integer'},- 'data': {'type': 'object', 'properties': {'values': {'type': 'array', 'items': {'type': 'number'}}}},- },- 'required': ['id'],- },- },- },- }-- try:- Model = create_pydantic_model_from_schema(schema, 'ArrayModel')- print(f'β Created array model: {Model}')-- # Test complex array data- data = {- 'simple_array': ['a', 'b', 'c'],- 'mixed_array': ['hello', 42, {'key': 'value'}],- 'nested_arrays': [[1, 2, 3], [4, 5, 6]],- 'array_of_objects': [{'id': 1, 'data': {'values': [1.1, 2.2, 3.3]}}, {'id': 2, 'data': {'values': [4.4, 5.5]}}],- }-- instance = Model(**data)- print(f'β Complex array instance: {instance}')-- print('β Array variations test passed\n')- return True- except Exception as e:- print(f'β Array variations test failed: {e}')- traceback.print_exc()- return False---def test_enums_and_constants():- """Test enum values and constant fields"""- print('=== Testing Enums and Constants ===')-- schema = {- 'type': 'object',- 'properties': {- 'status': {'type': 'string', 'enum': ['active', 'inactive', 'pending', 'suspended']},- 'priority': {'type': 'integer', 'enum': [1, 2, 3, 4, 5]},- 'type': {'const': 'user_account'},- 'category': {'anyOf': [{'const': 'premium'}, {'const': 'standard'}, {'const': 'basic'}]},- },- 'required': ['status', 'type'],- }-- try:- Model = create_pydantic_model_from_schema(schema, 'EnumModel')- print(f'β Created enum model: {Model}')-- # Test valid enum values- instance = Model(status='active', priority=3, type='user_account', category='premium')- print(f'β Enum instance: {instance}')-- print('β Enums and constants test passed\n')- return True- except Exception as e:- print(f'β Enums and constants test failed: {e}')- traceback.print_exc()- return False---def test_edge_cases():- """Test edge cases and malformed schemas"""- print('=== Testing Edge Cases ===')-- edge_cases = [- # Empty schema- {},- # Schema with no properties- {'type': 'object'},- # Schema with empty properties- {'type': 'object', 'properties': {}},- # Schema with additional properties- {'type': 'object', 'properties': {'name': {'type': 'string'}}, 'additionalProperties': True},- # Schema with pattern properties- {'type': 'object', 'patternProperties': {'^S_': {'type': 'string'}, '^I_': {'type': 'integer'}}},- ]-- success_count = 0- for i, schema in enumerate(edge_cases):- try:- Model = create_pydantic_model_from_schema(schema, f'EdgeCase{i}Model')- print(f'β Edge case {i}: {Model}')- success_count += 1- except Exception as e:- print(f'β οΈ Edge case {i} failed: {e}')-- print(f'β Edge cases test: {success_count}/{len(edge_cases)} passed\n')- return True---def test_very_complex_schema():- """Test an extremely complex real-world-like schema"""- print('=== Testing Very Complex Schema ===')-- schema = {- 'type': 'object',- 'properties': {- 'api_version': {'const': 'v1'},- 'metadata': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'namespace': {'type': 'string', 'default': 'default'},- 'labels': {'type': 'object', 'additionalProperties': {'type': 'string'}},- 'annotations': {'type': 'object', 'additionalProperties': {'type': 'string'}},- 'created_at': {'type': 'string', 'format': 'date-time'},- },- 'required': ['name'],- },- 'spec': {- 'type': 'object',- 'properties': {- 'replicas': {'type': 'integer', 'minimum': 1, 'maximum': 100},- 'selector': {- 'type': 'object',- 'properties': {'match_labels': {'type': 'object', 'additionalProperties': {'type': 'string'}}},- },- 'template': {- 'type': 'object',- 'properties': {- 'metadata': {- 'type': 'object',- 'properties': {'labels': {'type': 'object', 'additionalProperties': {'type': 'string'}}},- },- 'spec': {- 'type': 'object',- 'properties': {- 'containers': {- 'type': 'array',- 'items': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'image': {'type': 'string'},- 'ports': {- 'type': 'array',- 'items': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'container_port': {'type': 'integer'},- 'protocol': {'type': 'string', 'enum': ['TCP', 'UDP', 'SCTP']},- },- 'required': ['container_port'],- },- },- 'env': {- 'type': 'array',- 'items': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'value': {'type': 'string'},- 'value_from': {- 'type': 'object',- 'properties': {- 'secret_key_ref': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'key': {'type': 'string'},- },- 'required': ['name', 'key'],- },- 'config_map_key_ref': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'key': {'type': 'string'},- },- 'required': ['name', 'key'],- },- },- },- },- 'required': ['name'],- },- },- 'resources': {- 'type': 'object',- 'properties': {- 'requests': {- 'type': 'object',- 'properties': {- 'memory': {'type': 'string'},- 'cpu': {'type': 'string'},- },- },- 'limits': {- 'type': 'object',- 'properties': {- 'memory': {'type': 'string'},- 'cpu': {'type': 'string'},- },- },- },- },- },- 'required': ['name', 'image'],- },- },- 'volumes': {- 'type': 'array',- 'items': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'config_map': {'type': 'object', 'properties': {'name': {'type': 'string'}}},- 'secret': {'type': 'object', 'properties': {'secret_name': {'type': 'string'}}},- 'empty_dir': {'type': 'object', 'properties': {'size_limit': {'type': 'string'}}},- },- 'required': ['name'],- },- },- },- 'required': ['containers'],- },- },- 'required': ['spec'],- },- },- 'required': ['template'],- },- 'status': {- 'type': 'object',- 'properties': {- 'ready_replicas': {'type': 'integer'},- 'available_replicas': {'type': 'integer'},- 'conditions': {- 'type': 'array',- 'items': {- 'type': 'object',- 'properties': {- 'type': {'type': 'string'},- 'status': {'type': 'string', 'enum': ['True', 'False', 'Unknown']},- 'reason': {'type': 'string'},- 'message': {'type': 'string'},- 'last_update_time': {'type': 'string', 'format': 'date-time'},- },- 'required': ['type', 'status'],- },- },- },- },- },- 'required': ['api_version', 'metadata', 'spec'],- }-- try:- Model = create_pydantic_model_from_schema(schema, 'KubernetesDeployment')- print(f'β Created very complex model: {Model}')-- # Create a sample instance- data = {- 'api_version': 'v1',- 'metadata': {- 'name': 'my-app',- 'namespace': 'production',- 'labels': {'app': 'my-app', 'version': '1.0'},- 'created_at': '2024-01-01T00:00:00Z',- },- 'spec': {- 'replicas': 3,- 'template': {- 'spec': {- 'containers': [- {- 'name': 'my-app',- 'image': 'my-app:latest',- 'ports': [{'name': 'http', 'container_port': 8080, 'protocol': 'TCP'}],- 'env': [- {'name': 'ENV', 'value': 'production'},- {- 'name': 'SECRET_KEY',- 'value_from': {'secret_key_ref': {'name': 'my-secret', 'key': 'secret-key'}},- },- ],- 'resources': {- 'requests': {'memory': '128Mi', 'cpu': '100m'},- 'limits': {'memory': '256Mi', 'cpu': '200m'},- },- }- ]- }- },- },- }-- instance = Model(**data)- print('β Very complex instance created successfully')-- print('β Very complex schema test passed\n')- return True- except Exception as e:- print(f'β Very complex schema test failed: {e}')- traceback.print_exc()- return False---def test_string_schema_input():- """Test passing schema as JSON string instead of dict"""- print('=== Testing String Schema Input ===')-- schema_string = json.dumps(- {'type': 'object', 'properties': {'message': {'type': 'string'}, 'count': {'type': 'integer'}}, 'required': ['message']}- )-- try:- Model = create_pydantic_model_from_schema(schema_string, 'StringSchemaModel')- print(f'β Created model from string schema: {Model}')-- instance = Model(message='Hello', count=42)- print(f'β String schema instance: {instance}')-- print('β String schema input test passed\n')- return True- except Exception as e:- print(f'β String schema input test failed: {e}')- traceback.print_exc()- return False---def test_recursive_structures():- """Test recursive/self-referencing structures"""- print('=== Testing Recursive Structures ===')-- schema = {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'children': {- 'type': 'array',- 'items': {'$ref': '#'}, # Self-reference- },- 'parent': {'$ref': '#'}, # Self-reference- },- 'required': ['name'],- }-- try:- Model = create_pydantic_model_from_schema(schema, 'RecursiveModel')- print(f'β Created recursive model: {Model}')-- # Note: Recursive structures are complex and may not work perfectly- # This is more of a "does it crash?" test- print('β Recursive structures test passed (creation only)\n')- return True- except Exception as e:- print(f'β οΈ Recursive structures test failed (expected): {e}')- print('Note: Recursive structures are complex and may not be fully supported\n')- return True # Don't fail the overall test for this---def run_comprehensive_schema_tests():- """Run all comprehensive tests for create_pydantic_model_from_schema"""- print('π Starting comprehensive schema testing...\n')-- tests = [- ('Basic Types', test_basic_types),- ('Nested Objects', test_nested_objects),- ('Union Types', test_union_types),- ('Array Variations', test_array_variations),- ('Enums and Constants', test_enums_and_constants),- ('Recursive Structures', test_recursive_structures),- ('Edge Cases', test_edge_cases),- ('Very Complex Schema', test_very_complex_schema),- ('String Schema Input', test_string_schema_input),- ]-- passed = 0- total = len(tests)-- for test_name, test_func in tests:- print(f'Running {test_name}...')- try:- if test_func():- passed += 1- else:- print(f'β {test_name} failed')- except Exception as e:- print(f'β {test_name} crashed: {e}')- traceback.print_exc()-- print('-' * 50)-- print('\nπ Testing Complete!')- print(f'π Results: {passed}/{total} tests passed')-- if passed == total:- print('π All tests passed! The function handles complex schemas well.')- else:- print('β οΈ Some tests failed. Check the output above for details.')-- return passed == total---if __name__ == '__main__':- # Run the comprehensive tests- print('Running comprehensive schema tests first...')- run_comprehensive_schema_tests()-- print('\n' + '=' * 60)- print('Now running the original test...')diff --git a/eval/utils.py b/eval/utils.pydeleted file mode 100644index dd709fc6a..000000000--- a/eval/utils.py+++ /dev/null@@ -1,277 +0,0 @@-import json-import logging-import subprocess-import time-from typing import Any--from pydantic import BaseModel--logger = logging.getLogger(__name__)---def create_pydantic_model_from_schema(original_schema: dict | str, model_name: str = 'DynamicModel') -> type[BaseModel]:- """- Convert JSON schema to Pydantic model class using datamodel-code-generator.-- Args:- schema: JSON schema dictionary- model_name: Name for the generated model class-- Returns:- Pydantic model class that can be used with Controller(output_model=...)-- Example:- schema = {- "type": "object",- "properties": {- "name": {"type": "string"},- "age": {"type": "integer"},- "email": {"type": "string"}- },- "required": ["name", "age"]- }- PersonModel = create_pydantic_model_from_schema(schema, "Person")- controller = Controller(output_model=PersonModel)- """- try:- import importlib.util- import tempfile- from pathlib import Path-- from datamodel_code_generator import DataModelType, generate # type: ignore[import-untyped]-- # Handle case where schema might be a string (JSON)- if isinstance(original_schema, str):- schema: dict = json.loads(original_schema)- else:- schema = original_schema-- logger.debug(f'Creating Pydantic model from schema: {schema}')-- # Initialize paths for cleanup- schema_path = None- model_path = None-- try:- # Create temporary files for input schema and output model- with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as schema_file:- json.dump(schema, schema_file, indent=2)- schema_path = Path(schema_file.name)-- with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as model_file:- model_path = Path(model_file.name)-- # Generate Pydantic model code using datamodel-code-generator- generate(- input_=schema_path,- output=model_path,- output_model_type=DataModelType.PydanticV2BaseModel,- class_name=model_name,- )-- # Read the generated Python code- generated_code = model_path.read_text()- logger.debug(f'Generated Pydantic model code:\n{generated_code}')-- # Create a module and execute the generated code- spec = importlib.util.spec_from_loader(f'dynamic_model_{model_name}', loader=None)- if spec is None:- raise ValueError('Failed to create module spec')-- module = importlib.util.module_from_spec(spec)-- # Add necessary imports to the module namespace before executing- from typing import Any, Optional, Union-- from pydantic import BaseModel, Field-- module.__dict__.update(- {- 'Optional': Optional,- 'Union': Union,- 'list': list,- 'dict': dict,- 'Any': Any,- 'BaseModel': BaseModel,- 'Field': Field,- 'str': str,- 'int': int,- 'float': float,- 'bool': bool,- }- )-- # Execute the generated code in the module's namespace- exec(generated_code, module.__dict__)-- # Get the generated model class- if hasattr(module, model_name):- model_class = getattr(module, model_name)- # Rebuild the model to resolve forward references and type annotations- # Pass the module's namespace so it can resolve imports like Optional- model_class.model_rebuild(_types_namespace=module.__dict__)- logger.debug(f'Successfully created Pydantic model: {model_class}')- return model_class- else:- # Fallback: look for any BaseModel subclass in the module- for attr_name in dir(module):- attr = getattr(module, attr_name)- if isinstance(attr, type) and issubclass(attr, BaseModel) and attr != BaseModel:- # Rebuild the model to resolve forward references and type annotations- # Pass the module's namespace so it can resolve imports like Optional- attr.model_rebuild(_types_namespace=module.__dict__)- logger.debug(f'Using fallback model class: {attr}')- return attr-- raise ValueError('No Pydantic model class found in generated code')-- finally:- # Clean up temporary files safely- if schema_path is not None:- try:- schema_path.unlink()- except Exception as cleanup_error:- logger.warning(f'Failed to cleanup schema file: {cleanup_error}')- if model_path is not None:- try:- model_path.unlink()- except Exception as cleanup_error:- logger.warning(f'Failed to cleanup model file: {cleanup_error}')-- except ImportError as e:- logger.error(f'datamodel-code-generator not available: {e}')- logger.error('Falling back to basic schema conversion')-- try:- # Fallback to basic implementation if datamodel-code-generator is not available- from typing import Any, Optional-- from pydantic import create_model-- def json_type_to_python_type(json_type):- """Map JSON schema types to Python types"""- # Handle union types (arrays of types)- if isinstance(json_type, list):- # Handle union types like ["string", "null"]- types = []- for t in json_type:- if t == 'null':- continue # We'll handle null separately- types.append(json_type_to_python_type(t))-- if len(types) == 0:- return Any- elif len(types) == 1:- return types[0]- else:- # len(types) >= 2 - create Union dynamically- from typing import Union-- # For 2 types, use Union[type1, type2]- if len(types) == 2:- return Union[types[0], types[1]]- # For more types, we need to use a different approach- # Since Union doesn't support unpacking, we'll just use the first type- # This is a limitation of the fallback implementation- return types[0]-- # Handle single types- if json_type == 'string':- return str- elif json_type == 'integer':- return int- elif json_type == 'number':- return float- elif json_type == 'boolean':- return bool- elif json_type == 'array':- return list[Any]- elif json_type == 'object':- return dict[str, Any]- else:- return Any-- # Handle case where schema might be a string (JSON)- if isinstance(schema, str):- schema = json.loads(schema)-- # Extract properties and required fields from schema- properties = schema.get('properties', {})- required_fields = schema.get('required', [])-- # Build field definitions for create_model- field_definitions = {}-- for field_name, field_schema in properties.items():- json_type = field_schema.get('type')- field_type = json_type_to_python_type(json_type)-- # Check if the field allows null (either not required or explicitly allows null)- allows_null = field_name not in required_fields or (isinstance(json_type, list) and 'null' in json_type)-- # Handle required vs optional fields- if field_name in required_fields and not allows_null:- field_definitions[field_name] = (field_type, ...) # Required field- else:- optional_type = Optional[field_type] # Use Optional instead of Union[T, None]- field_definitions[field_name] = (optional_type, None) # Optional field with default None-- # Create the dynamic model using create_model- return create_model(model_name, **field_definitions)-- except Exception as fallback_error:- logger.error(f'Fallback schema conversion also failed: {fallback_error}')- raise ValueError(f'Both primary and fallback schema conversion failed: {fallback_error}') from fallback_error-- except Exception as e:- logger.error(f'Failed to create Pydantic model from schema: {e}')- logger.error(f'Schema: {schema}')- raise ValueError(f'Invalid JSON schema: {e}') from e---def make_json_serializable(obj: Any) -> Any:- """- Convert objects to JSON-serializable types.- Handles common non-serializable types like enums, custom objects, etc.- """- if obj is None:- return None- elif isinstance(obj, (str, int, float, bool)):- return obj- elif isinstance(obj, dict):- return {str(k): make_json_serializable(v) for k, v in obj.items()}- elif isinstance(obj, (list, tuple)):- return [make_json_serializable(item) for item in obj]- elif hasattr(obj, 'value'): # Handle enums- return obj.value- elif hasattr(obj, '__dict__'): # Handle custom objects- return str(obj)- else:- return str(obj)---def get_git_info():- """Retrieves git branch, commit hash, commit timestamp, and repository URL using subprocess."""- try:- branch = subprocess.run(- ['git', 'rev-parse', '--abbrev-ref', 'HEAD'], capture_output=True, text=True, check=True- ).stdout.strip()- commit_hash = subprocess.run(['git', 'rev-parse', 'HEAD'], capture_output=True, text=True, check=True).stdout.strip()- # Get commit timestamp as Unix epoch integer- commit_timestamp_str = subprocess.run(- ['git', 'log', '-1', '--format=%ct'], capture_output=True, text=True, check=True- ).stdout.strip()- commit_timestamp = int(commit_timestamp_str)- # Get repository URL- repo_url = subprocess.run(- ['git', 'config', '--get', 'remote.origin.url'], capture_output=True, text=True, check=True- ).stdout.strip()- return {'branch': branch, 'hash': commit_hash, 'timestamp': commit_timestamp, 'repo': repo_url}- except (subprocess.CalledProcessError, FileNotFoundError, ValueError) as e:- logger.warning(f'Could not retrieve git info: {type(e).__name__}: {e}. Using defaults.')- return {- 'branch': 'unknown',- 'hash': 'unknown',- 'timestamp': int(time.time()), # Fallback to current time- 'repo': 'unknown',- }diff --git a/eval/web_judge.py b/eval/web_judge.pydeleted file mode 100644index 9a952966f..000000000--- a/eval/web_judge.py+++ /dev/null@@ -1,253 +0,0 @@-# ==============================================================================================================-# This is the LLM as a judge evaluation system from the OSU-NLP Group paper-# Any adaptiations made should be explicitly stated here:-# Adaptations:-# We are using our own wrapper for the OpenAI API-# This means we changed model.generate to model.invoke. The behavior of the model should be identical.-# Added a Online_Mind2Web_eval_with_retry wrapper with retry logic in case of API rate limiting or other issues.---# @article{xue2025illusionprogressassessingcurrent,-# title={An Illusion of Progress? Assessing the Current State of Web Agents},-# author={Tianci Xue and Weijian Qi and Tianneng Shi and Chan Hee Song and Boyu Gou and Dawn Song and Huan Sun and Yu Su},-# year={2025},-# eprint={2504.01382},-# archivePrefix={arXiv},-# primaryClass={cs.AI},-# url={https://arxiv.org/abs/2504.01382},-# }--# @inproceedings{deng2023mind2web,-# author = {Deng, Xiang and Gu, Yu and Zheng, Boyuan and Chen, Shijie and Stevens, Sam and Wang, Boshi and Sun, Huan and Su, Yu},-# booktitle = {Advances in Neural Information Processing Systems},-# editor = {A. Oh and T. Naumann and A. Globerson and K. Saenko and M. Hardt and S. Levine},-# pages = {28091--28114},-# publisher = {Curran Associates, Inc.},-# title = {Mind2Web: Towards a Generalist Agent for the Web},-# url = {https://proceedings.neurips.cc/paper_files/paper/2023/file/5950bf290a1570ea401bf98882128160-Paper-Datasets_and_Benchmarks.pdf},-# volume = {36},-# year = {2023}-# }-# ==============================================================================================================-import asyncio-import base64-import io-import logging-import re--from PIL import Image--MAX_IMAGE = 5---logger = logging.getLogger(__name__)---def encode_image(image):- """Convert a PIL image to base64 string."""- if image.mode == 'RGBA':- image = image.convert('RGB')- buffered = io.BytesIO()- image.save(buffered, format='JPEG')- return base64.b64encode(buffered.getvalue()).decode('utf-8')---async def identify_key_points(task, model):- system_msg = """You are an expert tasked with analyzing a given task to identify the key points explicitly stated in the task description.--**Objective**: Carefully analyze the task description and extract the critical elements explicitly mentioned in the task for achieving its goal.--**Instructions**:-1. Read the task description carefully.-2. Identify and extract **key points** directly stated in the task description.- - A **key point** is a critical element, condition, or step explicitly mentioned in the task description.- - Do not infer or add any unstated elements.- - Words such as "best," "highest," "cheapest," "latest," "most recent," "lowest," "closest," "highest-rated," "largest," and "newest" must go through the sort function(e.g., the key point should be "Filter by highest").--**Respond with**:-- **Key Points**: A numbered list of the explicit key points for completing this task, one per line, without explanations or additional details."""- prompt = """Task: {task}"""- text = prompt.format(task=task)- messages = [- {'role': 'system', 'content': system_msg},- {- 'role': 'user',- 'content': [{'type': 'text', 'text': text}],- },- ]- response = await model.ainvoke(messages)- return response.completion---async def judge_image(task, image_path, key_points, model):- system_msg = """You are an expert evaluator tasked with determining whether an image contains information about the necessary steps to complete a task.--**Objective**: Analyze the provided image and decide if it shows essential steps or evidence required for completing the task. Use your reasoning to explain your decision before assigning a score.--**Instructions**:-1. Provide a detailed description of the image, including its contents, visible elements, text (if any), and any notable features.--2. Carefully examine the image and evaluate whether it contains necessary steps or evidence crucial to task completion:-- Identify key points that could be relevant to task completion, such as actions, progress indicators, tool usage, applied filters, or step-by-step instructions.-- Does the image show actions, progress indicators, or critical information directly related to completing the task?-- Is this information indispensable for understanding or ensuring task success?-- If the image contains partial but relevant information, consider its usefulness rather than dismissing it outright.--3. Provide your response in the following format:-- **Reasoning**: Explain your thought process and observations. Mention specific elements in the image that indicate necessary steps, evidence, or lack thereof.-- **Score**: Assign a score based on the reasoning, using the following scale:- - **1**: The image does not contain any necessary steps or relevant information.- - **2**: The image contains minimal or ambiguous information, unlikely to be essential.- - **3**: The image includes some relevant steps or hints but lacks clarity or completeness.- - **4**: The image contains important steps or evidence that are highly relevant but not fully comprehensive.- - **5**: The image clearly displays necessary steps or evidence crucial for completing the task.--Respond with:-1. **Reasoning**: [Your explanation]-2. **Score**: [1-5]"""-- jpg_base64_str = encode_image(Image.open(image_path))-- prompt = """**Task**: {task}--**Key Points for Task Completion**: {key_points}--The snapshot of the web page is shown in the image."""- text = prompt.format(task=task, key_points=key_points)-- messages = [- {'role': 'system', 'content': system_msg},- {- 'role': 'user',- 'content': [- {'type': 'text', 'text': text},- {- 'type': 'image_url',- 'image_url': {'url': f'data:image/jpeg;base64,{jpg_base64_str}', 'detail': 'high'},- },- ],- },- ]- response = await model.ainvoke(messages)- return response.completion---async def Online_Mind2Web_eval(task, last_actions, images_path, model, score_threshold):- system_msg = """You are an expert in evaluating the performance of a web navigation agent. The agent is designed to help a human user navigate a website to complete a task. Given the user's task, the agent's action history, key points for task completion, some potentially important web pages in the agent's trajectory and their reasons, your goal is to determine whether the agent has completed the task and achieved all requirements.--Your response must strictly follow the following evaluation criteria!-*Important Evaluation Criteria*:-1: The filtered results must be displayed correctly. If filters were not properly applied (i.e., missing selection, missing confirmation, or no visible effect in results), the task is not considered successful.-2: You must carefully check whether these snapshots and action history meet these key points. Ensure that specific filter conditions, such as "best," "highest," "cheapest," "latest," "most recent," "lowest," "closest," "highest-rated," "largest," and "newest" are correctly applied using the filter function(e.g., sort function).-3: Certain key points or requirements should be applied by the filter. Otherwise, a search with all requirements as input will be deemed a failure since it cannot guarantee that all results meet the requirements!-4: If the task requires filtering by a specific range of money, years, or the number of beds and bathrooms, the applied filter must exactly match the given requirement. Any deviation results in failure. To ensure the task is successful, the applied filter must precisely match the specified range without being too broad or too narrow.-Examples of Failure Cases:-- If the requirement is less than $50, but the applied filter is less than $25, it is a failure.-- If the requirement is $1500-$2500, but the applied filter is $2000-$2500, it is a failure.-- If the requirement is $25-$200, but the applied filter is $0-$200, it is a failure.-- If the required years are 2004-2012, but the filter applied is 2001-2012, it is a failure.-- If the required years are before 2015, but the applied filter is 2000-2014, it is a failure.-- If the task requires exactly 2 beds, but the filter applied is 2+ beds, it is a failure.-5: Some tasks require a submission action or a display of results to be considered successful.-6: If the retrieved information is invalid or empty(e.g., No match was found), but the agent has correctly performed the required action, it should still be considered successful.-7: If the current page already displays all available items, then applying a filter is not necessary. As long as the agent selects items that meet the requirements (e.g., the cheapest or lowest price), the task is still considered successful.--*IMPORTANT*-Format your response into two lines as shown below:--Thoughts: <your thoughts and reasoning process based on double-checking each key points and the evaluation criteria>-Status: "success" or "failure"-"""- prompt = """User Task: {task}--Key Points: {key_points}--Action History:-{last_actions}--The potentially important snapshots of the webpage in the agent's trajectory and their reasons:-{thoughts}"""-- key_points = await identify_key_points(task, model)- key_points = key_points.replace('\n\n', '\n')-- try:- key_points = key_points.split('**Key Points**:')[1]- key_points = '\n'.join(line.lstrip() for line in key_points.splitlines())- except IndexError:- key_points = key_points.split('Key Points:')[-1]- key_points = '\n'.join(line.lstrip() for line in key_points.splitlines())-- tasks = [judge_image(task, image_path, key_points, model) for image_path in images_path]- image_responses = await asyncio.gather(*tasks)-- whole_content_img = []- whole_thoughts = []- record = []- pattern = r'[1-5]'- for response, image_path in zip(image_responses, images_path):- try:- score_text = response.split('Score')[1]- thought = response.split('**Reasoning**:')[-1].strip().lstrip('\n').split('\n\n')[0].replace('\n', ' ')- score = re.findall(pattern, score_text)[0]- record.append({'Response': response, 'Score': int(score)})- except Exception as e:- logger.error(f'Error processing response: {type(e).__name__}: {e}')- score = 0- record.append({'Response': response, 'Score': 0})-- if int(score) >= score_threshold:- jpg_base64_str = encode_image(Image.open(image_path))- whole_content_img.append(- {'type': 'image_url', 'image_url': {'url': f'data:image/png;base64,{jpg_base64_str}', 'detail': 'high'}}- )- if thought != '':- whole_thoughts.append(thought)-- whole_content_img = whole_content_img[:MAX_IMAGE]- whole_thoughts = whole_thoughts[:MAX_IMAGE]- if len(whole_content_img) == 0:- prompt = """User Task: {task}--Key Points: {key_points}--Action History:-{last_actions}"""- text = prompt.format(- task=task,- last_actions='\n'.join(f'{i + 1}. {action}' for i, action in enumerate(last_actions)),- key_points=key_points,- thoughts='\n'.join(f'{i + 1}. {thought}' for i, thought in enumerate(whole_thoughts)),- )-- messages = [- {'role': 'system', 'content': system_msg},- {'role': 'user', 'content': [{'type': 'text', 'text': text}] + whole_content_img},- ]- return messages, text, system_msg, record, key_points---async def Online_Mind2Web_eval_with_retry(task, last_actions, images_path, model, score_threshold, max_retries=3):- """- Wrapper for Online_Mind2Web_eval with retry logic.-- Args:- task: The task description- last_actions: list of actions taken- images_path: list of image paths- model: The model to use for evaluation- score_threshold: Score threshold for image filtering- max_retries: Maximum number of retry attempts-- Returns:- Tuple of (messages, text, system_msg, record, key_points) or None if all retries fail- """- for attempt in range(max_retries):- try:- return await Online_Mind2Web_eval(task, last_actions, images_path, model, score_threshold)- except Exception as e:- if attempt == max_retries - 1: # Last attempt- logger.error(f'Failed to evaluate after {max_retries} attempts. Error: {type(e).__name__}: {str(e)}')- raise- logger.warning(f'Attempt {attempt + 1} failed. Retrying... Error: {type(e).__name__}: {str(e)}')- await asyncio.sleep(2**attempt) # Exponential backoff