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]

Finally fix cache control with anthropic

Kilian Lieret3f35533aa6ea
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index a0a09b0c..72335f5d 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -280,7 +280,7 @@ class Agent:
self._info_by_attempt[self._i_attempt] = value
@property
- def messages(self) -> list[dict[str, str]]:
+ def messages(self) -> list[dict[str, Any]]:
"""Return the history of the agent since the last reset, processed through all history processors."""
filtered_history = [entry for entry in self.history if entry["agent"] == self.name] # type: ignore
diff --git a/sweagent/agent/history_processors.py b/sweagent/agent/history_processors.py
index 921d088c..4241baa8 100644
--- a/sweagent/agent/history_processors.py
+++ b/sweagent/agent/history_processors.py
@@ -7,6 +7,7 @@ from typing import Annotated, Literal, Protocol
from pydantic import BaseModel, ConfigDict, Field
from sweagent.types import History, HistoryItem
+from sweagent.utils.log import get_logger
class AbstractHistoryProcessor(Protocol):
@@ -168,6 +169,9 @@ class CacheControlHistoryProcessor(BaseModel):
If set to <= 0, any set cache control will be removed from all messages.
"""
+ tagged_roles: list[str] = ["user", "tool"]
+ """Only add cache control to messages with these roles."""
+
# pydantic config
model_config = ConfigDict(extra="forbid")
@@ -180,8 +184,8 @@ class CacheControlHistoryProcessor(BaseModel):
def _clear_cache_control(self, entry: HistoryItem) -> None:
if isinstance(entry["content"], list):
assert len(entry["content"]) == 1, "Expected single message in content"
- if "cache_control" in entry["content"][0]:
- del entry["content"][0]["cache_control"]
+ entry["content"][0].pop("cache_control", None)
+ entry.pop("cache_control", None)
def _set_cache_control(self, entry: HistoryItem) -> None:
if not isinstance(entry["content"], list):
@@ -194,17 +198,25 @@ class CacheControlHistoryProcessor(BaseModel):
]
else:
entry["content"][0]["cache_control"] = {"type": "ephemeral"}
+ if entry["role"] == "tool":
+ # Workaruond for weird bug
+ entry["content"][0].pop("cache_control", None)
+ entry["cache_control"] = {"type": "ephemeral"}
def __call__(self, history: History) -> History:
new_history = []
n_tagged = 0
+ logger = get_logger("hp")
for entry in reversed(history):
# Clear cache control from previous messages
self._clear_cache_control(entry)
- if n_tagged < self.last_n_messages and entry["role"] == "user":
+ if n_tagged < self.last_n_messages and entry["role"] in self.tagged_roles:
self._set_cache_control(entry)
+ logger.debug("Tagged message %s", entry)
n_tagged += 1
new_history.append(entry)
+ if n_tagged != self.last_n_messages:
+ logger.warning(f"Expected {self.last_n_messages} messages tagged with cache_control, got {n_tagged}")
return list(reversed(new_history))
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index a3c1e3e8..14f1f3a2 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import copy
import json
import random
import shlex
@@ -609,6 +610,8 @@ class LiteLLMModel(AbstractModel):
self,
history: History,
) -> list[dict[str, str]]:
+ history = copy.deepcopy(history)
+
def get_role(history_item: HistoryItem) -> str:
if history_item["role"] == "system":
return "user" if self.config.convert_system_to_user else "system"
@@ -618,20 +621,21 @@ class LiteLLMModel(AbstractModel):
for history_item in history:
role = get_role(history_item)
if role == "tool":
- messages.append(
- {
- "role": role,
- "content": history_item["content"],
- # Only one tool call per observations
- "tool_call_id": history_item["tool_call_ids"][0], # type: ignore
- }
- )
+ message = {
+ "role": role,
+ "content": history_item["content"],
+ # Only one tool call per observations
+ "tool_call_id": history_item["tool_call_ids"][0], # type: ignore
+ }
elif "tool_calls" in history_item:
- messages.append(
- {"role": role, "content": history_item["content"], "tool_calls": history_item["tool_calls"]}
- )
+ message = {"role": role, "content": history_item["content"], "tool_calls": history_item["tool_calls"]}
else:
- messages.append({"role": role, "content": history_item["content"]})
+ message = {"role": role, "content": history_item["content"]}
+ if "cache_control" in history_item:
+ message["cache_control"] = history_item["cache_control"]
+ messages.append(message)
+ n_cache_control = str(messages).count("cache_control")
+ self.logger.debug(f"n_cache_control: {n_cache_control}")
return messages
diff --git a/sweagent/types.py b/sweagent/types.py
index 759a68aa..357b932f 100644
--- a/sweagent/types.py
+++ b/sweagent/types.py
@@ -66,6 +66,7 @@ class HistoryItem(_HistoryItem, total=False):
tool_calls: list[dict[str, str]] | None
tool_call_ids: list[str] | None
tags: list[str]
+ cache_control: dict[str, Any] | None
"""HistoryProcessors can add these tags to enable special processing"""