§
    ÷žyj ã                  ó˜  — d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZmZmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ  ej         e!¦  «        Z"dMd„Z#dNd„Z$dOd„Z%dPd„Z&dQd„Z'dRd „Z( e)d!d"h¦  «        Z*dSd%„Z+dTd(„Z,dUd-„Z-e-Z.dVd/„Z/dWd3„Z0dXd;„Z1e G d<„ d=¦  «        ¦   «         Z2	 dYddd>d?œdZdL„Z3dS )[u?  Per-turn setup for ``run_conversation`` (the turn prologue).

``run_conversation`` opened with ~470 lines of straight-line setup before the
tool-calling loop ever started: stdio guarding, runtime-main wiring, retry-counter
resets, user-message sanitization, todo/nudge-counter hydration, system-prompt
restore-or-build, session-row creation (before compression, whose DB writes
reference the row), preflight context compression, the ``pre_llm_call`` plugin
hook, external-memory prefetch, and crash-resilience persistence (last, so the
user row is written once with its final ``api_content`` sidecar).

All of that is *prologue* â€” it runs once per turn, has no back-references into the
loop, and produces a fixed set of values the loop then consumes. ``TurnContext``
captures those produced values; ``build_turn_context`` performs the setup work and
returns one. ``run_conversation`` is left to unpack the context and run the loop,
shrinking the orchestrator by the full prologue.

The builder still mutates ``agent`` heavily (counters, thread id, cached prompt,
session DB) exactly as the inline code did â€” those side effects are the point. The
``TurnContext`` it returns carries only the *locals* the loop reads back.

Behavior is identical to the original inline prologue; this is a pure
move-and-name refactor with no semantic change.
é    )ÚannotationsN)Ú	dataclass)ÚAnyÚDictÚListÚMappingÚOptional)ÚIDLE_COMPACTION_STATUS_TEMPLATEÚ%PREFLIGHT_COMPRESSION_STATUS_TEMPLATEÚcompression_skipped_due_to_lockÚ&conversation_history_after_compressionÚ#recover_rotated_compression_session)Ú#automatic_compaction_status_message)ÚIterationBudget)Úbuild_memory_context_block)Úis_trivial_prompt)Úestimate_messages_tokens_roughÚestimate_request_tokens_roughÚcontentr   Úext_prefetch_cacheÚstrÚplugin_user_contextÚreturnúOptional[str]c                óð   — t          | t          ¦  «        sdS g }|r&t          |¦  «        }|r|                     |¦  «         |r|                     |¦  «         |sdS | dz   d                     |¦  «        z   S )ue  Compose the API-bound content of the current turn's user message.

    Sources: memory-manager prefetch + ``pre_llm_call`` plugin context with
    target="user_message" (the default). Both are appended to the *API copy*
    of the user message only â€” the stored content stays clean.

    This is the single source of that composition. The prologue stamps the
    result onto the live message as ``api_content`` (persisted alongside the
    clean content) and the ``api_messages`` build in ``conversation_loop``
    sends the same helper's output, so the persisted sidecar can never drift
    from the bytes on the wire â€” which is the whole prompt-cache invariant:
    what turn N sends must be what turn N+1 replays.

    Returns ``None`` when nothing is injected (multimodal/non-string content,
    or no ephemeral context), meaning the message is sent as-is.
    Nú

)Ú
isinstancer   r   ÚappendÚjoin)r   r   r   Ú
injectionsÚfenceds        ú8/home/ragecks/.hermes/hermes-agent/agent/turn_context.pyÚcompose_user_api_contentr#   5   s˜   € õ* �g�sÑ#Ô#ð ØˆtØ€JØð &Ý+Ð,>Ñ?Ô?ˆØð 	&Ø×Ò˜fÑ%Ô%Ð%Øð /Ø×ÒÐ-Ñ.Ô.Ð.Øð ØˆtØ�VÑ˜fŸkšk¨*Ñ5Ô5Ñ5Ð5ó    Úapi_msgúDict[str, Any]c                ó˜   — |                       dd¦  «        }t          |t          ¦  «        r|r|                      d¦  «        dv r|| d<   |S )a}  Pop the ``api_content`` sidecar and substitute it into ``content``.

    Used at every API-bound message-build site (the ``api_messages`` build in
    ``conversation_loop``, the max-iterations summary in
    ``chat_completion_helpers``, the chat-completions transport). The sidecar
    carries the exact bytes previously sent to the API for this message when
    they differ from the clean stored content; substituting it here keeps the
    provider prompt-cache prefix byte-stable across turns.

    Returns the popped sidecar string (for callers that need the value for
    current-turn composition logic) or ``None`` when absent.
    Úapi_contentNÚrole)ÚuserÚ	assistantr   )Úpopr   r   Úget)r%   Úsidecars     r"   Úsubstitute_api_contentr/   X   sZ   € ð �kŠk˜-¨Ñ.Ô.€Gå�7�CÑ Ô ð%àð%ð �KŠK˜ÑÔÐ#8Ð8Ð8à$ˆ�	ÑØ€Nr$   ÚmsgÚNonec                ó2   — |                       dd¦  «         dS )u¢  Drop the ``api_content`` sidecar from a message whose content was rewritten.

    Called from every content-rewrite path (historical image strip,
    merge-summary-into-tail, consecutive-user repair merge, stale-confirmation
    redaction). Replaying the pre-rewrite sidecar would resend exactly what
    the rewrite removed, so it must be dropped â€” the cost is one cache
    boundary miss, never wrong content.
    r(   N)r,   )r0   s    r"   Údrop_stale_api_contentr3   o   s   € ð ‡G‚GˆM˜4Ñ Ô Ð Ð Ð r$   úMapping[str, Any]c                ó^   — |                       d¦  «        }t          |t          ¦  «        r|ndS )zéExtract the ``api_content`` sidecar from a message dict for persistence.

    Shared by the gateway/branch forwarding sites that copy the sidecar into a
    new row. Returns the string sidecar or ``None`` when absent/non-string.
    r(   N)r-   r   r   )r0   Úvs     r"   Úextract_api_content_sidecarr7   {   s.   € ð 	�Š�ÑÔ€AÝ˜1�cÑ"Ô"Ð,ˆ1ˆ1¨Ð,r$   Úagentc                ó¬   — t          | dd¦  «        pd}t          | d¦  «        r	 d| _        n# t          $ r Y nw xY wt	          |t
          ¦  «        r|ndS )aM  Pop the gateway's per-turn must-deliver notes off the agent (one-shot).

    The gateway relocates volatile per-turn facts OUT of the ephemeral system
    prompt (auto-reset notes, the first-contact intro, voice-channel changes)
    and delivers them on the current user message via the api_content sidecar
    instead, so the composed system prompt stays byte-stable turn-over-turn.
    It stages the rendered notes on ``agent._gateway_turn_context_notes``
    right before ``run_conversation``; this consumes them so a cached agent
    can never replay a stale note on a later turn.
    Ú_gateway_turn_context_notesÚ )ÚgetattrÚhasattrr:   Ú	Exceptionr   r   )r8   Únotess     r"   Ú"consume_gateway_turn_context_notesr@   …   sw   € õ �EÐ8¸"Ñ=Ô=ÐCÀ€EÝˆuÐ3Ñ4Ô4ð ð	Ø02ˆEÔ-Ð-øÝð 	ð 	ð 	ØˆDð	øøøå˜u¥cÑ*Ô*Ð2ˆ5ˆ5°Ð2s   ¥- ­
