/ core / resonance / __pycache__ / scoring.cpython-314.pyc
scoring.cpython-314.pyc
  1  +
  2  ��gioM�
  3  �R�Rt^RIHtHt^RIHtHt^RIHtHtH	t	H
  4  t
  5  ^RIt^RIH
t
Ht]!RR44t]!RR	44t]!R
  6  R44t!RR
4t]R8XEd>^RIt^RIt]P,P/]P0!4R4t]
!]4t]!R4]P9RRRR.R7t]P9RRR.R7t]P9RRRR.R7t]PA]PB4]PA]PB4]PA]PB4]!]4t"]!RRR7t#]]]3F�t$]"PK]$PB]#4t&]!R]$PNR,R 24]!R!]&PLR"
24]!R#]&PPPRR"
24]!R$]&PPPTR"
24]!R%]&PPPVR"
24]!4K�	]!R&4]"PYR]#^R'7t-].!]-^4FOwt/t&]Pa]&PB4t$]!R(]/R)]$PNR,R*]&PLR"
R+24KQ	]Pb!]4]!R,4R#R#)-a|
  7  Resonance Scoring Engine
  8  
  9  Computes multi-dimensional relevance scores that go beyond simple recency.
 10  What "resonates" isn't just what's recent - gravity wells from years ago
 11  can exert stronger pull than yesterday's ideas.
 12  
 13  Key principle: Resonance is computed from multiple factors, weighted
 14  and combined. The weights can be tuned per operator based on their
 15  selection behavior.
 16  )�	dataclass�field)�datetime�	timedelta)�Optional�List�Dict�AnyN)�GodDatabase�Bulletc�ha�]tRt^toRtRtRtRtRtRt	]
 17  !]R7t]
 18  !]R7t
