§
    Pòmj†  ã                  ó¸   — U d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
  edd¬¦  «        Zd	ed
<   dZdZded<   edd d„¦   «         Zd!d„Zd!d„Zd"d„Z	 dd#d„ZdS )$a¹  Context-local state for delegate_task child execution.

The parent Hermes process may itself be a Kanban dispatcher worker with
HERMES_KANBAN_* variables in process env. delegate_task children run inside the
same Python process, but they are not dispatcher-owned Kanban workers. This
module lets code paths that resolve tool schemas or spawn subprocesses fail
closed for delegated children without mutating global os.environ for the parent.
é    )Úannotations)Úcontextmanager)Ú
ContextVar)ÚIteratorÚMappingÚMutableMappingÚhermes_delegated_child_contextF)ÚdefaultzContextVar[bool]Ú_DELEGATED_CHILD_CONTEXTÚHERMES_DELEGATED_CHILD_CONTEXT)ÚHERMES_KANBAN_TASKÚHERMES_KANBAN_RUN_IDÚHERMES_KANBAN_WORKSPACEÚHERMES_KANBAN_WORKSPACES_ROOTÚHERMES_KANBAN_CLAIM_LOCKÚHERMES_KANBAN_BOARDÚHERMES_KANBAN_DBztuple[str, ...]ÚKANBAN_ENV_KEYSNÚ
session_idú
str | NoneÚreturnúIterator[None]c              #  ó  K  — t                                d¦  «        }	 ddlm}  || ¦  «        5  dV — ddd¦  «         n# 1 swxY w Y   t                                |¦  «         dS # t                                |¦  «         w xY w)a.  Mark child execution and isolate its task-local session identity.

    Child construction calls ``set_current_session_id`` internally, so even a
    context entered without an id must restore the parent's ContextVar.  Child
    execution passes its explicit id and receives it only for this scope.
    Tr   )Úscoped_current_session_idN)r   ÚsetÚgateway.session_contextr   Úreset)r   Útokenr   s      ú>/home/thesage/.hermes/hermes-agent/agent/delegation_context.pyÚdelegated_child_contextr    !   sÛ   è è € õ %×(Ò(¨Ñ.Ô.€Eð.ð 	FÐEÐEÐEÐEÐEà&Ð& zÑ2Ô2ð 	ð 	ØˆEˆEˆEð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	õ 	!×&Ò& uÑ-Ô-Ð-Ð-Ð-øÕ ×&Ò& uÑ-Ô-Ð-Ð-øøøs-   žA' ¯A ´A' Á AÁA' ÁAÁA' Á'BÚboolc                 óN   — t          t                               ¦   «         ¦  «        S )z<Return True while code is running for a delegate_task child.)r!   r   Úget© ó    r   Úis_delegated_child_contextr&   5   s   € åÕ(×,Ò,Ñ.Ô.Ñ/Ô/Ð/r%   c                 ó®   — ddl } t          t                               ¦   «         ¦  «        p+t          | j                             t
          ¦  «        ¦  «        S )z?Return True in this process or a subprocess spawned by a child.r   N)Úosr!   r   r#   ÚenvironÚDELEGATED_CHILD_ENV_MARKER)r(   s    r   Ú"is_delegated_child_process_contextr+   :   sK   € à€I€I€IåÕ(×,Ò,Ñ.Ô.Ñ/Ô/ð µ4Ø
Œ
ŠÕ1Ñ2Ô2ñ4ô 4ð r%   Úenvú,Mapping[str, str] | MutableMapping[str, str]údict[str, str]c                óx   — t          | ¦  «        }t          D ]}|                     |d¦  «         Œd|t          <   |S )z;Return *env* with dispatcher-only Kanban variables removed.NÚ1)Údictr   Úpopr*   )r,   ÚcleanedÚkeys      r   Úscrub_kanban_envr5   C   sC   € å3‰iŒi€GÝð ð ˆØŠC˜ÑÔÐÐØ*-€GÕ&Ñ'Ø€Nr%   ú3Mapping[str, str] | MutableMapping[str, str] | Noneúdict[str, str] | Nonec                ó|   — t          ¦   «         s| €dnt          | ¦  «        S | €ddl}|j        } t	          | ¦  «        S )a  Return an env override only when delegated-child lineage must cross fork.

    Most subprocess call sites historically used ``env=None`` to inherit the
    process environment.  In a ``delegate_task`` child, inheriting as-is leaks
    parent dispatcher ``HERMES_KANBAN_*`` vars while losing the ContextVar in
    the new process.  This helper preserves normal ``env=None`` semantics for
    non-delegated calls, and only materializes a scrubbed env when the lineage
    marker must be propagated across a child-process boundary.
    Nr   )r+   r1   r(   r)   r5   )r,   r(   s     r   Údelegated_child_subprocess_envr9   L   sI   € õ .Ñ/Ô/ð 2Ø{ˆtˆt­¨S©	¬	Ð1à
€{Øˆ	ˆ	ˆ	àŒjˆÝ˜CÑ Ô Ð r%   )N)r   r   r   r   )r   r!   )r,   r-   r   r.   )r,   r6   r   r7   )Ú__doc__Ú
__future__r   Ú
contextlibr   Úcontextvarsr   Útypingr   r   r   r   Ú__annotations__r*   r   r    r&   r+   r5   r9   r$   r%   r   ú<module>r@      sI  ððð ð ð #Ð "Ð "Ð "Ð "Ð "à %Ð %Ð %Ð %Ð %Ð %Ø "Ð "Ð "Ð "Ð "Ð "Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4à-7¨ZØ$Øð.ñ .ô .Ð ð ð ð ñ ð
 >Ð ð$€ð ð ð ñ ð ð.ð .ð .ð .ñ „ð.ð&0ð 0ð 0ð 0ð
ð ð ð ðð ð ð ð @Dð!ð !ð !ð !ð !ð !ð !r%   