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]

Ref: Move swe-agent commit hash info to AgentInfo

Kilian Lieret15a72a2af913
diff --git a/sweagent/__init__.py b/sweagent/__init__.py
index fc6d55eb..c9d88c02 100644
--- a/sweagent/__init__.py
+++ b/sweagent/__init__.py
@@ -1,5 +1,7 @@
from __future__ import annotations
+from git import Repo
+
__version__ = "0.7.0"
from logging import WARNING, getLogger
@@ -17,7 +19,26 @@ CONFIG_DIR = PACKAGE_DIR.parent / "config"
assert CONFIG_DIR.is_dir()
+def get_agent_commit_hash() -> str:
+ repo = Repo(REPO_ROOT, search_parent_directories=True)
+ return repo.head.object.hexsha
+
+
+def get_agent_version_info() -> str:
+ try:
+ hash = get_agent_commit_hash()
+ except Exception:
+ hash = "unknown"
+ return f"This is SWE-agent version {__version__} with commit hash {hash}."
+
+
+getLogger("swe-agent").info(get_agent_version_info())
+
+
__all__ = [
"PACKAGE_DIR",
"CONFIG_DIR",
+ "get_agent_commit_hash",
+ "get_agent_version_info",
+ "__version__",
]
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 829aa222..1055a5ab 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -13,6 +13,7 @@ from pydantic import BaseModel
from simple_parsing.helpers.fields import field
from tenacity import RetryError
+from sweagent import __version__, get_agent_commit_hash
from sweagent.agent.commands import Command, ParseCommand
from sweagent.agent.history_processors import HistoryProcessor
from sweagent.agent.models import (
@@ -968,6 +969,8 @@ class Agent:
self.trajectory = Trajectory()
self._env = env
self.info = AgentInfo()
+ self.info["swe_agent_hash"] = get_agent_commit_hash()
+ self.info["swe_agent_version"] = __version__
self.traj_dir = traj_dir
self.logger.info("Trajectory will be saved to %s", self.traj_path)
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 9414bddf..56dea4e8 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -10,14 +10,11 @@ from pathlib import Path, PurePath
from typing import Any, Literal
import gymnasium as gym
-from git import Repo
from pydantic import BaseModel, Field
from swerex.deployment import get_deployment
from swerex.deployment.abstract import AbstractDeployment
from swerex.runtime.abstract import BashAction, Command, CreateBashSessionRequest, UploadRequest, WriteFileRequest
-from sweagent import REPO_ROOT
-
# from sweagent.agent.interactive_commands import (
# INTERACTIVE_SESSIONS_CONFIG,
# InteractiveSession,
@@ -197,6 +194,7 @@ class EnvHook:
"""Called when the environment is closed"""
+# todo: Do we really need to inherit from gym.Env?
class SWEEnv(gym.Env):
"""Gym environment for SWE-bench. This class should handle all communication with the docker container."""
@@ -209,16 +207,6 @@ class SWEEnv(gym.Env):
self.logger = get_logger("SWEEnv")
self.returncode: None | int = None
- # : The commit hash of the swe-agent repository
- self.commit_sha = None
- try:
- repo = Repo(REPO_ROOT, search_parent_directories=True)
- self.commit_sha = repo.head.object.hexsha
- except KeyboardInterrupt:
- raise
- except Exception as e:
- self.logger.exception("Failed to get commit hash for this repo: %s", str(e))
-
# Establish connection with execution container
# self.docker_compose: Path | None = None
# self.challenge: dict[str, Any] | None = None
@@ -280,7 +268,6 @@ class SWEEnv(gym.Env):
info: additional information (e.g. debugging information)
"""
info = {}
- info["commit_sha"] = self.commit_sha
### Reset Container ###
# self._init_docker_compose()
diff --git a/sweagent/types.py b/sweagent/types.py
index b07bd9d9..5a3964f1 100644
--- a/sweagent/types.py
+++ b/sweagent/types.py
@@ -51,6 +51,8 @@ class AgentInfo(TypedDict, total=False):
edited_files70: str
# only if summarizer is used
summarizer: dict
+ swe_agent_hash: str
+ swe_agent_version: str
@dataclass