ó
    °"³j«@  ã                  óÂ  • % 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JrJ	r	  SSK
JrJrJrJr  SSKrSSKrSSKJr  SSKJr  SS	KJr  S
r\" \15      r Sq\" S5      r\" S5      r " S S\\\4   \\   5      rS S jrS!S jr S"S jr!Sq"S\#S'   S#S jr$S$S jr%S%S jr&S&S jr'S'S jr(S(S jr)S)S jr*S*S jr+            S+S jr,S,S jr-        S-S jr.S.S jr/g)/a  Registry-backed serialization for extensible event families.

Event classes like [`CustomEvent`][pydantic_ai.messages.CustomEvent] are open families: applications
and third-party packages define typed subclasses that must round-trip through the closed
`AgentStreamEvent` discriminated union. Like the native tools union (see
`AbstractNativeTool.__get_pydantic_core_schema__`), the family's inner tagged union is rebuilt from a
registry at schema-generation time, so registered subclasses validate to their own class. Unlike
native tools, an unregistered tag doesn't fail validation: it degrades to an "unknown" envelope
carrying the raw payload in `data`, which re-flattens on serialization so a downstream consumer that
does have the defining module imported recovers the typed event.
é    )ÚannotationsN)ÚCallableÚMapping)ÚAnyÚGenericÚTypeVarÚoverloadé   )Ú_utils)Úis_str_dict)Ú	UserErrorÚ__unknown__Ú_EventClassTÚ	_DefaultTc                  óx   ^ • \ rS rSrSrS	U 4S jjrS
U 4S jjr\SS j5       r\SS j5       rSU 4S jjrSr	U =r
$ )ÚEventRegistryé(   aæ  One event family's tag-to-class registry, versioned so schema caches can detect staleness.

`event_family_schema` snapshots the registry it builds from, so a schema built before a class
registered keeps degrading that class's events to the unknown envelope. That's correct for a
schema built once and thrown away, but a consumer that memoizes adapters for the life of a
process (e.g. Temporal's payload converter) would keep serving the stale one, making decoding
depend on import order. Such consumers key their memo on `event_registry_version()`, which the
mutations below bump, so a later registration rebuilds the adapter instead.

A registry is only ever mutated by item assignment (registration, from `__init_subclass__`),
`del`, and `pop` (test cleanup), which is why those are the ones overridden. `dict`'s bulk
mutators would change the registry without bumping the version, leaving a memoizing consumer on
a stale adapter; register through class definition rather than reaching for them.
c                ó8   >• [         TU ]  X5        [        5         g ©N)ÚsuperÚ__setitem__Ú_bump_registry_version)ÚselfÚtagÚ	event_clsÚ	__class__s      €ÚX/home/mande/repo/quber/.venv/lib/python3.13/site-packages/pydantic_ai/_event_registry.pyr   ÚEventRegistry.__setitem__8   s   ø€ Ü‰Ñ˜CÔ+ÜÕ ó    c                ó8   >• [         TU ]  U5        [        5         g r   )r   Ú__delitem__r   )r   r   r   s     €r   r!   ÚEventRegistry.__delitem__<   s   ø€ Ü‰Ñ˜CÔ ÜÕ r   c               ó   • g r   © )r   r   s     r   ÚpopÚEventRegistry.pop@   s   € Ø03r   c               ó   • g r   r$   )r   r   Údefaults      r   r%   r&   C   s   € Ø_br   c               ó>   >• [         TU ]  " U/UQ76 n[        5         U$ r   )r   r%   r   )r   r   r(   Úpoppedr   s       €r   r%   r&   F   s!   ø€ Ü‘’˜SÐ+ 7Ò+ˆÜÔ Øˆr   r$   )r   Ústrr   r   ÚreturnÚNone)r   r+   r,   r-   )r   r+   r,   r   )r   r+   r(   ú_EventClassT | _DefaultTr,   r.   )r   r+   r(   r   r,   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r!   r	   r%   Ú__static_attributes__Ú__classcell__)r   s   @r   r   r   (   s:   ø† ñ÷!÷!ð Û3ó Ø3àÛbó Øb÷õ r   r   c                 ó   • [         S-  q g )Nr
   ©Ú_registry_versionr$   r   r   r   r   L   s   € ä˜ÑÑr   c                 ó   • [         $ )zÍA counter bumped whenever any [`EventRegistry`][] changes.

Cache keys that include this are invalidated by a registration, so an adapter built before an
event class was imported isn't reused after it is.
r7   r$   r   r   Úevent_registry_versionr:   Q   s
   € ô Ðr   c                ót   • U R                   UR                   :H  =(       a    U R                  UR                  :H  $ )a  Whether `cls` is the same class as `existing` being defined again.

A re-run notebook cell, `importlib.reload`, a re-executed docs example, or the class recreation
`@dataclass(slots=True)` performs replaces its registration; only genuinely distinct classes
conflict.
)r0   r1   )ÚexistingÚclss     r   Úis_redefinitionr>   Z   s/   € ð ×Ñ #§.¡.Ñ0×^°X×5JÑ5JÈc×N^ÑN^Ñ5^Ð^r   zCallable[[], bool] | NoneÚ_replay_isolation_guardc                ó   • U q g)u‚  Declare when class definitions are happening in an isolated re-execution of the app's modules.

A durable execution runtime may re-execute application modules in an isolated interpreter view
while sharing `pydantic_ai` itself with the host process â€” Temporal's workflow sandbox does
exactly this. The re-executed copy of an event class is a redefinition of the host's, so left
alone it would take over the registry, and the host would then decode payloads into a class its
own `isinstance` checks (including `@on_event(MyEvent)` filtering) don't recognize.

While `guard()` is true, redefinitions keep the class already registered as the family's
canonical one. Instances of the re-executed copy still serialize and validate normally: the
family schema canonicalizes them (see `event_family_schema`), so application code holds one
event class regardless of which side of the boundary it runs on.
N©r?   )Úguards    r   Úset_replay_isolation_guardrC   g   s
   € ð $Ñr   c                 ó4   • [         SL=(       a
    [        5       $ )zQWhether a redefinition right now should leave the existing registration in place.NrA   r$   r   r   Úkeeps_canonical_registrationrE   y   s   € ä"¨$Ð.×LÔ3JÓ3LÐLr   c                óš  • [        U 5      nX!L d  [        X5      (       d  U $ [        R                  " U5      nU" S0 U Vs0 s H5  oDR                  (       d  M  UR
                  [        XR
                  5      _M7     snD6nU H?  nUR                  (       a  M  [        XTR
                  [        XR
                  5      5        MA     U$ s  snf )u  Rebuild a re-executed copy of `event_cls` as `event_cls` itself, so serialization stays exact.

Only a class the registry considers the same one (see `is_redefinition`) is converted. Such a
copy comes from re-executing the same module source, so it carries the same fields by
construction, and reading them off it by name is exact.

This is the exception to the "no `fields()` + `getattr` copying" rule in `agent_docs/index.md`:
that rule protects *our own* statically-known types, where Pyright can check field existence and
a rename would silently break the copy. `event_cls` is an application-defined subclass resolved
at runtime, so there is no static field set to check against, and a rename lands on both copies
at once â€” they are the same source, executed twice.
r$   )Útyper>   ÚdataclassesÚfieldsÚinitÚnameÚgetattrÚsetattr)Úvaluer   Ú
value_typerI   ÚfÚ	canonicals         r   Ú_canonicalizerR   ~   s¡   € ô �e“€JØÒ¤o°i×&LÑ&LØˆÜ×Ò 	Ó*€Fñ ÑWÁVÓVÂVÀÏvÍvÓ;˜QŸV™V¤W¨U·F±FÓ%;Ò;ÁVÑVÑW€IÛˆØ�v�v‰vÜ�IŸv™v¤w¨u·f±fÓ'=Ö>ñ ð Ðùò	 Ws   Á CÁ$Cc                óÈ   ^^• U R                   R                  S5      mTb  [        TSS5      (       a  g[        R                  " T5      SUU4S jj5       nSUl        X l        g)aÒ  Keep the base event guards running when a subclass defines its own `__post_init__`.

A dataclass-generated `__init__` calls only the most-derived `__post_init__`, so a subclass
that defines one without calling `super().__post_init__()` would silently skip the family's
construction guards (base instantiation, per-instance tag overrides). Wrapping at class
definition makes the guards unbypassable; a cooperative `super()` call just re-runs them,
which is harmless.
Ú__post_init__NÚ_event_guardedFc                ó>   >• T" U 5        T" U /UQ70 UD6  T" U 5        g r   r$   )r   ÚargsÚkwargsÚbase_post_initÚuser_post_inits      €€r   ÚguardedÚ guard_post_init.<locals>.guarded¦   s'   ø€ á�tÔÙ�tÐ-˜dÒ- fÒ-ñ 	�tÕr   T)r   r   rW   r   rX   r   r,   r-   )Ú__dict__ÚgetrL   Ú	functoolsÚwrapsrU   rT   )r=   rY   r[   rZ   s    ` @r   Úguard_post_initra   ™   s\   ù€ ð —\‘\×%Ñ% oÓ6€NØÑ¤¨Ð9IÈ5×!QÑ!QØä‡_‚_�^Ó$÷ó %ðð "€GÔØÕr   c                óŒ   • [        [        R                  " U 5      5      U-  nU(       a  SR                  [	        U5      5      $ S$ )zPThe class's own field names that shadow the family's envelope fields, or `None`.z, N)Úsetr   Úown_annotationsÚjoinÚsorted)r=   ÚreservedÚshadoweds      r   Úshadowed_envelope_fieldsri   ²   s7   € ä”6×)Ò)¨#Ó.Ó/°(Ñ:€HÞ*2ˆ4�9‰9”V˜HÓ%Ó&Ð<¸Ð<r   c                ó  • U R                   SS  Hs  n[        X!5      (       d  M  UR                  R                  S5      =n(       a  UR	                  S5      S   s  $ UR                  R                  S5      =n(       d  Mq  Us  $    g)as  The namespace `cls` inherits from its nearest base that carries one, or `None`.

A registered base holds its namespace inside `_registered_kind`; an `abstract=True` base never
registered, so it keeps the namespace on its own attribute instead. Either way the search stops at
the first base that has one, so a subclass takes the namespace of the family it was defined in.
r
   NÚ_registered_kindÚ.r   Ú_abstract_namespace)Ú__mro__Ú
issubclassr]   r^   Ú
rpartition)r=   ÚfamilyÚbaseÚ	base_kindÚbase_namespaces        r   Úinherited_namespaceru   ¸   s}   € ð —‘˜A˜B“ˆÜ˜$×'Ñ'ÙØŸ™×)Ñ)Ð*<Ó=Ð=ˆ9Õ=Ø×'Ñ'¨Ó,¨QÑ/Ò/Ø!Ÿ]™]×.Ñ.Ð/DÓEÐEˆ>×EØ!Ò!ñ  ð r   c                ó¾   • U R                   SS  HJ  nX!L d  [        X!5      (       d    gSUR                  ;   a  M+  [        R                  " U5      (       d  MH  Us  $    g)aÜ  A class between `cls` and `family` that declares payload fields but isn't a dataclass.

`@dataclass` only collects fields from bases that are dataclasses themselves, so an intermediate
base holding shared fields silently contributes nothing unless it is decorated too: the fields
vanish from the payload, the wire, and every consumer, with no error anywhere. Each base is fully
built by the time a subclass is being registered, so its decoration can be checked reliably here.
r
   NÚ__dataclass_fields__)rn   ro   r]   r   Údeclares_dataclass_fields)r=   rq   rr   s      r   Úundecorated_field_basery   É   s[   € ð —‘˜A˜B“ˆØŠ>¤¨D×!9Ñ!9Øð ð " T§]¡]Ó2Ùä×+Ò+¨D×1Ó1ØŠKñ  ð r   c           	     ó\  ^ ^^^• [         R                  S:¼  a_  SSKmTR                  T R                  5      mTb  S	UU UU4S jjnUT l        O?T[        0T R                  R                  S0 5      ET l        OTS0T R                  ET l        [        T T[        R                  " USS95        g)
