+
    .pÐjQ  ã                  óÂ   € R t ^ RIHt ^ RIt^ RIHtHtHtHtH	t	H
t
Ht ^ RIHtHt ]P                   ! ]4      tR R ltR R ltR	 R
 lt]! RR04      t ! R R4      tR# )u§  Crossed-profile durable state in ONE ``state.db`` â€” finders and fixers for
``hermes sessions repair-profiles`` (#88715 PR-6).

Every ``state.db`` belongs to exactly one profile (``<root>/state.db`` â†’ ``default``,
``<root>/profiles/<name>/state.db`` â†’ ``name``) and every gateway session key encodes the profile
that owns the conversation (``agent:main:â€¦`` for the default, ``agent:<name>:â€¦`` for a named one).
The per-profile store model (#88734) is forward-only: it routes NEW writes to the right store but
never touched rows that had already landed in the wrong one, and the identity fences
(``_INHERIT_PARENT_META_SQL``, ``_recovered_row_allowed_for_active_profile``) only refuse to
*widen* damage that already exists. This module is the backward-looking half: it names each crossed
row and can settle it.

Store-level only. Which profiles exist, which store owns which home, whether a live gateway holds the
routing index in memory, and the JSON files outside ``state.db`` are the orchestrator's concern
(``hermes_cli/sessions_repair_profiles.py``).

Moving a session between two stores is two single-store transactions â€” copy into the target, then
delete from the source â€” because a crash between them leaves a *duplicate*, which the next run
settles idempotently (:meth:`import_moved_session` reports ``present``; the delete only proceeds when
the target holds at least as many messages). The reverse order would lose the row.
)ÚannotationsN)ÚAnyÚDictÚIterableÚListÚOptionalÚSetÚTuple)Ú
_id_chunksÚ_placeholdersc               ó    € V ^8„  d   QhRRRR/# )é   Úsession_keyr   ÚreturnúOptional[str]© )Úformats   "Ú*/opt/hermes/hermes_state_profile_repair.pyÚ__annotate__r       s   € ÷ 	8ñ 	8 Sð 	8¨]ñ 	8ó    c                óÜ   € \        V \        4      '       g   R# V P                  R4      p\        V4      ^8  g   V^ ,          R8w  g   V^,          '       g   R# ^ RIHp V! V^,          4      # )u¹   Profile encoded in an ``agent:<ns>:â€¦`` gateway key (``default`` for ``main``), or None for a
keyless row (CLI/subagent lineage) or a key another producer minted (hosted rooms, tests).NÚ:Úagent)Ú"profile_from_session_key_namespace)Ú
isinstanceÚstrÚsplitÚlenÚgateway.sessionr   )r   Úpartsr   s   &  r   Úsession_key_profiler        sW   € ô �k¤3×'Ò'ÙØ×Ñ˜cÓ"€EÜ
ˆ5ƒz�A„~˜˜q� WÔ,°E¸!·H´HÙÝBÙ-¨e°A­hÓ7Ð7r   c               ó    € V ^8„  d   QhRRRR/# )r   Úvaluer   r   r   r   )r   s   "r   r   r   ,   s   € ÷ ñ ˜3ð  =ñ r   c                ó`   € \        T ;'       g    R4      P                  4       pT;'       g    R# )zY``sessions.profile_name`` as a comparable label; NULL/blank is "unowned", not a crossing.Ú N)r   Ústrip)r"   Únames   & r   Ú_stored_profiler'   ,   s'   € äˆu�{ˆ{˜Ó×!Ñ!Ó#€DØ�<ˆ<�4Ðr   c               ó    € V ^8„  d   QhRRRR/# )r   Útabler   r   z	list[str]r   )r   s   "r   r   r   2   s   € ÷ Mñ M ð M¨	ñ Mr   c                ób   € V P                  R V R24       Uu. uF  q"^,          NK  	  up# u upi )zPRAGMA table_info('z'))Úexecute)Úconnr)   Úrows   && r   Ú_table_columnsr.   2   s3   € Ø"Ÿl™lÐ-@ÀÀÀrÐ+JÔKÓLÑK�s��FˆFÑKÑLÐLùÒLs   ˜,ÚidÚdisplay_orderc                  óØ   € ] tR t^=tRtR R ltR R ltR R ltR R	 ltR
 R lt	R R lt
