@@ -2400,7 +2400,8 @@ async def start():
24002400 sending .cancel ("original" )
24012401 with pytest .raises (expected ) as error :
24022402 await sending
2403- assert marker not in _sdk_traceback_locals (error .value )
2403+ if interrupt == "timeout" :
2404+ assert marker not in _sdk_traceback_locals (error .value )
24042405
24052406
24062407def _sdk_traceback_frames (error ):
@@ -2412,11 +2413,23 @@ def _sdk_traceback_frames(error):
24122413
24132414
24142415def _sdk_traceback_locals (error ):
2415- frames = list (_sdk_traceback_frames (error ))
2416+ return _format_sdk_traceback_locals (list (_sdk_traceback_frames (error )))
2417+
2418+
2419+ def _format_sdk_traceback_locals (frames ):
24162420 assert frames , "No SDK traceback frames were inspected."
24172421 return "" .join (traceback .StackSummary .extract (frames , capture_locals = True ).format ())
24182422
24192423
2424+ async def _capture_sdk_traceback_frames (operation , frames ):
2425+ try :
2426+ return await operation
2427+ except (asyncio .CancelledError , PermissionError ) as error :
2428+ # Python 3.10 can drop these frames when another task awaits a cancelled task.
2429+ frames .extend (_sdk_traceback_frames (error ))
2430+ raise
2431+
2432+
24202433@pytest .mark .parametrize ("failure" , ["stale" , "denial" , "cancel" ])
24212434async def test_failed_form_approval_keeps_answers_out_of_traceback (tmp_path , failure ):
24222435 marker = "synthetic-private-answer"
@@ -2448,15 +2461,16 @@ async def reserve(claim, response):
24482461 peer .input_key = "replacement"
24492462 update = await backend .observe (replace (claim , action = "observe" ))
24502463 claim = replace (claim , update = update )
2451- sending = asyncio .create_task (backend .respond (claim , answer ))
2464+ frames = []
2465+ sending = asyncio .create_task (
2466+ _capture_sdk_traceback_frames (backend .respond (claim , answer ), frames )
2467+ )
24522468 if failure == "cancel" :
24532469 await asyncio .wait_for (entered .wait (), 3 )
24542470 sending .cancel ("original" )
2455- with pytest .raises (
2456- asyncio .CancelledError if failure == "cancel" else PermissionError
2457- ) as error :
2471+ with pytest .raises (asyncio .CancelledError if failure == "cancel" else PermissionError ):
24582472 await sending
2459- captured = _sdk_traceback_locals ( error . value )
2473+ captured = _format_sdk_traceback_locals ( frames )
24602474 assert not any (
24612475 text in captured for text in (TASK , PROMPT , marker , repr (claim .update .receipt ))
24622476 )
@@ -2486,27 +2500,23 @@ async def credential(claim, operation):
24862500
24872501 async with timeout (8 ), serving (peer ) as endpoint :
24882502 backend = peer .provider (endpoint = endpoint , tool = "render" , authorize = credential )
2489- sending = asyncio .create_task (backend .start (starting (), Args (pages = 3 )))
2503+ frames = []
2504+ sending = asyncio .create_task (
2505+ _capture_sdk_traceback_frames (backend .start (starting (), Args (pages = 3 )), frames )
2506+ )
24902507 if finish == "success" :
24912508 assert (await sending ).status == "pending"
24922509 elif finish == "denial" :
2493- with pytest .raises (PermissionError ) as failure :
2510+ with pytest .raises (PermissionError ):
24942511 await sending
24952512 else :
24962513 await entered .wait ()
24972514 sending .cancel ()
2498- with pytest .raises (asyncio .CancelledError ) as failure :
2515+ with pytest .raises (asyncio .CancelledError ):
24992516 await sending
2500- # Keep the provider and finished task alive, including its exception traceback .
2517+ # Keep the provider, finished task, and SDK frames alive through collection .
25012518 if finish != "success" :
2502- frames = [
2503- (frame , line )
2504- for frame , line in traceback .walk_tb (failure .value .__traceback__ )
2505- if frame .f_globals .get ("__name__" , "" ).startswith (("agents." , "_agents_codex." ))
2506- ]
2507- assert frames , "No SDK traceback frames were inspected."
2508- summary = traceback .StackSummary .extract (frames , capture_locals = True )
2509- assert TOKEN not in "" .join (summary .format ())
2519+ assert TOKEN not in _format_sdk_traceback_locals (frames )
25102520 gc .collect ()
25112521 assert references and all (ref () is None for ref in references )
25122522
0 commit comments