Ë
    òÏj\  ã            	      óÚ   — d dl mZ d dlZd dlZd dlmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZmZmZ d dlmZmZmZmZ dZ G d„ de
e   ee	e	e	f   «      Z y)é    )ÚannotationsN)ÚCallableÚSequence)ÚAnyÚGeneric)ÚPendingWrite)Ú_DeltaSnapshot)ÚSelf©ÚMISSING)ÚBaseChannelÚValue)Ú_get_overwriteÚ_operators_equalÚ_strip_extras)ÚEmptyChannelErrorÚ	ErrorCodeÚInvalidUpdateErrorÚcreate_error_message)ÚDeltaChannelc                  ó°   ‡ — e Zd ZU dZdZded<   	 dddœ	 	 	 	 	 	 	 dˆ fd„Zdd„Zedd	„«       Z	edd
„«       Z
dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zˆ xZS )r   aß  Reducer channel that stores only a sentinel in checkpoint blobs and
    reconstructs state by replaying ancestor writes through the reducer.

    !!! warning "Beta"

        `DeltaChannel` is in beta. The API and on-disk representation may
        change in future releases. Threads written with `DeltaChannel` today
        are expected to remain readable, but the surrounding contract
        (`BaseCheckpointSaver.get_delta_channel_history`, the
        `_DeltaSnapshot` blob shape, the `counters_since_delta_snapshot`
        metadata field) is not yet stable.

    The reducer receives the current accumulated value and a batch of writes
    in one call: `reducer(state, [write1, write2, ...]) -> new_state`.

    Reducers must be deterministic and batching-invariant (associative across
    folds): applying two consecutive write batches separately must produce the
    same state as applying their concatenation once:

        reducer(reducer(state, xs), ys) == reducer(state, xs + ys)

    This lets LangGraph replay checkpointed writes in larger batches than they
    were originally produced without changing reconstructed state.

    Snapshot cadence is driven by two counters: per-channel update count and
    total supersteps since last snapshot. `create_checkpoint` writes a full
    `_DeltaSnapshot` blob when EITHER the update count reaches
    `snapshot_frequency` OR the supersteps count reaches the system-wide
    `DELTA_MAX_SUPERSTEPS_SINCE_SNAPSHOT` bound (default 5000), bounding
    replay depth even for channels that stop receiving writes.

    Parameters:
        reducer: `(state, list[writes]) -> new_state`. Must be deterministic
            and batching-invariant as described above.
        typ: The value type (e.g. `list`, `dict`). Inferred automatically
            from the outer type when used inside `Annotated[T, DeltaChannel(...)]`.
        snapshot_frequency: Every Nth update to this channel writes a snapshot
            blob (default `1000`). Must be a positive int.
    )ÚvalueÚreducerÚsnapshot_frequencyzValue | Anyr   iè  ©r   c               ó  •— |dk  rt        d|› �«      ‚|€t        }t        ‰| �  |«       || _        || _        t        |«      }|t        j                  j                  t        j                  j                  fv rt        }|t        j                  j                  t        j                  j                  fv rt        }|t        j                  j                  t        j                  j                  fv rt         }|| _        t$        | _        y )Nr   z/snapshot_frequency must be a positive int, got )Ú
