55import copy
66import weakref
77from collections .abc import AsyncIterator
8- from dataclasses import dataclass , field
8+ from dataclasses import InitVar , dataclass , field
99from typing import Any , Literal , TypeVar , cast
1010
1111from .agent import Agent
@@ -76,8 +76,9 @@ def _populate_state_from_result(
7676 state ._previous_response_id = previous_response_id
7777 state ._auto_previous_response_id = auto_previous_response_id
7878
79- if result .interruptions :
80- state ._current_step = NextStepInterruption (interruptions = result .interruptions )
79+ interruptions = list (getattr (result , "interruptions" , []))
80+ if interruptions :
81+ state ._current_step = NextStepInterruption (interruptions = interruptions )
8182
8283 trace_state = getattr (result , "_trace_state" , None )
8384 if trace_state is None :
@@ -120,9 +121,6 @@ class RunResultBase(abc.ABC):
120121 context_wrapper : RunContextWrapper [Any ]
121122 """The context wrapper for the agent run."""
122123
123- interruptions : list [ToolApprovalItem ]
124- """Pending tool approval requests (interruptions) for this run."""
125-
126124 _trace_state : TraceState | None = field (default = None , init = False , repr = False )
127125 """Serialized trace metadata captured during the run."""
128126
@@ -228,6 +226,8 @@ class RunResult(RunResultBase):
228226 """Whether automatic previous response tracking was enabled."""
229227 max_turns : int = 10
230228 """The maximum number of turns allowed for this run."""
229+ interruptions : list [ToolApprovalItem ] = field (default_factory = list )
230+ """Pending tool approval requests (interruptions) for this run."""
231231
232232 def __post_init__ (self ) -> None :
233233 self ._last_agent_ref = weakref .ref (self ._last_agent )
@@ -337,8 +337,6 @@ class RunResultStreaming(RunResultBase):
337337 repr = False ,
338338 default = None ,
339339 )
340- _last_processed_response : ProcessedResponse | None = field (default = None , repr = False )
341- """The last processed model response. This is needed for resuming from interruptions."""
342340
343341 _model_input_items : list [RunItem ] = field (default_factory = list , repr = False )
344342 """Filtered items used to build model input between streaming turns."""
@@ -356,6 +354,11 @@ class RunResultStreaming(RunResultBase):
356354 _input_guardrails_task : asyncio .Task [Any ] | None = field (default = None , repr = False )
357355 _output_guardrails_task : asyncio .Task [Any ] | None = field (default = None , repr = False )
358356 _stored_exception : Exception | None = field (default = None , repr = False )
357+ _cancel_mode : Literal ["none" , "immediate" , "after_turn" ] = field (default = "none" , repr = False )
358+ _last_processed_response : ProcessedResponse | None = field (default = None , repr = False )
359+ """The last processed model response. This is needed for resuming from interruptions."""
360+ interruptions : list [ToolApprovalItem ] = field (default_factory = list )
361+ """Pending tool approval requests (interruptions) for this run."""
359362 _waiting_on_event_queue : bool = field (default = False , repr = False )
360363
361364 _current_turn_persisted_item_count : int = 0
@@ -369,9 +372,6 @@ class RunResultStreaming(RunResultBase):
369372 """Original turn input before session history was merged, used for
370373 persistence (matches JS sessionInputOriginalSnapshot)."""
371374
372- # Soft cancel state
373- _cancel_mode : Literal ["none" , "immediate" , "after_turn" ] = field (default = "none" , repr = False )
374-
375375 _max_turns_handled : bool = field (default = False , repr = False )
376376
377377 _original_input : str | list [TResponseInputItem ] | None = field (default = None , repr = False )
@@ -386,12 +386,16 @@ class RunResultStreaming(RunResultBase):
386386 """Response identifier returned by the server for the last turn."""
387387 _auto_previous_response_id : bool = field (default = False , repr = False )
388388 """Whether automatic previous response tracking was enabled."""
389+ _run_impl_task : InitVar [asyncio .Task [Any ] | None ] = None
389390
390- def __post_init__ (self ) -> None :
391+ def __post_init__ (self , _run_impl_task : asyncio . Task [ Any ] | None ) -> None :
391392 self ._current_agent_ref = weakref .ref (self .current_agent )
392393 # Store the original input at creation time (it will be set via input field)
393394 if self ._original_input is None :
394395 self ._original_input = self .input
396+ # Compatibility shim: accept legacy `_run_impl_task` constructor keyword.
397+ if self .run_loop_task is None and _run_impl_task is not None :
398+ self .run_loop_task = _run_impl_task
395399
396400 @property
397401 def last_agent (self ) -> Agent [Any ]:
0 commit comments