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]
Fix: TagToolCallObservations
Kilian Lierete7495492467d
diff --git a/sweagent/agent/history_processors.py b/sweagent/agent/history_processors.pyindex 7f4eff99..b82ea6f4 100644--- a/sweagent/agent/history_processors.py+++ b/sweagent/agent/history_processors.py@@ -6,6 +6,8 @@ from typing import Annotated, Literal, Protocolfrom pydantic import BaseModel, ConfigDict, Field+from sweagent.types import History, HistoryItem+class FormatError(Exception):pass@@ -13,7 +15,7 @@ class FormatError(Exception):class AbstractHistoryProcessor(Protocol):@abstractmethod- def __call__(self, history: list[str]) -> list[str]:+ def __call__(self, history: History) -> History:raise NotImplementedError@@ -24,7 +26,7 @@ class DefaultHistoryProcessor(BaseModel):# pydantic configmodel_config = ConfigDict(extra="forbid")- def __call__(self, history):+ def __call__(self, history: History) -> History:return history@@ -50,7 +52,7 @@ class LastNObservations(BaseModel):# pydantic configmodel_config = ConfigDict(extra="forbid")- def __call__(self, history):+ def __call__(self, history: History) -> History:if self.n <= 0:msg = "n must be a positive integer"raise ValueError(msg)@@ -92,21 +94,21 @@ class TagToolCallObservations(BaseModel):# pydantic configmodel_config = ConfigDict(extra="forbid")- def _add_tags(self, entry: dict) -> None:+ def _add_tags(self, entry: HistoryItem) -> None:tags = set(entry.get("tags", []))- tags.add(self.tags)+ tags.update(self.tags)entry["tags"] = list(tags)- def _should_add_tags(self, entry: dict) -> bool:+ def _should_add_tags(self, entry: HistoryItem) -> bool:if entry["message_type"] != "action":return False- function_calls = entry.get("function_calls", [])+ function_calls = entry.get("tool_calls", [])if not function_calls:return Falsefunction_names = {call["function"]["name"] for call in function_calls}return bool(self.function_names & function_names)- def __call__(self, history) -> None:+ def __call__(self, history: History) -> History:for entry in history:if self._should_add_tags(entry):self._add_tags(entry)diff --git a/sweagent/types.py b/sweagent/types.pyindex 73dff7c8..c006cccd 100644--- a/sweagent/types.py+++ b/sweagent/types.py@@ -50,18 +50,23 @@ class TrajectoryStep(TypedDict):messages: list[dict[str, Any]]+# required fields go hereclass _HistoryItem(TypedDict):role: strcontent: str+ message_type: Literal["thought", "action", "observation"]+# see _HistoryItem for required fieldsclass HistoryItem(_HistoryItem, total=False):agent: stris_demo: boolthought: straction: str | None- tool_calls: list[dict[str, str]] | None = None- tool_call_ids: list[str] | None = None+ tool_calls: list[dict[str, str]] | None+ tool_call_ids: list[str] | None+ tags: list[str]+ """HistoryProcessors can add these tags to enable special processing"""History = list[HistoryItem]diff --git a/tests/test_history_processors.py b/tests/test_history_processors.pyindex 3536fa0f..20b0b514 100644--- a/tests/test_history_processors.py+++ b/tests/test_history_processors.py@@ -1,25 +1,40 @@import jsonfrom pathlib import Path-from sweagent.agent.history_processors import LastNObservations+import pytest++from sweagent.agent.history_processors import LastNObservations, TagToolCallObservations+from sweagent.types import Historydef get_history(traj_path: Path):return json.loads((traj_path).read_text())["history"]-def count_elided_observations(history: list[dict]):+def count_elided_observations(history: History):return len([entry for entry in history if "Old environment output" in entry["content"]])-def test_last_n_observations(test_trajectories_path: Path):- history = get_history(+def test_history(test_trajectories_path: Path):+ return get_history(test_trajectories_path/ "gpt4__swe-agent-test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/6e44b9__sweagenttestrepo-1c2844.traj")+++def test_last_n_observations(test_history: History):processor = LastNObservations(n=3)- new_history = processor(history)- total_observations = len([entry for entry in history if entry["message_type"] == "observation"])+ new_history = processor(test_history)+ total_observations = len([entry for entry in test_history if entry["message_type"] == "observation"])# extra -1 because instance template is keptexpected_elided_observations = total_observations - 3 - 1assert count_elided_observations(new_history) == expected_elided_observations+++def test_add_tag_to_edits(test_history: History):+ processor = TagToolCallObservations(tags={"test"}, function_names={"edit"})+ new_history = processor(test_history)+ for entry in new_history:+ if entry.get("action", "").startswith("edit "):+ assert entry.get("tags") == ["test"], entry