Skip to content

Commit 92891a6

Browse files
committed
address 2nd Codex review round: guardrail race, event drain, deferred compaction
Three more issues from the automated Codex review on commit 75fc64b, all verified with live reproduction before being fixed: 1. The handoff transition committed current_agent/run_state before a still-in-flight parallel input guardrail had resolved. A non-tripwire exception from that guardrail then left the resumable state pointing at the delegate agent, even though the starting agent's input guardrails never definitively cleared. Fixed by explicitly awaiting input_guardrail_tripwire_triggered_for_stream() as the first statement in the handoff branch, before any state mutation. 2. Queuing AgentUpdatedStreamEvent before the fallible session append doesn't guarantee delivery: stream_events() checks a stored exception before draining the queue, so a real (non-instant) consumer can lose an already-queued event to a task that raised without ever being marked for draining. Fixed by marking the session-persistence exception via _mark_error_to_drain_stream_events() before re-raising, the same pattern already used for model-behavior errors. 3. The pending_session_write checkpoint recovers the raw item append but never carried enough information (response_id, store, whether the batch had local tool outputs) for a later, separate resume to replay the same post-write Responses compaction decision save_result_to_session would have applied inline. Extracted the compaction decision into a shared _apply_post_write_compaction() helper, extended the checkpoint schema with those fields (optional, so an old-shaped serialized RunState still round-trips), and call the helper from resume_pending_session_write() once a checkpoint settles -- whether inline or on a separate resume -- instead of duplicating the call at both sites. Added 3 new regression tests to tests/test_run_impl_resume_paths.py (72 total in the file, up from 69), each confirmed to fail against the pre-fix code and pass after. Full verification stack clean: make format/ lint/typecheck, and the full suite (9372 passed, 33 skipped, 0 failed).
1 parent 75fc64b commit 92891a6

4 files changed

Lines changed: 315 additions & 47 deletions

File tree

