+
    iHàj2 ã                  óò  a € 0 t $ R t^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	H
t
 ^ RIHt ^ RIHtHtHtHtHtHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIHt  ^ RI!H"t" ^ RI#H$t$ ^ RI%H&t&H't'H(t( ^ RI)H*t*H+t+H,t, ^ RI-H.t. ^ RI/H0t0H1t1 ^ RI2H3t3 ]Ph                  ! ]54      t6R R lt7R R lt8R R lt9R 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tAR) R* ltBR+ R, ltC]D! R-04      tER{R. R/ lltFR0 R1 ltGR2 R3 ltHR4 R5 ltIR6 R7 ltJ ! R8 R9]K4      tLR: R; ltMR< R= ltNR> R? ltOR@ RA ltPRB RC ltQRD^ /RE RF lltR] ! RG RH4      4       tSRI RJ ltTRK RL ltURM RN ltVRO RP ltWR|tXRR]YRS&   RT RU ltZRV RW lt[RX RY lt\RZ R[ lt]R{R\ R] llt^R^ R_ lt_R` Ra lt`Rb Rc ltaRd Re ltbRf Rg ltcR{Rh Ri lltdRj Rk lteRl Rm ltfRn Ro ltgR}RpRRqRRrRRsRQRtR/Ru Rv lllthRw Rx ltiRy Rz ltjR# )~aÇ  Per-turn setup for ``run_conversation`` (the turn prologue).

``build_turn_context`` runs the once-per-turn setup (stdio guard, sanitization, prompt
restore-or-build, session row, idle/preflight compaction via ``turn_context_compaction``,
pre_llm_call hook, prefetch, persistence), mutating ``agent`` as the loop expects, and
returns a ``TurnContext`` with only the locals the loop reads back.
``build_api_messages`` builds the wire copy for one API call.)ÚannotationsN)Úsuppress)Ú	dataclass)ÚAnyÚDictÚListÚMappingÚOptionalÚTuple)Ú#recover_rotated_compression_session)ÚIterationBudget)Úbuild_memory_context_block)Úis_trivial_prompt©Úflatten_message_text)Úforward_to_agent)Úpop_plugin_notices)Ústage_skills_index_note)ÚPERSISTENCE_ONLY_MESSAGE_FIELDSÚappend_messageÚstamp_message_timestamp)Úestimate_messages_tokens_roughÚ.estimate_native_anthropic_request_tokens_roughÚestimate_request_tokens_rough)Úbind_image_token_cost)Úanchored_context_tokensÚrestore_usage_anchor)Úparse_turn_authorc               ó$   € V ^8„  d   QhRRRRRR/# )é   Úagentr   ÚnameÚstrÚreturn© )Úformats   "Ú7/home/ubuntu/.hermes/hermes-agent/agent/turn_context.pyÚ__annotate__r'   )   s!   € ÷ *ñ *�Sð * ð *¨ñ *ó    c                ó.   € \        WR4      ;'       g    R# )uS   ``getattr(agent, name, "") or ""`` â€” route facts read off partial agents/doubles.Ú ©Úgetattr)r    r!   s   &&r&   Ú	_str_attrr-   )   s   € ä�5 Ó#×)Ð) rÐ)r(   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r    r   Úmessagesúlist[dict[str, Any]]Úsystem_promptr"   r#   Úintr$   )r%   s   "r&   r'   r'   .   s,   € ÷ 0ñ 0Øð0Ø.ð0Ø?Bð0àñ0r(   c                óú  € \        V\        V RR4      4      pVRJV n        Ve   V# \        V RR4      ;'       g    Rp ^ RIHp T! YT;'       g    RVR7      p\        V\        4      '       d    \        V\        4      '       g
   V^ 8¼  d   V# \        T 4      pTpT'       d•   \        T R
R4      R8X  dƒ   ^ RIHp	 T	! \        T RR4      \        T RR4      4      '       dX   ^ RIHp
 T Uu. uF&  p\        T\         4      '       d   \!        T4      MTNK(  	  ppT
! Y4       \#        Y‚;'       g    RTR7      # \%        Y‚;'       g    RTTR7      #   \         d    \        P                  RRR	7        Låi ; iu upi )zžToken estimate for automatic preflight compression: a valid provider usage anchor,
else the checkpoint-pruned native wire payload, else the generic estimator.Ú_usage_anchorNÚtools)Ú*estimate_native_responses_preflight_tokensr*   )r1   r5   zRnative Responses preflight estimate unavailable; using generic transcript estimateT©Úexc_infoÚapi_modeÚanthropic_messages)Ú)native_anthropic_preserves_prior_thinkingÚbase_urlÚmodel©Ú#apply_rejected_thinking_suppression)r1   r5   Úcharge_stale_thinking)r   r,   Ú_request_pressure_anchoredÚagent.codex_responses_adapterr6   Ú
isinstancer2   ÚboolÚ	ExceptionÚloggerÚdebugÚ_agent_stale_thinking_on_wireÚagent.anthropic_thinking_policyr;   Úagent.anthropic_thinking_replayr?   Údictr   r   )r    r/   r1   Úanchoredr5   r6   Únativer@   Úestimate_messagesr;   r?   Úmessages   &&&         r&   Ú_preflight_request_tokensrP   .   sŒ  € ô
 ' x´¸ÀÐQUÓ1VÓW€HØ'/°tÐ';€EÔ$ØÒØˆÜ�E˜7 DÓ)×1Ð1¨T€Eð
Ý\á;Ø¨=×+>Ð+>¸BÀeô
ˆô �fœc×"Ò"¬:°f¼d×+CÒ+CÈÐRSÌØˆMô :¸%Ó@ÐØ Ðß¤¨°
¸BÓ!?ÐCWÔ!WÝ]á4Ü�E˜: rÓ*¬G°E¸7ÀBÓ,G÷
ò 
õ \ñ  (ó!á'�Gô ",¨G´T×!:Ò!:”�W”ÀÒGÙ'ð ð !ñ 0°ÔIÜAØ!×1DÐ1DÀ"ÈEôð ô )Ø×)<Ð)<¸"ÀEØ3ôð øô7 ô 
Ü�‰ð0àð 	ö 	
ð
üò&!s   ¿E Á=E Ã+,E8Å"E5Å4E5c               ó    € V ^8„  d   QhRRRR/# ©r   r    r   r#   rD   r$   )r%   s   "r&   r'   r'   a   s   € ÷ 
ñ 
¨ð 
°ñ 
r(   c                óV   a €  ^ RI Hp V! V 3R lR 4       !  #   \         d     R# i ; i)z{Whether the active route replays stale thinking text; ``True`` (conservative full
charge) when route facts are unavailable.)Ústale_thinking_reaches_wirec              3  ó<   <"  € T F  p\        SV4      x € K  	  R # 5i©N)r-   )Ú.0Úkr    s   & €r&   Ú	<genexpr>Ú0_agent_stale_thinking_on_wire.<locals>.<genexpr>h   s   øé € ÐYÑ+X aŒi˜˜q×!Ð!Ó+Xùs   ƒT)r9   Úproviderr=   r<   )Úagent.message_sanitizationrT   rE   )r    rT   s   f r&   rH   rH   a   s5   ø€ ðÝJá*ÜYÑ+XÓYñ
ð 	
øô ô Úðús   ƒ ™(§(c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úext_prefetch_cacher"   Úplugin_user_contextr#   úOptional[str]r$   )r%   s   "r&   r'   r'   n   s$   € ÷ ;ñ ;Øð;Ø25ð;àñ;r(   c                ó¢   € V '       d   \        V 4      MRpW!3 Uu. uF  q3'       g   K  VNK  	  ppV'       d   RP                  V4      # R# u upi )zïThe ephemeral context of one turn (memory prefetch + ``pre_llm_call``) as one text
block; ``None`` when nothing is injected. The string sidecar appends it to ``content``;
a multimodal (list) turn carries it as a durable text part (#71998).r*   ú

N)r   Újoin)r^   r_   ÚfencedÚpartÚ
injectionss   &&   r&   Úcompose_multimodal_context_partrg   n   sP   € ÷ @RÔ'Ð(:Ô;ÐWY€FØ$*Ñ#@ÓIÑ#@˜4ÃD—$�$Ñ#@€JÐIß&0ˆ6�;‰;�zÓ"Ð:°dÐ:ùò Js
   ›	A©Ac               ó(   € V ^8„  d   QhRRRRRRRR/# )r   Úcontentr   r^   r"   r_   r#   r`   r$   )r%   s   "r&   r'   r'   y   s2   € ÷ Gñ GØðGØ&)ðGØ@CðGàñGr(   c                ót   € \        V \        4      '       g   R# \        W4      pVf   R# V R,           V,           # )a6  Compose the API-bound content of the current turn's string user message.

Single source for the ``api_content`` sidecar and the wire bytes so they never drift
(what turn N sends is what turn N+1 replays). ``None`` when nothing is injected or the
content is not a string (list content takes the text-part path).Nrb   )rC   r"   rg   )ri   r^   r_   Ú	injections   &&& r&   Úcompose_user_api_contentrl   y   s9   € ô �gœs×#Ò#ÙÜ/Ð0BÓX€IØÒ$ˆ4ÐF¨'°FÕ*:¸YÕ*FÐFr(   c               ó    € V ^8„  d   QhRRRR/# )r   Úapi_msgúdict[str, Any]r#   r`   r$   )r%   s   "r&   r'   r'   ‡   s   € ÷ ñ  Nð °}ñ r(   c                óš   € V P                  RR4      p\        V\        4      '       d#   V'       d   V P                  R4      R9   d   WR&   V# )z˜Pop the ``api_content`` sidecar and substitute it into ``content`` (keeps the
prompt-cache prefix byte-stable). Returns the popped sidecar, or ``None``.Úapi_contentNÚroleri   ©ÚuserÚ	assistant)ÚpoprC   r"   Úget)rn   Úsidecars   & r&   Úsubstitute_api_contentry   ‡   sA   € ð �k‰k˜-¨Ó.€GÜ�'œ3×Ò§G°·±¸FÓ0CÐG\Ô0\Ø$�	ÑØ€Nr(   c               ó    € V ^8„  d   QhRRRR/# )r   Úmsgro   r#   ÚNoner$   )r%   s   "r&   r'   r'   �   s   € ÷ !ñ ! ð !°4ñ !r(   c                ó*   € V P                  RR4       R# )z™Drop the ``api_content`` sidecar from a message whose content was rewritten
(replaying it would resend what the rewrite removed; cost is one cache miss).rq   N)rv   )r{   s   &r&   Údrop_stale_api_contentr~   �   s   € ð ‡G�GˆM˜4Ö r(   c               ó    € V ^8„  d   QhRRRR/# )r   r{   zMapping[str, Any]r#   r`   r$   )r%   s   "r&   r'   r'   –   s   € ÷ -ñ -Ð%6ð -¸=ñ -r(   c                óX   € V P                  R4      p\        V\        4      '       d   V# R# )zEExtract the ``api_content`` sidecar; ``None`` when absent/non-string.rq   N)rw   rC   r"   )r{   Úvs   & r&   Úextract_api_content_sidecarr‚   –   s'   € à�‰�Ó€AÜ˜1œc×"Ò"ˆ1Ð,¨Ð,r(   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r    r   Úattrr"   r#   r$   )r%   s   "r&   r'   r'   œ   s!   € ÷ 1ñ 1˜#ð 1 Sð 1¨Sñ 1r(   c                ó  € \        WR4      ;'       g    Rp\        W4      '       d-   \        \        4      ;_uu_ 4        \	        WR4       RRR4       \        V\        4      '       d   V# R#   + '       g   i     L*; i)z}One-shot per-turn note: read and clear, so the system prompt stays byte-stable and a
cached agent never replays a stale note.r*   N)r,   Úhasattrr   rE   ÚsetattrrC   r"   )r    r„   Únotes   && r&   Ú_pop_turn_noter‰   œ   s]   € ô �5 Ó#×)Ð) r€DÜˆu×ÒÜ”i× Õ Ü�E Ô$÷ !ä˜d¤C×(Ò(ˆ4Ð0¨bÐ0÷ !× ús   ¿A.Á.A>	c               ó    € V ^8„  d   QhRRRR/# ©r   r    r   r#   r"   r$   )r%   s   "r&   r'   r'   ¦   s   € ÷ @ñ @¨cð @°cñ @r(   c                ó   € \        V R4      # )z.Pop the gateway's per-turn must-deliver notes.Ú_gateway_turn_context_notes©r‰   ©r    s   &r&   Ú"consume_gateway_turn_context_notesr�   ¦   s   € ä˜%Ð!>Ó?Ð?r(   c               ó    € V ^8„  d   QhRRRR/# r‹   r$   )r%   s   "r&   r'   r'   «   s   € ÷ 9ñ 9 sð 9¨sñ 9r(   c                ó   € \        V R4      # )zžPop the surface-switch note staged by the system-prompt restore (#104414); rides the same
user-message channel as the gateway notes, behind the cached prefix.Ú_surface_switch_noterŽ   r�   s   &r&   Úconsume_surface_switch_noter”   «   s   € ô ˜%Ð!7Ó8Ð8r(   c               ó    € V ^8„  d   QhRRRR/# r‹   r$   )r%   s   "r&   r'   r'   ±   s   € ÷ 7ñ 7 Sð 7¨Sñ 7r(   c                ó   € \        V R4      # )zVPop the skills-index delta staged by the system-prompt restore; same channel as above.Ú_skills_index_noterŽ   r�   s   &r&   Úconsume_skills_index_noter˜   ±   s   € ä˜%Ð!5Ó6Ð6r(   c               ó$   € V ^8„  d   QhRRRRRR/# )r   ri   r   Únotesr`   r#   rD   r$   )r%   s   "r&   r'   r'   ¶   s!   € ÷ ñ °ð ¸Mð Èdñ r(   c                óÚ   € V'       d   \        V \        4      '       g   R# \        \        4      ;_uu_ 4        V P	                  RRRV/4        RRR4       R#   + '       g   i     R# ; i)z�Append must-deliver notes as a durable text part on a multimodal (list) user
message (the sidecar path returns ``None`` for non-string content).FÚtypeÚtextNT)rC   Úlistr   rE   Úappend)ri   rš   s   &&r&   Ú"append_notes_to_multimodal_contentr    ¶   sM   € ÷ œ
 7¬D×1Ò1ÙÜ	”)×	Õ	Ø�‰˜ ¨°Ð6Ô7Ø÷ 
