
    i+                         S r SSKrSSKrSSKrSSKJr  SSKJrJrJ	r	  SSK
r
Sr " S S\
R                  5      r " S S	5      rg)
af  
MEMORY CORE - Meta-Capability [PHI-FRACTAL]
Eden's self-model with tiered memory and recursive pattern understanding
Learns about patterns (recursive) through memory analysis

This adds recursive meta-capabilities to MemoryCore:
1. Self-model tracks patterns from memory (recursive)
2. Predicts based on observed patterns
3. Recognizes repeatable behaviors
    N)Path)DictListAnygV9?c                   B   ^  \ rS rSrSrU 4S jrS rSS jrS rSr	U =r
$ )	SelfModelUpdateThread   z9Background thread to update Eden's self-model from memoryc                 :   > [         TU ]  SS9  Xl        SU l        g )NT)daemonSelfModelUpdater)super__init__agentname)selfagent_instance	__class__s     B/home/james-whalen/Eden_Sovereign/CORE/eden_metacap_MEMORY_CORE.pyr   SelfModelUpdateThread.__init__   s    %#
&	    c                      [         R                  " S5         U R                  5         M)  ! [         a
  n S nANS nAff = f)Ni  )timesleep_update_from_memory	Exception)r   es     r   runSelfModelUpdateThread.run   s<    JJt((*	 
  s   + 
??c           	         [         R                  " S5      nUR                  S5      R                  5       nUR	                  5         U(       d  gU Vs/ s H'  oDS   (       d  M  [
        R                  " US   5      PM)     nn[        U5      S:  a  g0 0 S.n0 n0 nU GH9  n	U	R                  S5      n
U
(       a}  UR                  U
S5      S-   Xz'   U	R                  S	/ 5      nU HN  nUR                  S
5      nUR                  S5      nU(       d  M.  U(       d  M7  UR                  US5      U-   X'   MP     US:  d  M  U	R                  S0 5      nUR                  S	/ 5      nU Hn  nUR                  S5      nUR                  S5      nU(       d  M.  U(       d  M7  US:  d  M?  U R                  R                  R                  SUSUU0000US-
  S9  Mp     GM<     U R                  R                  UUS.US.SS9  gs  snf )zEUpdate self-model from memory patterns with recursive meta-processing/Eden/MEMORY/agent_longterm.dbz=SELECT content FROM memories ORDER BY timestamp DESC LIMIT 50Nr      )behavior_frequencyresource_trends	operation   toolsresourceimpactoperation_datameta_resourcemeta_impact   resource_metamodelimpact_patterns)state_updaterecursion_depth)operation_frequencyresource_impact_trends)past_behavior_analysisdetected_patterns   )sqlite3connectexecutefetchallclosejsonloadslengetr   memory_memory_update_self_model)r   r0   connrowsrpast_actionspredicted_patternsoperation_countsresource_changesactionopr&   toolr'   r(   op_datar*   r+   s                     r   r   )SelfModelUpdateThread._update_from_memory'   s    ?@||K

(* 	 	

26?$QA$(

1Q4($?|q  #%!

 "FK(B'7';';B'BQ'F $ 

7B/!D#xx
3H!XXh/FxFF5E5I5I(TU5VY_5_(2	 " " **%5r:GR0!D$(HH_$=M"&((="9K$}19L

00CC 4$1(9M;;W4&7"* -<a,? D 	 "' #H 	

%% ,<.>+ &8  	& 		
c @s   G<#G<c                 :  ^ [        U5      S:  a  gU Vs/ s H  o"R                  S5      PM     snm[        [        T5      TR                  SS9nTR	                  U5      S:  a1  UTR	                  U5      [        U4S j[        T5       5       5      S.$ gs  snf )z0Detect if same operations appear in prior window   Nr$   )keydefaultr!   c              3   d   >#    U  H%  nTR                  U5      [        T5      -  S -  v   M'     g7f)d   N)countr=   ).0rI   patterns     r   	<genexpr>@SelfModelUpdateThread._detect_common_sequence.<locals>.<genexpr>{   s*     !^Q]2'--"3CL"@3"FQ]s   -0)r$   	frequency
confidence)r=   r>   maxsetrS   min)r   actionsrH   most_commonrU   s       @r   _detect_common_sequence-SelfModelUpdateThread._detect_common_sequenceo   s    w<!9@Av::k*A#g,GMM4H==%)($]];7!!^QTU\Q]!^^ 
  Bs   B)r   r   )r5   )__name__
__module____qualname____firstlineno____doc__r   r   r   r_   __static_attributes____classcell__)r   s   @r   r   r      s!    C'
F
P r   r   c                       \ rS rSrSrS rS rSS\S\S\	4S	 jjr
SS
\\   4S jjrS
\4S jrS
\4S jrS
\4S jrSrg)
MemoryCore   z/Expanded memory with self-modeling capabilitiesc                     [         R                  " S5      U l        U R                  5         [	        U 5      U l        U R
                  R                  5         g )Nr    )r6   r7   	memory_db_initialize_memory_tablesr   self_model_threadstartr   s    r   r   MemoryCore.__init__   s@     )IJ&&( "7t!<$$&r   c                 p    U R                   R                  S5        U R                   R                  S5        g)zEnsure memory tables exista  
            CREATE TABLE IF NOT EXISTS memories (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp REAL DEFAULT (strftime('%s','now')),
                content TEXT,
                importance REAL DEFAULT 1.0
            )
        zUCREATE INDEX IF NOT EXISTS idx_memories_timestamp 
            ON memories(timestamp)N)rl   r8   rp   s    r   rm   $MemoryCore._initialize_memory_tables   s2       	 	  & 	'r   Ncontentcontext