:¹:r?   Úboolc                óŒ   — |rt          | t          ¦  «        sdS 	 |                      d|dœ¦  «         dS # t          $ r Y dS w xY w)uõ  Deliver must-deliver notes on a multimodal (list) user message.

    ``compose_user_api_content`` returns ``None`` for non-string content, so
    sidecar-borne facts would silently drop on image/attachment turns.  For
    gateway must-deliver notes we instead append a text part to the content
    list in place â€” the part becomes durable message content (persisted and
    replayed as-is), which keeps the wire and the transcript byte-identical.

    Returns ``True`` when a part was appended.
    FÚtext)ÚtyperC   T)r   Úlistr   r>   )r   r?   s     r"   Ú"append_notes_to_multimodal_contentrF   ™   sh   € ð ð �
 7­DÑ1Ô1ð ØˆuðØ�Š °Ð6Ð6Ñ7Ô7Ð7ØˆtøÝð ð ð Øˆuˆuðøøøs   ›5 µ
AÁAÚcronÚsubagentÚmessagesú	List[Any]c                ó  ‡ ‡	‡
— t          ‰ dd¦  «        }t          ‰ dd¦  «        }|r|sdS t          t          ‰ dd¦  «        pd¦  «                             ¦   «         t          v rdS 	 ddlm} ddlm} d}t          |pg ¦  «        D ]b}t          |t          ¦  «        rK|                     d	¦  «        d
k    r2 ||                     d¦  «        ¦  «                             ¦   «         } nŒc|sdS t          ‰ dd¦  «        s=t          ‰ dd¦  «        }t          |¦  «        r
 |¦   «          t          ‰ dd¦  «        sdS t          ‰ dd¦  «        Š	t          ‰ dd¦  «        Š
 |||||t          ‰ dd¦  «        pt          ‰ dd¦  «        ‰	‰
t          ‰ dd¦  «        t          ‰ dd¦  «        t          ‰ dd¦  «        dœt          ‰ dd¦  «        ˆ	ˆ
ˆ fd„¬¦  «         dS # t          $ r  t                               dd¬¦  «         Y dS w xY w)a2  Kick off auto-titling for this session's first user message.

    Called from the turn prologue, so every surface a human reads (CLI, gateway,
    TUI/desktop, ACP) gets identical behavior without each one re-implementing
    the call. Fully defensive: titling is cosmetic and must never break a turn.
    Ú_session_dbNÚ
