/ core / attention / __pycache__ / daemon.cpython-314.pyc
daemon.cpython-314.pyc
  1  +
  2  -5gi�,��p�Rt^RIt^RIt^RIt^RIHtHt^RIHt^RIH	t	H
  3  t
  4  Ht^RIt]PP^]!]!]4P"P"P"44^RIHtHt^RIHtHtHtHtHt^RIHtHtHt^RIH t H!t!!R	R
  5  4t"Rt#]$R8Xd
  6  ]#!4R#R#)
a
  7  Attention System Daemon
  8  
  9  The always-running service that activates the attention system:
 10  1. Watches session streams for new content
 11  2. Extracts decisions and attention events
 12  3. Computes coherence risk across sessions
 13  4. Injects cross-session awareness via hooks or file injection
 14  5. Surfaces aha moments to daily note
 15  
 16  Run as: python -m core.attention.daemon
 17  
 18  Or install as a launchd service (macOS):
 19      cp sovereign_attention.plist ~/Library/LaunchAgents/
 20      launchctl load ~/Library/LaunchAgents/sovereign_attention.plist
 21  N)�datetime�	timedelta��Path)�Optional�List�Dict)�create_cross_session_system�CrossSessionEvent)�create_coherence_system�SessionDecision�DecisionType�CoherenceRiskDetector�AdaptiveMembrane)�create_aha_system�AhaDetector�AhaCandidate)�create_context_stream�
ContextStreamc�^a�]tRt^'toRt^tR]P3R]P3R]P3R]P3.t
 22  RV3RlRlltV3R	lR
 23  ltV3RlRlt
V3R
lRltV3RlRltV3RlRltV3RlRltV3RlRltV3RlRltV3RlRltV3RlRltRtVtR#)�AttentionDaemonz�
 24  The attention system daemon.
 25  
 26  Continuously:
 27  1. Watches session files for changes
 28  2. Extracts attention events and decisions
 29  3. Updates coherence risk
 30  4. Injects cross-session awareness
 31  5. Detects and surfaces aha moments
 32  zO(?:let's|we should|I'll|going to|decided to|choosing to)\s+(\w+(?:\s+\w+){0,3})zN(?:the|this|our)\s+architecture\s+(?:is|will be|should)\s+(\w+(?:\s+\w+){0,3})z-(?:principle|rule|always|never):\s*(.{10,50})z3(?:design|approach|pattern):\s*(\w+(?:\s+\w+){0,3})Nc�,<�V^8�dQhRS[RS[RS[/#)��sessions_dir�daily_notes_dir�
 33  state_file)�str)�format�
__classdict__s"��8/Users/rcerf/repos/Sovereign_OS/core/attention/daemon.py�__annotate__�AttentionDaemon.__annotate__Bs)����������	�c��\V4Vn\V4VnV'd\V4MVPR,Vn\	\V44wVnVn\4wVn	Vn
 34  \\V4R7wVnVn
\RR7wVnVnVPP#VP$4/Vn/VnVP+4R#)z.attention_daemon_state.json)�daily_note_pathiP�)�
 35  max_tokensN)rrrrr	r�cross_session_tracker�session_watcherr�coherence_detector�adaptive_membraner�aha_detector�aha_managerr�context_stream�
stream_bridge�on_convergence�_on_convergence�_file_positions�_last_check�_load_state)�selfrrrs&&&&r�__init__�AttentionDaemon.__init__Bs���!��.���#�O�4���.8�$�z�*�d�>O�>O�Rp�>p���<W����<
 36  �8��"�D�$8�;R�:S�7����!7�.?���0�/
 37  �+���4�+�3H�SX�2Y�/���T�/�	