×	×	Ð	ñ ús   ¸AÁA*	Úcronc               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r    r   r/   ú	list[Any]Útitle_user_messager`   r#   r|   r$   )r%   s   "r&   r'   r'   Æ   s8   € ÷ GMñ GMØðGMØ#ðGMØ9FðGMà	ñGMr(   c                óØ  a a€ \        S RR4      p\        S RR4      pV'       d	   V'       g   R# \        \        S RR4      ;'       g    R4      P                  4       pV\        9   d   R#  ^ RIHp ^ RIHpHp Rp	Rp
\        T;'       g    . 4       F­  p\        V\        4      '       g   K  VP                  R4      R	8X  g   K3  V! VP                  R
4      4      P                  4       p	VP                  R4      p\        V\        4      '       d/   \        VP                  R4      \        4      '       d
   VR,          p
 M	  Ve   VP                  4       p	V	'       g   R# \        S RR4      '       g;   \        S RR4      p\        V4      '       d   V! 4        \        S RR4      '       g   R# VR8X  d   V! W4V	4       R# R Uu/ uF  pV\        S VR4      bK  	  upoT! TTT	T\        S RR4      ;'       g    \        S RR4      S\        S RR4      V V3R lV
R7	      pVe   VP                  f
   VS n        R# R# R# u upi   \"         d    \$        P'                  RRR7        R# i ; i)zHKick off auto-titling for the session's first user message; never fatal.Ú_session_dbNÚ
session_idÚplatformr*   r   )Úapply_subagent_titleÚmaybe_auto_titlerr   rt   ri   Údisplay_metadataÚtitle_previewÚ_session_db_createdFÚ_ensure_db_sessionÚsubagentÚ_title_failure_callbackÚ_emit_auxiliary_failureÚ_on_session_titlec                 óp   <€ \        S R R4      SR ,          8H  ;'       d    \        S RR4      SR,          8H  # )r=   Nr[   r+   )r    Úmain_runtimes   €€r&   Ú<lambda>Ú4_maybe_title_session_at_turn_start.<locals>.<lambda>  sA   ø€ Ü˜˜w¨Ó-°¸gÕ1FÑF÷ Qð QÜ˜E :¨tÓ4¸ÀZÕ8PÑPðQr(   )Úconversation_historyÚfailure_callbackr´   Útitle_callbackÚruntime_validatorr¬   z%Turn-start auto-title dispatch failedTr7   )r=   r[   r<   Úapi_keyr9   r§   )r,   r"   ÚlowerÚ_UNTITLED_PLATFORMSÚagent.message_contentr   Úagent.title_generatorr©   rª   ÚreversedrC   rK   rw   ÚstripÚcallableÚidentÚ_deferred_title_upgraderE   rF   rG   )r    r/   r¤   Ú
session_dbr§   r¨   r   r©   rª   Ú	user_textr¬   r{   ÚmetadataÚensurerX   Úupgrader´   s   f&&             @r&   Ú"_maybe_title_session_at_turn_startrÊ   Æ   sB  ù€ ô ˜ ¨tÓ4€JÜ˜ ¨dÓ3€JßŸZÙÜ”7˜5 *¨bÓ1×7Ð7°RÓ8×>Ñ>Ó@€HØÔ&Ô&Ùð<MÝ>ßPð ˆ	ØˆÜ˜HŸN˜N¨Ö+ˆCÜ˜#œt×$Ô$¨¯©°«¸FÖ)BÙ0°·±¸Ó1CÓD×JÑJÓL�	ØŸ7™7Ð#5Ó6�Ü˜h¬×-Ò-´*¸X¿\¹\È/Ó=ZÔ\_×2`Ò2`Ø$,¨_Õ$=�MÙñ ,ð Ò)Ø*×0Ñ0Ó2ˆIßÙô �uÐ3°U×;Ò;Ü˜UÐ$8¸$Ó?ˆFÜ˜×ÒÙ”Ü˜5Ð"7¸×?Ò?ÙØ�zÔ!Ù  ¸ÔCÙñ \ó
á[�ð Œw�u˜a Ó&Ò&Ù[ñ
ˆñ
 #ØØØØ!)ä˜Ð8¸$Ó?÷ Cð CÜ˜5Ð";¸TÓBà%Ü" 5Ð*=¸tÓDõð (ô
ˆð& Ò 7§=¡=Ò#8Ø,3ˆEÖ)ñ $9Ñùò1
øô4 ô MÜ�‰Ð<Àtˆ×LðMúsQ   Á+I ÂI Â-I ÃBI Å!AI Æ.I Ç I ÇH>ÇI Ç8A I È>I É"I)É(I)c               ó    € V ^8„  d   QhRRRR/# ©r   r    r   r#   r|   r$   )r%   s   "r&   r'   r'     s   € ÷ !ñ !¨ð !°ñ !r(   c                óV   € \        V RR4      pVf   R# RV n        ^ RIHp V! V4       R# )zYFire the title upgrade ``_maybe_title_session_at_turn_start`` held back; no-op when none.rÄ   N)Ústart_title_upgrade)r,   rÄ   r¿   rÎ   )r    rÉ   rÎ   s   &  r&   Ústart_deferred_title_upgraderÏ     s,   € ä�eÐ6¸Ó=€GØ‚ÙØ$(€EÔ!Ý9Ù˜Ö r(   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r/   r£   Úuser_messager   r#   r2   r$   )r%   s   "r&   r'   r'     s!   € ÷ $ñ $¨Yð $Àcð $Ècñ $r(   c                ód  € ^ RI Hp Rp\        \        V 4      ^,
          RR4       F†  pW,          p\	        V\
        4      '       d   VP                  R4      R8X  g   K9  VP                  R4      V8X  d   Vu # V! V4      pVf   Ka  VP                  R4      V8X  d   Vu # V^ 8  g   K„  TpKˆ  	  V# )uÿ  Locate this turn's user message after compaction rebuilt ``messages``.

Prefers the LAST user message whose content exactly matches this turn's text, else
the last user-originated turn; compaction handoffs are never the fallback.
Returns -1 when there is no user-originated message.

Compression replaces list entries with fresh copies (and may append a todo-snapshot user message or a
restored user turn AFTER the surviving copy of the current turn's message), so a pre-compression index
is meaningless. Prefer the LAST user message whose content exactly matches this turn's text â€” the
surviving copy in the common case â€” so the injection stamp and the #48677 persist override can't land on
a todo-snapshot or historical row. Fall back to the last *user-originated* turn when no exact match
survives (merge-summary-into-tail rewrites the content but the trackers still need a live anchor).
Compaction handoffs must never become the fallback anchor (#80622) â€” they are reference-only
scaffolding, not the active ask.
©Úuser_originated_turn_viewrr   rt   ri   éÿÿÿÿ)Úagent.context_compressorrÔ   ÚrangeÚlenrC   rK   rw   )r/   rÑ   rÔ   ÚfallbackÚir{   Ú	live_views   &&     r&   Úreanchor_current_turn_user_idxrÜ     s¥   € õ  Cà€HÜ”3�x“= 1Õ$ b¨"Ö-ˆØ�kˆÜ˜3¤×%Ò%¨#¯'©'°&«/¸VÔ*CÙð �7‰7�9Ó Ô-ØŠHÙ-¨cÓ2ˆ	ØÒÙØ�=‰=˜Ó# |Ô3ØŠHð �aŽ<ØŠHñ! .ð" €Or(   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r    r   ÚresultrÑ   r#   r$   )r%   s   "r&   r'   r'   A  s)   € ÷ #ñ #¨ð #°Sð #Èð #ÐPSñ #r(   c                ót  € \        V\        4      '       d   VP                  R4      R8X  d   V# VP                  R4      p\        \	        V RR4      ;'       g    R4      p\        V\
        4      '       d   V'       d   Vf   V# \        W24      pV^ 8  g   V\        V4      8¼  d   V# W5,          p\        V\        4      '       d   VP                  R4      R8X  g   V# ^ RIH	p V! V4      pVP                  R	4      V8w  d/   \        V\        4      '       d   VP                  R	4      V8X  g   V# WAR
&   WQR&   V# )a>  Stamp ``{turn_id, current_turn_user_idx}`` on a result envelope, proven against the
exact ``result["messages"]`` projection it travels with.

Hosts that settle their own transcript by index (hermes-webui) must never guess which
row is the current user turn after this loop rewrote history (alternation repair,
compaction, post-turn micro-compaction): a guessed index or a text match can relabel an
identical historical prompt and claim its old answer as this turn's. So the producer
exports the coordinate, computed on the final list, only when the addressed row is this
turn's user message verbatim. Otherwise the keys are omitted and hosts fail closed.

A preflight-timeout envelope carries the prior history without this turn's row (#7100), so a
repeated prompt would resolve to its historical copy: nothing is exported there.
Úturn_exit_reasonÚcontext_compression_timeoutr/   Ú_current_turn_idr*   rr   rt   rÓ   ri   Úturn_idÚcurrent_turn_user_idx)
rC   rK   rw   r"   r,   rž   rÜ   rØ   rÖ   rÔ   )	r    rÞ   rÑ   r/   rã   ÚidxÚrowrÔ   rÛ   s	   &&&      r&   Úexport_current_turn_boundaryrç   A  s
  € ô �fœd×#Ò# v§z¡zÐ2DÓ'EÐIfÔ'fØˆØ�z‰z˜*Ó%€HÜ”'˜%Ð!3°RÓ8×>Ð>¸BÓ?€GÜ�h¤×%Ò%¯W¸Ò8LØˆÜ
(¨Ó
@€CØ
ˆQ„w�#œ˜X›Ô&ØˆØ
�-€CÜ�sœD×!Ò! c§g¡g¨f£o¸Ô&?ØˆÝBá)¨#Ó.€IØ
‡w�wˆyÓ˜\Ô)Ü�9œd×#Ò#¨	¯©°iÓ(@ÀLÔ(PàˆØˆ9ÑØ&)Ð"Ñ#Ø€Mr(   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )r   Úorig_lenr2   Únew_lenÚorig_tokensÚ
new_tokensr#   rD   r$   )r%   s   "r&   r'   r'   g  s;   € ÷ Wñ WØðWØðWØ.1ðWØ?BðWà	ñWr(   c                óJ   € W8  ;'       g    V^ 8„  ;'       d    W2R,          8  # )uu  ``True`` if a compression pass materially reduced the request: fewer rows, or a
>5% token cut with the same rows (same floor as the overflow-handler retry).

Compression can succeed by summarising message contents â€” reducing the estimated request token count â€”
without reducing the message row count. Treating row count as the sole progress signal false-positives
on size-only wins and surfaces a misleading "Cannot compress further" failure even when post-compression
tokens are well below the model context window. See issue #39548 for an observed case: 220 â†’ 220
messages, ~288k â†’ ~183k tokens on a 1M-context model still triggered auto-reset.
The token reduction must be *material* (>5%) to count as progress â€” the same floor the overflow-handler
retry path uses (conversation_loop.py, 39550) â€” so a sub-5% wobble doesn't keep the multi-pass loop
spinning. See #39550.
çffffffî?r$   )ré   rê   rë   rì   s   &&&&r&   Úcompression_made_progressrï   g  s*   € ð Ñ×VÐV +°¡/×"UÐ"U°jÐQUÕCUÑ6UÐVr(   c                  ó   € ] tR tRtRtRtR# )ÚPreflightCompressionTimedOutiy  z=Raised when an oversized turn cannot safely finish preflight.r$   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__static_attributes__r$   r(   r&   rñ   rñ   y  s   † ÝGr(   rñ   c               ó    € V ^8„  d   QhRRRR/# ©r   Úrequest_tokensr2   r#   r|   r$   )r%   s   "r&   r'   r'   }  s   € ÷ ñ Àð Èñ r(   c                óÌ   € ^ RI HpHp V! V 4      '       g   R# V! W4      RJ d2   \        P	                  RVR V P
                  P                  R 4       R# \        RVR R24      h)u¯  Stop an oversized turn instead of sending its unchanged provider payload.
Only a request the model cannot accept (above its context window, or of unknown fit) is stopped: a
request that merely sits above the compression threshold is sent unchanged, exactly as the
cooldown-blocked path sends it every turn â€” otherwise a slow summariser turns a session that still
fits its window into a turn that can never run (#113646, #114594).)Úcontext_compression_timed_outÚrequest_exceeds_model_windowNFzzPreflight compression timed out but the request (~%s tokens) fits the model window (%s); sending it uncompressed this turnÚ,z_Context compression timed out before it could commit while the request was still approximately z] tokens. The provider call was not sent. Run /compress and wait for it to finish, then retry.)Úagent.conversation_compressionrü   rý   rF   ÚwarningÚcontext_compressorÚcontext_lengthrñ   )r    rú   rü   rý   s   &&  r&   Ú$_fail_closed_after_preflight_timeoutr  }  sz   € ÷ ká(¨×/Ò/ÙÙ# EÓ:¸eÓCÜ�‰ð0à˜aÐ  e×&>Ñ&>×&MÑ&MÈaÐ%Pô	
ñ
 	Ü
&ð	#Ø#1°!Ð"4ð 5Mð	Móð r(   c               ó    € V ^8„  d   QhRRRR/# rù   r$   )r%   s   "r&   r'   r'   •  s   € ÷ ñ Àð Èñ r(   c                ó¢   € ^ RI HpHp V! W4      RJd   R# V! V 4      '       d   R# V P                  P                  p\        RVR RVR R24      h)uD  Stop an over-window turn the moment preflight proves it cannot shrink the session, with
"start a new session" guidance, instead of sending a request the model cannot accept.

``_fail_closed_after_preflight_timeout`` only stops a turn whose compression wait timed out. A
pass that ran and reclaimed nothing (or under 5%) on a request still above the model window used
to fall through to the provider call: the provider rejected it, the overflow handler forced
another compression pass, and each pass re-waited its budget while the UI sat blocked (#116472:
~356k tokens on a 131k window). Only a ``True`` verdict fails closed â€” an unknown window or a
fitting request keeps the send-as-is behaviour â€” and a pass skipped by the summary-failure
cooldown is a defer, not proof of incompressibility, so it keeps its typed cooldown result.
)Úcompression_blocked_transientlyrý   TNzTContext compression could not bring this session under the model's context window (~rþ   z tokens vs zp). The provider call was not sent. Start a new session with /new; this session is too large to compress further.)rÿ   r  rý   r  r  rñ   )r    rú   r  rý   Úwindows   &&   r&   Ú%_fail_closed_on_insufficient_progressr  •  se   € ÷ má# EÓ:À$ÓFÙÙ& u×-Ò-ÙØ×%Ñ%×4Ñ4€FÜ
&ð	Ø˜AÐ˜k¨&°¨ð 4^ð	^óð r(   c               ó    € V ^8„  d   QhRRRR/# rR   r$   )r%   s   "r&   r'   r'   ¯  s   € ÷ ñ ¨cð °dñ r(   c                ób   € \        \        V RR4      ;'       d    \        V RR4      '       * 4      # )zÖWhether a detached review fork has yet to send its first provider request: it
replays the parent's FULL snapshot as a warm cache read, so compaction must wait
for that first response. Dormant without the attribute.Ú._review_defer_compaction_before_first_responseFÚ _turn_received_provider_response)rD   r,   r�   s   &r&   Ú"_review_fork_first_request_pendingr  ¯  s:   € ô Ü�ÐGÈÓO÷ 	Jð 	JÜ˜ÐAÀ5ÓIÔIóð r(   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   rë   r2   rì   Úthreshold_tokensr#   rD   r$   )r%   s   "r&   r'   r'   ¹  s2   € ÷ bñ bØðbØ"%ðbØ9<ðbà	ñbr(   c                óJ   € W8¬  ;'       d    V ^ 8„  ;'       d    WR,          8  # )z_Another immediate summary only if still over threshold AND the previous pass cut
tokens by >5%.rî   r$   )rë   rì   r  s   &&&r&   Ú,_compression_warrants_another_preflight_passr  ¹  s*   € ð
 Ñ)×aÐa¨k¸A©o×aÐaÀ*Ð]aÕOaÑBaÐar(   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   r/   r0   Úprotect_first_nr2   Úprotect_last_nr  r#   rD   r$   )r%   s   "r&   r'   r'   Á  s4   € ÷ 
ñ 
Ø"ð
Ø58ð
ØJMð
Øadð
à	ñ
r(   c                ód   € \        V 4      W,           ^,           8„  ;'       g    \        V 4      V8¬  # )a!  Cheap gate for the (expensive) full preflight estimate: message count exceeds the
protected ranges OR a rough char-based estimate crosses the threshold (few-but-huge
case). The estimator undercounts by design (omits system/tools) so one large base64
image is not mistaken for ~250K tokens.)rØ   r   )r/   r  r  r  s   &&&&r&   Ú_should_run_preflight_estimater  Á  s7   € ô 	ˆH‹˜Õ8¸1Õ<Ñ<÷ 	Hð 	HÜ)¨(Ó3Ð7GÑGðr(   Úlast_compaction_tokensc               ó8   € V ^8„  d   QhRRRRRRRRRRR	RR
RRR/# )r   ÚenabledrD   Úidle_after_secondsr2   Úidle_gap_secondsÚfloatÚtokensÚfloor_tokensÚcooldown_activer  r#   r$   )r%   s   "r&   r'   r'   Î  sN   € ÷ $ñ $Øð$Ø*-ð$ØAFð$ØPSð$àð$à(,ð$àFIð$ð 
ñ$r(   c                ó~   € V '       d   V^ 8:  g   W!8  g	   V'       d   R# TpV^ 8”  d   \        WvV,           4      pW78„  # )ug  Pure predicate: idle compaction fires after a wall-clock gap of
``idle_after_seconds`` (opt-in, <= 0 disables), independent of ``threshold_tokens``;
never at/below ``floor_tokens`` or during a compression-failure cooldown.

``floor_tokens`` (``threshold_tokens Ã— summary_target_ratio``) is a theoretical target a
real pass routinely misses (system prompt, tool schemas and protected head/tail are
incompressible), so a session compacted to above it would re-summarise on every idle
resume without growing. ``last_compaction_tokens`` â€” what the previous pass actually
produced (``ContextCompressor.last_compression_rough_tokens``, same rough shape as
``tokens``) â€” raises the floor to ``last + floor_tokens`` so the transcript must gain a
floor's worth of NEW content first. ``0`` (nothing compacted yet / counter reset) keeps
the original semantics exactly.

A session that compacted to well above that target therefore stays above it forever, so every later idle
resume re-runs a full summarisation over a transcript that has not grown â€” minutes of silently blocked
prompt on a slow route, reclaiming nothing (#97239).
F)Úmax)r  r  r  r  r  r  r  Úeffective_floors   $$$$$$$ r&   Ú_should_idle_compactr#  Î  s@   € ÷* Ð(¨AÔ-Ð1AÔ1V×ZiÙØ"€OØ Ô!Ü˜oÈÕ/TÓUˆØÑ#Ð#r(   c                  ó¤   € ] tR tRt$ RtR]R&   R]R&   R]R&   R	]R
&   R]R&   R]R&   R]R&   R]R&   RtR]R&   RtR]R&   RtR]R&   Rt	R]R&   Rt
R# )ÚTurnContextië  zCValues produced by the turn prologue and consumed by the turn loop.r"   rÑ   r   Úoriginal_user_messager0   r/   úOptional[list[dict[str, Any]]]r·   r`   Úactive_system_promptÚeffective_task_idrã   r2   rä   FrD   Úshould_review_memoryr*   r_   r^   Úpreflight_compression_blockedr$   N)rò   ró   rô   rõ   rö   Ú__annotations__r*  r_   r^   r+  r÷   r$   r(   r&   r%  r%  ë  s`   ‡ áMàÓØÓØ"Ó"Ø8Ó8Ø'Ó'ØÓØƒLØÓØ!&Ð˜$Ó&Ø!Ð˜Ó!Ø Ð˜Ó Ø*/Ð! 4×/r(   r%  c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r    r   Úfailure_msgr"   Úpending_cli_messager#   r|   r$   )r%   s   "r&   r'   r'   ý  s)   € ÷ 3ñ 3˜sð 3°Sð 3Èsð 3ÐW[ñ 3r(   c                óì  €  \        V RR4      pVf	   V! 4        MV;_uu_ 4        V! 4        RRR4       \        V\        4      '       d   VP                  R4      '       d
   RV n        R# R#   + '       g   i     LH; i  \         d/    \        P                  Y P                  ;'       g    RRR7        Lƒi ; i  \        T\        4      '       d   TP                  R4      '       d	   RT n        i i ; i)uG  Run ``fn`` under the session persist lock (when the agent has one), log-and-swallow
failures, then drop staged CLI input â€” unless it is an unmarked handoff kept for a
close retry (once ``_db_persisted`` the close path must not treat it as pre-worker
UI input). Eager clearing keeps a preflight crash from leaking stale input.Ú_session_persist_lockNÚnoneTr7   Ú_db_persisted)	r,   rE   rF   r   r§   rC   rK   rw   Ú_pending_cli_user_message)r    Úfnr.  r/  Úlocks   &&&& r&   Ú_persist_under_lockr7  ý  sÆ   € ð
3Ü�uÐ5°tÓ<ˆØŠ<Ù�Dç’Ù”÷ ô
 Ð-¬t×4Ò4Ð8K×8OÑ8OÐP_×8`Ò8`Ø.2ˆEÖ+ñ 9a÷ —ûäô OÜ�‰�{×$4Ñ$4×$>Ð$>¸ÈˆÖNðOûô Ð-¬t×4Ò4Ð8K×8OÑ8OÐP_×8`Ò8`Ø.2ˆEÕ+ð 9aús[   ‚B  šB< ›
B  ¥A-­B  Á-A=	Á8B  Á<B< Á=B  Â ,B9Â-	B9Â6B< Â8B9Â9B< Â<-C3Ã*	C3c               ó    € V ^8„  d   QhRRRR/# rÌ   r$   )r%   s   "r&   r'   r'     s   € ÷ 
ñ 
 ð 
¨ñ 
r(   c                ó*  € \        \        4      ;_uu_ 4        ^ RIHp ^ RIHp V! V 4      ;'       g    RpT! \        V R4      \        V R4      3/ R Uu/ uF  qD\        W4      bK  	  upBRV/B  RRR4       R# u upi   + '       g   i     R# ; i)	z�Tell auxiliary_client the live main provider/model for this turn (after primary
restoration settled the runtime). Never raises: failure loses only the scope.)Úset_runtime_main)Úresolve_prompt_cache_scope_safer*   r[   r=   Úcache_scopeN)Úrequested_providerr<   r»   r9   Ú	auth_moder§   )r   rE   Úagent.auxiliary_clientr:  Úagent.prompt_cache_scoper;  r-   )r    r:  r;  Ú_cache_scoperX   s   &    r&   Ú_publish_runtime_mainrB    s’   € ô 
”)×	Õ	Ý;ÝLñ 7°uÓ=×CÐCÀˆÙÜ�e˜ZÓ(¬)°E¸7Ó*Cñ	
ñ0ó ñ 0¨!”)˜EÓ%Ò%ñ 0ñ ñ	
ð
 %ó	
÷ 
Ñ	ùò÷ 
×	Ð	ús   ˜=BÁA<Á)	BÁ<BÂB	c               ó    € V ^8„  d   QhRRRR/# rÌ   r$   )r%   s   "r&   r'   r'   (  s   € ÷ Nñ N¨Cð N°Dñ Nr(   c                óD  €  R\         P                  9   d   ^ RIHp V! V 4       \	        V RR4      '       g@   R\         P                  9   d)   ^ RIHp ^ RIHp V! 4       '       d   V! V RRR	7       R# R# R# R#   \         d    \        P                  R
RR7        R# i ; i)zÏLate-connecting MCP servers land in THIS turn's snapshot, before the first API
call assembles ``tools=``. ``preserve_prefix`` keeps the tool array append-only so a
flapping ``check_fn`` can't fork the cache.ztools.connectors.mcp)Úadopt_late_connectionsÚ_skip_mcp_refreshFztools.mcp_tool)Úhas_registered_mcp_tools)Úrefresh_agent_mcp_toolsT)Ú
quiet_modeÚpreserve_prefixz&between-turns MCP tool refresh skippedr7   N)ÚsysÚmodulesÚtools.connectors.mcprE  r,   Útools.mcp_tool_discoveryrG  Útools.mcp_tool_agentrH  rE   rF   rG   )r    rE  rG  rH  s   &   r&   Ú _refresh_mcp_tools_between_turnsrP  (  s�   € ðNð "¤S§[¡[Ô0ÝCÙ" 5Ô)ô �uÐ1°5×9Ò9Ð>NÔRU×R]ÑR]Ô>]ÝIÝDÙ'×)Ò)Ù'¨¸$ÐPT×Uñ *ñ ?^Ñ9øô
 ô NÜ�‰Ð=Èˆ×MðNús   ‚A#A9 Á&A9 Á9"BÂBc               ó0   € V ^8„  d   QhRRRRRRRRRRR	R
/# )r   r    r   Útask_idr`   Úpersist_user_messageÚpersist_user_timestampúOptional[float]Úpersist_user_platform_idr#   ztuple[str, str]r$   )r%   s   "r&   r'   r'   =  s>   € ÷ &ñ &Øð&Ø&ð&ØORð&à+ð&àGTð&ð ñ&r(   c                óæ  € W n         RV n        W0n        W@n        WPn        T;'       g    \        \        P                  ! 4       4      pW`n        0 \        V RR	4      mVkV n
        \        \        V RR4      ;'       g    R4      ;'       gC    V P                  ;'       g    R RV R\        P                  ! 4       P                  R,           2pRV n        Wpn        RV n        ^ RIHp V! W4       Wg3# )
zStage callback/persist overrides on the agent and bind this turn's task and turn
ids. Returns ``(effective_task_id, turn_id)``.NÚ_process_owner_task_idsÚ_relay_pending_turn_idr*   ÚsessionÚ::Né   N)Únote_turn_startr$   )Ú_stream_callbackÚ_persist_user_message_idxÚ_persist_user_message_overrideÚ_persist_user_message_timestampÚ!_persist_user_message_platform_idr"   ÚuuidÚuuid4Ú_current_task_idr,   rX  r§   ÚhexrY  râ   Ú_current_api_request_idÚagent.agent_runtime_helpersr]  )	r    rR  Ústream_callbackrS  rT  rV  r)  rã   r]  s	   &&&&&&   r&   Ú_bind_turn_identityrj  =  sð   € ð -ÔØ&*€EÔ#Ø+?Ô(Ø,BÔ)Ø.FÔ+à×4Ð4¤3¤t§z¢z£|Ó#4ÐØ.ÔØ$g¤g¨eÐ5NÐPRÓ&SÐ$gÐUfÐ$g€EÔ!Ü”'˜%Ð!9¸2Ó>×DÐDÀ"ÓE÷ ð Ø×Ñ×(Ð(˜yÐ
)¨Ð+<Ð*=¸Q¼t¿zºz»|×?OÑ?OÐPRÕ?SÐ>TÐUð ð $(€EÔ Ø$ÔØ$&€EÔ!õ <Ù�EÔ#ØÐ%Ð%r(   Fztuple[tuple[str, Any], ...]Ú_PER_TURN_RESET_STATEc               ó    € V ^8„  d   QhRRRR/# rÌ   r$   )r%   s   "r&   r'   r'   m  s   € ÷ -ñ - sð -¨tñ -r(   c                ó  € \          F  w  r\        WV4       K  	  / V n        \        4       V n        V P
                  P                  4        \        V P                  RR4      p\        V4      '       d   V! 4        \        P                  ! 4       V n        V P                  R8w  dH   \        \        4      ;_uu_ 4        V P                  4       '       d   V P!                  R4       RRR4       \        \        4      ;_uu_ 4        ^ RIHp V! V 4       RRR4       V P&                  '       d   V P)                  4        RV n        \        V RR4      '       d   V P+                  4        \-        V P.                  4      V n        \        V RR4      '       d   \        P                  ! 4       MRV n        R F%  p\        WR4      pVf   K  VP5                  4        K'  	  R#   + '       g   i     EL; i  + '       g   i     Læ; i)	zJReset retry counters, guardrails, iteration and run budgets at turn start.Úreset_consolidation_failuresNr:   u~   ðŸ”Œ Detected stale connections from a previous provider issue â€” cleaned up automatically. Proceeding with fresh connection.)Úretry_recovered_local_serverÚ_pending_startup_noticesÚrun_budget_seconds)Ú_stream_context_scrubberÚ_stream_think_scrubber)rk  r‡   Ú_turn_failed_file_mutationsÚsetÚ_turn_file_mutation_pathsÚ_tool_guardrailsÚreset_for_turnr,   Ú_memory_storerÂ   ÚtimeÚ_current_turn_timestampr9   r   rE   Ú_cleanup_dead_connectionsÚ_emit_diagnostic_statusÚagent.chat_completion_localro  Ú_compression_warningÚ_replay_compression_warningÚ_replay_startup_warningsr   Úmax_iterationsÚiteration_budgetÚ_run_budget_started_atÚreset)r    r!   ÚvalueÚ_reset_consolro  Úscrubbers   &     r&   Ú_reset_per_turn_agent_stater‰  m  s„  € ç,‰ˆÜ�˜UÖ#ñ -à(*€EÔ%Ü&)£e€EÔ#Ø	×Ñ×)Ñ)Ô+Ü˜E×/Ñ/Ð1OÐQUÓV€MÜ�×ÒÙŒô
 %)§I¢I£K€EÔ!ð ‡~�~Ð-Ô-Ü”i× Õ Ø×.Ñ.×0Ò0Ø×-Ñ-ð"ô÷ !ô 
”)×	Õ	ÝLÙ$ UÔ+÷ 
ð ×!×!Ð!Ø×)Ñ)Ô+Ø%)ˆÔ"ÜˆuÐ0°$×7Ò7Ø×&Ñ&Ô(ä,¨U×-AÑ-AÓB€EÔô ˜uÐ&:¸D×AÒAŒ�	Š	ŒÀtð 
Ô ó GˆÜ˜5¨Ó-ˆØÔØ�N‰NÖó G÷1 !× Ð ú÷ 
×	ús   Â=(GÄG1ÇG.	Ç1H	c               ó8   € 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Ñ   rS  rT  rU  rV  r`   Úpersist_user_display_kindÚpersist_user_display_metadataúOptional[dict[str, Any]]r#   ztuple[dict[str, Any], Any]r$   )r%   s   "r&   r'   r'   �  sR   € ÷ ')ñ ')Øð')Ø!ð')Ø9<ð')à+ð')àGTð')ð  -ð')ð $<ð	')ð
  ñ')r(   c                ó6  € \        V RR4      pVe   TMTp\        V\        4      '       d   VP                  R4      V8X  d   Tp	WR&   M#RRRV/p	\        V\        4      '       d   RV n        \        W“R7       V'       d   WYR&   V'       d   WiR&   Ve   WIR	&   W—3# )
zåBuild this turn's user dict, reusing CLI-staged input only when its clean text
matches this turn (a stale handoff must not replace later input; voice turns
compare the clean override). Returns ``(user_msg, pending_cli_message)``.r4  Nri   rr   rt   )Ú	timestampÚdisplay_kindr«   Úplatform_message_id)r,   rC   rK   rw   r4  r   )
r    rÑ   rS  rT  rV  r‹  rŒ  r/  Úexpected_persist_contentÚuser_msgs
   &&&&&&&   r&   Ú_stage_turn_user_messager”  �  s¸   € ô " %Ð)DÀdÓKÐà 4Ò @ÑÀlð ô 	Ð&¬×-Ò-Ø×#Ñ# IÓ.Ð2JÔJà&ˆð +�Òà˜F I¨|Ð<ˆÜÐ)¬4×0Ò0Ø.2ˆEÔ+ô ˜HÕG÷ !Ø#<�Ñ ß$Ø'DÐ#Ñ$ð  Ò+Ø*BÐ&Ñ'ØÐ(Ð(r(   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r    r   r·   úOptional[list[Any]]r#   r|   r$   )r%   s   "r&   r'   r'   Ç  s(   € ÷ ,\ñ ,\ ð ,\Ð<Oð ,\ÐTXñ ,\r(   c                ól  € V'       g   R# V P                   P                  4       '       g   V P                  V4       V P                  ^ 8X  dÀ   \	        W4       \        \        V RR4      RR4      p\        V4      '       d,    ^ RIHp V! W4      '       d   T! 4        ^ RI	H
p V! V R4       \        R	 V 4       4      pV^ 8”  dF   WPn        V P                  ^ 8”  d-   V P                   ^ 8X  d   WPP                  ,          V n        R# R# R# R# R#   \         d    \        P                  RRR7        L†i ; i)
zMHydrate process-local state from persisted history on the first resumed turn.Nr  Ú!note_native_compaction_checkpoint)Ú+has_replayable_native_compaction_checkpoint)Úset_usage_anchorz,restored native checkpoint hydration skippedTr7   c              3  óT   "  € T F  qP                  R 4      R8X  g   K  ^x € K   	  R# 5i)rr   rt   N)rw   )rW   Úms   & r&   rY   Ú(_hydrate_from_history.<locals>.<genexpr>ï  s"   é € ÐZÑ*> QÇ%Á%ÈÃ-ÐSYÑBYŸqšqÓ*>ùs   ‚(ž
()Ú_todo_storeÚ	has_itemsÚ_hydrate_todo_storeÚ_user_turn_countr   r,   rÂ   rB   r™  Úagent.usage_anchorrš  rE   rF   rG   ÚsumÚ_memory_nudge_intervalÚ_turns_since_memory)r    r·   Únote_checkpointr™  rš  Úprior_user_turnss   &&    r&   Ú_hydrate_from_historyr¨  Ç  s-  € çÙØ×Ñ×&Ñ&×(Ò(Ø×!Ñ!Ð"6Ô7ð ×Ñ Ô"ô 	˜UÔ9Ü!Ü�EÐ/°Ó6Ø/Øó
ˆô
 �O×$Ò$ðõñ ?Ø÷ò ñ $Ô%õ Dá$ U¨DÔ1ô ÑZÑ*>ÓZÓZÐØ˜aÔØ%5Ô"Ø×+Ñ+¨aÔ/°E×4MÑ4MÐQRÔ4RØ,<×?[Ñ?[Õ,[�Ö)ñ 5SÑ/ñ  ñ= #øô0 ô Ü—‘ØBÈTð ö ðús   Â*D Ä"D3Ä2D3c               ó    € V ^8„  d   QhRRRR/# rR   r$   )r%   s   "r&   r'   r'   ö  s   € ÷ 	ñ 	˜cð 	 dñ 	r(   c                óè   € V P                   ^ 8”  da   RV P                  9   dP   V P                  '       d>   V ;P                  ^,          un        V P                  V P                   8¼  d
   ^ V n        R# R# )zRAdvance the turn-based memory nudge counter; ``True`` when the review should fire.ÚmemoryTF)r¤  Úvalid_tool_namesry  r¥  r�   s   &r&   Ú_tick_memory_nudger­  ö  s]   € à×$Ñ$ qÔ(Ø˜E×2Ñ2Ô2Ø×#×#Ð#Ø×!Ò! QÕ&Õ!Ø×$Ñ$¨×(DÑ(DÔDØ()ˆEÔ%ÙÙr(   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r    r   r&  r�  r`   r#   r|   r$   )r%   s   "r&   r'   r'     s)   € ÷ $ñ $˜#ð $°cð $Èð $Ðbfñ $r(   c                ó  € \        V RR4      pVe   VR8X  d   R# \        \        4      ;_uu_ 4        ^ RIHp ^ RIHp V! V4      e    RRR4       R# V! V4      pV'       d	   V! V4       RRR4       R#   + '       g   i     R# ; i)zõCosmetic side-signal: detect an affection reaction so the host can play hearts.
Token-free, never touches the conversation, never fatal. Only words the user typed
count: a hidden prompt or an expanded skill body is app/skill text, not affection.Úreaction_callbackNÚhidden)Údetect_reaction)Údescribe_skill_invocation)r,   r   rE   Úagent.reactionsr²  Úagent.skill_commandsr³  )r    r&  r�  r°  r²  r³  Úkinds   &&&    r&   Ú_emit_reactionr·    ss   € ô   Ð':¸DÓAÐØÒ  L°HÔ$<ÙÜ	”)×	Õ	Ý3ÝBá$Ð%:Ó;ÒGØ÷ 
Ñ	ñ Ð4Ó5ˆßÙ˜dÔ#÷ 
×	×	Ò	ús   ²A6ÁA6Á$A6Á6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'     s!   € ÷ ñ ˜sð ¸ð Àñ r(   c                ó4   € \        W P                  RV4       R# )zÞCreate the DB row now (system prompt populated => non-NULL) and BEFORE preflight
compression: compaction/rotation INSERTs reference this row under PRAGMA
foreign_keys=ON. Idempotent; the user-turn crash persist runs later.z5Turn-start session row creation failed for session=%sN)r7  r®   )r    r/  s   &&r&   Ú_ensure_session_rowrº    s   € ô Ø×'Ñ'Ø?ÐATör(   c               ó4   € 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–  r#   r$   )r%   s   "r&   r'   r'     sF   € ÷ 6ñ 6Øð6Ø&)ð6Ø47ð6ØPSð6àð6à/Bð6ð 	ñ6r(   c               ó¨  € \        V RR4      '       d   R#  ^ RIHp T! RV P                  TTT\	        V4      \        V4      '       * V P                  \        V RR4      ;'       g    R\        V RR4      ;'       g    R\        V R	R4      ;'       g    RR
7      p ^ RIHpH	p	 V! 4       p
. pV FŸ  p\        V\        4      '       d+   VP                  R4      '       d   \        VR,          4      pM1\        V\        4      '       d   VP                  4       '       d   TpMKt  V	e    V	! WÐP                  RV
R7      pVP%                  V4       K¡  	  RP'                  V4      #   \         d    Rp	Rp
 LÉi ; i  \         d!   p\         P#                  RT4        Rp?LaRp?ii ; i  \         d"   p\         P#                  RT4        Rp?R# Rp?ii ; i)zØRun ``pre_llm_call`` plugins; their context is injected into the user message
(never the system prompt). Oversized per-hook context is spilled to disk so a
runaway plugin can't inflate every subsequent turn's prompt.Ú_persist_disabledFr*   )Úinvoke_hookÚpre_llm_callr¨   NÚ_parent_session_idÚ_user_id)
r§   rR  rã   rÑ   r·   Úis_first_turnr=   r¨   Úparent_session_idÚ	sender_id)Úget_spill_configÚspill_if_oversizedÚcontextzplugin hook)r§   ÚsourceÚconfigzhook context spill failed: %srb   zpre_llm_call hook failed: %s)r,   Úhermes_cli.lifecycler¾  r§   rž   rD   r=   Útools.hook_output_spillrÅ  rÆ  rE   rC   rK   rw   r"   rÁ   rF   r   rŸ   rc   )r    r)  rã   r&  r/   r·   Ú_invoke_hookÚ_pre_resultsÚ
_spill_cfgÚ_spill_if_oversizedÚ_spill_config_cachedÚ
_ctx_partsÚrÚ_pieceÚ
_spill_excÚexcs   &$$$$$          r&   Ú_collect_pre_llm_call_contextrÖ    s¯  € ô ˆuÐ)¨5×1Ò1Ùð,<ÝDÙ#ØØ×'Ñ'Ø%ØØ.Ü!% h£Ü#Ð$8Ó9Ô9Ø—+‘+Ü˜U J°Ó5×;Ð;¸Ü% eÐ-AÀ4ÓH×NÐNÈBÜ˜e Z°Ó6×<Ð<¸"ô
ˆð		(÷ñ $.£<Ð ð !#ˆ
ÛˆAÜ˜!œT×"Ò" q§u¡u¨Y×'7Ò'7Ü˜Q˜y�\Ó*‘Ü˜Aœs×#Ò#¨¯©¯	ª	Ø‘áØ"Ò.ðPÙ0Ø×+;Ñ+;ÀMØ3ô�Fð ×Ñ˜fÖ%ñ ð  �{‰{˜:Ó&Ð&øô) ô 	(Ø"&ÐØ#'Ò ð	(ûô" !ô PÜ—N‘NÐ#BÀJ×OÑOûðPûô ô <Ü�‰Ð5°s×;Ð;Ùûð<ús~   —A$F% Á<F% ÂF% ÂE" Â*3F% Ã>F% Ä	F% Ä'E7Ä<%F% Å"E4Å1F% Å3E4Å4F% Å7F"ÆFÆF% ÆF"Æ"F% Æ%GÆ0GÇGc          
     ó,   € V ^8„  d   QhRRRRRRRRR	R/# )
r   r    r   r/   r£   rä   r2   r_   r"   r#   r$   )r%   s   "r&   r'   r'   X  s4   € ÷ ñ ØðØ#ðØ<?ðØVYðàñr(   c           	     óÆ  € \        V 4       RP                  R \        V 4      \        V 4      \	        V 4      \        V 4      3 4       4      pV'       g   V# ^ Tu;8:  d   \        V4      8  d7   M M3\        W,          \        4      '       d   W,          P                  R4      MRp\        V\        4      '       d   \        WT4       V# V'       d   VR,           V,           # T# )uO  Must-deliver per-turn notes ride the user-message injection channel (one-shot) so the
ephemeral system prompt stays byte-stable: the gateway's staged notes, plugin go-live notices no
tool result carried, then the surface-switch correction. Multimodal (list) content can't take the
string sidecar â€” append a durable text part instead.rb   c              3  ó:   "  € T F  pV'       g   K  Vx € K  	  R # 5irV   r$   )rW   re   s   & r&   rY   Ú'_merge_gateway_notes.<locals>.<genexpr>`  s    é € ð ñ<�ä?C÷ 	Šó <ùs   ‚
‘
ri   N)Úforward_lsp_verdictsrc   r�   r   r”   r˜   rØ   rC   rK   rw   rž   r    )r    r/   rä   r_   Ú_turn_notesÚ_gw_turn_contents   &&&&  r&   Ú_merge_gateway_notesrÞ  X  sÒ   € ô ˜ÔØ—+‘+ñ Ü<¸UÓCÔEWÐX]ÓE^Ü5°eÓ<Ü3°EÓ:ñ<óó €K÷
 Ø"Ð"ð Ð%Ö5¬¨H«×5Ü�xÕ6¼×=Ò=ð 	Õ'×+Ñ+¨IÔ6ð ð	 ô Ð"¤D×)Ò)Ü*Ð+;ÔIØ"Ð"ç6IÐ˜fÕ$ {Õ2ðØOZðr(   c               ó    € V ^8„  d   QhRRRR/# rÌ   r$   )r%   s   "r&   r'   r'   u  s   € ÷ 3ñ 3 ð 3¨Tñ 3r(   c           	     ó<  € \         P                  ! 4       P                  V n        V! 4       P	                  RV P                  4       V P
                  '       d0   V! 4       P	                  RV P                  \        V RR4      R7       MRV n        RV n        RV n	        R# )z£Record the execution thread so interrupt()/clear_interrupt() scope the tool-level