ValueErrorÚlistÚsuperÚ__init__r   r   r   ÚcollectionsÚabcr   ÚMutableSequenceÚSetÚ
MutableSetÚsetÚMappingÚMutableMappingÚdictÚtypr   r   )Úselfr   r*   r   Ú	__class__s       €úU/var/www/html/gaurav-v2/venv/lib/python3.12/site-packages/langgraph/channels/delta.pyr    zDeltaChannel.__init__E   sØ   ø€ ð  Ò"ÜØAÐBTÐAUÐVóð ð ˆ;ÜˆCÜ‰Ñ˜ÔØˆŒØ"4ˆÔÜ˜CÓ ˆØ”;—?‘?×+Ñ+¬[¯_©_×-LÑ-LÐMÑMÜˆCØ”;—?‘?×&Ñ&¬¯©×(BÑ(BÐCÑCÜˆCØ”;—?‘?×*Ñ*¬K¯O©O×,JÑ,JÐKÑKÜˆCØˆŒÜ!ˆ�
ó    c                ó˜   — t        |t        «      sy| j                  |j                  k7  ryt        | j                  |j                  «      S )NF)Ú
isinstancer   r   r   r   )r+   Úothers     r-   Ú__eq__zDeltaChannel.__eq___   s;   € Ü˜%¤Ô.ØØ×"Ñ" e×&>Ñ&>Ò>ØÜ §¡¨e¯m©mÓ<Ð<r.   c                ó   — | j                   S ©N©r*   ©r+   s    r-   Ú	ValueTypezDeltaChannel.ValueTypef   ó   € à�x‰xˆr.   c                ó   — | j                   S r4   r5   r6   s    r-   Ú
UpdateTypezDeltaChannel.UpdateTypej   r8   r.   c                ó  — | j                  | j                  | j                  | j                  ¬«      }| j                  |_        | j
                  t        u r| j
                  |_        |S t        j                  | j
                  «      |_        |S )Nr   )	r,   r   r*   r   Úkeyr   r   Ú_copyÚcopy)r+   Únews     r-   r>   zDeltaChannel.copyn   sn   € Ø�n‰nØ�L‰L˜$Ÿ(™(°t×7NÑ7Nð ó 
ˆð —(‘(ˆŒØ"&§*¡*´Ñ"7�D—J‘JˆŒ	Øˆ
ô >C¿Z¹ZÈÏ
É
Ó=SˆŒ	Øˆ
r.   c                ó  — | j                  | j                  | j                  | j                  ¬«      }| j                  |_        |t
        u r| j                  «       |_        |S t        |t        «      r|j                  |_        |S ||_        |S )a*  Initialize from a stored blob.

        Blob types:
          * `MISSING`: start empty; caller replays writes.
          * `_DeltaSnapshot(value)`: restore value directly from snapshot.
          * plain value (migration from old `BinaryOperatorAggregate` blobs):
            use directly.
        r   )	r,   r   r*   r   r<   r   r   r0   r	   )r+   Ú
checkpointr?   s      r-   Úfrom_checkpointzDeltaChannel.from_checkpointv   sƒ   € ð �n‰nØ�L‰L˜$Ÿ(™(°t×7NÑ7Nð ó 
ˆð —(‘(ˆŒØœÑ ØŸ™›
ˆCŒIð
 ˆ
ô	 ˜
¤NÔ3Ø"×(Ñ(ˆCŒIð ˆ
ð #ˆCŒIØˆ
r.   c                óH  — |D ��cg c]  \  }}}|‘Œ
 }}}|sy| j                   }d}t        |«      D ]B  \  }}t        |«      \  }}	|sŒ|	�t        j                  |	«      n| j                  «       }|dz   }ŒD ||d }
