Skip to content

Commit ad93f54

Browse files
authored
fix(core): restore nested agent tool state against the tool's own agent (#5142)
1 parent 5237420 commit ad93f54

2 files changed

Lines changed: 225 additions & 1 deletion

File tree

‎src/agents/run_state.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3039,9 +3039,15 @@ async def _restore_pending_nested_agent_tool_runs(
30393039
if not isinstance(tool_call, ResponseFunctionToolCall):
30403040
continue
30413041

3042+
# A nested run serializes agent references relative to the agent tool's own agent,
3043+
# so that agent must also be the root when the references are resolved again.
3044+
nested_root_agent = function_action.action.function_tool._agent_instance
3045+
if not isinstance(nested_root_agent, Agent):
3046+
nested_root_agent = current_agent
3047+
30423048
try:
30433049
nested_state = await _build_run_state_from_json(
3044-
initial_agent=current_agent,
3050+
initial_agent=nested_root_agent,
30453051
state_json=dict(nested_state_data),
30463052
context_deserializer=context_deserializer,
30473053
strict_context=strict_context,

‎tests/test_run_state.py‎

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
LocalShellTool,
119119
ProgrammaticToolCallingTool,
120120
ShellTool,
121+
Tool,
121122
function_tool,
122123
tool_namespace,
123124
)
@@ -237,6 +238,48 @@ def record_pending_nested_agent_tool_state(
237238
)
238239

239240

241+
def make_tool_calls_then_final_model(
242+
*,
243+
tool_calls: list[tuple[str, str]],
244+
final_text: str,
245+
call_prefix: str,
246+
expected_calls: int = 2,
247+
) -> ScriptedModel:
248+
"""Request the given tool calls, then finish once a tool output is present in the input."""
249+
250+
def _has_function_call_output(input_data: str | list[TResponseInputItem]) -> bool:
251+
if not isinstance(input_data, list):
252+
return False
253+
return any(
254+
(item.get("type") if isinstance(item, dict) else getattr(item, "type", None))
255+
== "function_call_output"
256+
for item in input_data
257+
)
258+
259+
def _respond(call: ModelCall) -> ModelResponse:
260+
if _has_function_call_output(call.input):
261+
return ModelResponse(
262+
output=[get_text_message(final_text)],
263+
usage=Usage(),
264+
response_id=f"{call_prefix}-done",
265+
)
266+
return ModelResponse(
267+
output=[
268+
ResponseFunctionToolCall(
269+
type="function_call",
270+
name=tool_name,
271+
call_id=f"{call_prefix}-{tool_name}",
272+
arguments=tool_arguments,
273+
)
274+
for tool_name, tool_arguments in tool_calls
275+
],
276+
usage=Usage(),
277+
response_id=f"{call_prefix}-call",
278+
)
279+
280+
return ScriptedModel(ModelStep.respond(_respond) for _ in range(expected_calls))
281+
282+
240283
def set_last_processed_response(
241284
state: RunState[Any, Agent[Any]],
242285
agent: Agent[Any],
@@ -5866,6 +5909,181 @@ async def inner_sensitive_tool(text: str) -> str:
58665909
inner_model.assert_complete()
58675910
outer_model.assert_complete()
58685911

5912+
@pytest.mark.asyncio
5913+
@pytest.mark.parametrize(
5914+
"approve_nested_tool",
5915+
[True, False],
5916+
ids=["approve", "reject"],
5917+
)
5918+
@pytest.mark.parametrize(
5919+
"agent_graph",
5920+
["inner_shares_outer_name", "sibling_agents_share_name", "cloned_sibling_agents"],
5921+
)
5922+
async def test_nested_agent_tool_hitl_resume_with_duplicate_agent_names(
5923+
self,
5924+
agent_graph: str,
5925+
approve_nested_tool: bool,
5926+
) -> None:
5927+
"""Nested decisions should reach the agent tool's own agent when agent names repeat."""
5928+
tool_calls: list[str] = []
5929+
5930+
def _make_sensitive_tool(label: str) -> FunctionTool:
5931+
@function_tool(name_override="inner_sensitive_tool", needs_approval=True)
5932+
async def inner_sensitive_tool(text: str) -> str:
5933+
tool_calls.append(f"{label}:{text}")
5934+
return f"approved:{text}"
5935+
5936+
return inner_sensitive_tool
5937+
5938+
def _make_inner_model(label: str) -> ScriptedModel:
5939+
return make_tool_calls_then_final_model(
5940+
tool_calls=[("inner_sensitive_tool", json.dumps({"text": "hello"}))],
5941+
final_text=f"{label}-complete",
5942+
call_prefix=label,
5943+
)
5944+
5945+
target_model = _make_inner_model("target")
5946+
target_tools: list[Tool] = [_make_sensitive_tool("target")]
5947+
outer_name = "OuterAgent"
5948+
sibling_agent: Agent[Any] | None = None
5949+
if agent_graph == "inner_shares_outer_name":
5950+
outer_name = "Assistant"
5951+
target_agent = Agent(name="Assistant", model=target_model, tools=target_tools)
5952+
elif agent_graph == "sibling_agents_share_name":
5953+
sibling_agent = Agent(
5954+
name="Worker",
5955+
model=_make_inner_model("sibling"),
5956+
tools=[_make_sensitive_tool("sibling")],
5957+
)
5958+
target_agent = Agent(name="Worker", model=target_model, tools=target_tools)
5959+
else:
5960+
base_agent = Agent(name="Worker")
5961+
sibling_agent = base_agent.clone(
5962+
model=_make_inner_model("sibling"),
5963+
tools=[_make_sensitive_tool("sibling")],
5964+
)
5965+
target_agent = base_agent.clone(model=target_model, tools=target_tools)
5966+
5967+
# The sibling is registered first so that the target needs a suffixed identity key.
5968+
outer_tools: list[Tool] = []
5969+
if sibling_agent is not None:
5970+
outer_tools.append(
5971+
sibling_agent.as_tool(
5972+
tool_name="sibling_agent_tool",
5973+
tool_description="Sibling agent tool",
5974+
)
5975+
)
5976+
outer_tools.append(
5977+
target_agent.as_tool(
5978+
tool_name="target_agent_tool",
5979+
tool_description="Target agent tool",
5980+
)
5981+
)
5982+
outer_model = make_tool_calls_then_final_model(
5983+
tool_calls=[("target_agent_tool", json.dumps({"input": "hello"}))],
5984+
final_text="outer-complete",
5985+
call_prefix="outer",
5986+
)
5987+
outer_agent = Agent(name=outer_name, model=outer_model, tools=outer_tools)
5988+
5989+
first_result = await Runner.run(outer_agent, "start")
5990+
assert first_result.final_output is None
5991+
assert len(first_result.interruptions) == 1
5992+
5993+
state_json = json.loads(json.dumps(first_result.to_state().to_json()))
5994+
del first_result
5995+
gc.collect()
5996+
5997+
restored_state = await RunState.from_json(outer_agent, state_json)
5998+
restored_interruptions = restored_state.get_interruptions()
5999+
assert len(restored_interruptions) == 1
6000+
assert restored_interruptions[0].agent is target_agent
6001+
if approve_nested_tool:
6002+
restored_state.approve(restored_interruptions[0])
6003+
else:
6004+
restored_state.reject(restored_interruptions[0])
6005+
6006+
resumed_result = await Runner.run(outer_agent, restored_state)
6007+
6008+
assert resumed_result.final_output == "outer-complete"
6009+
assert resumed_result.interruptions == []
6010+
assert tool_calls == (["target:hello"] if approve_nested_tool else [])
6011+
target_model.assert_complete()
6012+
outer_model.assert_complete()
6013+
6014+
async def test_multiple_nested_agent_tool_hitl_resumes_with_duplicate_agent_names(
6015+
self,
6016+
) -> None:
6017+
"""Pending nested states of same-name agent tools should keep separate decisions."""
6018+
tool_calls: list[str] = []
6019+
6020+
def _make_worker(label: str) -> tuple[Agent[Any], ScriptedModel]:
6021+
@function_tool(name_override="inner_sensitive_tool", needs_approval=True)
6022+
async def inner_sensitive_tool(text: str) -> str:
6023+
tool_calls.append(f"{label}:{text}")
6024+
return f"approved:{text}"
6025+
6026+
model = make_tool_calls_then_final_model(
6027+
tool_calls=[("inner_sensitive_tool", json.dumps({"text": "hello"}))],
6028+
final_text=f"{label}-complete",
6029+
call_prefix=label,
6030+
)
6031+
return Agent(name="Worker", model=model, tools=[inner_sensitive_tool]), model
6032+
6033+
first_worker, first_model = _make_worker("first")
6034+
second_worker, second_model = _make_worker("second")
6035+
outer_model = make_tool_calls_then_final_model(
6036+
tool_calls=[
6037+
("first_agent_tool", json.dumps({"input": "hello"})),
6038+
("second_agent_tool", json.dumps({"input": "hello"})),
6039+
],
6040+
final_text="outer-complete",
6041+
call_prefix="outer",
6042+
)
6043+
outer_agent = Agent(
6044+
name="OuterAgent",
6045+
model=outer_model,
6046+
tools=[
6047+
first_worker.as_tool(
6048+
tool_name="first_agent_tool",
6049+
tool_description="First agent tool",
6050+
),
6051+
second_worker.as_tool(
6052+
tool_name="second_agent_tool",
6053+
tool_description="Second agent tool",
6054+
),
6055+
],
6056+
)
6057+
6058+
first_result = await Runner.run(outer_agent, "start")
6059+
assert first_result.final_output is None
6060+
assert len(first_result.interruptions) == 2
6061+
6062+
state_json = json.loads(json.dumps(first_result.to_state().to_json()))
6063+
del first_result
6064+
gc.collect()
6065+
6066+
restored_state = await RunState.from_json(outer_agent, state_json)
6067+
restored_interruptions = restored_state.get_interruptions()
6068+
assert {id(interruption.agent) for interruption in restored_interruptions} == {
6069+
id(first_worker),
6070+
id(second_worker),
6071+
}
6072+
for interruption in restored_interruptions:
6073+
if interruption.agent is second_worker:
6074+
restored_state.approve(interruption)
6075+
else:
6076+
restored_state.reject(interruption)
6077+
6078+
resumed_result = await Runner.run(outer_agent, restored_state)
6079+
6080+
assert resumed_result.final_output == "outer-complete"
6081+
assert resumed_result.interruptions == []
6082+
assert tool_calls == ["second:hello"]
6083+
first_model.assert_complete()
6084+
second_model.assert_complete()
6085+
outer_model.assert_complete()
6086+
58696087
async def test_json_decode_error_handling(self):
58706088
"""Test that invalid JSON raises appropriate error."""
58716089
agent = Agent(name="TestAgent")

0 commit comments

Comments
 (0)