signal to THIS agent's thread; clear stale state, preserving a pending interrupt.FTÚ_tool_interrupt_reasonN)Úreason)
Ú	threadingÚcurrent_threadrÃ   Ú_execution_thread_idÚ_set_interruptÚ_interrupt_requestedr,   Ú_interrupt_messagerá  Ú _interrupt_thread_signal_pending)r    Úras   &&r&   Ú_bind_interrupt_scoperë  u  s†   € ô "+×!9Ò!9Ó!;×!AÑ!A€EÔÙƒD×Ñ˜˜u×9Ñ9Ô:Ø×!×!Ð!Ù
‹×ÑØ�%×,Ñ,´W¸UÐD\Ð^bÓ5cð 	õ 	
ð $(ˆÔ Ø'+ˆÔ$Ø-2€EÖ*r(   c               ó    € V ^8„  d   QhRRRR/# )r   r&  r   r#   r"   r$   )r%   s   "r&   r'   r'   „  s   € ÷ ñ ¨cð °cñ r(   c                óT   € \        V \        \        34      '       d   \        V 4      # R# )a  Semantic text of the turn for memory queries: a multimodal (list) turn carries its text
in parts, so keying off ``isinstance(str)`` collapsed it to ``""`` and ``is_trivial_prompt``
skipped prefetch entirely. An image-only turn still flattens to ``""`` (trivial).r*   )rC   r"   rž   r   )r&  s   &r&   Ú_memory_query_textrî  „  s%   € ô Ð'¬#¬t¨×5Ò5Ü#Ð$9Ó:Ð:Ùr(   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r    r   r&  Úturn_authorr�  r#   r"   r$   )r%   s   "r&   r'   r'   �  s,   € ÷ ñ ØðØ'*ðØ9Qðàñr(   c                ó”  € V P                   '       g   R# \        V4      p\        V\        4      '       d   TM/ p\	        \
        4      ;_uu_ 4        V P                   P                  V P                  TVP                  R4      ;'       g    RVP                  R4      ;'       g    R\        VP                  R4      4      R7       RRR4       Rp\	        \
        4      ;_uu_ 4        \        V4      '       g1   V P                   P                  W0P                  R7      ;'       g    RpRRR4       V'       dV   \	        \
        4      ;_uu_ 4        V P                   P                  4       pV'       d   V P                  V4       RRR4       V# V#   + '       g   i     LÒ; i  + '       g   i     L‚; i  + '       g   i     T# ; i)zÔNotify memory providers of the new turn, then prefetch external memory once
