Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def propagate_skip_from_edge(self, edge_id: str) -> None:

# If any edge is taken, node may still execute
if edge_states["has_taken"]:
# Enqueue node
# Start execution and enqueue node
print(f"Starting execution for node: {downstream_node_id}")
self._state_manager.start_execution(downstream_node_id)
self._state_manager.enqueue_node(downstream_node_id)
return
Comment on lines +61 to 65
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove debug print statement before merging.

The print() statement on line 62 appears to be debug output that should not be in production code. It will pollute logs/stdout during workflow execution.

🧹 Proposed fix
         # If any edge is taken, node may still execute
         if edge_states["has_taken"]:
             # Start execution and enqueue node
-            print(f"Starting execution for node: {downstream_node_id}")
             self._state_manager.start_execution(downstream_node_id)
             self._state_manager.enqueue_node(downstream_node_id)
             return
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Start execution and enqueue node
print(f"Starting execution for node: {downstream_node_id}")
self._state_manager.start_execution(downstream_node_id)
self._state_manager.enqueue_node(downstream_node_id)
return
# Start execution and enqueue node
self._state_manager.start_execution(downstream_node_id)
self._state_manager.enqueue_node(downstream_node_id)
return
🤖 Prompt for AI Agents
In `@api/core/workflow/graph_engine/graph_traversal/skip_propagator.py` around
lines 61 - 65, Remove the debug print statement that logs downstream_node_id
before starting execution; in the method that calls
self._state_manager.start_execution(downstream_node_id) and
self._state_manager.enqueue_node(downstream_node_id) (the skip propagator logic
in skip_propagator.py), delete the print(f"Starting execution for node:
{downstream_node_id}") so only the state manager calls remain and no debug
stdout is emitted in production.


Expand All @@ -79,9 +81,9 @@ def _propagate_skip_to_node(self, node_id: str) -> None:
# Mark all outgoing edges as skipped and propagate
outgoing_edges = self._graph.get_outgoing_edges(node_id)
for edge in outgoing_edges:
self._state_manager.mark_edge_skipped(edge.id)
# Recursively propagate skip
self.propagate_skip_from_edge(edge.id)
self._state_manager.mark_edge_skipped(edge.id)
Comment on lines 83 to +86
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# First, let's examine the skip_propagator.py file to understand the implementation
cat -n api/core/workflow/graph_engine/graph_traversal/skip_propagator.py | head -150

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 3859


🏁 Script executed:

#!/bin/bash
# Look at the specific lines in question (83-86) and surrounding context
cat -n api/core/workflow/graph_engine/graph_traversal/skip_propagator.py | sed -n '50,120p'

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 2240


🏁 Script executed:

#!/bin/bash
# Search for test files related to skip propagation
fd -t f '*test*.py' | xargs rg -l 'skip.*propagat|propagat.*skip' 2>/dev/null | head -10

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 741


🏁 Script executed:

#!/bin/bash
# Let's examine the test file for skip propagator
cat -n api/tests/unit_tests/core/workflow/graph_engine/graph_traversal/test_skip_propagator.py

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 13929


🏁 Script executed:

#!/bin/bash
# Look at the graph state manager to understand how analyze_edge_states works
rg -n 'analyze_edge_states|mark_edge_skipped' api/core/workflow/graph_engine/graph_state_manager.py -A 10 | head -80

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 740


🏁 Script executed:

#!/bin/bash
# Check how edge state is initialized and what the default state is
rg -n 'class NodeState|NodeState\.' api/core/workflow/ -A 3 | head -40

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 3014


🏁 Script executed:

#!/bin/bash
# Check initialization of edge states in GraphStateManager
rg -n 'class GraphStateManager|def __init__|edge.*state' api/core/workflow/graph_engine/graph_state_manager.py -B 2 -A 5 | head -80

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 2477


🏁 Script executed:

#!/bin/bash
# Check Edge class initialization to confirm state defaults
rg -n 'class Edge|self\.state.*=.*NodeState' api/core/workflow/graph.py -A 3 | head -40

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 149


🏁 Script executed:

#!/bin/bash
# Look at the git history or comments around this change
cat -n api/core/workflow/graph_engine/graph_traversal/skip_propagator.py | sed -n '70,100p'

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 1268


🏁 Script executed:

#!/bin/bash
# Search for any other test files that might test integration behavior
fd -type f -name '*.py' | xargs grep -l '_propagate_skip_to_node\|skip_propagator' | grep -i test

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 253


🏁 Script executed:

#!/bin/bash
# Find the Graph class definition
find api -name '*.py' -type f | xargs grep -l 'class Graph' | head -5

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 299


🏁 Script executed:

#!/bin/bash
# Search for Edge class initialization
find api -name '*.py' -type f | xargs grep -l 'class Edge' | head -5

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 233


🏁 Script executed:

#!/bin/bash
# Examine the Edge class
cat -n api/core/workflow/graph/edge.py | head -50

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 674


🏁 Script executed:

#!/bin/bash
# Let me verify this is indeed the problematic scenario by checking if there's a comment in the code explaining the order
cat -n api/core/workflow/graph_engine/graph_traversal/skip_propagator.py | sed -n '71,87p'

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 807


🏁 Script executed:

#!/bin/bash
# Check if there are any integration/end-to-end tests that would actually catch this issue
find api/tests -name '*.py' -type f | xargs grep -l 'propagate_skip\|skip_propagator'

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 170


🏁 Script executed:

#!/bin/bash
# Let me see if there are any comments or documentation about why this order was chosen
git log --oneline -n 20 api/core/workflow/graph_engine/graph_traversal/skip_propagator.py 2>/dev/null || echo "Git not available"

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 101


🏁 Script executed:

#!/bin/bash
# Check if there are any integration tests in the workflow engine that test end-to-end behavior
find api/tests -name '*.py' -type f | xargs grep -l 'graph_engine\|workflow.*execution' | head -5

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 559


🏁 Script executed:

#!/bin/bash
# Let me create a simple Python script to trace through the logic and confirm the bug
python3 << 'EOF'
from enum import Enum

class NodeState(Enum):
    UNKNOWN = "unknown"
    SKIPPED = "skipped"
    TAKEN = "taken"

def analyze_edge_states(edges):
    """Simulates the analyze_edge_states function"""
    states = {edge['state'] for edge in edges}
    
    return {
        "has_unknown": NodeState.UNKNOWN in states,
        "has_taken": NodeState.TAKEN in states,
        "all_skipped": states == {NodeState.SKIPPED} if states else True,
    }

# Scenario: propagate_skip_from_edge is called BEFORE mark_edge_skipped
print("=== SCENARIO 1: propagate_skip_from_edge called BEFORE mark_edge_skipped ===")
print("(Current buggy implementation in _propagate_skip_to_node)")

# Edge state is still UNKNOWN when propagate_skip_from_edge is called
incoming_edges = [
    {'state': NodeState.UNKNOWN}  # Edge being processed - NOT marked yet
]

edge_states = analyze_edge_states(incoming_edges)
print(f"Edge states: {edge_states}")
print(f"has_unknown={edge_states['has_unknown']} -> Early return at line 57")
print(f"Propagation STOPS prematurely\n")

# Scenario 2: mark_edge_skipped is called BEFORE propagate_skip_from_edge
print("=== SCENARIO 2: mark_edge_skipped called BEFORE propagate_skip_from_edge ===")
print("(Correct implementation in skip_branch_paths)")

# Edge state is SKIPPED when propagate_skip_from_edge is called
incoming_edges = [
    {'state': NodeState.SKIPPED}  # Edge was marked first
]

edge_states = analyze_edge_states(incoming_edges)
print(f"Edge states: {edge_states}")
print(f"all_skipped={edge_states['all_skipped']} -> Continues to propagate skip")
print(f"Propagation continues correctly")
EOF

Repository: qodo-benchmark/dify-combined-coderabbit

Length of output: 649


Swap the order: mark edge as skipped before recursive propagation.

At lines 85-86, propagate_skip_from_edge(edge.id) is called before mark_edge_skipped(edge.id). This causes the edge to remain in UNKNOWN state during the recursive call. When propagate_skip_from_edge calls analyze_edge_states on the downstream node's incoming edges, it will see has_unknown=True (because this edge hasn't been marked yet) and return early at line 57, stopping propagation prematurely.

In contrast, skip_branch_paths (lines 95-97) correctly marks the edge first before propagating. Swap the order to match that pattern:

for edge in outgoing_edges:
    self._state_manager.mark_edge_skipped(edge.id)
    self.propagate_skip_from_edge(edge.id)
🤖 Prompt for AI Agents
In `@api/core/workflow/graph_engine/graph_traversal/skip_propagator.py` around
lines 83 - 86, The loop in propagate_skip_from_edge marks edges after recursing
which leaves them UNKNOWN during downstream analysis; change the order so the
edge is marked skipped before recursion: in propagate_skip_from_edge's
outgoing_edges loop call self._state_manager.mark_edge_skipped(edge.id) first,
then call self.propagate_skip_from_edge(edge.id), mirroring the pattern used in
skip_branch_paths and ensuring analyze_edge_states on downstream nodes sees the
edge as SKIPPED.


def skip_branch_paths(self, unselected_edges: Sequence[Edge]) -> None:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for graph traversal components."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
"""Unit tests for skip propagator."""

from unittest.mock import MagicMock, create_autospec

from core.workflow.graph import Edge, Graph
from core.workflow.graph_engine.graph_state_manager import GraphStateManager
from core.workflow.graph_engine.graph_traversal.skip_propagator import SkipPropagator


class TestSkipPropagator:
"""Test suite for SkipPropagator."""

def test_propagate_skip_from_edge_with_unknown_edges_stops_processing(self) -> None:
"""When there are unknown incoming edges, propagation should stop."""
# Arrange
mock_graph = create_autospec(Graph)
mock_state_manager = create_autospec(GraphStateManager)

# Create a mock edge
mock_edge = MagicMock(spec=Edge)
mock_edge.id = "edge_1"
mock_edge.head = "node_2"

# Setup graph edges dict
mock_graph.edges = {"edge_1": mock_edge}

# Setup incoming edges
incoming_edges = [MagicMock(spec=Edge), MagicMock(spec=Edge)]
mock_graph.get_incoming_edges.return_value = incoming_edges

# Setup state manager to return has_unknown=True
mock_state_manager.analyze_edge_states.return_value = {
"has_unknown": True,
"has_taken": False,
"all_skipped": False,
}

propagator = SkipPropagator(mock_graph, mock_state_manager)

# Act
propagator.propagate_skip_from_edge("edge_1")

# Assert
mock_graph.get_incoming_edges.assert_called_once_with("node_2")
mock_state_manager.analyze_edge_states.assert_called_once_with(incoming_edges)
# Should not call any other state manager methods
mock_state_manager.enqueue_node.assert_not_called()
mock_state_manager.start_execution.assert_not_called()
mock_state_manager.mark_node_skipped.assert_not_called()

def test_propagate_skip_from_edge_with_taken_edge_enqueues_node(self) -> None:
"""When there is at least one taken edge, node should be enqueued."""
# Arrange
mock_graph = create_autospec(Graph)
mock_state_manager = create_autospec(GraphStateManager)

# Create a mock edge
mock_edge = MagicMock(spec=Edge)
mock_edge.id = "edge_1"
mock_edge.head = "node_2"

mock_graph.edges = {"edge_1": mock_edge}
incoming_edges = [MagicMock(spec=Edge)]
mock_graph.get_incoming_edges.return_value = incoming_edges

# Setup state manager to return has_taken=True
mock_state_manager.analyze_edge_states.return_value = {
"has_unknown": False,
"has_taken": True,
"all_skipped": False,
}

propagator = SkipPropagator(mock_graph, mock_state_manager)

# Act
propagator.propagate_skip_from_edge("edge_1")

# Assert
mock_state_manager.enqueue_node.assert_called_once_with("node_2")
mock_state_manager.start_execution.assert_called_once_with("node_2")
mock_state_manager.mark_node_skipped.assert_not_called()