session_idÚplatformr;   r   )Úflatten_message_text)Úmaybe_auto_titler)   r*   r   Ú_session_db_createdFÚ_ensure_db_sessionÚmodelÚproviderÚ_title_failure_callbackÚ_emit_auxiliary_failureÚbase_urlÚapi_keyÚapi_mode)rS   rT   rW   rX   rY   Ú_on_session_titlec                 óX   •— t          ‰dd ¦  «        ‰ k    ot          ‰dd ¦  «        ‰k    S )NrS   rT   )r<   )Ú_modelÚ	_providerr8   s   €€€r"   ú<lambda>z4_maybe_title_session_at_turn_start.<locals>.<lambda>û   s6   ø€ Ý˜˜w¨Ñ-Ô-°Ò7ð BÝ˜E :¨tÑ4Ô4¸	ÒAð r$   )Úconversation_historyÚfailure_callbackÚmain_runtimeÚtitle_callbackÚruntime_validatorz%Turn-start auto-title dispatch failedT©Úexc_info)r<   r   ÚlowerÚ_UNTITLED_PLATFORMSÚagent.message_contentrO   Úagent.title_generatorrP   Úreversedr   Údictr-   ÚstripÚcallabler>   ÚloggerÚdebug)r8   rI   Ú
session_dbrM   rO   rP   Ú	user_textr0   Úensurer\   r]   s   `        @@r"   Ú"_maybe_title_session_at_turn_startrs   ¼   s¨  øøø€ õ ˜ ¨tÑ4Ô4€JÝ˜ ¨dÑ3Ô3€JØð ˜Zð Øˆå
�7�5˜* bÑ)Ô)Ð/¨RÑ0Ô0×6Ò6Ñ8Ô8Õ<OÐOÐOØˆð6MØ>Ð>Ð>Ð>Ð>Ð>Ø:Ð:Ð:Ð:Ð:Ð:ð
 ˆ	Ý˜H˜N¨Ñ+Ô+ð 	ð 	ˆCÝ˜#�tÑ$Ô$ð ¨¯ª°©¬¸FÒ)BÐ)BØ0Ð0°·²¸Ñ1CÔ1CÑDÔD×JÒJÑLÔL�	Ø�øØð 	ØˆFõ
 �uÐ3°UÑ;Ô;ð 	Ý˜UÐ$8¸$Ñ?Ô?ˆFÝ˜ÑÔð Ø�‘”�Ý˜5Ð"7¸Ñ?Ô?ð Ø�õ
 ˜ ¨Ñ.Ô.ˆÝ˜E :¨tÑ4Ô4ˆ	àÐØØØØ!)å˜Ð8¸$Ñ?Ô?ð CÝ˜5Ð";¸TÑBÔBð  Ø%Ý# E¨:°tÑ<Ô<Ý" 5¨)°TÑ:Ô:Ý# E¨:°tÑ<Ô<ðð õ # 5Ð*=¸tÑDÔDðð ð ð ð ð ð#	
ñ 	
ô 	
ð 	
ð 	
ð 	
øõ, ð Mð Mð MÝ�ŠÐ<ÀtˆÑLÔLÐLÐLÐLÐLðMøøøs!   Á(BG Ã.AG Ä<BG Ç&HÈHÚuser_messageÚintc                ó,  — ddl m} d}t          t          | ¦  «        dz
  dd¦  «        D ]i}| |         }t	          |t
          ¦  «        r|                     d¦  «        dk    sŒ9|                     d¦  «        |k    r|c S |dk     r ||¦  «        r|}Œj|S )uŸ  Locate this turn's user message after compaction rebuilt ``messages``.

    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. Returns -1 when the list has no
    user-originated message at all.
    r   )Úis_user_originated_turnéÿÿÿÿé   r)   r*   r   )Úagent.context_compressorrw   ÚrangeÚlenr   rk   r-   )rI   rt   rw   ÚfallbackÚir0   s         r"   Úreanchor_current_turn_user_idxr     sº   € ð  AÐ@Ð@Ð@Ð@Ð@à€HÝ•3�x‘=”= 1Ñ$ b¨"Ñ-Ô-ð 	ð 	ˆØ�qŒkˆÝ˜3¥Ñ%Ô%ð 	¨#¯'ª'°&©/¬/¸VÒ*CÐ*CØØ�7Š7�9ÑÔ Ò-Ð-ØˆHˆHˆHð �aŠ<ˆ<Ð3Ð3°CÑ8Ô8ˆ<ØˆHøØ€Or$   Úorig_lenÚnew_lenÚorig_tokensÚ
new_tokensc                ó0   — || k     rdS |dk    o||dz  k     S )uF  Return ``True`` if a compression pass materially reduced the request.

    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.
    Tr   çffffffî?© )r€   r�   r‚   rƒ   s       r"   Úcompression_made_progressr‡   $  s-   € ð$ �ÒÐØˆtØ˜Š?Ð>˜z¨K¸$Ñ,>Ò>Ð>r$   Úthreshold_tokensc                ó,   — ||k    o| dk    o|| dz  k     S )aw  Whether an over-threshold request merits another immediate summary.

    Row-count progress is enough to prove that a compression boundary was real,
    but not enough to justify another expensive pass before trying the provider.
    Continue only when the request remains over threshold *and* the previous pass
    materially reduced its estimated token pressure (>5%).
    r   r…   r†   )r‚   rƒ   rˆ   s      r"   Ú,_compression_warrants_another_preflight_passrŠ   B  s0   € ð 	Ð&Ò&ð 	,Ø˜!ŠOð	,à˜ tÑ+Ò+ðr$   úList[Dict[str, Any]]Úprotect_first_nÚprotect_last_nc                ó^   — t          | ¦  «        ||z   dz   k    rdS t          | ¦  «        |k    S )uè  Cheap gate for the (expensive) full preflight token estimate.

    Returns ``True`` when either:
      (a) message count exceeds the protected ranges (the historical gate), or
      (b) a cheap char-based estimate already crosses the configured threshold
          â€” the few-but-huge case from issue #27405 that the count-only gate
          would silently skip (a handful of very large messages never trips
          the count condition, so compression was never attempted and the
          turn hit a hard context-overflow error).

    Branch (b) uses ``estimate_messages_tokens_rough`` (the shared char-based
    estimator) so a single large base64 image isn't mistaken for ~250K tokens.
    It intentionally undercounts vs. the full request estimate â€” it omits the
    system prompt and tool schemas â€” because it is only a *hint* deciding
    whether to pay for the authoritative ``estimate_request_tokens_rough``,
    which (together with ``should_compress``) makes the real decision.
    ry   T)r|   r   )rI   rŒ   r�   rˆ   s       r"   Ú_should_run_preflight_estimater�   S  s8   € õ. ˆ8�}„}�¨Ñ7¸!Ñ;Ò;Ð;ØˆtÝ)¨(Ñ3Ô3Ð7GÒGÐGr$   ÚenabledÚidle_after_secondsÚidle_gap_secondsÚfloatÚtokensÚfloor_tokensÚcooldown_activec                ó:   — | r|dk    rdS ||k     rdS |rdS ||k    S )ah  Decide whether an idle-triggered compaction should run this turn.

    Idle compaction is opt-in (``idle_after_seconds <= 0`` disables it). It
    fires when a session resumes after a wall-clock gap of at least
    ``idle_after_seconds`` since its last activity, so a long-lived thread
    that is paused and later resumed compacts its accumulated history up
    front instead of re-reading it on every subsequent turn.

    It is orthogonal to the token-threshold trigger: it does NOT require the
    context to exceed ``threshold_tokens``. It still skips work when the
    context is at or below ``floor_tokens`` (the size compaction would reduce
    *to*), so a small idle thread never pays for a summarisation that saves
    nothing, and it defers to an active compression-failure cooldown.

    Pure predicate so the policy is unit-testable without a live agent.
    r   Fr†   ©r�   r‘   r’   r”   r•   r–   s         r"   Ú_should_idle_compactr™   o  sE   € ð2 ð Ð(¨AÒ-Ð-ØˆuØÐ,Ò,Ð,ØˆuØð ØˆuØ�LÒ Ð r$   c                  óœ   — e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   dZded<   dZded<   dZded<   dZded<   dS )ÚTurnContextzCValues produced by the turn prologue and consumed by the turn loop.r   rt   r   Úoriginal_user_messager‹   rI   úOptional[List[Dict[str, Any]]]r_   r   Úactive_system_promptÚeffective_task_idÚturn_idru   Úcurrent_turn_user_idxFrA   Úshould_review_memoryr;   r   r   Úpreflight_compression_blockedN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r¢   r   r   r£   r†   r$   r"   r›   r›   ‘  s¿   € € € € € € àMÐMð ÐÐÑàÐÐÑà"Ð"Ð"Ñ"à8Ð8Ð8Ñ8à'Ð'Ð'Ñ'àÐÐÑØ€L€L�LàÐÐÑà!&ÐÐ&Ð&Ð&Ñ&à!ÐÐ!Ð!Ð!Ñ!à ÐÐ Ð Ð Ñ à*/Ð!Ð/Ð/Ð/Ñ/Ð/Ð/r$   r›   F)Úpersist_user_display_kindÚpersist_user_display_metadataÚ
moa_activeÚsystem_messager_   r�   Útask_idÚpersist_user_messageúOptional[Any]Úpersist_user_timestampúOptional[float]r©   rª   úOptional[Dict[str, Any]]r«   c       
        óL+  ‡ ‡‡a—  |¦   «          t          ‰ ¦  «        }|�|Š |‰ j        ¦  «          |t          ‰ dd¦  «        ¦  «         ‰                      ¦   «          	 ddlm}  |t          ‰ dd¦  «        pdt          ‰ dd¦  «        pdt          ‰ d	d¦  «        pdt          ‰ d
d¦  «        pdt          ‰ dd¦  «        pdt          ‰ dd¦  «        pdt          ‰ dd¦  «        pdt          ‰ dd¦  «        pd¬¦  «         n# t          $ r Y nw xY w	 t          ‰ dd¦  «        s,ddl}d|j        v rddl	m
}m}  |¦   «         r |‰ d¬¦  «         n,# t          $ r t                               dd¬¦  «         Y nw xY wt          |t          ¦  «        r ||¦  «        }t          |t          ¦  «        r ||¦  «        }|‰ _        d‰ _        |‰ _        |‰ _        |pt          t)          j        ¦   «         ¦  «        }|‰ _        t          t          ‰ dd¦  «        pd¦  «        }|s/‰ j        pd› d|› dt)          j        ¦   «         j        dd…         › �}d‰ _        |‰ _        d‰ _        ddlm}  |‰ |¦  «         d‰ _        d‰ _        d‰ _        d‰ _         d‰ _!        d‰ _"        d‰ _#        d‰ _$        d‰ _%        d‰ _&        d‰ _'        ‰ j(         )                    ¦   «          d‰ _*        t          ‰ j+        dd¦  «        }tY          |¦  «        r
 |¦   «          d‰ _-        ‰ j.        dk    r;	 ‰  /                    ¦   «         r‰  0                    d¦  «         n# t          $ r Y nw xY w‰ j1        r‰  2                    ¦   «          d‰ _1        tg          ‰ j4        ¦  «        ‰ _5         ||¦  «        }tm          |¦  «        d k    r|dd …         d!z   n|}| 7                    d"d#¦  «        }t           8                    d$‰ j        pd%‰ j9        ‰ j:        pd&‰ j;        pd&tm          ‰pg ¦  «        |¦  «         ‰rty          ‰¦  «        ng Šat          ‰ d'd¦  «        }|�|n|}t          |tz          ¦  «        r!| >                    d(¦  «        |k    r|}||d(<   n!d)|d*œ}t          |tz          ¦  «        rd‰ _?        ‰r.‰ j@         A                    ¦   «         s‰  B                    ‰¦  «         ‰rV‰ jC        dk    rKt‰          d+„ ‰D ¦   «         ¦  «        } | dk    r,| ‰ _C        ‰ jE        dk    r‰ jF        dk    r| ‰ jE        z  ‰ _F        |r||d,<   |	r|	|d-<   ‰a G                    |¦  «         tm          ‰a¦  «        d.z
  }!|!‰ _        ‰ xjC        d.z  c_C        d‰ _H        t          ‰ d/d¦  «        }"|"�|" I                    ¦   «          t          ‰ d0d¦  «        }#|#�|# I                    ¦   «          |�|n|}$d}%‰ jE        dk    r9d1‰ jJ        v r0‰ j+        r)‰ xjF        d.z  c_F        ‰ jF        ‰ jE        k    r	d}%d‰ _F        t          ‰ d2d¦  «        }&|&�0	 dd3lKmL}'  |'|$¦  «        }(|(r |&|(¦  «         n# t          $ r Y nw xY w‰ jM        sC ||¦  «        })‰  N                    d4|)dd5…         › tm          |)¦  «        d5k    rd!nd› d6�¦  «         ‰ jO        € |
‰ |‰¦  «         ‰ jO        }*t          ‰ d7d¦  «        }+	 |+€‰  P                    ¦   «          n.|+5  ‰  P                    ¦   «          ddd¦  «         n# 1 swxY w Y   n4# t          $ r' t           Q                    d8‰ j        pd%d¬¦  «         Y nw xY wt          |tz          ¦  «        r| >                    d9¦  «        rd‰ _?        n6# t          |tz          ¦  «        r| >                    d9¦  «        rd‰ _?        w xY wt          ‰ d:d¦  «        },‰ jR        �r¦|,dk    �rŸ‰a�rœt§          jS        ¦   «         t          ‰ d;t§          jS        ¦   «         ¦  «        z
  }-|-|,k    �r_‰ jT        }.t«          ‰a|*pd‰ jV        pd¬<¦  «        }/t¯          |.jX        |.jY        z  ¦  «        }0 t          |.d=d>„ ¦  «        ¦   «         }1tµ          ‰ jR        |,|-|/|0t·          |1¦  «        ¬?¦  «        ràt           8                    d@t¯          |-¦  «        |,|/dA›|0dA›‰ j        pd%¦  «         t¹          |.dBt»          j^        t¯          |-¦  «        |/¬C¦  «        |/t¯          |-¦  «        ‰ j9        ¬D¦  «        }2|2r‰  0                    |2¦  «         ‰a}3‰  _                    ‰a||/|¬E¦  «        \  Ša}*‰a|3ur(tÁ          ‰ ‰a‰¦  «        ŠtÃ          ‰a|¦  «        }!|!‰ _        d}4d}5d‰ _b        d‰ _c        ‰ jR        �rtÉ          ‰a‰ jT        je        ‰ jT        jf        ‰ jT        jX        ¦  «        �rÕt«          ‰a|*pd‰ jV        pd¬<¦  «        }6‰ jT        }.t          |.dFd¦  «        }7tY          |7¦  «        r; |7¦   «         }8t          |8t®          ¦  «        rt          |8t¶          ¦  «        s|8‰ _c        t          |.dGdH„ ¦  «        }9 |9|6¦  «        }:t          ‰ dd¦  «        dIk    o3t          t          ‰ dJdK¦  «        pdK¦  «         g                    ¦   «         dLv };|:s|.jh        }<|<dk    r|6|<k    r|6|._h         t          |.d=dM„ ¦  «        ¦   «         }=d}>d}?|:r.t           8                    dN|6dA›|.jX        dA›|.ji        dA›¦  «         n÷|=rlt           8                    dOt¯          |= >                    dPdQ¦  «        ¦  «        ‰ j        pd%¦  «         |6|.jX        k    r|= >                    dPdQ¦  «        }@dR|@dS›�}?n‰|;r+t           8                    dTt          ‰ dJdK¦  «        ¦  «         n\|. j                    |6¦  «        }>|>sEt          |.dUd¦  «        }AtY          |A¦  «        r%	  |A|6¦  «        d.         }?n# t          $ r d}?Y nw xY w|>�r!d}4t          ‰ dVd¦  «        }BtY          |B¦  «        r
 |B¦   «          t           8                    dW|6dA›|.jX        dA›‰ j9        |.jk        dA›¦  «         t¹          |.dXtÙ          j^        |6|.jX        ¬Y¦  «        |6|.jX        |.jk        ‰ j9        ¬Z¦  «        }C|Cr‰  0                    |C¦  «         tÛ          d.t¯          t          ‰ d[d\¦  «        pd\¦  «        ¦  «        }DtÝ          |D¦  «        D �],}Etm          ‰a¦  «        }F|6}G‰a}H‰  _                    ‰a||6|¬E¦  «        \  Ša}*‰a|Hu r3tß          ‰ ¦  «        r$t           8                    d]‰ j        pd%¦  «          nÅt«          ‰a|*pd‰ jV        pd¬<¦  «        }6tá          |Ftm          ‰a¦  «        |G|6¦  «        sd}5 n‡tÁ          ‰ ‰a‰¦  «        Šd‰ _        d‰ _"        d‰ _$        d‰ _%        d‰ _&        |. j                    |6¦  «        s n<tã          |G|6|.jX        ¦  «        s$d}5t           Q                    d^|GdA›|6dA›¦  «          n�Œ.�nm|?r‰  r                    |?|6|.jX        ¦  «         �nMt          ‰ dVd¦  «        }BtY          |B¦  «        r
 |B¦   «          |=s|:s|;rd}Int          |.d_d¦  «        }Id}JtY          |I¦  «        rN	 t·           |I‰a¦  «        ¦  «        }Jn4# t          $ r'}Kt                               d`|K¦  «         d}JY d}K~Knd}K~Kww xY w|Jr¨t           8                    dat          |.dbtç          |.¦  «        jt        ¦  «        |6dA›t          |.dcd¦  «        dA›¦  «         ‰a}L‰  _                    ‰a||6|¬E¦  «        \  Ša}*‰a|Lur5d}4tÁ          ‰ ‰a¦  «        Šd‰ _        d‰ _"        d‰ _$        d‰ _%        d‰ _&        |4rtÃ          ‰a|¦  «        }!|!‰ _        d}M	 dddlumv}N  |Nde‰ j        |||$ty          ‰a¦  «        t·          ‰¦  «         ‰ j9        t          ‰ dfd¦  «        pdt          ‰ dgd¦  «        pdt          ‰ dhd¦  «        pd¬i¦  «        }Og }P	 ddjlwmx}Qmy}R  |Q¦   «         }Sn# t          $ r d}Rd}SY nw xY w|OD ]Ð}Td}Ut          |Ttz          ¦  «        r+|T >                    dk¦  «        rt          |Tdk         ¦  «        }Un-t          |Tt          ¦  «        r|T z                    ¦   «         r|T}UnŒq|R�H	  |R|U‰ j        dl|S¬m¦  «        }Un2# t          $ r%}Vt           Q                    dn|V¦  «         Y d}V~Vnd}V~Vww xY w|P G                    |U¦  «         ŒÑ|Prdo {                    |P¦  «        }Mn2# t          $ r%}Wt           Q                    dp|W¦  «         Y d}W~Wnd}W~Www xY wtù          ‰ ¦  «        }X|Xr‡d|!cxk    rtm          ‰a¦  «        k     r9n n6t          ‰a|!         tz          ¦  «        r‰a|!          >                    d(¦  «        nd}Yt          |Ytx          ¦  «        rtû          |Y|X¦  «         n|Mr|Mdoz   |Xz   n|X}Mi ‰ _~        tÿ          ¦   «         ‰ _€        d‰ _�        d‰ _‚        �t          j„        ¦   «         j…        ‰ _†         |¦   «          ‡                    d‰ j†        ¦  «         ‰ jˆ        r+ |¦   «          ‡                    d‰ j†        ¦  «         d‰ _‰        nd‰ _Š        d‰ _‰        ‰ j‹        rK	 t          |$t          ¦  «        r|$nd}Z‰ j‹         Œ                    ‰ jC        |Z¦  «         n# t          $ r Y nw xY wd}[‰ j‹        rW	 t          |$t          ¦  «        r|$nd}\�t          |\¦  «        s‰ j‹         Ž                    |\¦  «        pd}[n# t          $ r Y nw xY w|�s:t          ‰ dd¦  «        dIk    �r$d|!cxk    rtm          ‰a¦  «        k     �r	n �n‰a|!          >                    dq¦  «        d)k    ræ‰a|!         }]�t          |] >                    d(d¦  «        |[|M¦  «        }^|^�¶|^|] >                    d(¦  «        k    r�|^|]dr<   |4r–t·          t          ‰ dsd¦  «        ¦  «        rxt          ‰ dtd¦  «        }_|_�e	 |_ �                    ‰ j        |] >                    d(¦  «        |^¦  «         n4# t          $ r' t           Q                    du‰ j        pd%d¬¦  «         Y nw xY wd{ˆ ˆˆafdx„}`	 |+€ |`¦   «          n$|+5   |`¦   «          ddd¦  «         n# 1 swxY w Y   n4# t          $ r' t           Q                    dy‰ j        pd%d¬¦  «         Y nw xY wt          |tz          ¦  «        r| >                    d9¦  «        rd‰ _?        n6# t          |tz          ¦  «        r| >                    d9¦  «        rd‰ _?        w xY w�t#          ‰ ‰a¦  «         �t%          ||$‰a‰|*|||!|%|M|[|5¬z¦  «        S )|a  Run the once-per-turn setup and return the loop's input context.

    The callables/helpers the original prologue referenced from the
    ``conversation_loop`` module are passed in explicitly to keep this module
    free of an import cycle with ``agent.conversation_loop``.
    NÚ_memory_write_originÚassistant_toolr   )Úset_runtime_mainrT   r;   rS   Úrequested_providerrW   rX   rY   Ú	auth_moderM   )r·   rW   rX   rY   r¸   rM   Ú_skip_mcp_refreshFztools.mcp_tool)Úhas_registered_mcp_toolsÚrefresh_agent_mcp_toolsT)Ú
