
    i                     @    d Z ddlZddlmZmZ ddlmZ  G d d      Zy)u   
Genesis Persistent Context Architecture — InterceptorChain
Story 1.02 — Track B

Ordered execution engine for interceptors. Runs all registered
interceptors in priority order (lower number = earlier execution).
    N)ListOptional   )BaseInterceptorc                       e Zd ZdZd ZdeddfdZdedefdZ	d	e
de
fd
Zde
d	e
ddfdZded	e
de
fdZdefdZdefdZy)InterceptorChainz0Ordered chain executor for Genesis interceptors.c                     g | _         y N)_interceptorsselfs    </mnt/e/genesis-system/core/interceptors/interceptor_chain.py__init__zInterceptorChain.__init__   s
    46    interceptorreturnNc                 t    | j                   j                  |       | j                   j                  d        y)zCRegister an interceptor. Chain re-sorts by priority after each add.c                 .    | j                   j                  S r
   )metadatapriority)xs    r   <lambda>z+InterceptorChain.register.<locals>.<lambda>   s    ajj.A.A r   )keyN)r   appendsort)r   r   s     r   registerzInterceptorChain.register   s.    !!+.$ABr   namec                     t        | j                        }| j                  D cg c]  }|j                  j                  |k7  s|  c}| _        t        | j                        |k  S c c}w )z>Remove interceptor by name. Returns True if found and removed.)lenr   r   r   )r   r   beforeis       r   
unregisterzInterceptorChain.unregister   sW    T''()-););WAqzzRV?VaW4%%&// Xs   A'A'payloadc                    K   |}| j                   D ]2  }|j                  j                  s|j                  |       d{   }4 |S 7 w)z<Run all enabled interceptors' pre_execute in priority order.N)r   r   enabledpre_execute)r   r#   enrichedr   s       r   execute_prezInterceptorChain.execute_pre   sQ     -- 	CK##++!,!8!8!BB	C  Cs   (A
A
A 	A
resultc                    K   | j                   D ]4  }|j                  j                  s	 |j                  ||       d{    6 y7 # t        $ r Y Ew xY ww)zIRun all enabled interceptors' post_execute. Continues even if one raises.N)r   r   r%   post_execute	Exception)r   r)   r#   r   s       r   execute_postzInterceptorChain.execute_post'   s\     -- 	K##++%2267CCC	 D  s9   &AA	A A	AA			AAAAerrorc                    K   | j                   D ]5  }|j                  j                  s	 |j                  ||       d{   c S  t        |      ddS 7 # t        $ r Y Sw xY ww)z@Run first matching on_error handler. Returns correction payload.NT)r.   	unhandled)r   r   r%   on_errorr,   str)r   r.   r#   r   s       r   execute_errorzInterceptorChain.execute_error0   sm     -- 	K##++!,!5!5eW!EEE	 U$77 F  s9   &A&AA AA&A	A# A&"A##A&c                     | j                   D cg c]D  }|j                  j                  |j                  j                  |j                  j                  dF c}S c c}w )z8Returns [{name, priority, enabled}] ordered by priority.)r   r   r%   )r   r   r   r   r%   )r   r!   s     r   get_chain_summaryz"InterceptorChain.get_chain_summary:   sS     ''
 	 

JJ//::--
 	
 
s   A	Ac                 ,    t        | j                        S r
   )r   r   r   s    r   __len__zInterceptorChain.__len__E   s    4%%&&r   )__name__
__module____qualname____doc__r   r   r   r2   boolr"   dictr(   r-   r,   r3   listr5   intr7    r   r   r   r      s    :7CO C C
0s 0t 0 $    8 8T 8d 8	
4 	
' 'r   r   )r;   asynciotypingr   r   base_interceptorr   r   r@   r   r   <module>rD      s     ! -8' 8'r   