/ core / database / __pycache__ / god_db.cpython-314.pyc
god_db.cpython-314.pyc
  1  +
  2  ��fi�c��&�Rt^RIt^RIt^RIt^RIHt^RIHt^RIH	t	H
  3  t
  4  HtHtH
t
^RIHt^RIHtHtHtHtHt!RR4t]R	8XEdC^RIt^RIt]P4P7]P8!4R
  5  4t]!]4t]!R4]PARR
RR.R7t!]!R]!PR,R24]PARR
R.R7t"]!R]"PR,R24]PG]!P]"P]PHR7t%]!R]%PR,R24]PM]!PRR7]PM]!PRR7]!R4]PO]!P4t(]!R4]!R](PRR,R24]!R](PT24]!R](PV24]PY]!P4t-]!R ].!]-424]P_]!P^R!7wt0t1]!R"].!]04R#].!]14R$24]PeR
4t3]!R%]324]Ph!]4]!R&4R#R#)'a1
  6  God Database Implementation
  7  
  8  The foundational data layer that stores all information as atomic bullets
  9  with full graph relationships, temporal indexing, and prediction state.
 10  
 11  This is the source of truth for all data in the Sovereign OS system.
 12  All other components read from and write to this database.
 13  N)�datetime)�Path)�Optional�List�Dict�Any�Tuple)�contextmanager)�Bullet�Edge�EdgeType�ContentType�PredictionTargetc��a�]tRt^toRtR/V3RlRllt]R4tRtR]	PRR3V3RlRlltV3R	lR
 14  ltR0V3RlRllt
V3R
lRltV3RlRltV3RlRlt]P$RRR3V3RlRlltV3RlRltV3RlRltV3RlRltV3RlRltV3R lR!ltV3R"lR#ltR1V3R$lR%lltR2V3R&lR'lltR3V3R(lR)lltR4V3R*lR+lltR5V3R,lR-lltR.tVt R#)6�GodDatabasea2
 15  The God Database - atomic storage for the entire system.
 16  
 17  Design principles:
 18  1. Bullets are atomic - they are the smallest unit
 19  2. UUIDs persist through all transformations
 20  3. Content never leaves the blanket
 21  4. Structure (edges, topology) can be extracted for sharing
 22  5. Temporal state is always tracked
 23  c� <�V^8�dQhRS[/#)��db_path��str)�format�
__classdict__s"��7/Users/rcerf/repos/Sovereign_OS/core/database/god_db.py�__annotate__�GodDatabase.__annotate__"s���	�	��	�c��\V4VnVPPPRRR7VP	4R#)zO
 24  Initialize the God Database.
 25  
 26  Args:
 27      db_path: Path to SQLite database file
 28  T)�parents�exist_okN)rr�parent�mkdir�_init_db)�selfrs&&r�__init__�GodDatabase.__init__"s5���G�}��������!�!�$��!�>��
