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]

Merge branch 'main' into reviewer

Kilian Lieret27b574eaaad1
diff --cc sweagent/agent/agents.py
index f33559c4,6d3852e3..0aeb7519
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@@ -813,10 -665,8 +813,9 @@@ class Agent
"exit_api",
f"exit due to retry error: {e}",
)
- return self.check_format_and_requery(output)
def init_environment_vars(self, env: SWEEnv):
+ assert self.config is not None
self.set_environment_vars(env, self.config.env_variables)
def set_environment_vars(self, env: SWEEnv, env_variables: dict[str, Any]) -> None:
diff --cc sweagent/environment/swe_env.py
index 5ac3a1d3,a5ceddca..e3eb650b
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@@ -423,20 -410,9 +426,20 @@@ class SWEEnv(gym.Env)
)
os.remove(path_to_patch)
- def step(self, action: str) -> tuple[str | None, int, bool, dict]:
+ def _get_edited_files_with_context(self, patch: str) -> dict[str, str]:
+ """Get the edited files with context from the patch"""
+ pf = PatchFormatter(patch, read_method=self.read_file) if patch else None
+ out = {}
+ for context_length in [30, 50, 70]:
+ value = "Empty. No edited files found."
+ if pf is not None:
+ value = pf.get_files_str(original=False, context_length=context_length)
+ out[f"edited_files{context_length}"] = value
+ return out
+
+ def step(self, action: str) -> tuple[str | None, int, bool, AgentInfo]:
"""
- Runs given action in environment and returns corresponding output
+ Runs an action proposed by the agent in the environment and returns the corresponding output.
Args:
action: command to run in bash shell
diff --cc tests/conftest.py
index 8a9f086a,8d1fcaf9..e9f43aa5
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@@ -56,46 -49,3 +56,46 @@@ def test_trajectory_path(test_trajector
@pytest.fixture()
def test_trajectory(test_trajectory_path):
return json.loads(test_trajectory_path.read_text())
+
+
[email protected](scope="module")
+def test_env_args(
+ tmpdir_factory,
+) -> Generator[EnvironmentArguments]:
+ """This will use a persistent container"""
- local_repo_path = tmpdir_factory.getbasetemp() / "swe-agent-test-repo"
- clone_cmd = ["git", "clone", "https://github.com/klieret/swe-agent-test-repo", local_repo_path]
++ local_repo_path = tmpdir_factory.getbasetemp() / "test-repo"
++ clone_cmd = ["git", "clone", "https://github.com/swe-agent/test-repo", local_repo_path]
+ subprocess.run(clone_cmd, check=True)
+ data_path = local_repo_path / "problem_statements" / "1.md"
+ test_env_args = EnvironmentArguments(
+ data_path=str(data_path),
+ repo_path=str(local_repo_path),
+ image_name="sweagent/swe-agent:latest",
+ container_name="test-container-this-is-a-random-string",
+ verbose=True,
+ )
+ yield test_env_args
+ # Cleanup (after session ends)
+ client = docker.from_env()
+ # fixme (?): What happens if user changed container_name?
+ try:
+ assert test_env_args.container_name is not None # mypy
+ container = client.containers.get(test_env_args.container_name)
+ container.remove(force=True)
+ except docker.errors.NotFound:
+ # Can happen if this fixture never runs because we only do a partial
+ # test run
+ pass
+
+
+@contextmanager
+def swe_env_context(env_args):
+ """Context manager to make sure we close the shell on the container
+ so that we can reuse it.
+ """
+
+ env = SWEEnv(env_args)
+ try:
+ yield env
+ finally:
+ env.close()