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]

Update replay_config saving to use paths relative to REPO_ROOT

carlosejimenez27a024079274
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index ac02366c..78f57068 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -34,7 +34,7 @@ from sweagent.tools.parsing import (
)
from sweagent.tools.tools import ToolConfig, ToolHandler
from sweagent.types import AgentInfo, AgentRunResult, History, StepOutput, Trajectory, TrajectoryStep
-from sweagent.utils.config import _convert_paths_to_abspath
+from sweagent.utils.config import _convert_paths_to_abspath, _strip_abspath_from_dict
from sweagent.utils.log import get_logger
from sweagent.utils.patch_formatter import PatchFormatter
@@ -161,7 +161,7 @@ class Agent:
self._chook = CombinedAgentHook()
- self.replay_config: BaseModel | None = None
+ self._replay_config: BaseModel | None = None
"""This can be set to a RunSingleConfig from the Run instance whenever possible.
It can be used to replay the agent's trajectory in an environment.
"""
@@ -185,6 +185,14 @@ class Agent:
# Properties
# ----------
+ @property
+ def replay_config(self) -> dict | None:
+ return self._replay_config
+
+ @replay_config.setter
+ def replay_config(self, value: BaseModel | None):
+ self._replay_config = _strip_abspath_from_dict(value.model_dump(mode="json")) if value else None
+
@property
def history(self) -> History:
"""History that is passed on to the model.
@@ -425,21 +433,21 @@ class Agent:
assert self._env is not None
# The deepcopy here is important because else the
# data["info"]["model_stats"] update will create havoc!
- return copy.deepcopy(
+ attempt_data = copy.deepcopy(
{
- "environment": self._env.name,
"trajectory": self._trajectory_by_attempt[attempt_idx],
"history": self._history_by_attempt[attempt_idx],
"info": self._info_by_attempt[attempt_idx],
}
)
+ attempt_data["replay_config"] = self.replay_config
+ attempt_data["environment"] = self._env.name
+ return attempt_data
data = {
**get_attempt_data(0),
}
- data["replay_config"] = self.replay_config.model_dump(mode="json") if self.replay_config else None
-
assert self.traj_path is not None
self.traj_path.write_text(json.dumps(data, indent=2))
@@ -508,7 +516,10 @@ class Agent:
assert self.tools is not None
submission = self.tools.parse_submission_cmd_output(observation or step.observation)
if submission is not None:
- step.submission = submission
+ if submission.strip() != "":
+ step.submission = None
+ else:
+ step.submission = None
step.observation = submission
if not step.exit_status:
step.exit_status = "submitted"
diff --git a/sweagent/utils/config.py b/sweagent/utils/config.py
index 638d8cc9..fc59c6be 100644
--- a/sweagent/utils/config.py
+++ b/sweagent/utils/config.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import os
from pathlib import Path
+from typing import Any
from dotenv import load_dotenv
@@ -11,6 +12,33 @@ from sweagent.utils.log import get_logger
logger = get_logger("swea-config", emoji="🔧")
+def _convert_path_relative_to_repo_root(path: Path | str, root: Path | None = None) -> Path | str:
+ original_type = type(path)
+ path = Path(path).resolve()
+ root = Path(root or os.getenv("SWE_AGENT_CONFIG_ROOT", REPO_ROOT))
+ relative_path = path.relative_to(root) if root in path.parents else path
+ return relative_path if original_type is Path else str(relative_path)
+
+
+def _could_be_a_path(v: Any) -> bool:
+ try:
+ return Path(v).exists()
+ except Exception:
+ return False
+
+
+def _strip_abspath_from_dict(value: dict | list | str, root: Path | None = None) -> dict | list | str:
+ root = Path(root or os.getenv("SWE_AGENT_CONFIG_ROOT", REPO_ROOT))
+ if isinstance(value, dict):
+ return {k: _strip_abspath_from_dict(v, root) for k, v in value.items()}
+ elif isinstance(value, list):
+ return [_strip_abspath_from_dict(v, root) for v in value]
+ elif isinstance(value, str) and _could_be_a_path(value):
+ return _convert_path_relative_to_repo_root(value, root)
+ else:
+ return value
+
+
def _convert_path_to_abspath(path: Path | str) -> Path:
"""If path is not absolute, convert it to an absolute path
using the SWE_AGENT_CONFIG_ROOT environment variable (if set) or