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]
SWEEnv: Separate __init__ and from_config
Kilian Lieret421410667ca6
diff --git a/run.py b/run.pyindex 056255dd..50b3b0be 100644--- a/run.py+++ b/run.py@@ -15,6 +15,7 @@ import tracebackfrom argparse import ArgumentParserfrom pprint import pprint+from sweagent.environment.config.deployment import DockerDeploymentConfigfrom sweagent.main.hooks.abstract import MainHookfrom sweagent.utils.log import add_file_handler, get_logger@@ -36,7 +37,7 @@ from unidiff import PatchSetfrom sweagent.agent.agents import Agent, AgentConfigfrom sweagent.agent.models import ModelArguments-from sweagent.environment.swe_env import DockerDeploymentConfig, EnvironmentConfig, SWEEnv+from sweagent.environment.swe_env import EnvironmentConfig, SWEEnv__doc__: str = """ Run inference. Usage examples:diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex dbaf80bc..6d7a1c94 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -305,7 +305,7 @@ class Agent:The path is reset for every new instance."""if self.traj_dir and self._env is not None:- return self.traj_dir / (self._env.args.instance.id + ".traj")+ return self.traj_dir / (self._env.instance.id + ".traj")return Nonedef add_hook(self, hook: AgentHook) -> None:diff --git a/sweagent/api/hooks.py b/sweagent/api/hooks.pyindex e6fbcd09..70507109 100644--- a/sweagent/api/hooks.py+++ b/sweagent/api/hooks.py@@ -8,7 +8,7 @@ from flask_socketio import SocketIOfrom sweagent import PACKAGE_DIRfrom sweagent.agent.agents import AgentHookfrom sweagent.api.utils import strip_ansi_sequences-from sweagent.environment.swe_env import EnvHook+from sweagent.environment.hooks.abstract import EnvHook# baaaaaaadsys.path.append(str(PACKAGE_DIR.parent))diff --git a/sweagent/api/server.py b/sweagent/api/server.pyindex dc3ae7b5..ac73f55b 100644--- a/sweagent/api/server.py+++ b/sweagent/api/server.py@@ -2,6 +2,8 @@ from __future__ import annotationsfrom omegaconf import OmegaConf+from sweagent.environment.config.deployment import DeploymentConfig+try:import flask # noqaexcept ImportError as e:@@ -32,7 +34,7 @@ from sweagent.agent.agents import AgentConfigfrom sweagent.agent.models import ModelArgumentsfrom sweagent.api.hooks import AgentUpdateHook, EnvUpdateHook, MainUpdateHook, WebUpdatefrom sweagent.api.utils import AttrDict, ThreadWithExc-from sweagent.environment.swe_env import DeploymentConfig, EnvironmentConfig+from sweagent.environment.swe_env import EnvironmentConfig# baaaaaaadsys.path.append(str(PACKAGE_DIR.parent))diff --git a/sweagent/environment/config/__init__.py b/sweagent/environment/config/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/sweagent/environment/config/deployment.py b/sweagent/environment/config/deployment.pynew file mode 100644index 00000000..2851e8de--- /dev/null+++ b/sweagent/environment/config/deployment.py@@ -0,0 +1,22 @@+from typing import Literal++from pydantic import BaseModel+++class DockerDeploymentConfig(BaseModel):+ """Configuration for the deployment of the environment"""++ image: str = "sweagent/swe-agent:latest"++ type: Literal["docker"] = "docker"+ """Discriminator for serialization. Do not change."""+++class ModalDeploymentConfig(BaseModel):+ image: str = "sweagent/swe-agent:latest"++ type: Literal["modal"] = "modal"+ """Discriminator for serialization. Do not change."""+++DeploymentConfig = DockerDeploymentConfig | ModalDeploymentConfigdiff --git a/sweagent/environment/config/instance.py b/sweagent/environment/config/instance.pynew file mode 100644index 00000000..6111d50c--- /dev/null+++ b/sweagent/environment/config/instance.py@@ -0,0 +1,65 @@+import hashlib+import uuid+from pathlib import Path+from typing import Literal++from pydantic import BaseModel, Field++from sweagent.utils._github import get_problem_statement_from_github_issue, parse_gh_issue_url+from sweagent.utils.config import keys_config+++class EmptyInstanceConfig(BaseModel):+ id: str = Field(default_factory=lambda: str(uuid.uuid4()))+ type: Literal["empty"] = "empty"+ """Discriminator for serialization. Do not change."""++ def get_problem_statement(self) -> str:+ return ""+++class TextInstanceConfig(BaseModel):+ problem_statement: str = ""++ type: Literal["text"] = "text"+ """Discriminator for serialization. Do not change."""++ @property+ def id(self) -> str:+ return hashlib.sha256(self.problem_statement.encode()).hexdigest()[:6]++ def get_problem_statement(self) -> str:+ return self.problem_statement+++class TextFileInstanceConfig(BaseModel):+ path: str = ""++ type: Literal["text_file"] = "text_file"+ """Discriminator for serialization. Do not change."""++ @property+ def id(self) -> str:+ return hashlib.sha256(self.get_problem_statement().encode()).hexdigest()[:6]++ def get_problem_statement(self) -> str:+ return Path(self.path).read_text()+++class GithubInstanceConfig(BaseModel):+ issue_url: str = ""++ type: Literal["github"] = "github"+ """Discriminator for serialization. Do not change."""++ @property+ def id(self) -> str:+ org, repo, issue = self.issue_url.split("/")[-3:]+ return f"{org}__{repo}-i{issue}"++ def get_problem_statement(self) -> str:+ owner, repo, issue_number = parse_gh_issue_url(self.issue_url)+ return get_problem_statement_from_github_issue(owner, repo, issue_number, token=keys_config.get("GITHUB_TOKEN"))+++InstanceConfig = TextInstanceConfig | GithubInstanceConfig | EmptyInstanceConfigdiff --git a/sweagent/environment/config/repo.py b/sweagent/environment/config/repo.pynew file mode 100644index 00000000..d67e4877--- /dev/null+++ b/sweagent/environment/config/repo.py@@ -0,0 +1,95 @@+import asyncio+import os+from pathlib import Path+from typing import Literal, Self++import pydantic+from git import InvalidGitRepositoryError, Repo+from pydantic import BaseModel+from swerex.deployment.abstract import AbstractDeployment+from swerex.runtime.abstract import Command, UploadRequest++from sweagent.utils.config import keys_config+++class LocalRepoConfig(BaseModel):+ path: str = ""+ base_commit: str = "HEAD"++ type: Literal["local"] = "local"+ """Discriminator for serialization. Do not change."""++ @property+ def repo_name(self) -> str:+ return Path(self.path).resolve().name.replace(" ", "-").replace("'", "")++ @pydantic.model_validator(mode="after")+ def check_valid_repo(self) -> Self:+ try:+ repo = Repo(self.path, search_parent_directories=True)+ except InvalidGitRepositoryError as e:+ msg = f"Could not find git repository at {self.path=}."+ raise ValueError(msg) from e+ if repo.is_dirty() and "PYTEST_CURRENT_TEST" not in os.environ:+ msg = f"Local git repository {self.path} is dirty. Please commit or stash changes."+ raise ValueError(msg)+ return self++ def copy(self, deployment: AbstractDeployment):+ asyncio.run(deployment.runtime.upload(UploadRequest(source_path=self.path, target_path=f"/{self.repo_name}")))+ r = asyncio.run(deployment.runtime.execute(Command(command=f"chown -R root:root {self.repo_name}", shell=True)))+ if r.exit_code != 0:+ msg = f"Failed to change permissions on copied repository (exit code: {r.exit_code}, stdout: {r.stdout}, stderr: {r.stderr})"+ raise RuntimeError(msg)+++class GithubRepoConfig(BaseModel):+ url: str = ""++ base_commit: str = "HEAD"++ type: Literal["github"] = "github"+ """Discriminator for serialization. Do not change."""++ @property+ def repo_name(self) -> str:+ # fixme: Need to replace ":" etc.+ org, repo = self.url.split("/")[-2:]+ return f"{org}__{repo}"++ def _get_url_with_token(self, token: str) -> str:+ """Prepend github token to URL"""+ if "@" in self.url:+ return self.url+ _, _, url_no_protocol = self.url.partition("://")+ return f"https://{token}@{url_no_protocol}"++ def copy(self, deployment: AbstractDeployment):+ base_commit = self.base_commit+ github_token = keys_config.get("GITHUB_TOKEN", "")+ url = self._get_url_with_token(github_token)+ r = asyncio.run(+ deployment.runtime.execute(+ Command(+ command=" && ".join(+ (+ f"mkdir {self.repo_name}",+ f"cd {self.repo_name}",+ "git init",+ f"git remote add origin {url}",+ f"git fetch --depth 1 origin {base_commit}",+ "git checkout FETCH_HEAD",+ "cd ..",+ )+ ),+ timeout=float(keys_config.get("SWE_AGENT_ENV_LONG_TIMEOUT", 500)),+ shell=True,+ )+ ),+ )+ if r.exit_code != 0:+ msg = f"Failed to clone repository (exit code: {r.exit_code}, stdout: {r.stdout}, stderr: {r.stderr})"+ raise RuntimeError(msg)+++RepoConfig = LocalRepoConfig | GithubRepoConfigdiff --git a/sweagent/environment/hooks/__init__.py b/sweagent/environment/hooks/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/sweagent/environment/hooks/abstract.py b/sweagent/environment/hooks/abstract.pynew file mode 100644index 00000000..800d8bb2--- /dev/null+++ b/sweagent/environment/hooks/abstract.py@@ -0,0 +1,22 @@+from sweagent.environment.config.repo import RepoConfig+++class EnvHook:+ """Hook to be used in `SWEEnv`.++ Subclass this class, add functionality and add it with `SWEEEnv.add_hook(hook)`.+ This allows to inject custom functionality at different stages of the environment+ lifecycle, in particular to connect SWE-agent to a new interface (like a GUI).+ """++ def on_init(self) -> None:+ """Gets called when the hook is added"""++ def on_copy_repo_started(self, repo: RepoConfig) -> None:+ """Gets called when the repository is being cloned to the container"""++ def on_install_env_started(self) -> None:+ """Called when we start installing the environment"""++ def on_close(self):+ """Called when the environment is closed"""diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 1baea65c..d25c89a7 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -1,34 +1,26 @@from __future__ import annotationsimport asyncio-import hashlibimport logging-import osimport reimport shlex-import time-import uuid-from pathlib import Path, PurePath-from typing import Any, Literal, Self+from pathlib import PurePath+from typing import Any, Selfimport gymnasium as gym-import pydantic-from git import InvalidGitRepositoryError, Repofrom pydantic import BaseModel, Fieldfrom swerex.deployment import get_deploymentfrom swerex.deployment.abstract import AbstractDeployment-from swerex.runtime.abstract import BashAction, Command, CreateBashSessionRequest, UploadRequest, WriteFileRequest+from swerex.runtime.abstract import BashAction, CreateBashSessionRequest, WriteFileRequest-# from sweagent.agent.interactive_commands import (-# INTERACTIVE_SESSIONS_CONFIG,-# InteractiveSession,-# InteractiveSessionConfig,-# )+from sweagent.environment.config.deployment import DeploymentConfig, DockerDeploymentConfig+from sweagent.environment.config.instance import EmptyInstanceConfig, InstanceConfig+from sweagent.environment.config.repo import RepoConfig+from sweagent.environment.hooks.abstract import EnvHookfrom sweagent.environment.utils import (NoOutputTimeoutError,)from sweagent.types import AgentInfo-from sweagent.utils._github import get_problem_statement_from_github_issue, parse_gh_issue_urlfrom sweagent.utils.config import keys_configfrom sweagent.utils.log import get_loggerfrom sweagent.utils.patch_formatter import PatchFormatter@@ -38,164 +30,6 @@ AGENT_ACTION_TIMEOUT = float(keys_config.get("SWE_AGENT_ACTION_TIMEOUT", 25))AGENT_ACTION_NO_OUTPUT_TIMEOUT = float(keys_config.get("SWE_AGENT_ACTION_NO_OUTPUT_TIMEOUT", AGENT_ACTION_TIMEOUT))-class DockerDeploymentConfig(BaseModel):- """Configuration for the deployment of the environment"""-- image: str = "sweagent/swe-agent:latest"-- type: Literal["docker"] = "docker"- """Discriminator for serialization. Do not change."""---class ModalDeploymentConfig(BaseModel):- image: str = "sweagent/swe-agent:latest"-- type: Literal["modal"] = "modal"- """Discriminator for serialization. Do not change."""---DeploymentConfig = DockerDeploymentConfig | ModalDeploymentConfig---class EmptyInstanceConfig(BaseModel):- id: str = Field(default_factory=lambda: str(uuid.uuid4()))- type: Literal["empty"] = "empty"- """Discriminator for serialization. Do not change."""-- def get_problem_statement(self) -> str:- return ""---class TextInstanceConfig(BaseModel):- problem_statement: str = ""-- type: Literal["text"] = "text"- """Discriminator for serialization. Do not change."""-- @property- def id(self) -> str:- return hashlib.sha256(self.problem_statement.encode()).hexdigest()[:6]-- def get_problem_statement(self) -> str:- return self.problem_statement---class TextFileInstanceConfig(BaseModel):- path: str = ""-- type: Literal["text_file"] = "text_file"- """Discriminator for serialization. Do not change."""-- @property- def id(self) -> str:- return hashlib.sha256(self.get_problem_statement().encode()).hexdigest()[:6]-- def get_problem_statement(self) -> str:- return Path(self.path).read_text()---class GithubInstanceConfig(BaseModel):- issue_url: str = ""-- type: Literal["github"] = "github"- """Discriminator for serialization. Do not change."""-- @property- def id(self) -> str:- org, repo, issue = self.issue_url.split("/")[-3:]- return f"{org}__{repo}-i{issue}"-- def get_problem_statement(self) -> str:- owner, repo, issue_number = parse_gh_issue_url(self.issue_url)- return get_problem_statement_from_github_issue(owner, repo, issue_number, token=keys_config.get("GITHUB_TOKEN"))---InstanceConfig = TextInstanceConfig | GithubInstanceConfig | EmptyInstanceConfig---class LocalRepoConfig(BaseModel):- path: str = ""- base_commit: str = "HEAD"-- type: Literal["local"] = "local"- """Discriminator for serialization. Do not change."""-- @property- def repo_name(self) -> str:- return Path(self.path).resolve().name.replace(" ", "-").replace("'", "")-- @pydantic.model_validator(mode="after")- def check_valid_repo(self) -> Self:- try:- repo = Repo(self.path, search_parent_directories=True)- except InvalidGitRepositoryError as e:- msg = f"Could not find git repository at {self.path=}."- raise ValueError(msg) from e- if repo.is_dirty() and "PYTEST_CURRENT_TEST" not in os.environ:- msg = f"Local git repository {self.path} is dirty. Please commit or stash changes."- raise ValueError(msg)- return self-- def copy(self, deployment: AbstractDeployment):- asyncio.run(deployment.runtime.upload(UploadRequest(source_path=self.path, target_path=f"/{self.repo_name}")))- r = asyncio.run(deployment.runtime.execute(Command(command=f"chown -R root:root {self.repo_name}", shell=True)))- if r.exit_code != 0:- msg = f"Failed to change permissions on copied repository (exit code: {r.exit_code}, stdout: {r.stdout}, stderr: {r.stderr})"- raise RuntimeError(msg)---class GithubRepoConfig(BaseModel):- url: str = ""-- base_commit: str = "HEAD"-- type: Literal["github"] = "github"- """Discriminator for serialization. Do not change."""-- @property- def repo_name(self) -> str:- # fixme: Need to replace ":" etc.- org, repo = self.url.split("/")[-2:]- return f"{org}__{repo}"-- def _get_url_with_token(self, token: str) -> str:- """Prepend github token to URL"""- if "@" in self.url:- return self.url- _, _, url_no_protocol = self.url.partition("://")- return f"https://{token}@{url_no_protocol}"-- def copy(self, deployment: AbstractDeployment):- base_commit = self.base_commit- github_token = keys_config.get("GITHUB_TOKEN", "")- url = self._get_url_with_token(github_token)- r = asyncio.run(- deployment.runtime.execute(- Command(- command=" && ".join(- (- f"mkdir {self.repo_name}",- f"cd {self.repo_name}",- "git init",- f"git remote add origin {url}",- f"git fetch --depth 1 origin {base_commit}",- "git checkout FETCH_HEAD",- "cd ..",- )- ),- timeout=LONG_TIMEOUT,- shell=True,- )- ),- )- if r.exit_code != 0:- msg = f"Failed to clone repository (exit code: {r.exit_code}, stdout: {r.stdout}, stderr: {r.stderr})"- raise RuntimeError(msg)---RepoConfig = LocalRepoConfig | GithubRepoConfig--class EnvironmentConfig(BaseModel):"""Configure data sources and setup instructions for the environment in which we solve the tasks."""@@ -210,63 +44,56 @@ class EnvironmentConfig(BaseModel):self.deployment = DockerDeploymentConfig()-class EnvHook:- """Hook to be used in `SWEEnv`.-- Subclass this class, add functionality and add it with `SWEEEnv.add_hook(hook)`.- This allows to inject custom functionality at different stages of the environment- lifecycle, in particular to connect SWE-agent to a new interface (like a GUI).- """-- def on_init(self) -> None:- """Gets called when the hook is added"""-- def on_copy_repo_started(self, repo: RepoConfig) -> None:- """Gets called when the repository is being cloned to the container"""-- def on_install_env_started(self) -> None:- """Called when we start installing the environment"""-- def on_close(self):- """Called when the environment is closed"""--# todo: Do we really need to inherit from gym.Env?class SWEEnv(gym.Env):"""Gym environment for SWE-bench. This class should handle all communication with the docker container."""name = "swe_main"- def __init__(self, args: EnvironmentConfig):+ def __init__(+ self,+ *,+ deployment: AbstractDeployment,+ repo: RepoConfig | None,+ instance: InstanceConfig,+ startup_commands: list[str],+ ):super().__init__()- t0 = time.perf_counter()- self.args = args- self.logger = get_logger("SWEEnv")+ self.deployment = deployment+ self.repo = repo+ self.instance = instance+ self._startup_commands = startup_commands+ self.logger = get_logger("🌱 SWEEnv")# todo: get rid of thisself.returncode: None | int = None- # Establish connection with execution container- # self.docker_compose: Path | None = None- # self.challenge: dict[str, Any] | None = None+ # todo: There should be a start function, so I can call `add_hook` before anything happensself._init_container()self._init_scripts()- # self.interactive_session: InteractiveSession | None = None-- self.idx = 0self.clean_multi_line_functions = lambda x: x- self.hooks: list[EnvHook] = []+ self._hooks: list[EnvHook] = []++ @classmethod+ def from_config(cls, config: EnvironmentConfig) -> Self:+ deployment_kwargs = {k: v for k, v in config.deployment.model_dump().items() if k != "type"}+ deployment = get_deployment(config.deployment.type, **deployment_kwargs) # type: ignore+ return cls(+ deployment=deployment, repo=config.repo, instance=config.instance, startup_commands=config.startup_commands+ )- self.logger.debug("Environment initialization took %.2f seconds", time.perf_counter() - t0)+ @property+ def hooks(self) -> list[EnvHook]:+ return self._hooks- def add_hook(self, hook: EnvHook):+ def add_hook(self, hook: EnvHook) -> None:"""Add `EnvHook` to the environment.This allows to inject custom functionality at different stages of the environmentlifecycle, in particular to connect SWE-agent to a new interface (like a GUI)."""hook.on_init()- self.hooks.append(hook)+ self._hooks.append(hook)# @property# def _repo_name(self) -> str:@@ -276,19 +103,17 @@ class SWEEnv(gym.Env):# todo: Ideally move that to the RepoClassesdef _copy_repo(self) -> None:"""Clone/copy repository/codebase in container"""- if self.args.repo is None:+ if self.repo is None:returnfolders = self.communicate(input="ls").split("\n")- if self.args.repo.repo_name in folders:+ if self.repo.repo_name in folders:return- # assert self.container_obj is not Nonefor hook in self.hooks:- if self.args.repo:- hook.on_copy_repo_started(self.args.repo)+ hook.on_copy_repo_started(self.repo)- self.args.repo.copy(self.deployment)+ self.repo.copy(self.deployment)def reset(self) -> tuple[str | None, dict]:"""@@ -347,17 +172,17 @@ class SWEEnv(gym.Env):def _reset_repository(self) -> None:"""Clean repository of any modifications + Checkout base commit"""- if self.args.repo is not None:+ if self.repo is not None:startup_commands = ["echo -n > /root/files_to_edit.txt",- f"cd /{self.args.repo.repo_name}",+ f"cd /{self.repo.repo_name}","export ROOT=$(pwd -P)",]# if self.challenge is None:startup_commands += ["git status","git restore .",- f"git reset --hard {self.args.repo.base_commit}",+ f"git reset --hard {self.repo.base_commit}","git clean -fdxq",]self.communicate_with_handling(@@ -632,8 +457,6 @@ class SWEEnv(gym.Env):Handles container initialization. Defines container name and creates it.If cached_image is provided, it will use that image name instead of the default."""- kwargs = {k: v for k, v in self.args.deployment.model_dump().items() if k != "type"}- self.deployment = get_deployment(self.args.deployment.type, **kwargs) # type: ignoreasyncio.run(self.deployment.start())asyncio.run(self.deployment.runtime.create_session(CreateBashSessionRequest(startup_source=["/root/.bashrc"])))self.logger.info("🌱 Environment Initialized")@@ -932,5 +755,5 @@ class SWEEnv(gym.Env):file_contents: Contents of file as string"""# todo: Just use the runtime for this instead- path_in_container = f"/{self.args.repo.repo_name}/{path}"+ path_in_container = f"/{self.repo.repo_name}/{path}"return self.communicate(f"cat {str(path_in_container)}")diff --git a/sweagent/main/run_basic.py b/sweagent/main/run_basic.pyindex 1f723408..dc6aba69 100644--- a/sweagent/main/run_basic.py+++ b/sweagent/main/run_basic.py@@ -30,7 +30,7 @@ class BasicMain:self.args = argsself.logger = get_logger("PlaygroundMain")self.logger.info("Initializing environment")- self.env = SWEEnv(args.env)+ self.env = SWEEnv.from_config(args.env)self.logger.info("Initializing agent")self.agent = Agent("main", args.agent)diff --git a/tests/test_env.py b/tests/test_env.pyindex 093dc921..e91c1462 100644--- a/tests/test_env.py+++ b/tests/test_env.py@@ -8,7 +8,7 @@ import pytestimport yamlfrom sweagent import CONFIG_DIR-from sweagent.environment.swe_env import EnvHook+from sweagent.environment.hooks.abstract import EnvHookfrom .conftest import swe_env_context