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]

Ref: Get reset commands from repo (#1025)

Kilian Lieret892801dde9a7
diff --git a/sweagent/environment/repo.py b/sweagent/environment/repo.py
index ee1fdf83..0abf4438 100644
--- a/sweagent/environment/repo.py
+++ b/sweagent/environment/repo.py
@@ -24,6 +24,17 @@ class Repo(Protocol):
def copy(self, deployment: AbstractDeployment): ...
+ def get_reset_commands(self) -> list[str]: ...
+
+
+def _get_git_reset_commands(base_commit: str) -> list[str]:
+ return [
+ "git status",
+ "git restore .",
+ f"git reset --hard {base_commit}",
+ "git clean -fdq",
+ ]
+
class PreExistingRepoConfig(BaseModel):
"""Use this to specify a repository that already exists on the deployment.
@@ -50,6 +61,10 @@ class PreExistingRepoConfig(BaseModel):
"""Does nothing."""
pass
+ def get_reset_commands(self) -> list[str]:
+ """Issued after the copy operation or when the environment is reset."""
+ return _get_git_reset_commands(self.base_commit)
+
class LocalRepoConfig(BaseModel):
path: Path
@@ -93,6 +108,10 @@ class LocalRepoConfig(BaseModel):
msg = f"Failed to change permissions on copied repository (exit code: {r.exit_code}, stdout: {r.stdout}, stderr: {r.stderr})"
raise RuntimeError(msg)
+ def get_reset_commands(self) -> list[str]:
+ """Issued after the copy operation or when the environment is reset."""
+ return _get_git_reset_commands(self.base_commit)
+
class GithubRepoConfig(BaseModel):
github_url: str
@@ -157,6 +176,10 @@ class GithubRepoConfig(BaseModel):
),
)
+ def get_reset_commands(self) -> list[str]:
+ """Issued after the copy operation or when the environment is reset."""
+ return _get_git_reset_commands(self.base_commit)
+
RepoConfig = LocalRepoConfig | GithubRepoConfig | PreExistingRepoConfig
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 0bf12efd..792a6633 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -149,15 +149,12 @@ class SWEEnv:
def _reset_repository(self) -> None:
"""Clean repository of any modifications + Checkout base commit"""
if self.repo is not None:
- self.logger.debug("Resetting repository %s to commit %s", self.repo.repo_name, self.repo.base_commit)
startup_commands = [
f"cd /{self.repo.repo_name}",
"export ROOT=$(pwd -P)",
- "git status",
- "git restore .",
- f"git reset --hard {self.repo.base_commit}",
- "git clean -fdq",
]
+ self.logger.debug("Resetting repository %s to commit %s", self.repo.repo_name, self.repo.base_commit)
+ startup_commands.extend(self.repo.get_reset_commands())
self.communicate(
input=" && ".join(startup_commands),
check="raise",