quiet_modez&between-turns MCP tool refresh skippedrd   Ú_relay_pending_turn_idÚsessionú:é   )Únote_turn_startÚreset_consolidation_failuresÚanthropic_messagesu~   ðŸ”Œ Detected stale connections from a previous provider issue â€” cleaned up automatically. Proceeding with fresh connection.éP   z...Ú
Ú zPconversation turn: session=%s model=%s provider=%s platform=%s history=%d msg=%rÚnoneÚunknownÚ_pending_cli_user_messager   r*   )r)   r   c              3  óL   K  — | ]}|                      d ¦  «        dk    ¯dV — Œ dS )r)   r*   ry   N)r-   )Ú.0Úms     r"   ú	<genexpr>z%build_turn_context.<locals>.<genexpr>~  s?   è è € ð 
ð 
Ø¨q¯uªu°V©}¬}ÀÒ/FÐ/FˆAÐ/FÐ/FÐ/FÐ/Fð
ð 
r$   Údisplay_kindÚdisplay_metadatary   Ú_stream_context_scrubberÚ_stream_think_scrubberÚmemoryÚreaction_callback)Údetect_reactionu   ðŸ’¬ Starting conversation: 'é<   ú'Ú_session_persist_lockz5Turn-start session row creation failed for session=%sÚ_db_persistedÚ&compression_idle_compact_after_secondsÚ_last_activity_ts)Úsystem_promptÚtoolsÚ'get_active_compression_failure_cooldownc                 ó   — d S ©Nr†   r†   r$   r"   r^   z$build_turn_context.<locals>.<lambda>  s   € ÐPT€ r$   r˜   zDIdle compaction: %ss idle >= %ss, ~%s tokens > %s floor (session %s)Ú,Úidle)Úidle_secondsr”   )ÚphaseÚdefault_messageÚapprox_tokensrâ   rS   )rå   r­   Ú!snapshot_preflight_display_tokensÚ$should_defer_preflight_to_real_usagec                ó   — dS )NFr†   )Ú_tokenss    r"   r^   z$build_turn_context.<locals>.<lambda>g  s   € ˜E€ r$   Úcodex_app_serverÚ codex_app_server_auto_compactionÚnative>   Úoffrì   c                 ó   — d S rß   r†   r†   r$   r"   r^   z$build_turn_context.<locals>.<lambda>‚  s   € �D€ r$   zpSkipping preflight compression: rough estimate ~%s >= %s, but last real provider prompt was %s after compressionz`Skipping preflight compression: same-session cooldown active (~%s seconds remaining, session %s)Úremaining_secondsg        z	cooldown:z.0fzsSkipping Hermes preflight compression for codex app-server (mode=%s); Hermes will not start thread compaction here.Úshould_compress_infoÚ_clear_context_overflow_warnzDPreflight compression: ~%s tokens >= %s threshold (model %s, ctx %s)Ú	preflight)r”   Ú	threshold)rã   rä   rå   rˆ   Úcontext_lengthrS   Úmax_compression_attemptsé   zRPreflight compression deferred: compression lock held by another path (session %s)zgPreflight compression made insufficient progress: ~%s -> ~%s request tokens; skipping additional passesÚshould_compress_preflightzQshould_compress_preflight raised %s; skipping engine-driven preflight maintenancez_Engine-driven preflight maintenance: %s requested compress() at ~%s tokens (below %s threshold)Únamerˆ   )Úinvoke_hookÚpre_llm_callrN   Ú_parent_session_idÚ_user_id)
rM   r­   r    rt   r_   Úis_first_turnrS   rN   Úparent_session_idÚ	sender_id)Úget_spill_configÚspill_if_oversizedÚcontextzplugin hook)rM   ÚsourceÚconfigzhook context spill failed: %sr   zpre_llm_call hook failed: %sr)   r(   Ú_last_compaction_in_placerL   z>in-place compaction api_content backfill failed for session=%sr   r1   c                 ó\   •— ‰                       ¦   «          ‰                      ‰‰¦  «         d S rß   )rR   Ú_persist_session)r8   r_   rI   s   €€€r"   Ú_ensure_and_persistz/build_turn_context.<locals>._ensure_and_persist4  s2   ø€ Ø× Ò Ñ"Ô"Ð"Ø×Ò˜xÐ)=Ñ>Ô>Ð>Ð>Ð>r$   z:Early turn-start session persistence failed for session=%s)rt   rœ   rI   r_   rž   rŸ   r    r¡   r¢   r   r   r£   )r   r1   )“r   rM   r<   Ú_restore_primary_runtimeÚagent.auxiliary_clientr¶   r>   ÚsysÚmodulesÚtools.mcp_toolrº   r»   rn   ro   r   r   Ú_stream_callbackÚ_persist_user_message_idxÚ_persist_user_message_overrideÚ_persist_user_message_timestampÚuuidÚuuid4Ú_current_task_idÚhexr½   Ú_current_turn_idÚ_current_api_request_idÚagent.agent_runtime_helpersrÁ   Ú_invalid_tool_retriesÚ_invalid_json_retriesÚ_empty_content_retriesÚ_incomplete_scratchpad_retriesÚ_codex_incomplete_retriesÚ_thinking_prefill_retriesÚ_post_tool_empty_retriedÚ_last_content_with_toolsÚ$_last_content_tools_all_housekeepingÚ_mute_post_responseÚ_unicode_sanitization_passesÚ_tool_guardrailsÚreset_for_turnÚ_tool_guardrail_halt_decisionÚ_memory_storerm   Ú_vision_supportedrY   Ú_cleanup_dead_connectionsÚ_emit_statusÚ_compression_warningÚ_replay_compression_warningr   Úmax_iterationsÚiteration_budgetr|   ÚreplaceÚinforS   rT   rN   rE   rk   r-   rÉ   Ú_todo_storeÚ	has_itemsÚ_hydrate_todo_storeÚ_user_turn_countÚsumÚ_memory_nudge_intervalÚ_turns_since_memoryr   Ú_is_user_initiated_turnÚresetÚvalid_tool_namesÚagent.reactionsrÔ   r¼   Ú_safe_printÚ_cached_system_promptrR   ÚwarningÚcompression_enabledÚtimeÚcontext_compressorr   rÜ   ru   rˆ   Úsummary_target_ratior™   rA   r   r
   ÚformatÚ_compress_contextr   r   Ú _turn_received_provider_responseÚ _turn_preflight_display_snapshotr�   rŒ   r�   rf   Úlast_prompt_tokensÚlast_real_prompt_tokensÚshould_compressrô   r   Úmaxr{   r   Ú_compression_made_progressrŠ   Ú_warn_context_overflow_blockedrD   r¤   Úhermes_cli.lifecyclerù   Útools.hook_output_spillr   r  rl   r   r@   rF   Ú_turn_failed_file_mutationsÚsetÚ_turn_file_mutation_pathsÚ_verification_stop_nudgesÚ_pre_verify_nudgesÚ	threadingÚcurrent_threadÚidentÚ_execution_thread_idÚ_set_interruptÚ_interrupt_requestedÚ _interrupt_thread_signal_pendingÚ_interrupt_messageÚ_memory_managerÚon_turn_startr   Úprefetch_allr#   Úset_latest_user_api_contentrs   r›   )br8   rt   r¬   r_   r­   Ústream_callbackr®   r°   r©   rª   Úrestore_or_build_system_promptÚinstall_safe_stdioÚsanitize_surrogatesÚsummarize_user_message_for_logÚset_session_contextÚset_current_write_originÚrar«   Úrecovered_historyr¶   Ú_sysrº   r»   rŸ   r    rÁ   Ú_reset_consolÚ_preview_textÚ_msg_previewÚpending_cli_messageÚexpected_persist_contentÚuser_msgÚprior_user_turnsr¡   ÚscrubberÚthink_scrubberrœ   r¢   rÓ   rÔ   ÚkindÚ_print_previewrž   Úpersist_lockÚ_idle_afterÚ	_idle_gapÚ_compressorÚ_idle_tokensÚ_idle_floorÚ_idle_cooldownÚ_idle_statusÚ_idle_inputÚ_preflight_compressedÚ_preflight_compression_blockedÚ_preflight_tokensÚ_snapshot_fnÚ_snapshot_valÚ_defer_preflightÚ_preflight_deferredÚ_codex_native_autoÚ_lastÚ_compression_cooldownÚ_should_compress_nowÚ_compress_block_reasonÚ_cooldown_secsÚ_infoÚ_clear_warnÚ_preflight_statusÚ_max_preflight_passesÚ_passÚ	_orig_lenÚ_orig_tokensÚ_preflight_inputÚ_engine_preflightÚ_wants_engine_preflightÚ_preflight_excÚ_engine_inputr   Ú_invoke_hookÚ_pre_resultsÚ
_ctx_partsÚ
_spill_cfgÚ_spill_if_oversizedÚ_spill_config_cachedÚrÚ_pieceÚ
_spill_excÚexcÚ_gateway_notesÚ_gw_turn_contentÚ	_turn_msgr   Ú_queryÚ_turn_user_msgÚ_api_contentÚ_dbr  rI   sb   `  `                                                                                             @r"   Úbuild_turn_contextr¨  ®  s”  øøø€ ð8 ÐÑÔÐõ
 <¸EÑBÔBÐØÐ$Ø0Ðð Ð˜Ô(Ñ)Ô)Ð)ð Ð�W UÐ,BÐDTÑUÔUÑVÔVÐVð 
