Skip to content
Merged
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
158 changes: 53 additions & 105 deletions api/dify_graph/graph_engine/response_coordinator/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from collections import deque
from collections.abc import Sequence
from threading import RLock
from typing import Literal, TypeAlias, final
from typing import Literal, final
from uuid import uuid4

from pydantic import BaseModel, Field
Expand All @@ -26,8 +26,8 @@
logger = logging.getLogger(__name__)

# Type definitions
NodeID: TypeAlias = str
EdgeID: TypeAlias = str
type NodeID = str
type EdgeID = str


class ResponseSessionState(BaseModel):
Expand All @@ -52,16 +52,17 @@ class StreamPositionState(BaseModel):


class ResponseStreamCoordinatorState(BaseModel):
"""Serialized snapshot of ResponseStreamCoordinator."""
"""Version 2 stores taken edges; residual paths are retained when loading version 1."""

type: Literal["ResponseStreamCoordinator"] = Field(default="ResponseStreamCoordinator")
version: str = Field(default="1.0")
version: str = Field(default="2.0")
response_nodes: Sequence[str] = Field(default_factory=list)
active_session: ResponseSessionState | None = None
waiting_sessions: Sequence[ResponseSessionState] = Field(default_factory=list)
pending_sessions: Sequence[ResponseSessionState] = Field(default_factory=list)
node_execution_ids: dict[str, str] = Field(default_factory=dict)
paths_map: dict[str, list[list[str]]] = Field(default_factory=dict)
taken_edges: list[str] = Field(default_factory=list)
stream_buffers: Sequence[StreamBufferState] = Field(default_factory=list)
stream_positions: Sequence[StreamPositionState] = Field(default_factory=list)
closed_streams: Sequence[tuple[str, ...]] = Field(default_factory=list)
Expand All @@ -75,6 +76,8 @@ class ResponseStreamCoordinator:
Ensures ordered streaming of responses based on upstream node outputs and constants.
"""

_taken_edges: set[EdgeID]

def __init__(self, variable_pool: "VariablePool", graph: GraphProtocol) -> None:
"""
Initialize coordinator with variable pool.
Expand All @@ -97,8 +100,9 @@ def __init__(self, variable_pool: "VariablePool", graph: GraphProtocol) -> None:
# Track response nodes
self._response_nodes: set[NodeID] = set()

# Store paths for each response node
# Only legacy snapshots retain already-enumerated, partially consumed paths.
self._paths_maps: dict[NodeID, list[Path]] = {}
self._taken_edges = set()

# Track node execution IDs and types for proper event forwarding
self._node_execution_ids: dict[NodeID, str] = {} # node_id -> execution_id
Expand All @@ -112,10 +116,6 @@ def register(self, response_node_id: NodeID) -> None:
return
self._response_nodes.add(response_node_id)

# Build and save paths map for this response node
paths_map = self._build_paths_map(response_node_id)
self._paths_maps[response_node_id] = paths_map

# Create and store response session for this node
response_node = self._graph.nodes[response_node_id]
session = ResponseSession.from_node(response_node)
Expand Down Expand Up @@ -145,93 +145,42 @@ def _get_or_create_execution_id(self, node_id: NodeID) -> str:
self._node_execution_ids[node_id] = str(uuid4())
return self._node_execution_ids[node_id]

def _build_paths_map(self, response_node_id: NodeID) -> list[Path]:
"""
Build a paths map for a response node by finding all paths from root node
to the response node, recording branch edges along each path.

Args:
response_node_id: ID of the response node to analyze
def _is_response_reachable(self, session: ResponseSession) -> bool:
"""Find a route whose blocking edges have all been taken, visiting each node once.

