Skip to content

Commit 1a24069

Browse files
committed
fix(streaming): stop the handoff transition on a real input-guardrail tripwire
The generic-loop handoff branch already awaited an in-flight parallel input guardrail before committing the transition, but discarded its boolean result. A guardrail that settled normally with tripwire_triggered=True (no exception) therefore still published the delegate and a resumable NextStepRunAgain -- the InputGuardrailTripwireTriggered exception only fired later, from the pre-existing end-of-run safety net, by which point the transition had already leaked to stream consumers and to current_agent. Capture the boolean and raise immediately when it's true, before any part of the transition (current_agent, run_state, published events, session save) commits -- matching the exception-raising branch this same block already had for a guardrail that raises outright.
1 parent 20e7c3a commit 1a24069

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎src/agents/run_internal/run_loop.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,18 @@ def _record_max_turns_handler_output(
18921892
# handoff transition, so a tripwire or guardrail exception is surfaced instead
18931893
# of the state (current_agent, run_state, published events) racing ahead of an
18941894
# input guardrail that was still validating the original input.
1895-
await input_guardrail_tripwire_triggered_for_stream(streamed_result)
1895+
triggered = await input_guardrail_tripwire_triggered_for_stream(streamed_result)
1896+
if triggered:
1897+
first_trigger = next(
1898+
(
1899+
result
1900+
for result in streamed_result.input_guardrail_results
1901+
if result.output.tripwire_triggered
1902+
),
1903+
None,
1904+
)
1905+
if first_trigger is not None:
1906+
raise InputGuardrailTripwireTriggered(first_trigger)
18961907
current_agent = turn_result.next_step.new_agent
18971908
if run_state is not None:
18981909
run_state._current_agent = current_agent

‎tests/test_run_impl_resume_paths.py‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from agents.agent import ToolsToFinalOutputResult
1414
from agents.agent_output import AgentOutputSchema
1515
from agents.decorators import tool, tool_input_guardrail, tool_output_guardrail
16-
from agents.exceptions import UserError
16+
from agents.exceptions import InputGuardrailTripwireTriggered, UserError
1717
from agents.guardrail import GuardrailFunctionOutput, input_guardrail
1818
from agents.items import (
1919
MessageOutputItem,
@@ -1439,6 +1439,53 @@ async def slow_failing_guardrail(
14391439
assert state._current_agent.name == "triage"
14401440

14411441

1442+
@pytest.mark.asyncio
1443+
async def test_fresh_streamed_handoff_stops_transition_on_real_tripwire() -> None:
1444+
"""Sibling to test_fresh_streamed_handoff_awaits_parallel_input_guardrail_before_transition:
1445+
that test covers a parallel guardrail raising a genuine exception. This one covers a
1446+
guardrail that settles normally with tripwire_triggered=True (no exception). The generic-loop
1447+
handoff branch awaits the in-flight guardrail before committing the transition, but must also
1448+
inspect its boolean result: a normal tripwire result must raise
1449+
InputGuardrailTripwireTriggered and stop the transition, not silently publish the delegate
1450+
and a resumable NextStepRunAgain.
1451+
"""
1452+
1453+
@input_guardrail(run_in_parallel=True)
1454+
async def slow_tripping_guardrail(
1455+
ctx: RunContextWrapper[Any],
1456+
agent: Agent[Any],
1457+
input: str | list[TResponseInputItem],
1458+
) -> GuardrailFunctionOutput:
1459+
await asyncio.sleep(0.3)
1460+
return GuardrailFunctionOutput(output_info=None, tripwire_triggered=True)
1461+
1462+
model = ScriptedModel(
1463+
[
1464+
[get_function_tool_call("transfer_to_delegate", "{}", call_id="handoff-1")],
1465+
]
1466+
)
1467+
delegate = Agent(name="delegate", model=model)
1468+
triage = Agent(
1469+
name="triage",
1470+
model=model,
1471+
handoffs=[delegate],
1472+
input_guardrails=[slow_tripping_guardrail],
1473+
)
1474+
1475+
streamed_result = Runner.run_streamed(
1476+
triage, "hello", run_config=RunConfig(tracing_disabled=True)
1477+
)
1478+
with pytest.raises(InputGuardrailTripwireTriggered):
1479+
async for _ in streamed_result.stream_events():
1480+
await asyncio.sleep(0.05)
1481+
# The handoff transition must not have been committed: a real tripwire result must stop
1482+
# the transition the same way a raised guardrail exception does.
1483+
assert streamed_result.current_agent.name == "triage"
1484+
state = streamed_result.to_state()
1485+
assert state._current_agent is not None
1486+
assert state._current_agent.name == "triage"
1487+
1488+
14421489
@pytest.mark.asyncio
14431490
async def test_fresh_streamed_handoff_replays_deferred_compaction_after_resume() -> None:
14441491
"""A checkpointed handoff batch that fails to append and later settles via a separate,

0 commit comments

Comments
 (0)