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]
Increase coverage of swe-env tests (#154)
Kilian Lieret1efbeb9bfe77
diff --git a/run.py b/run.pyindex 16082e77..9db90c8d 100644--- a/run.py+++ b/run.py@@ -143,7 +143,7 @@ def main(args: ScriptArguments):save_predictions(traj_dir, instance_id, info)save_patch(traj_dir, instance_id, info)if args.actions.open_pr and should_open_pr(args, info, token=env.token):- env.open_pr(args.actions, info, trajectory)+ env.open_pr(trajectory=trajectory, push_gh_repo_url=args.actions.push_gh_repo_url)except KeyboardInterrupt:logger.info("Exiting InterCode environment...")diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 80ea727f..08ef31fc 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -679,8 +679,14 @@ class SWEEnv(gym.Env):raise RuntimeError("Failed to interrupt container")- def open_pr(self, action_config, info, trajectory):- """Create PR to repository"""+ def open_pr(self, *, trajectory, push_gh_repo_url: Optional[str] = None, _dry_run: bool=False):+ """Create PR to repository++ Args:+ trajectory: Trajectory of actions taken by the agent+ push_gh_repo_url: URL of the repository to push the PR branch to+ _dry_run: Whether to actually push anything or just simulate it+ """logger.info("Opening PR")# todo: have better way of handling this# Adding random string suffix to avoid name conflicts if we had a previously failed run@@ -689,7 +695,7 @@ class SWEEnv(gym.Env):branch_name = f"swe-agent-fix-#{issue.number}-" + str(random.random())[2:10]self.communicate_with_handling(- input=f"rm model.patch",+ input=f"rm -f model.patch",error_msg="Failed to remove model patch",timeout_duration=10,)@@ -703,19 +709,20 @@ class SWEEnv(gym.Env):error_msg="Failed to add commits",timeout_duration=10,)+ dry_run_flag = "--allow-empty" if _dry_run else ""self.communicate_with_handling(- input=f"git commit -m 'Fix: {issue.title}' -m 'Closes #{issue.number}' ",+ input=f"git commit -m 'Fix: {issue.title}' -m 'Closes #{issue.number}' {dry_run_flag}",error_msg="Failed to commit changes",timeout_duration=10,)# If users want to push to a fork, we add a new remote and push to that# otherwise, we push to 'origin' which has already been set upremote = "origin"- if not action_config.push_gh_repo_url:+ if not push_gh_repo_url:owner, repo, _ = parse_gh_issue_url(issue_url)else:- owner, repo = parse_gh_repo_url(action_config.push_gh_repo_url)- if action_config.push_gh_repo_url:+ owner, repo = parse_gh_repo_url(push_gh_repo_url)+ if push_gh_repo_url:fork_url = f"https://{self.token}@github.com/{owner}/{repo}.git"self.communicate_with_handling(input=f"git remote add fork {fork_url}",@@ -723,8 +730,9 @@ class SWEEnv(gym.Env):timeout_duration=10,)remote = "fork"+ dry_run_prefix = "echo " if _dry_run else ""self.communicate_with_handling(- input=f"git push {remote} {branch_name}",+ input=f"{dry_run_prefix} git push {remote} {branch_name}",error_msg=("Failed to push branch to remote. Please check your token and permissions. ""You might want to push to a fork with the push_gh_repo_url option."@@ -739,17 +747,18 @@ class SWEEnv(gym.Env):)body += "\n\n" + format_trajectory_markdown(trajectory)api = GhApi(token=self.token)- pr_info = api.pulls.create(- owner=owner,- repo=repo,- title=f"SWE-agent[bot] PR to fix: {issue.title}",- head=branch_name,- base="main",- body=body,- draft=True,- )- logger.info(- f"🎉 PR created as a draft at {pr_info.html_url}. Please review it carefully, push "- "any required changes onto the branch and then click "- "'Ready for Review' to bring it to the attention of the maintainers."- )\ No newline at end of file+ if not _dry_run:+ pr_info = api.pulls.create(+ owner=owner,+ repo=repo,+ title=f"SWE-agent[bot] PR to fix: {issue.title}",+ head=branch_name,+ base="main",+ body=body,+ draft=True,+ )+ logger.info(+ f"🎉 PR created as a draft at {pr_info.html_url}. Please review it carefully, push "+ "any required changes onto the branch and then click "+ "'Ready for Review' to bring it to the attention of the maintainers."+ )\ No newline at end of filediff --git a/tests/test_env.py b/tests/test_env.pynew file mode 100644index 00000000..9a54289c--- /dev/null+++ b/tests/test_env.py@@ -0,0 +1,35 @@+import dataclasses+from pathlib import Path+import pytest+import yaml+from sweagent.environment.swe_env import EnvironmentArguments, SWEEnv+++def test_env_args():+ test_env_args = EnvironmentArguments(+ data_path="https://github.com/klieret/swe-agent-test-repo/issues/1",+ image_name="sweagent/swe-agent:latest",+ )+ return test_env_args+++def test_init_swe_env(test_env_args):+ env = SWEEnv(test_env_args)+ env.reset()++++def test_open_pr(test_env_args):+ env = SWEEnv(test_env_args)+ env.reset()+ env.open_pr(_dry_run=True, trajectory=[])+++def test_interrupt_close(test_env_args):+ env = SWEEnv(test_env_args)+ env.interrupt()+ env.close()\ No newline at end of file