11from __future__ import annotations
22
33import asyncio
4+ from typing import Any
45from uuid import uuid4
56
67import pytest
@@ -27,6 +28,18 @@ def approval_tool() -> str:
2728 return Agent (name = "test_agent" , model = model , tools = [approval_tool ])
2829
2930
31+ def _get_last_function_span_export (name : str ) -> dict [str , Any ]:
32+ matching_spans = [
33+ exported
34+ for span in fetch_ordered_spans ()
35+ if (exported := span .export ()) is not None
36+ and exported ["span_data" ]["type" ] == "function"
37+ and exported ["span_data" ]["name" ] == name
38+ ]
39+ assert matching_spans
40+ return matching_spans [- 1 ]
41+
42+
3043@pytest .mark .asyncio
3144async def test_single_run_is_single_trace ():
3245 agent = Agent (
@@ -358,6 +371,45 @@ async def test_completed_result_to_state_preserves_sensitive_trace_flag() -> Non
358371 assert state ._trace_include_sensitive_data is False
359372
360373
374+ @pytest .mark .asyncio
375+ async def test_resumed_run_honors_explicit_trace_include_sensitive_data () -> None :
376+ model = FakeModel ()
377+
378+ @function_tool (name_override = "send_email" , needs_approval = True )
379+ def send_email (recipient : str ) -> str :
380+ return recipient
381+
382+ agent = Agent (name = "trace_agent" , model = model , tools = [send_email ])
383+ model .add_multiple_turn_outputs (
384+ [
385+ [
386+ get_function_tool_call (
387+ "send_email" , '{"recipient":"alice@example.com"}' , call_id = "call-1"
388+ )
389+ ],
390+ [get_text_message ("done" )],
391+ ]
392+ )
393+
394+ first = await Runner .run (agent , input = "first_test" )
395+ assert first .interruptions
396+
397+ state = first .to_state ()
398+ state .approve (first .interruptions [0 ], override_arguments = {"recipient" : "bob@example.com" })
399+
400+ resumed = await Runner .run (
401+ agent ,
402+ state ,
403+ run_config = RunConfig (trace_include_sensitive_data = False ),
404+ )
405+
406+ assert resumed .final_output == "done"
407+ assert state ._trace_include_sensitive_data is False
408+ function_span = _get_last_function_span_export ("send_email" )
409+ assert function_span ["span_data" ]["input" ] is None
410+ assert function_span ["span_data" ]["output" ] is None
411+
412+
361413@pytest .mark .asyncio
362414async def test_wrapped_trace_is_single_trace ():
363415 model = FakeModel ()
@@ -643,6 +695,49 @@ async def test_resumed_streaming_run_reuses_original_trace_without_duplicate_tra
643695 assert all (span .trace_id == traces [0 ].trace_id for span in fetch_ordered_spans ())
644696
645697
698+ @pytest .mark .asyncio
699+ async def test_resumed_streaming_run_honors_explicit_trace_include_sensitive_data () -> None :
700+ model = FakeModel ()
701+
702+ @function_tool (name_override = "send_email" , needs_approval = True )
703+ def send_email (recipient : str ) -> str :
704+ return recipient
705+
706+ agent = Agent (name = "trace_agent" , model = model , tools = [send_email ])
707+ model .add_multiple_turn_outputs (
708+ [
709+ [
710+ get_function_tool_call (
711+ "send_email" , '{"recipient":"alice@example.com"}' , call_id = "call-1"
712+ )
713+ ],
714+ [get_text_message ("done" )],
715+ ]
716+ )
717+
718+ first = Runner .run_streamed (agent , input = "first_test" )
719+ async for _ in first .stream_events ():
720+ pass
721+ assert first .interruptions
722+
723+ state = first .to_state ()
724+ state .approve (first .interruptions [0 ], override_arguments = {"recipient" : "bob@example.com" })
725+
726+ resumed = Runner .run_streamed (
727+ agent ,
728+ state ,
729+ run_config = RunConfig (trace_include_sensitive_data = False ),
730+ )
731+ async for _ in resumed .stream_events ():
732+ pass
733+
734+ assert resumed .final_output == "done"
735+ assert state ._trace_include_sensitive_data is False
736+ function_span = _get_last_function_span_export ("send_email" )
737+ assert function_span ["span_data" ]["input" ] is None
738+ assert function_span ["span_data" ]["output" ] is None
739+
740+
646741@pytest .mark .asyncio
647742async def test_wrapped_streaming_trace_is_single_trace ():
648743 model = FakeModel ()
0 commit comments