|
r| j                  ||
«      | _         y|| _         yc c}}w )a
  Apply ancestor writes oldest-to-newest via a single reducer call.

        If any write is an Overwrite, the last one in the sequence acts as
        the reset point: its value becomes the new base and only writes
        after it are passed to the reducer.
        Nr   é   )r   Ú	enumerater   r=   r>   r*   r   )r+   ÚwritesÚ_ÚvÚvaluesÚbaseÚstartÚiÚis_owÚow_valueÚ	remainings              r-   Úreplay_writeszDeltaChannel.replay_writes‹   s¦   € ñ $*Ô*¡6™˜˜1˜a’! 6ˆÑ*ÙØØ�z‰zˆØˆÜ˜fÖ%‰DˆAˆqÜ,¨QÓ/‰OˆE�8ÚØ/7Ð/C”u—z‘z (Ô+ÈÏÉË�Ø˜A™‘ð	 &ð
 ˜5˜6�Nˆ	Ù6?�T—\‘\ $¨	Ó2ˆ�
ÀTˆ�
ùó +s   †Bc                óÒ  — |syd }t        |«      D ]@  \  }}t        |«      \  }}|sŒ|�&t        dt        j                  ¬«      }t        |«      ‚|}ŒB |�Dt        ||   «      \  }}|�t        j                  |«      | _	        y| j                  «       | _	        y| j                  t        u r| j                  «       n| j                  }	| j                  |	t        |«      «      | _	        y)NFz4Can receive only one Overwrite value per super-step.)ÚmessageÚ
error_codeT)rE   r   r   r   ÚINVALID_CONCURRENT_GRAPH_UPDATEr   r=   r>   r*   r   r   r   r   )
r+   rI   Úoverwrite_idxrL   rH   rM   rG   ÚmsgÚoverwrite_valuerJ   s
             r-   ÚupdatezDeltaChannel.updateŸ   sé   € ÙØØ$(ˆÜ˜fÖ%‰DˆAˆqÜ% aÓ(‰HˆE�1ÚØ Ð,Ü.Ø VÜ#,×#LÑ#Lô�Cô -¨SÓ1Ð1Ø !‘ð &ð Ð$Ü!/°°}Ñ0EÓ!FÑˆAˆð #Ð.ô —
‘
˜?Ó+ð ŒJð
 ð —X‘X“Zð ŒJð
 Ø!ŸZ™Z¬7Ñ2ˆt�x‰xŒz¸¿
¹
ˆØ—\‘\ $¬¨V«Ó5ˆŒ
Ør.   c                óR   — | j                   t        u r
t        «       ‚| j                   S r4   )r   r   r   r6   s    r-   ÚgetzDeltaChannel.get¹   s!   € Ø�:‰:œÑ Ü#Ó%Ð%Ø�z‰zÐr.   c                ó&   — | j                   t        uS r4   )r   r   r6   s    r-   Úis_availablezDeltaChannel.is_available¾   s   € Ø�z‰z¤Ð(Ð(r.   c                ó   — t         S )a�  Return stored representation: always `MISSING`.

        Snapshot decisions live in `create_checkpoint` (which has the channel
        version) and write `_DeltaSnapshot(ch.get())` directly into
        `channel_values`. For non-snapshot steps the channel does not appear
        in `channel_values`; reconstruction walks ancestor writes via the
        saver's `get_delta_channel_history`.
        r   r6   s    r-   rA   zDeltaChannel.checkpointÁ   s	   € ô ˆr.   r4   )r   z#Callable[[Any, Sequence[Any]], Any]r*   ztype[Value] | Noner   ÚintÚreturnÚNone)r1   Úobjectr_   Úbool)r_   r   )r_   r
   )rA   r   r_   r
   )rF   zSequence[PendingWrite]r_   r`   )rI   zSequence[Any]r_   rb   )r_   rb   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__Ú__annotations__r    r2   Úpropertyr7   r:   r>   rB   rP   rX   rZ   r\   rA   Ú__classcell__)r,   s   @r-   r   r      s¡   ø… ñ&ðP ;€IØÓð
 #'ð"ð
 #'ñ"à4ð"ð  ð"ð
  ð"ð 
õ"ó4=ð òó ðð òó ðóóó*Jó(ó4ó
)÷	r.   r   )!Ú
__future__r   Úcollections.abcr!   r>   r=   r   r   Útypingr   r   Úlanggraph.checkpoint.baser   Ú langgraph.checkpoint.serde.typesr	   Útyping_extensionsr
   Úlanggraph._internal._typingr   Úlanggraph.channels.baser   r   Úlanggraph.channels.binopr   r   r   Úlanggraph.errorsr   r   r   r   Ú__all__r   © r.   r-   Ú<module>rw      sY   ðÝ "ã Û ß .ß å 2Ý ;Ý "å /ß 6ß TÑ T÷ó ð €ôq�7˜5‘> ;¨s°C¸¨}Ñ#=õ qr.   