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]
Allow to supply installation commands when running on gh issues (#153)
Kilian Lieretbfd6532ab2ea
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 08ef31fc..0fe193ea 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -1,3 +1,4 @@+from pathlib import Pathimport randomimport configimport datetime@@ -16,6 +17,7 @@ from dataclasses import dataclassfrom git import Repofrom rich.logging import RichHandlerfrom simple_parsing.helpers import FrozenSerializable+import yamlfrom sweagent.environment.utils import (copy_file_to_container,format_trajectory_markdown,@@ -58,6 +60,11 @@ class EnvironmentArguments(FrozenSerializable):timeout: int = 35verbose: bool = Falseno_mirror: bool = False+ # Custom environment setup. Currently only used when data_path is a GitHub URL.+ # This needs to be either a string pointing to a yaml file (with yaml, yml file extension)+ # or a shell script (with sh extension).+ # See https://github.com/princeton-nlp/SWE-agent/pull/153 for more information+ environment_setup: Optional[str] = Noneclass SWEEnv(gym.Env):@@ -205,13 +212,7 @@ class SWEEnv(gym.Env):# Call install environment helper function if specifiedif self.install_environment:- if self.is_from_github_url:- logger.warning((- "install_environment is set to True, but the data path is a GitHub URL. "- "Skipping conda environment installation."- ))- else:- self.install_env()+ self.install_env()# Install mypy for linting purposesself.communicate_with_handling(f"pip install flake8",@@ -524,19 +525,71 @@ class SWEEnv(gym.Env):return Nonereturn match.group(1)+ def run_shell_script(self, script_path: Path, *, location: str) -> None:+ """Run custom script supplied by user at `script_path`++ Args:+ location: location of script file 'host' or 'container'+ """+ if location == "host":+ return self._run_shell_script_host(script_path)+ elif location == "container":+ raise NotImplementedError+ raise ValueError(f"Invalid 'location': {location}")++ def _run_shell_script_host(self, script_path: Path) -> None:+ """Run shell script file (located on host) in container"""+ if not script_path.is_file():+ raise FileNotFoundError(f"Script not found at {script_path}")+ shell_commands = Path(script_path).read_text().splitlines()+ for i, cmd in enumerate(shell_commands):+ self.communicate_with_handling(+ cmd,+ error_msg=f"Failed to execute line {i}.",+ timeout_duration=LONG_TIMEOUT,+ )+def install_env(self) -> None:"""Creates conda environment and installs third party dependencies to allow code execution"""+ if self.is_from_github_url and self.args.environment_setup is None:+ logger.warning((+ "install_environment is set to True, but the data path is a GitHub URL "+ "without an environment config file (environment_config key/flag). "+ "Skipping conda environment installation."+ ))+ returnrepo_name = self.record["repo"].replace("/", "__")+ if self.args.environment_setup is not None:+ assert isinstance(self.args.environment_setup, (str, os.PathLike))+ if Path(self.args.environment_setup).suffix in [".yml", ".yaml"]:+ try:+ install_configs = yaml.safe_load(Path(self.args.environment_setup).read_text())+ except Exception as e:+ msg = "Environment config file needs to be a yaml file"+ raise ValueError(msg) from e+ elif Path(self.args.environment_setup).suffix == ".sh":+ self.run_shell_script(Path(self.args.environment_setup), location="host")+ return+ else:+ raise ValueError("Environment config file needs to be a yaml file or shell script")+ else:+ try:+ install_configs = MAP_VERSION_TO_INSTALL[self.record["repo"]][+ str(self.record["version"])+ ]+ except KeyError as e:+ msg = (+ "Tried to look up install configs in swe-bench, but failed. "+ "You can set a custom environment config with the environment_config key/flag."+ )+ raise ValueError(msg) from e# Create environment if does not exist yetenv_name = f"{repo_name}__{self.record['version']}"env_check = self.communicate(f"conda env list | grep {env_name}", timeout_duration=LONG_TIMEOUT)- install_configs = MAP_VERSION_TO_INSTALL[self.record["repo"]][- str(self.record["version"])- ]if env_check.strip() == "":self.logger.info(f"{env_name} conda env not found, creating...")packages = (diff --git a/tests/test_env.py b/tests/test_env.pyindex 9a54289c..207f2c99 100644--- a/tests/test_env.py+++ b/tests/test_env.py@@ -5,6 +5,22 @@ import yamlfrom sweagent.environment.swe_env import EnvironmentArguments, SWEEnv+# TEST_INSTANCE = {"repo": "test/test",+# "instance_id": "test__test-4764",+# "base_commit": "",+# "patch": "",+# "problem_statement": "",+# "hints_text": "",+# "created_at": "2023-04-16T14:24:42Z",+# "version": "1.4",+# "FAIL_TO_PASS": [+# ],+# "PASS_TO_PASS": [+# ],+# "environment_setup_commit": "test"+# }++@pytest.fixturedef test_env_args():test_env_args = EnvironmentArguments(@@ -22,6 +38,28 @@ def test_init_swe_env(test_env_args):@pytest.mark.slow+def test_execute_setup_script(tmp_path, test_env_args):+ test_script = "echo 'hello world'"+ script_path = Path(tmp_path / "test_script.sh")+ script_path.write_text(test_script)+ test_env_args = dataclasses.replace(test_env_args, environment_setup=script_path)+ env = SWEEnv(test_env_args)+ env.reset()+++def test_execute_environment(tmp_path, test_env_args):+ test_env = {+ "python": "3.6",+ "packages": "pytest",+ "pip_packages": "tox",+ "install": "echo 'installing'",+ }+ env_config_path = Path(tmp_path / "env_config.yml")+ env_config_path.write_text(yaml.dump(test_env))+ test_env_args = dataclasses.replace(test_env_args, environment_setup=env_config_path)+ env = SWEEnv(test_env_args)+ env.reset()def test_open_pr(test_env_args):env = SWEEnv(test_env_args)env.reset()@@ -32,4 +70,4 @@ def test_open_pr(test_env_args):def test_interrupt_close(test_env_args):env = SWEEnv(test_env_args)env.interrupt()- env.close()\ No newline at end of file+ env.close()