
    >2iP                        U d 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m	Z	 ddl
m
Z
mZ ddlmZ ddlmZ ddlmZmZmZmZmZ  G d	 d
e      Ze G d d             Ze G d d             Ze G d d             Z G d d      Z G d d      Z G d d      Zdaee   ed<   d>dedefdZ e!dk(  rQ e"d        e       Z#e#jI                  ddd      Z% e"d e%d!   d"   d#d$e%d%           e#jI                  d&d'd(      Z% e"d)e%d!   d"   d#d$e%d%           e#jM                  d*d+d,-      Z'e#jQ                  e'd.      \  Z)Z* e"d/e)d#d0e*jV                  d#d1e*jX                  jZ                           e"d2e*j\                   d3e*j^                          e#ja                  d4d5d6d7i      Z1 e"d8e1jd                  d#d1e1jX                  jZ                          e#jg                         Z4 e"d9e4d:    d;e4d<   d#        e"d=       yy)?u3  
AIVA Surprise Memory System
============================
Production implementation of surprise-based memory routing.

Replaces all prior stubs with the full SurpriseDetector from:
    AIVA/queen_outputs/beta/beta_06_surprise_detector.py

The composite_score drives memory tier routing:
    > 0.7  → episodic (long-term storage, axiom candidate)
    0.5-0.7 → notable (working memory with elevated priority)
    < 0.5  → working (short-term, standard eviction policy)

Public API (backward-compatible with all existing callers):
    MemorySystem.evaluate(content, source, domain) -> Dict
    MemorySystem.observe(event_type, actual_outcome, context) -> SurpriseScore
    MemorySystem.reflect(actual) -> List[Dict]
    MemorySystem.get_stats() -> Dict

Queen AIVA Integration:
    This module is the primary surprise signal for:
    - Titan Memory axiom extraction
    - RLM Gateway interaction scoring
    - Memory promotion decisions
    - Learning rate modulation

Author: Genesis System (coronation integration 2026-02-20)
Version: 2.0.0  (real engine — no stubs)
    N)defaultdict)	dataclassfieldasdict)datetime	timedelta)Enum)Path)DictListOptionalAnyTuplec                   4    e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZy)SurpriseLevelz
    Surprise intensity classification.

    Maps to the beta_06 taxonomy with backward-compatible aliases added
    so callers that check for LOW/MEDIUM/HIGH/CRITICAL still work.
    mundanenotable
surprisingshockingparadigmN)__name__
__module____qualname____doc__MUNDANENOTABLE
SURPRISINGSHOCKINGPARADIGM_SHIFTLOWMEDIUMHIGHCRITICAL     -/mnt/e/genesis-system/core/surprise_memory.pyr   r   .   s8      GG"J H N  CF"D Hr%   r   c                   N    e Zd ZU dZeed<   eed<   eed<    ed       Zeed<   y)	
MemoryItemz*A single item stored in the memory system.contentsourcedomainc                  F    t        j                         j                         S N)r   now	isoformatr$   r%   r&   <lambda>zMemoryItem.<lambda>M   s    8<<>3K3K3M r%   )default_factory	timestampN)r   r   r   r   str__annotations__r   r2   r$   r%   r&   r(   r(   G   s$    4LKK+MNIsNr%   r(   c                       e Zd ZU dZeed<   eed<   eed<   eed<   eeef   ed<   eed<   dZ	e
e   ed	<   d
Zeed<   dZe
e   ed<   dZe
e   ed<   y)
Predictionz-A prediction registered for later resolution.prediction_idr+   expected_outcome
confidencecontext
created_atN
expires_atFresolvedactual_outcomeprediction_error)r   r   r   r   r3   r4   floatr   r   r<   r   r=   boolr>   r?   r$   r%   r&   r6   r6   P   sd    7K#s(^O $J$Hd$(NHSM((,huo,r%   r6   c                       e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   dZ
eed<   dZeed<   dZeed	<   ej                  Zeed
<   dZeed<   dZeed<   ddeeef   defdZdefdZy)SurpriseScoreaM  
    Multi-dimensional surprise evaluation.

    All four dimension fields are present for callers that reference them
    by name (the beta_06 set: prediction_error, novelty, impact, rarity)
    as well as the legacy set (violation, novelty, impact, rarity).
    'total' and 'composite_score' are synonyms for the weighted sum.
            r?   noveltyimpactrarity	violationtotalcomposite_scorelevelFshould_promote_memoryshould_generate_axiomNweightsreturnc                    |xs ddddd}| j                   | _        t        d| j                   |d   z  | j                  |d   z  z   | j                  |d   z  z   | j
                  |d	   z  z         | _        | j                  | _        | j                  d
