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]

Fixed debug_stop functionality, and added support for arguments in debug_start

Meet Udeshi4f794a565bda
diff --git a/config/commands/debug.sh b/config/commands/debug.sh
index 653f7ee3..3b343670 100644
--- a/config/commands/debug.sh
+++ b/config/commands/debug.sh
@@ -28,6 +28,10 @@ debug_start() {
fp=$(realpath $1)
_debug_command "START"
_debug_command "file $fp"
+ if [ ! -z "$2" ]
+ then
+ _debug_command "set args $2"
+ fi
_debug_command "starti"
}
@@ -82,7 +86,7 @@ debug_step() {
# arguments:
# command:
# type: string
-# description: command to execute
+# description: command to execute (wrap in single quotes to avoid shell escaping and substitution)
# required: true
debug_exec() {
if [ -z "$1" ]
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 5cd33aac..5723c447 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -37,7 +37,7 @@ from sweagent.environment.utils import (
copy_file_to_container,
format_trajectory_markdown,
get_container,
- get_interactive_gdb_session,
+ get_interactive_session_gdb,
get_docker_compose,
get_gh_issue_data,
get_instances,
@@ -560,18 +560,16 @@ class SWEEnv(gym.Env):
session_name, interactive_commands = self.get_interactive_commands(observation)
if session_name is not None:
observation = ""
- self.logger.debug(f"Interactive session: {session_name}\nCommands: {', '.join(interactive_commands)}")
for command in interactive_commands:
if command == "START":
# Start the session
- self.interactive_session = get_interactive_gdb_session(self.container_name)
+ self.interactive_session = get_interactive_session_gdb(self.container_name, "/" + self._repo_name)
elif command == "STOP":
- if self.interactive_session.poll() is not None:
- self.logger.warning("gdb did not quit successfully")
- observation = "Could not quit"
- else:
- observation = "Debug session stopped successfully"
- self.interactive_session = None
+ if self.interactive_session.poll() is None:
+ self.logger.warning("Session did not quit successfully, terminating.")
+ self.interactive_session.terminate()
+ observation = "Debug session stopped successfully"
+ self.interactive_session = None
else:
if self.interactive_session is None:
self.logger.warning("Tried to run gdb commands without starting session")
@@ -597,6 +595,15 @@ class SWEEnv(gym.Env):
self.container.terminate()
if self.docker_compose is not None:
terminate_docker_compose(self.docker_compose)
+ if self.interactive_session is not None:
+ try:
+ self.interactive_session.terminate()
+ except KeyboardInterrupt:
+ raise
+ except:
+ self.logger.warning("Failed to stop interactive session")
+ else:
+ self.logger.info("Interactive session stopped")
if self.container_obj is None:
pass
elif self.persistent:
@@ -943,18 +950,21 @@ class SWEEnv(gym.Env):
msg = "Failed to communicate with session"
raise RuntimeError(msg)
- if input.strip() != "quit":
- try:
- buffer = read_session_with_timeout(self.interactive_session, terminal_pattern, timeout_duration)
- except Exception:
- msg = f"Read with timeout failed on input:\n---\n{input}\n---"
- self.logger.error(msg)
- raise
- self.logger.log(logging.TRACE, "Output:\n%s", buffer) # type: ignore
- return buffer
- else:
+ self.logger.debug(f"Command: {input}")
+ if input.strip() == "quit":
+ # Give some time for gdb to stop
+ time.sleep(1)
return ""
+ try:
+ buffer = read_session_with_timeout(self.interactive_session, terminal_pattern, timeout_duration)
+ except Exception:
+ msg = f"Read with timeout failed on input:\n---\n{input}\n---"
+ self.logger.error(msg)
+ raise
+ self.logger.log(logging.TRACE, "Output:\n%s", buffer) # type: ignore
+ return buffer
+
def get_available_actions(self) -> list[str]:
"""
Returns list of available actions in current environment state
diff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.py
index 61914c2d..8c956ca5 100644
--- a/sweagent/environment/utils.py
+++ b/sweagent/environment/utils.py
@@ -400,14 +400,12 @@ def get_docker_compose(docker_compose_path: Path) -> Path:
logger.error(f"Unexpected compose setup error: {error}")
return docker_compose_path
-def get_interactive_gdb_session(ctr_name: str) -> subprocess.Popen:
- startup_cmd = ["docker", "exec", "-i", ctr_name, "gdb"]
+def get_interactive_session_gdb(ctr_name: str, cwd: str) -> subprocess.Popen:
+ startup_cmd = ["docker", "exec", "-i", "-w", cwd, ctr_name, "gdb"]
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)
- logger.debug(f"gdb session initial output: {output}")
-
return session
def _get_non_persistent_container(ctr_name: str, image_name: str) -> tuple[subprocess.Popen, set[str]]: