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]
added container mounts for caching ghidra out
Talor Abramovich1b60871ab497
diff --git a/.gitignore b/.gitignoreindex 86a5294d..2a834fcb 100644--- a/.gitignore+++ b/.gitignore@@ -193,3 +193,7 @@ data/**# PyCharm.idea/++# Disasm / Decomp output+*.decomp.json+*.disas.jsondiff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 21ca1f0b..ae71e2cf 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -11,7 +11,7 @@ import shleximport subprocessimport timeimport traceback-from dataclasses import dataclass+from dataclasses import dataclass, fieldfrom pathlib import Path, PurePathfrom typing import Any@@ -95,6 +95,8 @@ class EnvironmentArguments(FrozenSerializable):environment_setup: str | None = None# Only used when running on single issue. Path to local repository or github repository.repo_path: str = ""+ # Container mounts - additional folders to mount into the environment (useful for caching)+ container_mounts: list[str] = field(default_factory=list)def __post_init__(self):if self.timeout is not None:@@ -153,6 +155,7 @@ class SWEEnv(gym.Env):self.install_environment = args.install_environmentself.logger = get_logger("SWEEnv")self.persistent = args.container_name is not None+ self.container_mounts = args.container_mountsself.returncode: None | int = Noneif not self.args.verbose:# fixme: This creates problems if we have multiple instances of this class@@ -680,7 +683,9 @@ class SWEEnv(gym.Env):# 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/451self.container_name = self._get_container_name(image_name)- self.container, self.parent_pids = get_container(self.container_name, image_name, persistent=self.persistent)+ self.container, self.parent_pids = get_container(+ self.container_name, image_name, persistent=self.persistent, container_mounts=self.container_mounts+ )try:client = docker.from_env(timeout=600)except docker.errors.DockerException as e:diff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.pyindex ce8b57ed..da287f7c 100644--- a/sweagent/environment/utils.py+++ b/sweagent/environment/utils.py@@ -22,6 +22,7 @@ from git import InvalidGitRepositoryError, Repofrom unidiff import PatchSetimport docker+import docker.typesfrom docker.models.containers import Containerfrom sweagent.utils.config import keys_configfrom sweagent.utils.log import get_logger@@ -353,12 +354,27 @@ def get_docker_compose(docker_compose_path: Path) -> Path:return docker_compose_path-def _get_non_persistent_container(ctr_name: str, image_name: str) -> tuple[subprocess.Popen, set[str]]:+def _get_container_mounts_list(container_mounts: list[str]) -> list[docker.types.Mount]:+ try:+ for i in range(len(container_mounts)):+ path = Path(container_mounts[i]).absolute()+ if path.is_dir():+ container_mounts[i] = docker.types.Mount(source=str(path), target=f"/{path.name}")+ return container_mounts+ except Exception:+ logger.warning("Failed to process container mounts, skipping mount.")+ return []+++def _get_non_persistent_container(+ ctr_name: str, image_name: str, container_mounts: list[str]+) -> tuple[subprocess.Popen, set[str]]:startup_cmd = ["docker","run","-i","--rm",+ *[item for mount in container_mounts for item in ("-v", f"{Path(mount).absolute()}:/{Path(mount).name}")],"--name",ctr_name,image_name,@@ -386,7 +402,7 @@ def _get_non_persistent_container(ctr_name: str, image_name: str) -> tuple[subprdef _get_persistent_container(- ctr_name: str, image_name: str, persistent: bool = False+ ctr_name: str, image_name: str, container_mounts: list[str], persistent: bool = False) -> tuple[subprocess.Popen, set[str]]:client = docker.from_env()containers = client.containers.list(all=True, filters={"name": ctr_name})@@ -404,6 +420,7 @@ def _get_persistent_container(msg = f"Unexpected container status: {container_obj.status}"raise RuntimeError(msg)else:+ container_mounts = _get_container_mounts_list(container_mounts)container_obj = client.containers.run(image_name,command="/bin/bash -l -m",@@ -412,6 +429,7 @@ def _get_persistent_container(tty=True,detach=True,auto_remove=not persistent,+ mounts=container_mounts,)container_obj.start()startup_cmd = [@@ -460,7 +478,9 @@ def _get_persistent_container(return container, {str(bash_pid), "1"}-def get_container(ctr_name: str, image_name: str, persistent: bool = False) -> tuple[subprocess.Popen, set]:+def get_container(+ ctr_name: str, image_name: str, container_mounts: list[str], persistent: bool = False+) -> tuple[subprocess.Popen, set]:"""Get a container object for a given container name and image name@@ -480,9 +500,9 @@ def get_container(ctr_name: str, image_name: str, persistent: bool = False) -> traise RuntimeError(msg)if persistent:- return _get_persistent_container(ctr_name, image_name)+ return _get_persistent_container(ctr_name, image_name, container_mounts=container_mounts)else:- return _get_non_persistent_container(ctr_name, image_name)+ return _get_non_persistent_container(ctr_name, image_name, container_mounts=container_mounts)def image_exists(image_name: str) -> bool: