
    Cjb                   @
   % S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
JrJrJrJr  SSKJrJr  SSKJr  SSKJrJrJrJrJr  SS	KJr  SS
KJrJrJrJrJ r J!r!J"r"  SSK#J$r$J%r%J&r&J'r'  SSK(J)r)J*r*J+r+  SSK,J-r-J.r.  SSK/J0r0  SSK1J2r2J3r3J4r4J5r5J6r6J7r7J8r8J9r9J:r:  SSK;J<r<  SSK=J>r>  SSK?J@r@JArAJBrBJCrCJDrD  SSKEJFrF  SSKGJHrHJIrIJJrJJKrKJLrLJMrMJNrNJOrOJPrPJQrQJRrRJSrSJTrTJUrUJVrVJWrWJXrXJYrY  SSKZJZrZ  SSK[J\r\  SSK]J^r^  SSK_J`r`  SSKaJbrbJcrcJdrdJereJfrfJgrgJhrhJiri  SSKjJkrkJlrlJmrmJnrnJoroJprpJqrqJrrr  SSKsJtrtJuru  SSKvJwrwJxrx  SSKyJzrz  \(       a  SSK?JBrBJDrD  SS KGJOrOJQrQ  SS!K{J|r|J}r}  \0" S"\S#9r~\0" S$S%S%S&9r\\\4   \\\   -  r\\/\\\4   4   r\\\\4   /\4   rS'rS(S)S*S+S,S-S.S/S0S1S2.
rS3\S4'   \" \5      r\\;  a  \" S55      e\R                  5        V Vs/ s H  u  pUGR                  5       (       a  M  U PM!     snn r\(       a  \" S6S7GR                  \5       35      e\-" \%5      rS8\S9'   \-" \$5      rS:\S;'   \-" \&5      rS<\S='   \-" \%\$-  \&-  5      rS>\S?'   \-" \'5      rS@\SA'   \-" \W5      rSB\SC'   \-" \)5      rSD\SE'   \" 5       r\" SF15      rSSG jr\ " SH SI\\~\4   5      5       rSSSJ jjrSSK jrSSL jr          SSM jrSSN jrSSO jr      SSP jrSSQ jr S     SSR jjrSSS jrSST jrSUSUSV.       SSW jjrSUSUSV.           SSX jjrSSY jrSSZ jrSS[ jrSS\.       SS] jjr    SS^ jrSSSUSUS_.               SS` jjr " Sa Sb5      rSS\.     SSc jjr      SSd jrSSe jr      SSf jrSSg jrSSSUSh.             SSi jjrSSSSUSj.                 SSk jjrSSl jrSSm jrSSn jrSSo jr  S         SSp jjrSSq jrSSSSr.           SSs jjr    SSt jr      SSu jr      SSv jr    SSw jrSSx.         SSy jjr    SSz jr    SS{ jr   S           SS| jjrSS} jrSS~ jrSS jrSS jrSS jrSS jrSS jr\" \GR                  " 5       5      \" \GR                  " 5       5      4rSS jrSS.     SS jjrSS jrSS jrSS jr        SS jrSS jrSS jrSS jrSS jrSSx.       SS jjrSS jrgs  snn f )zVRunState class for serializing and resuming agent runs with human-in-the-loop support.    )annotationsN)deque)CallableIteratorMappingSequence)	dataclassfield)Path)TYPE_CHECKINGAnyGenericLiteralcast)uuid4)ResponseComputerToolCallResponseCustomToolCallResponseFunctionToolCallResponseOutputMessageResponseOutputRefusalResponseOutputTextResponseReasoningItem)ComputerCallOutputFunctionCallOutputLocalShellCallOutputMcpApprovalResponse)LocalShellCallMcpApprovalRequestMcpListTools)TypeAdapterValidationError)TypeVar   )	FunctionToolLookupKeyNamedToolLookupKeybuild_function_tool_lookup_map$deserialize_function_tool_lookup_keyget_function_tool_lookup_key%get_function_tool_lookup_key_for_toolget_function_tool_namespace get_function_tool_qualified_name"serialize_function_tool_lookup_key)Agent)	UserError)GuardrailFunctionOutputInputGuardrailInputGuardrailResultOutputGuardrailOutputGuardrailResult)Handoff)CompactionItemHandoffCallItemHandoffOutputItemMCPApprovalRequestItemMCPApprovalResponseItemMCPListToolsItemMessageOutputItemModelResponseReasoningItemRunItemToolApprovalItemToolCallItemToolCallOutputItemToolSearchCallItemToolSearchOutputItemTResponseInputItem coerce_tool_search_call_raw_item"coerce_tool_search_output_raw_item)logger)RunContextWrapper)
Capability)BaseSandboxSession)ApplyPatchToolComputerTool
CustomToolFunctionToolHostedMCPToolLocalShellTool	ShellTool
ToolOrigin)AllowBehaviorRaiseExceptionBehaviorRejectContentBehaviorToolGuardrailFunctionOutputToolInputGuardrailToolInputGuardrailResultToolOutputGuardrailToolOutputGuardrailResult)Trace
TraceState)deserialize_usageserialize_usage)_to_dump_compatible)r1   r3   )r<   r>   )NextStepInterruptionProcessedResponseTContextdefaultTAgent
Agent[Any])boundrd   1.9z=Initial RunState snapshot format for HITL pause/resume flows.zJSame payload as 1.0, but introduces explicit backward-read support policy.zKPersists reasoning_item_id_policy for resumed and streamed follow-up turns.zLUpdates resumed trace semantics to reattach traces without duplicate starts.z;Stores request_id alongside each serialized model response.zRRenumbered unreleased baseline for tool-search snapshots and richer tool metadata.zBPersists explicit approval rejection messages across resume flows.z[Persists duplicate-name agent identities across agent-owned state and sandbox resume state.z=Persists SDK-generated prompt cache keys across resume flows.zPPersists pending custom tool calls and tool origin metadata across resume flows.)
z1.0z1.1z1.2z1.3z1.4z1.5z1.6z1.7z1.8rh   zdict[str, str]SCHEMA_VERSION_SUMMARIESzNCURRENT_SCHEMA_VERSION must have a matching entry in SCHEMA_VERSION_SUMMARIES.zZEvery supported RunState schema version must have a non-empty summary. Missing summaries: , zTypeAdapter[FunctionCallOutput]_FUNCTION_OUTPUT_ADAPTERzTypeAdapter[ComputerCallOutput]_COMPUTER_OUTPUT_ADAPTERz!TypeAdapter[LocalShellCallOutput]_LOCAL_SHELL_OUTPUT_ADAPTERzKTypeAdapter[FunctionCallOutput | ComputerCallOutput | LocalShellCallOutput]_TOOL_CALL_OUTPUT_UNION_ADAPTERz TypeAdapter[McpApprovalResponse]_MCP_APPROVAL_RESPONSE_ADAPTERzTypeAdapter[TResponseInputItem]_HANDOFF_OUTPUT_ADAPTERzTypeAdapter[LocalShellCall]_LOCAL_SHELL_CALL_ADAPTERstatusc                .    [         R                  " U 5      $ )z>Best-effort deserialization for optional tool origin metadata.)rR   from_json_dictdatas    b/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/agents/run_state.py_deserialize_tool_originrx      s    $$T**    c                  t   \ rS rSr% SrSrS\S'    SrS\S'    \" SS	S
9r	S\S'    \" \
S9rS\S'    \" \
S9rS\S'    SrS\S'    \" \
S9rS\S'    \" \
S9rS\S'    SrS\S'    SrS\S'    SrS\S'    S	rS\S'    SrS\S'    SrS\S'    \" \
S9rS \S!'    \" \
S9rS"\S#'    \" \
S9rS$\S%'    \" \
S9rS&\S''    SrS(\S)'    SrS*\S+'    \" SS	S
9rS\S,'    SrS\S-'    \" \S9rS.\S/'    \" SS	S
9r S0\S1'    \" SS	S
9r!S\S2'    \" SS	S
9r"S3\S4'    \" \#S	S
9r$S5\S6'     SYSSS	S7.             SZS8 jjjr%S[S9 jr&S\S]S: jjr' S\SS;.       S^S< jjjr(S_S= jr)S`S> jr*SaS? jr+SS	S@.     SbSA jjr,ScSB jr-SdSC jr.SeSD jr/SeSE jr0SfSF jr1SS	S	SG.       SgSH jjr2SSS	S	SI.           ShSJ jjr3SiSK jr4SSL.     SjSM jjr5SkSN jr6SS	S	SG.       SlSO jjr7SmSP jr8SnSQ jr9SoSR jr:SpSS jr;SqST jr<\=SSS	SU.           SrSV jj5       r>\=SSS	SU.           SsSW jj5       r?SXr@g)tRunState   a  Serializable snapshot of an agent run, including context, usage, and interruptions.

``RunState`` is the durable pause/resume boundary for human-in-the-loop flows. It stores
enough information to continue an interrupted run, including model responses, generated
items, approval state, and optional server-managed conversation identifiers.

Context serialization is intentionally conservative:

- Mapping contexts round-trip directly.
- Custom contexts may require a serializer and deserializer.
- When no safe serializer is available, the snapshot is still written but emits warnings and
  records metadata describing what is required to rebuild the original context type.
r   int_current_turnNzTAgent | None_current_agentF)rd   repr_starting_agent)default_factorystr | list[Any]_original_inputlist[ModelResponse]_model_responsesz"RunContextWrapper[TContext] | None_contextlist[RunItem]_generated_items_session_items
   