R R ltR R lt]R R l4       tR R ltR R ltR R ltR R ltR R ltR R ltR tR!# )"ÚSessionProfileRepairMixinz^Per-store crossed-profile identity: find, relabel, sever, move, and settle routing/topic rows.c               ó    € V ^8„  d   QhRRRR/# )r   Úownerr   r   zdict[str, list[dict[str, Any]]]r   )r   s   "r   r   Ú&SessionProfileRepairMixin.__annotate__B   s   € ÷ "ñ "°3ð "Ð;Zñ "r   c                óJ  € V P                  R4      pR. R. R. /pV EF  p\        VR,          4      pVf   K  \        VR,          4      pVe5   We8w  d/   VR,          P                  RVR,          RVR,          RVRV/4       WQ8w  dI   VR,          P                  RVR,          RVR,          RTR	\	        VR	,          ;'       g    ^ 4      /4       \        VR
,          4      pVf   KÌ  Wu8w  g   KÔ  VR,          P                  RVR,          RVRVR,          RV/4       EK  	  V# )uV  Keyed session rows whose identity disagrees with itself or with this store.

``mislabelled``: ``profile_name`` names a profile other than the one in the row's own key
(the key is what routing, the agent cache and ``_db_for_key`` consult; the label is stamped
after the fact). ``foreign``: the key's profile is not *owner* â€” the row sits in a store that
the routed profile never reads. ``crossed_parents``: child and parent are both keyed and name
different profiles, so the NULL-fill inheritance fence would have been the only thing standing
between them. A row can appear in more than one list.
zþSELECT s.id, s.session_key, s.profile_name, s.parent_session_id, s.message_count,        p.session_key AS parent_session_key FROM sessions s LEFT JOIN sessions p ON p.id = s.parent_session_id WHERE s.session_key LIKE 'agent:%' ORDER BY s.started_at, s.idÚmislabelledÚforeignÚcrossed_parentsr   Úprofile_namer/   Úkey_profileÚmessage_countÚparent_session_keyÚparent_session_idÚparent_profile)Ú	_read_allr    r'   ÚappendÚint)Úselfr4   ÚrowsÚfoundr-   r;   Úlabelr?   s   &&      r   Úfind_crossed_profile_sessionsÚ7SessionProfileRepairMixin.find_crossed_profile_sessionsB   sO  € ð �~‰~ðMóNˆð
 3@ÀÀYÐPRÐTeÐgiÐ1jˆÜˆCÜ-¨c°-Õ.@ÓAˆKØÒ"ÙÜ# C¨Õ$7Ó8ˆEØÒ  UÔ%9Ø�mÕ$×+Ñ+Ø˜#˜d�) ]°C¸Õ4FØ" E¨=¸+ð-Gô Hð Ô#Ø�iÕ ×'Ñ'Ø˜#˜d�) ]°C¸Õ4FÈÐWbØ#¤S¨¨_Õ)=×)BÐ)BÀÓ%Cð)Eô Fô 1°Ð5IÕ1JÓKˆNØÔ)¨nÖ.KØÐ'Õ(×/Ñ/Ø˜#˜d�) ]°KØ'¨Ð-@Õ)AÐCSÐUcð1e÷ fñ ð$ ˆr   c               ó   € V ^8„  d   QhRR/# ©r   r   zlist[dict[str, Any]]r   )r   s   "r   r   r5   f   s   € ÷ ,ñ ,Ð+?ñ ,r   c                	ó`   € V P                  R 4       Uu. uF  p\        V4      NK  	  up# u upi )zbSELECT scope, session_key, entry_json, updated_at FROM gateway_routing ORDER BY scope, session_key)r@   Údict)rC   r-   s   & r   Úlist_gateway_routing_rowsÚ3SessionProfileRepairMixin.list_gateway_routing_rowsf   s:   € Ø%)§^¡^ð*ô&+ó ,ñ &+˜c”�S–	ñ &+ñ ,ð 	,ùò ,s   ”+c               ó   € V ^8„  d   QhRR/# rJ   r   )r   s   "r   r   r5   k   s   € ÷ ñ Ð7Kñ r   c                ó¤   € R p. pV P                  V4       F5  p\        VR,          4      pVR9  g   K  VP                  / VCRV/C4       K7  	  V# )a	  ``telegram_dm_topic_bindings`` rows labelled ``default`` whose ``session_key`` names a named
profile: written by a multiplexer that had not yet learned to stamp the routed profile
(#76423). Stores whose topic tables predate ``profile_name`` have nothing to relabel.c                óì   € V P                  R 4       Uu0 uF  q^ ,          kK  	  ppV'       d   R\        V R4      9  d   . # V P                  R4       Uu. uF  p\        V4      NK  	  up# u upi u upi )zYSELECT name FROM sqlite_master WHERE type='table' AND name = 'telegram_dm_topic_bindings'r:   Útelegram_dm_topic_bindingsz�SELECT chat_id, thread_id, session_key FROM telegram_dm_topic_bindings WHERE profile_name = 'default' ORDER BY chat_id, thread_id)r+   r.   rL   )r,   r-   Úexistings   &  r   Ú_readÚNSessionProfileRepairMixin.find_profile_less_telegram_topic_rows.<locals>._reado   s’   € Ø*.¯,©,Økô+mó nñ +m 3˜AŸ˜ñ +mˆHð nç˜~´^ÀDÐJfÓ5gÔgØ�	Ø)-¯©ðMô*Nó Oñ *N #”D˜–Iñ *Nñ Oð Oùò	nùòOs   ”A,ÁA1r   r;   ©NÚdefault)Ú_read_retrying_ioerrr    rA   )rC   rT   Úoutr-   r;   s   &    r   Ú%find_profile_less_telegram_topic_rowsÚ?SessionProfileRepairMixin.find_profile_less_telegram_topic_rowsk   s\   € ò	Oð ˆØ×,Ñ,¨UÖ3ˆCÜ-¨c°-Õ.@ÓAˆKØÐ"3Ö3Ø—
‘
Ð>˜cÐ> =°+Ñ>Ö?ñ 4ð ˆ
r   c               ó    € V ^8„  d   QhRRRR/# )r   Ú
session_idr   r   rB   r   )r   s   "r   r   r5   ~   s   € ÷ )ñ )¨Sð )°Sñ )r   c                	ó`   € V P                  R V34      pV'       d   \        V^ ,          4      # ^ # )z2SELECT COUNT(*) FROM messages WHERE session_id = ?)Ú	_read_onerB   )rC   r]   r-   s   && r   Úcount_messages_allÚ,SessionProfileRepairMixin.count_messages_all~   s,   € Ø�n‰nÐQÐT^ÐS`Óaˆß!Œs�3�q•6‹{Ð( qÐ(r   c               ó    € V ^8„  d   QhRRRR/# ©r   ÚidsúIterable[str]r   rB   r   )r   s   "r   r   r5   „   s   € ÷ (ñ (°=ð (ÀSñ (r   c                ób   a€ \        V4      oS'       g   ^ # R V3R llpV P                  V4      # )z†Set ``profile_name`` to the profile in each row's own key. Re-derives the target inside the
transaction (never trusts a stale report).c               ó   € V ^8„  d   QhRR/# ©r   r   rB   r   )r   s   "r   r   ÚOSessionProfileRepairMixin.relabel_sessions_to_key_profile.<locals>.__annotate__‹   s   € ÷ 	ñ 	˜ñ 	r   c           	     ó
  <€ ^ p\        S4       Fp  pV P                  R\        V4       R2V4      P                  4       pV F:  w  rE\	        V4      pVf   K  WP                  RWdV34      P
                  ,          pK<  	  Kr  	  V# )é    z2SELECT id, session_key FROM sessions WHERE id IN (Ú)zKUPDATE sessions SET profile_name = ? WHERE id = ? AND profile_name IS NOT ?©r
   r+   Ú_session_ids_placeholdersÚfetchallr    Úrowcount)r,   ÚchangedÚchunkrD   r]   r   ÚtargetÚwanteds   &      €r   Ú_doÚFSessionProfileRepairMixin.relabel_sessions_to_key_profile.<locals>._do‹   s’   ø€ ØˆGÜ# FÖ+�Ø—|‘|ØHÔIbÐchÓIiÐHjÐjkÐlØóç#™8›:ð ó 04Ñ+�JÜ0°Ó=�FØ’~Ù ØŸ|™|ØeØ¨VÐ4ó 6ç6>±hõ?’Gó	 04ñ	 ,ð ˆNr   ©ÚlistÚ_execute_write©rC   rd   ru   rt   s   && @r   Úrelabel_sessions_to_key_profileÚ9SessionProfileRepairMixin.relabel_sessions_to_key_profile„   s1   ø€ ô �c“ˆßÙ÷	ð 	ð ×"Ñ" 3Ó'Ð'r   c               ó    € V ^8„  d   QhRRRR/# rc   r   )r   s   "r   r   r5   ›   s   € ÷ (ñ (¨ð (¸3ñ (r   c                ób   a€ \        V4      oS'       g   ^ # R V3R llpV P                  V4      # )uß   Detach children from a parent keyed under another profile. Only ``parent_session_id`` is
cleared â€” ``profile_name`` stays the row's own (relabelled separately when it is wrong) â€” and
only while the crossing still holds.c               ó   € V ^8„  d   QhRR/# rh   r   )r   s   "r   r   ÚESessionProfileRepairMixin.sever_crossed_parents.<locals>.__annotate__£   s   € ÷ 	ñ 	˜ñ 	r   c                ó:  <€ ^ p\        S	4       Fˆ  pV P                  R\        V4       R2V4      P                  4       pV FR  w  rEp\	        V4      \	        V4      r‡Vf   K!  Vf   K'  Wx8w  g   K/  WP                  RV34      P
                  ,          pKT  	  KŠ  	  V# )rk   zwSELECT s.id, s.session_key, p.session_key FROM sessions s JOIN sessions p ON p.id = s.parent_session_id WHERE s.id IN (rl   z9UPDATE sessions SET parent_session_id = NULL WHERE id = ?rm   )
r,   Úseveredrr   rD   r]   ÚkeyÚ
parent_keyÚmineÚtheirsrt   s
   &        €r   ru   Ú<SessionProfileRepairMixin.sever_crossed_parents.<locals>._do£   s¯   ø€ ØˆGÜ# FÖ+�Ø—|‘|ð&ä&?ÀÓ&FÐ%GÀqðJàKPóR÷ S[ÑRZÓR\ð ó 48Ñ/�J ZÜ#6°sÓ#;Ô=PÐQ[Ó=\˜&ØÔ'¨FÔ,>À4Æ>Ø§<¡<ØWÐZdÐYfó$hßhpÑhpõqšó 48ñ ,ð ˆNr   rw   rz   s   && @r   Úsever_crossed_parentsÚ/SessionProfileRepairMixin.sever_crossed_parents›   s1   ø€ ô �c“ˆßÙ÷	ð 	ð ×"Ñ" 3Ó'Ð'r   c               ó    € V ^8„  d   QhRRRR/# )r   r]   r   r   zOptional[dict[str, Any]]r   )r   s   "r   r   r5   ´   s   € ÷ 0ñ 0°#ð 0Ð:Rñ 0r   c                ó2   a€ V3R lpV P                  V4      # )uæ   Everything the target store needs to hold *session_id* as its own: the row (every column),
the resolved system prompt, EVERY message row (inactive and compacted generations included â€”
a move is not an export) and its usage rows.c           
     óÊ  <a € S P                  R S34      P                  4       pVf   R# V 3R lpVR,          '       d   V! VR,          4      MRpVR,          '       d   V! VR,          4      MRpS P                  RS34       Uu. uF  p\        V4      NK  	  ppS P                  RS34       Uu. uF  p\        V4      NK  	  ppR\        V4      RVR	VR
VRV/# u upi u upi )z#SELECT * FROM sessions WHERE id = ?Nc                ól   <€ SP                  R V 34      P                  4       pV'       d
   V^ ,          # R# )z0SELECT prompt FROM system_prompts WHERE hash = ?N)r+   Úfetchone)Úprompt_hashr-   r,   s   & €r   Ú_storedÚQSessionProfileRepairMixin.export_session_for_move.<locals>._read.<locals>._stored¼   s2   ø€ Ø—l‘lÐ#UÐXcÐWeÓf×oÑoÓq�ß!$�s˜1•vÐ.¨$Ð.r   Úsystem_prompt_hashÚ
tool_namesz7SELECT * FROM messages WHERE session_id = ? ORDER BY idz6SELECT * FROM session_model_usage WHERE session_id = ?ÚsessionÚsystem_promptÚtool_pinÚmessagesÚusage)r+   rŽ   rL   )	r,   r”   r�   Úpromptr–   Úrr—   r˜   r]   s	   f       €r   rT   Ú@SessionProfileRepairMixin.export_session_for_move.<locals>._read¸   s  ù€ Ø—l‘lÐ#HÈ:È-ÓX×aÑaÓcˆGØŠÙõ/ð @GÐG[×?\Ô?\‘W˜WÐ%9Õ:Ô;ÐbfˆFà9@À×9NÔ9N‘w˜w |Õ4Ô5ÐTXˆHØ)-¯©ØIÈJÈ=ô*Zó [ñ *Z Aœ˜Qžñ *ZˆHð [à&*§l¡lØHÈ:È-ô'Yó Zñ 'Y ”T˜!–Wñ 'YˆEð Zàœt G›}¨o¸vÀzÐS[Ð]gÐiqØ˜Uð$ð $ùò	[ùòZs   ÂCÂ0C )rX   )rC   r]   rT   s   &f r   Úexport_session_for_moveÚ1SessionProfileRepairMixin.export_session_for_move´   s   ø€ õ	$ð  ×(Ñ(¨Ó/Ð/r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úpayloadúdict[str, Any]r:   r   r   r   )r   s   "r   r   r5   Ê   s"   € ÷  (ñ  (¨Nð  (ÈSð  (ÐUXñ  (r   c               ó~   a aaaa€ \        SR,          4      oSR,          oR VVV VV3R llpS P                  V4      # )uh  Insert a moved session into THIS store as *profile_name*'s. ``present`` when the id already
exists (an earlier run copied but did not delete), else ``imported``. The parent link survives
only when the parent is already here â€” a moved row must never point across stores or at a
row of another profile. Columns the target schema lacks are dropped, never invented. Titles
are unique per store only, so a title an unrelated row here already holds gets the moved
row's id tail (the :meth:`import_foreign_history` convention); the resident row keeps its
name, since it is the one this profile's clients resolve by title.r”   r/   c               ó   € V ^8„  d   QhRR/# )r   r   r   r   )r   s   "r   r   ÚDSessionProfileRepairMixin.import_moved_session.<locals>.__annotate__Õ   s   € ÷ 	ñ 	˜ñ 	r   c                ó  <€ V P                  R S
34      P                  4       '       d   R# SS	R&   S	P                  R4      pV'       d)   V P                  R V34      P                  4       f   RS	R&   S	P                  R4      pVeZ   V P                  RV34      P                  4       '       d3   RS
RR  R2pVRSP                  \	        V4      ,
           V,           S	R&   SP                  V SP                  R	4      4      S	R