�
�rc#��"�\P!\VP44p\PVnVx�VP
4R# TP
4i;i5i)z.Get a database connection with proper cleanup.N)�sqlite3�connectrr�Row�row_factory�close�r"�conns& r�	_get_conn�GodDatabase._get_conn-sF������s�4�<�<�0�1��"�;�;���	��J��J�J�L��D�J�J�L�s�?A-�A�A-�A*�*A-c���VP4;_uu_4pVPR4VPR4VPR4VPR4VPR4VPR4VPR4VPR4VPR	4VPR
 29  4VP4RRR4R# +'giR#;i)zInitialize database schema.a�
 30                  CREATE TABLE IF NOT EXISTS bullets (
 31                      uuid TEXT PRIMARY KEY,
 32                      blanket_id TEXT NOT NULL,
 33                      content TEXT NOT NULL,
 34                      content_type TEXT DEFAULT 'text',
 35                      parent_uuid TEXT,
 36                      child_uuids TEXT DEFAULT '[]',
 37                      created_at TEXT NOT NULL,
 38                      updated_at TEXT NOT NULL,
 39                      accessed_at TEXT NOT NULL,
 40                      access_count INTEGER DEFAULT 0,
 41                      visible_tags TEXT DEFAULT '[]',
 42                      metadata_tags TEXT DEFAULT '{}',
 43                      prediction_targets TEXT DEFAULT '[]',
 44                      gravity_well_strength REAL DEFAULT 0.0,
 45                      free_energy_score REAL DEFAULT 0.0,
 46                      resonance_score REAL DEFAULT 0.0,
 47                      last_resonance_factors TEXT DEFAULT '{}'
 48                  )
 49              a<
 50                  CREATE TABLE IF NOT EXISTS edges (
 51                      uuid TEXT PRIMARY KEY,
 52                      source_uuid TEXT NOT NULL,
 53                      target_uuid TEXT NOT NULL,
 54                      edge_type TEXT DEFAULT 'relates_to',
 55                      weight REAL DEFAULT 1.0,
 56                      confidence REAL DEFAULT 1.0,
 57                      created_at TEXT NOT NULL,
 58                      last_traversed TEXT NOT NULL,
 59                      traversal_count INTEGER DEFAULT 0,
 60                      predicted INTEGER DEFAULT 0,
 61                      prediction_strength REAL DEFAULT 0.0,
 62                      formation_surprise REAL DEFAULT 0.0,
 63                      FOREIGN KEY (source_uuid) REFERENCES bullets(uuid),
 64                      FOREIGN KEY (target_uuid) REFERENCES bullets(uuid)
 65                  )
 66              a�
 67                  CREATE TABLE IF NOT EXISTS attention_log (
 68                      id INTEGER PRIMARY KEY AUTOINCREMENT,
 69                      bullet_uuid TEXT NOT NULL,
 70                      session_id TEXT,
 71                      timestamp TEXT NOT NULL,
 72                      duration_ms INTEGER DEFAULT 0,
 73                      modality TEXT DEFAULT 'read',
 74                      FOREIGN KEY (bullet_uuid) REFERENCES bullets(uuid)
 75                  )
 76              zECREATE INDEX IF NOT EXISTS idx_bullets_blanket ON bullets(blanket_id)zECREATE INDEX IF NOT EXISTS idx_bullets_parent ON bullets(parent_uuid)zQCREATE INDEX IF NOT EXISTS idx_bullets_resonance ON bullets(resonance_score DESC)zLCREATE INDEX IF NOT EXISTS idx_bullets_accessed ON bullets(accessed_at DESC)zACREATE INDEX IF NOT EXISTS idx_edges_source ON edges(source_uuid)zACREATE INDEX IF NOT EXISTS idx_edges_target ON edges(target_uuid)zMCREATE INDEX IF NOT EXISTS idx_attention_bullet ON attention_log(bullet_uuid)N)r-�execute�commitr+s& rr!�GodDatabase._init_db7s���
