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]
Agent.run calls Agent.setup for all setup things
Kilian Lieretb742b07c8e21
diff --git a/.vscode/launch.json b/.vscode/launch.jsonindex 5f792643..652e20b5 100644--- a/.vscode/launch.json+++ b/.vscode/launch.json@@ -1,4 +1,5 @@{+ "version": "0.2.0","configurations": [{"name": "Python: Debug Tests",diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex 0c808d88..b326fc47 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -77,8 +77,6 @@ class AgentConfig(BaseModel):# todo: separate out from_config. In particular separate out model (as a class, etc.). Agent should only take templates, tools, history processor and model.# slight problem: get_model needs commands....class Agent:- """Agent handles the behaviour of the model and how it interacts with the environment."""-def __init__(self,*,@@ -90,6 +88,10 @@ class Agent:_catch_errors: bool = True,_always_require_zero_exit_code: bool = False,):+ """The agent handles the behaviour of the model and how it interacts with the environment.++ To run the agent, either call `self.run` or `self.setup` and then `self.step` in a loop.+ """self._catch_errors = _catch_errorsself._always_require_zero_exit_code = _always_require_zero_exit_codeself.name = name@@ -183,14 +185,29 @@ class Agent:self._chook.on_query_message_added(**item)self.history.append(item)- # todo: get rid of this. simply instantiate a new agent class for every instance?- def setup(self, init_model_stats: APIStats | None = None) -> None:+ def setup(+ self,+ env: SWEEnv,+ problem_statement: ProblemStatement | ProblemStatementConfig,+ output_dir: Path = Path("."),+ ) -> None:"""Setup the agent for a new instance. This includesformatting the system message and adding demonstrations to the history.+ This method is called by `self.run`.+Args:instance_args: Arguments for the instance"""+ self._problem_statement = problem_statement+ self._env = env++ # Save/reset some attributes+ self.info = AgentInfo()+ self.info["swe_agent_hash"] = get_agent_commit_hash()+ self.info["swe_agent_version"] = __version__+ self.traj_path = output_dir / (self._problem_statement.id + ".traj")+ self.logger.info("Trajectory will be saved to %s", self.traj_path)self._i_attempt = 0self._history_by_attempt = defaultdict(list)@@ -200,22 +217,16 @@ class Agent:# if self._rloop is not None:# self._forwarded_vars = self._rloop.get_forwarded_vars()self.tools.install(self._env)- self.setup_attempt(init_model_stats=init_model_stats)+ self.setup_attempt()self._chook.on_setup_done()- def setup_attempt(self, *, init_model_stats: APIStats | None = None) -> None:+ def setup_attempt(self) -> None:"""Setup the agent for a new attempt. This includes resetting the model stats."""- if self._i_attempt > 0 and init_model_stats is not None:- msg = (- "We might be dealing with nested retries, where subroutines are mixed with retries. "- "Currently, this messes up accounting with init_model_stats."- )- raise ValueError(msg)if self._i_attempt > 0:assert self._env is not None # mypyself._env.reset_for_new_attempt()- self.model.reset_stats(init_model_stats)+ self.model.reset_stats()self.add_system_message_to_history()self.add_demonstrations_to_history()@@ -656,11 +667,9 @@ class Agent:# todo: Set env already in __init__, i.e., initialize a new agent class for every instance?def run(self,- problem_statement: ProblemStatement | ProblemStatementConfig,env: SWEEnv,- observation: str = "",- traj_dir: Path = Path("."),- init_model_stats: APIStats | None = None,+ problem_statement: ProblemStatement | ProblemStatementConfig,+ output_dir: Path = Path("."),) -> AgentRunResult:"""Run the agent on a problem instance. This method contains themain loop that repeatedly calls `self._step` until the problem is solved.@@ -668,26 +677,15 @@ class Agent:Args:setup_args: Arguments to pass to the agent's setup method.env: The environment to run the agent on.- observation: Output from environment setuptraj_dir: Directory to save the trajectory toinit_model_stats: Initial model stats to use for the run."""- self._problem_statement = problem_statement- self._env = env- self.setup(init_model_stats)-- # Save/reset some attributes- self.trajectory = Trajectory()- self.info = AgentInfo()- self.info["swe_agent_hash"] = get_agent_commit_hash()- self.info["swe_agent_version"] = __version__- self.traj_path = traj_dir / (self._problem_statement.id + ".traj")-- self.logger.info("Trajectory will be saved to %s", self.traj_path)+ self.setup(env=env, problem_statement=problem_statement, output_dir=output_dir)# Run action/observation loopself._chook.on_run_start()done = False+ observation = ""while not done:observation, done = self.step(observation)self.save_trajectory()diff --git a/sweagent/run/run_batch.py b/sweagent/run/run_batch.pyindex b8663ce5..8c52f6e8 100644--- a/sweagent/run/run_batch.py+++ b/sweagent/run/run_batch.py@@ -125,10 +125,11 @@ class RunBatch:result = self.agent.run(problem_statement=instance.problem_statement,env=env,- traj_dir=Path(self.traj_dir),+ output_dir=Path(self.traj_dir),)- self._chooks.on_instance_completed(result=result)save_predictions(self.traj_dir, instance.problem_statement.id, result)+ self._chooks.on_instance_completed(result=result)+ env.close()def should_skip(self, instance: BatchInstance) -> bool:"""Check if we should skip this instance"""diff --git a/sweagent/run/run_single.py b/sweagent/run/run_single.pyindex 9d23ff4d..de90d7be 100644--- a/sweagent/run/run_single.py+++ b/sweagent/run/run_single.py@@ -111,12 +111,13 @@ class RunSingle:result = self.agent.run(problem_statement=self.problem_statement,env=self.env,- traj_dir=Path(self.traj_dir),+ output_dir=Path(self.traj_dir),)self._chooks.on_instance_completed(result=result)self.logger.info("Done")self._chooks.on_end()save_predictions(self.traj_dir, self.problem_statement.id, result)+ self.env.close()def run_from_config(args: RunSingleConfig):diff --git a/tests/test_agent.py b/tests/test_agent.pyindex 0a79dc93..b96ca9b3 100644--- a/tests/test_agent.py+++ b/tests/test_agent.py@@ -27,7 +27,7 @@ def test_exit_cost_manually_raised(dummy_env: SWEEnv, test_agent: Agent, tmp_patr = test_agent.run(problem_statement=EmptyProblemStatement(),env=dummy_env,- traj_dir=tmp_path,+ output_dir=tmp_path,)assert r.info["exit_status"] == "exit_cost" # type: ignore@@ -37,7 +37,7 @@ def test_exit_cost(dummy_env: SWEEnv, test_agent: Agent, tmp_path):r = test_agent.run(problem_statement=EmptyProblemStatement(),env=dummy_env,- traj_dir=tmp_path,+ output_dir=tmp_path,)assert r.info["exit_status"] == "exit_cost" # type: ignore@@ -47,7 +47,7 @@ def test_exit_context(dummy_env: SWEEnv, test_agent: Agent, tmp_path):r = test_agent.run(problem_statement=EmptyProblemStatement(),env=dummy_env,- traj_dir=tmp_path,+ output_dir=tmp_path,)assert r.info["exit_status"] == "exit_context" # type: ignore@@ -57,7 +57,7 @@ def test_exit_model_error(dummy_env: SWEEnv, test_agent: Agent, tmp_path):r = test_agent.run(problem_statement=EmptyProblemStatement(),env=dummy_env,- traj_dir=tmp_path,+ output_dir=tmp_path,)assert r.info["exit_status"] == "exit_error" # type: ignore@@ -67,7 +67,7 @@ def test_exit_format(dummy_env: SWEEnv, test_agent: Agent, tmp_path):r = test_agent.run(problem_statement=EmptyProblemStatement(),env=dummy_env,- traj_dir=tmp_path,+ output_dir=tmp_path,)assert r.info["exit_status"] == "exit_format" # type: ignore@@ -77,7 +77,7 @@ def test_exit_blocklist(dummy_env: SWEEnv, test_agent: Agent, tmp_path):r = test_agent.run(problem_statement=EmptyProblemStatement(),env=dummy_env,- traj_dir=tmp_path,+ output_dir=tmp_path,)assert r.info["exit_status"] == "exit_format" # type: ignore@@ -96,6 +96,6 @@ def test_early_exit(dummy_env: SWEEnv, test_agent: Agent, tmp_path):r = test_agent.run(problem_statement=EmptyProblemStatement(),env=dummy_env,- traj_dir=tmp_path,+ output_dir=tmp_path,)assert r.info["exit_status"] == "exit_environment_error" # type: ignore