def test_propagate_skip_from_edge_with_all_skipped_propagates_to_node(self) -> None:
"""When all incoming edges are skipped, should propagate skip to node."""
# Arrange
mock_graph = create_autospec(Graph)
mock_state_manager = create_autospec(GraphStateManager)

# Create a mock edge
mock_edge = MagicMock(spec=Edge)
mock_edge.id = "edge_1"
mock_edge.head = "node_2"

mock_graph.edges = {"edge_1": mock_edge}
incoming_edges = [MagicMock(spec=Edge)]
mock_graph.get_incoming_edges.return_value = incoming_edges

# Setup state manager to return all_skipped=True
mock_state_manager.analyze_edge_states.return_value = {
"has_unknown": False,
"has_taken": False,
"all_skipped": True,
}

propagator = SkipPropagator(mock_graph, mock_state_manager)

# Act
propagator.propagate_skip_from_edge("edge_1")

# Assert
mock_state_manager.mark_node_skipped.assert_called_once_with("node_2")
mock_state_manager.enqueue_node.assert_not_called()
mock_state_manager.start_execution.assert_not_called()

def test_propagate_skip_to_node_marks_node_and_outgoing_edges_skipped(self) -> None:
"""_propagate_skip_to_node should mark node and all outgoing edges as skipped."""
# Arrange
mock_graph = create_autospec(Graph)
mock_state_manager = create_autospec(GraphStateManager)

# Create outgoing edges
edge1 = MagicMock(spec=Edge)
edge1.id = "edge_2"
edge1.head = "node_downstream_1" # Set head for propagate_skip_from_edge

edge2 = MagicMock(spec=Edge)
edge2.id = "edge_3"
edge2.head = "node_downstream_2"

# Setup graph edges dict for propagate_skip_from_edge
mock_graph.edges = {"edge_2": edge1, "edge_3": edge2}
mock_graph.get_outgoing_edges.return_value = [edge1, edge2]

# Setup get_incoming_edges to return empty list to stop recursion
mock_graph.get_incoming_edges.return_value = []

propagator = SkipPropagator(mock_graph, mock_state_manager)

# Use mock to call private method
# Act
propagator._propagate_skip_to_node("node_1")

# Assert
mock_state_manager.mark_node_skipped.assert_called_once_with("node_1")
mock_state_manager.mark_edge_skipped.assert_any_call("edge_2")
mock_state_manager.mark_edge_skipped.assert_any_call("edge_3")
assert mock_state_manager.mark_edge_skipped.call_count == 2
# Should recursively propagate from each edge
# Since propagate_skip_from_edge is called, we need to verify it was called
# But we can't directly verify due to recursion. We'll trust the logic.

def test_skip_branch_paths_marks_unselected_edges_and_propagates(self) -> None:
"""skip_branch_paths should mark all unselected edges as skipped and propagate."""
# Arrange
mock_graph = create_autospec(Graph)
mock_state_manager = create_autospec(GraphStateManager)

# Create unselected edges
edge1 = MagicMock(spec=Edge)
edge1.id = "edge_1"
edge1.head = "node_downstream_1"

edge2 = MagicMock(spec=Edge)
edge2.id = "edge_2"
edge2.head = "node_downstream_2"

unselected_edges = [edge1, edge2]

# Setup graph edges dict
mock_graph.edges = {"edge_1": edge1, "edge_2": edge2}
# Setup get_incoming_edges to return empty list to stop recursion
mock_graph.get_incoming_edges.return_value = []

propagator = SkipPropagator(mock_graph, mock_state_manager)

# Act
propagator.skip_branch_paths(unselected_edges)

# Assert
mock_state_manager.mark_edge_skipped.assert_any_call("edge_1")
mock_state_manager.mark_edge_skipped.assert_any_call("edge_2")
assert mock_state_manager.mark_edge_skipped.call_count == 2
# propagate_skip_from_edge should be called for each edge
# We can't directly verify due to the mock, but the logic is covered

