π OpenHands: AI-Driven Development
fix(eval): add runtime.connect to all eval harness (#4565)
Xingyao Wang1f23dc89b6ed
diff --git a/docs/modules/usage/how-to/evaluation-harness.md b/docs/modules/usage/how-to/evaluation-harness.mdindex 32717675e3..daf144d11e 100644--- a/docs/modules/usage/how-to/evaluation-harness.md+++ b/docs/modules/usage/how-to/evaluation-harness.md@@ -134,9 +134,11 @@ To create an evaluation workflow for your benchmark, follow these steps:4. Create a function to process each instance:```python+ from openhands.utils.async_utils import call_async_from_syncdef process_instance(instance: pd.Series, metadata: EvalMetadata) -> EvalOutput:config = get_config(instance, metadata)runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance)instruction = get_instruction(instance, metadata)diff --git a/evaluation/EDA/run_infer.py b/evaluation/EDA/run_infer.pyindex 81c7455e00..2c896939a7 100644--- a/evaluation/EDA/run_infer.py+++ b/evaluation/EDA/run_infer.py@@ -23,6 +23,7 @@ from openhands.core.config import (from openhands.core.logger import openhands_logger as loggerfrom openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import MessageAction+from openhands.utils.async_utils import call_async_from_syncgame = None@@ -119,6 +120,7 @@ def process_instance(# Here's how you can run the agent (similar to the `main` function) and get the final task stateruntime = create_runtime(config)+ call_async_from_sync(runtime.connect)state: State | None = asyncio.run(run_controller(diff --git a/evaluation/agent_bench/run_infer.py b/evaluation/agent_bench/run_infer.pyindex f0ea180f4b..d6fcc62e07 100644--- a/evaluation/agent_bench/run_infer.py+++ b/evaluation/agent_bench/run_infer.py@@ -33,6 +33,7 @@ from openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import AgentFinishAction, CmdRunAction, MessageActionfrom openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncdef get_config(@@ -210,6 +211,7 @@ def process_instance(# =============================================runtime: Runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance=instance)diff --git a/evaluation/aider_bench/run_infer.py b/evaluation/aider_bench/run_infer.pyindex c59de4f441..fa1bb9534a 100644--- a/evaluation/aider_bench/run_infer.py+++ b/evaluation/aider_bench/run_infer.py@@ -33,6 +33,7 @@ from openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import CmdRunAction, MessageActionfrom openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_sync# Configure visibility of unit tests to the Agent.USE_UNIT_TESTS = os.environ.get('USE_UNIT_TESTS', 'false').lower() == 'true'@@ -207,6 +208,7 @@ def process_instance(# =============================================runtime: Runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance=instance)diff --git a/evaluation/biocoder/run_infer.py b/evaluation/biocoder/run_infer.pyindex 9dcff6d6ef..4535ccba4e 100644--- a/evaluation/biocoder/run_infer.py+++ b/evaluation/biocoder/run_infer.py@@ -30,6 +30,7 @@ from openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import CmdRunAction, MessageActionfrom openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncAGENT_CLS_TO_FAKE_USER_RESPONSE_FN = {'CodeActAgent': functools.partial(@@ -275,7 +276,7 @@ def process_instance(instruction += AGENT_CLS_TO_INST_SUFFIX[metadata.agent_class]runtime = create_runtime(config)-+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance)# Here's how you can run the agent (similar to the `main` function) and get the final task statediff --git a/evaluation/bird/run_infer.py b/evaluation/bird/run_infer.pyindex 0fba8c5c64..adb498cd2e 100644--- a/evaluation/bird/run_infer.py+++ b/evaluation/bird/run_infer.py@@ -33,6 +33,7 @@ from openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import CmdRunAction, MessageActionfrom openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncdef codeact_user_response(state: State) -> str:@@ -403,6 +404,7 @@ def process_instance(instruction += AGENT_CLS_TO_INST_SUFFIX[metadata.agent_class]runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance)# Here's how you can run the agent (similar to the `main` function) and get the final task statediff --git a/evaluation/gaia/run_infer.py b/evaluation/gaia/run_infer.pyindex f5794d3ad3..c02cd0aee7 100644--- a/evaluation/gaia/run_infer.py+++ b/evaluation/gaia/run_infer.py@@ -29,6 +29,7 @@ from openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import AgentFinishAction, CmdRunAction, MessageActionfrom openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncDATASET_CACHE_DIR = os.path.join(os.path.dirname(__file__), 'data')@@ -142,6 +143,7 @@ def process_instance(logger.info(f'Instruction:\n{instruction}', extra={'msg_type': 'OBSERVATION'})runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance)# Here's how you can run the agent (similar to the `main` function) and get the final task statediff --git a/evaluation/gorilla/run_infer.py b/evaluation/gorilla/run_infer.pyindex ac49a8078d..873cb7f896 100644--- a/evaluation/gorilla/run_infer.py+++ b/evaluation/gorilla/run_infer.py@@ -25,6 +25,7 @@ from openhands.core.config import (from openhands.core.logger import openhands_logger as loggerfrom openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import MessageAction+from openhands.utils.async_utils import call_async_from_syncAGENT_CLS_TO_FAKE_USER_RESPONSE_FN = {'CodeActAgent': codeact_user_response,@@ -81,6 +82,7 @@ def process_instance(# Here's how you can run the agent (similar to the `main` function) and get the final task stateruntime = create_runtime(config)+ call_async_from_sync(runtime.connect)state: State | None = asyncio.run(run_controller(config=config,diff --git a/evaluation/gpqa/run_infer.py b/evaluation/gpqa/run_infer.pyindex fe7ff4bf1c..8fd4034c9d 100644--- a/evaluation/gpqa/run_infer.py+++ b/evaluation/gpqa/run_infer.py@@ -48,6 +48,7 @@ from openhands.events.action import (MessageAction,)from openhands.events.observation import Observation+from openhands.utils.async_utils import call_async_from_syncACTION_FORMAT = """<<FINAL_ANSWER||@@ -215,7 +216,7 @@ Ok now its time to start solving the question. Good luck!"""runtime = create_runtime(config)-+ call_async_from_sync(runtime.connect)state: State | None = asyncio.run(run_controller(config=config,diff --git a/evaluation/humanevalfix/run_infer.py b/evaluation/humanevalfix/run_infer.pyindex 9b0abbc298..25fee65561 100644--- a/evaluation/humanevalfix/run_infer.py+++ b/evaluation/humanevalfix/run_infer.py@@ -38,6 +38,7 @@ from openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import CmdRunAction, MessageActionfrom openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncIMPORT_HELPER = {'python': [@@ -233,6 +234,7 @@ def process_instance(# Here's how you can run the agent (similar to the `main` function) and get the final task stateruntime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance)state: State | None = asyncio.run(run_controller(diff --git a/evaluation/integration_tests/run_infer.py b/evaluation/integration_tests/run_infer.pyindex 5c42efb802..a530041f92 100644--- a/evaluation/integration_tests/run_infer.py+++ b/evaluation/integration_tests/run_infer.py@@ -25,6 +25,7 @@ from openhands.core.logger import openhands_logger as loggerfrom openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import MessageActionfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncFAKE_RESPONSES = {'CodeActAgent': codeact_user_response,@@ -101,6 +102,7 @@ def process_instance(# =============================================runtime: Runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)test_class.initialize_runtime(runtime)diff --git a/evaluation/logic_reasoning/run_infer.py b/evaluation/logic_reasoning/run_infer.pyindex b07af7b08c..5b7d35f211 100644--- a/evaluation/logic_reasoning/run_infer.py+++ b/evaluation/logic_reasoning/run_infer.py@@ -30,6 +30,7 @@ from openhands.events.action import ()from openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncAGENT_CLS_TO_FAKE_USER_RESPONSE_FN = {'CodeActAgent': codeact_user_response,@@ -202,6 +203,7 @@ def process_instance(instruction += AGENT_CLS_TO_INST_SUFFIX[metadata.agent_class]runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance)# Here's how you can run the agent (similar to the `main` function) and get the final task statediff --git a/evaluation/miniwob/run_infer.py b/evaluation/miniwob/run_infer.pyindex ccd36f2d38..9c2aaf1e09 100644--- a/evaluation/miniwob/run_infer.py+++ b/evaluation/miniwob/run_infer.py@@ -35,6 +35,7 @@ from openhands.runtime.browser.browser_env import (BROWSER_EVAL_GET_GOAL_ACTION,BROWSER_EVAL_GET_REWARDS_ACTION,)+from openhands.utils.async_utils import call_async_from_syncSUPPORTED_AGENT_CLS = {'BrowsingAgent'}@@ -127,6 +128,7 @@ def process_instance(logger.info(f'Starting evaluation for instance {env_id}.')runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)task_str = initialize_runtime(runtime)state: State | None = asyncio.run(run_controller(diff --git a/evaluation/mint/run_infer.py b/evaluation/mint/run_infer.pyindex 80127420a4..8017b194d8 100644--- a/evaluation/mint/run_infer.py+++ b/evaluation/mint/run_infer.py@@ -33,6 +33,7 @@ from openhands.events.action import ()from openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncdef codeact_user_response_mint(state: State, task: Task, task_config: dict[str, int]):@@ -176,6 +177,7 @@ def process_instance()runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime)state: State | None = asyncio.run(diff --git a/evaluation/ml_bench/run_infer.py b/evaluation/ml_bench/run_infer.pyindex 0f1ba0ab95..deec068f33 100644--- a/evaluation/ml_bench/run_infer.py+++ b/evaluation/ml_bench/run_infer.py@@ -42,6 +42,7 @@ from openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import CmdRunAction, MessageActionfrom openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncconfig = load_app_config()@@ -233,6 +234,7 @@ def process_instance(instance: Any, metadata: EvalMetadata, reset_logger: bool =instruction += AGENT_CLS_TO_INST_SUFFIX[metadata.agent_class]runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime, instance)# Run the agentdiff --git a/evaluation/swe_bench/eval_infer.py b/evaluation/swe_bench/eval_infer.pyindex 14429fc985..cf6d71d3b3 100644--- a/evaluation/swe_bench/eval_infer.py+++ b/evaluation/swe_bench/eval_infer.py@@ -28,6 +28,7 @@ from openhands.core.logger import openhands_logger as loggerfrom openhands.core.main import create_runtimefrom openhands.events.action import CmdRunActionfrom openhands.events.observation import CmdOutputObservation+from openhands.utils.async_utils import call_async_from_sync# TODO: migrate all swe-bench docker to ghcr.io/openhandsDOCKER_IMAGE_PREFIX = os.environ.get('EVAL_DOCKER_IMAGE_PREFIX', 'docker.io/xingyaoww/')@@ -128,7 +129,7 @@ def process_instance()runtime = create_runtime(config)-+ call_async_from_sync(runtime.connect)# Get patch and save it to /tmp/patch.diffwith tempfile.TemporaryDirectory() as temp_dir:# Patch filediff --git a/evaluation/swe_bench/run_infer.py b/evaluation/swe_bench/run_infer.pyindex a05d17dd5c..9ac1e0cf66 100644--- a/evaluation/swe_bench/run_infer.py+++ b/evaluation/swe_bench/run_infer.py@@ -35,6 +35,7 @@ from openhands.events.observation import CmdOutputObservation, ErrorObservationfrom openhands.events.serialization.event import event_to_dictfrom openhands.runtime.base import Runtimefrom openhands.runtime.utils.shutdown_listener import sleep_if_should_continue+from openhands.utils.async_utils import call_async_from_syncUSE_HINT_TEXT = os.environ.get('USE_HINT_TEXT', 'false').lower() == 'true'USE_INSTANCE_IMAGE = os.environ.get('USE_INSTANCE_IMAGE', 'false').lower() == 'true'@@ -380,6 +381,7 @@ def process_instance(logger.info(f'Starting evaluation for instance {instance.instance_id}.')runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)try:initialize_runtime(runtime, instance)diff --git a/evaluation/toolqa/run_infer.py b/evaluation/toolqa/run_infer.pyindex 85f565120a..5c2c534227 100644--- a/evaluation/toolqa/run_infer.py+++ b/evaluation/toolqa/run_infer.py@@ -26,6 +26,7 @@ from openhands.core.main import create_runtime, run_controllerfrom openhands.events.action import CmdRunAction, MessageActionfrom openhands.events.observation import CmdOutputObservationfrom openhands.runtime.base import Runtime+from openhands.utils.async_utils import call_async_from_syncAGENT_CLS_TO_FAKE_USER_RESPONSE_FN = {'CodeActAgent': codeact_user_response,@@ -103,6 +104,7 @@ def process_instance(instance: Any, metadata: EvalMetadata, reset_logger: bool =logger.info(f'Instruction:\n{instruction}', extra={'msg_type': 'OBSERVATION'})runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)initialize_runtime(runtime)# Here's how you can run the agent (similar to the `main` function) and get the final task statediff --git a/evaluation/webarena/run_infer.py b/evaluation/webarena/run_infer.pyindex 2291aa08c8..cfc2bdae49 100644--- a/evaluation/webarena/run_infer.py+++ b/evaluation/webarena/run_infer.py@@ -35,6 +35,7 @@ from openhands.runtime.browser.browser_env import (BROWSER_EVAL_GET_GOAL_ACTION,BROWSER_EVAL_GET_REWARDS_ACTION,)+from openhands.utils.async_utils import call_async_from_syncSUPPORTED_AGENT_CLS = {'BrowsingAgent'}@@ -143,6 +144,7 @@ def process_instance(logger.info(f'Starting evaluation for instance {env_id}.')runtime = create_runtime(config)+ call_async_from_sync(runtime.connect)task_str = initialize_runtime(runtime)state: State | None = asyncio.run(diff --git a/openhands/core/main.py b/openhands/core/main.pyindex 23763bc457..110856d6e6 100644--- a/openhands/core/main.py+++ b/openhands/core/main.py@@ -122,7 +122,7 @@ async def run_controller(if runtime is None:runtime = create_runtime(config, sid=sid)- await runtime.connect()+ await runtime.connect()event_stream = runtime.event_stream# restore cli session if enableddiff --git a/openhands/runtime/utils/shutdown_listener.py b/openhands/runtime/utils/shutdown_listener.pyindex 9941c9f273..3aedd26722 100644--- a/openhands/runtime/utils/shutdown_listener.py+++ b/openhands/runtime/utils/shutdown_listener.py@@ -4,6 +4,7 @@ This module monitors the app for shutdown signalsimport asyncioimport signal+import threadingimport timefrom types import FrameType@@ -29,8 +30,11 @@ def _register_signal_handlers():if _should_exit is not None:return_should_exit = False- for sig in HANDLED_SIGNALS:- _register_signal_handler(sig)++ # Check if we're in the main thread of the main interpreter+ if threading.current_thread() is threading.main_thread():+ for sig in HANDLED_SIGNALS:+ _register_signal_handler(sig)def should_exit() -> bool: