/ core / attention / __pycache__ / membrane.cpython-314.pyc
membrane.cpython-314.pyc
  1  +
  2  1gi�5����Rt^RIHtHt^RIHtHt^RIHtHtH	t	H
  3  t
  4  Ht^RIH
t
!RR]
4t]!RR44t]!R	R
  5  44t!RR4t!R
R4t]P&3RRllt]R8Xd�]!R4]!]P&R7t]!]4t]P3R.RO4]P3R.R O4]P3R.R!O4]P5RR4]P5RR4]P5RR4R"F6t]!R]R24]P9]4t]!]P=44K8	]!R4]!]P?R44]!R4R#R#)#a	
  6  Cross-Session Membrane - Semi-Permeable Context Sharing
  7  
  8  The Markov blanket problem at the session level:
  9  - Each Claude instance is a closed system
 10  - But you want semi-permeability
 11  - Each session should have AWARENESS of others without being MERGED
 12  
 13  What crosses the membrane:
 14  1. Attractor signals - "These concepts are hot across sessions"
 15  2. Topic summaries - "Other sessions are discussing X, Y, Z"
 16  3. Coherence alerts - "Session B just touched on something you discussed"
 17  
 18  What doesn't cross:
 19  1. Full conversation history
 20  2. Detailed context
 21  3. Raw transcripts
 22  
 23  This maintains separation of concerns while enabling coordination.
 24  The membrane is the intelligence - what it lets through and what it filters.
 25  
 26  "The boundary IS the organism" - what permeates shapes identity.
 27  )�	dataclass�field)�datetime�	timedelta)�Optional�List�Dict�Set�Any)�Enumc�.�]tRt^tRtRtRtRtRtRt	Rt
 28  R#)	�PermeabilityLevelz&How much context crosses the membrane.�closed�
 29  attractors�	summaries�alerts�open�N)�__name__�
 30  __module__�__qualname__�__firstlineno__�__doc__�CLOSED�
 31  ATTRACTORS�	SUMMARIES�ALERTS�OPEN�__static_attributes__r��:/Users/rcerf/repos/Sovereign_OS/core/attention/membrane.pyr
r
s��0�
�F��J��I�
�F��Drr
c�ha�]tRt^(toRt]!]PR7tV3RlRlt	V3Rlt
 32  RtVtR#)�CrossSessionSignalz#A signal that crosses the membrane.��default_factoryc� <�V^8�dQhRS[/#���return��str)�format�
__classdict__s"�r �__annotate__�CrossSessionSignal.__annotate__1s���
 33  *�
 34  *��
 35  *rc�P�VPR8Xd'RVPR\VP4R2#VPR8XdRVP2#VPR8XdRVP2#VPR	8XdR
 36  VPR2#RVP2#)
z*Format for injection into session context.�	attractorzross-session attractor] 'z' active in z	 sessions�summaryz[Other sessions] �alertz[Coherence alert] �echoz[Echo detected] z resonating across sessionsz	[Signal] )�signal_type�content�len�source_sessions)�selfs&r �to_context_line�"CrossSessionSignal.to_context_line1s������{�*�0�����l�3�t�Oc�Oc�Kd�Je�en�o�o�
�
�
��
*�&�t�|�|�n�5�5�
�
�
��
(�'����~�6�6�
�
�
��
'�%�d�l�l�^�3N�O�O��4�<�<�.�)�)rc�f<�V^8�dQh/S[;R&S[;R&S[S[,;R&S[;R&S[;R&#)r'r4r5r7�strength�	timestamp)r*r�floatr)r+r,s"�r r-r.(sH��������L�	�
 37  �#�Y����O�
��=�rrN)
rrrrrrr�nowr=r9�__annotate_func__r�__classdictcell__�r,s@r r"r"(s)����-�
 38   ����=�I�
 39  *�
 40  *��rr"c�a�]tRt^>toRt]!]R7t]!]R7t]!]R7t	]!]
 41  PR7tV3RlRlt
V3RltRtVtR#)�SessionContextzContext package for a session.r#c� <�V^8�dQhRS[/#r&r))r+r,s"�r r-�SessionContext.__annotate__Gs��� � �#� rc	��VP'gR#RR.pVPUu.uFq"PR8XgKVNK	ppVPUu.uFq"PR8XgKVNK	ppVPUu.uFq"PR8XgKVNK	ppV'dhVPR4VR,F8pVPRVPR	\	VP
 42  4R
 43  24K:	VPR4V'dQVPR4VR,F!pVPRVP24K#	VPR4V'dQVPR
