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]
Enh(agent): Much more stable autosubmit after error
Kilian Lieretfbda09f78229
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex 0c377a66..ce550885 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -622,34 +622,44 @@ class Agent:step = step.model_copy()step.done = Trueassert self._env is not None+ repo_name = "/"+ if self._env.repo is not None:+ repo_name = f"/{self._env.repo.repo_name}"+ submission_command = "git add -A && git diff --cached > /root/model.patch"+ self.logger.info("Executing submission command %s in %s", submission_command, repo_name)try:- self._env.interrupt_session()+ self._env.execute_command(submission_command, check=True, cwd=repo_name)except Exception as e:- self.logger.debug("Failed to interrupt session before autosubmit: %s. Ignoring.", e)- try:- observation = self._env.communicate(input=self.tools.config.submit_command)- except Exception as e:- self.logger.debug("Failed to submit after error, got %s", e)- return step- step = self.handle_submission(step, observation=observation)+ self.logger.error("Failed to execute submission command, got %s", e)+ # There's still hope for the submission, because the `/root/model.patch` file might have been+ # generated by the state command+ step = self.handle_submission(step, observation="", force_submission=True)if step.submission:self.logger.info("Exiting with autosubmission")step.observation = "Exited (autosubmitted)"return step- def handle_submission(self, step: StepOutput, *, observation="") -> StepOutput:+ def handle_submission(self, step: StepOutput, *, observation="", force_submission: bool = False) -> StepOutput:"""Check if there was a submission in the observation and handle it.Args:step:observation: If specified, will use this rather than stepobservation+ force_submission: If True, will always submit even if no submission is found++ Returns:+ step: step with submission and observation updated (if submission was found)"""step = step.model_copy()assert self.tools is not Noneis_submission = self.tools.check_for_submission_cmd(observation or step.observation)- if is_submission:+ if is_submission or force_submission:assert self._env is not None- submission = self._env.read_file("/root/model.patch")+ try:+ submission = self._env.read_file("/root/model.patch")+ except FileNotFoundError:+ self.logger.warning("Submission file not found, no submission was made")+ return stepif submission.strip() != "":step.submission = submissionelse:diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex bc780a51..6c275fc0 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -8,6 +8,7 @@ from pydantic import BaseModel, ConfigDict, Fieldfrom swerex.deployment.abstract import AbstractDeploymentfrom swerex.deployment.config import DeploymentConfig, DockerDeploymentConfig, get_deploymentfrom swerex.runtime.abstract import BashAction, BashInterruptAction, CreateBashSessionRequest, ReadFileRequest+from swerex.runtime.abstract import Command as RexCommandfrom sweagent.environment.hooks.abstract import CombinedEnvHooks, EnvHookfrom sweagent.environment.repo import Repo, RepoConfig@@ -168,6 +169,7 @@ class SWEEnv:self.logger.info("Environment Initialized")def interrupt_session(self):+ self.logger.info("Interrupting session")asyncio.run(self.deployment.runtime.run_in_session(BashInterruptAction()))# todo: return exit code?@@ -225,3 +227,16 @@ class SWEEnv:_env_setters = [f"export {k}={shlex.quote(str(v))}" for k, v in env_variables.items()]command = " && ".join(_env_setters)self.communicate(command, check="raise")++ def execute_command(+ self,+ command: str,+ shell: bool = True,+ check: bool = False,+ env: dict[str, str] | None = None,+ cwd: str | None = None,+ ) -> None:+ """Execute a command in the environment independent of the session (i.e., as a subprocess)"""+ asyncio.run(+ self.deployment.runtime.execute(RexCommand(command=command, shell=shell, check=check, env=env, cwd=cwd))+ )