Backtestsopenai-agents-pythonrun-20261024-184640#4323
Support durable pending input on resumable RunState
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches implement the staged `add_input` API, serialize pending input, admit it after completed tool/handoff work, run only-pending-input guardrails, and handle session and server-managed exactly-once delivery; the independent suite confirms this with 24/25 passes for both. The one separating test is substantive: when an approved interruption's tool ends the run (stop_on_first_tool / stop_at_tool_names / callable tool_use_behavior), Patch B neither rejects nor delivers the late input. In B the final-output path never copies `run_state._pending_input` onto the result, so `to_state()` yields a terminal state with the message gone, which is exactly the 'silently dropping' outcome the issue forbids. Patch A detects this at `add_input` time by inspecting the interrupted tool names against `tool_use_behavior` and refuses before any mutation, and it also rejects when the turn budget is exhausted. A's terminal detection is step-based (`_current_step` of `NextStepRunAgain`/`NextStepInterruption` vs `None`) rather than B's bolt-on `_final_output_reached` flag, which defaults to False on legacy snapshots and would let input be staged on an old terminal state. A additionally gives admitted items a durable `input_id` so identical repeated messages are not fingerprint-deduped on server-managed resume, keeps server-managed input pending until the request is accepted, and checkpoints an accepted response so hook/validation failures after acceptance cannot resend it; its tests exercise these paths in both streaming and non-streaming modes. The cost is scope: A's response-acceptance checkpoint, `validate_pending_input_filter` heuristics, and incidental edits (computer-tool invocation marking, `track_server_items` in non-streaming `get_new_response`) are considerably more intrusive than the issue strictly needs and carry regression risk, and B is cleaner, better factored into a dedicated `pending_input` module, and adds user docs. But correctness leads the criteria, and B leaves a stated issue requirement unmet while A satisfies it, so A is the better fix despite its added complexity.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
30m 43s
Wall-clock time in the container
Full test suite
No new failures
3,463 passed · 3 skipped · 3,466 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
1,069vs 1,782 merged
Changed lines, ours vs merged
The issue
View on GitHubThis is a narrowed follow-up to #2671.
#2671 requested better support for application state changes between agent turns. The discussion covered several related but distinct cases, including:
- User input arriving while a tool is running.
- Application state changing before the next model call.
- Conversation history compaction.
- General host control over turn transitions.
The current SDK provides separate solutions for much of that original scope:
RunConfig.call_model_input_filtercan modify the prepared payload immediately before each model call.cancel(mode="after_turn")can stop a streaming run after the current model and tool work completes.- A host can start a new run with
result.last_agent, the same Session, and newly arrived input. RunStatesupports durable pause and resume for interrupted runs.- Session compaction is handled separately by the compaction Session APIs.
The remaining gap is narrower:
Applications cannot attach durable pending input to a resumable
RunStateand have that input admitted exactly once immediately before the state's next model call.
Concrete scenario
A streaming run produces a tool call. While the tool is executing, a new user message arrives.
The application allows the current turn to complete:
result = Runner.run_streamed(
agent,
initial_input,
session=session,
)
async for event in result.stream_events():
if new_user_input_available():
result.cancel(mode="after_turn")
After stream_events() finishes, the completed tool call and tool output are available in the result:
state = result.to_state()
The application should be able to attach the newly arrived input to that state, persist the updated state if necessary, and resume it later:
# Illustrative API only.
state.add_input("The user changed the requested destination.")
serialized = state.to_json()
# The state may be resumed immediately or by another worker.
state = await RunState.from_json(agent, serialized)
result = await Runner.run(agent, state, session=session)
The next model request should observe the following order exactly once:
initial user input
assistant tool call
tool output
late user input
The completed tool call must not be executed again.
Why existing approaches are not equivalent
Starting a new run
When a new run boundary is acceptable, the host can already use the same Session and pass the late message as the new run input:
result = await Runner.run(
stopped_result.last_agent,
late_input,
session=session,
)
This is the recommended existing workaround. It correctly places persisted tool work before the new input.
However, it represents the continuation as a new run rather than as a continuation of the existing resumable state. Applications that need to preserve the existing RunState boundary, current agent, remaining turn budget, trace resume state, or durable workflow record cannot express that requirement through the new-run pattern.
Using call_model_input_filter
call_model_input_filter can add late input to the next model payload, but the added item is only part of that prepared request. It is not automatically represented in:
- The client-managed Session.
RunResult.to_input_list().- A serialized
RunState. - Other SDK-owned conversation history and provenance surfaces.
This makes it appropriate for model-view filtering and rewriting, but not a complete durable-input admission mechanism.
Compaction
Conversation compaction is a separate state-ownership problem and is not part of this proposal. Existing model-input filtering and Session compaction APIs should continue to own that behavior.
Proposed behavior
A possible public shape is:
state.add_input(input)
The exact API shape may differ, but the behavior should follow these rules.
Staging
- Accept the same string or input-item forms supported by normal Runner input.
- Normalize string input as a user message.
- Preserve the order of multiple additions.
- Store the pending input in
RunState. - Preserve pending input through
to_json(),to_string(),from_json(), andfrom_string().
Adding pending input does not itself execute guardrails, tools, or model requests.
Admission boundary
Pending input is admitted only when the resumed run is ready to make its next model call.
If the state contains unfinished work, the Runner completes that work first. For example, when the state contains pending tool approvals, the ordering is:
resolve approvals
execute approved tools
record tool outputs
admit pending input
make the next model request
If unresolved approvals remain and no model request can begin, the input remains pending.
The input must not be inserted into an already-running model request or tool execution.
Guardrails
Admitting pending input creates a new input-admission boundary.
Immediately before admitting it, the Runner should:
- Run the input guardrails belonging to the agent that will receive the next model call.
- Run applicable
RunConfig.input_guardrails. - Evaluate only the newly pending input, not the complete replayed history.
If a guardrail trips or raises:
- The next model request must not start.
- Already completed tool work must not be rolled back or replayed.
- The pending input must remain inspectable and recoverable so the application can remove, replace, or retry it deliberately.
Ordering and exactly-once behavior
After successful admission, pending input must appear exactly once:
- After all previously completed tool and handoff items.
- Before the next model response.
- In client-managed Session history.
- In normalized replay output.
- In subsequent
RunStatesnapshots. - In the server-managed conversation delta when that conversation strategy is active.
The pending input must be marked as consumed only after the applicable persistence and conversation-ownership boundary has accepted it. Retrying or serializing the state must not duplicate it.
Run continuity
Adding pending input must not itself consume a turn.
The next model request should advance turn accounting normally while preserving the existing resumed-run behavior for:
- The current agent.
max_turnsand the current turn count.- Trace resume state.
- Tool-use tracking.
- Session persistence.
- Server-managed conversation tracking.
- Sandbox and other serialized resume state.
This proposal does not require changing the existing usage-accounting semantics across resume segments.
Supported states
Pending input may be attached to any non-terminal RunState that can eventually reach another model call.
This includes states produced by:
cancel(mode="after_turn").- Tool approval interruptions.
- Other supported resumable interruptions.
A terminal state with no possible next model call must reject the operation before side effects.
If a conversation strategy cannot provide the required ordering and exactly-once behavior, the SDK should reject pending input for that strategy before starting resume work rather than silently dropping or duplicating it.
Non-goals
This issue does not propose:
- Mutating an active model request or tool execution.
- A general
on_turn_startoron_turn_endhook. - A mutable
ActiveRunstepper API. - An SDK-owned cross-thread message queue.
- Thread-safe input injection into a live run.
- Arbitrary conversation-history replacement.
- Conversation compaction.
- Reordering or modifying already completed tool work.
- Changing normal input-guardrail behavior for runs without pending state input.
- Replacing the existing Session/new-run workaround.
Acceptance tests
A focused implementation should cover at least these cases:
- An after-turn state resumes with late input ordered after the completed tool output.
- The completed tool executes exactly once.
- Multiple pending input additions preserve their insertion order.
- Pending input survives a full
RunStateserialization round trip. - A client-managed Session persists the input exactly once.
- Server-managed continuation sends the input exactly once as the appropriate unsent delta.
- Approval resolution and tool execution complete before pending input is admitted.
- Remaining unresolved approvals keep the input pending.
- A guardrail trip prevents the next model request and leaves the pending input recoverable.
- Streaming and non-streaming resume produce equivalent conversation history.
- Retrying a failed model request does not duplicate the admitted input.
- Adding input to a terminal state fails before side effects.
Compatibility
This should be an additive capability:
- Existing
Runner.run(...)calls remain unchanged. - Existing
RunStatesnapshots deserialize with no pending input. - Existing input guardrails retain their current behavior unless pending input is explicitly attached.
- Existing Session, explicit replay, and server-managed conversation strategies retain their current ownership rules.
- The serialized
RunStateschema should be versioned according to the repository's durable-state compatibility policy.
Existing workaround
Applications that do not require continuation of the same RunState should continue using the simpler supported pattern:
- Call
cancel(mode="after_turn"). - Consume
stream_events()until completion. - Start a new run with
result.last_agent, the same Session, and the newly arrived input.
This issue is limited to applications for which that new-run boundary is materially insufficient.
The two patches
docs/streaming.mdindex f4abec97..99637c6f 100644@@ -62,6 +62,7 @@ If you need to stop a streaming run in the middle, call [`result.cancel()`][agen A streamed run is not complete until `result.stream_events()` finishes. The SDK may still be persisting session items, finalizing approval state, or compacting history after the last visible token. If you are manually continuing from [`result.to_input_list(mode="normalized")`][agents.result.RunResultBase.to_input_list], and `cancel(mode="after_turn")` stops after a tool turn, rerun `result.last_agent` with that normalized input to continue the unfinished existing user turn instead of appending a fresh user turn right away.+- If new user input arrived while the stopped turn was running and you need to keep the same `RunState` (current agent, turn budget, trace, and approvals), stage it on the state with `state = result.to_state()` and `state.add_input("...")`, then resume with `Runner.run(agent, state)`. The pending input survives `to_json()`/`from_json()`. It is admitted once, right before the next model call, after any completed tool work. The input guardrails of the receiving agent and `RunConfig.input_guardrails` run on that pending input first. If a guardrail trips, or the run pauses again before a model call, the input stays pending. You can inspect it with `state.get_pending_input()` or remove it with `state.clear_pending_input()`. If you don't need the same `RunState`, starting a new run with `result.last_agent`, the same session, and the new input is simpler. - If a streamed run stopped for tool approval, do not treat that as a new turn. Finish draining the stream, inspect `result.interruptions`, and resume from `result.to_state()` instead. - Use [`RunConfig.session_input_callback`][agents.run.RunConfig.session_input_callback] to customize how retrieved session history and the new user input are merged before the next model call. If you rewrite new-turn items there, the rewritten version is what gets persisted for that turn. src/agents/__init__.pyindex 8eeb38b2..ac7a095f 100644@@ -53,6 +53,7 @@ from .handoffs import ( set_conversation_history_wrappers, ) from .items import (+ AdmittedInputItem, CompactionItem, HandoffCallItem, HandoffOutputItem,@@ -463,6 +464,7 @@ __all__ = [ "OpenAIResponsesCompactionAwareSession", "is_openai_responses_compaction_aware_session", "CompactionItem",+ "AdmittedInputItem", "AgentHookContext", "RunContextWrapper", "TContext",src/agents/items.pyindex 115be2a6..7a0059b6 100644@@ -531,6 +531,23 @@ class CompactionItem(RunItemBase[TResponseInputItem]): return self.raw_item +@dataclass+class AdmittedInputItem(RunItemBase[TResponseInputItem]):+ """Represents input staged with `RunState.add_input()` and admitted before a model call.++ Pending input on a resumed `RunState` is admitted immediately before the run's next model+ request, after any completed tool or handoff work. Each admitted input item is recorded in+ run history as one of these items so session persistence, replay, and serialized state keep+ its position relative to that earlier work.+ """++ type: Literal["admitted_input_item"] = "admitted_input_item"++ def to_input_item(self) -> TResponseInputItem:+ """Converts this item into an input item suitable for passing to the model."""+ return self.raw_item++ # Union type for tool approval raw items - supports function tools, hosted tools, shell tools, etc. ToolApprovalRawItem: TypeAlias = ( ResponseFunctionToolCall | McpCall | McpApprovalRequest | LocalShellCall | dict[str, Any]@@ -681,6 +698,7 @@ RunItem: TypeAlias = ( | MCPApprovalRequestItem | MCPApprovalResponseItem | CompactionItem+ | AdmittedInputItem | ToolApprovalItem ) """An item generated by an agent."""src/agents/result.pyindex 6fa06a33..4322cc56 100644@@ -149,8 +149,17 @@ def _populate_state_from_result( else: state._generated_prompt_cache_key = getattr(result, "_generated_prompt_cache_key", None) state._reasoning_item_id_policy = getattr(result, "_reasoning_item_id_policy", None)+ pending_input = (+ source_state._pending_input+ if isinstance(source_state, RunState)+ else getattr(result, "_pending_input", [])+ )+ state._pending_input = copy.deepcopy(list(pending_input)) interruptions = list(getattr(result, "interruptions", []))+ state._final_output_reached = bool(getattr(result, "_final_output_reached", False)) and (+ not interruptions+ ) if interruptions: state._current_step = NextStepInterruption(interruptions=interruptions) @@ -299,6 +308,10 @@ class RunResultBase(abc.ABC): """Root agent graph used when converting the result back into RunState.""" _generated_prompt_cache_key: str | None = field(default=None, init=False, repr=False) """SDK-generated prompt cache key captured during the run."""+ _pending_input: list[TResponseInputItem] = field(default_factory=list, init=False, repr=False)+ """Input staged with `RunState.add_input()` that this run has not admitted yet."""+ _final_output_reached: bool = field(default=False, init=False, repr=False)+ """Whether this run reached a final output, making states derived from it terminal.""" @classmethod def __get_pydantic_core_schema__(src/agents/run.pyindex ca394108..e38c6e5f 100644@@ -2,6 +2,7 @@ from __future__ import annotations import asyncio import contextlib+import copy import warnings from typing import TYPE_CHECKING, Any, cast @@ -61,7 +62,7 @@ from .run_internal.agent_runner_helpers import ( build_resumed_stream_debug_extra, ensure_context_wrapper, finalize_conversation_tracking,- get_unsent_tool_call_ids_for_interrupted_state,+ get_unsent_tool_call_ids_for_resumed_state, input_guardrails_triggered, resolve_processed_response, resolve_resumed_context,@@ -89,6 +90,11 @@ from .run_internal.items import ( reconcile_nested_history_owned_input_after_rewrite, ) from .run_internal.oai_conversation import OpenAIServerConversationTracker+from .run_internal.pending_input import (+ admit_pending_input,+ commit_admitted_input,+ validate_pending_input_resumable,+) from .run_internal.prompt_cache_key import PromptCacheKeyResolver from .run_internal.run_grouping import resolve_run_grouping_id from .run_internal.run_loop import (@@ -531,6 +537,7 @@ class AgentRunner: if run_config.reasoning_item_id_policy is not None else (run_state._reasoning_item_id_policy if run_state is not None else None) )+ validate_pending_input_resumable(run_state) if run_state is not None: run_state._reasoning_item_id_policy = resolved_reasoning_item_id_policy starting_input = input if not is_resumed_state else None@@ -653,7 +660,7 @@ class AgentRunner: generated_items=run_state._generated_items, model_responses=run_state._model_responses, session_items=session_input_items,- unsent_tool_call_ids=get_unsent_tool_call_ids_for_interrupted_state(run_state),+ unsent_tool_call_ids=get_unsent_tool_call_ids_for_resumed_state(run_state), ) tool_use_tracker = AgentToolUseTracker()@@ -778,6 +785,7 @@ class AgentRunner: finalized_result._nested_history_owned_session_item_refs = list( run_state._nested_history_owned_session_item_refs )+ finalized_result._pending_input = copy.deepcopy(run_state._pending_input) completed_result = finalized_result return finalized_result @@ -1128,6 +1136,7 @@ class AgentRunner: store=store_setting, wrapper=context_wrapper, )+ result._final_output_reached = True result._original_input = copy_input_items(original_input) return _finalize_result(result) elif isinstance(turn_result.next_step, NextStepHandoff):@@ -1263,12 +1272,42 @@ class AgentRunner: store=store_setting, wrapper=context_wrapper, )+ result._final_output_reached = True result._original_input = copy_input_items(original_input) return _finalize_result(result) if run_state is not None and not resuming_turn: run_state._current_turn_persisted_item_count = 0 + admitted_input_awaiting_commit = False+ if run_state is not None and run_state._pending_input:+ # Unfinished work in the state has completed and no model request has+ # started yet, so this is the admission boundary for pending input.+ admitted_items = await admit_pending_input(+ run_state=run_state,+ agent=current_agent,+ run_config=run_config,+ context_wrapper=context_wrapper,+ input_guardrail_results=input_guardrail_results,+ session=session if session_persistence_enabled else None,+ store=store_setting,+ )+ # Rebind instead of extending: these lists may be shared with run_state.+ generated_items = generated_items + list(admitted_items)+ session_items = session_items + list(admitted_items)+ if pending_server_items is not None:+ pending_server_items = pending_server_items + list(admitted_items)+ if server_conversation_tracker is None:+ commit_admitted_input(+ run_state,+ generated_items=generated_items,+ session_items=session_items,+ )+ else:+ # The server owns the conversation, so the input is consumed only+ # once the model request carrying it has been accepted.+ admitted_input_awaiting_commit = True+ logger.debug("Running agent %s (turn %s)", current_agent.name, current_turn) if session_persistence_enabled:@@ -1424,6 +1463,13 @@ class AgentRunner: ) current_turn_span.finish(reset_current=True) + if admitted_input_awaiting_commit and run_state is not None:+ commit_admitted_input(+ run_state,+ generated_items=generated_items,+ session_items=session_items,+ )+ # Start hooks should only run on the first turn unless reset by a handoff. last_saved_input_snapshot_for_rewind = None should_run_agent_start_hooks = False@@ -1599,6 +1645,7 @@ class AgentRunner: result._current_turn_persisted_item_count = ( run_state._current_turn_persisted_item_count )+ result._final_output_reached = True result._original_input = copy_input_items(original_input) return _finalize_result(result) elif isinstance(turn_result.next_step, NextStepInterruption):@@ -1883,6 +1930,7 @@ class AgentRunner: if is_resumed_state: run_state = cast(RunState[TContext], input)+ validate_pending_input_resumable(run_state) ( conversation_id, previous_response_id,src/agents/run_internal/agent_runner_helpers.pyindex a8b65e57..5120e3b2 100644@@ -54,6 +54,7 @@ __all__ = [ "ensure_context_wrapper", "finalize_conversation_tracking", "get_unsent_tool_call_ids_for_interrupted_state",+ "get_unsent_tool_call_ids_for_resumed_state", "input_guardrails_triggered", "validate_session_conversation_settings", "resolve_trace_settings",@@ -228,6 +229,36 @@ def get_unsent_tool_call_ids_for_interrupted_state(run_state: RunState[Any] | No return tool_call_ids +def get_unsent_tool_call_ids_for_resumed_state(run_state: RunState[Any] | None) -> set[str]:+ """Return tool call IDs whose local outputs the server has not received yet.++ Besides the outputs belonging to the current interruption, this covers local outputs for+ calls emitted by the latest model response. No request has followed that response, so a+ state stopped between turns (for example with `cancel(mode="after_turn")`) still owes the+ server those outputs on resume.+ """+ tool_call_ids = get_unsent_tool_call_ids_for_interrupted_state(run_state)+ if run_state is None or not run_state._model_responses:+ return tool_call_ids+ for output_item in run_state._model_responses[-1].output:+ if output_item is None:+ continue+ has_output_payload = (isinstance(output_item, Mapping) and "output" in output_item) or (+ not isinstance(output_item, Mapping) and hasattr(output_item, "output")+ )+ if has_output_payload:+ # Server-side tool results arrive in the response itself and were never local.+ continue+ call_id = (+ output_item.get("call_id")+ if isinstance(output_item, Mapping)+ else getattr(output_item, "call_id", None)+ )+ if isinstance(call_id, str):+ tool_call_ids.add(call_id)+ return tool_call_ids++ def validate_session_conversation_settings( session: Session | None, *,src/agents/run_internal/oai_conversation.pyindex 38fa5a7d..658a7924 100644@@ -267,6 +267,9 @@ class OpenAIServerConversationTracker: "tool_search_call_item", "tool_search_output_item", }+ # Admitted input carries no server id, but a snapshot only records it after the+ # request that delivered it, so it must not be resent on resume.+ is_admitted_input_item = run_item.type == "admitted_input_item" if isinstance(raw_item, dict): item_id = _normalize_server_item_id(raw_item.get("id"))@@ -283,6 +286,7 @@ class OpenAIServerConversationTracker: item_id is not None or (has_call_id and (has_output_payload or is_tool_call_item)) or is_tool_search_item+ or is_admitted_input_item ) if not should_mark: continue@@ -316,6 +320,7 @@ class OpenAIServerConversationTracker: item_id is not None or (has_call_id and (has_output_payload or is_tool_call_item)) or is_tool_search_item+ or is_admitted_input_item ) if not should_mark: continue@@ -503,9 +508,17 @@ class OpenAIServerConversationTracker: if converted_input_item is None: continue fp = _fingerprint_for_tracker(converted_input_item)- if fp and fp in self.server_output_fingerprints:+ # Admitted input is deduped by object identity only: a new message may legitimately+ # repeat the text of earlier input ("yes", "continue") and must still be sent.+ is_admitted_input_item = run_item.type == "admitted_input_item"+ if fp and fp in self.server_output_fingerprints and not is_admitted_input_item: continue- if fp and self.primed_from_state and fp in self.sent_item_fingerprints:+ if (+ fp+ and self.primed_from_state+ and fp in self.sent_item_fingerprints+ and not is_admitted_input_item+ ): continue anonymous_tool_search_fp = _anonymous_tool_search_fingerprint(converted_input_item) if (src/agents/run_internal/pending_input.pynew file mode 100644index 00000000..88cb3ef4@@ -0,0 +1,174 @@+"""+Admission of input staged on a resumable RunState with `RunState.add_input()`.++Pending input is admitted exactly once, immediately before the resumed run's next model call and+after any unfinished work in the state (such as approved tool calls) has completed. Admission runs+the receiving agent's input guardrails against only the pending input, records each item as an+`AdmittedInputItem` after the completed tool and handoff items, and persists it to a client-managed+session. The pending input is cleared from the state only once the owning persistence boundary has+accepted it: immediately for local and session-backed history, and after the model request+succeeds for server-managed conversations.+"""++from __future__ import annotations++import copy+from typing import Any++from ..agent import Agent+from ..exceptions import InputGuardrailTripwireTriggered, UserError+from ..guardrail import InputGuardrail, InputGuardrailResult+from ..items import AdmittedInputItem, RunItem, TResponseInputItem+from ..memory import Session+from ..result import RunResultStreaming+from ..run_config import RunConfig+from ..run_context import RunContextWrapper+from ..run_state import RunState+from ..tracing import Span+from .guardrails import run_input_guardrails, run_input_guardrails_with_queue+from .session_persistence import save_result_to_session++__all__ = [+ "admit_pending_input",+ "admit_pending_input_streamed",+ "commit_admitted_input",+ "validate_pending_input_resumable",+]+++def validate_pending_input_resumable(run_state: RunState[Any] | None) -> None:+ """Reject pending input on a state that can never reach another model call."""+ if run_state is None or not run_state._pending_input:+ return+ if run_state._final_output_reached:+ raise UserError(+ "Cannot resume a RunState with pending input after its run produced a final output. "+ "Start a new run with the new input instead."+ )+++def _pending_input_guardrails(+ agent: Agent[Any],+ run_config: RunConfig,+) -> list[InputGuardrail[Any]]:+ return list(agent.input_guardrails) + list(run_config.input_guardrails or [])+++def _build_admitted_items(+ pending_input: list[TResponseInputItem],+ agent: Agent[Any],+) -> list[AdmittedInputItem]:+ return [AdmittedInputItem(agent=agent, raw_item=copy.deepcopy(item)) for item in pending_input]+++async def _persist_admitted_items(+ *,+ session: Session | None,+ admitted_items: list[AdmittedInputItem],+ store: bool | None,+ context_wrapper: RunContextWrapper[Any],+) -> None:+ if session is None or not admitted_items:+ return+ await save_result_to_session(+ session,+ [item.to_input_item() for item in admitted_items],+ [],+ None,+ store=store,+ wrapper=context_wrapper,+ )+++def commit_admitted_input(+ run_state: RunState[Any],+ *,+ generated_items: list[RunItem],+ session_items: list[RunItem],+) -> None:+ """Mark pending input as consumed and record the history that now contains it."""+ run_state._pending_input = []+ run_state._generated_items = list(generated_items)+ # The runner's model-facing history already reflects the last processed response; marking it+ # merged keeps serialization from re-appending processed items after the admitted input.+ run_state._mark_generated_items_merged_with_last_processed()+ run_state._session_items = list(session_items)+++async def admit_pending_input(+ *,+ run_state: RunState[Any],+ agent: Agent[Any],+ run_config: RunConfig,+ context_wrapper: RunContextWrapper[Any],+ input_guardrail_results: list[InputGuardrailResult],+ session: Session | None,+ store: bool | None,+) -> list[AdmittedInputItem]:+ """Run guardrails for pending input and return the admitted items for the next model call.++ Raises `InputGuardrailTripwireTriggered` (or the guardrail's own error) before any history or+ persistence changes, leaving the input pending on ``run_state``. ``session`` should be the+ client-managed session, or ``None`` when the SDK does not own session persistence for the run.+ """+ pending_input = copy.deepcopy(run_state._pending_input)+ if not pending_input:+ return []++ await run_input_guardrails(+ agent,+ _pending_input_guardrails(agent, run_config),+ pending_input,+ context_wrapper,+ input_guardrail_results,+ )++ admitted_items = _build_admitted_items(pending_input, agent)+ await _persist_admitted_items(+ session=session,+ admitted_items=admitted_items,+ store=store,+ context_wrapper=context_wrapper,+ )+ return admitted_items+++async def admit_pending_input_streamed(+ *,+ run_state: RunState[Any],+ agent: Agent[Any],+ run_config: RunConfig,+ context_wrapper: RunContextWrapper[Any],+ streamed_result: RunResultStreaming,+ parent_span: Span[Any] | None,+ session: Session | None,+ store: bool | None,+) -> list[AdmittedInputItem]:+ """Streaming counterpart of `admit_pending_input` that publishes guardrail results."""+ pending_input = copy.deepcopy(run_state._pending_input)+ if not pending_input:+ return []++ guardrails = _pending_input_guardrails(agent, run_config)+ if guardrails:+ existing_result_count = len(streamed_result.input_guardrail_results)+ await run_input_guardrails_with_queue(+ agent,+ guardrails,+ pending_input,+ context_wrapper,+ streamed_result,+ parent_span,+ )+ for result in streamed_result.input_guardrail_results[existing_result_count:]:+ if result.output.tripwire_triggered:+ raise InputGuardrailTripwireTriggered(result)++ admitted_items = _build_admitted_items(pending_input, agent)+ await _persist_admitted_items(+ session=session,+ admitted_items=admitted_items,+ store=store,+ context_wrapper=context_wrapper,+ )+ return admitted_itemssrc/agents/run_internal/run_loop.pyindex 936637fe..897b7948 100644@@ -93,7 +93,7 @@ from .agent_bindings import AgentBindings, bind_public_agent from .agent_runner_helpers import ( apply_resumed_conversation_settings, attach_usage_to_span,- get_unsent_tool_call_ids_for_interrupted_state,+ get_unsent_tool_call_ids_for_resumed_state, snapshot_usage, usage_delta, )@@ -130,6 +130,7 @@ from .model_retry import ( stream_response_with_retry, ) from .oai_conversation import OpenAIServerConversationTracker+from .pending_input import admit_pending_input_streamed, commit_admitted_input from .prompt_cache_key import PromptCacheKeyResolver, model_settings_with_prompt_cache_key from .run_steps import ( NextStepFinalOutput,@@ -600,6 +601,7 @@ async def _finalize_streamed_final_output( streamed_result.output_guardrail_results = output_guardrail_results streamed_result.final_output = output+ streamed_result._final_output_reached = True streamed_result.is_complete = True if not persist_before_output_guardrails:@@ -803,7 +805,7 @@ async def start_streaming( generated_items=run_state._generated_items, model_responses=run_state._model_responses, session_items=session_items,- unsent_tool_call_ids=get_unsent_tool_call_ids_for_interrupted_state(run_state),+ unsent_tool_call_ids=get_unsent_tool_call_ids_for_resumed_state(run_state), ) streamed_result._event_queue.put_nowait(AgentUpdatedStreamEvent(new_agent=current_agent))@@ -1217,6 +1219,7 @@ async def start_streaming( ) streamed_result.output_guardrail_results = output_guardrail_results streamed_result.final_output = validated_output+ streamed_result._final_output_reached = True streamed_result.is_complete = True streamed_result._stored_exception = None streamed_result._max_turns_handled = True@@ -1227,6 +1230,41 @@ async def start_streaming( streamed_result._event_queue.put_nowait(QueueCompleteSentinel()) break + admitted_input_awaiting_commit = False+ if run_state is not None and run_state._pending_input:+ # Unfinished work in the state has completed and no model request has started+ # yet, so this is the admission boundary for pending input.+ admitted_items = await admit_pending_input_streamed(+ run_state=run_state,+ agent=current_agent,+ run_config=run_config,+ context_wrapper=context_wrapper,+ streamed_result=streamed_result,+ parent_span=current_span,+ session=session if server_conversation_tracker is None else None,+ store=current_agent.model_settings.resolve(run_config.model_settings).store,+ )+ # Rebind instead of extending: these lists may be shared with run_state.+ streamed_result._model_input_items = list(+ streamed_result._model_input_items+ ) + list(admitted_items)+ streamed_result.new_items = list(streamed_result.new_items) + list(admitted_items)+ streamed_result._replay_from_model_input_items = list(+ streamed_result._model_input_items+ ) != list(streamed_result.new_items)+ if pending_server_items is not None:+ pending_server_items = pending_server_items + list(admitted_items)+ if server_conversation_tracker is None:+ commit_admitted_input(+ run_state,+ generated_items=streamed_result._model_input_items,+ session_items=streamed_result.new_items,+ )+ else:+ # The server owns the conversation, so the input is consumed only once the+ # model request carrying it has been accepted.+ admitted_input_awaiting_commit = True+ if current_turn == 1: if sequential_guardrails: await run_input_guardrails_with_queue(@@ -1318,6 +1356,12 @@ async def start_streaming( usage_delta(turn_usage_start, context_wrapper.usage), ) current_turn_span.finish(reset_current=True)+ if admitted_input_awaiting_commit and run_state is not None:+ commit_admitted_input(+ run_state,+ generated_items=streamed_result._model_input_items,+ session_items=streamed_result.new_items,+ ) logger.debug( "Turn %s complete, next_step type=%s", current_turn,src/agents/run_internal/streaming.pyindex 9fdd2882..a25e3cde 100644@@ -3,6 +3,7 @@ from __future__ import annotations import asyncio from ..items import (+ AdmittedInputItem, CompactionItem, HandoffCallItem, HandoffOutputItem,@@ -57,6 +58,8 @@ def stream_step_items_to_queue( event = None # approvals represent interruptions, not streamed items elif isinstance(item, CompactionItem): event = None # compaction items are session bookkeeping, not streamed items+ elif isinstance(item, AdmittedInputItem):+ event = None # admitted input is caller-supplied input, not agent-generated output else: logger.warning("Unexpected item type: %s", type(item)) event = Nonesrc/agents/run_state.pyindex 065ed859..57431ca8 100644@@ -75,9 +75,11 @@ from .guardrail import ( ) from .handoffs import Handoff, handoff as create_handoff from .items import (+ AdmittedInputItem, CompactionItem, HandoffCallItem, HandoffOutputItem,+ ItemHelpers, MCPApprovalRequestItem, MCPApprovalResponseItem, MCPListToolsItem,@@ -192,7 +194,8 @@ SCHEMA_VERSION_SUMMARIES: dict[str, str] = { "1.14": "Scopes hosted MCP approvals and restored requests by server label.", "1.15": ( "Persists canonical tool invocation identity plus sanitized mount authority and trusted "- "rebind metadata across resume flows."+ "rebind metadata, pending input staged with RunState.add_input(), admitted input items, "+ "and whether the run reached a final output across resume flows." ), } SUPPORTED_SCHEMA_VERSIONS = frozenset(SCHEMA_VERSION_SUMMARIES)@@ -339,6 +342,12 @@ class RunState(Generic[TContext, TAgent]): _sandbox: dict[str, Any] | None = field(default=None, repr=False) """Serialized sandbox resume payload for sandbox-aware runs.""" + _pending_input: list[TResponseInputItem] = field(default_factory=list)+ """Input staged with `add_input()` and admitted before the next model call."""++ _final_output_reached: bool = False+ """Whether the run that produced this state already reached a final output."""+ _schema_version: str = field(default=CURRENT_SCHEMA_VERSION, repr=False) """Schema version the snapshot was loaded from for schema-gated resume compatibility.""" @@ -380,6 +389,8 @@ class RunState(Generic[TContext, TAgent]): self._tool_use_tracker_snapshot = {} self._trace_state = None self._sandbox = None+ self._pending_input = []+ self._final_output_reached = False self._schema_version = CURRENT_SCHEMA_VERSION from .agent_tool_state import get_agent_tool_state_scope @@ -394,6 +405,65 @@ class RunState(Generic[TContext, TAgent]): return [] return self._current_step.interruptions + def add_input(self, input: str | list[TResponseInputItem]) -> None:+ """Stage input to be admitted immediately before this state's next model call.++ Accepts the same string or input-item forms as `Runner.run()`; a string becomes a user+ message. Staging does not run guardrails, tools, or model requests. When the state is+ resumed, the runner first finishes any unfinished work (for example approved tool calls),+ then runs the input guardrails of the agent that will receive the next model call plus+ `RunConfig.input_guardrails` against only the pending input, and finally admits it after+ all completed tool and handoff items. Pending input survives `to_json()`/`from_json()`.++ If those guardrails trip or raise, or the run pauses again before a model call can begin,+ the input stays pending and can be inspected with `get_pending_input()` or removed with+ `clear_pending_input()`.++ Args:+ input: A user message string or a list of input items.++ Raises:+ UserError: If the state already reached a final output, or the input is malformed.+ """+ if self._final_output_reached:+ raise UserError(+ "Cannot add input to a RunState whose run already produced a final output. "+ "Start a new run with the new input instead."+ )+ if isinstance(input, str):+ new_items = ItemHelpers.input_to_new_input_list(input)+ elif isinstance(input, list):+ for item in input:+ if not isinstance(item, Mapping):+ raise UserError(+ "RunState.add_input() expects a string or a list of input item "+ f"mappings, got an item of type {type(item).__name__}."+ )+ new_items = [+ cast(TResponseInputItem, copy.deepcopy(dict(item)))+ for item in ItemHelpers.input_to_new_input_list(input)+ ]+ else:+ raise UserError(+ "RunState.add_input() expects a string or a list of input items, got "+ f"{type(input).__name__}."+ )+ self._pending_input.extend(new_items)++ def get_pending_input(self) -> list[TResponseInputItem]:+ """Return a copy of input staged with `add_input()` that has not been admitted yet."""+ return copy.deepcopy(self._pending_input)++ def clear_pending_input(self) -> list[TResponseInputItem]:+ """Remove and return all input staged with `add_input()` that has not been admitted yet.++ Use this to drop or replace pending input, for example after an input guardrail rejected+ it during resume.+ """+ removed = self._pending_input+ self._pending_input = []+ return removed+ @staticmethod def _approval_items_match( candidate: ToolApprovalItem,@@ -1120,6 +1190,10 @@ class RunState(Generic[TContext, TAgent]): else None ) result["current_turn_persisted_item_count"] = self._current_turn_persisted_item_count+ result["pending_input"] = [+ _serialize_raw_item_value(item) for item in list(self._pending_input)+ ]+ result["final_output_reached"] = self._final_output_reached result["trace"] = self._serialize_trace_data( include_tracing_api_key=include_tracing_api_key )@@ -3410,6 +3484,11 @@ async def _build_run_state_from_json( state._trace_state = None sandbox_data = state_json.get("sandbox") state._sandbox = dict(sandbox_data) if isinstance(sandbox_data, Mapping) else None+ state._pending_input = _deserialize_pending_input(state_json.get("pending_input", []))+ final_output_reached = state_json.get("final_output_reached", False)+ if not isinstance(final_output_reached, bool):+ raise UserError("Run state final_output_reached must be a boolean")+ state._final_output_reached = final_output_reached _validate_completed_tool_invocations( state,@@ -3419,6 +3498,20 @@ async def _build_run_state_from_json( return state +def _deserialize_pending_input(pending_input_json: Any) -> list[TResponseInputItem]:+ """Restore input staged with `RunState.add_input()` from a serialized snapshot."""+ if pending_input_json is None:+ return []+ if not isinstance(pending_input_json, list):+ raise UserError("Run state pending_input must be a list of input item objects")+ pending_input: list[TResponseInputItem] = []+ for item in pending_input_json:+ if not isinstance(item, Mapping):+ raise UserError("Run state pending_input must be a list of input item objects")+ pending_input.append(cast(TResponseInputItem, copy.deepcopy(dict(item))))+ return pending_input++ def _validate_completed_tool_invocations( state: RunState[Any, Agent[Any]], *,@@ -4433,6 +4526,17 @@ def _deserialize_items( raw_item_compaction = normalized_raw_item # type: ignore[assignment] result.append(CompactionItem(agent=agent, raw_item=raw_item_compaction)) + elif item_type == "admitted_input_item":+ if not isinstance(normalized_raw_item, dict):+ raise UserError("Run state admitted input items must be JSON objects")+ # Keep the caller-supplied input verbatim so replay and dedupe fingerprints match.+ result.append(+ AdmittedInputItem(+ agent=agent,+ raw_item=cast(TResponseInputItem, normalized_raw_item),+ )+ )+ elif item_type == "mcp_list_tools_item": raw_item_mcp_list = McpListTools(**normalized_raw_item) result.append(MCPListToolsItem(agent=agent, raw_item=raw_item_mcp_list))tests/test_run_state_pending_input.pynew file mode 100644index 00000000..3a186be0@@ -0,0 +1,604 @@+"""Tests for durable pending input staged on a resumable RunState with `add_input()`."""++from __future__ import annotations++import json+from typing import Any++import pytest++from agents import (+ Agent,+ GuardrailFunctionOutput,+ InputGuardrail,+ InputGuardrailTripwireTriggered,+ RunConfig,+ RunContextWrapper,+ Runner,+ SQLiteSession,+ TResponseInputItem,+ UserError,+ function_tool,+)+from agents.items import AdmittedInputItem+from agents.run_state import CURRENT_SCHEMA_VERSION, RunState++from .fake_model import FakeModel+from .test_responses import get_function_tool_call, get_text_message+++def _user(content: str) -> Any:+ return {"content": content, "role": "user"}+++def _call_id_and_type(item: Any) -> tuple[Any, Any]:+ return item.get("call_id"), item.get("type")+++def _summarize(items: list[Any]) -> list[Any]:+ """Reduce model/session input items to comparable (kind, value) pairs."""+ summary: list[Any] = []+ for item in items:+ if item.get("role") == "user":+ summary.append(("user", item["content"]))+ elif item.get("type") == "function_call":+ summary.append(("call", item["call_id"]))+ elif item.get("type") == "function_call_output":+ summary.append(("output", item["call_id"]))+ elif item.get("role") == "assistant":+ summary.append(("assistant", item["content"][0]["text"]))+ else:+ summary.append(("other", item.get("type")))+ return summary+++EXPECTED_RESUMED_INPUT = [+ ("user", "initial"),+ ("call", "c1"),+ ("output", "c1"),+ ("user", "late"),+]+++class _CountingTool:+ def __init__(self, *, needs_approval: bool = False, name: str = "calc") -> None:+ self.calls = 0++ def _run() -> str:+ self.calls += 1+ return "42"++ self.tool = function_tool(_run, name_override=name, needs_approval=needs_approval)+++async def _after_turn_state(+ *,+ session: SQLiteSession | None = None,+ previous_response_id: str | None = None,+) -> tuple[Agent[Any], FakeModel, _CountingTool, RunState[Any]]:+ """Run a streamed turn that calls a tool, stop it after the turn, and return its state."""+ model = FakeModel()+ counter = _CountingTool()+ agent = Agent(name="assistant", model=model, tools=[counter.tool])+ model.add_multiple_turn_outputs(+ [+ [get_function_tool_call("calc", json.dumps({}), call_id="c1")],+ [get_text_message("done")],+ ]+ )+ result = Runner.run_streamed(+ agent,+ input="initial",+ session=session,+ previous_response_id=previous_response_id,+ )+ async for event in result.stream_events():+ if event.type == "run_item_stream_event" and event.name == "tool_called":+ result.cancel(mode="after_turn")+ assert counter.calls == 1+ assert result.final_output is None+ return agent, model, counter, result.to_state()+++@pytest.mark.asyncio+async def test_after_turn_state_admits_late_input_after_completed_tool_output() -> None:+ agent, model, counter, state = await _after_turn_state()+ state.add_input("late")++ result = await Runner.run(agent, state)++ assert _summarize(model.last_turn_args["input"]) == EXPECTED_RESUMED_INPUT+ assert result.final_output == "done"+ # The completed tool call must not run again.+ assert counter.calls == 1+ assert state.get_pending_input() == []+ admitted = [item for item in result.new_items if isinstance(item, AdmittedInputItem)]+ assert [item.raw_item for item in admitted] == [_user("late")]+ assert _summarize(result.to_input_list()) == EXPECTED_RESUMED_INPUT + [("assistant", "done")]+ assert _summarize(result.to_input_list(mode="normalized")) == EXPECTED_RESUMED_INPUT + [+ ("assistant", "done")+ ]+++@pytest.mark.asyncio+async def test_multiple_pending_inputs_preserve_insertion_order() -> None:+ agent, model, _counter, state = await _after_turn_state()+ state.add_input("first")+ state.add_input([_user("second"), _user("third")])+ state.add_input("fourth")++ assert state.get_pending_input() == [+ _user("first"),+ _user("second"),+ _user("third"),+ _user("fourth"),+ ]++ await Runner.run(agent, state)++ assert _summarize(model.last_turn_args["input"])[3:] == [+ ("user", "first"),+ ("user", "second"),+ ("user", "third"),+ ("user", "fourth"),+ ]+++@pytest.mark.asyncio+async def test_add_input_copies_caller_items_and_rejects_malformed_input() -> None:+ _agent, _model, _counter, state = await _after_turn_state()+ item: dict[str, Any] = _user("original")+ state.add_input([item]) # type: ignore[list-item]+ item["content"] = "mutated"+ assert state.get_pending_input() == [_user("original")]++ with pytest.raises(UserError):+ state.add_input([_user("ok"), "not an item"]) # type: ignore[list-item]+ with pytest.raises(UserError):+ state.add_input(42) # type: ignore[arg-type]+ # A rejected call must not stage any part of its input.+ assert state.get_pending_input() == [_user("original")]+++@pytest.mark.asyncio+async def test_pending_input_survives_serialization_round_trips() -> None:+ agent, model, counter, state = await _after_turn_state()+ state.add_input("late")++ serialized = state.to_json()+ assert serialized["$schemaVersion"] == CURRENT_SCHEMA_VERSION+ assert serialized["pending_input"] == [_user("late")]++ restored = await RunState.from_json(agent, serialized)+ assert restored.get_pending_input() == [_user("late")]+ restored = await RunState.from_string(agent, restored.to_string())+ assert restored.get_pending_input() == [_user("late")]++ result = await Runner.run(agent, restored)++ assert _summarize(model.last_turn_args["input"]) == EXPECTED_RESUMED_INPUT+ assert counter.calls == 1+ # The admitted input is now ordinary history in later snapshots, not pending input.+ later_state = result.to_state()+ assert later_state.get_pending_input() == []+ later_json = later_state.to_json()+ assert later_json["pending_input"] == []+ assert any(item["type"] == "admitted_input_item" for item in later_json["generated_items"])+ reloaded = await RunState.from_json(agent, later_json)+ assert [+ item.raw_item for item in reloaded._generated_items if isinstance(item, AdmittedInputItem)+ ] == [_user("late")]+++@pytest.mark.asyncio+async def test_snapshot_without_pending_input_field_restores_with_none() -> None:+ agent, _model, _counter, state = await _after_turn_state()+ serialized = state.to_json()+ del serialized["pending_input"]+ del serialized["final_output_reached"]+ serialized["$schemaVersion"] = "1.14"++ restored = await RunState.from_json(agent, serialized)++ assert restored.get_pending_input() == []+ assert restored._final_output_reached is False+++@pytest.mark.asyncio+async def test_session_persists_admitted_input_exactly_once() -> None:+ session = SQLiteSession("pending_input_session")+ await session.clear_session()+ agent, model, _counter, state = await _after_turn_state(session=session)+ state.add_input("late")++ await Runner.run(agent, state, session=session)++ items = await session.get_items()+ assert _summarize(list(items)) == EXPECTED_RESUMED_INPUT + [("assistant", "done")]+ assert _summarize(model.last_turn_args["input"]) == EXPECTED_RESUMED_INPUT+ session.close()+++@pytest.mark.asyncio+async def test_server_managed_resume_sends_admitted_input_once_as_unsent_delta() -> None:+ agent, model, counter, state = await _after_turn_state(previous_response_id="resp-0")+ approval_tool = _CountingTool(needs_approval=True, name="approve_me")+ agent.tools.append(approval_tool.tool)+ model.add_multiple_turn_outputs(+ [+ [get_function_tool_call("approve_me", json.dumps({}), call_id="c2")],+ [get_text_message("finished")],+ ]+ )+ # Drop the queued "done" output so the resumed turn asks for an approval instead.+ model.turn_outputs.pop(0)+ state.add_input("late")++ result = await Runner.run(agent, state)++ # The server already holds the initial input and tool call; only the local tool output and+ # the late input are sent, in that order, chained to the previous response.+ assert _summarize(model.last_turn_args["input"]) == [("output", "c1"), ("user", "late")]+ assert model.last_turn_args["previous_response_id"] == "resp-789"+ assert counter.calls == 1+ assert result.interruptions++ interrupted = await RunState.from_json(agent, result.to_state().to_json())+ assert interrupted.get_pending_input() == []+ interrupted.approve(interrupted.get_interruptions()[0])+ final = await Runner.run(agent, interrupted)++ # Resuming a later snapshot must not resend the already delivered input.+ assert _summarize(model.last_turn_args["input"]) == [("output", "c2")]+ assert final.final_output == "finished"+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed", [False, True])+@pytest.mark.parametrize("with_pending_input", [False, True])+async def test_server_managed_after_turn_resume_sends_unsent_tool_output(+ streamed: bool, with_pending_input: bool+) -> None:+ agent, model, counter, state = await _after_turn_state(previous_response_id="resp-0")+ if with_pending_input:+ state.add_input("late")++ if streamed:+ result = Runner.run_streamed(agent, state)+ async for _ in result.stream_events():+ pass+ else:+ await Runner.run(agent, state)++ # The stopped turn's local tool output was never delivered, so the resumed request owes it,+ # followed by any admitted input.+ expected = [("output", "c1")] + ([("user", "late")] if with_pending_input else [])+ assert _summarize(model.last_turn_args["input"]) == expected+ assert state.get_pending_input() == []+ assert model.last_turn_args["previous_response_id"] == "resp-789"+ assert counter.calls == 1+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed", [False, True])+async def test_server_managed_resume_sends_admitted_input_repeating_earlier_text(+ streamed: bool,+) -> None:+ agent, model, _counter, state = await _after_turn_state(previous_response_id="resp-0")+ model.turn_outputs.insert(0, [get_function_tool_call("calc", json.dumps({}), call_id="c2")])+ # Same text as the original input, which the server already holds.+ state.add_input("initial")++ streamed_result = Runner.run_streamed(agent, state)+ async for event in streamed_result.stream_events():+ if event.type == "run_item_stream_event" and event.name == "tool_called":+ streamed_result.cancel(mode="after_turn")+ assert _summarize(model.last_turn_args["input"]) == [("output", "c1"), ("user", "initial")]++ later = await RunState.from_json(agent, streamed_result.to_state().to_json())+ # Same text as an earlier admitted input that was already delivered.+ later.add_input("initial")+ if streamed:+ resumed = Runner.run_streamed(agent, later)+ async for _ in resumed.stream_events():+ pass+ else:+ await Runner.run(agent, later)++ assert _summarize(model.last_turn_args["input"]) == [("output", "c2"), ("user", "initial")]+ assert later.get_pending_input() == []+++@pytest.mark.asyncio+async def test_server_managed_model_failure_keeps_input_pending_until_delivered() -> None:+ agent, model, _counter, state = await _after_turn_state(previous_response_id="resp-0")+ model.turn_outputs.insert(0, RuntimeError("model unavailable"))+ state.add_input("late")++ with pytest.raises(RuntimeError, match="model unavailable"):+ await Runner.run(agent, state)++ assert state.get_pending_input() == [_user("late")]++ await Runner.run(agent, state)++ assert _summarize(model.last_turn_args["input"]) == [("output", "c1"), ("user", "late")]+ assert state.get_pending_input() == []+++async def _approval_interruption() -> tuple[+ Agent[Any], FakeModel, _CountingTool, _CountingTool, Any+]:+ model = FakeModel()+ first = _CountingTool(needs_approval=True, name="first")+ second = _CountingTool(needs_approval=True, name="second")+ agent = Agent(name="assistant", model=model, tools=[first.tool, second.tool])+ model.add_multiple_turn_outputs(+ [+ [+ get_function_tool_call("first", json.dumps({}), call_id="c1"),+ get_function_tool_call("second", json.dumps({}), call_id="c2"),+ ],+ [get_text_message("done")],+ ]+ )+ result = await Runner.run(agent, "initial")+ assert len(result.interruptions) == 2+ return agent, model, first, second, result+++@pytest.mark.asyncio+async def test_approved_tools_run_before_pending_input_is_admitted() -> None:+ agent, model, first, second, result = await _approval_interruption()+ state = result.to_state()+ state.add_input("late")+ for interruption in state.get_interruptions():+ state.approve(interruption)++ resumed = await Runner.run(agent, state)++ assert first.calls == 1+ assert second.calls == 1+ summary = _summarize(model.last_turn_args["input"])+ assert summary[0] == ("user", "initial")+ assert summary[-1] == ("user", "late")+ assert set(summary[1:-1]) == {+ ("call", "c1"),+ ("call", "c2"),+ ("output", "c1"),+ ("output", "c2"),+ }+ assert resumed.final_output == "done"+++@pytest.mark.asyncio+async def test_unresolved_approvals_keep_input_pending() -> None:+ agent, model, first, second, result = await _approval_interruption()+ state = result.to_state()+ state.add_input("late")+ state.approve(state.get_interruptions()[0])+ turn_args_before = model.last_turn_args++ still_interrupted = await Runner.run(agent, state)++ assert still_interrupted.interruptions+ # No model request could begin, so the input stays pending on the derived state.+ assert model.last_turn_args is turn_args_before+ next_state = await RunState.from_json(agent, still_interrupted.to_state().to_json())+ assert next_state.get_pending_input() == [_user("late")]++ for interruption in next_state.get_interruptions():+ next_state.approve(interruption)+ final = await Runner.run(agent, next_state)++ assert final.final_output == "done"+ assert first.calls == 1+ assert second.calls == 1+ summary = _summarize(model.last_turn_args["input"])+ assert summary[-1] == ("user", "late")+ assert summary.count(("user", "late")) == 1+++def _tripping_guardrail(seen: list[Any], *, trip_on: str) -> InputGuardrail[Any]:+ def _check(+ ctx: RunContextWrapper[Any], agent: Agent[Any], input: str | list[TResponseInputItem]+ ) -> GuardrailFunctionOutput:+ seen.append(input)+ text = json.dumps(input)+ return GuardrailFunctionOutput(output_info=None, tripwire_triggered=trip_on in text)++ return InputGuardrail(guardrail_function=_check, name=f"trip_on_{trip_on}")+++@pytest.mark.asyncio+async def test_guardrail_trip_blocks_model_call_and_keeps_input_recoverable() -> None:+ agent, model, counter, state = await _after_turn_state()+ agent_seen: list[Any] = []+ config_seen: list[Any] = []+ agent.input_guardrails = [_tripping_guardrail(agent_seen, trip_on="forbidden")]+ run_config = RunConfig(input_guardrails=[_tripping_guardrail(config_seen, trip_on="never")])+ state.add_input("forbidden request")+ turn_args_before = model.last_turn_args++ with pytest.raises(InputGuardrailTripwireTriggered):+ await Runner.run(agent, state, run_config=run_config)++ assert model.last_turn_args is turn_args_before+ assert counter.calls == 1+ # Guardrails evaluate only the newly pending input, not the replayed history.+ assert agent_seen == [[_user("forbidden request")]]+ assert state.get_pending_input() == [_user("forbidden request")]+ restored = await RunState.from_json(agent, state.to_json())+ assert restored.get_pending_input() == [_user("forbidden request")]++ assert restored.clear_pending_input() == [_user("forbidden request")]+ restored.add_input("late")+ result = await Runner.run(agent, restored, run_config=run_config)++ assert _summarize(model.last_turn_args["input"]) == EXPECTED_RESUMED_INPUT+ assert counter.calls == 1+ assert result.final_output == "done"+ assert {r.guardrail.get_name() for r in result.input_guardrail_results} >= {+ "trip_on_forbidden",+ "trip_on_never",+ }+++@pytest.mark.asyncio+async def test_streamed_guardrail_trip_blocks_model_call_and_keeps_input_pending() -> None:+ agent, model, counter, state = await _after_turn_state()+ seen: list[Any] = []+ agent.input_guardrails = [_tripping_guardrail(seen, trip_on="forbidden")]+ state.add_input("forbidden request")+ turn_args_before = model.last_turn_args++ streamed = Runner.run_streamed(agent, state)+ with pytest.raises(InputGuardrailTripwireTriggered):+ async for _ in streamed.stream_events():+ pass++ assert model.last_turn_args is turn_args_before+ assert counter.calls == 1+ assert seen == [[_user("forbidden request")]]+ assert state.get_pending_input() == [_user("forbidden request")]+++@pytest.mark.asyncio+async def test_guardrail_error_keeps_input_pending() -> None:+ agent, model, _counter, state = await _after_turn_state()++ def _raise(+ ctx: RunContextWrapper[Any], agent: Agent[Any], input: str | list[TResponseInputItem]+ ) -> GuardrailFunctionOutput:+ raise ValueError("guardrail backend down")++ agent.input_guardrails = [InputGuardrail(guardrail_function=_raise)]+ state.add_input("late")+ turn_args_before = model.last_turn_args++ with pytest.raises(ValueError, match="guardrail backend down"):+ await Runner.run(agent, state)++ assert model.last_turn_args is turn_args_before+ assert state.get_pending_input() == [_user("late")]+++@pytest.mark.asyncio+async def test_streaming_and_non_streaming_resume_produce_equivalent_history() -> None:+ sync_session = SQLiteSession("pending_input_sync")+ stream_session = SQLiteSession("pending_input_stream")+ await sync_session.clear_session()+ await stream_session.clear_session()++ agent, sync_model, _counter, sync_state = await _after_turn_state(session=sync_session)+ sync_state.add_input("late")+ sync_result = await Runner.run(agent, sync_state, session=sync_session)++ stream_agent, stream_model, _counter2, stream_state = await _after_turn_state(+ session=stream_session+ )+ stream_state.add_input("late")+ stream_result = Runner.run_streamed(stream_agent, stream_state, session=stream_session)+ async for _ in stream_result.stream_events():+ pass++ assert _summarize(stream_model.last_turn_args["input"]) == EXPECTED_RESUMED_INPUT+ assert _summarize(sync_model.last_turn_args["input"]) == _summarize(+ stream_model.last_turn_args["input"]+ )+ assert _summarize(sync_result.to_input_list()) == _summarize(stream_result.to_input_list())+ assert _summarize(sync_result.to_input_list(mode="normalized")) == _summarize(+ stream_result.to_input_list(mode="normalized")+ )+ assert _summarize(list(await sync_session.get_items())) == _summarize(+ list(await stream_session.get_items())+ )+ assert stream_state.get_pending_input() == []+ assert stream_result.to_state().get_pending_input() == []+ sync_session.close()+ stream_session.close()+++@pytest.mark.asyncio+async def test_streamed_after_turn_cancel_before_admission_keeps_input_pending() -> None:+ agent, model, _counter, state = await _after_turn_state()+ state.add_input("late")+ turn_args_before = model.last_turn_args++ streamed = Runner.run_streamed(agent, state)+ streamed.cancel(mode="after_turn")+ async for _ in streamed.stream_events():+ pass++ assert model.last_turn_args is turn_args_before+ assert streamed.to_state().get_pending_input() == [_user("late")]+++@pytest.mark.asyncio+async def test_retrying_failed_model_request_does_not_duplicate_admitted_input() -> None:+ session = SQLiteSession("pending_input_retry")+ await session.clear_session()+ agent, model, counter, state = await _after_turn_state(session=session)+ model.turn_outputs.insert(0, RuntimeError("model unavailable"))+ state.add_input("late")++ with pytest.raises(RuntimeError, match="model unavailable"):+ await Runner.run(agent, state, session=session)++ # The session already accepted the input, so the state records it as consumed history.+ assert state.get_pending_input() == []+ retry_state = await RunState.from_json(agent, state.to_json())++ result = await Runner.run(agent, retry_state, session=session)++ assert _summarize(model.last_turn_args["input"]) == EXPECTED_RESUMED_INPUT+ assert _summarize(list(await session.get_items())) == EXPECTED_RESUMED_INPUT + [+ ("assistant", "done")+ ]+ assert counter.calls == 1+ assert result.final_output == "done"+ session.close()+++@pytest.mark.asyncio+async def test_add_input_to_terminal_state_fails_before_side_effects() -> None:+ model = FakeModel()+ agent = Agent(name="assistant", model=model)+ model.set_next_output([get_text_message("all done")])+ result = await Runner.run(agent, "initial")+ state = result.to_state()++ with pytest.raises(UserError, match="final output"):+ state.add_input("late")+ assert state.get_pending_input() == []++ restored = await RunState.from_json(agent, state.to_json())+ with pytest.raises(UserError, match="final output"):+ restored.add_input("late")++ streamed = Runner.run_streamed(agent, "initial")+ model.set_next_output([get_text_message("all done")])+ async for _ in streamed.stream_events():+ pass+ with pytest.raises(UserError, match="final output"):+ streamed.to_state().add_input("late")+++@pytest.mark.asyncio+async def test_resuming_terminal_snapshot_with_pending_input_is_rejected() -> None:+ model = FakeModel()+ agent = Agent(name="assistant", model=model)+ model.set_next_output([get_text_message("all done")])+ result = await Runner.run(agent, "initial")+ serialized = result.to_state().to_json()+ serialized["pending_input"] = [_user("late")]+ state = await RunState.from_json(agent, serialized)+ turn_args_before = model.last_turn_args++ with pytest.raises(UserError, match="final output"):+ await Runner.run(agent, state)+ with pytest.raises(UserError, match="final output"):+ Runner.run_streamed(agent, state)++ assert model.last_turn_args is turn_args_before+ assert state.get_pending_input() == [_user("late")]
src/agents/__init__.pyindex 8eeb38b20..3105b4aa5 100644@@ -56,6 +56,7 @@ from .items import ( CompactionItem, HandoffCallItem, HandoffOutputItem,+ InputItem, ItemHelpers, MCPApprovalRequestItem, MCPApprovalResponseItem,@@ -434,6 +435,7 @@ __all__ = [ "HandoffInputData", "HandoffInputFilter", "TResponseInputItem",+ "InputItem", "MessageOutputItem", "ModelResponse", "RunItem",src/agents/items.pyindex 115be2a63..f3d2d1a46 100644@@ -6,6 +6,7 @@ import weakref from collections.abc import Mapping from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, Generic, Literal, TypeAlias, TypeVar, cast+from uuid import uuid4 import pydantic from openai.types.responses import (@@ -157,6 +158,19 @@ class RunItemBase(Generic[T], abc.ABC): raise AgentsException(f"Unexpected raw item type: {type(self.raw_item)}") +@dataclass+class InputItem(RunItemBase[TResponseInputItem]):+ """Represents input admitted while resuming a run."""++ raw_item: TResponseInputItem+ """The normalized input item admitted before the next model call."""++ type: Literal["input_item"] = "input_item"++ input_id: str = field(default_factory=lambda: uuid4().hex)+ """A durable occurrence identifier used for exactly-once conversation tracking."""++ @dataclass class MessageOutputItem(RunItemBase[ResponseOutputMessage]): """Represents a message from the LLM."""@@ -669,7 +683,8 @@ class ToolApprovalItem(RunItemBase[Any]): RunItem: TypeAlias = (- MessageOutputItem+ InputItem+ | MessageOutputItem | ToolSearchCallItem | ToolSearchOutputItem | HandoffCallItemsrc/agents/result.pyindex 6fa06a331..fc5a938f7 100644@@ -146,8 +146,12 @@ def _populate_state_from_result( source_state = getattr(result, "_state", None) if isinstance(source_state, RunState): state._generated_prompt_cache_key = source_state._generated_prompt_cache_key+ state._pending_input = copy.deepcopy(source_state._pending_input)+ state._current_step = source_state._current_step else: state._generated_prompt_cache_key = getattr(result, "_generated_prompt_cache_key", None)+ state._pending_input = copy.deepcopy(getattr(result, "_pending_input_for_state", []))+ state._current_step = getattr(result, "_current_step_for_state", None) state._reasoning_item_id_policy = getattr(result, "_reasoning_item_id_policy", None) interruptions = list(getattr(result, "interruptions", []))@@ -299,6 +303,12 @@ class RunResultBase(abc.ABC): """Root agent graph used when converting the result back into RunState.""" _generated_prompt_cache_key: str | None = field(default=None, init=False, repr=False) """SDK-generated prompt cache key captured during the run."""+ _pending_input_for_state: list[TResponseInputItem] = field(+ default_factory=list, init=False, repr=False+ )+ """Pending input preserved when a non-streaming result is converted back to RunState."""+ _current_step_for_state: Any = field(default=None, init=False, repr=False)+ """Current step preserved when a non-streaming result is converted back to RunState.""" @classmethod def __get_pydantic_core_schema__(src/agents/run.pyindex ca3941085..c2c4839fb 100644@@ -27,7 +27,9 @@ from .guardrail import ( OutputGuardrailResult, ) from .items import (+ InputItem, ItemHelpers,+ ModelResponse, RunItem, TResponseInputItem, )@@ -110,9 +112,12 @@ from .run_internal.run_steps import ( NextStepHandoff, NextStepInterruption, NextStepRunAgain,+ ProcessedResponse, ) from .run_internal.session_persistence import ( _session_get_items,+ admit_pending_input,+ commit_server_pending_input, persist_session_items_for_guardrail_trip, prepare_input_with_session, reconcile_nested_history_owned_session_item_refs,@@ -775,6 +780,8 @@ class AgentRunner: finalized_result._generated_prompt_cache_key = ( run_state._generated_prompt_cache_key )+ finalized_result._pending_input_for_state = run_state.pending_input+ finalized_result._current_step_for_state = run_state._current_step finalized_result._nested_history_owned_session_item_refs = list( run_state._nested_history_owned_session_item_refs )@@ -782,9 +789,44 @@ class AgentRunner: return finalized_result pending_server_items: list[RunItem] | None = None+ pending_input_admission_items: list[InputItem] = [] input_guardrail_results: list[InputGuardrailResult] = ( list(run_state._input_guardrail_results) if run_state is not None else [] )+ input_guardrail_attempt_start = len(input_guardrail_results)++ def _attempt_input_guardrail_results() -> list[InputGuardrailResult]:+ return input_guardrail_results[input_guardrail_attempt_start:]++ def _commit_pending_server_response(+ model_response: ModelResponse,+ processed_response: ProcessedResponse | None,+ ) -> bool:+ if (+ run_state is None+ or server_conversation_tracker is None+ or not pending_input_admission_items+ ):+ return False+ return commit_server_pending_input(+ run_state=run_state,+ tracker=server_conversation_tracker,+ admission_items=pending_input_admission_items,+ generated_items=generated_items,+ session_items=session_items,+ model_response=model_response,+ processed_response=processed_response,+ current_turn=current_turn,+ )++ def _mark_response_hooks_started() -> None:+ if run_state is None or not isinstance(+ run_state._current_step, NextStepInterruption+ ):+ return+ if run_state._current_step.response_accepted:+ run_state._current_step.llm_end_hooks_started = True+ # Output guardrails run once, at the end of the run. Accumulate their results # here so the failure handler below can report them on the raised exception. output_guardrail_results: list[OutputGuardrailResult] = []@@ -946,11 +988,15 @@ class AgentRunner: if run_state is not None and run_state._current_step is not None: if isinstance(run_state._current_step, NextStepInterruption): logger.debug("Continuing from interruption")- if (- not run_state._model_responses- or not run_state._last_processed_response- ):+ if not run_state._model_responses: raise UserError("No model response found in previous state")+ if run_state._last_processed_response is None:+ if run_state._current_step.response_accepted:+ raise UserError(+ "An accepted model response could not be processed; "+ "start a new run instead of retrying it"+ )+ raise UserError("No processed response found in previous state") turn_result = await resolve_interrupted_turn( bindings=current_bindings,@@ -963,6 +1009,7 @@ class AgentRunner: run_config=run_config, server_manages_conversation=server_conversation_tracker is not None, run_state=run_state,+ error_handlers=error_handlers, ) if run_state._last_processed_response is not None:@@ -1129,6 +1176,7 @@ class AgentRunner: wrapper=context_wrapper, ) result._original_input = copy_input_items(original_input)+ run_state._current_step = None return _finalize_result(result) elif isinstance(turn_result.next_step, NextStepHandoff): current_agent = cast(@@ -1148,7 +1196,42 @@ class AgentRunner: if run_state is not None: if run_state._current_step is None:- run_state._current_step = NextStepRunAgain() # type: ignore[assignment]+ run_state._current_step = NextStepRunAgain()++ pending_input = run_state.pending_input+ if pending_input:+ pending_guardrails = current_agent.input_guardrails + (+ run_config.input_guardrails or []+ )+ try:+ await run_input_guardrails(+ current_agent,+ pending_guardrails,+ pending_input,+ context_wrapper,+ input_guardrail_results,+ )+ finally:+ run_state._input_guardrail_results = list(input_guardrail_results)++ admission_items = await admit_pending_input(+ run_state=run_state,+ agent=current_agent,+ session=session,+ server_conversation_tracker=server_conversation_tracker,+ store=store_setting,+ wrapper=context_wrapper,+ )+ generated_items.extend(admission_items)+ session_items.extend(admission_items)+ if pending_server_items is not None:+ pending_server_items.extend(admission_items)+ pending_input_admission_items = [+ item for item in admission_items if isinstance(item, InputItem)+ ]+ if not run_state._pending_input:+ run_state._generated_items = list(generated_items)+ run_state._session_items = list(session_items) all_tools = await get_all_tools(execution_agent, context_wrapper) all_tools = await initialize_computer_tools( tools=all_tools, context_wrapper=context_wrapper@@ -1343,6 +1426,9 @@ class AgentRunner: prompt_cache_key_resolver=prompt_cache_key_resolver, error_handlers=error_handlers, agent_span=current_span,+ on_response_accepted=_commit_pending_server_response,+ on_response_hooks_started=_mark_response_hooks_started,+ run_state=run_state, ) ) @@ -1415,6 +1501,9 @@ class AgentRunner: prompt_cache_key_resolver=prompt_cache_key_resolver, error_handlers=error_handlers, agent_span=current_span,+ on_response_accepted=_commit_pending_server_response,+ on_response_hooks_started=_mark_response_hooks_started,+ run_state=run_state, ) finally: if current_turn_span is not None:@@ -1436,6 +1525,14 @@ class AgentRunner: # Accumulate unfiltered items for observability. turn_session_items = session_items_for_turn(turn_result) session_items.extend(turn_session_items)+ if pending_input_admission_items and run_state is not None:+ run_state._generated_items = list(generated_items)+ run_state._session_items = list(session_items)+ run_state._model_responses = list(model_responses)+ run_state._last_processed_response = turn_result.processed_response+ run_state._current_turn = current_turn+ run_state._mark_generated_items_merged_with_last_processed()+ pending_input_admission_items = [] if run_state is not None and turn_result.nested_history_owned_items is not None: run_state._nested_history_owned_session_item_refs = ( reconcile_nested_history_owned_session_item_refs(@@ -1538,7 +1635,7 @@ class AgentRunner: session=session, run_state=run_state, session_persistence_enabled=session_persistence_enabled,- input_guardrail_results=input_guardrail_results,+ input_guardrail_results=_attempt_input_guardrail_results(), items=_retained_items_for_blocked_output(items_to_save_turn), response_id=turn_result.model_response.response_id, store=store_setting,@@ -1552,7 +1649,7 @@ class AgentRunner: session=session, run_state=run_state, session_persistence_enabled=session_persistence_enabled,- input_guardrail_results=input_guardrail_results,+ input_guardrail_results=_attempt_input_guardrail_results(), items=items_to_save_turn, response_id=turn_result.model_response.response_id, store=store_setting,@@ -1564,7 +1661,7 @@ class AgentRunner: session=session, run_state=run_state, session_persistence_enabled=session_persistence_enabled,- input_guardrail_results=input_guardrail_results,+ input_guardrail_results=_attempt_input_guardrail_results(), items=items_to_save_turn, response_id=turn_result.model_response.response_id, store=store_setting,@@ -1600,10 +1697,14 @@ class AgentRunner: run_state._current_turn_persisted_item_count ) result._original_input = copy_input_items(original_input)+ if run_state is not None:+ run_state._current_step = None return _finalize_result(result) elif isinstance(turn_result.next_step, NextStepInterruption): if session_persistence_enabled:- if not input_guardrails_triggered(input_guardrail_results):+ if not input_guardrails_triggered(+ _attempt_input_guardrail_results()+ ): # Persist session items but skip approval placeholders. input_items_for_save_interruption: list[TResponseInputItem] = ( session_input_items_for_persistencesrc/agents/run_internal/agent_runner_helpers.pyindex a8b65e57d..f4dca6b1a 100644@@ -202,8 +202,20 @@ def _extract_tool_call_id(raw: Any) -> str | None: def get_unsent_tool_call_ids_for_interrupted_state(run_state: RunState[Any] | None) -> set[str]:- """Return tool call IDs whose local outputs belong to the current interruption."""- if run_state is None or not isinstance(run_state._current_step, NextStepInterruption):+ """Return tool call IDs whose local outputs have not reached a server conversation."""+ if run_state is None:+ return set()++ if isinstance(run_state._current_step, NextStepRunAgain):+ if not run_state._model_responses:+ return set()+ return {+ call_id+ for item in run_state._model_responses[-1].output+ if (call_id := _extract_tool_call_id(item)) is not None+ }++ if not isinstance(run_state._current_step, NextStepInterruption): return set() processed_response = run_state._last_processed_responsesrc/agents/run_internal/oai_conversation.pyindex 38fa5a7d3..1d00cc79a 100644@@ -9,7 +9,9 @@ from collections.abc import Sequence from dataclasses import dataclass, field from typing import Any, cast +from ..exceptions import UserError from ..items import (+ InputItem, ItemHelpers, ModelResponse, RunItem,@@ -114,7 +116,8 @@ def _untrack_object(items: list[Any], candidate: Any) -> None: return -_PreparedItemSource = tuple[TResponseInputItem, TResponseInputItem]+_PreparedItemSource = tuple[TResponseInputItem, TResponseInputItem, str | None]+_PreparedSourceIdentity = tuple[TResponseInputItem, str | None] @dataclass@@ -145,8 +148,9 @@ class OpenAIServerConversationTracker: server_tool_call_ids: set[str] = field(default_factory=set) server_output_fingerprints: set[str] = field(default_factory=set) - # Content-based dedupe for resume/retry paths where objects are reconstructed.+ # Durable occurrence and content dedupe for resume/retry paths where objects are rebuilt. sent_item_fingerprints: set[str] = field(default_factory=set)+ accepted_input_item_ids: set[str] = field(default_factory=set) restored_anonymous_tool_search_fingerprints: set[str] = field(default_factory=set) sent_initial_input: bool = False remaining_initial_input: list[TResponseInputItem] | None = None@@ -157,9 +161,10 @@ class OpenAIServerConversationTracker: # mark_input_as_sent() can mark the right object identities after the model call succeeds. # Keep the prepared item alive so its object ID cannot be reused before the input is marked. prepared_item_sources: dict[int, _PreparedItemSource] = field(default_factory=dict)- prepared_item_sources_by_fingerprint: dict[str, list[TResponseInputItem]] = field(+ prepared_item_sources_by_fingerprint: dict[str, list[_PreparedSourceIdentity]] = field( default_factory=dict )+ delivered_item_sources: dict[int, _PreparedItemSource] = field(default_factory=dict) def __post_init__(self): """Log initial tracker state to make conversation resume behavior debuggable."""@@ -263,6 +268,7 @@ class OpenAIServerConversationTracker: if raw_item is None: continue is_tool_call_item = run_item.type in {"tool_call_item", "handoff_call_item"}+ is_input_item = isinstance(run_item, InputItem) is_tool_search_item = run_item.type in { "tool_search_call_item", "tool_search_output_item",@@ -282,6 +288,7 @@ class OpenAIServerConversationTracker: should_mark = ( item_id is not None or (has_call_id and (has_output_payload or is_tool_call_item))+ or is_input_item or is_tool_search_item ) if not should_mark:@@ -289,7 +296,9 @@ class OpenAIServerConversationTracker: _track_object_once(self.sent_items, raw_item) fp = _fingerprint_for_tracker(raw_item)- if fp:+ if is_input_item:+ self.accepted_input_item_ids.add(run_item.input_id)+ elif fp: self.sent_item_fingerprints.add(fp) if is_tool_search_item: self.server_output_fingerprints.add(fp)@@ -315,6 +324,7 @@ class OpenAIServerConversationTracker: should_mark = ( item_id is not None or (has_call_id and (has_output_payload or is_tool_call_item))+ or is_input_item or is_tool_search_item ) if not should_mark:@@ -322,7 +332,9 @@ class OpenAIServerConversationTracker: _track_object_once(self.sent_items, raw_item) fp = _fingerprint_for_tracker(raw_item)- if fp:+ if is_input_item:+ self.accepted_input_item_ids.add(run_item.input_id)+ elif fp: self.sent_item_fingerprints.add(fp) if is_tool_search_item: self.server_output_fingerprints.add(fp)@@ -389,7 +401,7 @@ class OpenAIServerConversationTracker: for item in items: if item is None: continue- source_item = self._consume_prepared_item_source(item)+ source_item, input_id = self._delivery_source(item) if _is_tracked_object(delivered_sources, source_item): continue delivered_sources.append(source_item)@@ -397,7 +409,8 @@ class OpenAIServerConversationTracker: fp = _fingerprint_for_tracker(source_item) if fp: delivered_by_content.add(fp)- self.sent_item_fingerprints.add(fp)+ if input_id is None:+ self.sent_item_fingerprints.add(fp) if not self.remaining_initial_input: return@@ -413,6 +426,72 @@ class OpenAIServerConversationTracker: self.remaining_initial_input = remaining or None + def validate_pending_input_filter(self, items: Sequence[TResponseInputItem]) -> None:+ """Reject filter rewrites whose pending-input lineage cannot be determined safely."""+ pending_ids = {+ input_id+ for _prepared_item, _source_item, input_id in self.prepared_item_sources.values()+ if input_id is not None+ }+ if not pending_ids:+ return++ sources_by_fingerprint = {+ fingerprint: list(sources)+ for fingerprint, sources in self.prepared_item_sources_by_fingerprint.items()+ }+ filtered_counts_by_fingerprint: dict[str, int] = {}+ for item in items:+ fingerprint = _fingerprint_for_tracker(item)+ if fingerprint:+ filtered_counts_by_fingerprint[fingerprint] = (+ filtered_counts_by_fingerprint.get(fingerprint, 0) + 1+ )++ matched_pending_ids: set[str] = set()+ unmatched_filtered_item = False+ for item in items:+ direct_entry = self.prepared_item_sources.get(id(item))+ source: _PreparedSourceIdentity | None = None+ if direct_entry is not None and direct_entry[0] is item:+ source = (direct_entry[1], direct_entry[2])++ fingerprint = _fingerprint_for_tracker(item)+ candidates = (+ sources_by_fingerprint.get(fingerprint, []) if fingerprint is not None else []+ )+ if source is not None:+ for index, candidate in enumerate(candidates):+ if candidate[0] is source[0] and candidate[1] == source[1]:+ candidates.pop(index)+ break+ elif candidates:+ candidate_input_ids = {candidate[1] for candidate in candidates}+ if (+ None in candidate_input_ids+ and len(candidate_input_ids) > 1+ and fingerprint is not None+ and filtered_counts_by_fingerprint.get(fingerprint, 0) < len(candidates)+ ):+ raise UserError(+ "call_model_input_filter cannot safely associate a reconstructed item "+ "with pending RunState input. Preserve the input item object, return an "+ "unchanged copy, or omit the pending item."+ )+ source = candidates.pop(0)+ else:+ unmatched_filtered_item = True++ if source is not None and source[1] is not None:+ matched_pending_ids.add(source[1])++ if pending_ids - matched_pending_ids and unmatched_filtered_item:+ raise UserError(+ "call_model_input_filter cannot safely associate a reconstructed item with "+ "pending RunState input. Preserve the input item object, return an unchanged "+ "copy, or omit the pending item."+ )+ def rewind_input(self, items: Sequence[TResponseInputItem]) -> None: """Rewind previously marked inputs so they can be resent.""" if not items:@@ -422,11 +501,11 @@ class OpenAIServerConversationTracker: for item in items: if item is None: continue- source_item = self._consume_prepared_item_source(item)+ source_item, input_id = self._delivery_source(item) rewind_items.append(source_item) _untrack_object(self.sent_items, source_item) fp = _fingerprint_for_tracker(source_item)- if fp:+ if input_id is None and fp: self.sent_item_fingerprints.discard(fp) if not rewind_items:@@ -444,10 +523,11 @@ class OpenAIServerConversationTracker: """Assemble the next model input while skipping duplicates and approvals.""" self.prepared_item_sources.clear() self.prepared_item_sources_by_fingerprint.clear()+ self.delivered_item_sources.clear() prepared_initial_items: list[TResponseInputItem] = [] prepared_generated_items: list[TResponseInputItem] = []- generated_item_sources: dict[int, TResponseInputItem] = {}+ generated_item_sources: dict[int, _PreparedSourceIdentity] = {} if not self.sent_initial_input: initial_items = ItemHelpers.input_to_new_input_list(original_input)@@ -479,6 +559,10 @@ class OpenAIServerConversationTracker: if raw_item is None: continue + input_id = run_item.input_id if isinstance(run_item, InputItem) else None+ if input_id is not None and input_id in self.accepted_input_item_ids:+ continue+ item_id = _normalize_server_item_id( raw_item.get("id") if isinstance(raw_item, dict) else getattr(raw_item, "id", None) )@@ -505,7 +589,12 @@ class OpenAIServerConversationTracker: fp = _fingerprint_for_tracker(converted_input_item) if fp and fp in self.server_output_fingerprints: continue- if fp and self.primed_from_state and fp in self.sent_item_fingerprints:+ if (+ input_id is None+ and fp+ and self.primed_from_state+ and fp in self.sent_item_fingerprints+ ): continue anonymous_tool_search_fp = _anonymous_tool_search_fingerprint(converted_input_item) if (@@ -518,10 +607,13 @@ class OpenAIServerConversationTracker: continue prepared_generated_items.append(converted_input_item)- generated_item_sources[id(converted_input_item)] = cast(TResponseInputItem, raw_item)+ generated_item_sources[id(converted_input_item)] = (+ cast(TResponseInputItem, raw_item),+ input_id,+ ) normalized_generated_items = normalize_input_items_for_api(prepared_generated_items)- normalized_generated_sources = {+ normalized_generated_sources: dict[int, _PreparedSourceIdentity] = { id(normalized_item): generated_item_sources[id(source_item)] for normalized_item, source_item in zip( normalized_generated_items, prepared_generated_items, strict=False@@ -529,42 +621,47 @@ class OpenAIServerConversationTracker: } filtered_generated_items = drop_orphan_function_calls(normalized_generated_items) for item in filtered_generated_items:- prepared_source_item = normalized_generated_sources.get(id(item))- if prepared_source_item is not None:- self._register_prepared_item_source(item, prepared_source_item)+ prepared_source = normalized_generated_sources.get(id(item))+ if prepared_source is not None:+ source_item, input_id = prepared_source+ self._register_prepared_item_source(item, source_item, input_id=input_id) return prepared_initial_items + filtered_generated_items def _register_prepared_item_source(- self, prepared_item: TResponseInputItem, source_item: TResponseInputItem | None = None+ self,+ prepared_item: TResponseInputItem,+ source_item: TResponseInputItem | None = None,+ *,+ input_id: str | None = None, ) -> None: if source_item is None: source_item = prepared_item- self.prepared_item_sources[id(prepared_item)] = (prepared_item, source_item)+ self.prepared_item_sources[id(prepared_item)] = (prepared_item, source_item, input_id) fingerprint = _fingerprint_for_tracker(prepared_item) if fingerprint: self.prepared_item_sources_by_fingerprint.setdefault(fingerprint, []).append(- source_item+ (source_item, input_id) ) - def _consume_prepared_item_source(self, item: TResponseInputItem) -> TResponseInputItem:+ def _consume_prepared_item_source(self, item: TResponseInputItem) -> _PreparedSourceIdentity: direct_entry = self.prepared_item_sources.get(id(item))- direct_source = None+ direct_source: _PreparedSourceIdentity | None = None if direct_entry is not None and direct_entry[0] is item: self.prepared_item_sources.pop(id(item), None)- direct_source = direct_entry[1]+ direct_source = (direct_entry[1], direct_entry[2]) fingerprint = _fingerprint_for_tracker(item) if not fingerprint:- return direct_source if direct_source is not None else item+ return direct_source if direct_source is not None else (item, None) source_items = self.prepared_item_sources_by_fingerprint.get(fingerprint) if not source_items:- return direct_source if direct_source is not None else item+ return direct_source if direct_source is not None else (item, None) source_item = direct_source if direct_source is not None else source_items[0] for index, candidate in enumerate(source_items):- if candidate is source_item:+ if candidate[0] is source_item[0] and candidate[1] == source_item[1]: source_items.pop(index) break @@ -572,3 +669,24 @@ class OpenAIServerConversationTracker: self.prepared_item_sources_by_fingerprint.pop(fingerprint, None) return source_item++ def _delivery_source(self, item: TResponseInputItem) -> _PreparedSourceIdentity:+ delivered_entry = self.delivered_item_sources.get(id(item))+ if delivered_entry is not None and delivered_entry[0] is item:+ return delivered_entry[1], delivered_entry[2]++ source_item, input_id = self._consume_prepared_item_source(item)+ self.delivered_item_sources[id(item)] = (item, source_item, input_id)+ return source_item, input_id++ def mark_input_as_accepted(self, items: Sequence[TResponseInputItem]) -> set[str]:+ """Record pending-input occurrences present in a successful server request."""+ accepted_ids: set[str] = set()+ for item in items:+ if item is None:+ continue+ _source_item, input_id = self._delivery_source(item)+ if input_id is not None:+ accepted_ids.add(input_id)+ self.accepted_input_item_ids.update(accepted_ids)+ return accepted_idssrc/agents/run_internal/run_loop.pyindex 936637fe2..701e66953 100644@@ -42,6 +42,7 @@ from ..exceptions import ( ) from ..handoffs import Handoff from ..items import (+ InputItem, ItemHelpers, ModelResponse, RunItem,@@ -149,6 +150,8 @@ from .run_steps import ( ) from .session_persistence import ( _session_get_items,+ admit_pending_input,+ commit_server_pending_input, persist_session_items_for_guardrail_trip, prepare_input_with_session, reconcile_nested_history_owned_session_item_refs,@@ -786,8 +789,36 @@ async def start_streaming( hydrate_tool_use_tracker(tool_use_tracker, run_state, starting_agent) pending_server_items: list[RunItem] | None = None+ pending_input_admission_items: list[InputItem] = [] session_input_items_for_persistence: list[TResponseInputItem] | None = None + def _commit_pending_server_response(+ model_response: ModelResponse,+ processed_response: ProcessedResponse | None,+ ) -> bool:+ if (+ run_state is None+ or server_conversation_tracker is None+ or not pending_input_admission_items+ ):+ return False+ return commit_server_pending_input(+ run_state=run_state,+ tracker=server_conversation_tracker,+ admission_items=pending_input_admission_items,+ generated_items=streamed_result._model_input_items,+ session_items=streamed_result.new_items,+ model_response=model_response,+ processed_response=processed_response,+ current_turn=current_turn,+ )++ def _mark_response_hooks_started() -> None:+ if run_state is None or not isinstance(run_state._current_step, NextStepInterruption):+ return+ if run_state._current_step.response_accepted:+ run_state._current_step.llm_end_hooks_started = True+ if is_resumed_state and server_conversation_tracker is not None and run_state is not None: session_items: list[TResponseInputItem] | None = None if session is not None:@@ -973,8 +1004,15 @@ async def start_streaming( if is_resumed_state and run_state is not None and run_state._current_step is not None: if isinstance(run_state._current_step, NextStepInterruption):- if not run_state._model_responses or run_state._last_processed_response is None:+ if not run_state._model_responses: raise UserError("No model response found in previous state")+ if run_state._last_processed_response is None:+ if run_state._current_step.response_accepted:+ raise UserError(+ "An accepted model response could not be processed; "+ "start a new run instead of retrying it"+ )+ raise UserError("No processed response found in previous state") last_model_response = run_state._model_responses[-1] @@ -989,6 +1027,7 @@ async def start_streaming( run_config=run_config, server_manages_conversation=server_conversation_tracker is not None, run_state=run_state,+ error_handlers=error_handlers, ) tool_use_tracker.record_processed_response(@@ -1079,7 +1118,7 @@ async def start_streaming( streamed_result._event_queue.put_nowait( AgentUpdatedStreamEvent(new_agent=current_agent) )- run_state._current_step = NextStepRunAgain() # type: ignore[assignment]+ run_state._current_step = NextStepRunAgain() if await _wait_for_streamed_turn_events_and_stop_if_cancelled( streamed_result ):@@ -1099,6 +1138,7 @@ async def start_streaming( store_setting=store_setting, persist_before_output_guardrails=True, )+ run_state._current_step = None break if isinstance(turn_result.next_step, NextStepRunAgain):@@ -1107,7 +1147,7 @@ async def start_streaming( turn_result.model_response.response_id, store_setting, )- run_state._current_step = NextStepRunAgain() # type: ignore[assignment]+ run_state._current_step = NextStepRunAgain() if await _wait_for_streamed_turn_events_and_stop_if_cancelled( streamed_result ):@@ -1124,6 +1164,62 @@ async def start_streaming( if streamed_result.is_complete: break + if run_state is not None and run_state._pending_input:+ if run_state._current_step is None:+ run_state._current_step = NextStepRunAgain()+ pending_input = run_state.pending_input+ pending_guardrails = current_agent.input_guardrails + (+ run_config.input_guardrails or []+ )+ previous_result_count = len(streamed_result.input_guardrail_results)+ try:+ await run_input_guardrails_with_queue(+ current_agent,+ pending_guardrails,+ pending_input,+ context_wrapper,+ streamed_result,+ current_span,+ )+ finally:+ run_state._input_guardrail_results = list(+ streamed_result.input_guardrail_results+ )+ tripping_result = next(+ (+ result+ for result in streamed_result.input_guardrail_results[+ previous_result_count:+ ]+ if result.output.tripwire_triggered+ ),+ None,+ )+ if tripping_result is not None:+ raise InputGuardrailTripwireTriggered(tripping_result)++ store_setting = current_agent.model_settings.resolve(+ run_config.model_settings+ ).store+ admission_items = await admit_pending_input(+ run_state=run_state,+ agent=current_agent,+ session=session,+ server_conversation_tracker=server_conversation_tracker,+ store=store_setting,+ wrapper=context_wrapper,+ )+ streamed_result._model_input_items.extend(admission_items)+ streamed_result.new_items.extend(admission_items)+ if pending_server_items is not None:+ pending_server_items.extend(admission_items)+ pending_input_admission_items = [+ item for item in admission_items if isinstance(item, InputItem)+ ]+ if not run_state._pending_input:+ run_state._generated_items = list(streamed_result._model_input_items)+ run_state._session_items = list(streamed_result.new_items)+ all_tools = await get_all_tools(execution_agent, context_wrapper) all_tools = await initialize_computer_tools( tools=all_tools, context_wrapper=context_wrapper@@ -1310,6 +1406,9 @@ async def start_streaming( prompt_cache_key_resolver=prompt_cache_key_resolver, error_handlers=error_handlers, agent_span=current_span,+ on_response_accepted=_commit_pending_server_response,+ on_response_hooks_started=_mark_response_hooks_started,+ run_state=run_state, ) finally: if current_turn_span is not None:@@ -1348,6 +1447,14 @@ async def start_streaming( ) turn_session_items = session_items_for_turn(turn_result) streamed_result.new_items.extend(turn_session_items)+ if pending_input_admission_items and run_state is not None:+ run_state._generated_items = list(streamed_result._model_input_items)+ run_state._session_items = list(streamed_result.new_items)+ run_state._model_responses = list(streamed_result.raw_responses)+ run_state._last_processed_response = turn_result.processed_response+ run_state._current_turn = current_turn+ run_state._mark_generated_items_merged_with_last_processed()+ pending_input_admission_items = [] if turn_result.nested_history_owned_items is not None: owned_refs = reconcile_nested_history_owned_session_item_refs( streamed_result.new_items,@@ -1410,6 +1517,8 @@ async def start_streaming( store_setting=store_setting, persist_before_output_guardrails=False, )+ if run_state is not None:+ run_state._current_step = None break elif isinstance(turn_result.next_step, NextStepInterruption): processed_response_for_state = turn_result.processed_response@@ -1545,6 +1654,9 @@ async def run_single_turn_streamed( prompt_cache_key_resolver: PromptCacheKeyResolver | None = None, error_handlers: RunErrorHandlers[TContext] | None = None, agent_span: Span[AgentSpanData] | None = None,+ on_response_accepted: Callable[[ModelResponse, ProcessedResponse | None], bool] | None = None,+ on_response_hooks_started: Callable[[], None] | None = None,+ run_state: RunState[Any] | None = None, ) -> SingleStepResult: """Run a single streamed turn and emit events as results arrive.""" public_agent = bindings.public_agent@@ -1651,6 +1763,7 @@ async def run_single_turn_streamed( if isinstance(filtered.input, list): filtered.input = deduplicate_input_items_preferring_latest(filtered.input) if server_conversation_tracker is not None:+ server_conversation_tracker.validate_pending_input_filter(filtered.input) logger.debug( "filtered.input has %s items; ids=%s", len(filtered.input),@@ -1820,12 +1933,22 @@ async def run_single_turn_streamed( # Streaming uses the same rewind helper, so a successful retry must restore delivered # input tracking before the next turn computes server-managed deltas. server_conversation_tracker.mark_input_as_sent(filtered.input)+ server_conversation_tracker.mark_input_as_accepted(filtered.input) server_conversation_tracker.track_server_items(final_response) + response_accepted = False+ if on_response_accepted is not None:+ response_accepted = on_response_accepted(final_response, None)+ async def after_invocation_validation(- model_items: list[RunItem] | None,- ) -> None:- if model_items is not None:+ processed_response: ProcessedResponse | None,+ ) -> bool:+ if response_accepted and on_response_accepted is not None:+ on_response_accepted(final_response, processed_response)+ if response_accepted and on_response_hooks_started is not None:+ on_response_hooks_started()+ if processed_response is not None:+ model_items = processed_response.new_items emitted_model_item_occurrence_keys.update( _ensure_stream_event_item_occurrence_key(item) for item in model_items )@@ -1838,6 +1961,7 @@ async def run_single_turn_streamed( ), hooks.on_llm_end(context_wrapper, public_agent, final_response), )+ return response_accepted async def check_input_guardrails_before_side_effects() -> None: await raise_if_input_guardrail_tripwire_known()@@ -1858,6 +1982,7 @@ async def run_single_turn_streamed( server_manages_conversation=server_conversation_tracker is not None, after_invocation_validation=after_invocation_validation, before_side_effects=check_input_guardrails_before_side_effects,+ run_state=run_state, ) items_to_filter = session_items_for_turn(single_step_result)@@ -1891,6 +2016,9 @@ async def run_single_turn( prompt_cache_key_resolver: PromptCacheKeyResolver | None = None, error_handlers: RunErrorHandlers[TContext] | None = None, agent_span: Span[AgentSpanData] | None = None,+ on_response_accepted: Callable[[ModelResponse, ProcessedResponse | None], bool] | None = None,+ on_response_hooks_started: Callable[[], None] | None = None,+ run_state: RunState[Any] | None = None, ) -> SingleStepResult: """Run a single non-streaming turn of the agent loop.""" public_agent = bindings.public_agent@@ -1961,9 +2089,17 @@ async def run_single_turn( defer_llm_end_hooks=True, ) + response_accepted = False+ if on_response_accepted is not None:+ response_accepted = on_response_accepted(new_response, None)+ async def after_invocation_validation(- _validated_model_items: list[RunItem] | None,- ) -> None:+ _processed_response: ProcessedResponse | None,+ ) -> bool:+ if response_accepted and on_response_accepted is not None:+ on_response_accepted(new_response, _processed_response)+ if response_accepted and on_response_hooks_started is not None:+ on_response_hooks_started() await gather_with_cancel( ( public_agent.hooks.on_llm_end(context_wrapper, public_agent, new_response)@@ -1972,6 +2108,7 @@ async def run_single_turn( ), hooks.on_llm_end(context_wrapper, public_agent, new_response), )+ return response_accepted return await get_single_step_result_from_response( bindings=bindings,@@ -1988,6 +2125,7 @@ async def run_single_turn( tool_use_tracker=tool_use_tracker, server_manages_conversation=server_conversation_tracker is not None, after_invocation_validation=after_invocation_validation,+ run_state=run_state, ) @@ -2028,6 +2166,7 @@ async def get_new_response( model_settings = maybe_reset_tool_choice(public_agent, tool_use_tracker, model_settings) if server_conversation_tracker is not None:+ server_conversation_tracker.validate_pending_input_filter(filtered.input) server_conversation_tracker.mark_input_as_sent(filtered.input) await gather_with_cancel(@@ -2114,6 +2253,8 @@ async def get_new_response( # filtered input as delivered again once a retry succeeds so subsequent turns only send # new deltas. server_conversation_tracker.mark_input_as_sent(filtered.input)+ server_conversation_tracker.mark_input_as_accepted(filtered.input)+ server_conversation_tracker.track_server_items(new_response) context_wrapper.usage.add(new_response.usage) src/agents/run_internal/run_steps.pyindex 98df09a41..f692e1ca4 100644@@ -174,6 +174,12 @@ class NextStepInterruption: interruptions: list[ToolApprovalItem] """The list of tool calls awaiting approval.""" + response_accepted: bool = False+ """Whether the server accepted a response whose local processing is still incomplete."""++ llm_end_hooks_started: bool = True+ """Whether response-end hooks started before the interruption was persisted."""+ @dataclass class SingleStepResult:src/agents/run_internal/session_persistence.pyindex 04dd21120..df4c84a7a 100644@@ -15,7 +15,15 @@ from typing import Any, cast from .. import _debug from ..exceptions import UserError-from ..items import HandoffOutputItem, ItemHelpers, RunItem, ToolCallOutputItem, TResponseInputItem+from ..items import (+ HandoffOutputItem,+ InputItem,+ ItemHelpers,+ ModelResponse,+ RunItem,+ ToolCallOutputItem,+ TResponseInputItem,+) from ..logger import ( log_model_and_tool_action_debug, log_model_and_tool_action_warning,@@ -52,9 +60,11 @@ from .items import ( strip_internal_input_item_metadata, ) from .oai_conversation import OpenAIServerConversationTracker-from .run_steps import SingleStepResult+from .run_steps import NextStepInterruption, ProcessedResponse, SingleStepResult __all__ = [+ "admit_pending_input",+ "commit_server_pending_input", "prepare_input_with_session", "persist_session_items_for_guardrail_trip", "reconcile_nested_history_owned_session_item_refs",@@ -72,6 +82,110 @@ __all__ = [ _SESSION_LIMIT_UNSET = object() +async def admit_pending_input(+ *,+ run_state: RunState[Any],+ agent: Any,+ session: Session | None,+ server_conversation_tracker: OpenAIServerConversationTracker | None,+ store: bool | None,+ wrapper: RunContextWrapper[Any],+) -> list[RunItem]:+ """Admit staged RunState input into the active conversation ownership boundary.++ The caller must run pending-input guardrails first. Client-managed sessions accept the input+ before the model call, while server-managed conversations keep it pending until a model+ response confirms that the server accepted the request.+ """+ pending_input = run_state.pending_input+ if not pending_input:+ return []++ admission_items: list[RunItem] = [+ InputItem(agent=agent, raw_item=item) for item in pending_input+ ]++ if session is not None and server_conversation_tracker is None:+ await save_result_to_session(+ session,+ [],+ admission_items,+ None,+ store=store,+ wrapper=wrapper,+ )+ if server_conversation_tracker is None:+ run_state.clear_pending_input()++ return admission_items+++def commit_server_pending_input(+ *,+ run_state: RunState[Any],+ tracker: OpenAIServerConversationTracker,+ admission_items: list[InputItem],+ generated_items: list[RunItem],+ session_items: list[RunItem],+ model_response: ModelResponse,+ processed_response: ProcessedResponse | None,+ current_turn: int,+) -> bool:+ """Commit only pending-input occurrences accepted by a server-managed request."""+ if not admission_items:+ return False++ admission_ids = {item.input_id for item in admission_items}+ accepted_ids = admission_ids & tracker.accepted_input_item_ids++ def retain_accepted_admissions(items: list[RunItem]) -> None:+ items[:] = [+ item+ for item in items+ if not (+ isinstance(item, InputItem)+ and item.input_id in admission_ids+ and item.input_id not in accepted_ids+ )+ ]++ retain_accepted_admissions(generated_items)+ retain_accepted_admissions(session_items)+ run_state._pending_input = copy.deepcopy(+ [item.raw_item for item in admission_items if item.input_id not in accepted_ids]+ )++ # A model input filter may omit every staged occurrence. In that case the response does not+ # acknowledge pending input, so normal turn processing owns the response and the input remains+ # available for a later request.+ if not accepted_ids:+ return False++ state_generated_items = list(generated_items)+ state_session_items = list(session_items)++ run_state._generated_items = state_generated_items+ run_state._session_items = state_session_items+ if not run_state._model_responses or run_state._model_responses[-1] is not model_response:+ run_state._model_responses.append(model_response)+ run_state._last_processed_response = processed_response+ run_state._current_step = NextStepInterruption(+ interruptions=(+ list(processed_response.interruptions) if processed_response is not None else []+ ),+ response_accepted=True,+ llm_end_hooks_started=False,+ )+ run_state._current_turn = current_turn+ run_state._conversation_id = tracker.conversation_id+ run_state._previous_response_id = tracker.previous_response_id+ run_state._auto_previous_response_id = tracker.auto_previous_response_id+ # The accepted model response is durable, but its processed items have not yet been merged+ # because local hooks and tool work can still fail. Preserve that distinction across retries.+ run_state._clear_generated_items_last_processed_marker()+ return True++ async def _session_get_items( session: Session, limit: int | None | object = _SESSION_LIMIT_UNSET,src/agents/run_internal/tool_actions.pyindex 9bb1201b2..6bffae29c 100644@@ -128,6 +128,10 @@ class ComputerAction: tool=action.computer_tool, run_context=context_wrapper ) agent_hooks = agent.hooks+ context_wrapper._mark_tool_invocation_executed(+ action.tool_call,+ tool_name=action.computer_tool.name,+ ) await gather_with_cancel( hooks.on_tool_start(context_wrapper, agent, action.computer_tool), (src/agents/run_internal/turn_resolution.pyindex 9ad8143a2..8c756b0bc 100644@@ -795,6 +795,7 @@ async def execute_tools_and_side_effects( error_handlers: RunErrorHandlers[TContext] | None = None, server_manages_conversation: bool = False, precomputed_skipped_raw_item_ids: set[int] | None = None,+ run_state: RunState[Any] | None = None, ) -> SingleStepResult: """Run one turn of the loop, coordinating tools, approvals, guardrails, and handoffs.""" public_agent = bindings.public_agent@@ -836,6 +837,15 @@ async def execute_tools_and_side_effects( skipped_raw_item_ids=skipped_raw_item_ids, ) + def _commit_accepted_response_tool_output(item: RunItem) -> None:+ if run_state is None or not isinstance(run_state._current_step, NextStepInterruption):+ return+ if not run_state._current_step.response_accepted:+ return+ for target in (run_state._generated_items, run_state._session_items):+ if item not in target:+ target.append(item)+ ( function_results, tool_input_guardrail_results,@@ -851,6 +861,7 @@ async def execute_tools_and_side_effects( hooks=hooks, context_wrapper=context_wrapper, run_config=run_config,+ tool_output_committer=_commit_accepted_response_tool_output, ) new_step_items.extend( _build_tool_result_items(@@ -1132,12 +1143,49 @@ async def resolve_interrupted_turn( run_config: RunConfig, server_manages_conversation: bool = False, run_state: RunState | None = None,+ error_handlers: RunErrorHandlers[TContext] | None = None, nest_handoff_history_fn: Callable[..., HandoffInputData] | None = None, ) -> SingleStepResult: """Continue a turn that was previously interrupted waiting for tool approval.""" public_agent = bindings.public_agent execution_agent = bindings.execution_agent + current_step = run_state._current_step if run_state is not None else None+ if (+ isinstance(current_step, NextStepInterruption)+ and current_step.response_accepted+ and not current_step.llm_end_hooks_started+ ):+ current_step.llm_end_hooks_started = True+ await gather_with_cancel(+ (+ public_agent.hooks.on_llm_end(context_wrapper, public_agent, new_response)+ if public_agent.hooks is not None+ else _coro.noop_coroutine()+ ),+ hooks.on_llm_end(context_wrapper, public_agent, new_response),+ )++ if (+ isinstance(current_step, NextStepInterruption)+ and current_step.response_accepted+ and not processed_response.has_tools_or_approvals_to_run()+ ):+ return await execute_tools_and_side_effects(+ bindings=bindings,+ original_input=original_input,+ pre_step_items=original_pre_step_items,+ new_response=new_response,+ processed_response=processed_response,+ output_schema=get_output_schema(execution_agent),+ hooks=hooks,+ context_wrapper=context_wrapper,+ run_config=run_config,+ error_handlers=error_handlers,+ server_manages_conversation=server_manages_conversation,+ run_state=run_state,+ )+ execute_handoffs_call = execute_handoffs _register_tool_call_items(@@ -3363,8 +3411,10 @@ async def get_single_step_result_from_response( tool_use_tracker, error_handlers: RunErrorHandlers[TContext] | None = None, server_manages_conversation: bool = False,- after_invocation_validation: Callable[[list[RunItem] | None], Awaitable[None]] | None = None,+ after_invocation_validation: Callable[[ProcessedResponse | None], Awaitable[bool]]+ | None = None, before_side_effects: Callable[[], Awaitable[None]] | None = None,+ run_state: RunState[Any] | None = None, ) -> SingleStepResult: item_agent = bindings.public_agent try:@@ -3406,7 +3456,7 @@ async def get_single_step_result_from_response( ) if after_invocation_validation is not None:- await after_invocation_validation(processed_response.new_items)+ await after_invocation_validation(processed_response) if before_side_effects is not None: await before_side_effects()@@ -3426,4 +3476,5 @@ async def get_single_step_result_from_response( error_handlers=error_handlers, server_manages_conversation=server_manages_conversation, precomputed_skipped_raw_item_ids=skipped_raw_item_ids,+ run_state=run_state, )src/agents/run_state.pyindex 065ed8593..e6bb61477 100644@@ -78,6 +78,8 @@ from .items import ( CompactionItem, HandoffCallItem, HandoffOutputItem,+ InputItem,+ ItemHelpers, MCPApprovalRequestItem, MCPApprovalResponseItem, MCPListToolsItem,@@ -145,6 +147,7 @@ if TYPE_CHECKING: from .items import ModelResponse, RunItem from .run_internal.run_steps import ( NextStepInterruption,+ NextStepRunAgain, ProcessedResponse, ToolRunFunction, )@@ -192,7 +195,7 @@ SCHEMA_VERSION_SUMMARIES: dict[str, str] = { "1.14": "Scopes hosted MCP approvals and restored requests by server label.", "1.15": ( "Persists canonical tool invocation identity plus sanitized mount authority and trusted "- "rebind metadata across resume flows."+ "rebind metadata, durable pending input, and resumable next-model-call state." ), } SUPPORTED_SCHEMA_VERSIONS = frozenset(SCHEMA_VERSION_SUMMARIES)@@ -280,6 +283,9 @@ class RunState(Generic[TContext, TAgent]): _session_items: list[RunItem] = field(default_factory=list) """Full, unfiltered run items for session history.""" + _pending_input: list[TResponseInputItem] = field(default_factory=list)+ """Input staged for admission immediately before the next resumed model call."""+ _nested_history_owned_session_item_refs: list[NestedHistoryOwnedItemRef] = field( default_factory=list )@@ -315,8 +321,8 @@ class RunState(Generic[TContext, TAgent]): _tool_output_guardrail_results: list[ToolOutputGuardrailResult] = field(default_factory=list) """Results from tool output guardrails applied during the run.""" - _current_step: NextStepInterruption | None = None- """Current step if the run is interrupted (e.g., for tool approval)."""+ _current_step: NextStepInterruption | NextStepRunAgain | None = None+ """Current resumable step, or ``None`` when the state is terminal.""" _last_processed_response: ProcessedResponse | None = None """The last processed model response. This is needed for resuming from interruptions."""@@ -367,6 +373,7 @@ class RunState(Generic[TContext, TAgent]): self._model_responses = [] self._generated_items = [] self._session_items = []+ self._pending_input = [] self._nested_history_owned_session_item_refs = [] self._input_guardrail_results = [] self._output_guardrail_results = []@@ -385,6 +392,55 @@ class RunState(Generic[TContext, TAgent]): self._agent_tool_state_scope_id = get_agent_tool_state_scope(context) + @property+ def pending_input(self) -> list[TResponseInputItem]:+ """Return a copy of input currently staged for the next resumed model call."""+ return copy.deepcopy(self._pending_input)++ def add_input(self, input: str | list[TResponseInputItem]) -> None:+ """Stage input for admission immediately before the next resumed model call.++ String input is normalized to a user message. Multiple calls preserve insertion order.+ The input remains pending until its guardrails and conversation ownership boundary accept+ it. Terminal states reject new input before mutating the state.+ """+ from .run_internal.run_steps import NextStepInterruption, NextStepRunAgain++ if not isinstance(self._current_step, NextStepInterruption | NextStepRunAgain):+ raise UserError("Cannot add input to a terminal RunState")+ if self._max_turns is not None and self._current_turn >= self._max_turns:+ raise UserError("Cannot add input to a RunState with no remaining model turns")+ if isinstance(self._current_step, NextStepInterruption):+ if self._current_step.response_accepted:+ raise UserError(+ "Cannot add input while an accepted model response is awaiting local processing"+ )+ if self._current_agent is None:+ raise UserError("Cannot add input to a RunState without a current agent")+ tool_use_behavior = self._current_agent.tool_use_behavior+ interrupted_tool_names = {+ item.tool_name+ for item in self._current_step.interruptions+ if item.tool_name is not None+ }+ stops_before_next_model = tool_use_behavior == "stop_on_first_tool" or (+ isinstance(tool_use_behavior, dict)+ and bool(+ interrupted_tool_names & set(tool_use_behavior.get("stop_at_tool_names", []))+ )+ )+ if stops_before_next_model or callable(tool_use_behavior):+ raise UserError(+ "Cannot add input to an interrupted RunState whose tool result may end the run"+ )++ normalized = ItemHelpers.input_to_new_input_list(input)+ self._pending_input.extend(copy.deepcopy(normalized))++ def clear_pending_input(self) -> None:+ """Remove all input staged for the next resumed model call."""+ self._pending_input = []+ def get_interruptions(self) -> list[ToolApprovalItem]: """Return pending interruptions if the current step is an interruption.""" # Import at runtime to avoid circular import@@ -693,13 +749,13 @@ class RunState(Generic[TContext, TAgent]): for resp in self._model_responses ] - def _serialize_original_input(self) -> str | list[Any]:- """Normalize original input into the shape expected by Responses API."""- if not isinstance(self._original_input, list):- return self._original_input+ def _serialize_input(self, input: str | list[Any]) -> str | list[Any]:+ """Normalize input into the shape expected by Responses API."""+ if not isinstance(input, list):+ return input normalized_items = []- for item in self._original_input:+ for item in input: normalized_item = _serialize_raw_item_value(item) if isinstance(normalized_item, dict): normalized_item = dict(normalized_item)@@ -713,6 +769,10 @@ class RunState(Generic[TContext, TAgent]): normalized_items.append(normalized_item) return normalized_items + def _serialize_original_input(self) -> str | list[Any]:+ """Normalize original input into the shape expected by Responses API."""+ return self._serialize_input(self._original_input)+ def _generated_session_item_indexes( self, generated_items: Sequence[RunItem],@@ -1063,6 +1123,7 @@ class RunState(Generic[TContext, TAgent]): "current_turn": self._current_turn, "current_agent": current_agent_entry, "original_input": original_input_serialized,+ "pending_input": self._serialize_input(self._pending_input), "model_responses": model_responses, "context": context_entry, "tool_use_tracker": copy.deepcopy(self._tool_use_tracker_snapshot),@@ -1188,9 +1249,9 @@ class RunState(Generic[TContext, TAgent]): } def _serialize_current_step(self) -> dict[str, Any] | None:- """Serialize the current step if it's an interruption."""+ """Serialize the current resumable step.""" # Import at runtime to avoid circular import- from .run_internal.run_steps import NextStepInterruption+ from .run_internal.run_steps import NextStepInterruption, NextStepRunAgain agent_identity_keys_by_id = ( _build_agent_identity_keys_by_id(cast(Agent[Any], self._starting_agent))@@ -1198,6 +1259,9 @@ class RunState(Generic[TContext, TAgent]): else None ) + if isinstance(self._current_step, NextStepRunAgain):+ return {"type": "next_step_run_again"}+ if self._current_step is None or not isinstance(self._current_step, NextStepInterruption): return None @@ -1215,6 +1279,8 @@ class RunState(Generic[TContext, TAgent]): "type": "next_step_interruption", "data": { "interruptions": interruptions_data,+ "response_accepted": self._current_step.response_accepted,+ "llm_end_hooks_started": self._current_step.llm_end_hooks_started, }, } @@ -1236,6 +1302,9 @@ class RunState(Generic[TContext, TAgent]): ), } + if isinstance(item, InputItem):+ result["input_id"] = item.input_id+ # Add additional fields based on item type if hasattr(item, "output"): serialized_output = item.output@@ -2803,6 +2872,10 @@ def _run_state_raw_items(state_json: Mapping[str, Any]) -> list[Any]: if isinstance(original_input, list): raw_items.extend(original_input) + pending_input = state_json.get("pending_input")+ if isinstance(pending_input, list):+ raw_items.extend(pending_input)+ for response_key in ("model_responses", "last_model_response"): responses = state_json.get(response_key) if isinstance(responses, Mapping):@@ -3206,6 +3279,13 @@ async def _build_run_state_from_json( set_agent_tool_state_scope(context, state._agent_tool_state_scope_id) state._current_turn = state_json["current_turn"]+ pending_input_raw = state_json.get("pending_input", [])+ if not isinstance(pending_input_raw, list):+ raise UserError("Run state pending_input must be a list")+ state._pending_input = cast(+ list[TResponseInputItem],+ [dict(item) if isinstance(item, Mapping) else item for item in pending_input_raw],+ ) state._model_responses = _deserialize_model_responses(state_json.get("model_responses", [])) serialized_generated_items = state_json.get("generated_items", []) state._generated_items, generated_source_indexes = _deserialize_items_with_source_indexes(@@ -3368,7 +3448,11 @@ async def _build_run_state_from_json( ) current_step_data = state_json.get("current_step")- if current_step_data and current_step_data.get("type") == "next_step_interruption":+ if current_step_data and current_step_data.get("type") == "next_step_run_again":+ from .run_internal.run_steps import NextStepRunAgain++ state._current_step = NextStepRunAgain()+ elif current_step_data and current_step_data.get("type") == "next_step_interruption": interruptions: list[ToolApprovalItem] = [] interruptions_data = current_step_data.get("data", {}).get( "interruptions", current_step_data.get("interruptions", [])@@ -3385,8 +3469,16 @@ async def _build_run_state_from_json( from .run_internal.run_steps import NextStepInterruption state._current_step = NextStepInterruption(- interruptions=[item for item in interruptions if isinstance(item, ToolApprovalItem)]+ interruptions=[item for item in interruptions if isinstance(item, ToolApprovalItem)],+ response_accepted=bool(+ current_step_data.get("data", {}).get("response_accepted", False)+ ),+ llm_end_hooks_started=bool(+ current_step_data.get("data", {}).get("llm_end_hooks_started", True)+ ), )+ if state._current_step.response_accepted:+ state._clear_generated_items_last_processed_marker() for approval_item in state._current_step.interruptions: context._mark_restored_unbound_pending_approval(approval_item) @@ -3581,7 +3673,17 @@ def _validate_completed_tool_invocations( if state._last_processed_response is not None: for run_item in state._last_processed_response.new_items: record_run_item(run_item)- for response in state._model_responses:+ responses_for_invocation_validation = state._model_responses+ if (+ state._last_processed_response is None+ and getattr(state._current_step, "response_accepted", False)+ and responses_for_invocation_validation+ ):+ # A server-accepted response is checkpointed before fallible local processing. Its raw+ # invocations remain durable for diagnostics, but they are not registered runtime work+ # unless response processing succeeds.+ responses_for_invocation_validation = responses_for_invocation_validation[:-1]+ for response in responses_for_invocation_validation: for raw_item in response.output: record_raw_item(raw_item, allow_handoff_alternative=True) if isinstance(state._original_input, list):@@ -4307,7 +4409,22 @@ def _deserialize_items( ) try:- if item_type == "message_output_item":+ if item_type == "input_item":+ input_id = item_data.get("input_id")+ if isinstance(input_id, str):+ input_item = InputItem(+ agent=agent,+ raw_item=cast(TResponseInputItem, normalized_raw_item),+ input_id=input_id,+ )+ else:+ input_item = InputItem(+ agent=agent,+ raw_item=cast(TResponseInputItem, normalized_raw_item),+ )+ result.append(input_item)++ elif item_type == "message_output_item": raw_item_msg = _deserialize_message_output_item(normalized_raw_item) result.append(MessageOutputItem(agent=agent, raw_item=raw_item_msg)) tests/test_run_state_pending_input.pynew file mode 100644index 000000000..1c1959001@@ -0,0 +1,965 @@+from __future__ import annotations++import json+from typing import Any, cast++import pytest+from openai.types.responses.response_computer_tool_call import (+ ActionScreenshot,+ ResponseComputerToolCall,+)++from agents import Agent, ComputerTool, InputItem, RunConfig, Runner, function_tool+from agents.exceptions import InputGuardrailTripwireTriggered, ModelBehaviorError, UserError+from agents.guardrail import GuardrailFunctionOutput, InputGuardrail+from agents.items import ModelResponse, TResponseInputItem+from agents.lifecycle import AgentHooks, RunHooks+from agents.run import CallModelData, ModelInputData+from agents.run_context import RunContextWrapper+from agents.run_internal.oai_conversation import OpenAIServerConversationTracker+from agents.run_internal.run_steps import NextStepInterruption, NextStepRunAgain+from agents.run_state import CURRENT_SCHEMA_VERSION, RunState+from agents.tool import Tool+from agents.usage import Usage++from .fake_model import FakeModel+from .test_computer_tool_lifecycle import FakeComputer+from .test_responses import get_function_tool_call, get_text_message+from .utils.simple_session import SimpleListSession+++def _item_type(item: TResponseInputItem) -> str | None:+ if not isinstance(item, dict):+ return getattr(item, "type", None)+ return cast(str | None, item.get("type") or item.get("role"))+++def _message_text(item: TResponseInputItem) -> str | None:+ if not isinstance(item, dict) or item.get("role") != "user":+ return None+ content = item.get("content")+ if isinstance(content, str):+ return content+ if isinstance(content, list):+ return "".join(+ str(part.get("text", ""))+ for part in content+ if isinstance(part, dict) and part.get("type") in {"input_text", "output_text"}+ )+ return None+++async def _make_after_turn_state(+ *,+ session: SimpleListSession | None = None,+ auto_previous_response_id: bool = False,+) -> tuple[FakeModel, Agent[Any], RunState[Any], list[str]]:+ calls: list[str] = []++ @function_tool(name_override="record_destination")+ def record_destination(destination: str) -> str:+ calls.append(destination)+ return f"recorded:{destination}"++ model = FakeModel()+ model.set_next_output(+ [+ get_function_tool_call(+ "record_destination",+ json.dumps({"destination": "Paris"}),+ call_id="call-destination",+ )+ ]+ )+ agent = Agent(name="assistant", model=model, tools=[record_destination])+ streamed = Runner.run_streamed(+ agent,+ "Initial request",+ session=session,+ auto_previous_response_id=auto_previous_response_id,+ )+ async for event in streamed.stream_events():+ if event.type == "run_item_stream_event" and event.name == "tool_output":+ streamed.cancel(mode="after_turn")++ state = streamed.to_state()+ assert isinstance(state._current_step, NextStepRunAgain)+ assert calls == ["Paris"]+ return model, agent, state, calls+++@pytest.mark.asyncio+async def test_pending_input_preserves_order_and_serialization_round_trips() -> None:+ agent = Agent(name="assistant")+ state: RunState[Any] = RunState(+ context=RunContextWrapper(context={}),+ original_input="Initial request",+ starting_agent=agent,+ )+ state._current_step = NextStepRunAgain()+ starting_turn = state._current_turn++ state.add_input("First late message")+ state.add_input([{"role": "user", "content": "Second late message"}])+ assert state._current_turn == starting_turn++ assert [_message_text(item) for item in state.pending_input] == [+ "First late message",+ "Second late message",+ ]+ detached_view = state.pending_input+ cast(dict[str, Any], detached_view[0])["content"] = "mutated"+ assert _message_text(state.pending_input[0]) == "First late message"++ serialized = state.to_json()+ assert serialized["$schemaVersion"] == CURRENT_SCHEMA_VERSION+ restored = await RunState.from_json(agent, serialized)+ restored_from_string = await RunState.from_string(agent, state.to_string())++ for candidate in (restored, restored_from_string):+ assert isinstance(candidate._current_step, NextStepRunAgain)+ assert [_message_text(item) for item in candidate.pending_input] == [+ "First late message",+ "Second late message",+ ]++ legacy = state.to_json()+ legacy["$schemaVersion"] = "1.14"+ legacy.pop("pending_input")+ legacy["current_step"] = None+ restored_legacy = await RunState.from_json(agent, legacy)+ assert restored_legacy.pending_input == []+++@pytest.mark.asyncio+async def test_after_turn_resume_admits_input_after_tool_output_exactly_once() -> None:+ session = SimpleListSession()+ model, agent, state, calls = await _make_after_turn_state(session=session)+ state.add_input("Change the destination to Tokyo")+ model.set_next_output([get_text_message("Updated")])++ result = await Runner.run(agent, state, session=session)++ assert result.final_output == "Updated"+ assert calls == ["Paris"]+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_item_type(item) for item in model_input] == [+ "user",+ "function_call",+ "function_call_output",+ "user",+ ]+ assert [_message_text(item) for item in model_input].count(+ "Change the destination to Tokyo"+ ) == 1+ assert state.pending_input == []++ session_items = await session.get_items()+ assert [_message_text(item) for item in session_items].count(+ "Change the destination to Tokyo"+ ) == 1+ replay_items = result.to_input_list()+ assert [_message_text(item) for item in replay_items].count(+ "Change the destination to Tokyo"+ ) == 1+ for terminal_state in (state, result.to_state()):+ with pytest.raises(UserError, match="terminal RunState"):+ terminal_state.add_input("Too late")+++@pytest.mark.asyncio+async def test_streamed_resume_matches_pending_input_ordering() -> None:+ model, agent, state, calls = await _make_after_turn_state()+ state.add_input("Change the destination to Tokyo")+ model.set_next_output([get_text_message("Updated")])++ result = Runner.run_streamed(agent, state)+ async for _ in result.stream_events():+ pass++ assert result.final_output == "Updated"+ assert calls == ["Paris"]+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_item_type(item) for item in model_input] == [+ "user",+ "function_call",+ "function_call_output",+ "user",+ ]+ assert [_message_text(item) for item in model_input].count(+ "Change the destination to Tokyo"+ ) == 1+ assert state.pending_input == []+ for terminal_state in (state, result.to_state()):+ with pytest.raises(UserError, match="terminal RunState"):+ terminal_state.add_input("Too late")+++@pytest.mark.asyncio+async def test_server_managed_resume_sends_pending_input_as_unsent_delta_once() -> None:+ model, agent, state, calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Change the destination to Tokyo")+ model.set_next_output([get_text_message("Updated")])++ result = await Runner.run(agent, state)++ assert result.final_output == "Updated"+ assert calls == ["Paris"]+ assert model.last_turn_args["previous_response_id"] == "resp-789"+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_item_type(item) for item in model_input] == ["function_call_output", "user"]+ assert [_message_text(item) for item in model_input].count(+ "Change the destination to Tokyo"+ ) == 1+ assert state.pending_input == []+++def test_server_tracker_distinguishes_identical_input_occurrences_after_restore() -> None:+ agent = Agent(name="assistant")+ admitted_first = InputItem(+ agent=agent,+ raw_item={"role": "user", "content": "Repeat"},+ )+ admitted_second = InputItem(+ agent=agent,+ raw_item={"role": "user", "content": "Repeat"},+ )+ tracker = OpenAIServerConversationTracker(previous_response_id="resp-latest")+ tracker.hydrate_from_state(+ original_input="Initial request",+ generated_items=[admitted_first],+ model_responses=[ModelResponse(output=[], usage=Usage(), response_id="resp-latest")],+ )++ assert tracker.prepare_input("Initial request", [admitted_first, admitted_second]) == [+ admitted_second.raw_item+ ]+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed_second_resume", [False, True])+async def test_server_managed_resume_sends_identical_late_input_in_later_occurrence(+ streamed_second_resume: bool,+) -> None:+ model, agent, state, calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Repeat")+ model.set_next_output(+ [+ get_function_tool_call(+ "record_destination",+ json.dumps({"destination": "Rome"}),+ call_id="call-second-destination",+ )+ ]+ )++ first_resume = Runner.run_streamed(agent, state)+ async for event in first_resume.stream_events():+ if event.type == "run_item_stream_event" and event.name == "tool_output":+ first_resume.cancel(mode="after_turn")++ state = await RunState.from_json(agent, first_resume.to_state().to_json())+ admitted_before = next(item for item in state._generated_items if isinstance(item, InputItem))+ state.add_input("Repeat")+ model.set_next_output([get_text_message("Done")])++ if streamed_second_resume:+ streamed_result = Runner.run_streamed(agent, state)+ async for _event in streamed_result.stream_events():+ pass+ final_output = streamed_result.final_output+ else:+ run_result = await Runner.run(agent, state)+ final_output = run_result.final_output++ assert final_output == "Done"+ assert calls == ["Paris", "Rome"]+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in model_input].count("Repeat") == 1+ admitted_after = [item for item in state._generated_items if isinstance(item, InputItem)]+ assert [item.input_id for item in admitted_after].count(admitted_before.input_id) == 1+ assert len({item.input_id for item in admitted_after}) == 2+++@pytest.mark.asyncio+async def test_unresolved_approval_keeps_pending_input_until_tool_finishes() -> None:+ calls: list[str] = []++ @function_tool(needs_approval=True)+ def protected_tool(value: str) -> str:+ calls.append(value)+ return f"approved:{value}"++ model = FakeModel()+ model.set_next_output(+ [get_function_tool_call("protected_tool", '{"value":"one"}', call_id="call-protected")]+ )+ agent = Agent(name="assistant", model=model, tools=[protected_tool])+ interrupted = await Runner.run(agent, "Initial request")+ state = interrupted.to_state()+ state.add_input("Late input")++ still_interrupted = await Runner.run(agent, state)+ assert still_interrupted.interruptions+ assert calls == []+ assert _message_text(state.pending_input[0]) == "Late input"++ state.approve(state.get_interruptions()[0])+ model.set_next_output([get_text_message("Done")])+ resumed = await Runner.run(agent, state)++ assert resumed.final_output == "Done"+ assert calls == ["one"]+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_item_type(item) for item in model_input][-2:] == ["function_call_output", "user"]+ assert _message_text(model_input[-1]) == "Late input"+++@pytest.mark.asyncio+async def test_streamed_after_turn_cancel_keeps_pending_input_for_next_resume() -> None:+ calls: list[str] = []++ @function_tool(needs_approval=True)+ def protected_tool(value: str) -> str:+ calls.append(value)+ return f"approved:{value}"++ model = FakeModel()+ model.set_next_output(+ [get_function_tool_call("protected_tool", '{"value":"one"}', call_id="call-protected")]+ )+ agent = Agent(name="assistant", model=model, tools=[protected_tool])+ interrupted = await Runner.run(agent, "Initial request")+ state = interrupted.to_state()+ state.add_input("Late input")+ state.approve(state.get_interruptions()[0])++ resumed = Runner.run_streamed(agent, state)+ async for event in resumed.stream_events():+ if event.type == "run_item_stream_event" and event.name == "tool_output":+ resumed.cancel(mode="after_turn")++ assert calls == ["one"]+ assert _message_text(state.pending_input[0]) == "Late input"++ model.set_next_output([get_text_message("Done")])+ result = await Runner.run(agent, state)+ assert result.final_output == "Done"+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in model_input].count("Late input") == 1+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed", [False, True])+@pytest.mark.parametrize(+ "tool_use_behavior",+ [+ "stop_on_first_tool",+ {"stop_at_tool_names": ["protected_tool"]},+ lambda _context, _results: None,+ ],+)+async def test_interruption_without_guaranteed_next_model_rejects_input(+ streamed: bool,+ tool_use_behavior: Any,+) -> None:+ @function_tool(needs_approval=True)+ def protected_tool(value: str) -> str:+ return value++ model = FakeModel(+ initial_output=[+ get_function_tool_call(+ "protected_tool",+ '{"value":"one"}',+ call_id="call-protected-terminal",+ )+ ]+ )+ agent = Agent(+ name="assistant",+ model=model,+ tools=[protected_tool],+ tool_use_behavior=cast(Any, tool_use_behavior),+ )+ if streamed:+ interrupted_stream = Runner.run_streamed(agent, "Initial request")+ async for _event in interrupted_stream.stream_events():+ pass+ state = interrupted_stream.to_state()+ else:+ interrupted = await Runner.run(agent, "Initial request")+ state = interrupted.to_state()++ before = state.to_json()+ with pytest.raises(UserError, match="tool result may end the run"):+ state.add_input("Late input")+ assert state.to_json() == before+++@pytest.mark.asyncio+async def test_pending_input_guardrail_trip_keeps_input_recoverable() -> None:+ model, agent, state, _calls = await _make_after_turn_state()+ guarded_inputs: list[list[TResponseInputItem]] = []++ def trip_pending_input(+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ input: str | list[TResponseInputItem],+ ) -> GuardrailFunctionOutput:+ guarded_inputs.append(cast(list[TResponseInputItem], input))+ return GuardrailFunctionOutput(output_info=None, tripwire_triggered=True)++ agent.input_guardrails = [InputGuardrail(guardrail_function=trip_pending_input)]+ state.add_input("Unsafe late input")+ model.set_next_output([get_text_message("Must not run")])+ queued_outputs = len(model.turn_outputs)++ with pytest.raises(InputGuardrailTripwireTriggered):+ await Runner.run(agent, state)++ assert len(model.turn_outputs) == queued_outputs+ assert [[_message_text(item) for item in batch] for batch in guarded_inputs] == [+ ["Unsafe late input"]+ ]+ assert _message_text(state.pending_input[0]) == "Unsafe late input"+ state.clear_pending_input()+ assert state.pending_input == []+++@pytest.mark.asyncio+async def test_pending_input_runs_agent_and_run_config_guardrails_on_only_pending() -> None:+ model, agent, state, _calls = await _make_after_turn_state()+ guarded_inputs: list[tuple[str, list[TResponseInputItem]]] = []++ def inspect_agent_input(+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ input: str | list[TResponseInputItem],+ ) -> GuardrailFunctionOutput:+ guarded_inputs.append(("agent", cast(list[TResponseInputItem], input)))+ return GuardrailFunctionOutput(output_info=None, tripwire_triggered=False)++ def inspect_config_input(+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ input: str | list[TResponseInputItem],+ ) -> GuardrailFunctionOutput:+ guarded_inputs.append(("config", cast(list[TResponseInputItem], input)))+ return GuardrailFunctionOutput(output_info=None, tripwire_triggered=False)++ agent.input_guardrails = [InputGuardrail(guardrail_function=inspect_agent_input)]+ run_config = RunConfig(+ input_guardrails=[InputGuardrail(guardrail_function=inspect_config_input)]+ )+ state.add_input("Guard only this")+ model.set_next_output([get_text_message("Done")])++ result = await Runner.run(agent, state, run_config=run_config)++ assert result.final_output == "Done"+ assert {source for source, _batch in guarded_inputs} == {"agent", "config"}+ assert [[_message_text(item) for item in batch] for _source, batch in guarded_inputs] == [+ ["Guard only this"],+ ["Guard only this"],+ ]+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed_retry", [False, True])+async def test_guardrail_retry_persists_successful_turn_with_session(+ streamed_retry: bool,+) -> None:+ session = SimpleListSession()+ model, agent, state, _calls = await _make_after_turn_state(session=session)+ should_trip = True++ def inspect_pending_input(+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ _input: str | list[TResponseInputItem],+ ) -> GuardrailFunctionOutput:+ return GuardrailFunctionOutput(output_info=None, tripwire_triggered=should_trip)++ agent.input_guardrails = [InputGuardrail(guardrail_function=inspect_pending_input)]+ state.add_input("Late input")++ if streamed_retry:+ tripped = Runner.run_streamed(agent, state, session=session)+ with pytest.raises(InputGuardrailTripwireTriggered):+ async for _event in tripped.stream_events():+ pass+ else:+ with pytest.raises(InputGuardrailTripwireTriggered):+ await Runner.run(agent, state, session=session)++ should_trip = False+ model.set_next_output([get_text_message("Recovered")])+ if streamed_retry:+ streamed_result = Runner.run_streamed(agent, state, session=session)+ async for _event in streamed_result.stream_events():+ pass+ final_output = streamed_result.final_output+ else:+ run_result = await Runner.run(agent, state, session=session)+ final_output = run_result.final_output++ assert final_output == "Recovered"+ session_items = await session.get_items()+ assert [_message_text(item) for item in session_items].count("Late input") == 1+ assert _item_type(session_items[-1]) == "message"+ assert cast(dict[str, Any], session_items[-1]).get("role") == "assistant"+ assert [result.output.tripwire_triggered for result in state._input_guardrail_results] == [+ True,+ False,+ ]+++@pytest.mark.asyncio+async def test_failed_model_request_does_not_duplicate_admitted_input_on_resume() -> None:+ model, agent, state, _calls = await _make_after_turn_state()+ state.add_input("Late input")+ model.set_next_output(RuntimeError("model failed"))++ with pytest.raises(RuntimeError, match="model failed"):+ await Runner.run(agent, state)++ assert state.pending_input == []+ admitted_items = [item for item in state._generated_items if isinstance(item, InputItem)]+ assert [_message_text(item.raw_item) for item in admitted_items] == ["Late input"]+ admitted_input_id = admitted_items[0].input_id++ state = await RunState.from_json(agent, state.to_json())+ assert (+ next(item.input_id for item in state._generated_items if isinstance(item, InputItem))+ == admitted_input_id+ )+ model.set_next_output([get_text_message("Recovered")])+ result = await Runner.run(agent, state)+ assert result.final_output == "Recovered"+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in model_input].count("Late input") == 1+++@pytest.mark.asyncio+async def test_failed_model_request_with_session_persists_admitted_input_once() -> None:+ session = SimpleListSession()+ model, agent, state, _calls = await _make_after_turn_state(session=session)+ state.add_input("Late input")+ model.set_next_output(RuntimeError("model failed"))++ with pytest.raises(RuntimeError, match="model failed"):+ await Runner.run(agent, state, session=session)++ assert state.pending_input == []+ assert [_message_text(item) for item in await session.get_items()].count("Late input") == 1++ state = await RunState.from_json(agent, state.to_json())+ model.set_next_output([get_text_message("Recovered")])+ result = await Runner.run(agent, state, session=session)+ assert result.final_output == "Recovered"+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in model_input].count("Late input") == 1+ assert [_message_text(item) for item in await session.get_items()].count("Late input") == 1+++@pytest.mark.asyncio+async def test_failed_server_managed_request_keeps_pending_input_for_retry() -> None:+ model, agent, state, _calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Late input")+ model.set_next_output(RuntimeError("model failed"))++ with pytest.raises(RuntimeError, match="model failed"):+ await Runner.run(agent, state)++ assert _message_text(state.pending_input[0]) == "Late input"+ state = await RunState.from_json(agent, state.to_json())+ model.set_next_output([get_text_message("Recovered")])+ result = await Runner.run(agent, state)+ assert result.final_output == "Recovered"+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in model_input].count("Late input") == 1+ assert state.pending_input == []+++@pytest.mark.asyncio+async def test_server_filter_omission_remains_pending_for_later_nonstream_turn() -> None:+ model, agent, state, calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Late input")+ model.add_multiple_turn_outputs(+ [+ [+ get_function_tool_call(+ "record_destination",+ json.dumps({"destination": "Rome"}),+ call_id="call-filtered-destination",+ )+ ],+ [get_text_message("Done")],+ ]+ )+ filter_calls = 0++ def omit_first_request(data: CallModelData[Any]) -> ModelInputData:+ nonlocal filter_calls+ filter_calls += 1+ return ModelInputData(+ input=[] if filter_calls == 1 else data.model_data.input,+ instructions=data.model_data.instructions,+ )++ result = await Runner.run(+ agent,+ state,+ run_config=RunConfig(call_model_input_filter=omit_first_request),+ )++ assert result.final_output == "Done"+ assert calls == ["Paris", "Rome"]+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in model_input].count("Late input") == 1+ assert state.pending_input == []+++@pytest.mark.asyncio+async def test_server_filter_omission_survives_streamed_state_round_trip() -> None:+ model, agent, state, calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Late input")+ model.set_next_output(+ [+ get_function_tool_call(+ "record_destination",+ json.dumps({"destination": "Rome"}),+ call_id="call-filtered-destination",+ )+ ]+ )++ def omit_pending(data: CallModelData[Any]) -> ModelInputData:+ return ModelInputData(input=[], instructions=data.model_data.instructions)++ filtered = Runner.run_streamed(+ agent,+ state,+ run_config=RunConfig(call_model_input_filter=omit_pending),+ )+ async for event in filtered.stream_events():+ if event.type == "run_item_stream_event" and event.name == "tool_output":+ filtered.cancel(mode="after_turn")++ state = await RunState.from_json(agent, filtered.to_state().to_json())+ assert [_message_text(item) for item in state.pending_input] == ["Late input"]+ assert not any(isinstance(item, InputItem) for item in state._generated_items)++ model.set_next_output([get_text_message("Done")])+ result = await Runner.run(agent, state)+ assert result.final_output == "Done"+ assert calls == ["Paris", "Rome"]+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in model_input].count("Late input") == 1+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed", [False, True])+async def test_server_filter_reconstructed_pending_rewrite_is_rejected(streamed: bool) -> None:+ model, agent, state, _calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Late input")+ model.set_next_output([get_text_message("Done")])++ def reconstruct_pending(data: CallModelData[Any]) -> ModelInputData:+ rewritten = [+ {"role": "user", "content": "Filtered late input"}+ if _message_text(item) == "Late input"+ else item+ for item in data.model_data.input+ ]+ return ModelInputData(+ input=cast(list[TResponseInputItem], rewritten),+ instructions=data.model_data.instructions,+ )++ queued_outputs = len(model.turn_outputs)+ run_config = RunConfig(call_model_input_filter=reconstruct_pending)+ if streamed:+ failed = Runner.run_streamed(agent, state, run_config=run_config)+ with pytest.raises(UserError, match="cannot safely associate"):+ async for _event in failed.stream_events():+ pass+ else:+ with pytest.raises(UserError, match="cannot safely associate"):+ await Runner.run(agent, state, run_config=run_config)++ assert len(model.turn_outputs) == queued_outputs+ assert [_message_text(item) for item in state.pending_input] == ["Late input"]+++@pytest.mark.asyncio+async def test_server_filter_in_place_pending_rewrite_preserves_occurrence() -> None:+ model, agent, state, _calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Late input")+ model.set_next_output([get_text_message("Done")])++ def rewrite_pending_in_place(data: CallModelData[Any]) -> ModelInputData:+ for item in data.model_data.input:+ if isinstance(item, dict) and _message_text(item) == "Late input":+ cast(dict[str, Any], item)["content"] = "Filtered late input"+ return data.model_data++ result = await Runner.run(+ agent,+ state,+ run_config=RunConfig(call_model_input_filter=rewrite_pending_in_place),+ )++ assert result.final_output == "Done"+ model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in model_input].count("Filtered late input") == 1+ assert state.pending_input == []+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed_failure", [False, True])+async def test_server_response_acceptance_commits_before_hook_failure(+ streamed_failure: bool,+) -> None:+ class CountAgentResponseHook(AgentHooks[Any]):+ def __init__(self) -> None:+ self.call_count = 0++ async def on_llm_end(+ self,+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ _response: ModelResponse,+ ) -> None:+ self.call_count += 1++ class FailAfterResponse(RunHooks[Any]):+ async def on_llm_end(+ self,+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ _response: ModelResponse,+ ) -> None:+ raise RuntimeError("after response")++ model, agent, state, _calls = await _make_after_turn_state(auto_previous_response_id=True)+ agent_hooks = CountAgentResponseHook()+ agent.hooks = agent_hooks+ state.add_input("Late input")+ model.set_next_output([get_text_message("Accepted")])++ if streamed_failure:+ failed = Runner.run_streamed(agent, state, hooks=FailAfterResponse())+ with pytest.raises(RuntimeError, match="after response"):+ async for _event in failed.stream_events():+ pass+ else:+ with pytest.raises(RuntimeError, match="after response"):+ await Runner.run(agent, state, hooks=FailAfterResponse())++ accepted_model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in accepted_model_input].count("Late input") == 1+ assert state.pending_input == []+ assert isinstance(state._current_step, NextStepInterruption)+ assert state._current_step.response_accepted+ assert state._current_step.llm_end_hooks_started+ assert agent_hooks.call_count == 1+ state = await RunState.from_json(agent, state.to_json())+ queued_outputs = len(model.turn_outputs)++ recovered = await Runner.run(agent, state)+ assert recovered.final_output == "Accepted"+ assert agent_hooks.call_count == 1+ assert len(model.turn_outputs) == queued_outputs+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed_failure", [False, True])+async def test_server_acceptance_commits_before_invocation_validation_failure(+ streamed_failure: bool,+) -> None:+ model, agent, state, calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Late input")+ model.set_next_output(+ [+ get_function_tool_call(+ "record_destination",+ json.dumps({"destination": "Rome"}),+ call_id="call-destination",+ )+ ]+ )++ if streamed_failure:+ failed = Runner.run_streamed(agent, state)+ with pytest.raises(ModelBehaviorError, match="completed tool call ID"):+ async for _event in failed.stream_events():+ pass+ else:+ with pytest.raises(ModelBehaviorError, match="completed tool call ID"):+ await Runner.run(agent, state)++ accepted_model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in accepted_model_input].count("Late input") == 1+ assert state.pending_input == []+ assert isinstance(state._current_step, NextStepInterruption)+ assert state._current_step.response_accepted+ assert state._last_processed_response is None+ assert calls == ["Paris"]++ state = await RunState.from_json(agent, state.to_json())+ queued_outputs = len(model.turn_outputs)+ with pytest.raises(UserError, match="accepted model response could not be processed"):+ await Runner.run(agent, state)+ assert len(model.turn_outputs) == queued_outputs+ assert calls == ["Paris"]+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed_failure", [False, True])+async def test_server_accepted_computer_start_hook_failure_is_not_replayed(+ streamed_failure: bool,+) -> None:+ screenshots: list[str] = []++ class RecordingComputer(FakeComputer):+ def screenshot(self) -> str:+ screenshots.append("screenshot")+ return "img"++ class FailComputerStart(RunHooks[Any]):+ def __init__(self) -> None:+ self.call_count = 0++ async def on_tool_start(+ self,+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ tool: Tool,+ ) -> None:+ if isinstance(tool, ComputerTool):+ self.call_count += 1+ raise RuntimeError("computer hook failed")++ model, agent, state, _calls = await _make_after_turn_state(auto_previous_response_id=True)+ agent.tools = [ComputerTool(computer=RecordingComputer())]+ state.add_input("Late input")+ model.set_next_output(+ [+ ResponseComputerToolCall(+ id="computer-item",+ type="computer_call",+ action=ActionScreenshot(type="screenshot"),+ call_id="computer-call",+ pending_safety_checks=[],+ status="completed",+ )+ ]+ )+ hooks = FailComputerStart()++ if streamed_failure:+ failed = Runner.run_streamed(agent, state, hooks=hooks)+ with pytest.raises(RuntimeError, match="computer hook failed"):+ async for _event in failed.stream_events():+ pass+ else:+ with pytest.raises(RuntimeError, match="computer hook failed"):+ await Runner.run(agent, state, hooks=hooks)++ assert hooks.call_count == 1+ assert screenshots == []+ assert isinstance(state._current_step, NextStepInterruption)+ assert state._current_step.response_accepted++ state = await RunState.from_json(agent, state.to_json())+ with pytest.raises(ModelBehaviorError, match="output was not committed"):+ await Runner.run(agent, state)+ assert hooks.call_count == 1+ assert screenshots == []+++@pytest.mark.asyncio+@pytest.mark.parametrize("streamed_failure", [False, True])+@pytest.mark.parametrize("failure_phase", ["start", "end"])+async def test_server_accepted_tool_side_effect_failure_is_safe(+ streamed_failure: bool,+ failure_phase: str,+) -> None:+ class FailToolHook(RunHooks[Any]):+ async def on_tool_start(+ self,+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ _tool: Tool,+ ) -> None:+ if failure_phase == "start":+ raise RuntimeError("tool hook failed")++ async def on_tool_end(+ self,+ _context: RunContextWrapper[Any],+ _agent: Agent[Any],+ _tool: Tool,+ _result: object,+ ) -> None:+ if failure_phase == "end":+ raise RuntimeError("tool hook failed")++ model, agent, state, calls = await _make_after_turn_state(auto_previous_response_id=True)+ state.add_input("Late input")+ model.add_multiple_turn_outputs(+ [+ [+ get_function_tool_call(+ "record_destination",+ json.dumps({"destination": "Rome"}),+ call_id="call-retry-destination",+ )+ ],+ [get_text_message("Recovered")],+ ]+ )++ if streamed_failure:+ failed = Runner.run_streamed(agent, state, hooks=FailToolHook())+ with pytest.raises(UserError, match="tool hook failed"):+ async for _event in failed.stream_events():+ pass+ else:+ with pytest.raises(UserError, match="tool hook failed"):+ await Runner.run(agent, state, hooks=FailToolHook())++ assert state.pending_input == []+ assert isinstance(state._current_step, NextStepInterruption)+ assert state._current_step.response_accepted+ assert state._current_step.llm_end_hooks_started+ assert calls == (["Paris"] if failure_phase == "start" else ["Paris", "Rome"])++ state = await RunState.from_json(agent, state.to_json())+ if failure_phase == "start":+ with pytest.raises(ModelBehaviorError, match="output was not committed"):+ await Runner.run(agent, state)+ assert calls == ["Paris"]+ return++ recovered = await Runner.run(agent, state)+ assert recovered.final_output == "Recovered"+ assert calls == ["Paris", "Rome"]+ retry_model_input = cast(list[TResponseInputItem], model.last_turn_args["input"])+ assert [_message_text(item) for item in retry_model_input].count("Late input") == 0+++@pytest.mark.asyncio+async def test_terminal_state_rejects_pending_input_without_mutation() -> None:+ model = FakeModel(initial_output=[get_text_message("Done")])+ agent = Agent(name="assistant", model=model)+ result = await Runner.run(agent, "Initial request")+ state = result.to_state()+ before = state.to_json()++ with pytest.raises(UserError, match="terminal RunState"):+ state.add_input("Too late")++ assert state.to_json() == before