Fix bug in GraphFlow cycle check (#6629)

Resolve #6628
This commit is contained in:
Eric Zhu 2025-06-04 21:35:27 -07:00 committed by GitHub
parent 796e349cc3
commit 4358dfd5c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 97 deletions

View File

@ -159,7 +159,7 @@ class DiGraph(BaseModel):
cycle_edges: List[DiGraphEdge] = []
for n in cycle_nodes:
cycle_edges.extend(self.nodes[n].edges)
if not any(edge.condition is not None for edge in cycle_edges):
if all(edge.condition is None and edge.condition_function is None for edge in cycle_edges):
raise ValueError(
f"Cycle detected without exit condition: {' -> '.join(cycle_nodes + cycle_nodes[:1])}"
)

View File

@ -219,6 +219,18 @@ def test_cycle_detection_with_exit_condition() -> None:
)
assert graph.has_cycles_with_exit()
# Use a lambda condition
graph_with_lambda = DiGraph(
nodes={
"A": DiGraphNode(name="A", edges=[DiGraphEdge(target="B")]),
"B": DiGraphNode(name="B", edges=[DiGraphEdge(target="C")]),
"C": DiGraphNode(
name="C", edges=[DiGraphEdge(target="A", condition=lambda msg: "test" in msg.to_model_text())]
), # Cycle with lambda
}
)
assert graph_with_lambda.has_cycles_with_exit()
def test_cycle_detection_without_exit_condition() -> None:
"""Test that cycle without exit condition raises an error."""

File diff suppressed because one or more lines are too long