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]

Only warn if state command has non-zero exit_code

Kilian Lieret1c5196af7127
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 38e58ab7..1965fe2d 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -627,7 +627,7 @@ class Agent:
input=run_action,
timeout=self.tools.config.execution_timeout,
set_last_action=True,
- check=self._always_require_zero_exit_code,
+ check="raise" if self._always_require_zero_exit_code else "ignore",
)
except CommandTimeoutError:
try:
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 7457352c..8b2cfd0b 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -2,7 +2,7 @@ import asyncio
import logging
import shlex
from pathlib import PurePath
-from typing import Self
+from typing import Literal, Self
from pydantic import BaseModel, ConfigDict, Field
from swerex.deployment.abstract import AbstractDeployment
@@ -93,14 +93,14 @@ class SWEEnv:
self._init_deployment()
self.reset()
for command in self._post_startup_commands:
- self.communicate(command, check=True)
+ self.communicate(command, check="raise")
def _copy_repo(self) -> None:
"""Clone/copy repository/codebase in container"""
if self.repo is None:
return
- folders = self.communicate(input="ls", check=True).split("\n")
+ folders = self.communicate(input="ls", check="raise").split("\n")
if self.repo.repo_name in folders:
return
@@ -118,7 +118,7 @@ class SWEEnv:
info: additional information (e.g. debugging information)
"""
info = {}
- self.communicate(input="cd /", check=True)
+ self.communicate(input="cd /", check="raise")
self._copy_repo()
self._reset_repository()
self._chook.on_environment_startup()
@@ -138,7 +138,7 @@ class SWEEnv:
]
self.communicate(
input=" && ".join(startup_commands),
- check=True,
+ check="raise",
error_msg="Failed to clean repository",
# Sometimes this is slow because it rebuilds some index
timeout=120,
@@ -173,7 +173,7 @@ class SWEEnv:
timeout: int | float = 25,
*,
set_last_action: bool = False,
- check: bool = False,
+ check: Literal["warn", "ignore", "raise"] = "ignore",
error_msg: str = "Command failed",
) -> str:
"""Executes a command in the running shell. The details of this are handled by
@@ -183,7 +183,8 @@ class SWEEnv:
input: input to send to container
timeout_duration: duration to wait for output
set_last_action: whether to set the LAST_ACTION environment variable
- check: whether to raise an error if the exit code is non-zero
+ check: `ignore`: do not extract exit code (more stable), `warn`: extract exit code and log error if
+ exit code is non-zero, `raise`: raise error if exit code is non-zero
error_msg: error message to raise if the command fails
Returns:
@@ -196,11 +197,13 @@ class SWEEnv:
)
output = r.output
self.logger.log(logging.TRACE, "Output:\n%s", output) # type: ignore
- if check and r.exit_code != 0:
+ if check != "ignore" and r.exit_code != 0:
self.logger.error(f"{error_msg}:\n{output}")
- self.close()
msg = f"Command {input!r} failed ({r.exit_code=}): {error_msg}"
- raise RuntimeError(msg)
+ self.logger.error(msg)
+ if check == "raise":
+ self.close()
+ raise RuntimeError(msg)
# todo: What do we do with this?
if set_last_action:
# Cannot merge this with last command, because of multiline command
@@ -233,4 +236,4 @@ class SWEEnv:
"""Set environment variables in the environment."""
_env_setters = [f"export {k}={v}" for k, v in env_variables.items()]
command = " && ".join(_env_setters)
- self.communicate(command, check=True)
+ self.communicate(command, check="raise")
diff --git a/sweagent/tools/tools.py b/sweagent/tools/tools.py
index 5d204590..573846b2 100644
--- a/sweagent/tools/tools.py
+++ b/sweagent/tools/tools.py
@@ -193,7 +193,7 @@ class ToolHandler:
def reset(self, env: SWEEnv) -> None:
self.logger.info("Resetting tools")
env.set_env_variables(self.config.env_variables)
- env.communicate(" && ".join(self._reset_commands), check=True, timeout=self.config.install_timeout)
+ env.communicate(" && ".join(self._reset_commands), check="raise", timeout=self.config.install_timeout)
async def _upload_bundles(self, env: SWEEnv) -> None:
await asyncio.gather(
@@ -222,7 +222,7 @@ class ToolHandler:
def _install_commands(self, env: SWEEnv) -> None:
"""Make sure all commands are available in the container"""
env.set_env_variables(self.config.env_variables)
- cwd = env.communicate("pwd", check=True).strip()
+ cwd = env.communicate("pwd", check="raise").strip()
asyncio.run(self._upload_bundles(env))
for bundle in self.config.bundles:
cmds = [
@@ -234,11 +234,11 @@ class ToolHandler:
cmds.append(f"chmod +x /root/tools/{bundle.path.name}/bin/*")
env.communicate(
" && ".join(cmds),
- check=True,
+ check="raise",
timeout=self.config.install_timeout,
)
- env.communicate(f"cd {cwd}", check=True)
- path = env.communicate("echo $PATH", check=True).strip()
+ env.communicate(f"cd {cwd}", check="raise")
+ path = env.communicate("echo $PATH", check="raise").strip()
asyncio.run(self._check_available_commands(env, {"PATH": path}))
# Getting state
@@ -246,7 +246,8 @@ class ToolHandler:
def _get_state(self, state_command: str, env: SWEEnv) -> dict[str, str]:
"""Execute state command in the environment and parse the output as a json object."""
- output = env.communicate(state_command, check=True).strip()
+ # Enough to warn, because we're gonna load the output anyway, so that probably catches all real errors
+ output = env.communicate(state_command, check="warn").strip()
if not output:
self.logger.warning(f"State command {state_command!r} returned empty output")
return {}