/ core / attention / __pycache__ / context_stream.cpython-314.pyc
context_stream.cpython-314.pyc
  1  +
  2  �9giIy�
  3  �b�Rt^RIHtHt^RIHtHt^RIHtHtH	t	H
  4  t
  5  HtHtH
t
^RIHt^RIt^RIt^RIHtHt!RR]4t]!R	R
  6  44t]!RR44t!R
R4t!RR4tRARRllt]!RR44t]!RR44t!RR4tRBRRllt]R8XEd�] !R4] !R4]!RR7wt!t"] !R 4]"PGR!R"R#R$R%R&7]"PGR'R(R)R*R%R&7]"PGR+R,R-R.R%R&7]!PI4t%] !R/]%PLR0]%PNR1
24] !R24^RI(t(]!PRPU4FQt+]+PXR!8Xd
  7  R3]+n-K]+PXR'8Xd
  8  R4]+n-K7]+PXR+8XgKJR5]+n-KS	]!P]4t/] !R6]/Uu.uFqPXNK	up24] !R74]!Pa4F;t+] !R8]+PZR9
R:]+PXR;]+PbR<,R=24K=	] !R>4]!PeR'4]!PgR'4t4] !R?]4PjPl24] !R@4R#R#uupi)CaZ
  9  Context Stream - Continuous attention-weighted context management.
 10  
 11  Instead of fill-compact-fill sawtooth, maintain a continuous stream
 12  where items flow in and out based on attention.
 13  
 14  Key insight: Context is a river, not a bucket.
 15  - Items enter when attention lands on them
 16  - Items exit when attention decays below threshold
 17  - High-attention items move slowly (high friction)
 18  - Low-attention items flow quickly (low friction)
 19  - The stream is always near capacity but breathing
 20  
 21  This eliminates the jarring "compaction" events and creates
 22  smooth context evolution that mirrors how human memory works.
 23  )�	dataclass�field)�datetime�	timedelta)�Optional�List�Dict�Tuple�Any�Callable�Set)�EnumN)�detect_signals�SignalDetectionc�.�]tRt^tRtRtRtRtRtRt	Rt
 24  R#)	�StreamPriorityz!Priority levels for stream items.�pinned�active�recent�decaying�exiting�N)�__name__�
 25  __module__�__qualname__�__firstlineno__�__doc__�PINNED�ACTIVE�RECENT�DECAYING�EXITING�__static_attributes__r��@/Users/rcerf/repos/Sovereign_OS/core/attention/context_stream.pyrrs��+�
�F�
�F�
�F��H��Gr#rc��a�]tRt^%toRtRtRt]!]PR7t
 26  ]!]PR7tRtRt
]PtRtRt]!]R7tRtRtRtV3R	ltR
 27  tVtR#)�
 28  StreamItemz�
 29  An item in the context stream.
 30  
 31  Each item has:
 32  - Content (the actual context)
 33  - Attention score (current relevance)
 34  - Velocity (how fast it's moving toward exit)
 35  - Entry time (when it entered the stream)
 36  ��?��default_factory���?�Fc�4�VPVP8#)z;For heap ordering - lower attention = higher exit priority.��attention_score)�self�others&&r$�__lt__�StreamItem.__lt__Hs���#�#�e�&;�&;�;�;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
 37  &S[;R&S[;R&S[S[,;R
&S[;R&S[;R&#)��id�content�token_countr/�peak_attention�
last_attended�
 38  entry_time�velocity�friction�priority�source�	item_type�signal_tags�
signal_weight�needs_validation)�str�int�floatrrr�bool)�format�
__classdict__s"�r$�__annotate__�StreamItem.__annotate__%s�����	�G���L����� � �!�"��#�$�A�%�&�>�'�,��-�.��/�0�4�1�6
��7�8��9�>�S��6�?�@��A�B�"�Cr#rN)rrrrrr/r9rr�nowr:r;r<r=rrr>r?r@�setrArBrCr2�__annotate_func__r"�__classdictcell__�rIs@r$r&r&%sy�����!�O��N�#�H�L�L�A�M� ����>�J��H��H�-�4�4�H��F��I�"�#�6�K��M�"��<�G�r#r&c�Xa�]tRt^MtoRt^tRt^tRtRt	Rt
 39  ^t^t^t
^tV3RltRtVtR#)�StreamStatez$Current state of the context stream.順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
 40  &#)r5�total_tokens�
 41  max_tokens�
 42  item_count�pressure�	flow_rate�
avg_attention�
items_entered�items_exited�tokens_entered�
tokens_exited�rErF)rHrIs"�r$rJ�StreamState.__annotate__Ms����������	�
 43  ��������������������� ��!r#rN)rrrrrrUrVrWrXrYrZr[r\r]r^rNr"rOrPs@r$rRrRMsE����.��L��J��J��H��I��M��M��L��N��M�!�r#rRc��a�]tRt^`toRtRtRtRtRtR4V3RlRllt	V3RlR	lt
 44  V3R
 45  lRltR5V3R
lRlltV3RlRlt
V3RlRltV3RlRltV3RlRltV3RlRltV3RlRltR6V3RlRlltV3RlRltV3RlR ltV3R!lR"ltV3R#lR$ltV3R%lR&ltR7V3R'lR(lltV3R)lR*ltV3R+lR,ltV3R-lR.ltV3R/lR0ltV3R1lR2ltR3tVt R#)8�
ContextStreama�
 46  Continuous attention-weighted context stream.
 47  
 48  Usage:
 49      stream = ContextStream(max_tokens=100000)
 50  
 51      # Items enter when attended
 52      stream.attend("item_1", content="...", tokens=500, intensity=0.8)
 53  
 54      # Stream flows continuously
 55      stream.tick()  # Call periodically to update velocities
 56  
 57      # Get current context (items sorted by attention)
 58      context = stream.get_context(max_tokens=50000)
 59  
 60      # Check what's about to exit
 61      exiting = stream.get_exiting_items()
 62  i,�{�G�z�?皙�����?�$@c�,<�V^8�dQhRS[RS[RS[/#)r5rV�exit_threshold�pressure_thresholdr_)rHrIs"�r$rJ�ContextStream.__annotate__~s.���@�@��@��@�"�	@r#c��WnW nW0n/Vn.Vn^Vn\P!4Vn.Vn	.Vn
 63  R#)�N)rVrgrh�_items�_exit_queue�
_total_tokensrrL�
 64  _last_tick�_on_exit�	_on_enter)r0rVrgrhs&&&&r$�__init__�ContextStream.__init__~sO��%��,��"4��.0���.0������"�,�,�.���=?��
�=?��r#c�:<�V^8�dQhRS[S[.R3,RR/#�r5�callbackN�return�rr&)rHrIs"�r$rJri�s'���'�'��*��t�);� <�'��'r#c�<�VPPV4R#)z1Register callback for when items exit the stream.N)rp�append�r0rvs&&r$�on_exit�ContextStream.on_exit�s���
�
���X�&r#c�:<�V^8�dQhRS[S[.R3,RR/#rurx)rHrIs"�r$rJri�s'���(�(��:�,��*<�!=�(�$�(r#c�<�VPPV4R#)z2Register callback for when items enter the stream.N)rqrzr{s&&r$�on_enter�ContextStream.on_enter�s�������h�'r#Nc�J<�V^8�dQhRS[RS[RS[RS[RS[RS[RS[RS[/#)	r5�item_idr7�tokens�	intensityr?r@rrw)rDrErFrGr&)rHrIs"�r$rJri�se���N�N��N��N��	N�
 65  �N��
N��N��N�
 66  �Nr#c���\P!4pWP9d�VPV,p	\RV	PVR,,4V	n\V	PV	P4V	nW�nRV	nVPV	P4V	n
 67  V'd\PV	n
V	#VeVf\R4h\V4p
 68  WJP ,p\RV4pT;'g
V
 69  P"p\%TTTTTTTRVPV4V'd\PM\P&VVV
 70  P(V
 71  P RV
 72  P(9R7p	W�PV&V;P*V,
un\,P.!VP0V	4VP2Fp
V
!V	4K
	V	#)a�
 73  Attend to an item - either boosting existing or adding new.
 74  
 75  Args:
 76      item_id: Unique identifier for the item
 77      content: Content (required for new items)
 78      tokens: Token count (required for new items)
 79      intensity: Attention intensity (0-1)
 80      source: Where this came from
 81      item_type: Type of item
 82      pinned: If True, item never exits
 83  
 84  Returns:
 85      The StreamItem (new or updated)
 86  r+�333333�?r*z)content and tokens required for new itemsrC)r6r7r8r/r9r:r;r<r=r>r?r@rArBrC)rrLrl�minr/�maxr9r:r<�_compute_frictionr=rrr>�
 87  ValueErrorr�weight_modifier�
 88  should_pinr&r�suggested_tagsrn�heapq�heappushrmrq)r0r�r7r�r�r?r@rrL�item�signal_detection�adjusted_intensityr�rvs&&&&&&&&      r$�attend�ContextStream.attend�s���2�l�l�n���k�k�!��;�;�w�'�D�#&�s�D�,@�,@�9�s�?�,R�#S�D� �"%�d�&9�&9�4�;O�;O�"P�D��!$���D�M� �2�2�4�3G�3G�H�D�M�� .� 5� 5��
��K��?�f�n��H�I�I�*�'�2��&�)I�)I�I�� ��&8�9���:�:�/�:�:�
 89  �����.�-�����+�+�,>�?�.8�^�*�*�n�>S�>S���(�7�7�*�:�:�/�3C�3R�3R�R�
 90  ��$ $���G�����f�$��
���t�'�'��.����H��T�N�'��r#c�&<�V^8�dQhRS[RS[/#�r5r�rw�rDrG)rHrIs"�r$rJri�s�����3��4�r#c�v�WP9d)\PVPV,nR#R#)zPin an item so it never exits.TF)rlrrr>�r0r�s&&r$�pin�ContextStream.pin�s)���k�k�!�,:�,A�,A�D�K�K�� �)��r#c�&<�V^8�dQhRS[RS[/#r�r�)rHrIs"�r$rJri�s�����S��T�r#c��WP9dJVPV,pVP\P8Xd\PVnR#R#)z&Unpin an item so it can exit normally.TF)rlr>rrr�r0r�r�s&& r$�unpin�ContextStream.unpin�s?���k�k�!��;�;�w�'�D��}�}�� 5� 5�5� .� 5� 5��
��r#c�0<�V^8�dQhRS[S[,/#�r5rw�rr&)rHrIs"�r$rJri�s���<�<�d�:�&�<r#c��\P!4pWP,
 91  P4pWnV^8:d.#.pVP	4p\VPP44EF�pVP\P8XdK%WP,
 92  P4p\P!RW`P,4pVPV,VnVP#VP 4VnVP&pW@P(8�dDW@P(,
 93  ^VP(,
 94  ,p	W�P*V	,,
pV\-RVP$4,VnVP VP08d\P2VnMcVP R8d\P4VnM<VP R8d\P6VnM\P8VnVP\P28XgEK�VP;V4EK�	VFpVP=V4K	V#)z�
 95  Advance the stream - decay attention, update velocities, exit items.
 96  
 97  Call this periodically (every second or so) to keep the stream flowing.
 98  
 99  Returns:
100      List of items that exited during this tick
101  r'皙�����?r��333333�?)rrLro�
total_seconds�_compute_pressure�listrl�valuesr>rrr:�math�pow�ATTENTION_HALF_LIFE_SECONDSr9r/r�r=�
BASE_VELOCITYrh�PRESSURE_VELOCITY_BOOSTr�r<rgr!r rrrz�
102  _exit_item)
103  r0rL�dt�exitedrXr��time_since_attention�decay_factor�
base_velocity�pressure_boosts
104  &         r$�tick�ContextStream.tick�s����l�l�n���O�O�#�
2�
2�
4����
��7��I����)�)�+������+�+�-�.�D��}�}�� 5� 5�5��%(�*<�*<�$<�#K�#K�#M� ��8�8�C�)=�@`�@`�)`�a�L�#'�#6�#6��#E�D� �!�2�2�4�3G�3G�H�D�M�!�.�.�M��1�1�1�"*�-D�-D�"D��T�Md�Md�Id�!e���!=�!=��!N�N�
�*�C��T�]�]�,C�C�D�M��#�#�d�&9�&9�9� .� 6� 6��
��%�%��+� .� 7� 7��
��%�%��+� .� 5� 5��
� .� 5� 5��
��}�}�� 6� 6�6��
�
�d�#�G/�L�D��O�O�D�!���
r#c�$<�V^8�dQhRS[RR/#)r5r�rwN)r&)rHrIs"�r$rJri<s�����z��d�r#c���VPVP9dXVPVPV;PVP,unVPFpV!V4K
	R#R#)zRemove an item from the stream.N)r6rlrnr8rp)r0r�rvs&& r$r��ContextStream._exit_item<sS���7�7�d�k�k�!����D�G�G�$����$�"2�"2�2�� �M�M�����*�	"r#c�&<�V^8�dQhRS[RS[/#)r5�	attentionrw�rF)rHrIs"�r$rJriEs���?�?�5�?�U�?r#c�4�RWP,,#)z&Compute friction from attention score.r+)�ATTENTION_FRICTION_SCALE)r0r�s&&r$r��ContextStream._compute_frictionEs���Y�!>�!>�>�>�>r#c� <�V^8�dQhRS[/#r�r�)rHrIs"�r$rJriJs���>�>�5�>r#c�P�\RVPVP,4#)z)Compute stream pressure (how full it is).r+)r�rnrV�r0s&r$r��ContextStream._compute_pressureJs���3��*�*�T�_�_�<�=�=r#c�<<�V^8�dQhRS[RS[RS[S[,/#)r5rV�include_exitingrw)rErGrr&)rHrIs"�r$rJriNs.���#�#��#��#�
105  �j�	�	#r#c�|�VPP4Uu.uF.pV'g"VP\P8wgK,VNK0	ppVPRRR7VfV#.p^pVF@pWcP,V8�dV#VPV4WcP,
pKB	V#uupi)z�
106  Get current context items, sorted by attention.
107  
108  Args:
109      max_tokens: Limit total tokens (default: all)
110      include_exiting: Include items about to exit
111  
112  Returns:
113      List of StreamItems sorted by attention (highest first)
114  c��VP#�Nr.)�xs&r$�<lambda>�+ContextStream.get_context.<locals>.<lambda>cs
115  ���!2�!2r#T)�key�reverse)rlr�r>rr!�sortr8rz)r0rVr�r��items�result�totals&&&    r$�get_context�ContextStream.get_contextNs��� "�[�[�/�/�1�
116  �1�T��$�-�-�>�3I�3I�"I�
�D�1�	�
117  �	�
118119  �2�D�
120  �A����L������D��'�'�'�*�4���
�
�M�M�$���%�%�%�E�	��
��)
121  s�B9�B9�B9c�0<�V^8�dQhRS[S[,/#r�r�)rHrIs"�r$rJriss���
122  �
123  �4�
124  �#3�
125  r#c��VPP4Uu.uF&pVP\P8XgK$VNK(	up#uupi)z!Get items that are about to exit.)rlr�r>rr!�r0r�s& r$�get_exiting_items�ContextStream.get_exiting_itemsssJ��"�[�[�/�/�1�
126  �1�T��}�}�� 6� 6�6�
�D�1�
127  �	
128  ��
129  s�!A�Ac�0<�V^8�dQhRS[S[,/#r�r�)rHrIs"�r$rJrizs���
130  �
131  �d�:�.>�
132  r#c��VPP4Uu.uFpVP'gKVNK	up#uupi)zEGet items flagged as needing validation (e.g., 'what do you think?').)rlr�rCr�s& r$�get_items_needing_validation�*ContextStream.get_items_needing_validationzsA��"�[�[�/�/�1�
133  �1�T��$�$�
�D�1�
134  �	
135  ��
136  s�?�?c�6<�V^8�dQhRS[RS[S[,/#)r5�tagrw)rDrr&)rHrIs"�r$rJri�s ���
137  �
138  �C�
139  �D��,<�
140  r#c��VPP4Uu.uFpWP9gKVNK	up#uupi)z%Get items with a specific signal tag.)rlr�rA)r0r�r�s&& r$�get_items_by_tag�ContextStream.get_items_by_tag�sD��"�[�[�/�/�1�
141  �1�T��&�&�&�
�D�1�
142  �	
143  ��
144  s�=�=c�0<�V^8�dQhRS[S[,/#r�r�)rHrIs"�r$rJri�s���2�2��Z� 0�2r#c�$�VPR4#)zGet items tagged as principles.�	principle�r�r�s&r$�get_principles�ContextStream.get_principles�s���$�$�[�1�1r#c�0<�V^8�dQhRS[S[,/#r�r�)rHrIs"�r$rJri�s���1�1�t�J�/�1r#c�$�VPR4#)zGet items tagged as decisions.�decisionr�r�s&r$�
get_decisions�ContextStream.get_decisions�s���$�$�Z�0�0r#c�,<�V^8�dQhRS[RS[RS[/#)r5r��	validatedrwr�)rHrIs"�r$rJri�s"�����S��T��T�r#c���WP9dR#VPV,pRVnV'dz\RVPR,4Vn\	VP
145  VP4Vn\PVnVPPR4R#V;PR,unVPPR4R#)z�
146  Mark an item as validated (or not).
147  
148  If validated=True, the item gets boosted and the needs_validation flag cleared.
149  If validated=False, the item gets demoted.
150  Fr+g�������?r�r'�rejectedT)rlrCr�r/r�r9rrr>rA�add)r0r�r�r�s&&& r$�
validate_item�ContextStream.validate_item�s����+�+�%���{�{�7�#�� %����#&�s�D�,@�,@�3�,F�#G�D� �"%�d�&9�&9�4�;O�;O�"P�D��*�1�1�D�M���� � ��-��
� � �C�'� ���� � ��,�r#c� <�V^8�dQhRS[/#r�)rR)rHrIs"�r$rJri�s���
151  
152153  
154  �;�
155  
156  r#c
��\VPP44p\VPVP
157  \
V4VP4\RV44\^\
V44,R7#)zGet current stream state.c3�8"�TFqPx�K	R#5ir�r.)�.0�is& r$�	<genexpr>�*ContextStream.get_state.<locals>.<genexpr>�s���?��A�/�/��s�)rUrVrWrXrZ)
158  r�rlr�rRrnrV�lenr��sumr�)r0r�s& r$�	get_state�ContextStream.get_state�sg���T�[�[�'�'�)�*����+�+�����5�z��+�+�-��?��?�?�#�a��U��BT�T�
159  �	
160  r#c�&<�V^8�dQhRS[RS[/#r�r�)rHrIs"�r$rJri�s�����#��$�r#c�r�WP9d'VPV,pVPV4R#R#)z"Force an item to exit immediately.TF)rlr�r�s&& r$�
161  force_exit�ContextStream.force_exit�s,���k�k�!��;�;�w�'�D��O�O�D�!��r#c�6<�V^8�dQhRS[RS[S[,/#r�)rDrr&)rHrIs"�r$rJri�s ���(�(��(���(<�(r#c�8�VPPV4#)zGet a specific item by ID.)rl�getr�s&&r$�get_item�ContextStream.get_item�s���{�{���w�'�'r#c� <�V^8�dQhRS[/#r�)rE)rHrIs"�r$rJri�s��� � �� r#c�,�\VP4#r�)r�rlr�s&r$�__len__�ContextStream.__len__�s���4�;�;��r#c�&<�V^8�dQhRS[RS[/#r�r�)rHrIs"�r$rJri�s���&�&�C�&�D�&r#c��WP9#r�)rlr�s&&r$�__contains__�ContextStream.__contains__�s���+�+�%�%r#)	rmrlrorqrprnrgrVrh)rSr���������?)NNr'r,r,F)NF)T)!rrrrrr�r�r�r�rrr|r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr	r
rr"rOrPs@r$rbrb`s
162  �����(#&���M�"�� $��@�@�0'�'�(�(�N�N�`����<�<�|��?�?�
163  >�>�#�#�J
164  �
165  �
166  �
167  �
168  �
169  �2�2�1�1���4
170  
171172  
173  ���(�(� � �&�&r#rbc�La�]tRtRtoRtR	V3RlRlltV3RlRltRtVtR#)
174  �AttentionStreamBridgei�z�
175  Bridges the attention system to the context stream.
176  
177  Attention events flow into the stream, maintaining continuous
178  context that evolves with attention.
179  c�&<�V^8�dQhRS[RS[/#)r5�stream�default_tokens)rbrE)rHrIs"�r$rJ�"AttentionStreamBridge.__annotate__�s���-�-��-��-r#c��WnW nR#r�)rr)r0rrs&&&r$rr�AttentionStreamBridge.__init__�s��
180  ��,�r#c�><�V^8�dQhRS[RS[RS[RS[RS[RS[/#)r5�	target_idr7r��modalityr?rw)rDrFr&)rHrIs"�r$rJr�sG���%
181  �%
182  ��%
183  ��%
184  ��	%
185186  �%
187  ��
%
188  �
189  �%
190  r#c���RRRRRRRRR	R
191  RR/pVPVR
192  4pW7,p\VP\V4^,4p	VPPVVV	VVVR
7#)u�
193  Process an attention event into the stream.
194  
195  Higher intensity → higher initial attention
196  Different modalities have different weights
197  �	highlightr+�read�ffffff�?�gaze皙�����?�listenr��searchr'�glancer�)r�r7r�r�r?r@)rr�rr�rr�)
198  r0rr7r�rr?�modality_weights�weightr�r�s
199  &&&&&&    r$�on_attention_event�(AttentionStreamBridge.on_attention_event�s��� 
���C��C��c��c��c�

200  ��"�%�%�h��4��&�/���T�(�(�#�g�,�!�*;�<���{�{�!�!����(���
"�
201  �	
202  r#)rrN)�d)	rrrrrrrr*r"rOrPs@r$rr�s�����-�-�%
203  �%
204  r#rc�^�V^8�dQhR\R\R\\\3,/#)r5rVrgrw)rErFr	rbr)rHs"r$rJrJs1���������=�/�/�0�r#c�8�\VVR7p\V4pW#3#)ze
205  Create a context stream with attention bridge.
206  
207  Returns:
208      (ContextStream, AttentionStreamBridge)
209  )rVrg)rbr)rVrgr�bridges&&  r$�create_context_streamr0s(����%��F�#�6�
*�F��>�r#c�Za�]tRtRtoRt]!]PR7tRt	V3Rlt
210  RtVtR#)�EngagementEventiz�
211  Records when operator engages with a context item.
212  
213  Used to learn which items they actually care about vs what
214  the model predicted they'd care about.
215  r(Nc�<�V^8�dQh/S[;R&S[;R&S[;R&S[;R&S[;R&S[S[S[S[3,,;R&#)r5r��engagement_type�attention_at_engagement�time_in_stream�	timestamp�context)rDrFrrrr
216  )rHrIs"�r$rJ�EngagementEvent.__annotate__s_�����L�����#�"������=���d�3��8�n�
%�,�r#r)
rrrrrrrrLr7r8rNr"rOrPs@r$r2r2s&����� ����=�I�(,�G��r#r2c�pa�]tRtRtoRtRtRtRtRtRt	Rt
217  ]!R	R
218  7t]!RR
219  7t
V3RltR
tVtR#)�StreamParametersi'z,
220  Tunable parameters for the context stream.
221  g�r@rcrdrer�rc��RRRRRRRRRR	R
222  R/#)r r+r!r"r#r$r%r�r&r'r'r�rrr#r$r��StreamParameters.<lambda>4s&���S������#��#��#�
Hr#r(c��RRRRRRRR/#)�conversationr+�filer$r&r��externalr'rrr#r$r�r=>s�������#��C�	Fr#c�<�V^8�dQh/S[;R&S[;R&S[;R&S[;R&S[;R&S[;R&S[S[S[3,;R&S[S[S[3,;R&#)	r5�attention_half_lifer��pressure_velocity_boost�attention_friction_scalergrhr(�source_weights)rFrrD)rHrIs"�r$rJ�StreamParameters.__annotate__'s�����
223  �&����
�#�)��$�*������#���3��:�&���.��e��$��/r#rN)rrrrrrCr�rDrErgrhrr(rFrNr"rOrPs@r$r;r;'sY�����"'���M�%)��&*���N� #��*/�@�*��(-�>�(�N�/�r#r;c�a�]tRtRtoRtRt^�t]3V3RlRlltRV3RlRlltV3R	lR
224  lt	Rt
225  V3RlR
ltV3RlRltV3RlRlt
RtVtR#)�ContextStreamTuneriFaV
226  Learns optimal context stream parameters from operator behavior.
227  
228  The key insight: what the operator ACTUALLY engages with tells us
229  what should stay in context vs what should decay faster.
230  
231  Learns:
232  - Half-life: If operator often references "old" items, increase half-life
233  - Exit threshold: If operator revives exiting items, threshold is too high
234  - Modality weights: Which input modalities predict actual engagement
235  - Source weights: Which sources provide lasting-relevant context
236  
237  This is the context window equivalent of the ResonanceWeightTuner -
238  learning preferences implicitly from behavior.
239  rdc�,<�V^8�dQhRS[RS[RS[/#)r5r�operator_id�
learning_rate)rbrDrF)rHrIs"�r$rJ�ContextStreamTuner.__annotate__Zs)���'�'��'��'��	'r#c�r�WnW nW0n\4Vn.Vn.VnRVnR#)r*N)rrKrLr;�params�engagement_events�prediction_errors�revival_rate)r0rrKrLs&&&&r$rr�ContextStreamTuner.__init__Zs=����&��*��'�(���9;���/1���#&��r#Nc	�R<�V^8�dQhRS[RS[RS[S[S[S[3,,/#)r5r�r4r8)rDrrr
240  )rHrIs"�r$rJrMns6���+-�+-��+-��+-��$�s�C�x�.�)�	+-r#c	�
241  �VPPV4pV'dBVPp\P!4VP
242  ,
243  P
4pMRpRp\VVVV\P!4VR7pVPPV4\VP4VP^,8�d VPVP)RVnVPWt4R#)a�
244  Record when operator engages with a context item.
245  
246  engagement_type:
247  - 'reference': Operator explicitly referenced this item
248  - 'expand': Operator asked for more detail on this item
249  - 'action': Operator took action based on this item
250  - 'ignore': Item was presented but not engaged with
251  - 'dismiss': Operator explicitly dismissed/cleared this item
252  - 'revive': Operator brought back an exiting/exited item
253  r*)r�r4r5r6r7r8N�����)
rr	r/rrLr;r�r2rPrzr��WINDOW_SIZE�_update_parameters)r0r�r4r8r�r5r6�events&&&&    r$�record_engagement�$ContextStreamTuner.record_engagementns���"�{�{�#�#�G�,���&*�&:�&:�#�&�l�l�n�t���>�M�M�O�N�'*�#��N���+�$;�)��l�l�n��

254  ��	
���%�%�e�,��t�%�%�&��)9�)9�A�)=�=�%)�%;�%;�T�=M�=M�<M�<N�%O�D�"�	
����,r#c�6<�V^8�dQhRS[RS[S[,/#)r5rYr�)r2rr&)rHrIs"�r$rJrM�s$���A!�A!��A!�x�
255  �?S�A!r#c�(�VPR9dDVPR8dRVP,
256  pMRpVPPV4MIVPR8Xd9VPR8�d(VPpVPPV4VPR8XgVP^8d�VP
257  ;P^VP,
258  ,un\RVP
259  P4VP
260  nVP
261  ;P^VPR,,,un	VPR9d�VPVP
262  P8�d}\R	VPVP
263  P,4pVPVR,
264  ,R
265  ,pVP
266  ;P^V,,un	VPR8XdMVPR8�d<VP
267  ;P^VPR,,,unV'd�VP'd�VPR9dVPpW`P
268  P9dYVP
269  PV,p\R
V^VP,,4VP
270  PV&V'd�VP'd�VPR8XdVPpW`P
271  P9dYVP
272  PV,p\R
273  V^VP,
274  ,4VP
275  PV&\VP4VP8�d VPVP)RVnVP!4R#)z3Update stream parameters based on engagement event.r�r+r*�ignorer"�revivercr'g@r��dismissr�g�?N)�	reference�expand�action)r4r5rQrzr6rOrgrLr�rCr�r�r@r(r�rW�_apply_parameters)r0rYr��error�factor�
276  adjustmentr�currents&&&     r$rX�%ContextStreamTuner._update_parameters�s���� � �$E�E��,�,�s�2��e�;�;�;�����"�"�)�)�%�0�
�
"�
"�h�
.��,�,�s�2��5�5���&�&�-�-�e�4�
277  � � �H�,��0D�0D�q�0H��K�K�&�&�1�t�/A�/A�+A�B�&�),�T�4�;�;�3M�3M�)N�D�K�K�&�
�K�K�+�+��D�4F�4F��4L�0L�M�+�� � �$E�E��#�#�d�k�k�&E�&E�E��S�%�"6�"6����9X�9X�"X�Y��!�/�/�6�C�<�@�3�F�
278  ����/�/�A�
279  �N�C�/�� � �I�-�%�2O�2O�RU�2U�
�K�K�%�%�!�d�.@�.@�3�.F�*F�G�%��D�N�N�N�u�'<�'<�@a�'a��~�~�H��;�;�7�7�7��+�+�6�6�x�@��9<�S�'�Q�QU�Qc�Qc�Mc�Bd�9e����,�,�X�6��D�N�N�N�u�'<�'<��'H��~�~�H��;�;�7�7�7��+�+�6�6�x�@��9<�S�'�Q�QU�Qc�Qc�Mc�Bd�9e����,�,�X�6��t�%�%�&��)9�)9�9�%)�%;�%;�T�=M�=M�<M�<N�%O�D�"�	
��� r#c���VPPVPnVPPVPnVPPVPnVPPVPn	VPPVPn
280  VPPVPnR#)z'Apply learned parameters to the stream.N)rOrCrr�r�r�rDr�rEr�rgrhr�s&r$rd�$ContextStreamTuner._apply_parameters�s���26�+�+�2Q�2Q����/�$(�K�K�$=�$=����!�.2�k�k�.Q�.Q����+�/3�{�{�/S�/S����,�%)�[�[�%?�%?����"�)-���)G�)G����&r#c�6<�V^8�dQhRS[S[S[3,/#r��rrDr
281  )rHrIs"�r$rJrM�s���
282  �
283  �4��S��>�
284  r#c��VP'dVPR
RM.p\V4\^\V44,p/pVPVP
285  )RF2pVP
VP^4^,W4P&K4	RVPR\VP4RVRVRRVPPRVPPR	VPPR
286  VPP/R\VPP4R\VPP 4/#)z#Get report on current tuning state.NrK�total_events�avg_prediction_error�engagement_counts�current_parametersrCr�rgrhr(rFi����)rQr�r�r�rPrWrr4rKrOrCr�rgrh�dictr(rF)r0�
recent_errors�	avg_error�type_countsrYs&    r$�get_tuning_report�$ContextStreamTuner.get_tuning_report�s+��8<�8N�8N�8N��.�.�s�t�4�TV�
��
�&��Q��M�0B�)C�C�	����+�+�T�-=�-=�,=�,>�?�E�1<����AV�AV�XY�1Z�]^�1^�K�-�-�.�@�
�4�+�+��C�� 6� 6�7�"�I��� �%�t�{�{�'F�'F�����!:�!:� �$�+�+�"<�"<�$�d�k�k�&D�&D�	#�
��T�[�[�%A�%A� B��d�4�;�;�#=�#=�>�

287288  r#c�6<�V^8�dQhRS[S[S[3,/#r�rm)rHrIs"�r$rJrMs���
289  �
290  �4��S��>�
291  r#c��RVPPRVPPRVPPRVPPRVPP
292  RVPPR\VPP4R\VPP4/#)	z"Export parameters for persistence.rCr�rDrErgrhr(rF)
293  rOrCr�rDrErgrhrsr(rFr�s&r$�export_parameters�$ContextStreamTuner.export_parameterss���
"�4�;�;�#B�#B��T�[�[�6�6�%�t�{�{�'J�'J�&����(L�(L��d�k�k�8�8� �$�+�+�"@�"@���T�[�[�%A�%A� B��d�4�;�;�#=�#=�>�	
294295  r#c�6<�V^8�dQhRS[S[S[3,/#)r5rOrm)rHrIs"�r$rJrM
s���!�!��S�#�X��!r#c�:�RV9dVR,VPnRV9dVR,VPnRV9dVR,VPnRV9dVR,VPnRV9dVR,VPnRV9dVR,VPnRV9d"\VR,4VPnRV9d"\VR,4VPn	VP4R	#)
296  z#Import parameters from persistence.rCr�rDrErgrhr(rFN)rOrCr�rDrErgrhrsr(rFrd)r0rOs&&r$�import_parameters�$ContextStreamTuner.import_parameters
s��� �F�*�.4�5J�.K�D�K�K�+��f�$�(.��(?�D�K�K�%�$��.�28�9R�2S�D�K�K�/�%��/�39�:T�3U�D�K�K�0��v�%�)/�0@�)A�D�K�K�&��6�)�-3�4H�-I�D�K�K�*���'�+/��7I�0J�+K�D�K�K�(��v�%�)-�f�5E�.F�)G�D�K�K�&���� r#)rPrLrKrOrQrRrr�)rrrrr�DEFAULT_LEARNING_RATErWrrrZrXrdrwr{rr"rOrPs@r$rIrIFsb����� !���K� 5�	'�'�(+-�+-�ZA!�A!�FH�
297  �
298  �2
299  �
300  �!�!r#rIc��V^8�dQhR\R\R\\\3,R\\
301  \\3,/#)r5rKrV�saved_paramsrw)rDrErr
302  r	rbrrI)rHs"r$rJrJ#sF��!�!��!��!��s�C�x�.�!��=�/�1C�C�D�	!r#c��\VR7p\V4p\W04pV'dVPV4W4V3#)zz
303  Create a context stream with tuning capability.
304  
305  Returns:
306      (ContextStream, AttentionStreamBridge, ContextStreamTuner)
307  �rV)rbrrIr)rKrVr�rr/�tuners&&&   r$�create_tuned_context_streamr�#s>���j�
1�F�
"�6�
*�F��v�3�E��
����-��5� � r#�__main__z=== Context Stream Demo ===
308  z"Context is a river, not a bucket.
309  i'r�z)1. Adding items with varying attention...�item_1z.High attention item - actively working on thisrr r?)rr7r�rr?�item_2z$Medium attention - mentioned brieflyr'r!�item_3zLow attention - barely glancedg�������?r'z
310     Items: z, Pressure: z.1%z!
311  2. Simulating attention decay...r$r�rdz   Exited: z*
312  3. Current context (sorted by attention):z   [z.2fz] z: :N�(Nz...z4
313  4. Pinning an item (unresolved items never exit)...z   item_2 priority: z'
314  === Stream breathing, not sawteeth ===)rSr�)rSN)7r�dataclassesrrrr�typingrrrr	r
315  rr�enumr
r�r��signal_wordsrrrr&rRrbrr0r2r;rIr�r�printrr/r*r�staterWrX�timerlr�r�r6r/r�r�r�r7r�r	�item2r>�value)�es0r$�<module>r�s����")�(�B�B�B����9��T���$<�$<��$<�N�����$f&�f&�R5
316  �5
317  �p�,�-�-��-������<Z!�Z!�z!�*�z��	�
318  )�*�	�
319  /�0�*�e�<�N�F�F�
320321  5�6�
322  ����@����������6����������0������
����E�	�J�u�'�'�(��U�^�^�C�4H�
323  I�J�
324325  .�/���
�
�$�$�&���7�7�h��#&�D� �
�W�W��
 �#&�D� �
�W�W��
 �#'�D� �
'��[�[�]�F�	�K�v�.�v�!���v�.�/�
326  0�1�
327328  7�8��"�"�$��
��T�)�)�#�.�b����	��D�L�L��<M�;N�c�R�S�%�
329330  A�B�
331  �J�J�x���O�O�H�%�E�	� ����!5�!5� 6�
332  7�8�	�
333  4�5�}��d/s�#J,