k\  rt        j                  | _
        n| j                  dk\  rt        j                  | _
        n_| j                  dk\  rt        j                  | _
        n:| j                  dk\  rt        j                  | _
        nt        j                  | _
        | j                  dk\  | _        | j                  dk\  | _        | j                  S )z;Compute weighted composite and populate all derived fields.gffffff?g      ?g333333?)r?   rE   rF   rG         ?r?   rE   rF   rG   ?ffffff?      ?333333?)r?   rH   minrE   rF   rG   rI   rJ   r   r   rK   r   r   r   r   rL   rM   )selfrN   s     r&   compute_totalzSurpriseScore.compute_total{   s?    
 $ $ $ $	
 ..!!G,>$??llGI$667kkGH$556 kkGH$556
  $zz ::&55DJZZ4&//DJZZ4&11DJZZ4&..DJ&..DJ%)ZZ4%7"%)ZZ4%7"zzr%   c                 N    t        |       }| j                  j                  |d<   |S )NrK   )r   rK   value)rW   ds     r&   to_dictzSurpriseScore.to_dict   s#    4LZZ%%'
r%   r-   )r   r   r   r   r?   r@   r4   rE   rF   rG   rH   rI   rJ   r   r   rK   rL   rA   rM   r   r3   rX   r\   r$   r%   r&   rC   rC   _   s     "e!GUFEFE Iu E5 OU (00E=0 #(4'"'4'"T#u*%5 " "H r%   rC   c                   Z    e Zd ZdZdZddefdZd Zd Zde	fd	Z
d
edefdZdedefdZy)SurpriseHistorya  
    Tracks historical surprise events.

    Persists to a JSON sidecar so novelty calculations survive session
    restarts.  File location defaults to the genesis data directory but
    gracefully degrades to in-memory-only if the path is not writable.
    z0/mnt/e/genesis-system/data/surprise_history.jsonNhistory_pathc                     t        |xs | j                        | _        g | _        i | _        i | _        d| _        | j                          y )NT)r
   _DEFAULT_PATHr_   eventsdomain_baselinescontent_hashes_persistent_loadrW   r_   s     r&   __init__zSurpriseHistory.__init__   sB     !C1C1CD"$24.0

r%   c                 h   | j                   j                         r}	 t        | j                         5 }t        j                  |      }d d d        j                  dg       dd  | _        |j                  di       | _        |j                  di       | _        y y # 1 sw Y   SxY w# t        $ r Y y w xY w)Nrb   rc   rd   )
r_   existsopenjsonloadgetrb   rc   rd   	Exception)rW   fdatas      r&   rf   zSurpriseHistory._load   s    ##%$++, (99Q<D(*.((8R*@*H*.((3Er*J%*.((3CR*H# &( (
  s)   B% BAB% B"B% %	B10B1c                    | j                   sy 	 | j                  j                  j                  dd       t	        | j                  d      5 }t        j                  | j                  dd  | j                  t        t        | j                  j                               dd        d|d       d d d        y # 1 sw Y   y xY w# t        $ r
 d	| _         Y y w xY w)
NT)parentsexist_okwrj   ix)rb   rc   rd      )indentF)re   r_   parentmkdirrl   rm   dumprb   rc   dictlistrd   itemsrp   )rW   rq   s     r&   _savezSurpriseHistory._save   s    		%$$**4$*Gd''-  		(,EF(;(,(=(=(,T$2E2E2K2K2M-Nuv-V(W Q	        	%$D	%s0   =C A!B6-C 6B?;C ?C CCeventc                 2   | j                   j                  |       |j                  dd      }|j                  dd      }d}|| j                  vr|| j                  |<   n(||z  d|z
  | j                  |   z  z   | j                  |<   | j	                          y )Nr+   generalsurprise_scorerT   皙?   )rb   appendro   rc   r   )rW   r   r+   scorealphas        r&   	add_eventzSurpriseHistory.add_event   s    5!8Y/+S1...,1D!!&) Ud.C.CF.K KK !!&) 	

