/ core / attention / __pycache__ / coherence_risk.cpython-314.pyc
coherence_risk.cpython-314.pyc
  1  +
  2  �4gi�?����Rt^RIHtHt^RIHtHt^RIHtHtH	t	H
  3  t
  4  Ht^RIH
t
^RIHt!RR]
4t]P"R]P$R	]P&R
  5  ]P(R]P*R]P,R
/t]!RR44t]!RR44t!RR4t!RR4tRRlt]R8XEd#]!R4]!4wtt]!R4]PA]!R]PB!4]P$RRRR74]PA]!R]PB!4]P"R R!RR74]!R"4]PA]!R#]PB!4]P$RR$R%R74]PA]!R#]PB!4]P$R&R'R(74]!R)4]PERR#4t#]!R*]#PHR+
24]!R,]#PJR+
24]!R-]#PLR+
24]!R.]#PN24]!R/]#PP24]!R0]#PRPT24]!R14]PW4F0wt,t-t.]!R2]-R+
R3],R4R5P_R6].44R724K2	]!R84]PaR4t1]!R9]1PT24]!R:4R;#R;#)<a
  6  Coherence Risk Detection
  7  
  8  Detects when concurrent sessions risk architectural divergence.
  9  
 10  The problem: You're in Session A designing an attention system.
 11  Session B is also making architecture decisions. If they diverge,
 12  you'll have to reconcile later - expensive.
 13  
 14  The solution: Detect high-coherence-risk situations and increase
 15  membrane permeability automatically. The sessions should "feel"
 16  each other more strongly when they're making overlapping decisions.
 17  
 18  Risk factors:
 19  1. Semantic overlap - same concepts being discussed
 20  2. Decision weight - architecture/principle decisions vs implementation
 21  3. Recency - both sessions active recently
 22  4. Conflict signals - different conclusions on same topic
 23  
 24  When risk is high, membrane opens. Sessions become more aware.
 25  )�	dataclass�field)�datetime�	timedelta)�Optional�List�Dict�Set�Tuple)�Enum)�PermeabilityLevelc�2�]tRt^tRtRtRtRtRtRt	Rt
 26  RtR	#)
 27  �DecisionTypez4Types of decisions with different coherence weights.�	principle�architecture�	interface�implementation�naming�style�N)�__name__�
 28  __module__�__qualname__�__firstlineno__�__doc__�	PRINCIPLE�ARCHITECTURE�	INTERFACE�IMPLEMENTATION�NAMING�STYLE�__static_attributes__r��@/Users/rcerf/repos/Sovereign_OS/core/attention/coherence_risk.pyrrs#��>��I�!�L��I�%�N�
�F��Er"r��?g�������?�ffffff�?�333333�?��?g�������?c�Ja�]tRt^4toRtRtRt]!]R7t	V3Rlt
 29  RtVtR#)�SessionDecisionzA decision made in a session.r'N��default_factoryc�<�V^8�dQh/S[;R&S[;R&S[;R&S[;R&S[;R&S[;R&S[S[,;R&S[S[,;R&#)	��
 30  session_id�	timestamp�
decision_type�topic�content�
 31  confidence�position�alternatives_considered)�strrr�floatrr)�format�
__classdict__s"�r#�__annotate__�SessionDecision.__annotate__4sr�����O����	�
 32   ����J�
��L������s�m�"��"�#�Y�D�r"r)
rrrrrr3r4r�listr5�__annotate_func__r!�__classdictcell__�r9s@r#r)r)4s(����'��J�#�H�).�t�)D���r"r)c�la�]tRt^CtoRt]!]R7t]!]R7t]	PtV3RltRt
VtR#)�
CoherenceRiskz.Assessment of coherence risk between sessions.r*c�<�V^8�dQh/S[;R&S[;R&S[;R&S[;R&S[;R&S[S[,;R&S[S[S[S[3,,;R&S[;R&#)	r-�
 33  risk_score�semantic_overlap�decision_weight�recency_factor�conflict_detected�overlapping_topics�conflicting_decisions�recommended_permeability)r7�boolrr6r
 34  r)r8r9s"�r#r:�CoherenceRisk.__annotate__Cs~���������	�
 35  �����
�����S�	�?�� ��c�3�h��0�N��0�N�r"rN)rrrrrrr<rHrIr�
 36  ATTRACTORSrJr=r!r>r?s@r#rArACs3����8�%*�$�$?��38��3N��2C�2N�2N���r"rAc�Fa�]tRt^RtoRtRtRtRt]P.RO]P.RO]P.RO]P.RO]P.RO/tRV3RlRlltV3RlRltV3R	lR
 37  ltV3RlRltV3R
lRltV3RlRltRV3RlRlltRtVtR#)�CoherenceRiskDetectora�
 38  Detects coherence risk across concurrent sessions.
 39  
 40  Usage:
 41      detector = CoherenceRiskDetector()
 42  
 43      # Record decisions from sessions
 44      detector.record_decision(SessionDecision(
 45          session_id="abc123",
 46          timestamp=datetime.now(),
 47          decision_type=DecisionType.ARCHITECTURE,
 48          topic="attention_system",
 49          content="Using continuous stream instead of batch compaction"
 50      ))
 51  
 52      # Check risk between sessions
 53      risk = detector.assess_risk("abc123", "def456")
 54      print(f"Risk: {risk.risk_score:.0%}, recommend: {risk.recommended_permeability}")
 55  
 56      # Get recommended permeability for a session
 57      permeability = detector.get_recommended_permeability("abc123")
 58  r%皙�����?皙�����?c�&<�V^8�dQhRS[RS[/#)r-�recency_window_minutes�overlap_threshold)�intr7)r8r9s"�r#r:�"CoherenceRiskDetector.__annotate__�s#���
 59  E�
 60  E� #�
 61  E�!�
 62  Er"c�P�\VR7VnW n/Vn/VnR#))�minutesN)r�recency_windowrT�
 63  _decisions�_topic_decisions)�selfrSrTs&&&r#�__init__�CoherenceRiskDetector.__init__�s)��
 64  (�0F�G���!2��=?���BD��r"c�$<�V^8�dQhRS[RR/#)r-�decision�returnN)r))r8r9s"�r#r:rV�s���
 65  ?�
 66  ?��
 67  ?�D�
 68  ?r"c��VPVP9d.VPVP&VPVP,PV4VPVP9d.VPVP&VPVP,PV4R#)z!Record a decision from a session.N)r.rZ�appendr1r[)r\r`s&&r#�record_decision�%CoherenceRiskDetector.record_decision�s������d�o�o�5�35�D�O�O�H�/�/�0�����+�+�,�3�3�H�=��>�>��!6�!6�6�46�D�!�!�(�.�.�1����h�n�n�-�4�4�X�>r"c�&<�V^8�dQhRS[RS[/#)r-r2ra)r6r)r8r9s"�r#r:rV�s���+�+�C�+�L�+r"c�*a�VP4o/pVPP4Fwr4\V3RlV44pWRV&K 	\	VP44'g\P#\W"PR7#)z"Detect decision type from content.c3�8<"�TFqS9gK^x�K	R#5i)�Nr)�.0�kw�
content_lowers& �r#�	<genexpr>�=CoherenceRiskDetector.detect_decision_type.<locals>.<genexpr>�s����D�H�b�m�0C���H�s��
 69  )�key)
 70  �lower�DECISION_KEYWORDS�items�sum�any�valuesrr�max�get)r\r2�scores�dtype�keywords�scorerls&&    @r#�detect_decision_type�*CoherenceRiskDetector.detect_decision_type�ss����
�
��
���#�5�5�;�;�=�O�E��D�H�D�D�E�!�5�M� >��6�=�=�?�#�#��.�.�.��6�z�z�*�*r"c�,<�V^8�dQhRS[RS[RS[/#)r-�	session_a�	session_bra)r6rA)r8r9s"�r#r:rV�s.���`
 71  �`
 72  ��`
 73  ��`
 74  �
 75  �	`
 76  r"c���\P!4pVPW4pVPW#4pV'd	V'g \RRRRR\P
 77  R7#\
RV44p\
RV44pWg,pV'd	V'gRp	M.\V4\\V4\V44,p	Rp
 78  VFQpVPPV.4F.p\PVPR4p
\W�4p
 79  K0	KS	\RV44p\RV44p\W�,
 80  P44p\^^VVP P4,,
 81  4p.pVF�pVPPV.4p/pVFkpVP"'gKVP$V9d\
4VVP$&VVP$,P'VP"4Km	\V4^8�gK�\)VP+44pV^,V^,8wgK�VP-VR	34K�	\V4^8�pV	R
 82  ,V
 83  R,,VR,,V'dRMR,p\RV4pVVP.8�g	V'd\P0pMTVVP28�d\P4pM2VVP68�d\P8pM\P
 84  p\VV	V
 85  VV\)V4VVR
7#)ze
 86  Assess coherence risk between two sessions.
 87  
 88  Returns a CoherenceRisk with score and recommendation.
 89  �F)rCrDrErFrGrJc3�8"�TFqPx�K	R#5i�N�r1�rj�ds& r#rm�4CoherenceRiskDetector.assess_risk.<locals>.<genexpr>�����4��1�w�w����c3�8"�TFqPx�K	R#5ir�r�r�s& r#rmr��r�r�r&c3�8"�TFqPx�K	R#5ir��r/r�s& r#rmr������=��A�K�K��r�c3�8"�TFqPx�K	R#5ir�r�r�s& r#rmr��r�r��position_mismatchrPrQr$)rCrDrErFrGrHrIrJ)r�now�_get_recent_decisionsrAr�CLOSED�set�len�minr[rw�DECISION_WEIGHTSr0rv�abs�
total_secondsrYr4r.�addr<rurc�ALERT_THRESHOLD�ALERTS�SUMMARY_THRESHOLD�	SUMMARIES�ATTRACTOR_THRESHOLDrM)r\rr�r��decisions_a�decisions_b�topics_a�topics_b�overlappingrDrEr1r��weight�
most_recent_a�
most_recent_b�time_gaprF�	conflicts�topic_decisions�	positions�
all_positionsrGrC�recommendeds&&&                      r#�assess_risk�!CoherenceRiskDetector.assess_risk�s����l�l�n���0�0��@���0�0��@���+� ��!$� #�"�"'�):�)A�)A�
�
��4��4�4���4��4�4���)���x�"��"�;�/�#�c�(�m�S��]�2S�S���� �E��*�*�.�.�u�b�9��)�-�-�a�o�o�s�C��"%�o�">��:�!��=��=�=�
��=��=�=�
��
�5�D�D�F�G���Q��H�t�/B�/B�/P�/P�/R�$R� R�S���	� �E�"�3�3�7�7��r�B�O��I�$���:�:�:��|�|�9�4�25�%�	�!�,�,�/��a�l�l�+�/�/��
 90 91  �;�	%��9�~��"� $�Y�%5�%5�%7� 8�
� ��#�}�Q�'7�7��$�$�e�-@�%A�B�!� �	�N�Q�.��
�s�"��c�!�
"��S� �
!�&�S�3�
0�	���j�)�
 92  ���-�-�-�1B�+�2�2�K�
�4�1�1�
1�+�5�5�K�
�4�3�3�
3�+�6�6�K�+�2�2�K��!�-�+�)�/�#�K�0�"+�%0�	
 93 94  r"c�<<�V^8�dQhRS[RS[RS[S[,/#)r-r.r�ra)r6rrr))r8r9s"�r#r:rVs.���

 95  �

 96  ��

 97  ��

 98  �
 99  �o�	�	

100  r"c���WP9d.#W P,
101  pVPV,Uu.uFpVPV8�gKVNK	up#uupi)z7Get decisions from a session within the recency window.)rZrYr/)r\r.r��cutoffr�s&&&  r#r��+CoherenceRiskDetector._get_recent_decisionss]���_�_�,��I��*�*�*�����z�2�
102  �2�!��{�{�f�$�
�A�2�
103  �	
104  ��
105  s�A�Ac�&<�V^8�dQhRS[RS[/#�r-r.ra�r6r)r8r9s"�r#r:rVs��� � �� �
106  � r"c��\PpVPP4FOpW18XdKVP	W4pVP
107  PVP8�gKCVP
108  pKQ	V#)z�
109  Get recommended permeability for a session based on risk with all others.
110  
111  Returns the highest permeability recommended across all session pairs.
112  )rr�rZ�keysr�rJ�value)r\r.�max_permeability�
other_session�risks&&   r#�get_recommended_permeability�2CoherenceRiskDetector.get_recommended_permeabilitysq��-�3�3��!�_�_�1�1�3�M��*���#�#�J�>�D��,�,�2�2�5E�5K�5K�K�#'�#@�#@� �4� �r"c	�`<�V^8�dQhRS[RS[S[S[S[S[S[,3,,/#)r-�	thresholdra)r7rr
113  r6)r8r9s"�r#r:rV5s8���$C�$C��$C�
114  �e�C���S�	�)�*�	+�$Cr"c�(�.pVPP4F�wr4\\RV444p\	V4^8dK2\RV44p/pVFBpVP'gKVPV9gK*VPWxP&KD	\	\VP444^8�dRMRp	VR,V	,p
115  W�8�gK�VPW:V34K�	\VRRR7#)	zY
116  Get topics with high coherence risk.
117  
118  Returns list of (topic, risk_score, session_ids).
119  c3�8"�TFqPx�K	R#5ir�)r.r�s& r#rm�=CoherenceRiskDetector.get_high_risk_topics.<locals>.<genexpr>As���"C��A�<�<��r�c3�b"�TF%p\PVPR4x�K'	R#5i)r&N)r�rwr0r�s& r#rmr�Fs+����"�A�!�$�$�Q�_�_�c�:�:�"�s�-/r&r�r%c��V^,#)rir)�xs&r#�<lambda>�<CoherenceRiskDetector.get_high_risk_topics.<locals>.<lambda>Ys��q��tr"T)ro�reverse)r[rrr<r�r�rvr4r.rurc�sorted)r\r��	high_riskr1�	decisions�session_ids�
120  max_weightr�r��conflict_factorr�s&&         r#�get_high_risk_topics�*CoherenceRiskDetector.get_high_risk_topics5s����	� $� 5� 5� ;� ;� =��E��s�"C��"C�C�D�K��;��!�#����"���J��I����:�:�:��|�|�9�4�23�*�*�	�,�,�/��
121  &)��Y�-=�-=�-?�)@�%A�A�%E�c�3�O���#�o�5�D�� �� � �%�{�!;�<�/!>�2�i�^�T�B�Br")rZr[rTrYN)r�
122  philosophy�approach�belief�always�never�should�must�fundamental�core�foundational)
123  r�design�	structure�system�	component�module�layer�pattern�flow�pipeline)	r�api�contract�protocol�schema�endpoint�method�function�	signature)	�	implement�code�write�build�create�fix�bug�feature�todo)�name�call�rename�term�
124  vocabulary�
125  convention�label)�r&)r')rrrrrr�r�r�rrrrrrrqr]rdr|r�r�r�r�r!r>r?s@r#rOrORs������0�O�����	���!
126  �	�!�!�$
127  �	���!
128  �	�#�#�&
129  �	���
130  �#��.
131  E�
132  E�
133  ?�
134  ?�+�+�`
135  �`
136  �D