before the tool loop (skipped on trivial prompts with no semantic signal).
Returns the prefetch text (``""`` when nothing was injected).r*   ÚidNr!   Úis_bot)Ú	author_idÚauthor_nameÚauthor_is_bot)r§   )Ú_memory_managerrî  rC   rK   r   rE   Úon_turn_startr¡  rw   rD   r   Úprefetch_allr§   Údescribe_recallÚ_emit_status)r    r&  rð  Ú_queryÚ_authorr^   Ú_recall_indicators   &&&    r&   Ú_memory_turn_start_and_prefetchrÿ  �  sW  € ð × × Ð ÙÜÐ 5Ó6€Fä'¨´T×:Ò:‰kÀ€GÜ	”)×	Õ	Ø×Ñ×+Ñ+Ø×"Ñ" FØ—k‘k $Ó'×/Ð/¨4¸W¿[¹[ÈÓ=P×=XÐ=XÐTXÜ˜wŸ{™{¨8Ó4Ó5ð 	,ô 	
÷ 
ð ÐÜ	”)×	Õ	Ü  ×(Ò(Ø!&×!6Ñ!6×!CÑ!CÀF×WgÑWgÐ!CÓ!h×!nÐ!nÐlnÐ÷ 
÷
 Ü”i× Õ Ø %× 5Ñ 5× EÑ EÓ GÐß Ø×"Ñ"Ð#4Ô5÷ !ð ÐÐÐ÷# 
×	ú÷ 
×	ú÷ !Ö ð Ðús1   ÁAFÂ%!FÃ'>F#Ä&F#Å4F6ÆF 	Æ#F3	Æ6G	c               ó4   € 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ä   r2   r^   r"   r_   Úpreflight_compressedrD   r#   r|   r$   )r%   s   "r&   r'   r'   ¬  sO   € ÷ ,tñ ,tØð,tØ#ð,tØ<?ð,tØUXð,tàð,tà7;ð,tð 
ñ,tr(   c          
     óö  € W,          pVP                  R4      p^ RIHpHp	 T	! YT\	        T;'       g    RW44      4      w  r«Ve   Wº8X  d   R# W¶R&   V! V 4      ;_uu_ 4        VP                  R4      pT;'       d    \        \        V RR4      4      p\        V R	R4      pVe   \        V\        4      '       g   V'       g    RRR4       R#  \        V\        4      '       d   VP                  V P                  WÊV4       MVP                  V P                  W«4        RRR4       R#   \         d0    \        P                  R
T P                  ;'       g    RRR7        LCi ; i  + '       g   i     R# ; i)u–   api_content sidecar â€” persist what you send: injected context lives only in the
API copy, so stamp the exact sent bytes on the live dict for replay.ri   )Ú_persist_lockÚdurable_user_row_contentr*   Nrq   Ú_row_idÚ_last_compaction_in_placeFr¦   z*api_content backfill failed for session=%sr2  Tr7   )rw   Úagent.session_persistencer  r  rl   rD   r,   rC   r2   Úset_message_api_contentr§   Úset_latest_user_api_contentrE   rF   r   )r    r/   rä   r^   r_   r  Ú_turn_user_msgÚlive_contentr  r  Údurable_contentÚ_api_contentr  Ú_in_place_compactedÚ_dbs   &&&&&$         r&   Ú_stamp_api_content_sidecarr  ¬  sR  € ð Õ4€NØ!×%Ñ% iÓ0€LßQá$<Ø˜|Ü  ×!3Ð!3°Ð5GÓ]ó%Ñ!€Oð Ò˜|Ô>ÙØ$0�=Ñ!ñ 
�u×	Õ	Ø ×$Ñ$ YÓ/ˆØ2×oÐo´t¼GÀEÐKfÐhmÓ<nÓ7oÐÜ�e˜]¨DÓ1ˆØŠ;œz¨'´3×7Ò7×;NØ÷ 
Ñ	ð	tÜ˜'¤3×'Ò'Ø×+Ñ+¨E×,<Ñ,<¸gÐXdÕeð ×/Ñ/°×0@Ñ0@À/Õ`÷ 
Ñ	øô ô 	tÜ�N‰NÐGÈ×IYÑIY×IcÐIcÐ]cÐnrˆNÖsð	tú÷ 
×	Ð	úsI   Á"A E'ÃE'Ã3D*ÄE'ÄD*ÄE'Ä*-E$Å	E$Å!E'Å#E$Å$E'Å'E8	c               ó0   € V ^8„  d   QhRRRRRRRRRR	R
R/# )r   r    r   Úturn_user_msgro   r^   r"   r_   r  rD   r#   r|   r$   )r%   s   "r&   r'   r'   Û  sF   € ÷ {ñ {Øð{Ø-ð{ØCFð{Ø]`ð{à!ð{ð 
ñ{r(   c          	     ó†  € \        W#4      p\        VP                  R4      V4      '       g   R# ^ RIHpHp V! V 4      ;_uu_ 4        VP                  R4      p\        V RR4      p	V	e   \        V\        4      '       g    RRR4       R# T;'       d    \        \        V RR4      4      p
V
'       d
   VR,          MV! VR,          4      p V	P                  V P                  W‹4       RRR4       R#   \         d0    \        P                  RT P                  ;'       g    R	R
R7        LCi ; i  + '       g   i     R# ; i)aú  Multimodal (list) content takes no string sidecar: the turn's context becomes a durable
text part on the current turn's live list (the gateway must-deliver-note channel, #71998),
so wire, persisted row, compaction and replay all carry the same parts. Runs once per turn,
before the first request; historical rows are never touched.

A user row another writer materialized BEFORE the prologue (in-place preflight compaction,
a close/early flush that raced it) is updated in place: the crash persist marker-skips that
message, so without this a resumed session replays a view the model never saw. Same
``_row_id``-under-lock protocol as the string sidecar backfill; the row keeps its writer's
shape (compaction inserted the raw parts, a flush the text projection).ri   N)Ú_durable_contentr  r  r¦   r  Fz1multimodal context backfill failed for session=%sr2  Tr7   )rg   r    rw   r  r  r  r,   rC   r2   rD   Úset_user_message_contentr§   rE   rF   r   )r    r  r^   r_   r  Ú_mm_ctxr  r  r  r  r  ri   s   &&&&$       r&   Ú_append_multimodal_contextr  Û  s  € ô .Ð.@ÓV€GÜ-¨m×.?Ñ.?À	Ó.JÈG×TÒTÙßIá	�u×	Õ	Ø×#Ñ# IÓ.ˆÜ�e˜]¨DÓ1ˆØŠ;œj¨´#×6Ò6Ø÷	 
Ñ	ð
 3×oÐo´t¼GÀEÐKfÐhmÓ<nÓ7oÐß.A�- 	Ö*ÑGWÐXeÐfoÕXpÓGqˆð	{Ø×(Ñ(¨×)9Ñ)9¸7ÔL÷ 
Ñ	øô ô 	{Ü�N‰NÐNÐPU×P`ÑP`×PjÐPjÐdjÐuyˆNÖzð	{ú÷ 
×	Ð	úsB   Á:D/ÂD/ÂD/Â3D/ÃC2Ã2-D,Ä 	D,Ä)D/Ä+D,Ä,D/Ä/E 	c          
     ó,   € 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%   s   "r&   r'   r'   û  s6   € ÷ ñ ØðØ#ðØ;Nðàðð 
ñr(   c                ó>   a aa€ R V VV3R llp\        S VRV4       R# )zâCrash-resilience: persist the inbound user turn once, with final api_content,
before the first LLM call. Same critical section as CLI close persistence; retries
the row create if the pre-compression attempt failed transiently.c               ó   € V ^8„  d   QhRR/# )r   r#   r|   r$   )r%   s   "r&   r'   Ú)_persist_turn_start.<locals>.__annotate__  s   € ÷ ?ñ ? ñ ?r(   c                 óL   <€ S P                  4        S P                  SS4       R # rV   )r®   Ú_persist_session)r    r·   r/   s   €€€r&   Ú_ensure_and_persistÚ0_persist_turn_start.<locals>._ensure_and_persist  s    ø€ Ø× Ñ Ô"Ø×Ñ˜xÐ)=Ö>r(   z:Early turn-start session persistence failed for session=%sN)r7  )r    r/   r·   r/  r  s   fff& r&   Ú_persist_turn_startr   û  s#   ú€ ÷?ò ?ô ØÐ"ØDÐFYör(   r‹  rŒ  rð  Ú
moa_activer¤   c               óL   € 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R/# )r   rÑ   r   Úsystem_messager`   r·   r'  rR  rS  zOptional[Any]rT  rU  rV  r‹  rŒ  r�  rð  r!  rD   r¤   r#   r%  r$   )r%   s   "r&   r'   r'     s›   € ÷ yñ yØðyØ.;ðyà8ðyàCPðyð (ðyð BQðyð ,ð	yð Q^ð	yð
 $<ðyð
 Phðyð /3ðyð &ðyð ñyr(   c	               óê	  a*€ ^ RI Hp V! 4        \        V4      pW°n        \	        V 4      pVe   TpT! V P
                  4       V! \        V RR4      4       ^ RIHp V! \        V RR4      4       V P                  4        \        V 4       \        V 4       \        V\        4      '       d	   V! V4      p\        V\        4      '       d	   V! V4      p\        WWVWx4      w  pp\        V 4       V! V4      pVR,          \!        V4      ^P8”  d   R	MR
,           pRpV P
                  ;'       g    RV P"                  V P$                  ;'       g    RV P&                  ;'       g    R\!        T;'       g    . 4      VP)                  RR4      .p\        V RR4      ;p'       d   VR,          pVP+                  V4       \,        P.                  ! V.VO5!   V'       d   \1        V4      M. p\3        WWgW‰V
4      w  p p!\5        W4       \7        V 4       \9        VV 4       \!        V4      ^,
          p"V"V n        V ;P<                  ^,          un        RV n        Ve   TMTp#\A        V 4      p$\C        V V#V	4       V PD                  '       g1   T PG                  RVR,           \!        V4      ^<8”  d   R	MR
 R24       V PH                  f
   V! WV4       T PH                  p% ^ RI%H&p& V&! V 4       \S        V V!4       ^ RI*H+o* V'       dR   \X        ;QJ d    V*3R lV 4       F  '       g   K   RM	  RM! V*3R lV 4       4      '       d   V P[                  W34       V! V VVV%VV"VVR7      p'V'P\                  pV'P^                  p%V'P`                  pV'Pb                  p"\e        V VVV#VVR7      p(\g        V V%V4       \i        V VV"V(4      p(\k        V V4       \m        V V#V4      p)\o        V VV4       ^ T"u;8:  d   \!        V4      8  d¤   M M VV",          Pq                  R4      R8X  d„   \        VV",          Pq                  R4      \0        4      '       d#   \s        V VV",          V)V(V'Pt                  R 7       M5V'       g.   \        V R!R4      R"8w  d   \w        V VV"V)V(V'Pt                  R 7       \y        V VVV!4       \{        VV#VVV%VVV"V$V(V)V'P|                  R#7      #   \N         d    \,        PQ                  RRR7        ELi ; i)$aD  Run the once-per-turn setup and return the loop's input context.

Helpers are passed in to avoid an import cycle with ``agent.conversation_loop``.
Order matters: the DB session row is created only AFTER the system prompt is built
(else it persists system_prompt=NULL and costs a cache miss) and BEFORE preflight
compression.)Úrun_turn_start_compactionNÚ_memory_write_originÚassistant_tool)Úset_review_attendedÚ_review_attendedF:NéP   Nz...r*   zPconversation turn: session=%s model=%s provider=%s platform=%s history=%d msg=%rr2  ÚunknownÚ
Ú Ú_turn_originz
 origin=%sTu   ðŸ’¬ Starting conversation: ':Né<   NÚ')Úensure_message_agent_toolzmessage_agent injection skippedr7   )Ú"_PERSIST_AFTER_ADMISSION_INTERRUPTc              3  óv   <"  € T F.  p\        V\        4      ;'       d    VP                  S4      x € K0  	  R # 5irV   )rC   rK   rw   )rW   r{   r2  s   & €r&   rY   Ú%build_turn_context.<locals>.<genexpr>Š  s4   øé € ð $á'ˆCô 	�3œÓ×MÐM #§'¡'Ð*LÓ"MÔMÛ'ùs   ƒ9Ÿ9)r/   r#  r(  r·   rä   rÑ   r)  )r)  rã   r&  r/   r·   rr   rt   ri   )r  r9   Úcodex_app_server)rÑ   r&  r/   r·   r(  r)  rã   rä   r*  r_   r^   r+  )?Úagent.turn_context_compactionr%  r   Ú_turn_authorr   r§   r,   Útools.skill_provenancer(  Ú_restore_primary_runtimerB  rP  rC   r"   rj  r‰  rØ   r=   r[   r¨   ÚreplacerŸ   rF   Úinforž   r”  r¨  r   r   r_  r¡  Ú_is_user_initiated_turnr­  r·  rI  Ú_safe_printÚ_cached_system_promptÚtools.bot_mode_dmr1  rE   rG   rº  r  r2  ÚanyÚ_flush_messages_to_session_dbr/   r(  r·   rä   rÖ  r   rÞ  rë  rÿ  rÊ   rw   r  Ú
compressedr  r   r%  Úblocked)+r    rÑ   r#  r·   rR  ri  rS  rT  rV  r‹  rŒ  rð  Úrestore_or_build_system_promptÚinstall_safe_stdioÚsanitize_surrogatesÚsummarize_user_message_for_logÚset_session_contextÚset_current_write_originrê  r!  r¤   r%  Úrecovered_historyr(  r)  rã   Ú_preview_textÚ_msg_previewÚ	_turn_fmtÚ
_turn_argsr.  r/   r“  r/  rä   r&  r*  r(  r1  Ú
compactionr_   r^   r2  s+   &&&&&&&&&$$$$$$$$$$$$                     @r&   Úbuild_turn_contextrP    sí  ø€ õ" Hñ Ôô $ KÓ0€KØ$Ôô <¸EÓBÐØÒ$Ø0Ðñ ˜×(Ñ(Ô)ÙœW UÐ,BÐDTÓUÔVÝ:Ùœ Ð'9¸5ÓAÔBØ	×"Ñ"Ô$Ü˜%Ô Ü$ UÔ+ä�,¤×$Ò$Ù*¨<Ó8ˆÜÐ&¬×,Ò,Ù2Ð3GÓHÐä!4Ø˜Øó"ÑÐ�wô   Ô&á2°<Ó@€MØ  Õ%´#°mÓ2DÀrÔ2I©ÈrÕR€LàZð ð 	×Ñ×"Ð"˜F E§K¡K°·±×1LÐ1LÀ9Ø�‰×#Ð#˜)¤SÐ)=×)CÐ)CÀÓ%DØ×Ñ˜T 3Ó'ð€Jô    ~°tÓ<Ð<ˆÖ<Ø�\Õ!ˆ	Ø×Ñ˜,Ô'Ü
‡K‚K�	Ð'˜JÔ'÷ .BŒtÐ(Ô)Àr€HÜ$<ØÐ1Ø Ð=Zó%Ñ!€HÐ!ô ˜%Ô6ä˜%Ô ä�8˜XÔ&Ü ›M¨AÕ-ÐØ&;€EÔ#à	×Ò˜aÕÕð %)€EÔ!ð 5IÒ4TÑ0ÐZfÐÜ-¨eÓ4ÐÜ�5Ð/Ð1JÔKà××ÐØ×ÑØ+¨M¸#Õ,>Ð+?Ü˜MÓ*¨RÔ/‰u°RÐ8¸ð;ô	
ð ×"Ñ"Ò*Ù& uÐ>RÔSØ ×6Ñ6ÐðGÝ?á! %Ô(ô ˜Ð2Ô3õ
 Mç§£ô $á'ó$§§¢ô $á'ó$÷ !ò !ð 	×+Ñ+Ð,@ÔWá*Ø˜°Ø1ÐH\Ø3À,Ø+ô	€Jð ×"Ñ"€HØ%×:Ñ:ÐØ%×:Ñ:ÐØ&×<Ñ<Ðä7ØÐ!2¸GØ3¸hØ1ôÐô ˜EÐ#7¸ÔBÜ.ØˆxÐ.Ð0CóÐô ˜% Ô$Ü8¸Ð@UÐWbÓcÐô
 ' u¨hÐ8JÔKð 	Ð!Ö1¤C¨£M×1°hÐ?TÕ6U×6YÑ6YÐZ`Ó6aÐekÔ6kÜ�hÐ4Õ5×9Ñ9¸)ÓDÄd×KÒKÜ&Ø�xÐ 5Õ6Ð8JÐL_Ø%/×%:Ñ%:ö÷ ¤¨¨z¸4Ó @ÐDVÔ VÜ&Ø�xÐ!6Ð8JØ#¸*×:OÑ:Oõô
 ˜˜xÐ)=Ð?RÔSäØ!Ð9NÐYaØ1ÐH\Ø+°WØ3ÐJ^Ø/ÐDVØ&0×&8Ñ&8ôð øô} ô GÜ�‰Ð6Àˆ×FðGús   ËS Ó"S2Ó1S2c               ó$   € V ^8„  d   QhRRRRRR/# )r   r    r   Ú
