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]

Fix: multi-threading save way to track total cost; hook for tools install

Kilian Lierete5dc7a56ff53
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 8e69fa84..71e0e877 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -18,10 +18,10 @@ from sweagent.agent.history_processors import DefaultHistoryProcessor, HistoryPr
from sweagent.agent.hooks.abstract import AbstractAgentHook, CombinedAgentHook
from sweagent.agent.models import (
AbstractModel,
- APIStats,
ContextWindowExceededError,
CostLimitExceededError,
HumanModel,
+ InstanceStats,
ModelConfig,
get_model,
)
@@ -685,7 +685,7 @@ class Agent:
self.info["submission"] = step_output.submission
self.info["exit_status"] = step_output.exit_status # type: ignore
self.info.update(self._get_edited_files_with_context(patch=step_output.submission or "")) # type: ignore
- model_stats: APIStats = self.model.stats
+ model_stats: InstanceStats = self.model.stats
self.info["model_stats"] = model_stats.model_dump()
self.add_step_to_trajectory(step_output)
diff --git a/sweagent/agent/hooks/abstract.py b/sweagent/agent/hooks/abstract.py
index 3cc59164..ca6db3d7 100644
--- a/sweagent/agent/hooks/abstract.py
+++ b/sweagent/agent/hooks/abstract.py
@@ -45,6 +45,8 @@ class AbstractAgentHook:
def on_setup_done(self): ...
+ def on_tools_installation_started(self): ...
+
class CombinedAgentHook(AbstractAgentHook):
def __init__(self, hooks: list[AbstractAgentHook] | None = None):
@@ -100,3 +102,10 @@ class CombinedAgentHook(AbstractAgentHook):
hook.on_query_message_added(
role=role, content=content, agent=agent, is_demo=is_demo, thought=thought, action=action
)
+
+ def on_setup_done(self):
+ return super().on_setup_done()
+
+ def on_tools_installation_started(self):
+ for hook in self.hooks:
+ hook.on_tools_installation_started()
diff --git a/sweagent/agent/hooks/status.py b/sweagent/agent/hooks/status.py
index 340d9ac2..1e217a39 100644
--- a/sweagent/agent/hooks/status.py
+++ b/sweagent/agent/hooks/status.py
@@ -20,3 +20,6 @@ class SetStatusAgentHook(AbstractAgentHook):
def on_step_done(self, *, step: StepOutput, info: AgentInfo):
self._cost = info.get("cost", 0.0)
+
+ def on_tools_installation_started(self):
+ self._update("Installing tools...")
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index 7d3fc190..c5dd30aa 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -5,6 +5,7 @@ import random
import time
from abc import ABC, abstractmethod
from pathlib import Path
+from threading import Lock
from typing import Annotated, Any, Literal
import litellm
@@ -115,17 +116,23 @@ ModelConfig = Annotated[
]
-class APIStats(PydanticBaseModel):
+class GlobalStats(PydanticBaseModel):
total_cost: float = 0
"""Cumulative cost for all instances so far"""
+
+GLOBAL_STATS = GlobalStats()
+GLOBAL_STATS_LOCK = Lock()
+
+
+class InstanceStats(PydanticBaseModel):
instance_cost: float = 0
tokens_sent: int = 0
tokens_received: int = 0
api_calls: int = 0
- def __add__(self, other: APIStats) -> APIStats:
- return APIStats(
+ def __add__(self, other: InstanceStats) -> InstanceStats:
+ return InstanceStats(
**{field: getattr(self, field) + getattr(other, field) for field in self.model_fields.keys()},
)
@@ -149,10 +156,10 @@ class TotalCostLimitExceededError(CostLimitExceededError):
class AbstractModel(ABC):
def __init__(self, config: PydanticBaseModel, commands: list[Command]):
self.config: PydanticBaseModel
- self.stats: APIStats
+ self.stats: InstanceStats
def reset_stats(self):
- self.stats = APIStats()
+ self.stats = InstanceStats()
@abstractmethod
def query(self, history: History, action_prompt: str = "> ") -> str: ...
@@ -161,7 +168,7 @@ class AbstractModel(ABC):
class HumanModel(AbstractModel):
def __init__(self, config: HumanModelConfig, commands: list[Command]):
self.config = config
- self.stats = APIStats()
+ self.stats = InstanceStats()
# Determine which commands require multi-line input
self.multi_line_command_endings = {
@@ -216,7 +223,7 @@ class HumanThoughtModel(HumanModel):
class ReplayModel(AbstractModel):
def __init__(self, config: ReplayModelConfig, commands: list[Command]):
self.config = config
- self.stats = APIStats()
+ self.stats = InstanceStats()
if not self.config.replay_path.exists():
msg = f"Replay file {self.config.replay_path} not found"
@@ -261,7 +268,7 @@ class PredeterminedTestModel(AbstractModel):
def __init__(self, outputs: list[str]):
self._outputs = outputs
self._idx = -1
- self.stats = APIStats()
+ self.stats = InstanceStats()
def query(self, *args, **kwargs) -> str:
self._idx += 1
@@ -279,7 +286,7 @@ class InstantEmptySubmitTestModel(AbstractModel):
def __init__(self, args: InstantEmptySubmitModelConfig, commands: list[Command]):
"""This model immediately submits. Useful for testing purposes"""
self.config: InstantEmptySubmitModelConfig = args
- self.stats = APIStats()
+ self.stats = InstanceStats()
self._action_idx = 0
def query(self, history: list[dict[str, str]]) -> str:
@@ -299,10 +306,11 @@ class LiteLLMModel(AbstractModel):
def __init__(self, args: ModelConfig, commands: list[Command]):
self.args = args
self.commands = commands
- self.stats = APIStats()
+ self.stats = InstanceStats()
def _update_stats(self, *, input_tokens: int, output_tokens: int, cost: float) -> None:
- self.stats.total_cost += cost
+ with GLOBAL_STATS_LOCK:
+ GLOBAL_STATS.total_cost += cost
self.stats.instance_cost += cost
self.stats.tokens_sent += input_tokens
self.stats.tokens_received += output_tokens
@@ -318,13 +326,13 @@ class LiteLLMModel(AbstractModel):
logger.debug(
f"total_tokens_sent={self.stats.tokens_sent:,}, "
f"total_tokens_received={self.stats.tokens_received:,}, "
- f"total_cost={self.stats.total_cost:.2f}, "
+ f"total_cost={GLOBAL_STATS.total_cost:.2f}, "
f"total_api_calls={self.stats.api_calls:,}",
)
# Check whether total cost or instance cost limits have been exceeded
- if 0 < self.args.total_cost_limit <= self.stats.total_cost:
- logger.warning(f"Cost {self.stats.total_cost:.2f} exceeds limit {self.args.total_cost_limit:.2f}")
+ if 0 < self.args.total_cost_limit <= GLOBAL_STATS.total_cost:
+ logger.warning(f"Cost {GLOBAL_STATS.total_cost:.2f} exceeds limit {self.args.total_cost_limit:.2f}")
msg = "Total cost limit exceeded"
raise TotalCostLimitExceededError(msg)