Skip to content

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]

changed get_interactive_session_gdb to be generic

Talor Abramovichbffa34c9c0d0
diff --git a/.gitignore b/.gitignore
index 86a5294d..2a834fcb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -193,3 +193,7 @@ data/**
# PyCharm
.idea/
+
+# Disasm / Decomp output
+*.decomp.json
+*.disas.json
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 5723c447..0ae2e0dc 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -37,15 +37,15 @@ from sweagent.environment.utils import (
copy_file_to_container,
format_trajectory_markdown,
get_container,
- get_interactive_session_gdb,
get_docker_compose,
get_gh_issue_data,
get_instances,
+ get_interactive_session,
image_exists,
parse_gh_issue_url,
+ read_session_with_timeout,
read_with_timeout,
read_with_timeout_experimental,
- read_session_with_timeout,
terminate_docker_compose,
)
from sweagent.types import AgentInfo
@@ -563,7 +563,9 @@ class SWEEnv(gym.Env):
for command in interactive_commands:
if command == "START":
# Start the session
- self.interactive_session = get_interactive_session_gdb(self.container_name, "/" + self._repo_name)
+ self.interactive_session = get_interactive_session(
+ self.container_name, "/" + self._repo_name, "gdb"
+ )
elif command == "STOP":
if self.interactive_session.poll() is None:
self.logger.warning("Session did not quit successfully, terminating.")
@@ -577,7 +579,7 @@ class SWEEnv(gym.Env):
else:
observation += self.communicate_session(command, GDB_TERMINAL_PATTERN)
observation += "\n"
-
+
return observation, 0, False, info
def close(self) -> None:
diff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.py
index 8c956ca5..278956ca 100644
--- a/sweagent/environment/utils.py
+++ b/sweagent/environment/utils.py
@@ -273,6 +273,7 @@ def read_with_timeout_experimental(container: subprocess.Popen, timeout_duration
exit_code = _results.group(1)
return body, exit_code
+
def read_session_with_timeout(session: subprocess.Popen, terminal_pattern: str, timeout_duration: int | float) -> str:
"""
Read data from a subprocess with a timeout.
@@ -295,6 +296,7 @@ def read_session_with_timeout(session: subprocess.Popen, terminal_pattern: str,
# Select is not available on windows
import select
+
def ready_to_read(fd) -> bool:
return bool(select.select([fd], [], [], 0.01)[0])
@@ -319,8 +321,8 @@ def read_session_with_timeout(session: subprocess.Popen, terminal_pattern: str,
raise TimeoutError(msg)
decoded = buffer.decode("utf-8", errors="backslashreplace").replace("\r\n", "\n")
- body = "\n".join(line for line in decoded.splitlines() if not line.startswith(terminal_pattern))
- return body
+ return "\n".join(line for line in decoded.splitlines() if not line.startswith(terminal_pattern))
+
def get_background_pids(container_obj: Container):
pids = container_obj.exec_run("ps -eo pid,comm --no-headers").output.decode().split("\n")
@@ -400,14 +402,20 @@ def get_docker_compose(docker_compose_path: Path) -> Path:
logger.error(f"Unexpected compose setup error: {error}")
return docker_compose_path
-def get_interactive_session_gdb(ctr_name: str, cwd: str) -> subprocess.Popen:
- startup_cmd = ["docker", "exec", "-i", "-w", cwd, ctr_name, "gdb"]
+
+def get_interactive_session(ctr_name: str, cwd: str, *args) -> subprocess.Popen:
+ """
+ Starts a new interacitve session on the given container name.
+ Returns a subprocess.Popen object that is available for further read/writes for submitting commands and reading output.
+ """
+ startup_cmd = ["docker", "exec", "-i", "-w", cwd, ctr_name, *args]
logger.debug("Starting interactive gdb session with command: %s", shlex.join(startup_cmd))
session = subprocess.Popen(startup_cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, text=True, bufsize=1)
time.sleep(DOCKER_START_UP_DELAY)
- output = read_with_timeout(session, lambda: list(), timeout_duration=1)
+ _ = read_with_timeout(session, lambda: list(), timeout_duration=1)
return session
+
def _get_non_persistent_container(ctr_name: str, image_name: str) -> tuple[subprocess.Popen, set[str]]:
startup_cmd = [
"docker",