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]

Add logger names to loggers

Kilian Lieretd31b47de2395
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 5b4bdfe3..58ed3bef 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -124,7 +124,7 @@ class Agent:
self.tools = tools
self.history_processor = history_processor
self.max_requeries = max_requeries
- self.logger = get_logger("agent", emoji="🤠")
+ self.logger = get_logger(f"agent-{name}", emoji="🤠")
# Set in run method
self._env: SWEEnv | None = None
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index bbd6aada..7d3fc190 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import json
+import random
import time
from abc import ABC, abstractmethod
from pathlib import Path
@@ -282,7 +283,7 @@ class InstantEmptySubmitTestModel(AbstractModel):
self._action_idx = 0
def query(self, history: list[dict[str, str]]) -> str:
- time.sleep(self.config.delay)
+ time.sleep(random.uniform(0, self.config.delay))
# Need to at least do _something_ to submit
if self._action_idx == 0:
self._action_idx = 1
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 21978672..94b10411 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -29,10 +29,10 @@ class EnvironmentConfig(BaseModel):
# pydantic config
model_config = ConfigDict(extra="forbid")
+ name: str = "main"
-class SWEEnv:
- name = "swe_main"
+class SWEEnv:
def __init__(
self,
*,
@@ -40,14 +40,15 @@ class SWEEnv:
repo: Repo | RepoConfig | None,
startup_commands: list[str],
hooks: list[EnvHook] | None = None,
+ name: str = "main",
):
"""This class represents the environment in which we solve the tasks."""
super().__init__()
self.deployment = deployment
self.repo = repo
self._startup_commands = startup_commands
- self.logger = get_logger("swe_env", emoji="🌱")
-
+ self.logger = get_logger(f"env-{name}", emoji="🌱")
+ self.name = name
self.clean_multi_line_functions = lambda x: x
self._chook = CombinedEnvHooks()
for hook in hooks or []:
@@ -59,6 +60,7 @@ class SWEEnv:
deployment=get_deployment(config.deployment),
repo=config.repo,
startup_commands=config.startup_commands,
+ name=config.name,
)
def add_hook(self, hook: EnvHook) -> None:
diff --git a/sweagent/run/run_batch.py b/sweagent/run/run_batch.py
index e1146b11..b9faff92 100644
--- a/sweagent/run/run_batch.py
+++ b/sweagent/run/run_batch.py
@@ -37,7 +37,7 @@ from sweagent.run.common import BasicCLI, save_predictions
from sweagent.run.hooks.abstract import CombinedRunHooks, RunHook
from sweagent.run.hooks.apply_patch import SaveApplyPatchHook
from sweagent.utils.config import load_environment_variables
-from sweagent.utils.log import get_logger
+from sweagent.utils.log import add_logger_names_to_stream_handlers, get_logger
class RunBatchConfig(BaseSettings, cli_implicit_flags=True):
@@ -163,6 +163,7 @@ class RunBatch:
break
def main_multi_worker(self):
+ add_logger_names_to_stream_handlers()
self._main_progress_bar = Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
@@ -270,16 +271,17 @@ class RunBatch:
self._task_progress_bar.update(self._spinner_tasks[instance_id], status=message, instance_id=instance_id)
def _run_instance(self, instance: BatchInstance):
+ self.agent_config.name = f"{instance.problem_statement.id}"
agent = Agent.from_config(self.agent_config)
agent.logger.setLevel(logging.DEBUG if self._num_workers == 1 else logging.WARNING)
agent.add_hook(SetStatusAgentHook(instance.problem_statement.id, self._update_task_progress))
self._update_task_progress(instance.problem_statement.id, "Starting environment")
+ instance.env.name = f"{instance.problem_statement.id}"
env = SWEEnv.from_config(instance.env)
env.add_hook(SetStatusEnvironmentHook(instance.problem_statement.id, self._update_task_progress))
env.logger.setLevel(logging.DEBUG if self._num_workers == 1 else logging.INFO)
env.deployment.logger.setLevel(logging.DEBUG if self._num_workers == 1 else logging.WARNING) # type: ignore
env.start()
- self.logger.info("Running agent")
self._chooks.on_instance_start(index=0, env=env, problem_statement=instance.problem_statement)
try:
result = agent.run(
diff --git a/sweagent/utils/log.py b/sweagent/utils/log.py
index add759a8..f29221a2 100644
--- a/sweagent/utils/log.py
+++ b/sweagent/utils/log.py
@@ -24,6 +24,7 @@ def _interpret_level_from_env(level: str | None, *, default=logging.DEBUG) -> in
_STREAM_LEVEL = _interpret_level_from_env(os.environ.get("SWE_AGENT_LOG_STREAM_LEVEL"))
_FILE_LEVEL = _interpret_level_from_env(os.environ.get("SWE_AGENT_LOG_FILE_LEVEL"), default=logging.TRACE) # type: ignore
+_INCLUDE_LOGGER_NAME_IN_STREAM_HANDLER = False
class _RichHandlerWithEmoji(RichHandler):
@@ -58,6 +59,8 @@ def get_logger(name: str, *, emoji: str = "") -> logging.Logger:
_SET_UP_LOGGERS.add(name)
for handler in _ADDITIONAL_HANDLERS:
logger.addHandler(handler)
+ if _INCLUDE_LOGGER_NAME_IN_STREAM_HANDLER:
+ add_logger_name_to_stream_handler(logger)
return logger
@@ -75,4 +78,18 @@ def add_file_handler(path: PurePath | str) -> None:
_ADDITIONAL_HANDLERS.append(handler)
+def add_logger_name_to_stream_handler(logger: logging.Logger) -> None:
+ for handler in logger.handlers:
+ if isinstance(handler, _RichHandlerWithEmoji):
+ formatter = logging.Formatter("[%(name)s] %(message)s")
+ handler.setFormatter(formatter)
+
+
+def add_logger_names_to_stream_handlers() -> None:
+ global _INCLUDE_LOGGER_NAME_IN_STREAM_HANDLER
+ _INCLUDE_LOGGER_NAME_IN_STREAM_HANDLER = True
+ for logger in _SET_UP_LOGGERS:
+ add_logger_name_to_stream_handler(logging.getLogger(logger))
+
+
default_logger = get_logger("swe-agent")