SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]
Enh: Make timeout for agent commands configurable (#674)
Kilian Lieret2f14deed86e8
diff --git a/docs/config/env.md b/docs/config/env.mdindex 2663896d..d2126a58 100644--- a/docs/config/env.md+++ b/docs/config/env.md@@ -9,6 +9,7 @@ This page details all environment variables that are currently in use by SWE-age* All API keys (for LMs and GitHub) can be set as an environment variable. See [here](../installation/keys.md) for more information.* `SWE_AGENT_CONFIG_ROOT`: Used to resolve relative paths in the [config](config.md)* `SWE_AGENT_ENV_LONG_TIMEOUT` (default: 500): Timeout in seconds used for commands that install instance environment.+* `AGENT_ACTION_TIMEOUT` (default: 25): Timeout in seconds used for commands issued by the agent* `SWE_AGENT_MODEL_MAX_RETRIES` (default: 10): Maximum retries when querying the modelThe following three variables can only be set as environment variables, not in the config filediff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 01810a86..a5ceddca 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -46,6 +46,7 @@ from sweagent.utils.config import keys_configfrom sweagent.utils.log import default_logger, get_loggerLONG_TIMEOUT = float(keys_config.get("SWE_AGENT_ENV_LONG_TIMEOUT", 500))+AGENT_ACTION_TIMEOUT = float(keys_config.get("SWE_AGENT_ACTION_TIMEOUT", 25))PATH_TO_REQS = "/root/requirements.txt"PATH_TO_ENV_YML = "/root/environment.yml"@@ -451,7 +452,7 @@ class SWEEnv(gym.Env):# Attempt to run action in containerobservation = ""try:- observation = self.communicate(input=action, timeout_duration=25, set_last_action=True)+ observation = self.communicate(input=action, timeout_duration=AGENT_ACTION_TIMEOUT, set_last_action=True)except TimeoutError:try:self.interrupt()@@ -640,7 +641,7 @@ class SWEEnv(gym.Env):def _communicate_experimental(self,input: str,- timeout_duration=25,+ timeout_duration: int | float = 25,) -> str:"""Experimental version of `_communicate`"""assert self.container is not None@@ -689,7 +690,7 @@ class SWEEnv(gym.Env):def _communicate(self,input: str,- timeout_duration=25,+ timeout_duration: int | float = 25,) -> str:"""Runs command in container and returns output@@ -740,7 +741,7 @@ class SWEEnv(gym.Env):output = self._communicate(f"/bin/bash -n <<'EOF'\n{input}\nEOF\n")return output, self.returncode == 0- def communicate(self, input: str, timeout_duration: int = 25, *, set_last_action: bool = False) -> str:+ def communicate(self, input: str, timeout_duration: int | float = 25, *, set_last_action: bool = False) -> str:"""Sends input to container and returns output@@ -776,7 +777,7 @@ class SWEEnv(gym.Env):self.communicate_output = ""return ""- def communicate_with_handling(self, input: str, error_msg: str, timeout_duration: int = 25) -> str:+ def communicate_with_handling(self, input: str, error_msg: str, timeout_duration: int | float = 25) -> str:"""Wrapper for communicate function that raises error if return code is non-zerodiff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.pyindex 6dbc80e8..a4a7cc91 100644--- a/sweagent/environment/utils.py+++ b/sweagent/environment/utils.py@@ -120,7 +120,7 @@ def copy_anything_to_container(container: Container, host_path: str, container_praise RuntimeError(msg) from e-def read_with_timeout(container: subprocess.Popen, pid_func: Callable, timeout_duration: int) -> str:+def read_with_timeout(container: subprocess.Popen, pid_func: Callable, timeout_duration: int | float) -> str:"""Read data from a subprocess with a timeout.This function uses a file descriptor to read data from the subprocess in a non-blocking way.@@ -182,7 +182,7 @@ PROCESS_DONE_MARKER_END = ":PROCESS-DONE///"PROCESS_DONE_REGEX = re.compile(rf"{PROCESS_DONE_MARKER_START}(.+?){PROCESS_DONE_MARKER_END}")-def read_with_timeout_experimental(container: subprocess.Popen, timeout_duration: int) -> tuple[str, str]:+def read_with_timeout_experimental(container: subprocess.Popen, timeout_duration: int | float) -> tuple[str, str]:"""Read data from a subprocess with a timeout.This function uses a file descriptor to read data from the subprocess in a non-blocking way.