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: Always write log to traj directory (#588)
Kilian Lieretce98ca442e68
diff --git a/run.py b/run.pyindex ce5232cc..e2c4629e 100644--- a/run.py+++ b/run.py@@ -3,7 +3,7 @@ from __future__ import annotationsimport loggingfrom sweagent import CONFIG_DIR-from sweagent.utils.log import get_logger+from sweagent.utils.log import add_file_handler, get_loggertry:import rich@@ -29,6 +29,7 @@ try:except ImportError:msg = "Please install the rich_argparse package with `pip install rich_argparse`."raise ImportError(msg)+import datetimefrom dataclasses import dataclassfrom getpass import getuserfrom pathlib import Path@@ -109,7 +110,7 @@ class ScriptArguments(FlattenedAccess, FrozenSerializable):print_config: bool = True@property- def run_name(self):+ def run_name(self) -> str:"""Generate a unique name for this run based on the arguments."""model_name = self.agent.model.model_name.replace(":", "-")data_stem = get_data_path_name(self.environment.data_path)@@ -306,13 +307,17 @@ class OpenPRHook(MainHook):class Main:def __init__(self, args: ScriptArguments):+ self.traj_dir = Path("trajectories") / Path(getuser()) / args.run_name+ self.traj_dir.mkdir(parents=True, exist_ok=True)+ timestamp = datetime.datetime.now().strftime("%y%m%d%H%M%S")+ log_path = self.traj_dir / f"run-{timestamp}.log"+ logger.info("Logging to %s", log_path)+ add_file_handler(log_path)if args.print_config:logger.info(f"📙 Arguments: {args.dumps_yaml()}")self.args = argsself.agent = Agent("primary", args.agent)self.env = SWEEnv(args.environment)- self.traj_dir = Path("trajectories") / Path(getuser()) / args.run_name- self.traj_dir.mkdir(parents=True, exist_ok=True)self._save_arguments()default_hooks = [SaveApplyPatchHook(),diff --git a/sweagent/utils/log.py b/sweagent/utils/log.pyindex 1e2a7987..5313d768 100644--- a/sweagent/utils/log.py+++ b/sweagent/utils/log.py@@ -2,13 +2,18 @@ from __future__ import annotationsimport loggingimport os+from pathlib import PurePathfrom rich.logging import RichHandler_SET_UP_LOGGERS = set()+_ADDITIONAL_HANDLERS = []def get_logger(name: str) -> logging.Logger:+ """Get logger. Use this instead of `logging.getLogger` to ensure+ that the logger is set up with the correct handlers.+ """logger = logging.getLogger(name)if name in _SET_UP_LOGGERS:# Already set up@@ -22,7 +27,23 @@ def get_logger(name: str) -> logging.Logger:logger.addHandler(handler)logger.propagate = False_SET_UP_LOGGERS.add(name)+ for handler in _ADDITIONAL_HANDLERS:+ logger.addHandler(handler)return logger+def add_file_handler(path: PurePath | str) -> None:+ """Adds a file handler to all loggers that we have set up+ and all future loggers that will be set up with `get_logger`.+ """+ handler = logging.FileHandler(path)+ formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")+ handler.setFormatter(formatter)+ handler.setLevel(logging.DEBUG)+ for name in _SET_UP_LOGGERS:+ logger = logging.getLogger(name)+ logger.addHandler(handler)+ _ADDITIONAL_HANDLERS.append(handler)++default_logger = get_logger("swe-agent")