importancec                 x   U(       d  0 nUR                  S5      UR                  S/ 5      UR                  S0 5      UR                  S5      UR                  S/ 5      S.nU R                  R                  S[        R                  " U5      U[
        -  45        U R                  R                  5         SUS	S
  S3$ )z)Store memory with phi-weighted importancer$   r&   	variablesanalysisoutcomes)r$   r&   rx   ry   rz   zEINSERT INTO memories (content, importance) 
            VALUES (?, ?)zMemory stored: N2   z...)r>   rl   r8   r;   dumpsPHIcommit)r   rt   ru   rv   mem_datas        r   store_memoryMemoryCore.store_memory   s     G ![1[["- ["5J/J3
 	  #zz(3Z#5EF	H "c22r   returnc                 
   U R                   R                  S[        U5      45      n/ nUR                  5        H.  n [        R
                  " US   5      nUR                  U5        M0     U$ ! [         a  n SnAMD  SnAff = f)zRetrieve recent memoriesz=SELECT content FROM memories ORDER BY timestamp DESC LIMIT ? r   N)rl   r8   intr9   r;   r<   appendr   )r   limitcursormemoriesrowmemr   s          r   get_recent_memoriesMemoryCore.get_recent_memories   sy    ''KcRWj]
 ??$CjjQ($ %   s   *A--
B=Bc                 4    SSSS.U R                  5       0 SS.$ )zReturn Eden's model of herselfTrecursive_phir5   )tiered_storageself_awareness_levelmeta_metacapabilities)active_memory_systemsmemory_statspredicted_behaviorsr0   )_get_memory_statisticsrp   s    r   
self_modelMemoryCore.self_model   s0     #'(7)*&
 !779#% 	
 		
r   c                     U R                   R                  S5      nUR                  5       u  p#[        U5      [	        U5      S.$ )zGet stats about own memoriesz.SELECT COUNT(*), AVG(importance) FROM memories)total_memoriesaverage_importance)rl   r8   fetchoner   float)r   r   totalavgs       r   r   !MemoryCore._get_memory_statistics   s8    ''(XY__&
"%e*E#JOOr   c                 h    U R                  S5      nU R                  5       nSUS    S3SSSS/S	.nU$ )
z3Autobiographical analysis using own memory patterns   zOperated on r   u    resources with φ-weightingz)Tiered recursive storage using phi-ratiosu   4-tire recursion (φ^4)zDistribution modelingzCross-system patterns)self_summarymemory_patternr   improvements_possible)r   r   )r   recentstatsry   s       r   analyze_self_overview MemoryCore.analyze_self_overview   sU     ))"-++- +51A+B*CC_`I$=&=?V%W	
 r   )rl   rn   )Ng      ?)
   )ra   rb   rc   rd   re   r   rm   strr   r   r   r   r   r   r   r   rf    r   r   ri   ri      s_    9''3C 3$ 35 3*tDz  
D 
P Pt r   ri   )re   r6   r;   r   pathlibr   typingr   r   r   	threadingr}   Threadr   ri   r   r   r   <module>r      sG   	     " " gI,, gR^ ^r   