@@ -175,9 +175,8 @@ def traverse(
175175 depth = 0
176176 max_depth_limit = 10 # Infinite loop protection limit
177177 max_depth_encountered = 0
178- visited : Set [str ] = {root_node .get_key ()} # Track visited nodes in BFS to prevent cycles
178+ seen_nodes : Set [str ] = {root_node .get_key ()}
179179
180- # Continue BFS to discover all nodes, but stop recording depths after max_depth_limit
181180 while current_level :
182181 next_level : List [AgentGraphNode ] = []
183182 depth += 1
@@ -192,14 +191,14 @@ def traverse(
192191 node_depths [child_key ] = depth
193192 max_depth_encountered = max (max_depth_encountered , depth )
194193 # Add to next level if not already visited (prevents cycles)
195- if child_key not in visited :
196- visited .add (child_key )
194+ if child_key not in seen_nodes :
195+ seen_nodes .add (child_key )
197196 next_level .append (child )
198197 else :
199198 max_depth_encountered = max (max_depth_encountered , depth )
200- if child_key not in visited :
199+ if child_key not in seen_nodes :
201200 # Push this to the next level to be visited
202- visited .add (child_key )
201+ seen_nodes .add (child_key )
203202 next_level .append (child )
204203
205204 current_level = next_level
@@ -209,6 +208,7 @@ def traverse(
209208
210209 # Group all nodes by depth
211210 nodes_by_depth : Dict [int , List [AgentGraphNode ]] = {}
211+ # New visited for children nodes
212212 visited : Set [str ] = set ()
213213
214214 self ._collect_nodes (root_node , node_depths , nodes_by_depth , visited , max_depth )
0 commit comments