+
    iHàjJ  ã                   óà   € R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ]P                  ! R4      t	 ! R R4      t
R R ltR R	 ltR
 R ltR R ltRR/R ltR R ltR R lt ! R R4      tR# )ae  Host-side ``AIAgent._compress_context`` wrapper.

Publishes the commit fence ``hard_interrupt()`` reads, runs the compressor on a snapshot under the progress
timeout, mirrors ``_DB_PERSISTED_MARKER`` stamps back onto the live lists and rebinds the session context.
Extracted from ``run_agent.py``; every method resolves through ``AIAgent``'s MRO unchanged.
N)ÚActivityProvenanceÚ	run_agentc                   ó:   a € ] tR t^t o RtRtV 3R lR ltRtV tR# )Ú_CommitFenceRegistrationaŸ  One ``_compress_context`` attempt's entry on the agent's commit-fence stack.

The fence slot is a stack, not a save/restore cell: overlapping attempts register in
order but may complete in any order, so each attempt removes only its own entry and the
published slot always tracks the newest *live* attempt. The stall-fallback retry swaps
``fence`` in place so the slot update stays tied to the owning registration.
c                ó   <€ V ^8„  d   QhRR/# ©é   ÚreturnN© )ÚformatÚ__classdict__s   "€Ú=/home/ubuntu/.hermes/hermes-agent/agent/compression_facade.pyÚ__annotate__Ú%_CommitFenceRegistration.__annotate__   s   ø€ ÷ ñ  ñ ó    c                ó   € Wn         R # ©N©Úfence)Úselfr   s   &&r   Ú__init__Ú!_CommitFenceRegistration.__init__   s   € ØŽ
r   r   N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú	__slots__r   Ú__static_attributes__Ú__classdictcell__©r   s   @r   r   r      s   ø‡ € ñð €I÷ö r   r   c                ó0   € V ^8„  d   QhR\         R\         /# )r   Úsystem_messager	   )Ústr)r   s   "r   r   r   #   s   € ÷ 
$ñ 
$´Cð 
$¼Cñ 
$r   c                ó¼   € \        V RR4      ;p'       d   V#  V P                  V4      #   \         d'    \        P	                  RRR7       T;'       g    Ru # i ; i)zóCached prompt, else a fresh build, else the raw ``system_message`` (never raises).
Resolved lazily by the timeout wrapper: an eager rebuild would raise before compress_context runs when
``_cached_system_prompt`` is unset and the builder fails.Ú_cached_system_promptNzQcompress_context timeout fallback prompt rebuild failed; using raw system_messageT©Úexc_infoÚ )ÚgetattrÚ_build_system_promptÚ	ExceptionÚloggerÚdebug)Úagentr"   Úcacheds   && r   Ú_timeout_fallback_promptr0   #   sc   € ô ˜Ð 7¸Ó>Ð>€vÖ>Øˆð$Ø×)Ñ)¨.Ó9Ð9øÜô $Ü�‰ÐhÐswˆÔxØ×#Ð# Ò#ð$ús   ™* ª*AÁAÁAc                ód   € V ^8„  d   QhR\         R\         R\         R\         R\        R\        RR/# )	r   ÚidleÚwaitedÚsince_progressÚtotal_ceilingÚtotal_exhaustedÚprogress_observedr	   N)ÚfloatÚbool)r   s   "r   r   r   0   sF   € ÷ 0
ñ 0
Üð0
Ü#(ð0
Ü:?ð0
ÜPUð0
Ühlð0
äð0
ð 
ñ0
r   c               óÒ  € ^ RI Hp V! V 4       V'       d   \        P                  RW&4       M\        P                  RW2V4       \	        V RR4      p\        V4      '       d    V! R\        P                  R7       \	        \	        V RR4      RR4      p	\        V	4      '       d     V'       d   V	! RRR7       MV	! RRR7        \	        V RR4      p
\        V
4      '       g   R# V'       d   V'       d   RMRpV
! RVR RV R24       R# V
! RVR R24       R#   \         d    \        P                  RR	R
7        LÁi ; i  \         d    \        P                  RR	R
7        L¡i ; i)zRHost-side timeout bookkeeping: log, activity stamp, cooldown ladder, user warning.)Ú"mark_context_compression_timed_outzpContext compression reached its total ceiling after %.1fs (progress observed=%s); continuing without compressionzpContext compression made no progress for %.1fs (total wait %.1fs, ceiling %.1fs); continuing without compressionÚ_touch_activityNzcontext compression timed out)Ú
provenancez.compress_context timeout activity touch failedTr&   Úcontext_compressorÚrecord_timeout_failurez-host compress_context total ceiling exhaustedÚceiling_exhausted)Úfailure_kindz3host compress_context timeout (no summary progress)Ústalledz2failed to record compress_context timeout cooldownÚ_emit_warningz" after summary output was observedr(   u8   âš  Context compression reached its total ceiling after z.1fÚsur   . No messages were dropped â€” continuing without compression. Run /compress to retry or /new for a clean session.u(   âš  Context compression timed out after u·   s with no output from the summary model. No messages were dropped â€” continuing without compression. Run /compress to retry, /new for a clean session, or check auxiliary.compression.)
Úagent.conversation_compressionr;   r,   Úwarningr)   Úcallabler   ÚAGENT_COMPRESSION_TIMEOUTr+   r-   )r.   r2   r3   r4   r5   r6   r7   r;   ÚtouchÚrecordÚemitÚprogresss   &$$$$$$     r   Ú_report_compression_timeoutrM   0   sn  € õ
 RÙ& uÔ-ßÜ�‰Ø~Øõ	
ô
 	�‰Ø~Ø Mô	
ô �EÐ,¨dÓ3€EÜ�‡‚ð	ZÙÐ1Ô>P×>jÑ>jÕkô ”W˜UÐ$8¸$Ó?ÐAYÐ[_Ó`€FÜ�×Òð	^ßÙÐFÐUhÖiáÐLÐ[dÖeô �5˜/¨4Ó0€DÜ�D�>Š>Ùßß;LÑ7ÐRTˆÙðØ˜C�L  ( ð ,nðnö	
ñ 	Ø6°t¸C°jð ACð Cö	
øô/ ô 	ZÜ�L‰LÐIÐTXˆLÖYð	Zûô ô 	^Ü�L‰LÐMÐX\ˆLÖ]ð	^ús0   Á$D Â&E Â.
E Â9
E Ä"D>Ä=D>Å"E&Å%E&c                ó4   € V ^8„  d   QhR\         R\         RR/# )r   r3   Úceilingr	   N)r8   )r   s   "r   r   r   c   s!   € ÷ 
ñ 
¬ð 
¼ð 
À$ñ 
r   c                óh   € \        V RR4      p\        V4      '       d   V! RVR RVR R24       R# R# )zVCommit-phase ceiling breach: the SessionDB mutation must complete, so only surface it.rC   Nu9   âš  Context compression commit is taking unusually long (z.0fzs, ceiling uj   s). Waiting for it to finish safely â€” if this persists, check SessionDB health (disk / lock contention).)r)   rG   )r.   r3   rO   rK   s   &&& r   Ú_warn_commit_overrunrQ   c   sH   € ä�5˜/¨4Ó0€DÜ�‡~‚~ÙØGÈÈsÀ|ÐS^Ð_fÐgjÐ^kð luð uö	
ñ r   c                ó   € V ^8„  d   QhRR/# r   r
   )r   s   "r   r   r   m   s   € ÷ Añ AÀñ Ar   c                óþ   € ^ RI Hp ^ RIHp \	        V \
        4      '       d   \	        V\
        4      '       g   R# V F<  p\	        V\        4      '       g   K  VP                  V4      '       g   K4  V! W4       K>  	  R# )aF  Mirror ``_DB_PERSISTED_MARKER`` stamps from the worker's snapshot onto a live list.
Matched by scoped identity; timestamp-less repeated content is ambiguous, so every scoped match is
stamped. Imported UNCONDITIONALLY: a silent fallback literal would split the stamping key from the flush's
and resurrect the duplicate-row bug.)Ú_DB_PERSISTED_MARKER)Ú_stamp_scoped_twinsN)Úagent.context_compressorrT   rE   rU   Ú
isinstanceÚlistÚdictÚget)Útarget_messagesÚsource_messagesrT   rU   Úsource_messages   &&   r   Ú_sync_persisted_markersr^   m   sW   € õ
 >ÝBÜ�o¤t×,Ò,´J¸ÔPT×4UÒ4UÙÛ)ˆÜ�n¤d×+Ô+°×0BÑ0BÐCW×0XÔ0XÙ Ö@ó *r   Úapprox_tokensc               óú   a aaaaaaaa€ ^ RI HoHp
Hp RRR/VV3R lllp\        P
                  ! VRR7      pRRRR/oV3R	 lpV VV3R
 lpVV VV3R lpV! VSV V3R lVSVVV 3R lVS VVV
! S V	4      RJ R7      # )uŒ  Run ``run(fence, target_messages=snapshot)`` on the pool under the progress-aware timeout.
The pooled worker must NEVER share the caller's live transcript â€” a late engine after a host timeout could
rewrite it. It deep-snapshots on the worker and publishes only via an ADMITTED commit; a no-op/abort
returns the snapshot unchanged, so the ORIGINAL list is handed back to keep identity semantics.)ÚCompressionCommitFenceÚrequest_exceeds_model_windowÚ*run_compress_context_with_progress_timeoutÚsame_turn_fallback_recoveryFc                ób   <€ \         P                  ! S4      pS! WVR 7      w  r4W2J d   SV3# TV3# ))r[   rd   )ÚcopyÚdeepcopy)r   rd   ÚsnapshotÚresult_msgsÚresult_promptÚmessagesÚruns   &$   €€r   Ú_snapshot_workerÚ5_run_under_progress_timeout.<locals>._snapshot_worker‡   sB   ø€ ô —=’= Ó*ˆÙ%(ØÐIdô&
Ñ"ˆð (Ó3�À}ÐTÐT¸À}ÐTÐTr   T)rd   r6   r7   c                 ó,   <€ SP                  WR 7       R# ))r6   r7   N)Úupdate)r6   r7   Útimeout_causes   &&€r   Ú_on_timeout_causeÚ6_run_under_progress_timeout.<locals>._on_timeout_causež   s   ø€ Ø×Ñ¨_ÐÖbr   c                 ó0   <€ \        S3R V RVRVRS/SB  R# )r2   r3   r4   r5   N)rM   )r2   r3   r4   r.   rq   r5   s   &&&€€€r   Ú_on_timeoutÚ0_run_under_progress_timeout.<locals>._on_timeout¡   s6   ø€ Ü#Øñ	
Øð	
Ø%+ð	
Ø<Jð	
ØZgð	
Økxô	
r   c                  óò   <€ S! 4       p S;_uu_ 4        V Sn         \        S4      P                  R 4      ;'       g    RpV'       d   VR,          SJ d   V Sn        RRR4       V #   + '       g   i     T # ; i)Ú_compression_commit_fence_stackNr
   éÿÿÿÿ)r   ÚvarsrZ   Ú _active_compression_commit_fence)Úretry_fenceÚstackra   r.   Úfence_registration_lockÚregistrations     €€€€r   Ú_publish_new_fenceÚ7_run_under_progress_timeout.<locals>._publish_new_fence¦   sj   ø€ ñ -Ó.ˆß$Ò$Ø!,ˆLÔÜ˜“K—O‘OÐ$EÓF×LÐLÈ"ˆEß˜˜r� lÓ2Ø9D�Ô6÷	 %ð
 Ð÷ %Ö$ð
 Ðús   “3A%ÁA%Á%A6	c                  ó   <€ \        S S4      # r   )r0   )r.   r"   s   €€r   Ú<lambda>Ú-_run_under_progress_timeout.<locals>.<lambda>´   s   ø€ Ô'?ÀÀ~Ô'Vr   c                 ó   <€ \        SW4      # r   )rQ   )r3   rO   r.   s   &&€r   rƒ   r„   ·   s   ø€ Ô2FÀuÈfÔ2^r   )Úworkerrk   Úsystem_prompt_fallbackÚidle_timeout_secondsÚtotal_ceiling_secondsÚ
on_timeoutÚon_timeout_causeÚon_commit_overrunr   Útelemetry_agentÚ	new_fenceÚfallback_workerÚrequest_exceeds_windowr   )rE   ra   rb   rc   Ú	functoolsÚpartial)r.   rl   rk   r"   Úactive_fencer   r~   Úidle_timeoutr5   r_   rb   rc   rm   Ú_same_turn_fallback_workerrr   ru   r€   ra   rq   s   ffff$dd$d$       @@r   Ú_run_under_progress_timeoutr–   {   sž   ÿø€ ÷ñ ñUÀE÷ Uñ Uô& "+×!2Ò!2Ð3CÐaeÔ!fÐà&¨Ð/BÀEÐJ€Mõc÷
÷

ð 
ñ 6Ø¨(ÝVØ)ÀÐ[fØ*Ü^ÐfrØÐ);ÐMgÙ;¸EÀ=ÓQÐUYÐYôð r   c                ó(   € V ^8„  d   QhR\         RR/# )r   Údirect_pathr	   N)r9   )r   s   "r   r   r   ½   s   € ÷ Fñ FÌDð FÐUYñ Fr   c               óÒ  € \        V\        4      '       d&   V'       d   \        V^ ,          \        4      '       g   R# V^ ,          pV'       g   WBJd   \        W$4       \	        V RR4      p\        V\        4      '       d   WRJd   \        WT4       WBJd=   ^ RIHp \        P                  ! \        4      ;_uu_ 4        V! W$R7       RRR4       R# R#   + '       g   i     R# ; i)zJMirror persisted-marker stamps from the result list onto the live list(s).NÚ_session_messages)Úforget_message_token_memo)Úkeep)
rW   ÚtuplerX   r^   r)   Úagent.model_metadata_token_memor›   Ú
contextlibÚsuppressr+   )r.   Úresultrk   r˜   Úresult_messagesÚsession_messagesr›   s   &&&$   r   Ú_mirror_result_onto_live_listsr¤   ½   s¨   € ä�vœu×%Ò%¯&´ZÀÀqÅ	Ì4×5PÒ5PÙØ˜Q•i€Oç�oÓ5Ü Ô:Ü˜uÐ&9¸4Ó@ÐÜÐ"¤D×)Ò)Ð.>Ó.NäÐ 0ÔBØÓ&õ 	NÜ× Ò ¤×+Õ+Ù% hÕE÷ ,Ñ+ñ	 '÷ ,×+Ð+ús   Â?
CÃC&	c                ó   € V ^8„  d   QhRR/# r   r
   )r   s   "r   r   r   Ñ   s   € ÷ Yñ Y¨Tñ Yr   c                ó^  € \         P                  ! \        4      ;_uu_ 4        ^ RIHp V! V P
                  4       RRR4        ^ RIHp V P
                  '       d   V! V P
                  4       R# R#   + '       g   i     L?; i  \         d    \        P                  RRR7        R# i ; i)zÛPropagate a rotated session id to the CALLER's thread/ContextVar (idempotent otherwise).
The worker thread rotated hermes_logging's thread-local id; post-compression tools must resolve
HERMES_SESSION_ID to the child id.)Úset_session_contextN)Úset_current_session_idz1post-compression session ContextVar rebind failedTr&   )
rŸ   r    r+   Úhermes_loggingr§   Ú
session_idÚgateway.session_contextr¨   r,   r-   )r.   r§   r¨   s   &  r   Ú_rebind_caller_session_contextr¬   Ñ   s€   € ô 
×	Ò	œY×	'Õ	'Ý6Ù˜E×,Ñ,Ô-÷ 
(ðYÝBØ××ÐÙ" 5×#3Ñ#3Ö4ñ ÷ 
(×	'ûô ô YÜ�‰ÐHÐSWˆ×XðYús#   £A3ÁB ÁB Á3B	Â"B,Â+B,c                   óf   a € ] tR t^àt o RtRRRRRRRRR	RR
RRRRRRRRRRR/V 3R lR lltRtV tR# )ÚCompressionFacadeMixinz-``_compress_context`` (see module docstring).r_   NÚtask_idÚdefaultÚfocus_topicÚforceFÚbypass_cooldownÚ!defer_context_engine_notificationÚcommit_fenceÚverbatim_tailÚtriggerÚsnapshot_is_currentÚoverflow_reasonc                ó¨   <€ V ^8„  d   QhRS[ RS[RS[R,          RS[RS[R,          RS[RS[R	S[R
S[ R,          RS[R,          RS[R,          RS[/# )r   rk   r"   r_   Nr¯   r±   r²   r³   r´   r¶   r·   r¹   r	   )rX   r#   Úintr9   r�   )r   r   s   "€r   r   Ú#CompressionFacadeMixin.__annotate__ã   s    ø€ ÷ k5ñ k5Ùðk5Ù.1ðk5ÙEHÈ4ÅZðk5Ùadðk5á˜4•Zðk5á/3ðk5áNRðk5ñ ,0ðk5ñ \`ÐbfÕ[fðk5ñ �t•ð	k5ñ PSÐUYÍzð	k5ñ
 
ñk5r   c               óœ  a aaaaaaaaa
aaaa&€ ^ RI HpHo&HpHp V! S 4       ^ RIHpHpHpH	pH
pHp ^ RIHp RpV! 4       f!   S P                  4       pV'       d	   V! V4      pRpV! 4       f   V! S 4      pV'       d	   V! V4      pT	;'       g    V! 4       p\        S 4      P!                  R\"        P$                  ! 4       4      p\'        V4      p V;_uu_ 4        \        S 4      P!                  R. 4      pVP)                  V4       VS n        RRR4       RVVV&VVVVVV VVVVV
3R llp V	RJp!V!'       g   V! 4       w  p"p#V"^ 8*  p!V!'       d
   V ! V4      p$M\-        S V SSVVVX"X#SR7
      p$\/        S V$SV!R	7       \1        S 4       T$T;_uu_ 4        \        S 4      P3                  R4      ;'       g    Rp\5        \7        V4      ^,
          RR4       F  p%VV%,          VJ g   K  VV%  M	  V'       d   VR,          P8                  S n        M\        S 4      P;                  R
R4       RRR4       Ve	   V! V4       Ve
   V! V4       # #   + '       g   i     ELE; i  + '       g   i     L>; i  T;_uu_ 4        \        S 4      P3                  R4      ;'       g    Rp\5        \7        T4      ^,
          RR4       F  p%TT%,          TJ g   K  TT%  M	  T'       d   TR,          P8                  S n        M\        S 4      P;                  R
R4       RRR4       M  + '       g   i     M; iTe	   T! T4       Te
   T! T4       i i ; i)uÚ  Forwarder â€” see ``agent.conversation_compression.compress_context``.
``force=True`` (manual /compress) bypasses the summary-failure cooldown; ``bypass_cooldown=True``
(provider-proven overflow recovery) runs one real attempt while the cooldown stays armed.

``force=True`` is passed by the manual ``/compress`` slash command so users can bypass the
summary-failure cooldown after an auto-compress abort. Auto-compress callers use the default
``force=False``. See #100661.
)ra   Úcompress_contextÚ)reset_context_compression_timeout_outcomeÚ$resolve_context_compression_timeouts)Úget_affinity_scopeÚget_conversation_contextÚreset_affinity_scopeÚreset_conversation_contextÚset_affinity_scopeÚset_conversation_context)Ú declared_conversation_scope_safeNÚ_compression_commit_fence_lockrx   c                 óP   <€ S! SVe   TMS	SSSSSS;'       g    TSV SSSS
R7      # )N)r_   r¯   r±   r²   r³   r´   rµ   r¶   r·   r¸   r¹   r
   )r   r[   rd   r_   r³   r¾   r´   r±   r²   rk   r¹   r   r¸   r"   r¯   r·   r¶   s   &&&€€€€€€€€€€€€€€r   Ú_runÚ6CompressionFacadeMixin._compress_context.<locals>._run  sF   ø€ Ù'Ø¨_Ò-H™/ÈhÐXfØ"/¸ÈkÐafØ$3×$RÐ$RÐ7RØ7XÐhmØ"/¸ÐViØ$3ôð r   )r“   r   r~   r”   r5   r_   )r˜   r{   )NNFr
   ry   )rE   ra   r¾   r¿   rÀ   Úagent.portal_tagsrÁ   rÂ   rÃ   rÄ   rÅ   rÆ   Úagent.prompt_cache_scoperÇ   Ú_conversation_root_idrz   Ú
setdefaultÚ	threadingÚRLockr   Úappendr{   r–   r¤   r¬   rZ   ÚrangeÚlenr   Úpop)'r   rk   r"   r_   r¯   r±   r²   r³   r´   rµ   r¶   r·   r¸   r¹   ra   r¿   rÀ   rÁ   rÂ   rÃ   rÄ   rÅ   rÆ   rÇ   ÚtokenÚrootÚaffinity_tokenÚdeclaredr“   r~   r   Úfence_stackrÊ   r˜   r”   r5   r¡   Úidxr¾   s'   fffdddddd$dddd                        @r   Ú_compress_contextÚ(CompressionFacadeMixin._compress_contextã   sñ  ÿý€ ÷(	
ó 	
ñ 	2°$Ô7÷	
÷ 	
õ 	Nð ˆÙ#Ó%Ò-Ø×-Ñ-Ó/ˆDßÙ0°Ó6�ð ˆÙÓÒ'Ù7¸Ó=ˆHßÙ!3°HÓ!=�ð $×?Ð?Ñ'=Ó'?ˆÜ"& t£*×"7Ñ"7Ð8XÔZc×ZiÒZiÓZkÓ"lÐÜ/°Ó=ˆð7	5ß(Ò(Ü" 4›j×3Ñ3Ð4UÐWYÓZ�Ø×"Ñ" <Ô0Ø8D�Ô5÷ )÷
÷ ô ð '¨dÐ2ˆKßÙ.RÓ.TÑ+�˜mØ*¨aÑ/�ßÙ˜lÓ+‘ä4Ø˜$ ¨.Ø!-¸LØ,CØ!-¸]ÐZgô	�ô +¨4°¸È{Õ[Ü*¨4Ô0Ø÷ )Ò(ô # 4›jŸn™nÐ-NÓO×UÐUÐSU�Ü ¤ [Ó!1°AÕ!5°r¸2Ö>�CØ" 3Õ'¨<Õ7Ø'¨Ð,Ùñ ?÷ Ø<GÈ½O×<QÑ<Q�DÕ9ä˜“J—N‘NÐ#EÀtÔL÷ )ð Ò Ù*¨5Ô1ØÒ)Ù$ ^Õ4ð *÷k )×(Ð(ú÷L )×(û×(Ò(ô # 4›jŸn™nÐ-NÓO×UÐUÐSU�Ü ¤ [Ó!1°AÕ!5°r¸2Ö>�CØ" 3Õ'¨<Õ7Ø'¨Ð,Ùñ ?÷ Ø<GÈ½O×<QÑ<Q�DÕ9ä˜“J—N‘NÐ#EÀtÔL÷ )×(×(Ð(úð Ò Ù*¨5Ô1ØÒ)Ù$ ^Õ4ð *úsh   Ã
I3 Ã4IÄ?I3 Å9I3 ÆAI Ç'I Ç64I ÉI	É	I3 É I0É3MÉ>AL	ËL	Ë!4L	Ì	MÌL.Ì)"M)r{   )r   r   r   r   r   rÜ   r   r   r    s   @r   r®   r®   à   s�   ø‡ € Ù7ðk5ØRVðk5Øgpðk5à"&ðk5à6;ðk5àUZðk5ð 38ðk5ð GKðk5ð jnðk5ð #ð	k5ð 9=ð	k5ð ]a÷	k5÷ k5ð k5r   r®   )r   rŸ   rf   r‘   ÚloggingrÐ   Úagent.session_activityr   Ú	getLoggerr,   r   r0   rM   rQ   r^   r–   r¤   r¬   r®   r
   r   r   Ú<module>rá      sv   ðñó Û Û Û Û å 5ð 
×	Ò	˜;Ó	'€÷ñ õ
$õ0
õf
õAð?à/3ô?õDFõ(Y÷n5ó n5r   