+
    iHàjŠ9  ã                  óÐ   € R t ^ RIHt ^ RIHt ^ RIt^ RIHt ^ RIH	t	 ^ RI
Ht ^ RIHtHt ^ RIHt ]P"                  ! R	4      t] ! R
 R4      4       tR R ltR R ltR R ltR# )uO  Per-iteration API request assembly for the conversation turn loop: build ``api_messages``
from the transcript, append MoA context, inject prefills, run the context-engine selection
hook and the send-time sanitizers, canonicalize for bit-perfect cache prefixes, build the
request-local prompt-cache plan LAST (after every transcript mutation), prepare the
persistent-MoA request, then measure request pressure. Nothing here imports
``agent.conversation_loop`` at module level (cycle) â€” loop-internal helpers resolve lazily
so ``patch("agent.conversation_loop.X")`` sites keep intercepting.
)Úannotations)Ú	dataclassN)ÚAny)Ú_sanitize_messages_surrogates)Úanchored_context_tokens)Úbuild_prompt_cache_planÚeffective_cache_ttl)Úbuild_api_messageszagent.conversation_loopc                  ól   € ] tR t^t$ RtR]R&   R]R&   R]R&   R]R&   R]R&   R]R	&   R]R
&   R]R&   RtR# )ÚAssembledRequestuä   Always ``action == "fallthrough"``; the fields are the iteration locals the assembly
produces (``api_messages``/``tools_for_api`` are the decorated request copies â€” the
canonical ``messages``/``agent.tools`` stay undecorated).ÚstrÚactionr   Úapi_messagesÚtools_for_apiÚ_moa_prepared_requestÚpending_moa_prepared_requestÚapprox_tokensÚrequest_pressure_tokensÚtotal_chars© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__Ú__static_attributes__r   ó    Ú@/home/ubuntu/.hermes/hermes-agent/agent/turn_request_assembly.pyr   r      s9   ‡ ñAð ƒKØÓØÓØÓØ"%Ó%ØÓØ Ó Ø×r   r   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )é   Úagentr   r   Ú
moa_configÚoriginal_user_messageÚreturnÚNoner   )Úformats   "r   Ú__annotate__r'   (   s8   € ÷ +Gñ +G˜sð +G°#ð +GÀ3ð +GÐ_bð +GÐgkñ +Gr   c                ó,  €  ^ RI Hp ^ RIHpHp T! \        V\        4      '       d   TMV! V4      TVP                  R4      ;'       g    . VP                  R4      ;'       g    / V! VR4      V! VR4      VP                  R4      '       d   \        VR,          4      MR\        VP                  R	4      ;'       g    R
4      V R7	      pV'       g   R# \        V4       F‚  pVP                  R4      R8X  g   K  VP                  RR4      p	\        V	\        4      '       d   V	R,           V,           VR&   M*\        V	\        4      '       d   . V	ORRRRV,           /NVR&    R# 	  R#   \         d"   p
\        P                  RT
4        Rp
?
R# Rp
?
ii ; i)z“Run the MoA reference models and append their aggregated context to the last user
message (as a trailing text part on multimodal turns). Fail-open.)Úflatten_message_text)Ú_preset_temperatureÚaggregate_moa_contextÚreference_modelsÚ
aggregatorÚreference_temperatureÚaggregator_temperatureÚreference_timeoutNÚdegraded_reference_policyÚloud)	Úuser_promptr   r,   r-   Útemperaturer/   r0   r1   r!   ÚroleÚuserÚcontentÚ z

ÚtypeÚtextz"MoA context aggregation failed: %s)Úagent.message_contentr)   Úagent.moa_loopr*   r+   Ú
isinstancer   ÚgetÚfloatÚreversedÚlistÚ	ExceptionÚloggerÚwarning)r!   r   r"   r#   Ú_flatten_mtr*   r+   Ú_moa_contextÚ_msgÚ_baseÚ_moa_excs   &&&&       r   Ú_append_moa_contextrJ   (   sp  € ð(GÝMßMá,ô Ð3´S×9Ò9ñ &ñ !Ð!6Ó7à%Ø'Ÿ^™^Ð,>Ó?×EÐEÀ2Ø!—~‘~ lÓ3×9Ð9°rÙ+¨JÐ8OÓPÙ#6°zÐC[Ó#\ð
 —>‘>Ð"5×6Ò6ô �jÐ!4Õ5Ô6àä&)Ø—‘Ð:Ó;×EÐE¸vó'ð ô/
