Bug Summary
The BaseAgent class has a memory leak where agent instances accumulate message history without proper cleanup, causing memory usage to grow unbounded in long-running processes.
Severity: MEDIUM
Bug Details
Location
- File:
/spoon_ai/agents/base.py
- Lines: 40-60 (Memory class and message management)
- Class:
BaseAgent and Memory
Issue Description
The agent memory system accumulates messages indefinitely without any cleanup mechanism:
def add_message(self, role: Literal["user", "assistant", "tool"], content: str, ...):
if role == "user":
self.memory.add_message(Message(role=Role.USER, content=content))
elif role == "assistant":
# ... message creation logic
self.memory.add_message(Message(...))
# BUG: No memory management or cleanup mechanism
Specific Issues Identified
-
Unbounded Memory Growth:
- Messages are added to memory but never removed
- Long-running agents accumulate thousands of messages
- No automatic cleanup based on age or size limits
-
Missing Memory Management Controls:
- No configurable memory limits (message count or size)
- No automatic pruning of old messages
- No memory pressure handling
-
Inefficient Message Storage:
- Full message objects stored regardless of importance
- No compression or summarization of old conversations
- Tool call results stored indefinitely
-
No Memory Analytics:
- No visibility into memory usage patterns
- No metrics for memory growth rates
- No alerts for excessive memory consumption
Impact
- Performance: Degraded performance as memory usage grows
- Reliability: Potential out-of-memory crashes in long-running processes
- Cost: Increased infrastructure costs due to memory requirements
- Scalability: Limited agent lifetime due to memory constraints
Steps to Reproduce
- Create a
BaseAgent instance
- Run continuous conversation for extended period
- Monitor memory usage over time
- Observe unbounded growth in message history
- Note lack of cleanup mechanisms
Expected Behavior
- Configurable memory limits for message history
- Automatic cleanup of old or less important messages
- Memory pressure handling and graceful degradation
- Memory usage monitoring and alerting
Current Behavior
- Unlimited message accumulation
- No memory management controls
- Progressive memory consumption increase
- No cleanup or optimization mechanisms
Fix Required
class Memory:
def __init__(self, max_messages: int = 1000, max_memory_mb: int = 100):
self.messages: List[Message] = []
self.max_messages = max_messages
self.max_memory_mb = max_memory_mb
def add_message(self, message: Message) -> None:
self.messages.append(message)
self._cleanup_if_needed()
def _cleanup_if_needed(self) -> None:
# Remove old messages if over limit
if len(self.messages) > self.max_messages:
# Keep system messages, remove oldest user/assistant messages
self._prune_old_messages()
# Check memory usage and compress if needed
if self._get_memory_usage_mb() > self.max_memory_mb:
self._compress_old_messages()
def _prune_old_messages(self) -> None:
# Implementation for removing old messages while preserving important ones
pass
def _compress_old_messages(self) -> None:
# Implementation for compressing or summarizing old conversations
pass
Business Impact
- Infrastructure Costs: Reduced memory requirements and server costs
- Reliability: Prevention of memory-related crashes
- Performance: Consistent agent performance over time
- Scalability: Support for long-running agent processes
Bug Summary
The
BaseAgentclass has a memory leak where agent instances accumulate message history without proper cleanup, causing memory usage to grow unbounded in long-running processes.Severity: MEDIUM
Bug Details
Location
/spoon_ai/agents/base.pyBaseAgentandMemoryIssue Description
The agent memory system accumulates messages indefinitely without any cleanup mechanism:
Specific Issues Identified
Unbounded Memory Growth:
Missing Memory Management Controls:
Inefficient Message Storage:
No Memory Analytics:
Impact
Steps to Reproduce
BaseAgentinstanceExpected Behavior
Current Behavior
Fix Required
Business Impact