&   SP                  R4      f*   \	        S	P                  R4      ;'       g    R4      ^@8X  d%   SP                  V SP                  R4      4      S	R&   SP                  V RS	\        4       R7       SP                  R4      ;'       g    .  F"  pSP                  V R/ VCRS
/C\        R7       K$  	  SP                  R4      ;'       g    .  F&  pSP                  V R/ VCRS
/C\        4       R7       K(  	  R# )ú#SELECT 1 FROM sessions WHERE id = ?Úpresentr:   r>   NÚtitlez&SELECT 1 FROM sessions WHERE title = ?ú (rl   r•   r’   r–   r“   r$   Úsessions)Úskipr—   r]   r˜   Úsession_model_usageÚimportediôÿÿÿ)	r+   rŽ   ÚgetÚMAX_TITLE_LENGTHr   Ú_store_system_promptÚ_insert_rowÚ	frozensetÚ_MESSAGE_MOVE_SKIP)r,   Ú	parent_idr§   ÚsuffixÚmessager˜   rŸ   r:   rC   r”   r]   s   &     €€€€€r   ru   Ú;SessionProfileRepairMixin.import_moved_session.<locals>._doÕ   sï  ø€ Ø�|‰|ÐAÀJÀ=ÓQ×ZÑZ×\Ò\Ù Ø&2ˆG�NÑ#ØŸ™Ð$7Ó8ˆIß˜TŸ\™\Ð*OÐR[ÐQ]Ó^×gÑgÓiÒqØ/3�Ð+Ñ,Ø—K‘K Ó(ˆEØÒ  T§\¡\Ð2ZÐ]bÐ\dÓ%e×%nÑ%n×%pÒ%pØ˜j¨¨Ð.Ð/¨qÐ1�Ø#(Ð)M¨$×*?Ñ*?Ä#ÀfÃ+Õ*MÐ#NÐQWÕ#W�˜Ñ Ø,0×,EÑ,EÀdÈGÏKÉKÐXgÓLhÓ,iˆGÐ(Ñ)Ø�{‰{˜:Ó&Ò2´c¸'¿+¹+ÀlÓ:S×:YÐ:YÐWYÓ6ZÐ^`Ô6`à(,×(AÑ(AÀ$ÈÏÉÐT^ÓH_Ó(`�˜Ñ%Ø×Ñ˜T :¨w¼Y»[ÐÔIØ"Ÿ;™; zÓ2×8Ð8°bÒ8�Ø× Ñ   zÐ3X°gÐ3X¸|ÈZÑ3XÔ_qÐ Örñ 9à Ÿ™ WÓ-×3Ð3°Ò3�Ø× Ñ  Ð'<Ð>aÀÐ>aÈÐV`Ñ>aÔhqÓhsÐ Ötñ 4ár   )rL   ry   )rC   rŸ   r:   ru   r”   r]   s   ffd @@r   Úimport_moved_sessionÚ.SessionProfileRepairMixin.import_moved_sessionÊ   s:   ü€ ô �w˜yÕ)Ó*ˆØ˜T•]ˆ
÷	ô 	ð* ×"Ñ" 3Ó'Ð'r   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r)   r   Úvaluesr    rª   r±   r   ÚNoner   )r   s   "r   r   r5   í   s)   € ÷ *ñ * ð *¨nð *Àyð *ÐUYñ *r   c               	ó  € \        W4       Uu. uF  qDV9   g   K  WC9  g   K  VNK  	  ppT P                  R V RRP                  V4       RRP                  R V 4       4       R2V Uu. uF  qBV,          NK  	  up4       R# u upi u upi )zINSERT INTO r¨   z, z
) VALUES (c              3  ó&   "  € T F  pR x € K	  	  R# 5i)Ú?Nr   )Ú.0Ú_s   & r   Ú	<genexpr>Ú8SessionProfileRepairMixin._insert_row.<locals>.<genexpr>ð   s   é € ÐKaÑY`ÐTUÍCÓY`ùs   ‚rl   N)r.   r+   Újoin)r,   r)   rº   rª   ÚcÚcolumnss   &&&$  r   r°   Ú%SessionProfileRepairMixin._insert_rowì   sŠ   € ä,¨TÔ9Ó[Ñ9˜À&¹[”1ÈQÉ]—1�1Ñ9ˆÐ[Ø�‰Ø˜5˜'  D§I¡I¨gÓ$6Ð#7°zÀ$Ç)Á)ÑKaÑY`ÓKaÓBaÐAbÐbcÐdÙ 'Ó(¡˜1�A�YˆY¡Ñ(ö	*ùò \ùò )s   ŽB›B£BÁ*B	
c               ó    € V ^8„  d   QhRRRR/# )r   r]   r   r   Úboolr   )r   s   "r   r   r5   ó   s   € ÷ .ñ .¨sð .°tñ .r   c                óN   a a€ R V V3R llp\        S P                  V4      4      # )zÀRemove a session this store no longer owns after the target confirmed it. Children left
behind are detached (same FK rule as :meth:`delete_session`); topic bindings on the row
cascade with it.c               ó   € V ^8„  d   QhRR/# )r   r   rÈ   r   )r   s   "r   r   ÚDSessionProfileRepairMixin.delete_moved_session.<locals>.__annotate__÷   s   € ÷ 	ñ 	˜ñ 	r   c                ó  <€ V P                  R S34      P                  4       f   R# V P                  RS34       V P                  RS34       V P                  RS34       V P                  RS34       SP                  V 4       R# )r¥   FzHUPDATE sessions SET parent_session_id = NULL WHERE parent_session_id = ?z)DELETE FROM messages WHERE session_id = ?z4DELETE FROM session_model_usage WHERE session_id = ?z!DELETE FROM sessions WHERE id = ?T)r+   rŽ   Ú#_delete_unreferenced_system_prompts)r,   rC   r]   s   &€€r   ru   Ú;SessionProfileRepairMixin.delete_moved_session.<locals>._do÷   s|   ø€ Ø�|‰|ÐAÀJÀ=ÓQ×ZÑZÓ\ÒdÙØ�L‰LÐcÐfpÐerÔsØ�L‰LÐDÀzÀmÔTØ�L‰LÐOÐR\ÐQ^Ô_Ø�L‰LÐ<¸z¸mÔLØ×4Ñ4°TÔ:Ùr   )rÈ   ry   )rC   r]   ru   s   ff r   Údelete_moved_sessionÚ.SessionProfileRepairMixin.delete_moved_sessionó   s%   ù€ ÷	ñ 	ô �D×'Ñ'¨Ó,Ó-Ð-r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   rd   re   Úprofiler   r   rB   r   )r   s   "r   r   r5     s!   € ÷ (ñ (¨mð (Àcð (Ècñ (r   c                ó”   aaa€ ^ RI Hp \        V4      oS'       g   ^ # V! S4      R,           oR VVV3R llpV P                  V4      # )u	  ``agent:main:â€¦`` â†’ ``agent:<profile>:â€¦`` (+ ``profile_name``) for rows a standalone
gateway wrote before this store's profile was multiplexed (#113884). Only rows still under
the legacy namespace change; a ``main``-named profile is written in its marked form.)Ú_session_key_namespacer   c               ó   € V ^8„  d   QhRR/# rh   r   )r   s   "r   r   ÚJSessionProfileRepairMixin.rekey_legacy_main_sessions.<locals>.__annotate__  s   € ÷ 	ñ 	˜ñ 	r   c                ó˜   <€ ^ p\        S4       F7  pWP                  R\        V4       R2SS.VO54      P                  ,          pK9  	  V# )rk   z^UPDATE sessions SET session_key = ? || substr(session_key, 12), profile_name = ? WHERE id IN (z0) AND substr(session_key, 1, 11) = 'agent:main:')r
   r+   rn   rp   )r,   rq   rr   Únew_nsrÒ   rt   s   &  €€€r   ru   ÚASessionProfileRepairMixin.rekey_legacy_main_sessions.<locals>._do  s^   ø€ ØˆGÜ# FÖ+�ØŸ<™<ð$Ü$=¸eÓ$DÐ#EÐEuðwà˜WÐ- uÑ-ó/÷ 08©xõ8’ñ ,ð
 ˆNr   )r   rÔ   rx   ry   )rC   rd   rÒ   rÔ   ru   rØ   rt   s   &&f  @@r   Úrekey_legacy_main_sessionsÚ4SessionProfileRepairMixin.rekey_legacy_main_sessions  sC   ú€ õ 	;Ü�c“ˆßÙÙ'¨Ó0°3Õ6ˆ÷	ò 	ð ×"Ñ" 3Ó'Ð'r   c               ó    € V ^8„  d   QhRRRR/# )r   rD   zIterable[tuple[str, str]]r   rB   r   )r   s   "r   r   r5     s   € ÷ 	(ñ 	(Ð0Ið 	(Ècñ 	(r   c                	ób   a€ \        V4      oS'       g   ^ # R V3R llpV P                  V4      # )rk   c               ó   € V ^8„  d   QhRR/# rh   r   )r   s   "r   r   ÚKSessionProfileRepairMixin.delete_gateway_routing_rows.<locals>.__annotate__  s   € ÷ 	*ñ 	*˜ñ 	*r   c                ó0   <a € \        V 3R  lS 4       4      # )c              3  ób   <"  € T F$  w  rSP                  R W34      P                  x € K&  	  R# 5i)z?DELETE FROM gateway_routing WHERE scope = ? AND session_key = ?N©r+   rp   )r¿   Úscoperƒ   r,   s   &  €r   rÁ   ÚUSessionProfileRepairMixin.delete_gateway_routing_rows.<locals>._do.<locals>.<genexpr>  s9   øé € ð *á"(‘J�Eð —|‘|ØQÐTYÐS_óaßaiÑaiôjã"(ùs   ƒ,/©Úsum©r,   rt   s   f€r   ru   ÚBSessionProfileRepairMixin.delete_gateway_routing_rows.<locals>._do  s   ù€ Üô *á"(ó*ó *ð *r   rw   ©rC   rD   ru   rt   s   && @r   Údelete_gateway_routing_rowsÚ5SessionProfileRepairMixin.delete_gateway_routing_rows  s/   ø€ Ü�d“ˆßÙ÷	*ð 	*ð ×"Ñ" 3Ó'Ð'r   c               ó    € V ^8„  d   QhRRRR/# )r   rD   z%Iterable[tuple[str, str, str, float]]r   rB   r   )r   s   "r   r   r5   #  s   € ÷ (ñ (Ð:_ð (Ðdgñ (r   c                ób   a€ \        V4      oS'       g   ^ # R V3R llpV P                  V4      # )u¯   Adopt ``(scope, session_key, entry_json, updated_at)`` rows another store held for this