�"�"�1�1�$�2F�2F�G�02���02���	
���r"c�<�V^8�dQhRR/#�r�returnN�)rrs"�rr r!`s���=�=�T�=r"c�h�VPP4'dW\VP4;_uu_4p\P!V4pVPR/4VnRRR4R#R# +'giR#;i \dp\RT24Rp?R#Rp?ii;i)zLoad daemon state from file.�file_positionsNz[Daemon] Failed to load state: )	r�exists�open�json�load�getr0�	Exception�print)r3�f�state�es&   rr2�AttentionDaemon._load_state`s����?�?�!�!�#�#�
=��$�/�/�*�*�a� �I�I�a�L�E�+0�9�9�5E�r�+J�D�(�+�*�$�*�*�*���
=��7��s�;�<�<��
=�s4�B
�.A9�-B
�9B
 38  	�B
�
 39  B
�
B1�B,�,B1c�<�V^8�dQhRR/#r7r9)rrs"�rr r!js���
 40  9�
 41  9�T�
 42  9r"c�Z�RVPR\P!4P4/p\	VP
 43  R4;_uu_4p\P!W4RRR4R# +'giR#;i \dp\RT24Rp?R#Rp?ii;i)zSave daemon state to file.r;�	last_save�wNz[Daemon] Failed to save state: )
 44  r0r�now�	isoformatr=rr>�dumprArB)r3rDrCrEs&   r�_save_state�AttentionDaemon._save_statejs��	9� �$�"6�"6��X�\�\�^�5�5�7��E��d�o�o�s�+�+�q��	�	�%�#�,�+�+�+���	9��3�A�3�7�8�8��	9�s6�AB�A2�(B�2B	�=B�B�B*�B%�%B*c�@<�V^8�dQhRS[RS[S[,RS[RR/#)r�topic�session_ids�	intensityr8N)rr�float)rrs"�rr r!vs7���
 45  �
 46  ��
 47  ��#�Y�
 48  ��	
 49 50  
 51  �
 52  r"c�B�VPPVVVR7R#)z)Called when sessions converge on a topic.)rQrR�combined_intensityN)r*�detect_from_cross_session)r3rQrRrSs&&&&rr/�AttentionDaemon._on_convergencevs&��	
���3�3��#�(�	4�	
 53  r"c�<<�V^8�dQhRS[RS[RS[S[,/#)r�
 54  session_id�contentr8)rrr)rrs"�rr r!�s.���,�,��,��,�
 55  �o�	�	,r"c
���.p\4pVPEF�wrV\P!WR\P4pVEF�pVP
 56  'dVP
^4MVP
^4p	\V	4^8gV	P4V9dK_VPV	P44\^VP4^d,
 57  4p
 58  \\V4VP4^d,4pW*Vp\P!RV4p
V
'g\P!RV4p
V
'g\P!RV4p
V
'd
 59  V
