@@ -1085,24 +1085,6 @@ async def _start_streaming(
10851085 if server_conversation_tracker is not None :
10861086 server_conversation_tracker .track_server_items (turn_result .model_response )
10871087
1088- # Check for soft cancel after tool execution completes (before next step)
1089- if streamed_result ._cancel_mode == "after_turn" : # type: ignore[comparison-overlap]
1090- # Save session with complete tool execution (tool calls + tool results)
1091- if session is not None :
1092- should_skip_session_save = (
1093- await AgentRunner ._input_guardrail_tripwire_triggered_for_stream (
1094- streamed_result
1095- )
1096- )
1097- if should_skip_session_save is False :
1098- await AgentRunner ._save_result_to_session (
1099- session , [], turn_result .new_step_items
1100- )
1101-
1102- streamed_result .is_complete = True
1103- streamed_result ._event_queue .put_nowait (QueueCompleteSentinel ())
1104- break
1105-
11061088 if isinstance (turn_result .next_step , NextStepHandoff ):
11071089 # Save the conversation to session if enabled (before handoff)
11081090 # Note: Non-streaming path doesn't save handoff turns immediately,
@@ -1125,6 +1107,12 @@ async def _start_streaming(
11251107 streamed_result ._event_queue .put_nowait (
11261108 AgentUpdatedStreamEvent (new_agent = current_agent )
11271109 )
1110+
1111+ # Check for soft cancel after handoff (before next turn)
1112+ if streamed_result ._cancel_mode == "after_turn" : # type: ignore[comparison-overlap]
1113+ streamed_result .is_complete = True
1114+ streamed_result ._event_queue .put_nowait (QueueCompleteSentinel ())
1115+ break
11281116 elif isinstance (turn_result .next_step , NextStepFinalOutput ):
11291117 streamed_result ._output_guardrails_task = asyncio .create_task (
11301118 cls ._run_output_guardrails (
@@ -1170,6 +1158,12 @@ async def _start_streaming(
11701158 await AgentRunner ._save_result_to_session (
11711159 session , [], turn_result .new_step_items
11721160 )
1161+
1162+ # Check for soft cancel after tool execution completes (before next turn)
1163+ if streamed_result ._cancel_mode == "after_turn" : # type: ignore[comparison-overlap]
1164+ streamed_result .is_complete = True
1165+ streamed_result ._event_queue .put_nowait (QueueCompleteSentinel ())
1166+ break
11731167 except AgentsException as exc :
11741168 streamed_result .is_complete = True
11751169 streamed_result ._event_queue .put_nowait (QueueCompleteSentinel ())
0 commit comments