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]

Web: Improve handling of container closing

Kilian Lieret62b6bdf45099
diff --git a/run.py b/run.py
index 81ac7eda..dc13cb0d 100644
--- a/run.py
+++ b/run.py
@@ -369,6 +369,8 @@ class Main:
break
except SystemExit:
logger.critical(f"❌ Exiting because SystemExit was called")
+ self.env.close()
+ logger.info("Container closed")
raise
except Exception as e:
traceback.print_exc()
diff --git a/sweagent/api/hooks.py b/sweagent/api/hooks.py
index af7bf70f..2ae8b592 100644
--- a/sweagent/api/hooks.py
+++ b/sweagent/api/hooks.py
@@ -7,6 +7,7 @@ from sweagent.agent.agents import AgentHook
from flask_socketio import SocketIO
from sweagent.api.utils import strip_ansi_sequences
+from sweagent.environment.swe_env import EnvHook
# baaaaaaad
sys.path.append(str(PACKAGE_DIR.parent))
@@ -112,6 +113,15 @@ class AgentUpdateHook(AgentHook):
self._wu.up_env(message=msg, thought_idx=self._thought_idx, type_=type_)
+class EnvUpdateHook(EnvHook):
+ def __init__(self, wu: WebUpdate):
+ """This hooks into the environment class to update the web interface"""
+ self._wu = wu
+
+ def on_close(self):
+ self._wu.up_env(message="Environment closed", format="text", type_="info")
+
+
# def on_query_message_added(
# self,
diff --git a/sweagent/api/server.py b/sweagent/api/server.py
index d75fe51d..96cd0f88 100644
--- a/sweagent/api/server.py
+++ b/sweagent/api/server.py
@@ -11,7 +11,7 @@ from sweagent.agent.agents import AgentArguments
from sweagent.agent.models import ModelArguments
from sweagent.api.utils import ThreadWithExc
from sweagent.environment.swe_env import EnvironmentArguments
-from sweagent.api.hooks import WebUpdate, MainUpdateHook, AgentUpdateHook
+from sweagent.api.hooks import EnvUpdateHook, WebUpdate, MainUpdateHook, AgentUpdateHook
import sweagent.environment.utils as env_utils
from flask_socketio import SocketIO
from flask_cors import CORS
@@ -58,6 +58,7 @@ class MainThread(ThreadWithExc):
main = Main(self._settings)
main.add_hook(MainUpdateHook(self._wu))
main.agent.add_hook(AgentUpdateHook(self._wu))
+ main.env.add_hook(EnvUpdateHook(self._wu))
main.main()
except Exception as e:
self._wu.up_agent(f"Error (see log for details): {e}")
@@ -71,6 +72,8 @@ class MainThread(ThreadWithExc):
while self.is_alive():
self.raise_exc(SystemExit)
time.sleep(0.1)
+ self._wu.finish_run()
+ self._wu.up_agent("Run stopped by user")
@app.route('/')
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 924bd285..8137ac8e 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -88,6 +88,9 @@ class EnvHook:
def on_install_env_started(self):
...
+ def on_close(self):
+ ...
+
class SWEEnv(gym.Env):
"""Gym environment for SWE-bench. This class should handle all communication with the docker container."""
@@ -400,6 +403,8 @@ class SWEEnv(gym.Env):
except:
pass
self.logger.info("Agent container stopped")
+ for hook in self.hooks:
+ hook.on_close()
# MARK: Helper functions #