ˆ÷2 ÙÜ˜\Ö*ˆDØ�x‰x˜Ó 6Ö)ØŸ™ ¨BÓ/�Ü˜e¤S×)Ò)Ø&+¨f¥n°|Õ&C�D˜’OÜ ¤t×,Ò,Ø&_¨Ð&_°¸ÀÈÐQ]ÕH]Ð/^Ð&_�D˜‘OÚó +øô ô GÜ�‰Ð;¸X×FÒFûðGúsC   ‚AE' Á
E' Á$(E' Â/E' Â=E' Ã!E' Ã:A'E' Å#E' Å'FÅ2FÆFc               ó(   € V ^8„  d   QhRRRRRRRR/# )r    r!   r   r   r   r$   Útupler   )r&   s   "r   r'   r'   V   s0   € ÷ @ñ @ ð @°3ð @ÐVYð @Ð^cñ @r   c                ó  € \        \        V P                  RR4      RR4      pRpVe)   \        VRR4      p\        V4      '       d	   V! W!4      pRpVf'   \        VRR4      p\        V4      '       d	   V! V4      pVe
   VR,          pWAV3# )zÔPersistent-MoA request: rebase the pending prepared request onto the new messages
when the client supports it, else prepare a fresh one. Returns
``(prepared_request, api_messages, pending_moa_prepared_request)``.ÚchatNÚcompletionsÚrebase_prepared_requestÚprepareÚmessages)ÚgetattrÚclientÚcallable)r!   r   r   Ú_moa_completionsÚpreparedÚ_rebaseÚ_prepares   &&&    r   Ú_prepare_moa_requestrZ   V   sž   € ô œw u§|¡|°V¸TÓBÀMÐSWÓXÐØ€HØ#Ò/ÜÐ*Ð,EÀtÓLˆÜ�G×ÒÙÐ;ÓJˆHØ'+Ð$ØÒÜÐ+¨Y¸Ó=ˆÜ�H×ÒÙ Ó-ˆHØÒØ 
Õ+ˆØÐ#?Ð?Ð?r   c               óD   € V ^8„  d   Qh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   Úcurrent_turn_user_idxÚ_ext_prefetch_cacheÚ_plugin_user_contextr"   Úactive_system_promptr#   r   Úrequest_loggerr$   r   r   )r&   s   "r   r'   r'   j   s|   € ÷ tñ tØðtØ ðtØ9<ðtØSVðtàðtà+.ðtàFIðtð ðtð ?Bðtð TWðtð ñ	tr   c       	        óÊ  € ^ RI Hp
HpHpHpHpHp ^ RIHpH	p \        WVW4WVR7      w  ppV'       d   \        V VWW4       V P                  '       dh   V'       d    V^ ,          P                  R4      R8X  d   ^M^ p\        V P                  4       F%  w  ppVP                  VV,           V! V4      4       K'  	  ^ Tu;8:  d   \!        V4      8  d   M MW,          MRpV! V VVVV	R7      p^ RIHp V! V V4       V P'                  V4      p^ R	IHp ^ R
IHp V! VV! V 4      4        ^ RIHp V! 4       pV'       d   V P5                  V4       T P<                  R8g  pT P?                  TTT'       d   T
MRR7      pT FD  p\A        TP                  R4      \B        4      '       g   K*  TR,          PE                  4       TR&   KF  	  T! T4       \G        T4       T PH                  pT PJ                  '       dÆ   T PL                  R8w  dµ   ^ RI'H(p  \S        T RR4      p!\U        TT\W        T PX                  T PL                  T PZ                  R7      T P\                  \A        T!\B        4      '       d   T!MRT P_                  4       T ! \S        T RR4      \S        T RR4      4      R7      p"T"P`                  pT"PH                  pRp#T PL                  R8X  d   \c        T TT4      w  p#pp^ RI2H3p$ T$! T 4      '       dY   \S        T RR4      R8X  d>   ^ RI4H5p% T%! \S        T RR4      \S        T RR4      4      '       d
   T! T4      p&MT! T4      p&MT! T4      p&M
T! TRR 7      p&T! T TT;'       g    RT&4      p'\m        T\S        T R!R4      4      p(T(RJT n7        T(e   T(p'MT! T Pp                  T'4      p'\s        R"TTT#TT&T'T&^,          4      #   \6         d    \8        P;                  RRR7        EL”i ; i)#a  Assemble the request in the original order. ORDER IS LOAD-BEARING: cache breakpoints
are injected only after whitespace normalization, the orphan sweep, thinking-only drop /
user merge and surrogate stripping, so the same row's bytes never vary across turns.)Ú_CODEX_INCOMPLETE_NUDGEÚ_apply_context_engine_selectionÚ_canonicalize_api_tool_callsÚ_clone_message_for_sendÚ _midturn_request_pressure_tokensÚ_pressure_with_real_floor)Úestimate_messages_tokens_roughÚ/estimate_native_anthropic_messages_tokens_rough)r\   Úext_prefetch_cacheÚplugin_user_contextr"   r_   r5   ÚsystemN)rC   )Ú#apply_rejected_thinking_suppression)Ú evict_stale_outbound_tool_images)Úoutbound_retired_image_ids)Ú%consume_pending_sanitizer_heal_noticez%sanitizer heal notice delivery failedT)Úexc_infoÚcodex_responses)Údrop_codex_reasoning_itemsÚdrop_nudge_markerr7   Úmoa)Ú*envelope_tool_part_cache_markers_supportedÚ_cached_system_prompt_static)ÚproviderÚmodelrx   r8   Úbase_url)Ú	cache_ttlÚnative_anthropicÚstatic_system_prefixÚdirect_native_tool_cacheÚtool_part_markers)Ú_agent_stale_thinking_on_wireÚapi_modeÚanthropic_messages)Ú)native_anthropic_preserves_prior_thinkingry   F)Úcharge_stale_thinkingÚ_usage_anchorÚfallthrough):Úagent.conversation_looprb   rc   rd   re   rf   rg   Úagent.model_metadatarh   ri   r	   rJ   Úprefill_messagesr>   Ú	enumerateÚinsertÚlenÚagent.anthropic_thinking_replayrm   Ú_sanitize_api_messagesÚagent.context_compressorrn   Úagent.image_eviction_policyro   Úagent.agent_runtime_helpersrp   Ú_emit_warningrB   rC   Údebugr�   Ú#_drop_thinking_only_and_merge_usersr=   r   Ústripr   ÚtoolsÚ_use_prompt_cachingrx   Úagent.prompt_cachingrv   rS   r   r   Ú
_cache_ttlry   Ú_use_native_cache_layoutÚ._direct_native_anthropic_tool_cache_capabilityrR   rZ   Úagent.turn_contextr€   Úagent.anthropic_thinking_policyrƒ   r   Ú_request_pressure_anchoredÚcontext_compressorr   ))r!   rR   r\   r]   r^   r"   r_   r#   r   r`   rb   rc   rd   re   rf   rg   rh   ri   r   Úeffective_systemÚ
sys_offsetÚidxÚpfmÚ_sel_incomingrm   rn   ro   rp   Ú_heal_noticeÚ_cross_protocolÚamr   rv   Ú_static_system_prefixÚ_initial_cache_planr   r€   rƒ   r   r   Ú_anchored_pressures)   &$$$$$$$$$                               r   Úassemble_api_requestr«   j   sã  € ÷÷ ÷ô
 &8ØÐ/DØ.Øô&Ñ"€LÐ"÷ Ü˜E <°ÔSð ××Ðß'¨L¸­O×,?Ñ,?ÀÓ,GÈ8Ô,S‘QÐZ[ˆ
