Skip to content

Commit 75fc64b

Browse files
committed
address Codex review: checkpoint the handoff batch and reorder the agent-update event
Two issues from the automated Codex review on this PR, both real: 1. _save_stream_items_without_count() never registered a pending_session_write checkpoint for the handoff batch, unlike the sibling is_resumed_state branch's _save_resumed_stream_items(). A failed append followed by a successful resume invoked the correct (delegate) agent but permanently dropped the handoff's function_call/function_call_output pair from session history, since nothing recorded the batch for the existing resume_pending_session_write() recovery path to replay. Fixed by threading resumed_write_state through _save_stream_items into save_result_to_session, gated on the handoff branch already having set _current_step to NextStepRunAgain. 2. AgentUpdatedStreamEvent was still queued after the fallible session append, so a live stream_events() consumer would see handoff items followed directly by an error with no semantic agent-transition event, even though the result and resumed run both correctly identify the new agent. Moved the event queue call to sit with the other state-transition updates, before the append. Both fixes are scoped to only the generic-loop branch this PR already touches; the already-merged is_resumed_state branch (openai#4725) has the same pre-existing event-ordering gap but is out of scope here. Extended test_fresh_streamed_handoff_preserves_agent_after_session_append_failure with a session-history assertion for issue 1, and added test_fresh_streamed_handoff_publishes_agent_update_before_session_append_failure for issue 2 (using a new session double that yields before failing, since a purely synchronous raise never gives stream_events() a scheduling boundary to prove event delivery either way).
1 parent 09e928a commit 75fc64b

2 files changed

Lines changed: 75 additions & 4 deletions

File tree

‎src/agents/run_internal/run_loop.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ async def _save_stream_items(
418418
response_id: str | None,
419419
update_persisted_count: bool,
420420
store: bool | None = None,
421+
resumed_write_state: RunState | None = None,
421422
) -> None:
422423
if not await _should_persist_stream_items(
423424
session=session,
@@ -433,6 +434,7 @@ async def _save_stream_items(
433434
response_id=response_id,
434435
store=store,
435436
wrapper=streamed_result.context_wrapper,
437+
resumed_write_state=resumed_write_state,
436438
)
437439
if update_persisted_count and streamed_result._state is not None:
438440
streamed_result._current_turn_persisted_item_count = (
@@ -1152,6 +1154,12 @@ async def _save_stream_items_without_count(
11521154
response_id=response_id,
11531155
update_persisted_count=False,
11541156
store=store_setting,
1157+
resumed_write_state=(
1158+
run_state
1159+
if run_state is not None
1160+
and isinstance(run_state._current_step, NextStepRunAgain)
1161+
else None
1162+
),
11551163
)
11561164

11571165
async def _save_max_turns_items(
@@ -1885,6 +1893,11 @@ def _record_max_turns_handler_output(
18851893
_publish_streamed_result_agent(streamed_result, current_agent)
18861894
if streamed_result._state is not None:
18871895
streamed_result._state._current_step = NextStepRunAgain()
1896+
# Queue the agent-transition event before the fallible session append so
1897+
# stream consumers observe the transition even if the append later raises.
1898+
streamed_result._event_queue.put_nowait(
1899+
AgentUpdatedStreamEvent(new_agent=current_agent)
1900+
)
18881901
await _save_stream_items_without_count(
18891902
turn_session_items,
18901903
turn_result.model_response.response_id,
@@ -1893,9 +1906,6 @@ def _record_max_turns_handler_output(
18931906
current_span.finish(reset_current=True)
18941907
current_span = None
18951908
should_run_agent_start_hooks = True
1896-
streamed_result._event_queue.put_nowait(
1897-
AgentUpdatedStreamEvent(new_agent=current_agent)
1898-
)
18991909

19001910
if await _wait_for_streamed_turn_events_and_stop_if_cancelled(streamed_result):
19011911
break

‎tests/test_run_impl_resume_paths.py‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from openai.types.responses import ResponseFunctionToolCall, ResponseOutputMessage
1010

1111
import agents.run as run_module
12-
from agents import Agent, Runner, function_tool, handoff
12+
from agents import Agent, AgentUpdatedStreamEvent, Runner, function_tool, handoff
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
@@ -101,6 +101,20 @@ async def add_items(self, items: list[TResponseInputItem]) -> None:
101101
await super().add_items(items)
102102

103103

104+
class _FailSecondAddItemsSessionWithYield(_FailSecondAddItemsSession):
105+
"""Same failure shape as ``_FailSecondAddItemsSession``, but the failing call performs a
106+
genuine ``await`` (a scheduler yield) before raising, like a real I/O-backed Session
107+
(SQLite, network, etc.) would. A purely synchronous raise never yields control back to the
108+
``stream_events()`` consumer before the run-loop task finishes, so a test built on it cannot
109+
observe whether an already-queued stream event was delivered before the error surfaced.
110+
"""
111+
112+
async def add_items(self, items: list[TResponseInputItem]) -> None:
113+
if self._call_count == 1:
114+
await asyncio.sleep(0)
115+
await super().add_items(items)
116+
117+
104118
class _LostAckSQLiteSession(SQLiteSession):
105119
fail_after_commit = False
106120
error = RuntimeError("session append failed")
@@ -1277,3 +1291,50 @@ async def test_fresh_streamed_handoff_preserves_agent_after_session_append_failu
12771291
assert result.final_output == "done"
12781292
assert result.last_agent.name == "delegate"
12791293
assert len(model.calls) == 2
1294+
expected_pair = ["function_call", "function_call_output"]
1295+
stored = await session.get_items()
1296+
assert _call_pair(stored, "handoff-1") == expected_pair
1297+
assert "pending_session_write" not in result.to_state().to_json()
1298+
1299+
1300+
@pytest.mark.asyncio
1301+
async def test_fresh_streamed_handoff_publishes_agent_update_before_session_append_failure() -> (
1302+
None
1303+
):
1304+
"""The generic-loop handoff branch must queue ``AgentUpdatedStreamEvent`` for the new agent
1305+
before the fallible session append, so ``stream_events()`` consumers observe the transition
1306+
even when the append later raises. Mirrors the already-merged ordering fix for the
1307+
``is_resumed_state``-specific branch (lines ~1407-1437), which is out of scope here.
1308+
1309+
Uses a session whose failing append performs a genuine ``await`` before raising: a purely
1310+
synchronous raise (as in ``_FailSecondAddItemsSession``) never yields control back to this
1311+
consumer before the run-loop task finishes, so it cannot prove event delivery either way
1312+
(a separate, pre-existing gate: ``stream_events()`` only drains an already-queued event past
1313+
a terminal error when that error was marked via ``_mark_error_to_drain_stream_events()``,
1314+
which session-append failures never are).
1315+
"""
1316+
model = ScriptedModel(
1317+
[
1318+
[get_function_tool_call("transfer_to_delegate", "{}", call_id="handoff-1")],
1319+
[get_text_message("done")],
1320+
]
1321+
)
1322+
delegate = Agent(name="delegate", model=model)
1323+
triage = Agent(name="triage", model=model, handoffs=[delegate])
1324+
session = _FailSecondAddItemsSessionWithYield()
1325+
1326+
failed_result = Runner.run_streamed(
1327+
triage, "hello", session=session, run_config=RunConfig(tracing_disabled=True)
1328+
)
1329+
collected_events: list[Any] = []
1330+
caught: RuntimeError | None = None
1331+
try:
1332+
async for event in failed_result.stream_events():
1333+
collected_events.append(event)
1334+
except RuntimeError as error:
1335+
caught = error
1336+
assert caught is session.error
1337+
assert any(
1338+
isinstance(event, AgentUpdatedStreamEvent) and event.new_agent.name == "delegate"
1339+
for event in collected_events
1340+
)

0 commit comments

Comments
 (0)