r%   r)   rO   c           	      8   t        j                  |j                               j                         dd }|| j                  v rI| j                  |   }|dz   | j                  |<   t        dddt        j                  |dz         z   z        S d| j                  |<   y)zENovelty score in [0.1, 0.9] based on whether content was seen before.N   r   r   rQ   rR   )hashlibmd5encode	hexdigestrd   maxmathlog)rW   r)   chcounts       r&   get_noveltyzSurpriseHistory.get_novelty   s    [[)*446s;$$$''+E&+aiD#sC1txx	':#:;<<"#Br%   r+   c                 :    | j                   j                  |d      S )NrT   )rc   ro   )rW   r+   s     r&   get_domain_baselinez#SurpriseHistory.get_domain_baseline   s    $$((55r%   r-   )r   r   r   r   ra   r3   rh   rf   r   r   r   r@   r   r   r$   r%   r&   r^   r^      sW     GMS 	% t 3 5 6# 6% 6r%   r^   c                   J   e Zd ZdZi dddddddd	d
ddddddd	dddd	dddddddd	dddddddd	d	ddZg dZd5d efd!Z	 	 	 d6d"ed#ed$ed%e	d&e
d'efd(Zd)ed*ed'eeef   fd+Zd,ed-ed$ed'efd.Z	 	 d7d/ed"ed%e	d0ed'ef
d1Zd/ed'efd2Zd"ed'efd3Zd'e	fd4Zy)8_SurpriseDetectorz
    Internal full-fidelity surprise detector.

    Implements the beta_06 multi-dimensional algorithm with prediction
    resolution, domain baselines, and rarity tracking.
    errorg?criticalrR   successrU   failurerS   
unexpectedwarningrT   breakthrough333333?
discoveredpatentg333333?revenue	validatedg?invalidkinang?aivaqueeng      ?deployshutdown)restartpaymentstripelaunch)r   unknown	unhandled	assertiontimeoutretryfallbackoverridebypass
workaroundhacktodofixmeNr_   c                 2    t        |      | _        i | _        y r-   )r^   historypredictionsrg   s     r&   rh   z_SurpriseDetector.__init__  s    *<824r%   r+   r8   r9   r:   ttl_minutesrO   c                 ~   t        j                  | d| dt        j                         j	                          j                               j                         d d }t        j                         }t        |||||xs i |j	                         |t        |      z   j	                               | j                  |<   |S )N:   )minutes)r7   r+   r8   r9   r:   r;   r<   )
r   r   r   r.   r/   r   r   r6   r   r   )rW   r+   r8   r9   r:   r   pred_idr.   s           r&   make_predictionz!_SurpriseDetector.make_prediction  s     ++ha()8<<>+C+C+E*FGNNP

)+cr lln$.!-!Mr}}i<<GGI%
! r%   r7   r>   c                 <   || j                   vr| j                  |di       }d|fS | j                   |   }| j                  |j                  ||j                        }d|_        ||_        ||_        | j                  ||j                  |j                  |      }||fS )Nr   rT   T)r?   )
r   evaluate_calc_prediction_errorr8   r9   r=   r>   r?   r+   r:   )rW   r7   r>   r   pred
pred_errors         r&   resolve_predictionz$_SurpriseDetector.resolve_prediction1  s    
  0 00MM.)R@E:.00!!>4??

 #, *DKK'  
 5  r%   expectedactualc                 2   |j                         }|j                         }||k(  ryt        |j                               }t        |j                               }|r|syt        ||z        }t        ||z        }	||	z  }
d|
z
  }t	        d|dd|z  z   z        S )NrD   rT   rQ   )lowersetsplitlenrV   )rW   r   r   r9   	exp_lower	act_lower	exp_words	act_wordsoverlaprI   