one's routing index. An existing key wins â€” the row the gateway actually loads stays.c               ó   € V ^8„  d   QhRR/# rh   r   )r   s   "r   r   ÚUSessionProfileRepairMixin.insert_gateway_routing_rows_if_absent.<locals>.__annotate__*  s   € ÷ 	Hñ 	H˜ñ 	Hr   c                ó0   <a € \        V 3R  lS 4       4      # )c              3  ó\   <"  € T F!  pSP                  R V4      P                  x € K#  	  R# 5i)zfINSERT OR IGNORE INTO gateway_routing (scope, session_key, entry_json, updated_at) VALUES (?, ?, ?, ?)Nrâ   )r¿   r-   r,   s   & €r   rÁ   Ú_SessionProfileRepairMixin.insert_gateway_routing_rows_if_absent.<locals>._do.<locals>.<genexpr>+  s4   øé € ð Há@F¸ð —|‘|ð&Ø'*ó,ç,4©Hô5ã@Fùs   ƒ),rå   rç   s   f€r   ru   ÚLSessionProfileRepairMixin.insert_gateway_routing_rows_if_absent.<locals>._do*  s!   ù€ Üô Há@FóHó Hð Hr   rw   ré   s   && @r   Ú%insert_gateway_routing_rows_if_absentÚ?SessionProfileRepairMixin.insert_gateway_routing_rows_if_absent#  s3   ø€ ô �d“ˆßÙ÷	Hð 	Hð ×"Ñ" 3Ó'Ð'r   c               ó    € V ^8„  d   QhRRRR/# )r   rD   zIterable[dict[str, Any]]r   údict[str, int]r   )r   s   "r   r   r5   2  s   € ÷ "(ñ "(Ð0Hð "(È^ñ "(r   c                óv   aa€ \        V4      oR^ R^ R^ /oS'       g   S# R VV3R llpV P                  V4      # )zãStamp the key's profile onto ``default``-labelled bindings (and the chat's mode row when
only the default one exists). A binding that would collide with one the named profile already
wrote is the stale duplicate and is removed.Úbindings_relabelledÚbindings_duplicates_removedÚmode_rows_relabelledc               ó   € V ^8„  d   QhRR/# )r   r   r÷   r   )r   s   "r   r   ÚKSessionProfileRepairMixin.relabel_telegram_topic_rows.<locals>.__annotate__;  s   € ÷ 	ñ 	˜ñ 	r   c           
     ój  <€ R \        V R4      9   pS EF  p\        VR,          4      pVR9   d   K  VR,          VR,          rTV P                  RW4V34      P                  4       pV'       d2   SR;;,          V P                  RWE34      P                  ,          uu&   M1SR;;,          V P                  R	W4V34      P                  ,          uu&   V'       g   KÆ  V P                  R
