
    i>                         d Z ddlZddlZddlZddlmZmZ ddlmZ ddlm	Z	m
Z
  e ej                  dd            Z G d	 d
e	      Zy)u   
Genesis Persistent Context Architecture — Execution Telemetry Interceptor
Story 1.04 — Track B

Logs every dispatch lifecycle event to events.jsonl.
Priority 0 — runs first in chain, captures everything.
    N)datetimetimezone)Path   )BaseInterceptorInterceptorMetadataEVENTS_LOG_PATHz5/mnt/e/genesis-system/data/observability/events.jsonlc                       e Zd ZdZ eddd      Z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defdZdeddfdZy)ExecutionTelemetryInterceptoru:   Logs dispatch lifecycle events. Priority 0 — runs first.execution_telemetryr   T)namepriorityenabledc                     i | _         y )N)_start_times)selfs    >/mnt/e/genesis-system/core/interceptors/execution_telemetry.py__init__z&ExecutionTelemetryInterceptor.__init__   s
    "$    task_payloadreturnc                 `  K   |j                  dd      }t        j                         | j                  |<   | j	                  d||j                  d|j                  dd            |j                  dd      t        j                  t        j                        j                         d       |S w)	z/Record start time and log dispatch_start event.task_idunknowndispatch_starttype	task_typetierdefault)eventr   r   r   	timestamp)
gettime	monotonicr   _appendr   nowr   utc	isoformat)r   r   r   s      r   pre_executez)ExecutionTelemetryInterceptor.pre_execute   s     ""9i8%)^^%5'"%%))&,2B2B;PY2Z[ $$VY7!hll3==?
 	 s   B,B.resultNc           	      d  K   |j                  dd      }| j                  j                  |d      }|rt        j                         |z
  dz  nd}| j                  d||j                  dd      t        |d      t        j                  t        j                        j                         d	       yw)
z$Log dispatch_complete with duration.r   r   Ni  dispatch_completestatus   )r    r   r.   duration_msr!   )r"   r   popr#   r$   r%   roundr   r&   r   r'   r(   )r   r*   r   r   startr0   s         r   post_executez*ExecutionTelemetryInterceptor.post_execute*   s     ""9i8!!%%gt4;@t~~'%/47b(jj95 a0!hll3==?
 	s   B.B0errorc           	      l  K   |j                  dd      }| j                  j                  |d       | j                  d|t	        |      j
                  t        |      t        j                  t        j                        j                         d       t        |      t	        |      j
                  dS w)zLog dispatch_error event.r   r   Ndispatch_error)r    r   
error_type	error_msgr!   )r5   r8   )r"   r   r1   r%   r   __name__strr   r&   r   r'   r(   )r   r5   r   r   s       r   on_errorz&ExecutionTelemetryInterceptor.on_error7   s     ""9i8gt,%u+..U!hll3==?
 	 U4;3G3GHHs   B2B4correction_payloadc                    K   |j                  dd      }|j                  dd      }| j                  d||t        j                  t        j
                        j                         d       |S w)zLog dispatch_correction event.r   r   attemptr   dispatch_correction)r    r   r?   r!   )r"   r%   r   r&   r   r'   r(   )r   r=   r   r?   s       r   on_correctionz+ExecutionTelemetryInterceptor.on_correctionD   se     $((I>$((A6*!hll3==?	
 	 "!s   A,A.recordc                     	 t         j                  j                  dd       t        t         d      5 }|j	                  t        j                  |      dz          ddd       y# 1 sw Y   yxY w# t        $ r Y yw xY w)z@Append a record to the events log. Append-only, never overwrite.T)parentsexist_oka
N)r	   parentmkdiropenwritejsondumps	Exception)r   rB   fs      r   r%   z%ExecutionTelemetryInterceptor._appendP   sl    	""(((Eos+ 3q

6*T123 3 3 		s.   1A0 (A$A0 $A-)A0 -A0 0	A<;A<)r:   
__module____qualname____doc__r   metadatar   dictr)   r4   rN   r<   rA   r%    r   r   r   r      s    D"(=SWXH%d t  T d II IT Id I
"d 
"t 
"d t r   r   )rR   rL   osr#   r   r   pathlibr   base_interceptorr   r   getenvr	   r   rU   r   r   <module>rZ      sI     	  '  Byryy; BO Br   