
    i#                         d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ  e ej                  dd            Zd	Zd
Z	 ddede	e   dededef
dZdedee   fdZde	e   dee   fdZdedee   fdZy)uE  
Genesis Persistent Context Architecture — Scatter-Gather Memory Fetch
Story 2.04 — Track B

scatter_gather_memory() — Concurrent 3-layer memory fetch.
L1: Redis working state (task-keyed fast lookup)
L2: KG entity topology (file-based JSONL scan)
L3: Qdrant scar similarity (semantic/cosine search)

All 3 fetches fire concurrently via asyncio.gather.
Each is wrapped in asyncio.wait_for for individual timeout enforcement.
On any fetch failure or timeout → that field gets None (partial result is valid).
Total latency is wall-clock time across all 3 concurrent fetches.
    N)Path)OptionalList   )MemoryContextKG_ENTITIES_DIRz./mnt/e/genesis-system/KNOWLEDGE_GRAPH/entities   x   task_idtarget_entitiesintent_string
timeout_msreturnc           	        K   t        j                         }|dz  }t        j                  t        j                  t        |       |      t        j                  t        |      |      t        j                  t        |      |      d       d{   }t        j                         |z
  dz  }d }t         ||d          ||d          ||d	         |
      S 7 Kw)a&  
    Scatter-gather across 3 memory layers concurrently.

    Fires L1, L2, and L3 fetches at the same time using asyncio.gather.
    Each fetch has its own asyncio.wait_for timeout so a slow layer cannot
    block the others beyond the shared wall-clock budget.

    Args:
        task_id:         Identifier for the current agent task (L1 lookup key).
        target_entities: Entity names/IDs to search for in the KG (L2).
        intent_string:   Natural-language intent used as L3 similarity query.
        timeout_ms:      Per-fetch timeout in milliseconds (default 45 ms).

    Returns:
        MemoryContext with whatever data arrived before timeout.
        Any layer that failed or timed out contributes None for its field.
        latency_ms always records the real wall-clock elapsed time.
    g     @@)timeoutT)return_exceptionsNc                 (    t        | t              ry| S )z>Return result value or None on any exception (timeout, error).N)
isinstanceBaseException)results    3/mnt/e/genesis-system/core/memory/scatter_gather.py_safez$scatter_gather_memory.<locals>._safeP   s    fm,    r   r      )working_statekg_topologylearned_constraints
latency_ms)	time	monotonicasynciogatherwait_for_fetch_redis_l1_fetch_kg_l2_fetch_qdrant_l3r   )	r   r   r   r   start	timeout_sresultslatencyr   s	            r   scatter_gather_memoryr+   ,   s     0 NNEV#INN19Eo6	J)-8)L	 G ~~%'61G GAJ''!*%!'!*-	 s   BC CAC c                 F  K   	 ddl m} t        j                  dd      } ||r|nd      }|j	                  |        d{   }|y|j
                  rdj                  |j
                        nd}d|j                   d	| d
|j                   S 7 O# t        $ r Y yw xY ww)z
    Fetch working state from Redis L1.

    Returns a human-readable summary string of the RedisL1State, or None
    on cache miss or any connection/import error.
    r   )RedisL1Client	REDIS_URL Nz, nonezTask: z	, Focus: z, Hypothesis: )
redis_l1_schemar-   osgetenv	get_statefocus_entitiesjoinr   current_hypothesis	Exception)r   r-   	redis_urlclientstatefocuss         r   r$   r$   b   s     2IIk2.	Iy4@&&w//=383G3G		%../VU]]O $W  3346	
	 0  sG   B!<B  BB B!AB B!B 	BB!BB!c                   K   | sy| D cg c]  }|j                          }}	 t        j                         syg }t        t        j	                  d            D ]I  }t        |      t        k\  r n4	 t        |ddd      5 }|D ]  }|j                         }|s	 t        j                  |      }t        |j                  dd            j                         }t        |j                  d	d            j                         }	|D ]{  }