Ü! %×"8Ñ"8Ö9‰HˆC�ð ×Ñ 
¨SÕ 0Ñ2IÈ#Ó2NÖOñ :ð ,-Ð0EÖ+UÌÈHË×+UˆÖ'Ð[_ð ñ 3Øˆ|˜X }¸^ô€Lõ Tá'¨¨|Ô<ð ×/Ñ/°Ó=€Lõ
 JÝFá$ \Ñ3MÈeÓ3TÔUðMÝUá<Ó>ˆßØ×Ñ Ô-ð —n‘nÐ(9Ñ9€OØ×<Ñ<Ø°ß5DÑ1È$ð =ó €Ló ˆÜ�b—f‘f˜YÓ'¬×-Ô-Ø˜y�M×/Ñ/Ó1ˆBˆy‹Mñ ñ ! Ô.ô " ,Ô/ð —K‘K€MØ× × Ð  U§^¡^°uÔ%<ÝSä '¨Ð/MÈtÓ TÐÜ5ØØô *Ø× Ñ ¨5¯>©>ÀÇÁôð #×;Ñ;ä)3Ð4IÌ3×)OÒ)OÑ%ÐUYà%*×%YÑ%YÓ%[ñ IÜ˜˜z¨2Ó.´¸¸zÈ2Ó0Nóô
Ðð& +×3Ñ3ˆØ+×1Ñ1ˆð
 !ÐØ‡~�~˜ÔÜL`Ø�<Ð!=óM
ÑIÐ˜|Ð-Iõ Aá$ U×+Ò+Ü�5˜* bÓ)Ð-AÔAÝaá8Ü˜˜z¨2Ó.´¸¸wÈÓ0K÷ò ñ !PÐP\Ó ]‘á >¸|Ó L‘á:¸<ÓH‰Má6°|Ð[`Ôaˆñ ?Øˆ|Ð-×3Ð3°°]óÐô
 1°¼7À5È/Ð[_Ó;`ÓaÐØ'9ÀÐ'E€EÔ$ØÒ%Ø"4Ññ
 #<Ø×$Ñ$Ð&=ó#
Ðô Ø�| ]Ð4IØ$ mÐ5LÈmÐ^_ÕN_óð øôY ô MÜ�‰Ð<Àtˆ×LðMús   Ä'N< Ä<N< Î<"O"Ï!O")r   Ú
__future__r   Údataclassesr   ÚloggingÚtypingr   Úagent.message_sanitizationr   Úagent.usage_anchorr   r˜   r   r   rœ   r	   Ú	getLoggerrC   r   rJ   rZ   r«   r   r   r   Ú<module>r³      sa   ðñõ #å !Û Ý å DÝ 6ß MÝ 1à	×	Ò	Ð4Ó	5€ð ÷ð ó ðõ+Gõ\@÷(tr   