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]
Fix compatibility with main branch
Kilian Lieret3927f24cd1ea
diff --git a/sweagent/utils/config.py b/sweagent/utils/config.pyindex a25e2bb1..02e4daca 100644--- a/sweagent/utils/config.py+++ b/sweagent/utils/config.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport loggingimport os+from pathlib import Pathfrom typing import Anyimport config as config_file@@ -10,22 +11,22 @@ logger = logging.getLogger("config")class Config:- def __init__(- self,- ):+ def __init__(self, *, keys_cfg_path: Path | None = None):"""This wrapper class is used to load keys from environment variables or keys.cfg file.Whenever both are presents, the environment variable is used."""- # Defer import to avoid circular import- from sweagent import PACKAGE_DIR+ if keys_cfg_path is None:+ # Defer import to avoid circular import+ from sweagent import PACKAGE_DIR+ keys_cfg_path = PACKAGE_DIR.parent / "keys.cfg"self._keys_cfg = None- keys_cfg_path = PACKAGE_DIR / "keys.cfg"if keys_cfg_path.exists():try:- self._keys_cfg = config_file.Config(PACKAGE_DIR / "keys.cfg")+ self._keys_cfg = config_file.Config(str(keys_cfg_path))except Exception as e:- raise RuntimeError("Error loading keys.cfg. Please check the file.") from e+ msg = f"Error loading keys.cfg from {keys_cfg_path}. Please check the file."+ raise RuntimeError(msg) from eelse:logger.error(f"keys.cfg not found in {PACKAGE_DIR}")diff --git a/tests/test_env_utils.py b/tests/test_env_utils.pynew file mode 100644index 00000000..224b3a3b--- /dev/null+++ b/tests/test_env_utils.py@@ -0,0 +1,187 @@+from __future__ import annotations++import hashlib+import os+import subprocess++import pytest++from sweagent.environment.utils import (+ _MARKDOWN_TRAJECTORY_EMOJI_MAPPING,+ InvalidGithubURL,+ format_trajectory_markdown,+ get_associated_commit_urls,+ get_instances,+ is_github_issue_url,+ is_github_repo_url,+ parse_gh_issue_url,+ parse_gh_repo_url,+ remove_triple_backticks,+)++_TOKEN = {"token": os.environ.get("GITHUB_TOKEN", "")}+++def test_format_trajectory_markdown(test_trajectory):+ formatted = format_trajectory_markdown(test_trajectory["trajectory"])+ assert formatted.startswith("<details>")+ assert formatted.endswith("</details>")+ for emoji in _MARKDOWN_TRAJECTORY_EMOJI_MAPPING.values():+ assert emoji in formatted+++def test_remove_triple_backticks():+ assert remove_triple_backticks("```") == ""+++def test_is_github_repo_url():+ assert is_github_repo_url("https://github.com/princeton-nlp/SWE-agent")+ assert is_github_repo_url("https://github.com/princeton-nlp/SWE-agent/anything")+ assert is_github_repo_url("github.com/princeton-nlp/SWE-agent/anything")+ assert not is_github_repo_url("")+ assert not is_github_repo_url("/path/to/file")+++def test_parse_gh_repo_url():+ assert parse_gh_repo_url("https://github.com/princeton-nlp/SWE-agent") == ("princeton-nlp", "SWE-agent")+ assert parse_gh_repo_url("github.com/princeton-nlp/SWE-agent") == ("princeton-nlp", "SWE-agent")+ assert parse_gh_repo_url("github.com/princeton-nlp/SWE-agent/asdfjsdfg") == ("princeton-nlp", "SWE-agent")+ assert parse_gh_repo_url("[email protected]/princeton-nlp/SWE-agent/asdfjsdfg") == ("princeton-nlp", "SWE-agent")+++def test_parse_gh_repo_url_fails():+ with pytest.raises(InvalidGithubURL):+ parse_gh_repo_url("adfkj;lasdfl;kj")+ with pytest.raises(InvalidGithubURL):+ parse_gh_repo_url("github.com/")+ with pytest.raises(InvalidGithubURL):+ parse_gh_repo_url("github.com//a/")+++def test_parse_gh_issue_url():+ url = "https://github.com/princeton-nlp/SWE-agent/issues/43"+ owner, repo, no = parse_gh_issue_url(url)+ assert owner == "princeton-nlp"+ assert repo == "SWE-agent"+ assert no == "43"+++def test_parse_gh_issue_url_fails():+ with pytest.raises(InvalidGithubURL):+ parse_gh_issue_url("https://github.com/a/b")+ with pytest.raises(InvalidGithubURL):+ parse_gh_issue_url("https://github.com/a/b////")+++def test_is_from_github_url():+ assert not is_github_issue_url("")+ assert is_github_issue_url("https://github.com/princeton-nlp/SWE-agent/issues/43")+++def test_get_associated_commit_urls():+ assoc = get_associated_commit_urls(+ org="princeton-nlp", repo="SWE-agent", issue_number="41", token=os.environ.get("GITHUB_TOKEN", "")+ )+ assert len(assoc) > 0+++def test_get_instance_gh_issue():+ instance = get_instances("https://github.com/klieret/swe-agent-test-repo/issues/1", **_TOKEN)[0]+ compare_with = {+ "repo": "klieret/swe-agent-test-repo",+ "instance_id": "klieret__swe-agent-test-repo-i1",+ "repo_type": "github",+ }+ for key in compare_with:+ assert instance[key] == compare_with[key]+ assert "SyntaxError" in instance["problem_statement"]+ assert len(instance["base_commit"]) > 10+ assert instance["version"]+++def clone_repo(tmp_path, repo_url):+ cmd = [+ "git",+ "clone",+ repo_url,+ ]+ subprocess.run(cmd, check=True, cwd=tmp_path)+++def test_get_instance_gh_issue_local_repo(tmp_path):+ clone_repo(tmp_path, "https://github.com/klieret/swe-agent-test-repo/")+ instance = get_instances(+ file_path="https://github.com/klieret/swe-agent-test-repo/issues/1",+ repo_path=str(tmp_path / "swe-agent-test-repo"),+ **_TOKEN,+ )[0]+ compare_with = {+ "repo": str(tmp_path.resolve() / "swe-agent-test-repo"),+ "repo_type": "local",+ "instance_id": "klieret__swe-agent-test-repo-i1",+ }+ for key in compare_with:+ assert instance[key] == compare_with[key]+ assert "SyntaxError" in instance["problem_statement"]+ assert len(instance["base_commit"]) > 10+ assert instance["version"]+++def test_get_instance_local_issue_local_repo(tmp_path):+ clone_repo(tmp_path, "https://github.com/klieret/swe-agent-test-repo/")+ issue_path = tmp_path / "issue.txt"+ issue_path.write_text("asdf")+ instance = get_instances(+ file_path=str(issue_path),+ repo_path=str(tmp_path / "swe-agent-test-repo"),+ )[0]+ compare_with = {+ "repo": str(tmp_path.resolve() / "swe-agent-test-repo"),+ "repo_type": "local",+ "instance_id": hashlib.sha256(b"asdf").hexdigest()[:6],+ "problem_statement": "asdf",+ }+ for key in compare_with:+ assert instance[key] == compare_with[key]+ assert len(instance["base_commit"]) > 10+ assert instance["version"]+++def test_get_instance_gh_issue_gh_repo(tmp_path):+ instance = get_instances(+ file_path="https://github.com/klieret/swe-agent-test-repo/issues/1",+ repo_path="https://github.com/princeton-nlp/SWE-agent",+ **_TOKEN,+ )[0]+ compare_with = {+ "repo": "princeton-nlp/SWE-agent",+ "repo_type": "github",+ "instance_id": "klieret__swe-agent-test-repo-i1",+ }+ for key in compare_with:+ assert instance[key] == compare_with[key]+ assert "SyntaxError" in instance["problem_statement"]+ assert len(instance["base_commit"]) > 10+ assert instance["version"]+++def test_get_instance_text_issue_gh_repo(tmp_path):+ instance = get_instances(+ file_path="text://this is a test", repo_path="https://github.com/princeton-nlp/SWE-agent", **_TOKEN+ )[0]+ compare_with = {+ "repo": "princeton-nlp/SWE-agent",+ "repo_type": "github",+ "problem_statement": "this is a test",+ }+ for key in compare_with:+ assert instance[key] == compare_with[key]+ assert len(instance["base_commit"]) > 10+ assert instance["version"]+++def test_load_instances(test_data_path, caplog):+ test_data_sources = test_data_path / "data_sources"+ examples = list(test_data_sources.iterdir())+ for example in examples:+ get_instances(file_path=str(example), **_TOKEN)diff --git a/tests/test_utils.py b/tests/test_utils.pyindex 224b3a3b..010e6890 100644--- a/tests/test_utils.py+++ b/tests/test_utils.py@@ -1,187 +1,33 @@from __future__ import annotations-import hashlib-import os-import subprocess+from unittest import mockimport pytest-from sweagent.environment.utils import (- _MARKDOWN_TRAJECTORY_EMOJI_MAPPING,- InvalidGithubURL,- format_trajectory_markdown,- get_associated_commit_urls,- get_instances,- is_github_issue_url,- is_github_repo_url,- parse_gh_issue_url,- parse_gh_repo_url,- remove_triple_backticks,-)+from sweagent.utils.config import Config-_TOKEN = {"token": os.environ.get("GITHUB_TOKEN", "")}+def test_config_retrieval_fails():+ config = Config()+ with pytest.raises(KeyError):+ config["DOESNTEXIST"]-def test_format_trajectory_markdown(test_trajectory):- formatted = format_trajectory_markdown(test_trajectory["trajectory"])- assert formatted.startswith("<details>")- assert formatted.endswith("</details>")- for emoji in _MARKDOWN_TRAJECTORY_EMOJI_MAPPING.values():- assert emoji in formatted+def test_config_retrieval_get():+ config = Config()+ assert config.get("asdfasdf", "default") == "default"-def test_remove_triple_backticks():- assert remove_triple_backticks("```") == ""+def test_retrieve_from_file(tmp_path):+ tmp_keys_cfg = tmp_path / "keys.cfg"+ tmp_keys_cfg.write_text("MY_KEY: 'VALUE'\n")+ config = Config(keys_cfg_path=tmp_keys_cfg)+ assert config["MY_KEY"] == "VALUE"-def test_is_github_repo_url():- assert is_github_repo_url("https://github.com/princeton-nlp/SWE-agent")- assert is_github_repo_url("https://github.com/princeton-nlp/SWE-agent/anything")- assert is_github_repo_url("github.com/princeton-nlp/SWE-agent/anything")- assert not is_github_repo_url("")- assert not is_github_repo_url("/path/to/file")--def test_parse_gh_repo_url():- assert parse_gh_repo_url("https://github.com/princeton-nlp/SWE-agent") == ("princeton-nlp", "SWE-agent")- assert parse_gh_repo_url("github.com/princeton-nlp/SWE-agent") == ("princeton-nlp", "SWE-agent")- assert parse_gh_repo_url("github.com/princeton-nlp/SWE-agent/asdfjsdfg") == ("princeton-nlp", "SWE-agent")- assert parse_gh_repo_url("[email protected]/princeton-nlp/SWE-agent/asdfjsdfg") == ("princeton-nlp", "SWE-agent")---def test_parse_gh_repo_url_fails():- with pytest.raises(InvalidGithubURL):- parse_gh_repo_url("adfkj;lasdfl;kj")- with pytest.raises(InvalidGithubURL):- parse_gh_repo_url("github.com/")- with pytest.raises(InvalidGithubURL):- parse_gh_repo_url("github.com//a/")---def test_parse_gh_issue_url():- url = "https://github.com/princeton-nlp/SWE-agent/issues/43"- owner, repo, no = parse_gh_issue_url(url)- assert owner == "princeton-nlp"- assert repo == "SWE-agent"- assert no == "43"---def test_parse_gh_issue_url_fails():- with pytest.raises(InvalidGithubURL):- parse_gh_issue_url("https://github.com/a/b")- with pytest.raises(InvalidGithubURL):- parse_gh_issue_url("https://github.com/a/b////")---def test_is_from_github_url():- assert not is_github_issue_url("")- assert is_github_issue_url("https://github.com/princeton-nlp/SWE-agent/issues/43")---def test_get_associated_commit_urls():- assoc = get_associated_commit_urls(- org="princeton-nlp", repo="SWE-agent", issue_number="41", token=os.environ.get("GITHUB_TOKEN", "")- )- assert len(assoc) > 0---def test_get_instance_gh_issue():- instance = get_instances("https://github.com/klieret/swe-agent-test-repo/issues/1", **_TOKEN)[0]- compare_with = {- "repo": "klieret/swe-agent-test-repo",- "instance_id": "klieret__swe-agent-test-repo-i1",- "repo_type": "github",- }- for key in compare_with:- assert instance[key] == compare_with[key]- assert "SyntaxError" in instance["problem_statement"]- assert len(instance["base_commit"]) > 10- assert instance["version"]---def clone_repo(tmp_path, repo_url):- cmd = [- "git",- "clone",- repo_url,- ]- subprocess.run(cmd, check=True, cwd=tmp_path)---def test_get_instance_gh_issue_local_repo(tmp_path):- clone_repo(tmp_path, "https://github.com/klieret/swe-agent-test-repo/")- instance = get_instances(- file_path="https://github.com/klieret/swe-agent-test-repo/issues/1",- repo_path=str(tmp_path / "swe-agent-test-repo"),- **_TOKEN,- )[0]- compare_with = {- "repo": str(tmp_path.resolve() / "swe-agent-test-repo"),- "repo_type": "local",- "instance_id": "klieret__swe-agent-test-repo-i1",- }- for key in compare_with:- assert instance[key] == compare_with[key]- assert "SyntaxError" in instance["problem_statement"]- assert len(instance["base_commit"]) > 10- assert instance["version"]---def test_get_instance_local_issue_local_repo(tmp_path):- clone_repo(tmp_path, "https://github.com/klieret/swe-agent-test-repo/")- issue_path = tmp_path / "issue.txt"- issue_path.write_text("asdf")- instance = get_instances(- file_path=str(issue_path),- repo_path=str(tmp_path / "swe-agent-test-repo"),- )[0]- compare_with = {- "repo": str(tmp_path.resolve() / "swe-agent-test-repo"),- "repo_type": "local",- "instance_id": hashlib.sha256(b"asdf").hexdigest()[:6],- "problem_statement": "asdf",- }- for key in compare_with:- assert instance[key] == compare_with[key]- assert len(instance["base_commit"]) > 10- assert instance["version"]---def test_get_instance_gh_issue_gh_repo(tmp_path):- instance = get_instances(- file_path="https://github.com/klieret/swe-agent-test-repo/issues/1",- repo_path="https://github.com/princeton-nlp/SWE-agent",- **_TOKEN,- )[0]- compare_with = {- "repo": "princeton-nlp/SWE-agent",- "repo_type": "github",- "instance_id": "klieret__swe-agent-test-repo-i1",- }- for key in compare_with:- assert instance[key] == compare_with[key]- assert "SyntaxError" in instance["problem_statement"]- assert len(instance["base_commit"]) > 10- assert instance["version"]---def test_get_instance_text_issue_gh_repo(tmp_path):- instance = get_instances(- file_path="text://this is a test", repo_path="https://github.com/princeton-nlp/SWE-agent", **_TOKEN- )[0]- compare_with = {- "repo": "princeton-nlp/SWE-agent",- "repo_type": "github",- "problem_statement": "this is a test",- }- for key in compare_with:- assert instance[key] == compare_with[key]- assert len(instance["base_commit"]) > 10- assert instance["version"]---def test_load_instances(test_data_path, caplog):- test_data_sources = test_data_path / "data_sources"- examples = list(test_data_sources.iterdir())- for example in examples:- get_instances(file_path=str(example), **_TOKEN)+def test_retrieve_from_env(tmp_path):+ with mock.patch.dict("os.environ", {"MY_KEY": "VALUE"}):+ tmp_keys_cfg = tmp_path / "keys.cfg"+ tmp_keys_cfg.write_text("MY_KEY: 'other VALUE'\n")+ config = Config(keys_cfg_path=tmp_keys_cfg)+ assert config["MY_KEY"] == "VALUE"