_max_turns
str | None_conversation_id_previous_response_idbool_auto_previous_response_id_generated_prompt_cache_key"Literal['preserve', 'omit'] | None_reasoning_item_id_policylist[InputGuardrailResult]_input_guardrail_resultslist[OutputGuardrailResult]_output_guardrail_resultslist[ToolInputGuardrailResult]_tool_input_guardrail_resultslist[ToolOutputGuardrailResult]_tool_output_guardrail_resultszNextStepInterruption | None_current_stepzProcessedResponse | None_last_processed_response&_generated_items_last_processed_marker"_current_turn_persisted_item_countdict[str, list[str]]_tool_use_tracker_snapshotzTraceState | None_trace_state_agent_tool_state_scope_iddict[str, Any] | None_sandboxstr_schema_version)conversation_idprevious_response_idauto_previous_response_idc                  Xl         [        U5      U l        X0l        X0l        X@l        XPl        X`l        Xpl        SU l	        SU l
        / U l        / U l        / U l        / U l        / U l        / U l        / U l        SU l        SU l        SU l        SU l        SU l        0 U l        SU l        SU l        [4        U l        SSKJn  U" U5      U l        g)zInitialize a new RunState.Nr   r#   )get_agent_tool_state_scope)r   _clone_original_inputr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   r   r   r   r   r   r   CURRENT_SCHEMA_VERSIONr   agent_tool_stater   r   )	selfcontextoriginal_inputstarting_agent	max_turnsr   r   r   r   s	            rw   __init__RunState.__init__  s      4^D-,# /%9"*C'+/()-& " " (*%)+&-/*.0+!(,%6:323/*,' 5@*DW*M'ry   c                    SSK Jn  U R                  b  [        U R                  U5      (       d  / $ U R                  R                  $ )zDReturn pending interruptions if the current step is an interruption.r#   r`   )run_internal.run_stepsr`   r   
isinstanceinterruptions)r   r`   s     rw   get_interruptionsRunState.get_interruptionsA  s>     	A%Z8J8JL`-a-aI!!///ry   c                f    U R                   c  [        S5      eU R                   R                  XS9  g)z:Approve a tool call and rerun with this state to continue.Nz,Cannot approve tool: RunState has no context)always_approve)r   r.   approve_tool)r   approval_itemr   s      rw   approveRunState.approveJ  s-    == JKK""="Pry   )rejection_messagec               j    U R                   c  [        S5      eU R                   R                  UUUS9  g)zReject a tool call and rerun with this state to continue.

When ``rejection_message`` is provided, that exact text is sent back to the model when the
run resumes. Otherwise the run-level tool error formatter or the SDK default message is
used.
Nz+Cannot reject tool: RunState has no context)always_rejectr   )r   r.   reject_tool)r   r   r   r   s       rw   rejectRunState.rejectP  s:     == IJJ!!'/ 	" 	
ry   c                   U R                   c  0 $ 0 nU R                   R                  R                  5        H  u  p#[        UR                  [
        5      (       a  UR                  O[        UR                  5      [        UR                  [
        5      (       a  UR                  O[        UR                  5      S.X'   UR                  (       a  [        UR                  5      X   S'   UR                  c  M  UR                  X   S'   M     U$ )z8Serialize approval records into a JSON-friendly mapping.)approvedrejectedrejection_messagessticky_rejection_message)r   
_approvalsitemsr   r   r   listr   r   dictr   )r   approvals_dict	tool_namerecords       rw   _serialize_approvalsRunState._serialize_approvalse  s    == I46!%!9!9!?!?!AI foot44 #OO&//*foot44 #OO&//*)N% ((BFvG`G`Ba)*>?..:33 )*DE "B ry   c                    U R                    VVs/ s HU  n[        UR                  5      UR                   Vs/ s H  n[	        U5      PM     snUR
                  UR                  S.PMW     snn$ s  snf s  snnf )zSerialize model responses.usageoutputresponse_id
request_id)r   r^   r   r   _serialize_raw_item_valuer   r   )r   respitems      rw   _serialize_model_responses#RunState._serialize_model_responses{  ss     --
 . )4GK{{S{t4T:{S#//"oo	 .
 	
 T
s   (A5A0A50A5c                   [        U R                  [        5      (       d  U R                  $ / nU R                   H  n[        U5      n[        U[        5      (       a\  [	        U5      nUR                  S5      nUS:X  a:  UR                  S5      n[        U[        5      (       a	  SUS./US'   SU;  a  SUS'   UR                  U5        M     U$ )zBNormalize original input into the shape expected by Responses API.role	assistantcontentoutput_text)typetextrr   	completed)r   r   r   r   r   getr   append)r   normalized_itemsr   normalized_itemr   r   s         rw   _serialize_original_input"RunState._serialize_original_input  s    $..55'''((D7=O/400"&"7&**62;&-11)<G!'3//?LV]6^5_	264?1##O4 )  ry   context_serializerstrict_contextc               p   U R                   c  S[        SSSSS94$ U R                   R                  nUc  S[        USSSS94$ [        U[        5      (       a  [        U5      [        USSSS94$ U(       a  Uc  [        S5      eUb@   U" U5      n[        U[        5      (       d  [        S5      e[        U5      [        US	S
SS94$ [        US5      (       a]   UR                  S
S9n[        U[        5      (       d  [        S5      e[        R                  " S5        [        U5      [        USS
SS94$ [        R                  " U5      (       aq  [        R                  " [!        ["        U5      5      n[        U[        5      (       d  [        S5      e[        R                  " S5        [        U5      [        USS
SS94$ [        R                  " S[%        U5      R&                  5        0 [        USS
S
S94$ ! [         a  n[        S5      UeSnAff = f! [         a    UR                  5       n GNLf = f)aW  Validate and serialize the stored run context.

The returned metadata captures how the context was serialized so restore-time code can
decide whether a deserializer or override is required. This lets RunState remain durable
for simple mapping contexts without silently pretending that richer custom objects can be
reconstructed automatically.
NnoneF)serialized_viarequires_deserializeromittedmappingzRunState serialization requires context to be a mapping when strict_context is True. Provide context_serializer to serialize custom contexts.z=Context serializer failed while serializing RunState context.z)Context serializer must return a mapping.r   T
model_dumpexclude_unsetz2RunState context model_dump must return a mapping.zRunState context was serialized from a Pydantic model. Provide context_deserializer or context_override to restore the original type.z7RunState dataclass context must serialize to a mapping.zRunState context was serialized from a dataclass. Provide context_deserializer or context_override to restore the original type.asdictzrRunState context of type %s is not serializable; storing empty context. Provide context_serializer to preserve it.r   )r   _build_context_metar   r   r   r   r.   	Exceptionhasattrr   	TypeErrorrG   warningdataclassesis_dataclassr   r   r   r   __name__)r   r   r   raw_context_payload
serializedexcs         rw   _serialize_context_payload#RunState._serialize_context_payload  s    == ,%&+	   #mm33&,#%&+	   )733()#'#,*/!	  08T 
 )/0CD

 j'22 KLLZ #'#7*.!	  &55>0;;$;O
 j'22 TUUNNa
 Z #'#/*.!	  ##$788$++D6I,JKJj'22 YZZNNa
 Z #'#+*.!	  	9$%..	
 #(&*	
 	
A  S&  >0;;=
>s*   G9 H 9
HHHH54H5c                6   Uc  g[         R                  " U5      (       a$  [         R                  " [        [        U5      5      $ [        US5      (       a   UR                  SS9n[        U5      $ [        U5      $ ! [         a    UR                  5       n N2f = f)z,Normalize tool input for JSON serialization.Nr   Tr   )	r  r  r   r   r   r  r   r  r_   )r   
tool_inputr  s      rw   _serialize_tool_inputRunState._serialize_tool_input  s    ##J//%%d3
&;<<:|,,5'222F
 'z22":..	  5'224
5s   A; ;BBc                   U R                   (       a  U R                   R                  (       d  gU R                  (       a  U R                  S   R                  OSnU R                  b*  [        [        [        [           U R                  5      5      OSnU R                   R                   Vs/ s H  nU R                  X2S9PM     nn[        R                  " U R                  UUS.S[        S9$ s  snf )zQReturn a marker for the processed response already reflected in _generated_items.Nagent_identity_keys_by_id)current_turnlast_response_id	new_itemsT)	sort_keysrd   )r   r  r   r   r    _build_agent_identity_keys_by_idr   r-   r   _serialize_itemjsondumpsr~   r   )r   latest_response_idr  r   serialized_itemss        rw   %_current_generated_items_merge_marker.RunState._current_generated_items_merge_marker)  s    --$2O2O2Y2Y 6:5J5JD!!"%11PT 	
 ##/ -T%*d>R>R-ST 	" 55??
?    [? 	 
 zz $ 2 2$6-
 
 	
	
s   +C.c                .    U R                  5       U l        g)zNRemember that _generated_items already include the current processed response.N)r  r   r   s    rw   0_mark_generated_items_merged_with_last_processed9RunState._mark_generated_items_merged_with_last_processedD  s    6:6`6`6b3ry   c                    SU l         g)zAForget any prior merge marker after _generated_items is replaced.N)r   r!  s    rw   ,_clear_generated_items_last_processed_marker5RunState._clear_generated_items_last_processed_markerH  s
    6:3ry   c                   [        U R                  5      nU R                  (       a  U R                  R                  (       d  U$ U R	                  5       nUb  U R
                  U:X  a  U$ [        5       n[        5       n[        5       nSS jnU Hj  nU" U5      u  pn
U(       a  U	(       a  UR                  X45        U
(       a  U	(       a  UR                  X45        MP  U
(       d  MY  UR                  U
5        Ml     U R                  R                   H  nU" U5      u  pn
U
(       a  U	(       a
  X4U;   a  M%  OU
(       a  X;   a  M4  U(       a  U	(       a	  X4U;   a  MK  U(       a  U	(       a  UR                  X45        U
(       a  U	(       a  UR                  X45        OU
(       a  UR                  U
5        UR                  U5        M     Ub  X l        U$ )z>Merge persisted and newly processed items without duplication.c                   S nS nS n[        U S5      (       ay  U R                  n[        U[        5      (       a4  UR	                  S5      nUR	                  S5      nUR	                  S5      nO$[        US5      n[        US5      n[        US5      nUc  [        U S5      (       a  [        U S5      nUc  [        U S5      (       a  [        U S5      nXU4$ )Nraw_itemidr   call_id)r  r)  r   r   r   	_get_attr)r   item_id	item_typer+  raws        rw   _id_type_callERunState._merge_generated_items_with_processed.<locals>._id_type_call]  s    GIGtZ((mmc4((!ggdmG #I!ggi0G'T2G )#v 6I'Y7G74#6#6#D$/ WT6%:%:%dF3	w..ry   )r   r   returnz)tuple[str | None, str | None, str | None])	r   r   r   r  r  r   setaddr   )r   generated_itemscurrent_merge_markerseen_id_typesseen_call_idsseen_call_id_typesr0  existingr-  r.  r+  new_items               rw   %_merge_generated_items_with_processed.RunState._merge_generated_items_with_processedL  s   t445--$2O2O2Y2Y""#IIK ,;;?SS"".1e"%%365	/( (H*7*A'G9!!7"679"&&';<!!'* ( 55??H*7*A'G9'+== >W59')=)N9!!7"679"&&';<!!'*""8, @"  +:N7ry   r   r   include_tracing_api_keyc               P   U R                   c  [        S5      eU R                  c  [        S5      eU R                  5       nU R	                  5       nU R                  5       nU R                  UUS9u  px[        U R                  R                  5      UUUS.n	U R                  U R                  R                  5      n
U
b  XS'   U R                  b*  [        [        [        [           U R                  5      5      OSn[!        [        [        [           U R                   5      US9n0 S["        _S	U R$                  _S
U_SU_SU_SU	_S[&        R(                  " U R*                  5      _SU R,                  _SS_S[/        U R0                  US9_S[/        U R2                  US9_S[5        U R6                  SS9_S[5        U R8                  SS9_SU R:                  _SU R<                  _SU R>                  _SU R@                  _SU RB                  0EnU RE                  5       nU Vs/ s H  nU RG                  XS9PM     snUS'   [I        U RJ                  5       Vs/ s H  nU RG                  XS9PM     snUS'   U RM                  5       US'   [O        U5      US '   U RP                  (       a  U RS                  U RP                  UUUUS!9OSUS"'   U RT                  US#'   U RW                  US$9US%'   U RX                  b#  [&        R(                  " U RX                  5      US&'   U$ s  snf s  snf )'a,  Serializes the run state to a JSON-compatible dictionary.

This method is used to serialize the run state to a dictionary that can be used to
resume the run later.

Args:
    context_serializer: Optional function to serialize non-mapping context values.
    strict_context: When True, require mapping contexts or a context_serializer.
    include_tracing_api_key: When True, include the tracing API key in the trace payload.

Returns:
    A dictionary representation of the run state.

Raises:
    UserError: If required state (agent, context) is missing.
Nz+Cannot serialize RunState: No current agentz%Cannot serialize RunState: No contextr   )r   	approvalsr   context_metar  r  $schemaVersionr  current_agentr   model_responsesr   tool_use_trackerr   no_active_agent_runTinput_guardrail_resultsoutput_guardrail_resultstool_input_guardrail_results)
type_labeltool_output_guardrail_resultstool_outputr   r   r   generated_prompt_cache_keyreasoning_item_id_policyr5  session_itemscurrent_steplast_model_responser  r   r   r?  last_processed_response!current_turn_persisted_item_countr?  tracesandbox)-r   r.   r   r   r   r   r
  r^   r   r  r  r   r  r   r-   r   _serialize_agent_referencer   r~   copydeepcopyr   r   _serialize_guardrail_resultsr   r   !_serialize_tool_guardrail_resultsr   r   r   r   r   r   r   r<  r  r   r   _serialize_current_step_serialize_last_model_responser   _serialize_processed_responser   _serialize_trace_datar   )r   r   r   r?  r   rE  original_input_serializedcontext_payloadrB  context_entryr  r  current_agent_entryresultr5  r   s                   rw   to_jsonRunState.to_json  s   . &IJJ== CDD22499;$($B$B$D!(,(G(G1) )H )
% %T]]%8%89'&()
 //0H0HI
!*4,' ##/ -T%*d>R>R-ST 	"
 9sT001&?


4
D..
 0
 7	

 
 }
 d.M.M N
 
 "4
 &'C--*C(
 '(D..*C)
$ +,M22|-%
* ,-N33.+
0 t441
2 #D$>$>3
4 ()H)H5
6 )$*J*J7
8 '(F(F9
> DDF (%
'    ['%
 ! T001#
1    [1#
 "&!=!=!?~(F(W$% ,, ..--*C#5-(? /   	() 7;6]6]2344$; 5 
w ==$ $dmm <F99%
#
s   "LL#rS  c          
        [        U5      n[        U UR                  S/ 5      UR                  U R                  UUUS9  UR
                   Vs/ s H%  n[        U[        5      (       d  M  [        USUS9PM'     nnUR                   V	s/ s H  n	U R                  XS9PM     sn	UR                  S.UESU0E$ s  snf s  sn	f )zSerialize a ProcessedResponse to JSON format.

Args:
    processed_response: The ProcessedResponse to serialize.

Returns:
    A dictionary representation of the ProcessedResponse.
	functions)parent_statefunction_entriesfunction_runsscope_idr   r   r?  Tinclude_tool_namer  r  )r  
tools_usedr   )_serialize_tool_action_groups)_serialize_pending_nested_agent_tool_runsr   rj  r   r   r   r?   %_serialize_tool_approval_interruptionr  r  rq  )
r   processed_responser  r   r   r?  action_groupsinterruptioninterruptions_datar   s
             rw   r`  &RunState._serialize_processed_response  s    $ 66HI1*..{B?,66441)$;	
  !3 @ @
 !A,(891"&*C
 !A 	 
 /888D $$T$_8 -77
 
 /
 	

s   B8*B8	B=c           	        SSK Jn  U R                  b*  [        [	        [
        [           U R                  5      5      OSnU R                  b  [        U R                  U5      (       d  gU R                  R                   Vs/ s H1  n[        U[        5      (       d  M  [        UUR                  SLUS9PM3     nnSSU0S.$ s  snf )z3Serialize the current step if it's an interruption.r#   r   Nro  next_step_interruptionr   )r   rv   )r   r`   r   r  r   r-   r   r   r   r   r?   rt  r   )r   r`   r  r   rx  s        rw   r^   RunState._serialize_current_step7  s     	A ##/ -T%*d>R>R-ST 	" %Z8J8JL`-a-a **88
 9$ 011"&.."<*C
 9 	 
 -!3
 	

s   CCr  c                  [        UR                  5      nUR                  U[        UR                  US9S.n[        US5      (       an  UR                  n [        US5      (       a  UR                  SS9nO1[        R                  " U5      (       a  [        R                  " U5      n[        U5      nXTS'   [        US5      (       a  [        UR                  US9US'   [        US5      (       a  [        UR                  US9US'   [        US	5      (       a  UR                   b  UR                   US	'   [        US5      (       a  UR"                  b  UR"                  US'   [%        ['        USS
5      5      nUb  XdS'   ['        USS5      (       a  SUS'   [        US5      (       a  UR(                  b  UR(                  US'   [        US5      (       a  UR*                  b  UR*                  US'   ['        USS
5      n[-        U[.        5      (       a  UR1                  5       US'   U$ ! [         a    [        UR                  5      n GNf = f)z-Serialize a run item to JSON-compatible dict.r  r   r)  agentr   r   Tr   source_agenttarget_agentr   Ntool_namespacetool_lookup_key_allow_bare_name_aliasFallow_bare_name_aliasdescriptiontitletool_origin)r   r)  r   rY  r  r  r   r   r  r  r   _ensure_json_compatibler   r   r  r  r   r  r,   getattrr  r  r   rR   to_json_dict)r   r   r  raw_item_dictrf  serialized_outputr  r  s           rw   r  RunState._serialize_itemV  s1    7t}}E II%/

*C"
 4"" $5,l;;(9(D(DSW(D(X% --.?@@(3(:(:;L(M%$;<M$N!  184((%?!!*C&F>" 4((%?!!*C&F>" 4%%$..*D"&..F;4)**t/B/B/N'+':':F#$<WTK\^b=cd&(7$%41599.2F*+4''D,<,<,H$($4$4F=!4!!djj&<"jjF7OdM48k:..$/$<$<$>F=!=  5$'$4!5s   AH& &I	I	c                  ^ T(       d  gS	U4S jjnU R                    H.  nUR                  S:w  a  M  U" UR                  5      nUc  M,  Us  $    U R                  bH  U R                  R                   H.  nUR                  S:w  a  M  U" UR                  5      nUc  M,  Us  $    [        U R                  [        5      (       a  U R                   H  n[        U[        5      (       d  M  UR                  S5      S:w  a  M1  [        [        S-  UR                  S5      5      nUT:X  d  M[  UR                  SS5      nU(       a  [        U5      s  $ Ss  $    g)
z;Attempt to find the function name for the provided call_id. c                X  > [        U [        5      (       aP  [        [        S -  U R	                  S5      5      nUT:X  a&  U R	                  SS5      nU(       a  [        U5      $ S$  g [        [        S -  [        U S5      5      nUT:X  a!  [        U SS5      nU(       a  [        U5      $ S$ g )Nr+  namer  )r   r   r   r   r   r,  )r/  candidate_call_id
name_valuer+  s      rw   _extract_name5RunState._lookup_function_name.<locals>._extract_name  s    #t$$$(tSWWY5G$H!$/!$!4J.83z?@b@ 0 	 %)tYsI5N$O!$/!*3!;J.83z?@b@ry   tool_call_itemNr   function_callr+  r  )r/  r   r2  r   )r   r   r)  r   r  r   r   r   r   r   r   r   )r   r+  r  run_itemr  
input_itemitem_call_idr  s    `      rw   _lookup_function_nameRunState._lookup_function_name  s+   	 --H}} 00 !2!23D . ((4 99CC==$44$X%6%67#K D d**D11"22
!*d33>>&)_<#C$J
y0IJ7*!+!;J.83z?@b@ 3 ry   c               J    [         R                  " U R                  UUUS9SS9$ )zSerializes the run state to a JSON string.

Args:
    include_tracing_api_key: When True, include the tracing API key in the trace payload.

Returns:
    JSON string representation of the run state.
r>     )indent)r  r  rg  )r   r   r   r?  s       rw   	to_stringRunState.to_string  s4     zzLL#5-(?  
 
 	
ry   c                :    [         R                  " U5      U l        g)z4Capture trace metadata for serialization/resumption.N)r\   
from_tracer   )r   rW  s     rw   	set_traceRunState.set_trace  s    &11%8ry   c               X    U R                   (       d  g U R                   R                  US9$ )NrV  )r   rg  )r   r?  s     rw   ra  RunState._serialize_trace_data  s)        ((AX(YYry   c                    U(       d  0 U l         g0 nUR                  5        HH  u  p4[        U[        5      (       d  M  U Vs/ s H  n[        U[        5      (       d  M  UPM     snX#'   MJ     X l         gs  snf )z5Store a copy of the serialized tool-use tracker data.N)r   r   r   r   )r   snapshot
normalized
agent_nametoolstools         rw   set_tool_use_tracker_snapshot&RunState.set_tool_use_tracker_snapshot  sh    .0D++-
!)!1Jj#..7<%Vut
4QT@Udu%VJ" "2 +5' &Ws   A5A5c                    Xl         g)zDStore how reasoning item IDs should appear in next-turn model input.N)r   )r   policys     rw   set_reasoning_item_id_policy%RunState.set_reasoning_item_id_policy  s    )/&ry   c                    U R                   R                  5        VVs0 s H  u  pU[        U5      _M     snn$ s  snnf )z9Return a defensive copy of the tool-use tracker snapshot.)r   r   r   )r   r  
tool_namess      rw   get_tool_use_tracker_snapshot&RunState.get_tool_use_tracker_snapshot  sE     +/*I*I*O*O*Q
*Q&
 Z((*Q
 	
 
s   :)context_overridecontext_deserializerr   c                  #     [         R                  " U5      n[        R                  U UUUUS9I Sh  vN $ ! [         R                   a  n[        SU 35      UeSnAff = f N/7f)a  Deserializes a run state from a JSON string.

This method is used to deserialize a run state from a string that was serialized using
the `to_string()` method.

Args:
    initial_agent: The initial agent (used to build agent map for resolution).
    state_string: The JSON string to deserialize.
    context_override: Optional context mapping or RunContextWrapper to use instead of the
        serialized context.
    context_deserializer: Optional function to rebuild non-mapping context values.
    strict_context: When True, require a deserializer or override for non-mapping contexts.

Returns:
    A reconstructed RunState instance.

Raises:
    UserError: If the string is invalid JSON or has incompatible schema version.
z Failed to parse run state JSON: Ninitial_agent
state_jsonr  r  r   )r  loadsJSONDecodeErrorr.   r{   	from_json)r  state_stringr  r  r   r  es          rw   from_stringRunState.from_string  s{     8	KL1J '''!-!5) ( 
 
 	
 ## 	K>qcBCJ	K
s/   A&9 A&A$A&A!AA!!A&c               8   #    [        U UUUUS9I Sh  vN $  N7f)a  Deserializes a run state from a JSON dictionary.

This method is used to deserialize a run state from a dict that was created using
the `to_json()` method.

Args:
    initial_agent: The initial agent (used to build agent map for resolution).
    state_json: The JSON dictionary to deserialize.
    context_override: Optional context mapping or RunContextWrapper to use instead of the
        serialized context.
    context_deserializer: Optional function to rebuild non-mapping context values.
    strict_context: When True, require a deserializer or override for non-mapping contexts.

Returns:
    A reconstructed RunState instance.

Raises:
    UserError: If the dict has incompatible schema version.
r  N)_build_run_state_from_jsonr  s        rw   r  RunState.from_json$  s-     8 0'!-!5)
 
 	
 
s   )r   r   r   r   r   r   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   )r   zRunContextWrapper[TContext]r   r   r   re   r   r}   r   r   r   r   r   r   )r2  zlist[ToolApprovalItem])F)r   r?   r   r   r2  None)r   r?   r   r   r   r   r2  r  )r2  zdict[str, dict[str, Any]])r2  list[dict[str, Any]])r2  r   )r   ContextSerializer | Noner   r   r2  z,tuple[dict[str, Any] | None, dict[str, Any]])r  r   r2  r   )r2  r   )r2  r  )r2  r   )r   r  r   r   r?  r   r2  dict[str, Any])ru  ra   r  Mapping[int, str] | Noner   r  r   r   r?  r   r2  r  )r2  r   )r   r>   r  r  r2  r  )r+  r   r2  r   )r   r  r   r   r?  r   r2  r   )rW  zTrace | Noner2  r  )r?  r   r2  r   )r  z"Mapping[str, Sequence[str]] | Noner2  r  )r  r   r2  r  )r2  r   )r  rf   r  r   r  ContextOverride | Noner  ContextDeserializer | Noner   r   r2  RunState[Any, Agent[Any]]r  rf   r  r  r  r  r  r  r   r   r2  r  )Ar  
__module____qualname____firstlineno____doc__r~   __annotations__r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
  r  r  r"  r%  r<  rg  r`  r^  r  r  r  r  ra  r  r  r  staticmethodr  r  __static_attributes__ ry   rw   r{   r{      s    M32$(NM(8%*4e%DO]DW',T'BO_B6,1$,G)G*37H07D&+D&AmAU$)$$?NM?9J;#'j'K(,:,B',,K.22IDHAHJ;@QU;V8V;=BSW=X:X<DIZ^D_!#A_DFK\`Fa"$CaE15M.5K9=6=\9>tRW9X*JX^./&/S7<T7R 4RT&+Du&EL#EA-24e-L
LZ&+Du&EH#EC )?eLOSL\ (N '++/*/(N,(N ((N 	(N
 (N $(N )(N $((NT0Q $

 )-
'
 

 &
 

*,

 . 8<$	{
 5{
 	{

 
6{
z/"
6c;AL 8<$(-u 5u 	u
 "&u 
uv ?C7;$(-/
-/
 $<	/

 5/
 /
 "&/
 
/
b
F ?C	99 $<	9
 
9v/h 8<$(-
 5
 	

 "&
 

09Z
50
 
 48;?$&
!&
&
 1	&

 9&
 &
 
#&
 &
P 
 48;?$!
!!
"!
 1	!

 9!
 !
 
#!
 !
ry   r{   c                    [        XU5      $ )zBReturn attribute value if present, otherwise the provided default.)r  )objattrrd   s      rw   r,  r,  N  s    3g&&ry   c                    U c  g[        U [        5      (       a  g[        U S5      (       a  g[        R                  " U 5      (       a  gg)z6Summarize a context object for serialization metadata.r   r   r   pydanticr	   custom)r   r   r  r  r  values    rw   _describe_context_typer  S  sA    }%!!ul##&&ry   c                    U c  gU R                   n[        USS5      n[        USS5      nU(       a  U(       d  gU SU 3$ )z2Return module and qualname for debugging purposes.Nr  r  r  :)	__class__r  )r  clsmodulequalnames       rw   _context_class_pathr  `  sH    }
//CS,+FsNB/HXQxj!!ry   c               b    [        U 5      nUUUUS.n[        U 5      nU(       a
  US;  a  XeS'   U$ )zHCapture context serialization metadata for debugging and recovery hints.)original_typer   r   r   >   r   r   
class_path)r  r  )original_contextr   r   r   r  metar  s          rw   r   r   l  sG     ++;<M&(!6	D %%56Jm+>>'\Kry   c                    [        U [        5      (       d  gU R                  S5      (       a  g[        U R                  S5      5      $ )zPReturn True when metadata indicates a non-mapping context needs help to restore.Fr   Tr   )r   r   r   r   )rB  s    rw   #_context_meta_requires_deserializerr    s=    lG,,	""  !89::ry   c                    [        U [        5      (       d   gU R                  S5      =(       d    SnU R                  S5      nU(       a  U SU S3O
[        U5      nU R                  S5      (       a  SU S	3$ S
U S3$ )zHBuild a warning message describing context deserialization requirements.zsRunState context was serialized from a custom type; provide context_deserializer or context_override to restore it.r  r  r  z ()r   z6RunState context was omitted during serialization for z(; provide context_override to supply it.z%RunState context was serialized from zA; provide context_deserializer or context_override to restore it.)r   r   r   r   )rB  r  r  rK  s       rw   _context_meta_warning_messager    s    lG,,1	
 !$$_5AM!!,/J6@M?"ZL2c-FXJ	""DlBD	

 	0,W	Yry   c                   [        U [        5      (       a]  0 nU R                  5        HE  u  p4UR                  X35      n[        U[        [        -  5      (       a  [        XA5      X%'   MA  XBU'   MG     U$ [        U [        5      (       a:  U  Vs/ s H,  n[        U[        [        -  5      (       a  [        Xa5      OUPM.     sn$ U $ s  snf )z9Recursively remap field names using the provided mapping.)r   r   r   r   r   _transform_field_names)rv   	field_maptransformedkeyr  
mapped_keyr   s          rw   r  r    s     $&(**,JC"s0J%--*@*R'*/J' ' $ 
 8B$t7T7T"43Z^^
 	

 K
s   3Cc                    [        U S5      (       a  U R                  SS9$ [        U [        5      (       a  [        U 5      $ U $ )z3Return a serializable representation of a raw item.r   Tr   )r  r   r   r   )r)  s    rw   r   r     s?    x&&"""66(D!!H~Ory   c                    SU R                   0nUb0  UR                  [        U 5      5      nUb  X0R                   :w  a  X2S'   U$ )zJSerialize an agent reference with an optional duplicate-name identity key.r  identity)r  r   r*  )r  r  entryr  s       rw   rY  rY    sH    
 $UZZ0E ,,00E;H

$: (*Lry   c                     [         R                  " [         R                  " U [        S95      $ ! [         a    [        U 5      s $ f = f)Nrc   )r  r  r  r   r   r  s    rw   r  r    s:    zz$**UC899 5zs   ,/ AAc                    [        U 5      $ )z1Convert a tool call to a serializable dictionary.)r   )	tool_calls    rw   _serialize_tool_call_datar    s    $Y//ry   Finclude_descriptioninclude_params_schemac               r   S[        U S5      (       a  U R                  OS0n[        U 5      nUb  XCS'   [        U 5      nUb  XSS   :w  a  XSS'   [	        [        U 5      5      nUb  XcS'   U(       a   [        U S5      (       a  U R                  US'   U(       a   [        U S5      (       a  U R                  US'   U$ )	z6Build a dictionary of tool metadata for serialization.r  N	namespacequalifiedName	lookupKeyr  params_json_schemaparamsJsonSchema)r  r  r*   r+   r,   r)   r  r	  )r  r  r  metadatar  qualified_name
lookup_keys          rw   _serialize_tool_metadatar    s     !'WT65J5J		PTUH+D1I )5d;N!n8H&H$2!34YZ^4_`J *wt];;"&"2"2/C!D!D'+'>'>#$Ory   c                   / nU  H@  n[        Xa5      n[        UUUS9nUR                  S[        UR                  5      X(05        MB     U$ )z9Serialize tool action runs that share the same structure.r  r   )r  r  r   r  r   )	actions	tool_attrwrapper_keyr  r  serialized_actionsactionr  	tool_dicts	            rw   _serialize_tool_actionsr    sb     v), 3"7
	
 	!!6v7G7GH	
  ry   c                    / nU  HW  nUR                   n[        US5      =(       d    [        US5      nUR                  [        UR                  5      SU0S.5        MY     U$ )zSerialize handoff tool calls.r   r  r   handoff)r  r,  r   r  r   )handoffsserialized_handoffsr  handoff_targethandoff_names        rw   _serialize_handoffsr    se      =b>[aAb""6w7H7HI'6	
  ry   c                    / nU  HA  n[        UR                  5      nUR                  SU0[        UR                  5      S.5        MC     U$ )z7Serialize MCP approval requests in a consistent format.r)  request_itemmcp_tool)r   r!  r   _serialize_mcp_toolr"  )requestsserialized_requestsrequestrequest_item_dicts       rw    _serialize_mcp_approval_requestsr(    sU    5g6J6JK""!+-> ?/0@0@A	
  ry   c                   U c  0 $ Sn[        U S5      (       a9   U R                  5       n[        U[        5      (       a  [        U5      nOUb  SU0nUc  [        U 5      nUR                  S5      c  [        U S5      US'   [        U S5      nUb  SU;  a  [        U5      US'   [        U5      n[        U[        5      (       a  [        U5      $ SU0$ ! [         a    Sn Nf = f)z3Serialize an MCP tool into a JSON-friendly mapping.Nrg  r  r  tool_config)r  rg  r   r   r   r   r  r   r,  r   r  )r"  r  	tool_jsonr*  r  s        rw   r#  r#  (  s    	'+Ix##	 ((*I i))YI" ),I,X6	}}V$%h7	&Hm4K=	#A#<[#I	- (3J*g&&JZ  )  	I	s   C C"!C"r  c                  S[        U R                  5      [        U R                  US9S.nU(       a  U R                  b  U R                  US'   U R
                  b  U R
                  US'   U R                  b  U R                  R                  5       US'   [        [        U SS5      5      nUb  XCS'   U R                  (       a  S	US
'   U$ )z*Serialize a ToolApprovalItem interruption.tool_approval_itemr  r~  Nr   r  r  r  Tr  )r   r)  rY  r  r   r  r  r  r,   r  r  )rw  rp  r  interruption_dictr  s        rw   rt  rt  H  s     %-l.C.CD+&?
) \33?)5)?)?+&""..:.I.I*+++7+C+C+P+P+R-(8/6O "/>+,**5912ry   c                   SU R                   SSSS4SU R                  SSSS4S	U R                  S
S
SS4SU R                  SSSS4SU R                  SSSS4SU R
                  SSSS4/nU VVVVVVs0 s H  u  nnnnnnU[        UUUUUS9_M     nnnnnnn[        U R                  5      US'   [        U R                  5      US'   U$ s  snnnnnnf )z9Serialize tool-related action groups using a shared spec.rj  function_toolr  Tcomputer_actionscomputer_toolcomputerFcustom_tool_actionscustom_toollocal_shell_actionslocal_shell_toollocal_shellshell_actions
shell_toolshellapply_patch_actionsapply_patch_toolapply_patch)r  r  r  r  r  mcp_approval_requests)rj  r1  custom_tool_callslocal_shell_callsshell_callsapply_patch_callsr  r  r  r(  r?  )	ru  action_specsr  r  r  r  r  r  r  s	            rw   rr  rr  g  sb    ((	
 //	
 "00	
 "00	
 **	
 "00	
S1	 H 3 3 
! 	$# 3"7
 	
   3" 11C1L1LMJz*J00+J&' +3s   2C
)rn  r   r   r?  c                   U(       a  U(       d  gSSK Jn  [        XSS9 H  u  p[        U	SS5      n
[	        U
[
        5      (       d  M)  U" XS9nUc  M4  [        USS5      n[	        U[        5      (       a  U(       d  M_  [        US	S5      n[        U5      (       d  M~   U" 5       n[	        U[        5      (       d  M  XL a  M   UR                  UUUS9US'   M     g! [         a-    U(       a  e [        R                  " S
U
R                  5         M  f = f! [         a.    U(       a  e [        R                  " SU
R                  5         GM.  f = f)zKAttach serialized nested run state for pending agent-as-tool interruptions.Nr#   )peek_agent_tool_run_resultFstrictr   rn  r   to_statez:Failed to capture nested agent run state for tool call %s.r>  agent_run_statez<Failed to serialize nested agent run state for tool call %s.)r   rF  zipr  r   r   r   callabler   rG   r  r+  r{   rg  )rk  rl  rm  rn  r   r   r?  rF  r  function_runr   pending_run_resultr   rJ  nested_states                  rw   rs  rs    sN    =<"#35QL+t<	)%=>>7	U% 2OTJ-..m-z4@!!		#:L ,11'	'3';';#5-(? (< (E#$G  R&  	NNL!! 	*  	NNN!! 	s$   CD3DD3EEc                  ,    \ rS rSrSrSS jrSS jrSrg)	_SerializedAgentToolRunResulti  zLMinimal run-result wrapper used to restore nested agent-as-tool resumptions.c                Z    Xl         [        UR                  5       5      U l        S U l        g N)_stater   r   r   final_output)r   states     rw   r   &_SerializedAgentToolRunResult.__init__  s%    !%"9"9";< ry   c                    U R                   $ rT  )rU  r!  s    rw   rJ  &_SerializedAgentToolRunResult.to_state  s    {{ry   )rU  rV  r   N)rW  r  r2  r  )r2  r  )r  r  r  r  r  r   rJ  r  r  ry   rw   rR  rR    s    V!
ry   rR  c               p   / nU  H  n[        U[        5      (       a  SOSUR                  R                  S.UR                  R
                  UR                  R                  S.S.n[        U[        5      (       a&  UR                  US'   [        UR                  US9US'   UR                  U5        M     U$ )	z,Serialize guardrail results for persistence.r   inputr   r  )tripwireTriggered
outputInfo	guardrailr   agentOutputr  r  )r   r3   ra  r  r   tripwire_triggeredoutput_infoagent_outputrY  r  r   )resultsr  r  rf  r  s        rw   r\  r\    s     (*J %/v7L$M$MSZ((--
 &,]]%E%E$mm77	
 f344#)#6#6E- 7*CE'N 	% # $ ry   c               6   / nU  H  n[        UR                  S5      (       a  UR                  R                  5       O[        UR                  SS5      nUR	                  XS.UR
                  R                  UR
                  R                  S.S.5        M     U$ )z1Serialize tool guardrail results for persistence.get_namer  Nr]  )r_  behaviorr`  )r  ra  rh  r  r   r   rd  ri  )rf  rK  r  rf  guardrail_names        rw   r]  r]  !  s     (*J v''44 %%'))648 	
 	&0I"(--";"; & 6 6	
  ry   c                    U (       d  gU S   $ )z2Return the last serialized model response, if any.Nr  r  )rE  s    rw   r_  r_  :  s    2ry   c                   U[         L aN  [        [        [        [        4   [        U  Vs/ s H  n[        U[         5      (       d  M  UPM     sn5      5      $ 0 nU  Hx  n[        X!5      (       a  [        US5      (       d  M&  [        USS5      n[        U[        5      (       a  U(       d  MQ  X#U'   U[        L d  M`  US:X  a  X#S'   Ml  US:X  d  Mt  X#S'   Mz     U$ s  snf )z3Build a name-indexed map for tools of a given type.r  Nr3  computer_use_preview)rN   r   r   r%   r   r&   r   r  r  r   rL   )r  	tool_typer  tool_mapr   s        rw   _build_named_tool_maprp  A  s     L #S()*"'J%$:dL+I%J
 	
 /1H$**'$2G2GD&$/	)S))"$ J&37/044'+$  O' Ks   CCc                    0 n[        U S5      (       d  U$ U R                   HH  n[        U[        5      (       d  M  [	        USS5      =(       d    [	        USS5      nU(       d  MD  X!U'   MJ     U$ )z=Map handoff tool names to their definitions for quick lookup.r  r   Nr  )r  r  r   r4   r  )rD  handoffs_mapr  r  s       rw   _build_handoffs_maprs  _  sl    8:L=*-- ))'7++wT:\ggvW[>\<)0& * ry   )rn  r  r   c                N  #    U(       a  U(       d  gSSK JnJn  [        XSS9 H  u  p[	        U[
        5      (       d  M  UR                  S5      n
[	        U
[
        5      (       d  MD  [        U	SS5      n[	        U[        5      (       d  Mh   [        U [        U
5      UUS9I Sh  vN n[        U5      nUR                   (       d  M  U" XS
9  U" U[#        [$        U5      US
9  M     g NC! [         a-    U(       a  e [        R                  " S	UR                  5         M  f = f7f)zLRehydrate nested agent-as-tool run state into the ephemeral tool-call cache.Nr#   )drop_agent_tool_run_resultrecord_agent_tool_run_resultFrG  rK  r   )r  r  r  r   z>Failed to deserialize nested agent run state for tool call %s.rI  )r   ru  rv  rL  r   r   r   r  r   r  r   r   rG   r  r+  rR  r   r   r   )rD  rl  rm  rn  r  r   ru  rv  r  rN  nested_state_datar   rP  pending_results                 rw   '_restore_pending_nested_agent_tool_runsry  n  s     =Z"#35Q%))!II&78+W55L+t<	)%=>>	!;+ 12%9-	" L 7|D++ 	#9@$YS.0IT\]E  R  	NNP!! 	s<   B
D%C+%C)&C+*?D%)C++3D"D%!D""D%agent_identity_maprn  r  r   c               n  ^ ^"^#^$^%^&^'^(^)^*^+^,^-^.^/^0^1^2#    [        T R                  S/ 5      UUS9n[        US5      (       a  UR                  U5      I Sh  vN n	O/ n	[	        U	[
        5      m2[	        U	[        5      m-[	        U	[        5      m.[	        U	[        5      m0[	        U	[        5      m1[	        U	[        5      m,[	        U	[        5      n
[        U5      m/SSKJnJm"Jm#Jm$Jm%Jm&Jm'JnJm(  SS.             SS jjm)SS	 jm+S S
 jm*S!U"U#U$U%U&U'U(U)U*U+U,U-U.U/U0U U1U24S jjnU" 5       nUS   nUS   nUS   nUS   nUS   nUS   nUS   n[/        UT R                  S/ 5      UUUUS9I Sh  vN   / nT R                  S/ 5       H  nUR                  S0 5      n[1        U[2        5      (       a  UR                  S0 5      O0 n[5        [6        5      nUR9                  U5      nUR                  S0 5      nU(       d  My  UR                  S5      nU(       a  U
R                  U5      OSnU(       d  M  UR;                  U" UUS95        M     / nT R                  S/ 5       H%  n [=        U UUUS9n!U!c  M  UR;                  U!5        M'     U" UUUUUUUUT R                  S/ 5      UUS9$  GNZ GN@7f)"aQ  Deserialize a ProcessedResponse from JSON data.

Args:
    processed_response_data: Serialized ProcessedResponse dictionary.
    current_agent: The current agent (used to get tools and handoffs).
    context: The run context wrapper.
    agent_map: Map of agent names to agents.

Returns:
    A reconstructed ProcessedResponse instance.
r  r{  get_all_toolsNr#   )	ra   ToolRunApplyPatchCallToolRunComputerActionToolRunCustomToolRunFunctionToolRunHandoffToolRunLocalShellCallToolRunMCPApprovalRequestToolRunShellCall)name_resolverc               @   / nU =(       d    /  GHy  n[        U[        5      (       a  UR                  U0 5      O0 nU(       a	  U" U5      n	O)[        U[        5      (       a  UR                  S5      n	OSn	U	(       a  UR                  U	5      OSn
U
c  Uc~  [        U[        5      (       ai  [        UR                  S5      [        5      (       dE  UR                  S5      n[        U[        5      (       a  [	        U5      nUb  UR                  U5      n
U
(       d  GM  [        U[        5      (       a  UR                  S0 5      O0 n[        U[        5      (       a  [        U5      O0 n U" U5      nUR                  U" X5      5        GM|     U$ ! [         a     GM  f = f)z/Deserialize tool actions with shared structure.r  Nr  r   )r   r   r   r   r(   r   r   r   )entriestool_keyro  call_parseraction_factoryr  deserializedr  tool_containerr   r  	bare_namebare_lookup_keytool_call_data_rawtool_call_datar   s                   rw   _deserialize_actions=_deserialize_processed_response.<locals>._deserialize_actions  sk    #%]]E8B5'8R8RUYYx4XZN)%0	ng66 . 2 26 :I $I.78<<	*TD!)~w77">#5#5k#BCHH*..v6	i--&B9&MO&2'||O<?I%QX?Y?Y;!;_a,67I7,S,S'(Y[ '7	 y ?@A #B   s   (F
FFc                J     U R                  U5      $ ! [         a    Us $ f = frT  )validate_pythonr!   )adapterrv   s     rw   _parse_with_adapter<_deserialize_processed_response.<locals>._parse_with_adapter  s,    	**400 	K	s    ""c                >     [        S0 U D6$ ! [         a    U s $ f = fNr  )r   r   ru   s    rw   _parse_apply_patch_call@_deserialize_processed_response.<locals>._parse_apply_patch_call  s)    	+3d33 	K	s   
 c                 0  > SS jn S S jnSSTS U4S jU 4SSTS	 U4S
 jU4SSTS U4S jS 4SSTS U4S jS 4SSTU4S jU4S jS 4SSTU4S jU4S jS 4SSTTU
4S jS 4/n0 nU H'  u  nnnnnn	T" TR                  U/ 5      UUUUU	S9X4'   M)     U$ )!Nc                    U R                  S0 5      n[        U[        5      (       d  g UR                  S5      n[        [        S -  [        U[
        5      (       a  U5      $ S 5      $ )Nr  r   )r   r   r   r   r%   r   )rv   handoff_datar   s      rw   _resolve_handoff_tool_nameg_deserialize_processed_response.<locals>._deserialize_action_groups.<locals>._resolve_handoff_tool_name  sb    88Ir2LlG44$((5I"T)
9c8R8R9 X\ ry   c           	     B   U R                  S0 5      n[        U[        5      (       a  [        UR                  S5      5      nUb  U$ U R                  S0 5      n[        U[        5      (       aQ  [	        [        [        S -  UR                  S5      5      [        [        S -  UR                  S5      5      5      nUb  U$ [        U[        5      (       d  g [	        [        [        S -  UR                  S5      5      [        [        S -  UR                  S5      5      5      $ )Nr  r  r   r  r  )r   r   r   r'   r(   r   r   )rv   	tool_datar  r  s       rw   _resolve_function_tool_nameh_deserialize_processed_response.<locals>._deserialize_action_groups.<locals>._resolve_function_tool_name  s    ,I)W--A)--P[B\]
)%%!XXk26N.'229t^%7%7%?@t^%7%7%DE
 )%%i11/S4Zv!67S4Z{!;< ry   r  r  c                    [        S0 U D6$ r  r   ru   s    rw   <lambda>U_deserialize_processed_response.<locals>._deserialize_action_groups.<locals>.<lambda>A      5==ry   c                   > T" XS9$ )Nr  r  )r   r  r  s     rw   r  r  B  s
    >I+_ry   rj  r  c                    [        S0 U D6$ r  r  ru   s    rw   r  r  I  r  ry   c                   > T" XS9$ )N)r   r0  r  )r   r0  r  s     rw   r  r  J  s    '2ry   r1  r3  c                    [        S0 U D6$ r  )r   ru   s    rw   r  r  S  r  ry   c                   > T" XS9$ )N)r   r2  r  )r   r2  r  s     rw   r  r  T  s    1F'2ry   r4  r5  c                    [        S0 U D6$ r  )r   ru   s    rw   r  r  ]  s    3;d;ry   c                   > T" XS9$ )N)r   r5  r  )r   r5  r  s     rw   r  r  ^  s    }'0ry   r6  r8  c                   > T" [         U 5      $ rT  rq   rv   r  s    rw   r  r  g      01JDQry   c                   > T" XS9$ )N)r   r7  r  )r   r7  r  s     rw   r  r  h      4I'5ry   r9  r;  c                   > T" [         U 5      $ rT  r  r  s    rw   r  r  q  r  ry   c                   > T" XS9$ )N)r   r:  r  )r   r:  r  s     rw   r  r  r  s    .>'/ry   r<  r>  c                   > T" XS9$ )N)r   r=  r  )r   r=  r  s     rw   r  r  |  r  ry   )r  ro  r  r  r  )rv   Mapping[str, Any]r2  zNamedToolLookupKey | None)rv   r  r2  zFunctionToolLookupKey | None)r   )r  r  rD  rv  r  r  ro  r  r  r  r  r  r  r  r  r  r  r  r  r  apply_patch_tools_mapcomputer_tools_mapcustom_tools_maprr  local_shell_tools_mapprocessed_response_datashell_tools_map	tools_maps             rw   _deserialize_action_groupsC_deserialize_processed_response.<locals>._deserialize_action_groups  s>   		D =_* = ,	 #"= 	 & ; 	 &%Q 	  Q 	 &%' 	wE
 	` /1 
!5'++C4!!'-+"M  ry   r  rj  r1  r4  r6  r9  r<  )rD  rl  rm  rn  r  r   r?  r!  r)  r"  r  r   r   	agent_mapr{  fallback_agentrq  )r  r  rj  r1  r@  rA  rB  rC  rq  r?  r   )r  r  r  r   ro  z Mapping[NamedToolLookupKey, Any]r  zCallable[[dict[str, Any]], Any]r  zCallable[[Any, Any], Any]r  z?Callable[[Mapping[str, Any]], NamedToolLookupKey | None] | Noner2  z	list[Any])r  zTypeAdapter[Any]rv   r  r2  r   )rv   r  r2  r   )r2  zdict[str, list[Any]])_deserialize_itemsr   r  r~  rp  rN   rL   rM   rP   rQ   rK   rO   rs  r   ra   r  r  r  r  r  r  r  r  ry  r   r   r    r   r  r   _deserialize_tool_approval_item)3r  rD  r   r  r{  rn  r  r   r  	all_toolsmcp_tools_mapra   r  r  rv  r  rj  r1  r4  r6  r9  r<  r?  request_datarequest_item_dataraw_item_datarequest_item_adapterr!  mcp_tool_datamcp_tool_namer"  r   interruption_datar   r  r  r  r  r  r  r  r  r  r  r  r  r  rr  r  r  r  s3   `                                 @@@@@@@@@@@@@@@@@rw   _deserialize_processed_responser    s.    , ###K4-I }o..'55g>>		%i>I.y,G,Y
C1)^L+IyAO1)^L))]CM&}5L
 
 
& Z^,%, , 3	,
 5, 2, W, 
,\B B B BH /0MZ(Hk*I$%78'(=>'(=>!/2M'(=>
1#044["E1%   >@/334KRP(,,^R@5?@QSZ5[5[!!*b1ac 	 ALL^@_+;;MJ$((R8%))&17D=$$]3$8!(()!-%! Q. -/M488"M71(	
 $  / N )--!-*..|R@3# Y ?@s2   AJ5J/DJ52J23CJ58>J5:6J52J5c                    [        U [        5      (       d  U $ U R                  S5      nUS:X  a   [        S0 U D6$ US;   a  U $  [        S0 U D6$ ! [         a    U s $ f = f! [         a    U s $ f = f)zUDeserialize a tool call raw item when possible, falling back to the original mapping.r   r  >   
shell_callapply_patch_callhosted_tool_calllocal_shell_callr  )r   r   r   r   r   )normalized_raw_itemrn  s     rw   _deserialize_tool_call_raw_itemr    s    )733""#''/IO#	'+B.ABB ^^""#'>*=>>  	'&&	'  #""#s#   
A 
A! AA!A0/A0c                6   U R                  5        Vs1 s Hi  nUR                  S5      S:X  d  M  [        UR                  S5      [        5      (       d  M@  UR                  S5      (       d  MX  [	        US   S   5      iMk     nnU(       d  gU[
        :*  $ s  snf )Nr   missinglocr   F)errorsr   r   tupler   _ALLOWED_MISSING_MESSAGE_FIELDS)r	  errormissing_fieldss      rw   !_can_construct_statusless_messager    s     ZZ\!E99V	) 	 uyy'/ 	 IIe	 	E%LO!   <<<s   B"BB.Bc                    [        U [        5      (       d  U $ U R                  S5      nUS:X  a  [        R                  " S0 [        U 5      D6$ US:X  a  [        R                  " S0 [        U 5      D6$ [        U 5      $ )Nr   r   refusalr  )r   r   r   r   model_constructr   r   )r  	part_types     rw   !_deserialize_message_content_partr    sl    eW%%		&!IM!!11@DK@@I$44CtE{CC;ry   c                H    [        S0 U D6$ ! [         a  n[        U5      (       d  e  S nAOS nAff = fU R                  S5      n[	        U[
        5      (       a"  U Vs/ s H  n[        U5      PM     Os  snf snOUn[        U 5      nXES'   [         R                  " S0 UD6$ )Nr   r  )	r   r!   r  r   r   r   r  r   r  )payloadr	  r   partnormalized_contentnormalized_payloads         rw    _deserialize_message_output_itemr  	  s    $/w// 055 6 kk)$G gt$$ >EEWT	*4	0WE 
 g$6y! 00F3EFFs   
 
2-2A5c                   SnSn[        U [        5      (       a#  U R                  S5      nU R                  S5      nO[        U [        5      (       a  U n[        U[        5      (       a'  Ub$  UR                  U5      nUb  U$ [	        SU 35      eU(       a3  Ub  UR                  U5      nUb  U$ UR                  U5      =(       d    U$ U$ )z@Resolve an agent from serialized data with an optional fallback.Nr  r  zRRun state references an agent identity that is not present in the restored graph: )r   r   r   r   r.   )
agent_datar  r{  r  r  agent_identityresolveds          rw   _resolve_agent_from_datar    s     JN*g&&#
3^^F+
	J	$	$
.#&&+=+I%)).9O` 
 	

 ))--j9H#}}Z(:N:ry   c                X    [        U [        5      (       d  U $ [        [        U 5      5      $ )zNDeserialize a tool approval raw item, preferring function calls when possible.)r   r   r  r   )r  s    rw   #_deserialize_tool_approval_raw_itemr  <  s'    )733""*40C+DEEry   )r{  r  pre_normalized_raw_itemc          
        [        U R                  S5      UUU5      nUc  gUnUcR  U R                  S5      =(       d    U R                  S5      =(       d    0 n[        U[        5      (       a  [	        U5      nU R                  S5      nU R                  S5      n[        U R                  S5      5      n	[        U R                  S5      5      n
U R                  S	5      S
L n[        U5      n[        UUUUU	U
US9$ )z4Deserialize a ToolApprovalItem from serialized data.r  Nr)  rawItemr   r  r  r  r  T)r  r)  r   r  r  r  r  )	r  r   r   r   r   rx   r'   r  r?   )	item_datar  r{  r  r  r  r  r   r  r  r  r  r)  s                rw   r  r  D  s     %g	E }0M!j1SY]]95MSQSmW-- /Mk*I]]#34N*9==+GHK:9==IZ;[\O%MM*ABdJ2=AH%'4 ry   c                &   [        U [        5      (       d2  [        [        [        -  [
        -  [        [        [        4   -  U 5      $ [        U 5      nUR                  S5      nUS:X  a  [        R                  U5      $ US:X  a  [        R                  U5      $ US:X  a  [        R                  U5      $ US;   a  U$  [        [        [        -  [
        -  [        [        [        4   -  [        R                  U5      5      $ ! [         a     gf = f)zKDeserialize a tool call output raw item; return None when validation fails.r   function_call_outputcomputer_call_outputlocal_shell_call_output>   shell_call_outputapply_patch_call_outputcustom_tool_call_outputN)r   r   r   r   r   r   r   r   r   r   rk   r  rl   rm   rn   r!   )r)  r  output_types      rw   &_deserialize_tool_call_output_raw_itemr  m  s    h((!336JJTRUWZRZ^[
 	

 x.%))&1K,,'778KLL,,'778KLL//*::;NOOaa""!336JJTRUWZRZ^[+;;<OP
 	
  s   >AD 
DDc                  [        U [        5      (       a  U O0 nUR                  S0 5      n[        U[        5      (       a  UO0 nUR                  S5      nU(       a  XQ:w  a  g UR                  S5      =(       d    SU S3nUR                  S0 5      n[        U[        5      (       a  UO0 n[        UR                  S5      [	        UR                  S5      5      S	9n	XiU4$ )
Nra  r   r  deserialized_
_guardrailr   r_  r^  )rd  rc  )r   r   r   r/   r   )
r  expected_type
entry_dictguardrail_info_rawguardrail_infoguardrail_typer  output_data_rawoutput_dataguardrail_outputs
             rw   _parse_guardrail_entryr    s     %UD11rJ#R8+56H$+O+O'UWN#''/N.9f%R=z)RD nnXr2O%/%F%F/BK.OOL10C DE :--ry   c               <   [        U [        5      (       a  U O0 nUR                  S0 5      n[        U[        5      (       a  UO0 nUR                  S5      nU(       a  XQ:w  a  g UR                  S5      =(       d    SU S3nUR                  S0 5      n[        U[        5      (       a  UO0 nUR                  S5      n	[        U	[        5      (       a%  SU	;   a  [        [        [
        -  [        -  U	5      n
O	[        SS	9n
UR                  S
5      n[        UU
S9nXl4$ )Nra  r   r  r  r  r   ri  allow)r   r_  )rd  ri  )r   r   r   r   rU   rT   rS   rV   )r  r  r  r	  r
  r  r  r  r  behavior_datari  rd  r  s                rw   _parse_tool_guardrail_entryr    s    %UD11rJ#R8+56H$+O+O'UWN#''/N.9f%R=z)RD nnXr2O%/%F%F/BKOOJ/M-&&6]+B!$::]J

 !g.//,/K2 !!ry   c                    / nU =(       d    /  HN  n[        USS9nU(       d  M  Uu  pEnUS.         SS jjn[        XtS9nUR                  [        XS95        MP     U$ )z7Rehydrate input guardrail results from serialized data.r\  r  _outputc                   U$ rT  r  )r   r  r\  r  s       rw   _input_guardrail_fnA_deserialize_input_guardrail_results.<locals>._input_guardrail_fn  	     Nry   guardrail_functionr  r`  )
r   RunContextWrapper[Any]r  rf   r\  r   r  r/   r2  r/   )r  r0   r   r1   )	results_datar  r  parsedr  r  _r  ra  s	            rw   $_deserialize_input_guardrail_resultsr"    s     02L##'WE$*! 0@	+		 	
 -	 %	 #6IU	09^_! $" ry   r}  c          
     4   / nU =(       d    /  H  n[        USS9nU(       d  M  Uu  pxn	U	R                  S5      n
U	R                  S5      n[        UUUU5      nUc  UnUS.         S	S jjn[        XS9nUR	                  [        UU
UUS95        M     U$ )
z8Rehydrate output guardrail results from serialized data.r   r  rb  r  r  c                   U$ rT  r  )r   agent_paramagent_output_paramr  s       rw   _output_guardrail_fnC_deserialize_output_guardrail_results.<locals>._output_guardrail_fn  r  ry   r  )ra  re  r  r   )
r   r  r%  rf   r&  r   r  r/   r2  r/   )r  r   r  r2   r   r3   )r  r  r{  r  r  r  r   r  r  r  re  r  resolved_agentr'  ra  s                  rw   %_deserialize_output_guardrail_resultsr*    s     13L##'XF-3*
!~~m4^^G,
1	
 !+N 0@	+	#	 !$	
 -	 %	 $7KW	!#)$'		
5 $D ry   c                    / nU =(       d    /  HI  n[        USS9nU(       d  M  Uu  pEUS.     SS jjn[        XdS9nUR                  [        XuS95        MK     U$ )z<Rehydrate tool input guardrail results from serialized data.r  r  r  c                   U$ rT  r  rv   r  s     rw   _tool_input_guardrail_fnK_deserialize_tool_input_guardrail_results.<locals>._tool_input_guardrail_fn	  	    
 Nry   r  r`  rv   r   r  rV   r2  rV   )r  rW   r   rX   )r  r  r  r   r  r  r.  ra  s           rw   )_deserialize_tool_input_guardrail_resultsr2  	  s     46L##,U,O!'
 4D		 1	 )		 .@7.
	 	4ybc! $" ry   c                    / nU =(       d    /  HI  n[        USS9nU(       d  M  Uu  pEUS.     SS jjn[        XdS9nUR                  [        XuS95        MK     U$ )z=Rehydrate tool output guardrail results from serialized data.rM  r  r  c                   U$ rT  r  r-  s     rw   _tool_output_guardrail_fnM_deserialize_tool_output_guardrail_results.<locals>._tool_output_guardrail_fn'	  r0  ry   r  r`  r1  )r  rY   r   rZ   )r  r  r  r   r  r  r5  ra  s           rw   *_deserialize_tool_output_guardrail_resultsr7  	  s     57L##,U-P!'
 4D		 1	 )		 /B8/
	 	5	cd! $" ry   c                  #    UR                  S5      nU(       d  [        S5      eU[        ;  a7  SR                  [	        [        5      5      n[        SU SU S[
         S35      e[        U 5      n[        U 5      nUS   n	U	S	   n
[        U	UUS
9nU(       d  [        SU
 S35      eUS   n[        UR                  S0 5      5      nUR                  S[        5      nU[        L a  0 nUR                  S5      n[        U[        5      (       a  UOSnUcF  UcC  [        U5      (       a3  [        U5      nU(       a  [        U5      e[        R                   " U5        [        U["        5      (       a  UnOUb
  [#        US9nOUc
  [#        SS9nOUbT  [        U[        5      (       d  [        S5      e U" [%        U5      5      n[        U["        5      (       a  UnO4[#        US9nO*[        U[        5      (       a
  [#        US9nO[        S5      eUUl        UR+                  UR                  S0 5      5        UR                  S5      nUc  Ub  [-        USS5      c  UUl        US   n[        U[0        5      (       aP  / nU HG  n[        U[        5      (       d  UR3                  U5        M+  [%        U5      nUR3                  U5        MI     OUn[5        UUUUS   UR                  S5      UR                  S5      [7        UR                  SS5      5      S9nU Ul        UUl        SSKJn  [A        5       RB                  Ul"        U" UURD                  5        US    Ul#        [I        UR                  S!/ 5      5      Ul%        [M        UR                  S"/ 5      UUS
9Ul'        UR                  S#5      nU(       a?  URP                  b2  [S        UUURP                  UUURD                  UUS$9I Sh  vN Ul*        OSUl*        S%U;   a!  [M        UR                  S%/ 5      UUS
9Ul+        OURY                  5       Ul+        UR[                  5         []        UR                  S&/ 5      5      Ul/        [a        UR                  S'/ 5      UUUS(9Ul1        [e        UR                  S)/ 5      5      Ul3        [i        UR                  S*/ 5      5      Ul5        UR                  S+5      nU(       a  UR                  S,5      S-:X  a  / nUR                  S.0 5      R                  S/UR                  S// 5      5      nU H$  n [m        U UUS09n!U!c  M  UR3                  U!5        M&     SS1K7J8n"  U"" U Vs/ s H  n[        U[r        5      (       d  M  UPM     snS29Ul:        UR                  S3S45      Ul;        UR                  S55      n#U#S6;   a  [y        [z        S7   U#5      Ul>        OSUl>        UR                  S85      n$[        U$[~        5      (       a  U$OSUl@        UR                  UR                  S90 5      5        UR                  S:5      n%[        U%[        5      (       a  [        R                  " U%5      UlD        OSUlD        UR                  S;5      n&[        U&[        5      (       a  [%        U&5      UlE        U$ SUlE        U$ ! [&         a  n[        S5      UeSnAff = f GNs  snf 7f)<a  Shared helper to rebuild RunState from JSON payload.

Context restoration follows this precedence order:

1. ``context_override`` when supplied.
2. ``context_deserializer`` applied to serialized mapping data.
3. Direct mapping restore for contexts that were serialized as plain mappings.

When the snapshot metadata indicates that the original context type could not round-trip
safely, this function warns or raises (in ``strict_context`` mode) rather than silently
claiming that the rebuilt mapping is equivalent to the original object.
rC  z#Run state is missing schema versionrj   zRun state schema version z+ is not supported. Supported versions are: z'. New snapshots are written as version .rD  r  r}  Agent z not found in agent mapr   r   rB  N)r   zKSerialized run state context must be a mapping to use context_deserializer.z>Context deserializer failed while rebuilding RunState context.zCSerialized run state context must be a mapping. Please provide one.rA  r  r   r   r   r   r   F)r   r   r   r   r   r   r   r#   )set_agent_tool_state_scoper  rE  r5  rT  rz  rP  rH  rI  r  rJ  rL  rQ  r   r{  rv   r   )r  r{  r   )r   rU  r   rO  >   omitpreserve)r=  r<  rN  rF  rW  rX  )Fr   r.   SUPPORTED_SCHEMA_VERSIONSjoinsortedr   _build_agent_identity_map_build_agent_mapr  r]   _MISSING_CONTEXT_SENTINELr   r   r  r  rG   r  rH   r   r   r   _rebuild_approvalsr  r  r   r   r{   r   r   r   r   r;  r   hexr   r~   _deserialize_model_responsesr   r  r   r   r  r   r   r<  r"  r"  r   r*  r   r2  r   r7  r   r  r   r`   r?   r   r   r   r   r   r   r   r  r\   r  r   r   )'r  r  r  r  r   schema_versionsupported_versionsr{  r  current_agent_datacurrent_agent_namerD  context_datar   serialized_contextcontext_meta_rawrB  warning_messager   rebuilt_contextr	  serialized_tool_inputoriginal_input_rawnormalized_original_inputr   	item_dictrW  r;  last_processed_response_datacurrent_step_datar   rx  r  r   r`   serialized_policyserialized_prompt_cache_key
trace_datasandbox_datas'                                          rw   r  r  5	  s    &  ^^$45N=>>66!YYv.G'HI''7 8''9&: ;44J3K1N
 	
 3=A /I#O4+F3,-M
 &!3 44KLMMi(Ll..w;<E*..y:ST66#''7'12BG'L'L#RVL
 	  (/==7EO,,'"$566"		%#,<=		##D1		),g66] 	248J3KLO
 o'899%G'@G	&	0	0#,>?]^^GM|//R@A(,,\: !-G\4082#$45$d++$&!&DdG,,)006T
I%,,Y7 ' %7!0$[)"'89'^^,BC"&z~~6QSX'Y"ZE *E*E<',w{{E$w(H(HI$^4E9*..IZ\^:_`E/("--E $.>>2K#L #(B/N(NN155!5)	0
 	*
& *.&*$1NN?B/1 
  %JJL	::<%I0"5&E" 'L126-$	'E# +T5r:+E' ,V6;,E( #~6.226:>VV02.2262>BB.22?BG
 ,I;##5M
 ($$]3 , 	A2,9`MDZN^=_4M`
 0:~~+Q0E, #'AB00*.w7I/JL]*^'*.'",..1M"N'12Ms'S'S#Y] 
% 
''
7I2(NO(J*g&&'11*=!>>),L+5lG+L+LT,'ENL SWENL]  	P	x	*
t asW   F/[2Z9 H+[.[/E[![$[[D2[9
[[[[[c              #    #    [        U /5      n[        5       nU(       GaM  UR                  5       n[        U5      nXB;   a  M*  UR	                  U5        Uv   UR
                   GH  nSnSn[        U[        5      (       a  [        USS5      =(       d    [        USS5      n[        U[        5      (       a  Un[        USS5      n	[        U	5      (       a  U	" 5       OSnUc(  [        USS5      n
U
b  U
n[        R                  " S5        Uc&  [        USS5      n[        U[        5      (       a  UOSnUb  [        US5      (       d!  U(       a  [        R                  " SU5        GM  OQ[        USS5      n
U
b  U
n[        R                  " S	5        OUn[        USS5      n[        U[        5      (       a  UOSnUc  GM\  U(       d  GMf  UR                  [        [         ["           U5      5        GM     [        US
S5      nU(       aZ  U HT  n[        USS5      (       d  M  [        USS5      n[        USS5      nU(       d  M:  U(       d  MC  UR                  U5        MV     U(       a  GML  gg7f)zFYield agents reachable from the starting agent in breadth-first order.Nr  r  
_agent_refr  zyUsing legacy handoff `.agent` fallback while building agent map. This compatibility path is not recommended for new code.r  z?Skipping unresolved handoff target while building agent map: %szFUsing legacy non-`Handoff` `.agent` fallback while building agent map.r  _is_agent_toolF_agent_instance)r   r3  popleftr*  r4  r  r   r4   r  r   rM  rG   debugr  r   r   r-   r   )r  queueseen_agent_idscurrent
current_idhandoff_itemhandoff_agenthandoff_agent_namecandidate_namehandoff_reflegacy_agentr  r  
tool_agenttool_agent_names                  rw   _iter_agent_graphrl  
  sB    $m_5E"uN
--/[
':&#,,L(,M-1,00 ")|T!J "g &$O nc22)7&%lL$G19+1F1FD ( $+<$#GL#/(4W &-%,]FD%IN;EnVY;Z;Z`d& (z0R0R)].  1S  '|WdC+$0MLL` %1M!(!E7A.RU7V7V^\`"(-?-?T%*m<=e -j $/t%5u==$T+<dC
")*fd"C://LL,  %s%   F9I/ I/
A7I/I/I/-I/c                `    U nSnX!;   a  US-  nU  SU 3nX!;   a  M  UR                  U5        U$ )zOReturn a deterministic identity key without colliding with literal agent names.r#   #)r4  )r  used_identities	candidate
next_indexs       rw   _allocate_unique_agent_identityrr  `
  sI    IJ

&a
!l!J<0	 
& 	"ry   c                \    [        U 5      R                   S[        U 5      R                   3$ )Nr9  )r   r  r  r  s    rw   _identity_type_namert  k
  s)    5k$$%QtE{'?'?&@AAry   c                    [        U S[        U 5      R                  5      n[        U S[        U 5      R                  5      nU SU 3$ )Nr  r  r9  )r  r   r  r  )r  r  r  s      rw   _callable_identity_namerv  o
  sC    UL$u+*@*@AFund5k.F.FGHXQxj!!ry   c           	     ,   U b*  [        U [        [        -  [        -  [        -  5      (       a  U $ [        U [
        [        -  5      (       a  S[        U 5      S.$ [        U 5      (       a  S[        U 5      0$ [        R                  " U 5      (       a9  [        U 5      [        [        R                  " [        [         U 5      5      5      S.$ [#        U S5      (       a'   U R%                  SS9n[        U 5      [        U5      S.$ [        U [(        5      (       aA  [+        U R-                  5       S	 S
9 VVs0 s H  u  p#[        U5      [        U5      _M     snn$ [        U [.        5      (       a?  [        U [        [
        -  [        -  5      (       d  U  Vs/ s H  n[        U5      PM     sn$ [1        U SS 5      n[        U[        5      (       a  [        U 5      US.$ S[        U 5      0$ ! [&         a    U R%                  5       n GNf = fs  snnf s  snf )Nbytesr   lengthrM  r	   r  r   Tr   modelr  c                    [        U S   5      $ Nr   r   pairs    rw   r  +_normalize_identity_value.<locals>.<lambda>
  s    CQLry   r  r  r]  r   )r   r   r}   floatr   rx  	bytearraylenrM  rv  r  r  rt  _normalize_identity_valuer   r   r   r  r   r  r   r@  r   r   r  )r  dumpedr  r   
value_names        rw   r  r  u
  s   }
5#)e*;d*BCC%*++3u:663E:;;&&,U3.{/A/A$sEBR/ST
 	
 ul##	(%%D%9F )/.v6
 	
 %!! $EKKM7PQ
Q	 H/55Q
 	
 %"":eS5[9=T+U+U<ABED)$/EBB-J*c""+E2JGG'.//#  	(%%'F	(

 Cs   G* 4!HH*HHc                @    [         R                  " [        U 5      SSS9$ )NT),r  )r  
separators)r  r  r  r  s    rw   _stable_identity_textr  
  s!    ::!%( ry   c                0   [        U 5      [        U SS 5      S.n[        U 5      nUb  X!S'   [        U 5      nUb  X1S'   [	        U S5      (       a  [        U R                  5      US'   [        U SS5      (       a  [        U SS 5      n[        USS 5      US	'   U$ )
Nr  r]  r  r  environmentr\  Fr]  agent_tool_target)rt  r  r*   r+   r  r  r  )r  	signaturer  r  nested_agents        rw   _tool_identity_signaturer  
  s    #D)fd+!I ,D1I!*+5d;N!&4"#t]###<T=M=M#N	- t%u--t%6=)0vt)L	%&ry   c           	         [        U [        [        R                  [        R                  [        R
                  [        R                  [        R                  /[        Q75      $ rT  )	r   rJ   asyncioEventLock	Semaphore	Condition	threading_THREADING_LOCK_TYPESr  s    rw   !_is_capability_runtime_only_valuer  
  sK    MMLLOO	
 #	
 ry   seenc               
   Uc
  [        5       nU b*  [        U [        [        -  [        -  [
        -  5      (       a  U $ [        U [        5      (       a  U R                  5       $ [        U [        [        -  5      (       a  S[        U 5      S.$ [        U 5      (       a  S[        U 5      0$ [        U 5      (       a  S[        U 5      0$ [        U [        [         -  ["        -  [$        -  [&        -  [(        -  5      (       a  [+        U 5      $ [-        U 5      nX!;   a  S[        U 5      0$ [.        R0                  " U 5      (       a  UR3                  U5         [.        R4                  " U 5       Vs0 s H"  o3R6                  [9        XR6                  5      _M$     nn[;        U S5      (       aC  [=        U 5      R?                  5        H&  u  pVURA                  S5      (       d  XT;   a  M"  XdU'   M(     [        U 5      [C        UR?                  5       5       VVs0 s H  u  pVU[E        UUS9_M     snnS	.URG                  U5        $ [        U [H        5      (       a  UR3                  U5         0 nU RJ                  RL                  R?                  5        HA  u  pWURN                  (       d  URA                  S5      (       d  US
:X  a  M4  [9        X5      XE'   MC     [        U 5      [C        UR?                  5       5       VVs0 s H  u  pVU[E        UUS9_M     snnS.URG                  U5        $ [;        U S5      (       aI  UR3                  U5          U RQ                  SSS9n[        U 5      [E        XS9S.URG                  U5        $ [        U [T        5      (       ab  UR3                  U5         [C        U R?                  5       S S9 V	Vs0 s H  u  p[        U	5      [E        XaS9_M     snn	URG                  U5        $ [        U [         [V        -  5      (       aJ  UR3                  U5         U  Vs/ s H  n[E        XaS9PM     n
n[C        U
[X        S9URG                  U5        $ [        U [Z        5      (       a`  [        U [        [        -  [        -  5      (       d=  UR3                  U5         U  Vs/ s H  n[E        XaS9PM     snURG                  U5        $ [;        U S5      (       a  UR3                  U5         [        U 5      [C        [=        U 5      R?                  5       5       VVs0 s H'  u  pVURA                  S5      (       a  M  U[E        XaS9_M)     snnS.URG                  U5        $ [9        U SS 5      n[        U[        5      (       a  [        U 5      US.$ S[        U 5      0$ s  snf s  snnf ! URG                  U5        f = fs  snnf ! URG                  U5        f = f! [R         a    U RQ                  SS9n GNf = f! URG                  U5        f = fs  snn	f ! URG                  U5        f = fs  snf ! URG                  U5        f = fs  snf ! URG                  U5        f = fs  snnf ! URG                  U5        f = f)Nrx  ry  rM  runtime_only	recursive__dict__r!  r  r{  session)
capabilityr  r   r  T)mode
round_trip)r  r|  c                    [        U S   5      $ r  r  r  s    rw   r  6_normalize_capability_identity_value.<locals>.<lambda>!  s    DQRGry   r  )objectr  r  r]  r   ).r3  r   r   r}   r  r   r   as_posixrx  r  r  rM  rv  r  rt  rK   rL   rN   rO   rP   rQ   r  r*  r  r  r4  fieldsr  r  r  varsr   
startswithr@  $_normalize_capability_identity_valueremoverI   r  model_fieldsexcluder   r  r   	frozensetr  r   )r  r  	object_idr
   merged_fieldsr  r   
field_infor  r  r   r  s               rw   r  r  
  s   
 |u}
5#)e*;d*BCC%~~%*++3u:663E:;;(// 3E :;;%4}D~UXaa  (..5	I0788&&	#DODVDVW\D]D]5

GE::66D]   uj))"&u+"3"3"5JDs++t/D *.$' #6
 17 '-]-@-@-B&C
 'D
	 >!  'D	 KK	"%$$	#M$)OO$@$@$F$F$H %%)=)=AR&-e&:# %I
 2%8 '-]-@-@-B&C
 'D
	 >!  'D	 KK	"ul##
	#7))v$)G -U3=fP
 KK	"%!!	# "(;T!U!UIC C>tOO!U
 KK	"%y))	#RW RW$4TERW    *0EFKK	"%"":eS5[9=T+U+U	#V[\V[d8IV[\KK	"uj!!
	#-e4 '-T%[->->-@&A&A
??3/ PD>tOO&A KK	"-J*c""+E2JGG'.//I KK	" KK	"  7))v)67 KK	"

 KK	"
 
 KK	"
 ]KK	" KK	"s   V/ +)V$A<V/ V)'V/ %BW 6WW W! X %X X X #X8 'X3:X8 %Y )Y<Y 3/Y/ "Y)Y)Y/ $V/ /WW W!W>:X =W>>X XX X03X8 8YY Y&)Y/ /Zc                0    [        U 5      [        U 5      S.$ )N)r   r  )rt  r  )r  s    rw   _capability_identity_signaturer  K  s    #J/5jA ry   c           
     d   [        U [        5      (       a  [        U SS 5      n[        U[        5      (       d  [        U SS 5      n[        U SS 5      n[	        U 5      U[        U[        5      (       a  UOS [        [        U SS 5      5      [        U SS 5      S.$ [	        U 5      [        U SS 5      S.$ )Nr   r  r  input_filternest_handoff_history)r   r   r  r  r  )r   r  )r   r4   r  r   rt  r  )rd  r   r  s      rw   _handoff_identity_signaturer  R  s    ,((L+t<	)S))fd;I\<>
'5"(2:s(C(C*5glN\`6ab$+L:PRV$W
 	
 $L1lFD9 ry   c           
        0 S[        U 5      _S[        U SS 5      _S[        [        U SS 5      5      _S[        [        U SS 5      5      _S[        [        U SS 5      5      _S[        [        U SS 5      5      _S[        [        U SS 5      5      _S[        [        U SS 5      5      _S	[	        S
 [        U S	/ 5       5       5      _S[	        S [        U S/ 5       5       5      _S[        [        U SS 5      5      _S[        [        U SS 5      5      _S[        U SS 5      _S[	        S [        U S/ 5       5       5      _S[	        S [        U S/ 5       5       5      _S[	        S [        U S/ 5       5       5      _n[        U SS 5      nUb  [        U5      US'   [        U SS 5      nUb  [        U5      US'   [        U SS 5      n[        U[        5      (       a  [	        S U 5       5      US'   [        U5      $ )N
agent_typehandoff_descriptioninstructionspromptr}  model_settings
mcp_confighooksinput_guardrailsc              3  J   #    U  H  n[        [        U5      5      v   M     g 7frT  r  r  .0ra  s     rw   	<genexpr>,_agent_identity_signature.<locals>.<genexpr>p  s%      #
C	 ""Fy"QRRC   !#output_guardrailsc              3  J   #    U  H  n[        [        U5      5      v   M     g 7frT  r  r  s     rw   r  r  t  s%      $
D	 ""Fy"QRRDr  r  tool_use_behaviorreset_tool_choicer  c              3  J   #    U  H  n[        [        U5      5      v   M     g 7frT  )r  r  )r  r  s     rw   r  r  }  s%      
3 "":4"@AA3r  r  c              3  J   #    U  H  n[        [        U5      5      v   M     g 7frT  )r  r  )r  rd  s     rw   r  r    s%      
 > ""=l"KLL >r  mcp_serversc              3  8   #    U  H  n[        U5      v   M     g 7frT  )r  )r  servers     rw   r  r    s      
8Yf!&))8Ys   default_manifestbase_instructionscapabilitiesc              3  J   #    U  H  n[        [        U5      5      v   M     g 7frT  )r  r  )r  r  s     rw   r  r    s%      +
*
 ""@"LMM*r  )rt  r  r  r  r@  r   r   r  )r  r  r  r  r  s        rw   _agent_identity_signaturer  f  s~   !!)%0!!wu.CTJ!! 	1'%QU2VW!! 	+GE8T,JK	!!
 	*75'4+HI!! 	3GECSUY4Z[!! 	:75,X\;]^!! 	5geWd6ST!! 	F #
$U,>C#
 
!! 	V $
$U,?D$
 
!!" 	0t1TU#!!$ 	AE.5
%!!* 	WU,?F+!!, 	 
w3
 
-!!4 	F 
 'z2 >
 
5!!< 	v 
8?}VX8Y
 
=!!IF u&8$?#(LM](^	$%':DA$)BCT)U	%&5.$7L,))$* +
*+
 %
	.!
 !++ry   c               *    XL a  SOS[        U 5      U4$ )Nr   r#   )r  )r  
root_agentoriginal_indexs      rw   _agent_identity_sort_keyr    s#      a!%( ry   c                  ^ ^ [        [        T 5      5      n[        U5       VVs0 s H  u  p#[        U5      U_M     snnmU Vs1 s H  o3R                  iM     nn0 nU H.  nUR                  UR                  / 5      R                  U5        M0     0 n[        5       n[        5       nU H  nUR                  n	X;   a  M  UR                  U	5        XY   n
[        U
U U4S jS9nUS   nUR                  U	5        XU	'   SnUSS  HH  nU	 SU 3nX;   d  X;   a  US-  nU	 SU 3nX;   a  M  X;   a  M  UR                  U5        XU'   US-  nMJ     M     U$ s  snnf s  snf )zABuild a stable identity map that preserves duplicate agent names.c                2   > [        U TT[        U 5         S9$ )N)r  r  )r  r*  )rp  r  original_indicess    rw   r  +_build_agent_identity_map.<locals>.<lambda>  s    ":(/9>#ry   r  r   r  r#   Nrn  )
r   rl  	enumerater*  r  
setdefaultr   r3  r4  r@  )r  ordered_agentsindexr  literal_namesagents_by_namer{  ro  processed_namesr  groupsorted_group
base_agentrq  duplicate_agentrp  r  s   `               @rw   rA  rA    s   +M:;N=F~=VW=V\U5	5(=VW-;<^EZZ^M<24N!!%**b188?   13 #O #OZZ
(J'*
 "!_
J')3:&
+AB/O%,a
|4I.)2La
)l!J<8	 .)2L 	*,;y)!OJ  0+  < Q X<s   EEc                ~    [        U 5      R                  5        VVs0 s H  u  p[        U5      U_M     snn$ s  snnf )z9Build stable identity keys for the reachable agent graph.)rA  r   r*  )r  r  r  s      rw   r  r    s@     4M]3[3a3a3c3c5	83c  s   9c                f    0 n[        U 5       H  nUR                  UR                  U5        M!     U$ )zBuild a map of agent names to agents by traversing handoffs.

Args:
    initial_agent: The starting agent.

Returns:
    Dictionary mapping agent names to agent instances.
)rl  r  r  )r  r  r  s      rw   rB  rB    s4     (*I"=1UZZ/ 2 ry   c           
     j   / nU  H  n[        UR                  S0 5      5      nUS    Vs/ s H:  n[        U[        5      (       a   UR                  S5      S:X  a  [	        U5      OUPM<     nnUR                  S5      nUR                  S5      nUR                  [        UUUUS95        M     U$ s  snf )zDeserialize model responses from JSON data.

Args:
    responses_data: List of serialized model response dictionaries.

Returns:
    List of ModelResponse instances.
r   r   r   messager   r   r   )r]   r   r   r   r  r   r<   )responses_datarf  	resp_datar   r   r   r   r   s           rw   rF  rF    s     F#	!)--"<= "(+	
 , $((TXXf-=-J -T2 ,	 	 
  mmM2]]<0
'%		
 $, M'
s   AB0c               
	  ^^ / n      S%UU4S jjnU  GH  nUR                  S5      nU(       d  [        R                  " S5        M4  U" XV5      u  pxU(       d=  U(       a  [        R                  " SU S35        O[        R                  " SU 35        M  US   n	[        U	[        5      (       a  [        U	5      OU	n
 US:X  a%  [        U
5      nUR                  [        X{S	95        M  US
:X  a&  [        U
5      nUR                  [        X|S	95        GM  US:X  a&  [        U
5      nUR                  [        X}S	95        GM-  US:X  af  [        U
5      nUR                  S5      nUR                  S5      n[        UR                  S5      5      nUR                  [        UUUUUS95        GM  US:X  aW  [!        U
5      nUc  GM  UR                  [#        UUUR                  SS5      [        UR                  S5      5      S95        GM  US:X  a'  [%        S&0 U
D6nUR                  ['        UUS	95        GM#  US:X  a'  [)        S&0 U
D6nUR                  [+        UUS	95        GMP  US:X  a  [-        UR                  S5      TT5      n[-        UR                  S5      TT5      nU(       a  U(       d=  UR                  S5      nUR                  S5      n[        R                  " SUU5        GM   [.        R1                  U
5      nUR                  [5        UUUUS95        GM  US:X  a2   [.        R1                  U
5      nUR                  [7        UUS	95        GME  US:X  a'  [9        S&0 U
D6nUR                  [;        UUS	95        GMr  US:X  a'  [=        S&0 U
D6nUR                  [?        UUS	95        GM  US :X  a1  [@        R1                  U
5      nUR                  [C        UUS	95        GM  US!:X  a'  [E        UTTUU
S"9nUb  UR                  U5        GM   GM  GM     U$ ! [2         a    U
n GN*f = f! [2         a    U
n GNf = f! [F         a    e [H         a(  n[        R                  " S#U S$U 35         SnAGMg  SnAff = f)'zDeserialize run items from JSON data.

Args:
    items_data: List of serialized run item dictionaries.
    agent_map: Map of agent names to agent instances.

Returns:
    List of RunItem instances.
c                P  > SnS/nUS:X  a  UR                  SS/5        U H~  nU R                  U5      n[        U[        5      (       a  UR                  S5      =(       d    UnO[        U[        5      (       a  Un[        UTT5      nU(       d  Mq  XfR                  4s  $    SU4$ )z#Resolve agent from serialized data.Nr  handoff_output_itemr  r  r  )extendr   r   r   r   r  r  )	r  r.  rg  r  agent_field	raw_agentagent_candidater{  r  s	          rw   _resolve_agent_info/_deserialize_items.<locals>._resolve_agent_info$  s     &*--MM>>:;!K!k2I)W--!*v!6!H.Is++!*6"O
 &(<(<<< " ^##ry   r   z!Item missing type field, skippingr:  z not found, skipping itemz$Item missing agent field, skipping: r)  message_output_item)r  r)  tool_search_call_itemtool_search_output_itemr  r  r  r  )r  r)  r  r  r  tool_call_output_itemNr   r  )r  r)  r   r  reasoning_itemhandoff_call_itemr  r  r  zNSkipping handoff_output_item: could not resolve agents (source=%s, target=%s).)r  r)  r  r  compaction_itemmcp_list_tools_itemmcp_approval_request_itemmcp_approval_response_itemr-  )r  r{  r  r  z#Failed to deserialize item of type z: )r  r  r.  r   r2  z$tuple[Agent[Any] | None, str | None]r  )%r   rG   r  r   r   r   r  r   r;   rE   rB   rF   rC   r  rx   r@   r  rA   r   r=   r   r6   r  rp   r  r!   r7   r5   r   r:   r   r8   ro   r9   r  r.   r   ) 
items_datar  r{  rf  r  r  r.  r  r  r  r  raw_item_msgraw_item_tool_search_callraw_item_tool_search_outputraw_item_toolr  r  r  raw_item_outputraw_item_reasonraw_item_handoffr  r  source_nametarget_nameraw_item_handoff_outputraw_item_compactionraw_item_mcp_listraw_item_mcp_reqraw_item_mcp_responser   r  s     ``                             rw   r  r    s     F$$$14$	-$ $4  	MM&)	NN>?/	E
|3LMN!Ei[QR!*-#-mW#E#ED= 	N	11?@ST/eST55,LM`,a)0uab77.P'/+ (u[ .. !@@S T'mmM:!g.6y}}]7ST #!.$/#$/ 55 #II\"]"*&#!0(}}Xr:$<Y]]==Y$Z	 .."7"N:M"Nm%/RS11#;#R>Q#R oEDTUV337MM.1& 
  8MM.1&  $<"+--"?K"+--"?KNN2##	 
B.E.U.U+/+ %#!8%1%1	 //>*A*Q*Q++'
 n5CVWX33$0$G3F$G!.UEVWX99#5#L8K#L 45K[\]::(F(V(V')% 5ELabc22 ?''9#(,?! !,MM-0 - 3e  F Mo ' B /B+	B& ' >*='><  	 	NN@2aSQR	s    )Q+)Q)QA)Q/QAQ*Q9*Q&BQ0P Q#Q*P3?Q*Q*Q54Q,'Q P0,Q/P00Q3Q?QQQRQ==Rc                \    [        U [        5      (       a  U $ [        R                  " U 5      $ )zXReturn a deep copy of the original input so later mutations don't leak into saved state.)r   r   rZ  r[  )r   s    rw   r   r     s$    .#&&==((ry   )rv   r   r2  zToolOrigin | NonerT  )r  r   r  r   rd   r   r2  r   )r  r   r2  r   )r  r   r2  r   )
r  r   r   r   r   r   r   r   r2  r  )rB  Mapping[str, Any] | Noner2  r   )rB  r  r2  r   )rv   z dict[str, Any] | list[Any] | Anyr  zMapping[str, str]r2  r   )r)  r   r2  r   )r  rf   r  r  r2  r  )r  r   r2  r   )r   r   r2  r   )r  r   r  r   r  r   r2  r  )r  Sequence[Any]r  r   r  r   r  r   r  r   r2  r  )r  r  r2  r  )r$  r  r2  r  )r"  r   r2  r  )rw  r?   rp  r   r  r  r2  r  )ru  ra   r2  zdict[str, list[dict[str, Any]]])rk  zRunState[Any, Any]rl  zSequence[dict[str, Any]]rm  r  rn  r   r   r  r   r   r?  r   r2  r  )rf  z6Sequence[InputGuardrailResult | OutputGuardrailResult]r  r  r2  r  )rf  z>Sequence[ToolInputGuardrailResult | ToolOutputGuardrailResult]rK  $Literal['tool_input', 'tool_output']r2  r  )rE  r  r2  r   )r  r  rn  z	type[Any]r2  zdict[NamedToolLookupKey, Any])rD  rf   r2  z#dict[str, Handoff[Any, Agent[Any]]])rD  rf   rl  r  rm  r  rn  r   r  r  r   r   r2  r  )r  r  rD  rf   r   r  r  dict[str, Agent[Any]]r{  Mapping[str, Agent[Any]] | Nonern  r   r  r  r   r   r2  ra   )r  r  r2  r   )r	  r!   r2  r   )r  r  r2  r  )r  r  r2  r   )NN)
r  r   r  Mapping[str, Agent[Any]]r{  r  r  Agent[Any] | Noner2  r  )r  r   r2  r   )r  r  r  r  r{  r  r  r  r  z
Any | Noner2  zToolApprovalItem | None)r)  r  r2  zVFunctionCallOutput | ComputerCallOutput | LocalShellCallOutput | dict[str, Any] | None)r  r   r  zLiteral['input', 'output']r2  z:tuple[str, GuardrailFunctionOutput, dict[str, Any]] | None)r  r   r  r  r2  z.tuple[str, ToolGuardrailFunctionOutput] | None)r  r  r2  r   )
r  r  r  r  r{  r  r  rf   r2  r   )r  r  r2  r   )r  r  r2  r   )NNFr  )r  rf   r2  zIterator[Agent[Any]])r  r   ro  zset[str]r2  r   )r  r   r2  r  )r  r   r2  r   )r  r   r  zset[int] | Noner2  r   )r  r   r2  r  )rd  zAgent[Any] | Handoff[Any, Any]r2  r  )r  rf   r2  r   )r  rf   r  rf   r  r}   r2  ztuple[int, str, int])r  rf   r2  r  )r  rf   r2  zdict[int, str])r  r  r2  r   )r  r  r  r  r{  r  r2  r   )r   r   r2  r   )r  
__future__r   r  rZ  r  r  r  collectionsr   collections.abcr   r   r   r   r	   r
   pathlibr   typingr   r   r   r   r   uuidr   openai.types.responsesr   r   r   r   r   r   r   +openai.types.responses.response_input_paramr   r   r   r   +openai.types.responses.response_output_itemr   r   r   r  r    r!   typing_extensionsr"   _tool_identityr$   r%   r&   r'   r(   r)   r*   r+   r,   r  r-   
exceptionsr.   ra  r/   r0   r1   r2   r3   r  r4   r   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   run_contextrH   sandbox.capabilities.capabilityrI   $sandbox.session.base_sandbox_sessionrJ   r  rK   rL   rM   rN   rO   rP   rQ   rR   tool_guardrailsrS   rT   rU   rV   rW   rX   rY   rZ   tracing.tracesr[   r\   r   r]   r^   
util._jsonr_   r   r`   ra   rb   re   r   ContextOverrideContextSerializerContextDeserializerr   ri   r  r  r>  AssertionErrorstrip!_missing_schema_version_summariesr?  rk   rl   rm   rn   ro   rp   rq   r  rC  r  rx   r{   r,  r  r  r   r  r  r  r   rY  r  r  r  r  r  r(  r#  rt  rr  rs  rR  r\  r]  r_  rp  rs  ry  r  r  r  r  r  r  r  r  r  r  r  r"  r*  r2  r7  r  rl  rr  rt  rv  r  r  r  r   r  RLockr  r  r  r  r  r  r  rA  r  rB  rF  r  r   )versionsummarys   00rw   <module>r9     s   \ "       A A (  = =     
 2 %
 
 
  !      (  * 7 D	 	 		 	 	 . 5 +F-
 :s+	|	D#s(#&7&<<cUGCH$556 S 12C78    KWXYH_O	$ K], .  &&>? !99
X 
 %=$B$B$D%$D GMMOG$D% ! %
	"ii(IJK	M 
 =HHZ<[ 9 [<GHZ<[ 9 [ALMaAb > b "%77:NNO   " P DOObCc  @ c;FGY;Z 8 Z9D^9T 6 T"H "+XJ"7 +
 O
wx'( O
 O
n'

	"   	
  ,;*
*7H. ;?

7
 
0 !&"'	
   	
 : !&"'  	
    4!H ;?	"   8	
 >N)N$Nl  37 $)>$> /> !	>
 > 1> > "> 
>B	 	 ;?C  8 	8K 5 	2%."<(  7; 1^1^ $1^ !	1^
 1^ 51^ 1^ 
1^t ;?7; u+uu $u %	u 8u u 5u u up	#,
=	G* ;?(,	' 8 &	
 BF ;?(,*.& & (& 8	&
 && (& &R[>.."<.?.&"""F"3":&: ;?	+&+ %+ 8	+
 + !+\&#2&$8 047; [[[ -[ 5	[
 [ [|J-ZB" 0F& inn./ioo6G1HI $ !C0C0 C0 		C0L(3,l

 
 	

 
+\ !P ;?	O$O$O 8	O
 Od)Sb%s   T.T