Skip to content

Commit 54b276f

Browse files
committed
Python: scope the stream role gate per message and keep billed-call metadata
Four review fixes on the caller-facing filter: - the streaming role gate remembers the declared role per message id, so a filtered user declaration no longer stains an unrelated role-less message from another stream, while the user message's own continuations stay filtered; id-less updates keep one conservative fallback slot - informational-only function calls have no tool result by contract and are no longer treated as internal call envelopes, so a child agent emitting only provider-hosted transcript calls still returns them - usage details and creation time merge even when every message of a child AgentResponse was filtered, keeping billing and telemetry intact Signed-off-by: Manohar Paturi <[email protected]>
1 parent 5668df2 commit 54b276f

2 files changed

Lines changed: 131 additions & 21 deletions

File tree

‎python/packages/core/agent_framework/_workflows/_agent.py‎

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,16 @@
5959

6060

6161
def _contains_internal_call_content(contents: Sequence[Content] | None) -> bool:
62-
"""True when any content is a function-call envelope (a call or an approval response)."""
63-
return any(getattr(content, "type", None) in _INTERNAL_CALL_CONTENT_TYPES for content in contents or ())
62+
"""True when any content is a function-call envelope (a call or an approval response).
63+
64+
Informational-only calls are transcript records that intentionally have no tool
65+
result, so they stay caller-facing instead of being treated as internal envelopes.
66+
"""
67+
return any(
68+
getattr(content, "type", None) in _INTERNAL_CALL_CONTENT_TYPES
69+
and not getattr(content, "informational_only", False)
70+
for content in contents or ()
71+
)
6472

6573

6674
def _caller_facing_messages(messages: Iterable[Message]) -> list[Message]:
@@ -81,21 +89,23 @@ def _caller_facing_messages(messages: Iterable[Message]) -> list[Message]:
8189

8290

