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]
Tests to use fast experimental communication strategy (#230)
Kilian Liereta9f3bb1d67b7
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex da750e89..ad780678 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -19,6 +19,8 @@ from rich.logging import RichHandlerfrom simple_parsing.helpers.serialization.serializable import FrozenSerializableimport yamlfrom sweagent.environment.utils import (+ PROCESS_DONE_MARKER_END,+ PROCESS_DONE_MARKER_START,copy_anything_to_container,copy_file_to_container,format_trajectory_markdown,@@ -28,6 +30,7 @@ from sweagent.environment.utils import (parse_gh_issue_url,read_with_timeout,LOGGER_NAME,+ read_with_timeout_experimental,)from swebench import (get_environment_yml,@@ -445,11 +448,40 @@ class SWEEnv(gym.Env):error_msg="Failed to add commands directory to PATH",)+ def _communicate_experimental(+ self,+ input: str,+ timeout_duration=25,+ ) -> str:+ """Experimental version of `_communicate`"""++ command_suffix = f"echo {PROCESS_DONE_MARKER_START}$?{PROCESS_DONE_MARKER_END}\n"+ try:+ self.returncode = None+ cmd = input if input.endswith("\n") else input + "\n"+ cmd += command_suffix+ print(cmd)+ os.write(self.container.stdin.fileno(), cmd.encode())+ time.sleep(0.03)+ self.container.stdin.flush()+ except BrokenPipeError:+ traceback.print_exc()+ self.logger.error(+ "Failed to communicate with container. Check docker logs for more information."+ )+ raise RuntimeError("Failed to communicate with container")++ buffer, exit_code = read_with_timeout_experimental(self.container, timeout_duration)+ self.returncode = int(exit_code)+ return buffer+def _communicate(self,input: str,timeout_duration=25,) -> str:+ if "SWE_AGENT_EXPERIMENTAL_COMMUNICATE" in os.environ:+ return self._communicate_experimental(input, timeout_duration)try:self.returncode = Nonecmd = input if input.endswith("\n") else input + "\n"diff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.pyindex 3ca40c84..4dd3c36c 100644--- a/sweagent/environment/utils.py+++ b/sweagent/environment/utils.py@@ -161,6 +161,57 @@ def read_with_timeout(container, pid_func, timeout_duration):return buffer.decode()+PROCESS_DONE_MARKER_START = "///PROCESS-DONE:"+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, timeout_duration):+ """+ 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.++ NOTE: This is an experimental implementation that is faster than `read_with_timeout`, but+ has not been thoroughly tested.++ Args:+ container (subprocess.Popen): The subprocess container.+ timeout_duration (int): The timeout duration in seconds.++ Returns:+ str: The data read from the subprocess, stripped of trailing newline characters.++ Raises:+ TimeoutError: If the timeout duration is reached while reading from the subprocess.+ """+ buffer = b""+ fd = container.stdout.fileno()+ end_time = time.time() + timeout_duration++ while time.time() < end_time:+ ready_to_read, _, _ = select.select([fd], [], [], 0.01)+ if ready_to_read:+ data = os.read(fd, 4096)+ if data:+ buffer += data+ if PROCESS_DONE_MARKER_START in buffer.decode():+ break+ time.sleep(0.01) # Prevents CPU hogging++ if container.poll() is not None:+ raise RuntimeError("Subprocess exited unexpectedly.\nCurrent buffer: {}".format(buffer.decode()))+ if time.time() >= end_time:+ raise TimeoutError("Timeout reached while reading from subprocess.\nCurrent buffer: {}".format(buffer.decode()))+ decoded = buffer.decode()+ body = "\n".join(line for line in decoded.splitlines() if not line.startswith(PROCESS_DONE_MARKER_START))+ last_line = decoded.splitlines()[-1]+ _results = PROCESS_DONE_REGEX.search(last_line)+ if _results is None:+ raise ValueError(f"Could not find process done marker in last line: {last_line=}, {body=}")+ exit_code = _results.group(1)+ return body, exit_code++class timeout:def __init__(self, seconds=TIMEOUT_DURATION, error_message="Timeout"):self.seconds = secondsdiff --git a/tests/conftest.py b/tests/conftest.pyindex 085b6e3a..5345e892 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -1,3 +1,4 @@+import osimport jsonimport sysfrom pathlib import Path@@ -10,7 +11,7 @@ root_dir = _this_dir.parentpackage_dir = root_dir / "sweagent"sys.path.insert(0, str(root_dir))sys.path.insert(1, str(package_dir))-print("Adjusted path: ", sys.path)+os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"] = "1"@fixturediff --git a/tests/test_env.py b/tests/test_env.pyindex 68034480..1b204260 100644--- a/tests/test_env.py+++ b/tests/test_env.py@@ -1,4 +1,5 @@import dataclasses+import osfrom pathlib import Pathimport pytestimport yaml@@ -80,3 +81,15 @@ def test_interrupt_close(test_env_args):env = SWEEnv(test_env_args)env.interrupt()env.close()+++def test_communicate_old(test_env_args):+ del os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"]+ try:+ env = SWEEnv(test_env_args)+ env.reset()+ except:+ raise+ finally:+ os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"] = "1"\ No newline at end of file