W434      P                  4       e   Kë  SR;;,          V P                  RW434      P                  ,          uu&   EK  	  S# )r:   Útelegram_dm_topic_moder   Úchat_idÚ	thread_idzaSELECT 1 FROM telegram_dm_topic_bindings WHERE profile_name = ? AND chat_id = ? AND thread_id = ?rú   zgDELETE FROM telegram_dm_topic_bindings WHERE profile_name = 'default' AND chat_id = ? AND thread_id = ?rù   zwUPDATE telegram_dm_topic_bindings SET profile_name = ? WHERE profile_name = 'default' AND chat_id = ? AND thread_id = ?zKSELECT 1 FROM telegram_dm_topic_mode WHERE profile_name = ? AND chat_id = ?rû   zaUPDATE telegram_dm_topic_mode SET profile_name = ? WHERE profile_name = 'default' AND chat_id = ?rV   )r.   r    r+   rŽ   rp   )	r,   Úmode_has_profiler-   rs   r   r  ÚcollidesÚcountsrt   s	   &      €€r   ru   ÚBSessionProfileRepairMixin.relabel_telegram_topic_rows.<locals>._do;  s6  ø€ Ø-´ÀÐF^Ó1_Ñ_ÐÜ�Ü,¨S°Õ-?Ó@�ØÐ.Ô.ÙØ%(¨¥^°S¸Õ5E˜ØŸ<™<ð(Ø*0¸9Ð)EóGçGOÁxÃzð ÷ ØÐ8×9¸T¿\¹\ð<Ø>EÐ=Qó>SçS[ÑS[õ\Ô9ð Ð0×1°T·\±\ð<Ø>DÈyÐ=Yó6[ç[cÑ[cõdÓ1÷ $Ñ#¨¯©ØeØÐ)ó)+ç+3©8«:ô)>ð Ð1×2°d·l±lð*Ø,2Ð+<ó7>ç>F¹hõGÖ2ñ' ð, ˆMr   rw   )rC   rD   ru   r  rt   s   && @@r   Úrelabel_telegram_topic_rowsÚ5SessionProfileRepairMixin.relabel_telegram_topic_rows2  sH   ù€ ô �d“ˆØ'¨Ð,IÈ1ÐNdÐfgÐhˆßØˆM÷	ñ 	ð2 ×"Ñ" 3Ó'Ð'r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úplatformr   r   r   zset[str]r   )r   s   "r   r   r5   X  s&   € ÷ cñ c¨cð c¸Cð cÀHñ cr   c                ó‚   € V P                  RV\        V4      34      pR V 4        Uu0 uF
  qDf   K  VkK  	  up# u upi )u–   Profiles whose keyed sessions hold *(platform, chat_id)* in this store â€” the evidence for