moa_configr#   r$   )r%   s   "r&   r'   r'   È  s!   € ÷ ñ ˜sð °ð ¸ñ r(   c                óf  € V P                   pV P                  R8X  d”   V'       d<   VP                  R4      ;'       g    / pVP                  R4      '       d
   VR,          pW P                   8X  dA   \        \        V RR4      RR4      pV'       d!   VP                  R4      '       d
   VR,          pV# )z¸Model name for strict-API tool-call sanitization. In MoA mode ``agent.model`` is
the virtual preset name; use the resolved aggregator so Gemini keeps
thought_signature (extra_content).ÚmoaÚ
aggregatorr=   ÚclientNÚlast_aggregator_slot)r=   r[   rw   r,   )r    rR  Ú_sanitize_modelÚ_aggÚ	_agg_slots   &&   r&   Ú_sanitize_model_forr[  È  s�   € ð —k‘k€OØ‡~�~˜ÔßØ—>‘> ,Ó/×5Ð5°2ˆDØ�x‰x˜× Ò Ø"& w¥-�ØŸk™kÔ)ô  ¤¨¨x¸Ó >Ð@VÐX\Ó]ˆIß˜YŸ]™]¨7×3Ò3Ø"+¨GÕ"4�ØÐr(   c               ó8   € 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/   r0   rä   r^   r_   rR  r(  r#   z tuple[list[dict[str, Any]], str]r$   )r%   s   "r&   r'   r'   Û  sX   € ÷ q*ñ q*Øðq*Ø.ðq*ØJMðq*àðq*à25ðq*àCFðq*à^aðq*ð &ñq*r(   c               ój  € ^ RI Hp ^ RIHp ^ RIHp	 \        V\        4      ;'       d     ^ Tu;8*  ;'       d    \        V4      8  Mu p
V
'       d	   W,          MRpV P                  pV
'       d   TM^ pV	! VRV VR7      WR ,           p. p\        V4       EF�  w  ppV! V4      pVP                  RR4      p\         F  pVP                  VR4       K  	  VVJ da   VP                  R4      R8X  dK   \        V\        4      '       d   V'       d   VVR	&   M_\        VP                  R	R
4      W44      pVe   VVR	&   M9\        V\        4      '       d$   V'       d   VP                  R4      R9   d   VVR	&   V P!                  VV4       VP                  RR4       V! VV\        V4      ^,
          8H  R7       VP                  RR4       VP                  RR4       V P#                  4       '       d3   V P%                  V\'        W4      V P(                  V P*                  R7       VP-                  V4       EK   	  ^ RIHp V! W4       ^ RIHp V! W4       T;'       g    R
pV P6                  '       d)   VR,           V P6                  ,           P9                  4       pV'       d   RRR	V/.V,           pVV3# )uw  Build the wire copy of ``messages`` for one API call plus the effective system
message. Returns ``(api_messages, effective_system)``.

Prompt-cache invariant: historical user/assistant rows replay their ``api_content``
sidecar (the exact bytes sent live) so the prefix stays byte-stable; the current
user turn reuses the prologue's stamp (or composes live when a caller bypassed the
prologue). Ephemeral context (prefetch, ``pre_llm_call`` hooks,
``ephemeral_system_prompt``) is added at API time only â€” ``messages`` stays untouched
beyond the sidecar stamp, and the system prompt is built ONCE per session and
replayed verbatim.)Ú!fill_empty_non_final_wire_payload)Ú_clone_message_for_send)Úcanonicalize_replay_historyN)Únowrq   rr   rt   ri   r*   Úfinish_reason)Úis_finalÚ_length_continuation_fragmentÚ_length_continuation_nudge)r=   r<   r[   )Úreplay_for_agentr>   rb   Úsystemrs   )rh  r^  Úagent.conversation_loopr_  Úagent.replay_cleanupr`  rC   r2   rØ   r{  Ú	enumeraterv   r   rw   r"   rl   Ú_copy_reasoning_content_for_apiÚ_should_sanitize_tool_callsÚ#_sanitize_tool_calls_for_strict_apir[  r<   r[   rŸ   Úagent.image_replayrf  rJ   r?   Úephemeral_system_promptrÁ   )r    r/   rä   r^   r_   rR  r(  r^  r_  r`  Úhas_currentÚcurrent_turn_messageÚturn_nowÚsplitÚcanonical_messagesÚapi_messagesrå   r{   rn   r  ÚkeyÚ	_composedrf  r?   Úeffective_systems   &&$$$$$                  r&   Úbuild_api_messagesry  Û  sƒ  € õ NÝ?Ý@äÐ2´CÓ8×gÐg¸QÐBW×=gÔ=gÔZ]Ð^fÓZgÔ=g€Kß>I˜8Ö:ÈtÐð ×,Ñ,€Hß%0Ñ!°a€EÙ4°X¸f¸uÐ5EÈ8ÔTÐW_Ð`fÐWgÕgÐà€LÜÐ0×1‰ˆˆSñ *¨#Ó.ˆð —{‘{ =°$Ó7ˆß2ˆCØ�K‰K˜˜TÖ"ñ 3ð
 Ð&Ó&¨3¯7©7°6«?¸fÔ+DÜ˜,¬×,Ò,·ð &2�˜	Ò"ô 5Ø—K‘K 	¨2Ó.Ð0Bó�	ð Ò(Ø)2�G˜IÑ&øä�|¤S×)Ò)¯lØ—‘˜“Ð#8Ô8ð
 ".ˆG�IÑð 	×-Ñ-¨c°7Ô;à�‰�O TÔ*ñ 	*¨'¸SÄCÐHZÓD[Ð^_ÕD_Ñ=_Õað 	�‰Ð3°TÔ:Ø�‰Ð0°$Ô7ð ×,Ñ,×.Ò.Ø×5Ñ5ØÔ2°5ÓEØŸ™°%·.±.ð 6ô ð 	×Ñ˜G×$ñu 2õ| 4Ù�UÔ)õ TÙ'¨Ô<ð
 ,×1Ð1¨rÐØ×$×$Ð$Ø,¨vÕ5¸×8UÑ8UÕU×\Ñ\Ó^ÐßØ ¨9Ð6FÐGÐHÈ<ÕWˆØÐ)Ð)Ð)r(   rV   ))Ú_invalid_tool_retriesé    )Ú_invalid_json_retriesr{  )Ú_empty_content_retriesr{  )Ú_incomplete_scratchpad_retriesr{  )Ú_codex_incomplete_retriesr{  )Ú_codex_reasoning_only_streakr{  )Ú_thinking_prefill_retriesr{  )Ú_post_tool_empty_retriedF)Ú_last_content_with_toolsN)Ú$_last_content_tools_all_housekeepingF)Ú_reused_response_textN)Ú_mute_post_responseF)Ú_unicode_sanitization_passesr{  )Ú_tool_guardrail_halt_decisionN)Ú_harness_metrics_turnN)Ú"_iteration_budget_warning_injectedF)Ú_run_budget_wrapup_injectedF)Ú_verification_stop_nudgesr{  )Ú_pre_verify_nudgesr{  )Ú_lsp_turn_end_notesr{  )NN)kÚ__conditional_annotations__rö   Ú
__future__r   ÚloggingrK  rã  rz  rc  Ú
contextlibr   Údataclassesr   Útypingr   r   r   r   r	   r
   rÿ   r   Úagent.iteration_budgetr   Úagent.memory_contextr   Úagent.memory_providerr   r¾   r   Úagent.lsp.deferredr   rÛ  Úagent.plugin_live_notesr   Úagent.skills_index_deltar   Úagent.message_metadatar   r   r   Úagent.model_metadatar   r   r   Úagent.image_token_costr   r¢  r   r   Úagent.turn_authorr   Ú	getLoggerrò   rF   r-   rP   rH   rg   rl   ry   r~   r‚   r‰   r�   r”   r˜   r    Ú	frozensetr½   rÊ   rÏ   rÜ   rç   rï   ÚRuntimeErrorrñ   r  r  r  r  r  r#  r%  r7  rB  rP  rj  rk  r,  r‰  r”  r¨  r­  r·  rº  rÖ  rÞ  rë  rî  rÿ  r  r  r   rP  r[  ry  )r�  s   @r&   Ú<module>r¢     s÷  øðôAõ #ã Û 
Û Û Û Ý Ý !ß <× <å NÝ 2Ý ;Ý 3Ý 6Ý GÝ 6Ý <ß kÑ k÷ñ õ
 9ß LÝ /à	×	Ò	˜8Ó	$€õ*õ
0õf
õ;õGõõ!õ-õ1õ@õ
9õ7õ
ñ    Ó)Ð ÷GMõT!õ$õN#õLWô$H <ô Hõõ0õ4õbõ
ð$àLM÷$ð: ÷0ð 0ó ð0õ"3õ&
õ0Nõ*&ð<6Ð Ð2ó õ$-õ`')õT,\õ^	÷$õ&õ6õrõ:3õ÷õ>,tõ^{õ@ñ"yð _cð	yð
 =Aðyð
 imðyð 49ðyð '+÷yð yõx÷&q*r(   