V3RltRtVtR#)�ResonanceContextzn
 19  Context for computing resonance scores.
 20  
 21  The same bullet can have different resonance in different contexts.
 22  N��default_factoryc��<�V^8�dQh/S[S[,;R&S[S[,;R&S[S[,;R&S[S[,;R&S[S[,;R&S[S[,;R&S[S[,;R&#)��
 23  session_id�
current_topic�current_altitude�time_of_day�day_of_week�recent_bullet_uuids�
recent_topics)r�str�intr)�format�
__classdict__s"��core/resonance/scoring.py�__annotate__�ResonanceContext.__annotate__s|������
�$���C�=�'���s�m�*���#��%���#��%��"�c��@�#�$��9�:�%��)�__name__�
 24  __module__�__qualname__�__firstlineno__�__doc__rrrrrr�listrr�__annotate_func__�__static_attributes__�__classdictcell__�rs@rr
r
sF�����!%�J�#'�M�&*��"&�K�!%�K�&+�4�%@��$�T�:�M�%�r r
c�za�]tRt^*toRtRtRtRtRtRt	Rt
 25  RtRtRt
RtRtRtRtRtV3RlRltV3RltRtVtR#)	�ResonanceFactorsz�
 26  The components that contribute to resonance score.
 27  
 28  Each factor is computed independently and then combined
 29  using configurable weights.
 30  ���?c�6<�V^8�dQhRS[S[S[3,/#�r�return�rr�float)rrs"�rr�ResonanceFactors.__annotate__Ls���
 31  �
 32  ��c�5�j�)�
 33  r c�V�RVPRVPRVPRVPRVPRVP
 34  RVPRVPR	VPR
 35  VPRVPRVPR
VPRVP/#)zConvert to dictionary.�recency�temporal_pattern�gravity_well_proximity�prediction_involvement�edge_density�altitude_match�topic_relevance�access_frequency�importance_markers�explicit_flags�
nagging_score�	eeg_focus�eeg_flow�eeg_engagement�r7r8r9r:r;r<r=r>r?r@rArBrCrD��selfs&r�to_dict�ResonanceFactors.to_dictLs���
�t�|�|��� 5� 5�$�d�&A�&A�$�d�&A�&A��D�-�-��d�1�1��t�3�3��� 5� 5� �$�"9�"9��d�1�1��T�/�/�������
�
��d�1�1�
 36  �	
 37  r c��<�V^8�dQh/S[;R&S[;R&S[;R&S[;R&S[;R&S[;R&S[;R&S[;R&S[;R	&S[;R
 38  &S[;R&S[;R&S[;R
&S[;R&#)rr7r8r9r:r;r<r=r>r?r@rArBrCrD)r4)rrs"�rrr5*s���������!��"�'��"�'�����$��%�&� �'�,�!�-�.�#�/�4��5�6��7�<��=�>��?�@��Ar r!N)r"r#r$r%r&r7r8r9r:r;r<r=r>r?r@rArBrCrDrHr(r)r*r+s@rr-r-*ss������G�!��%(��$'���L� �N� �O�"�� #�� �N��M��I��H��N�
 39  �
 40  �E�r r-c�Za�]tRt^`toRt]!]PR7tRt	V3Rlt
 41  RtVtR#)�ResonanceScorez"
 42  Computed resonance for a bullet.
 43  rNc�f<�V^8�dQh/S[;R&S[;R&S[;R&S[;R&S[S[,;R&#)r�uuid�score�factors�computed_at�context)rr4r-rrr
)rrs"�rr�ResonanceScore.__annotate__`sJ����
 44  �I���L�
�����?���&�
'�.�r r!)
r"r#r$r%r&rr�nowrQrRr(r)r*r+s@rrLrL`s&�����"�(�,�,�?�K�*.�G��r rLc��a�]tRt^ltoRtRRRRRRRRR	RR
 45  RRRRR
RR
RRRRRRRRRR/tRRRRRRRRRRRR/tR.RGOR.RHOR .RIOR!.RJO/tRKV3R#lR$lltRKV3R%lR&llt	RLV3R'lR(llt
 46  RKV3R)lR*lltV3R+lR,ltV3R-lR.lt
V3R/lR0ltV3R1lR2ltV3R3lR4ltV3R5lR6ltV3R7lR8ltV3R9lR:ltV3R;lR<ltV3R=lR>ltV3R?lR@ltV3RAlRBltV3RClRDltREtRFtVtR"#)M�ResonanceScoringEnginez�
 47  Computes resonance scores for bullets based on multiple factors.
 48  
 49  The engine uses configurable weights that can be tuned per operator
 50  based on their selection behavior (see ResonanceWeightTuner).
 51  r7g���Q��?r8g{�G�z�?r9r:g{�G�z�?r;r<r=r>g���Q��?r?r@rArBrCrD�	#decision皙�����?�
 52  #principleg�������?z#insight�ffffff�?z
 53  #important�333333�?z	#priorityz#blocker�
philosophical�	strategic�tactical�operationalNc�L<�V^8�dQhRS[RS[S[S[S[3,,/#)r�god_db�weights)r
 54  rrrr4)rrs"�rr�#ResonanceScoringEngine.__annotate__�s-���
 55  �
 56  ��
 57  ��$�s�E�z�*�+�
 58  r c�~�WnT;'gVPP4VnRRRRRR/VnR#)z�
 59  Initialize the resonance scoring engine.
 60  
 61  Args:
 62      god_db: The God Database instance
 63      weights: Optional custom weights (defaults used if not provided)
 64  rBr/rCrDN)ra�DEFAULT_WEIGHTS�copyrb�_eeg_factors)rGrarbs&&&r�__init__�ResonanceScoringEngine.__init__�sB�����=�=�$�"6�"6�";�";�"=���
�����c�/
 65  ��r c�L<�V^8�dQhRS[RS[S[,RS[S[,/#)rrNrRr2)rrr
rL)rrs"�rrrc�s4���6
 66  �6
 67  ��6
 68  ��*�+�6
 69  �
 70  �.�	!�	6
 71  r c�Za�VPPV4pV'gR#T;'g\4p\VP	V4VPW24VP
V4VPV4VPV4VPW24VPW24VPV4VPV4VPV4VPV4VPP!RR4VPP!RR4VPP!RR4R7o\#V3RlVP$P'444p\)R\+R	V44p\-VVS\.P0!4VR
 72  7#)z�
 73  Compute resonance score for a bullet in the current context.
 74  
 75  Args:
 76      uuid: Bullet UUID
 77      context: Optional context for computation
 78  
 79  Returns:
 80      ResonanceScore or None if bullet not found
 81  NrBr/rCrDrEc3�N<"�TFwr\SV4V,x�K	R#5i�N)�getattr)�.0�factor�weightrPs&  �r�	<genexpr>�;ResonanceScoringEngine.compute_resonance.<locals>.<genexpr>�s)����
 82  �"6���
�G�V�$�v�-�-�"6�s�"%r.��?)rNrOrPrQrR)ra�
 83  get_bulletr
r-�_compute_recency�_compute_temporal_pattern�_compute_gravity_proximity�_compute_prediction_involvement�_compute_edge_density�_compute_altitude_match�_compute_topic_relevance�_compute_access_frequency�_compute_importance_markers�_compute_explicit_flags�_compute_nagging_scorerg�get�sumrb�items�max�minrLrrT)rGrNrR�bulletrOrPs&&&  @r�compute_resonance�(ResonanceScoringEngine.compute_resonance�ss������'�'��-�����/�/�-�/��"��)�)�&�1�!�;�;�F�L�#'�#B�#B�6�#J�#'�#G�#G��#O��3�3�F�;��7�7��H� �9�9�&�J�!�;�;�F�C�#�?�?��G��7�7��?��5�5�f�=��'�'�+�+�K��=��&�&�*�*�:�s�;��,�,�0�0�1A�3�G�
 84  ��&�
 85  �"&�,�,�"4�"4�"6�
 86  �
 87  ���C��S�%��)������ �����
 88  �	
 89  r c	�R<�V^8�dQhRS[RS[S[,RS[RS[S[,/#)r�
 90  blanket_idrR�kr2)rrr
rrrL)rrs"�rrrc�s>��������*�+���	�
 91  
 92  �n�	�r c���VPPV4p.pVF:pVPVPV4pV'gK)VP	V4K<	VPRRR7VRV#)z�
 93  Get the top-k most resonant bullets in current context.
 94  
 95  Args:
 96      blanket_id: Which Markov blanket to search
 97      context: Context for computation
 98      k: Number of results to return
 99  
100  Returns:
101      List of ResonanceScore sorted by score descending
102  c��VP#rm)rO)�ss&r�<lambda>�9ResonanceScoringEngine.get_top_resonant.<locals>.<lambda>s��!�'�'r T)�key�reverseN)ra�get_bullets_for_blanketr�rN�append�sort)rGr�rRr��bullets�scoresr�rOs&&&&    r�get_top_resonant�'ResonanceScoringEngine.get_top_resonant�sm��"�+�+�5�5�j�A�����F��*�*�6�;�;��@�E��u��
�
�e�$��	���)�4��8��b�q�z�r c�6<�V^8�dQhRS[RS[S[,/#)rrNrR)rrr
)rrs"�rrrcs!�����C��(�CS�:T�r c���VPW4pV'dEVPPVRVPRVPP4/4R#R#)zx
103  Compute and store resonance score for a bullet.
104  
105  This updates the bullet's resonance_score and last_resonance_factors.
106  �resonance_score�last_resonance_factorsN)r�ra�
update_bulletrOrPrH)rGrNrRrOs&&& r�update_bullet_resonance�.ResonanceScoringEngine.update_bullet_resonancesP���&�&�t�5����K�K�%�%�d�!�5�;�;�(�%�-�-�*?�*?�*A�-�
�r c�&<�V^8�dQhRS[RS[/#�rr�r2�rr4)rrs"�rrrcs�����v��%�r c��\P!4pW!P,
107  p^pVP4R,pRWT,,pV#)z�
108  How recently was this bullet accessed?
109  
110  Uses exponential decay from last access time.
111  Half-life of ~1 day means yesterday's items get 0.5, etc.
112  ir/)rrT�accessed_at�
total_seconds)rGr�rT�age�half_life_hours�	hours_old�decays&&     rrv�'ResonanceScoringEngine._compute_recencysK���l�l�n���&�&�&�����%�%�'�$�.�	��	�3�4���r c�,<�V^8�dQhRS[RS[RS[/#�rr�rRr2�rr
r4)rrs"�rrrc's.���C�C��C�"�C�
113  �	Cr c��VPPVP^2R7pV'dVPfR#VPp^pVFLp\P
114  !VR,4p\
VPV,
115  4^8:gKCV^,
pKN	\V4^8XdR#\RV\V4,R,4#)z�
116  Does this bullet match temporal access patterns?
117  
118  If a bullet is typically accessed at certain times, boost it then.
119  For now, this is a simplified implementation.
120  )�bullet_uuid�limitr/�	timestamprt�333333�?)
121  ra�get_attention_logrNrr�
fromisoformat�abs�hour�lenr�)rGr�rR�
attention_log�current_hour�matching_hours�event�
122  event_times&&&     rrw�0ResonanceScoringEngine._compute_temporal_pattern's������5�5�&�+�+�UW�5�X�
��� 3� 3� ;���*�*����"�E�!�/�/��k�0B�C�J��:�?�?�\�1�2�a�7��!�#��#��}���"���3���]�);�;�c�A�B�Br c�&<�V^8�dQhRS[RS[/#r�r�)rrs"�rrrcGs�������E�r c�.�VPpVPPVP4pRpVR,F=pVPP	V4pV'gK(\WFP4pK?	VR,VR,,pV#)z�
123  How close is this bullet to a gravity well (strong attractor)?
124  
125  Uses the bullet's own gravity_well_strength and connection to
126  other high-strength bullets.
127  r.:N�
128  Nr[g�������?)�gravity_well_strengthra�
get_neighborsrNrur�)rGr��own_strength�	neighbors�neighbor_strength�
neighbor_uuid�neighbor�combineds&&      rrx�1ResonanceScoringEngine._compute_gravity_proximityGs����3�3���K�K�-�-�f�k�k�:�	���&�s�^�^�M��{�{�-�-�m�<�H��x�$'�(9�;Y�;Y�$Z�!�,� �#�%�(9�C�(?�?���r c�&<�V^8�dQhRS[RS[/#r�r�)rrs"�rrrc_s���
�
�f�
��
r c�f�VP'd\RVP44pV#R#)zy
129  Is this bullet involved in active edge predictions?
130  
131  Bullets that are predicted to connect to others are more relevant.
132  c3�8"�TFqPx�K	R#5irm)�prediction_strength�ro�pts& rrr�IResonanceScoringEngine._compute_prediction_involvement.<locals>.<genexpr>gs���Z�@Y�"�5�5�@Y�s�r.)�prediction_targetsr�)rGr��max_strengths&& rry�6ResonanceScoringEngine._compute_prediction_involvement_s1���$�$�$��Z��@Y�@Y�Z�Z�L���r c�&<�V^8�dQhRS[RS[/#r�r�)rrs"�rrrcns���$�$�F�$�u�$r c���\VPPVP44p\VPP	VP44pW#,p\RV^
133  ,4#)zV
134  How well-connected is this bullet?
135  
136  Well-connected bullets are often more important.
137  rt)r�ra�get_edges_fromrN�get_edges_tor�)rGr��outgoing�incoming�totals&&   rrz�,ResonanceScoringEngine._compute_edge_densitynsY���t�{�{�1�1�&�+�+�>�?���t�{�{�/�/����<�=���#���3���
138  �#�#r c�,<�V^8�dQhRS[RS[RS[/#r�r�)rrs"�rrrc{s)���!�!��!�"�!�
139  �	!r c���VP'gR#VPPR4pW2P8XdR#VPF0pW@PPVP.49gK/R#	.ROpW59dkVPV9dVPVP4MRpVPV4p\
Wg,
140  4p\RRVR,,
141  4#R#)	z�
142  Does this bullet's altitude match the current context?
143  
144  If the operator is thinking philosophically, philosophical bullets
145  should resonate more.
146  r/�altitudertrXr.r�)r_r^r]r\�����)r�
metadata_tagsr��visible_tags�
ALTITUDE_TAGS�indexr�r�)	rGr�rR�bullet_altitude�tag�altitude_order�current_idx�
147  bullet_idx�distances	&&&      rr{�.ResonanceScoringEngine._compute_altitude_match{s����'�'�'��!�.�.�2�2�:�>���6�6�6���&�&�C��(�(�,�,�W�-E�-E�r�J�J��'�
148  S���,�LS�Ld�Ld�hv�Lv�.�.�.�w�/G�/G�H�|~�K�'�-�-�o�>�J��;�3�4�H��s�C�(�S�.�0�1�1�r c�,<�V^8�dQhRS[RS[RS[/#r�r�)rrs"�rrrc�s)�������"��
149  �	r c�4�VP'gR#VPP4pVPP4pW49dR#VPVP9dR#VP
150  FpW5P49gKR#	R#)zp
151  Is this bullet relevant to the current topic?
152  
153  Simple text matching for now - can be enhanced with embeddings.
154  r/rtrXrZg�������?)r�lower�contentrNrr�)rGr�rR�topic_lower�
content_lowerr�s&&&   rr|�/ResonanceScoringEngine._compute_topic_relevance�s����$�$�$���+�+�1�1�3�����,�,�.�
��'���;�;�'�5�5�5���&�&�C��i�i�k�)��'�r c�&<�V^8�dQhRS[RS[/#r�r�)rrs"�rrrc�s���2�2��2�5�2r c�<�\RVP^,4#)zk
155  How often has this bullet been accessed historically?
156  
157  Frequently accessed bullets may be more important.
158  rt)r��access_count�rGr�s&&rr}�0ResonanceScoringEngine._compute_access_frequency�s���3��+�+�b�0�1�1r c�&<�V^8�dQhRS[RS[/#r�r�)rrs"�rrrc�s���
159  �
160  �&�
161  �U�
162  r c��RpVPF1pW0P9gK\W PV,4pK3	V#)z0
163  Is this bullet tagged with importance markers?
164  r.)r��IMPORTANCE_TAGSr�)rGr��max_importancer�s&&  rr~�2ResonanceScoringEngine._compute_importance_markers�sB�����&�&�C��*�*�*�!$�^�5I�5I�#�5N�!O��'��r c�&<�V^8�dQhRS[RS[/#r�r�)rrs"�rrrc�s�����f���r c���VPPR4'dR#VPPR4'dR#VPPR4'dR#R#)z8
165  Has the operator explicitly flagged this as important?
166  �pinnedrt�flaggedrX�starredrZr.)r�r�r�s&&rr�.ResonanceScoringEngine._compute_explicit_flags�sU��
167  ���#�#�H�-�-�����#�#�I�.�.�����#�#�I�.�.��r c�&<�V^8�dQhRS[RS[/#r�r�)rrs"�rrrc�s�����V���r c��VP'd\RVP44pV#RVP9dR#R#)zd
168  Is this bullet nagging for attention?
169  
170  Bullets with unfulfilled predictions or open questions nag.
171  c3�~"�TF3pVP\RVP^,4,x�K5	R#5i)rtN)r�r��prediction_countr�s& rrr�@ResonanceScoringEngine._compute_nagging_score.<locals>.<genexpr>�s7����3�B��'�'�#�c�2�3F�3F��3J�*K�K�K�3�s�;=�?r�r.)r�r�r�)rGr��naggings&& rr��-ResonanceScoringEngine._compute_nagging_score�sH���$�$�$��� �3�3���G��N��&�.�.� ��r c�6<�V^8�dQhRS[S[S[3,/#)rrPr3)rrs"�rrrc�s ���
J�
J�$�s�E�z�*:�
Jr c	�z�RF4pW!9gK\R\RW,44VPV&K6	R#)z�
172  Update EEG-derived factors from the EEG Bridge.
173  
174  Called by EEGResonanceIntegration when new EEG data arrives.
175  Factors are used in subsequent resonance computations.
176  
177  Args:
178      factors: Dict with keys like 'eeg_focus', 'eeg_flow', 'eeg_engagement'
179  r.rtN)rBrCrD)r�r�rg)rGrPr�s&& r�update_eeg_factors�)ResonanceScoringEngine.update_eeg_factors�s3��?�C��~�),�S�#�c�7�<�2H�)I��!�!�#�&�?r c�6<�V^8�dQhRS[S[S[3,/#r1r3)rrs"�rrrcs���(�(��c�5�j�!1�(r c�6�VPP4#)zGet current EEG factors.)rgrfrFs&r�get_eeg_factors�&ResonanceScoringEngine.get_eeg_factorss��� � �%�%�'�'r c� �RRRRRR/VnR#)z=Reset EEG factors to neutral (0.5) when EEG is not available.rBr/rCrDN)rgrFs&r�reset_eeg_factors�(ResonanceScoringEngine.reset_eeg_factors
180  s��
�����c�
181  ��r )rgrarb)�#philosophyrYz#valuez#meaning)z	#strategyz#pattern�
#architecturez#design)rWz#optionz	#tradeoffz#method)�#taskz#actionz#todoz#implementationrm)N�)r"r#r$r%r&rer�r�rhr�r�r�rvrwrxryrzr{r|r}r~rr�r
rrr)r*r+s@rrVrVls\�����	�4��D� �$� �$����$��4��D��d��$����T��D��$��O�(	�S��c��C��c��S��C�
�O�	�L��J��D��G�	�M�
182  �
183  �,6
184  �6
185  �p��<����&C�C�@��0
�
�$�$�!�!�F��>2�2�
186  �
187  �����.
J�
J�(�(�
188  �
189  r rV�__main__ztest_resonance.dbz=== Resonance Scoring Test ===
190  z3This is a high-priority decision about architecture�testrWr)r�r�r�z"This is a routine operational taskrz-This is a philosophical principle about truthrYr�architecturer])rrzBullet: :N�(Nz...z	  Score: z.3fz  Recency: z  Topic relevance: z  Importance markers: zTop resonant bullets:)r�z  z. z... (score: �)z
191  === Test Complete ===)2r&�dataclassesrrrr�typingrrrr	�math�databaser
192  rr
r-rLrVr"�tempfile�os�path�join�
193  gettempdir�db_path�db�print�
create_bullet�bullet1�bullet2�bullet3�record_attentionrN�enginerRr�r�rOr�rPr7r=r?r��top�	enumerate�iru�remover!r r�<module>r5s���
194  �)�(�,�,��*��;�;��;�(�2
195  �2
196  ��2
197  �j�/�/��/�d
198  �d
199  �P
�z���
��g�g�l�l�8�.�.�0�2E�F�G�	�W�	�B�	�
200  ,�-����E��!�?�3���G����4���Y���G����?��"�M�2���G�������%�������%�������%�$�B�
'�F��$�$��G��G�W�-���(�(����g�>��
������,�-�S�1�2�
�	�%�+�+�c�*�+�,�
��E�M�M�1�1�#�6�7�8�
�#�E�M�M�$A�$A�#�#F�G�H�
�&�u�}�}�'G�'G��&L�M�N�
��.�
201202  !�"�
203  �
204  !�
205  !�&�'�Q�
206  !�
207  7�C��c�1�%���5����u�z�z�*��
��1�#�R����s�+�,�L����S�8I��K�L�&�
208  �I�I�g��	�
209  #�$�}r