
    	hA                        S r SSKrSSKJr  SSKJrJr  SSKJr   " S S5      r\	S:X  a  \
" S	5        \" 5       r\" S
5       H"  r\R                  S\ 3S\S-  S:g  SS.5        M$     \R                  5         \
" S\" \R#                  S5      5       35        \
" S\" \R%                  5       5       35        \
" S5        gg)zu
Consolidated Memory - Human-like memory that scales
Recent: Detailed | Old: Summarized | Important: Never forgotten
    N)Path)datetime	timedelta)defaultdictc                   ^    \ rS rSrS rS rS rS rS rS r	SS jr
S	 rSS
 jrS rS rSrg)ConsolidatedMemory
   c                 N    / U l         0 U l        / U l        U R                  5         g )N)recent_memoriesconsolidated_memoriesimportant_memoriesload_memoryselfs    F/home/james-whalen/eden-agi-project/core/memory/consolidated_memory.py__init__ConsolidatedMemory.__init__   s&    !%'""$    c                 H   [        S5      nUR                  5       (       aq  [        US5       n[        R                  " U5      nUR                  S/ 5      U l        UR                  S0 5      U l        UR                  S/ 5      U l        S S S 5        g g ! , (       d  f       g = f)Ndata/consolidated_memory.jsonrrecentconsolidated	important)	r   existsopenjsonloadgetr   r   r   )r   mem_filefdatas       r   r   ConsolidatedMemory.load_memory   s    78??h$yy|'+xx"'=$-1XXnb-I**.((;*C'	 %$ $$s   AB
B!c                    [        S5      nUR                  R                  SS9  [        US5       n[        R
                  " U R                  U R                  U R                  S.USS9  S S S 5        g ! , (       d  f       g = f)Nr   T)exist_okw)r   r   r      )indent)	r   parentmkdirr   r   dumpr   r   r   )r   r    r!   s      r   save_memoryConsolidatedMemory.save_memory   sm    78t,(C AII.. $ : :!44 	 !  s   9A33
Bc                     SnUR                  SS5      (       d  US-  nSUR                  SS5      R                  5       ;   a  US-  nUR                  S	S
5      nUS;   a  US-  n[        US5      $ )z)Score how important this memory is (0-10)   successTr'   breakthroughtask    	task_typegeneral)error_recoveryself_improvementr	   )r   lowermin)r   
experiencescorer5   s       r   calculate_importance'ConsolidatedMemory.calculate_importance$   sv     ~~i..QJEZ^^FB7==??QJENN;	:	>>QJE5"~r   c                    [         R                  " 5       R                  5       US'   U R                  U5      US'   U R                  R                  U5        [        U R                  5      S:  aQ  U R                  R                  S5      nUS   S:  a  U R                  R                  U5        OU R                  U5        U R                  5         g)z%Add new experience with smart storage	timestamp
importanced   r      N)r   now	isoformatr=   r   appendlenpopr   _consolidate_old_memoryr,   )r   r;   olds      r   add_experience!ConsolidatedMemory.add_experience5   s    "*,,.":":"<
;#'#<#<Z#H
<  	##J/ t##$s*&&**1-C < A%''..s3 ,,S1r   c                 j   [         R                  " US   5      nUR                  S5      nX0R                  ;  a"  SSS[	        [
        5      / S.U R                  U'   U R                  U   nUS==   S-  ss'   UR                  SS5      (       a  US	==   S-  ss'   OUS
==   S-  ss'   UR                  SS5      nUS   U==   S-  ss'   US   S:  a_  US   R                  UR                  SS5      UR                  SS5      S.5        [        US   5      S:  a  US   R                  S5        ggg)z'Compress old memory into weekly summaryr@   %Y-W%Ur   )experiences	successesfailures
task_typeskey_learningsrO      r0   FrP   rQ   r5   r6   rR   rA      rS   r2   Unknownoutcome)r2   rW   r	   N)
r   fromisoformatstrftimer   r   intr   rF   rG   rH   )r   memoryr@   week_keysummaryr5   s         r   rI   *ConsolidatedMemory._consolidate_old_memoryJ   sE   **6++>?	%%h/555 )#.!#4D&&x0 ,,X6!#::i''K A% J1$JJ{I6	i(A-( ,1$O$++