‎src/agents/run_internal/run_loop.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
_detach_data_redacted_error_traceback,
4444
_is_error_data_redacted,
4545
_mark_error_data_redacted,
46+
_mark_error_to_drain_stream_events,
4647
_prepare_data_redacted_error,
4748
)
4849
from ..guardrail import OutputGuardrailResult
@@ -1887,6 +1888,11 @@ def _record_max_turns_handler_output(
18871888
server_conversation_tracker.track_server_items(turn_result.model_response)
18881889

18891890
if isinstance(turn_result.next_step, NextStepHandoff):
1891+
# Resolve any still-in-flight parallel input guardrail before committing the
1892+
# handoff transition, so a tripwire or guardrail exception is surfaced instead
1893+
# of the state (current_agent, run_state, published events) racing ahead of an
1894+
# input guardrail that was still validating the original input.
1895+
await input_guardrail_tripwire_triggered_for_stream(streamed_result)
18901896
current_agent = turn_result.next_step.new_agent
18911897
if run_state is not None:
18921898
run_state._current_agent = current_agent
@@ -1898,11 +1904,15 @@ def _record_max_turns_handler_output(
18981904
streamed_result._event_queue.put_nowait(
18991905
AgentUpdatedStreamEvent(new_agent=current_agent)
19001906
)
1901-
await _save_stream_items_without_count(
1902-
turn_session_items,
1903-
turn_result.model_response.response_id,
1904-
store_setting,
1905-
)
1907+
try:
1908+
await _save_stream_items_without_count(
1909+
turn_session_items,
1910+
turn_result.model_response.response_id,
1911+
store_setting,
1912+
)
1913+
except BaseException as session_persistence_error:
1914+
_mark_error_to_drain_stream_events(session_persistence_error)
1915+
raise
19061916
current_span.finish(reset_current=True)
19071917
current_span = None
19081918
should_run_agent_start_hooks = True

‎src/agents/run_internal/session_persistence.py‎

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,65 @@ def update_run_state_after_resume(
556556
run_state._current_step = next_step # type: ignore[assignment]
557557

558558

559+
async def _apply_post_write_compaction(
560+
session: Session,
561+
*,
562+
response_id: str | None,
563+
store: bool | None,
564+
has_local_tool_outputs: bool,
565+
wrapper: RunContextWrapper[Any] | None = None,
566+
) -> None:
567+
"""Evaluate deferred/forced Responses compaction for a settled session append.
568+
569+
Shared by the immediate-write path in ``save_result_to_session`` and the checkpoint
570+
replay path in ``resume_pending_session_write``, so a batch that only settles later
571+
(via a separate resume) still gets the same compaction decision it would have gotten
572+
had the original append succeeded inline. ``wrapper`` is the caller's raw (pre-gating)
573+
context wrapper; it is used as-is for ``run_compaction`` and re-gated here for
574+
``_defer_compaction``, mirroring the two call sites this helper replaces.
575+
"""
576+
if not response_id or not is_openai_responses_compaction_aware_session(session):
577+
return
578+
579+
if has_local_tool_outputs:
580+
defer_compaction = getattr(session, "_defer_compaction", None)
581+
if callable(defer_compaction):
582+
await _call_session_method(
583+
defer_compaction,
584+
response_id,
585+
store=store,
586+
wrapper=_get_session_wrapper(session, wrapper),
587+
)
588+
logger.debug(
589+
"skip: deferring compaction for response %s due to local tool outputs",
590+
response_id,
591+
)
592+
return
593+
594+
deferred_response_id = None
595+
get_deferred = getattr(session, "_get_deferred_compaction_response_id", None)
596+
if callable(get_deferred):
597+
deferred_response_id = get_deferred()
598+
force_compaction = deferred_response_id is not None
599+
if force_compaction:
600+
logger.debug(
601+
"compact: forcing for response %s after deferred %s",
602+
response_id,
603+
deferred_response_id,
604+
)
605+
compaction_args: OpenAIResponsesCompactionArgs = {
606+
"response_id": response_id,
607+
"force": force_compaction,
608+
}
609+
if store is not None:
610+
compaction_args["store"] = store
611+
await _call_session_method(
612+
session.run_compaction,
613+
compaction_args,
614+
wrapper=wrapper,
615+
)
616+
617+
559618
async def save_result_to_session(
560619
session: Session | None,
561620
original_input: str | list[TResponseInputItem],
@@ -663,6 +722,10 @@ async def save_result_to_session(
663722
run_state._current_turn_persisted_item_count = already_persisted + saved_run_items_count
664723
return saved_run_items_count
665724

725+
has_local_tool_outputs = any(
726+
isinstance(item, ToolCallOutputItem | HandoffOutputItem) for item in new_items
727+
)
728+
666729
if resumed_write_state is not None:
667730
if resumed_write_state._pending_session_write is not None:
668731
raise UserError("Resolve the pending Session write before saving another batch")
@@ -673,53 +736,31 @@ async def save_result_to_session(
673736
"persisted_count": (
674737
resumed_write_state._current_turn_persisted_item_count + saved_run_items_count
675738
),
739+
"response_id": response_id,
740+
"store": store,
741+
"has_local_tool_outputs": has_local_tool_outputs,
676742
}
677-
await resume_pending_session_write(resumed_write_state, session, wrapper=wrapper)
743+
# resume_pending_session_write() applies post-write compaction itself once the
744+
# checkpoint settles, whether that happens inline below or on a later, separate
745+
# resume -- so it is not repeated after this call returns.
746+
await resume_pending_session_write(
747+
resumed_write_state,
748+
session,
749+
wrapper=wrapper,
750+
compaction_wrapper=compaction_wrapper,
751+
)
678752
else:
679753
await _session_add_items(session, items_to_save, wrapper=wrapper)
680754

681755
if run_state is not None:
682756
run_state._current_turn_persisted_item_count = already_persisted + saved_run_items_count
683757

684-
if response_id and is_openai_responses_compaction_aware_session(session):
685-
has_local_tool_outputs = any(
686-
isinstance(item, ToolCallOutputItem | HandoffOutputItem) for item in new_items
687-
)
688-
if has_local_tool_outputs:
689-
defer_compaction = getattr(session, "_defer_compaction", None)
690-
if callable(defer_compaction):
691-
await _call_session_method(
692-
defer_compaction,
693-
response_id,
694-
store=store,
695-
wrapper=wrapper,
696-
)
697-
logger.debug(
698-
"skip: deferring compaction for response %s due to local tool outputs",
699-
response_id,
700-
)
701-
return saved_run_items_count
702-
703-
deferred_response_id = None
704-
get_deferred = getattr(session, "_get_deferred_compaction_response_id", None)
705-
if callable(get_deferred):
706-
deferred_response_id = get_deferred()
707-
force_compaction = deferred_response_id is not None
708-
if force_compaction:
709-
logger.debug(
710-
"compact: forcing for response %s after deferred %s",
711-
response_id,
712-
deferred_response_id,
713-
)
714-
compaction_args: OpenAIResponsesCompactionArgs = {
715-
"response_id": response_id,
716-
"force": force_compaction,
717-
}
718-
if store is not None:
719-
compaction_args["store"] = store
720-
await _call_session_method(
721-
session.run_compaction,
722-
compaction_args,
758+
if resumed_write_state is None:
759+
await _apply_post_write_compaction(
760+
session,
761+
response_id=response_id,
762+
store=store,
763+
has_local_tool_outputs=has_local_tool_outputs,
723764
wrapper=compaction_wrapper,
724765
)
725766

@@ -764,12 +805,18 @@ async def resume_pending_session_write(
764805
session: Session | None,
765806
*,
766807
wrapper: RunContextWrapper[Any] | None = None,
808+
compaction_wrapper: RunContextWrapper[Any] | None = None,
767809
) -> None:
768810
"""Settle a resumed output batch before allowing further model work.
769811
770812
The application must supply the original backend and serialize access to its history,
771813
including independently restored RunState copies. Session has no distributed compare-and-swap
772814
or backend identity contract. A changed tail is not repaired or searched for similar items.
815+
816+
``compaction_wrapper`` defaults to ``wrapper`` when omitted; ``save_result_to_session``
817+
passes its own raw (pre-gating) wrapper explicitly so a batch that settles here -- either
818+
inline or on a later, separate resume -- gets the exact same post-write Responses
819+
compaction decision ``save_result_to_session`` would otherwise have applied itself.
773820
"""
774821
pending = run_state._pending_session_write
775822
if pending is None:
@@ -819,6 +866,14 @@ def digests(items: Sequence[TResponseInputItem]) -> list[str]:
819866
finally:
820867
run_state._session_write_in_progress = False
821868

869+
await _apply_post_write_compaction(
870+
session,
871+
response_id=pending.get("response_id"),
872+
store=pending.get("store"),
873+
has_local_tool_outputs=pending.get("has_local_tool_outputs", False),
874+
wrapper=compaction_wrapper if compaction_wrapper is not None else wrapper,
875+
)
876+
822877

823878
async def rewind_session_items(
824879
session: Session | None,

‎src/agents/run_state.py‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
ProgramOutput,
3939
)
4040
from pydantic import BaseModel, StringConstraints, TypeAdapter, ValidationError
41-
from typing_extensions import TypedDict, TypeVar
41+
from typing_extensions import NotRequired, TypedDict, TypeVar
4242

4343
from ._tool_identity import (
4444
FunctionToolLookupKey,
@@ -172,6 +172,14 @@ class _PendingSessionWrite(TypedDict):
172172
items: list[TResponseInputItem]
173173
before: list[str] | None
174174
persisted_count: int
175+
# Compaction inputs for the batch this checkpoint is settling, so a later, separate
176+
# resume_pending_session_write() call (not the original save_result_to_session() call)
177+
# can still apply the same post-write Responses compaction decision. Optional so a
178+
# RunState serialized before these fields existed degrades to "skip compaction" on
179+
# read instead of raising KeyError.
180+
response_id: NotRequired[str | None]
181+
store: NotRequired[bool | None]
182+
has_local_tool_outputs: NotRequired[bool]
175183

176184

177185
def _default_run_state_validation_error(
@@ -4360,11 +4368,18 @@ async def _build_run_state_from_json(
43604368
if pending_write is not None:
43614369
from .run_internal.run_steps import NextStepInterruption, NextStepRunAgain
43624370

4371+
required_pending_write_keys = {"session_id", "items", "before", "persisted_count"}
4372+
# response_id/store/has_local_tool_outputs carry the compaction inputs needed to replay
4373+
# deferred/forced Responses compaction on a later, separate resume; they are optional so
4374+
# a RunState serialized before these fields existed (same, unreleased schema version)
4375+
# still round-trips.
4376+
optional_pending_write_keys = {"response_id", "store", "has_local_tool_outputs"}
43634377
if (
43644378
(schema_major, schema_minor) < (1, 17)
43654379
or not isinstance(state._current_step, NextStepRunAgain | NextStepInterruption)
43664380
or not isinstance(pending_write, dict)
4367-
or set(pending_write) != {"session_id", "items", "before", "persisted_count"}
4381+
or not required_pending_write_keys <= set(pending_write)
4382+
or not set(pending_write) <= required_pending_write_keys | optional_pending_write_keys
43684383
or not isinstance(pending_write.get("session_id"), str)
43694384
or not isinstance(pending_write.get("items"), list)
43704385
or not pending_write["items"]
@@ -4378,6 +4393,20 @@ async def _build_run_state_from_json(
43784393
)
43794394
or type(pending_write.get("persisted_count")) is not int
43804395
or pending_write["persisted_count"] < 0
4396+
or (
4397+
"response_id" in pending_write
4398+
and pending_write["response_id"] is not None
4399+
and not isinstance(pending_write["response_id"], str)
4400+
)
4401+
or (
4402+
"store" in pending_write
4403+
and pending_write["store"] is not None
4404+
and not isinstance(pending_write["store"], bool)
4405+
)
4406+
or (
4407+
"has_local_tool_outputs" in pending_write
4408+
and not isinstance(pending_write["has_local_tool_outputs"], bool)
4409+
)
43814410
):
43824411
raise validation_error_factory("Run state pending Session write is invalid", UserError)
43834412
state._pending_session_write = copy.deepcopy(cast(_PendingSessionWrite, pending_write))

0 commit comments

Comments
 (0)