8391
class _StreamingRoleGate:
84-
"""Track the last declared role across a stream of response updates.
92+
"""Track the declared role per streamed message across response updates.
8593
8694
Non-assistant streams may declare ``role`` only on their first delta and use
87-
``role=None`` continuation chunks. Updates without a declared role inherit the
88-
last declared one, so user content streamed as role-less chunks cannot leak
89-
through the assistant-only filter.
95+
``role=None`` continuation chunks. A declared role is remembered per message id, so
96+
continuation chunks of a user stream cannot leak through the assistant-only filter
97+
while an unrelated role-less message from another stream (or a later message of the
98+
same run) is not stained by it. Updates without a message id share one fallback
99+
slot, which stays conservative for streams that never identify their messages.
90100
"""
91101

92102
def __init__(self) -> None:
93-
self._last_declared_role: str | None = None
103+
self._declared_roles: dict[str | None, str] = {}
94104

95-
def should_forward(self, role: str | None) -> bool:
105+
def should_forward(self, role: str | None, message_id: str | None = None) -> bool:
96106
if role is not None:
97-
self._last_declared_role = role
98-
effective_role = role if role is not None else self._last_declared_role
107+
self._declared_roles[message_id] = role
108+
effective_role = role if role is not None else self._declared_roles.get(message_id)
99109
return effective_role is None or effective_role == "assistant"
100110

101111

@@ -636,19 +646,21 @@ def _convert_workflow_events_to_agent_response(
636646

637647
if isinstance(data, AgentResponse):
638648
# Only caller-facing assistant messages survive; see
639-
# _caller_facing_messages for the full policy.
649+
# _caller_facing_messages for the full policy. Usage and creation
650+
# time describe the billed child-agent call itself, so they merge
651+
# even when every message of the response was filtered out.
640652
assistant_messages = _caller_facing_messages(data.messages)
653+
messages.extend(assistant_messages)
641654
if assistant_messages:
642-
messages.extend(assistant_messages)
643655
raw_representations.append(data.raw_representation)
644-
merged_usage = add_usage_details(merged_usage, data.usage_details)
645-
latest_created_at = (
646-
data.created_at
647-
if not latest_created_at
648-
else max(latest_created_at, data.created_at)
649-
if data.created_at
650-
else latest_created_at
651-
)
656+
merged_usage = add_usage_details(merged_usage, data.usage_details)
657+
latest_created_at = (
658+
data.created_at
659+
if not latest_created_at
660+
else max(latest_created_at, data.created_at)
661+
if data.created_at
662+
else latest_created_at
663+
)
652664
elif isinstance(data, Message):
653665
if data.role == "assistant" and not _contains_internal_call_content(data.contents):
654666
messages.append(data)
@@ -721,7 +733,9 @@ def _convert_workflow_event_to_agent_response_updates(
721733
# Role-less continuation chunks inherit the last declared role from
722734
# the gate, and updates carrying function-call envelopes are dropped
723735
# so their orphaned calls cannot be persisted by from_updates.
724-
if not role_gate.should_forward(data.role) or _contains_internal_call_content(data.contents):
736+
if not role_gate.should_forward(data.role, data.message_id) or _contains_internal_call_content(
737+
data.contents
738+
):
725739
return []
726740
# Construct a fresh AgentResponseUpdate so we don't mutate a payload
727741
# that AgentExecutor still holds a reference to in its `updates` list.

‎python/packages/core/tests/workflow/test_workflow_agent.py‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,3 +2953,99 @@ async def handle_messages(
29532953
assert len(chunks2) == 1
29542954
assert chunks2[0].role == "assistant"
29552955
assert chunks2[0].text == "Answer: stream_q2"
2956+
2957+
2958+
class TestWorkflowAsAgentFilteringRound8:
2959+
"""Round-8 review fixes: informational calls, per-message role gate, usage retention."""
2960+
2961+
async def test_workflow_as_agent_keeps_informational_only_call(self) -> None:
2962+
"""Informational-only calls have no result by contract and stay caller-facing."""
2963+
2964+
@executor
2965+
async def informational_call_executor(
2966+
messages: list[Message],
2967+
ctx: WorkflowContext[Never, list[Message]], # type: ignore[valid-type]
2968+
) -> None:
2969+
await ctx.yield_output([
2970+
Message(
2971+
role="assistant",
2972+
contents=[
2973+
Content.from_function_call(
2974+
call_id="info-1", name="mcp_server_status", arguments={}, informational_only=True
2975+
),
2976+
],
2977+
),
2978+
])
2979+
2980+
workflow = WorkflowBuilder(start_executor=informational_call_executor).build()
2981+
agent = workflow.as_agent("informational-call-agent")
2982+
2983+
result = await agent.run("test")
2984+
assert len(result.messages) == 1
2985+
assert result.messages[0].contents[0].type == "function_call"
2986+
2987+
updates: list[AgentResponseUpdate] = []
2988+
async for chunk in agent.run("test", stream=True):
2989+
updates.append(chunk)
2990+
assert len(updates) >= 1
2991+
assert updates[0].contents[0].type == "function_call"
2992+
2993+
async def test_workflow_as_agent_stream_role_gate_scoped_per_message(self) -> None:
2994+
"""A declared role stains only its own message; other streams are not stained.
2995+
2996+
A user declaration on one message id must not drop an unrelated role-less
2997+
chunk of a different message, while the user message's own continuations
2998+
stay filtered (review: gate shared per run survives concurrent branches).
2999+
"""
3000+
3001+
@executor
3002+
async def mixed_stream_executor(
3003+
messages: list[Message],
3004+
ctx: WorkflowContext[Never, AgentResponseUpdate], # type: ignore[valid-type]
3005+
) -> None:
3006+
await ctx.yield_output(
3007+
AgentResponseUpdate(
3008+
contents=[Content.from_text("user said hi")], role="user", message_id="m-user"
3009+
)
3010+
)
3011+
await ctx.yield_output(
3012+
AgentResponseUpdate(contents=[Content.from_text("assistant answer")], message_id="m-answer")
3013+
)
3014+
await ctx.yield_output(
3015+
AgentResponseUpdate(contents=[Content.from_text(" and more user")], message_id="m-user")
3016+
)
3017+
3018+
workflow = WorkflowBuilder(start_executor=mixed_stream_executor).build()
3019+
agent = workflow.as_agent("mixed-stream-agent")
3020+
3021+
updates: list[AgentResponseUpdate] = []
3022+
async for chunk in agent.run("test", stream=True):
3023+
updates.append(chunk)
3024+
3025+
assert len(updates) == 1
3026+
assert updates[0].text == "assistant answer"
3027+
3028+
async def test_workflow_as_agent_keeps_usage_when_every_message_is_filtered(self) -> None:
3029+
"""Usage and creation time describe the billed call and survive message filtering."""
3030+
3031+
@executor
3032+
async def filtered_usage_executor(
3033+
messages: list[Message],
3034+
ctx: WorkflowContext[Never, AgentResponse], # type: ignore[valid-type]
3035+
) -> None:
3036+
await ctx.yield_output(
3037+
AgentResponse(
3038+
messages=[Message(role="user", contents=[Content.from_text("echoed user")])],
3039+
usage_details=UsageDetails(input_token_count=11, output_token_count=7, total_token_count=18),
3040+
)
3041+
)
3042+
3043+
workflow = WorkflowBuilder(start_executor=filtered_usage_executor).build()
3044+
agent = workflow.as_agent("filtered-usage-agent")
3045+
3046+
result = await agent.run("test")
3047+
assert result.messages == []
3048+
assert result.usage_details is not None
3049+
usage = result.usage_details
3050+
total = usage.total_token_count if hasattr(usage, "total_token_count") else usage.get("total_token_count")
3051+
assert total == 18

0 commit comments

Comments
 (0)