695!::i;- 
 7?+,r1(,,Q/ 2 %r   c                 "    U R                   U* S $ )zGet detailed recent memoriesN)r   )r   ns     r   recall_recent ConsolidatedMemory.recall_recentm   s    ##QBC((r   c                     U R                   $ )z+Get memories that should never be forgotten)r   r   s    r   recall_important#ConsolidatedMemory.recall_importantq   s    &&&r   c                     [         R                  " 5       [        US9-
  nUR                  S5      nU R                  R                  US5      $ )z'Get consolidated summary of a past week)weeksrN   N)r   rD   r   rY   r   r   )r   	weeks_agotarget_dater\   s       r   get_weekly_summary%ConsolidatedMemory.get_weekly_summaryu   s@    llnyy'AA''1))--h==r   c                 0   [        S U R                  R                  5        5       5      n[        U R                  5      [        U R
                  5      U[        U R                  5      [        U R                  5      U-   [        U R
                  5      -   S.$ )z&How much is remembered vs consolidatedc              3   ,   #    U  H
  nUS    v   M     g7f)rO   N ).0weeks     r   	<genexpr>6ConsolidatedMemory.get_memory_stats.<locals>.<genexpr>}   s      !
; ;s   )recent_detailedimportant_preservedconsolidated_experiencesconsolidated_weekstotal_experiences)sumr   valuesrG   r   r   )r   total_consolidateds     r   get_memory_stats#ConsolidatedMemory.get_memory_stats{   s      !
2299;!
 
  #4#7#78#&t'>'>#?(:"%d&@&@"A!$T%9%9!:=O!ORUVZVmVmRn!n
 	
r   c                    [        S5        [        S5        [        S5        U R                  5       n[        S5        [        SUS    S35        [        SUS	    S35        [        S
US    SUS    S35        [        SUS    S35        [        S5        [        S5        [        S5        [        S5        [        S5        [        S5        g)z*Show how memory stays manageable over timezG
======================================================================u*   🧠 CONSOLIDATED MEMORY - SCALABLE DESIGNzF======================================================================u   
📊 Current Memory Usage:z   Recent (detailed):     rs   z experiencesz   Important (preserved): rt   z   Consolidated (summary): ru   u    → rv   z weeksz   Total tracked:         rw   u   
📈 Projected Scaling:zE   After 1 year:  ~52 weeks consolidated + 100 recent + ~50 importantzH   After 10 years: ~520 weeks consolidated + 100 recent + ~500 importantz/   Storage growth: O(weeks) not O(experiences)!u-   
✅ Memory stays fast and manageable foreverN)printr{   )r   statss     r   demonstrate_scaling&ConsolidatedMemory.demonstrate_scaling   s    m:;f%%',.*51B+C*DLQR*51F+G*HUV+E2L,M+NeTYZnToSppvwx*51D+E*FlST)+UWXZ?A>@fr   )r   r   r   N)r	   )r   )__name__
__module____qualname____firstlineno__r   r   r,   r=   rK   rI   ra   rd   rj   r{   r   __static_attributes__rn   r   r   r   r   
   s;    D"*!0F)'>
r   r   __main__zCONSOLIDATED MEMORY TEST   zTask testr/   	completed)r2   r5   r0   rW   u   
💡 Recent memories: u   🌟 Important memories: u$   
✅ CONSOLIDATED MEMORY OPERATIONAL)__doc__r   pathlibr   r   r   collectionsr   r   r   r~   r[   rangeirK   r   rG   ra   rd   rn   r   r   <module>r      s      ( #T Tl z	
$%!F 3ZA3K1uz"	
 	   	$S)=)=a)@%A$B
CD	%c&*A*A*C&D%E
FG	
12) r   