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]
Completely broken WIP
Kilian Lieret9fbe4ee3c16c
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex 5c116bd0..d8feff7b 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -2,10 +2,10 @@ from __future__ import annotationsimport copyimport json+import loggingimport time-from collections import defaultdictfrom pathlib import Path-from typing import Any+from typing import Any, Literalimport yamlfrom jinja2 import Template@@ -24,12 +24,11 @@ from sweagent.agent.models import (AbstractModel,HumanModel,HumanThoughtModel,- InstanceStats,ModelConfig,get_model,)from sweagent.agent.problem_statement import ProblemStatement, ProblemStatementConfig-from sweagent.agent.reviewer import AbstractRetryLoop, RetryLoopConfig, get_retry_loop_from_config+from sweagent.agent.reviewer import RetryLoopConfig, get_retry_loop_from_configfrom sweagent.environment.swe_env import SWEEnvfrom sweagent.exceptions import (ContentPolicyViolationError,@@ -42,7 +41,7 @@ from sweagent.tools.parsing import (ThoughtActionParser,)from sweagent.tools.tools import ToolConfig, ToolHandler-from sweagent.types import AgentInfo, AgentRunResult, History, ReviewSubmission, StepOutput, Trajectory, TrajectoryStep+from sweagent.types import AgentInfo, AgentRunResult, ReviewSubmission, StepOutput, Trajectory, TrajectoryStepfrom sweagent.utils.config import _convert_paths_to_abspath, _strip_abspath_from_dictfrom sweagent.utils.jinja_warnings import _warn_probably_wrong_jinja_syntaxfrom sweagent.utils.log import get_logger@@ -124,7 +123,7 @@ class TemplateConfig(BaseModel):return self-class AgentConfig(BaseModel):+class DefaultAgentConfig(BaseModel):"""This configuration object specifies the behavior of an agent."""name: str = "main"@@ -137,13 +136,24 @@ class AgentConfig(BaseModel):"""Maximum number of times to requery the model after an error, such as aformatting error, a blocked action, or a bash syntax error."""- retry_loop: RetryLoopConfig | None = Noneaction_sampler: ActionSamplerConfig | None = None+ type: Literal["default"] = "default"+# pydantic configmodel_config = ConfigDict(extra="forbid")+class RetryAgentConfig(BaseModel):+ agent_configs: list[DefaultAgentConfig]+ retry_loop: RetryLoopConfig+ type: Literal["retry"] = "retry"+ model_config = ConfigDict(extra="forbid")+++AgentConfig = DefaultAgentConfig | RetryAgentConfig++class _BlockedActionError(Exception):"""Raised when the agent's action is blocked"""@@ -158,7 +168,110 @@ RETRY_WITH_OUTPUT_TOKEN = "###SWE-AGENT-RETRY-WITH-OUTPUT###"RETRY_WITHOUT_OUTPUT_TOKEN = "###SWE-AGENT-RETRY-WITHOUT-OUTPUT###"-class Agent:+class AbstractAgent:+ def __init__(self, *args, **kwargs):+ model: AbstractModel+ replay_config: BaseModel | None+ logger: logging.Logger++ @classmethod+ def from_config(cls, config: AgentConfig) -> Self: ...++ def add_hook(self, hook: AbstractAgentHook) -> None: ...++ def run(self, *args, **kwargs) -> AgentRunResult: ...+++class RetryAgent(AbstractAgent):+ def __init__(self, config: RetryAgentConfig):+ self.config = config+ self._hooks = []+ self._i_attempt = 0+ self.logger = get_logger("swea-agent", emoji="🤠")++ @classmethod+ def from_config(cls, config: RetryAgentConfig) -> Self:+ return cls(config)++ def add_hook(self, hook: AbstractAgentHook) -> None:+ self._hooks.append(hook)++ def save_trajectory(self) -> None: ...++ def run(+ self, env: SWEEnv, problem_statement: ProblemStatement | ProblemStatementConfig, output_dir: Path = Path(".")+ ) -> AgentRunResult:+ output_dir.mkdir(parents=True, exist_ok=True)+ self._chook.on_run_start()+ self._rloop = get_retry_loop_from_config(self.config.retry_loop, instance=problem_statement)+ assert self._rloop is not None+ assert env is not None+ while True:+ agent = Agent.from_config(self.config.agent_configs[self._i_attempt])+ # todo: change the output dir+ attempt_output = agent.run(env, problem_statement, output_dir)+ self._rloop.on_submit(ReviewSubmission(trajectory=attempt_output.trajectory, info=attempt_output.info))+ attempt_output.info["review"] = self._rloop.reviews[-1].model_dump() # type: ignore+ self.save_trajectory()+ if not self._rloop.retry():+ break+ self._i_attempt += 1+ env.hard_reset()++ self._chook.on_run_done(trajectory=self.trajectory, info=self.info)++ self.logger.info("Trajectory saved to %s", self.traj_path)++ # Here we want to return the "global" information (e.g., submission should+ # be the best submission instead of the last one, etc.), so we get it from the traj file+ data = self.get_trajectory_data()+ return AgentRunResult(info=data["info"], trajectory=data["trajectory"])++ return AgentRunResult(info=attempt_output.info, trajectory=attempt_output.trajectory)++ def _run_attempt(+ self,+ env: SWEEnv,+ problem_statement: ProblemStatement | ProblemStatementConfig,+ output_dir: Path = Path("."),+ ) -> StepOutput:+ """Run the agent on a problem instance. This method contains the+ main loop that repeatedly calls `self._step` until the problem is solved.++ Args:+ setup_args: Arguments to pass to the agent's setup method.+ env: The environment to run the agent on.+ traj_dir: Directory to save the trajectory to+ """+ output_dir.mkdir(parents=True, exist_ok=True)+ self.setup(env=env, problem_statement=problem_statement, output_dir=output_dir)++ # Run action/observation loop+ self._chook.on_run_start()+ step_output = StepOutput()+ while not step_output.done:+ step_output = self.step()+ self.save_trajectory()+ if step_output.done:+ if self._rloop is not None:+ self._rloop.on_submit(ReviewSubmission(trajectory=self.trajectory, info=self.info))+ self.info["review"] = self._rloop.reviews[-1].model_dump() # type: ignore+ self.save_trajectory()+ if self._rloop.retry():+ assert self._env is not None+ self.setup_attempt(hard_reset=True)+ step_output.done = False+ self._chook.on_run_done(trajectory=self.trajectory, info=self.info)++ self.logger.info("Trajectory saved to %s", self.traj_path)++ # Here we want to return the "global" information (e.g., submission should+ # be the best submission instead of the last one, etc.), so we get it from the traj file+ data = self.get_trajectory_data()+ return AgentRunResult(info=data["info"], trajectory=data["trajectory"])+++class Agent(AbstractAgent):def __init__(self,*,@@ -170,7 +283,6 @@ class Agent:name: str = "main",_catch_errors: bool = True,_always_require_zero_exit_code: bool = False,- retry_loop_config: RetryLoopConfig | None = None,action_sampler_config: ActionSamplerConfig | None = None,):"""The agent handles the behaviour of the model and how it interacts with the environment.@@ -190,25 +302,16 @@ class Agent:self.history_processors = history_processorsself.max_requeries = max_requeriesself.logger = get_logger("swea-agent", emoji="🤠")- self.retry_loop_config = retry_loop_config# Set in run methodself._env: SWEEnv | None = Noneself._problem_statement: ProblemStatement | ProblemStatementConfig | None = Noneself.traj_path: Path | None = None- #: Number of attempts to solve the issue when using a review loop- self._i_attempt: int = 0- self._rloop: AbstractRetryLoop | None = None-#: The following three attributes collect the information about how the agent#: solved the problem.- self._history_by_attempt: dict[int, list] = defaultdict(list)- self._trajectory_by_attempt: dict[int, Trajectory] = defaultdict(list)- self._info_by_attempt: dict[int, AgentInfo] = defaultdict(dict) # type: ignore-- #: Variables to be referenced in the templates that are forwarded from one- #: solution attempt to the next- self._forwarded_vars: dict[str, Any] = {}+ self.history = []+ self._trajectory = []+ self.info = AgentInfo()self._chook = CombinedAgentHook()@@ -226,7 +329,7 @@ class Agent:self._n_consecutive_timeouts = 0@classmethod- def from_config(cls, config: AgentConfig) -> Self:+ def from_config(cls, config: DefaultAgentConfig) -> Self:model = get_model(config.model, config.tools)return cls(templates=config.templates,@@ -234,7 +337,6 @@ class Agent:history_processors=config.history_processors,model=model,max_requeries=config.max_requeries,- retry_loop_config=config.retry_loop,action_sampler_config=config.action_sampler,)@@ -246,6 +348,10 @@ class Agent:# Properties# ----------+ @property+ def trajectory(self) -> Trajectory:+ return self._trajectory+@propertydef replay_config(self) -> BaseModel | None:return self._replay_config@@ -257,39 +363,6 @@ class Agent:self._replay_config = RunSingleConfig.model_validate(_strip_abspath_from_dict(value.model_dump()))- @property- def history(self) -> History:- """History that is passed on to the model for the current attempt.-- Note: Use `_append_history` to modify.- """- return self._history_by_attempt[self._i_attempt]-- @history.setter- def history(self, value: History):- self._history_by_attempt[self._i_attempt] = value-- @property- def trajectory(self) -> Trajectory:- """Trajectory of the agent for the current instance & attempt. In contrast to `history`,- this is mostly for the informational value of how the agent interacted with- the environment and is also what is being used when replaying the trajectory- """- return self._trajectory_by_attempt[self._i_attempt]-- @trajectory.setter- def trajectory(self, value: Trajectory):- self._trajectory_by_attempt[self._i_attempt] = value-- @property- def info(self) -> AgentInfo:- """Information about the agent's run for current attempt"""- return self._info_by_attempt[self._i_attempt]-- @info.setter- def info(self, value: AgentInfo):- self._info_by_attempt[self._i_attempt] = value-@propertydef messages(self) -> list[dict[str, Any]]:"""Return the history of the agent for this attempt since the last reset,@@ -304,14 +377,6 @@ class Agent:return messages # type: ignore- @property- def attempt_model_stats(self) -> InstanceStats:- """Model stats of the current attempt"""- total_stats = copy.deepcopy(self.model.stats)- for attempt_idx in range(self._i_attempt):- total_stats -= InstanceStats.model_validate(self._info_by_attempt[attempt_idx]["model_stats"]) # type: ignore- return total_stats-# Methods# -------@@ -340,51 +405,21 @@ class Agent:self.traj_path = output_dir / (self._problem_statement.id + ".traj")self.logger.info("Trajectory will be saved to %s", self.traj_path)- # Gets inremented to 0 in setup_attempt which we call from here- self._i_attempt = -1- self._history_by_attempt = defaultdict(list)- self._trajectory_by_attempt = defaultdict(list)- self._info_by_attempt = defaultdict(dict) # type: ignore- self._forwarded_vars = {}- if self.retry_loop_config is not None:- # Make sure to initialize a new model, so that the reviews itself do not count- # to the instance stats and in particular we can't run out of cost there.- rloop_model = get_model(- self.model.config.model_copy(update={"per_instance_cost_limit": 0}), self.tools.config- )- self._rloop = get_retry_loop_from_config(self.retry_loop_config, problem_statement, rloop_model)- if self._rloop is not None:- self._forwarded_vars = self._rloop.get_forwarded_vars()self._chook.on_tools_installation_started()self.tools.install(self._env)- self.setup_attempt()-- self._chook.on_setup_done()-- def setup_attempt(self, hard_reset: bool = False) -> None:- """Setup the agent for a new attempt."""self._chook.on_setup_attempt()- self._i_attempt += 1- if self._rloop is not None:- self._rloop.on_attempt_started(self._i_attempt, self)self.info = AgentInfo()self.info["swe_agent_hash"] = get_agent_commit_hash()self.info["swe_agent_version"] = __version__self.info["swe_rex_version"] = get_rex_version()self.info["swe_rex_hash"] = get_rex_commit_hash()assert self._env is not None- if self._i_attempt > 0:- if hard_reset:- self._chook.on_tools_installation_started()- self._env.hard_reset()- self.tools.install(self._env)- else:- self._env.reset()assert self._problem_statement is not Noneself._env.set_env_variables({"PROBLEM_STATEMENT": self._problem_statement.get_problem_statement()})self.add_system_message_to_history()self.add_demonstrations_to_history()self.add_instance_template_to_history(state=self.tools.get_state(self._env))+ self._chook.on_setup_done()def add_system_message_to_history(self) -> None:"""Add system message to history"""@@ -448,7 +483,6 @@ class Agent:command_docs=self.tools.config.command_docs,**self.tools.config.env_variables,**kwargs,- **self._forwarded_vars,problem_statement=self._problem_statement.get_problem_statement(),repo=self._env.repo.repo_name if self._env.repo is not None else "",**self._problem_statement.get_extra_fields(),@@ -542,40 +576,19 @@ class Agent:def get_trajectory_data(self) -> dict[str, Any]:"""Get all data that we save in .traj files."""- def get_attempt_data(attempt_idx: int) -> dict[str, Any]:- """Get data saved for every attempt"""- assert self._env is not None- # The deepcopy here is important because else the- # data["info"]["model_stats"] update will create havoc!- attempt_data = copy.deepcopy(- {- "trajectory": self._trajectory_by_attempt[attempt_idx],- "history": self._history_by_attempt[attempt_idx],- "info": self._info_by_attempt[attempt_idx],- }- )- attempt_data["replay_config"] = (- self.replay_config.model_dump_json() if self.replay_config is not None else None- )- attempt_data["environment"] = self._env.name- return attempt_data-- if self._rloop is not None:- best_attempt_idx = self._rloop.get_best()- data = {- "attempts": [get_attempt_data(i) for i in range(self._i_attempt + 1)],- **get_attempt_data(best_attempt_idx),- }- data["info"]["best_attempt_idx"] = best_attempt_idx- # Total model stats- data["info"]["model_stats"] = self.model.stats.model_dump()- # data["extra_info"] = {"comparisons": [(a, b, comp.model_dump()) for a, b, comp in self._rloop.comparisons]}- else:- data = {- **get_attempt_data(0),+ assert self._env is not None+ # The deepcopy here is important because else the+ # data["info"]["model_stats"] update will create havoc!+ attempt_data = copy.deepcopy(+ {+ "trajectory": self.trajectory,+ "history": self.history,+ "info": self.info,}-- return data+ )+ attempt_data["replay_config"] = self.replay_config.model_dump_json() if self.replay_config is not None else None+ attempt_data["environment"] = self._env.name+ return attempt_datadef save_trajectory(self,@@ -790,8 +803,6 @@ class Agent:step = StepOutput()try:# Forward model and get actions- if self._rloop is not None:- self._rloop.on_model_query(attempt_stats=self.attempt_model_stats)self._chook.on_model_query(messages=history, agent=self.name)# todo: Add all options to the extra infoif self._action_sampler is not None:@@ -995,7 +1006,7 @@ class Agent:self.info["submission"] = step_output.submissionself.info["exit_status"] = step_output.exit_status # type: ignoreself.info.update(self._get_edited_files_with_context(patch=step_output.submission or "")) # type: ignore- self.info["model_stats"] = self.attempt_model_stats.model_dump()+ self.info["model_stats"] = self.model.stats.model_dump()self.add_step_to_trajectory(step_output)@@ -1025,15 +1036,6 @@ class Agent:while not step_output.done:step_output = self.step()self.save_trajectory()- if step_output.done:- if self._rloop is not None:- self._rloop.on_submit(ReviewSubmission(trajectory=self.trajectory, info=self.info))- self.info["review"] = self._rloop.reviews[-1].model_dump() # type: ignore- self.save_trajectory()- if self._rloop.retry():- assert self._env is not None- self.setup_attempt(hard_reset=True)- step_output.done = Falseself._chook.on_run_done(trajectory=self.trajectory, info=self.info)self.logger.info("Trajectory saved to %s", self.traj_path)diff --git a/sweagent/agent/reviewer.py b/sweagent/agent/reviewer.pyindex ce29f849..e6da8bf4 100644--- a/sweagent/agent/reviewer.py+++ b/sweagent/agent/reviewer.py@@ -13,10 +13,19 @@ from jinja2 import Templatefrom pydantic import BaseModel, ConfigDictfrom sweagent.agent.history_processors import _set_cache_control-from sweagent.agent.models import AbstractModel, HumanModel, HumanThoughtModel, InstanceStats, LiteLLMModel+from sweagent.agent.models import (+ AbstractModel,+ HumanModel,+ HumanThoughtModel,+ InstanceStats,+ LiteLLMModel,+ ModelConfig,+ get_model,+)from sweagent.agent.problem_statement import ProblemStatementfrom sweagent.exceptions import AttemptCostLimitExceededError-from sweagent.types import BinaryReviewerResult, History, ReviewerResult, ReviewSubmission, Trajectory, TrajectoryStep+from sweagent.tools.tools import ToolConfig+from sweagent.types import ReviewerResult, ReviewSubmission, Trajectory, TrajectoryStepfrom sweagent.utils.log import get_logger# --- INTERFACES ---@@ -32,45 +41,6 @@ class AbstractReviewer(ABC):"""Returns True if the submission is believed to be correct"""-class AbstractBinaryReviewer(ABC):- """The binary reviewer checks two solutions and tries to predict- which one is better.- """-- @abstractmethod- def compare_submissions(- self,- *,- instance: ProblemStatement,- sub1: ReviewSubmission,- sub2: ReviewSubmission,- rev1: ReviewerResult | None,- rev2: ReviewerResult | None,- ) -> BinaryReviewerResult:- """Returns 0 if sub1 is better, 1 if sub2 is better"""---class AbstractGraveToCradle(ABC):- """Forward messages from past attempts to the next one"""-- @abstractmethod- def get_forwarded_vars(- self,- submissions: list[ReviewSubmission],- reviews: list[ReviewerResult],- breviews: list[tuple[int, int, BinaryReviewerResult]],- ) -> dict[str, Any]:- """Get the variables that should be forwarded to the next iteration.-- Note: Must return a dictionary with the correct keys even when called- with empty lists. This is because else we cannot use the variables in the template- when we call for the first attempt.-- Returns:- A dictionary of variables that should be forwarded to the next iteration.- """--class AbstractRetryLoop(ABC):"""The review loop controls how often the agent tries to solvethe issue and how it selects the best solution.@@ -140,24 +110,6 @@ class ReviewerConfig(BaseModel):return Reviewer(self, model)-class BinaryReviewerConfig(BaseModel):- """The configuration for the binary reviewer"""-- system_template: str- instance_template: str- traj_formatter: TrajFormatterConfig-- type: Literal["binary_reviewer"] = "binary_reviewer"-- model_config = ConfigDict(extra="forbid")---class GTCConfig(BaseModel):- """The configuration for the GraveToCradle"""-- model_config = ConfigDict(extra="forbid")--class ScoreRetryLoopConfig(BaseModel):"""The configuration for the review loop"""@@ -179,6 +131,8 @@ class ScoreRetryLoopConfig(BaseModel):#: Override model temperature for first len(list) attemptstemperature_override: list[float] = [0.0]+ model: ModelConfig+model_config = ConfigDict(extra="forbid")def validate(self):@@ -188,8 +142,8 @@ class ScoreRetryLoopConfig(BaseModel):def __post_init__(self):self.validate()- def get_retry_loop(self, instance: ProblemStatement, model: AbstractModel) -> AbstractRetryLoop:- return ScoreRetryLoop(self, instance, model)+ def get_retry_loop(self, instance: ProblemStatement) -> AbstractRetryLoop:+ return ScoreRetryLoop(self, instance)RetryLoopConfig = ScoreRetryLoopConfig@@ -315,113 +269,15 @@ class TrajectoryFormatter:)-class BinaryReviewer:- def __init__(self, config: BinaryReviewerConfig, model: LiteLLMModel):- self._config = config- self._model = model- self._traj_formatter = TrajectoryFormatter(config=config.traj_formatter)- self.logger = get_logger("binary_reviewer", emoji="⚖️")-- def format_messages(self, instance: ProblemStatement, sub1: ReviewSubmission, sub2: ReviewSubmission):- system_message = self._config.system_template- self.logger.debug(f"MODEL INPUT (system)\n{system_message}")- ps_format_dict = {- "problem_statement": instance.get_problem_statement(),- **instance.get_extra_fields(),- }- format_dict = {- **ps_format_dict,- **sub1.to_format_dict(suffix="1"),- **sub2.to_format_dict(suffix="2"),- "traj1": self._traj_formatter.format_trajectory(sub1.trajectory, i_traj=1),- "traj2": self._traj_formatter.format_trajectory(sub2.trajectory, i_traj=2),- }- # print(format_dict)- user_message = Template(self._config.instance_template).render(**format_dict)- self.logger.debug(f"MODEL INPUT (user)\n{user_message}")- return [- {"role": "system", "content": system_message},- {"role": "user", "content": user_message},- ]-- def interpret(self, response: str) -> tuple[Literal[0, 1], float]:- """Interpret response from LM. Note: 1-based indexing"""- last_line = response.strip().split("\n")[-1].strip()- number = re.search(r"\d+\.?\d*", last_line)- if number:- confidence = float(number.group(0))- else:- self.logger.warning(f"No confidence found in {last_line}")- confidence = 0.0- if confidence > 100.0:- self.logger.warning(f"Confidence {confidence} is greater than 100.0")- confidence = 100.0- confidence /= 100.0- if "first" in last_line.lower():- return (0, confidence)- elif "second" in last_line.lower():- return (1, confidence)- self.logger.warning(- "Could not interpret response: %s, will choose first submission with confidence 0.0.",- response,- )- return (0, 0.0)-- def compare_submissions(- self,- instance: ProblemStatement,- sub1: ReviewSubmission,- sub2: ReviewSubmission,- rev1: ReviewerResult | None,- rev2: ReviewerResult | None,- ) -> BinaryReviewerResult:- messages: History = self.format_messages(instance, sub1, sub2) # type: ignore- answer = self._model.query(messages, temperature=0.0)["message"]- idx, confidence = self.interpret(answer)- # Use words because else confusion with 0-based vs 1-based indices- choice_emoji = "first" if idx == 0 else "second"- self.logger.info(f"{choice_emoji}\n{answer}")- return BinaryReviewerResult(choice=idx, output=answer, messages=messages, confidence=confidence) # type: ignore---class GraveToCradle(AbstractGraveToCradle):- def __init__(self, config: GTCConfig, model: AbstractModel):- self._config = config- self._model = model-- def get_forwarded_vars(- self,- submissions: list[ReviewSubmission],- reviews: list[ReviewerResult],- breviews: list[tuple[int, int, BinaryReviewerResult]],- ) -> dict[str, Any]:- assert len(submissions) == len(reviews)- failed_idxs = [i for i, r in enumerate(reviews) if not r.accept]- if not failed_idxs:- return {"failed_verdicts_with_submissions": ""}- msg_lines = ["The following previous submissions were deemed to be incorrect:"]- for i, idx in enumerate(failed_idxs):- info = submissions[idx].info- if not info.get("submission"):- continue- submission = info["submission"] # type: ignore- # todo: currently we only take the first output- review = reviews[idx].outputs[0]- msg_lines.append(f"Submission {i + 1}:\n\n{submission}\n\nReview {i + 1}:\n\n{review}")- msg = "\n\n".join(msg_lines)- return {"failed_verdicts_with_submissions": msg}--class ScoreRetryLoop(AbstractRetryLoop):def __init__(self,loop_config: ScoreRetryLoopConfig,instance: ProblemStatement,- model: AbstractModel,):- self._model = model+ self._model = get_model(loop_config.model, tools=ToolConfig())self._instance = instance- self._reviewer: AbstractReviewer = loop_config.reviewer_config.get_reviewer(model)+ self._reviewer: AbstractReviewer = loop_config.reviewer_config.get_reviewer(self._model)self._loop_config = loop_config# Note: These are "cumulative" submissions, i.e., they include all retries# up to that point.@@ -529,9 +385,7 @@ class ScoreRetryLoop(AbstractRetryLoop):return best_indices[0]-def get_retry_loop_from_config(- config: RetryLoopConfig | None, instance: ProblemStatement, model: AbstractModel-) -> AbstractRetryLoop | None:+def get_retry_loop_from_config(config: RetryLoopConfig | None, instance: ProblemStatement) -> AbstractRetryLoop | None:if config is None:return None- return config.get_retry_loop(instance=instance, model=model)+ return config.get_retry_loop(instance=instance)