@@ -1440,3 +1440,77 @@ async def user_only_executor(
14401440
14411441 # No assistant messages means no updates
14421442 assert len (updates ) == 0
1443+
1444+ async def test_streaming_agent_response_filters_system_and_tool_roles (self ):
1445+ """Test that system and tool role messages are also filtered out.
1446+
1447+ The allowlist filter (role == "assistant") intentionally drops all non-assistant
1448+ roles, including "system" and "tool". This test documents that behavior explicitly
1449+ to prevent future regressions if someone expects those messages to pass through.
1450+ """
1451+
1452+ @executor
1453+ async def mixed_role_executor (
1454+ messages : list [Message ], ctx : WorkflowContext [Never , AgentResponse ]
1455+ ) -> None :
1456+ response = AgentResponse (
1457+ messages = [
1458+ Message (role = "system" , text = "You are a helpful assistant." ),
1459+ Message (role = "user" , text = "hi" ),
1460+ Message (role = "assistant" , text = "Hello!" , author_name = "Bot" ),
1461+ Message (role = "tool" , text = "tool result data" ),
1462+ Message (role = "assistant" , text = "Based on the tool result..." , author_name = "Bot" ),
1463+ ],
1464+ )
1465+ await ctx .yield_output (response )
1466+
1467+ workflow = WorkflowBuilder (start_executor = mixed_role_executor ).build ()
1468+ agent = workflow .as_agent ("mixed-role-agent" )
1469+
1470+ # Streaming path
1471+ updates : list [AgentResponseUpdate ] = []
1472+ async for update in agent .run ("hi" , stream = True ):
1473+ updates .append (update )
1474+
1475+ # Only assistant messages should survive (2 out of 5)
1476+ assert len (updates ) == 2 , f"Expected 2 assistant updates, got { len (updates )} "
1477+ for update in updates :
1478+ assert update .role == "assistant"
1479+ texts = [u .text for u in updates ]
1480+ assert texts == ["Hello!" , "Based on the tool result..." ]
1481+
1482+ # Non-streaming path
1483+ workflow2 = WorkflowBuilder (start_executor = mixed_role_executor ).build ()
1484+ agent2 = workflow2 .as_agent ("mixed-role-agent-2" )
1485+ result = await agent2 .run ("hi" )
1486+
1487+ assert len (result .messages ) == 2 , f"Expected 2 messages, got { len (result .messages )} "
1488+ result_texts = [msg .text for msg in result .messages ]
1489+ assert result_texts == ["Hello!" , "Based on the tool result..." ]
1490+
1491+ async def test_non_streaming_agent_response_empty_after_filtering (self ):
1492+ """Test that non-streaming AgentResponse with only non-assistant messages produces empty result.
1493+
1494+ Counterpart to test_streaming_agent_response_empty_after_filtering for the
1495+ non-streaming code path.
1496+ """
1497+
1498+ @executor
1499+ async def user_only_executor (
1500+ messages : list [Message ], ctx : WorkflowContext [Never , AgentResponse ]
1501+ ) -> None :
1502+ response = AgentResponse (
1503+ messages = [
1504+ Message (role = "user" , text = "user msg 1" ),
1505+ Message (role = "user" , text = "user msg 2" ),
1506+ ],
1507+ )
1508+ await ctx .yield_output (response )
1509+
1510+ workflow = WorkflowBuilder (start_executor = user_only_executor ).build ()
1511+ agent = workflow .as_agent ("user-only-agent" )
1512+
1513+ result = await agent .run ("test" )
1514+
1515+ # No assistant messages means empty messages list
1516+ assert len (result .messages ) == 0 , f"Expected 0 messages, got { len (result .messages )} "
0 commit comments