4VR,F!pVPRVP24K#	VPR4RP
V4#uupiuupiuupi)z(Format as a context block for injection.�z## Cross-Session Awarenessr0r2r1z**Hot across sessions:**:N�N�  - z (z
 44   sessions)z**Recent coherence:**:N�Nz**Other threads:**�
 45  )�signalsr4�appendr5r6r7�join)r8�lines�srrrs&     r �to_context_block�SessionContext.to_context_blockGs{���|�|�|��-�r�2��"&���N��A���+�1M�a�a��
 46  �N�!�\�\�F�\��]�]�g�-E�!�!�\��F� $���K��1�
�
��0J�Q�Q��	�K���L�L�3�4���^�^�����t�A�I�I�;�b��Q�5F�5F�1G�0H�
 47  �S�T�$��L�L�����L�L�0�1��B�Z�Z�����t�A�I�I�;�/�0� ��L�L�����L�L�-�.��r�]�]�����t�A�I�I�;�/�0�#��L�L����y�y�����-O��F��Ks"�G�G�G�+G�G�Gc�<�V^8�dQh/S[;R&S[S[,;R&S[S[,;R&S[S[,;R&S[;R&#)r'�
 48  session_idrM�
active_topics�related_sessions�last_updated)r*rr"r)r+r,s"�r r-rF>sS�����O���$�
%�C�	�
 49  ��9�:���3�i�=�
��@�rrN)rrrrrr�listrMrVrWrr?rXrRr@rrArBs@r rDrD>sF����(�(-�d�(C�G�$�T�:�M�"'��"=��"�8�<�<�@�L� � ��rrDc��a�]tRt^htoRt]P^^
 50  3V3RlRlltV3RlRltV3RlRlt	V3RlR	lt
 51  V3R
 52  lRltRV3R
lRlltV3RlRlt
RtVtR#)�SessionMembranez�
 53  The semi-permeable membrane between sessions.
 54  
 55  Controls what context flows between sessions.
 56  Each session can query its membrane for cross-session awareness.
 57  c�,<�V^8�dQhRS[RS[RS[/#)r'�permeability�attractor_threshold�max_signals_per_session)r
�int)r+r,s"�r r-�SessionMembrane.__annotate__ps)���
<�
<�'�
<�!�
<�"%�	
<rc�T�WnW nW0n/Vn/Vn.VnR#�N)r]r^�max_signals�_session_topics�_session_summaries�_recent_signals)r8r]r^r_s&&&&r �__init__�SessionMembrane.__init__ps0��)��#6� �2��57���24���9;��rc�$<�V^8�dQhRS[RR/#)r'rUr(Nr))r+r,s"�r r-ras���5�5�3�5�4�5rc�X�WP9d\4VPV&R#R#)zRegister a new session.N)re�set)r8rUs&&r �register_session� SessionMembrane.register_sessions%���1�1�1�/2�u�D� � ��,�2rc�L<�V^8�dQhRS[RS[S[,RS[S[,/#)r'rU�topicsr()r*rr")r+r,s"�r r-ra�s3���%�%��%��S�	�%�
 58  � �	!�	%rc
 59  �:�VPV4VPV,p\V4pW@PV&.pWC,
 60  F�pVPP4UUu.uFwrrWq8wgK
Wb9gKVNK	ppp\	V4VP
 61  ^,
 62  8�gKc\
RVV.V,\	V4\	VP4,R7p	VPV	4VPPV	4K�	V#uuppi)zM
 63  Update topics for a session.
 64  
 65  Returns any signals triggered by this update.
 66  r0�r4r5r7r<)	rmrerl�itemsr6r^r"rNrg)
 67  r8rUrp�
 68  old_topics�
 69  new_topicsrM�topic�sid�other_sessions�signals
 70  &&&       r �update_session_topics�%SessionMembrane.update_session_topics�s��	
���j�)��)�)�*�5�
 71  ���[�
 72  �+5���Z�(����,�,�E�(,�';�';�'A�'A�'C��'C����$��).����'C�
��
 73  �>�"�d�&>�&>��&B�B�+� +�!�%/�L�>�$A� ��0�3�t�7K�7K�3L�L�	�����v�&��$�$�+�+�F�3�!-�$���!s�(
 74  D�7D�?Dc�*<�V^8�dQhRS[RS[RR/#)r'rUr1r(Nr))r+r,s"�r r-ra�s)���6�6��6��6�
 75  �	6rc�"�W PV&R#)z!Update the summary for a session.N)rf)r8rUr1s&&&r �update_session_summary�&SessionMembrane.update_session_summary�s��/6���
 76  �+rc�&<�V^8�dQhRS[RS[/#�r'rUr()r*rD)r+r,s"�r r-ra�s���:�:��:�
 77  �:rc�>�\VR7pVP\P8XdV#VP	VR7pVP4FYwrEVPP\RV\V4\V4\VP4,R74K[	\VP44Vn
VP\P\P39dsVP P4FTwrgWa8wgK
V'gKVPP\RRVR,RVR	,2V.R
 78  R74KV	VP\P"\P39dDVP$RRF0pWP&9gKVPPV4K2	VPP)V\+44p	VPP4F8wrjWa8wgK
W�,'gKVP,PV4K:	\/VPRR
R7RVP0Vn\2P4!4VnV#)z`
 79  Get cross-session context for a specific session.
 80  
 81  What crosses depends on permeability level.
 82  )rU)�exclude_sessionr0rrr1zSession �N�Nz: :N�dNg�?Nc��VP#rc)r<)rQs&r �<lambda>�9SessionMembrane.get_context_for_session.<locals>.<lambda>�s��!�*�*rT)�key�reversei����)rDr]r
r�_get_attractorsrsrMrNr"rYr6re�keysrVrrrfrrgr7�getrlrW�sortedrdrr?rX)r8rU�contextrrv�sessionsrwr1ry�	my_topicsrps&&         r �get_context_for_session�'SessionMembrane.get_context_for_session�s��!�J�7����� 1� 8� 8�8��N��)�)�*�)�E�
 83  �)�/�/�1�O�E��O�O�"�"�#5�'�� $�X���X���T�-A�-A�)B�B�	$�
� 2�!%�Z�_�_�%6� 7������!2�!<�!<�>O�>T�>T� U�U� $� 7� 7� =� =� ?����$����O�O�*�*�+=�$-�"*�3�r�7�)�2�g�d�m�_� E�),��!$�	,��!@����!2�!9�!9�;L�;Q�;Q� R�R��.�.�s�t�4���%;�%;�;��O�O�*�*�6�2�5�
 84  �(�(�,�,�Z���?�	��/�/�5�5�7�K�C�� �V�%7�%7��(�(�/�/��4�8�
 85  !��O�O�$��
 86  ��4���	��� (�|�|�~����rNc�L<�V^8�dQhRS[RS[S[S[S[,3,/#)r'r�r()r*rr	)r+r,s"�r r-ra�s,���
 87  �
 88  ��
 89  �
 90  �c�3�s�8�m�	�
 91  rc�L�/pVPP4F?wr4W18XdK
VF,pWR9d
\4W%&W%,PV4K.	KA	VP4UUu/uF#wrV\	V4VP
 92  8�gK!WVbK%	upp#uuppi)z,Get topics that appear in multiple sessions.)rersrl�addr6r^)r8r��topic_sessionsrUrprvr�s&&     r r��SessionMembrane._get_attractors�s���
 93  /1��"&�"6�"6�"<�"<�">��J��,�����.�,/�E�N�)��%�)�)�*�5� �#?�$2�#7�#7�#9�
 94  �#9����8�}�� 8� 8�8�
�E�O�#9�
 95  �	
 96  ��
 97  s�3B �B c	�R<�V^8�dQhRS[RS[RS[S[,RS[S[,/#)r'rUr5rpr()r*rrr")r+r,s"�r r-ras=����������S�	�	�
 98  
 99  �$�	%�rc
��VF�pVPP4UUu.uFwrVWQ8wgK
WF9gKVNK	pppV'gKJ\RRVRRPRV442V.V,\	V4\^\	VP4^,
100  4,R7u#	R#uuppi)zO
101  Detect if content echoes across sessions.
102  
103  Returns a signal if echo detected.
104  r3�'z' resonating: also in sessions �, c3�2"�TF
qR,x�K	R#5i�r�Nr��.0rQs& r �	<genexpr>�.SessionMembrane.detect_echo.<locals>.<genexpr>s���On�_m�Z[�RT�PU�PU�_m���rrN)rersr"rOr6�max)r8rUr5rprvrw�sess_topicsrxs&&&&    r �detect_echo�SessionMembrane.detect_echos����E�,0�,@�,@�,F�,F�,H��,H�(���$��).�)=���,H�
���~�)� &���w�&E�d�i�i�On�_m�On�Fn�Eo�p�%/�L�>�$A� ��0�3�q�#�d�>R�>R�:S�VW�:W�3X�X�	��
���s�
105  B:�B:�B:)rgrfrer^rdr]rc)rrrrrr
rrhrmrzr~r�r�r�rrArBs@r r[r[hsc�����+<�*F�*F�#$�')�	
<�
<�5�5�
106  %�%�N6�6�:�:�x
107  �
108  �,�rr[c�Za�]tRtRtoRtV3RlRltV3RlRltV3RlRltR	tVt	R
109  #)�CrossSessionContextInjectori z�
110  Injects cross-session context into session prompts.
111  
112  This is the mechanism by which sessions become aware of each other.
113  Can be integrated with the session streaming pipeline.
114  c� <�V^8�dQhRS[/#)r'�membrane)r[)r+r,s"�r r-�(CrossSessionContextInjector.__annotate__(s���!�!��!rc��WnR#rc�r�)r8r�s&&r rh�$CrossSessionContextInjector.__init__(s�� �
rc�&<�V^8�dQhRS[RS[/#r�r))r+r,s"�r r-r�+s���*�*�S�*�S�*rc�X�VPPV4pVP4#)zp
115  Get cross-session context to inject into system prompt.
116  
117  Returns a formatted string to add to session context.
118  )r�r�rR)r8rUr�s&& r �get_system_context�.CrossSessionContextInjector.get_system_context+s'���-�-�7�7�
119  �C���'�'�)�)rc�&<�V^8�dQhRS[RS[/#r�r))r+r,s"�r r-r�4s���" �" �C�" �C�" rc��VPPV4pVP'gR#.ROpVPUu.uFqDPR8XgKVNK	ppV'dJVP	R4VF!pVP	RVP
120  24K#	VP	R4VP'dFVP	RRPRVP4424VP	R4VP	R4VP	R	4VP	R
121  4RPV4#uupi)
zh
122  Format cross-session awareness for Claude.
123  
124  Returns text suitable for injection into Claude's context.
125  rHr0z?**Cross-session attractors** (topics hot in multiple sessions):rJz**Related sessions:** r�c3�2"�TF
qR,x�K	R#5ir�rr�s& r r��@CrossSessionContextInjector.format_for_claude.<locals>.<genexpr>Os���;d�Kc�a�b�E�E�Kc�r�zHThese signals indicate coherent threads across the operator's attention.z;Consider how your conversation relates to these attractors.z</cross-session-awareness>rL)z<cross-session-awareness>zHThe operator has concurrent conversations. What's crossing the membrane:rH)r�r�rMr4rNr5rWrO)r8rUr�rPrQr�as&&     r �format_for_claude�-CrossSessionContextInjector.format_for_claude4s
���-�-�7�7�
126  �C�������
127  ��")���Q��A�M�M�[�4P�a�a��
128  �Q���L�L�Z�[������t�A�I�I�;�/�0� ��L�L����#�#�#��L�L�1�$�)�)�;d�7�Kc�Kc�;d�2d�1e�f�g��L�L���
���_�`�
���R�S�
���1�2��y�y�����!Rs�E
�E
r�N)
129  rrrrrrhr�r�rrArBs@r r�r� s(�����!�!�*�*�" �" rr�c�0�V^8�dQhR\R\/#)r'r]r()r
�tuple)r+s"r r-r-Zs����#��
130  �rc�6�\VR7p\V4pW3#)z�
131  Create the membrane system.
132  
133  Args:
134      permeability: How permeable the membrane should be
135  
136  Returns:
137      (SessionMembrane, CrossSessionContextInjector)
138  �r])r[r�)r]r��injectors&  r �create_membrane_systemr�Zs!���L�9�H�*�8�4�H���r�__main__z$=== Cross-Session Membrane Test ===
139  r��	session_a�	session_b�	session_cz"Building attention tracking systemz&Discussing Markov blanket architecturezWorking on podcast pipelinez
140  --- Context for z ---z'
141  --- Claude-formatted for session_a ---z
142  === Test Complete ===N)�	attention�pipeline�markov)r��blankets�architecture)�podcastr�r�)r�r�r�) r�dataclassesrrrr�typingrrrr	r
143  �enumrr
r"rDr[r�rr�r�printr�r�rzr~rUr�r�rRr�rrr �<module>r�s����0)�(�1�1������*�*��*�*�& �& ��& �Ru�u�p6 �6 �v'8�&B�&B��"�z��	�
144  1�2��,=�,H�,H�I�H�*�8�4�H�
�"�"�;�0S�T��"�"�;�0V�W��"�"�;�0T�U�
�#�#�K�1U�V��#�#�K�1Y�Z��#�#�K�1N�O�>�
145  �
�"�:�,�d�3�4��2�2�:�>��
�g�&�&�(�)�>�
146147  4�5�	�(�
148  $�
149  $�[�
150  1�2�	�
151  #�$�5r