@@ -211,6 +211,102 @@ async def test_handoffs_parsed_correctly():
211211 assert handoff_agent == agent_1
212212
213213
214+ @pytest .mark .asyncio
215+ async def test_history_nesting_disabled_by_default (monkeypatch : pytest .MonkeyPatch ):
216+ source_agent = Agent (name = "source" )
217+ target_agent = Agent (name = "target" )
218+ default_handoff = handoff (target_agent )
219+ tool_call = cast (ResponseFunctionToolCall , get_handoff_tool_call (target_agent ))
220+ run_handoffs = [ToolRunHandoff (handoff = default_handoff , tool_call = tool_call )]
221+ run_config = RunConfig ()
222+ context_wrapper = RunContextWrapper (context = None )
223+ hooks = RunHooks ()
224+ original_input = [get_text_input_item ("hello" )]
225+ pre_step_items : list [RunItem ] = []
226+ new_step_items : list [RunItem ] = []
227+ new_response = ModelResponse (output = [tool_call ], usage = Usage (), response_id = None )
228+
229+ def fail_if_called (
230+ _handoff_input_data : HandoffInputData ,
231+ * ,
232+ history_mapper : Any ,
233+ ) -> HandoffInputData :
234+ _ = history_mapper
235+ raise AssertionError ("nest_handoff_history should be opt-in." )
236+
237+ monkeypatch .setattr ("agents._run_impl.nest_handoff_history" , fail_if_called )
238+
239+ result = await RunImpl .execute_handoffs (
240+ agent = source_agent ,
241+ original_input = list (original_input ),
242+ pre_step_items = pre_step_items ,
243+ new_step_items = new_step_items ,
244+ new_response = new_response ,
245+ run_handoffs = run_handoffs ,
246+ hooks = hooks ,
247+ context_wrapper = context_wrapper ,
248+ run_config = run_config ,
249+ )
250+
251+ assert result .original_input == original_input
252+
253+
254+ @pytest .mark .asyncio
255+ async def test_run_level_history_nesting_can_be_enabled (monkeypatch : pytest .MonkeyPatch ):
256+ source_agent = Agent (name = "source" )
257+ target_agent = Agent (name = "target" )
258+ default_handoff = handoff (target_agent )
259+ tool_call = cast (ResponseFunctionToolCall , get_handoff_tool_call (target_agent ))
260+ run_handoffs = [ToolRunHandoff (handoff = default_handoff , tool_call = tool_call )]
261+ run_config = RunConfig (nest_handoff_history = True )
262+ context_wrapper = RunContextWrapper (context = None )
263+ hooks = RunHooks ()
264+ original_input = [get_text_input_item ("hello" )]
265+ pre_step_items : list [RunItem ] = []
266+ new_step_items : list [RunItem ] = []
267+ new_response = ModelResponse (output = [tool_call ], usage = Usage (), response_id = None )
268+
269+ calls : list [HandoffInputData ] = []
270+
271+ def fake_nest (
272+ handoff_input_data : HandoffInputData ,
273+ * ,
274+ history_mapper : Any ,
275+ ) -> HandoffInputData :
276+ _ = history_mapper
277+ calls .append (handoff_input_data )
278+ return handoff_input_data .clone (
279+ input_history = (
280+ {
281+ "role" : "assistant" ,
282+ "content" : "nested" ,
283+ },
284+ )
285+ )
286+
287+ monkeypatch .setattr ("agents._run_impl.nest_handoff_history" , fake_nest )
288+
289+ result = await RunImpl .execute_handoffs (
290+ agent = source_agent ,
291+ original_input = list (original_input ),
292+ pre_step_items = pre_step_items ,
293+ new_step_items = new_step_items ,
294+ new_response = new_response ,
295+ run_handoffs = run_handoffs ,
296+ hooks = hooks ,
297+ context_wrapper = context_wrapper ,
298+ run_config = run_config ,
299+ )
300+
301+ assert calls
302+ assert result .original_input == [
303+ {
304+ "role" : "assistant" ,
305+ "content" : "nested" ,
306+ }
307+ ]
308+
309+
214310@pytest .mark .asyncio
215311async def test_handoff_can_disable_run_level_history_nesting (monkeypatch : pytest .MonkeyPatch ):
216312 source_agent = Agent (name = "source" )
@@ -233,6 +329,7 @@ def fake_nest(
233329 * ,
234330 history_mapper : Any ,
235331 ) -> HandoffInputData :
332+ _ = history_mapper
236333 calls .append (handoff_input_data )
237334 return handoff_input_data
238335
@@ -274,6 +371,7 @@ def fake_nest(
274371 * ,
275372 history_mapper : Any ,
276373 ) -> HandoffInputData :
374+ _ = history_mapper
277375 return handoff_input_data .clone (
278376 input_history = (
279377 {
0 commit comments