similarity
base_errors               r&   r   z(_SurpriseDetector._calc_prediction_errorH  s     NN$	LLN		!	)*		)*		Y./Y./u_
:%
3
cC*,<&<=>>r%   r)   r?   c                    |xs i }|,| j                   j                  |      }t        d|z
        dz   }| j                   j                  |      }| j	                  |      }| j                  |      }t        |||||      }	|	j                          | j                   j                  t        j                         j                         ||d d |	j                  |	j                  j                  d       |	S )NrT   rU   )r?   rH   rE   rF   rG   d   )r2   r+   content_previewr   rK   )r   r   absr   _calc_impact_calc_rarityrC   rX   r   r   r.   r/   rI   rK   rZ   )
rW   r)   r+   r:   r?   baselinerE   rF   rG   r   s
             r&   r   z_SurpriseDetector.evaluate^  s     -R#"ll>>vFH"3>2T9,,**73##G,##F+-&
 	'||~779%&t}${{${{00 
 	 r%   c                     |j                         }d}| j                  j                         D ]  \  }}||v st        ||      } |S )NrU   )r   _IMPACT_INDICATORSr~   r   )rW   r)   cl
max_impactkwvals         r&   r   z_SurpriseDetector._calc_impact  sO    ]]_
..446 	2GBRx S1
	2 r%   c                     | j                   j                  D cg c]  }|j                  d      |k(  s| }}t        |      dk  ryt	        ddt        |      dz  z
        S c c}w )Nr+   
   rT   g?rQ   r   )r   rb   ro   r   r   )rW   r+   edomain_eventss       r&   r   z_SurpriseDetector._calc_rarity  sh    ||**
aeeHo.GA
 
 }"4s=1C7788
s
   A#A#c                    | j                   j                  }|sddg i dS t        d |D              t        |      z  }t	        t
              }|D ]  }||j                  dd      xx   dz  cc<   ! t        |      |t        | j                   j                  j                               | j                   j                  t        |      t        | j                  j                         D cg c]  }|j                  r| c}      dS c c}w )	Nr   rT   )total_eventsavg_surprisedomainslevel_distributionc              3   @   K   | ]  }|j                  d d        yw)r   rT   N)ro   ).0r   s     r&   	<genexpr>z._SurpriseDetector.get_stats.<locals>.<genexpr>  s     ?1!%%(#.?s   rK   r   r   )r   r   r   rc   r   active_predictions)r   rb   sumr   r   intro   r}   rc   keysr|   r   valuesr=   )rW   rb   avglevel_countsr   ps         r&   	get_statsz_SurpriseDetector.get_stats  s    $$ ! #&(	  ???#f+M'23'7 	9Aw	23q83	9 "%V!$!%dll&C&C&H&H&J!K!%!>!>"&|"4"% ,,335HqQZZH#	
 		
 Is   (D:Dr-   rS   N<   )NN)r   r   r   r   r   _RARITY_SIGNALSr3   rh   r@   r   r   r   r   rC   r   r   r   r   r   r   r$   r%   r&   r   r      s   		(T	3<t		*T	3<t	 		 +T	 4<t	 			 *T		 4=t		 		 %T	 4;t	 		 )T	 @DTt	O5S 5    	
   
0!! ! 
um#	$	!.?? ? 	?
 
?4 "&!! ! 	!
  ! 
!FC E 93 95 9
4 
r%   r   c                       e Zd ZdZdZddefdZdededed	efd
Z	 ddededed	e	fdZ
ded	ee   fdZd	efdZ	 	 	 ddededededed	efdZdeded	eee	f   fdZy)MemorySystemu  
    Surprise-based memory routing system.

    Drop-in replacement for the previous stub.  All prior callers
    (GenesisKernel, RLM Gateway, Titan Memory, orchestrator) continue
    to work via the same three-method API:

        evaluate(content, source, domain) → Dict
        observe(event_type, actual_outcome, context) → SurpriseScore
        reflect(actual) → List[Dict]
        get_stats() → Dict

    The underlying engine is the full beta_06 SurpriseDetector.
    r   Npersistence_pathc                 P    t        |      | _        g | _        t               | _        y r-   )r   _enginememoriesr   _seen_content_hashes)rW   r  s     r&   rh   zMemorySystem.__init__  s     *+;<*,*-%!r%   r)   r*   r+   rO   c                 
   |rt        |      | j                  k  r5t        dt        j                        }d|_        |j                         ddS | j                  j                  ||      }t        j                  |j                               j                         }| j                  j                  |       | j                  j!                  t#        |||             |j
                  dkD  rdnd}|j                         |dS )z
        Evaluate content surprise and route to memory tier.

        Returns:
            {
                "score": SurpriseScore.to_dict(),
                "tier":  "episodic" | "working"
            }
        r   )rJ   rK   working)r   tier)r)   r*   r+   rS   episodic)r   _MIN_CONTENT_LENGTHrC   r   r   rI   r\   r  r   r   r   r   r   r  addr  r   r(   )rW   r)   r*   r+   r   r   r  s          r&   r   zMemorySystem.evaluate  s     #g,)A)AA!$m>S>STEEK"]]_i@@%%gv6 [[)*446!!%%b)ZvVW"[[4/zY$77r%   
