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]

Feat: experimental fast cloning (#498)

Kilian Lieret8f93aceedd77
diff --git a/docs/config/env.md b/docs/config/env.md
index 77f13125..94379b84 100644
--- a/docs/config/env.md
+++ b/docs/config/env.md
@@ -6,4 +6,5 @@ This page details all environment variables that are currently in use by SWE-age
* [Experimental] `SWE_AGENT_EXPERIMENTAL_COMMUNICATE` switches to a faster way of communicating with running docker
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_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 7b1c9ad9..22bd65ce 100644
--- a/docs/usage/cl_tutorial.md
+++ b/docs/usage/cl_tutorial.md
@@ -117,6 +117,21 @@ The config file can have the following keys:
* `install`: A custom command
* `post_install`: A list of custom commands
+## Speeding up SWE-agent
+
+If you solve multiple issues from the same repository/in the same environment, you can specify the
+`--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):
+
+```bash
+# Faster communication with docker container
+export SWE_AGENT_EXPERIMENTAL_COMMUNICATE="1"
+# Sparse git clone
+export SWE_AGENT_EXPERIMENTAL_CLONE="1"
+```
+
## 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/environment/swe_env.py b/sweagent/environment/swe_env.py
index a5788fd1..5b9779b0 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -201,6 +201,7 @@ class SWEEnv(gym.Env):
def _copy_repo(self) -> str:
"""Clone/copy repository/codebase in container
+
Returns:
folder name of clone
"""
@@ -225,20 +226,34 @@ class SWEEnv(gym.Env):
# fixme: This if statement is brittle and should probably be replaced with better logic
if not self.args.no_mirror and self.record["problem_statement_source"] == "swe-bench":
self.logger.info(f"{self._repo_name} not found in container, cloning...")
+ clone_url = f"https://{token_prefix}github.com/swe-bench/{self._repo_name}.git"
+ 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"):
self.communicate_with_handling(
- input=f"git clone https://{token_prefix}github.com/swe-bench/{self._repo_name}.git",
- error_msg="Failed to clone repository from mirror",
+ input=f"git clone {clone_url}",
+ error_msg="Failed to clone repository from conservative method",
timeout_duration=LONG_TIMEOUT,
)
- return self._repo_name
else:
- logger.info("Trying to clone from non-mirror...")
+ base_commit = self.record["base_commit"]
self.communicate_with_handling(
- input=f"git clone https://{token_prefix}github.com/{self.record['repo']}.git {self._repo_name}",
- error_msg="Failed to clone repository from non-mirror",
+ input="&&".join(
+ (
+ f"mkdir {self._repo_name}",
+ f"cd {self._repo_name}",
+ "git init",
+ f"git remote add origin {clone_url}",
+ f"git fetch --depth 1 origin {base_commit}",
+ "git checkout FETCH_HEAD",
+ "cd ..",
+ )
+ ),
+ error_msg="Failed to clone repository with fast method",
timeout_duration=LONG_TIMEOUT,
)
- return self._repo_name
+ return self._repo_name
def reset(self, index: int | None = None, apply_test_patch: bool = False) -> tuple[str | None, dict]:
"""
diff --git a/tests/conftest.py b/tests/conftest.py
index cd2cc5db..1a632427 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -16,6 +16,7 @@ package_dir = root_dir / "sweagent"
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