who owns a profile-less ``gateway_voice_mode.json`` entry.ziSELECT DISTINCT session_key FROM sessions WHERE source = ? AND chat_id = ? AND session_key LIKE 'agent:%'c              3  óF   "  € T F  p\        VR ,          4      x € K  	  R# 5i)r   N)r    )r¿   rš   s   & r   rÁ   ÚBSessionProfileRepairMixin.key_profiles_for_chat.<locals>.<genexpr>^  s   é € ÐPÉ4ÀaÔ/°°-Õ0@×AÐAË4ùs   ‚!)r@   r   )rC   r	  r   rD   Úps   &&&  r   Úkey_profiles_for_chatÚ/SessionProfileRepairMixin.key_profiles_for_chatX  sH   € ð �~‰~ð-Ø/7¼¸W»Ð.FóHˆñ QÉ4ÔPÓbÑP�a—�ÑPÑbÐbùÒbs   ©<³<r   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rG   rM   rZ   r`   r{   rˆ   rœ   r·   Ústaticmethodr°   rÏ   rÚ   rê   rô   r  r  Ú__static_attributes__r   r   r   r2   r2   =   si   † Ùhõ"õH,õ
õ&)õ(õ.(õ20õ, (ðD ô*ó ð*õ.õ"(õ(	(õ(õ"(÷Lcñ cr   r2   )r  Ú
__future__r   ÚloggingÚtypingr   r   r   r   r   r   r	   Úhermes_state_commonr
   r   rn   Ú	getLoggerr  Úloggerr    r'   r.   r±   r²   r2   r   r   r   Ú<module>r     s^   ðñõ* #ã ß B× BÑ Bç Và	×	Ò	˜8Ó	$€õ	8õõMñ   oÐ6Ó7Ð ÷acó acr   