event_typer>   r:   c                 t    |xs |}|xs i j                  d|      }| j                  j                  |||      S )zy
        Observe an event and return surprise score.

        Compatible with GenesisKernel.observe() signature.
        r+   )ro   r  r   )rW   r  r>   r:   r)   r+   s         r&   observezMemorySystem.observe  s=     !.J=b%%h
;||$$Wfg>>r%   r   c                     g S )z-Legacy stub kept for interface compatibility.r$   )rW   r   s     r&   reflectzMemorySystem.reflect  s    	r%   c                 6    | j                   j                         S r-   )r  r   )rW   s    r&   r   zMemorySystem.get_stats  s    ||%%''r%   r8   r9   r   c                 @    | j                   j                  |||||      S )z-Register a prediction; returns prediction_id.)r  r   )rW   r+   r8   r9   r:   r   s         r&   r   zMemorySystem.make_prediction  s'     ||++$j';
 	
r%   r7   c                 :    | j                   j                  ||      S )z;Resolve a prediction; returns (error_score, SurpriseScore).)r  r   )rW   r7   r>   s      r&   r   zMemorySystem.resolve_prediction  s     ||..}nMMr%   r-   r   )r   r   r   r   r
  r3   rh   r   r   rC   r  r   r  r   r@   r   r   r   r   r$   r%   r&   r   r     s    0 08 8S 8# 8$ 8: 	?? ? 	?
 
?c d4j (4 (  

 
 	

 
 
 

NN N 
um#	$	Nr%   r   _default_memory_systemr  rO   c                 0    t         t        |       a t         S )z/Return the module-level singleton MemorySystem.)r  r   )r  s    r&   get_memory_systemr    s     %!-.>!?!!r%   __main__u2   === AIVA Surprise Memory System — Self-Test ===
zThe sky is blue today.testr   zRoutine:  score=r   rJ   z.3fz  tier=r  zOCRITICAL error: AIVA server deployment failed unexpectedly during revenue call.system
operationszCritical: score=
deploymentz!Deployment completed successfullyr   )r+   r8   r9   zSCritical failure: deployment rolled back due to unexpected database migration errorzPredict:  error=z  score=z  level=z          promote_memory=z  gen_axiom=
call_endedz Caller booked a demo appointmentr+   saleszObserve:  score=z
Stats:    total_events=r   z  avg_surprise=r   z
Self-test PASSEDr-   )5r   r   rm   r   collectionsr   dataclassesr   r   r   r   r   enumr	   pathlibr
   typingr   r   r   r   r   r   r(   r6   rC   r^   r   r   r  r4   r3   r  r   printmsr   resultr   r   r   r   r   rI   rK   rZ   rL   rM   r  obsrJ   r   statsr$   r%   r&   <module>r(     s  <    # 0 0 (   3 3!D !2 O O O - - - B B BRH6 H6^o
 o
l^N ^NJ 26 . 5" "| " z	
?@	B [[ &)F 
VG_->?DGFSYNK[
\] [[Y,F 
VG_->?DGFSYNK[
\]   < ! G
 ((]LE5 
U3KxC/@IZIZH[
\]	%e&A&A%B,uOjOjNk
lm **\#ERYGZ
[C	S005Xciioo=N
OP LLNE	%eN&;%<OER`LabeKf
gh	
M r%   