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]

Enh: Make docker start up delay configurable (#509)

Kilian Liereta27b4c15304a
diff --git a/docs/config/env.md b/docs/config/env.md
index 94379b84..c7d8cc31 100644
--- a/docs/config/env.md
+++ b/docs/config/env.md
@@ -1,5 +1,9 @@
# Environment variables
+!!! hint "Persisting environment variables"
+ All environment variables can also be added to `keys.cfg` instead.
+ See [here](../installation/keys.md) for more information.
+
This page details all environment variables that are currently in use by SWE-agent.
* All API keys (for LMs and GitHub) can be set as an environment variable. See [here](../installation/keys.md) for more information.
@@ -7,4 +11,5 @@ This page details all environment variables that are currently in use by SWE-age
shell sessions. This has not been tested on SWE-bench, so it might sometimes break.
However, it can be very useful when running on single issues.
* [Experimental] `SWE_AGENT_EXPERIMENTAL_CLONE`: Uses sparse clones
+* `SWE_AGENT_DOCKER_START_UP_DELAY`: Number of seconds to wait after starting a docker container
* `SWE_AGENT_CONFIG_ROOT`: Used to resolve relative paths in the [config](config.md)
\ No newline at end of file
diff --git a/docs/usage/cl_tutorial.md b/docs/usage/cl_tutorial.md
index 22bd65ce..8fd3e228 100644
--- a/docs/usage/cl_tutorial.md
+++ b/docs/usage/cl_tutorial.md
@@ -123,15 +123,19 @@ If you solve multiple issues from the same repository/in the same environment, y
`--cache_task_images` flag. This will create a persistent docker image with the initialized environment
required for the problem.
-We are also testing several changes that speed up SWE-agent. They can be enabled by [environment variables](../config/env.md):
+We are also testing several changes that speed up SWE-agent. They can be enabled as [environment variables](../config/env.md) or added to `keys.cfg`:
```bash
# Faster communication with docker container
export SWE_AGENT_EXPERIMENTAL_COMMUNICATE="1"
# Sparse git clone
export SWE_AGENT_EXPERIMENTAL_CLONE="1"
+# Reduce time for docker startup
+export SWE_AGENT_DOCKER_START_UP_DELAY="1"
```
+See [environment variables](../config/env.md) for more details.
+
## Taking actions
* As mentioned [above](#specifying-the-repository), you can use `--apply_patch_locally` to have SWE-agent apply successful solution attempts to local files.
diff --git a/sweagent/api/server.py b/sweagent/api/server.py
index 87200dcb..4e63c03a 100644
--- a/sweagent/api/server.py
+++ b/sweagent/api/server.py
@@ -50,7 +50,7 @@ app.config["SESSION_TYPE"] = "memcache"
THREADS: dict[str, MainThread] = {}
os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"] = "1"
-env_utils.START_UP_DELAY = 1
+env_utils.DOCKER_START_UP_DELAY = 1
def ensure_session_id_set():
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 3371dd32..8343f030 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -232,7 +232,7 @@ class SWEEnv(gym.Env):
else:
logger.info("Trying to clone from non-mirror...")
clone_url = f"https://{token_prefix}github.com/{self.record['repo']}.git"
- if not os.environ.get("SWE_AGENT_EXPERIMENTAL_CLONE"):
+ if not keys_config.get("SWE_AGENT_EXPERIMENTAL_CLONE"):
self.communicate_with_handling(
input=f"git clone {clone_url}",
error_msg="Failed to clone repository from conservative method",
diff --git a/sweagent/environment/utils.py b/sweagent/environment/utils.py
index 78b23148..c33efdad 100644
--- a/sweagent/environment/utils.py
+++ b/sweagent/environment/utils.py
@@ -22,10 +22,10 @@ from ghapi.all import GhApi
from git import InvalidGitRepositoryError, Repo
import docker
+from sweagent.utils.config import keys_config
LOGGER_NAME = "intercode"
-START_UP_DELAY = 5
-TIMEOUT_DURATION = 25
+DOCKER_START_UP_DELAY = int(keys_config.get("SWE_AGENT_DOCKER_START_UP_DELAY", 5))
GITHUB_ISSUE_URL_PATTERN = re.compile(r"github\.com\/(.*?)\/(.*?)\/issues\/(\d+)")
GITHUB_REPO_URL_PATTERN = re.compile(r".*[/@]?github\.com\/([^/]+)\/([^/]+)")
@@ -255,7 +255,7 @@ def get_background_pids(container_obj):
return bash_pids, other_pids
-def _get_non_persistent_container(ctr_name: str, image_name: str) -> tuple[subprocess.Popen, set]:
+def _get_non_persistent_container(ctr_name: str, image_name: str) -> tuple[subprocess.Popen, set[str]]:
startup_cmd = [
"docker",
"run",
@@ -276,17 +276,20 @@ def _get_non_persistent_container(ctr_name: str, image_name: str) -> tuple[subpr
text=True,
bufsize=1, # line buffered
)
- time.sleep(START_UP_DELAY)
+ time.sleep(DOCKER_START_UP_DELAY)
# try to read output from container setup (usually an error), timeout if no output
output = read_with_timeout(container, lambda: list(), timeout_duration=2)
if output:
logger.error(f"Unexpected container setup output: {output}")
+ # bash PID is always 1 for non-persistent containers
return container, {
"1",
- } # bash PID is always 1 for non-persistent containers
+ }
-def _get_persistent_container(ctr_name: str, image_name: str, persistent: bool = False) -> tuple[subprocess.Popen, set]:
+def _get_persistent_container(
+ ctr_name: str, image_name: str, persistent: bool = False
+) -> tuple[subprocess.Popen, set[str]]:
client = docker.from_env()
containers = client.containers.list(all=True, filters={"name": ctr_name})
if ctr_name in [c.name for c in containers]:
@@ -330,7 +333,7 @@ def _get_persistent_container(ctr_name: str, image_name: str, persistent: bool =
text=True,
bufsize=1, # line buffered
)
- time.sleep(START_UP_DELAY)
+ time.sleep(DOCKER_START_UP_DELAY)
# try to read output from container setup (usually an error), timeout if no output
output = read_with_timeout(container, lambda: list(), timeout_duration=2)
if output:
@@ -338,28 +341,25 @@ def _get_persistent_container(ctr_name: str, image_name: str, persistent: bool =
# Get the process IDs of the container
# There should be at least a head process and possibly one child bash process
bash_pids, other_pids = get_background_pids(container_obj)
- total_time_slept = START_UP_DELAY
+ total_time_slept = DOCKER_START_UP_DELAY
+ # Let's wait for a maximum of 5 x DOCKER_START_UP_DELAY seconds
+ # and then check again.
while len(bash_pids) > 1 or len(other_pids) > 0:
time.sleep(1)
total_time_slept += 1
bash_pids, other_pids = get_background_pids(container_obj)
- if total_time_slept > 5 * START_UP_DELAY:
+ if total_time_slept > 5 * DOCKER_START_UP_DELAY:
break
bash_pid = 1
if len(bash_pids) == 1:
bash_pid = bash_pids[0][0]
elif len(bash_pids) > 1 or len(other_pids) > 0:
- msg = f"Detected alien processes attached or running. Please ensure that no other agents are running on this container. PIDs: {bash_pids}, {other_pids}"
+ msg = (
+ "Detected alien processes attached or running. Please ensure that no other agents "
+ f"are running on this container. PIDs: {bash_pids}, {other_pids}"
+ )
raise RuntimeError(msg)
- return container, set(
- map(
- str,
- [
- bash_pid,
- 1,
- ],
- ),
- )
+ return container, {str(bash_pid), "1"}
def get_container(ctr_name: str, image_name: str, persistent: bool = False) -> tuple[subprocess.Popen, set]:
diff --git a/sweagent/utils/config.py b/sweagent/utils/config.py
index 97cfb324..97576201 100644
--- a/sweagent/utils/config.py
+++ b/sweagent/utils/config.py
@@ -17,7 +17,7 @@ def convert_path_to_abspath(path: Path | str) -> Path:
REPO_ROOT as base.
"""
path = Path(path)
- root = Path(os.environ.get("SWE_AGENT_CONFIG_ROOT", REPO_ROOT))
+ root = Path(keys_config.get("SWE_AGENT_CONFIG_ROOT", REPO_ROOT))
assert root.is_dir()
if not path.is_absolute():
path = root / path
diff --git a/tests/conftest.py b/tests/conftest.py
index 1a632427..e3d4b8cd 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -17,7 +17,7 @@ sys.path.insert(0, str(root_dir))
sys.path.insert(1, str(package_dir))
os.environ["SWE_AGENT_EXPERIMENTAL_COMMUNICATE"] = "1"
os.environ["SWE_AGENT_EXPERIMENTAL_CLONE"] = "1"
-env_utils.START_UP_DELAY = 1
+env_utils.DOCKER_START_UP_DELAY = 1
@pytest.fixture()