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]

Ensure container_name is reset for non-persistent containers (#463)

Kilian Lieret82e6d03a5e1c
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index c15e81ef..d9aff994 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -477,6 +477,17 @@ class SWEEnv(gym.Env):
self.container_obj = None
self._reset_container()
+ @staticmethod
+ def _get_container_name(image_name: str) -> str:
+ """Return name of container"""
+ process_id = str(os.getpid())
+ current_time = str(datetime.datetime.now())
+ unique_string = current_time + process_id
+ hash_object = hashlib.sha256(unique_string.encode())
+ image_name_sanitized = image_name.replace("/", "-")
+ image_name_sanitized = image_name_sanitized.replace(":", "-")
+ return f"{image_name_sanitized}-{hash_object.hexdigest()[:10]}"
+
def _init_container(self, cached_image: str | None = None) -> None:
"""
Handles container initialization. Defines container name and creates it.
@@ -486,16 +497,12 @@ class SWEEnv(gym.Env):
if cached_image is not None:
image_name = cached_image
logger.info(f"Using cached image: {image_name}")
- if self.container_name is None:
- process_id = str(os.getpid())
- current_time = str(datetime.datetime.now())
- unique_string = current_time + process_id
- hash_object = hashlib.sha256(unique_string.encode())
- # Cannot have colons/slashes in container name, but those are important in image names
- # i.e., when we want swe-agent to pull the image from dockerhub
- image_name_sanitized = image_name.replace("/", "-")
- image_name_sanitized = image_name_sanitized.replace(":", "-")
- self.container_name = f"{image_name_sanitized}-{hash_object.hexdigest()[:10]}"
+ if self.persistent:
+ assert self.container_name is not None
+ else:
+ # Make sure that we get a new container name just in case removing didn't work.
+ # Might be a fix for https://github.com/princeton-nlp/SWE-agent/issues/451
+ self.container_name = self._get_container_name(image_name)
self.container, self.parent_pids = get_container(self.container_name, image_name, persistent=self.persistent)
try:
client = docker.from_env(timeout=600)