Returns:
List of Path objects, where each path contains branch edge IDs
Nonblocking edges allow early streaming without waiting for their source to finish.
Track observed edge events separately from graph state, which may be restored later.
"""
# Get root node ID
root_node_id = self._graph.root_node.id

# If root is the response node, return empty path
if root_node_id == response_node_id:
return [Path()]

# Extract variable selectors from the response node's template
response_node = self._graph.nodes[response_node_id]
response_session = ResponseSession.from_node(response_node)
template = response_session.template

# Collect all variable selectors from the template
variable_selectors: set[tuple[str, ...]] = set()
for segment in template.segments:
if isinstance(segment, VariableSegment):
variable_selectors.add(tuple(segment.selector[:2]))

# Step 1: Find all complete paths from root to response node
all_complete_paths: list[list[EdgeID]] = []

def find_paths(
current_node_id: NodeID, target_node_id: NodeID, current_path: list[EdgeID], visited: set[NodeID]
) -> None:
"""Recursively find all paths from current node to target node."""
if current_node_id == target_node_id:
# Found a complete path, store it
all_complete_paths.append(current_path.copy())
return

# Mark as visited to avoid cycles
visited.add(current_node_id)

# Explore outgoing edges
outgoing_edges = self._graph.get_outgoing_edges(current_node_id)
for edge in outgoing_edges:
edge_id = edge.id
next_node_id = edge.head

# Skip if already visited in this path
if next_node_id not in visited:
# Add edge to path and recurse
new_path = current_path + [edge_id]
find_paths(next_node_id, target_node_id, new_path, visited.copy())

# Start searching from root node
find_paths(root_node_id, response_node_id, [], set())

# Step 2: For each complete path, filter edges based on node blocking behavior
filtered_paths: list[Path] = []
for path in all_complete_paths:
blocking_edges: list[str] = []
for edge_id in path:
edge = self._graph.edges[edge_id]
source_node = self._graph.nodes[edge.tail]

# Check if node is a branch, container, or response node
if source_node.execution_type in {
NodeExecutionType.BRANCH,
NodeExecutionType.CONTAINER,
NodeExecutionType.RESPONSE,
} or source_node.blocks_variable_output(variable_selectors):
blocking_edges.append(edge_id)

# Keep the path even if it's empty
filtered_paths.append(Path(edges=blocking_edges))

return filtered_paths
variable_selectors = {
tuple(segment.selector[:2]) for segment in session.template.segments if isinstance(segment, VariableSegment)
}
pending = [self._graph.root_node.id]
visited = set(pending)
while pending:
node_id = pending.pop()
if node_id == session.node_id:
return True

node = self._graph.nodes[node_id]
blocks = node.execution_type in {
NodeExecutionType.BRANCH,
NodeExecutionType.CONTAINER,
NodeExecutionType.RESPONSE,
} or node.blocks_variable_output(variable_selectors)
for edge in self._graph.get_outgoing_edges(node_id):
if blocks and edge.id not in self._taken_edges:
continue
if edge.head not in visited:
visited.add(edge.head)
pending.append(edge.head)
return False

def on_edge_taken(self, edge_id: str) -> Sequence[NodeRunStreamChunkEvent]:
"""
Handle when an edge is taken (selected by a branch node).

This method updates the paths for all response nodes by removing
the taken edge. If any response node has an empty path after removal,
it means the node is now deterministically reachable and should start.
A pending response starts once reachable through nonblocking or taken edges.
Legacy snapshots continue consuming their saved residual path conditions.

Args:
edge_id: The ID of the edge that was taken
Expand All @@ -242,24 +191,21 @@ def on_edge_taken(self, edge_id: str) -> Sequence[NodeRunStreamChunkEvent]:
events: list[NodeRunStreamChunkEvent] = []

with self._lock:
self._taken_edges.add(edge_id)
# Check each response node in order
for response_node_id in self._response_nodes:
if response_node_id not in self._paths_maps:
session = self._response_sessions.get(response_node_id)
if session is None:
continue

paths = self._paths_maps[response_node_id]
has_reachable_path = False

# Update each path by removing the taken edge
for path in paths:
# Remove the taken edge from this path
path.remove_edge(edge_id)

# Check if this path is now empty (node is reachable)
if path.is_empty():
has_reachable_path = True
paths = self._paths_maps.get(response_node_id)
if paths is not None:
for path in paths:
path.remove_edge(edge_id)
has_reachable_path = any(path.is_empty() for path in paths)
else:
has_reachable_path = self._is_response_reachable(session)

# If node is now reachable (has empty path), start/queue session
if has_reachable_path:
# Pass the node_id to the activation method
# The method will handle checking and removing from map
Expand Down Expand Up @@ -641,6 +587,7 @@ def dumps(self) -> str:
node_id: [path.edges.copy() for path in paths]
for node_id, paths in sorted(self._paths_maps.items())
},
taken_edges=sorted(self._taken_edges),
stream_buffers=[
StreamBufferState(
selector=selector,
Expand All @@ -657,14 +604,14 @@ def dumps(self) -> str:
return state.model_dump_json()

def loads(self, data: str) -> None:
"""Restore coordinator state from JSON."""
"""Restore native reachability state or version 1 residual paths without enumerating paths."""

state = ResponseStreamCoordinatorState.model_validate_json(data)

if state.type != "ResponseStreamCoordinator":
raise ValueError(f"Invalid serialized data type: {state.type}")

if state.version != "1.0":
if state.version not in {"1.0", "2.0"}:
raise ValueError(f"Unsupported serialized version: {state.version}")

with self._lock:
Expand All @@ -673,6 +620,7 @@ def loads(self, data: str) -> None:
node_id: [Path(edges=list(path_edges)) for path_edges in paths]
for node_id, paths in state.paths_map.items()
}
self._taken_edges = set(state.taken_edges)
self._node_execution_ids = dict(state.node_execution_ids)

self._stream_buffers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,14 @@ def consumer_thread() -> set[bytes]:
producer_futures = [executor.submit(producer_thread, i) for i in range(producer_count)]

sent_msgs: set[bytes] = set()
# Wait for completion
for future in as_completed(producer_futures, timeout=30.0):
sent_msgs.update(future.result())
try:
for future in as_completed(producer_futures, timeout=30.0):
sent_msgs.update(future.result())

subscription.close()
consumer_received_msgs = consumer_future.result(timeout=30.0)
# Publishing can finish before the subscriber drains its pending messages.
consumer_received_msgs = consumer_future.result(timeout=30.0)
finally:
subscription.close()

# Verify message content
assert sent_msgs == consumer_received_msgs
Expand Down
Loading
Loading