�^�^�
�
���L�L��
�.
�L�L��
�(
�L�L�
 77  �
 78  
�
�L�L�`�a��L�L�`�a��L�L�l�m��L�L�g�h��L�L�\�]��L�L�\�]��L�L�h�i��K�K�M�E�
�
�
�s�B;C�C/	Nc�<�V^8�dQhRS[RS[RS[S[,RS[RS[S[S[,,RS[S[S[S[3,,RS[/#)r�content�
 79  blanket_id�parent_uuid�content_type�visible_tags�
metadata_tags�return)rrr
rrrr
 80  )rrs"�rrrst���O�O��O��O��c�]�	O�
 81  "�O��t�C�y�)�
O� ��S�#�X��/�O�
 82  �Orc���\P!4p\\\P
 83  !44TTTTTTTT;'g.T;'g/R7
 84  pVP
4;_uu_4p	V	PRVPVPVPVPPVP\P!VP 4VP"P%4VP&P%4VP(P%4VP*\P!VP,4\P!VP.4\P!.4VP0VP2VP4\P!VP6434V'dqVP9V4p
 85  V
 86  'dXV
 87  P P;VP4V	PR\P!V
 88  P 4V34V	P=4RRR4V# +'giT#;i)a5
 89  Create a new atomic bullet.
 90  
 91  Args:
 92      content: The content of the bullet
 93      blanket_id: Which Markov blanket owns this
 94      parent_uuid: Optional parent for hierarchy
 95      content_type: Type of content
 96      visible_tags: Human-readable tags
 97      metadata_tags: System metadata
 98  
 99  Returns:
100      The created Bullet
101  )
102  �uuidr5r4r7r6�
103  created_at�
104  updated_at�accessed_atr8r9a�
105                  INSERT INTO bullets (
106                      uuid, blanket_id, content, content_type, parent_uuid,
107                      child_uuids, created_at, updated_at, accessed_at,
108                      access_count, visible_tags, metadata_tags,
109                      prediction_targets, gravity_well_strength,
110                      free_energy_score, resonance_score, last_resonance_factors
111                  ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
112              z1UPDATE bullets SET child_uuids = ? WHERE uuid = ?N)r�nowr
113  r�uuid_lib�uuid4r-r0r<r5r4r7�valuer6�json�dumps�child_uuidsr=�	isoformatr>r?�access_countr8r9�gravity_well_strength�free_energy_score�resonance_score�last_resonance_factors�
114  get_bullet�appendr1)r"r4r5r6r7r8r9r@�bulletr,rs&&&&&&&    r�
create_bullet�GodDatabase.create_bullets���.�l�l�n����X�^�^�%�&�!��%�#����%�+�+��'�-�-�2�
115  ���^�^�
�
���L�L������!�!�����#�#�)�)��"�"��
116117  �6�-�-�.��!�!�+�+�-��!�!�+�+�-��"�"�,�,�.��#�#��
118119  �6�.�.�/��
120121  �6�/�/�0��
122123  �2���,�,��(�(��&�&��
124125  �6�8�8�9�#�
�:�����5����&�&�-�-�f�k�k�:��L�L�K����F�$6�$6�7��E��
126  
�K�K�M�O�R�
�S�
�R�
�s�3E<I!�0A'I!�!I2	c�6<�V^8�dQhRS[RS[S[,/#�rr<r:)rrr
127  )rrs"�rrr�s ���
128  ,�
129  ,�s�
130  ,�x��'7�
131  ,rc���VP4;_uu_4pVPRV34P4pV'gRRR4R#VPV4uuRRR4# +'giR#;i)zGet a bullet by UUID.z$SELECT * FROM bullets WHERE uuid = ?N)r-r0�fetchone�_row_to_bullet�r"r<r,�rows&&  rrM�GodDatabase.get_bullet�s_��
�^�^�
�
���,�,�6�����h�j�
���
�
��&�&�s�+��
�
�
���+A)�A)�)A:	c	�R<�V^8�dQhRS[RS[S[,RS[RS[S[,/#)rr5�limit�order_byr:�rr�intrr
132  )rrs"�rrr�s;���
>�
>��
>���}�
>��	
>�
133  
134  �f��
>rc�,�VP4;_uu_4pRV2pV'd
VRV2,
pVPWQ34P4pVUu.uFqpPV4NK	upuuRRR4#uupi +'giR#;i)z%Get all bullets for a Markov blanket.z4SELECT * FROM bullets WHERE blanket_id = ? ORDER BY z LIMIT N�r-r0�fetchallrV)r"r5r\r]r,�query�rowsrXs&&&&    r�get_bullets_for_blanket�#GodDatabase.get_bullets_for_blanket�s����^�^�
�
��J�8�*�U�E���7�5�'�*�*���<�<��}�5�>�>�@�D�8<�=���'�'��,��=�
�
��>�
�
�
�s�>B�A=�0B�=B�B	c�R<�V^8�dQhRS[RS[S[S[3,RS[S[,/#)rr<�updatesr:)rrrrr
135  )rrs"�rrr�s6���<�<��<��c�3�h��<�
136  �&�	�	<rc���VPV4pV'gR#VP4F$wrE\W44'gK\W4V4K&	\P
137  !4VnVP4;_uu_4pTPRVPVPPVP\P!VP4VPP!4\P!VP"4\P!VP$4\P!VP&Uu.uFqwP(NK	up4VP*VP,VP.\P!VP04V3
4VP34RRR4V#uupi +'giT#;i)z�
138  Update a bullet.
139  
140  Args:
141      uuid: Bullet UUID
142      updates: Dict of field -> value to update
143  
144  Returns:
145      Updated bullet or None if not found
146  Na/
147                  UPDATE bullets SET
148                      content = ?,
149                      content_type = ?,
150                      parent_uuid = ?,
151                      child_uuids = ?,
152                      updated_at = ?,
153                      visible_tags = ?,
154                      metadata_tags = ?,
155                      prediction_targets = ?,
156                      gravity_well_strength = ?,
157                      free_energy_score = ?,
158                      resonance_score = ?,
159                      last_resonance_factors = ?
160                  WHERE uuid = ?
161              )rM�items�hasattr�setattrrr@r>r-r0r4r7rCr6rDrErFrGr8r9�prediction_targets�__dict__rIrJrKrLr1)r"r<rhrO�keyrCr,�pts&&&     r�
update_bullet�GodDatabase.update_bullet�se������&����"�-�-�/�J�C��v�#�#���U�+�*�%�L�L�N����^�^�
�
���L�L�� ����#�#�)�)��"�"��
162163  �6�-�-�.��!�!�+�+�-��
164165  �6�.�.�/��
166167  �6�/�/�0��
168169  �&�2K�2K�L�2K�B�K�K�2K�L�M��,�,��(�(��&�&��
170171  �6�8�8�9���
�<
�K�K�M�?�B�
��M�1�
�B�
�s �CG�G
 �&AG�
G�G#	c�&<�V^8�dQhRS[RS[/#rS)r�bool)rrs"�rrr)s���
'�
'�#�
'�$�
'rc� �VP4;_uu_4pVPRW34VPRV34VPRV34pVP4VP^8�uuRRR4# +'giR#;i)zDelete a bullet and its edges.z:DELETE FROM edges WHERE source_uuid = ? OR target_uuid = ?z/DELETE FROM attention_log WHERE bullet_uuid = ?z"DELETE FROM bullets WHERE uuid = ?N)r-r0r1�rowcount)r"r<r,�results&&  r�
delete_bullet�GodDatabase.delete_bullet)sp��
�^�^�
�
���L�L�U�X\�Wc�d�
�L�L�J�T�G�T��\�\�"F���P�F��K�K�M��?�?�Q�&��
�
�
�s�AA<�<B
	c�:<�V^8�dQhRS[PRS[/#�rrXr:)r&r(r
172  )rrs"�rrr8s��� 
173  � 
174  �'�+�+� 
175  �&� 
176  rc�(�.p\P!VR,;'gR4F�p\V\4'gKTP	\VP
RR4\VP
RR44VP
RR4VP
R	R4R
177  V9d\P!VR
178  ,4M\P!4VP
R^4R74K�	\R/R
VR
,bRVR,bRVR,bR\VR,;'gR4bRVR,bR\P!VR,;'gR4bR\P!VR,4bR\P!VR,4bR\P!VR,4bRVR,;'g^bR\P!VR,;'gR4bR\P!VR,;'gR4bRVbRVR,;'gRbRVR,;'gRbRVR,;'gRbR\P!VR,;'gR4b#) z&Convert database row to Bullet object.rmz[]�target_uuid��predicted_edge_type�
179  relates_to�prediction_strength��free_energy_reduction�first_predicted�prediction_count)r}rr�r�r�r�r<r5r4r7�textr6rFr=r>r?rHr8r9z{}rIrJrKrL�)
rD�loads�
180  isinstance�dictrNr�getrr�
fromisoformatr@r
181  r
)r"rXrm�pt_datas&&  rrV�GodDatabase._row_to_bullet8sr�����z�z�#�&:�";�"C�"C�t�D�G��'�4�(�(�"�)�)�*:� '���M�2� >�(0����=R�T`�1a�(b�(/���4I�3�(O�*1�+�+�6M�s�*S�Zk�ov�Zv�H�$:�$:�7�CT�;U�$V�}E�}I�}I�}K�%,�[�[�1C�Q�%G�
+��E��
182  ��V��
183  ��<�(�
184  ��	�N�
185  �%�S��%8�%B�%B�F�C�	
186187  �M�*�
188  ��
189190  �3�}�#5�#=�#=��>�

191  � �-�-�c�,�.?�@�
192  � �-�-�c�,�.?�@�
193  �!�.�.�s�=�/A�B�
194  ��^�,�1�1���
195  ����C��$7�$?�$?�4�@�
196  ��*�*�S��%9�%A�%A�T�B�
197  � 2�
198  �#&�&=�">�"E�"E�#��
199  �"�"5�6�=�=�#��
200  �  � 1�2�9�9�c��!
201  �"$(�:�:�c�2J�.K�.S�.S�t�#T�#
202  �	
203  r��?Fr�c�T<�V^8�dQhRS[RS[RS[RS[RS[RS[RS[S[,/#)r�source_uuidr}�	edge_type�weight�	predictedr�r:)rr�floatrtrr)rrs"�rrr\s^���K�K��K��K��	K�
204  �K��
K�#�K�
205  �$��Krc�Z�VPV4pVPV4pV'd	V'gR#\P!4p	Rp
206  V'gV^8XdRp
207  MV'd
208  RV,
209  p
210  \\	\
211  P!44VVVVV	V	VVV
212  R7
213  pVP4;_uu_4pTPRVPVPVPVPPVPVPVP P#4VP$P#4VP&VP('d^M^VP*VP,34VP/4RRR4V# +'giT#;i)a[
214  Create an edge between two bullets.
215  
216  Args:
217      source_uuid: Source bullet UUID
218      target_uuid: Target bullet UUID
219      edge_type: Type of relationship
220      weight: Strength of connection (0-1)
221      predicted: Was this edge predicted?
222      prediction_strength: How strongly was it predicted?
223  
224  Returns:
225      Created Edge or None if bullets don't exist
226  Nr�r�)
227  r<r�r}r�r�r=�last_traversedr�r��formation_surpriseaJ
228                  INSERT INTO edges (
229                      uuid, source_uuid, target_uuid, edge_type, weight,
230                      confidence, created_at, last_traversed, traversal_count,
231                      predicted, prediction_strength, formation_surprise
232                  ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
233              )rMrr@rrrArBr-r0r<r�r}r�rCr��
234  confidencer=rGr��traversal_countr�r�r�r1)
r"r�r}r�r�r�r��source�targetr@r��edger,s
&&&&&&&      r�create_edge�GodDatabase.create_edge\s]��0����-������-���V���l�l�n��!���0�A�5�!$��
�!$�':�!:����X�^�^�%�&�#�#������ 3�1�
235  ���^�^�
�
���L�L���	�	�� � �� � ����$�$����������)�)�+��#�#�-�-�/��$�$��^�^�^����(�(��'�'�
�

�(
�K�K�M�+�.��/�
�.��s
�8CF�F*	c�6<�V^8�dQhRS[RS[S[,/#rS)rrr)rrs"�rrr�s���
236  *�
237  *�S�
238  *�X�d�^�
239  *rc���VP4;_uu_4pVPRV34P4pV'gRRR4R#VPV4uuRRR4# +'giR#;i)zGet an edge by UUID.z"SELECT * FROM edges WHERE uuid = ?N)r-r0rU�_row_to_edgerWs&&  r�get_edge�GodDatabase.get_edge�s_��
�^�^�
�
���,�,�4�t�g���h�j�
���
�
��$�$�S�)��
�
�
�rZc�6<�V^8�dQhRS[RS[S[,/#)rr�r:�rrr)rrs"�rrr�s���<�<�#�<�$�t�*�<rc���VP4;_uu_4pVPRV34P4pVUu.uFq@PV4NK	upuuRRR4#uupi +'giR#;i)z#Get all edges from a source bullet.z)SELECT * FROM edges WHERE source_uuid = ?N�r-r0rbr�)r"r�r,rdrXs&&   r�get_edges_from�GodDatabase.get_edges_from��f��
�^�^�
�
���<�<�;�k�^���h�j�
�7;�;�d�s�%�%�c�*�d�;�	�
��<�	�
�
���&A*�A%�A*�%A*�*A;	c�6<�V^8�dQhRS[RS[S[,/#)rr}r:r�)rrs"�rrr�s���<�<��<��T�
240  �<rc���VP4;_uu_4pVPRV34P4pVUu.uFq@PV4NK	upuuRRR4#uupi +'giR#;i)z!Get all edges to a target bullet.z)SELECT * FROM edges WHERE target_uuid = ?Nr�)r"r}r,rdrXs&&   r�get_edges_to�GodDatabase.get_edges_to�r�r�c�6<�V^8�dQhRS[RS[S[,/#rS)rr)rrs"�rrr�s���
241  �
242  �#�
243  �$�s�)�
244  rc���\4pVPV4FpVPVP4K 	VP	V4FpVPVP
245  4K 	\
V4#)z/Get UUIDs of all bullets connected to this one.)�setr��addr}r�r��list)r"r<�	neighborsr�s&&  r�
get_neighbors�GodDatabase.get_neighbors�se���E�	��'�'��-�D��M�M�$�*�*�+�.��%�%�d�+�D��M�M�$�*�*�+�,��I��rc� <�V^8�dQhRS[/#)r�	edge_uuidr)rrs"�rrr�s���	�	�s�	rc���VP4;_uu_4pVPR\P!4P	4V34VP4RRR4R# +'giR#;i)z@Record that an edge was traversed (attention flowed through it).z�
246                  UPDATE edges SET
247                      last_traversed = ?,
248                      traversal_count = traversal_count + 1
249                  WHERE uuid = ?
250              N)r-r0rr@rGr1)r"r�r,s&& r�
traverse_edge�GodDatabase.traverse_edge�sU��
�^�^�
�
���L�L��
251  �,�,�.�*�*�,�i�8�
:�
�K�K�M��
�
�
�s�AA)�)A:	c�:<�V^8�dQhRS[PRS[/#r{)r&r(r)rrs"�rrr�s���
252  �
253  ����
254  ��
255  rc���\VR,VR,VR,\VR,;'gR4VR,;'gRVR,;'gR\P!VR	,4\P!VR
256  ,4VR,;'g^\	VR,4VR
,;'gRVR,;'gRR7#)z$Convert database row to Edge object.r<r�r}r�r�r�r�r�r=r�r�r�r�r�r�)r<r�r}r�r�r�r=r�r�r�r�r�)rrrr�rt)r"rXs&&rr��GodDatabase._row_to_edge�s�����V���M�*��M�*��s�;�/�?�?�<�@��x�=�'�'�C��<�(�/�/�C��-�-�c�,�.?�@�#�1�1�#�6F�2G�H�� 1�2�7�7�a��3�{�+�,� #�$9� :� A� A�c�"�#7�8�?�?�C�

257258  rc�B<�V^8�dQhRS[RS[S[,RS[RS[/#)r�bullet_uuid�
259  session_id�duration_ms�modality)rrr_)rrs"�rrr�s7��������S�M���	�
260  �rc
261  �D�\P!4pVP4;_uu_4pVPRVP	4V34VPRWVP	4W434VP4RRR4R# +'giR#;i)zq
262  Record that attention was paid to a bullet.
263  
264  This updates both the bullet's access metadata and logs the event.
265  z�
266                  UPDATE bullets SET
267                      accessed_at = ?,
268                      access_count = access_count + 1
269                  WHERE uuid = ?
270              z�
271                  INSERT INTO attention_log (bullet_uuid, session_id, timestamp, duration_ms, modality)
272                  VALUES (?, ?, ?, ?, ?)
273              N)rr@r-r0rGr1)r"r�r�r�r�r@r,s&&&&&  r�record_attention�GodDatabase.record_attention�s���l�l�n��
�^�^�
�
���L�L��
274  �-�-�/�;�/�
1�
�L�L���3�=�=�?�K�R�
T�
275  
�K�K�M��
�
�
�s�AB�B	c�x<�V^8�dQhRS[S[,RS[S[,RS[RS[S[S[S[3,,/#)rr�r�r\r:)rrr_rrr)rrs"�rrrsH���/�/��c�]�/��S�M�/��	/�
276  
277  �d�3��8�n�	�/rc��VP4;_uu_4pV'd#VPRW34P4pMKV'd#VPRW#34P4pM!VPRV34P4pVUu.uFp\V4NK	upuuRRR4#uupi +'giR#;i)zGet attention log entries.z�
278                      SELECT * FROM attention_log
279                      WHERE bullet_uuid = ?
280                      ORDER BY timestamp DESC LIMIT ?
281                  z�
282                      SELECT * FROM attention_log
283                      WHERE session_id = ?
284                      ORDER BY timestamp DESC LIMIT ?
285                  zu
286                      SELECT * FROM attention_log
287                      ORDER BY timestamp DESC LIMIT ?
288                  N)r-r0rbr�)r"r�r�r\r,rdrXs&&&&   r�get_attention_log�GodDatabase.get_attention_logs����^�^�
�
����|�|�%�"�)�	+�,4�8�:�	�
289  ��|�|�%�!�(�	*�+3�(�*�	��|�|�%��X�� (�x�z��
290  *.�.��#�D��I��.�'�
��&/�'�
�
�s$�2B9�AB9�B4�'B9�4B9�9C
291  	c	�b<�V^8�dQhRS[RS[RS[S[S[,S[S[,3,/#)r�center_uuid�depthr:)rr_rrr
292  r)rrs"�rrr,s;���(�(��(��(�
293  �t�F�|�T�$�Z�'�	(�	(rc�`aaaaaa�\4o.o.oRVVVVVV3RlloS!V^4SS3#)z�
294  Get a subgraph centered on a bullet.
295  
296  Args:
297      center_uuid: The center bullet
298      depth: How many hops to include
299  
300  Returns:
301      Tuple of (bullets, edges) in the subgraph
302  c�0�V^8�dQhR\R\/#)rr<�
current_depth)rr_)rs"rr�.GodDatabase.get_subgraph.<locals>.__annotate__?s��	A�	A�#�	A�c�	Arc�<�VS	9gVS8�dR#S	PV4SPV4pV'dSPV4VS8d�SPV4F.pSPV4S!VPV^,4K0	SPV4F.pSPV4S!VPV^,4K0	R#R#�N)r�rMrNr�r}r�r�)
303  r<r�rOr��bulletsr��edges�explorer"�visiteds
304  &&  ������rr��)GodDatabase.get_subgraph.<locals>.explore?s�����w��-�%�"7���K�K����_�_�T�*�F�����v�&��u�$� �/�/��5�D��L�L��&��D�,�,�m�a�.?�@�6�!�-�-�d�3�D��L�L��&��D�,�,�m�a�.?�@�4�%r)r�)r"r�r�r�r�r�r�sf&f@@@@r�get_subgraph�GodDatabase.get_subgraph,s8����%������	A�	A�&	��Q����~�rc	�R<�V^8�dQhRS[RS[S[,RS[RS[S[,/#)rrcr5r\r:r^)rrs"�rrrVs;���>�>��>��S�M�>��	>�
305  
306  �f��>rc	�f�VP4;_uu_4pV'd)VPRVRVR2V34P4pM&VPRRVR2V34P4pVUu.uFq`PV4NK	upuuRRR4#uupi +'giR#;i)zZ
307  Search bullets by content.
308  
309  Simple text search for now - can be enhanced with FTS later.
310  z�
311                      SELECT * FROM bullets
312                      WHERE blanket_id = ? AND content LIKE ?
313                      ORDER BY resonance_score DESC, accessed_at DESC
314                      LIMIT ?
315                  �%z�
316                      SELECT * FROM bullets
317                      WHERE content LIKE ?
318                      ORDER BY resonance_score DESC, accessed_at DESC
319                      LIMIT ?
320                  Nra)r"rcr5r\r,rdrXs&&&&   r�search_bullets�GodDatabase.search_bulletsVs����^�^�
�
����|�|�%�
321  !�A�e�W�A�,��6�8�
322  9A��
323  ���|�|�%�
324  �%���l�E�*�,�
325  -5�H�J��9=�=���'�'��,��=�!�
�� >�!�
�
�s�AB�4B�
B�B�B0	c�L<�V^8�dQhRS[S[,RS[S[S[3,/#)rr5r:)rrrr)rrs"�rrrus'�����H�S�M��T�#�s�(�^�rc���VP4;_uu_4pV'dRVPRV34P4^,pVPRV34P4^,pMLVPR4P4^,pVPR4P4^,pRVRVRV/uuRRR4# +'giR#;i)	zGet database statistics.z1SELECT COUNT(*) FROM bullets WHERE blanket_id = ?z�
326                      SELECT COUNT(*) FROM edges e
327                      JOIN bullets b ON e.source_uuid = b.uuid
328                      WHERE b.blanket_id = ?
329                  zSELECT COUNT(*) FROM bulletszSELECT COUNT(*) FROM edges�bullet_count�
330  edge_countr5N)r-r0rU)r"r5r,r�r�s&&   r�	get_stats�GodDatabase.get_statsus���
�^�^�
�
���#�|�|�G�*�� ��(�*�Q�  ��"�\�\�+�!�]�	$�%-�H�J�q�	2�
331  � $�|�|�,J�K�T�T�V�WX�Y��!�\�\�*F�G�P�P�R�ST�U�
332  ����j��j���
�
�
�s�B-C�C"	)r)zgod.db)Nzcreated_at DESC)N��read)NN�d)r)N�r�)!�__name__�
333  __module__�__qualname__�__firstlineno__�__doc__r#r	r-r!r
�TEXTrPrMrerqrxrVr�
334  RELATES_TOr�r�r�r�r�r�r�r�r�r�r�r��__static_attributes__�__classdictcell__)rs@rrrs����	�	�	�����D�X&*�$/�$4�$4�,0�26�O�O�b
335  ,�
336  ,�
>�
>�<�<�|
'�
'� 
337  � 
338  �P'�1�1���%(�K�K�Z
339  *�
340  *�<�<�<�<�
341  �
342  �	�	�
343  �
344  �&��>/�/�<(�(�T>�>�>��rr�__main__ztest_god.dbz=== God Database Test ===
345  zFThis is the first atomic bullet - a Markov blanket at the lowest levelz
346  rick-graphz#testz
#architecture)r4r5r8zCreated bullet 1: :N�Nz...z1This is a second bullet that relates to the firstzCreated bullet 2: )r�r}r�zCreated edge: ztest-session)r�zRecorded attention eventsz
347  Retrieved bullet:z  Content: :N�2Nz  Access count: z  Tags: z
348  Neighbors of bullet 1: )r�z
349  Subgraph: z
350   bullets, z edgesz
351  Stats: z
352  === Test Complete ===)5r�r&r<rArDr�pathlibr�typingrrrrr�
353  contextlibr	�modelsr
354  rrr
rrr��tempfile�os�path�join�
355  gettempdirr�db�printrP�bullet1�bullet2r�r�r�r�rM�	retrievedr4rHr8r�r��lenr�r�r�r��stats�remover�rr�<module>r	s���������3�3�%�I�I�s	�s	�n�z���
��g�g�l�l�8�.�.�0�-�@�G�	�W�	�B�	�
356  '�(����X����/���G�
357  
358  ��w�|�|�B�/�0��
359  4�5����C���Y���G�
360  
361  ��w�|�|�B�/�0��
362  4�5��>�>��L�L��L�L��%�%���D�
363  
364  �N�4�9�9�R�=�/��
365  -�.���������@���������@�	�
366  %�&��
�
�g�l�l�+�I�	��!�	�K�	�)�)�#�.�/�s�
367  3�4�	��Y�3�3�4�
368  5�6�	�H�Y�+�+�,�
369  -�.�� � ����.�I�	�%�c�)�n�%5�
370  6�7��_�_�W�\�\��_�;�N�G�U�	�J�s�7�|�n�J�s�5�z�l�&�
371  A�B�
�L�L��&�E�	�I�e�W�
372  ���I�I�g��	�
373  #�$�wr