×"Ò"Ñ$Ô$Ð$ðØ;Ð;Ð;Ð;Ð;Ð;ØÐÝ�E˜: rÑ*Ô*Ð0¨bÝ�E˜7 BÑ'Ô'Ð-¨2Ý& uÐ.BÀBÑGÔGÐMÈ2Ý˜U J°Ñ3Ô3Ð9°rÝ˜E 9¨bÑ1Ô1Ð7°RÝ˜U J°Ñ3Ô3Ð9°rÝ˜e [°"Ñ5Ô5Ð;¸Ý˜u l°BÑ7Ô7Ð=¸2ð		
ñ 		
ô 		
ð 		
ð 		
øõ ð ð ð ØˆðøøøðNÝ�uÐ1°5Ñ9Ô9ð 	Dð ÐÐÐØ 4¤<Ð/Ð/Ø\Ð\Ð\Ð\Ð\Ð\Ð\Ð\Ø+Ð+Ñ-Ô-ð DØ+Ð+¨E¸dÐCÑCÔCÐCøøÝð Nð Nð NÝ�ŠÐ=ÈˆÑMÔMÐMÐMÐMðNøøøõ �,¥Ñ$Ô$ð 9Ø*Ð*¨<Ñ8Ô8ˆÝÐ&­Ñ,Ô,ð IØ2Ð2Ð3GÑHÔHÐð -€EÔØ&*€EÔ#Ø+?€EÔ(Ø,B€EÔ)àÐ4¥3¥t¤z¡|¤|Ñ#4Ô#4ÐØ.€EÔÝ•'˜%Ð!9¸2Ñ>Ô>ÐDÀ"ÑEÔE€GØð 
àÔÐ, 9ÐYÐYÐ/@ÐYÐYÅ4Ä:Á<Ä<ÔCSÐTVÐUVÐTVÔCWÐYÐYð 	ð $(€EÔ Ø$€EÔØ$&€EÔ!ð <Ð;Ð;Ð;Ð;Ð;Ø€O�E˜7Ñ#Ô#Ð#ð #$€EÔØ"#€EÔØ#$€EÔ Ø+,€EÔ(Ø&'€EÔ#Ø&'€EÔ#Ø%*€EÔ"Ø%)€EÔ"Ø16€EÔ.Ø %€EÔØ)*€EÔ&Ø	Ô×)Ò)Ñ+Ô+Ð+Ø*.€EÔ'Ý˜EÔ/Ð1OÐQUÑVÔV€MÝ�ÑÔð Øˆ‰ŒˆØ"€EÔð „~Ð-Ò-Ð-ð	Ø×.Ò.Ñ0Ô0ð Ø×"Ò"ð"ñô ð øøõ
 ð 	ð 	ð 	ØˆDð	øøøð Ô!ð *Ø×)Ò)Ñ+Ô+Ð+Ø%)ˆÔ"õ -¨UÔ-AÑBÔB€EÔð 3Ð2°<Ñ@Ô@€MÝ36°}Ñ3EÔ3EÈÒ3JÐ3J�M # 2 #Ô&¨Ñ.Ð.ÐP]€LØ×'Ò'¨¨cÑ2Ô2€LÝ