def test_propagate_skip_from_edge_recursively_propagates_through_graph(self) -> None:
"""Skip propagation should recursively propagate through the graph."""
# Arrange
mock_graph = create_autospec(Graph)
mock_state_manager = create_autospec(GraphStateManager)

# Create edge chain: edge_1 -> node_2 -> edge_3 -> node_4
edge1 = MagicMock(spec=Edge)
edge1.id = "edge_1"
edge1.head = "node_2"

edge3 = MagicMock(spec=Edge)
edge3.id = "edge_3"
edge3.head = "node_4"

mock_graph.edges = {"edge_1": edge1, "edge_3": edge3}

# Setup get_incoming_edges to return different values based on node
def get_incoming_edges_side_effect(node_id):
if node_id == "node_2":
return [edge1]
elif node_id == "node_4":
return [edge3]
return []

mock_graph.get_incoming_edges.side_effect = get_incoming_edges_side_effect

# Setup get_outgoing_edges to return different values based on node
def get_outgoing_edges_side_effect(node_id):
if node_id == "node_2":
return [edge3]
elif node_id == "node_4":
return [] # No outgoing edges, stops recursion
return []

mock_graph.get_outgoing_edges.side_effect = get_outgoing_edges_side_effect

# Setup state manager to return all_skipped for both nodes
mock_state_manager.analyze_edge_states.return_value = {
"has_unknown": False,
"has_taken": False,
"all_skipped": True,
}

propagator = SkipPropagator(mock_graph, mock_state_manager)

# Act
propagator.propagate_skip_from_edge("edge_1")

# Assert
# Should mark node_2 as skipped
mock_state_manager.mark_node_skipped.assert_any_call("node_2")
# Should mark edge_3 as skipped
mock_state_manager.mark_edge_skipped.assert_any_call("edge_3")
# Should propagate to node_4
mock_state_manager.mark_node_skipped.assert_any_call("node_4")
assert mock_state_manager.mark_node_skipped.call_count == 2

def test_propagate_skip_from_edge_with_mixed_edge_states_handles_correctly(self) -> None:
"""Test with mixed edge states (some unknown, some taken, some skipped)."""
# Arrange
mock_graph = create_autospec(Graph)
mock_state_manager = create_autospec(GraphStateManager)

mock_edge = MagicMock(spec=Edge)
mock_edge.id = "edge_1"
mock_edge.head = "node_2"

mock_graph.edges = {"edge_1": mock_edge}
incoming_edges = [MagicMock(spec=Edge), MagicMock(spec=Edge), MagicMock(spec=Edge)]
mock_graph.get_incoming_edges.return_value = incoming_edges

# Test 1: has_unknown=True, has_taken=False, all_skipped=False
mock_state_manager.analyze_edge_states.return_value = {
"has_unknown": True,
"has_taken": False,
"all_skipped": False,
}

propagator = SkipPropagator(mock_graph, mock_state_manager)

# Act
propagator.propagate_skip_from_edge("edge_1")

# Assert - should stop processing
mock_state_manager.enqueue_node.assert_not_called()
mock_state_manager.mark_node_skipped.assert_not_called()

# Reset mocks for next test
mock_state_manager.reset_mock()
mock_graph.reset_mock()

# Test 2: has_unknown=False, has_taken=True, all_skipped=False
mock_state_manager.analyze_edge_states.return_value = {
"has_unknown": False,
"has_taken": True,
"all_skipped": False,
}

# Act
propagator.propagate_skip_from_edge("edge_1")

# Assert - should enqueue node
mock_state_manager.enqueue_node.assert_called_once_with("node_2")
mock_state_manager.start_execution.assert_called_once_with("node_2")

# Reset mocks for next test
mock_state_manager.reset_mock()
mock_graph.reset_mock()

# Test 3: has_unknown=False, has_taken=False, all_skipped=True
mock_state_manager.analyze_edge_states.return_value = {
"has_unknown": False,
"has_taken": False,
"all_skipped": True,
}

# Act
propagator.propagate_skip_from_edge("edge_1")

# Assert - should propagate skip
mock_state_manager.mark_node_skipped.assert_called_once_with("node_2")