+
    .pÐj˜  ã                   óä   € R t ^ RIt^ RIt^ RIHt ^ RIHt ^ RIHt ^ RIH	t	 ]P                  ! ]4      t]	! 4       R,          t]R,          t]tR R	 ltR
 R ltRR R lltRR R lltR R ltR# )a,  Session mirroring for cross-platform message delivery.

When a message is sent to a platform (send_message or cron delivery), append a
"delivery-mirror" record to the target session's transcript so the receiving-side
agent knows what was sent.  Standalone: works from CLI, cron and gateway contexts.
N)Údatetime©ÚPath)ÚOptional)Úget_hermes_homeÚsessionsúsessions.jsonc                ó$   € V ^8„  d   QhR\         /# )é   Úreturnr   )Úformats   "Ú/opt/hermes/gateway/mirror.pyÚ__annotate__r      s   € ÷ Cñ C¤ñ Có    c                 ó\   € \         \        8w  d   \         # \        4       R,          R,          # )uL  Active profile's ``sessions.json`` at call time: the patched ``_SESSIONS_INDEX`` when a test
changed it, else live profile-scoped HERMES_HOME â€” under the multiplexed gateway one process
serves every profile, so the import-time constant would resolve every profile's pre-migration
session lookup against the launch profile's index.r   r   )Ú_SESSIONS_INDEXÚ_SESSIONS_INDEX_AT_IMPORTr   © r   r   Ú_resolve_sessions_indexr      s+   € ô
  /Ô2KÔKŒOð CÜ Ó" ZÕ/°/ÕAðCr   c                ó0   € V ^8„  d   QhR\         R\        /# ©r
   Úentryr   )ÚdictÚstr)r   s   "r   r   r       s   € ÷ Añ Aœ4ð A¤Cñ Ar   c                 ó|   € \        V P                  R 4      ;'       g    / P                  R4      ;'       g    R4      # )ÚoriginÚuser_idÚ )r   Úget©r   s   &r   Ú_origin_user_idr        s1   € Ü�—	‘	˜(Ó#×)Ð) r×.Ñ.¨yÓ9×?Ð?¸RÓ@Ð@r   c                óÆ   € V ^8„  d   QhR\         R\         R\         R\         R\        \         ,          R\        \         ,          R\         R\        \         ,          R	\        /	# )
r
   ÚplatformÚchat_idÚmessage_textÚsource_labelÚ	thread_idr   ÚroleÚ
session_idr   )r   r   Úbool)r   s   "r   r   r   $   se   € ÷ %ñ %Üð%Üð%Ü/2ð%ÜBEð%ÜZbÔcfÕZgð%ä”c�]ð%ä),ð%äHPÔQTÍð%ô 
ñ%r   c                ó„  €  V'       g   \        V \        V4      WER7      pV'       g   \        P                  RWWE4       R# \	        VRVRVR\
        P                  ! 4       P                  4       RRR	V/4       \        P                  R
Ws4       R#   \         d$   p\        P                  RYYEYx4        Rp?R# Rp?ii ; i)u�  Append a delivery-mirror message to the target session's SQLite transcript.

Pass ``session_id`` when the caller already holds the exact session (e.g. the
cron in_channel seed) to skip the origin scan, which refuses to guess on a
populated chat (flat + N thread sessions per chat_id) and would drop the mirror.
Text that is NOT the agent speaking (e.g. a cron brief) must pass
``role="user"``: ``mirror`` metadata is dropped at the SQLite boundary, so an
assistant-role mirror replays as a real turn and yields assistantâ†’assistant
pairs that break strict-alternation providers, while a user-role mirror
collapses safely via the consecutive-user merge.
Returns True if mirrored, False if no matching session or error. Never raises.

``role`` defaults to ``"assistant"`` â€” correct for the interactive ``send_message`` mirror, where the
mirrored text is the agent's own outgoing reply (a genuine assistant turn). See #2221.
)r&   r   z[Mirror: no session found for %s:%s thread=%s user=%s (explicit_id=none, origin-scan bailed)Fr'   ÚcontentÚ	timestampÚmirrorTÚmirror_sourcez%Mirror: wrote to session %s (from %s)z8Mirror failed for %s:%s thread=%s user=%s session=%s: %sN)
Ú_find_session_idr   ÚloggerÚwarningÚ_append_to_sqliter   ÚnowÚ	isoformatÚdebugÚ	Exception)	r"   r#   r$   r%   r&   r   r'   r(   Úes	   &&&&&&&& r   Úmirror_to_sessionr8   $   s°   € ð&ßÜ)¨(´C¸³LÈIÔgˆJßÜ�N‰NØmØ 9ôñ Ü˜*Ø�D˜) \°;ÄÇÂÃ×@XÑ@XÓ@ZØ�d˜O¨\ð'
ô 	ô 	�‰Ð<¸jÔWÙøÜô ä�‰ÐQÐS[Ðfoð  {Eô  	IÝûðús(   ‚B ŠB ©B ÁAB ÂB?ÂB:Â:B?c                ó–   € V ^8„  d   QhR\         R\         R\        \         ,          R\        \         ,          R\        \         ,          /# )r
   r"   r#   r&   r   r   )r   r   )r   s   "r   r   r   L   sP   € ÷ 2\ñ 2\œsð 2\¬Sð 2\¼XÄc½]ð 2\Ô\dÔehÕ\ið 2\Ôu}ô  Bõ  vCñ 2\r   c                óÆ  a aa€  ^ RI HpHp V! 4       p \        VRR4      p\	        V4      '       d   V! S SSVR7      MRpV'       d   \        V4      V! V4       #  V! V4       \        4       p
T
P                  4       '       g   R#  \        P                  ! T
P                  RR7      4      pR TT T3R	 llpTP                  4        UU	u. uFQ  w  rÙ\        T4      P                  R
4      '       d   K'  \!        T	\"        4      '       g   K?  T! T	4      '       g   KO  T	NKS  	  ppp	T'       g   R# T'       dK   T U	u. uF   p	\%        T	4      \        T4      8X  g   K  T	NK"  	  pp	T'       d   TpMy\'        T4      ^8”  d   R# Mf\'        T4      ^8”  dW   \'        \)        \$        T4       Uu0 uF+  pTP+                  4       '       g   K  TP+                  4       kK-  	  up4      ^8”  d   R# \-        TR R7      P/                  R4      #   T! T4       i ; i  \         d"   p	\        P                  RT	4        Rp	?	ELÔRp	?	ii ; i  \         d     R# i ; iu up	pi u up	i u upi )uõ  Active session_id for a platform + chat_id pair.

state.db is primary; sessions.json is the pre-migration fallback.  DM keys
don't embed the chat_id ("agent:main:telegram:dm"), so match on the persisted
origin.  With *user_id*, exact sender matches win; several same-chat candidates
with no user match â†’ None rather than contaminate another participant's session.

Queries state.db gateway session rows (primary source since #9006); falls back to scanning sessions.json
for pre-migration databases.
©ÚacquireÚrelease_or_closeÚfind_session_by_originN)r"   r#   r&   r   z)Mirror state.db session lookup failed: %sz	utf-8-sig)Úencodingc                ó0   € V ^8„  d   QhR\         R\        /# r   )r   r)   )r   s   "r   r   Ú&_find_session_id.<locals>.__annotate__l   s   € ÷ añ aœð a¤ñ ar   c                 ó´  <€ V P                  R 4      ;'       g    / pVP                  R4      ;'       g    V P                  RR4      P                  4       SP                  4       8H  ;'       dl    \        VP                  RR4      4      \        S4      8H  ;'       d=    SRJ ;'       g1    \        VP                  R4      ;'       g    R4      \        S4      8H  # )r   r"   r   r#   Nr&   )r   Úlowerr   )r   r   r#   r"   r&   s   & €€€r   Ú_matchesÚ"_find_session_id.<locals>._matchesl   s¼   ø€ Ø—‘˜8Ó$×*Ð*¨ˆØ—‘˜JÓ'×DÐD¨5¯9©9°ZÀÓ+D×KÑKÓMÐQY×Q_ÑQ_ÓQaÑa÷ `ð `Ü˜Ÿ
™
 9¨bÓ1Ó2´c¸'³lÑB÷`ð `à $Ð&×^Ð^¬#¨f¯j©j¸Ó.E×.KÐ.KÈÓ*LÔPSÐT]ÓP^Ñ*^ð	ar   Ú_c                 ó&   € V P                  R R4      # )Ú
updated_atr   )r   r   s   &r   Ú<lambda>Ú"_find_session_id.<locals>.<lambda>~   s   € ¨U¯Y©Y°|ÀRÔ-Hr   )Úkeyr(   )Úhermes_state_registryr<   r=   ÚgetattrÚcallabler   r6   r0   r5   r   ÚexistsÚjsonÚloadsÚ	read_textÚitemsÚ
startswithÚ
isinstancer   r    ÚlenÚmapÚstripÚmaxr   )r"   r#   r&   r   r<   r=   ÚdbÚfinderr(   r7   Úsessions_indexÚdatarD   ÚkÚ
candidatesÚexact_user_matchesÚus   fff&             r   r/   r/   L   sþ  ú€ ðEßCÙ‹Yˆð	!Ü˜RÐ!9¸4Ó@ˆFÜmuÐv|×m}Òm}™¨¸7ÈiÐahÕið  DHˆJßÜ˜:“á˜RÕ ð ñ ˜RÔ ô -Ó.€NØ× Ñ ×"Ò"ÙðÜ�zŠz˜.×2Ñ2¸KÐ2ÓHÓIˆ÷aò að !%§
¡
¤Ôs¡™˜´C¸³F×4EÑ4EÀc×4J”!ÌzÐZ[Ô]a×Ob”!ÑgoÐpq×gr—!�!¡€JÑsßÙßÙ)3ÓZ© A´ÀqÓ7IÌSÐQXË\Ñ7YŸa˜a©ÐÐZßØ+‰JÜ�‹_˜qÔ Ùð !ä	ˆZ‹˜1Ô	¤¼¼_ÈjÔ9YÓ%gÑ9Y°AÐ]^×]dÑ]d×]f¤i a§g¡g¦iÑ9YÑ%gÓ!hÐklÔ!lÙÜˆzÑHÔI×MÑMÈlÓ[Ð[øñ= ˜RÕ ûÜô EÜ�‰Ð@À!×DÒDûðEûô ô Úðüó tùò [ùò
 &hs�   …H •H ³H Á	
H ÁH ÁH Â&I Ã$IÃ5IÄIÄIÄ<IÅIÆ)IÇIÈ
HÈH ÈH>ÈH9È9H>ÉIÉIc                ó4   € V ^8„  d   QhR\         R\        RR/# )r
   r(   Úmessager   N)r   r   )r   s   "r   r   r   �   s!   € ÷ ñ ¤#ð ´ð ¸ñ r   c                ó´   € ^ RI HpHp V! 4       p VP                  WP	                  RR4      VP	                  R4      R7       V! V4       R#   T! T4       i ; i)uã   Append a message to the SQLite session database.

Raises on failure: ``mirror_to_session`` reports ``False`` (and warns) only when the
exception reaches it â€” swallowing it here made every failed write look mirrored (#10130).
r;   r'   Ú	assistantr+   )r(   r'   r+   N)rL   r<   r=   Úappend_messager   )r(   rc   r<   r=   rZ   s   &&   r   r2   r2   �   sQ   € ÷ @á	‹€BðØ
×Ñ Z·k±kÀ&È+Ó6VÐ`g×`kÑ`kÐluÓ`vÐÔwá˜ÖøÑ˜Õús   ‘2A Á
A)ÚcliNNre   N)NN)Ú__doc__rP   Úloggingr   Úpathlibr   Útypingr   Úhermes_cli.configr   Ú	getLoggerÚ__name__r0   Ú_SESSIONS_DIRr   r   r   r    r8   r/   r2   r   r   r   Ú<module>rp      si   ðñó Û Ý Ý Ý å -à	×	Ò	˜8Ó	$€áÓ! JÕ.€Ø /Õ1€Ø+Ð õCõA÷%÷P2\÷jr   