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]
Ref: Extract _guard_multiline_input
Kilian Lieret2ab6ec0100e6
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex 8cd70777..2b616959 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -27,6 +27,7 @@ from sweagent.agent.models import (from sweagent.agent.parsing import FormatError, ParseFunction# from sweagent.agent.summarizer import SummarizerConfig+from sweagent.agent.utils import _guard_multiline_inputfrom sweagent.environment.swe_env import SWEEnvfrom sweagent.types import AgentInfo, History, HistoryItem, Trajectory, TrajectoryStepfrom sweagent.utils.config import _convert_paths_to_abspath@@ -320,6 +321,7 @@ class Agent:system_msg = self.config.system_template.format(**self.system_args, **self.instance_args)self.logger.info(f"SYSTEM ({self.name})\n{system_msg}")self._append_history(HistoryItem({"role": "system", "content": system_msg, "agent": self.name}))+ # todo: This should be moved somewhereif "history_to_messages" in dir(self.model):for demonstration_path in self.config.demonstrations:if self.config.demonstration_template is None and not self.config.put_demos_in_history:@@ -405,22 +407,17 @@ class Agent:assert self.traj_path is not Noneself.traj_path.write_text(json.dumps(data, indent=2))- def _get_first_match(self, action: str, pattern_type: str) -> re.Match | None:- """Return the first match of a command pattern in the action string."""-- if pattern_type == "subroutine":- patterns = {k: v for k, v in self.subroutine_patterns.items()}- elif pattern_type == "multi_line":- patterns = {- k: v- for k, v in self.command_patterns.items()- if k in self.config.multi_line_command_endings or k == self.config.submit_command- }- elif pattern_type == "multi_line_no_subroutines":- patterns = {k: v for k, v in self.command_patterns.items() if k in self.config.multi_line_command_endings}- else:- msg = f"Unknown pattern type: {pattern_type}"- raise ValueError(msg)+ def _get_first_multiline_cmd(self, action: str) -> re.Match | None:+ """Return the first match of a command pattern in the action string.+ Where first match is defined by the start of the match.++ The match object has three groups: (1) command name, (2) command arguments, (3) end name+ """+ patterns = {+ k: v+ for k, v in self.command_patterns.items()+ if k in self.config.multi_line_command_endings or k == self.config.submit_command+ }matches = list()for _, pat in patterns.items():match = pat.search(action)@@ -431,36 +428,13 @@ class Agent:matches = sorted(matches, key=lambda x: x.start())return matches[0]- #: todo: Should that be a utility function??def _guard_multiline_input(self, action: str) -> str:"""Split action by multiline commands, then append the first line in each multiline command with "<< '{end_name}'".Multiline commands (which are specified by an end_name) are commands that span multiple lines and are terminated by a specific end_name.Their multi-line argument is sent using a heredoc, which is a way to send a multi-line string to a command in bash."""- parsed_action = list()- rem_action = action- while rem_action.strip():- first_match = self._get_first_match(rem_action, "multi_line_no_subroutines")- if first_match:- pre_action = rem_action[: first_match.start()]- match_action = rem_action[first_match.start() : first_match.end()]- rem_action = rem_action[first_match.end() :]- if pre_action.strip():- parsed_action.append(pre_action)- if match_action.strip():- eof = first_match.group(3).strip()- if not match_action.split("\n")[0].strip().endswith(f"<< '{eof}'"):- guarded_command = match_action[first_match.start() :]- first_line = guarded_command.split("\n")[0]- guarded_command = guarded_command.replace(first_line, first_line + f" << '{eof}'", 1)- parsed_action.append(guarded_command)- else:- parsed_action.append(match_action)- else:- parsed_action.append(rem_action)- rem_action = ""- return "\n".join(parsed_action)+ return _guard_multiline_input(action, self._get_first_multiline_cmd)def _parse_command_patterns(self) -> None:"""Sets self.command_patterns and self.subroutine_patterns"""@@ -476,7 +450,6 @@ class Agent:else:pat = re.compile(rf"^\s*({command.name})\s*(.*?)$", re.MULTILINE)self.command_patterns[command.name] = pat- self.subroutine_patterns = dict()if hasattr(self.config, "submit_command_end_name"):submit_pat = re.compile(rf"^\s*({self.config.submit_command})\s*(.*?)^({self.config.submit_command_end_name})\s*$",@@ -484,7 +457,6 @@ class Agent:)else:submit_pat = re.compile(rf"^\s*({self.config.submit_command})(\s*)$", re.MULTILINE) # group 2 is nothing- self.subroutine_patterns[self.config.submit_command] = submit_patself.command_patterns[self.config.submit_command] = submit_patdef forward(self, observation: str, available_actions: list[str], state: dict[str, str]) -> tuple[str, str, str]:@@ -744,6 +716,7 @@ class Agent:return env_varsdef get_state(self) -> dict[str, str]:+ """If a state command is defined, execute it in the environment and return the result as a dictionary."""if not self.config.state_command:return {}assert self._env is not None@@ -754,9 +727,10 @@ class Agent:msg = f"State {state!r} is not valid json. This is an internal error, please report it."raise ValueError(msg) from e- def _step(self, observation: str) -> tuple[str, bool]:- """Run a step of the agent: Forward the last observation to the LM,- then execute the action and save the trajectory.+ def step(self, observation: str) -> tuple[str, bool]:+ """Run a step of the agent: Take the last observation, combine it with all other context+ (i.e., the history of thoughts and actions), then execute the action and return the new observation.+ Also saves the new trajectory.Returns:observation: Observation@@ -807,9 +781,8 @@ class Agent:traj_dir: Path | None = None,init_model_stats: APIStats | None = None,) -> tuple[AgentInfo, Trajectory]:- """- Run the agent on an environment.- Return the final value of the specified return type.+ """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.@@ -839,7 +812,7 @@ class Agent:self._chook.on_run_start()done = Falsewhile not done:- observation, done = self._step(observation)+ observation, done = self.step(observation)self.save_trajectory()self._chook.on_run_done(trajectory=self.trajectory, info=self.info)diff --git a/sweagent/agent/utils.py b/sweagent/agent/utils.pynew file mode 100644index 00000000..172a7f5c--- /dev/null+++ b/sweagent/agent/utils.py@@ -0,0 +1,33 @@+import re+from collections.abc import Callable+++def _guard_multiline_input(action: str, match_fct: Callable[[str], re.Match | None]) -> str:+ """Split action by multiline commands, then append the first line in each multiline command with "<< '{end_name}'".+ Multiline commands (which are specified by an end_name) are commands that span multiple lines and are terminated by a specific end_name.++ Their multi-line argument is sent using a heredoc, which is a way to send a multi-line string to a command in bash.+ """+ parsed_action = []+ rem_action = action+ while rem_action.strip():+ first_match = match_fct(rem_action)+ if first_match:+ pre_action = rem_action[: first_match.start()]+ match_action = rem_action[first_match.start() : first_match.end()]+ rem_action = rem_action[first_match.end() :]+ if pre_action.strip():+ parsed_action.append(pre_action)+ if match_action.strip():+ eof = first_match.group(3).strip()+ if not match_action.split("\n")[0].strip().endswith(f"<< '{eof}'"):+ guarded_command = match_action[first_match.start() :]+ first_line = guarded_command.split("\n")[0]+ guarded_command = guarded_command.replace(first_line, first_line + f" << '{eof}'", 1)+ parsed_action.append(guarded_command)+ else:+ parsed_action.append(match_action)+ else:+ parsed_action.append(rem_action)+ rem_action = ""+ return "\n".join(parsed_action)diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 61c50557..7f2ffb9e 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -35,8 +35,6 @@ class EnvironmentInstanceConfig(BaseModel):class SWEEnv:- """Gym environment for SWE-bench. This class should handle all communication with the docker container."""-name = "swe_main"def __init__(@@ -51,6 +49,7 @@ class SWEEnv:_catch_errors: bool = False,_always_require_zero_exit_code: bool = False,):+ """This class represents the environment in which we solve the tasks."""super().__init__()self._catch_errors = _catch_errorsself._require_zero_exit_code = _always_require_zero_exit_code@@ -115,8 +114,7 @@ class SWEEnv:self.repo.copy(self.deployment)def reset(self) -> tuple[str | None, dict]:- """- Function to reset container between each task instance.+ """Reset the container between each task instance.* Clones instance's repository* Cleans repository of prior modifications@@ -205,6 +203,7 @@ class SWEEnv:error_msg="Failed to reset environment variables",)+ # todo: Shouldn't this be the same as reset meanwhile?def reset_for_new_attempt(self,) -> None:@@ -228,8 +227,7 @@ class SWEEnv:# todo: Have a return type here# todo: Break this updef step(self, action: str) -> tuple[str, int, bool, AgentInfo]:- """- Runs an action proposed by the agent in the environment and returns the corresponding output.+ """Runs an action proposed by the agent in the environment and returns the corresponding output.Args:action: command to run in bash shell@@ -312,9 +310,7 @@ class SWEEnv:return observation, 0, False, infodef close(self) -> None:- """- Handle environment shutdown- """+ """Shoutdown SWE-ReX deployment etc."""self.logger.info("Beginning environment shutdown...")asyncio.run(self.deployment.stop())self._fire_hooks("on_close")@@ -329,8 +325,7 @@ class SWEEnv:def _init_container(self,) -> None:- """- Handles container initialization. Defines container name and creates it.+ """Handles container initialization. Defines container name and creates it.If cached_image is provided, it will use that image name instead of the default."""asyncio.run(self.deployment.start())@@ -338,9 +333,7 @@ class SWEEnv:self.logger.info("Environment Initialized")def _init_scripts(self):- """- Initialize custom commands within container- """+ """Initialize custom commands within container"""self.communicate("mkdir -p /root/commands",require_zero=True,@@ -366,14 +359,16 @@ class SWEEnv:require_zero: bool = False,error_msg: str = "Command failed",) -> str:- """Sends input to container and returns output+ """Executes a command in the running shell. The details of this are handled by+ the SWE-ReX deployment/runtime.Args:input: input to send to containertimeout_duration: duration to wait for outputset_last_action: whether to set the LAST_ACTION environment variable- require_zero_exit_code: whether to raise an error if the exit code is non-zero+ require_zero: whether to raise an error if the exit code is non-zeroerror_msg: error message to raise if the command fails+Returns:output: output from container"""@@ -400,16 +395,14 @@ class SWEEnv:return outputdef get_available_actions(self) -> list[str]:- """- Returns list of available actions in current environment state+ """Returns list of available actions in current environment stateCurrently not in use."""return []def get_submission(self, output: str) -> str | None:- """- Function for extracting diff patch submission at the end of an episode.+ """Function for extracting diff patch submission at the end of an episode.Args:output: `submit` observation@@ -424,15 +417,11 @@ class SWEEnv:return match.group(1)def on_environment_startup(self) -> None:- """- Creates conda environment and installs third party dependencies to allow code execution- """+ """Creates conda environment and installs third party dependencies to allow code execution"""self._fire_hooks("on_environment_startup")def add_commands(self, commands: list[dict]) -> None:- """- Adds custom commands to container- """+ """Adds custom commands to container"""for command in commands:name = command["name"]contents = command["contents"]