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]

terminating interactive sessions and getting back output

Talor Abramovichfed7c85af7a3
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 86998bec..7e5db245 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -484,6 +484,14 @@ class SWEEnv(gym.Env):
exit_command = self.args.interactive_sessions_config[self.interactive_session.name].exit_command
return f"Interactive session already open. Please close the current interactive session: {self.interactive_session.name} with the command: `{exit_command}`"
+ def _terminate_interactive_session(self, session_name: str):
+ try:
+ self.interactive_session.session_process.terminate()
+ self.communicate(self.args.interactive_sessions_config[session_name].exit_command)
+ except Exception as e:
+ self.logger.warning(f"Failed to terminate interactive session {session_name}: {e}")
+ self.interactive_session = None
+
def _handle_interactive_commands(self, observation):
session_name, interactive_commands = self.get_interactive_commands(observation)
if session_name is None:
@@ -521,8 +529,7 @@ class SWEEnv(gym.Env):
elif self.interactive_session and self.interactive_session.session_process.poll() is not None:
start_command = self.args.interactive_sessions_config[session_name].start_command
observation = f"Interactive session {session_name} was unexpectedly closed! Please start it again using `{start_command}`"
- self.communicate(self.args.interactive_sessions_config[session_name].exit_command)
- self.interactive_session = None
+ self._terminate_interactive_session(session_name=session_name)
else:
try:
observation += self.communicate_session(
@@ -545,20 +552,18 @@ class SWEEnv(gym.Env):
"\nEXECUTION TIMED OUT AND INTERRUPT FAILED. TERMINATING INTERACTIVE SESSION."
)
self.logger.warning(f"Failed to interrupt container: {e}\nTERMINATING INTERACTIVE SESSION.")
- self.interactive_session.session_process.terminate()
- self.interactive_session = None
+ self._terminate_interactive_session(session_name=session_name)
return observation
except RuntimeError as e:
+ observation += e.args[1] if len(e.args) > 1 else ""
observation += "\nCOMMAND FAILED TO EXECUTE. TERMINATING INTERACTIVE SESSION."
self.logger.warning(f"Failed to execute command: {e}\nTERMINATING INTERACTIVE SESSION.")
- self.interactive_session.session_process.terminate()
- self.interactive_session = None
+ self._terminate_interactive_session(session_name=session_name)
return observation
except BrokenPipeError as e:
observation += "\nBROKEN PIPE ERROR. TERMINATING INTERACTIVE SESSION."
self.logger.error(f"Broken pipe error: {e}\nTERMINATING INTERACTIVE SESSION.")
- self.interactive_session.session_process.terminate()
- self.interactive_session = None
+ self._terminate_interactive_session(session_name=session_name)
return observation
except Exception:
observation += "\nEXECUTION FAILED OR COMMAND MALFORMED"
@@ -633,12 +638,14 @@ class SWEEnv(gym.Env):
else f" BECAUSE THE COMMAND WAS RUNNING FOR MORE THAN {AGENT_ACTION_TIMEOUT} SECONDS."
)
except RuntimeError as e:
+ observation += e.args[1] if len(e.args) > 1 else ""
observation += "\nEXECUTION TIMED OUT AND INTERRUPT FAILED. RESTARTING PROCESS."
info["exit_status"] = "early_exit"
self.logger.warning(f"Failed to interrupt container: {e}\nRESTARTING PROCESS.")
self.reset_container()
return observation, 0, True, info
except RuntimeError as e:
+ observation += e.args[1] if len(e.args) > 1 else ""
observation += "\nCOMMAND FAILED TO EXECUTE. RESTARTING PROCESS."
info["exit_status"] = "early_exit"
self.logger.warning(f"Failed to execute command: {e}\nRESTARTING PROCESS.")
diff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.py
index 73410f8c..9454ce77 100644
--- a/sweagent/environment/utils.py
+++ b/sweagent/environment/utils.py
@@ -277,7 +277,7 @@ def read_with_timeout_experimental(
if container.poll() is not None:
msg = f"Subprocess exited unexpectedly.\nCurrent buffer: {decoded}"
- raise RuntimeError(msg)
+ raise RuntimeError(msg, body)
current_time = time.time()
if not process_done and current_time >= min(end_time, end_time_no_output):
@@ -351,7 +351,7 @@ def read_session_with_timeout(
if session.poll() is not None:
msg = f"Subprocess exited unexpectedly.\nCurrent buffer: {decoded}"
- raise RuntimeError(msg)
+ raise RuntimeError(msg, body)
current_time = time.time()
if not command_done and current_time >= min(end_time, end_time_no_output):
if current_time >= end_time: