Closed
Description
Issue Description
The functions get_all_nodes
and get_all_edges
in src/agents/extensions/visualization.py
can lead to infinite recursion when there are circular references between agents (when agents form a cycle through handoffs).
Current Behavior
When agents have circular references through handoffs, the visualization functions will recursively traverse the agent graph without any cycle detection, leading to infinite recursion and eventual stack overflow.
Expected Behavior
The visualization functions should handle circular references gracefully by detecting cycles and preventing repeated traversal of the same agents.
Proposed Solution
Add a visited
set to track already visited agents and prevent repeated recursion. Here's a sketch of the fix:
def get_all_nodes(agent: Agent, parent: Optional[Agent] = None, visited: Optional[set] = None) -> str:
if visited is None:
visited = set()
if agent.name in visited: # Prevent infinite recursion
return ""
visited.add(agent.name)
# ... rest of the function ...
Similar changes would be needed for get_all_edges
.