§
    éŽ’jr  ã                  óH   — d Z ddlmZ ddlZddlmZmZ  G d„ d¦  «        ZdS )zDThread-safe hierarchical key-value store for extensibility contexts.é    )ÚannotationsN)ÚAnyÚCallablec                  óR   — e Zd ZdZddd„Zddd„Zdd„Z	 ddd„Zdd„Zdd„Z	d d„Z
dS )!Ú
StateStorea@  Thread-safe hierarchical key-value store for extensibility contexts.

  Provides atomic state operations (``get_state``, ``set_state``,
  ``update_state``) and explicit lock/context manager support across both
  flat and hierarchical (parent-child) extensibility handles such as
  ``ToolContext`` and ``HookContext``.
  NÚparentúStateStore | NoneÚreturnÚNonec                óR   — || _         i | _        t          j        ¦   «         | _        dS )zŒInitializes the StateStore.

    Args:
      parent: Optional parent state store for fallback lookups in hierarchical
        contexts.
    N)r   Ú_storeÚ	threadingÚRLockÚ_lock)Úselfr   s     úLC:\Projects\DSCons\.venv\Lib\site-packages\google\antigravity\utils\state.pyÚ__init__zStateStore.__init__    s%   € ð €D„KØ"$€D„KÝ”Ñ"Ô"€D„J€J€Jó    ÚkeyÚstrÚdefaultr   c                óÎ   — | j         5  || j        v r| j        |         cddd¦  «         S 	 ddd¦  «         n# 1 swxY w Y   | j        �| j                             ||¦  «        S |S )a  Retrieves a value from the local state store or its parents.

    Args:
      key: The state key.
      default: Value returned when key is absent in this store and any
        ancestors.

    Returns:
      The stored value, or ``default`` if the key is not found.
    N)r   r   r   Ú	get_state)r   r   r   s      r   r   zStateStore.get_state+   sÊ   € ð 
Œð  ð  Ø	�”Ð	Ð	ØŒ{˜3Ôð ð  ð  ð  ñ  ô  ð  ð  Ø	ð ð  ð  ñ  ô  ð  ð  ð  ð  ð  ð  øøøð  ð  ð  ð  ð „{ÐØŒ[×"Ò" 3¨Ñ0Ô0Ð0Ø€Ns   ˆ8¸<¿<Úvaluec                óZ   — | j         5  || j        |<   ddd¦  «         dS # 1 swxY w Y   dS )z{Stores a value in the local state store.

    Args:
      key: The state key.
      value: The value to store locally.
    N)r   r   )r   r   r   s      r   Ú	set_statezStateStore.set_state=   s|   € ð 
Œð ð Ø€d„k�#Ñðð ð ñ ô ð ð ð ð ð ð ð øøøð ð ð ð ð ð s   ˆ  $§$Ú
updater_fnúCallable[[Any], Any]c                óž   — | j         5  |                      ||¦  «        } ||¦  «        }|| j        |<   |cddd¦  «         S # 1 swxY w Y   dS )a\  Atomically updates a value in the local state store.

    Note: ``updater_fn`` executes while holding the global reentrant lock
    (``self._lock``). To avoid blocking concurrent execution across the session,
    ``updater_fn`` should be a fast, non-blocking callback without synchronous
    I/O or long sleeps.

    If the key exists only in a parent store, the current parent value is passed
    to ``updater_fn``, and the updated result is stored in the local store so
    that local turn updates do not mutate parent session scope.

    Args:
      key: The state key.
      updater_fn: Function taking current value (or default if missing) and
        returning updated value to store.
      default: Value passed to ``updater_fn`` if key is not found across the
        hierarchy.

    Returns:
      The updated value after applying ``updater_fn``.
    N)r   r   r   )r   r   r   r   ÚvalÚnew_vals         r   Úupdate_statezStateStore.update_stateG   sŸ   € ð6 
Œð ð Ø�NŠN˜3 Ñ(Ô(€cØ�
˜3‘”€gØ €d„k�#ÑØð	ð ð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð ð ð s   ˆ-AÁAÁ	Aúthreading.RLockc                ó   — | j         S )z�Returns the underlying reentrant lock for custom critical sections.

    Returns:
      The `threading.RLock` protecting this state store.
    )r   ©r   s    r   ÚlockzStateStore.lockh   s   € ð Œ:Ðr   c                ó8   — | j                              ¦   «          | S )z6Acquires the store lock for use in a `with` statement.)r   Úacquirer%   s    r   Ú	__enter__zStateStore.__enter__p   s   € à„J×ÒÑÔÐØ€Kr   Úexc_typeÚexc_valÚexc_tbc                ó8   — | j                              ¦   «          dS )z8Releases the store lock when exiting a `with` statement.N)r   Úrelease)r   r*   r+   r,   s       r   Ú__exit__zStateStore.__exit__u   s   € à„J×ÒÑÔÐÐÐr   )N)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   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r&   r)   r/   © r   r   r   r      s¿   € € € € € ðð ð	#ð 	#ð 	#ð 	#ð 	#ðð ð ð ð ð$ð ð ð ð ð	ð ð ð ð ðBð ð ð ðð ð ð ð
ð ð ð ð ð r   r   )r3   Ú
__future__r   r   Útypingr   r   r   r4   r   r   ú<module>r7      s€   ðð KÐ Jà "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  ð`ð `ð `ð `ð `ñ `ô `ð `ð `ð `r   