^,MRpVP\!V\"P$!4VVP4P'RR4V	P)4R74EK�	EK�	V#)z'Extract decisions from session content.z#(\w+)z)\b([a-z]+_[a-z_]+|[a-z]+[A-Z][a-z]+\w*)\bz'\b([A-Z][a-z]{3,}(?:\s+[A-Z][a-z]+)*)\b�general� �_)rZ�	timestamp�
decision_typerQr[)�set�DECISION_PATTERNS�re�finditer�
 60  IGNORECASE�	lastindex�group�len�lower�add�max�start�min�end�findall�appendrrrK�replace�strip)r3rZr[�	decisions�seen�pattern�default_type�matches�match�
decision_textrmro�context�topicsrQs&&&            r�_extract_decisions�"AttentionDaemon._extract_decisions�sn���	��u��%)�%;�%;�!�G��k�k�'�B�M�M�B�G� ��27�/�/�/����A��u�{�{�ST�~�
��}�%��)�]�-@�-@�-B�d�-J������,�,�.�/��A�u�{�{�}�s�2�3���#�g�,��	�	��c�(9�:��!��,�����I�w�7����Z�Z�(T�V]�^�F���Z�Z�(R�T[�\�F�%+��q�	���� � ��)�&�l�l�n�".��+�+�-�/�/��S�9�)�/�/�1�"��3!�&<�F�r"c�$<�V^8�dQhRS[RR/#)r�session_filer8Nr)rrs"�rr r!�s���'�'��'��'r"c��"�RVP9d#VPPR4^,MVPpVP4pTP
 61  P
\T4^4p\T4T8:dR#Y5Rp\T4TP
 62  \T4&TPY&4pTFMpTPPT4\	RTPPRTP24KO	TPP!TR\"P$!4P'42TR,R	R
 63  TR7TU	u.uFq�PNK	p
 64  p	T
 65  'd TP(P+TT
 66  R7R#R# \dp\	RTRT24Rp?R#Rp?ii;iuup	i5i)
z+Process updates from a single session file.�-z[Daemon] Failed to read z: Nz[Daemon] Decision: [�] r_:Ni�N��?�read)�	target_idr[rS�modality�source)rZr|)�stem�split�	read_textrArBr0r@rrir}r(�record_decisionra�valuerQr-�on_attention_eventrrKr`r&�record_activity)r3r�rZr[rE�	start_pos�new_contentrt�decision�dr|s&&         r�_process_session_file�%AttentionDaemon._process_session_file�s����8;�|�?P�?P�8P�\�&�&�,�,�S�1�!�4�Vb�Vg�Vg�
 67  �	�"�,�,�.�G�
 68  �(�(�,�,�S��->��B�	��w�<�9�$���j�)��25�g�,����S��.�/��+�+�J�D�	�!�H��#�#�3�3�H�=��(��)?�)?�)E�)E�(F�b����HX�Y�Z�"�
 69  	
���-�-�#��A�h�l�l�n�&>�&>�&@�%A�B���%����	.�	
 70  �$-�-�9�a�'�'�9��-���&�&�6�6�%��
7�
���9�	��,�\�N�"�Q�C�@�A���	��6.�s=�AG�F#�DG�$G
�8+G�#G
 71  �.G�?G�G
 72  �
 73  Gc�<�V^8�dQhRR/#r7r9)rrs"�rr r!�s���`�`��`r"c	��"�VPPRR7pVFwr#p\RVRVR
RV24K	R#5i)z,Check if any sessions need coherence alerts.r�)�	thresholdz[Daemon] High risk topic: z (z.0%z	) across N)r(�get_high_risk_topicsrB)r3�	high_riskrQ�
 74  risk_scorerRs&    r�_check_for_alerts�!AttentionDaemon._check_for_alerts�sN����+�+�@�@�3�@�O�	�.7�*�E�{��.�u�g�R�
 75  �3�7G�y�Q\�P]�^�_�/8�s�AAc�<�V^8�dQhRR/#r7r9)rrs"�rr r!�s���I�I�T�Ir"c���"�VPP\P!4\	^R7,
 76  R7pVF2p\RVPPRVP24K4	R#5i)z"Surface any validated aha moments.)�minutes)�sincez[Daemon] Aha: [r�N)	r*�get_validated_ahasrrKrrB�aha_typer��summary)r3�recent_ahas�ahas&  r�
_surface_ahas�AttentionDaemon._surface_ahas�sc����'�'�:�:��,�,�.�9�Q�#7�7�;�
 77  ���C��O�C�L�L�$6�$6�#7�r�#�+�+��G�H��s�A;A=c�<�V^8�dQhRR/#r7r9)rrs"�rr r!�s�����T�r"c���"�\VPPR44pVFpVPV4GRjx�L
 78  K	VPP4pV'd/\
RVUu.uFqDPR,NK	up24VP4GRjx�L
 79  VP4GRjx�L
 80  VP4R#L�uupiL5L5i)zOne tick of the daemon loop.z	*-live.mdNz[Daemon] Context exited: :N�N)�listr�globr�r,�tickrB�idr�r�rN)r3�
session_filesr��exitedrEs&    r�_tick�AttentionDaemon._tick�s�����T�.�.�3�3�K�@�A�
�*�L��,�,�\�:�:�:�*��$�$�)�)�+����-�&�.I�&�Q�t�t�C�y�y�&�.I�-J�K�L��$�$�&�&�&�� � �"�"�"�	
����
;��
 81  /J�	'�	#�sG�>C&�C�)C&�+
 82  C&�5C
 83  �C&�,C"�-C&�C$�C&�C&�$C&c�<�V^8�dQhRR/#r7r9)rrs"�rr r!s���<�<�4�<r"c��\"�\R4\RVP24\RVP24VP4GRjx�L
 84  \
 85  P!VP4GRjx�L
 86  KDL. \dp\RT24Rp?LJRp?ii;iL.5i)zRun the daemon loop.z"[Daemon] Starting attention daemonz[Daemon] Watching: z[Daemon] Daily notes: Nz[Daemon] Error in tick: )rBrrr�rA�asyncio�sleep�POLL_INTERVAL_SECONDS)r3rEs& r�run�AttentionDaemon.runs����
�2�4�
�#�D�$5�$5�#6�7�8�
�&�t�';�';�&<�=�>��
6��j�j�l�"�"��-�-�� :� :�;�;�;�	#���
6��0���4�5�5��
6��
<�sM�=B,�B�B�B�#B,�;B*�<B,�B�B'�B"�B,�"B'�'B,c� <�V^8�dQhRS[/#)rr8)r)rrs"�rr r!s���
 87  �
 88  �$�
 89  r"c
 90  ��^RIpVP!4pVP!V4VPVP	44VP4VPP4pR\VP4RVPR\VP4R\VPP4/# TP4i;i)z,Run one tick and return stats (for testing).N�sessions�
 91  attractors�stream_items�aha_candidates)r��new_event_loop�set_event_loop�run_until_completer��closer&�	get_staterir��cross_session_attractorsr,r*�
 92  candidates)r3r��looprDs&   r�run_once�AttentionDaemon.run_onces�����%�%�'�����t�$�	��#�#�D�J�J�L�1��J�J�L��*�*�4�4�6����E�N�N�+��%�8�8��C�� 3� 3�4��c�$�"3�"3�">�">�?�	
 93  �	
 94  ��
�J�J�L�s�C	�	C)
r0r1r)r*r+r(r,r&rr'rrr-)N)�__name__�
 95  __module__�__qualname__�__firstlineno__�__doc__r�r
�IMPLEMENTATION�ARCHITECTURE�	PRINCIPLErcr4r2rNr/r}r�r�r�r�r�r��__static_attributes__�__classdictcell__)rs@rrr's�����	���
 96  
 97  \�]i�]x�]x�y�	Z�\h�\u�\u�v�	9�<�;Q�;Q�R�	?��AZ�AZ�[�	����<=�=�
 98  9�
 99  9�
100  �
101  �,�,�\'�'�R`�`�I�I���.<�<�
102  �
103  r"rc��^RIpVPRR7pVPRRRR7VPRR	R
104  R7VPRRR
R7VP4p\	VP
105  VPR7pVP'd!VP4p\RV24R#\P!VP44R#)zMain entry point.NzAttention System Daemon)�descriptionz--sessions-dirz3/Users/rcerf/repos/Sovereign_Estate/daily/sessions/z!Path to session streams directory)�default�helpz--daily-dirz*/Users/rcerf/repos/Sovereign_Estate/daily/zPath to daily notes directoryz--once�
106  store_truezRun once and exit (for testing))�actionr�)rrz
107  [Daemon] Stats: )�argparse�ArgumentParser�add_argument�
108  parse_argsrr�	daily_dir�oncer�rBr�r�)r��parser�args�daemon�statss     r�mainr�'s����
�
$�
$�1J�
$�
K�F�
109  ����E�
0���
110  ����<�
,���
111  �����
.�������D�
��&�&�����F�
112  �y�y�y����!��
�"�5�'�*�+����F�J�J�L�!r"�__main__)%r�r�rd�sysrr�pathlibr�typingrrrr>�path�insertr�__file__�parent�core.attention.cross_sessionr	r
113  �core.attention.coherence_riskrrr
rr�core.attention.aha_detectionrrr�core.attention.context_streamrrrr�r�r9r"r�<module>r�s����"�	�
114  �(��'�'��������3�t�H�~�,�,�3�3�:�:�;�<�W���V�U�N�}
115  �}
116  �@ "�F�z���F�r"