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]
Add file handlers to "run" logs
Kilian Lieret7bde63dde335
diff --git a/sweagent/run/run_batch.py b/sweagent/run/run_batch.pyindex 95d226e6..50ca9ff8 100644--- a/sweagent/run/run_batch.py+++ b/sweagent/run/run_batch.py@@ -91,6 +91,11 @@ class RunBatch:num_workers: Number of parallel workers to use. Default is 1 (sequential execution)."""self.logger = get_logger("swea-run", emoji="🏃")+ add_file_handler(+ output_dir / "run_batch.log",+ id_="progress",+ filter=lambda name: "swea-run" in name or "config" in name,+ )self.instances = instancesself.agent_config = agent_configself.output_dir = output_dir@@ -101,7 +106,6 @@ class RunBatch:self._num_workers = min(num_workers, len(instances))for hook in hooks or [SaveApplyPatchHook()]:self.add_hook(hook)-self._progress_manager = RunBatchProgressManager(num_instances=len(instances))@classmethoddiff --git a/sweagent/run/run_single.py b/sweagent/run/run_single.pyindex 4175a338..b061d5a8 100644--- a/sweagent/run/run_single.py+++ b/sweagent/run/run_single.py@@ -21,7 +21,7 @@ from sweagent.run.hooks.abstract import CombinedRunHooks, RunHookfrom sweagent.run.hooks.apply_patch import SaveApplyPatchHookfrom sweagent.run.hooks.open_pr import OpenPRConfig, OpenPRHookfrom sweagent.utils.config import load_environment_variables-from sweagent.utils.log import get_logger+from sweagent.utils.log import add_file_handler, get_loggerclass RunSingleActionConfig(BaseModel, cli_implicit_flags=True):@@ -82,6 +82,14 @@ class RunSingle:See `from_config` for an example."""self.logger = get_logger("swea-run", emoji="🏃")+ instance_id = problem_statement.id+ _log_filename_template = f"{instance_id}.{{level}}.log"+ for level in ["trace", "debug", "info"]:+ add_file_handler(+ output_dir / _log_filename_template.format(level=level),+ level=level,+ id_=f"{instance_id}-{level}",+ )self.env = envself.agent = agentself.output_dir = output_dirdiff --git a/sweagent/utils/log.py b/sweagent/utils/log.pyindex e1657901..4d850081 100644--- a/sweagent/utils/log.py+++ b/sweagent/utils/log.py@@ -4,6 +4,7 @@ import loggingimport osimport threadingimport uuid+from collections.abc import Callablefrom pathlib import PurePathfrom rich.logging import RichHandler@@ -75,19 +76,33 @@ def get_logger(name: str, *, emoji: str = "") -> logging.Logger:logger.propagate = False_SET_UP_LOGGERS.add(name)for handler in _ADDITIONAL_HANDLERS.values():- if getattr(handler, "my_filter", "") in name:+ my_filter = getattr(handler, "my_filter", None)+ if my_filter is None:+ logger.addHandler(handler)+ elif isinstance(my_filter, str) and my_filter in name:+ logger.addHandler(handler)+ elif callable(my_filter) and my_filter(name):logger.addHandler(handler)if _INCLUDE_LOGGER_NAME_IN_STREAM_HANDLER:_add_logger_name_to_stream_handler(logger)return logger-def add_file_handler(path: PurePath | str, *, filter: str = "", level: int | str = _FILE_LEVEL, id_: str = "") -> str:+def add_file_handler(+ path: PurePath | str,+ *,+ filter: str | Callable[[str], bool] | None = None,+ level: int | str = _FILE_LEVEL,+ id_: str = "",+) -> str:"""Adds a file handler to all loggers that we have set upand all future loggers that will be set up with `get_logger`.Args:- filter: If provided, only add the handler to loggers that contain the filter string.+ filter: If str: Check that the logger name contains the filter string.+ If callable: Check that the logger name satisfies the condition returned by the callable.+ level: The level of the handler.+ id_: The id of the handler. If not provided, a random id will be generated.Returns:The id of the handler. This can be used to remove the handler later.@@ -97,8 +112,11 @@ def add_file_handler(path: PurePath | str, *, filter: str = "", level: int | strhandler.setFormatter(formatter)handler.setLevel(_interpret_level(level))for name in _SET_UP_LOGGERS:- if filter and filter not in name:- continue+ if filter is not None:+ if isinstance(filter, str) and filter not in name:+ continue+ if callable(filter) and not filter(name):+ continuelogger = logging.getLogger(name)logger.addHandler(handler)handler.my_filter = filter # type: ignore