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]

Reset environment between every attempt

Kilian Lieret808560ccd8db
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 90d9b775..ce2a5a1a 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -375,6 +375,9 @@ class Agent:
"Currently, this messes up accounting with init_model_stats."
)
raise ValueError(msg)
+ if self._i_attempt > 0:
+ assert self._env is not None # mypy
+ self._env.reset_for_new_attempt()
self.model.reset_stats(init_model_stats)
# self.model = get_model(self._args.model, self.config._commands + self.config.subroutine_types)
# fixme: This doesn't reset total cost
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 1f315167..845297c0 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -325,33 +325,8 @@ class SWEEnv(gym.Env):
if self._repo_name not in folders:
self._copy_repo()
- # Clean repository of any modifications + Checkout base commit
- for cmd in [
- "echo -n > /root/files_to_edit.txt",
- f"cd {self._repo_name}",
- "export ROOT=$(pwd -P)",
- "git status",
- "git restore .",
- f"git reset --hard {self.base_commit}",
- "git clean -fdxq",
- ]:
- self.communicate_with_handling(
- input=cmd,
- error_msg="Failed to clean repository",
- )
-
- # Reset environment variables
- for cmd in [
- 'export CURRENT_FILE=""',
- "export CURRENT_LINE=0",
- "export SEARCH_RESULTS=()",
- "export SEARCH_FILES=()",
- "export SEARCH_INDEX=0",
- ]:
- self.communicate_with_handling(
- input=cmd,
- error_msg="Failed to reset environment variables",
- )
+ self._reset_repository()
+ self._reset_environment_variables()
# Set up environment
self.communicate_with_handling(
@@ -387,6 +362,45 @@ class SWEEnv(gym.Env):
# Write any metadata to info if necessary
return None, info
+ def _reset_repository(self) -> None:
+ """Clean repository of any modifications + Checkout base commit"""
+ cmds = [
+ "echo -n > /root/files_to_edit.txt",
+ f"cd /{self._repo_name}",
+ "export ROOT=$(pwd -P)",
+ "git status",
+ "git restore .",
+ f"git reset --hard {self.base_commit}",
+ "git clean -fdxq",
+ ]
+ self.communicate_with_handling(
+ input=" && ".join(cmds),
+ error_msg="Failed to clean repository",
+ )
+
+ def _reset_environment_variables(self) -> None:
+ """Reset environment variables (`CURRENT_FILE`) etc. within container"""
+ cmd = [
+ 'export CURRENT_FILE=""',
+ "export CURRENT_LINE=0",
+ "export SEARCH_RESULTS=()",
+ "export SEARCH_FILES=()",
+ "export SEARCH_INDEX=0",
+ ]
+ self.communicate_with_handling(
+ input=" && ".join(cmd),
+ error_msg="Failed to reset environment variables",
+ )
+
+ def reset_for_new_attempt(
+ self,
+ ) -> None:
+ """Compared to `reset`, which prepares the container for a new instance,
+ this prepares the container for taking another shot at the same instance.
+ """
+ self._reset_repository()
+ self._reset_environment_variables()
+
def _apply_test_patch(self):
"""
Apply test patch for oracle setting