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]

Defensive: Model copies

Kilian Lieretc9a5a7bad287
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index e4872460..acaac6cc 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -201,7 +201,8 @@ def get_agent_from_config(config: AgentConfig) -> AbstractAgent:
class RetryAgent(AbstractAgent):
def __init__(self, config: RetryAgentConfig):
- self.config = config
+ # Always copy config to avoid shared state between different instances
+ self.config = config.model_copy(deep=True)
self._hooks = []
self._i_attempt = 0
self.logger = get_logger("swea-agent", emoji="🤠")
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index ca785dbb..5eaf401d 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -538,7 +538,8 @@ class InstantEmptySubmitTestModel(AbstractModel):
class LiteLLMModel(AbstractModel):
def __init__(self, args: GenericAPIModelConfig, tools: ToolConfig):
"""Model served by the `litellm` library."""
- self.config: GenericAPIModelConfig = args
+ # Always copy config to avoid shared state between different instances
+ self.config: GenericAPIModelConfig = args.model_copy(deep=True)
self.stats = InstanceStats()
self.tools = tools
if tools.use_function_calling:
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 94aa5869..1aaff360 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -79,6 +79,8 @@ class SWEEnv:
This is the recommended way to create an environment instance, unless you need
more flexibility.
"""
+ # Always copy config to avoid shared state between different instances
+ config = config.model_copy(deep=True)
return cls(
deployment=get_deployment(config.deployment),
repo=config.repo,
diff --git a/sweagent/tools/tools.py b/sweagent/tools/tools.py
index 51b442a2..5de24c47 100644
--- a/sweagent/tools/tools.py
+++ b/sweagent/tools/tools.py
@@ -174,7 +174,8 @@ class ToolHandler:
- Decide if an action should be blocked
- Get the current state of the environment
"""
- self.config = tools
+ # Always copy config to avoid shared state between different instances across threads
+ self.config = tools.model_copy(deep=True)
# partially initialized in `install_commands`.
self._reset_commands = []
self._command_patterns = self._get_command_patterns()