aÀ  Redeclare `tag_field` on the subclass so it defaults to (and serializes as) the registered tag.

The annotation is redeclared on the subclass so `@dataclass` (which runs after
`__init_subclass__`) picks up the new default. On Python 3.14+ the merge wraps the class's lazy
`__annotate__` function instead of materializing `__annotations__`, preserving PEP 649 deferred
evaluation for payload fields that reference names defined later in the module.
)é   é   r   Nc               óT   >• T[         0TR                  TTR                  U 5      TS9E$ )N)Úowner)r+   Úcall_annotate_functionÚFormat)ÚformatÚannotationlibr=   Úoriginal_annotateÚ	tag_fields    €€€€r   ÚannotateÚ"inject_tag_field.<locals>.annotateê   s:   ø€ àœsðà#×:Ñ:Ð;LÈm×NbÑNbÐciÓNjÐruÐ:Ðvðð r   Ú__annotations__r+   T)r(   Úkw_only)r�   Úintr,   zdict[str, Any])ÚsysÚversion_infor‚   Ú!get_annotate_from_class_namespacer]   Ú__annotate__r+   r^   r‡   rM   rH   Úfield)r=   r„   Ú	tag_valuer…   r‚   rƒ   s   ``  @@r   Úinject_tag_fieldr�   Ü   s�   û€ ô ×Ñ˜7Ó"Ûà)×KÑKÈCÏLÉLÓYÐØÑ(÷ò ð  (ˆCÕð $-¬cÐ"]°S·\±\×5EÑ5EÐFWÐY[Ó5\Ð"]ˆCÕà(¨%ÐG°3×3FÑ3FÐGˆÔÜˆC�œK×-Ò-°iÈÑNÕOr   c               óô  ^^^
• [        U5      m
SU
UU4S jjn[        R                  R                  [	        TTU5      U R                  T5      [        R                  R                  [        5      S9n0 nUR                  5        HJ  u  p‰[        R                  " U	5      (       d  [        SU	R                   SU< S35      e[        X	5      Xx'   ML     Xg[        '   [        R                  R                  Xu5      $ )z]Build the tagged union over an event registry, degrading unregistered tags to `unknown_type`.c                ó  >• [        U 5      (       a4  U R                  T5      n[        U[        5      (       a  UT;   a  U$ [        $ [        U TS 5      n[        U[        5      (       a  UT;   a  U$ [        U T5      (       a  [        $ S $ r   )r   r^   Ú
isinstancer+   Ú_UNKNOWN_TAGrL   )rN   r   Ú
known_tagsr„   Úunknown_types     €€€r   ÚdiscriminatorÚ*event_family_schema.<locals>.discriminator  sy   ø€ Ü�u×ÑØ—)‘)˜IÓ&ˆCÜ˜#œs×#Ñ#¨¨zÓ(9Ø�
ÜÐÜ�e˜Y¨Ó-ˆÜ�cœ3×Ñ C¨:Ó$5ØˆJÜ)¨%°×>Ñ>Œ|ÐHÀDÐHr   ©ÚserializationzEvent class z (registered as z) must be a dataclass.)rN   r   r,   ú
str | None)Ú	frozensetÚpydantic_coreÚcore_schemaÚ!no_info_before_validator_functionÚ_gather_unknown_payloadÚgenerate_schemaÚ#wrap_serializer_function_ser_schemaÚ_flatten_unknownÚitemsrH   Úis_dataclassr   r1   Ú_canonicalizing_schemar”   Útagged_union_schema)ÚhandlerÚregistryr„   r–   Úenvelope_fieldsr—   Úunknown_schemaÚchoicesr   r   r•   s     ``      @r   Úevent_family_schemar­   ú   sì   ú€ ô ˜8Ó$€J÷	Iñ 	Iô #×.Ñ.×PÑPÜ 	¨<¸ÓIØ×Ñ Ó-Ü#×/Ñ/×SÑSÔTdÓeð Qð €Nð
 @B€GØ"Ÿ.™.Ö*‰ˆÜ×'Ò'¨	×2Ñ2ÜØ˜y×5Ñ5Ð6Ð6FÀsÁgÐMcÐdóð ô .¨gÓAˆ‹ñ +ð +ŒLÑÜ×$Ñ$×8Ñ8¸ÓPÐPr   c                ó¬   ^• SU4S jjn[         R                  R                  S U R                  T5      [         R                  R	                  U5      S9$ )u½  The event class's own schema, serializing a re-executed copy of it as the registered class.

Under a `set_replay_isolation_guard`, code on the isolated side holds its own copy of the class
while the registry keeps the host's. The copy is the same class by every meaning that matters
here â€” same module, same qualname, same fields â€” so serializing it as the registered one is
exact, and it's what lets both sides use the class they imported.
c                ó(   >• U" [        U T5      5      $ r   )rR   )rN   Ú
serializerr   s     €r   Ú	serializeÚ)_canonicalizing_schema.<locals>.serialize+  s   ø€ Ùœ-¨¨yÓ9Ó:Ð:r   c                ó   • U $ r   r$   )rN   s    r   Ú<lambda>Ú(_canonicalizing_schema.<locals>.<lambda>/  s   € ‘er   r™   ©rN   r   r°   z7pydantic_core.core_schema.SerializerFunctionWrapHandlerr,   r   )r�   rž   rŸ   r¡   r¢   )r¨   r   r±   s    ` r   r¦   r¦   "  sO   ø€ ÷;ô ×$Ñ$×FÑFÙØ×Ñ 	Ó*Ü#×/Ñ/×SÑSÐT]Ó^ð Gð ð r   c                ó,   ^ ^^• US1-
  mSUU U4S jjnU$ )uæ  Before-validator for the unknown-event envelope: move unrecognized payload fields into `data`.

The envelope's own `data` slot is synthetic â€” only this validator ever fills it â€” so a `data` key
on the wire is one of the unknown event's payload fields, not the envelope's, and is gathered
like any other. Treating it as the envelope's would let `_flatten_unknown` promote its entries to
top-level fields, changing the wire shape of an event whose only payload field is named `data`.
Údatac           	     ó†  >• [        U 5      (       a£  U R                  5        VVs0 s H  u  pUT;   d  M  X_M     nnnU R                  5        VVs0 s H  u  pUT;  d  M  X_M     nnnU(       a  XCS'   [        R                  " ST SU R	                  T5      < STR
                   S3[        SS9  U$ U $ s  snnf s  snnf )Nr¸   zUnknown event Ú z; validating as z„. Is the module that defines this event imported? (A serializer built before the event class was defined also treats it as unknown.)é   )Ú
stacklevel)r   r¤   ÚwarningsÚwarnr^   r/   ÚUserWarning)rN   ÚkÚvÚenvelopeÚpayloadÚcarried_fieldsr„   r–   s        €€€r   ÚgatherÚ'_gather_unknown_payload.<locals>.gatherA  sÃ   ø€ Ü�u×ÑØ).¯©¬ÔNª¡ ¸!¸~Ñ:M›˜š©ˆHÑNØ(-¯©¬ÔQª¡ ¸À.Ñ9P“t�q’t©ˆGÑQÞØ#*˜Ñ Ü�MŠMØ   ¨1¨U¯Y©Y°yÓ-AÑ,DÐDTÐUa×UjÑUjÐTkð l@ð Aô Øòð ˆOØˆùó OùÛQs   ¥B7µB7ÁB=Á!B=)rN   r   r,   r   r$   )r„   r–   rª   rÅ   rÄ   s   ``  @r   r    r    5  s"   ú€ ð %¨ xÑ/€N÷ñ ð  €Mr   c                ó’   • U" U 5      n[        U5      (       d  U$ [        UR                  SS5      =n5      (       a  0 UEUE$ X2S'   U$ )zaSerializer for the unknown-event envelope: re-flatten `data` so the typed event can be recovered.r¸   N)r   r%   )rN   r°   Údumpedr¸   s       r   r£   r£   T  sS   € á˜UÓ#€Fä�v×ÑØˆÜ˜6Ÿ:™: f¨dÓ3Ð3�4×4Ñ4Ø!�$Ð!˜&Ð!Ð!Øˆ6�NØ€Mr   )r,   r-   )r,   r‰   )r<   rG   r=   rG   r,   Úbool)rB   zCallable[[], bool]r,   r-   )r,   rÉ   )rN   Úobjectr   rG   r,   r   )r=   rG   rY   zCallable[[Any], None]r,   r-   )r=   rG   rg   úfrozenset[str]r,   r›   )r=   rG   rq   rG   r,   r›   )r=   rG   rq   rG   r,   ztype | None)r=   rG   r„   r+   r�   r+   r,   r-   )r¨   úpydantic.GetCoreSchemaHandlerr©   zMapping[str, type[Any]]r„   r+   r–   ú	type[Any]rª   rË   r,   z$pydantic_core.core_schema.CoreSchema)r¨   rÌ   r   rÍ   r,   r   )r„   r+   r–   rÍ   rª   rË   r,   zCallable[[Any], Any]r¶   )0r3   Ú
__future__r   rH   r_   rŠ   r½   Úcollections.abcr   r   Útypingr   r   r   r	   Úpydanticr�   Ú r   r   Ú
exceptionsr   r”   rœ   ÚRESERVED_EVENT_TAGSr8   r   r   Údictr+   r   r   r:   r>   r?   r‡   rC   rE   rR   ra   ri   ru   ry   r�   r­   r¦   r    r£   r$   r   r   Ú<module>rÖ      s>  ðò
õ #ã Û Û 
Û ß -ß 2Ó 2ã Û å Ý Ý !à€á  Ó/Ð Ø dàÐ á�~Ó&€Ù�KÓ €	ô!�D˜˜lÐ*Ñ+¨W°\Ñ-Bô !ôHô
ô_ð 6:Ð Ð2Ó 9ô$ô$Mô
ô6 ô2=ôô"ô&Pð<%QØ*ð%Qð &ð%Qð ð	%Qð
 ð%Qð $ð%Qð *ô%QôPð&ØðØ"+ðØ>Lðàôõ>	r   