
    >i                     z    d 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	 ddl
mZ  ed      ZdZ G d	 d
e      Zy)u  
Genesis Persistent Context Architecture — JIT Hydration Interceptor
Story 2.06 — Track B

JITHydrationInterceptor — wires zero-amnesia memory injection into the
interceptor chain.

Priority 10 — runs after telemetry (0), before business logic (50+).
    N)datetimetimezone)Path)BaseInterceptorInterceptorMetadata)interceptor_jit_hydrationz(/mnt/e/genesis-system/data/observabilityzH<ZERO_AMNESIA_STATE><ERROR>Hydration failed</ERROR></ZERO_AMNESIA_STATE>c                   x    e Zd ZU dZ edd      Zeed<   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y
)JITHydrationInterceptoruC  
    Injects ZERO_AMNESIA_STATE XML envelope into every task payload.

    Priority 10 — runs after telemetry (0), before business logic (50+).
    Failure in hydration NEVER blocks execution: an error envelope is
    inserted instead, guaranteeing the downstream LLM call always receives
    a system_injection key.
    jit_hydration
   )nameprioritymetadatatask_payloadreturnc                 j   K   	 t        |       d{   S 7 # t        $ r t        |d<   |cY S w xY ww)a  
        Calls interceptor_jit_hydration, attaches system_injection to payload.

        On any hydration failure, injects an ERROR envelope so execution is
        never blocked by memory unavailability.

        Args:
            task_payload: Dict with task data. May contain "task_id", "prompt",
                          "description", "task", "file", "code", "context".

        Returns:
            The same dict with "system_injection" key set to the XML envelope.
        Nsystem_injectionr   	Exception_ERROR_ENVELOPE)selfr   s     >/mnt/e/genesis-system/core/memory/jit_hydration_interceptor.pypre_executez#JITHydrationInterceptor.pre_execute(   s;     	 2<@@@@ 	 />L+,	 s(   3  3 0303resultNc                   K   |j                  dd      }	 t        j                  dd       t        j                  t
        j                        j                         dt        |      |j                  dd      d}t        t        d	z  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w)u8  
        Logs system_injection length to observability events.

        Side-effects only — does NOT mutate result or task_payload.

        Args:
            result:       The execution result dict (not mutated).
            task_payload: The enriched task dict (used for task_id + injection length).
        r    T)parentsexist_okjit_hydration_completetask_idunknown)	timestamp
event_typeinjection_lengthr    zevents.jsonla
N)get
EVENTS_DIRmkdirr   nowr   utc	isoformatlenopenwritejsondumpsr   )r   r   r   	injectioneventfs         r   post_executez$JITHydrationInterceptor.post_execute<   s      !$$%7<		TD9%\\(,,7AAC6$'	N'++IyA	E j>137 21

5)D012 2 2 		sM   CA9C (C 7C ?C C	C C	C 	CCCCerrorc                 0   K   t        |      |t        dS w)a7  
        Returns a correction payload with error envelope in system_injection.

        Args:
            error:        The exception that caused the failure.
            task_payload: The original task dict.

        Returns:
            Dict with "error", "task_payload", and "system_injection" keys.
        )r6   r   r   )strr   )r   r6   r   s      r   on_errorz JITHydrationInterceptor.on_errorT   s      Z( /
 	
s   correction_payloadc                    K   d|v rd|d    |d<   	 t        |       d{   S 7 # t        $ r t        |d<   |cY S w xY ww)a  
        Prepends CORRECTION: prefix to the prompt and re-hydrates.

        If no "prompt" key is present the payload is still safely re-hydrated
        without mutation. On hydration failure, inserts an error envelope.

        Args:
            correction_payload: Task dict for the correction pass.

        Returns:
            Re-hydrated payload with updated system_injection.
        promptzCORRECTION: Nr   r   )r   r:   s     r   on_correctionz%JITHydrationInterceptor.on_correctione   sa      ))-9:LX:V9W+Xx(	&23EFFFF 	&5D12%%	&s,   A( &( A( ?A?A)__name__
__module____qualname____doc__r   r   __annotations__dictr   r5   r   r9   r=        r   r
   r
      s{     %8%H! 
 d  t  ( T d 0
I 
T 
d 
"&d &t &rE   r
   )rA   asyncior0   r   r   pathlibr   "core.interceptors.base_interceptorr   r   core.memory.jit_hydrationr   r(   r   r
   rD   rE   r   <module>rJ      sB      '  S ?<=
 O 
_&o _&rE   