|
|v s|
|	v s|j                  dd
      }|j                  dd
      }t        |j                  d|j                  dd                  dt         }|j                  | d| d|         n t        |      t        k\  s n ddd       L |syddj%                  |      z   S c c}w # t        j                  $ r Y Sw xY w# 1 sw Y   BxY w# t         t"        f$ r Y w xY w# t&        $ r Y yw xY ww)a  
    Fetch KG topology for matching entities from JSONL files in KG_ENTITIES_DIR.

    Scans all *.jsonl files, matching lines whose 'id' or 'name' field
    contains any of the target_entities (case-insensitive substring match).
    Returns up to _KG_MAX_MATCHES formatted entity summaries, or None if
    target_entities is empty, the directory is missing, or no matches found.
    Nz*.jsonlrzutf-8replace)encodingerrorsidr/   name?typedescriptioncontent: u    — zKG Topology:

)lowerr   existssortedgloblen_KG_MAX_MATCHESopenstripjsonloadsJSONDecodeErrorstrget_KG_EXCERPT_LENappendOSErrorPermissionErrorr6   r8   )r   elower_termsmatches
jsonl_filefhlineentryentry_id
entry_nametermeidetypedescs                 r   r%   r%   ~   s      '6617796K6-%%' !5!5i!@A 	J7|.*cGIN "RT " "#zz|#$%$(JJt$4E $'uyyr':#;#A#A#C%(62)>%?%E%E%G
$/ 	&D#x/4:3E&+iic&:(-		&#(>'*$)IIm.3ii	2.F%H(" #3?(4 !(#buTF/K L %	& w<?:!1""		B $))G"444Y 7&  $33 %$%" "4 _-   s   HF=HG> H9G> 3G(GG2AGBGGG(#G> (H)G> <HG	GG	GG%	!G((G;7G> :G;;G> >	H
H	H

Hc           
        K   | sy	 ddl }ddlm} t        j                  dd      }t        j                  dd      } |||r|ndd	      }|j                  | j                               j                         }t        d
      D cg c]  }t        ||t        |      z           dz  ! }}|j                  d|d      }	|	syg }
|	D ]T  }|j                  xs i }|j                  dd      }|
j                  d|j                   d| d|j                   dd       V ddj#                  |
      z   S c c}w # t$        $ r Y yw xY ww)a?  
    Fetch learned constraints from Qdrant L3 'aiva_scars' collection.

    Uses a deterministic hash-based pseudo-vector so this layer works
    without an embedding model at query time. In production, replace the
    hash vector with real embeddings.

    Returns up to 2 scar descriptions, or None on any error.
    Nr   )QdrantClient
QDRANT_URLz/https://qdrant-b3knu-u50607.vm.elestio.app:6333QDRANT_API_KEYr/   r   )urlapi_keyr   i   g     o@
aiva_scars)collection_namequery_vectorlimitrF   unknownzScar rH   z (score=z.2f)zLearned Constraints:
rI   )hashlibqdrant_clientri   r2   r3   sha256encodedigestrangefloatrN   searchpayloadrV   rX   rB   scorer6   r8   )r   rt   ri   
qdrant_url
qdrant_keyr:   rx   ivectorr)   scarsr>   r|   rg   s                 r   r&   r&      se     *.YY=

 YY/4
",J$
  4 4 67>>@ 3Z
 &S[)*U2
 

 --(   
  	GAiio2G;;}i8DLL5bhqwwsm1EF	G
 ($))E*:::)
,  sH   EA?D7 $D2*D7 EA.D7 1E2D7 7	E EEE)-   )__doc__r!   rR   r2   r   pathlibr   typingr   r   zero_amnesia_enveloper   r3   r   rO   rW   rU   rz   r+   r$   r%   r&    r   r   <module>r      s      	   ! 0 yryy4    	//#Y/ / 	/
 /l3 8C= 8<S	 <hsm <F7# 7(3- 7r   