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]
Typing fixes & improvements (#187)
Kilian Lieret74c6123d5abd
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex 4ba2ed08..1e7d6f63 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -20,7 +20,7 @@ from sweagent.agent.parsing import ParseFunction, FormatErrorfrom sweagent.environment.utils import LOGGER_NAMEfrom sweagent.environment.swe_env import SWEEnvfrom tenacity import RetryError-from typing import Optional, Tuple, Any+from typing import Dict, List, Optional, Tuple, Anylogger = logging.getLogger(LOGGER_NAME)@@ -190,7 +190,7 @@ class AgentArguments(FlattenedAccess, FrozenSerializable):# If unassigned, we load the config from the file to store its contents with the overall argumentsconfig = AgentConfig.load_yaml(self.config_file)object.__setattr__(self, "config", config)- assert self.config is not None+ assert self.config is not None # mypyfor subroutine in getattr(self.config, "subroutines", {}).values():model_args = getattr(subroutine, "model")object.__setattr__(@@ -212,6 +212,7 @@ class Agent:args.model, args.config._commands + args.config.subroutine_types)self.config = args.config+ assert self.config is not None # mypyself.system_args = {"command_docs": self.config.command_docs,**self.config.env_variables,@@ -221,15 +222,17 @@ class Agent:self.history = []self.last_container_id = None+def setup(self, instance_args, init_model_stats=None) -> None:"""Setup the agent for a new instance."""+ assert self.config is not None # mypyself.model.reset_stats(init_model_stats)self.instance_args = instance_argssystem_msg = self.config.system_template.format(**self.system_args)logger.info(f"SYSTEM ({self.name})\n{system_msg}")- self.history = [+ self.history: List[Dict[str, Any]] = [{"role": "system", "content": system_msg, "agent": self.name},]@@ -309,6 +312,7 @@ class Agent:def _get_first_match(self, action: str, pattern_type: str) -> Optional[re.Match]:"""Return the first match of a command pattern in the action string."""+ assert self.config is not None # mypyif pattern_type == "subroutine":patterns = {k: v for k, v in self.subroutine_patterns.items()}elif pattern_type == "multi_line":@@ -373,7 +377,7 @@ class Agent:rem_action = ""return "\n".join(parsed_action)- def split_actions(self, action: str, pattern_type="subroutine") -> list[str]:+ def split_actions(self, action: str, pattern_type="subroutine") -> List[Dict[str, Any]]:"""Split an action into a list of actions in a greedy manner, each of which is a subroutine call or a single command."""parsed_action = list()rem_action = action@@ -413,6 +417,7 @@ class Agent:return parsed_actiondef _parse_command_patterns(self):+ assert self.config is not None # mypyself.command_patterns = dict()for command in self.config._commands:if command.end_name is not None:@@ -471,10 +476,11 @@ class Agent:"""Query the model with the current state and observation with the appropriate template.Returns the model output."""+ assert self.config is not None # mypystate_vars = json.loads(state)- templates = []+ templates: List[str] = []# Determine observation template based on what prior observation wasif self.history[-1]["role"] == "system" or self.history[-1].get("is_demo", False@@ -595,6 +601,9 @@ class Agent:def forward_with_error_check(self, observation: str, state: str) -> Tuple[str, str, str]:+ """Wrapper around `self.forward_model` that handles errors and retries+ due to format errors or blocked actions.+ """try:output = self.forward_model(observation, state)except KeyboardInterrupt:@@ -625,6 +634,7 @@ class Agent:self.set_environment_vars(env, self.config.env_variables)def set_environment_vars(self, env, env_variables):+ assert self.config is not None # mypycommands_to_execute = ([self.config.state_command.code]+@@ -675,12 +685,14 @@ class Agent:env.add_commands(command_files)def get_environment_vars(self, env):+ assert self.config is not None # mypyenv_vars = dict()for var in self.config.env_variables:env_vars[var] = env.communicate(f"echo ${var}").strip()return env_varsdef call_subroutine(self, agent_name, sub_action, env):+ assert self.config is not None # mypyenv_vars = self.get_environment_vars(env)cwd = env.communicate("pwd -P").strip()init_observation = self.config._subroutines[agent_name].init_observation@@ -710,9 +722,9 @@ class Agent:def run(self,- setup_args,+ setup_args: Dict[str, Any],env: SWEEnv,- observation: str = None,+ observation: Optional[str] = None,traj_dir: Optional[Path] = None,return_type: Optional[str] = "info",init_model_stats: Optional[APIStats] = None,@@ -722,6 +734,8 @@ class Agent:Return the final value of the specified return type."""done = False+ assert env.container_obj is not None+ assert self.config is not None # mypyif env.container_obj.id != self.last_container_id:logger.info(diff --git a/sweagent/agent/models.py b/sweagent/agent/models.pyindex fdcff884..45740645 100644--- a/sweagent/agent/models.py+++ b/sweagent/agent/models.py@@ -103,7 +103,7 @@ class BaseModel:else:raise ValueError(f"Unregistered model ({args.model_name}). Add model name to MODELS metadata to {self.__class__}")- def reset_stats(self, other: APIStats = None):+ def reset_stats(self, other: Optional[APIStats] = None):if other is None:self.stats = APIStats(total_cost=self.stats.total_cost)logger.info("Resetting model stats")diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex 4a923d18..9f30e59e 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -238,7 +238,7 @@ class SWEEnv(gym.Env):# Write any metadata to info if necessaryreturn None, info- def step(self, action: str) -> Tuple[str, int, bool, dict]:+ def step(self, action: str) -> Tuple[Optional[str], int, bool, dict]:"""Runs given action in environment and returns corresponding output@@ -327,6 +327,8 @@ class SWEEnv(gym.Env):raiseexcept:pass+ assert self.container is not None+ assert self.container_obj is not Noneself.container.terminate()if self.persistent:if self.container_obj.status not in {"paused", "exited"}:@@ -441,7 +443,7 @@ class SWEEnv(gym.Env):self.returncode = int(exit_code)return buffer- def _check_syntax(self, input: str) -> None:+ def _check_syntax(self, input: str):"""Saves environment variables to file"""