refactor(graph): Use piped Runnables for worker nodes to enable subgraph event streaming

This commit is contained in:
Yunxiao Xu
2026-02-23 17:48:23 -08:00
parent 02d93120e0
commit b8d8651924
3 changed files with 40 additions and 43 deletions

View File

@@ -1,11 +1,11 @@
import pytest
from ea_chatbot.graph.workflow import create_workflow, data_analyst_worker_node
from ea_chatbot.graph.workflow import create_workflow, data_analyst_worker_runnable
from ea_chatbot.graph.state import AgentState
from unittest.mock import MagicMock, patch
from langchain_core.messages import HumanMessage, AIMessage
def test_worker_merge_sets_summary_for_reflector():
"""Verify that worker node (wrapper) sets the 'summary' field for the Reflector."""
def test_worker_merge_sets_summary_for_reflector(monkeypatch):
"""Verify that worker node (runnable) sets the 'summary' field for the Reflector."""
state = AgentState(
messages=[HumanMessage(content="test")],
@@ -21,22 +21,28 @@ def test_worker_merge_sets_summary_for_reflector():
summary="Initial Planner Summary" # Stale summary
)
# Mock the compiled worker subgraph to return a specific result
with patch("ea_chatbot.graph.workflow._DATA_ANALYST_WORKER") as mock_worker:
mock_worker.invoke.return_value = {
"result": "Actual Worker Findings",
"messages": [AIMessage(content="Internal")],
"vfs_state": {},
"plots": []
}
# Execute the wrapper node
updates = data_analyst_worker_node(state)
# Verify that 'summary' is in updates and has the worker result
assert "summary" in updates
assert updates["summary"] == "Actual Worker Findings"
# When applied to state, it should overwrite the stale summary
state.update(updates)
assert state["summary"] == "Actual Worker Findings"
# Create a mock for the invoke method
mock_invoke = MagicMock()
mock_invoke.return_value = {
"summary": "Actual Worker Findings",
"messages": [AIMessage(content="Actual Worker Findings")],
"vfs": {},
"plots": []
}
# Manually replace the runnable with a mock object that has an invoke method
mock_runnable = MagicMock()
mock_runnable.invoke = mock_invoke
monkeypatch.setattr("ea_chatbot.graph.workflow.data_analyst_worker_runnable", mock_runnable)
# Execute via the module reference (which is now mocked)
from ea_chatbot.graph.workflow import data_analyst_worker_runnable
updates = data_analyst_worker_runnable.invoke(state)
# Verify that 'summary' is in updates and has the worker result
assert "summary" in updates
assert updates["summary"] == "Actual Worker Findings"
# When applied to state, it should overwrite the stale summary
state.update(updates)
assert state["summary"] == "Actual Worker Findings"