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]

Add test for caching of task envs

Kilian Lieret98d533ad37d6
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index d9bd4eaa..224044ea 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -39,7 +39,7 @@ from swebench import (
get_requirements,
MAP_VERSION_TO_INSTALL
)
-from typing import Optional, Tuple
+from typing import List, Optional, Tuple
LONG_TIMEOUT = 500
PATH_TO_REQS = "/root/requirements.txt"
@@ -142,13 +142,11 @@ class SWEEnv(gym.Env):
self.image_name = args.image_name
self._reset_container()
- # Prepare image tag prefix for cached task environments
+ #: Prefix for cached task docker images
+ self.cached_image_prefix: str = ""
if self.args.cache_task_images:
logger.info("Task environment caching enabled")
- tag = f"{self.args.data_path.replace('/', '_')}__{self.args.split}__{self.args.base_commit or 'head'}__"
- assert len(tag) < 128, f"Cached image tag {tag} too long, probably due to long data path or base commit hash."
- image_name_without_tag = self.image_name.split(":")[0]
- self.cached_image_prefix = f"{image_name_without_tag}:{tag}"
+ self.cached_image_prefix = self._get_cached_task_image_prefix()
# Set timeout
self.timeout = self.args.timeout
@@ -156,6 +154,16 @@ class SWEEnv(gym.Env):
self.clean_multi_line_functions = lambda x: x
self.hooks = []
+ def _get_cached_task_image_prefix(self) -> str:
+ inputs: List[str] = [
+ self.args.data_path,
+ self.args.split,
+ self.args.base_commit or "head",
+ ]
+ tag = hashlib.sha256("".join(inputs).encode()).hexdigest()[:50]
+ image_name_without_tag = self.image_name.split(":")[0]
+ return f"{image_name_without_tag}:task-{tag}"
+
def add_hook(self, hook: EnvHook):
hook.on_init()
self.hooks.append(hook)
diff --git a/tests/test_env.py b/tests/test_env.py
index 8baa9aab..0be45862 100644
--- a/tests/test_env.py
+++ b/tests/test_env.py
@@ -2,6 +2,7 @@ import dataclasses
import os
from pathlib import Path
import subprocess
+import time
import pytest
import yaml
from sweagent.environment.swe_env import EnvHook, EnvironmentArguments, SWEEnv
@@ -55,6 +56,33 @@ def test_init_swe_env_non_persistent(test_env_args):
env.reset()
+def test_init_swe_env_cached_task_image(test_env_args):
+ test_env_args = dataclasses.replace(test_env_args, cache_task_images=True)
+ start = time.perf_counter()
+ with swe_env_context(test_env_args) as env:
+ env.reset()
+ duration_no_cache = time.perf_counter() - start
+ start = time.perf_counter()
+ # now it should be cached, so let's run again
+ image_prefix = None
+ with swe_env_context(test_env_args) as env:
+ env.reset()
+ image_prefix = env.cached_image_prefix
+ assert image_prefix
+ duration_cache = time.perf_counter() - start
+ assert duration_cache < duration_no_cache
+ # Retrieve all images with a prefix "prefix"
+ client = docker.from_env()
+ # Remove the images
+ for image in client.images.list():
+ if not image.tags:
+ continue
+ if not image.tags[0].startswith(image_prefix):
+ continue
+ client.images.remove(image.id)
+
+
@pytest.mark.slow
def test_execute_setup_script(tmp_path, test_env_args):
test_script = "echo 'hello world'"