137  �

138  � � �.$C�$C�$Cr"rOc�ta�]tRtRtoRt]P3V3RlRlltV3RlRltV3RlRlt	R	t
139  VtR
140  #)�AdaptiveMembranei\z{
141  Membrane that adapts permeability based on coherence risk.
142  
143  Wraps the base membrane and adjusts permeability dynamically.
144  c�&<�V^8�dQhRS[RS[/#)r-�
risk_detector�base_permeability)rOr)r8r9s"�r#r:�AdaptiveMembrane.__annotate__cs���3�3�,�3�-�3r"c��WnW nR#r�)rr)r\rrs&&&r#r]�AdaptiveMembrane.__init__cs��
145  +��!2�r"c�&<�V^8�dQhRS[RS[/#r�r�)r8r9s"�r#r:rks���/�/��/�
146  �/r"c�N�VPPV4p\P\P\P
147  \P\P.pVPVP4pVPV4pV\WE4,#)zq
148  Get current permeability for a session.
149  
150  Returns the higher of base permeability and risk-based recommendation.
151  )rr�rr�rMr�r��OPEN�indexrrv)r\r.�risk_permeability�levels�base_idx�risk_idxs&&    r#�get_permeability_for_session�-AdaptiveMembrane.get_permeability_for_sessionks���!�.�.�K�K�J�W��
�$�$��(�(��'�'��$�$��"�"�
152  ���<�<�� 6� 6�7���<�<� 1�2���c�(�-�.�.r"c�,<�V^8�dQhRS[RS[RS[/#)r-r.r1ra)r6rK)r8r9s"�r#r:r�s"�����s��3��4�r"c�x�VPPRR7pVFwrEpW$8XgKW9gKR#	R#)z�
153  Check if we should send an alert about a topic to a session.
154  
155  Returns True if the topic is high-risk and involves this session.
156  r')r�TF)rr�)r\r.r1r��
157  risk_topicrC�sessionss&&&    r#�should_alert�AdaptiveMembrane.should_alert�s?���&�&�;�;�c�;�J�	�09�,�J�H��"�z�'=��1:�r")rrN)rrrrrrrMr]rrr!r>r?s@r#r�r�\s4�����0A�/K�/K�3�3�/�/�4�r"r�c�F�V^8�dQhR\\\3,/#)r-ra)r
158  rOr�)r8s"r#r:r:�s�����'<�>N�'N�!O�r"c�2�\4p\V4pW3#)za
159  Create the coherence risk detection system.
160  
161  Returns (CoherenceRiskDetector, AdaptiveMembrane).
162  )rOr�)�detector�membranes  r#�create_coherence_systemr�s��%�&�H���)�H���r"�__main__z!=== Coherence Risk Detection ===
163  z%Recording decisions from Session A...r�context_managementz1Using stream model instead of sawtooth compaction�for)r.r/r0r1r2r4�attention_systemz,Attention is all you need - the core insightz%Recording decisions from Session B...r�z+Considering batch compaction for simplicity�against�
aha_detectionz&Two types: discovery and architectural)r.r/r0r1r2z
164  --- Risk Assessment ---zRisk score: z.0%zSemantic overlap: zDecision weight: zConflict detected: zOverlapping topics: zRecommended permeability: z
165  --- High Risk Topics ---z  [z] z (z, c#�2"�TF
qR,x�K	R#5i):N�NNr)rj�ss& r#rmrm�s���3L�8�a�b�E�E�8�s��)z
166  --- Adaptive Membrane ---zSession A permeability: z
167  === Coherence protected ===N)2r�dataclassesrrrr�typingrrrr	r
168  �enumrrrrrrrrrr r�r)rArOr�rr�printrrdr�r�r�rCrDrErGrHrJr�r�r1r{r�joinr�permrr"r#�<module>r+s���,)�(�3�3��'��4�����C����s����C�������������
���E�E��E��O�O��O�GC�GC�T5�5�p��z��	�
169  .�/�0�2��H�h�
170171  1�2����_���,�,�.�"�/�/�"�C��
��
���_���,�,�.�"�,�,� �>��
��
172173  1�2����_���,�,�.�"�/�/�"�=��
��
���_���,�,�.�"�/�/��8���
174175  %�&�����[�9�D�	�L�����-�
176  .�/�	��t�4�4�S�9�
177  :�;�	��d�2�2�3�7�
178  8�9�	��� 6� 6�7�
179  8�9�	� ��!8�!8� 9�
180  :�;�	�&�t�'D�'D�'J�'J�&K�
181  L�M�
182183  &�'�"*�"?�"?�"A���u�h�
��E�#�;�b���r�$�)�)�3L�8�3L�*L�)M�Q�O�P�#B�
184185  '�(��0�0��=�D�	�$�T�Z�Z�L�
186  1�2�	�
187  )�*�r"