Skip to content

Commit 826e3f6

Browse files
CopilotCopilot
andcommitted
Fix #4589: clean up approval resolution and add missing tests
- Extract duplicated TOOL_CALL_RESULT emission block into _make_approval_tool_result_events helper to prevent drift - Remove dead rejection_results construction in _resolve_approval_responses; _replace_approval_contents_with_results already handles rejections inline - Pass only approved_results (not all_results) to clarify the contract - Add mixed approve/reject test validating the core splitting logic - Add zero-updates test covering the no-updates fallback emission path - Add direct unit test for _resolve_approval_responses return value Co-authored-by: Copilot <[email protected]>
1 parent 1065060 commit 826e3f6

2 files changed

Lines changed: 252 additions & 33 deletions

File tree

‎python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py‎

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,24 @@ def _handle_step_based_approval(messages: list[Any]) -> list[BaseEvent]:
370370
return events
371371

372372

373+
def _make_approval_tool_result_events(resolved_approval_results: list[Content]) -> list[ToolCallResultEvent]:
374+
"""Build TOOL_CALL_RESULT events for tools executed during approval resolution."""
375+
events: list[ToolCallResultEvent] = []
376+
for resolved in resolved_approval_results:
377+
if resolved.call_id:
378+
raw = resolved.result if resolved.result is not None else ""
379+
result_str = raw if isinstance(raw, str) else json.dumps(make_json_safe(raw))
380+
events.append(
381+
ToolCallResultEvent(
382+
message_id=generate_event_id(),
383+
tool_call_id=resolved.call_id,
384+
content=result_str,
385+
role="tool",
386+
)
387+
)
388+
return events
389+
390+
373391
def _evict_oldest_approvals(registry: dict[str, str], max_size: int = 10_000) -> None:
374392
"""Evict the oldest entries from the pending-approvals registry (LRU).
375393
@@ -516,17 +534,7 @@ async def _resolve_approval_responses(
516534
Content.from_function_result(call_id=call_id, result="Error: Tool call invocation failed.")
517535
)
518536

519-
# Build rejection results (included in message history but not emitted as events)
520-
rejection_results: list[Content] = []
521-
for rejection in rejected_responses:
522-
func_call = rejection.function_call
523-
call_id = (func_call.call_id if func_call else None) or rejection.id or ""
524-
rejection_results.append(
525-
Content.from_function_result(call_id=call_id, result="Error: Tool call invocation was rejected by user.")
526-
)
527-
528-
all_results = approved_results + rejection_results
529-
_replace_approval_contents_with_results(messages, fcc_todo, all_results) # type: ignore
537+
_replace_approval_contents_with_results(messages, fcc_todo, approved_results) # type: ignore
530538

531539
# Post-process: Convert user messages with function_result content to proper tool messages.
532540
# After _replace_approval_contents_with_results, approved tool calls have their results
@@ -864,17 +872,8 @@ async def run_agent_stream(
864872
yield StateSnapshotEvent(snapshot=flow.current_state)
865873
run_started_emitted = True
866874

867-
# Emit TOOL_CALL_RESULT events for tools executed during approval resolution
868-
for resolved in resolved_approval_results:
869-
if resolved.call_id:
870-
raw = resolved.result if resolved.result is not None else ""
871-
result_str = raw if isinstance(raw, str) else json.dumps(make_json_safe(raw))
872-
yield ToolCallResultEvent(
873-
message_id=generate_event_id(),
874-
tool_call_id=resolved.call_id,
875-
content=result_str,
876-
role="tool",
877-
)
875+
for event in _make_approval_tool_result_events(resolved_approval_results):
876+
yield event
878877

879878
# Feature #4: Detect tool-only messages (no text content)
880879
# Emit TextMessageStartEvent to create message context for tool calls
@@ -930,17 +929,8 @@ async def run_agent_stream(
930929
if state_schema and flow.current_state:
931930
yield StateSnapshotEvent(snapshot=flow.current_state)
932931

933-
# Emit TOOL_CALL_RESULT events for tools executed during approval resolution
934-
for resolved in resolved_approval_results:
935-
if resolved.call_id:
936-
raw = resolved.result if resolved.result is not None else ""
937-
result_str = raw if isinstance(raw, str) else json.dumps(make_json_safe(raw))
938-
yield ToolCallResultEvent(
939-
message_id=generate_event_id(),
940-
tool_call_id=resolved.call_id,
941-
content=result_str,
942-
role="tool",
943-
)
932+
for event in _make_approval_tool_result_events(resolved_approval_results):
933+
yield event
944934
if response_format is not None and all_updates:
945935
from agent_framework import AgentResponse
946936
from pydantic import BaseModel

‎python/packages/ag-ui/tests/ag_ui/test_approval_result_event.py‎

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,232 @@ async def test_rejection_does_not_emit_tool_call_result() -> None:
223223
assert len(tool_result_events) == 0, (
224224
f"Expected no TOOL_CALL_RESULT for rejected tool, got {len(tool_result_events)}"
225225
)
226+
227+
228+
def _make_temperature_tool() -> FunctionTool:
229+
"""Create a real executable temperature tool with approval_mode='always_require'."""
230+
231+
def get_temperature(city: str) -> str:
232+
return f"72F in {city}"
233+
234+
return FunctionTool(
235+
name="get_temperature",
236+
description="Get the temperature for a city",
237+
func=get_temperature,
238+
approval_mode="always_require",
239+
)
240+
241+
242+
async def test_mixed_approve_reject_emits_only_approved_tool_result() -> None:
243+
"""When one tool call is approved and another rejected, only the approved one produces a TOOL_CALL_RESULT event."""
244+
weather_tool = _make_weather_tool()
245+
temperature_tool = _make_temperature_tool()
246+
approved_call_id = "call_approved"
247+
rejected_call_id = "call_rejected"
248+
249+
agent = StubAgent(
250+
updates=[AgentResponseUpdate(contents=[Content.from_text(text="Here are the results.")], role="assistant")],
251+
default_options={"tools": [weather_tool, temperature_tool]},
252+
)
253+
config = AgentConfig()
254+
255+
resume_messages: list[dict[str, Any]] = [
256+
{"role": "user", "content": "Weather and temperature in Seattle?"},
257+
{
258+
"role": "assistant",
259+
"content": "",
260+
"tool_calls": [
261+
{
262+
"id": approved_call_id,
263+
"type": "function",
264+
"function": {
265+
"name": "get_weather",
266+
"arguments": json.dumps({"city": "Seattle"}),
267+
},
268+
},
269+
{
270+
"id": rejected_call_id,
271+
"type": "function",
272+
"function": {
273+
"name": "get_temperature",
274+
"arguments": json.dumps({"city": "Seattle"}),
275+
},
276+
},
277+
],
278+
},
279+
{
280+
"role": "tool",
281+
"content": json.dumps({"accepted": True}),
282+
"toolCallId": approved_call_id,
283+
},
284+
{
285+
"role": "tool",
286+
"content": json.dumps({"accepted": False}),
287+
"toolCallId": rejected_call_id,
288+
},
289+
]
290+
291+
input_data: dict[str, Any] = {
292+
"thread_id": "thread-mixed",
293+
"run_id": "run-mixed",
294+
"messages": resume_messages,
295+
}
296+
297+
events: list[Any] = []
298+
async for event in run_agent_stream(input_data, agent, config):
299+
events.append(event)
300+
301+
tool_result_events = [e for e in events if getattr(e, "type", None) == "TOOL_CALL_RESULT"]
302+
303+
# Only the approved tool call should produce a TOOL_CALL_RESULT event
304+
assert len(tool_result_events) == 1, (
305+
f"Expected exactly 1 TOOL_CALL_RESULT (approved only), got {len(tool_result_events)}"
306+
)
307+
assert tool_result_events[0].tool_call_id == approved_call_id
308+
assert tool_result_events[0].content == "Sunny in Seattle"
309+
310+
311+
async def test_approval_resume_zero_updates_emits_tool_result() -> None:
312+
"""When the agent produces zero updates, TOOL_CALL_RESULT events should still be emitted via the fallback path."""
313+
tool_name = "get_weather"
314+
call_id = "call_zero_updates"
315+
weather_tool = _make_weather_tool()
316+
317+
agent = StubAgent(
318+
updates=[],
319+
default_options={"tools": [weather_tool]},
320+
)
321+
config = AgentConfig()
322+
323+
resume_messages: list[dict[str, Any]] = [
324+
{"role": "user", "content": "What's the weather?"},
325+
{
326+
"role": "assistant",
327+
"content": "",
328+
"tool_calls": [
329+
{
330+
"id": call_id,
331+
"type": "function",
332+
"function": {
333+
"name": tool_name,
334+
"arguments": json.dumps({"city": "Boston"}),
335+
},
336+
}
337+
],
338+
},
339+
{
340+
"role": "tool",
341+
"content": json.dumps({"accepted": True}),
342+
"toolCallId": call_id,
343+
},
344+
]
345+
346+
input_data: dict[str, Any] = {
347+
"thread_id": "thread-zero-updates",
348+
"run_id": "run-zero-updates",
349+
"messages": resume_messages,
350+
}
351+
352+
events: list[Any] = []
353+
async for event in run_agent_stream(input_data, agent, config):
354+
events.append(event)
355+
356+
event_types = [getattr(e, "type", None) for e in events]
357+
assert "RUN_STARTED" in event_types
358+
359+
tool_result_events = [e for e in events if getattr(e, "type", None) == "TOOL_CALL_RESULT"]
360+
assert len(tool_result_events) == 1, (
361+
f"Expected 1 TOOL_CALL_RESULT in zero-updates fallback path, got {len(tool_result_events)}"
362+
)
363+
assert tool_result_events[0].tool_call_id == call_id
364+
assert tool_result_events[0].content == "Sunny in Boston"
365+
366+
367+
async def test_resolve_approval_responses_returns_only_approved() -> None:
368+
"""_resolve_approval_responses should return only approved results; rejection results go into messages only."""
369+
from agent_framework import Message
370+
from agent_framework_ag_ui._agent_run import _resolve_approval_responses
371+
372+
weather_tool = _make_weather_tool()
373+
temperature_tool = _make_temperature_tool()
374+
approved_call_id = "call_a"
375+
rejected_call_id = "call_r"
376+
377+
messages: list[Any] = [
378+
Message(role="user", contents=[Content.from_text(text="Hi")]),
379+
Message(
380+
role="assistant",
381+
contents=[
382+
Content(
383+
type="function_approval_request",
384+
id=approved_call_id,
385+
function_call=Content(
386+
type="function_call",
387+
name="get_weather",
388+
call_id=approved_call_id,
389+
arguments='{"city": "NYC"}',
390+
),
391+
),
392+
Content(
393+
type="function_approval_request",
394+
id=rejected_call_id,
395+
function_call=Content(
396+
type="function_call",
397+
name="get_temperature",
398+
call_id=rejected_call_id,
399+
arguments='{"city": "NYC"}',
400+
),
401+
),
402+
],
403+
),
404+
Message(
405+
role="user",
406+
contents=[
407+
Content(
408+
type="function_approval_response",
409+
id=approved_call_id,
410+
approved=True,
411+
function_call=Content(
412+
type="function_call",
413+
name="get_weather",
414+
call_id=approved_call_id,
415+
arguments='{"city": "NYC"}',
416+
),
417+
),
418+
Content(
419+
type="function_approval_response",
420+
id=rejected_call_id,
421+
approved=False,
422+
function_call=Content(
423+
type="function_call",
424+
name="get_temperature",
425+
call_id=rejected_call_id,
426+
arguments='{"city": "NYC"}',
427+
),
428+
),
429+
],
430+
),
431+
]
432+
433+
agent = StubAgent(
434+
updates=[],
435+
default_options={"tools": [weather_tool, temperature_tool]},
436+
)
437+
438+
results = await _resolve_approval_responses(
439+
messages, [weather_tool, temperature_tool], agent, {}
440+
)
441+
442+
# Return value should only contain approved results
443+
assert len(results) == 1
444+
assert results[0].call_id == approved_call_id
445+
assert results[0].type == "function_result"
446+
447+
# Rejection result should be written into messages (by _replace_approval_contents_with_results)
448+
all_contents = [c for msg in messages for c in msg.contents]
449+
rejection_results = [
450+
c for c in all_contents
451+
if c.type == "function_result" and c.call_id == rejected_call_id
452+
]
453+
assert len(rejection_results) == 1
454+
assert "rejected" in str(rejection_results[0].result).lower()

0 commit comments

Comments
 (0)