‡K‚KØZØÔÐ"˜F E¤K°´Ð1LÀ9ØŒÐ#˜)¥SÐ)=Ð)CÀÑ%DÔ%DØñ	ô ð ð .BÐI�tÐ(Ñ)Ô)Ð)Àr€Hõ " %Ð)DÀdÑKÔKÐà 4Ð @ÐÐÀlð õ 	Ð&­Ñ-Ô-ð3à×#Ò# IÑ.Ô.Ð2JÒJÐJà&ˆð +ˆ�ÑÐà"¨|Ð<Ð<ˆÝÐ)­4Ñ0Ô0ð 	3Ø.2ˆEÔ+ð ð 8 EÔ$5×$?Ò$?Ñ$AÔ$Að 8Ø×!Ò!Ð"6Ñ7Ô7Ð7ð ð \ Ô 6¸!Ò ;Ð ;Ýð 
ð 
Ø+ð
ñ 
ô 
ñ 
ô 
Ðð ˜aÒÐØ%5ˆEÔ"ØÔ+¨aÒ/Ð/°EÔ4MÐQRÒ4RÐ4RØ,<¸uÔ?[Ñ,[�Ô)ð !ð IØ#<ˆ�Ñ Ø(ð 	IØ+HˆHÐ'Ñ(à‡O‚O�HÑÔÐÝ ™MœM¨AÑ-ÐØ&;€EÔ#ð 
ÐÔ˜aÑÐÔð %)€EÔ!õ �uÐ8¸$Ñ?Ô?€HØÐØ�ŠÑÔÐå˜UÐ$<¸dÑCÔC€NØÐ!Ø×ÒÑÔÐð 5IÐ4TÐ0Ð0ÐZfÐð !ÐØÔ$ qÒ(Ð(Ø˜EÔ2Ð2Ð2ØÔ#ð 3àÐ!Ô! QÑ&Ð!Ô!ØÔ$¨Ô(DÒDÐDØ#'Ð Ø()ˆEÔ%õ
   Ð':¸DÑAÔAÐØÐ$ð	Ø7Ð7Ð7Ð7Ð7Ð7à"�?Ð#8Ñ9Ô9ˆDØð (Ø!Ð! $Ñ'Ô'Ð'øøÝð 	ð 	ð 	ØˆDð	øøøð Ôð 
Ø7Ð7¸ÑEÔEˆØ×Òð<¨N¸3¸B¸3Ô,?ð <Ý˜NÑ+Ô+¨bÒ0Ð0ˆuˆu°bð<ð <ð <ñ	
ô 	
ð 	
ð Ô"Ð*Ø&Ð& u¨nÐ>RÑSÔSÐSà Ô6Ðõ ˜5Ð"9¸4Ñ@Ô@€Lð3ØÐØ×$Ò$Ñ&Ô&Ð&Ð&àð +ð +Ø×(Ò(Ñ*Ô*Ð*ð+ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +øøåð 
ð 
ð 
Ý�ŠØCØÔÐ& Øð 	ñ 	
ô 	
ð 	
ð 	
ð 	
ð
øøøõ Ð-­tÑ4Ô4ð 	3Ð8K×8OÒ8OÐP_Ñ8`Ô8`ð 	3Ø.2ˆEÔ+øøõ Ð-­tÑ4Ô4ð 	3Ð8K×8OÒ8OÐP_Ñ8`Ô8`ð 	3Ø.2ˆEÔ+Ð2Ð2Ð2Ð2õ ˜%Ð!IÈ1ÑMÔM€KØÔ ñ CL [°1¢_¡_¸¡_Ý”I‘K”K¥'¨%Ð1DÅdÄiÁkÄkÑ"RÔ"RÑRˆ	Ø˜Ò#Ñ#ØÔ2ˆKÝ8ØØ2Ð8°bØ”kÐ) Tðñ ô ˆLõ ØÔ,¨{Ô/OÑOñô ˆKð�WØÐFÈÈñô ñ ô ˆNõ $ØÔ1Ø#.Ø!*Ø#Ø(Ý $ ^Ñ 4Ô 4ðñ ô ð 2Lõ —’ð#å˜	‘N”NØØ#Ð'Ð'Ø"Ð&Ð&ØÔ$Ð.¨ñô ð õ  CØØ Ý$CÔ$JÝ%(¨¡^¤^¸Lð%ñ %ô %ð #/Ý!$ Y¡¤Øœ+ð	 ñ 	 ô 	 �ð  ð 5Ø×&Ò& |Ñ4Ô4Ð4Ø&�Ø16×1HÒ1HØ˜n¸LØ-ð 2Iñ 2ô 2Ñ.�Ð.ð  ;Ð.Ð.Ý+QØ˜xÐ)=ñ,ô ,Ð(õ -KØ  ,ñ-ô -Ð)ð 7L�EÔ3ð "ÐØ%*Ð"Ø-2€EÔ*Ø-1€EÔ*ØÔ ñ h6Õ%CØØÔ Ô0ØÔ Ô/ØÔ Ô1ñ	&ô &ñ h6õ :ØØ.Ð4°"Ø”+Ð% ð
ñ 
ô 
Ðð
 Ô.ˆõ
 ØÐ<¸dñ
ô 
ˆõ �LÑ!Ô!ð 	GØ(˜L™NœNˆMõ ˜-­Ñ-Ô-ð GµjØ�tñ7ô 7ð Gð :G�Ô6Ý"ØØ2Ø!Ð!ñ
ô 
Ðð
 /Ð.Ð/@ÑAÔAÐõ �E˜: tÑ,Ô,Ð0BÒBð 	!ÝÝØØ6Øñô ð ð
 ñô ÷ Še‰gŒgØ ð!ð 	ð #ð 	CØÔ2ˆEà˜ŠzˆzÐ/°%Ò7Ð7Ø1B�Ô.ð!
¥ØØ5ØˆLñ!
ô !
ñ !ô !Ðð  %ÐØ!%ÐØð (	6Ý�KŠKðIà$Ð(Ð(ØÔ/Ð3Ð3ØÔ6Ð:Ð:ñô ð ð ð #ð  	6Ý�KŠKð6åÐ)×-Ò-Ð.AÀ3ÑGÔGÑHÔHØÔ Ð* Fñ	ô ð ð ! KÔ$@Ò@Ð@ð "7×!:Ò!:Ð;NÐPSÑ!TÔ!T�Ø)I°^Ð)IÐ)IÐ)IÐ&øØð 	6Ý�KŠKðKå˜ÐAÀ8ÑLÔLñô ð ð ð $/×#>Ò#>Ð?PÑ#QÔ#QÐ Ø'ð 6õ   Ð-CÀTÑJÔJ�Ý˜E‘?”?ð 6ð6Ø16°Ð7HÑ1IÔ1IÈ!Ô1LÐ.Ð.øÝ$ð 6ð 6ð 6Ø15Ð.Ð.Ð.ð6øøøàñ A	6Ø$(Ð!õ " %Ð)GÈÑNÔNˆKÝ˜Ñ$Ô$ð Ø�‘”�Ý�KŠKØVØ$Ð(Ð(ØÔ/Ð3Ð3Ø”ØÔ-Ð1Ð1ñô ð õ !DØØ!Ý EÔ LØ,Ø)Ô:ð!ñ !ô !ð 0Ø!,Ô!=Ø*Ô9Ø”kð!ñ !ô !Ðð !ð 6Ø×"Ò"Ð#4Ñ5Ô5Ð5õ %(Ø•3•w˜uÐ&@À!ÑDÔDÐIÈÑJÔJñ%ô %Ð!õ Ð4Ñ5Ô5ð >ñ >�Ý ™MœM�	Ø0�Ø#+Ð Ø16×1HÒ1HØ˜nÐ<MØ-ð 2Iñ 2ô 2Ñ.�Ð.ð
 Ð 0Ð0Ð0Ý7¸Ñ>Ô>ð 1õ —K’Kð<àÔ(Ð2¨Fñô ð ð
 �Eõ
 %BØØ"6Ð"<¸"Øœ+Ð-¨ð%ñ %ô %Ð!õ
 2Ø�s 8™}œ}¨lÐ<Mñô ð ð 6:Ð2Ø�EÝ'MØ˜8Ð%9ñ(ô (Ð$ð 01�Ô,Ø23�Ô/Ø15�Ô.Ø=B�Ô:Ø,1�Ô)Ø"×2Ò2Ð3DÑEÔEð Ø�EÝCØ Ø%ØÔ0ñô ð ð
 6:Ð2Ý—N’NðPà'Ð+Ð+Ø,Ð0Ð0ñ	ô ð ð �Eñùð $ð ]	6ð ×0Ò0Ø&Ø!ØÔ,ñô ð ñ õ " %Ð)GÈÑNÔNˆKÝ˜Ñ$Ô$ð Ø�‘”�ð
 %ð Ð(;ð Ð?Qð Ø$(Ð!Ð!å$+ØÐ!<¸dñ%ô %Ð!ð2 ',Ð#ÝÐ)Ñ*Ô*ð 4ð
4Ý.2Ð3DÐ3DÀXÑ3NÔ3NÑ.OÔ.OÐ+Ð+øÝ ð 4ð 4ð 4õ —L’Lð>à&ñô ð ð
 /4Ð+Ð+Ð+Ð+Ð+Ð+øøøøð4øøøð 'ð 6Ý—’ðDå˜K¨µ°kÑ1BÔ1BÔ1KÑLÔLØ(Ð,Ð,Ý˜{Ð,>ÀÑBÔBÐFÐFñô ð ð !)�Ø16×1HÒ1HØ˜nÐ<MØ-ð 2Iñ 2ô 2Ñ.�Ð.ð  =Ð0Ð0Ø,0Ð)Ý+QØ˜xñ,ô ,Ð(ð 45�EÔ0Ø67�EÔ3Ø59�EÔ2ØAF�EÔ>Ø05�EÔ-àð @õ !?Ø�lñ!
ô !
Ðð +@ˆÔ'ð Ðð2<ØDÐDÐDÐDÐDÐDØ#�|ØØÔ'Ø%ØØ.Ý!% h¡¤Ý#Ð$8Ñ9Ô9Ð9Ø”+Ý˜U J°Ñ5Ô5Ð;¸Ý% eÐ-AÀ4ÑHÔHÐNÈBÝ˜e Z°Ñ6Ô6Ð<¸"ð
ñ 
ô 
ˆð !#ˆ
ð	(ðð ð ð ð ð ð ð ð $. :¡<¤<Ð Ð øÝð 	(ð 	(ð 	(Ø"&ÐØ#'Ð Ð Ð ð	(øøøð ð 	&ð 	&ˆAØˆFÝ˜!�TÑ"Ô"ð  q§u¢u¨YÑ'7Ô'7ð Ý˜Q˜yœ\Ñ*Ô*��Ý˜A�sÑ#Ô#ð ¨¯ª©	¬	ð Ø��àØ"Ð.ðPØ0Ð0ØØ#(Ô#3Ø,Ø3ð	ñ ô �F�Føõ !ð Pð Pð PÝ—N’NÐ#BÀJÑOÔOÐOÐOÐOÐOÐOÐOøøøøðPøøøà×Ò˜fÑ%Ô%Ð%Ð%Øð 	:Ø"(§+¢+¨jÑ"9Ô"9ÐøøÝð <ð <ð <Ý�ŠÐ5°sÑ;Ô;Ð;Ð;Ð;Ð;Ð;Ð;øøøøð<øøøõ 8¸Ñ>Ô>€NØð ð Ð)Ð9Ð9Ò9Ð9­C°©M¬MÒ9Ð9Ð9Ð9Ð9Ý˜8Ð$9Ô:½DÑAÔAð :ð Ð*Ô+×/Ò/°	Ñ:Ô:Ð:ð ð	 	õ Ð&­Ñ-Ô-ð 	Ý.Ð/?ÀÑPÔPÐPÐPð 'ð$Ð# fÑ,¨~Ñ=Ð=à#ð  ð )+€EÔ%Ý&)¡e¤e€EÔ#Ø&'€EÔ#Ø €EÔö "+Ô!9Ñ!;Ô!;Ô!A€EÔð €B�D„D×Ò˜˜uÔ9Ñ:Ô:Ð:ØÔ!ð 7Ø
ˆ‰Œ×Ò˜D %Ô"<Ñ=Ô=Ð=Ø16ˆÔ.Ð.à#'ˆÔ Ø16ˆÔ.ð Ôð ð	Ý1;Ð<QÕSVÑ1WÔ1WÐ_Ð-Ð-Ð]_ˆIØÔ!×/Ò/°Ô0FÈ	ÑRÔRÐRÐRøÝð 	ð 	ð 	ØˆDð	øøøð ÐØÔð ð	Ý.8Ð9NÕPSÑ.TÔ.TÐ\Ð*Ð*ÐZ\ˆFÞ$ VÑ,Ô,ð VØ%*Ô%:×%GÒ%GÈÑ%OÔ%OÐ%UÐSUÐ"øøÝð 	ð 	ð 	ØˆDð	øøøð* ñ%å�E˜: tÑ,Ô,Ð0BÒBÑBØÐ&Ð6Ð6Ò6Ð6­¨X©¬Ò6Ñ6Ð6Ð6Ñ6ØÐ*Ô+×/Ò/°Ñ7Ô7¸6ÒAÐAà!Ð"7Ô8ˆÞ/Ø×Ò˜y¨"Ñ-Ô-Ð/AÐCVñ
ô 
ˆð Ð#¨¸×8JÒ8JÈ9Ñ8UÔ8UÒ(UÐ(UØ,8ˆN˜=Ñ)ð %ð ­Ý˜Ð:¸EÑBÔBñ*ô *ð õ ˜e ]°DÑ9Ô9�Ø�?ðØ×7Ò7Ø!Ô,Ø*×.Ò.¨yÑ9Ô9Ø(ñô ð ð øõ
 %ð ð ð ÝŸšð-à!Ô,Ð6°Ø%)ð	 'ñ ô ð ð ð ðøøøð?ð ?ð ?ð ?ð ?ð ?ð ?ð ?ð3ØÐØÐÑ!Ô!Ð!Ð!àð &ð &Ø#Ð#Ñ%Ô%Ð%ð&ð &ð &ñ &ô &ð &ð &ð &ð &ð &ð &øøøð &ð &ð &ð &øøåð 
ð 
ð 
Ý�ŠØHØÔÐ& Øð 	ñ 	
ô 	
ð 	
ð 	
ð 	
ð
øøøõ Ð-­tÑ4Ô4ð 	3Ð8K×8OÒ8OÐP_Ñ8`Ô8`ð 	3Ø.2ˆEÔ+øøõ Ð-­tÑ4Ô4ð 	3Ð8K×8OÒ8OÐP_Ñ8`Ô8`ð 	3Ø.2ˆEÔ+Ð2Ð2Ð2Ð2ö ' u¨hÑ7Ô7Ð7æØ!Ø3ØØ1Ø1Ø+ØØ3Ø1Ø/Ø-Ø&Dðñ ô ð sû  Á B!D Ä
DÄDÄ=E Å&E:Å9E:Ì))M Í
M ÍM Ø4Y Ù
Y ÙY Û\  Û3\Ü\  Ü\Ü\  Ü\Ü\  Ü^ Ü .]Ý^ Ý]Ý^ Þ3^9ï&o8 ï8pðpú$z= ú=
{.û{)û){.þ7A6AE  Á@.AA ÁA AE  ÁAAAÁAAE  ÁAAAÁAA8AE  ÁCAC ÁCAE  ÁC 
ADÁC*AD
ÁDAE  ÁD
ADÁD0AE  ÁE 
AE/ÁE
AE*ÁE*AE/ÁJ;9AK5 ÁK5
ALÁLALÁLAAM ÁM
AM"ÁM!AM"ÁP>/AQ. ÁQ..ARÁRARÁR+AS ÁR:ASÁSAS ÁSASÁSAS ÁSASÁSAS ÁSAU ÁS.ATÁTAU ÁTATÁTAU ÁU3AU6)r   r   r   r   r   r   r   r   )r%   r&   r   r   )r0   r&   r   r1   )r0   r4   r   r   )r8   r   r   r   )r   r   r?   r   r   rA   )r8   r   rI   rJ   r   r1   )rI   rJ   rt   r   r   ru   )
r€   ru   r�   ru   r‚   ru   rƒ   ru   r   rA   )r‚   ru   rƒ   ru   rˆ   ru   r   rA   )
rI   r‹   rŒ   ru   r�   ru   rˆ   ru   r   rA   )r�   rA   r‘   ru   r’   r“   r”   ru   r•   ru   r–   rA   r   rA   rß   )rt   r   r¬   r   r_   r�   r­   r   r®   r¯   r°   r±   r©   r   rª   r²   r«   rA   r   r›   )4r§   Ú
__future__r   ÚloggingrT  r@  r  Údataclassesr   Útypingr   r   r   r   r	   Úagent.conversation_compressionr
   r   r   r   r   Úagent.context_enginer   Úagent.iteration_budgetr   Úagent.memory_managerr   Úagent.memory_providerr   Úagent.model_metadatar   r   Ú	getLoggerr¤   rn   r#   r/   r3   r7   r@   rF   Ú	frozensetrg   rs   r   r‡   rK  rŠ   r�   r™   r›   r¨  r†   r$   r"   ú<module>rµ     sð  ððð ð0 #Ð "Ð "Ð "Ð "Ð "à €€€Ø Ð Ð Ð Ø €€€Ø €€€Ø !Ð !Ð !Ð !Ð !Ð !Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5ðð ð ð ð ð ð ð ð ð ð ð ð ð ð EÐ DÐ DÐ DÐ DÐ DØ 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3ðð ð ð ð ð ð ð ð
 
ˆÔ	˜8Ñ	$Ô	$€ð 6ð  6ð  6ð  6ðFð ð ð ð.	!ð 	!ð 	!ð 	!ð-ð -ð -ð -ð3ð 3ð 3ð 3ð(ð ð ð ð@  �i ¨Ð 4Ñ5Ô5Ð ðEMð EMð EMð EMðPð ð ð ð@?ð ?ð ?ð ?ð6 7Ð ðð ð ð ð"Hð Hð Hð Hð8!ð !ð !ð !ðD ð0ð 0ð 0ð 0ð 0ñ 0ô 0ñ „ð0ðH /3ðtð 04Ø>Bð ð'tð tð tð tð tð tð tð tr$   