
    ,j                    j   U d Z 	 ddlZn# e$ r Y nw xY wddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl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 ddlm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! dd	l"m#Z# dd
l$m%Z% ddl&m'Z' ddl(m)Z) dZ*dZ+dZ,dZ- e	j.        d          Z/ e	j.        de	j0        e	j1        z            Z2 e	j.        de	j0                  Z3 e	j.        de	j0                  Z4 e	j.        de	j0                  Z5 e	j.        de	j0                  Z6 e	j.        d           e	j.        d           e	j.        d           e	j.        d           e	j.        d           e	j.        d          fZ7d.dZ8dede9fd Z:d!e;de<fd"Z=d#d$d%ee9ef         ddfd&Z>d'e9de9fd(Z?d'e9de9fd)Z@ e	j.        d*e	j0                  ZAd'e9de<fd+ZBded'e9de9fd,ZCded-e9d.e9dee9         fd/ZDde9fd0ZEd1 ZFd'e9dede9fd2ZGd3ZHd4edeeI         fd5ZJdeIfd6ZKd7e9d8eIdeIfd9ZLddd:d4ed;eeI         d<eeI         de<fd=ZMd>ZNeOe9d?f         ePd@<   dAe9dBedCee9ef         dee9ef         fdDZQdEZRdFZSdGZTdHee9         de<fdIZUddJdKeee9ef                  dHee9         deOeee9ef                  ee9         f         fdLZVd.edMee9         defdNZWdKeeee9ef                           defdOZXh dPZYdQZZ e	j.        dRe	j0                  Z[	 	 d/dSeee9ef                  dTe\dUee]         deOee9         e<f         fdVZ^d.dWZ_dXe9de9fdYZ`dXe9de9fdZZade<fd[Zbdefd\Zcde<fd]Zdd.d^Zed_ejf        d`<    e_             ejg        h                    d e9 eei          jj        jj                             ddalkmlZl ddblmmnZnmoZompZpmqZq  el            ZrddclsmtZt dddlumvZv erdez  Zw ever eei          x                                jy        df         dez  g           d.dhZz e	j.        di          Z{djdkhZ|erdlz  Z}e}~                                rw	 ddlZ ee}dmn          5 Z ej        e          pi Zddd           n# 1 swxY w Y   ddol&mZ  ee          Ze                                D ]1\  ZZ eee9e\eIe<f          reejf        vr e9e          ejf        e<   2e                    dpi           Zer eee          ri dqdrdsdtdudvdwdxdydzd{d|d}d~dddddddddddddddddddddddddddddZe                                D ]\  ZZeev ree         Zedsk    r e9e          dv r%edsk    r& eee9          rejg                            e          Z eeeef          r ej        e          ejf        e<   x e9e          ejf        e<   e                    di           Zer eee          rh dZ	 ddlmZ  e            D ]Ze                    ed                    n# e$ r Y nw xY weD ]DZe                    ei           Z eee          s& e9e                    dd                                                    Z e9e                    dd                                                    Z e9e                    dd                                                    Z e9e                    dd                                                    Ze                                Zeredk    reejf        de d<   ereejf        de d<   ereejf        de d<   ereejf        de d<   Fe                    di           Zer eee          rdev r e9ed                   ejf        d<   dev r e9ed                   ejf        d<   dev r e9ed                   ejf        d<   dev r e9ed                   ejf        d<   dev r e9ed                   ejf        d<   dev r e9ed                   ejf        d<   e                    di           Zerc eee          rWdev r e9ed                   ejf        d<   dev r e9ed                   ejf        d<   dev r e9ed                   ejf        d<   e                    dd          Zer( eee9          re                                ejf        d<   e                    di           Z eee          r<e                    dæ          Ze% e9e                                          ejf        d<   e                    di           Z eee          re                    dƦ          Zeerd_ndejf        d<   e                    dɦ          ZerP eee9          reZn5 eeeeOf          r%ej                            dʄ eD                       ZndZer
eejf        d<   e                    d̦          Zeerd_ndejf        d<   e                    dΦ          Ze e9e          ejf        d<   nJ# e$ rBZ ed ee          j         de ej        Ҧ            edej        Ҧ           Y dZ[ndZ[ww xY w	 ddlkmZ d e            v reni                     di           Z eee          r!e                    dצ          r edج٦           n'# e$ rZ ede ej        Ҧ           Y dZ[ndZ[ww xY w	 ddl&mZ  e             n'# e$ rZ ede ej        Ҧ           Y dZ[ndZ[ww xY w	 ddl&mZ  e             n'# e$ rZ ede ej        Ҧ           Y dZ[ndZ[ww xY wd_ejf        d<   d_ejf        d<   ejf                            dtd          Zeredv r2 ej        d          p e9 ej                              Zeejf        dt<   ddlmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZmZmZmZ ddlmZmZmZ ddlmZmZmZ  ej        e          Z e            ZdefdZdedz  fdZde9fdZde9de9de9de9fdZdeIde9fdZde9dee9         fdZde9dedz  fdZdZdZdZdZdZdZ ee                                e                                e                                e                                e                                e                                h          Zd.ee9         de<fdZdedeOe9dz  e9dz  f         fd Zde9de9dz  fdZddde9fdZde<fdZ defdZdefdZd0dedz  de9fd	Zdeee9                  fd
Zde9ddfdZdeddfdZddlZd a	ej
        ePd<   dddede9de\de9fdZdede<fdZdededefdZd1dZ G d deee֦          Zd d!d"ej        d#ej        d#eIddfd$Zd2d"ej        d&e\fd'Zd3dee         d)e<d*ee\         de<fd+Zd, Zed-k    r e             dS dS (4  a<  
Gateway runner - entry point for messaging platform integrations.

This module provides:
- start_gateway(): Start all configured platform adapters
- GatewayRunner: Main class managing the gateway lifecycle

Usage:
    # Start the gateway
    python -m gateway.run
    
    # Or from CLI
    python cli.py --gateway
    N)OrderedDict)copy_contextPath)datetime)DictOptionalAnyListUnion)fetch_account_usagerender_account_usage_lines)safe_schedule_threadsafe)t)cfg_get)get_fallback_chain         @      >@      @z'(?<![\w:/])/([A-Za-z0-9][A-Za-z0-9_-]*)u&  (auxiliary\s+.+\s+failed|compression\s+summary\s+failed|fallback\s+context\s+marker|configured\s+compression\s+model\s+.+\s+failed|no\s+auxiliary\s+llm\s+provider\s+configured|auto-lowered\s+compression\s+threshold|compacting\s+context\s+[—-]\s+summarizing\s+earlier\s+conversation|preflight\s+compression|rate\s+limited\.\s+waiting\s+\d|retrying\s+in\s+\d|max\s+retries\s+\(\d+\).*(?:trying\s+fallback|exhausted|invalid\s+responses)|stream\s+(?:drop|drop\s+mid\s+tool-call).+retry\s+\d|stale\s+connections\s+from\s+a\s+previous\s+provider\s+issue)z(api\s+(?:call\s+)?failed|provider\s+authentication\s+failed|non-retryable\s+error|rate\s+limited\s+after\s+\d+\s+retries|error\s+code\s*:|\bhttp\s*\d{3}\b|incorrect\s+api\s+key|invalid\s+api\s+key)z(cybersecurity\s+risk|security\s+policy|safety\s+policy|policy\s+violation|violat(?:e|es|ed|ion)|blocked\s+(?:because|by|under)|request\s+(?:was\s+)?(?:blocked|rejected)|disallowed|moderation)zV(provider\s+authentication\s+failed|incorrect\s+api\s+key|invalid\s+api\s+key|\b401\b)z7(rate\s+limit|rate-limited|\b429\b|quota|usage\s+limit)z%\bsk-[A-Za-z0-9][A-Za-z0-9_\-]{12,}\bz\bgh[pousr]_[A-Za-z0-9_]{20,}\bz!\bxox[baprs]-[A-Za-z0-9\-]{20,}\bz\bhf_[A-Za-z0-9]{20,}\bz\bglpat-[A-Za-z0-9_\-]{20,}\bz'(?i)\b(Bearer\s+)[A-Za-z0-9._\-]{20,}\breturnc                     t           j        dk    rdS t          t                                                    j        j        } g }t          j                            d          r2|	                    t          t          j        d                              |	                    | dz             t                      }|D ]}	 |                                }n# t          $ r |}Y nw xY wt          |                                          }||v rQ|                    |           |dz  dz  }|                                st          |           }t          |          }|t           j        vr t           j                            d|           t%          j        |           |t           j        v rt           j                            |           t           j        rt           j        d         |k    rdnd}	t           j                            |	|           t          |          t          j        d<   ||g}
t          j                            d	          r%|
	                    t          j        d	                    t          j                            t.                              |
                    t          j        d	<    dS dS )
a  Make detached Windows gateway runs see the Hermes venv packages.

    Some Windows restart paths run the gateway under uv's base ``pythonw.exe``
    to avoid the venv launcher respawning a visible console interpreter.  That
    mode can import the source tree via cwd/PYTHONPATH but still miss optional
    packages installed only in ``venv/Lib/site-packages`` (notably the MCP SDK).
    Patch the live process before MCP discovery so tool injection does not
    depend on every launcher preserving PYTHONPATH perfectly.
    win32NVIRTUAL_ENVvenvLibsite-packagesr      
PYTHONPATH)sysplatformr   __file__resolveparentosenvirongetappendsetOSErrorstrloweraddexistspathinsertsite
addsitedirremovepathsepjoindictfromkeys)project_root
candidatesseenvenv_dirresolved_venvvenv_keysite_packagesproject_entry
site_entry	insert_at
pythonpaths              0/home/ubuntu/.hermes/hermes-agent/gateway/run.py$_ensure_windows_gateway_venv_importsrD      si    |w>>))++29LJ	z~~m$$ ;$rz-899:::lV+,,,UUD  	%$,,..MM 	% 	% 	%$MMM	%}%%++--t%-?##%% 	L))''
((HOOA}--- 	
###!!HOOJ'''Ichqk]&B&BAA		:...$'$6$6
=!#Z0
:>>,'' 	8bj6777#%:??4==3L3L#M#M
< ? s   CC&%C&r!   c                     t          t          | d|           pd                                                                          S )zDReturn a normalized gateway platform value for enums or raw strings.value )r+   getattrstripr,   r!   s    rC   _gateway_platform_valuerK      s9    wx(339r::@@BBHHJJJ    excc                     t                      }| }d}h d}|d|dk     r^t          |          }||v rnJ|                    |           |dz  }t          |          j        }||v rdS |j        p|j        }||dk     ^dS )uh  Return True for transient network errors safe to log + swallow.

    The crash class targeted by #31066 / #31110: an unhandled Telegram
    ``TimedOut`` (or peer ``NetworkError`` / ``httpx`` connection error)
    propagating to the event loop and killing the entire gateway
    process. These are by definition transient — the next poll cycle or
    user action recovers — so they must never crash the process.

    Walk the exception cause chain so wrapped errors (e.g. PTB's
    ``NetworkError`` wrapping ``httpx.ConnectError``) are still
    classified. The chain is bounded to avoid pathological cycles.
    r   >   TimedOut	ReadError
WriteErrorPoolTimeoutReadTimeoutConnectErrorNetworkErrorWriteTimeoutClientOSErrorConnectTimeoutRemoteProtocolErrorClientConnectorErrorServerDisconnectedErrorN   r   TF)r)   idr-   type__name__	__cause____context__)rM   r:   curdepthtransient_class_namesidentnames          rC   _is_transient_network_errorrg      s     UUD#&CE   /ebjj3D==
Cyy!(((4m.s /ebjj 5rL   loopzasyncio.AbstractEventLoopcontextc           	      B   |                     d          }|t          |          r|                     d          pd}|                     d          p|                     d          }d}|T	 t          |d          r|                                nt	          |          }n# t
          $ r t	          |          }Y nw xY wt                              d	|pd
t          |          j	        |t          |          ||j
        f           dS |                     |           dS )a  Loop-level safety net for transient network errors.

    Installed once during :func:`start_gateway`. Catches the
    ``telegram.error.TimedOut`` crash class (issues #31066 / #31110)
    and any peer transient network error before it can kill the
    gateway process. Logs at WARNING with full traceback so the
    originating call site stays diagnosable; non-transient errors
    are forwarded to the default loop handler so real bugs still
    surface.
    	exceptionNmessageztransient network errorfuturetaskrG   get_namez9Gateway swallowed transient network error from %s: %s: %sz<unknown task>exc_info)r'   rg   hasattrro   repr	Exceptionloggerwarningr^   r_   __traceback__default_exception_handler)rh   ri   rM   rl   rn   	task_names         rC   _gateway_loop_exception_handlerrz      s-    ++k
"
"C
6s;;++i((E,E{{8$$;F(;(;	'/6tZ/H/HXDMMOOOdSWjj		 ' ' ' JJ			'G))II3iic&78 	 	
 	
 	
 	""7+++++s   -3B! !B=<B=textc                 j    t          | pd          }t          D ]}|                    d |          }|S )z?Best-effort secret redaction before text can leave the gateway.rG   c                 D    | j         r|                     d          nddz   S )Nr   rG   z
[REDACTED])	lastindexgroup)ms    rC   <lambda>z5_redact_gateway_user_facing_secrets.<locals>.<lambda>  s     *K!''!***|)[ rL   )r+   _GATEWAY_SECRET_PATTERNSsub)r{   redactedpatterns      rC   #_redact_gateway_user_facing_secretsr     sA    4:2H+ g g;;[[]effOrL   c                     t                               |           r	 dS t                              |           r	 dS t                              |           rdS 	 dS )z@Map raw provider/API errors to a short user-safe Telegram reply.uv   ⚠️ Provider authentication failed. Check the configured credentials; raw provider details are in the gateway logs.u   ⚠️ The model provider rejected the request. I kept the raw provider error out of chat; check gateway logs for details or try rephrasing.uX   ⏱️ The model provider is rate-limiting requests. Please wait a moment and try again.u|   ⚠️ The model provider failed after retries. I kept raw provider details out of chat; check gateway logs for diagnostics.)_GATEWAY_AUTH_ERROR_REsearch_GATEWAY_PROVIDER_POLICY_RE_GATEWAY_RATE_LIMIT_REr{   s    rC   _gateway_provider_error_replyr     s}    $$T** 
<	
 	
 #))$// 
S	
 	
 $$T** jii	; rL   z^\s*(\W*\s*)?(api\s+(?:call\s+)?failed|provider\s+authentication\s+failed|non-retryable\s+error|rate\s+limited\s+after\s+\d+\s+retries|error\s+code\s*:|http\s*\d{3}\b|incorrect\s+api\s+key|invalid\s+api\s+key)c                     | sdS t          |                                           }t          |          dk    s|                    d          dk    rdS t	          t
                              |                    S )uI  True when text is infrastructure/provider failure, not normal content.

    Two heuristics combined so the rewrite only fires on actual provider
    error envelopes, not on assistant prose that happens to mention an
    HTTP status code:

    1. The text is short — real provider errors are 1–3 lines of envelope
       text; assistant answers are usually longer.
    2. AND the error marker appears at the start of the message (optionally
       behind a punctuation/symbol prefix), not buried mid-paragraph in an
       explanation like "HTTP 404 means 'not found' — ...".
    F  
   )r+   rI   lencountbool _GATEWAY_PROVIDER_ERROR_SHAPE_REr   )r{   bodys     rC   "_looks_like_gateway_provider_errorr   @  so      ut99??D 4yy3$**T**Q..u077==>>>rL   c                     |s|S t          |           dk    r|S t          t          |                    }t          |          rt	          |          S |S )a&  Sanitize final gateway replies before sending them to high-noise chats.

    Telegram is Bob's mobile inbox, so it should receive concise, safe provider
    failure categories instead of raw HTTP bodies, request IDs, or policy text.
    Other platforms keep the existing behaviour for now.
    telegram)rK   r   r+   r   r   )r!   r{   r   s      rC    _sanitize_gateway_final_responser   W  s_      x((J6623t99==H)(33 7,X666OrL   
event_typerl   c                    t          |pd                                          }|sdS t          |           dk    r|S t          |          }t                              |          rdS t          |          rt          |          S |S )z@Filter/sanitize agent status callbacks before platform delivery.rG   Nr   )r+   rI   rK   r   _TELEGRAM_NOISY_STATUS_REr   r   r   )r!   r   rl   r{   s       rC   _prepare_gateway_status_messager   i  s    w}"##%%D tx((J66.t44D ''-- t)$// 3,T222KrL   c                 f    t          t          | dd          pd                                          S )u  Render an AgentNotice to a single plaintext line for messaging platforms.

    Messaging has no persistent status bar (unlike the TUI), so a notice is a
    one-shot standalone push. The notice policy already bakes the level glyph
    (⚠ / • / ✕ / ✓) into the text, and the TUI + CLI REPL render that text
    verbatim — so we emit it as-is here too. Prepending a per-level glyph would
    DOUBLE it ("⚠ ⚠ Credits 90% used", "⛔ ✕ Credit access paused"). Plaintext
    only — no markdown — so it renders uniformly across Telegram/Discord/Slack/
    SMS without per-platform escaping. Fail-soft: a malformed/empty notice
    degrades to "" rather than raising on the agent's callback path.
    r{   rG   )r+   rH   rI   )notices    rC   render_notice_liner   y  s/     wvvr**0b1177999rL   c                    K   t          | dd          }t          |          r |||||           d{V S |                     |||           d{V S )a5  Route a status message through adapter.send_or_update_status when supported.

    Issue #30045: adapters that implement send_or_update_status (currently
    Telegram) edit the previous bubble for the same status_key instead of
    appending a new one. Adapters without the method fall back to plain send.
    send_or_update_statusNmetadata)rH   callablesend)adapterchat_id
status_keycontentr   senders         rC   _send_or_update_status_coror     s       W5t<<F MVGZ8LLLLLLLLLLgwBBBBBBBBBrL   c                     t          |d|          }|dk    r| S ddlm dt          j        t
                   dt
          ffd}t                              ||           S )a%  Rewrite slash-command mentions to Telegram-valid command names.

    Telegram Bot API command names allow only lowercase letters, digits, and
    underscores.  Keep other platform renderings unchanged, but normalize
    Telegram help text so command mentions remain clickable/valid there.
    rF   r   r   )_sanitize_telegram_namematchr   c                 x     |                      d                    }|rd| n|                      d          S )Nr   /r   )r   )r   	sanitizedr   s     rC   _replacez/_telegramize_command_mentions.<locals>._replace  s<    ++EKKNN;;	"+?9Q?rL   )rH   hermes_cli.commandsr   reMatchr+   _TELEGRAM_COMMAND_MENTION_REr   )r{   r!   platform_valuer   r   s       @rC   _telegramize_command_mentionsr     s     Xw99N##;;;;;;@ @# @ @ @ @ @ @ (++Hd;;;rL     rF   c                 z   | dS t          | t                    r|                                 S t          | t                    rdS t          | t          t
          f          r4t          |           dk    rt          |           dz  nt          |           S t          | t                    r|                                 }|sdS 	 t          |          }|dk    r|dz  n|S # t          $ r Y nw xY w	 t          j	        |
                    dd                                                    S # t          $ r Y dS w xY wdS )a  Best-effort conversion of stored gateway timestamps to epoch seconds.

    Missing/unparseable timestamps return None so legacy transcripts keep the
    historical auto-continue behaviour instead of being silently dropped.
    Accepts: datetime, epoch seconds (int/float), epoch milliseconds (when
    the magnitude exceeds year-2286), ISO-8601 strings (with or without a
    trailing ``Z``), and numeric strings.
    Nl    d(	 g     @@Zz+00:00)
isinstancer   	timestampr   intfloatr+   rI   
ValueErrorfromisoformatreplace)rF   r{   numerics      rC   _coerce_gateway_timestampr     sX    }t%"" !   % t%#u&& X(-e~(E(EuU||f$$5QV<<W% {{}} 	4	DkkG'.'?'?7V##WL 	 	 	D		)$,,sH*E*EFFPPRRR 	 	 	44	4s$   C 
C,+C,09D* *
D87D8c                      t           j                            d          } | | dk    rt          t                    S 	 t          |           S # t
          t          f$ r t          t                    cY S w xY w)a  Return the configured auto-continue freshness window in seconds.

    Reads ``HERMES_AUTO_CONTINUE_FRESHNESS`` (bridged from
    ``config.yaml`` ``agent.gateway_auto_continue_freshness`` at gateway
    startup, same pattern as ``HERMES_AGENT_TIMEOUT``).  Falls back to the
    module default when unset or malformed.  Non-positive values disable
    the freshness gate (restores the pre-fix "always fresh" behaviour for
    users who want to opt out).
    HERMES_AUTO_CONTINUE_FRESHNESSNrG   )r%   r&   r'   r   %_AUTO_CONTINUE_FRESHNESS_SECS_DEFAULT	TypeErrorr   )raws    rC   _auto_continue_freshness_windowr     sz     *..9
:
:C
{cRii:;;;<Szzz" < < <:;;;;;<s   A %A43A4rf   defaultc                     t           j                            |           }||dk    rt          |          S 	 t          |          S # t          t
          f$ r t          |          cY S w xY w)zRead an env var as float, falling back to ``default`` on typos/empty.

    A misconfigured env var (e.g. ``HERMES_AGENT_TIMEOUT=abc``) must not
    crash the gateway or an agent turn.  Unset/empty also falls back.
    NrG   )r%   r&   r'   r   r   r   )rf   r   r   s      rC   
_float_envr     ss     *..

C
{cRiiW~~Szzz"   W~~s   A  A*)A*)nowwindow_secsr   r   c                    |t          |          nt          t                    }|dk    rdS t          |           }|dS |t          j                    n|}||z
  |k    S )a  Return True when an interruption marker is fresh enough to auto-continue.

    Unknown timestamps are treated as fresh for backward compatibility with
    legacy transcripts (pre-dating timestamp persistence) and with in-memory
    test scaffolding that constructs history entries without timestamps.

    A non-positive ``window_secs`` disables the gate (always fresh), which
    restores the pre-fix behaviour for users who opt out via config.
    Nr   T)r   r   r   time)rF   r   r   windowr   currents         rC   _is_fresh_gateway_interruptionr     sv    " " 	k899 
 {{t)%00It [dikkkcGY&((rL   )	reasoningreasoning_contentreasoning_detailscodex_reasoning_itemscodex_message_itemsfinish_reason._ASSISTANT_REPLAY_FIELDSroler   msgc                     | |d}| dk    r6t           D ].}||vr|                    |          }|dk    r|%n|s)|||<   /|S )aW  Build a replay entry for a non-tool-calling message, preserving the
    assistant fields the agent's API builders rely on for multi-turn fidelity.

    Lifted out of the inline ``run_sync`` closure so the field whitelist can
    be unit-tested in isolation.  Mirrors the ``_ASSISTANT_REPLAY_FIELDS``
    contract above.

    Empty values: most fields are dropped when falsy (matching the original
    PR #2974 behaviour) since an empty list/string for those carries no
    information.  The exception is ``reasoning_content``: DeepSeek/Kimi
    thinking-mode replay treats an empty string as a meaningful sentinel
    that ``_copy_reasoning_content_for_api`` upgrades to a single space.
    Dropping it here would make the gateway send no ``reasoning_content`` at
    all on the next turn, which can cause HTTP 400 from strict thinking
    providers.
    r   r   	assistantr   )r   r'   )r   r   r   entry_rkey_rvals         rC   _build_replay_entryr   E  s|    " &*g>>E{- 
	! 
	!ECGGENNE+++= !  E%LLLrL   zobserved Telegram group contextz>[Observed Telegram group context - context only, not requests]zh[Current addressed message - answer only this unless it explicitly asks you to use the observed context]channel_promptc                 2    t          | ot          | v           S )a  Return True for Telegram group turns that may include observed chatter.

    Telegram's observe-unmentioned mode persists skipped group chatter so a
    later @mention can see it. Those rows must not replay as ordinary user
    turns: a weak wake word like ``@bot cambio`` should not make the model treat
    old unmentioned chatter as pending work. The Telegram adapter marks these
    turns with a channel prompt; this helper keeps the run-path check explicit
    and unit-testable.
    )r   (_TELEGRAM_OBSERVED_CONTEXT_PROMPT_MARKERr   s    rC   %_uses_telegram_observed_group_contextr   k  s     ]#K~#]^^^rL   r   historyc                   g }g }t          |          }| pg D ]3}|                    d          }|s|dv r |dk    r'|                    d          }|rR|                    d          r=|dk    r7|r5|                    t          |                                                     d|v }d|v }	|d	k    }
|s|	s|
r4d
 |                                D             }|                    |           |rY|                    d          r|                    dd          }d| d| }t          |||          }|                    |           5d                    |                                          pd}||fS )a  Convert stored gateway transcript rows into agent replay messages.

    Observed Telegram group rows are returned as API-only context for the
    current addressed message instead of being replayed as normal prior user
    turns.  Keeping that context out of ``conversation_history`` avoids
    consecutive-user repair merging it with the live user turn and then hiding
    the current message behind ``history_offset`` during persistence.
    r   >   session_metasystemr   observeduser
tool_callstool_call_idtoolc                 "    i | ]\  }}|d v	||S )>   r   r    ).0kvs      rC   
<dictcomp>z0_build_gateway_agent_history.<locals>.<dictcomp>  s)    \\\$!QB[9[9[A9[9[9[rL   mirrormirror_sourcezanother sessionz[Delivered from ] r   N)r   r'   r(   r+   rI   itemsr   r5   )r   r   agent_historyobserved_group_contextseparate_observed_contextr   r   r   has_tool_callshas_tool_call_idis_tool_message	clean_msg
mirror_srcr   observed_contexts                  rC   _build_gateway_agent_historyr  y  s    +-M(* En U U}" "( "(wwv 	 $$$ 8'')$$$ 	)<)< 	T["))#g,,*<*<*>*>??? &,)S0&. 		(- 		( 		(\\#))++\\\I  ++++ 	(wwx   E WW_6GHH
DZDD7DD'gs;;E  '''yy!788>>@@HD***rL   r  c                    |s| S t            d| dt           d}t          | t                    r| |  S t          | t                    r{d | D             }|D ]Q}t          |t
                    r:|                    d          dk    r!| |                    dd           |d<   |c S Rd|                                dg|z   S | S )zDPrepend observed Telegram context to the API-only current user turn.r   

c                 Z    g | ](}t          |t                    rt          |          n|)S r   )r   r6   r   parts     rC   
<listcomp>z?_wrap_current_message_with_observed_context.<locals>.<listcomp>  s1    VVVdD$!7!7A4:::TVVVrL   r^   r{   rG   )r^   r{   )_OBSERVED_GROUP_CONTEXT_HEADER!_CURRENT_ADDRESSED_MESSAGE_HEADERr   r+   listr6   r'   rstrip)rl   r  prefixwrappedr  s        rC   +_wrap_current_message_with_observed_contextr    s      * 	1 	1	1 	1,	1 	1 	1  '3 $#'###'4   EVVgVVV 	 	D$%% $((6*:*:f*D*D"(@$((62*>*>@@V99:WDDNrL   c                     | sdS t          |           D ]Q}t          |t                    s|                    d          }|r|dv r4|                    d          }||c S  dS dS )uI  Return the ``timestamp`` of the last usable transcript row, if any.

    Skips metadata-only rows (``session_meta``, system injections) that are
    dropped before being handed to the agent.  Returns ``None`` when no
    usable row carries a timestamp — callers should treat that as "fresh"
    for backward compatibility.
    Nr   >   r   r   r   )reversedr   r6   r'   )r   r   r   tss       rC   _last_transcript_timestampr    s      t    #t$$ 	wwv 	t999WW[!!>III tt4rL   >   image_generatetext_to_speechtext_to_speech_tool)
host_imageimageagent_visible_imageMEDIA:((?:[A-Za-z]:[/\\]|/|~\/)\S+\.(?:png|jpe?g|gif|webp|mp4|mov|avi|mkv|webm|ogg|opus|mp3|wav|m4a|flac|epub|pdf|zip|rar|7z|docx?|xlsx?|pptx?|txt|csv|apk|ipa))messageshistory_offsethistory_media_pathsc                    |pt                      }|rt          |           |k    r| |d         }n| }i }|D ]}|                    d          dk    r|                    d          pg D ]}|                    d          p|                    d          }|                    d          pi }t          |                    d          p|                    d          pd	          }	|r|	r|	|t          |          <   g }
d
}|D ]}|                    d          dvrt          |                    d          p|                    d          pd	          }|                    |          t          vrqt          |                    d          pd	          }|                    |          }|dk    rd|vr	 t          j        |          }n# t          $ r d}Y nw xY wt          |t                    r|                    d          rot          D ]g}|                    |          }t          |t                    r;t                              d|           r||vr|
                    d|             nhwd|vr}t                              |          D ]Z}|                    d                                                              d          }|r||vr|
                    d|            [d|v rd}|
|fS )a9  Collect real media tags from current-turn producer-tool results only.

    Two layered guards keep stale/example MEDIA: strings out of the reply:

    1. Producer-tool allowlist: only tools that intentionally emit deliverable
       artifacts (TTS) are eligible. Documentation, logs, and search results can
       contain example strings such as MEDIA:/absolute/path/to/file, which must
       never be delivered as attachments. (Fixes the original report behind #16721.)
    2. Current-turn isolation: only messages produced this turn are scanned, so a
       tool result from an earlier turn (still present in the full message list)
       cannot leak onto a later text-only reply (#34608).

    Mid-run context compression can rewrite/shrink the message list below the
    original history length. When that happens the slice boundary is no longer
    trustworthy, so fall back to scanning every message and rely on
    ``history_media_paths`` for dedup, preserving the compression-safe behaviour
    of #160. The producer-tool allowlist still applies on the fallback path.
    Nr   r   r   r]   call_idfunctionrf   rG   F)r   r&  r   r   r  MEDIA:successr   ",}[[audio_as_voice]]T)r)   r   r'   r+   _AUTO_APPEND_MEDIA_TOOL_NAMESjsonloadsrt   r   r6   _JSON_MEDIA_TOOL_PATH_FIELDS_TOOL_MEDIA_RE	fullmatchr(   finditerr   rI   r  )r!  r"  r#  new_messagestool_name_by_call_idr   callr%  fnrf   
media_tagshas_voice_directiver   	tool_namepayloadfieldr/   r   s                     rC   _collect_auto_append_media_tagsr;    s^   . .6   #h-->990+- : :776??k))GGL))/R 	: 	:Dhhtnn;(;(;G*%%+Brvvf~~?&)9)9?R@@D :4 :59$S\\2	: J  '  '776??"666cggn--I1C1CIrJJ##G,,4QQQcggi((.B//(,,W55	 (((XW-D-D*W--   '4(( W[[-C-C 9  E";;u--D"4--  . 8 8$ I I $,? ? ?"))/4//:::7""#,,W55 	3 	3E;;q>>''))0077D 3$777!!/4//2227**"&***s   9GGGc                     t           j                            d          } | rnt           j                            |           rdS t          j        t                                        d|            t           j        	                    dd           ddl
}|                                }|j        |j        fD ]5}|r1t           j                            |          r|t           j        d<    dS 6	 ddl}|                                t           j        d<   dS # t           $ r Y nw xY wdD ]3}t           j                            |          r|t           j        d<    dS 4dS )a  Set SSL_CERT_FILE if the system doesn't expose CA certs to Python.

    Windows startup paths (Desktop, Scheduled Tasks, installer children) can
    occasionally inherit a stale SSL_CERT_FILE. Returning just because the
    variable is present makes every later httpx/OpenAI client construction fail
    with FileNotFoundError from ssl.load_verify_locations(). Treat a missing
    path as unset and fall back to certifi instead.
    SSL_CERT_FILENz?Ignoring stale SSL_CERT_FILE=%r because the path does not existr   )z"/etc/ssl/certs/ca-certificates.crtz /etc/pki/tls/certs/ca-bundle.crtz1/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pemz/etc/ssl/ca-bundle.pemz/etc/ssl/cert.pemz/etc/pki/tls/cert.pemz#/usr/local/etc/openssl@1.1/cert.pemz&/opt/homebrew/etc/openssl@1.1/cert.pem)r%   r&   r'   r/   r.   logging	getLoggerr_   rv   popsslget_default_verify_pathscafileopenssl_cafilecertifiwhereImportError)configured_certrA  paths	candidaterE  s        rC   _ensure_ssl_certsrK  Y  ss    jnn_55O .7>>/** 	F(##++M	
 	
 	
 	
---JJJ ((**ElE$89  	 		22 	*3BJ'FF&-mmoo
?#   	  	 7>>)$$ 	*3BJ'FF	 s   -%D 
D! D!platform_namec                 Z    ddl m}  ||           }|r|S |                                  dS )a  Return the configured home-target env var for a platform.

    Consults built-in ``_HOME_TARGET_ENV_VARS`` first, then the plugin
    registry via ``cron.scheduler._resolve_home_env_var``, then falls back
    to ``<PLATFORM>_HOME_CHANNEL`` for unknown names.
    r   )_resolve_home_env_var_HOME_CHANNEL)cron.schedulerrN  upper)rL  rN  resolveds      rC   _home_target_env_varrS    sN     544444$$]33H !!##2222rL   c                 &    t          |            dS )zDReturn the optional thread/topic env var for a platform home target.
_THREAD_ID)rS  )rL  s    rC   _home_thread_env_varrV    s    "=11====rL   c                  :    t           dz                                  S )zIReturn True when a /restart completion marker is waiting to be delivered..restart_notify.json)_hermes_homer.   r   rL   rC   _restart_notification_pendingrZ    s    1199;;;rL   c                      t           dz  S )Nz.restart_pending.json)rY  r   rL   rC   "_planned_restart_notification_pathr\    s    111rL   c                  B    t                                                      S )zHReturn True when a non-chat planned restart should notify home channels.)r\  r.   r   rL   rC   %_planned_restart_notification_pendingr^    s    -//66888rL   c                  J    t                                          d           d S )NT
missing_ok)r\  unlinkr   rL   rC   #_clear_planned_restart_notificationrc    s%    &((//4/@@@@@rL   1_HERMES_GATEWAY)get_hermes_home)atomic_json_writeatomic_yaml_writebase_url_host_matchesis_truthy_value)load_dotenv)load_hermes_dotenv.envr   hermes_homeproject_envc                  F   t          t          t          t                                                    j        d         dz             t          dz  } |                                 sdS 	 ddl}t          | d          5 } |j	        |          pi }ddd           n# 1 swxY w Y   dd	l
m}  ||          }n# t          $ r Y dS w xY w|                    d
i           }t          |t                    r(d|v r&t!          |d                   t"          j        d<   dS dS dS )a~  Reload .env for fresh credentials without letting stale .env override config.

    Gateway processes are long-lived, so per-turn code reloads ~/.hermes/.env to
    pick up rotated API keys. config.yaml remains authoritative for agent budget
    settings such as agent.max_turns; otherwise a stale HERMES_MAX_ITERATIONS in
    .env can replace the startup bridge on later turns.
    r   rm  rn  config.yamlNr   utf-8encoding_expand_env_varsagent	max_turnsHERMES_MAX_ITERATIONS)rl  rY  r   r"   r#   parentsr.   yamlopen	safe_loadhermes_cli.configrw  rt   r'   r   r6   r+   r%   r&   )config_path_yamlfcfgrw  	agent_cfgs         rC   /_reload_runtime_env_preserving_config_authorityr    s     NN**,,4Q7&@   
 .K +000 	+A!%/!$$*C	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+666666s##    $$I)T"" J{i'?'?.1)K2H.I.I
*+++J J'?'?s6   *B; ?BB; B""B; %B"&B; ;
C	C	z=^(?P<host>.+):(?P<container>/[^:]+?)(?::(?P<options>[^:]+))?$z/outputz/outputsrr  rs  rt  rv  terminalbackendTERMINAL_ENVcwdTERMINAL_CWDtimeoutTERMINAL_TIMEOUTlifetime_secondsTERMINAL_LIFETIME_SECONDSdocker_imageTERMINAL_DOCKER_IMAGEdocker_forward_envTERMINAL_DOCKER_FORWARD_ENVsingularity_imageTERMINAL_SINGULARITY_IMAGEmodal_imageTERMINAL_MODAL_IMAGEdaytona_imageTERMINAL_DAYTONA_IMAGEssh_hostTERMINAL_SSH_HOSTssh_userTERMINAL_SSH_USERssh_portTERMINAL_SSH_PORTssh_keyTERMINAL_SSH_KEYcontainer_cpuTERMINAL_CONTAINER_CPUcontainer_memoryTERMINAL_CONTAINER_MEMORYcontainer_diskTERMINAL_CONTAINER_DISKcontainer_persistentTERMINAL_CONTAINER_PERSISTENTTERMINAL_DOCKER_VOLUMESTERMINAL_DOCKER_ENV&TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE TERMINAL_DOCKER_RUN_AS_HOST_USER(TERMINAL_DOCKER_PERSIST_ACROSS_PROCESSESTERMINAL_DOCKER_ORPHAN_REAPERTERMINAL_SANDBOX_DIRTERMINAL_PERSISTENT_SHELL)docker_volumes
docker_envdocker_mount_cwd_to_workspacedocker_run_as_host_userdocker_persist_across_processesdocker_orphan_reapersandbox_dirpersistent_shell>   r  auto.	auxiliary>   visionapprovalweb_extract)get_plugin_auxiliary_taskskeyproviderrG   modelbase_urlapi_keyr  
AUXILIARY_	_PROVIDER_MODEL	_BASE_URL_API_KEYrx  ry  rz  gateway_timeoutHERMES_AGENT_TIMEOUTgateway_timeout_warningHERMES_AGENT_TIMEOUT_WARNINGgateway_notify_intervalHERMES_AGENT_NOTIFY_INTERVALrestart_drain_timeoutHERMES_RESTART_DRAIN_TIMEOUTgateway_auto_continue_freshnessr   displaybusy_input_modeHERMES_GATEWAY_BUSY_INPUT_MODEbusy_text_modeHERMES_GATEWAY_BUSY_TEXT_MODEbusy_ack_enabledHERMES_GATEWAY_BUSY_ACK_ENABLEDtimezoneHERMES_TIMEZONEsecurityredact_secretsHERMES_REDACT_SECRETSgatewaystrict0HERMES_MEDIA_DELIVERY_STRICTmedia_delivery_allow_dirsc              #   8   K   | ]}|t          |          V  d S Nr+   r   ps     rC   	<genexpr>r    s.      5W5WUV5Wc!ff5W5W5W5W5W5WrL   HERMES_MEDIA_ALLOW_DIRStrust_recent_filesHERMES_MEDIA_TRUST_RECENT_FILEStrust_recent_files_seconds!HERMES_MEDIA_TRUST_RECENT_SECONDSu.     Warning: config.yaml → env bridge failed: : )filezz  Gateway will fall back to .env values, which may not match your current config.yaml. Run `hermes doctor` to investigate.)apply_ipv4_preference_cfgnetwork
force_ipv4Tforcez/  Warning: IPv4 preference application failed: )print_config_warningsz%  Warning: config validation failed: )warn_deprecated_cwd_env_varsz%  Warning: deprecation check failed: HERMES_QUIETHERMES_EXEC_ASKMESSAGING_CWD)Platform_BUILTIN_PLATFORM_VALUESGatewayConfigHomeChannelPlatformConfigload_gateway_config)SessionStoreSessionSourceSessionContextbuild_session_contextbuild_session_context_promptbuild_session_keyis_shared_multi_user_session)DeliveryRouter)GatewayAuthorizationMixin)GatewayKanbanWatchersMixin)GatewaySlashCommandsMixin)BasePlatformAdapterEphemeralReplyMessageEventMessageType_reply_anchor_for_eventmerge_pending_message_event)%DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUT!GATEWAY_SERVICE_RESTART_EXIT_CODEparse_restart_drain_timeout)canonical_whatsapp_identifierexpand_whatsapp_aliasesnormalize_whatsapp_identifierc            
      ^   ddl m} m}m} ddlm}m} 	  |             }n# |$ rw} ||          rt                              d|           nt                              d|           t                      }||cY d}~S t           ||                    |d}~wt          $ r}t           ||                    |d}~ww xY w |            }	d}
t          j                            d          }|r*	 t          |          }
nZ# t           t"          f$ r d}
Y nEw xY wt%          |	t&                    r,|	                    d          }t%          |t                    r|}
|
2|                    d	          }t%          |t                    r|dk    r|}
|                    d
          |                    d          |                    d          |                    d          |                    d          t)          |                    d          pg           |                    d          |
dS )u5  Resolve provider credentials for gateway-created AIAgent instances.

    Provider is read from ``config.yaml`` ``model.provider`` (the single
    source of truth). ``resolve_runtime_provider()`` falls through to env
    var lookups internally for legacy compatibility, but the gateway does
    not consult environment variables for behavioral config — config.yaml
    is authoritative.

    If the primary provider fails with an authentication error, attempt to
    resolve credentials using the fallback provider chain from config.yaml
    before giving up.
    r   )resolve_runtime_providerformat_runtime_provider_error_get_model_config)	AuthErroris_rate_limited_auth_erroru;   Primary provider rate-limited (429): %s — trying fallbacku4   Primary provider auth failed: %s — trying fallbackNHERMES_MAX_TOKENS
max_tokensmax_output_tokensr  r  r  api_modecommandargscredential_poolr  r  r  r"  r#  r$  r%  r   )hermes_cli.runtime_providerr  r  r  hermes_cli.authr  r  ru   rv   _try_resolve_fallback_providerRuntimeErrorrt   r%   r&   r'   r   r   r   r   r6   r  )r  r  r  r  r  runtimeauth_exc	fb_configrM   	model_cfgr   _env_mtmt_runtime_mots                 rC   _resolve_runtime_agent_kwargsr2    s            
 FEEEEEEEH**,, R R R
 &%h// 	]NNXZbccccNNQS[\\\244	 88BBCCQ H H H88==>>CGH "!##IJjnn011G 	WJJI& 	 	 	JJJ		It	$	$ ]]<((b# 	J {{#677lC(( 	&\A-=-=%J ;;y))KK
++KK
++KK
++;;y))W[[((.B//";;'899 	 	 	s?   
 CAB7C=BC#B<<C2D DDc                  V   ddl m}  	 ddl}t          dz  }|                                sdS t          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   t          |          }|sdS |D ]}	 |                    d          }|svt          |                    d          p|                    d	          pd
          
                                }|r)t          j        |d
          
                                pd} | |                    d          |                    d          |          }	t                              d|                    d          p|	                    d          |                    d                     |	                    d          |	                    d          |	                    d          |	                    d          |	                    d          t          |	                    d          pg           |	                    d          |                    d          dc S # t           $ r:}
t                              d|                    d          |
           Y d}
~
d}
~
ww xY wn# t           $ r Y nw xY wdS )zQAttempt to resolve credentials from the fallback_model/fallback_providers config.r   )r  Nrr  rs  rt  r  key_envapi_key_envrG   r  r  )	requestedexplicit_base_urlexplicit_api_keyz'Fallback provider resolved: %s model=%sr  r"  r#  r$  r%  )r  r  r  r"  r#  r$  r%  r  zFallback entry %s failed: %s)r'  r  r|  rY  r.   r}  r~  r   r'   r+   rI   r%   getenvru   infor  rt   debug)r  _ycfg_path_fr  fb_listr   r8  r4  r+  fb_excs              rC   r)  r)  H  s   DDDDDD/-/   	4(W--- 	),,r""(bC	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)$S)) 	4 #	 #	E"#(99Y#7#7 ' R!		),,N		-0H0HNB egg   R+-9Wb+A+A+G+G+I+I+QT(22#ii
33&+ii
&;&;%5   =IIj))DW[[-D-DIIg&&    '{{955 'J 7 7 'J 7 7 'J 7 7&{{955 V!4!4!:;;'.{{3D'E'E"YYw//	 	 	 	 	    ;UYYz=R=RTZ[[[C#	H    4sj   "J J A!J !A%%J (A%)J ?J G	IJ 
J/J
J JJ 
J&%J&c                    g }t          | dd          pg }t          | dd          pg }t          |          D ]\  }}|t          |          k     r||         nd}|                    d          st          | dd          t          j        k    r|                    d| d           p|                    d	          r|                    d
| d           |                    d| d           d                    |          S )ag  Build a text placeholder for media-only events so they aren't dropped.

    When a photo/document is queued during active processing and later
    dequeued, only .text is extracted.  If the event has no caption,
    the media would be silently lost.  This builds a placeholder that
    the vision enrichment pipeline will replace with a real description.
    
media_urlsNmedia_typesrG   image/message_typez[User sent an image: ]audio/z[User sent audio: z[User sent a file: r   )rH   	enumerater   
startswithr  PHOTOr(   r5   )eventpartsrB  rC  iurlmtypes          rC   _build_media_placeholderrP  ~  s*    Ed339rJ%55;KJ'' 7 73"#c+&6&6"6"6ABH%% 	7)M)MQ\Qb)b)bLL77778888h'' 	7LL4c4445555LL5s555666699UrL   display_name
agent_pathrO  c                 P    |                     d          r	d|  d| dS d|  d| dS )uf  Context note prepended to a user turn when they attach a document.

    Text documents (``text/*``) have their content inlined upstream by the
    platform adapter, so the note just confirms that and records the path.

    Binary documents (PDF, DOCX, XLSX, …) cannot be inlined as text. The note
    must tell the agent to *extract* the text itself before answering — earlier
    wording ("Ask the user what they'd like you to do with it") steered the
    model into punting back to the user, which is why attached PDFs/DOCX looked
    "unreadable" to the agent even though it has the tools to read them.
    text/z![The user sent a text document: 'zC'. Its content has been included below. The file is also saved at: rF  z[The user sent a document: ''. It is saved at: u
  . Its text is not inlined here (it's a binary format such as PDF or DOCX). To read it, extract the document's text yourself — for example with the terminal tool or the ocr-and-documents skill — before answering, instead of asking the user to paste the contents.])rI  )rQ  rR  rO  s      rC   _build_document_context_noterV    si        
8 8 8*48 8 8	
	6| 	6 	6
 	6 	6 	6rL   secondsc                     t          t          |                     }|dk     rd}t          |d          \  }}t          |d          \  }}|r| d|dd|dS | d|dS )Nr   r   <   :02d)r   rounddivmod)rW  totalhoursremminutessecss         rC   _format_durationrc    s    gEqyyt$$JE33OOMGT 322'2222222""""""rL   r/   c                 J   K   t           j                                       d                                         }|dk    rG	 dt          f fd}t          j        |           d{V }t          |          S # t          $ r Y nw xY w|dv rG	 dt          f fd}t          j        |           d{V }t          |          S # t          $ r Y nw xY w	 t          j	        dd	d
dddd t
          j
        j        t
          j
        j        
  
         d{V }t          j        |                                d           d{V \  }}|j        dk    r@t          t	          |                                                                                    S n# t          $ r Y nw xY wdS )zSBest-effort duration probe. Returns formatted MM:SS / HH:MM:SS, or None on failure.r   z.wavr   c                      dd l } |                     d          5 }|                                }|                                pd}|t	          |          z  cd d d            S # 1 swxY w Y   d S )Nr   rbr   )waver}  
getnframesgetframerater   )rg  wfframesrater/   s       rC   _wav_durationz,_probe_audio_duration.<locals>._wav_duration  s    YYtT** 0b]]__F??,,1D!E$KK/0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0s   <A%%A),A)N)z.oggz.opusz.ogac                  T    ddl m}  t           |           j        j                  S )Nr   )OggOpus)mutagen.oggopusro  r   r:  length)ro  r/   s    rC   _ogg_durationz,_probe_audio_duration.<locals>._ogg_duration  s1    333333WWT]]/6777rL   ffprobe-verrorz-show_entrieszformat=durationz-ofz"default=noprint_wrappers=1:nokey=1)stdoutstderrr   r  r   )r%   r/   splitextr,   r   asyncio	to_threadrc  rt   create_subprocess_exec
subprocessPIPEwait_forcommunicate
returncodedecoderI   )r/   extrm  rb  rr  procrv  _s   `       rC   _probe_audio_durationr    s7     
'

4
 
 
#
)
)
+
+C
f}}
	05 0 0 0 0 0 0 !*=99999999D#D))) 	 	 	D	 '''	85 8 8 8 8 8 8 !*=99999999D#D))) 	 	 	D	
3tWo7H7%*73E3J
 
 
 
 
 
 
 
 

 "*4+;+;+=+=sKKKKKKKKK	?a#E&--//*?*?*A*A$B$BCCC      4s7   5A8 8
BB5C 
CCB=F 
F F session_keyc                 ,    |                      |          S )zConsume and return the full pending event for a session.

    Queued follow-ups must preserve their media metadata so they can re-enter
    the normal image/STT/document preprocessing path instead of being reduced
    to a placeholder string.
    )get_pending_message)r   r  s     rC   _dequeue_pending_eventr    s     &&{333rL   zStop requestedzSession reset requestedz Execution timed out (inactivity)zSSE client disconnectedzGateway shutting downzGateway restartingc                     | sdS d                     t          |                                                                                                                     }|t
          v S )z?Return True when an interrupt message is internal control flow.F )r5   r+   rI   splitr,   _CONTROL_INTERRUPT_MESSAGES)rl   
normalizeds     rC   _is_control_interrupt_messager    sV     u#g,,,,..446677==??J444rL   skill_mdc                 l   	 |                      dd          }n# t          $ r Y dS w xY w|                    d          sdS |                    dd          }|dk     rdS d	}|d|                                         D ]}|                                }|                    d
          r}|                    dd          d                                         }t          |          dk    r&|d         |d         k    r|d         dv r
|dd         }|                                } n|sdS |                                	                    dd          	                    dd          }dd	l
}|                    dd|          }|                    dd|                              d          }|sd	|fS ||fS )u  Derive the /command slug and declared frontmatter name from a SKILL.md.

    Matches the exact normalization used by
    :func:`agent.skill_commands.scan_skill_commands` so the slug here is the
    same string a user types after the leading ``/`` (e.g. a skill with
    frontmatter ``name: Stable Diffusion Image Generation`` resolves to
    ``stable-diffusion-image-generation`` — NOT the parent directory name,
    which is commonly shorter/different, e.g. ``stable-diffusion``).

    Using the directory name silently broke :func:`_check_unavailable_skill`
    for every skill whose directory name drifted from its frontmatter name
    (19 such skills on a standard install as of 2026-05), causing a generic
    "unknown command" response where a "disabled — enable with …" or
    "not installed — install with …" hint was expected.

    Returns ``(slug, declared_name)`` or ``(None, None)`` when the file
    can't be read or lacks a ``name:`` in its frontmatter.
    rs  r   )ru  errorsNNz---z
---   r   Nzname:rZ  r      >   "'r  -r  z
[^a-z0-9-]rG   z-{2,})	read_textrt   rI  find
splitlinesrI   r  r   r,   r   r   r   )r  r   enddeclared_nameliner   slug_res           rC   _skill_slug_from_frontmatterr  	  s   &$$gi$HH   zze$$ z
,,w
"
"C
Qwwz $M#))++  zz||??7## 	**S!$$Q'--//C3xx1}}Q3r7!2!2s1v7K7K!B$iIIKKME	  z  ((c22::3DDD77="d++D778S$''--c22D #]""s    
((command_namec                 \   |                                                      dd          }	 ddlm} ddlm}m}  |            } |            D ]f}|                                s|                    d          D ]9} ||          rt          |          \  }}	|r|	s%||k    r|	|v r
d|  dc c S :gdd	l
m}
 t          t                                                    j        j        } |
|d
z            }|                                r|                    d          D ]z} ||          rt          |          \  }}|s#||k    rQ|j                            |          }t#          |j                  }dd                    |           }d|  d| dc S {n# t(          $ r Y nw xY wdS )u  Check if a command matches a known-but-inactive skill.

    Returns a helpful message if the skill exists but is disabled or only
    available as an optional install. Returns None if no match found.

    The slug for each on-disk skill is derived from its frontmatter ``name:``
    (via :func:`_skill_slug_from_frontmatter`), NOT from its containing
    directory name — because the two can differ (e.g. directory
    ``stable-diffusion`` + frontmatter ``Stable Diffusion Image Generation``
    yields slug ``stable-diffusion-image-generation``). Matching on
    directory name would miss that slug entirely and fall through to the
    generic "unknown command" path.
    r  r  r   )_get_disabled_skill_names)get_all_skills_dirsis_excluded_skill_pathzSKILL.mdThe **zJ** skill is installed but disabled.
Enable it with: `hermes skills config`)get_optional_skills_dirzoptional-skillsz	official/r   zQ** skill is available but not installed.
Install it with: `hermes skills install `N)r,   r   tools.skills_toolr  agent.skill_utilsr  r  r.   rglobr  hermes_constantsr  r   r"   r#   r$   relative_tor  rL  r5   rt   )r  r  r  r  r  disabled
skills_dirr  r  r  r  	repo_rootoptional_dir	_declaredrelrL  install_paths                    rC   _check_unavailable_skillr  ;  s    ##%%--c377J,??????QQQQQQQQ,,.. .-// 	 	J$$&& &,,Z88  ))(33 &B8&L&L#m =  :%%-8*C*CB B B B     	=<<<<<NN**,,3:	..y;L/LMM   	(..z::  ))(33 ">x"H"Hi :%%"/55lCCC OOE#@sxx#@#@LS S SCOS S S   &    4s   BF 1C(F F 
F)(F)r  c                 4    | t           j        k    rdn| j        S )uN   Map a Platform enum to its config.yaml key (LOCAL→"cli", rest→enum value).cli)r  LOCALrF   rJ   s    rC   _platform_config_keyr  {  s    ..55HNBrL   c                      t                      } t          | ddg           }t          |t                    sdS d|v pd|v S )zAReturn True when the standalone Teams pipeline plugin is enabled.pluginsenabledr   Fteams_pipelinezteams-pipeline)_load_gateway_configr   r   r  )configr  s     rC   _teams_pipeline_plugin_enabledr    sP    !##FfiB???Ggt$$ uw&E*:g*EErL   c                     t           dz  } 	 ddlm}m} |  |            k    r
 |            S n# t          $ r Y nw xY w	 |                                 rEddl}t          | dd          5 }|                    |          pi cddd           S # 1 swxY w Y   n+# t          $ r t          
                    d|            Y nw xY wi S )	a  Load and parse ~/.hermes/config.yaml, returning {} on any error.

    Uses the module-level ``_hermes_home`` (so tests that monkeypatch it
    still see their fixture) and shares the mtime-keyed raw-yaml cache
    from ``hermes_cli.config.read_raw_config`` when the paths match.
    rr  r   )get_config_pathread_raw_configNrrs  rt  z%Could not load gateway config from %s)rY  r  r  r  rt   r.   r|  r}  r~  ru   r;  )r  r  r  r|  r  s        rC   r  r    sV    .K	FFFFFFFF
 //++++"?$$$ ,   K 	/KKKk3999 /Q~~a((.B/ / / / / / / / / / / / / / / / K K K<kJJJJJKIsA   - 
::*B (B?B BB BB %C ?C c                      t                      } t          | t                    r| si S ddlm}  ||           }t          |t                    r|ni S )u  Load gateway config for runtime reads, expanding supported ``${VAR}`` refs.

    Runtime helpers should honor the same env-template expansion documented for
    ``config.yaml`` while still respecting tests that monkeypatch
    ``gateway.run._hermes_home``. Build on ``_load_gateway_config()`` rather
    than calling the canonical loader directly so both behaviors stay aligned.

    Expansion failures are intentionally NOT swallowed — silently returning
    the unexpanded dict would mask the very bug this helper exists to fix.
    r   rv  )r  r   r6   r  rw  )r  rw  expandeds      rC   _load_gateway_runtime_configr    sh     
 
 Cc4    	222222$$H!(D11988r9rL   r  c                    | | nt                      }|                    di           }t          |t                    r|S t          |t                    r,|                    d          p|                    d          pdS dS )u   Read model from config.yaml — single source of truth.

    Without this, temporary AIAgent instances (e.g. /compress) fall
    back to the hardcoded default which fails when the active provider is
    openai-codex.
    Nr  r   rG   )r  r'   r   r+   r6   )r  r  r.  s      rC   _resolve_gateway_modelr    s     &&&,@,B,BC$$I)S!! H	It	$	$ H}}Y''G9==+A+AGRG2rL   c                      ddl } |                     d          }|r|gS 	 ddl}|j                            d          t
          j        ddgS n# t          $ r Y nw xY wdS )us  Resolve the Hermes update command as argv parts.

    Tries in order:
    1. ``shutil.which("hermes")`` — standard PATH lookup
    2. ``sys.executable -m hermes_cli.main`` — fallback when Hermes is running
       from a venv/module invocation and the ``hermes`` shim is not on PATH

    Returns argv parts ready for quoting/joining, or ``None`` if neither works.
    r   Nhermes
hermes_cliz-mzhermes_cli.main)shutilwhichimportlib.utilutil	find_specr    
executablert   )r  
hermes_bin	importlibs      rC   _resolve_hermes_binr    s     MMMh''J |>##L11=ND*;<< >    4s   ,A 
AAzdict | Nonec                 
   |                      d          }t          |          dk    rZ|d         dk    rN|d         dk    rB|d         |d         |d	         d
}t          |          dk    r|d         dv r|d         |d<   |S dS )a@  Parse a session key into its component parts.

    Session keys follow the format
    ``agent:main:{platform}:{chat_type}:{chat_id}[:{extra}...]``.
    Returns a dict with ``platform``, ``chat_type``, ``chat_id``, and
    optionally ``thread_id`` keys, or None if the key doesn't match.

    The 6th element is only returned as ``thread_id`` for chat types where
    it is unambiguous (``dm`` and ``thread``).  For group/channel sessions
    the suffix may be a user_id (per-user isolation) rather than a
    thread_id, so we leave ``thread_id`` out to avoid mis-routing.
    rZ     r   rx  r   mainr  r  r   )r!   	chat_typer   >   dmthread	thread_idN)r  r   )r  rL  results      rC   _parse_session_keyr    s     c""E
5zzQ58w..58v3E3EaqQx
 

 u::>>eAh*:::"'(F;4rL   evtz
str | Nonec                    |                      dd          }|                      dd          }|                      dd          }|dk    rd|                      dd	           d
S |dk    rb|                      dd          }|                      dd	          }|                      dd          }d| d| d| d| }|r	|d| dz  }|d
z  }|S dS )zOFormat a watch pattern event from completion_queue into a [IMPORTANT:] message.r^   
completion
session_idunknownr#  watch_disabledz[IMPORTANT: rl   rG   rF  watch_matchr   ?output
suppressedr   [IMPORTANT: Background process z matched watch pattern "z".
Command: z
Matched output:
z
(z/ earlier matches were suppressed by rate limit)Nr'   )r  evt_type_sid_cmd_pat_out_supr{   s           rC   $_format_gateway_process_notificationr    s   wwv|,,H77<++D779i((D###7cggi447777=  wwy#&&wwx$$ww|Q'''d ' '#' '' ' !%' ' 	  	PO$OOOOD4rL   c                      d S r  r   r   rL   rC   r   r     s    D rL   _gateway_runner_refhistory_lenagent_resultresponser  c                D   |r|S |                      d          r||                      dd          }t          |                                          t          fddD                       p	dv o|dk    }|r	 dS d	t          |          d
d          dS t	          |                      dd          pd          }|dk    r^|                      d          sI|                      d          r1|                      dd          }dt          |          d
d          dS 	 dS |S )zNormalize empty/None agent responses into user-facing messages.

    Consolidates the existing ``failed`` handler and adds a catch-all for
    the case where the agent did work (api_calls > 0) but returned no text.
    Fix for #18765.
    failedru  unknown errorc              3       K   | ]}|v V  	d S r  r   )r   r  	error_strs     rC   r  z2_normalize_empty_agent_response.<locals>.<genexpr>3  s;       !
 !
 N!
 !
 !
 !
 !
 !
rL   )ri   tokenz	too largeztoo longexceedr9  4002   }   ⚠️ Session too large for the model's context window.
Use /compact to compress the conversation, or /reset to start fresh.zThe request failed: N,  z2
Try again or use /reset to start a fresh session.	api_callsr   interruptedpartialzprocessing incompleteu   ⚠️ Processing stopped:    z. Try again.u|   ⚠️ Processing completed but no response was generated. This may be a transient error — try sending your message again.)r'   r+   r,   anyr   )r  r  r  error_detailis_context_failurer  errr   s          @rC   _normalize_empty_agent_responser  !  s     !! 
#''AA%%++--	  !
 !
 !
 !
W!
 !
 !
 
 
 7 y 5[2%5 	  	) @3|#4#4TcT#: @ @ @	

 L$$[!449::I1}}\--m<<}I&& 	N""7,CDDCMS$3$MMMMP	
 	

 OrL   c                    t          | t                    sdS |                     d          rdS |                     d          s*|                     d          s|                     d          rdS |                     d          du rdS dS )a  Return True only when a gateway turn really completed successfully.

    Restart recovery uses ``resume_pending`` as a durable marker for sessions
    interrupted during gateway drain.  A soft interrupt can still bubble out as
    a syntactically normal agent result with an empty final response; clearing
    the marker in that case loses the recovery signal and startup auto-resume
    has nothing to schedule.
    Fr  r  r	  ru  	completedT)r   r6   r'   )r  s    rC   '_should_clear_resume_pending_after_turnr  O  s     lD)) u&& u!! \%5%5i%@%@ LDTDTU\D]D] u$$--u4rL   current_resultfollowup_resultc                 F   t          |t                    s|S t          | t                    s|S |                     d          }|                    d          }t          |t                    s|S t          |t                    r||k    r|S t          |          }||d<   |S )ae  Carry the outer history offset through queued follow-up drains.

    ``_process_message_background()`` persists transcript rows only once, after the
    entire in-band queued-follow-up chain returns.  Each recursive ``_run_agent()``
    call advances ``history_offset`` to the history it received, so without
    correction the outermost persistence step sees only the *last* queued turn as
    "new" and silently drops earlier turns from the same drain chain.

    Preserve the earliest (outermost) history offset so the final transcript slice
    still includes every queued turn that ran during the chain.
    r"  )r   r6   r'   r   )r  r  current_offsetfollowup_offsetmergeds        rC   (_preserve_queued_followup_history_offsetr  c  s     ot,, nd++ #''(899N%))*:;;Onc** /3'' O~,M,M/""F-FMrL   r   BasePlatformAdapter | Nonec                    K   | dS 	 |                                   d{V  dS # t          $ rB t                              dt	          | dt          |           j                  d           Y dS w xY w)uT  Best-effort dispose for an adapter that never made it onto ``self.adapters``.

    The reconnect watcher in ``GatewayRunner._platform_reconnect_watcher``
    constructs a fresh adapter on every retry attempt. When the connect
    call fails — for any of the three reasons (non-retryable error,
    retryable error, exception during connect) — the adapter is dropped
    without ever being installed, so nothing else will call its
    ``disconnect()``. Any resources the adapter opened in ``__init__``
    (e.g. ``APIServerAdapter`` opens a SQLite ``ResponseStore`` that
    holds 2 fds — the db file and its WAL sidecar) stay open until
    garbage collection sweeps the unreachable object, which Python's
    cyclic GC does not do promptly for asyncio-bound objects with
    native handles. The cumulative leak is 2 fds × every retry at the
    300s backoff cap ≈ 12 fds/hour, and the default 2560-fd ulimit
    is exhausted in ~12h of continuous failure, after which every
    open() call on the gateway raises ``OSError: [Errno 24] Too many
    open files`` and the gateway becomes a zombie (#37011).

    This helper centralises the dispose-with-suppression so the three
    failure paths in the reconnect watcher can all call it without
    each one having to know that ``disconnect()`` may itself raise
    on a half-constructed adapter.

    ``adapter`` may be ``None``: the reconnect watcher initialises
    ``adapter = None`` before the ``try`` so the ``except Exception``
    arm can dispose a half-constructed object, and also early-returns
    here when ``_create_adapter()`` returned ``None``.
    Nz,Adapter dispose raised on unowned adapter %rrf   Trp   )
disconnectrt   ru   r;  rH   r^   r_   r   s    rC   _dispose_unused_adapterr    s      : 
  """"""""""" 
 
 
 	:GVT']]%;<< 	 	
 	
 	
 	
 	
 	

s   $ AA0/A0c                   0   e Zd ZU dZi Zeeef         ed<   dZ	eed<   dZ
eed<   eZeed<   dZee         ed<   d	Zeed
<   d	Zeed<   d	Zeed<   d	Zeed<   d	Zeed<   dZee         ed<   dZeej                 ed<   i Zeeeeef         f         ed<   i Zeeeeef         f         ed<   d>dee         fdZd?dZ d?dZ!defdZ"e#dz  Z$de%dedefdZ&deeef         fdZ'd?dZ(dededdfd Z)ded!eddfd"Z*d?d#Z+d?d$Z,defd%Z-defd&Z.defd'Z/e0defd(            Z1e0defd)            Z2e0dee         fd*            Z3e0dee         fd+            Z4d,edefd-Z5d,edefd.Z6 e7d/d0h          Z8d,edefd1Z9d,edefd2Z:d3Z;d,edefd4Z<defd5Z=defd6Z>d,edee         fd7Z?d,eddfd8Z@d,ed9eddfd:ZAd,edee         fd;ZBd,edefd<ZCdddd=d,ee         d>ee         d?eeD         deEeeDf         fd@ZFdAedBedCeDdeDfdDZGdEeHddfdFZId9eddfdGZJdefdHZKdefdIZLdefdJZMdefdKZNd>edLdMdEeddfdNZOd>edEedOedM         dedM         fdPZPddQd>edEedefdRZQeRdSedefdT            ZSd>edEedefdUZTdVedefdWZUd@dXee         dYee         ddfdZZVdddd[ded\ee         d]ee         d^ee         ddf
d_ZWd/d`d9eddfdaZXdefdbZYeRdeZeeef                  fdc            Z[eRdefdd            Z\eRdeDdz  fde            Z]eRdfedeEeef         fdg            Z^dddhd,ee         d>ee         deDdz  fdiZ_d>edjeeD         ddfdkZ`eRdedz  fdl            ZaeRdefdm            ZbeRdefdn            ZceRdefdo            ZdeRdefdp            ZeeRdefdq            ZfeRdeDfdr            ZgeRdehdz  fds            Zideeef         fdtZjdee         fduZkd>edee         fdvZld>ed,edeEeee         f         fdwZmeRdxedefdy            ZndzZod>ed{epddfd|Zqd{epd>edefd}Zrd~edeEeeef         ef         fdZsd9eddfdZtd?dZudeeef         ddfdZvdeddfdZwdZxdZydezddfdZ{defdZ|d>eddfdZ}d?dZ~d?dZd	d	ddededefdZ e7h d          Zd>defdZdefdZdAdeddfdZdeeef         ddfdZdBdefdZdefdZd?dZd	d	d	ddedededdfdZd?dZde%dedeeH         fdZdeddfdZd{epdee         fdZd{epd,edeZeeef                  dee         fdZd>edeZe         fdZd>eddfdZd>efdZdedefdZdefdZd,ededee         fdZd,ededehfdZd{epdefdZd{epdefdZd{epfdZdefdZdCdZd,ededdfdZd,ededdfdZded,ededdfdZeRd{epdee         fd            Zd{epdefdZd{epdefdZdeddfdÄZdedededefdǄZdededefdȄZ	 dDd{epdedehdedef
d̄Zd{epdeddfd΄Zded{epddfdτZ	 	 	 dEded,ddedee         deeZe                  deeZe                  ddfdքZd,edeDfdׄZd,eddfd؄Zd,eddfdلZdedefdۄZd,edVededdfd܄Zd,edefd݄Zd,edVededdfdބZdZd,edefdZdefdZd,edefdZd,edefdZd{epdedefdZd{epdefdZd{epdedededeeddf         f
dZd{epdedededee         f
dZdeeef         fdZ	 d>dee         deeeef                  fdZdddddee%         dee         dee         dee         dee         dEee         deeeef                  fdZeRddddee%         dee         dee         dee         dEee         defd            ZeRd{epdee         fd            ZdZ e7e%j        e%j        e%j        e%j        e%j        e%j        e%j        e%j        e%j        e%j        e%j        e%j        e%j        e%j        e%j        h          Zΐd?dZ	 	 	 dFdeded~eddfdZdefdZdeeEeeee         f                  fdZdddeezeEeeee         f                           dezeEeeee         f                  fd ZӐdedehfdZՐdehddfdZ֐d ZdefdZؐdedeZe         defd	Zِded
eZe         deEeeZe         f         fdZd>ededz  fdZېdeDfdZܐdedeDddfdZݐdeDddfdZdZߐdZeEed<   dZi ZeDeEeedz  f         eDeef         f         ed<   edeDeef         fd            ZedeDeef         fd            Zed?eDdz  deDfd            ZeR	 	 	 dEdBedeDdehdedeDdz  dedz  dedz  defd            Zd>edBedCeDdeEfd Zd>ed!edefd"Zdd#d>edee         defd$Zd>eddfd%Zd>edefd&Zd/d`d>ed9edefd'Zd>ed(edefd)ZdEed>ed(edz  ddfd*Zd+d,d>ed,ed-ed.ed/eddfd0Zd>eddfd1ZeRded2eddfd3            Zdeddfd4Zd?d5Zdefd6Zdee         fd7Z	 	 	 dEded8edeZeeef                  d,ddVed>edee         dee         deeef         fd9Z	 	 	 	 	 dGded8edeZeeef                  d,edVed>edee         d;edee         d<ee         deeef         fd=ZdS (H  GatewayRunnerz
    Main gateway controller.

    Manages the lifecycle of all platform adapters and routes
    messages to/from the agent.
    _running_agents_ts	interrupt_busy_input_mode_busy_text_mode_restart_drain_timeoutN
_exit_codeF	_draining_restart_requested_restart_task_started_restart_detached_restart_via_service_restart_command_source
_stop_task_session_model_overrides_session_reasoning_overridesr  c                 T   |pt                      | _        i | _        |                                  t	          j        |           a|                                 | _        | 	                                | _
        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        ddlm t;          | j        j        | j        fd          | _        tA          | j                  | _!        d| _"        d | _#        tI          j%                    | _&        d| _'        d| _(        d | _)        d | _*        d| _+        d| _,        d| _-        d| _.        d| _/        d| _0        d | _1        d | _2        i | _3        i | _4        i | _5        i | _6        i | _7        i | _8        i | _9        i | _:        i | _;        ty                      | _=        d| _>        dd l?}ty                      | _@        |A                                | _B        i | _C        i | _D        | E                                | _F        d | _G        d | _H        i | _I        i | _J        i | _K        dd lL}|M                    d          | _N        	 ddlOmP}  |d	           n# t          $ r Y nw xY w	 dd
lRmS}  |            }t          t          |ddd          pd          V                                W                                }t          t          |ddd                    }t          |ddd           }	|dk    r|s|	st          Z                    d           n,# t          $ r t          [                    dd           Y nw xY wd | _\        	 ddl]m^}
  |
            | _\        n2# t          $ r%}t          Z                    d|           Y d }~nd }~ww xY w| j\        	 dd
lRmS}  |            _                    d          pi }|_                    dd          r| j\        `                    t          |_                    dd                    t          |_                    dd                    t          |_                    dd                    | j        j                    n2# t          $ r%}t          [                    d!|           Y d }~nd }~ww xY w	 dd
lRmS}  |            _                    d"          pi }|_                    dd          rdd#lbmc}  |t          |_                    dd$                    t          |_                    dd                    t          |_                    d%d                    t          |_                    d&d'                    (           n2# t          $ r%}t          [                    d)|           Y d }~nd }~ww xY wdd*ldme}  |            | _f        dd+lgmh}  |            | _i        | j                                | _k        i | _l        t                      | _n        d S ),Nr   process_registryc                 .                         |           S r  )has_active_for_session)r  r2  s    rC   r   z(GatewayRunner.__init__.<locals>.<lambda>  s    0@0W0WX[0\0\ rL   )has_active_processes_fnF   r   )ensure_installed)log_failuresload_config	approvalsmodemanualr  r  tirith_enabledTr  r  a6  Gateway approvals.mode=manual with no automated risk assessor (security.tirith_enabled is false and auxiliary.approval is unset): dangerous commands and execute_code scripts will BLOCK until a human approves them in chat. Enable security.tirith_enabled or configure auxiliary.approval for unattended operation.z$approvals.mode startup check skippedrp   )	SessionDBz&SQLite session store not available: %ssessions
auto_pruneretention_daysZ   min_interval_hours   vacuum_after_prune)rB  rD  vacuumsessions_dirz%state.db auto-maintenance skipped: %scheckpoints)maybe_auto_prune_checkpoints   delete_orphansmax_total_size_mb  )rB  rD  rL  rM  z'checkpoint auto-maintenance skipped: %s)PairingStore)HookRegistry)or  r  adapters'_warn_if_docker_media_delivery_is_risky_weakrefrefr  _load_prefill_messages_prefill_messages_load_ephemeral_system_prompt_ephemeral_system_prompt_load_reasoning_config_reasoning_config_load_service_tier_service_tier_load_show_reasoning_show_reasoning_load_busy_input_moder#  _load_busy_text_moder$  _load_restart_drain_timeoutr%  _load_provider_routing_provider_routing_load_fallback_model_fallback_modeltools.process_registryr2  r  rH  session_storer	  delivery_router_running_gateway_looprz  Event_shutdown_event_exit_cleanly_exit_with_failure_exit_reasonr&  r'  r(  _signal_initiated_shutdownr)  r*  r+  r,  r-  _running_agentsr!  _active_session_leases_pending_messages_last_resolved_model_queued_events&_pending_native_image_paths_by_session_busy_ack_ts_session_run_generationr   _session_sources_session_sources_max	threading_agent_cacheLock_agent_cache_lockr.  r/  _active_profile_name_kanban_notifier_profile_teams_pipeline_runtime_teams_pipeline_runtime_error_pending_approvals_failed_platforms_update_prompt_pending	itertoolsr   _slash_confirm_countertools.tirith_securityr7  rt   r  r:  r+   r   rI   r,   r   ru   rv   r;  _session_dbhermes_stater?  r'   maybe_auto_prune_and_vacuumr   tools.checkpoint_managerrJ  gateway.pairingrO  pairing_storegateway.hooksrP  hooks_load_voice_modes_voice_mode_recent_voice_transcriptsr)   _background_tasks)selfr  
_threading
_itertoolsr7  _load_full_config	_appr_cfg
_appr_mode
_tirith_on_aux_approvalr?  e	_sess_cfgrM   	_ckpt_cfgrJ  rO  rP  r2  s                     @rC   __init__zGatewayRunner.__init__  s   5 3 5 5=?44666&l400 "&!<!<!>!>(,(J(J(L(L%!%!<!<!>!>!4466#88:: $ : : < <#88::&*&F&F&H&H#!%!<!<!>!>#88:: 	<;;;;;)K$dk$\$\$\$\
 
 
  .dk::BF&}""'+/)-"' +0'%*"!&$)!@D$26 024668#13 57! >@LN3.079$ DO==$'! 	'&&&7B}}!+!2!2 DF% HJ)(,(A(A(C(C%'+$<@* >@ BD 8:# 	'&&&&0&6&6q&9&9#	>>>>>>%00000 	 	 	D		PJJJJJJ))++I	;IIIUX eggeegg  gi=MW[\\\]]J#I{JPTUUUMX%%j%%C    	P 	P 	PLL?$LOOOOO	P  
	H......(y{{D 	H 	H 	H NNCQGGGGGGGG	H 'KNNNNNN..0044Z@@FB	==u55 $@@'*9==9I2+N+N'O'O+.y}}=QSU/V/V+W+W#IMM2F$M$MNN%)[%=	 A     K K KDcJJJJJJJJK	IJJJJJJ**,,00??E2I}}\511 QQQQQQ,,#&y}}5Eq'I'I#J#J'*9==9Mr+R+R'S'S#'	6F(M(M#N#N&))--8KS*Q*Q&R&R	     	I 	I 	ILLBCHHHHHHHH	I 	100000)\^^ 	/.....!\^^
 ,0+A+A+C+C Z\& '*eeso   4L 
LLB-O &O/.O/:P 
P?P::P?
CT 
U T;;U CX 
Y#YYr   c                    t           j        | j        vrdS t                      st                              d           dS 	 ddlm} n3# t          $ r&}t          	                    d|           Y d}~dS d}~ww xY w	  ||           }n3# t          $ r&}t          	                    d|           Y d}~dS d}~ww xY w|rt          
                    d           dS | j        r"t          	                    d| j                   dS dS )	u  Bind the Teams meeting pipeline runtime to Graph webhook ingress.

        No-op when the msgraph_webhook adapter isn't running or the
        teams_pipeline plugin isn't enabled — lets the gateway start cleanly
        whether or not the user has opted into the pipeline.
        Nz:Teams pipeline plugin is disabled; skipping runtime wiringr   )bind_gateway_runtimez(Teams pipeline runtime import failed: %sz(Teams pipeline runtime wiring failed: %sz7Teams pipeline runtime bound to msgraph webhook ingressz&Teams pipeline runtime unavailable: %s)r  MSGRAPH_WEBHOOKrQ  r  ru   r;  plugins.teams_pipeline.runtimer  rt   rv   r:  r  )r  r  rM   bounds       rC   _wire_teams_pipeline_runtimez*GatewayRunner._wire_teams_pipeline_runtime  sS    #4=88F-// 	LLUVVVF	KKKKKKK 	 	 	NNEsKKKFFFFF		((..EE 	 	 	NNEsKKKFFFFF	  	KKQRRRRR/ 	NN82    	 	s0   A 
A8A33A8<B 
B8B33B8c                    t          j        dd                                                                          dk    rdS | j                                        }d |D             }|sdS t          j        dd                                          }g }|rc	 t          j        |          }t          |t                    rd |D             }n,# t          $ r t                              dd	
           Y nw xY wd}|D ]A}t                              |          }|s|                    d          }	|	t           v rd	} nB|rdS t                              d           dS )a  Warn when Docker-backed gateways lack an explicit export mount.

        MEDIA delivery happens in the gateway process, so paths emitted by the model
        must be readable from the host. A plain container-local path like
        `/workspace/report.txt` or `/output/report.txt` often exists only inside
        Docker, so users commonly need a dedicated export mount such as
        `host-dir:/output`.
        r  rG   dockerNc                 \    g | ])}|t           j        t           j        t           j        hv'|*S r   )r  r  
API_SERVERWEBHOOKr  s     rC   r  zIGatewayRunner._warn_if_docker_media_delivery_is_risky.<locals>.<listcomp>  s3    xxxQqQYQdfnfv@w7w7wq7w7w7wrL   r  c                 V    g | ]&}t          |t                    t          |          'S r   )r   r+   r   r   s     rC   r  zIGatewayRunner._warn_if_docker_media_delivery_is_risky.<locals>.<listcomp>  s-    LLL!As9K9KLs1vvLLLrL   zACould not parse TERMINAL_DOCKER_VOLUMES for gateway media warningTrp   F	containeraF  Docker backend is enabled for the messaging gateway but no explicit host-visible output mount (for example '/home/user/.hermes/cache/documents:/output') is configured. This is fine if the model already emits host-visible paths, but MEDIA file delivery can fail for container-local paths like '/workspace/...' or '/output/...'.)r%   r9  rI   r,   r  get_connected_platformsr,  r-  r   r  rt   ru   r;  _DOCKER_VOLUME_SPEC_REr   r   $_DOCKER_MEDIA_OUTPUT_CONTAINER_PATHSrv   )
r  	connectedmessaging_platformsraw_volumesvolumesparsedhas_explicit_output_mountspecr   container_paths
             rC   rR  z5GatewayRunner._warn_if_docker_media_delivery_is_risky  s    9^R((..006688HDDFK7799	xx)xxx" 	Fi 92>>DDFF 	qqK00fd++ MLLvLLLG q q q`kopppppq %*! 	 	D*0066E "[[55N!EEE,0) F % 	FP	
 	
 	
 	
 	
s   5C &C43C4c                 L    	 ddl m}  |d          duS # t          $ r Y dS w xY w)z3Check if the hermes-agent-setup skill is installed.r   )_find_skillzhermes-agent-setupNF)tools.skill_manager_toolr  rt   )r  r  s     rC   _has_setup_skillzGatewayRunner._has_setup_skill	  sQ    	<<<<<<;344D@@ 	 	 	55	s    
##zgateway_voice_mode.jsonr!   r   c                     |j          d| S )z6Return a platform-namespaced key for voice mode state.rZ  rF   )r  r!   r   s      rC   
_voice_keyzGatewayRunner._voice_key!	  s    .,,7,,,rL   c                    	 t          j        | j                                                  }n$# t          t           j        t          f$ r i cY S w xY wt          |t                    si S h d}i }|	                                D ]>\  }}||vr
t          |          }d|vrt                              d|           9|||<   ?|S )N>   alloff
voice_onlyrZ  z}Skipping legacy unprefixed voice mode key %r during migration. Re-enable voice mode on that chat to rebuild the prefixed key.)r,  r-  _VOICE_MODE_PATHr  FileNotFoundErrorJSONDecodeErrorr*   r   r6   r   r+   ru   rv   )r  datavalid_modesr  r   r<  r  s          rC   r  zGatewayRunner._load_voice_modes%	  s    	:d3==??@@DD!4#7A 	 	 	III	 $%% 	I222!ZZ\\ 	 	MGT;&&g,,C#~~U  
 F3KKs   +. AAc                    	 | j         j                            dd           | j                             t	          j        | j        d                     d S # t          $ r&}t          	                    d|           Y d }~d S d }~ww xY w)NT)r{  exist_okr  indentzFailed to save voice modes: %s)
r  r$   mkdir
write_textr,  dumpsr  r*   ru   rv   )r  r  s     rC   _save_voice_modeszGatewayRunner._save_voice_modes?	  s    	@!(..td.KKK!,,
4+A666      	@ 	@ 	@NN;Q?????????	@s   AA 
B"BBr  c                 ,   t          |dd          }t          |t                    sdS |rT|                    |           t          |dd          }t          |t                    r|                    |           dS dS |                    |           dS )zBUpdate an adapter's in-memory auto-TTS suppression set if present._auto_tts_disabled_chatsN_auto_tts_enabled_chatsrH   r   r)   r-   discard)r  r   r   r  disabled_chatsenabled_chatss         rC   _set_adapter_auto_tts_disabledz,GatewayRunner._set_adapter_auto_tts_disabledH	  s     *DdKK.#.. 	F 	,w'''#G-FMMM--- /%%g...../ / ""7+++++rL   r  c                 ,   t          |dd          }t          |t                    sdS |rT|                    |           t          |dd          }t          |t                    r|                    |           dS dS |                    |           dS )zUpdate an adapter's per-chat auto-TTS opt-in set if present.

        Used for ``/voice on``/``/voice tts`` where the user explicitly wants
        auto-TTS even when ``voice.auto_tts`` is False globally.
        r  Nr  r  )r  r   r   r  r  r  s         rC   _set_adapter_auto_tts_enabledz+GatewayRunner._set_adapter_auto_tts_enabledV	  s      )BDII--- 	F 	+g&&&$W.H$OON.#.. 0&&w/////0 0 !!'*****rL   c                 v   t          |dd          }t          |t                    sdS t          |dd          }t          |dd          }t          |t                    st          |t                    sdS 	 ddlm}  |            }t          |                    d          pi                     dd	                    }n# t          $ r d	}Y nw xY wt          |d
          r||_
        |j         dt          |t                    rL|                                 |                    fd| j                                        D                        t          |t                    rN|                                 |                    fd| j                                        D                        dS dS )ae  Restore persisted /voice state into a live platform adapter.

        Populates three fields from config + ``self._voice_mode``:
          - ``_auto_tts_default``: global default from ``voice.auto_tts``
          - ``_auto_tts_enabled_chats``: chats with mode ``voice_only``/``all``
          - ``_auto_tts_disabled_chats``: chats with mode ``off``
        r!   Nr  r  r   r9  voiceauto_ttsF_auto_tts_defaultrZ  c              3      K   | ]9\  }}|d k    |                                |t                    d         V  :dS )r  NrI  r   r   r  r<  r  s      rC   r  zBGatewayRunner._sync_voice_mode_state_to_adapter.<locals>.<genexpr>	  sX       " "&/c45==S^^F%;%;= CKKLL! ===" "rL   c              3   ~   K   | ]7\  }}|d v 	|                               |t                    d         V  8dS )>   r  r  Nr  r  s      rC   r  zBGatewayRunner._sync_voice_mode_state_to_adapter.<locals>.<genexpr>	  s_       ! !&/c4000S^^F5K5K0 CKKLL!0000! !rL   )rH   r   r  r)   r  r:  r   r'   rt   rr   r  rF   clearupdater  r   )	r  r   r!   r  r  r  	_full_cfgr  r  s	           @rC   !_sync_voice_mode_state_to_adapterz/GatewayRunner._sync_voice_mode_state_to_adapterh	  s"    7J55(H-- 	F *DdKK)BDII.#.. 	z-QT7U7U 	F	&JJJJJJ))++I $w''-222:uEE! !  	& 	& 	& %	&7/00 	:(9G%N%%%nc** 	  """!! " " " "373C3I3I3K3K" " "    mS)) 	!!!   ! ! ! !373C3I3I3K3K! ! !     	 	s   9AC CCc                   K   |                                  }	 |dk    r|                                 d{V  dS t          j        |                                |           d{V  dS # t          j        $ r) t
                              d|||j        nd           Y dS t          $ r0}t
          	                    d||j        nd|           Y d}~dS d}~ww xY w)u  Call adapter.disconnect() defensively, swallowing any error.

        Used when adapter.connect() failed or raised — the adapter may
        have allocated partial resources (aiohttp.ClientSession, poll
        tasks, child subprocesses) that would otherwise leak and surface
        as "Unclosed client session" warnings at process exit.

        Must tolerate partial-init state and never raise, since callers
        use it inside error-handling blocks.
        r   Nrx  zITimed out after %.1fs while disconnecting %s adapter; continuing shutdownr   z7Defensive %s disconnect after failed connect raised: %s)
 _adapter_disconnect_timeout_secsr  rz  r  TimeoutErrorru   rv   rF   rt   r;  )r  r   r!   r  r  s        rC   _safe_adapter_disconnectz&GatewayRunner._safe_adapter_disconnect	  sB      7799	!||((***********&w'9'9';';WMMMMMMMMMMMM# 	 	 	NN["*"6I     
  	 	 	LLI"*"6I        	s"    A* .A* *4C!	C*%CCc                     t          j        dd                                          }|rK	 t          |          }t	          d|          S # t
          $ r t                              d|           Y nw xY wt          S )z?Return the per-adapter disconnect timeout used during shutdown.)HERMES_GATEWAY_ADAPTER_DISCONNECT_TIMEOUTrG           z=Ignoring invalid HERMES_GATEWAY_ADAPTER_DISCONNECT_TIMEOUT=%r)	r%   r9  rI   r   maxr   ru   rv   (_ADAPTER_DISCONNECT_TIMEOUT_SECS_DEFAULTr  r   r  s      rC   r  z.GatewayRunner._adapter_disconnect_timeout_secs	  s    iCRHHNNPP 		))** 3(((    S     87   A
 
%A21A2c                     t          j        dd                                          }|rK	 t          |          }t	          d|          S # t
          $ r t                              d|           Y nw xY wt          S )zBReturn the per-platform connect timeout used during startup/retry.'HERMES_GATEWAY_PLATFORM_CONNECT_TIMEOUTrG   r  z;Ignoring invalid HERMES_GATEWAY_PLATFORM_CONNECT_TIMEOUT=%r)	r%   r9  rI   r   r  r   ru   rv   &_PLATFORM_CONNECT_TIMEOUT_SECS_DEFAULTr  s      rC   _platform_connect_timeout_secsz,GatewayRunner._platform_connect_timeout_secs	  s    iA2FFLLNN 		))** 3(((    Q     65r  c                 2  K   |                                  }|dk    r|                                 d{V S 	 t          j        |                                |           d{V S # t          j        $ r!}t	          |j         d|dd          |d}~ww xY w)zAConnect an adapter without allowing one platform to block others.r   Nrx  z connect timed out after gs)r  connectrz  r  r  rF   )r  r   r!   r  rM   s        rC   _connect_adapter_with_timeoutz+GatewayRunner._connect_adapter_with_timeout	  s      5577a<< *********	 )'//*;*;WMMMMMMMMMM# 	 	 	>HHGHHHH 	s   -A& &B5BBc                     | j         S r  )rm  r  s    rC   should_exit_cleanlyz!GatewayRunner.should_exit_cleanly	  s    !!rL   c                     | j         S r  )rn  r  s    rC   should_exit_with_failurez&GatewayRunner.should_exit_with_failure	  s    &&rL   c                     | j         S r  )ro  r  s    rC   exit_reasonzGatewayRunner.exit_reason	  s      rL   c                     | j         S r  )r&  r  s    rC   	exit_codezGatewayRunner.exit_code	  s
    rL   sourcec           	      <   t          | d          rL| j        E	 | j                            |          }t          |t                    r|r|S n# t
          $ r Y nw xY wt          | dd          }t          |t          |dd          t          |dd                    S )	zUResolve the current session key for a source, honoring gateway config when available.rg  Nr  group_sessions_per_userTthread_sessions_per_userFr  r  )rr   rg  _generate_session_keyr   r+   rt   rH   r  )r  r  r  r  s       rC   _session_key_for_sourcez%GatewayRunner._session_key_for_source	  s    4)) 	d.@.L"0FFvNNk3// 'K '&&   x.. $+F4Mt$T$T%,V5OQV%W%W
 
 
 	
s   2A 
AAc                 L   |j         t          j        k    s|j        dk    rdS t	          | dd          }|dS 	 |                    t          |j                  t          |j                            }n-# t          $ r  t                              dd           Y dS w xY w|du S )	z>Return whether Telegram DM topic mode is active for this chat.r  Fr  Nr   user_idz(Failed to read Telegram topic mode stateTrp   )r!   r  TELEGRAMr  rH   is_telegram_topic_mode_enabledr+   r   r  rt   ru   r;  )r  r  
session_dbr   s       rC   _telegram_topic_mode_enabledz*GatewayRunner._telegram_topic_mode_enabled	  s    ?h///63Ct3K3K5T=$77
5	;;FN++FN++ <  CC  	 	 	LLCdLSSS55	 d{s   ;A5 5&BBrG   rd  c                     |j         t          j        k    s|j        dk    rdS |                     |          sdS t          |j        pd          }|| j        v S )zUTrue for the main Telegram DM (or General topic) when topic mode has made it a lobby.r  FrG   r!   r  r  r  r  r+   r  _TELEGRAM_GENERAL_TOPIC_IDSr  r  tids      rC   _is_telegram_topic_root_lobbyz+GatewayRunner._is_telegram_topic_root_lobby
  sb    ?h///63Ct3K3K50088 	5&"(b))d666rL   c                     |j         t          j        k    s|j        dk    rdS |                     |          sdS t          |j        pd          }|r	|| j        v rdS dS )z9True for a user-created Telegram private-chat topic lane.r  FrG   Tr  r  s      rC   _is_telegram_topic_lanez%GatewayRunner._is_telegram_topic_lane
  ss    ?h///63Ct3K3K50088 	5&"(b)) 	cT===5trL   r   c                    t          | d          si | _        t          |j        pd          }|sdS ddl}|                                }| j                            |d          }||z
  | j        k     rdS || j        |<   dS )a+  Rate-limit root-DM lobby reminders to one message per cooldown window.

        A user who forgets multi-session mode is enabled and types several
        prompts in the root DM would otherwise get a reminder for every
        message. Cap it so the first one lands and the rest stay quiet.
        _telegram_lobby_reminder_tsrG   Tr   Nr  F)rr   r  r+   r   r   	monotonicr'   #_TELEGRAM_LOBBY_REMINDER_COOLDOWN_Sr  r  r   _timer   lasts         rC   $_should_send_telegram_lobby_reminderz2GatewayRunner._should_send_telegram_lobby_reminder+
  s     t:;; 	2/1D,fn*++ 	4oo/33GSAA:@@@547(1trL   c                     	 dS )Na  This main chat is reserved for system commands.

To start a new Hermes chat, open the All Messages topic at the top of this bot interface and send any message there. Telegram will create a new topic for that message; each topic works as an independent Hermes session.r   r  s    rC   "_telegram_topic_root_lobby_messagez0GatewayRunner._telegram_topic_root_lobby_message?
  s    *	
 	
rL   c                     	 dS )Na0  To start a new parallel Hermes chat, open the All Messages topic at the top of this bot interface and send any message there. Telegram will create a new topic for it.

Each topic is an independent Hermes session. Use /new inside an existing topic only if you want to replace that topic's current session.r   r  s    rC    _telegram_topic_root_new_messagez.GatewayRunner._telegram_topic_root_new_messageH
  s    W	
 	
rL   c                 6    |                      |          sd S 	 dS )NzStarted a new Hermes session in this topic.

Tip: for parallel work, open All Messages and send a message there to create a separate topic instead of using /new here. /new replaces the session attached to the current topic.)r  )r  r  s     rC   _telegram_topic_new_headerz(GatewayRunner._telegram_topic_new_headerQ
  s,    ++F33 	49	
 	
rL   c                    t          | dd          }||j        r|j        sdS |                    t	          |j                  t	          |j                  t	          |j        pd          |j        |j                   dS )zEPersist the Telegram topic -> Hermes session binding for topic lanes.r  NrG   )r   r  r  r  r  )rH   r   r  bind_telegram_topicr+   r  r  r  )r  r  session_entryr  s       rC   _record_telegram_topic_bindingz,GatewayRunner._record_telegram_topic_binding[
  s     T=$77
V^6;KF&&''&*++,"--%1$/ 	' 	
 	
 	
 	
 	
rL   reasonc                    |                      |          sdS 	 |                     ||           dS # t          $ r! t                              d|d           Y dS w xY w)u4  Update the topic binding to point at ``session_entry.session_id``.

        Telegram topic lanes persist a (chat_id, thread_id) -> session_id row
        so reopening a topic in a fresh process resumes the right Hermes
        session. When compression rotates ``session_entry.session_id`` mid-turn,
        the binding goes stale and the next inbound message in that topic
        reloads the oversized parent transcript instead of the compressed
        child, retriggering preflight compression — sometimes in a loop
        (#20470, #29712, #33414).
        Nz*telegram topic binding refresh failed (%s)Trp   )r  r,  rt   ru   r;  )r  r  r+  r-  s       rC   _sync_telegram_topic_bindingz*GatewayRunner._sync_telegram_topic_bindingl
  s    " ++F33 	F	//FFFFF 	 	 	LL<ft       	s   1 'AAc                    |j         t          j        k    s.|j        dk    s#|j        r|j        r|                     |          sdS t          |j        pd          }| p|| j	        v }|sdS t          | dd          }|dS 	 |                    t          |j                            }n-# t          $ r  t                              dd           Y dS w xY w|sdS t          |j                  }|D ]]}t          |                    d	          pd          |k    r3t          |                    d
          pd          }|r
||k    r|c S  dS ^dS )aO  Pin DM-topic routing to the user's last-active topic.

        Telegram can omit ``message_thread_id`` or surface General (``1``)
        for some topic-mode DM replies. In those lobby-shaped cases, keep the
        conversation attached to the user's most-recent bound topic.

        Do not rewrite a non-lobby, previously-unbound thread id: a newly
        created Telegram DM topic is also "unknown" until the first inbound
        message is recorded, and rewriting it would send that brand-new topic's
        answer into an older lane. Returns None to leave the source alone.
        r  NrG   r  r   ztopic-recover: read failedTrp   r  r  )r!   r  r  r  r   r  r  r+   r  r  rH   %list_telegram_topic_bindings_for_chatrt   ru   r;  r'   )	r  r  inboundis_lobbyr  bindingsr  b	recovereds	            rC   !_recover_telegram_topic_thread_idz/GatewayRunner._recover_telegram_topic_thread_id
  s     Ox0004''> (> ( 44V<< (
 4f&,"--;M'T-M"M 	
 4T=$77
4	!GGFN++ H  HH  	 	 	LL5LEEE44	  	4fn%% 	 	A155##)r**g55k 2 2 8b99	 %g!5!5$$$$tt	 6
 ts   (B+ +&CCc                     	 |                      |          }n# t          $ r |cY S w xY w||S t          j        ||          S )u  Apply Telegram DM topic recovery to a source for session-key purposes.

        ``_handle_message_with_agent`` rewrites ``source.thread_id`` via
        ``_recover_telegram_topic_thread_id`` *before* deriving the session
        key for a normal message turn (a lobby/stripped reply gets pinned to
        the user's last-active topic).  Session-scoped command handlers like
        ``/model`` and ``/reasoning`` derive their override key from the raw
        inbound ``event.source``, which skips that recovery — so the override
        is stored under a different key than the next message turn reads,
        and the override is silently dropped on Telegram forum topics and
        after compression session splits (#30479).

        Returns a recovery-normalized copy when a rewrite applies, otherwise
        the original source unchanged.  Always derive the override storage key
        from the result so storage and read use an identical key.
        Nr  )r8  rt   dataclassesr   )r  r  r7  s      rC   !_normalize_source_for_session_keyz/GatewayRunner._normalize_source_for_session_key
  sa    (	>>vFFII 	 	 	MMM	M"6Y????s    ''r  r  user_configr  r>  c          	         |}|s+|)	 |                      |          }n# t          $ r d}Y nw xY wt          |          }|r| j                            |          nd}|r|                    d|          }|                    d          |                    d          |                    d          |                    d          |                    d          d}|                    d          r7t
                              d	|pd
|||                    d                     ||fS t
                              d|pd
||           nTt
                              d|pd
|| j        r.t          | j                                                  dd         nd           t                      }	|	
                    dd          }
|
rt
                              d||
           |
}|r|r|                     |||	          \  }}	|sb|	                    d          rM	 ddlm}  ||	d                   }|r"t
                              d||	d                    n# t          $ r Y nw xY wt          | dd          }|_|sO|                    |pd
          p|                    d          }|r t
                              d|pd
|           |}n|r|r|||<   ||d<   ||	fS )a$  Resolve model/runtime for a session, honoring session-scoped /model overrides.

        If the session override already contains a complete provider bundle
        (provider/api_key/base_url/api_mode), prefer it directly instead of
        resolving fresh global runtime state first.
        Nr  r  r  r  r"  r   )r  r  r  r"  r   zZSession model override (fast): session=%s config_model=%s -> override_model=%s provider=%srG   z[Session model override (no api_key, fallback): session=%s config_model=%s override_model=%szFNo session model override: session=%s config_model=%s override_keys=%sr  z[]z;Runtime provider supplied explicit model override: %s -> %sr   )get_default_model_for_provideru8   No model configured — defaulting to %s for provider %srt  *u{   Empty model resolved for session=%s — recovering last-known-good model %s (config read likely returned empty; see #35314))r  rt   r  r.  r'   ru   r;  r  keysr2  r@  r:  _apply_session_model_overridehermes_cli.modelsr@  rH   rv   )r  r  r  r>  resolved_session_keyr  overrideoverride_modeloverride_runtimeruntime_kwargsruntime_modelr@  
_last_good
_recovereds                 rC   _resolve_session_agent_runtimez,GatewayRunner._resolve_session_agent_runtime
  s     +# 	,(:,'+'C'CF'K'K$$ , , ,'+$$$, '{33Nbl40445IJJJhl 	%\\'599N$LL44#<<	22$LL44$LL44&ll<88     ##I.. 8p(.B~$((44  
 &'777 LLm$*E>   
 LLX$*EBFB_iT27799::2A2>>ei   788&**7D99 	"KKM  
 "E 	, 	$($F$F$e^% %!E>  
	++J77 
		LLLLLL66~j7QRR KKR~j9       T#94@@
! ('^^,@,FBGG^:>>Z]K^K^
 'NN- -2J	   'E (' =7<J34"'
3n$$s    --;I 
IIuser_messager  rI  c                 p   ddl m} |                    d          |                    d          |                    d          |                    d          |                    d          t          |                    d          pg           |                    d	          |                    d
          d}||||d         |d         |d         |d         t	          |d                   fd}t          | dd          }|si |d<   |S 	  ||d                   }n# t          $ r d}Y nw xY w|pi |d<   |S )a2  Build the effective model/runtime config for a single turn.

        Always uses the session's primary model/provider.  If `/fast` is
        enabled and the model supports Priority Processing / Anthropic fast
        mode, attach `request_overrides` so the API call is marked
        accordingly.
        r   )resolve_fast_mode_overridesr  r  r  r"  r#  r$  r%  r   r&  )r  r+  	signaturer\  Nrequest_overridesr  )rD  rP  r'   r  tuplerH   rt   )	r  rN  r  rI  rP  r+  routeservice_tier	overridess	            rC   _resolve_turn_agent_configz(GatewayRunner._resolve_turn_agent_configC  s    	BAAAAA &)))44&**:66&**:66&**:66%)))44++F339r::-112CDD(,,\::	
 	
 
#
#
#	"gfo&&
 
 t_d;; 	)+E%&L	33E'NCCII 	 	 	III	%._"!"s   D D,+D,r   c                   K   t                               d|j        j        |j        pd|j        pd           |                     |j        j        |j        rdnd|j        |j                   | j        	                    |j                  }||u r	 |
                                 d{V  | j                            |j        d           | j        | j        _        n6# | j                            |j        d           | j        | j        _        w xY w|j        r| j        j        	                    |j                  }|rZ|j        | j        vrL|dt!          j                    d	z   d
| j        |j        <   t                               d|j        j                   | j        st| j        sm|j        pd| _        |j        r"d| _        t                               d           nt                               d           |                                  d{V  dS | j        s6| j        r1t                               dt/          | j                             dS dS dS )zReact to an adapter failure after startup.

        If the error is retryable (e.g. network blip, DNS failure), queue the
        platform for background reconnection instead of giving up permanently.
        zFatal %s adapter error (%s): %sr  r  retryingfatalplatform_state
error_codeerror_messageNr      r  attempts
next_retryz%%s queued for background reconnectionz#All messaging adapters disconnectedTzSNo connected messaging platforms remain. Shutting down gateway for service restart.zGNo connected messaging platforms remain. Shutting down gateway cleanly.u   No connected messaging platforms remain, but %d platform(s) queued for reconnection — gateway staying alive, watcher will retry in background.)ru   ru  r!   rF   fatal_error_codefatal_error_message_update_platform_runtime_statusfatal_error_retryablerQ  r'   r  r@  rh  r  	platformsr  r   r  r:  ro  rn  stoprv   r   )r  r   existingplatform_configs       rC   _handle_adapter_fatal_errorz)GatewayRunner._handle_adapter_fatal_errorp  s      	-"$1	':?		
 	
 	
 	,,")0)FS::G/!5	 	- 	
 	
 	
 =$$W%566w>((*********!!'"2D99904$-- !!'"2D99904$-==== ( 	"k3778HIIO 	7#34;Q#Q#Q- !"&."2"2R"7< <&w'78
 ;$*  
 } 	T%; 	 ' ; d?dD, h*.'rssssfggg))++ 	4#9 	 NN' D*++	    	 	 	 	s   C 3Dc                 T    d| _         || _        | j                                         d S NT)rm  ro  rl  r)   )r  r-  s     rC   _request_clean_exitz!GatewayRunner._request_clean_exit  s-    !"  """""rL   c                 *    t          | j                  S r  )r   rq  r  s    rC   _running_agent_countz"GatewayRunner._running_agent_count  s    4'(((rL   c                     | j         rdndS )Nrestartshutdownr(  r  s    rC   _status_action_labelz"GatewayRunner._status_action_label  s     3CyyCrL   c                     | j         rdndS )N
restartingshutting downrt  r  s    rC   _status_action_gerundz#GatewayRunner._status_action_gerund  s    #6K||OKrL   c                 "    | j         o| j        dv S )N>   queuesteer)r(  r#  r  s    rC   _queue_during_drain_enabledz)GatewayRunner._queue_during_drain_enabled  s     &V4+@DV+VVrL   queued_eventr  c                     |dS t          |dd          }|dS t          | dd          }|	i }|| _        ||v r+|                    |g                               |           dS |||<   dS )z6Append a /queue event to the FIFO chain for a session.Nrs  ru  )rH   ru  
setdefaultr(   )r  r  r~  r   pending_slotqueued_eventss         rC   _enqueue_fifozGatewayRunner._enqueue_fifo  s    ?Fw(;TBBF&6== M"/D,&&$$["55<<\JJJJJ(4L%%%rL   pending_eventc                 R   t          | dd          }|s|S |                    |          }|s|S |                    d          }|s|                    |d           ||S |t          |d          r||j        |<   n*|                    |g                               d|           |S )aY  Promote the next overflow item after the slot was drained.

        Called at the drain site after _dequeue_pending_event consumed
        (or failed to consume) the slot.  If there's an overflow item:
          - When pending_event is None (slot was empty), return the
            overflow head as the new pending_event.
          - When pending_event already exists (slot was populated by an
            interrupt follow-up or similar), stage the overflow head in
            the slot so the NEXT recursion picks it up.
        Returns the (possibly updated) pending_event for drain to use.
        ru  Nr   rs  )rH   r'   r@  rr   rs  r  r0   )r  r  r   r  r  overflownext_queueds          rC   _promote_queued_eventz#GatewayRunner._promote_queued_event  s    "  &6== 	!   $$[11 	!  ll1oo 	1k4000 774G#H#H5@G%k22 $$["55<<QLLLrL   r  c                    t          | dd          pi }t          |                    |g                     }||t          |di           v r|dz  }|S )u=   Total pending /queue items for a session — slot + overflow.ru  Nrs  r   )rH   r   r'   )r  r  r   r  rc   s        rC   _queue_depthzGatewayRunner._queue_depth  sb    &6==CM%%k26677;''CVXZ2[2[#[#[QJErL   event_or_textc                 l    t          | d|           pd}t          |                              d          S )zReturn True for synthetic /goal continuation turns.

        Goal continuations are normal queued user-role events, so pause/clear
        must distinguish them from real user /queue messages before removing or
        suppressing them.
        r{   rG   z,[Continuing toward your standing goal]
Goal:)rH   r+   rI  )r  r{   s     rC   _is_goal_continuation_eventz)GatewayRunner._is_goal_continuation_event  s5     }fm<<B4yy##$STTTrL   c                    d}|t          |dd          nd}t          |t                    rE|                    |          }|                     |          r|                    |d           |dz  }t          | dd          }t          |t                    rn|                    |          pg }|rUg }|D ]2}	|                     |	          r|dz  }|                    |	           3|r|||<   n|                    |d           |S )a  Remove queued synthetic /goal continuations for one session.

        User-issued /goal pause/clear can race with a continuation already
        queued by the judge.  Remove only synthetic goal continuations while
        preserving normal /queue and user follow-up events.
        r   Nrs  r   ru  )rH   r   r6   r'   r  r@  r(   )
r  r  r   removedr  r  r  r  keptr~  s
             rC   !_clear_goal_pending_continuationsz/GatewayRunner._clear_goal_pending_continuations  s>    FMFYww(;TBBB_clD)) 	(,,[99M//>>   d3331&6==mT** 	9$((55;H 
9$, 2 2L77EE 21L1111 915M+..!%%k4888rL   r  c                     |sdS 	 ddl m}  ||                                          S # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)z@Best-effort fresh DB check before running a queued continuation.Fr   GoalManagerr  z2goal continuation: active-state recheck failed: %sN)hermes_cli.goalsr  	is_activert   ru   r;  )r  r  r  rM   s       rC   _goal_still_active_for_sessionz,GatewayRunner._goal_still_active_for_session7  s     	5	444444;*555??AAA 	 	 	LLMsSSS55555	s   #* 
AAAgateway_stater  c                     	 ddl m}  |||| j        |                                            d S # t          $ r Y d S w xY w)Nr   write_runtime_status)r  r  restart_requestedactive_agents)gateway.statusr  r(  rp  rt   )r  r  r  r  s       rC   _update_runtime_statusz$GatewayRunner._update_runtime_statusB  s|    		;;;;;;  +'"&"9"7799	       	 	 	DD	s   ,0 
>>r[  r\  r]  r^  c                T    	 ddl m}  |||||           d S # t          $ r Y d S w xY w)Nr   r  )r!   r\  r]  r^  )r  r  rt   )r  r!   r\  r]  r^  r  s         rC   re  z-GatewayRunner._update_platform_runtime_statusN  sm    		;;;;;;  !-%+	       	 	 	DD	s    
''r-  c                   t          | di                               |          }|dS |                    d          rdS d|d<   |pd|d<   t          d          |d<   	 |                     |j        dd|d         	           n# t
          $ r Y nw xY wt                              d
|j        |                    dd          |d         |j                   dS )u"  Mark a queued platform as paused — keep it in ``_failed_platforms``
        but stop the reconnect watcher from hammering it.

        Used by ``/platform pause <name>`` for manual operator intervention.
        Paused platforms are surfaced in ``/platform list`` and resumed with
        ``/platform resume <name>``.  Note: the reconnect watcher does NOT
        auto-pause — retryable (network/DNS) failures keep retrying at the
        backoff cap indefinitely so a transient outage self-heals without
        manual intervention.
        r  NpausedTz#auto-paused after repeated failurespause_reasoninfrb  r[  u   %s paused after %d consecutive failures (%s) — fix the underlying issue then run `/platform resume %s` to retry, or `hermes gateway restart` to restart the gateway.ra  r   )rH   r'   r   re  rF   rt   ru   rv   )r  r!   r-  r:  s       rC   _pause_failed_platformz$GatewayRunner._pause_failed_platformf  s    t0"5599(CC<F88H 	FX%N)N^ #5\\\	00'">2	 1      	 	 	D	L NDHHZ33 (.	
 	
 	
 	
 	
s   $B 
BBc                    t          | di                               |          }|dS |                    d          sdS d|d<   |                    dd           d|d<   t          j                    |d<   	 |                     |j        d	dd
           n# t          $ r Y nw xY wt          	                    d|j                   dS )u   Unpause a platform — reset its attempt counter and schedule an
        immediate retry.  Returns True if the platform was paused and is
        now queued; False if it wasn't paused (or wasn't in the queue).
        r  NFr  r  r   ra  rb  rY  r[  u,   %s resumed — retrying on next watcher tickT)
rH   r'   r@  r   r  re  rF   rt   ru   r:  )r  r!   r:  s      rC   _resume_paused_platformz%GatewayRunner._resume_paused_platform  s    
 t0"5599(CC<5xx!! 	5X&&&Z!^--\	00)"	 1      	 	 	D	BHNSSSts   7B 
B#"B#c                     t          j        dd          } | sWt                      }t          |                    dd          pd          } | s"t          t          |ddd          pd          } | sg S t          |                                           }|                                s
t          |z  }|
                                st                              d|           g S 	 t          |dd	          5 }t          j        |          }d
d
d
           n# 1 swxY w Y   t!          |t"                    st                              d|           g S |S # t$          $ r(}t                              d||           g cY d
}~S d
}~ww xY w)a\  Load ephemeral prefill messages from config or env var.
        
        Checks HERMES_PREFILL_MESSAGES_FILE env var first, then falls back to
        the top-level prefill_messages_file key in ~/.hermes/config.yaml.
        agent.prefill_messages_file is accepted as a legacy fallback.
        Relative paths are resolved from ~/.hermes/.
        HERMES_PREFILL_MESSAGES_FILErG   prefill_messages_filerx  r  z#Prefill messages file not found: %sr  rs  rt  Nz3Prefill messages file must contain a JSON array: %sz+Failed to load prefill messages from %s: %s)r%   r9  r  r+   r'   r   r   
expanduseris_absoluterY  r.   ru   rv   r}  r,  loadr   r  rt   )	file_pathr  r/   r  r  r  s         rC   rU  z$GatewayRunner._load_prefill_messages  s    I<bAA	 	b.00CCGG$;R@@FBGGI bW6MWY Z Z Z `^`aa	 	II))++!! 	'$&D{{}} 	NN@$GGGI		dC'222 $ay||$ $ $ $ $ $ $ $ $ $ $ $ $ $ $dD)) TVZ[[[	K 	 	 	NNH$PQRRRIIIIII	sH   $E 6DE DE D4E E 
F F=FFc                      t          j        dd          } | r| S t                      }t          t	          |ddd          pd                                          S )zLoad ephemeral system prompt from config or env var.
        
        Checks HERMES_EPHEMERAL_SYSTEM_PROMPT env var first, then falls back to
        agent.system_prompt in ~/.hermes/config.yaml.
        HERMES_EPHEMERAL_SYSTEM_PROMPTrG   rx  system_promptr  )r%   r9  r  r+   r   rI   )promptr  s     rC   rW  z+GatewayRunner._load_ephemeral_system_prompt  s^     ;R@@ 	M*,,73"EEEKLLRRTTTrL   c                     ddl m}  t                      }t          t	          |ddd          pd                                          } | |          }|r1|                                r|t                              d|           |S )	zLoad reasoning effort from config.yaml.

        Reads agent.reasoning_effort from config.yaml. Valid: "none",
        "minimal", "low", "medium", "high", "xhigh". Returns None to use
        default (medium).
        r   )parse_reasoning_effortrx  reasoning_effortrG   r  Nz5Unknown reasoning_effort '%s', using default (medium))r  r  r  r+   r   rI   ru   rv   )r  r  effortr  s       rC   rY  z$GatewayRunner._load_reasoning_config  s     	<;;;;;*,,WS'+=rJJJPbQQWWYY''// 	\fllnn 	\NNRTZ[[[rL   raw_argsc                    ddl }t          | pd                                                              dd          }|sdS 	  |j        |          }n$# t
          $ r |                                }Y nw xY wd}g }|D ] }|dk    rd	}|                    |           !d
                    |                                                                          |fS )zParse `/reasoning` args into `(value, persist_global)`.

        `/reasoning <level>` is session-scoped by default. `--global` may be
        supplied in any position to persist the change to config.yaml.
        r   NrG   u   —z--)rG   FFz--globalTr  )	shlexr+   rI   r   r  r   r(   r5   r,   )r  r  r{   tokenspersist_globalvalue_tokensr  s          rC   _parse_reasoning_command_argsz+GatewayRunner._parse_reasoning_command_args  s     	8>r""((**225$?? 	9	" U[&&FF 	" 	" 	"ZZ\\FFF	"  	+ 	+E
""!%##E****xx%%++--3355~EEs   A A32A3r  r  c                    |}|s+|)	 |                      |          }n# t          $ r d}Y nw xY wt          | di           pi }|r||v r||         S |                                 S )zCResolve reasoning effort for a session, honoring session overrides.Nr/  )r  rt   rH   rY  )r  r  r  rE  rV  s        rC   !_resolve_session_reasoning_configz/GatewayRunner._resolve_session_reasoning_config  s      +# 	,(:,'+'C'CF'K'K$$ , , ,'+$$$, D"@"EEK	 	3$8I$E$E122**,,,s    --reasoning_configc                     |sdS t          | d          si | _        || j                            |d           dS t          |          | j        |<   dS )z3Set or clear the session-scoped reasoning override.Nr/  )rr   r/  r@  r6   )r  r  r  s      rC   _set_session_reasoning_overridez-GatewayRunner._set_session_reasoning_override  sm      	Ft;<< 	302D-#-11+tDDDDD=ABR=S=SD-k:::rL   c                     t                      } t          t          | ddd          pd                                          }|                                }|r|dv rdS |dv rdS t
                              d	|           dS )
a  Load Priority Processing setting from config.yaml.

        Reads agent.service_tier from config.yaml. Accepted values mirror the CLI:
        "fast"/"priority"/"on" => "priority", while "normal"/"off" disables it.
        Returns None when unset or unsupported.
        rx  rU  rG   r  >   r  nonenormalr   standardN>   onfastpriorityr  z#Unknown service_tier '%s', ignoring)r  r+   r   rI   r,   ru   rv   )r  r   rF   s      rC   r[  z GatewayRunner._load_service_tier!  s     +,,'#wCCCIrJJPPRR		 	!QQQ4...:<cBBBtrL   c                  ^    t                      } t          t          | dd          d          S )z<Load show_reasoning toggle from config.yaml display section.r  show_reasoningFr  )r  rj  r   )r  s    rC   r]  z"GatewayRunner._load_show_reasoning4  s9     +,,C$455
 
 
 	
rL   c                  D   t          j        dd                                                                          } | sTt	                      }t          t          |ddd          pd                                                                          } | dk    rdS | dk    rdS dS )	z<Load gateway drain-time busy-input behavior from config/env.r  rG   r  r  r  r{  r|  r"  )r%   r9  rI   r,   r  r+   r   )r<  r  s     rC   r_  z#GatewayRunner._load_busy_input_mode=  s     y92>>DDFFLLNN 	e.00CwsI/@"MMMSQSTTZZ\\bbddD7??77??7{rL   c                     t          j        dd                                                                          } | sTt	                      }t          t          |ddd          pd                                                                          } | dk    rdS | dk    rdS t                                          }|dk    rdndS )a  Resolve normal busy TEXT follow-up behavior.

        ``busy_input_mode`` is the single source of truth (default
        ``interrupt``). The legacy ``busy_text_mode`` knob is honored only
        when a user explicitly set it, so existing queue setups keep
        working; new installs follow ``busy_input_mode``. Returns one of
        ``interrupt`` | ``queue`` (``steer`` is handled upstream by
        ``busy_input_mode`` and maps to non-queue text handling here).
        r  rG   r  r  r  r"  r{  )	r%   r9  rI   r,   r  r+   r   r   r_  )legacyr  
input_modes      rC   r`  z"GatewayRunner._load_busy_text_modeJ  s     :B??EEGGMMOO 	f.00Ci1A2NNNTRTUU[[]]cceeF[  ;W7"88::
$//ww[@rL   c                     t          j        dd                                          } | sBt                      }t	          t          |ddd          pd                                          } t          |           }| rT|t          k    rI	 t          |            n8# t          t          f$ r$ t                              d| t                     Y nw xY w|S )z<Load graceful gateway restart/stop drain timeout in seconds.r  rG   rx  r  r  z7Invalid restart_drain_timeout '%s', using default %.0fs)r%   r9  rI   r  r+   r   r  r  r   r   r   ru   rv   )r   r  rF   s      rC   ra  z)GatewayRunner._load_restart_drain_timeoutb  s     i6;;AACC 	`.00Cgc7,CRPPPVTVWW]]__C+C00 	5AAAc



z*   M9     s   	B 2CCc                  @   t          j        dd          } | s9t                      }t          |dd          }|du rd} n|dvrt	          |          } | pd                                                                } h d	}| |vrt                              d
|            dS | S )u  Load background process notification mode from config or env var.

        Modes:
          - ``all``    — push running-output updates *and* the final message (default)
          - ``result`` — only the final completion message (regardless of exit code)
          - ``error``  — only the final message when exit code is non-zero
          - ``off``    — no watcher messages at all
        HERMES_BACKGROUND_NOTIFICATIONSrG   r   background_process_notificationsFr     NrG   r  >   r  r  ru  r  zBUnknown background_process_notifications '%s', defaulting to 'all')	r%   r9  r  r   r+   rI   r,   ru   rv   )r<  r  r   valids       rC   #_load_background_notifications_modez1GatewayRunner._load_background_notifications_modeu  s     y:B?? 	 .00C#y*LMMCe||J&&3xx$$&&,,..111uNNT   5rL   c                     	 ddl } t          dz  }|                                rXt          |d          5 }|                     |          pi }ddd           n# 1 swxY w Y   |                    di           pi S n# t          $ r Y nw xY wi S )z>Load OpenRouter provider routing preferences from config.yaml.r   Nrr  rs  rt  provider_routing)r|  rY  r.   r}  r~  r'   rt   )r<  r=  r>  r  s       rC   rb  z$GatewayRunner._load_provider_routing  s    	#m3H   =(W555 1,,r**0bC1 1 1 1 1 1 1 1 1 1 1 1 1 1 1ww1266<"<=  	 	 	D		s4   3A= AA= AA=  A!A= =
B
	B
c                     	 ddl } t          dz  }|                                rSt          |d          5 }|                     |          pi }ddd           n# 1 swxY w Y   t          |          }|r|S n# t          $ r Y nw xY wdS )zLoad fallback provider chain from config.yaml.

        Returns the merged effective chain from ``fallback_providers`` plus any
        legacy ``fallback_model`` entries. ``fallback_providers`` stays first
        when both keys are present.
        r   Nrr  rs  rt  )r|  rY  r.   r}  r~  r   rt   )r<  r=  r>  r  fbs        rC   rd  z"GatewayRunner._load_fallback_model  s    
	#m3H   (W555 1,,r**0bC1 1 1 1 1 1 1 1 1 1 1 1 1 1 1',, I 	 	 	D	ts4   3A8 AA8 AA8  A!A8 8
BBc                 H    d | j                                         D             S )Nc                 ,    i | ]\  }}|t           u||S r   )_AGENT_PENDING_SENTINEL)r   r  rx  s      rC   r   z:GatewayRunner._snapshot_running_agents.<locals>.<dictcomp>  s4     
 
 
"U333 333rL   )rq  r   r  s    rC   _snapshot_running_agentsz&GatewayRunner._snapshot_running_agents  s2    
 
&*&:&@&@&B&B
 
 
 	
rL   c                 f    	 ddl m}  |t          | dd                    S # t          $ r Y dS w xY w)z:Return the configured active chat session cap, if enabled.r   )resolve_max_concurrent_sessionsr  N)hermes_cli.active_sessionsr  rH   rt   )r  r  s     rC   _get_max_concurrent_sessionsz*GatewayRunner._get_max_concurrent_sessions  sX    	RRRRRR227443P3PQQQ 	 	 	44	s   " 
00c                     |                                  }|dS |t          | di           v rdS t          t          | di                     }||k     rdS d| d| dS )zKReturn a user-facing rejection when starting a new session exceeds the cap.Nrq  z'Hermes is at the active session limit (r   z+). Try again when another session finishes.)r  rH   r   )r  r  max_sessionsactive_counts       rC   _active_session_limit_messagez+GatewayRunner._active_session_limit_message  s    88::4'$(92>>>>474):B??@@,&&47l 7 7\ 7 7 7	
rL   c                    |t          | di           v rdS |                     |          }|d|fS 	 ddlm} |r|j        r|j        j        nd} ||d| t          | dd          |t          |d	d
          pd
t          |dd
          pd
d          S # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)zAClaim a cross-process active-session slot for a new gateway turn.rq  r  Nr   )try_acquire_active_sessionr  zgateway:r  r   rG   r  )r!   r   r  )r  surfacer  r   z'Failed to claim active session slot: %s)	rH   r  r  r  r!   rF   rt   ru   rv   )r  r  r  local_limit_messager  r!   rM   s          rC   _claim_active_session_slotz(GatewayRunner._claim_active_session_slot  s)    '$(92>>>>:"@@MM*,,,	MMMMMM06Y6?Yv,,PYH--&-8--tXt44 (&vy"==C&vy"==C 		 	 	 	  	 	 	NNDcJJJ:::::	s   A#B 
C CCrunning_agentc                 H   | 	| t           u rdS t          | dd          }t          |t          t          t
          f          sdS |sdS t          | dd          }	 |)|5  t          |          cddd           S # 1 swxY w Y   t          |          S # t          $ r Y dS w xY w)aL  Return True when *running_agent* is currently driving subagents
        via the ``delegate_task`` tool.

        Background (#30170): ``AIAgent.interrupt()`` cascades through the
        parent's ``_active_children`` list and calls ``interrupt()`` on
        every child synchronously, which aborts in-flight subagent work
        and produces a fallback cascade with no actionable signal.
        Demoting ``busy_input_mode='interrupt'`` to ``queue`` semantics
        whenever this helper returns True protects subagent work from
        conversational follow-ups while leaving the explicit ``/stop``
        path (which goes through ``_interrupt_and_clear_session``)
        untouched. Safe-by-default: returns False on any attribute or
        lock error so a missing/broken parent never blocks the existing
        interrupt path.
        NF_active_children_active_children_lock)r  rH   r   r  rS  r)   r   rt   )r  childrenlocks      rC   _agent_has_active_subagentsz)GatewayRunner._agent_has_active_subagents  s   "  M5L$L$L5=*<dCC (T5#$677 	5 	5}&=tDD	 * *>>* * * * * * * * * * * * * * * *>>! 	 	 	55	s6   B A9,B 9A==B  A=B 
B! B!    rK  c                    | j                             |j        j                  }|sd S t	          |dd           }t          |t                    r|                    |          nd }|t	          |dd           t          j        k    sQ|j	        t          j        k    s<t          t	          |dd                     st          t	          |dd                     r-t          |j        |||j	        t          j        k               d S |                     ||          | j        k    r#t                               d|| j                   d S |                     |||           d S )Nrs  rE  rB  
merge_textr  uJ   Dropping busy-mode follow-up for session %s — pending queue at cap (%d).)rQ  r'   r  r!   rH   r   r6   r  rJ  rE  r   r  rs  TEXTr  _BUSY_QUEUE_MAX_PENDINGru   rv   r  )r  r  rK  r   r  ri  s         rC   _queue_or_replace_pending_eventz-GatewayRunner._queue_or_replace_pending_event  sp   -##EL$9:: 	F w(;TBB4>|T4R4R\<##K000X\Hnd33{7HHH![%666GHlD99:: 7GE<6677 7
 () -1AA	    F['::d>ZZZNN\,  
 F;w77777rL   c                 @  K   |                      |j                  sQt                              d|j        j        |j        j        |j        j        r|j        j        j        nd|           dS | j        rC| j	        
                    |j        j                  }|sdS |                     |          }|                     |j        |          }|                                 r/|                     ||           d|                                  d}nd|                                  d}|                    |j        j        ||j        j        t$          j        k    r|j        j        dk    r|j        j        r|n.|j        j        t$          j        k    r|j        j        rd n|j        |	           d {V  dS | j	        
                    |j        j                  }|sd
S | j        
                    |          }| j        }t3          | dd          }	|j        t6          j        k    r|	dk    r|dk    rd
S |dk    o|                     |          }
|
rt                              d|           d}d
}|dk    r|j        pd                                 }|o|d uo|tB          uotE          |d          }|rY	 tG          |$                    |                    }n5# tJ          $ r(}t                              d||           d
}Y d }~nd }~ww xY w|sd}|s+tM          |j'        |||j        t6          j        k               |dk    }|dk    }|dk    r7|r5|tB          ur,	 |(                    |j                   n# tJ          $ r Y nw xY wtR          j*        
                    dd          +                                dk    }|st          ,                    d|           dS d}t[          j-                    }| j.        
                    |d          }||z
  |k     rdS || j.        |<   ddl/m0} g }tG           |tc                      te          |j        j                  dd                    }|r|r|tB          ur	 |3                                }|
                    dd          }|
                    dd          }|
                    d          }| j4        
                    |d          }|r3tk          ||z
  dz            }|dk    r|6                    | d           |r|6                    d| d |            |r|6                    d!|            n# tJ          $ r Y nw xY w|rd"d#7                    |           d$nd}|rd%| d&}n|r	|
rd'| d(}n|rd)| d*}nd+| d,}	 dd-l8m9}m:} m;}!m<}" tc                      }# |!|#|          s0|rd}$n|rd}$nd}$| d. | |$           } |"tz          d/z  |           n2# tJ          $ r%}%t          ,                    d0|%           Y d }%~%nd }%~%ww xY w|                     |          }|                     |j        |          }	 |                    |j        j        ||j        j        t$          j        k    r|j        j        dk    r|j        j        r|n.|j        j        t$          j        k    r|j        j        rd n|j        |	           d {V  n2# tJ          $ r%}&t          ,                    d1|&           Y d }&~&nd }&~&ww xY wdS )2Nz`Dropping message from unauthorized user in active session: user=%s (%s), platform=%s, session=%sr  T   ⏳ Gateway 2    — queued for the next turn after it comes back.   ⏳ Gateway is - and is not accepting another turn right now.r  r   r   reply_tor   Fr$  r"  r{  r|  zvDemoting busy_input_mode 'interrupt' to 'queue' for session %s because the running agent has active subagents (#30170)rG   z'Gateway steer failed for session %s: %sr  r  truez"Busy ack suppressed for session %sr_  r   resolve_display_settingbusy_ack_detailapi_call_countmax_iterationscurrent_toolrY  z min elapsed
iteration r   z	running:  (, )u   ⏩ Steered into current runz0. Your message arrives after the next tool call.u   ⏳ Subagent workinguR    — your message is queued for when it finishes (use /stop to cancel everything).u   ⏳ Queued for the next turnz.. I'll respond once the current task finishes.u   ⚡ Interrupting current taskz'. I'll respond to your message shortly.)BUSY_INPUT_FLAGbusy_input_hint_gatewayis_seen	mark_seenr
  rr  z.Failed to apply busy-input onboarding hint: %szFailed to send busy-ack: %s)>_is_user_authorizedr  ru   rv   r  	user_namer!   rF   r'  rQ  r'   r  _thread_metadata_for_sourcer}  r  ry  _send_with_retryr   r  r  r  r  
message_idrq  r#  rH   rE  r  r  r  r:  r{   rI   r  rr   r   r|  rt   r  rs  r"  r%   r&   r,   r;  r   rw  gateway.display_configr  r  r  get_activity_summaryr!  r   r(   r5   agent.onboardingr  r  r  r  rY  )'r  rK  r  r   reply_anchorthread_metarl   r  effective_moder  demoted_for_subagentssteered
steer_text	can_steerrM   is_queue_modeis_steer_moder  _BUSY_ACK_COOLDOWNr   last_ackr  status_partsbusy_ack_detail_enabledsummary	iterationmax_iterr
  start_tselapsed_minstatus_detailr  r  r  r  	_user_cfg
_hint_mode_onb_errr  s'                                          rC   #_handle_active_session_busy_messagez1GatewayRunner._handle_active_session_busy_messageE  s	      ''55 		NN8$&/4|/DS%++)   4 > 	m''(=>>G t77>>L::5<VVK//11 x44[%HHHy)C)C)E)EyyywD,F,F,H,Hwww**, |,0AAA.$66. 7 !L #(,"78;L"L"LQVQ]Qg"L$$mrm}$ +          4 -##EL$9:: 	5,00==. '8+FF+"222'))'))5 k) @00?? 	 ! 	%KKJ  
 %NW$$**1133J 4!-4!)@@4 M733	   $$"=#6#6z#B#BCCGG  $ $ $NN#Lk[^___#GGGGGG$  )!(  	') -1AA	    ''1&'1
 [((](}Tk?k?k''
3333    :>>*KVTTZZ\\`ff 	LL={KKK4  ikk$((a88>...4),+&
 	CBBBBB"&##$&&$U\%:;;!	 #
 #
 # 	} 	Ne9e9e'<<>>#KK(8!<<	";;'7;;&{{>::266{AFF J"%sX~&;"<"<K"Q$++{,H,H,HIII M ''(KY(K(K(K(KLLL D ''(BL(B(BCCC    <HO7TYY|447777R 	B} B B B G  	4 	
F} F F F G  		@} @ @ @ G9 9 9 9 	U            -..I79o66 I  -!(JJ" -!(JJ!,J = =..z::= =  	,6HHH 	U 	U 	ULLI8TTTTTTTT	U 33E::66u|\RR	;**, |,0AAA.$66. 7 !L #(,"78;L"L"LQVQ]Qg"L$$mrm}$ +            	; 	; 	;LL6::::::::	; tso   5"K 
L
"LL
M7 7
NN5CU 
UU#AW: :
X)X$$X)B[, ,
\6\\r  c                 ~   K                                     }                                 dd
dt          dd f fd} j        s |d           |dfS  |d           |dk    r|dfS t	          j                                                    |z   } j        r}t	          j                                                    |k     rT |             t	          j        d	           d {V   j        r)t	          j                                                    |k     Tt           j                  } |d           ||fS )Nr  Fr  r   c                     t          j                                                    }                                }| s|k    s	|z
  dk    r                    d           ||d S d S )N      ?draining)rz  get_running_loopr   rp  r  )r  r   r  last_active_countlast_status_atr  s      rC   _maybe_update_statusz@GatewayRunner._drain_active_agents.<locals>._maybe_update_statusC  s}    *,,1133C4466L %(999cN>RWZ=Z=Z++J777$0!!$ >[=ZrL   Tr  r   皙?F)r  rp  r   rq  rz  r6  r   sleep)r  r  snapshotr9  deadline	timed_outr7  r8  s   `     @@rC   _drain_active_agentsz"GatewayRunner._drain_active_agents>  s     0022 5577	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% # 	#  t,,,,U?"4((((a<<T>!+--2244w>" 	%w'?'A'A'F'F'H'H8'S'S  """-$$$$$$$$$ " 	%w'?'A'A'F'F'H'H8'S'S -..	4((((""rL   c                 8   t          | j                                                  D ]r\  }}|t          u r	 |                    |           t
                              d|           A# t          $ r%}t
                              d|           Y d }~kd }~ww xY wd S )Nz8Interrupted running agent for session %s during shutdownz-Failed interrupting agent during shutdown: %s)r  rq  r   r  r"  ru   r;  rt   )r  r-  r  rx  r  s        rC   _interrupt_running_agentsz'GatewayRunner._interrupt_running_agents\  s    "&t';'A'A'C'C"D"D 	Q 	QK///Q'''WYdeeee Q Q QLaPPPPPPPPQ	Q 	Qs   0A((
B2BBc                 n  K   |                                  }| j        r| j        nd}| j        rdnd}| j        rdnd}d| d| }t                      }|D ]}d}	 t	          | dd          M| j                                         | j        j                            |          }	|	rt	          |	d	d          nd}n3# t          $ r&}
t                              d
||
           Y d}
~
nd}
~
ww xY w||                     |          }|(|j        j        }t          |j                  }|j        }n7t%          |          }|s|d         }|d         }|                    d          }|||rt          |          ndf}||v r,	 t'          |          }| j                            |          }|sZ| j        j                            |          }|$|j        st                              d|           |t	          |dd          nd}|k|i	 |j        j        }t          |j                  }|j        rt          |j                  nd}|||f|k    rt	          |dd          }n# t          $ r Y nw xY w|                     ||||t	          |dd          nd||          }|                    |||           d{V }|At	          |dd          du r.t                              d||t	          |dd                     |                    |           t                              d||           # t          $ r(}
t                              d|||
           Y d}
~
d}
~
ww xY w| j        r|t                              d           dS t9          | j                                                  D ]\  }}| j                            |          }|r|j        s*| j        j                            |          }|(|j        s!t                              d|j                   s|j        t          |j                  |j        rt          |j                  ndf}||v r	 |                     ||j        |j        |          }|r1|                    t          |j                  ||           d{V }n.|                    t          |j                  |           d{V }|Kt	          |dd          du r8t                              d|j        |j        t	          |dd                     |                    |           t                              d|j        |j                   # t          $ r2}
t                              d|j        |j        |
           Y d}
~
d}
~
ww xY wdS )u4  Send shutdown/restart notifications to active chats and home channels.

        Called at the very start of stop() — adapters are still connected so
        messages can be delivered. Best-effort: individual send failures are
        logged and swallowed so they never block the shutdown sequence.
        Nrw  rx  zpYour current task will be interrupted. Send any message after restart and I'll try to resume where you left off.z&Your current task will be interrupted.u   ⚠️ Gateway     — rg  originz>Failed to load session origin for shutdown notification %s: %sr!   r   r  z^Shutdown notification suppressed for active session: %s has gateway_restart_notification=falser  r  r  reply_to_message_idr   r   r(  TFz1Failed to send shutdown notification to %s:%s: %sru  send returned success=Falsez/Sent shutdown notification to active chat %s:%sz@Skipping home-channel shutdown notifications for in-chat restartz\Shutdown notification suppressed for home channel: %s has gateway_restart_notification=falser  z>Failed to send shutdown notification to home channel %s:%s: %sz0Sent shutdown notification to home channel %s:%s)r  r(  r,  r)   rH   rg  _ensure_loaded_entriesr'   rt   ru   r;  _get_cached_session_sourcer!   rF   r+   r   r  r  r  rQ  r  rg  gateway_restart_notificationr:  _thread_metadata_for_targetr   r-   r  r   get_home_channel)r  activerestart_sourceactionhintr   notifiedr  r  r   r  platform_strr   r  _parsed	dedup_keyr!   r   platform_cfgrG  restart_platformrestart_chat_idrestart_thread_idr   r  homes                             rC   #_notify_active_sessions_of_shutdownz1GatewayRunner._notify_active_sessions_of_shutdownf  s      ..009=9PZ55VZ!%!8Mo &: X X :	 	 433T338;! [	 [	KF
4$77C&55777 .7;;KHHE?DNWUHd;;;$F   T        ~88EE!%4fn--",		 -[99 &z2!),#KK44	
 &w)0UIQUVIH$$5#L11-++H55 #{488BB+L4]+KKx$   MSM_gflD&I&I&Iei#&.>3M+9+B+H(*-n.D*E*EM[Me,oC0H,I,I,Iko),o?PQU^^^29.,X\2]2]/$     ;;DJDVgfk4@@@\`(;# <    '||GS8|LLLLLLLL%'&)T*J*Je*S*SLLK$1NOO	   Y'''E '       G '1        " 	~'ALL[\\\F "&dm&9&9&;&;!<!< 1	 1	Hg;//99D t| ;044X>>L'0Y'rN   !T\):):SWSa<kC<O<O<OgklIH$$ ;;LN#	 <    H#*<<DL0A0A3QY<#Z#ZZZZZZZFF#*<<DL0A0A3#G#GGGGGGGF%'&)T*J*Je*S*SLLX 1NOO	   Y'''FNL   
    TNL	       W1	 1	s   AB::
C*C%%C*+L8/AL84L8AI&%L8&
I30L82I33BL81L88
M*M%%M*(CU69;U66
V2 'V--V2r  c           	          |                                 D ]M}	 ddlm}  |dt          |dd           dd           n# t          $ r Y nw xY w|                     |           Nd S )Nr   invoke_hookon_session_finalizer  r  rs  r  r!   r-  )valueshermes_cli.pluginsr_  rH   rt   _cleanup_agent_resources)r  r  rx  _invoke_hooks       rC   _finalize_shutdown_agentsz'GatewayRunner._finalize_shutdown_agents  s    "))++ 	1 	1E	JJJJJJ)&ulDAA&%	        ))%0000	1 	1s   $=
A
	A
rx  c                    |dS 	 t          |d          rPt          |dd          }t          |t                    r|                    |           n|                                 n# t
          $ r Y nw xY w	 t          |d          r|                                 n# t
          $ r Y nw xY w	 ddlm}  |             dS # t
          $ r Y dS w xY w)z<Best-effort cleanup for temporary or cached agent instances.Nshutdown_memory_provider_session_messagescloser   )cleanup_stale_async_clients)	rr   rH   r   r  rh  rt   rj  agent.auxiliary_clientrk  )r  rx  session_messagesrk  s       rC   rd  z&GatewayRunner._cleanup_agent_resources!  s*   =F	u899 5 $+52Et#L#L .55 5223CDDDD22444 	 	 	D	
	ug&&  	 	 	D		JJJJJJ''))))) 	 	 	DD	s6   A A' '
A43A48$B 
B*)B*.C   
CCr  z.restart_failure_countsactive_session_keysc                 R   ddl }t          | j        z  }	 |                                r" |j        |                                          ni }n# t          $ r i }Y nw xY wi }|D ]}|                    |d          dz   ||<   	 t          ||d           dS # t          $ r Y dS w xY w)a  Increment restart-failure counters for sessions active at shutdown.

        Persists to a JSON file so counters survive across restarts.
        Sessions NOT in active_session_keys are removed (they completed
        successfully, so the loop is broken).
        r   Nr   r  )	r,  rY  _STUCK_LOOP_FILEr.   r-  r  rt   r'   rg  )r  rn  r,  r/   counts
new_countsr  s          rC   !_increment_restart_failure_countsz/GatewayRunner._increment_restart_failure_countsM  s     	d33	59[[]]JZTZ 0 0111FF 	 	 	FFF	 
& 	5 	5C$jja0014JsOO	dJt<<<<<< 	 	 	DD	s#   8A AAB 
B&%B&c                     ddl }t           j        z  }|                                sdS 	  |j        |                                          }n# t          $ r Y dS w xY wd} fd|                                D             }|D ]i}	  j        j	        
                    |          }|r5|j        s.d|_        |dz  }t                              d|||                    Z# t          $ r Y fw xY w|r+	  j                                         n# t          $ r Y nw xY w	 |                    d           n# t          $ r Y nw xY w|S )u,  Suspend sessions that have been active across too many restarts.

        Returns the number of sessions suspended.  Called on gateway startup
        AFTER suspend_recently_active() to catch the stuck-loop pattern:
        session loads → agent gets stuck → gateway restarts → repeat.
        r   Nc                 0    g | ]\  }}|j         k    |S r   )_STUCK_LOOP_THRESHOLD)r   r   r   r  s      rC   r  z>GatewayRunner._suspend_stuck_loop_sessions.<locals>.<listcomp>{  s*    VVVDAqa4;U6U6Ua6U6U6UrL   Tr   u_   Auto-suspended stuck session %s (active across %d consecutive restarts — likely a stuck loop)r`  )r,  rY  rp  r.   r-  r  rt   r   rg  rJ  r'   	suspendedru   rv   _saverb  )r  r,  r/   rq  rw  
stuck_keysr  r   s   `       rC   _suspend_stuck_loop_sessionsz*GatewayRunner._suspend_stuck_loop_sessionsh  s    	d33{{}} 	1	TZ 0 011FF 	 	 	11	 	VVVVFLLNNVVV
% 	 	K*377DD  &*EONINNH#VK%8  
      	"((****   	KK4K(((( 	 	 	D	 sH   "A 
AAAC
C+*C+1D 
DDD3 3
E ?E c                 *   ddl }t          | j        z  }|                                sdS 	  |j        |                                          }||v r1||= |rt          ||d           dS |                    d           dS dS # t          $ r Y dS w xY w)zClear the restart-failure counter for a session that completed OK.

        Called after a successful agent turn to signal the loop is broken.
        r   Nr  Tr`  )	r,  rY  rp  r.   r-  r  rg  rb  rt   )r  r  r,  r/   rq  s        rC   _clear_restart_failure_countz*GatewayRunner._clear_restart_failure_count  s    
 	d33{{}} 	F		TZ 0 011Ff$$;' 1%dF4@@@@@@KK4K00000 %$  	 	 	DD	s   =B *B 
BBc                   K   dd l }dd l}t                      }|st                              d           d S t          j                    }t          j        dk    rdd l	}ddl
m} g |dd}|                    d                                          }t
          j                                        }	|	                    dd            t#          t$                                                    j        j        }
t#          |	                    d	          p|
d
z            }|dz  dz  }|                                rt/          |          |	d	<   t/          |
          t/          |          g}|	                    d          r|                    |	d                    t
          j                            t6                              |                    |	d<    |j        t          j        d|t/          |          g|f|j        |j        |	d |             d S d                    d |D                       }d| d| d}t
          j                                        }	|	                    dd            |                     d          }|r*|                    |dd|g|j        |j        |	d           d S |                    dd|g|j        |j        |	d           d S )Nr   z4Could not locate hermes binary for detached /restartr   )windows_detach_popen_kwargsr  rr  u  
                import os, subprocess, sys, time
                pid = int(sys.argv[1])
                cmd = sys.argv[2:]
                deadline = time.monotonic() + 120

                def _alive(p):
                    # On Windows, os.kill(pid, 0) is NOT a no-op — it maps to
                    # GenerateConsoleCtrlEvent(0, pid) (bpo-14484). Use the
                    # Win32 handle-based existence check instead.
                    if os.name == 'nt':
                        import ctypes
                        k32 = ctypes.windll.kernel32
                        k32.OpenProcess.restype = ctypes.c_void_p
                        k32.WaitForSingleObject.restype = ctypes.c_uint
                        k32.GetLastError.restype = ctypes.c_uint
                        h = k32.OpenProcess(0x1000 | 0x100000, False, int(p))
                        if not h:
                            return k32.GetLastError() != 87
                        try:
                            return k32.WaitForSingleObject(h, 0) == 0x102
                        finally:
                            k32.CloseHandle(h)
                    try:
                        os.kill(int(p), 0)
                        return True
                    except ProcessLookupError:
                        return False
                    except PermissionError:
                        return True
                    except OSError:
                        return False

                while time.monotonic() < deadline:
                    if not _alive(pid):
                        break
                    time.sleep(0.2)
                _CREATE_NEW_PROCESS_GROUP = 0x00000200
                _DETACHED_PROCESS = 0x00000008
                _CREATE_NO_WINDOW = 0x08000000
                subprocess.Popen(
                    cmd,
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
                    creationflags=_CREATE_NEW_PROCESS_GROUP | _DETACHED_PROCESS | _CREATE_NO_WINDOW,
                )
                re  r   r   r   r   r   -crv  rw  envr  c              3   >   K   | ]}t          j        |          V  d S r  )r  quoter  s     rC   r  zAGatewayRunner._launch_detached_restart_command.<locals>.<genexpr>  s,      @@Tu{4((@@@@@@rL   while kill -0 " 2>/dev/null; do sleep 0.2; done; z gateway restartsetsidbash-lcT)rv  rw  r  start_new_session)!r  r}  r  ru   ru  r%   getpidr    r!   textwraphermes_cli._subprocess_compatr~  dedentrI   r&   copyr@  r   r"   r#   r$   r'   r.   r+   r(   r4   r5   r6   r7   Popenr  DEVNULLr  )r  r  r}  
hermes_cmdcurrent_pidr  r~  cmd_argvwatcherwatcher_envr8   r;   r>   rB   cmd	shell_cmd
setsid_bins                    rC    _launch_detached_restart_commandz.GatewayRunner._launch_detached_restart_command  s:     (**
 	LLOPPPFikk <7""OOOQQQQQQ::Y:	:Hoo.0 0` egga b *//++K OO-t444>>1133:ALKOOM::SlV>STTH$u,>M##%% W-0]]M*!,//]1C1CD
??<00 A%%k,&?@@@,.JOODMM*<U<U,V,VL)JwK0@0@L8L!)!)	 
 .-//   Fhh@@Z@@@@@%[ % %% % % 	 joo'')4000\\(++
 	VUI6!)!)"&       	*!)!)"&      rL   c           
      Z   t           j        dk    st          j                            d          sdS 	 ddl}ddl}|                    d          }|                    d          }|r|sdS 	 ddlm	}  |            }n# t          $ r d}Y nw xY wt          j                    }|                    |d	d
|ddgddd          }|j        pd                                t          |          k    rdS d}	t!          j        |          }
d| d|	 d|
 d|	 d|
 
}| d|                     dd          }|                    |d	dd|dd|g|j        |j        d           t*                              d||           dS # t          $ r&}t*                              d |           Y d}~dS d}~ww xY w)!a  Best-effort helper to bypass systemd's automatic restart delay.

        For planned in-chat restarts, the gateway exits cleanly so systemd does
        not record a failure.  However, units with RestartSteps still count
        automatic restarts and can delay repeated /restart tests.  A transient
        user service survives our cgroup teardown and explicitly starts the
        gateway as soon as this PID exits, while the unit keeps its normal
        backoff for real crash loops.
        linuxINVOCATION_IDNr   zsystemd-run	systemctl)get_service_namezhermes-gatewayz--usershowz--property=MainPIDz--valueTr  )capture_outputr{   r  rG   zsystemctl --userr  r  z reset-failed ; z	 restart z-planned-restart-r  r  z	--collectz--unitz/bin/shr  )rv  rw  r  z7Launched systemd planned-restart helper for %s (pid=%s)z3Failed to launch systemd planned-restart helper: %s)r    r!   r%   r&   r'   r  r}  r  hermes_cli.gatewayr  rt   r  runrv  rI   r+   r  r  r   r  r  ru   r:  r;  )r  r  r}  systemd_runr  r  service_namer  r  systemctl_userservice_argr  	unit_namer  s                 rC    _launch_systemd_restart_shortcutz.GatewayRunner._launch_systemd_restart_shortcut)  s    <7"""*..*I*I"F>	SMMM ,,}55K[11I i 0??????//11 0 0 0/0 )++K>> (  $ "  D !r((**c+.>.>>>/N+l33K: : :!: :1<: :!: :,7: : 
 (GG+GGOOPSUXYYI	 ")!)"&     KKI    
  	S 	S 	SLLNPQRRRRRRRRR	SsC   6E: ,A= <E: =B	E: BA!E: /B	E: :
F*F%%F*detachedvia_servicer  r  c                     j         rdS d _         _         _        d _         d fd}t	          j         |                      } j                            |           |                     j        j	                   dS )NFTr   c                  |   K   t          j        d           d {V                      d            d {V  d S )N皙?Trr  detached_restartservice_restart)rz  r<  rh  )r  r  r  s   rC   _run_restartz3GatewayRunner.request_restart.<locals>._run_restart~  sZ      -%%%%%%%%%))D8U`)aaaaaaaaaaarL   r   N)
r)  r(  r*  r+  rz  create_taskr  r-   add_done_callbackr  )r  r  r  r  rn   s   ```  rC   request_restartzGatewayRunner.request_restartv  s    % 	5"&!)$/!%)"	b 	b 	b 	b 	b 	b 	b 	b "<<>>22""4(((t5=>>>trL   >   restart_timeoutshutdown_timeoutrestart_interruptedc           
          t                      }	  j        j        5   j                                          fd j        j                                        D             }ddd           n# 1 swxY w Y   n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY wt          j
                    }d}|D ]#}|j        p|j        }|||z
                                  |k    r/|j         j        v r>|j        }	 j                            |	j                  }
|
;t                              d|j        t+          |	j        d|	j                             t-          dt.          j        |	d	          }t3          j        |
                    |                    } j                            |           |                     j        j                   |d
z  }%|rt                               d|           |S )u  Auto-continue fresh restart-interrupted sessions after startup.

        ``resume_pending`` already preserves the transcript AND the existing
        ``_is_resume_pending`` branch in ``_handle_message_with_agent``
        injects a reason-aware recovery system note on the next turn.  This
        method closes the UX gap by synthesizing that next turn once
        adapters are back online — the event text is empty so the existing
        injection path owns the wording and we never double up.

        Adapters that are not yet ready (adapter missing from
        ``self.adapters``) are skipped silently; their sessions stay
        ``resume_pending`` and will auto-resume on the next real user
        message, or when the platform reconnects — the reconnect watcher
        calls this again scoped to that ``platform``.

        ``platform`` (a ``Platform``) restricts the pass to sessions that
        originated on that platform.  The reconnect path passes it so a
        platform coming back online retries only its own sessions and never
        re-touches another platform's in-flight recoveries.  Sessions whose
        agent is already running are skipped regardless, so a session
        scheduled at startup is never resumed a second time.
        c                 ~    g | ]9}|j         r0|j        s)|j        "|j        j        v r|j        j        k    7|:S r  )resume_pendingrw  rE  resume_reason_AUTO_RESUME_REASONSr!   )r   r   r!   r  s     rC   r  zCGatewayRunner._schedule_resume_pending_sessions.<locals>.<listcomp>  sm       #+ "O 0+t/HHH!)U\-Bh-N-N 
 .O-N-NrL   Nz/Failed to enumerate resume-pending sessions: %sr   z5Skipping auto-resume for %s: adapter not ready for %srF   rG   T)r{   rE  r  internalr   z;Scheduled auto-resume for %d restart-interrupted session(s))!r   rg  _lock_ensure_loaded_lockedrJ  rb  rt   ru   rv   r   r   last_resume_marked_at
updated_attotal_secondsr  rq  rE  rQ  r'   r!   r;  rH   r  r  r  rz  r  handle_messager  r-   r  r  r:  )r  r!   r   r9   rM   r   	scheduledr   markerr  r   rK  rn   s   ``           rC   !_schedule_resume_pending_sessionsz/GatewayRunner._schedule_resume_pending_sessions  s   . 122	#) 	 	"88:::    '+'9'B'I'I'K'K  
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  	 	 	NNLcRRR11111	 lnn	  	  	E0DE4DF!sV|&B&B&D&Dv&M&M  D$888\Fm''88GK%FOWfoFF  
 
 !(-	  E &w'='=e'D'DEED"&&t,,,""4#9#ABBBNII 	KKM   s;   A; AA/#A; /A33A; 6A37A; ;
B+B&&B+c           
      X  K   t                               d           	 t          j                    | _        n# t
          $ r
 d| _        Y nw xY wt                               d| j        j                   	 ddlm	}  || j
                  }|Y|                    d          rDt                               d|                    dd	          |d
         |d         |d                    n2# t          $ r%}t                               d|           Y d}~nd}~ww xY w	 t          t!          j        dd                    }t                               d|           n# t          $ r Y nw xY w	 t!          j        dd          }|                                dv }|rt                               d           nt                               d|           n# t          $ r Y nw xY w	 ddlm}  |            }|r!|dk    rt                               d|           n# t          $ r Y nw xY w	 ddlm}	  |	dd           n# t          $ r Y nw xY w	 ddlm}
m}  |
            } ||          }|r5t                               d|           t                               d           n,# t          $ r t                               dd !           Y nw xY wd"}d#}d$}d$}	 dd%lm} t9          d& |                                D                       }t9          d' |                                D                       }n# t          $ r Y nw xY wt=          d( ||z   D                       }t!          j        d)d*                                          d+v pt=          d, ||z   D                       }|s|st                               d-           	 dd.lm }  |             n,# t          $ r t                               d/d !           Y nw xY w	 dd0l!m"} dd1l#m$}  | |            d23           n,# t          $ r t                               d4d !           Y nw xY w| j%        &                                 	 dd5l'm(} |)                                }|rt                               d6|           n2# t          $ r%}t                               d7|           Y d}~nd}~ww xY wtT          d8z  }|+                                r@t                               d9           	 |,                                 nz# t          $ r Y nnw xY w	 | j-        .                                }|rt                               d:|           n2# t          $ r%}t                               d;|           Y d}~nd}~ww xY w	 | /                                }|rt                               d<|           n2# t          $ r%}t                               d=|           Y d}~nd}~ww xY wd}d}g } g }!| j        j0        1                                D ]\  }"}#|#j2        s|d>z  }| 3                    |"|#          }$|$sk|"j4        }%d? tj          j6        7                                D             }&|%|&vrt                               d@|%           nt                               dA|%           |$8                    | j9                   |$:                    | j;                   |$<                    | j-                   |$=                    | j>                   |$?                    | j@                   | jA        |$_A        t                               dB|"j4                   | B                    |"j4        dCddD           	 | C                    |$|"           d{V }'|'rd|$| jD        |"<   | E                    |$           |d>z  }| B                    |"j4        dEddD           t                               dF|"j4                   n*t                               dG|"j4                   | F                    |$|"           d{V  |$jG        r| B                    |"j4        |$jH        rdHndI|$jI        |$jJ        D           |$jH        r|!n| }(|(K                    |"j4         dJ|$jJ                    |$jH        r"|#d>t          jM                    dKz   dL| jN        |"<   n]| B                    |"j4        dHddMD           |!K                    |"j4         dN           |#d>t          jM                    dKz   dL| jN        |"<   # t          $ r}t           O                    dO|"j4        |           | F                    |$|"           d{V  | B                    |"j4        dHdt          |          D           |!K                    |"j4         dJ|            |#d>t          jM                    dKz   dL| jN        |"<   Y d}~d}~ww xY w|dk    r.| rldPQ                    |           })t           O                    dQ|)           	 ddlm}	  |	dR|)           n# t          $ r Y nw xY w| R                    |)           d S |dk    r|!rhdPQ                    |!          })t                               dSt          | jN                  |)           	 ddlm}	  |	dTd           n# t          $ r Y nw xY wt                               dU|           n4t                               dV           t                               dW           | jD        | jT        _D        | U                                 d | _V        | W                    dX           t          | j%        jX                  }*|*rt                               dY|*           | j%        Y                    dZd[d\ | jD        Z                                D             i           d{V  |dk    rt                               d]|           	 dd^l[m\}+  |+| jD                   d{V },t          d_ |,                    d[i           7                                D                       }-t                               d`|-           n2# t          $ r%}t                               da|           Y d}~nd}~ww xY w| ^                                 d{V }.|.s?t=          db tT          dcz  tT          ddz  fD                       r| _                                 |dk    rt          j`        de           d{V  t                      }/| b                                 d{V  |/r?	 | c                    df           d{V  t                       n# t                       w xY w| e                                 	 dd5l'm(} |jf        }0g |_f        t          |0          D ]}\  }1}2t          jh        | i                    |2                     t                               dg|2                    dh                     |1diz  djk    rt          j`        d           d{V  ~n2# t          $ r%}t           O                    dk|           Y d}~nd}~ww xY wt          jh        | j                                           t          jh        | k                                           t          jh        | l                                           | jN        rPt                               dlt          | jN                  dmQ                    dn | jN        D                                  t          jh        | m                                           t          jh        | n                                           t                               do           d S )pz
        Start the gateway and all configured platform adapters.
        
        Returns True if at least one adapter connected successfully.
        zStarting Hermes Gateway...NzSession storage: %sr   )check_systemd_timing_alignmentmismatchzStale systemd unit detected: %s has TimeoutStopSec=%.0fs but drain_timeout=%.0fs (expected >=%.0fs). systemd may SIGKILL the gateway mid-drain. Run `hermes gateway service install --replace` to regenerate the unit, or shorten agent.restart_drain_timeout.unitz	(unknown)timeout_stop_secdrain_timeoutexpected_minz)check_systemd_timing_alignment failed: %srz  90zuAgent budget: max_iterations=%d (agent.max_turns from config.yaml, or HERMES_MAX_ITERATIONS from .env, or default 90)r  r  >   rd  r  yesr  z^Secret redaction: ENABLED (tool output, logs, and chat responses are scrubbed before delivery)zSecret redaction: DISABLED (HERMES_REDACT_SECRETS=%s). API keys and tokens may appear verbatim in chat output, session JSONs, and logs. Set security.redact_secrets: true in config.yaml to re-enable.get_active_profile_namer   zActive profile: %sr  starting)r  r  )detect_compromisedgateway_log_messagez%szCRun `hermes doctor` on the gateway host for full remediation steps.z1security advisory check failed at gateway startupTrp   )TELEGRAM_ALLOWED_USERSDISCORD_ALLOWED_USERSWHATSAPP_ALLOWED_USERSWHATSAPP_CLOUD_ALLOWED_USERSSLACK_ALLOWED_USERSSIGNAL_ALLOWED_USERSSIGNAL_GROUP_ALLOWED_USERSTELEGRAM_GROUP_ALLOWED_USERSTELEGRAM_GROUP_ALLOWED_CHATSEMAIL_ALLOWED_USERSSMS_ALLOWED_USERSMATTERMOST_ALLOWED_USERSMATRIX_ALLOWED_USERSDINGTALK_ALLOWED_USERSFEISHU_ALLOWED_USERSWECOM_ALLOWED_USERSWECOM_CALLBACK_ALLOWED_USERSWEIXIN_ALLOWED_USERSBLUEBUBBLES_ALLOWED_USERSQQ_ALLOWED_USERSYUANBAO_ALLOWED_USERSGATEWAY_ALLOWED_USERS)TELEGRAM_ALLOW_ALL_USERSDISCORD_ALLOW_ALL_USERSWHATSAPP_ALLOW_ALL_USERSWHATSAPP_CLOUD_ALLOW_ALL_USERSSLACK_ALLOW_ALL_USERSSIGNAL_ALLOW_ALL_USERSEMAIL_ALLOW_ALL_USERSSMS_ALLOW_ALL_USERSMATTERMOST_ALLOW_ALL_USERSMATRIX_ALLOW_ALL_USERSDINGTALK_ALLOW_ALL_USERSFEISHU_ALLOW_ALL_USERSWECOM_ALLOW_ALL_USERSWECOM_CALLBACK_ALLOW_ALL_USERSWEIXIN_ALLOW_ALL_USERSBLUEBUBBLES_ALLOW_ALL_USERSQQ_ALLOW_ALL_USERSYUANBAO_ALLOW_ALL_USERSr   platform_registryc              3   2   K   | ]}|j         	|j         V  d S r  )allowed_users_envr   r  s     rC   r  z&GatewayRunner.start.<locals>.<genexpr>v  sB       ) )()&)#) ) ) ) ) )rL   c              3   2   K   | ]}|j         	|j         V  d S r  )allow_all_envr  s     rC   r  z&GatewayRunner.start.<locals>.<genexpr>z  s@       + +$%?++ + + + + +rL   c              3   >   K   | ]}t          j        |          V  d S r  )r%   r9  r  s     rC   r  z&GatewayRunner.start.<locals>.<genexpr>  s;       
 
BIaLL
 
 
 
 
 
rL   GATEWAY_ALLOW_ALL_USERSrG   >   rd  r  r  c              3   h   K   | ]-}t          j        |d                                           dv V  .dS )rG   >   rd  r  r  N)r%   r9  r,   r  s     rC   r  z&GatewayRunner.start.<locals>.<genexpr>  s\       e
 e
 Ia""$$(<<e
 e
 e
 e
 e
 e
rL   zNo user allowlists configured. All unauthorized users will be denied. Set GATEWAY_ALLOW_ALL_USERS=true in ~/.hermes/.env to allow open access, or configure platform allowlists (e.g., TELEGRAM_ALLOWED_USERS=your_id).)discover_pluginsz*plugin discovery failed at gateway startupr9  )register_from_configF)accept_hooksz1shell-hook registration failed at gateway startupr1  z5Recovered %s background process(es) from previous runzProcess checkpoint recovery: %s.clean_shutdownu?   Previous gateway exited cleanly — skipping session suspensionz=Marked %d in-flight session(s) as resumable from previous runz(Session suspension on startup failed: %sz'Auto-suspended %d stuck-loop session(s)zStuck-loop detection failed: %sr   c                     h | ]	}|j         
S r   r  r   r   s     rC   	<setcomp>z&GatewayRunner.start.<locals>.<setcomp>  s    !Q!Q!Qa!'!Q!Q!QrL   uq   No adapter for '%s' — is the plugin installed? (platform is enabled in config.yaml but no plugin registered it)zNo adapter available for %szConnecting to %s...
connectingr[  r  u   ✓ %s connectedu   ✗ %s failed to connectrY  rZ  r  r_  r`  zfailed to connectz: failed to connectu   ✗ %s error: %sr  z0Gateway hit a non-retryable startup conflict: %sstartup_faileduS   Gateway started with no connected platforms — %d platform(s) queued for retry: %sdegradedzNo adapter could be created for any of the %d configured platform(s). Check that required dependencies are installed and credentials are set. Gateway will continue for cron job execution.zNo messaging platforms enabled.z5Gateway will continue running for cron job execution.runningz%s hook(s) loadedzgateway:startuprg  c                     g | ]	}|j         
S r   r  r  s     rC   r  z'GatewayRunner.start.<locals>.<listcomp>  s    @@@a!'@@@rL   z#Gateway running with %s platform(s)build_channel_directoryc              3   4   K   | ]}t          |          V  d S r  )r   )r   chss     rC   r  z&GatewayRunner.start.<locals>.<genexpr>  s(      WW3s88WWWWWWrL   z%Channel directory built: %d target(s)z"Channel directory build failed: %sc              3   >   K   | ]}|                                 V  d S r  )r.   )r   r/   s     rC   r  z&GatewayRunner.start.<locals>.<genexpr>  s>        
  
 KKMM 
  
  
  
  
  
rL   .update_pending.json.update_pending.claimed.jsonr4  skip_targetsz(Resumed watcher for recovered process %sr  d   c   z!Recovered watcher setup error: %sz;Starting reconnection watcher for %d failed platform(s): %sr  c              3   $   K   | ]}|j         V  d S r  r  r  s     rC   r  z&GatewayRunner.start.<locals>.<genexpr>  s$      BBa!'BBBBBBrL   zPress Ctrl+C to stop)oru   r:  rz  r6  rj  r*  r  rH  gateway.shutdown_forensicsr  r%  r'   rv   rt   r;  r   r%   r9  r,   hermes_cli.profilesr  r  r  hermes_cli.security_advisoriesr  r  gateway.platform_registryr  rS  plugin_entriesr  rc  r
  r  r:  agent.shell_hooksr  r  discover_and_loadrf  r2  recover_from_checkpointrY  r.   rb  rg  suspend_recently_activerz  rg  r   r  _create_adapterrF   r  __members__rb  set_message_handler_handle_messageset_fatal_error_handlerrk  set_session_storeset_busy_session_handlerr1  set_topic_recovery_fnr8  r$  re  r  rQ  r  r  has_fatal_errorrf  rc  rd  r(   r   r  r  ru  r+   r5   rn  r   rh  r  ri  r  loaded_hooksemitrB  gateway.channel_directoryr  sum_send_update_notification#_schedule_update_notification_watchr<  r^  _send_restart_notification(_send_home_channel_startup_notificationsrc  r  pending_watchersrH  r  _run_process_watcher_session_expiry_watcher_kanban_notifier_watcher_kanban_dispatcher_watcher_platform_reconnect_watcher_handoff_watcher)3r  r  
_alignment_e_effective_max_iter_redact_raw
_redact_onr  _profiler  r  r  	_adv_hits_adv_msg_builtin_allowed_vars_builtin_allow_all_vars_plugin_allowed_vars_plugin_allow_all_varsr  _any_allowlist
_allow_allr
  r:  r  r2  r7  r  _clean_markerrw  stuckconnected_countenabled_platform_countstartup_nonretryable_errorsstartup_retryable_errorsr!   rj  r   _pval_builtin_namesr(  targetr-  
hook_countr  	directorych_countrS  $planned_restart_notification_pendingwatchersrM  r  s3                                                      rC   startzGatewayRunner.start  s'      	0111	&!(!9!;!;D 	& 	& 	&!%D	&)4;+CDDD	JQQQQQQ778STTJ%*..*D*D%V NN6;7712/~.	 	 	  	J 	J 	JLLDbIIIIIIII	J
	"%bi0G&N&N"O"OKKE#   
  	 	 	D		)$;VDDK$**,,0JJJ >   
 3      	 	 	D		CCCCCC..00H <H	110(;;; 	 	 	D		;;;;;;  ztLLLLL 	 	 	D		        +*,,I**955H tX...)    	 	 	LLC      	!
&#
$ ')(*	CCCCCC#( ) )->-M-M-O-O) ) ) $ $  &+ + +):)I)I)K)K+ + + & &""  	 	 	D	 
 
"7:N"N
 
 
 
 
 Y8"==CCEEI]] 
ad e
 e
,/EEe
 e
 e
 b
 b

  	j 	NN[  	;;;;;; 	 	 	NN<t      		555555>>>>>>  UCCCCC 	 	 	LLC      	 	
$$&&&	A??????(@@BBI `SU^___ 	A 	A 	ANN<a@@@@@@@@	A %'88!! 	NKKYZZZ$$&&&&   N .FFHH	 lKK _ajkkk N N NI1MMMMMMMMN	?5577E QH%PPP 	? 	? 	?LL:A>>>>>>>>	? !"13#.0  *.)>)D)D)F)F t	 t	%Ho"* "a'"**8_EEG  !Q!Q83G3N3N3P3P!Q!Q!Q..NN[    NN#@%HHH ''(<===++D,LMMM%%d&8999,,T-UVVV))$*PQQQ&*&:G# KK-x~>>>00+"	 1   P $ B B7H U UUUUUUU ;.5DM(+::7CCC#q(O88 '2#'&*	 9    KK 2HNCCCCNN#=x~NNN 77JJJJJJJJJ. %<<$N9@9V+c::\c'.'?*1*E	 =     '<=44!< 
 '~NN1LNN   #8 *9,-.2n.>.>.C@ @D28< <<$N+5'+*=	 =    177'~BBB  
 '6()*..*:*:R*?< <.x8
    /CCC 33GXFFFFFFFFF44N#-#"%a&&	 5    )//8>0H0HQ0H0HIII . !"&."2"2R"74 4&x000000( a* 	#>??OQWXXXCCCCCC((7GU[\\\\\    D((000t%))+  "YY'?@@FNN>D233V  
GGGGGG,,*4(,     %    D +	    @AAASTTT )-%))+++##I... 011
 	9KK+Z888joo/@@4=+=+=+?+?@@@2
   	 	 	 	 	 	 	 QKK=OOO	DIIIIII55dmDDDDDDDDIWWy}}["/M/M/T/T/V/VWWWWWHKK?JJJJ 	D 	D 	DNN?CCCCCCCC	D
 7799999999 	7C  
  
 55== 
  
  
 
 
 	7 44666
 Q-$$$$$$$$$ 0U/V/V,--///////// 0 	66CC!% D          4555535555 	..000	A??????
 (8H02- (11 + +
7#D$=$=g$F$FGGGFT`HaHabbbs7b==!-*********	+
  	A 	A 	ALL<a@@@@@@@@	A 	D88::;;;
 	D99;;<<< 	D;;==>>> ! 	KKMD*++		BB4+ABBBBB  
 	D<<>>??? 	D1133444*+++ts  7 A
A4A1C& &
D0DD=E 
E$#E$(A#G 
GG3H 
HH"H6 6
IIAJ &KKAL. .
L;:L;>O &O87O8<!P &QQ$7R 
S&SST 
T)(T)-6U$ $
V.VV1W	 	
W8W33W8F,d22
g0<B)g++g0-i 
ii4k 
kkA6q< <
r+r&&r+u? ?v'B!y	 	
y8y33y8       @intervalc                 ,  K   t          j        d           d{V  | j        ro	 | j        t          j        |           d{V  +| j                                        }|D ]}|                    d          }|s| j                            |          s5	 |                     |           d{V  | j                            |           l# t          $ rP}t                              d||d           | j                            |t          |                     Y d}~d}~ww xY wnC# t           j        $ r  t          $ r'}t                              d|d           Y d}~nd}~ww xY wt          j        |           d{V  | j        mdS dS )u  Background task that processes pending CLI→gateway session handoffs.

        Polls ``state.db`` for sessions in ``handoff_state='pending'`` and,
        for each one:

        1. Atomically claims it (pending → running).
        2. Resolves the destination platform's configured home channel.
        3. Re-binds the gateway's session_key for that home channel to the
           CLI's existing session_id via ``session_store.switch_session`` so
           the full role-aware transcript replays on the next agent turn.
        4. Forges a synthetic ``MessageEvent`` (``internal=True``) with a
           handoff-notice text and dispatches through the normal gateway
           message pipeline so the agent runs and replies on the platform.
        5. Marks the row ``completed`` (or ``failed`` with ``handoff_error``).

        The CLI process is poll-blocked on the row's terminal state and
        prints the result to the user.
        r  Nr]   z!Handoff for session %s failed: %sTrp   zHandoff watcher tick error: %s)rz  r<  ri  r  list_pending_handoffsr'   claim_handoff_process_handoffcomplete_handoffrt   ru   rv   fail_handoffr+   CancelledErrorr;  )r  ra  pendingrowr  rM   s         rC   rB  zGatewayRunner._handoff_watcher  s.     * mAm 	*S#+!-111111111*@@BB" L LC!$J% ! +99*EE ! L"33C888888888(99*EEEE$ L L L?&d '    (55j#c((KKKKKKKKLL  )    S S S=sTRRRRRRRRS-)))))))))5 m 	* 	* 	* 	* 	*sI   !D- AD- 5CD- 
D)AD$D- $D))D- -E-E((E-rj  c           	      H  K   ddl m} ddlm}m} ddlm} |d         }|                    d          pd                                	                                }|st          d          	  ||          }n(# t          t          f$ r t          d	| d
          w xY w| j                            |          }	|	st          d| d          | j                            |          }
|
r|
j        st          d| d          |                    d          p	|dd         }d| }	 |	                    t%          |
j                  |           d{V }n7# t&          $ r*}t(                              d||d           d}Y d}~nd}~ww xY w|p|
j        rt%          |
j                  nd}|rd}nd} ||t%          |
j                  |
j        |dd|          }| j        j                            |          }|r|j        ni } |||                    dd          |                    dd                    }| j                            |           | j                            ||          }|t          d| d |           |                     |           |                     |           d!| d"} |||d#          }t(                              d$|||
j        ||           |                      |           d{V }|sdS i }|r||d%<   	 |	!                    t%          |
j                  ||pd&           d{V }n%# t&          $ r}t          d'|           |d}~ww xY wtE          |d(d          s#tE          |d)d*          }t          d'|           dS )+zAExecute one handoff row. Raises on failure (caller marks failed).r   r  )r  r  )r  r]   handoff_platformrG   zhandoff_platform is emptyzunknown platform 'r  z
platform 'z' is not active in this gatewayzno home channel configured for z(; run /sethome on the desired chat firsttitleN   u   Hermes — z/Handoff: create_handoff_thread raised on %s: %sTrp   r  r  zsystem:handoffHandoff)r!   r   	chat_namer  r  r  r  r  r  Fr	  zcould not switch session key u    → z([Session was just handed off from CLI ("z") to this channel. The full prior conversation history is loaded above. Briefly confirm you're working here and summarize what we were working on, so the user can continue from this device.])r{   r  r  ub   Handoff: dispatching synthetic turn for CLI session %s → %s (home=%s, thread=%s, session_key=%s)r  r   r   r   zadapter.send failed: r(  ru  rH  )#gateway.configr  gateway.sessionr  r  gateway.platforms.baser  r'   rI   r,   r*  r   KeyErrorrQ  r  rN  r   create_handoff_threadr+   rt   ru   r;  r  rf   rg  extrarg  get_or_create_sessionswitch_session_evict_cached_agent_release_running_agent_stater:  r.  r   rH   )r  rj  r  r  r  r  cli_session_idrL  r!   r   r[  	cli_titlethread_namenew_thread_idrM   effective_thread_iddest_chat_typedest_sourcerW  rx  r  switchedsynthetic_textsynthetic_eventresponse_textsend_metadatar  r  s                               rC   re  zGatewayRunner._process_handoff:  sH     ++++++DDDDDDDD777777T!344:AACCIIKK 	<:;;;	Fx..HHH% 	F 	F 	FDMDDDEEE	F -##H-- 	K]KKK  
 {++H55 	4< 	:- : : :  
 GGG$$:rr(:	 0I//		!")"?"?DL!!;# #      MM  	! 	! 	!LLAsT     !MMMMMM	! , 
#'>;Ct 	  	"%NN "N#m%%i$$)
 
 
 {,00::&2:""''$)II.G$N$N%*YY/I5%Q%Q
 
 
 	00=== %44[.QQRRR.RR   	  --- 	))+666G	 G G G 	 ',
 
 
 	3M4<9L		
 	
 	
 #22?CCCCCCCC 	 F
 )+ 	=)<M+&	G"<<DL))%&.$ (        FF
  	G 	G 	G<s<<==3F	G vy$// 	>&'+HIIC<s<<===	> 	>s<   ,A8 8%B*.E 
F# FF2M	 	
M+M&&M+r  c           	        K   t          j        d           d{V  i }d}| j        r	 | j                                         g }t          | j        j                                                  D ]?\  }}|j        r| j        	                    |          s(|
                    ||f           @|ri }|D ]S\  }}	|                    d          }
t          |
          dk    r|
d         nd}|                    |d          dz   ||<   Td	                    d
 t          |                                          D                       }t                               dt          |          |           |D ]`\  }}	 	 ddlm} |                    d          }
t          |
          dk    r|
d         nd} |d|j        |d           n# t*          $ r Y nw xY wd}t-          | dd          }|W|5  | j                            |          }t1          |t2                    r|d         n|r|nd}ddd           n# 1 swxY w Y   || j                            |          }|r|t6          ur|                     |           |                     |           | j                            |d           |                      |d           tC          | d          r| j"                            |d           t-          | dd          }t1          |tF                    r|                    |d           t-          | dd          }t1          |tF                    r|                    |d           | j        j$        5  d|_        | j        %                                 ddd           n# 1 swxY w Y   t           &                    d|j                   |                    |j        d           w# t*          $ r}|                    |j        d          dz   }|||j        <   ||k    rt           '                    d||j        |           | j        j$        5  d|_        | j        %                                 ddd           n# 1 swxY w Y   |                    |j        d           n#t           &                    d|||j        |           Y d}~Zd}~ww xY w|retQ          d |D                       }t          |          |z
  }|rt                               d||           nt                               d|           	 | )                                }|rt                               d|           n2# t*          $ r%}	t           &                    d|	           Y d}	~	nd}	~	ww xY wt-          | dd          }d }tU          j*                    |z
  |k    r	 tW          t-          | j,        d!d          pd          }|dk    r7| j        -                    |          }|rt                               d"|           n2# t*          $ r%}	t           &                    d#|	           Y d}	~	nd}	~	ww xY wtU          j*                    | _.        n2# t*          $ r%}t           &                    d$|           Y d}~nd}~ww xY wt_          |          D ]%}| j        s nt          j        d           d{V  &| j        dS dS )%a  Background task that finalizes expired sessions.

        Runs every ``interval`` seconds (default 5 min).  For each session
        whose reset policy has expired, invokes ``on_session_finalize``
        hooks, cleans up the cached AIAgent's tool resources, evicts the
        cache entry so it can be garbage-collected, and marks the session
        so it won't be finalized again.
        rY  Nr  rZ  r  r  r   r   r  c              3   *   K   | ]\  }}| d | V  dS )rZ  Nr   )r   r  cs      rC   r  z8GatewayRunner._session_expiry_watcher.<locals>.<genexpr>  sA       . .'+q!1

q

. . . . . .rL   z,Session expiry: %d sessions to finalize (%s)r^  rG   r`  session_expiredra  r~  _pending_model_notesr  r  TzSession expiry finalized for %szkSession finalize gave up after %d attempts for %s: %s. Marking as finalized to prevent infinite retry loop.z*Session finalize failed (%d/%d) for %s: %sc              3   .   K   | ]\  }}|j         d V  dS )r   N)expiry_finalized)r   r  r  s      rC   r  z8GatewayRunner._session_expiry_watcher.<locals>.<genexpr>Y  sB          "a!:L            rL   z3Session expiry done: %d finalized, %d pending retryz!Session expiry done: %d finalizedz+Agent cache idle sweep: evicted %d agent(s)zIdle agent sweep failed: %s_last_session_store_prune_tsr  r   session_store_max_age_daysz,SessionStore prune: dropped %d stale entrieszSessionStore prune failed: %sz Session expiry watcher error: %s)0rz  r<  ri  rg  rI  r  rJ  r   r  _is_session_expiredr(   r  r   r'   r5   sortedru   r:  rc  r_  r  rt   rH   r|  r   rS  rq  r  rd  r{  r.  r@  r  rr   r  r6   r  rx  r;  rv   r7  _sweep_idle_cached_agentsr   r   r  prune_old_entriesr  range)r  ra  _finalize_failures_MAX_FINALIZE_RETRIES_expired_entriesr  r   
_platforms_krD  _parts_plat_plat_summaryre  	_platform_cached_agent_cache_lock_cachedr  r  r  failures_done_failed_idle_evicted_last_prune_ts_prune_interval_max_age_prunedr  s                                 rC   r>  z%GatewayRunner._session_expiry_watcher  s)	      mB-/ !m d	'^D"11333#% "&t'9'B'H'H'J'J"K"K : :JC- ! -AA%HH ! $++S%L9999#  24J"2 I IB!##-0[[1__q		),6NN5!,D,Dq,H
5))$(II . ./5j6F6F6H6H/I/I. . . % %M KKF,--}  
 #3 L LJCK!VVVVVV%(YYs^^F58[[1__q		"I(L 5+0+;)2'8	      ) ! ! ! D!
 )-&-d4G&N&N&2!, { {*.*;*?*?*D*D>HRW>X>X0z

ip^z^e^evz{ { { { { { { { { { { { { { {
 )0,0,@,D,DS,I,IM( I]BY-Y-Y 99-HHH
 00555 599#tDDD<<S$GGG"4)?@@ E 599#tDDD-4T;OQU-V-V*%&8$?? >.223===18?WY]1^1^.%&<dCC B266sDAAA!/5 7 759E2 .446667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 =!,   +..u/?FFFF$   #5#9#9%:JA#N#NQR#R?G*5+;<#'<<<"NN!W (%*:A  
 "&!3!9 ; ;9= 6 $ 2 8 8 : : :; ; ; ; ; ; ; ; ; ; ; ; ; ; ; /2253CTJJJJ"LL L (*?AQST  & $     &6       E ""233e;G Q!7   
 ?  D$($B$B$D$DM$ I)   ! D D DLL!>CCCCCCCCD ")/Ms!S!S"(9;;//AAJ#&#DK1MqQQVUV$ $ $a<<&*&8&J&J8&T&TG& " &$R$+!" !" !" % J J J%DbIIIIIIIIJ8<	D5 D D D?CCCCCCCCD 8__ ' '} EmA&&&&&&&&&&I m d	' d	' d	' d	' d	's&  EY= ;AGO*
GO*GO*/>H9-O*9H=	=O* H=	D4O*5!N"O*"N&	&O*)N&	*>O*(Y= *
S4AS!Q=1S=RSRASY= SA*Y= =1U/ .Y= /
V9VY= V0Y= A"X2 1Y= 2
Y!<YY= Y!!Y= =
Z,Z''Z,c                 J    	 ddl m}  |            pdS # t          $ r Y dS w xY w)z0Return the profile name this gateway represents.r   r  r   )r#  r  rt   )r  r  s     rC   r  z"GatewayRunner._active_profile_name  sO    	CCCCCC**,,9	9 	 	 	99	s    
""c           
      	  K   d}t          j        d           d{V  | j        r| j        s7t	          d          D ]&}| j        s dS t          j        d           d{V  'Ft          j                    }t          | j                                                  D ]}| j        s dS | j        |         }|	                    d          r0||d         k     r=|d         }|d	         dz   }t                              d
|j        |           d}	 |                     ||          }|s)t                              d|j                   | j        |= |                    | j                   |                    | j                   |                    | j                   |                    | j                   |                    | j                   | j        |_        |                     ||           d{V }	|	r|| j        |<   |                     |           | j        | j        _        | j        |= |                     |j        ddd           t                              d|j                   	 ddlm }
  |
| j                   d{V  n# tB          $ r Y nw xY w	 | "                    |           nD# tB          $ r& t          #                    d|j        d           Y nw xY w|j$        rs|j%        sl|                     |j        d|j&        |j'                   t                              d|j        |j'                   tQ          |           d{V  | j        |= n|                     |j        d|j&        |j'        pd           tS          dd|dz
  z  z  |          }||d	<   t          j                    |z   |d<   t                              d|j        |           tQ          |           d{V  d# tB          $ r}|tQ          |           d{V  |                     |j        ddtU          |                     tS          dd|dz
  z  z  |          }||d	<   t          j                    |z   |d<   t                              d|j        ||           Y d}~d}~ww xY wt	          d          D ]&}| j        s dS t          j        d           d{V  '| j        dS dS )u7  Background task that periodically retries connecting failed platforms.

        Uses exponential backoff: 30s → 60s → 120s → 240s → 300s (cap).
        Retryable failures (network/DNS blips) keep retrying at the backoff
        cap indefinitely — they self-heal once connectivity returns, so a
        transient outage never requires manual intervention. Non-retryable
        failures (bad auth, etc.) drop out of the queue immediately. The
        circuit breaker (``_pause_failed_platform`` / ``/platform pause``)
        remains available for manual operator control via ``/platform list``
        and ``/platform resume <name>``, but is no longer triggered
        automatically — auto-pausing a recovered platform was the cause of
        bots silently staying dead after a transient DNS failure.
        r  
   Nr_  r   r  rb  r  ra  zReconnecting %s (attempt %d)...zGReconnect %s: adapter creation returned None, removing from retry queuer  r[  u   ✓ %s reconnected successfullyr   r  rJ   z3resume-pending reschedule after %s reconnect failedTrp   rZ  zAReconnect %s: non-retryable error (%s), removing from retry queuerY  zfailed to reconnectr  z&Reconnect %s failed, next retry in %dsz)Reconnect %s error: %s, next retry in %ds)+rz  r<  ri  r  r  r   r  r  rB  r'   ru   r:  rF   r+  rv   r-  r.  r/  rk  r0  rg  r1  r1  r2  r8  r$  r  rQ  r  rh  re  r6  r  rt   r  r;  r3  rf  rc  rd  r  minr+   )r  _BACKOFF_CAPr  r   r!   r:  rj  attemptr   r(  r  backoffr  s                rC   rA  z)GatewayRunner._platform_reconnect_watcher  sU      mBm c	') r + +A= !-**********.""C !7!<!<!>!>?? P P} FF-h7 88H%% l+++"&x.z*Q.5NG  
 }"228_MMG" !e$N   !28< //0DEEE33D4TUUU--d.@AAA44T5]^^^11$2XYYY.2.BG+$($F$FwPX$Y$YYYYYYYG M?29h/>>wGGG8<,5 28<<<$N+6'+*.	 =    $Ex~VVV!YYYYYY"9"9$-"H"HHHHHHHHH( ! ! ! D! BBHBUUUU(   "LL U ()- )      !0 *?9V *?<<$N+2'.'?*1*E	 =    _$NG,G   6g>>>>>>>>> 28<<<<$N+5'.'?*1*E*^I^	 =    #&bA'A+,>&?"N"N+2Z(-1^-=-=-G\*D$NG   6g>>>>>>>>> !   * 6g>>>>>>>>>88 '1#'&)!ff	 9    ""gk(:";\JJG'.D$)-)9)9G)CD&NNC 7       %6 2YY ' '} FFmA&&&&&&&&&&G m c	' c	' c	' c	' c	'sj   A PD"P6JP
J PJ  P$J<:P<,K,(P+K,,DP
R2BR--R2r  rr  r  r  c                    K   |rd _         | _        | _         j         j         d{V  dS d fd}t	          j         |                       _         j         d{V  dS )z-Stop the gateway and disconnect all adapters.TNr   c            	      X  K   dt           dd fd} t                              dj        rdnd           t	          j                    dt          ffd}d_        d	_        	                                 d {V  t                              d
 |                       j
        }g }t          j                                                  D ]|\  }}|t          u r	 j                            |j        rdnd           |                    |           J# t$          $ r&}t                              d||           Y d }~ud }~ww xY wt	          j                    }                    |           d {V \  }}	t                              d |            t	          j                    |z
  |	t+          |                                                     |	s]|D ]Z}|j        vrO	 j                            |           '# t$          $ r&}t                              d||           Y d }~Rd }~ww xY w[|	rt                              d|                                           j        rdnd}
t          j                                                  D ]^\  }}|t          u r	 j                            ||
           ,# t$          $ r&}t                              d||           Y d }~Wd }~ww xY w                    j        rt4          nt6                     t9          j                                                    dz   }j        rt9          j                                                    |k     r_                    d           t9          j        d           d {V  j        r)t9          j                                                    |k     _ | d           t                              d |                       j        rUj         rN	 !                                 d {V  n2# t$          $ r%}t          "                    d|           Y d }~nd }~ww xY w#                    |           tI          dd           }tI          dd           }|||5  t          |%                                          }|&                                 d d d            n# 1 swxY w Y   |D ]6}tO          |tP                    r|d         n|})                    |           7t          j*                                                  D ]
\  }}t	          j                    }	 |+                                 d {V  n8# t$          $ r+}t                              d|j,        |           Y d }~nd }~ww xY w	 |-                                 d {V  t                              d|j,        t	          j                    |z
             # t$          $ rA}t          "                    d|j,        t	          j                    |z
  |           Y d }~d }~ww xY wt                              d |                       t          j.                  D ] }|j/        u r|0                                 !j.        &                                 j*        &                                 t          j                  D ]}1                    |           j        &                                 j2        &                                 tg          d          rj4        &                                 j5        &                                 j6        &                                 tg          d           rj7        &                                 j8        9                                  | d!           t                              d" |                       	 dd#l:m;}  |             n2# t$          $ r%}t                              d$|           Y d }~nd }~ww xY wtI          d%d           fD ]q}|rtI          |d&d           nd }|tg          |d'          s*	 |<                                 @# t$          $ r%}t                              d(|           Y d }~jd }~ww xY wt                              d) |                       dd*l=m>}m?}  |              |             |	s.	 t          d+z  A                                 n*# t$          $ r Y nw xY wt                              d,           |r4B                    ts          |C                                                     j        rjD        	 t          t                      t	          j                    t          jH                  t          j                   d-d .           n2# t$          $ r%}t                              d/|           Y d }~nd }~ww xY wj        rfjH        r_I                                 t          jK        d0k    st          jM        N                    d1          st          nd_P        jQ        pd2_Q        d_        tI          d3d          r=j        s6t                              d4                               d5jQ                   n                    d6jQ                   t                              d7 |                       d S )8Nphaser   c                    	 ddl m} |                                }|rt                              d| |           n3# t
          $ r&}t                              d| |           Y d}~nd}~ww xY w	 ddlm}  |             n3# t
          $ r&}t                              d| |           Y d}~nd}~ww xY w	 ddl	m
}  |             dS # t
          $ r'}t                              d	| |           Y d}~dS d}~ww xY w)
uw  Kill tool subprocesses + tear down terminal envs + browsers.

                Called twice in the shutdown path: once eagerly after a
                drain timeout forces agent interrupt (so we reclaim bash/
                sleep children before systemd TimeoutStopSec escalates to
                SIGKILL on the cgroup — #8202), and once as a final
                catch-all at the end of _stop_impl() for the graceful
                path or anything respawned mid-teardown.

                All steps are best-effort; exceptions are swallowed so
                one subsystem's failure doesn't block the rest.
                r   r1  z,Shutdown (%s): killed %d tool subprocess(es)z(process_registry.kill_all (%s) error: %sN)cleanup_all_environmentsz'cleanup_all_environments (%s) error: %s)cleanup_all_browsersz#cleanup_all_browsers (%s) error: %s)rf  r2  kill_allru   r:  rt   r;  tools.terminal_toolr  tools.browser_toolr  )r  r2  _killedrD  r  r  s         rC   _kill_tool_subprocesseszGGatewayRunner.stop.<locals>._stop_impl.<locals>._kill_tool_subprocessesi  s   	XGGGGGG.7799G J!7   ! X X XLL!KUTVWWWWWWWWXWLLLLLL,,....  W W WLL!JESUVVVVVVVVWSGGGGGG((*****  S S SLL!FrRRRRRRRRRSsE   8; 
A+A&&A+/B   
B0
B++B04C 
C7C22C7zStopping gateway%s...z for restartrG   c                  0    t          j                     z
  S r  )r   r  )_stop_started_ats   rC   _phase_elapsedz>GatewayRunner.stop.<locals>._stop_impl.<locals>._phase_elapsed  s    ~''*:::rL   FTz5Shutdown phase: notify_active_sessions done at +%.2fsr  r  z/pre-drain mark_resume_pending failed for %s: %szhShutdown phase: drain done at +%.2fs (drain took %.2fs, timed_out=%s, active_at_start=%d, active_now=%d)z2clear_resume_pending after drain failed for %s: %szYGateway drain timed out after %.1fs with %d active agent(s); interrupting remaining work.z%mark_resume_pending failed for %s: %sr   r5  r:  zpost-interruptz7Shutdown phase: post-interrupt tool kill done at +%.2fsz-Failed to launch detached gateway restart: %sr~  r|  r   u'   ✗ %s background-task cancel error: %su   ✓ %s disconnected (%.2fs)u'   ✗ %s disconnect error after %.2fs: %sz3Shutdown phase: all adapters disconnected at +%.2fsrr  rw  zfinal-cleanupz6Shutdown phase: final-cleanup tool kill done at +%.2fs)shutdown_cached_clientsz!shutdown_cached_clients error: %srg  _dbrj  zSessionDB close error: %sz.Shutdown phase: SessionDB close done at +%.2fs)remove_pid_filerelease_gateway_runtime_lockr  u   Skipping .clean_shutdown marker — drain timed out with interrupted agents; next startup will suspend recently active sessions.)requested_atr  r  r  z7Failed to write planned restart notification marker: %sdarwinr  zGateway restart requestedrp  u   Gateway stopped by an unexpected signal — persisting gateway_state=running so container_boot auto-starts on the next boot (issue #42675)r  stoppedz&Gateway stopped (total teardown %.2fs))Rr+   ru   r:  r(  r   r  r   ri  r'  r\  r%  r  rq  r   r  rg  mark_resume_pendingr(   rt   r;  r@  r   rp  clear_resume_pendingrv   rB  !_INTERRUPT_REASON_GATEWAY_RESTART"_INTERRUPT_REASON_GATEWAY_SHUTDOWNrz  r6  r  r<  r*  r  ru  rf  rH   rb  r  r   rS  rd  rQ  cancel_background_tasksrF   r  r  r-  cancelr|  r!  rr   rr  rs  r  rw  rl  r)   rl  r  rj  r  r  r  rY  touchrs  rB  r,  rg  r\  r   r+  r  r    r!   r%   r&   r'   r  r&  ro  )r  r  r  _pre_drain_keys_sk_agentrD  _drain_started_atr  r?  _resume_reasoninterrupt_deadliner  r  _cache_idle_agents_entryr!   r   _adapter_started_at_task_session_keyr  
_db_holderr  r  r  r  r  s                              @rC   
_stop_implz&GatewayRunner.stop.<locals>._stop_implh  s      Ss  St  S  S  S  SD KK'"&"9Ar    $~//;E ; ; ; ; ; ; "DM!DN ::<<<<<<<<<KKG    
 1G *,O#D$8$>$>$@$@AA 
] 
]V444]&::-1-D\))J\   $**3////  ] ] ]LL!RTWY[\\\\\\\\] !% 0 0-1-F-Fw-O-O'O'O'O'O'O'O$M9KKC    #44M""))++     +  C$"666 .CCCHHHH(   "LL T #R        7  <o--//  6 *.)@X%%FX  $((<(B(B(D(D#E#E 	 	KC!888 *>>sNSSSS$   C       
 ..9=9Px55Vx   &-%=%?%?%D%D%F%F%L"* -w/G/I/I/N/N/P/PSe/e/e//
;;;!-,,,,,,,,, * -w/G/I/I/N/N/P/PSe/e/e ('(8999M"N$$  
 & U4+A UU??AAAAAAAAAA  U U ULL!PRSTTTTTTTTU **=999 "$(;TBBKT>488F&6+=  # ##'#8#8LLLNNN# # # # # # # # # # # # # # # + : :F%/%>%>Jq		F  11&9999%)$-*=*=*?*?%@%@  !'&*n&6&6#_!99;;;;;;;;;;  _ _ _LL!JHN\]^^^^^^^^_!,,.........KK5 ((+>>   
 !   LLA ((+>>	        KKE    
 d455  DO++"((***M!!! $T%9 : : @ @11,???? &&(((#))+++t566 4+11333"((***#))+++t^,, *!''))) $$&&& $#O444KKH    FJJJJJJ'')))) F F F@"EEEEEEEEF  $WT?D%I%IJ B B
:DNgj%666$;gc7&;&;;BIIKKKK  B B BLL!<bAAAAAAAABKK@    
 UTTTTTTTO((***  
!$55<<>>>>    D '    R66s=;M;M;O;O7P7PQQQ& _4+G+O_%:<<,0IKK+/0I+J+J(,T-C(D(D 
  $     ! _ _ _LL!Z\]^^^^^^^^_ & U4+D U55777 |x//rz~~o7V7V/ 65 
 %)$5$T9T!"DN, t95AA J$Ja J3  
 ++It7HIIII++It7HIIIKK@..BRBRSSSSSs    9D
E
$EE
(H
H3H..H3)K
K5K00K5P/ /
Q9QQ6S  S$'S$$U??
V4	!V//V48AX
Y6YY
` 
a
%aa
b  
c*c

cd1 1
d>=d> Ag8 8
h'h""h'r  )r(  r*  r+  r-  rz  r  )r  rr  r  r  r  s   `    rC   rh  zGatewayRunner.stopX  s        	8&*D#%5D"(7D%?&/!!!!!!!Fo	T o	T o	T o	T o	T o	Tb "-jjll;;orL   c                 H   K   | j                                          d{V  dS )zWait for shutdown signal.N)rl  waitr  s    rC   wait_for_shutdownzGatewayRunner.wait_for_shutdown  s3      "'')))))))))))rL   c                    t          |d          rnt          |j        t                    rT|j                            d| j        j                   |j                            dt          | j        dd                     	 ddlm	} |
                    |j                  rX|                    |j        |          }|t          |d          r| |_        |S t                              d	|j                   dS n8# t           $ r+}t                              d
|j        |           Y d}~nd}~ww xY w|t$          j        k    rddlm}m}  |            st                              d           dS  ||          }t1          j        dd          }|sj	 t5                      }	t7          |	dddd          }
|
dvr3t9          |
                                                                          }n# t           $ r Y nw xY w|pd}|dvrt                              d|           d}||_        |S |t$          j         k    r9ddl!m"}m#}  |            st                              d           dS  ||          S |t$          j$        k    r9ddl%m&}m'}  |            st                              d           dS  ||          S |t$          j(        k    r9ddl)m*}m+}  |            st                              d           dS  ||          S |t$          j,        k    r9ddl-m.}m/}  |            st                              d           dS  ||          S |t$          j0        k    r9ddl1m2}m3}  |            st                              d            dS  ||          S |t$          j4        k    r9dd!l5m6}m7}  |            st                              d"           dS  ||          S |t$          j8        k    r9dd#l9m:}m;}  |            st                              d$           dS  ||          S |t$          j<        k    r9dd%l=m>}m?}  |            st                              d&           dS  ||          S |t$          j@        k    r9dd'lAmB}mC}  |            st                              d(           dS  ||          S |t$          jD        k    r9dd)lEmF}mG}  |            st                              d*           dS  ||          S |t$          jH        k    r9dd+lImJ}mK}   |             st                              d,           dS  ||          S |t$          jL        k    r9dd-lMmN}!mO}"  |"            st                              d.           dS  |!|          S |t$          jP        k    r9dd/lQmR}#mS}$  |$            st                              d0           dS  |#|          S |t$          jT        k    rBdd1lUmV}%mW}&  |&            st                              d2           dS  |%|          }| |_        |S |t$          jX        k    r9dd3lYmZ}'m[}(  |(            st                              d4           dS  |'|          S |t$          j\        k    r9dd5l]m^})m_}*  |*            st                              d6           dS  |)|          S |t$          j`        k    r9dd7lamb}+mc},  |,            st                              d8           dS  |+|          S |t$          jd        k    r1dd9lemf}-mg}. |.st                              d:           dS  |-|          S dS );zCreate the appropriate adapter for a platform.

        Checks the platform_registry first (plugin adapters), then falls
        through to the built-in if/elif chain for core platforms.
        rx  r  r  Fr   r   Ngateway_runnerzWPlatform '%s' is registered but adapter creation failed (check dependencies and config)z,Platform registry lookup for '%s' failed: %s)TelegramAdaptercheck_telegram_requirementsz+Telegram: python-telegram-bot not installedHERMES_TELEGRAM_NOTIFICATIONSrG   r  rg  r   notificationsr  	important>   r  r  z[Unknown telegram notifications mode '%s', defaulting to 'important' (valid: all, important))WhatsAppAdaptercheck_whatsapp_requirementsz8WhatsApp: Node.js not installed or bridge not configured)WhatsAppCloudAdapter!check_whatsapp_cloud_requirementsu@   WhatsApp Cloud: aiohttp/httpx missing — reinstall hermes-agent)SlackAdaptercheck_slack_requirementszGSlack: slack-bolt not installed. Run: pip install 'hermes-agent[slack]')SignalAdaptercheck_signal_requirementsz8Signal: SIGNAL_HTTP_URL or SIGNAL_ACCOUNT not configured)EmailAdaptercheck_email_requirementszQEmail: EMAIL_ADDRESS, EMAIL_PASSWORD, EMAIL_IMAP_HOST, or EMAIL_SMTP_HOST not set)
SmsAdaptercheck_sms_requirementszJSMS: aiohttp not installed or TWILIO_ACCOUNT_SID/TWILIO_AUTH_TOKEN not set)DingTalkAdaptercheck_dingtalk_requirementszLDingTalk: dingtalk-stream not installed or DINGTALK_CLIENT_ID/SECRET not set)FeishuAdaptercheck_feishu_requirementsz?Feishu: lark-oapi not installed or FEISHU_APP_ID/SECRET not set)WecomCallbackAdapter!check_wecom_callback_requirementsz5WeComCallback: aiohttp/httpx/defusedxml not installed)WeComAdaptercheck_wecom_requirementsz;WeCom: aiohttp not installed or WECOM_BOT_ID/SECRET not set)WeixinAdaptercheck_weixin_requirementsz*Weixin: aiohttp/cryptography not installed)MatrixAdaptercheck_matrix_requirementsz\Matrix: mautrix not installed or credentials not set. Run: pip install 'mautrix[encryption]')APIServerAdaptercheck_api_server_requirementsz!API Server: aiohttp not installed)WebhookAdaptercheck_webhook_requirementszWebhook: aiohttp not installed)MSGraphWebhookAdapter"check_msgraph_webhook_requirementsz&MSGraph webhook: aiohttp not installed)BlueBubblesAdaptercheck_bluebubbles_requirementsz`BlueBubbles: aiohttp/httpx missing or BLUEBUBBLES_SERVER_URL/BLUEBUBBLES_PASSWORD not configured)	QQAdaptercheck_qq_requirementszIQQBot: aiohttp/httpx missing or QQ_APP_ID/QQ_CLIENT_SECRET not configured)YuanbaoAdapterWEBSOCKETS_AVAILABLEz>Yuanbao: websockets not installed. Run: pip install websockets)hrr   r   rx  r6   r  r  r  rH   r%  r  is_registeredrF   create_adapterr  ru   ru  rt   r;  r  r  gateway.platforms.telegramr  r  rv   r%   r9  r  r   r+   rI   r,   _notifications_modeWHATSAPPgateway.platforms.whatsappr  r  WHATSAPP_CLOUD gateway.platforms.whatsapp_cloudr  r  SLACKgateway.platforms.slackr  r  SIGNALgateway.platforms.signalr  r  EMAILgateway.platforms.emailr  r  SMSgateway.platforms.smsr  r  DINGTALKgateway.platforms.dingtalkr  r  FEISHUgateway.platforms.feishur  r  WECOM_CALLBACK gateway.platforms.wecom_callbackr  r  WECOMgateway.platforms.wecomr  r  WEIXINgateway.platforms.weixinr  r  MATRIXgateway.platforms.matrixr  r  r  gateway.platforms.api_serverr  r  r  gateway.platforms.webhookr   r  r  !gateway.platforms.msgraph_webhookr  r  BLUEBUBBLESgateway.platforms.bluebubblesr  r  QQBOTgateway.platforms.qqbotr  r  YUANBAOgateway.platforms.yuanbaor  r	  )/r  r!   r  r  r   r  r  r  _notify_mode_gw_cfg_rawr  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  r  r  r  r  r  r	  s/                                                  rC   r+  zGatewayRunner._create_adapter  s	    67## 	
6<(F(F 	L##)3   L##*%?GG  	\CCCCCC ..x~>> +::8>6RR&
 w(899 615."N 6N  
 t#$  	\ 	\ 	\LLGYZ[[[[[[[[	\ x(((________..00 LMMMt%of--G 9%DbIIL 244G"7I{JP_``D:--'*4yy'8'8'>'>'@'@    D'6;L#777H   
  +*6G'N***________..00 YZZZt"?6***000        5466 V   t''///''VVVVVVVV++-- hiiit<'''((YYYYYYYY,,.. YZZZt =(((''VVVVVVVV++-- rssst<'''%%PPPPPPPP))++ klllt:f%%%***________..00 mnnnt"?6***((YYYYYYYY,,.. `aaat =(((000        5466 VWWWt''///''VVVVVVVV++-- \]]]t<'''((YYYYYYYY,,.. KLLLt =(((((YYYYYYYY,,.. }~~~t =(((,,,dddddddd0022 BCCCt##F+++)))\\\\\\\\--// ?@@@t$nV,,G%)G"N111        6577 GHHHt((000---hhhhhhhh1133    B  C  C  Ct%%f---''PPPPPPPP((** jkkkt9V$$$)))VVVVVVVV' _```t!>&)))ts2    AC9  C9 9
D.!D))D.AG+ +
G87G8r   c           	      `  K   | j                             |j                  }|sdS t          | dd          }d}|r*t	          |d          r|                    |j                  }|                     |          }|dk    rt          |dd          rz	 |                    |j        |j	        ||           d{V }t          |dd	          rdS n<# t          $ r/ t                              d
t          |dd          d           Y nw xY w|                    |j        ||           d{V  dS )zIDeliver a setup/operational notice using platform-specific privacy rules.Nr  publicget_notice_deliveryprivater  r   r(  Fz7[%s] send_private_notice failed, falling back to publicr!   r  Trp   )rQ  r'   r!   rH   rr   r4  r  send_private_noticer   r  rt   ru   r;  r   )r  r  r   r   r  notice_deliveryr   r  s           rC   _deliver_platform_noticez&GatewayRunner._deliver_platform_notice  s     -##FO44 	Fx.." 	Jgf&;<< 	J$88IIO33F;;i''GFIt,L,L'&::NN%	  ;           69e44 F   MFJ44!       ll6>7XlFFFFFFFFFFFs   :C 6DDc                 P   K   j         }t          t          dd                    }|s*	 ddlm}  |d  j                  }n4# t          $ r'}t                              d|           g }Y d}~nd}~ww xY w|D ]}t          |t                    s|                    d	          }|d
k    rMt                              d|                    d          |j        r|j        j        nd|j        pd            dS |dk    rI|                    d          }	t          |	t                     rt#          j        |	          j         } n	|dk    r n|rn|j        >                     |          s't                              d|j        j                   dS n                     |          snt                              d|j        |j        |j        j                   |j        dk    r/                     |j                  dk    r|j        r|j        j        nd}
 j                            |
|j                  rdS  j                            |
|j        |j        pd          }|rM j                            |j                  }|r+|                    |j        d| d|
 d| d           d{V  nb j                            |j                  }|r!|                    |j        d           d{V   j                            |
|j                   dS                      |          }t           di           }|                    |          rAj         pd!                                }"                                }|dv rd}nX|dv rd }nQd}|rF	 dd!l#m$} n# t          $ r d}Y nw xY w|*	  ||          }|r|j%        nd}n# t          $ r d}Y nw xY w|rd}n|}|rtL          d"z  }tL          d#z  }	 |'                    d$          }|(                    |           |                    |           |)                    d%&           n7# tT          $ r*}t                              d'|           d(| cY d}~S d}~ww xY w|+                    |d           tY          |          d)k    r|n|dd)         d*z   }d+| d,S |rtL          d"z  }tL          d#z  }	 |'                    d$          }|(                    d           |                    |           |)                    d%&           t                              d-||           n2# tT          $ r%}t                              d.|           Y d}~nd}~ww xY w|+                    |d           	 dd/l-m.} |/                    |          }n# t          $ r d}Y nw xY w|rj         pd!                                }|rU|0                    d0          s@|1                    |j2        |          }|r#t                              d1||j2                   dS dd2l-m3} |4                    |          }d} 	 dd3l5m6}!  |!|          } n# t          $ r d} Y nw xY w|r| s̉j         pd!                                }"|"7                    d4          8                                }#"                                }$d}%|$d5v rd6}%n"|$d7v rd8}%n|$d9v rd:}%n|#d;v rd6}%n|#d<v rd8}%n|#d=v rd:}%|%4|9                    ||                    d>          |%           d{V }|pdS |:                    |           tw          d?d@          }& j<                            |d          }'| j=        v rz|'rwt}          j>                    |'z
  }( j=                            |          })t          dA          }*d}+|)rt          |)dB          r	 |)A                                },|,                    dCt          dA                    }*dD|,                    dEd           dF|*dGdH|,                    dId           d0|,                    dJd           }+n# t          $ r Y nw xY w|&dk    rt          |&dKz  dL          nt          dA          }-|)t          uo|&dk    r|*|&k    p|(|-k    }.|.rKt                              dM||(|*|&|+            D                    |dNO            E                    |           | j=        v r"                                dPk    r F                               d{V S ddQl#mG}/m$}0 "                                }1|1r |0|1          nd}2|1r!|2 H                    ||2j%                  }3|3|3S |2r(|2j%        dRk    rt                              dS|           dS |2r&|2j%        dTk    r I                               d{V S |2rf|2j%        dUk    r[ J                    ||t          dVW           d{V  t                              dX|           t          t          dY                    S |2rJ|2j%        dZk    r? J                    ||t          d[W           d{V   O                               d{V S "                                d\v rщP                                !                                }4|4sd]S  j                            |j                  }|rDt          |4t          jS        j         jT        jU        ^          }5 V                    ||5|            W                    | j                            |j                  _          }6|6d`k    rdaS db|6 dcS |2r|2j%        ddk    rP                                !                                }7|7sdeS  j=                            |          }8|8t          u rZ j                            |j                  }|r7t          |7t          jS        j         jT        jU        ^          }5|5|jX        |<   dfS |8rt          |8dd          r{	 |8Y                    |7          }9n8# t          $ r+}:t                              dg||:           dh|: cY d}:~:S d}:~:ww xY w|9r(|7ddi         tY          |7          dik    rdjndz   };dk|; dlS dmS  j                            |j                  }|r7t          |7t          jS        j         jT        jU        ^          }5|5|jX        |<   dnS |2r|2j%        dok    rdpS |2r|2j%        dqk    rdrS |2rJ|2j%        dsv rA|2j%        dtk    r Z                               d{V S  [                               d{V S |2r&|2j%        duk    r \                               d{V S |2r&|2j%        dvk    r ]                               d{V S |2r&|2j%        dwk    r ^                               d{V S |2rh|2j%        dxk    r]P                                pd!                                8                                }<|<r|<dyv r _                               d{V S dzS |2r&|2j%        d{k    r `                               d{V S |2r{|2j%        d|v rr|2j%        d}k    r a                               d{V S |2j%        d~k    r b                               d{V S |2j%        dk    r c                               d{V S |2r|2j%        |/v r|2j%        dk    r d                               d{V S |2j%        dk    r e                               d{V S |2j%        dk    r f                               d{V S |2j%        dk    r g                               d{V S |2j%        dk    r h                               d{V S |2rd|2j%         dS ji        t          jj        k    rTt                              d|            j                            |j                  }|rt          |jX        |           dS t          t          jm        dd                    }= j<                            |d          }>|j        t          jo        k    rŉji        t          jS        k    r|=dk    r|>rt}          j>                    |>z
  |=k    rt                              dt}          j>                    |>z
  |            j                            |j                  }|r; jp        dk    r V                    ||           nt          |jX        |d%           dS  j=                            |          }8|8t          u r"                                dUk    r? E                    |           t                              d|           t          d          S  j                            |j                  }|rt          |jX        |d%           dS  jq        rn r                                r s                    |            r                                rd t                                 dnd t                                 dS  jp        dk    r3t                              d|            s                    |           dS  jp        ddk    rډj         pd!                                }7d}?|7rit          |8dd          rY	 t          |8Y                    |7                    }?n5# t          $ r(}:t                              d||:           d}?Y d}:~:nd}:~:ww xY w|?rt                              d|           dS t                              d|            s                    |           dS  u                    |8          r3t                              d|            s                    |           dS t                              d|           |8v                    j                    dS "                                }@ddl#mw}Amx}Bm$}C |@r |C|@          nd}|r|j%        n|@}D|@rd|at           jy        t                    r jy                            di           pi }Ent           jy        di           pi }Et          |Et                    r|@|Ev r|E|@         }F|F                    d          dk    r|F                    dd          !                                }G|Gr|G0                    d0          r|Gnd0|G }G|G7                    d0          }HP                                !                                }I|G d|I !                                _         |Hr|Hz                                d         n|H}@|@r |C|@          nd}|r|j%        n|@}D|@r'|Dr% |B|D          r H                    ||D          }3|3|3S |@rg |B|D          r[P                                !                                }J|j        r|j        j        nd|j        |D|@|J|Jd}K	  j{        |                    d|D |K           d{V }Ln5# t          $ r(}Mt                              d|D|M           g }LY d}M~Mnd}M~Mww xY w|LD ]}Nt          |Nt                    st!          |N                    dd                    !                                8                                }O|Or|Odk    ri|Odk    r8|N                    d          }Pt          |Pt                     r|Pr|Pc S d|@ dc S |Odk    r2|N                    d          }Pt          |Pt                     r|Pr|Pndc S |Odk    rt!          |N                    dd                    !                                7                    d0          }Q|Qs1t!          |N                    dd                    !                                }Rd0|Q d|R !                                _         "                                }@|@r |C|@          nd}|r|j%        n|@}D n|DdZk    rO }                    |          r ~                                S  fd}S                     dZdd|S           d{V S |Ddk    r                                d{V S |Ddk    r d                               d{V S |DdRk    rt                              d|           dS |Ddk    r e                               d{V S |Ddk    r f                               d{V S |Ddk    r                                d{V S |DdPk    r F                               d{V S |Dduk    r \                               d{V S |Ddk    r                                d{V S |DdTk    r I                               d{V S |DdUk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Dd~k    r b                               d{V S |Ddk    r c                               d{V S |Dd}k    r a                               d{V S |Ddok    r                                d{V S |Ddqk    r                                d{V S |Ddk    r                                d{V S |Ddwk    r ^                               d{V S |Ddk    r                                d{V S |Ddk    r                                d{V }Tt          |Tdd          }U|Urt          |Tdd          pd}V|Vr	  j                            |j                  }|rE                     |          }W|                    t!          |j                  |V|W           d{V  n,# t          $ r t                              dd%           Y nw xY w	 |U_         n6# t          $ r t          |Tdd          pdcY S w xY wt          |Tdd          pdS |Ddk    r                                d{V S |Ddk    r fd}Xd`}YP                                !                                }Z|ZrS	 t          d`t          |Zz                                d                             }Yn# t           t"          f$ r d`}YY nw xY w|Yd`k    rdnd|Y d}[                     dd|[|X           d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddtk    r Z                               d{V S |Ddk    r [                               d{V S |Ddk    r g                               d{V S |Ddk    r h                               d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddk    r                                d{V S |Ddvk    r ]                               d{V S |Dddk    rCP                                !                                }\|\sdS 	 |\_         n# t          $ r Y nw xY w|Ddxk    r _                               d{V S |Dd{k    r `                               d{V S |Ddk    r                                d{V S  jq        rd t                                 dҝS |@rt           jy        t                    r jy                            di           pi }Ent           jy        di           pi }Et          |Et                    si }E|@|Ev r2|E|@         }F|F                    d          dk    r-|F                    dd          }]|]r	 ddlm}^  |^t          j                                                  }_tI          j        |]tH          j        j        tH          j        j        |_֦           d{V }`tI          j        |`                                d׬ئ           d{V \  }a}b|ap|b                                !                                }c|crddlm}d  |d|c          }c|cr|cndS # tH          j        $ r Y dS t          $ r}d| cY d}~S d}~ww xY wd|@ dޝS |F                    d          dk    r|F                    dd          !                                }G|Gr|G0                    d0          r|Gnd0|G }G|G7                    d0          }HP                                !                                }I|G d|I !                                _         |Hr|Hz                                d         n|H}@nd|@ dߝS d|@ dS |@r	 ddlm}e  |e|@                    dd                    }f|fraP                                !                                }I |f|I          }gtI          j        |g          r|g d{V }g|grt!          |g          ndS n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|@rd}h	 ddlm}im}j  |j|@          }k|kvP                                !                                }l |i|k|l|          }m|mr@|m\  }n}o}p|n_         d%}h|pr/t                              d|kd蠳                    |p                     n2# t          $ r%}:t                              d|:           Y d}:~:nd}:~:ww xY w|@rjti                                          dd          sF	 ddlm}qm}rm}s  |q            }t |s|@          }u|u|t|u                             dd          }v|j        r|j        j        nd}w|wr|vrddlm}x |v |x|w          v r	d|v d|w dS P                                !                                }l |r|u|l|          }n|nr|n_         natw          |@          }y|yr|yS |@                    dd          |Avr5t                              d|@|j        r|j        j        nd           d|@ dS n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w }                    |          r+                     |          r                                 S dS                      ||          \  }z}{|{t                              d|           |{S |z!t           d          si  _        |z j        |<   t           j=        |<   t}          j>                     j<        |<                        |          }|	                      ||||           d{V }}	 d}~t          |}t                    r%t!          |}                    d          pd          }~nt          |}t                     r|}}~|~!                                rN	  j                            |          }n# t          $ r d}Y nw xY w|                     |||~           d{V  n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|} E                    |           S #  E                    |           w xY w)a  
        Handle an incoming message from any platform.
        
        This is the core message processing pipeline:
        1. Check user authorization
        2. Check for commands (/new, /reset, etc.)
        3. Check for running agent and interrupt if needed
        4. Get or create session
        5. Build context for agent
        6. Run agent conversation
        7. Return response
        r  Fr   r^  pre_gateway_dispatch)rK  r  rg  z*pre_gateway_dispatch invocation failed: %sNrQ  skipz8pre_gateway_dispatch skip: reason=%s platform=%s chat=%sr-  r  rewriter{   r   allowz(Ignoring message with no user_id from %sz Unauthorized user: %s (%s) on %sr  pairrG   z;Hi~ I don't recognize you yet!

Here's your pairing code: `z5`

Ask the bot owner to run:
`hermes pairing approve r  r  z<Too many pairing requests right now~ Please try again later!r  >   r  approvey>   nodenynresolve_command.update_response.update_prompt.jsonz.tmpTr`  z#Failed to write update response: %su/   ✗ Failed to send response to update process:       …u
   ✓ Sent `z` to the update process.ziRecognized /%s during pending update prompt for %s; cancelled prompt with default and dispatching commandz=Failed to write cancel response for pending update prompt: %sclarify_gatewayr   z=Gateway intercepted clarify text response (session=%s, id=%s)slash_confirm)has_blocking_approvalz!/>   okr  r?  confirmonce>   alwaysrememberrR  >   rA  rB  r  	nevermindr  >   approve oncerQ  r?  >   always approverR  >   rA  r  rT  
confirm_idr    r  r  seconds_since_activityz | last_activity=last_activity_descr  .0fzs ago) | iteration=r  r	  r  i   zWEvicting stale _running_agents entry for %s (age: %.0fs, idle: %.0fs, timeout: %.0fs)%sstale_running_agent_evictionr  status)ACTIVE_SESSION_BYPASS_COMMANDSrE  r_  z3Ignoring /start platform ping for active session %srr  rh  stop_command)interrupt_reasoninvalidation_reasonu@   STOP for session %s — agent interrupted, session lock releasedzgateway.stop.stoppednewnew_command>   qr{  zUsage: /queue <prompt>r{   rE  r  r  r   r  r   zQueued for the next turn.zQueued for the next turn. (z queued)r|  zUsage: /steer <prompt>u9   Agent still starting — /steer queued for the next turn.zSteer failed for session %s: %su   ⚠️ Steer failed: rY  ...u8   ⏩ Steer queued — arrives after the next tool call: 'r  zSteer rejected (empty payload).u4   No active agent — /steer queued for the next turn.r  u=   Agent is running — wait or /stop first, then switch models.zcodex-runtimeu>   Agent is running — wait or /stop first, then change runtime.>   rB  r?  r?  agents
backgroundkanbangoal>   donerh  r  pauseresumer]  ub   Agent is running — use /goal status / pause / clear mid-run, or /stop before setting a new goal.subgoal>   yoloverbosero  rp  footerhelpcommandsprofiler  versionu   ⏳ Agent is running — `/zE` can't run mid-turn. Wait for the current response or `/stop` first.uF   PRIORITY photo follow-up for session %s — queueing without interrupt&HERMES_TELEGRAM_FOLLOWUP_GRACE_SECONDSz3.0uV   Telegram follow-up arrived %.2fs after run start for %s — queueing without interruptr{  r  u7   HARD STOP (pending) for session %s — sentinel cleareduE   ⚡ Force-stopped. The agent was still starting — session unlocked.r  r  r   r  z'PRIORITY queue follow-up for session %sz(PRIORITY steer failed for session %s: %szPRIORITY steer for session %sz/PRIORITY steer-fallback-to-queue for session %szjPRIORITY interrupt demoted to queue for session %s because the running agent has active subagents (#30170)z!PRIORITY interrupt for session %s)GATEWAY_KNOWN_COMMANDSis_gateway_known_commandrE  quick_commandsr^   aliasrY  )r!   r  r#  raw_commandr$  r  zcommand:z/command:%s hook dispatch failed (non-fatal): %sdecisionrB  rl   z
Command `/z` was blocked by a hook.handledr  r  c                  >   K                                    d {V S r  )_handle_reset_commandrK  r  s   rC   	_do_resetz0GatewayRunner._handle_message.<locals>._do_reset  s-      !77>>>>>>>>>rL   z/newzJThis starts a fresh session and discards the current conversation history.)rK  r#  rn  detailexecutetopicz,Ignoring /start platform ping for session %swhoamir!   r   memoryskillsr  personalitysuggestions	blueprint
agent_seedr   zblueprint ack send failedrp   retryundoc                  >   K                                    d {V S r  )_handle_undo_commandr  s   rC   _do_undoz/GatewayRunner._handle_message.<locals>._do_undoR  s-      !66u=========rL   z;This removes the last user/assistant exchange from history.zThis removes the last z user turns from history.z/undosethomecompressusagecreditsinsightsz
reload-mcpzreload-skillsbundlesr;  rn  rm  branchrollbackzJUsage: /steer <prompt>  (no agent is running; sending as a normal message)r  z) and is not accepting new work right now.execr#  )_sanitize_subprocess_envr  r_  rx  )redact_sensitive_textzCommand returned no output.zQuick command timed out (30s).zQuick command error: zQuick command '/z' has no command defined.z' has no target defined.z4' has unsupported type (supported: 'exec', 'alias').)get_plugin_command_handlerr  r  z"Plugin command dispatch failed: %s)build_bundle_invocation_messageresolve_bundle_command_keytask_idz$Bundle %s skipped missing skills: %sr  zBundle dispatch failed: %s_bundle_handled)get_skill_commandsbuild_skill_invocation_messageresolve_skill_command_keyrf   )get_disabled_skill_namesrJ   r  z** skill is disabled for z(.
Enable it with: `hermes skills config`uO   Unrecognized slash command /%s from %s — replying with unknown-command noticer  zUnknown command `/zl`. Type /commands to see what's available, or resend without the leading slash to send as a regular message.z*Skill command check failed (non-fatal): %sz@Rejecting new active session %s: max_concurrent_sessions reachedrr  final_response)r+  r  r  z!goal continuation hook failed: %s)r  r   rH   rc  r_  rg  rt   ru   rv   r   r6   r'   r:  r!   rF   r   r+   r;  r   r  r  r;  r  r  _get_unauthorized_dm_behaviorr  _is_rate_limitedgenerate_coderQ  r   _record_rate_limitr  r{   rI   get_commandr   rE  rf   rY  with_suffixr  rb  r*   r@  r   toolsrK  get_pending_for_sessionrI  resolve_gateway_clarify
clarify_idrM  get_pendingtools.approvalrN  lstripr,   r#   clear_if_staler   r!  rq  r   r   rr   r  r  r  "_invalidate_session_run_generationr|  _handle_status_commandr^  _check_slash_access_handle_restart_command_interrupt_and_clear_session_INTERRUPT_REASON_STOPr  r   _INTERRUPT_REASON_RESETr  get_command_argsr  r  r  r  r   r  r  rs  r|  _handle_approve_command_handle_deny_command_handle_agents_command_handle_background_command_handle_kanban_command_handle_goal_command_handle_subgoal_command_handle_yolo_command_handle_verbose_command_handle_footer_command_handle_help_command_handle_commands_command_handle_profile_command_handle_update_command_handle_version_commandrE  rJ  r  r%   r9  r  r  r#  r'  r}  r  ry  r  r"  rw  rx  r  r  r  emit_collectr  r&   _maybe_confirm_destructive_slash_handle_topic_command_handle_whoami_command_handle_platform_command_handle_stop_command_handle_reasoning_command_handle_memory_command_handle_skills_command_handle_fast_command_handle_model_command_handle_codex_runtime_command_handle_personality_command_handle_suggestions_command_handle_blueprint_commandr  _handle_retry_commandr   r   
IndexError_handle_set_home_command_handle_compress_command_handle_usage_command_handle_credits_command_handle_insights_command_handle_reload_mcp_command_handle_reload_skills_command_handle_bundles_command_handle_debug_command_handle_title_command_handle_resume_command_handle_branch_command_handle_rollback_command_handle_voice_commandtools.environments.localr  r&   r  rz  create_subprocess_shellr}  r~  r  r  r  agent.redactr  r  r  iscoroutineagent.skill_bundlesr  r  r5   localsagent.skill_commandsr  r  r  r  r  r  r"  r$  r  rr  _begin_session_run_generation_handle_message_with_agentry  _post_turn_goal_continuation)r  rK  r  is_internalre  _hook_results	_hook_exc_result_action	_new_textrL  coder   
_quick_key_update_promptsr   r  r  _recognized_cmd_resolve_update_cmd_cmd_defresponse_pathprompt_pathtmpr  label_clarify_mod_pending_clarify_raw_clarify_reply	_resolved_slash_confirm_mod_pending_confirm_tool_approval_liverN  
_raw_reply_norm_reply
_cmd_reply_confirm_choice_raw_stale_timeout	_stale_ts
_stale_age_stale_agent_stale_idle_stale_detail_sa	_wall_ttl_should_evict_DEDICATED_HANDLERS_resolve_cmd_inner_evt_cmd_cmd_def_inner_deniedqueued_textr~  rc   r   r  acceptedrM   preview	_goal_arg_telegram_followup_grace_started_atr  r#  rw  rx  _resolve_cmd	canonicalry  qcmdrY  target_command	user_argsr  hook_ctxhook_results	_hook_errhook_resultr|  rl   rc  new_argsr  _blueprint_result_blueprint_seed_ack	_ack_metar  _undo_n	_undo_raw_undo_detailsteer_payloadexec_cmdr  sanitized_envr  rv  rw  r  r  r  plugin_handlerr  r  r  r  
bundle_keyuser_instructionbundle_resultr   _loadedmissingr  r  r  
skill_cmdscmd_key_skill_namer  _get_plat_disabled_unavail_msg_active_session_lease_limit_message_run_generation_agent_result_final_textr+  	_goal_excs   ``                                                                                                                               rC   r.  zGatewayRunner._handle_message  s6       75*e<<==   	
#JJJJJJ ,* "&"4	! ! !  # # #KYWWW "# )  !'400 !++h//f$$KKRH--17O--i3)	    44i'' 'F 3 3I!)S11 . + 3E	 J J J!&Eg%%E &  -	^# ++F33 GI^___t ))&11 !	NN=v~vO_agapavwww4''D,N,Nv,_,_ci,i,i9? W 5 5i %66}fnUU  4)77!6>63C3Ir   Y"m//@@G %ll"NO:>O O 8EO O HLO O O         #m//@@G %ll"N6         &99-XXX4 11&99
!$(@"EEz** @	6:#**,,C##%%C((( #&& #"& 
33^^^^^^^$ 3 3 3.2+++3*63':':3'?'?H?G.QhmmTOO( 3 3 3.2OOO3" ($&MM$'M D ,/A A*-BBQ'33F;;CNN=111KK...&&$&7777 Q Q QNN#H!LLLPQPPPPPPPPQ  ##J555),]););r)A)A}UXVXUXGY\aGaCECCCC  6 ,/A A*-BB'33F;;CNN2&&&KK...&&$&777KKP'"	       NNW       
  ##J555	$======+CCJOO 	$ 	$ 	$#	$'"'*"2!9!9!;!;
 " *<*G*G*L*L (@@$/1C 	  KKW"$4$?   2 	>=====-99*EE#	(<<<<<<"7"7
"C"C 	( 	( 	("'	( 	:$7 	:**1133J %++D117799K**,,J"O@@@"(555"*DDD"* CCC"( <<<"* ==="**"4"<"< 0 4 4\ B BO# #      	 !B& --j999 ((>EE+//
A>>	---)-y0J/33J??L  ,,KM 
6L M M 
	&;;==C"%''*BE%LL"Q"QKeCGG4H),T,T e e'4e e'*ww/?'C'Ce eFIggN^`aFbFbe e "M
 !   D
 ?QST>T>T.3T:::Z_`eZfZfI$;; '!+Q?Q0Q .!I-	   >B
K&	   779 8    11*===---  ""h..!88?????????        ((**H=EO//9994N  #N6226>;NOO&"N
  ."5"@"@QS]^^^r A."5"B"B!99%@@@@@@@@@  A."5"?"?77%;(6	 8          ^`jkkk%a(>&?&?@@@  
?."5">">77%<(5	 8          "77>>>>>>>>>   ""n44#4466<<>>" 433-++FO<< J#/(%0%5$|#(#3',';$ $ $L &&z<III))*dm>O>OPVP_>`>`)aaA::66DUDDDD  'N."5"@"@"3355;;==
! 433 $ 4 8 8 D D $;;;"m//@@G M'3!+)4)9#(<','7+0+?( ( ( AM1*=VV  	=W]G%D%D 	==#0#6#6z#B#B$ = = ='H*VYZZZ<s<<<<<<<<=   e",SbS/c*ooPR>R>RUUXZ"[dZadddd<<-++FO<< I#/'%0%5$|#(#3',';$ $ $L =IG-j9MM  W."5"@"@VV  +."5"H"H* *  >."59L"L"L!&)33!%!=!=e!D!DDDDDDDD!66u=========  @."5"A"A!88?????????  D."5"E"E!<<UCCCCCCCCC  @."5"A"A!88?????????  |."5"?"?"3355;BBDDJJLL	  BI1g$g$g!%!:!:5!A!AAAAAAAA{{
  A."5"B"B!99%@@@@@@@@@  D."59L"L"L!&&00!%!:!:5!A!AAAAAAAA!&)33!%!=!=e!D!DDDDDDDD!&(22!%!<!<U!C!CCCCCCCC  
E."59L"L"L!&&00!%!:!:5!A!AAAAAAAA!&*44!%!>!>u!E!EEEEEEEE!&)33!%!=!=e!D!DDDDDDDD!&(22!%!<!<U!C!CCCCCCCC!&)33!%!=!=e!D!DDDDDDDD  Q.2E Q Q Q
 ![%666egqrrr-++FO<< ^/0I:W\]]]t',	BEJJ( ($ 155j!DDK8#444&+*:::,q00 1Y[[;.3KKKlIKK+-  
 -++FO<< 	,77**:ugFFFF3#5&!'+	    t 044Z@@M 777$$&&&0055jAAAKK Y[efff)*qrrr -++FO<< /1"#'	    t~ 3355 L88UKKK 7799ws4#=#=#?#?ssssv4+E+E+G+Gvvv
 $//F
SSS44ZGGGt$// $j.B5577
 ('-"A"A (("&}':'::'F'F"G"G$ ( ( ('QS]_bccc"'(   LL!@*MMM4NPZ[[[44ZGGGt //>> N  
 44ZGGGtLL<jIII##EJ/// 4 ##%%	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 -4=<<(((%-:HMM7	  	Kx'$+t,, R!%1A2!F!F!L"!(6F!K!K!Qr.$// KG~4M4M%g.88F##w..!XXh3399;;F K+1+<+<S+A+A!S|6||)/s););$)$:$:$<$<$B$B$D$D	(.%<%<%<%<%B%B%D%D
?M"a."6"6"8"8";";Sa<C#M<<#8#8#85=$JHMM7	  	y 	%=%=i%H%H 	..vyAAG"  .	//	:: .	--//5577H5;_LFO11"!>$& $ H	"%)Z%<%<*y**H& &              " " "Ey    ""  ,  !+t44 {z2>>??EEGGMMOO 8w#6#6v%%)ooi88G!'3// 'G '&IIIIIIIy(()ooi88G&0#&>&>T7T77PTTTTy(("%#;;# #eggffSkk   ' ! ";??:r#B#BCCIIKKH!=[!=!=8!=!=!C!C!E!EEJ#//11G8?I||G444TH19 FwIE ) 11&99 ?<<>>>? ? ? ? ? ?>>, " ? 	 	 	 	 	 	 	 	 	 33E:::::::::225999999999KKF
SSS2
""66u=========	!!55e<<<<<<<<<  44U;;;;;;;;;  44U;;;;;;;;;  44U;;;;;;;;;
""66u=========	!!55e<<<<<<<<<225999999999##77>>>>>>>>>  44U;;;;;;;;;  44U;;;;;;;;;225999999999	!!55e<<<<<<<<<  44U;;;;;;;;;22599999999933E:::::::::'';;EBBBBBBBBB%%99%@@@@@@@@@  44U;;;;;;;;;%%99%@@@@@@@@@##&*&D&DU&K&K K K K K K K%&7tLLO F 0&"==C QQ"&-"3"3FO"D"D" ^(,(H(H(P(PI"),,s6>/B/BDS\,"]"]]]]]]]]$ Q Q Q%@4PPPPPQJ!0EJJ  J J J"#4fbAAITIIIJ 0&"==EE33E:::::::::> > > > > >G..006688I   !!S):):1)=%>%>??GG"J/      GGG  a<< NMPgPPP 
 >>#  ?          	!!66u=========
""66u=========33E:::::::::	!!55e<<<<<<<<<
""66u=========$$88?????????'';;EBBBBBBBBB	!!55e<<<<<<<<<	!!55e<<<<<<<<<225999999999  44U;;;;;;;;;	!!55e<<<<<<<<<33E:::::::::33E:::::::::  44U;;;;;;;;;  44U;;;;;;;;;
""66u=========$$88????????? "2244::<<M  dcc*

    225999999999	!!55e<<<<<<<<<33E:::::::::> 	mlT%?%?%A%Allll  1	l$+t,, R!%1A2!F!F!L"!(6F!K!K!Qrnd33 $!#.((%g.88F##v--#xx	266H U? ZYYYYY,D,DRZ__EVEV,W,WM)0)H ('.'9'>'.'9'>$1	* * * $ $ $ $ $ $D 4;3CDDTDTDVDV`b3c3c3c-c-c-c-c-c-cNFF&,&6%>%>%@%@%F%F%H%HF% G N N N N N N)>)>v)F)F-3#V669VV&3 D D D#C#C#C( ? ? ?#>1#>#>>>>>>>?  U'TTTTXXf%%00!XXh3399;;F T+1+<+<S+A+A!S|6||)/s););$)$:$:$<$<$B$B$D$D	(.%<%<%<%<%B%B%D%D
?M"a."6"6"8"8";";Sa  T'SSSSkgkkkk  	HHIIIIII "<!;GOOCQT<U<U!V!V! ; % 6 6 8 8 > > @ @I+^I66F*622 .'-*0:3v;;;d:;  H H HCQGGGGGGGGH  	B $OB        87@@
)','='='?'?'E'E'G'G$$C$C"$4j% % %M % 0=-Wg%(
*." "KK F *DIIg,>,>  
  B B B;SAAAAAAAAB  :	N688<<(95AA :	N9N         
 0/11
33G<<&
 #-W"5"9"9&""E"EK5;_NFO11$E  dddddd&*<*<e*L*L*LLL!J !J !Ju !J !J !J (-'='='?'?'E'E'G'G$88!1:  C  )%(

 $<G#D#DL# ,++ sC008NNNC#5;_MFO11#	  5 5 5 5  N N NI1MMMMMMMMN --f55 	 88@@ A>>@@@4 150O0O1
 1
-~ %KKR   "! ,4!9:: 1.0+6KD'
3+BZ(.2ikk
+<<ZHH'	:"&"A"A%Q[]l"m"mmmmmmmMM mT22 0"%m&7&78H&I&I&OR"P"PKKs33 0"/K $$&& 
-(,(:(P(PQW(X(X$ - - -(,-$0"??*7#)+6 @         
  M M M@)LLLLLLLLM  --j9999D--j9999s  A	 	
A:A55A:"N) )N87N8>O O$#O$AQ 
R%R
R
R'A1U 
V#VV"V> >WW"Y4 4ZZ"Ba% %
a21a2q 
r& rrrL"AL' L'
AML1AMMAMY$AY1 Y1
AZ#Y;AZZAZ#o8A&Aq q&ArrArrAr rAr4r3Ar4t"6Au uAu1u0Au1@%B@- @-
B@:@9B@:E*CBI	 I	BI4I	BI4I%BI/I)BI4I/BI4M#BBO, O,
BPO6BPPBPP#BBR/ R/
BSR9BSSBST	A2BX U<ABX WABX X
BYX&BYYBY\)Ba ]A'B_> ^0B_ _
B_> _B__B_> _B__#B_> _=Ba _>
B`-`B`(`#Ba `(B`-`-Ba aBar   c                  8K   |pg }|j         pd8t          | j        dd          }t          | j        dd          }|                     |          }|                     |           t          |||          }|r|j        rd|j         d8 8t          |d	d
          r|j         d8 8g }|j        rIg }	g }
t          |j                  D ]\  }}|t          |j                  k     r|j        |         nd}|                    d          s|j        t          j        k    r|	                    |           |j        t          j        k    r|                    |           |j        t          j        k    s4|                    d          r4|j        t          j        t          j        hvr|
                    |           |	r|                                 }|dk    rWt          | dd
          }|	i }|| _        t-          |	          ||<   t.                              dt          |	                     nEt.                              d|t          |	                     |                     8|	           d
{V 8|
r|                     8|
           d
{V \  8}|r| j                            |j                  }|                     ||                     |                    }|r_|D ]\}	 |                     |j!        d| d|           d
{V  +# tD          $ r%}t.          #                    d|           Y d
}~Ud
}~ww xY wd}tI          8fd|D                       r| j                            |j                  }|                     ||                     |                    }|rP	 d}| %                                r|dz  }|                     |j!        ||           d
{V  n# tD          $ r Y nw xY w|rddl&m'} |D ]}tP          j)        *                    |          }|+                    dd          }t          |          dk    r|d         n|}tY          j-        dd|          } ||          }d | d!| d"}| d#8 8|j        rf|j        t          j        k    rPdd
l.} ddl&m'}! h d$}"t          |j                  D ],\  }}|t          |j                  k     r|j        |         nd}|d%v rZtP          j)        /                    |          d&         0                                }#|#|"v rd'}n| 1                    |          \  }$}%|$r|$}|                    d(          stP          j)        *                    |          }&|&+                    dd          }'t          |'          dk    r|'d         n|&}(tY          j-        dd|(          }( |!|          })te          |(|)|          }*|* d#8 8.t          |d)d
          r|j3        r|j4        d
d*         }+d+|+ d,8 8d-8v r	 dd.l5m6}, dd/l7m8}- tP          j9                            d0tP          j)        :                    d1                    }.tw                      }/d
}0	 ty                      }1|1                    d2i           }2t{          |2t|                    r&|2                    d3          }3|3t          |3          }0n# tD          $ r Y nw xY w |-| j@        | jA        p|/                    d4          pd|/                    d5          pd|06          }4 |,8|.|4|.7           d
{V }5|5jB        r^| j                            |j                  }6|6r;|6                     |j!        d8C                    |5jD                  pd9           d
{V  d
S |5jE        r|5jF        8n2# tD          $ r%}7t.          #                    d:|7           Y d
}7~7nd
}7~7ww xY w8S );a  Prepare inbound event text for the agent.

        Keep the normal inbound path and the queued follow-up path on the same
        preprocessing pipeline so sender attribution, image enrichment, STT,
        document notes, reply context, and @ references all behave the same.

        Side effect: buffers per-session native image paths when the active
        model supports native vision AND the user has images attached. The
        caller consumes and clears that session-scoped buffer at the
        ``run_conversation`` site to build a multimodal user turn. When the
        list is empty, the ``_enrich_message_with_vision`` text path has
        already run and images are represented in-text.
        rG   r  Tr  Fr	  [r   channel_contextNz

[New message]
rD  rG  nativerv  zSImage routing: native (model supports vision). %d image(s) will be attached inline.zLImage routing: text (mode=%s). Pre-analyzing %d image(s) via vision_analyze.	   🎙️ "r  r   &Transcript echo failed (non-fatal): %s)No STT providerzSTT is disabledzcan't listenVOICE_TOOLS_OPENAI_KEYc              3       K   | ]}|v V  	d S r  r   )r   r  message_texts     rC   r  z>GatewayRunner._prepare_inbound_message_text.<locals>.<genexpr>   s(      NN&v-NNNNNNrL   uH  🎤 I received your voice message but can't transcribe it — no speech-to-text provider is configured.

To enable voice: install faster-whisper (`uv pip install faster-whisper` in the Hermes venv; `pip install faster-whisper` also works if pip is on PATH) and set `stt.enabled: true` in config.yaml, then /restart the gateway.z@

For full setup instructions, type: `/skill hermes-agent-setup`r   )to_agent_visible_cache_pathr  r  r  z	[^\w.\- ]z*[The user sent an audio file attachment: 'rU  u3  . Its content is not inlined here. If the user's request involves what the audio contains, transcribe or process it yourself — for example by passing the path to a transcription or media tool — instead of asking the user to describe it. Only ask what to do with it if their intent is genuinely unclear.]r
  >   .md.cfg.csv.ini.log.txt.xml.yml.json.toml.yaml>   application/octet-streamrG   r   z
text/plain)zapplication/rT  reply_to_textrN  z[Replying to: "z"]

@)#preprocess_context_references_async)get_model_context_lengthr  ~r  context_lengthr  r  )r  r  config_context_length)r  re  allowed_rootr   zContext injection refused.z(@ context reference expansion failed: %s)Gr{   rH   r  r  #_consume_pending_native_image_pathsr  r  rK  rB  rH  r   rC  rI  rE  r  rJ  r(   AUDIOVOICEDOCUMENT_decide_image_input_moderv  r  ru   r:  _enrich_message_with_vision"_enrich_message_with_transcriptionrQ  r'   r!   r  r  r   r   rt   r;  r  r  tools.credential_filesrS  r%   r/   basenamer  r   r   	mimetypesry  r,   
guess_typerV  rG  r`  agent.context_referencesrb  agent.model_metadatarc  r&   r  r2  r  r   r6   r   _model	_base_urlblockedr5   warningsr  rl   )9r  rK  r  r   _group_sessions_per_user_thread_sessions_per_userr  _is_shared_multi_useraudio_file_pathsimage_pathsaudio_pathsrM  r/   rO  	_img_modepending_native_successful_transcripts_echo_adapter
_echo_meta_tx	_echo_exc_stt_fail_markers_stt_adapter	_stt_meta_stt_msg_to_agent_path_apath	_basenamer  _display_agent_path_note
_mimetypesrS  _TEXT_EXTENSIONS_extguessedr  rp  rL  rQ  rR  context_notereply_snippetrb  rc  _msg_cwd_msg_runtime_msg_config_ctx_msg_cfg_msg_model_cfg_msg_raw_ctx_msg_ctx_len_ctx_result_adapterrM   rR  s9                                                           @rC   _prepare_inbound_message_textz+GatewayRunner._prepare_inbound_message_text  s
     ( -Rz'R#*4;8QSW#X#X $+DK9SUZ$[$[! 226:: 	00=== <$<%>!
 !
 !

 ! 	BV%5 	BAv/AA<AAL
 5+T22 	W#3VVVVL ') ]	!KK$U%566 - -401C8I4J4J0J0J)!,,PR##H-- -1C{GX1X1X&&t,,, %):::$++D1111';+<<<$$X.. =*;3DkFZ2[[[&&t,,,  !99;;	((%,T3[]a%b%bN%-)+FTC26{2C2CN;/KKmK((   
 KKf!3{#3#3   *.)I)I$#* * $ $ $ $ $ $L
  3!>B>e>e ? ? 9 9 9 9 9 955 + "$(M$5$5fo$F$FM!%!A!A&$JfJfglJmJm!n!nJ$ "#: 
" 
"C	"&3&8&8$*N$6$6$6$6-7 '9 '" '" !" !" !" !" !" !" !" !"
 $- " " " &$Li!" !" !" !" !" !" !" !""%! NNNN<MNNNNN !#'=#4#4V_#E#EL $ @ @IeIefkIlIl m mI# !!!= %  $4466 q (,p p"."3"3 & ()2 #4 # #        
  ) ! ! ! D!  	<\\\\\\* < <G,,V44	"a00(+Fq(8(86!99i6,X>>,nV44F F F'2F F F  #(;;\;; 	C 2k6J J J****JJJJJJyyy$U%566 C C401C8I4J4J0J0J)!,,PR<<<7++D11!4::<<D/// ,%/%:%:4%@%@
" ,$+E''(ABB 7++D11 sA..+.u::??uQxx!vlCFF
 98>>
;L*V[\\".BBLBB5/400 	QU5N 	Q "/5MP]PP,PPL,'NXXXXXXIIIIII:>>."':L:LS:Q:QRR<>>"&355H%-\\'2%>%>N!.$77 @'5'9'9:J'K'K'3.1,.?.?O    D77K!^Q|/?/?
/K/KQr(,,Y77=2*9	      %H$G  #/!)	% % %       &  #}00AAH &mm"N IIk&:;;[?[          4' 7#.#6L N N NGMMMMMMMMN so   .'L
M M  M0>O/ /
O<;O<2A_
 A[, +_
 ,
[96_
 8[99C _
 ;_
 

_9_44_9c                 v    t          | dd           }|sg S t          |                    |g           pg           S )Nrv  )rH   r  r@  )r  r  r  s      rC   rh  z1GatewayRunner._consume_pending_native_image_paths  sE     'OQUVV 	IN&&{B77=2>>>rL   c                    |r|d S t          | dd           }|t                      }|| _        	 t          j        |          ||<   n.# t
          $ r! t                              d|d           Y d S w xY w	 |                    |           t          | dd          }t          |          |k    r+|
                    d           t          |          |k    )d S d S # t
          $ r Y d S w xY w)	Nry  z*Failed to cache live session source for %sTrp   rz  r6  F)r!  )rH   r   ry  r;  r   rt   ru   r;  move_to_endr   popitem)r  r  r  cached_sourcesmax_sizes        rC   _cache_session_sourcez#GatewayRunner._cache_session_source  s-    	fnF '94@@!(]]N$2D!	*5*=f*E*EN;'' 	 	 	LLE{]aLbbbFF		&&{333t%;SAAHn%%00&&E&222 n%%000000 	 	 	DD	s$   A 'A32A37A"C 
C+*C+c                     |sd S t          | dd           }|sd S |                    |          }|'	 |                    |           n# t          $ r Y nw xY w|S )Nry  )rH   r'   r  rt   )r  r  r  r  s       rC   rK  z(GatewayRunner._get_cached_session_source  s     	4 '94@@ 	4##K00**;7777   s   A 
AAr  run_generationc                  F  K   t          j                     }t          |j        d          r|j        j        nt	          |j                  }|j        pddd                             dd          }t                              d||j	        p|j
        pd|j        pd|           |                     |          }|\t                              d	|j        |j
        |j        |           t          j        ||
          }	 ||_        n# t           $ r Y nw xY w| j                            |          }	|	j        }
|                     |
|           |                     |          r	 | j        r@| j                            t	          |j                  t	          |j                            nd}n.# t           $ r! t                              dd           d}Y nw xY w|rt	          |                    d          pd          }|r\| j        U	 | j                            |          }n/# t           $ r" t                              d|d           |}Y nw xY w|r||k    r|}|r*||	j        k    r| j                            |
|          }||}	|r@|t	          |                    d          pd          k    r|                     ||	d           nD	 |                     ||	           n,# t           $ r t                              dd           Y nw xY wt?          |	dd          r\| j         !                    |
d           | "                    |
d           t          | d          r| j#        !                    |
d           |	j$        |	j%        k    p!t?          |	dd          pt?          |	dd          }t?          |	dd          rd|	_&        |rC| j'        (                    d|j        r|j        j        nd|j
        |	j        |
d           d{V  tS          || j*        |	          }| +                    |          }d}	 tY                      }t[          |                    d          pi                     dd                    }n# t           $ r Y nw xY wt]          ||          }t?          |	dd          rt?          |	dd          pd}|dk    rd }n|d!k    rd"}nd#}|d$z   |z   }	 | j        j*        /                    |j        t?          |d%d&          '          }|j        r|j        j        nd}t?          |	d(d          }|dk    p|j0        o
|o||j1        v}|r| j2                            |j                  }|r|dk    rd)}nA|d!k    rd*|j3         d+}n/|j4        d,z  }|j4        d,z  }|s| d-n|r| d.| d/n| d/}d0| }d1| d2} 	 | 5                                }!|!r|  d$|! } n# t           $ r Y nw xY w|6                    |j        | | 7                    |          3           d{V  n2# t           $ r%}"t                              d4|"           Y d}"~"nd}"~"ww xY wd|	_8        d|	_9        t?          |d5d          }#|r2|#r/tu          |#t                    r|#gntw          |#          }$	 d6d7l<m=}%m>}& g }'g }(|$D ]r}) |%|)|8          }*|*rF|*\  }+},}-d9|- d:}. |&|+|,|.          }/|/r*|'?                    |/           |(?                    |)           Wt          @                    d;|)           s|'rP|'?                    |j                   d$A                    |'          |_        t                              d<|(|
           n3# t           $ r&}"t          @                    d=|$|"           Y d}"~"nd}"~"ww xY w| j        B                    |	j                  }0|0r&t          |0          d>k    rd6d?lDmE}1mF}2 d@}3dA}4d}5dB}6d}7d}8d}9d}:i };	 tY                      };|;r|;                    dCi           }<tu          |<t                    r|<}3ntu          |<t                    r|<                    dD          p|<                    dC          p|3}3|<                    dE          }=|=(	 t          |=          }7n# t          t          f$ r Y nw xY w|<                    dF          pd}8|<                    dG          pd}9|;                    dHi           }>tu          |>t                    r~t	          |>                    dId                    K                                dJv }5|>                    dK          }?|?0	 t          |?          }@|@d6k    r|@}6n# t          t          f$ r Y nw xY w	 | L                    ||
tu          |;t                    r|;ndL          \  }3}A|A                    dF          p|8}8|A                    dG          p|9}9|A                    dM          p|:}:n# t           $ r Y nw xY w|7I|9rF	 	 d6dNlMmN}B  |B|;          }Cn<# t           $ r/ |;                    dO          }Ctu          |Ctv                    sg }CY nw xY w|CD ]}Dtu          |Dt                    s|D                    dG          pdO                    dP          }E|Er|E|9O                    dP          k    r~|D                    dQi           }Ftu          |Ft                    rQ|F                    |3i           }Gtu          |Gt                    r&|G                    dE          }H|Ht          |H          }7 nn# t          t          f$ r Y nw xY wn# t           $ r Y nw xY w|5rx |2|3|9pd|:pd|7|8pdR          }It          |I|4z            }Jt          |IdSz            }Kt          |0          }L|	jP        }M|Md6k    r|MdT}Nn |1|0          dU}N|6}O|Jk    p|L|Ok    }P|Prt                              dV|LdW|Nt          |4dXz            |IdW|JdW           | 7                    || Q                    |                    }Q	 d6dYlRmS}R | L                    ||
tu          |;t                    r|;ndL          \  }3}A|A                    dM          rdZ |0D             t                    d>k    r |Rdi |A|3d>ddd[g|	j        d\	 d] _T        t          jV                    }S|SW                    dfd^           d{V \  }T}Uj        }V|V|	j        k    r8|V|	_        | j        X                                 |                     ||	d_           | j        Y                    |	j        |T           d6|	_P        |T}0t          |T          }W |1|T          }Xt                              d`|L|WdW|XdW           |X|Kk    rt          @                    da|XdW           t?          dbd          }Y|Yt?          |Ydcd          rt?          |Yddd          pde}Zdf|Z dg}[	 | j2                            |j                  }\|\r*|j        r#|\6                    |j        |[|Q3           d{V  n# t           $ r%}]t          @                    dh|]           Y d}]~]nd}]~]ww xY w|Yt?          |Ydid          rt?          |Ydid          }^t?          |Ydjd          pde}_dk|^ dl|_ dm}`	 | j2                            |j                  }\|\r*|j        r#|\6                    |j        |`|Q3           d{V  n2# t           $ r%}]t          @                    dn|]           Y d}]~]nd}]~]ww xY w| Z                    |
           | [                               n/# | Z                    |
           | [                               w xY wn2# t           $ r%}"t          @                    do|"           Y d}"~"nd}"~"ww xY w|0s| j        \                                sdp}a	 d6dql]m^}bm_}cm`}dma}emb}f tY                      }g |f|g          drk    r. |c|g|b          s"| |e            z  } |dt          dsz  |b           n||az  }n7# t           $ r*}ht                              dt|h           ||az  }Y d}h~hnd}h~hww xY w|0s|j        r|j        t          je        k    r|j        t          jf        k    r|j        j        }t          |          }it          ji        |i          sP|j        t          jj        k    rdundv}jdw|k                                 dx|j dy} | l                    ||            d{V  |j        t          jm        k    rl| j2                            t          jm                  }| n                    |          }k|kr1|r/t          |dz          r|o                    |k          }l|lr|d$|l z  }| p                    |||0{           d{V }m|mdS | q                    | j2                            |j                  |
|           	 |j        r|j        j        nd|j
        |j        pdt?          |d|d          rt	          t?          |d|d                    ndt?          |d%d          pd|	j        |mdd}         d~}n| j'        (                    d|n           d{V  | r                    |m||0||	j        |
|| Q                    |          |js        	  	         d{V }o	 | j2                            |j                  }p|pr0t          |pd          r |pt                    |j                   d{V  n# t           $ r Y nw xY w| u                    ||          st                              d|pd|           | j2                            |j                  }qt?          t          |q          dd          |qw                    ||           n-|qr+t          |qd          r|qjx        !                    |d           	 | y                    |           dS |o                    d          pd}r|rdk    rd}r|o                    dg           }st          j                     |z
  }t|o                    dd6          }ut          |r          }vt                              d||j        pd|t|u|v           |
rst          |o          rd| {                    |
           	 | j        |                    |
           n3# t           $ r&}wt                              d|
|w           Y d}w~wnd}w~www xY wt          |o|rt          |0                    }rt          |j        |r          }r|o                    d          rO|od         |	j        k    r>|od         |	_        | j        X                                 |                     ||	d           	 d6dlm}x  |xtY                      t          |j                  dt?          | dd                    }yn!# t           $ r t?          | dd          }yY nw xY w|yr|rr|o                    d          }z|zr|z                                                                }{t          |{          dk    r7dA                    |{dd                   }|||dt          |{          dz
   dz  }|n|z                                }|d|| d|r }rd}}	 d6dlm}~  |~tY                      t          |j                  |o                    dC          |o                    dd6          pd6|o                    dE          pdt          j                            dd                    }}n4# t           $ r'}t                              d|           d}}Y d}~nd}~ww xY w|}r|rr|o                    d          s|r d$|} }r| j'        (                    di |nd|rpddd}         i           d{V  	 d6dlm} |j        }g |_        t          |          D ]O\  }}t          j        |                     |                     |dXz  dk    rt          j        d6           d{V  Pn2# t           $ r%}"t                              d|"           Y d}"~"nd}"~"ww xY w	 d6dlm} g }|j                                        sa|j                                        }|                    dd          }|dv r|?                    |           |j                                        a|D ]d}t%          |          }|rP	 |                     ||           d{V  2# t           $ r%}t                              d|           Y d}~\d}~ww xY wen2# t           $ r%}"t                              d|"           Y d}"~"nd}"~"ww xY wt[          |o                    d                    }t	          |o                    dd                    K                                |oTt[          |o                    d                    p2t)          fddD                       pdv ot          |0          dk    }|r!t                              d|	j                   n"|r t                              d|	j                   |o                    d          r|	r|
rt                              d|	j                   | j                            |
           | Z                    |
           | j         !                    |
d           | "                    |
d           t          | d          r| j#        !                    |
d           |rpddz   }rt-          j                                                    }|rn`|0s^|o                    dg           }| j                            |	j        d|pg t5                      |j        r|j        j        nd|d           | j        du}|rn|rHd|m|d}|j        rt	          |j                  |d<   | j                            |	j        ||           n=|o                    dt          |0                    }t          |s          |k    r
|s|d         ng }|sod|m|d}|j        rt	          |j                  |d<   | j                            |	j        ||           |rr&| j                            |	j        d|r|d|           nd}|D ]}|                    d          dk    ri |d|i}|s=|                    d          dk    r$|j        rd|vrt	          |j                  |d<   d}| j                            |	j        ||           | j                            |	j        |o                    dd6                     t[          |o                    d                    }|                     ||r|s|          r|                     ||r           d{V  |o                    d          r|o                    d          s|rr>| j2                            |j                  }|r|                     |r||           d{V  |}r	 | j2                            |j                  }|rJ|6                    |j        |}| 7                    || Q                    |                    3           d{V  n2# t           $ r%}wt                              d|w           Y d}w~wnd}w~www xY w	 | y                    |           dS |r| y                    |           S # t           $ rk}"	 | j2                            |j                  }|r0t          |d          r |t                    |j                   d{V  n# t           $ r Y nw xY wt                              d|
           	 dĐtC                      v r|m|	d}	 | j        B                    |	j                  }n# t           $ r g }Y nw xY wtE          |dd                   D ]6}|                    d          dk    r|                    dƦ          |mk    } n7|srd|mt-          j                                                    d}t?          |dd          rt	          |j                  |d<   | j                            |	j        |           n,# t           $ r t                              dd           Y nw xY wt          |"          j        }t	          |"          rt	          |"          ddȅ         nd}d}t?          |"dd          }dːtC                      v rt          |0          nd6}|dk    rd}n|dk    rd}n|dk    rt?          |"dd          }i }	 |?|                                                    di           }tu          |t                    si }n# t           $ r Y nw xY w|                    d          dk    rC|                    dҦ          }|r)|d6k    r#d6dl}|                    |dz            }d| d՝}n=d}n:d}n7|dk    rd}n.|dv r*|dk    r	 Y d}"~"| y                    |           dS |dBk    rd}d| d| d| dߝcY d}"~"| y                    |           S d}"~"ww xY w# | y                    |           w xY w)zAInner handler that runs under the _running_agents sentinel guard.rF   rG   NP   r   r  z3inbound message: platform=%s user=%s chat=%s msg=%rr  z1telegram topic recovery: chat=%s user=%s %r -> %sr:  r   r  z%Failed to read Telegram topic bindingTrp   r  z$compression-tip lookup failed for %szcompression-tip-walkr  z'Failed to record Telegram topic bindingwas_auto_resetFr  is_fresh_resetzsession:start)r!   r  r  r  privacy
redact_pii)r  auto_reset_reasonidlerw  zy[System note: The user's previous session was stopped and suspended. This is a fresh conversation with no prior context.]dailyz[System note: The user's session was automatically reset by the daily schedule. This is a fresh conversation with no prior context.]zy[System note: The user's previous session expired due to inactivity. This is a fresh conversation with no prior context.]r
  r  r  )r!   session_typereset_had_activityz+previous session was stopped or interruptedzdaily schedule at z:00rY  hzh r   zinactive for u!   ◐ Session automatically reset (z). Conversation history cleared.
Use /resume to browse and restore a previous session.
Adjust reset timing in config.yaml under session_reset.r   z.Auto-reset notification failed (non-fatal): %s
auto_skillr   )_load_skill_payload_build_skill_messager  z[IMPORTANT: The "zB" skill is auto-loaded. Follow its instructions for this session.]z#[Gateway] Auto-skill '%s' not foundz0[Gateway] Auto-loaded skill(s) %s for session %sz-[Gateway] Failed to auto-load skill(s) %s: %sr   )estimate_messages_tokens_roughrc  zanthropic/claude-sonnet-4.6g333333?r   r  r   re  r  r  compressionr  >   rd  r  r  hygiene_hard_message_limitr=  r  get_compatible_custom_providerscustom_providersr   models)r  r  rf  r  ffffff?actual	estimateduf   Session hygiene: %s messages, ~%s tokens (%s) — auto-compressing (threshold: %s%% of %s = %s tokens),r  AIAgentc                     g | ]Y}|                     d           dv |                     d          .|                     d           |                     d          dZS )r   >   r   r   r   r   r  r  s     rC   r  z<GatewayRunner._handle_message_with_agent.<locals>.<listcomp>m   sk     ) ) )$%#$55==4I#I#I$%EE)$4$4 $J *+v155CSCS T T#I#I#IrL   r  )r  r	  
quiet_modeskip_memoryenabled_toolsetsr  c                      d S r  r   )akws     rC   r   z:GatewayRunner._handle_message_with_agent.<locals>.<lambda>   s    D rL   c                  4                         d           S )NrG   )approx_tokens)_compress_context)_approx_tokens
_hyg_agent	_hyg_msgss   rC   r   z:GatewayRunner._handle_message_with_agent.<locals>.<lambda>   s%    
0L0L,5r:H 1M 1* 1* rL   zhygiene-compressionu>   Session hygiene: compressed %s → %s msgs, ~%s → ~%s tokensz3Session hygiene: still ~%s tokens after compressioncontext_compressor_last_compress_aborted_last_summary_errorr  u$   ⚠️ Context compression aborted (u   ). No messages were dropped — conversation is unchanged. Run /compress to retry, /reset for a clean session, or check your auxiliary.compression model configuration.z9Failed to deliver compression-failure warning to user: %s_last_aux_model_failure_model_last_aux_model_failure_erroru%   ℹ️ Configured compression model `z
` failed (u   ). Recovered using your main model — context is intact — but you may want to check `auxiliary.compression.model` in config.yaml.z7Failed to deliver aux-model-fallback notice to user: %sz(Session hygiene auto-compress failed: %sz

[System note: This is the user's very first message ever. Briefly introduce yourself and mention that /help shows available commands. Keep the introduction concise -- one or two sentences max.])PROFILE_BUILD_FLAGr  r  profile_build_directiveprofile_build_modeaskrr  z@Profile-build onboarding directive failed, using plain intro: %sz/hermes sethomez/sethomeu    📬 No home channel is set for z^. A home channel is where Hermes delivers cron job results and cross-platform messages.

Type z8 to make this chat your home channel, or ignore to skip.get_voice_channel_contextrK  r  r   r  rN  )r!   r  r   r  r  r  rl   zagent:start)	rl   context_promptr   r  r  r  r  event_message_idr   stop_typinguK   Discarding stale agent result for %s — generation %d is no longer currentr  pop_post_delivery_callback
generation_post_delivery_callbacksr  (empty)u   ⚠️ The model returned no response after processing tool results. This can happen with some models — try again or rephrase your question.r!  r  zMresponse ready: platform=%s chat=%s time=%.1fs api_calls=%d response=%d charsz&clear_resume_pending failed for %s: %sr  zagent-result-compressionr  r  r^  last_reasoning   z
_... (z more lines)_u   💭 **Reasoning:**
```
z
```

)build_footer_linelast_prompt_tokensr  )r>  platform_keyr  context_tokensre  r  zruntime_footer build failed: %salready_sentz	agent:endr  r1  r   zProcess watcher setup error: %sr^   r  >   r  r  &Watch notification injection error: %szWatch queue drain error: %sr  ru  compression_exhaustedc              3       K   | ]}|v V  	d S r  r   )r   r  _err_str_for_classifys     rC   r  z;GatewayRunner._handle_message_with_agent.<locals>.<genexpr>;"  s9        aq11      rL   )zcontext lengthzcontext sizezcontext windowzmaximum contextztoken limitztoo many tokenszreduce the lengthzexceeds the limitzrequest entity too largezprompt is too longzpayload too largezinput is too longr  r  zjSkipping transcript persistence for context-overflow failure in session %s to prevent session growth loop.up   Transient agent failure in session %s — persisting user message so conversation context is preserved on retry.z7Auto-resetting session %s after compression exhaustion.u   

🔄 Session auto-reset — the conversation exceeded the maximum context size and could not be compressed further. Your next message will start a fresh session.r  r   )r   r  r  r!   r   r   )r   r   r   r  )skip_dbr"  r   r   r   r   )r  )r  ztrailing footer send failed: %szAgent error in session %srR  ir   z<Failed to persist inbound user message after agent exceptionr  zno details availablestatus_coder   i  zH Check your API key or run `claude /login` to refresh OAuth credentials.i  zG Your API balance or quota is exhausted. Check your provider dashboard.i  usage_limit_reachedresets_in_secondsr   z9 Your plan's usage limit has been reached. It resets in ~zh.zG Your plan's usage limit has been reached. Please wait until it resets.z@ You are being rate-limited. Please wait a moment and try again.i  z= The API is temporarily overloaded. Please try again shortly.>   r   rN  r  z% The request was rejected by the API.zSorry, I encountered an error (z).
z1Try again or use /reset to start a fresh session.r   )r   rr   r!   rF   r+   r{   r   ru   r:  r  r  r   r8  r  r;  r  rt   rg  ry  r  r  r  r  get_telegram_topic_bindingr;  r'   get_compression_tipr  rz  r/  r,  rH   r.  r@  r  r  
created_atr  r  r  r5  r  r  _set_session_envr  r   r  get_reset_policynotifynotify_exclude_platformsrQ  at_houridle_minutes_format_session_infor   r  r  r  r   r  r  r  r  r(   rv   r5   load_transcriptr   rt  r  rc  r6   r   r   r   r,   rM  r  r  r  r  r  	run_agentr  	_print_fnrz  r6  run_in_executorrx  rewrite_transcriptr{  rd  has_any_sessionsr  r  r  r  r  r  rY  r  r  r  rS  r%   r9  r  rn  r8  DISCORD_get_guild_idr  r  _bind_adapter_run_generation
_run_agentr   r  _is_session_run_currentr^   r  r  _clear_session_envr  r|  r  r  r   r  r  r  rI   r  gateway.runtime_footerr  r&   rf  r2  r<  rH  r  r=  r<  ru  completion_queueempty
get_nowaitr  _inject_watch_notificationr  reset_sessionr   r   	isoformatappend_to_transcriptr  r  update_session_should_send_voice_reply_send_voice_reply_deliver_media_from_responserk   r  r  r_   r,  mathceil)r  rK  r  r  r  _msg_start_time_platform_name_msg_previewr7  r+  r  bindingbound_session_idcanonical_session_idr  _is_new_sessionri   _session_env_tokens_redact_pii_pcfgr  reset_reasonr  policyrL  had_activityshould_notifyr   reason_textr_  minsdurationr   session_infor  _auto_skill_namesr  r  _combined_parts_loaded_names_snamer<  _loaded_skill
_skill_dir_display_namer  _partr   r  rc  
_hyg_model_hyg_threshold_pct_hyg_compression_enabled_hyg_hard_msg_limit_hyg_config_context_length_hyg_provider_hyg_base_url_hyg_api_key	_hyg_data
_model_cfg_raw_ctx	_comp_cfg_raw_hard_limitrU  _hyg_runtime_gw_gcp_hyg_custom_providers_cp_cp_url
_cp_models_cp_model_cfg_cp_ctx_hyg_context_length_compress_token_threshold_warn_token_threshold
_msg_count_stored_tokens_token_source_HARD_MSG_LIMIT_needs_compress	_hyg_metar  rh   _compressedr  _hyg_new_sid
_new_count_new_tokens_comp_err	_warn_msgr  _werr
_aux_model_aux_err_aux_msg_intro_noter  r  r  r  r  _onb_cfg_pb_errenv_keysethome_cmdguild_id
vc_contextrR  r)  r  _typing_adapter_stale_adapterr  agent_messages_response_time
_api_calls	_resp_lenrD  _rds_show_reasoning_effectiver  linesdisplay_reasoning_footer_line_bfl_footer_errr2  r^  rM  r  _pr_watch_eventsr  r  
synth_texte2agent_failed_earlyis_context_overflow_failurer  	tool_defsagent_persisted_user_entryr  r2  _user_msg_id_attachedr   r   _already_sent_media_adapter_foot_adapter_err_adapter_already_persisted_recent_transcript_msg
error_typer  status_hintr  	_hist_len	_err_body	_err_json
_resets_inr	  _hoursr  r  r  r  s                                                                                                                                                                         @@@@rC   r  z(GatewayRunner._handle_message_with_agent  s.     )++29&/72S2Sm..Y\]c]lYmYm
(b#2#.66tSAAAF,KK)N'i	
 	
 	
 ::6BB	 KKC0@)   !(9EEEF%    *@@HH#/"";777''// 7	[ %0$*EE//!&"233 F    ,0     DtTTT  .[#&w{{<'@'@'FB#G#G  $ @(8(D	@/3/?/S/S,0 0,, % @ @ @B,t %    0@,,,@ -@04DDD+?(# 1(8M<T(T(T  $1@@N^__H+(0 %(CL0I0I0OR,P,PPP556L 6   [77NNNN  [ [ [LL!JUYLZZZZZ[="2E:: 	A
 )--k4@@@00dCCCt344 A)--k4@@@ $(@@ ?}&6>>?}&6>> 	 ="2E:: 	1+0M( 	*///5;_LFO11"!>+6*	4 4          (]KK #33G<< 	(**E		) 4 4 :??eTTUUKK 	 	 	D	 6g+VVV ="2E:: <	3"=2EtLLVPVL{**  [((  f  [)F2^CN+R+2CC#_!(d!C!C D   :@ P 5 5b&}6JERR !- ; !M M$M%V-LL 
 ! "m//@@G ';66*WKK)W44*Rv~*R*R*RKK$*$72$=E#)#6#;D:>'p%{{{{[`DpuDWDWPTDWDWDWDWimfpfpfpH*D(*D*DKW W W W !+/+D+D+F+FL+ G,2)F)F)F)F( ! ! ! D!%ll"NF%)%E%Ef%M%M +           R R RMqQQQQQQQQR ,1M(.2M+ |T22 	au 	a&0&<&<ME77$u++LaZZZZZZZZ-/+-* V VF11&*MMMG VCJ@z=J J J J  !5 4]JPU V V  9+225999)00888'LfUUUU" #**5:666!'_!=!=EJKKJ%{    a a aNP\^_````````a $44]5MNN"  m	s7||q((        7J!%'+$"%)-& M MLIN022	 "%!*w!;!;J!*c22 K%/

#J55 K%/^^I%>%>%g*..QXBYBY%g]g
 $.>>2B#C#C#/%=@]] : :$-z#: % % % $% )3z(B(B(Jd(2z(B(B(Jd
 !*mR @ @I!)T22 %36%MM)T::4 4%''%94:0 +4--8T*U*U*6%*-o*>*>#*Q;;:A$7$-z#: % % % $%
/3/R/R%$/1;It1L1L$VIIRV 0S 0 0,J
 %1$4$4Z$@$@$QMM$0$4$4Z$@$@$QMM#/#3#3I#>#>#N,LL    D .5-5;dddddd4;GI4F4F11( ; ; ;4=MMBT4U4U1#-.CT#J#J ;8: 5; $9 & &C#-c4#8#8 ) ('*wwz':':'@b&H&H&M&MG& &7m6J6J36O6O+O+O-0WWXr-B-B
#-j$#?#? !V4>NN:r4R4RM'1-'F'F %V2?2C2CDT2U2U+2+>ILW,F %%z2       ( F&>&>*0b(.B*D*0b' ' '# -0'*<<- -) ),,?$,F(G(G% \\
 "/!A!A%%%3N$,MM%C%CG%L%LN$/M$ #6"&?? 5!_4  
 # WKK>"~$9$9=.455.22488   !% @ @IeIefkIlIl m mIK555555373V3V#)(35?	45P5P(Z		VZ 4W 4 40
L
 (++I66 ~N) ))0) ) )I  #9~~22-4W ." ."&2."*434/3046>Z/</G." ." ." ."
l!N;P;PJ$8+2+C+E+ED;?;O;O(,)* )* )* )* )* )*<& <& 6& 6& 6& 6& 6& 6&NK 4>3HL'3}7O'O'OCO(@(,(:(@(@(B(B(B(,(I(I,2M3H )J )* )* )*
 %)$6$I$I(5(@+%& %& %& HIM$D.9G14[1A1AJ2P2P(33& 3&K %+KK)=(2J+9(=(=+?Q?Q	%& %& %& (36K'K'K(.-:/:,>,>)* )* )* -4J@TVZ,[,[E','8WULdfk=l=l'8/6u>SUY/Z/Z/m^m-=04-= -= -= )2).7;}7H7H7Y7YH/7 -sFN -s6>mmFNT]hqm6r6r0r0r0r0r0r0r0r/8 ). ). ).,2NN0k05-. -. -. -. -. -. -. -.). */):wuNmos?t?t):5<UDceg5h5h
3:5Bacg3h3h3{l{-bT^ -b -b7?-b -b -b )1).7;}7H7H7Y7YH/7 -rFN -r6>mmFNT\gpm6q6q0q0q0q0q0q0q0q/8 ). ). ).,2NN0i05-. -. -. -. -. -. -. -.). %)$<$<[$I$I$I$($A$A*$M$M$M$M %)$<$<[$I$I$I$($A$A*$M$M$M$M$   F         #	.t1BBDD #	.N .              011&&x00E99#GH.@AA : #&=&=&?&??NIl]:<NOOOO"k1N . . .V   +-.  	D6? 	Dv(./P/PU[UdhphxUxUx"O1M*=99G9W%% D (.88 &%# *}7J7J7L7L * * (* * *  33FFCCCCCCCCC ?h...m''(899G))%00H :G :9T(U(U :$>>xHH
 :"&9Z&9&99N "?? @ 
 
 
 
 
 
 
 

 F
 	))Mfo..	
 	
 	
u	9 6<_LFO11"!>!>/RHOPVXceiHjHjrSd!C!CDDDpr$V["==C+6'- H *//-::::::::: "&$-(3'-!%!=!=e!D!D$3 "1 
" 
" 
 
 
 
 
 
L"&-"3"3FO"D"D" Fw'N'N F)55fnEEEEEEEEE    //
NKK a%#"  
 "&!2!26?!C!C4//1MtTT`"=="#1 >     $ R@Z([([ R";??
DQQQJ ##$788888G $''(899?RH 9$$. 
 *--j"==N!Y[[?:N%))+q99JHIKK_ ;)
I    F|TT 11+>>>&;;KHHHH    LL@#R        7hCLL  H 8RRH -- ,|2LP]Ph2h2h+7+E("((***11M2L 2   
	TRRRRRR,0D(**(99$D"3U;;	- -))  T T T,3D:KU,S,S)))T( 
cX 
c!-!1!12B!C!C! c*0022==??E5zzB,0IIeCRCj,A,A))-VE

R-V-V-VV)),:,@,@,B,B)b<MbbX`bbH L"LLLLLL#t 4 6 6!5fo!F!F&**733#/#3#34H!#L#L#QPQ#/#3#34D#E#E#M
~r::       " " ">LLL!"  ; ;1A1A.1Q1Q ;&::L:: *//+ 00X^TcT20 0         CCCCCCC ,<46 1"+H"5"5 / /JAw'(A(A'(J(JKKK3w"}}%mA........./  C C C>BBBBBBBBC?JJJJJJ ".4466 2.99;;C"wwv|<<H#DDD%,,S111	 .4466 2 ) W WC!Ec!J!JJ! WW"&"A"A*c"R"RRRRRRRRR( W W W"LL)QSUVVVVVVVVWWW  ? ? ?:A>>>>>>>>?2 "&l&6&6x&@&@!A!A$'(8(8"(E(E$F$F$L$L$N$N!
 += 
+\%%&=>>?? J     <     J 22Hs7||b7H ( + L!,   
 $ M!,    788 ] { M!,   "00===((555-11+tDDD44[$GGG4!788 E-11+tDDD$ND ))++B
 +  (,,Wb99	"77!, .!*b!7!9!9=C_$TFO$9$9RT%' 	 	 	 #.d:O + :# 8
 (.,UWXX# F03E4D0E0EK-"77!,+ 8     +../?WNN?B>?R?RU`?`?`~kll;;fh $ &+1lY["\"\K' J478H4I4IL1&;;%0# / <   
   *??)4%0XTVWW$3 @    -2)+  776??h66$ 83 8R 8 8 59 #6 9 9 % 0 !: ,E 9 925e6F2G2GE,/481*??)4e$3 @     --)#/#3#34H!#L#L .    !!1!1.!A!ABBM,,UHn[h,ii >,,UH========= // 8H8H8R8R  %)]%6%6v%G%GN% "??$e^           
L	L(,(9(9&/(J(J( "/"4"4 & ,)-)I)I&RVRnRnotRuRu)v)v #5 # #       
 % L L L%FKKKKKKKKL@ ##$788888} | ##$78888y  Y	 Y	 Y	#}00AA CGL-$H$H C&226>BBBBBBBBB   8+FFFl!VXX--,2J}Oh).&0-1-?-O-OP]Ph-i-i**$ 0 0 0-/***0 ();CDD)A B B " "88F++v5526((92E2E2U.!E 6 . $*'3)1)A)A)C)C' '
 #5,== N8;E<L8M8MK5*??)4'    l l l[fjkkkkkla)J+.q66M3q66$3$<<7MLK!!]D99K(1VXX(=(=G1Ic!!h##g###Az488		 ,$-NN$4$4$8$8"$E$E	))T:: +(*I    D==((,AAA!*/B!C!CJ! pj1nn#!%:+<!=!=&lbh&l&l&l&o"dKK##]
** r>>1    ##$788888 !C''"IKD* D DD DD D D     ##$78888yY	x ##$78888s  D
 

DD(A	F2 2(GGH+ +)IIK+ +&LLAR 
RR+C&X7 W0 /X7 0
W=:X7 <W==9X7 7
Y&Y!!Y&6C^
 

^:^55^:Bm: &b6 5m: 6c
m: 	c

B*m: 5f m: f!m:  f!!m: %A7h m: 
h*'m: )h**	m: 5i m" 6j =m" ?j  C!m" !m: "m63m: 5m66m: :
nn&B ,E~# ;Az ~# 
z6z1,~# 1z66A~# :A} ~# 
}5}0+~# 0}55~# 8+ #, 
A@==A@@#AAB B
AB6B AB1B1AB6ICA}= LAAM. M-A}= M.
AM;M8A}= M:AM;M;B9A}= QB<A}= T	AT$ T#A}= T$
AUT.AUU
A}= UAUUBA}= W0AAX3 X2A}= X3AYYA}= YAYYB,A}= [>BA^ ^A}= ^
A_^A^=^8A}= ^=A__AA}= `A4Ab bA}= b
Ab;bAb6b1A}= b6Ab;b;A}= b?BAf) eAe5e4Af) e5
Af$e?AffAf) fAf$f$Af) f(A}= f)
Agf3AggA}= gAggSA}= z0A+A| |A}= |
A}|&A}}A}= }A}}A}= }&A}= }=BK3~	AABK.
A(%BK.'A((BK.@BD@B@?@>BD@?BAABDABAACBDDBK.D&BD>D;BK.D=BD>D>B!BK.G ABH"H!BK.H"
BH/H,BK.H.BH/H/A6BK.J%BK6 K BK.KBK3KBK6 K.BK3K3BK6 K6BLc                     ddl m}m} t                      }d}d}d}d}d}d}		 t	                      }	|	r|	                    di           }
t          |
t                    rm|
                    d          }|(	 t          |          }n# t          t          f$ r Y nw xY w|
                    d          pd}|
                    d          pd}	 ddlm}  ||	          }n%# t          $ r |	                    d	          }Y nw xY wn# t          $ r Y nw xY w|`|	r]	 |	                    d	g           }|r2|D ].}t          |t                    s|                    d          pd
}|                    d          pi }|rF||k    r@|                    d          }|)	 t          |          } n# t          t          f$ r Y nw xY wt          |t                    r|                    |          }t          |t                    r|                    d          }n|}|Ft          |t          t          f          r*	 t          |          } n# t          t          f$ r Y *w xY w0n# t          $ r Y nw xY w	 t                      }|p|                    d          }|p|                    d          }|                    d          }n# t          $ r Y nw xY w |||pd
|pd
||pd
|          }|d}n||k    rd}nd}|dk    r
|dz  dd}n|dk    r	|dz   d}nt!          |          }d| dd|pd d| d| dg}|r$d|v sd|v sd|v r|                    d |            d!                    |          S )"a  Resolve current model config and return a formatted info block.

        Surfaces model, provider, context length, and endpoint so gateway
        users can immediately see if context detection went wrong (e.g.
        local models falling to the 128K default).
        r   )rc  DEFAULT_FALLBACK_CONTEXTNr  re  r  r  r  r  rG   r  r  )r  r  rf  r  r  r  u:   default — set model.context_length in config to overridedetectedi@B z.1fMi  Ku   ◆ Model: `r  u   ◆ Provider: 
openrouteru   ◆ Context: z	 tokens (r  	localhostz	127.0.0.1z0.0.0.0u   ◆ Endpoint: r   )rt  rc  r	  r  r  r'   r   r6   r   r   r   r  r  rt   r   r2  r+   r(   r5   )r  rc  r	  r  rf  r  r  r  custom_provsr  r.  raw_ctxr  r  cpcp_model	cp_models
raw_cp_ctxmodel_entry	model_ctxr+  re  
ctx_sourcectx_displayrl	  s                            rC   r  z"GatewayRunner._format_session_infoO#  s    	\[[[[[[[&(( $	'))D @ HHWb11	i.. A'mm,<==G*!47LL11 ):6 ! ! ! D!(}}Z88@DH(}}Z88@DH@QQQQQQ#B#B4#H#HLL  @ @ @#'88,>#?#?LLL@ 	 	 	D	 !(T(#'88,>#C#C # ). ) ))"d33 %$#%66'??#8b$&FF8$4$4$:	# )E(9(9)+0@)A)AJ)5!)<?
OO$9$)E(1:'> !) !) !)$(D!) &i66 )*3--*>*>K)+t<< 8,7OO<L,M,M		,7	(4IPSUZ|9\9\4!)<?	NN$9$)E(1:'> !) !) !)$(D!)   	355G:7;;z#:#:H:7;;z#:#:Hkk),,GG 	 	 	D	 21^Mr"7^)
 
 
 !,!JJ777UJJ#J Y&&+i7>>>>KKu$$+u4777KKn--K $5###7X577?K??*???
  	600K84K4Ky\dOdOdLL4(44555yys   AD 7B D BD B1D C D D>D  DD 
DDBI( F/-I( /G I( GA8I( <II( I"I( !I""I( (
I54I59AK 
KKcanonical_cmdc                    ddl m} |sdS  || j        |          }|j        r|                    |j        |          rdS t                              d||j        r|j        j	        nd|j                   t          |j                  }|rFdd                    d |dd	         D                       z   t          |          d	k    rd
ndz   dz   }nd}d| d| S )u  Return a denial message if ``source`` cannot run ``canonical_cmd``,
        else None. Used by both the cold and running-agent dispatch paths
        in ``_handle_message`` so admin/user gating can't be bypassed by
        an in-flight agent.

        Backward-compat semantics live in
        :func:`gateway.slash_access.policy_for_source` — when the operator
        hasn't set ``allow_admin_from`` for the scope, the policy returns
        ``enabled=False`` and this method always returns None.
        r   )policy_for_sourceNzLSlash command /%s denied for %s:%s (not admin, not in user_allowed_commands)r  zYou can run: r  c              3       K   | ]	}d | V  
dS )r   Nr   )r   r  s     rC   r  z4GatewayRunner._check_slash_access.<locals>.<genexpr>#  s(      BBGGGBBBBBBrL   r\   rI  rG   z . Use /whoami for the full list.zNo slash commands are enabled for non-admins on this platform. Ask an admin to add you to allow_admin_from or to set user_allowed_commands.u   ⛔ /z is admin-only here. )gateway.slash_accessr	  r  r  can_runr  ru   r:  r!   rF   r  user_allowed_commandsr5   r   )r  r  r	  _policy_for_sourcer$	  allowed_previewsuffixs          rC   r  z!GatewayRunner._check_slash_access#  s/    	QPPPPP 	4##DK88~ 	!N!N 	4Z%+_=FO!!#N		
 	
 	
 !!=>> 	))BB_SbS-ABBBBBC00255552? 55 F3 
 D}CC6CCCrL   own_keyc           	         t          |dd          }t          |dd          }|r|sg S |j        j        }t          |dd          pd}d                    d||t	          |          t	          |          g          }g }t          | j                                                  D ]K\  }	}
|	|k    r|
t          u s|
s|	|k    s|		                    |dz             r|
                    |	           L|S )u  Find running-agent keys for OTHER participants in the same thread.

        Only applies when the message originates in a thread.  In per-user
        thread mode (``thread_sessions_per_user=True``) each participant gets
        an isolated session key of the form
        ``agent:main:{platform}:{chat_type}:{chat_id}:{thread_id}:{user_id}``,
        so a run started by another user is invisible to the caller's own
        ``/stop``.  This returns the keys of any *actually running* agents
        (not the pending sentinel, not the caller's own key) whose key shares
        the caller's ``{chat_id}:{thread_id}`` prefix.

        Returns an empty list when the source is not in a thread, or when no
        sibling runs exist — callers must still gate on authorization.
        r  Nr   r  rG   rZ  z
agent:main)rH   r!   rF   r5   r+   r  rq  r   r  rI  r(   )r  r  r	  r  r   r!   r  r  matchesr  rx  s              rC   _sibling_thread_run_keysz&GatewayRunner._sibling_thread_run_keys#  s    FK66	&)T22 	 	I?(FK66<"	 8YGc)nnM
 
 t399;;<< 	$ 	$JCg~~///u/f}}v| < <}s###rL   c                    ||j         dS |j        dS |j         j        dS 	 |j         j        j        }n# t          $ r Y dS w xY w|dk    rdS 	 t
          dz  }|                                sdS t          j        |	                                          }n# t          $ r Y dS w xY w|
                    d          |k    rdS |
                    d          }t          |t                    sdS |
                    d          }t          |t          t          f          rt          j                    |z
  dk    rdS |j        |k    S )	aB  Return True if this /restart is a Telegram re-delivery we already handled.

        The previous gateway wrote ``.restart_last_processed.json`` with the
        triggering platform + update_id when it processed the /restart.  If
        we now see a /restart on the same platform with an update_id <= that
        recorded value AND the marker is recent (< 5 minutes), it's a
        redelivery and should be ignored.

        Only applies to Telegram today (the only platform that exposes a
        numeric cross-session update ordering); other platforms return False.
        NFr   z.restart_last_processed.jsonr!   	update_idr  r  )r  platform_update_idr!   rF   rt   rY  r.   r,  r-  r  r'   r   r   r   r   )r  rK  r   marker_pathr  recorded_uidr  s          rC   _is_stale_restart_redeliveryz*GatewayRunner._is_stale_restart_redelivery#$  sy    =EL05#+5< (5	"\28NN 	 	 	55	Z''5	&)GGK%%'' u:k335566DD 	 	 	55	 88J>115xx,,,,, 	5 xx//lS%L11 	y{{\)C//u'<77s'   6 
AAB 0&B 
B%$B%c                 "  K   |                                 pd                                }|j        }d}	 t          |j        dd          pt          t          |dd          pd          }t          |dd          }|r4|r2|t          |          t          |dd          t          |dd          d}n# t          $ r d}Y nw xY w	 d	d
lm}  |||d          S # t          $ r*}t          
                    d|           d| cY d}~S d}~ww xY w)zHandle /suggestions in the gateway.

        Delegates to the shared handler so CLI and gateway never drift. The
        origin is built from the event source so an accepted suggestion's job
        delivers back to this chat/thread.
        rG   NrF   r!   r   rq  r  r!   r   rq  r  r   )handle_suggestions_commandr  rE  r  zsuggestions command failed: %szSuggestions command failed: )r  rI   r  rH   r!   r+   rt   hermes_cli.suggestions_cmdr	  ru   r;  )	r  rK  r$  r  rE  r!   r   r	  r  s	            rC   r  z)GatewayRunner._handle_suggestions_command\$  sl      &&((.B5577	v>>l#gfV`bdFeFeFkikBlBlHfi66G G  ("7||!(d!C!C!(d!C!C	   	 	 	FFF		6MMMMMM--d69UUUU 	6 	6 	6LL91===5!55555555	6s0   A=B3 3CCC 
D$D	D	Dc                 @  K   |                                 pd                                }|j        }d}	 t          |j        dd          pt          t          |dd          pd          }t          |dd          }|r4|r2|t          |          t          |dd          t          |dd          d}n# t          $ r d}Y nw xY w	 d	d
lm}  |||d          S # t          $ r9}t          
                    d|           d	dlm}	  |	d|           cY d}~S d}~ww xY w)a
  Handle /blueprint in the gateway.

        Delegates to the shared handler so CLI, TUI, and gateway never drift.
        Returns a BlueprintCommandResult: ``text`` is shown to the user, and if
        ``agent_seed`` is set the dispatch site rewrites ``event.text`` to the
        seed and falls through to the agent (the ``/steer`` pattern) so the
        agent gathers the slot values conversationally. Origin is built from the
        event source so a directly created blueprint job delivers back to this chat.
        rG   NrF   r!   r   rq  r  r	  r   )handle_blueprint_commandr  r	  zblueprint command failed: %s)BlueprintCommandResultzCron blueprint command failed: )r  rI   r  rH   r!   r+   rt   hermes_cli.blueprint_cmdr	  ru   r;  r	  )
r  rK  r$  r  rE  r!   r   r	  r  r	  s
             rC   r  z'GatewayRunner._handle_blueprint_commandz$  s      &&((.B5577	v>>l#gfV`bdFeFeFkikBlBlHfi66G G  ("7||!(d!C!C!(d!C!C	   	 	 	FFF		QIIIIII++DSSSS 	Q 	Q 	QLL7;;;GGGGGG))*OA*O*OPPPPPPPP		Qs0   A=B3 3CCC 
D$.DDDc                 `   	 t          | j        t                    r| j        pi                     di           nt	          | j        di           pi }|s'ddlm}  |            pi                     d          pi }t          |                    dd          pd          S # t          $ r Y dS w xY w)a%  Resolve the configured /goal turn budget for gateway sessions.

        GatewayRunner.config is a GatewayConfig dataclass, not the full
        user config mapping. Top-level config blocks such as ``goals`` are
        therefore only available through hermes_cli.config.load_config().
        goalsr   r9  ry  rH  )	r   r  r6   r'   rH   r  r:  r   rt   )r  	goals_cfgr:  s      rC   _goal_max_turns_from_configz)GatewayRunner._goal_max_turns_from_config$  s    	 dk400="''444T['266<" 
  E999999([]]0b55g>>D"	y}}["55;<<< 	 	 	22	s   BB 
B-,B-c                    	 ddl m} n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY w	 | j                            |j                  }n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY wt          |dd          pd}|sdS | 	                                } |||	          |fS )
zReturn a GoalManager bound to the session for this gateway event.

        Returns ``(manager, session_entry)`` or ``(None, None)`` if the
        goals module can't be loaded.
        r   r  zgoal manager unavailable: %sNr  z'goal manager: session lookup failed: %sr  rG   r  default_max_turns)
r  r  rt   ru   r;  rg  ry  r  rH   r	  )r  rK  r  rM   r+  sidry  s          rC   _get_goal_manager_for_eventz)GatewayRunner._get_goal_manager_for_event$  s   	4444444 	 	 	LL7===:::::		 .DDU\RRMM 	 	 	LLBCHHH:::::	 m\488>B 	:4466	{cYGGGVVs'   	 
949A 
B'BBrl   c                   K   | j                             |j                  }|s,t                              dt          |dd                     dS 	 |                     |          }n# t          $ r d}Y nw xY w|                    |j	        ||           d{V }|=t          |dd          s.t          
                    dt          |dd	                     dS dS dS )
zCSend a /goal judge status line back to the originating chat/thread.$goal continuation: no adapter for %sr!   Nr   r(  T)goal continuation: status send failed: %sru  r  )rQ  r'   r!   ru   r;  rH   r  rt   r   r   rv   )r  r  rl   r   r   r  s         rC   _send_goal_status_noticez&GatewayRunner._send_goal_status_notice$  s     -##FO44 	LL?Q[]aAbAbcccF	77??HH 	 	 	HHH	 ||FNGh|OOOOOOOOgfi&F&FNN;99     s   A' 'A65A6c                 \   K    j                             j                  }|s,t                              dt          dd                     dS d fd}	                                }n# t          $ r d}Y nw xY w|rt          |d          r	 d}t          |di                               |          }|t          |dd          }|	                    |||	           dS # t          $ r%}t                              d
|           Y d}~nd}~ww xY w |             d{V  dS )a  Send a /goal status line after the main response is delivered.

        The gateway message handler returns the agent response to the platform
        adapter, which sends it after this method's caller has returned.  For a
        natural Discord/Telegram reading order, goal status belongs after that
        send.  Platform adapters provide a one-shot post-delivery callback for
        exactly this boundary; when unavailable, fall back to direct awaited
        delivery rather than silently dropping the notice.
        r	  r!   Nr   c                     K   	                                 d {V  d S # t          $ r(} t                              d| d           Y d } ~ d S d } ~ ww xY w)Nr	  Trp   )r	  rt   ru   rv   )rM   rl   r  r  s    rC   _deliverzHGatewayRunner._defer_goal_status_notice_after_delivery.<locals>._deliver$  s      `33FGDDDDDDDDDDD ` ` `JCZ^_________`s   # 
AAAregister_post_delivery_callback_active_sessions_hermes_run_generationr  zAgoal continuation: post-delivery callback registration failed: %sr  )
rQ  r'   r!   ru   r;  rH   r  rt   rr   r	  )	r  r  rl   r   r	  r  r  rO  rM   s	   ```      rC   (_defer_goal_status_notice_after_deliveryz6GatewayRunner._defer_goal_status_notice_after_delivery$  s      -##FO44 	LL?Q[]aAbAbcccF	` 	` 	` 	` 	` 	` 	` 	`	66v>>KK 	 	 	KKK	  	g77,MNN 	gg!
 *<bAAEEkRR%!(1I4!P!PJ77) 8   
  g g g`beffffffffg hjjs+   A2 2B BAC* *
D4DDr+  r  c                n  K   	 ddl m} n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY wt          |dd          pd}|sdS |                                 } |||          }|                                sdS |                    |pdd	          }	|		                    d
          pd}
|
r|| 
                    ||
           d{V  |		                    d          sdS |		                    d          pd}|r|dS 	 | j        	                    |j                  }|                     |          }|r9|r9t          |t          j        |dd          }|                     |||           dS dS dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)a  Run the goal judge after a gateway turn and, if still active,
        enqueue a continuation prompt for the same session.

        Called from ``_handle_message_with_agent`` at turn boundary, AFTER
        the response has been delivered. Safe when no goal is set.

        We use the adapter's pending-message / FIFO machinery so any real
        user message that arrives simultaneously is handled by the same
        queue and takes priority naturally.
        r   r  z/goal continuation: goals module unavailable: %sNr  rG   r	  T)user_initiatedrl   should_continuecontinuation_promptre  z%goal continuation: enqueue failed: %s)r  r  rt   ru   r;  rH   r	  r  evaluate_after_turnr'   r	  rQ  r!   r  r  r  r  r  )r  r+  r  r  r  rM   r	  ry  mgrr|  r   r  r   r  
cont_events                  rC   r  z*GatewayRunner._post_turn_goal_continuation%  sY     "	4444444 	 	 	LLJCPPPFFFFF	 m\488>B 	F4466	kSIFFF}} 	F**>+?RPT*UUll9%%+  	M6%??LLLLLLLLL||-.. 	F344: 	F	Gm''88G55f==J D: D)!,!1!##'  
 "":z7CCCCCD D D D  	G 	G 	GLL@#FFFFFFFFF	Gs)    
;6;A-F 
F4F//F4c                     t          | dd          }|dS t          |d          r|j        rt          |j                  S t          |d          r|j        r|j        j        S dS )z5Extract Discord guild_id from the raw message object.raw_messageNrb	  guild)rH   rr   rb	  r   r	  r]   )rK  r   s     rC   r	  zGatewayRunner._get_guild_idR%  st     e]D11;43
## 	% 	%s|$$$3   	 SY 	 9<trL   c                 x   K    j                             |j        j                  }t	          |d          sdS                      |          }|sdS |                    ||j        j                   d{V }|sdS t	          |d          r j        |_	        t	          |d          r j
        |_        t	          |d          r
 fd	|_        	 |                    |           d{V }n# t          $ rs}t                              d
|           d|_	        t#          |                                          }d|v sd|v sd|v rdt&          j         dcY d}~S d| cY d}~S d}~ww xY w|rt+          |j        j                  |j        |<   t	          |d          r!|j                                        |j        |<   d j                             |j        j        |j        j                  <                                                          ||j        j        d           d|j         dS d|_	        dS )z.Join the user's current Discord voice channel.join_voice_channelz2Voice channels are not supported on this platform.z,This command only works in a Discord server.Nz(You need to be in a voice channel first._voice_input_callback_on_voice_disconnect_voice_mode_getterc                     j                                                 t          j        t          |                     d          S )Nr  )r  r'   r  r  r	  r+   )r   r  s    rC   r   z:GatewayRunner._handle_voice_channel_join.<locals>.<lambda>z%  s6    9I9M9M 0#g,,??: : rL   z Failed to join voice channel: %spynaclnacldaveyz@Voice dependencies are missing (PyNaCl / davey). Install with: `z -m pip install PyNaCl`zFailed to join voice channel: _voice_sourcesr  T)r  zJoined voice channel **zL**.
I'll speak my replies and listen to you. Use /voice leave to disconnect.zFFailed to join voice channel. Check bot permissions (Connect + Speak).)rQ  r'   r  r!   rr   r	  get_user_voice_channelr  _handle_voice_channel_inputr	  _handle_voice_timeout_cleanupr	  r	  r	  rt   ru   rv   r+   r,   r    r  r   r   _voice_text_channelsto_dictr	  r  r  r  r  rf   )r  rK  r   rb	  voice_channelr(  r  	err_lowers   `       rC   _handle_voice_channel_joinz(GatewayRunner._handle_voice_channel_joina%  s     -##EL$9::w 455 	HGG%%e,, 	BAA%<<el*
 
 
 
 
 
 
 
  	>== 7344 	M,0,LG)7233 	N+/+MG( 7011 	* * * *G&	8#66}EEEEEEEEGG 		8 		8 		8NN=qAAA,0G)AI9$$)(;(;w)?S?SN&)nN N N      8A77777777		8  
	589M5N5NG(2w 011 J38<3G3G3I3I&x0]bDT__U\-BELDXYYZ""$$$..w8LVZ.[[[\-*< \ \ \
 )-%WWs+   C, ,
E)6AE$E)E$E)$E)c                   K   | j                             |j        j                  }|                     |          }|rt          |d          sdS t          |d          r|                    |          sdS 	 |                    |           d{V  n2# t          $ r%}t          
                    d|           Y d}~nd}~ww xY wd| j        |                     |j        j        |j        j                  <   |                                  |                     ||j        j        d           t          |d	          rd|_        d
S )z Leave the Discord voice channel.leave_voice_channelzNot in a voice channel.is_in_voice_channelNzError leaving voice channel: %sr  Tr  r	  zLeft voice channel.)rQ  r'   r  r!   r	  rr   r	  r	  rt   ru   rv   r  r  r   r  r  r	  )r  rK  r   rb	  r  s        rC   _handle_voice_channel_leavez)GatewayRunner._handle_voice_channel_leave%  su     -##EL$9::%%e,, 	-ww0EFF 	-,,w 566 	-g>Y>YZb>c>c 	-,,	A--h7777777777 	A 	A 	ANN<a@@@@@@@@	A Z_)>@TUUV   ++GU\5ITX+YYY7344 	1,0G)$$s   8B 
CB>>Cc                     d| j         |                     t          j        |          <   |                                  | j                            t          j                  }|                     ||d           dS )zCalled by the adapter when a voice channel times out.

        Cleans up runner-side voice_mode state that the adapter cannot reach.
        r  Tr	  N)r  r  r  r	  r  rQ  r'   r  )r  r   r   s      rC   r	  z+GatewayRunner._handle_voice_timeout_cleanup%  so    
 HM)97CCD   -##H$455++GWt+LLLLLrL   rb	  r  
transcriptc                    ddl m} t          j        dd|                                                                          }t          j        dd|          }|sdS t          j                    d||f}t          | d	d
          }t          |t                    s	i }|| _        fd|                    |g           D             }|D ]d\  }	}
|
|k    r|||<    dS t          |
          dk    r>t          |          dk    r+ |d
|
|                                          dk    r|||<    dS e|                    |f           |dd
         ||<   dS )ar  Suppress repeated STT outputs for the same recent utterance.

        Voice capture can occasionally emit the same utterance twice a few
        seconds apart, which creates a second queued agent run and overlapping
        spoken replies. Dedup exact and near-exact repeats per guild/user over a
        short window while allowing genuinely new turns through.
        r   )SequenceMatcher\s+r  z[^\w\s]rG   Fg      (@r  Nc                 0    g | ]\  }}|z
  k    ||fS r   r   )r   r  txtr   window_secondss      rC   r  z@GatewayRunner._is_duplicate_voice_transcript.<locals>.<listcomp>%  s:     
 
 
CRx>)) I)))rL   T   r  )difflibr	  r   r   rI   r,   r   r  rH   r   r6   r  r'   r   ratior(   )r  rb	  r  r	  r	  r  r  recent_storerecentr  priorr   r	  s              @@rC   _is_duplicate_voice_transcriptz,GatewayRunner._is_duplicate_voice_transcript%  s    	,+++++VFC44::<<BBDD
VJJ77
 	5n!t%@$GG,-- 	:L-9D*
 
 
 
 
'++C44
 
 
  	  	 HAu
""$*S!tt5zzRC
OOr$9$9"?4
;;AACCtKK(.L%44sJ'((("233KSurL   c           	      Z  K   | j                             t          j                  }|sdS |j                            |          }|sdS t          |di                               |          }|r=t          j        |          }t          |          |_	        t          |          |_
        nEt          t          j        t          |          t          |          t          |          d          }|                     |          st                              d|           dS |                     |||          r't                              d|||dd                    dS 	 |j                            |          }|rS|dd                             d	d
                              dd          }	|                    d| d|	            d{V  n# t(          $ r Y nw xY wddlm}
 t/          ||t0          j         |
|d                    }|                    |           d{V  dS )zHandle transcribed voice from a user in a voice channel.

        Creates a synthetic MessageEvent and processes it through the
        adapter's full message pipeline (session, typing, agent, TTS reply).
        Nr	  channel)r!   r   r  r  r  z/Unauthorized voice input from user %d, ignoringz?Suppressing duplicate voice transcript for guild=%s user=%s: %sr    z	@everyoneu   @​everyonez@hereu   @​herez**[Voice]** <@z>: r   )SimpleNamespace)rb	  r	  )r  r{   rE  r	  )rQ  r'   r  r	  r	  rH   r  	from_dictr+   r  r  r  ru   r;  r	  r:  _clientget_channelr   r   rt   typesr	  r  r  rj  r  )r  rb	  r  r	  r   
text_ch_idsource_datar  r	  	safe_textr	  rK  s               rC   r	  z)GatewayRunner._handle_voice_channel_input%  s|      -##H$455 	F155h??
 	F g'7<<@@JJ 	",[99F \\FN"7||F"!)JGg,,#  F ''// 	LLJGTTTF..x*MM 	KKQ4C4 	   F	o11*==G M&uu-55kCTUU]]^egtuu	ll#KG#K#K	#K#KLLLLLLLLL 	 	 	D	 	*)))))$*'FFF	
 
 
 $$U+++++++++++s   "A/G 
GGr  rf	  r  c                 V   |r|                     d          rdS |j        j        }| j                            |                     |j        j        |          d          }|j        t          j	        k    }|dk    p|dk    o|}|sdS t          d |D                       }	|	rdS |r|sdS dS )a  Decide whether the runner should send a TTS voice reply.

        Returns False when:
        - voice_mode is off for this chat
        - response is empty or an error
        - agent already called text_to_speech tool (dedup)
        - voice input and base adapter auto-TTS already handled it (skip_double)
          UNLESS streaming already consumed the response (already_sent=True),
          in which case the base adapter won't have text for auto-TTS so the
          runner must handle it.
        zError:Fr  r  r  c              3      K   | ]K}|                     d           dk    o-t          d |                     d          pg D                       V  LdS )r   r   c              3   p   K   | ]1}|                     d i                                d          dk    V  2dS )r&  rf   r  Nr  )r   tcs     rC   r  zCGatewayRunner._should_send_voice_reply.<locals>.<genexpr>.<genexpr>K&  sX         z2&&**6226FF     rL   r   N)r'   r  )r   r   s     rC   r  z9GatewayRunner._should_send_voice_reply.<locals>.<genexpr>I&  s       
 
  GGFOO{*   77<006B    
 
 
 
 
 
rL   T)rI  r  r   r  r'   r  r!   rE  r  rj  r  )
r  rK  r  rf	  r  r   
voice_modeis_voice_inputshouldhas_agent_ttss
             rC   r	  z&GatewayRunner._should_send_voice_reply(&  s    $  	8..x88 	5,&%))$//%,:OQX*Y*Y[`aa
,0AA 5  ?l*=~ 	  	5  
 
 &
 
 
 
 
  	5  	, 	5trL   r{   c                    K   ddl }d}d}	 ddlm}m}  ||dd                   }|s3	 ||hdhz
  D ]'}		 t	          j        |	           # t          $ r Y $w xY wdS |j        j        t          j
        k    rdnd}
t          j                            t          j                    dd|                                j        dd	          d
|
           }t	          j        t          j                            |          d           t'          j        |||           d{V }	 t+          j        |          }nt# t*          j        t0          f$ r[ t2                              d|r
|dd         n|           Y ||hdhz
  D ]'}		 t	          j        |	           # t          $ r Y $w xY wdS w xY w|                    d|          }|                    d          rt          j                            |          sat2                              d|                    d                     	 ||hdhz
  D ]'}		 t	          j        |	           # t          $ r Y $w xY wdS | j                            |j        j                  }|                     |          }|rRt?          |d          rBt?          |d          r2|                     |          r|!                    ||           d{V  n|rt?          |d          ro| "                    |          }| #                    |j        |          }|tI          |          }d|d<   nddi}|j        j%        |||d} |j&        di | d{V  n4# tN          $ r'}t2                              d|d           Y d}~nd}~ww xY w||hdhz
  D ]'}		 t	          j        |	           # t          $ r Y $w xY wdS # ||hdhz
  D ]'}		 t	          j        |	           # t          $ r Y $w xY ww xY w)zEGenerate TTS audio and send as a voice message before the text reply.r   N)r  _strip_markdown_for_tts  oggmp3hermes_voice
tts_reply_r\   r  T)r  )r{   output_pathz.Auto voice reply TTS returned invalid JSON: %sr
  r  r(  zAuto voice reply TTS failed: %sru  play_in_voice_channelr	  
send_voicer  )r   
audio_pathr  r   zAuto voice reply failed: %srp   r   )(uuidtools.tts_toolr  r
  r%   rb  r*   r  r!   r  r  r/   r5   tempfile
gettempdiruuid4hexmakedirsdirnamerz  r{  r,  r-  r  r   ru   rv   r'   isfilerQ  r	  rr   r	  r
  r  r  r6   r   r
  rt   )r  rK  r{   _uuidr
  actual_pathr  r
  tts_textr  	audio_extresult_jsonr  r   rb	  r  r  send_kwargsr  s                      rC   r	  zGatewayRunner._send_voice_reply^&  su     
E	SSSSSSSS..tETE{;;H x !+.$7  IaLLLL   D q "'!6(:K!K!KQVI#%%~AU[[]].ss3AAiAA J K
33dCCCC ' 1#(
! ! !      KK00()4   Ofq  RCQ\]a^a]aQbQb  xC  D  D  DT !+.$7  IaLLLL   D Y
 !**[*==K::i(( {0K0K @&**WBUBUVVVH !+.$7  IaLLLL   D E m''(=>>G ))%00H 8)@AA8)>??8  33H==8 33HkJJJJJJJJJJ 8WWl;; 8#;;EBB">>u|\ZZ *"&{"3"3K,0K))#+T"2K$|3"- , +	/ / )g(77;777777777 	L 	L 	LNN8!dNKKKKKKKK	L !+.$7  IaLLLL   D j+.$7  IaLLLL   Ds   M% A


AACM% "D7 6M% 7=F(4M%  F
F"!F"'F((A;M% /I
IIDM% $O %
N/NO NO $N99
OOO=O,+O=,
O9	6O=8O9	9O=c                   K   ddl m} ddlm 	 d|v }ddlm}m} |                    |          \  }}	 |j        |          }|	                    |	          \  }
}	|
                    |	          \  }}
 |j        |          }|                     |j        |                     |                    }h d}h d}g }g }|D ]\\  }} ||          j                                        }||v r|s|s|                    |           E|                    ||f           ]g }|D ]S} ||          j                                        |v r|s|                    |           >|                    |           T|rp	 fd|D             }|                    |j        j        ||	           d
{V  n8# t(          $ r+}t*                              d|j        |           Y d
}~nd
}~ww xY w|D ]\  }}	  ||          j                                        } ||j        j        ||          r)|                    |j        j        ||           d
{V  nU||v r)|                    |j        j        ||           d
{V  n(|                    |j        j        ||           d
{V  # t(          $ r+}t*                              d|j        |           Y d
}~d
}~ww xY w|D ]}	  ||          j                                        }||v r)|                    |j        j        ||           d
{V  n(|                    |j        j        ||           d
{V  {# t(          $ r+}t*                              d|j        |           Y d
}~d
}~ww xY wd
S # t(          $ r&}t*                              d|           Y d
}~d
S d
}~ww xY w)u=  Extract MEDIA: tags and local file paths from a response and deliver them.

        Called after streaming has already sent the text to the user, so the
        text itself is already delivered — this only handles file attachments
        that the normal _process_message_background path would have caught.
        r   r   )r  z[[as_document]])r  should_send_media_as_audio   .3gp.avi.mkv.mov.mp4.webm   .gif.jpg.png.jpeg.webpc                 0    g | ]}d  |           dfS )zfile://rG   r   )r   r  _quotes     rC   r  z>GatewayRunner._deliver_media_from_response.<locals>.<listcomp>&  s/    OOOa444b9OOOrL   )r   imagesr   Nz0[%s] Post-stream image batch delivery failed: %s)is_voicer   r
  r   r   
video_pathr   r   r  r   z*[%s] Post-stream media delivery failed: %sz)[%s] Post-stream file delivery failed: %sz'Post-stream media extraction failed: %s)pathlibr   urllib.parser  ru  r  r*
  extract_mediafilter_media_delivery_pathsextract_imagesextract_local_filesfilter_local_delivery_pathsr  r  r  r	  r,   r(   send_multiple_imagesr   rt   ru   rv   rf   r!   r
  
send_videosend_document)r  r  rK  r   r   force_document_attachmentsr  r*
  media_filescleanedr  local_files_thread_meta_VIDEO_EXTS_IMAGE_EXTSr}  non_image_media
media_pathr;
  r  non_image_localr  r:
  r  r9
  s                           @rC   r	  z*GatewayRunner._deliver_media_from_response&  s      	!     000000h	I
 *;h)F&^^^^^^^^#*#8#8#B#B KI-I+VVK !//88JAw$88AANKI-I+VVK;;EL$JfJfglJmJmnnLKKKKDDDK !#K$&O(3 C C$
Hd:&&-3355;&& ( ' : '  &&z2222#**J+ABBBB$&O( 6 6	DOO*0022kAA : B&&y1111#**95555 	hhOOOO;OOOF!66 % 4%!- 7          
 ! h h hNN#UW^Wcefggggggggh )8 b b$
Hb$z**17799C11%,2GW_``` %00$)L$8'1%1 1          
 ++%00$)L$8'1%1 1           &33$)L$8&0%1 4         
 ! b b bNN#OQXQ]_`aaaaaaaab - a a	a$y//06688Ck))%00$)L$8'0%1 1           &33$)L$8&/%1 4         
 ! a a aNN#NPWP\^_````````aa a$  	I 	I 	INNDaHHHHHHHHH	Is   E'N 96F0 /N 0
G%:!G N  G%%
N 0B8J)(N )
K3!KN KN &A7MN 
N(!N	N NN 
O"OOr  r  r  r  rB  rC  c                 >   $%&'()*+,K   ddl m$ |pg }|pg } j                            j                  }|s#t
                              dj                   dS                      |          }	 t                      }	 	                    |	          \  }
}|                    d          s)|
                    j        d d|	           d{V  dS t          j                  )dd
lm} t           ||	)                    &|	                    d          pi }|                    d          pd% j        *t#          t%          j        dd                    (                               ++ _                                          _                             ||
|          ,|'|rg }t3          |          D ]L\  }}|t5          |          k     r||         nd}|                    d          r|                    |           M|rP	                      ||           d{V 'n2# t<          $ r%}t
                              d|           Y d}~nd}~ww xY w$%&'()*+ ,fd}                     |           d{V }|r|                    dd          nd}|s"|r |                    d          rd|d          }|r|                     |          \  }}ddl!m"}  |j#        |          }|$                    |          \  }}|dd         t5          |          dk    rdndz   }d| d}|r'|
                    j        ||z   |           d{V  n*|s(|s&|
                    j        |dz   |           d{V  |pg D ]:\  }}	 |%                    j        |||           d{V  +# t<          $ r Y 7w xY wddl!m&} h d }h d!} |pg D ]\  }!}"t$          j'        (                    |!          d"         )                                }#	  |j        |#|"          r$|*                    j        |!|#           d{V  ns|#| v r$|+                    j        |!|$           d{V  nK|#|v r$|,                    j        |!|%           d{V  n#|-                    j        |!|&           d{V  # t<          $ r Y w xY wdS |dd         t5          |          dk    rdndz   }|
                    j        d| d'|           d{V  dS # t<          $ rg}t
          .                    d(           	 |
                    j        d d)| |           d{V  n# t<          $ r Y n
w xY wY d}~dS Y d}~dS d}~ww xY w)*zCExecute a background agent task and deliver the result to the chat.r   r  z0No adapter for platform %s in background task %sN)r  r>  r  u   ❌ Background task z, failed: no provider credentials configured.r   _get_platform_toolsrx  disabled_toolsetsrz  r  )r  rG   rD  z,Background task vision enrichment failed: %sc            
          d%dd         id         i ddddddd	d
d	j         d                    d          d                    d          d                    d          d                    d          d                    d          d                    dd          d                    d          ddd
j        d
j        d
j        d
j        d
j        d 
j        d!
j        d"	j	        d#	j
        } 	 |                     $          	                    |            S # 	                    |            w xY w)&Nr  r+  r	  r  Tverbose_loggingFr  rW
  r  rU  rR  providers_allowedonlyproviders_ignoredignoreproviders_orderorderprovider_sortsortprovider_require_parametersrequire_parametersprovider_data_collectiondata_collectionr  r!   r  user_id_altr  r   rq  r  r  r  fallback_model)rN  r  r   )r\  r'   r  rf
  r  r   rq  r  r  r  re  run_conversationrd  )rx  r  rW
  r  enriched_promptr	  r  prr  r  r  r  
turn_routes    rC   run_syncz4GatewayRunner._run_background_task.<locals>.run_synch'  s=     $W- +   $2>  $t	
 %*E &6%5 '8&7 &6%5 "&!3!3 '1nn5H&I&I&I ')ffVnnn ')ffX&6&6&6 %'FF7OOO #%&&... 137KU0S0S0S  .0VV4E-F-F-F!"  'w#$ *\%& #NN'( !' 2 2)* %..+, #NN-. %../0 %..12 %..34  $//56 $(#7#77 :9 11%4 ' 2  
 11%8888D11%8888s   E
 
E!r  ru  zError: )r  rY  rf  u&   ✅ Background task complete
Prompt: "z"

rr  z(No response generated))r   	image_urlcaptionr   )r*
  r2
  r+
  r   r<
  r=
  )r   
image_pathr   r?
  z"

(No response generated)zBackground task %s failedz	 failed: )/r 	  r  rQ  r'   r!   ru   rv   r  r  rM  r   r   r  hermes_cli.tools_configrV
  r  rc  r   r%   r9  r  rZ  r[  r\  rW  rH  r   rI  r(   rm  rt   _run_in_executor_with_contextrB
  ru  r  rC
  rD
  
send_imager*
  r/   ry  r,   r
  rH
  send_image_filerI
  rk   )-r  r  r  r  r  rB  rC  r   _thread_metadatar>  r  rI  rV
  r  r}  rM  r/   rO  r  rl
  r  r  rK
  r  r:
  text_contentr   headerrm
  alt_text_should_send_media_as_audiorP
  rO
  rR
  	_is_voicer  r  rW
  r  ri
  r	  r  rj
  r  rk
  s-   ` ``                                @@@@@@@@@rC   _run_background_taskz"GatewayRunner._run_background_task%'  s!	      	&%%%%%%2
!'R-##FO44 	NNMv`ghhhF;;FDTUUr	.00K$($G$G' %H % %!E> "%%i00 llN`7```- #         
 /@@LCCCCCC%&9&9+|&T&TUU#006BI ).A B B Jd'B +BD!I!IJJN#EEVETT%5D"!%!8!8!:!:D88WWJ %O Z (44 1 1GAt./#k2B2B.B.BKNNE''11 1#**4000 ZZ040P0P"K1 1 + + + + + + % Z Z Z'UWXYYYYYYYYZ$9 $9 $9 $9 $9 $9 $9 $9 $9 $9 $9 $9 $9 $9 $9 $9L  ==hGGGGGGGGF;AIvzz"2B777rH 7 76::g+>+> 76VG_66  M(/(=(=h(G(G%XFFFFFFM1MkZZ'.'='=h'G'G$ "+#f++2B2BKQ7QQQ !,, & & 5!1 '          
    !,, & &)B B!1 '          -3Lb 	 	'Ix%00$*N&/$,%5	 1           %         IHHOOO.9.?R  )J	7++J77:@@BBD66viXX ")"4"4(.+5)9 #5 # #        
 "[00")"4"4(.+5)9 #5 # #        
 "[00")"9"9(.+5)9 #: # #         #*"7"7(.*4)9 #8 # #       
 %   7 < !"+#f++2B2BKll"Nkgkkk- #             		 		 		8'BBBll"NH7HHQHH- #          
          		s   8A$T+ D;T+ H7 6T+ 7
I&I!T+ !I&&D9T+  $OT+ 
OT+ OAT+ $B)ST+ 
ST+ ST+  A	T+ +
V5V)U;:V;
VVVVVc                   K   t          | dd          r| j                            |j                  nd}t          |dd          }|t	          |d          sddiS 	 |                                 d{V n0# t          $ r# t                              dd	           ddicY S w xY wd
t          ffd}d |d           |d          dS )z?Read Telegram private-topic capability flags via Bot API getMe.rQ  N_botget_mecheckedFz1Failed to fetch Telegram getMe topic capabilitiesTrp   rf   c                    t          |           rt          |           S t          dd           }t          |t                    r| |v r|                    |           S t          t                    r                    |           S d S )N
api_kwargs)rr   rH   r   r6   r'   )rf   r
  mes     rC   _fieldz>GatewayRunner._get_telegram_topic_capabilities.<locals>._field(  s    r4   )r4((( \488J*d++ ,
0B0B!~~d+++"d## $vvd||#4rL   has_topics_enabledallows_users_to_create_topics)r~
  r
  r
  )
rH   rQ  r'   r!   rr   r}
  rt   ru   r;  r+   )r  r  r   botr
  r
  s        @rC    _get_telegram_topic_capabilitiesz.GatewayRunner._get_telegram_topic_capabilities'  s$     8?jRV8W8Wa$-##FO444]agvt,,;gc844;u%%	&zz||######BB 	& 	& 	&LLLW[L\\\u%%%%	&	 	 	 	 	 	 	 "(&)=">">-3V4S-T-T
 
 	
s   A9 9*B&%B&c                   K   t          | dd          r| j                            |j                  nd}||j        sdS d}t          |dd          }t          |          rR	  |t          |j                  d           d{V }n,# t          $ r t          	                    dd           Y nw xY w|sdS d}	 |
                    |j        dd	t          |          i
           d{V }t          |dd          }n,# t          $ r t          	                    dd           Y nw xY w|sdS t          |dd          }|t          |d          sdS 	 |                    t          |j                  t          |          d           d{V  dS # t          $ r  t          	                    dd           Y dS w xY w)zJCreate/pin the managed System topic after /topic activation when possible.rQ  N_create_dm_topicSystemz&Failed to create Telegram System topicTrp   z,System topic for Hermes commands and status.r  r   r  z*Failed to send Telegram System topic intror|
  pin_chat_message)r   r  disable_notificationz)Failed to pin Telegram System topic intro)rH   rQ  r'   r!   r   r   r   rt   ru   r;  r   r+   rr   r
  )r  r  r   r  create_topicr  send_resultr
  s           rC   _ensure_telegram_system_topicz+GatewayRunner._ensure_telegram_system_topic(  sq     8?jRV8W8Wa$-##FO444]a?&.?F	w(:DAAL!! 	VV".,s6>/B/BH"M"MMMMMMM		 V V VEPTUUUUUV 	F
	V '>%s9~~6 !- ! !      K
 !lDAAJJ 	V 	V 	VLLEPTLUUUUU	V 	Fgvt,,;gc+=>>;F	U&&FN++z??%) '           
  	U 	U 	ULLDtLTTTTTT	Us7   #$B &B10B1;AC? ?&D('D(=F &F>=F>c           	      *  K   t          | dd          r| j                            |j                  nd}||j        rt          |d          sdS t          t                                                    j	        dz  dz  }|
                                sdS 	 |                    |j        t          |          d|j        rdt          |j                  ind           d{V  dS # t          $ r  t                              d	d
           Y dS w xY w)zFSend the bundled BotFather Threads Settings screenshot when available.rQ  Nrs
  assetsz'telegram-botfather-threads-settings.jpgu/   BotFather → Bot Settings → Threads Settingsr  )r   ro
  rn
  r   z)Failed to send Telegram topic setup imageTrp   )rH   rQ  r'   r!   r   rr   r   r"   r#   r$   r.   rs
  r+   r  rt   ru   r;  )r  r  r   ro
  s       rC    _send_telegram_topic_setup_imagez.GatewayRunner._send_telegram_topic_setup_image:(  sP     8?jRV8W8Wa$-##FO444]a?&.?IZ8[8[?F(^^++--4x?Bkk
  "" 	F	U))z??IAGAQ[+s6+;'<'<==W[	 *             	U 	U 	ULLDtLTTTTTT	Us   AC( (&DDrn  c                     t          j        ddt          |pd                                                    }|sdS t	          |          dk    r|dd                                         dz   }|S )	zFReturn a Bot API-safe forum topic name from a generated session title.r	  r  rG   zHermes Chatx   Nu   rf  )r   r   r+   rI   r   r  )r  rn  rL
  s      rC   _sanitize_telegram_topic_titlez,GatewayRunner._sanitize_telegram_topic_titleL(  sp    &c%+2&6&677==?? 	! = w<<#dsdm**,,u4GrL   c                   K   |                      |          r|j        r|j        sdS |                     |          rdS t	          | dd          r| j                            |j                  nd}|t	          t          |          dd          }t          |          r\	  ||t          |j                  t          |j                            }n# t          $ r d}Y nw xY wt          |t                    rdS t	          | dd          }|	 |                    t          |j                  t          |j                            }|r7t          |                    d          pd          t          |          k    rdS n-# t          $ r  t                              dd	
           Y dS w xY w|dS |                     |          }		 t	          |dd          }
|
: |
t          |j                  t          |j                  |	           d{V  dS t	          |dd          }|t	          |dd          nd}||t	          |dd          nd}|dS 	  |t%          |j                  t%          |j                  |	           d{V  dS # t&          t(          f$ r"  ||j        |j        |	           d{V  Y dS w xY w# t          $ r  t                              dd	
           Y dS w xY w)zLBest-effort rename of a Telegram DM topic when Hermes auto-titles a session.NrQ  _get_dm_topic_infor  r  r  rG   z5Failed to verify Telegram topic binding before renameTrp   rename_dm_topic)r   r  rf   r|
  edit_forum_topiceditForumTopic)r   message_thread_idrf   z8Failed to rename Telegram topic for auto-generated title)r  r   r  $_telegram_topic_auto_rename_disabledrH   rQ  r'   r!   r^   r   r+   rt   r   r6   r  ru   r;  r
  r   r   r   )r  r  r  rn  r   get_infooperator_topicr  r	  
topic_namerename_topicr
  r
  s                rC   (_rename_telegram_topic_for_session_titlez6GatewayRunner._rename_telegram_topic_for_session_titleW(  s      ++F33 	6> 	QWQa 	F 44V<< 	F 9@jRV8W8Wa$-##FO444]atG}}.BDIIH!! *%-Xgs6>7J7JCPVP`LaLa%b%bNN  * * *%)NNN* nd33 FT=$77
!	$??//!&"233 @    s7;;|#<#<#BCCs:VVF   T_cddd ?F88??
	d"7,=tDDL'"l//!&"233#         
 '6400CILws,>EEE^b'KN?730@$#G#G#G`d '&&//&)&*:&;&;#           
 z*   &&"N&,&6#              	d 	d 	dLLS^bLcccccc	ds\   !1C C"!C"A2F &F/.F/AK ?K 8J /KK KK &K54K5c                    t          | dd          r:t          | j        dd          r$| j        j                            |j                  nd}|dS t          |dd          pi }|                    d          }|dS t          |t                    r|S t          |t                    r(|                                	                                dv S t          |          S )zReturn True when operator disabled per-topic auto-rename for this Telegram chat.

        Controlled via ``gateway.platforms.telegram.extra.disable_topic_auto_rename``.
        Default is False (auto-rename enabled, preserves prior behaviour).
        r  Nrg  Frx  disable_topic_auto_rename>   rd  r  r  r  )
rH   r  rg  r'   r!   r   r   r+   rI   r,   )r  r  rW  rx  rF   s        rC   r
  z2GatewayRunner._telegram_topic_auto_rename_disabled(  s     tXt,,18kSW1X1XDK!%%fo666 	
 5gt44:		566=5eT"" 	LeS!! 	G;;==&&((,FFFE{{rL   c                    |r|                      |          sdS |                     |          rdS 	 t          j                    }n!# t          $ r t          | dd          }Y nw xY w||                                rdS 	 t          j        |          }n# t          $ r |}Y nw xY wt          |                     |||          |t          d          }|dS dd}|                    |           dS )z>Schedule a topic rename from the auto-title background thread.Nrj  z.Telegram topic title rename failed to scheduleru   log_messager   c                     	 |                                   d S # t          $ r  t                              dd           Y d S w xY w)Nz"Telegram topic title rename failedTrp   )r  rt   ru   r;  )futs    rC   _log_rename_failurezPGatewayRunner._schedule_telegram_topic_title_rename.<locals>._log_rename_failure(  sX    R

 R R RADQQQQQQRs    &AAr  )r  r
  rz  r6  r*  rH   	is_closedr;  r   rt   r   r
  ru   r  )r  r  r  rn  rh   copied_sourcerm   r
  s           rC   %_schedule_telegram_topic_title_renamez3GatewayRunner._schedule_telegram_topic_title_rename(  sM     	D88@@ 	F44V<< 	F	8+--DD 	8 	8 	84$77DDD	8<4>>++<F	#'/77MM 	# 	# 	#"MMM	#)99-UZ[[H	
 
 
 >F	R 	R 	R 	R 	  !455555s#   A A$#A$ B B$#B$g     r@c                    t          | d          si | _        t          |j        pd          }|sdS ddl}|                                }| j                            |d          }||z
  | j        k     rdS || j        |<   dS )zRate-limit the BotFather Threads Settings screenshot.

        If a user sends /topic repeatedly while Threads Settings are still
        off, we shouldn't keep re-uploading the screenshot every time.
        _telegram_capability_hint_tsrG   Tr   Nr  F)rr   r
  r+   r   r   r  r'   $_TELEGRAM_CAPABILITY_HINT_COOLDOWN_Sr  s         rC   %_should_send_telegram_capability_hintz3GatewayRunner._should_send_telegram_capability_hint(  s     t;<< 	302D-fn*++ 	4oo044WcBB:AAA558)'2trL   c                     	 dS )Nuz  /topic — enable multi-session DM mode (one bot, many parallel chats)

Usage:
  /topic             Enable topic mode, or show status if already on
  /topic help        Show this message
  /topic off         Disable topic mode and clear topic bindings
  /topic <id>        Inside a topic: restore a previous session by ID

How it works:
1. Run /topic once in this DM — Hermes checks BotFather Threads
   Settings are enabled and flips on multi-session mode.
2. Tap All Messages at the top of the bot and send any message.
   Telegram creates a new topic for that message; each topic is
   an independent Hermes session (fresh history, fresh context).
3. The root DM becomes a system lobby — send /topic, /status,
   /help, /usage there. Normal prompts go in a topic.
4. /new inside a topic resets just that topic's session.
5. /topic <id> inside a topic restores an old session into it.r   r  s    rC   _telegram_topic_help_textz'GatewayRunner._telegram_topic_help_text(  s    M	
 	
rL   c                 @   | j         sddlm}  |t          d                    S t	          |j        pd          }|sdS 	 | j                             |t	          |j        pd                    }n# t          $ r d}Y nw xY w|sd	S 	 | j         	                    |
           n6# t          $ r)}t                              d           d| cY d}~S d}~ww xY wdD ]>}t          | |d          }t          |t                    r|                    |d           ?	 dS )z5Cleanly disable topic mode for a chat via /topic off.r   )format_session_db_unavailablez,gateway.shared.session_db_unavailable_prefix)r  rG   zCould not determine chat ID.r  Fz@Multi-session topic mode is not currently enabled for this chat.r1  z%Failed to disable Telegram topic modezFailed to disable topic mode: N)r  r
  u   Multi-session topic mode is now OFF for this chat.

Existing topics in Telegram aren't removed — they'll just stop being gated as independent sessions. The root DM works as a normal Hermes chat again. Run /topic to re-enable later.)r  r  r
  r   r+   r   r  r  rt   disable_telegram_topic_moderu   rk   rH   r   r6   r@  )r  r  r
  r   currently_enabledrM   attrstores           rC   %_disable_telegram_topic_mode_for_chatz3GatewayRunner._disable_telegram_topic_mode_for_chat)  s    	kBBBBBB00:h8i8ijjjjfn*++ 	211	& $ 0 O OFN0b11 !P ! !  	& 	& 	& %	&  	VUU	:888IIII 	: 	: 	:DEEE9C99999999	:
 T 	) 	)DD$--E%&& )		'4(((G	
 	
s0   0A3 3BB
B& &
C0CCCc           
      8   g d}	 | j                             t          |j                  t          |j                  d          }n.# t
          $ r! t                              dd           g }Y nw xY w|r|                    d           |D ]}t          |	                    d          pd	          }t          |	                    d
          pd          }t          |	                    d          pd	          
                                }d| d| d}|r|d| z  }|                    |           |                    d	dddd|d         	                    d           dg           n|                    g d           d                    |          S )N)z*Telegram multi-session topics are enabled.rG   zTo create a new Hermes chat, open All Messages at the top of this bot interface and send any message there. Telegram will create a new topic for it.rG   r  )r   r  limitz)Failed to list unlinked Telegram sessionsTrp   zPrevious unlinked sessions:r]   rG   rn  zUntitled sessionr   z- u    — `r  rD  zTo restore one:]1. Create or open a topic. To create a new one, open All Messages and send any message there..2. Send /topic <session-id> inside that topic.zExample: Send /topic r   z inside a topic.)z-No previous unlinked Telegram sessions found.rG   z$To restore a previous session later:r
  r
  r   )r  (list_unlinked_telegram_sessions_for_userr+   r   r  rt   ru   r;  r(   r'   rI   extendr5   )	r  r  rl	  r@  sessionr  rn  r   r  s	            rC   #_telegram_topic_root_status_messagez1GatewayRunner._telegram_topic_root_status_message8)  s   
 
 
	'PPFN++FN++ Q  HH
  	 	 	LLDtLTTTHHH	  	LL6777# # # T!2!2!8b99
GKK00F4FGGgkk)44:;;AACC6E66666 .-G---DT""""LL!o@O(=(=OOO     LL       yys   AA (A32A3raw_session_idc                 p  K   |j         }| j                            |                                          }|sd|                                 S | j                            |          }|sd|                                 S t          |                    d          pd          dk    rdS t          |                    d          pd          t          |j                  k    rdS | j                            |          }| j        	                    t          |j
                  t          |j                  	          }|r|r|                    d
          |k    rdS |                     |          }	 | j                            t          |j
                  t          |j                  t          |j                  ||d           n*# t          $ r}	dt          |	          v rY d}	~	dS  d}	~	ww xY w| j                            |          p|}
d}	 t!          | j                            |                    D ]T}|                    d          dk    r9|                    d          r$t          |                    d                    } nUn# t$          $ r d}Y nw xY wd|
 }|r|d| z  }|S )zBRestore an existing Telegram-owned Hermes session into this topic.zSession not found: r  rG   r   zNThat session is not a Telegram session and cannot be restored into this topic.r  z3That session does not belong to this Telegram user.r  r  r  z9That session is already linked to another Telegram topic.restored)r   r  r  r  r  managed_modezalready linkedNr   r   r   zSession restored: z

Last Hermes message:
)r  r  resolve_session_idrI   get_sessionr+   r'   r  #is_telegram_session_linked_to_topicr  r   r  r  r*  r   get_session_titler  get_messagesrt   )r  rK  r
  r  r  r
  linkedcurrent_bindingr  rM   rn  last_assistantrl   r  s                 rC   _restore_telegram_topic_sessionz-GatewayRunner._restore_telegram_topic_sessionf)  s     %889M9M9O9OPP
 	BA)=)=)?)?AAA"..z:: 	BA)=)=)?)?AAAw{{8$$*++z99ccw{{9%%+,,FN0C0CCCHH!EEQ[E\\*EE''&*++ F 
 
  	S" So&9&9,&G&G:&U&URR226::	00FN++f.//FN++'%' 1      	 	 	3s88++RRRRRR	
  22:>>L*	"#D$4$A$A*$M$MNN  ;;v&&+55'++i:P:P5%(Y)?)?%@%@NE 	" 	" 	"!NNN	" 0// 	FE^EEEHs2   8AG 
G6G10G11G6A<J J$#J$c           
      	  K   t          j                    }	 ddlm}m}m}m} |5  t          |                                          }ddd           n# 1 swxY w Y   |	                    d|           d{V  |	                    d|           d{V }|5  t          |                                          }	ddd           n# 1 swxY w Y   |	|z
  }
||	z
  }|	|z  }t          d          g}|rD|                    t          dd                    t          |                                         |
rD|                    t          dd                    t          |
                                         |rD|                    t          d	d                    t          |                                         |	s#|                    t          d
                     n?|                    t          dt          |          t          |	                               	 ddlm} t#          | dd          }t#          | dd          }||r|5  t%          |                                          D ]\  }}	 t)          |t*                    r|d         n|}n# t,          $ r Y 2w xY w|9 |t#          |dd          t#          |dd          d          }||_        |rd |D             nt                      |_        	 ddd           n# 1 swxY w Y   n2# t,          $ r%}t2                              d|           Y d}~nd}~ww xY wg }|
r8|                    dd                    t          |
                                |r8|                    dd                    t          |                                |r8|                    dd                    t          |                                |rt          |           dnd}|rd                    |          dz   nd}dd| | dd }	 | j                            |j                  }| j                            |j        |           n# t,          $ r Y nw xY wd!                    |          S # t,          $ r6}t2                               d"|           t          d#|$          cY d}~S d}~ww xY w)%a  Actually disconnect, reconnect, and notify MCP tool changes.

        Split out from ``_handle_reload_mcp_command`` so the confirmation
        wrapper can invoke the same path whether the user confirmed via
        button, text reply, or has the confirm gate disabled.
        r   )shutdown_mcp_serversdiscover_mcp_tools_serversr  Nzgateway.reload_mcp.headerzgateway.reload_mcp.reconnectedr  )nameszgateway.reload_mcp.addedzgateway.reload_mcp.removedz!gateway.reload_mcp.none_connectedz"gateway.reload_mcp.tools_available)r  servers)get_tool_definitionsr|  r~  r  rW
  T)r  rW
  r  c                 *    h | ]}|d          d         S )r&  rf   r   )r   r   s     rC   r  z4GatewayRunner._execute_mcp_reload.<locals>.<setcomp>)  s.     7 7 7:;*f 57 7 7rL   z8Failed to update cached agent tools after MCP reload: %szAdded servers: zRemoved servers: zReconnected servers: z MCP tool(s) now availablezNo MCP tools availablez. rG   r   z,[IMPORTANT: MCP servers have been reloaded. zD. The tool list for this conversation has been updated accordingly.]r   r   zMCP reload failed: %szgateway.reload_mcp.failed)ru  )!rz  r6  tools.mcp_toolr
  r
  r
  r  r)   rB  r	  r   r(   r5   r  r   model_toolsr
  rH   r  r   r   rS  rt   r  valid_tool_namesru   r;  rg  ry  r  r	  r  rv   )r  rK  rh   r
  r
  r
  r  old_servers	new_toolsconnected_serversaddedr  reconnectedrl	  r
  r  r  	_sess_keyr  r  new_defs_excchange_partstool_summarychange_detail
reload_msgr+  r  s                               rC   _execute_mcp_reloadz!GatewayRunner._execute_mcp_reload)  s      '))`	;````````````  3 3!(--//223 3 3 3 3 3 3 3 3 3 3 3 3 3 3
 &&t-ABBBBBBBBB #2249KLLLLLLLLI  9 9$'$8$8!9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 &3E!$55G+k9K2334E hQ?tyyQWXcQdQdGeGefffggg \Q96RW==AYAYZZZ[[[ `Q;499VT[__C]C]^^^___$ |QBCCDDDDQC3y>>cfgxcycyzzz{{{<<<<<< ~t<<%d,?FF*v*$ 5 515fllnn1E1E 5 5-Iv)6@6O6O)[U[#, ) ) ) ()%~ (';';18ASUY1Z1Z29&BUW[2\2\+/( ( (H
 ,4FL "*75 7 7?G7 7 7 7/2uu #3355 5 5 5 5 5 5 5 5 5 5 5 5 5 5"    N        L R##$Pdiiu6N6N$P$PQQQ V##$T		&//8R8R$T$TUUU ^##$\DIIf[FYFY<Z<Z$\$\]]]LUsc)nnHHHH[sL>JRDIIl33d::PRM l-  lYe  l  l  l J $ 2 H H V V"77!,j        99U### 	; 	; 	;NN2A6660:::::::::	;s   R# "AR# AR# A=R# "C:R# C

R# C
ER# '.L 'L=JL
J*'L)J**ALL LL LL R# 
M(MR# MC0R# >?Q> =R# >
RR# 
RR# #
S#-+SS#S#r#  r  r  c                <  K   d}	 |                                  }t          |t                    r|                    d          nd}t          |t                    r#t	          |                    dd                    }n# t
          $ r Y nw xY w|s              d{V S |                     |j                  dt          ffd}	| 	                    |j        j
                  }
d d| d	|
 d
|
 d|
 d}|                     ||||	           d{V S )u  Gate a destructive session slash command (/new, /reset, /undo).

        ``execute`` is an async callable ``execute() -> str | EphemeralReply``
        that performs the destructive action.  If the
        ``approvals.destructive_slash_confirm`` config gate is off, ``execute``
        runs immediately (returning its result).  Otherwise this routes
        through ``_request_slash_confirm`` — native yes/no buttons on
        Telegram/Discord/Slack, text fallback elsewhere.

        Three-option resolution:

          - ``once``  — run ``execute`` and return its result
          - ``always`` — persist ``approvals.destructive_slash_confirm: false``,
                        then run ``execute``
          - ``cancel`` — return a "cancelled" message; do not run ``execute``
        Tr;  Ndestructive_slash_confirmchoicec                 Z  K   | dk    rd dS | dk    ra	 ddl m}  |dd           t                              d	           n2# t          $ r%}t                              d
|           Y d }~nd }~ww xY w              d {V }| dk    rd}t          |t                    r||z   S |S |S )Nr  u   🟡 /z# cancelled. Conversation unchanged.rR  r   )save_config_valuez#approvals.destructive_slash_confirmFz8User opted out of destructive slash confirm (session=%s)z5Failed to persist destructive_slash_confirm=false: %su   

ℹ️ Future /clear, /new, /reset, and /undo will run without confirmation. Re-enable via `approvals.destructive_slash_confirm: true` in config.yaml.)r  r
  ru   r:  rt   rv   r   r+   )r
  r
  rM   r  noter#  r  r  s        rC   _on_confirmzCGatewayRunner._maybe_confirm_destructive_slash.<locals>._on_confirmF*  s)     !!LLLLL!!
555555%%&KUSSSKKR#    !   NNOQT        #799______F!!R 
 fc** )!D=( Ms   -A 
A4A//A4u   ⚠️ **Confirm /z**

u   

Choose:
• **Approve Once** — proceed this time only
• **Always Approve** — proceed and silence this prompt permanently
• **Cancel** — keep current conversation

_Text fallback: reply `zapprove`, `zalways`, or `z	cancel`._)rK  r#  rn  rl   handler)_read_user_configr   r6   r'   r   rt   r  r  r+   _typed_command_prefix_forr!   _request_slash_confirm)r  rK  r#  rn  r  r  confirm_requiredr  r;  r
  _pprompt_messager  s     `  `      @rC   r  z.GatewayRunner._maybe_confirm_destructive_slash*  s     4  	((**C0:30E0EO,,,4I)T** Z#'	6QSW(X(X#Y#Y  	 	 	D	   	# ??????"225<@@	c 	 	 	 	 	 	 	 	< ++EL,ABBT T TT T ')T T 68T T GIT T T 	 00" 1 
 
 
 
 
 
 
 
 	
s   A8B 
BBc                  K   ddl m} |j        }|                     |          }t	          | dd          }	|	 ddl}
|
                    d          }	|	| _        t          |	           }|	                    ||||           | j
                            |j                  }|                     ||                     |                    }d}|v	 |                    |j        |||||           d{V }|rt	          |dd          rd	}n9# t"          $ r,}t$                              d
||j        |           Y d}~nd}~ww xY w|rdS |S )aR  Ask the user to confirm an expensive slash command.

        ``handler`` is an async callable ``handler(choice: str) -> str``
        where ``choice`` is ``"once"``, ``"always"``, or ``"cancel"``.
        The handler runs on the event loop when the user responds; its
        return value is sent back as a gateway message.

        Returns a short acknowledgment string to send immediately (before
        the user's response).  If buttons rendered successfully the ack
        is ``None`` (buttons are self-explanatory); if we fell back to
        text the message itself IS the ack.
        r   rL  r  Nr   F)r   rn  rl   r  rW  r   r(  Tz*send_slash_confirm failed for %s on %s: %s)r  rM  r  r  rH   r  r   r  nextregisterrQ  r'   r!   r  r  send_slash_confirmr   rt   ru   r;  )r  rK  r#  rn  rl   r
  r	  r  r  counterr  rW  r   r   used_buttonsbutton_resultrM   s                    rC   r
  z$GatewayRunner._request_slash_confirmv*  s     * 	>=====226::
 $ 8$???**** &&q))G*1D'W'
 	##KWgNNN-##FO4433FD<X<XY^<_<_``&-&@&@"N# +)% 'A ' ' ! ! ! ! ! ! ! (W]Iu%M%M (#'L   @V_c         	4s   ;D	 	
D?"D::D?c                 z    	 ddl m}  |            }t          |t                    r|ni S # t          $ r i cY S w xY w)zRead the user's raw config.yaml (cached) for gate lookups.

        Used by slash-confirm gates that must reflect on-disk state changes
        (e.g. a prior "Always Approve" click) without a gateway restart.
        r   r9  )r  r:  r   r6   rt   )r  r:  r  s      rC   r
  zGatewayRunner._read_user_config*  sa    	555555+--C$S$//733R7 	 	 	III	s   (+ ::rG  c                     |                      t          |dd          t          |dd          t          |dd          t          |dd          |pt          |dd                    S )z@Build the metadata dict platforms need for thread-aware replies.r!   Nr   r  r  r  )r  rG  )rM  rH   )r  r  rG  s      rC   r  z)GatewayRunner._thread_metadata_for_source*  sr     //FJ--FIt,,FK..fk488 3 Zwv|UY7Z7Z 0 
 
 	
rL   rF  r  r  c                    |dS d|i}|                      |||||          r3d|d<   t          |          }|r	|dvr||d<   |t          |          |d<   |S )	zGBuild thread metadata for synthetic sends that only have routing state.Nr  r  r   T telegram_dm_topic_reply_fallback>   rG   rd  direct_messages_topic_idtelegram_reply_to_message_id)_is_telegram_dm_topic_targetr+   )	r  r!   r   r  r  rG  r   r   r  s	            rC   rM  z)GatewayRunner._thread_metadata_for_target*  s     4$/#;,, - 
 
 	T <@H78 i..C ;s)++7:34".;>?R;S;S78rL   r
  c                r   | t           j        k    s|dS |dk    rdS ||rt          t          |          dd          }t	          |          ri	  ||t          |          t          |                    }t          |t                    S # t          $ r t          
                    dd           Y nw xY wdS )z>Return True when a target is a Telegram private DM topic lane.NFr  Tr
  z,Failed to inspect Telegram DM topic metadatarp   )r  r  rH   r^   r   r+   r   r6   rt   ru   r;  )r!   r   r  r  r   get_dm_topic_info
topic_infos          rC   r  z*GatewayRunner._is_telegram_dm_topic_target*  s     x(((I,=54 7 'W7KT R R)** 88!2!27CLL#i..!Y!YJ &j$777 ! ` ` `LL!OZ^L_____` us   'B &B43B4c                      t          |           S )zBReturn the platform-specific reply anchor for GatewayRunner sends.)r  )rK  s    rC   r  z%GatewayRunner._reply_anchor_for_event+  s     'u---rL   c                    t          | dd          }|r|                                sdS 	 t          j        |                                           | _        dS # t          $ r t                              d           Y dS w xY w)z;Ensure a background task is watching for update completion._update_notification_taskNz;Skipping update notification watcher: no running event loop)	rH   rk  rz  r  _watch_update_progressr	  r*  ru   r;  )r  existing_tasks     rC   r9  z1GatewayRunner._schedule_update_notification_watch2+  s    &A4HH 	!3!3!5!5 	F	X-4-@++--. .D***  	X 	X 	XLLVWWWWWW	Xs   +A $B ?B       @      @poll_intervalstream_intervalc                    !"#$%&K   t           dz  }t           dz  }t           dz  }t           dz  }t           dz  }t          j                    %%                                |z   }	d!d#d}
d&||fD ]!}|                                r		 t          j        |                                          }|                    d          }|                    d          #|                    d	          }|                    d
          }
|                    d          }|                    d          }|rO#rMt          |          }| j
                            |          !|                     |#|||!          &|
s| d# }
 n# t          $ r Y w xY w#!r#sQt                              d           |                                s|                                r%                                |	k     r|                                r|                                  d{V rdS t          j        |           d{V  |                                s|                                r%                                |	k     |                                s|                                rC|                                s/|                    d           |                                  d{V  dS dt$          dt$          fd d}%                                $d"d3 !"#$%&fd}%                                |	k     rc|                                r|                                rU	 |                                }t'          |          |k    r"||d         z  "t'          |          }n# t(          $ r Y nw xY w |             d{V  	 |                                                                pd}t-          |          }|dk    r!                    #d&           d{V  n1!                    #d                    |          &           d{V  t                              d||
           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|||||fD ]}|                    d           t           dz                      d           | j                            |
d           dS |                                rU	 |                                }t'          |          |k    r"||d         z  "t'          |          }n# t(          $ r Y nw xY w"                                r+%                                $z
  |k    r |             d{V  |                                r|
r| j                            |
          s	 t          j        |                                          }|                    d d          }|                    d!d          }|r |             d{V  d"}t;          t=          !          d#d          V	 !                    #|||
&$           d{V  d}n2# t          $ r%}t                               d%|           Y d}~nd}~ww xY w|sE|rd&| d'nd}t;          !d(d)          }!                    #d*| | d+| d,| d-&           d{V  d| j        |
<   t                              d.|
|dd/                    n># t
          j!        t(          f$ r%}t                               d0|           Y d}~nd}~ww xY wt          j        |           d{V  %                                |	k     c|                                st                              d1|           |                    d            |             d{V  	 !                    #d2&           d{V  n# t          $ r Y nw xY w|||||fD ]}|                    d           t           dz                      d           | j                            |
d           dS dS )4a  Watch ``hermes update --gateway``, streaming output + forwarding prompts.

        Polls ``.update_output.txt`` for new content and sends chunks to the
        user periodically.  Detects ``.update_prompt.json`` (written by the
        update process when it needs user input) and forwards the prompt to
        the messenger.  The user's next message is intercepted by
        ``_handle_message`` and written to ``.update_response``.
        r  r  .update_output.txt.update_exit_coderG  Nr!   r   r  r  r  r  rF  rZ  zOUpdate watcher: cannot resolve adapter/chat_id, falling back to completion-only124r{   r   c                 .    t          j        dd|           S )Nz\x1b\[[0-9;]*[A-Za-z]rG   )r   r   r   s    rC   _strip_ansiz9GatewayRunner._watch_update_progress.<locals>._strip_ansi+  s    62B===rL   r   rG   c                    K                                    sddS                                             d
                                	sdS dfdt          dt                              D             } | D ]W}	                     d| d           d{V  &# t
          $ r%}t                              d	|           Y d}~Pd}~ww xY wdS )
z!Send buffered output to the user.rG   N  c                 *    g | ]}||z            S r   r   )r   rM  clean	max_chunks     rC   r  zOGatewayRunner._watch_update_progress.<locals>._flush_buffer.<locals>.<listcomp>+  s&    VVVeAa)mO,VVVrL   r   ```

```r   zUpdate stream send failed: %s)rI   r   r  r   r   rt   ru   r;  )chunkschunkr  r  r  r  r   bufferr   last_stream_timerh   r   s      @@rC   _flush_bufferz;GatewayRunner._watch_update_progress.<locals>._flush_buffer+  s6      <<>> K''--//EF#yy{{ IVVVVVeAs5zz96U6UVVVF E EE!,,w0D0D0D0Dx,XXXXXXXXXX  E E ELL!@!DDDDDDDDEE Es   "B**
C4CCrd  u   ✅ Hermes update finished.r   u(   ❌ Hermes update failed (exit code {}).z&Update finished (exit=%s), notified %sz$Update final notification failed: %sTr`  rF  r  r   Fsend_update_prompt)r   r  r   r  r   z%Button-based update prompt failed: %sz (default: r  typed_command_prefixr   u"   ⚕ **Update needs your input:**

	

Reply `zapprove` (yes) or `z)deny` (no), or type your answer directly.z!Forwarded update prompt to %s: %sr  z Failed to read update prompt: %sz$Update watcher timed out after %.0fsu-   ❌ Hermes update timed out after 30 minutes.r  )"rY  rz  r6  r   r.   r,  r-  r  r'   r  rQ  rM  rt   ru   rv   r8  r<  r  r+   r   r*   rI   r   r   formatr:  rb  r  r@  rH   r^   r"  r;  r  )'r  r  r  r  pending_pathclaimed_pathr
  exit_code_pathr  r>  r  r/   ri  rT  r  r  r  r!   
bytes_sentr!  r   exit_code_rawr  r  r  prompt_dataprompt_textr   sent_buttonsbtn_errdefault_hintr
  r  r   r  r   r   rh   r   s'                                   @@@@@@@rC   r
  z$GatewayRunner._watch_update_progress?+  s      $&<<#&DD"%99%(;;"%::'))99;;( !<0 	 	D{{}} "j)9)9::G#*;;z#:#:L%kk)44G 'K 8 8I")++m"<"<K 'K 8 8I!(\!:!:J# F F#+L#9#9"&-"3"3H"="=#'#C#C$#%&/0:$+ $D $ $  + F-9*E*EG*E*EKE    D16  	g 	NNlmmm  &&(( 3L,?,?,A,A 3tyy{{U]G]G]!((** T5S5S5U5U/U/U/U/U/U/U FmM222222222  &&(( 3L,?,?,A,A 3tyy{{U]G]G] ##%% 7)<)<)>)> 7H]H]H_H_ 7))%00044666666666F	>c 	>c 	> 	> 	> 	> 
99;;	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E* iikkH$$$$&& "%%'' "-"7"7"9"9w<<*44"gjkk&::F),WJ"   #moo%%%%%%%N$2$<$<$>$>$D$D$F$F$M#M #M 2 2I A~~%ll74Q\dleeeeeeeeee%ll#FMMiXX%- +         
 KK H)U`aaaa  N N NNN#I1MMMMMMMMN 'k(+7 . .AHHH---- 22::d:KKK+//TBBB !!## )3355G7||j00'*++"66%(\\
   D ||~~ &499;;1A#Ao"U"U#moo%%%%%%% ""$$ ,H ,H 7;;KHH,H*H"&*[-B-B-D-D"E"EK"-//(B"?"?K)ooi<<G" $h ,moo-------',"4==2FMMY
_&-&@&@,3+6,30;-5 'A '" '" !" !" !" !" !" !" !" 04#, _ _ _ &-TV] ^ ^ ^ ^ ^ ^ ^ ^_+ 
GN+V+C+C+C+C+CTVL!(2H#!N!NB"),, '!A#.!A0<!A !A*,!A !AAC!A !A !A *2 #/ # #        DH3K@$GVabecebeVfggg,g6 H H HLL!CQGGGGGGGGH -.........M iikkH$$R $$&& 	?NNA7KKK%%e,,,-//!!!!!!!llC% #          
    "L+$k3 * *D))))..66$6GGG'++K>>>>>!	? 	?s   C5F
FF$AN( (
N54N5	B)Q3 3
R"=RR"AU 
U&%U&B\, !"Z \, 
Z3Z.)\, .Z33A8\, ,]']""]'2` 
``c                   K   t           dz  }t           dz  }t           dz  }t           dz  }|                                s|                                sdS d}|}	 |                                r	 |                    |           n# t          $ rv |                                s_Y |rZ|                    d           |                    d           |                    d           |                    d           dS dS Y nww xY w|                                s_	 |rZ|                    d           |                    d           |                    d           |                    d           dS dS t          j        |                                          }|                    d          }|                    d	          }	|                    d
          }
|                    d          }|                    d          }|                                st          
                    d           d}|}|                    |           	 |rZ|                    d           |                    d           |                    d           |                    d           dS dS |                                                                pd}t          |          }d}|                                r|                                }t          |          }| j                            |          }|s|	rt          
                    d|           d}|}|                    |           	 |rZ|                    d           |                    d           |                    d           |                    d           dS dS |r|	r|                     ||	||
||          }t!          j        dd|                                          }|r4t%          |          dk    rd|dd         z   }|dk    rd| d}nd| d}n|dk    rd}nd}|                    |	||           d{V  t          
                    d||	|           n2# t(          $ r%}t                              d|           Y d}~nd}~ww xY w|rX|                    d           |                    d           |                    d           |                    d           n`# |rY|                    d           |                    d           |                    d           |                    d           w w xY wdS ) a  If an update finished, notify the user.

        Returns False when the update is still running so a caller can retry
        later. Returns True after a definitive send/skip decision.

        This is the legacy notification path used when the streaming watcher
        cannot resolve the adapter (e.g. after a gateway restart where the
        platform hasn't reconnected yet).
        r  r  r  r  FTr`  r!   r   r  r  r  z2Update notification deferred: update still runningrd  rG   z:Update notification deferred: %s adapter not connected yetrF  z\x1b\[[0-9;]*mr  rI  iTNr   u!   ✅ Hermes update finished.

```
r  u   ❌ Hermes update failed.

```
u(   ✅ Hermes update finished successfully.u]   ❌ Hermes update failed. Check the gateway logs or run `hermes update` manually for details.r   z0Sent post-update notification to %s:%s (exit=%s)z#Post-update notification failed: %s)rY  r.   r   r  rb  r,  r-  r  r'   ru   r:  rI   r   r  rQ  rM  r   r   r   r   rt   rv   )r  r&  r'  r
  r(  cleanupactive_pending_pathri  rT  r   r  r  r  r*  r  r  r!   r   r   r   r  s                        rC   r8  z'GatewayRunner._send_update_notification ,  s      $&<<#&DD"%99%(;;""$$ 	\-@-@-B-B 	5*Z	7""$$ $ ((6666( $ $ $'..00 $#`  7#**d*;;;##t#444""d"333%%%66666	7 7c$ $$ "((** \  7#**d*;;;##t#444""d"333%%%66666	7 7Y j!7!7!9!9::G";;z22Lkk),,GK00IK00I \22J!((** PQQQ&2#$$\222@  7#**d*;;;##t#444""d"333%%%66666	7 7} +4466<<>>E#MM**I F!!## 1$..00  --Hm''11G w  P     &2#$$\222D  7#**d*;;;##t#444""d"333%%%66666	7 7A  7 ;;'(2# <    12v>>DDFF 
z6{{T))!&!7 A~~RVRRRP6PPP!^^DCCyCll7C(lCCCCCCCCCF 	    	E 	E 	ENN@!DDDDDDDD	E  7#**d*;;;##t#444""d"333%%%666	  7#**d*;;;##t#444""d"333%%%6666	7 tsp   R /B R D#R R DR ;CR 0CR CR T" 
SR?:T" ?ST" "AU?c           
        K   t           dz  }|                                sdS 	 t          j        |                                          }|                    d          }|                    d          }|                    d          }|                    d          }|                    d          }|r|s	 |                    d	           dS t          |          }| j                            |          }	|	s4t          
                    d
|           	 |                    d	           dS | j        j                            |          }
|
;|
j        s4t                              d|           	 |                    d	           dS |                     ||||||	          }|	                    t#          |          d|           d{V }|Xt%          |dd          du rEt                              d||t%          |dd                     	 |                    d	           dS t                              d||           t#          |          t#          |          |rt#          |          ndf|                    d	           S # t(          $ r<}t                              d|           Y d}~|                    d	           dS d}~ww xY w# |                    d	           w xY w)zANotify the chat that initiated /restart that the gateway is back.rX  Nr!   r   r  r  r  Tr`  z6Restart notification skipped: %s adapter not connectedzJRestart notification suppressed: %s has gateway_restart_notification=falserF  u;   ♻ Gateway restarted successfully. Your session continues.r   r(  Fz3Restart notification to %s:%s was not delivered: %sru  rH  z"Sent restart notification to %s:%szRestart notification failed: %s)rY  r.   r,  r-  r  r'   rb  r  rQ  ru   r;  r  rg  rL  r:  rM  r   r+   rH   rv   rt   )r  notify_pathr  rT  r   r  r  r  r!   r   rW  r   r  r  s                 rC   r:  z(GatewayRunner._send_restart_notification,  st     "%;;!!## 	4@	0:k335566D88J//Lhhy))G--I--I,//J w n $/////k  --Hm''11G L    \ $/////Y  ;044X>>L'0Y'`    L $/////I 77#$. 8  H #<<GM! (        F !gfi&F&F%&O&OI FG-JKK	    $///// KK4  
 |$$c'lli4YC	NNNUYY
 $////	  	 	 	NN<a@@@444$/////		 $////sF   BJ AJ /AJ BJ +AJ 
KK2K KK K.r  r  c                ~  K   t                      }|pt                      }d}| j                                        D ]\  }}| j                            |          }|r|j        s*| j        j                            |          }|(|j        s!t          
                    d|j                   s|j        t          |j                  |j        rt          |j                  ndf}	|	|v s|	|v r	 |                     ||j        |j        |          }
|
r1|                    t          |j                  ||
           d{V }n.|                    t          |j                  |           d{V }|Kt!          |dd          du r8t                              d	|j        |j        t!          |d
d                     |                    |	           t          
                    d|j        |j                   # t&          $ r2}t                              d	|j        |j        |           Y d}~d}~ww xY w|S )a3  Notify configured home channels that the gateway is back online.

        The notification is best-effort and sent once per connected platform
        home channel. ``skip_targets`` lets startup avoid duplicate messages
        when a more specific restart notification is queued for the same chat.
        u3   ♻️ Gateway online — Hermes is back and ready.NzWHome-channel startup notification suppressed: %s has gateway_restart_notification=falser  r   r(  TFz6Home-channel startup notification failed for %s:%s: %sru  rH  z/Sent home-channel startup notification to %s:%s)r)   rQ  r   r  rN  r   rg  r'   rL  ru   r:  rF   r+   r  rM  r   rH   rv   r-   rt   )r  r  	deliveredskippedrl   r!   r   r[  rW  rY  r   r  rM   s                rC   r;  z6GatewayRunner._send_home_channel_startup_notifications,  s      :=	'#%%G!%!4!4!6!6 1	 1	Hg;//99D t| ;044X>>L'0Y'mN   nc$,&7&7PTP^9hT^9L9L9LdhiF  Fi$7$7 ;;LN#	 <    L#*<<DL0A0A7U]<#^#^^^^^^^FF#*<<DL0A0A7#K#KKKKKKKF%'&)T*J*Je*S*SNNP 1NOO	   f%%%ENL   
    LNL	        s   0CG>;G>>
H:'H55H:ri   c                    ddl m}  ||j        j        j        |j        j        |j        j        pd|j        j        rt          |j        j                  nd|j        j	        rt          |j        j	                  nd|j        j
        rt          |j        j
                  nd|j        |j        j        rt          |j        j                  nd          S )a@  Set session context variables for the current async task.

        Uses ``contextvars`` instead of ``os.environ`` so that concurrent
        gateway messages cannot overwrite each other's session state.

        Returns a list of reset tokens; pass them to ``_clear_session_env``
        in a ``finally`` block.
        r   )set_session_varsrG   )r!   r   rq  r  r  r  r  r  )gateway.session_contextr9  r  r!   rF   r   rq  r  r+   r  r  r  r  )r  ri   r9  s      rC   r  zGatewayRunner._set_session_env-  s     	=<<<<<^,2N*n.4"7>~7OWc'.2333UW3:>3IQC.///r7>~7OWc'.2333UW+9@9RZs7>4555XZ	
 	
 	
 		
rL   r  c                 (    ddl m}  ||           dS )z>Restore session context variables to their pre-handler values.r   )clear_session_varsN)r:  r<  )r  r  r<  s      rC   r
	  z GatewayRunner._clear_session_env3-  s+    >>>>>>6"""""rL   c                 ~   K   t          j                    }t                      } |j        d|j        |g|R   d{V S )zJRun blocking work in the thread pool while preserving session contextvars.N)rz  r6  r   r	  r  )r  funcr$  rh   ctxs        rC   rq
  z+GatewayRunner._run_in_executor_with_context8-  sP      '))nn)T)$EEEEEEEEEEErL   c                     	 ddl m} ddlm}m} ddlm}  |            } |            } |            } ||||          S # t          $ r&}t          	                    d|           Y d}~dS d}~ww xY w)a  Resolve the image-input routing for the currently active model.

        Returns ``"native"`` (attach pixels on the user turn) or ``"text"``
        (pre-analyze with vision_analyze and prepend the description). See
        agent/image_routing.py for the full decision table.

        The active provider/model are read from config.yaml so the decision
        tracks ``/model`` switches automatically on the next message.
        r   )decide_image_input_mode)_read_main_model_read_main_providerr9  u;   image_routing: decision failed, falling back to text — %sNr{   )
agent.image_routingrA  rl  rB  rC  r  r:  rt   ru   r;  )	r  rA  rB  rC  r:  r  r  r  rM   s	            rC   rl  z&GatewayRunner._decide_image_input_mode>-  s    	CCCCCCTTTTTTTT555555+--C**,,H$$&&E**8UC@@@ 	 	 	LLVX[\\\66666	s   >A 
A1A,,A1	user_textr}  c                 l  K   ddl m} ddlm} d}g }|D ]}	 t                              d|            |||           d{V }t          j        |          }	|	                    d          r>|	                    d	d
          }
 ||
          }
|	                    d|
 d| d           n|	                    d| d           # t          $ r>}t                              d|           |	                    d| d           Y d}~d}~ww xY w|r d                    |          }|r| d| S |S |S )a  
        Auto-analyze user-attached images with the vision tool and prepend
        the descriptions to the message text.

        Each image is analyzed with a general-purpose prompt.  The resulting
        description *and* the local cache path are injected so the model can:
          1. Immediately understand what the user sent (no extra tool call).
          2. Re-examine the image with vision_analyze if it needs more detail.

        Args:
            user_text:   The user's original caption / message text.
            image_paths: List of local file paths to cached images.

        Returns:
            The enriched message string with vision descriptions prepended.
        r   )vision_analyze_tool)sanitize_contextzDescribe everything visible in this image in thorough detail. Include any text, code, data, objects, people, layout, colors, and any other notable visual information.zAuto-analyzing user image: %s)rm
  user_promptNr(  analysisrG   z0[The user sent an image~ Here's what I can see:
zA]
[If you need a closer look, use vision_analyze with image_url: z ~]z[The user sent an image but I couldn't quite see it this time (>_<) You can try looking at it yourself with vision_analyze using image_url: rF  zVision auto-analysis error: %sz[The user sent an image but something went wrong when I tried to look at it~ You can try examining it yourself with vision_analyze using image_url: r
  )tools.vision_toolsrG  agent.memory_managerrH  ru   r;  r,  r-  r'   r(   rt   ru  r5   )r  rE  r}  rG  rH  analysis_promptenriched_partsr/   r'
  r  descriptionr  r  s                rC   rm  z)GatewayRunner._enrich_message_with_visionU-  s#     * 	;:::::9999998 	  	 	D<dCCC$7$7" /% % %       K00::i(( "(**Z"<"<K"2"2;"?"?K"))0K 0 0&*0 0 0    #))H@DH H H  
    =qAAA%%D<@D D D         	[[00F 2 11i111Ms   B.C
D4D

Dr~  c                   K   t          | j        dd          sg }|D ]n}t          j                            |          }t          |           d{V }|r|                    d| d| d           U|                    d| d           o|s|g fS d                    |          }d	}|r|                                |k    r|g fS |r	| d| g fS |g fS d
dl	m
}	 g }
g }|D ]>}	 t                              d|           t          j        |	|           d{V }|d         r7|d         }|                    |           |
                    d| d           n~|                    dd          }d|v s|                    d          r6d}|                                 r|dz  }|dz  }|
                    |           n|
                    d| d           # t$          $ r;}t                              d|           |
                    d           Y d}~8d}~ww xY w|
rDd                    |
          }d	}|r|                                |k    r||fS |r	| d| |fS ||fS ||fS )aF  
        Auto-transcribe user voice/audio messages using the configured STT provider
        and prepend the transcript to the message text.

        Args:
            user_text:   The user's original caption / message text.
            audio_paths: List of local file paths to cached audio files.

        Returns:
            A tuple of ``(enriched_text, successful_transcripts)``:
              - ``enriched_text``: the message string with transcription wrappers
                prepended (same as before).
              - ``successful_transcripts``: the raw transcript strings for audio
                clips that were successfully transcribed, in input order. Empty
                list if every clip failed or STT is disabled. Callers can use
                this to echo transcripts back to the user before the agent loop.
        stt_enabledTNz [The user sent a voice message: z (duration: z)]rF  r
  z.(The user sent a message with no text content)r   )transcribe_audiozTranscribing user voice: %sr(  r	  z8[The user sent a voice message~ Here's what they said: "z"]ru  r  rO  z8Neither VOICE_TOOLS_OPENAI_KEY nor OPENAI_API_KEY is setu   [The user sent a voice message but I can't listen to it right now — no STT provider is configured. A direct message has already been sent to the user with setup instructions.z{ You have a skill called hermes-agent-setup that can help users configure Hermes features including voice, tools, and more.zC[The user sent a voice message but I had trouble transcribing it~ (zTranscription error: %sze[The user sent a voice message but something went wrong when I tried to listen to it~ Let them know!])rH   r  r%   r/   abspathr  r(   r5   rI   tools.transcription_toolsrR  ru   r;  rz  r{  r'   rI  r  rt   ru  )r  rE  r~  notesr/   abs_pathduration_strr  _placeholderrR  rN  successful_transcriptsr  r	  ru  _no_stt_noter  s                    rC   rn  z0GatewayRunner._enrich_message_with_transcription-  s     , t{M488 	E# Q Q7??400%:8%D%DDDDDDD QLLa8aaQ]aaa    LL!OH!O!O!OPPPP % "}$[[''FKL "Y__..,>>rz! 6 11i112552:>>>>>>,. )	 )	D(:DAAA&01A4HHHHHHHH)$ !'!5J*11*==="))C4>C C C   
 #JJw@@E)U22 ++,fgg 37 %  0022 (!DL
 %+&--l;;;;&--;16; ; ;      6:::%%D         		2[[00F LL 6Y__..,>>555 J 11i113III111000s   &C3G
H$0HHc                 H  K   |                     |          }|sdS |j        pd}g }t          |dd          pg }t          |dd          pg }t          |          D ]w\  }	}
|	t	          |          k     r||	         nd}|                    d          p(t          |dd          t          j        t          j        fv }|r|	                    |
           x|r| 
                    ||           d{V \  }}|r| j                            |j                  }|j        r	d|j        ind}|r_|D ]\}	 |                    |j        d| d	|
           d{V  +# t"          $ r%}t$                              d|           Y d}~Ud}~ww xY w|pdS |s|rt)          |          }|pdS )u  Dequeue a pending queued message, auto-transcribing audio media.

        When a voice/audio message arrives during an active agent run, the
        adapter stores the event in its pending queue and signals an interrupt
        (see base.BaseAdapter.handle_message). The adapter path bypasses
        _handle_message entirely, so the normal STT pipeline at message-receive
        time never runs.

        This helper fills that gap: when the dequeued event has audio media,
        we transcribe inline, echo the raw transcript back to the user (same
        "🎙️" format as the fresh-message path), and return enriched text.
        Non-audio events fall back to _build_media_placeholder, matching the
        original _dequeue_pending_text behavior.
        NrG   rB  rC  rG  rE  r  rM  r  r   rN  )r  r{   rH   rH  r   rI  r  rj  ri  r(   rn  rQ  r'   r!   r  r   r   rt   ru   r;  rP  )r  r   r  r  rK  r{   r~  rB  rC  rM  r/   rO  is_audioenriched_textrY  echo_adapter	echo_metatxecho_excs                      rC   #_dequeue_pending_with_transcriptionz1GatewayRunner._dequeue_pending_with_transcription .  sw     ( ++K88 	4zR!#UL$77=2
e]D99?R ,, 	) 	)GAt&'#k*:*:&:&:KNNE  ** b5.$77K<M{O`;aa   )""4((( 	):>:a:ak; ; 5 5 5 5 5 51M1
 & #}00AA?E?OY[&*:;;UY	 4 
 
	"."3"3 & 1B 1 1 1)2 #4 # #        
  )   "LL H(        !(D(  	3
 	3+E22D|ts   /'E
F!FFr  c                 \   ddl m} t          |                    d          pd                                          }d}d}d}|r	 | j                                         | j        j                            |          }|rt          |dd          r|j	        S n3# t          $ r&}t                              d||           Y d}~nd}~ww xY w|                     |          }	|	|	S t          |          }
|
r|
d         }|
d	         }|
d
         }t          |                    d          p|pd                                                                          }t          |                    d	          p|pd                                                                          }t          |                    d
          p|pd                                          }|r|r|sdS 	 t!          |          }|j        t$          vrN	 ddlm} |                    |j                  st-          |          n# t          $ r t-          |          w xY wn,# t          $ r t                              d|           Y dS w xY w ||||t          |                    d          pd                                          pdt          |                    d          pd                                          pdt          |                    d          pd                                          pd          S )a  Resolve the canonical source for a synthetic background-process event.

        Prefer the persisted session-store origin for the event's session key.
        Falling back to the currently active foreground event is what causes
        cross-topic bleed, so don't do that.
        r   )r  r  rG   rE  Nz>Synthetic process-event session-store lookup failed for %s: %sr!   r  r   r   z9Synthetic process event has invalid platform metadata: %rr  r  r  )r!   r   r  r  r  r  )rt  r  r+   r'   rI   rg  rI  rJ  rH   rE  rt   ru   r;  rK  r  r,   r  rF   r  r%  r  r
  r   rv   )r  r  r  r  derived_platformderived_chat_typederived_chat_idr   rM   cached_sourcerU  rL  r  r   r!   r  s                   rC   _build_process_event_sourcez)GatewayRunner._build_process_event_sourceB.  s    	211111#''-006B77==?? 	5
"11333*377DD (WUHd;; ( <'   T        !;;KHHM($$(55G 5#*:#6 $+K$8!"))"4CGGJ//I3CIrJJPPRRXXZZ,,G0AGRHHNNPPVVXX	cggi((AOArBBHHJJ 	I 	W 	4	..H ~%===4KKKKKK,::8>JJ 8(7778  4 4 4$]3334 	 	 	NNK   44	 }#''+..4"55;;==E	**0b117799AT#''+..4"55;;==E
 
 
 	
sC   AB 
C	#CC	#I /H1 0I 1II %I87I8rs	  c                    K   |                      |          }|s1t                              d|                    dd                     dS t	          |j        d          r|j        j        nt          |j                  }d}| j        	                                D ]\  }}|j        |k    r|} n|sdS 	 t          |t          j        |dt          |                    d          pd                                          pd	          }t                              d
||j        |j                   |                    |           d{V  dS # t$          $ r&}	t                              d|	           Y d}	~	dS d}	~	ww xY w)zInject a watch-pattern notification as a synthetic message event.

        Routing must come from the queued watch event itself, not from whatever
        foreground message happened to be active when the queue was drained.
        zCDropping watch notification with no routing metadata for process %sr  r  NrF   Tr  rG   r{   rE  r  r  r  uA   Watch pattern notification — injecting for %s chat=%s thread=%sr  )rh  ru   rv   r'   rr   r!   rF   r+   rQ  r   r  r  r  rI   r:  r   r  r  rt   ru  )
r  rs	  r  r  rL  r   r  r  synth_eventr  s
             rC   r	  z(GatewayRunner._inject_watch_notification.  s      11#66 	NNUi00   F18'1R1Rl--X[\b\kXlXlM'')) 	 	DAqw-'' (  	F	F&(-sww|44:;;AACCKt  K KKS 	   ((55555555555 	F 	F 	FLLA1EEEEEEEEE	Fs   5BE 
E=E88E=r  c           
      
  K   ddl m} |d         }|d         }|                    dd          }|                    dd          }|                    dd          }|                    d	d          }|                    d
d          }	|                    dd          }
t          |                    d          pd                                          pd}|                    dd          }|                                 }t                              d||||           |dk    rZ|sX	 t          j	        |           d{V  |                    |          }||j
        rn:t                              d|           dS d}	 t          j	        |           d{V  |                    |          }|nqt          |j                  }||k    }|}|j
        rddl m} |r|                    |          sddlm} |j        r ||j                  nd}d}t          |          |k    rK|| d         }|                    d          }|dk    r||dz   d         n|}dt          |           d| }n|}d| d|j         d|j         d| d	}|                     ||||||	|
d           }|st                              d!|           nXd}| j                                        D ]\  }}||j        k    r|} n|r|j        r	 t3          |t4          j        |d|"          }t                              d#|||j        |j                   |                    |           d{V  n2# t>          $ r%}t                               d$|           Y d}~nd}~ww xY wn|d%v p|d&k    o|j        d'v} | r|j        r|j        d(d         nd}!d)| d*|j         d+|! d}"d}| j                                        D ]\  }}|j!        |k    r|} n|r\|rZ	 |rd	|ind}#|"                    ||"|#,           d{V  n2# t>          $ r%}t                               d-|           Y d}~nd}~ww xY wn|r|d.k    r|s|j        r|j        d/d         nd}!d)| d0|! d}"d}| j                                        D ]\  }}|j!        |k    r|} n|r\|rZ	 |rd	|ind}#|"                    ||"|#,           d{V  n2# t>          $ r%}t                               d-|           Y d}~nd}~ww xY wt                              d1|           dS )2u  
        Periodically check a background process and push updates to the user.

        Runs as an asyncio task. Stays silent when nothing changed.
        Auto-removes when the process exits or is killed.

        Notification mode (from ``display.background_process_notifications``):
          - ``all``    — running-output updates + final message
          - ``result`` — final completion message only
          - ``error``  — final message only when exit code != 0
          - ``off``    — no messages at all
        r   r1  r  check_intervalr  rG   r!   r   r  r  r  r  Nnotify_on_completeFzCProcess watcher started: %s (every %ss, notify=%s, agent_notify=%s)r  Tz"Process watcher ended (silent): %s)
strip_ansir	  r   r  r   u'   [… output truncated — showing last z chars]
r  z completed (exit code z).
Command: z	
Output:
rF  )r  r  r!   r   r  r  r  zHDropping completion notification with no routing metadata for process %srj  uU   Process %s finished — injecting agent notification for session %s chat=%s thread=%sz Agent notify injection error: %s>   r  r  ru  >   Nr   iz[Background process z finished with exit code z~ Here's the final output:
r   zWatcher delivery error: %sr  iz is still running~ New output:
zProcess watcher ended: %s)#rf  r2  r'   r+   rI   r  ru   r;  rz  r<  exitedr   output_bufferis_completion_consumedtools.ansi_stripro  r  r  r#  rh  rv   rQ  r   r!   r   r  r  r  r:  r  r  rt   ru  rF   r   )$r  r  r2  r  ra  r  rL  r   r  r  r  r  agent_notifynotify_moder
  last_output_lencurrent_output_lenhas_new_output	_pr_checkro  r1  _LIMIT_tail_nlr  rs	  r  r   r  r  rk  r  r&	  
new_outputrR  	send_metas$                                       rC   r=  z"GatewayRunner._run_process_watcher.  s      	<;;;;;\*
+,kk-44J33++i,,KKR00	++i,,KKR00	\228b99??AAIT
{{#7??>>@@Z (K	G 	G 	G %mH---------*..z::?gn?	
 LL=zJJJFy	F-)))))))))&**:66G!$W%:!;!;//AN0O~ nF QPPPPP =	(H(H(T(T =;;;;;;@G@U]::g&;<<<[]D
 "F4yy6)) $fWXX#jj..36"99cAghh%eUee^cee#,* , ,&-&7, ,$+O, , %), , ,  "==&0'2$1#*%.#*%.? ?  F " f&   "G $ 3 3 5 5 " "1//&'G!E 0  P6> PP*6%/-8-='-)-+5+ + +K #KK w * + & & 0   #*"8"8"E"EEEEEEEEE( P P P"LL)KQOOOOOOOOP
  #44 W#w.U73DI3U  ! JBIBW!_!6uvv!>!>]_JCz C CT[Te C C5?C C C ! #G $ 3 3 5 5 " "17m33&'G!E 4  J7 JJDM(Wi(@(@SWI"),,wy,"Y"YYYYYYYYY( J J J"LL)EqIIIIIIIIJ FK5$8$8$8 >E=RZW245599XZ
2: 2 2$.2 2 2   M//11  DAqw-//"# 0  Fw FF@I$S[)$<$<t	%ll7L9lUUUUUUUUUU$ F F F%A1EEEEEEEEFqy	Fv 	0*=====sI   A!M( (
N2NN&P6 6
Q% Q  Q%	&S0 0
T:TT))r  re  )r  r   )r  r  )r  	threshold)r  target_ratio)r  protect_last_n)rx  rW
  )r  r  _CACHE_BUSTING_CONFIG_KEYSzhoncho.peer_namezhoncho.ai_peerzhoncho.pin_peer_namezhoncho.runtime_peer_prefixzhoncho.user_peer_aliases_HONCHO_CACHE_BUSTING_MEMOc                 $    d | j         D             S )Nc                     i | ]}|d S r  r   )r   r  s     rC   r   zDGatewayRunner._empty_honcho_cache_busting_config.<locals>.<dictcomp>w/  s    DDDcTDDDrL   )_HONCHO_CACHE_BUSTING_KEYS)clss    rC   "_empty_honcho_cache_busting_configz0GatewayRunner._empty_honcho_cache_busting_configu/  s    DDS%CDDDDrL   c                 r   	 ddl m}m}  |            }	 |                                j        }n# t
          $ r d}Y nw xY wt          |          |f}| j                            |          }|t          |          S |
                    |          }|j        pi }|j        |j        t          |j                  |j        pdt#          |t                    r!t%          |                                          ng d}	||	i| _        t          |	          S # t(          $ r |                                 cY S w xY w)z<Extract Honcho identity keys, memoized by honcho.json mtime.r   )HonchoClientConfigresolve_config_pathN)r  rG   r  )plugins.memory.honcho.clientr  r  statst_mtime_nsr*   r+   r  r'   r6   from_global_configuser_peer_aliases	peer_nameai_peerr   pin_peer_nameruntime_peer_prefixr   r  r   rt   r  )
r  r  r  r/   mtime_nsmemo_keycachedhcfgaliasesrb  s
             rC   $_extract_honcho_cache_busting_configz2GatewayRunner._extract_honcho_cache_busting_configy/  sj   	<\\\\\\\\&&((D 99;;2       D		8,H377AAF!F||#%88T8JJD,2G$(N"&,(,T-?(@(@.2.F.L"GQRY[_G`G`,hF7==??,C,C,Cfh F /7-?C*<< 	< 	< 	<99;;;;;	<s3   D / D >D >>D =BD D65D6c                 v   i }t          |t                    r|ni }| j        D ]W\  }}|                    |          }t          |t                    r|                    |          || d| <   Md|| d| <   X	 ddlm} t          |dd          |d<   n# t          $ r d|d<   Y nw xY wt          |dd          }t          |t                    r@|
                                d	k    r(|                    |                                            n'|                    |                                            |S )
al  Pull values that must bust the cached agent.

        Returns a flat dict keyed by 'section.key'.  Missing config keys and
        non-dict sections yield None values, which still contribute to the
        signature (so 'absent' vs 'present-and-null' differ).

        The live tool registry generation is included too.  MCP reloads and
        dynamic MCP tool-list changes mutate the registry without necessarily
        changing config.yaml.  Cached AIAgent instances freeze their tool
        schemas at construction time, so a registry generation change must
        rebuild the agent before the next turn.
        r  Nr   )registry_generationztools.registry_generationr  r  honcho)r   r6   r  r'   tools.registryr  rH   rt   r   r+   r,   r  r  r  )	r  r>  outr  sectionr  section_valr  r  s	            rC   _extract_cache_busting_configz+GatewayRunner._extract_cache_busting_config/  sx    !'T::Bkk: 	/ 	/LGS'''**K+t,, /*5//#*>*>w&&&&''*.w&&&&''	4///////6xPT/U/UC+,, 	4 	4 	4/3C+,,,	4
 3*55h$$ 	A)9)9X)E)EJJs??AABBBBJJs==??@@@
s   <B B)(B)r+  r  ephemeral_prompt
cache_keysrf
  c                    ddl }ddl}t          |                    dd          pd          }	|	r9|                    |	                                                                          nd}
t          |pi                                           }|	                    | |
|                    dd          |                    dd          |                    dd          |rt          |          ng |pd|t          |pd          t          |pd          g
dt          	          }|                    |                                                                          dd
         S )u:  Compute a stable string key from agent config values.

        When this signature changes between messages, the cached AIAgent is
        discarded and rebuilt.  When it stays the same, the cached agent is
        reused — preserving the frozen system prompt and tool schemas for
        prompt cache hits.

        ``cache_keys`` is an optional flat dict of additional config values
        that should invalidate the cache when they change.  Callers pass
        the output of ``_extract_cache_busting_config(user_config)`` so
        edits to model.context_length / compression.* in config.yaml are
        picked up on the next gateway message without a manual restart.

        ``user_id`` and ``user_id_alt`` are the runtime user identities
        carried by the current message's gateway source.  They participate
        in the cache key because the Honcho memory provider freezes them
        into ``HonchoSessionManager`` at first-message init (see
        ``plugins/memory/honcho/__init__.py::_do_session_init``).  Without
        them in the signature, a shared-thread session_key (one in which
        ``build_session_key`` intentionally omits the participant ID,
        e.g. ``thread_sessions_per_user=False``) would reuse the cached
        AIAgent across distinct users, causing the second user's messages
        to be attributed to the first user's resolved Honcho peer.  This
        broke #27371's per-user-peer contract in multi-user gateways.
        Per-user agent rebuilds in shared threads trade prompt-cache
        warmth for correct memory attribution.
        r   Nr  rG   r  r  r"  T)	sort_keysr   r	  )
hashlibr,  r+   r'   sha256encode	hexdigestr  r   r  )r  r+  r  r  r  r  rf
  r  _j_api_key_api_key_fingerprint_cache_keys_sortedblobs                rC   _agent_config_signaturez%GatewayRunner._agent_config_signature/  sb   J 	#""""""" w{{9b117R88PX`w~~hoo.?.?@@JJLLL^`#Z%52$<$<$>$>??xx$J++J++J++,<D'(((" !&B"GMr""K%2&& !  
 
$ ~~dkkmm,,6688"==rL   c                     | j                             |          }|s||fS |                    d|          }dD ]}|                    |          }||||<   ||fS )a  Apply /model session overrides if present, returning (model, runtime_kwargs).

        The gateway /model command stores per-session overrides in
        ``_session_model_overrides``.  These must take precedence over
        config.yaml defaults so the switched model is actually used for
        subsequent messages.  Fields with ``None`` values are skipped so
        partial overrides don't clobber valid config defaults.
        r  )r  r  r  r"  r.  r'   )r  r  r  rI  rF  r  vals          rC   rC  z+GatewayRunner._apply_session_model_override0  s{     044[AA 	).((We,,B 	* 	*C,,s##C&)s#n$$rL   agent_modelc                 p    | j                             |          }|duo|                    d          |k    S )zGReturn True if *agent_model* matches an active /model session override.Nr  r  )r  r  r  rF  s       rC   _is_intentional_model_switchz*GatewayRunner._is_intentional_model_switch0  s9    044[AAt#LW(=(=(LLrL   r  c                   |sdS ||                      ||          sdS t          | di                               |d          }|B	 |                                 n,# t          $ r t
                              dd           Y nw xY w| j                            |d           | j                            |d           t          | d          r| j
                            |d           dS )u  Pop ALL per-running-agent state entries for ``session_key``.

        Replaces ad-hoc ``del self._running_agents[key]`` calls scattered
        across the gateway.  Those sites had drifted: some popped only
        ``_running_agents``; some also ``_running_agents_ts``; only one
        path also cleared ``_busy_ack_ts``.  Each missed entry was a
        small, persistent leak — a (str_key → float) tuple per session
        per gateway lifetime.

        Use this at every site that ends a running turn, regardless of
        cause (normal completion, /stop, /reset, /resume, sentinel
        cleanup, stale-eviction).  Per-session state that PERSISTS
        across turns (``_session_model_overrides``, ``_voice_mode``,
        ``_pending_approvals``, ``_update_prompt_pending``) is NOT
        touched here — those have their own lifecycles.

        When ``run_generation`` is provided, only clear the slot if that
        generation is still current for the session.  This prevents an
        older async run whose generation was bumped by /stop or /new from
        clobbering a newer run's state during its own unwind.  Returns
        True when the slot was cleared, False when an ownership guard
        blocked it.
        FNrr  z%Failed to release active session slotTrp   rw  )r		  rH   r@  releasert   ru   r;  rq  r!  rr   rw  )r  r  r  leases       rC   r|  z*GatewayRunner._release_running_agent_state0  s   :  	5%d.J.J/
 /
% 56;;??TRRU U U UDtTTTTTU  d333##K6664(( 	5!!+t444ts   A &BBc                    |sdS t          | dd          }t          |t                    r|                    |d           t          | dd          }t          |t                    r|                    |d           t          | dd          }t          |t                    r|                    |d           	 ddlm} n# t          $ r d}Y nw xY w|J	 |                    |           n3# t          $ r&}t          	                    d||           Y d}~nd}~ww xY w	 ddl
m} n# t          $ r Y dS w xY w	  ||           dS # t          $ r'}t          	                    d	||           Y d}~dS d}~ww xY w)
zHClear per-session control state that must not survive a boundary switch.N_pending_skills_reload_notesr  r  r   rL  z?Failed to clear slash-confirm state for session boundary %s: %sclear_sessionz:Failed to clear approval state for session boundary %s: %s)rH   r   r6   r@  r  rM  rt   r  ru   r;  r  r  )r  r  pending_skills_reload_notespending_approvalsupdate_prompt_pendingr	  r  _clear_approval_sessions           rC   &_clear_session_boundary_security_statez4GatewayRunner._clear_session_boundary_security_stateK0  s8    	F&-0$'
 '
# 1488 	?'++K>>>#D*>EE'.. 	5!!+t444 '.F M M+T22 	9!%%k4888	&AAAAAAA 	& 	& 	&!%	&)"((5555   U       	OOOOOOO 	 	 	FF		##K00000 	 	 	LLL        	sT   :C CCC, ,
D6DD D' '
D54D59E 
E7E22E7c                     |sdS | j                             d          }|	i }|| _        t          |                    |d                    dz   }|||<   |S )ae  Claim a fresh run generation token for ``session_key``.

        Every top-level gateway turn gets a monotonically increasing token.
        If a later command like /stop or /new invalidates that token while the
        old worker is still unwinding, the late result can be recognized and
        dropped instead of bleeding into the fresh session.
        r   rx  Nr   )__dict__r'   rx  r   )r  r  generationsnext_generations       rC   r  z+GatewayRunner._begin_session_run_generationz0  sj      	1m''(ABBK+6D(kook1==>>B#2K rL   c                n    |                      |          }|rt                              d|||           |S )z7Invalidate any in-flight run token for ``session_key``.u-   Invalidated run generation for %s → %d (%s))r  ru   r:  )r  r  r-  r  s       rC   r  z0GatewayRunner._invalidate_session_run_generation0  sG    77DD
 	KK?	   rL   r  c                     |sdS | j                             d          pi }t          |                    |d                    t          |          k    S )zEReturn True when ``generation`` is still current for ``session_key``.Trx  r   )r  r'   r   )r  r  r  r  s       rC   r		  z%GatewayRunner._is_session_run_current0  sQ     	4m''(ABBHb;??;2233s:FFrL   c                     |r|r|dS 	 t          |di                               |          }| t          |dt          |                     dS dS # t          $ r Y dS w xY w)zDBind a gateway run generation to the adapter's active-session event.Nr	  r	  )rH   r'   setattrr   rt   )r  r   r  r  interrupt_events        rC   r	  z*GatewayRunner._bind_adapter_run_generation0  s      	k 	Z-?F	%g/A2FFJJ;WWO*)A3z??SSSSS +* 	 	 	DD	s   AA 
A A T)release_running_stater`  ra  r  c                  K   |sdS | j                             |          }|r|t          ur|                    |           |                     ||           | j                            |j                  }|r1t          |d          r!|                    ||j	                   d{V  |r%t          |d          r|
                    |           | j                            |d           |r|                     |           dS dS )zFInterrupt the current run and clear queued session state consistently.Nr  interrupt_session_activityr  )rq  r'   r  r"  r  rQ  r!   rr   r  r   r  rs  r@  r|  )r  r  r  r`  ra  r  r  r   s           rC   r  z*GatewayRunner._interrupt_and_clear_session0  s=       	F,00== 	6]2III##$4555//DW/XXX-##FO44 	Rww(DEE 	R44[&.QQQQQQQQQ 	5ww(=>> 	5''444"";555  	;--k:::::	; 	;rL   c           	         t          | dd          }d}|r6|5  | j                            |d          }ddd           n# 1 swxY w Y   n)t          | dd          }||                    |d          }t          |t                    r
|r|d         n|}|	|t
          u rdS d t          | di                                           D             }t          |          |v rdS 	 t          j	        | j
        |fddt          |          dd	          
                                           dS # t          $ r, 	 | 
                    |           Y dS # t          $ r Y Y dS w xY ww xY w)u  Remove a cached agent for a session (called on /new, /model, etc).

        Pops the entry AND soft-releases the evicted agent's LLM client
        pool so the httpx connection (sockets + held buffers) is freed
        promptly rather than waiting on CPython GC — AIAgent holds
        reference cycles (callbacks, tool state) that delay refcount
        collection, so a manual release is required to keep gateway RSS
        flat across many /new, /model, undo and reset operations (#29298,
        same leak class as #25315).

        The release is soft (``release_clients()``): it frees the client
        pool and per-turn child subagents but PRESERVES the session's
        terminal sandbox, browser daemon, and tracked bg processes (keyed
        on task_id), because the session may resume with a freshly-built
        agent.  Call sites that want a hard teardown (true conversation
        boundaries like /new) already call ``_cleanup_agent_resources``
        before evicting; ``release_clients`` is idempotent and safe to
        run again after that (the client is already None).

        Cleanup runs on a daemon thread so we never block holding
        ``_agent_cache_lock`` on slow socket teardown — mirrors the
        cap-enforcer and idle-sweeper paths.
        r~  Nr|  r   c                 B    h | ]}||t           ut          |          S r  r  r]   r   r  s     rC   r  z4GatewayRunner._evict_cached_agent.<locals>.<setcomp>0  8     
 
 
}*A!A!A qEE!A!A!ArL   rq  Tzagent-evict-rE  rY  r$  daemonrf   )rH   r|  r@  r   rS  r  rb  r]   r{  Thread_release_evicted_agent_softr+   r_  rt   )r  r  r  evictedr  rx  running_idss          rC   r{  z!GatewayRunner._evict_cached_agent0  s   0 1488 	8 C C+//TBBC C C C C C C C C C C C C C C T>488F! **[$77(%88QWQ

'=E%<<<F
 
T#4b99@@BB
 
 

 e99##F	7X;C$4$4SbS$9;;	  
 eggggg 	 	 	00777777   	s<   A  AA$AD. .
E$9E
E E$E  E$interrupt_depthc                     |dk    r6t          j                     | _        d| _        t          | d          rd| _        d| _        dS )u  Reset per-turn state on a cached agent before a new turn starts.

        Both _last_activity_ts and _last_activity_desc are only reset for
        fresh external turns (depth 0); they are semantically paired —
        desc describes the activity *at* ts, so updating one without the
        other would make get_activity_summary() misleading.
        For interrupt-recursive turns both are preserved so the inactivity
        watchdog can accumulate stuck-turn idle time and fire the 30-min
        timeout (#15654).  The depth-0 reset is still needed: a session
        idle for 29 min would otherwise trip the watchdog before the new
        turn makes its first API call (#9051).
        r   zstarting new turn (cached)_last_flushed_db_idxN)r   _last_activity_ts_last_activity_descrr   r  _api_call_count)rx  r  s     rC   _init_cached_agent_for_turnz)GatewayRunner._init_cached_agent_for_turn1  sP     a&*ikkE#(DE% u455 /-.* !rL   c                     |dS 	 t          |d          r|                                 n|                     |           n# t          $ r Y nw xY wt          |d          r	g |_        dS dS )u  Soft cleanup for cache-evicted agents — preserves session tool state.

        Called from _enforce_agent_cache_cap and _sweep_idle_cached_agents.
        Distinct from _cleanup_agent_resources (full teardown) because a
        cache-evicted session may resume at any time — its terminal
        sandbox, browser daemon, and tracked bg processes must outlive
        the Python AIAgent instance so the next agent built for the
        same task_id inherits them.
        Nrelease_clientsri  )rr   r  rd  rt   ri  )r  rx  s     rC   r  z)GatewayRunner._release_evicted_agent_soft1  s     =F	u/00 5%%'''' --e444 	 	 	D	 5-.. 	)&(E###	) 	)s   :A 
AAc           
         t          | dd          }|dS t          |d          sdS d t          | di                                           D             }t          dt	          |          t
          z
            }g }|dk    rt          |                                          }|d|         D ]c}|                    |          }t          |t                    r
|r|d         nd}|t          |          |v rL|                    ||f           d|D ]\  }}	|                    |d           t	          |          t
          z
  }
|
dk    r/t                              dt	          |          t
          |
           |D ]k\  }}t                              d|t	          |                     |;t#          j        | j        |fd	d
|dd                                                     ldS )u  Evict oldest cached agents when cache exceeds _AGENT_CACHE_MAX_SIZE.

        Must be called with _agent_cache_lock held.  Resource cleanup
        (memory provider shutdown, tool resource close) is scheduled
        on a daemon thread so the caller doesn't block on slow teardown
        while holding the cache lock.

        Agents currently in _running_agents are SKIPPED — their clients,
        terminal sandboxes, background processes, and child subagents
        are all in active use by the running turn.  Evicting them would
        tear down those resources mid-turn and crash the request.  If
        every candidate in the LRU order is active, we simply leave the
        cache over the cap; it will be re-checked on the next insert.
        r|  Nr  c                 B    h | ]}||t           ut          |          S r  r  r  s     rC   r  z9GatewayRunner._enforce_agent_cache_cap.<locals>.<setcomp>V1  r  rL   rq  r   uk   Agent cache over cap (%d > %d); %d excess slot(s) held by mid-turn agents — will re-check on next insert.z;Agent cache at cap; evicting LRU session=%s (cache_size=%d)Tzagent-cache-evict-rE  r  )rH   rr   rb  r  r   _AGENT_CACHE_MAX_SIZEr  rB  r'   r   rS  r]   r(   r@  ru   rv   r:  r{  r  r  r_  )r  r  r  excess
evict_planordered_keysr  r   rx  r  remaining_over_caps              rC   _enforce_agent_cache_capz&GatewayRunner._enforce_agent_cache_cap<1  s,    ~t44>F v}-- 	F

 
T#4b99@@BB
 
 
 QF&;;<<"$
A::..L#GVG, 0 0

3$.ue$<$<PPaD$Ek)A)A!!3,////  	" 	"FCJJsD!!!! [[+@@!!NNDF24F   % 	 	JCKKMS[[     ;8c#2#h88	  
 %'''	 	rL   c                 t   t          | dd          }t          | dd          }||dS t          j                    }g }d t          | di                                           D             }|5  t          |                                          D ]t\  }}t          |t                    r
|r|d         nd}|)t          |          |v r;t          |dd          }	|	O||	z
  t          k    r|	                    ||f           u|D ]\  }}
|
                    |d           	 ddd           n# 1 swxY w Y   |D ]n\  }}t                              d||t          |d|          z
             t          j        | j        |fd	d
|dd                                                     ot#          |          S )u  Evict cached agents whose AIAgent has been idle > _AGENT_CACHE_IDLE_TTL_SECS.

        Safe to call from the session expiry watcher without holding the
        cache lock — acquires it internally.  Returns the number of entries
        evicted.  Resource cleanup is scheduled on daemon threads.

        Agents currently in _running_agents are SKIPPED for the same reason
        as _enforce_agent_cache_cap: tearing down an active turn's clients
        mid-flight would crash the request.
        r|  Nr~  r   c                 B    h | ]}||t           ut          |          S r  r  r  s     rC   r  z:GatewayRunner._sweep_idle_cached_agents.<locals>.<setcomp>1  r  rL   rq  r  z3Agent cache idle-TTL evict: session=%s (idle=%.0fs)Tzagent-cache-idle-rE  r  )rH   r   rb  r  r   r   rS  r]   _AGENT_CACHE_IDLE_TTL_SECSr(   r@  ru   r:  r{  r  r  r_  r   )r  r  r  r   to_evictr  r  r   rx  last_activityr  s              rC   r  z'GatewayRunner._sweep_idle_cached_agents1  sC    ~t441488>U]1ikk "
 
T#4b99@@BB
 
 

  	& 	&"6<<>>22 
2 
2
U$.ue$<$<PPaD=e99++ '/BD I I (-'+EEEOOS%L111" & &Q

3%%%%&	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& # 
	 
	JCKKES75*=sCCC   7X3SbS33	  
 egggg8}}s   -B5D//D36D3c                 F   t          j        dd                                          }|r|                    d          S t	                      }|                    d          pi                     dd                                          }|r|                    d          S dS )zReturn the proxy URL if proxy mode is configured, else None.

        Checks GATEWAY_PROXY_URL env var first (convenient for Docker),
        then ``gateway.proxy_url`` in config.yaml.
        GATEWAY_PROXY_URLrG   r   r  	proxy_urlN)r%   r9  rI   r  r  r'   )r  rN  r  s      rC   _get_proxy_urlzGatewayRunner._get_proxy_url1  s     i+R006688 	#::c??""$$wwy!!'R,,["==CCEE 	#::c??"trL   r  c	                    K   	 ddl m}	m}
 n# t          $ r
 dg dg dcY S w xY w                                 }|sdg dg dS t          j        dd                                          }dt          f fd	}g }|r|	                    d
|d           |D ]J}|
                    d          }|
                    d          }|dv r|r|	                    ||d           K|	                    d|d           ddi}|rd| |d<   |r||d<   d|dd}d}t          t           dd          dd          }|ddlm}  |            }t          |j                  }t!                      }ddlm}  |||d          }||j        o
|j        dk    nt          |          }                     ||          }|r	 ddlm}m}  j        
                    |j                  }|rt          |dd          } | r|j        nd}!d }"|j        t6          j        k    rd}!d}"|j        t6          j        k    r t=          t          |d!d"          pd"          nd"}# ||j        |j         |!|"|#|j        pd#t          |d$d          pd%          }$ |||j!        |$||&          }n2# tD          $ r%}%tF          $                    d'|%           Y d}%~%nd}%~%ww xY wd}&|r&tK          j&        |'                                          }& j        
                    |j                  }|r4	 |(                    |j!        |(           d{V  n# tD          $ r Y nw xY wd}'tS          j)                    }(	  |
dd)*          }) |	|)+          4 d{V }*|**                    | d,||-          4 d{V 	 }+|+j+        d.k    r|+,                                 d{V },tF          -                    d/|+j+        ||,dd0                    d1|+j+         d2|,dd3          g dg dcddd          d{V  cddd          d{V  |r|.                                 |&rS	 tK          j/        |&d4+           d{V  S # tJ          j0        tJ          j1        f$ r |&2                                 Y S w xY wS d}-|+j3        4                                2 3 d{V }. |            stF          5                    d5pd6pd           dg dg tm          |          |d d7c cddd          d{V  cddd          d{V  |r|.                                 |&rS	 tK          j/        |&d4+           d{V  S # tJ          j0        tJ          j1        f$ r |&2                                 Y S w xY wS |.7                    d8d9:          }/|-|/z  }-d;|-v r|-8                    d;d<          \  }0}-|0                                }0|0s4|09                    d=          r|0d>d         }1|1                                d?k    rn	 tu          j;        |1          }2|2
                    d@g           }3|3rP|3d         
                    dAi           }4|4
                    dd          }|r|'|z  }'|r|<                    |           n# tt          j=        $ r Y nw xY wd;|-v 6 	 ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  n# 1 d{V swxY w Y   n# tJ          j1        $ r  tD          $ r}5tF          >                    dB||5           |'szdC|5 g dg dcY d}5~5|r|.                                 |&rS	 tK          j/        |&d4+           d{V  S # tJ          j0        tJ          j1        f$ r |&2                                 Y S w xY wS Y d}5~5nd}5~5ww xY w|r|.                                 |&rS	 tK          j/        |&d4+           d{V  n# tJ          j0        tJ          j1        f$ r |&2                                 Y nuw xY wnp# |r|.                                 |&rS	 tK          j/        |&d4+           d{V  w # tJ          j0        tJ          j1        f$ r |&2                                 Y w w xY ww xY wtS          j)                    |(z
  }6 |            s7tF          5                    dDpd6pd           dg dg tm          |          |d d7S tF          5                    dE||pdddF         |6tm          |'                     |'pdGd|ddH|'dgd<g tm          |          ||duot          |'          d7S )IaT  Forward the message to a remote Hermes API server instead of
        running a local AIAgent.

        When ``GATEWAY_PROXY_URL`` (or ``gateway.proxy_url`` in config.yaml)
        is set, the gateway becomes a thin relay: it handles platform I/O
        (encryption, threading, media) and delegates all agent work to the
        remote server via ``POST /v1/chat/completions`` with SSE streaming.

        This lets a Docker container handle Matrix E2EE while the actual
        agent runs on the host with full access to local files, memory,
        skills, and a unified session store.
        r   )ClientSessionClientTimeoutuE   ⚠️ Proxy mode requires aiohttp. Install with: pip install aiohttpr  r!  r  r  uH   ⚠️ Proxy URL not configured (GATEWAY_PROXY_URL or gateway.proxy_url)GATEWAY_PROXY_KEYrG   r   c                  <     sdS                                 S rm  r		  r  r  r  s   rC   _run_still_currentz>GatewayRunner._run_agent_via_proxy.<locals>._run_still_current1  )    %[%t//^LLLrL   r   r   r   r   >   r   r   r   zContent-Typezapplication/jsonzBearer AuthorizationzX-Hermes-Session-Idzhermes-agentT)r  r!  streamNr  	streamingStreamingConfigr  r  GatewayStreamConsumerStreamConsumerConfigSUPPORTS_MESSAGE_EDITINGFfresh_final_after_secondsr  editr  edit_intervalbuffer_thresholdcursorbuffer_onlyr
  	transportr  )r   r   r  r   initial_reply_to_idz+Proxy: could not set up stream consumer: %sr   rX  )r^  	sock_readrx  z/v1/chat/completions)r,  headersr
  zProxy error (%d) from %s: %srN  u   ⚠️ Proxy error (z): r  r   uK   Discarding stale proxy stream for %s — generation %d is no longer currentr  )r  r!  r  r  r"  r  response_previewedrs  r   )r  r   r   zdata:    z[DONE]choicesdeltaz Proxy connection error to %s: %su   ⚠️ Proxy connection error: uK   Discarding stale proxy result for %s — generation %d is no longer currentz>proxy response: url=%s session=%s time=%.1fs response=%d charsrH  z(No response from remote agent)r   )?aiohttpr  r  rG  r  r%   r9  rI   r   r(   r'   rH   rs  r  r  r!   r  r  r  r  r  r  gateway.stream_consumerr  r  rQ  r  r  r$  r  r   r  r  r   rt   ru   r;  rz  r  r  send_typingr   postr]  r{   rv   finishr  r  rh  r  r   iter_anyr:  r   r  r  rI  r,  r-  on_deltar  ru  )7r  rl   r  r   r  r  r  r  r  _AioClientSessionr  r  	proxy_keyr  api_messagesr   r   r   r  r   _stream_consumer_scfgr  r  r>  r  _plat_streaming_streaming_enabledrt
  r  r  r  _adapter_supports_edit_effective_cursor_buffer_only_fresh_final_secs_consumer_cfg_sc_errstream_taskfull_response_start_timeoutr
  resp
error_textr  r  r{   r  r  objr  r  r  _elapseds7   `     ``                                               rC   _run_agent_via_proxyz"GatewayRunner._run_agent_via_proxy1  s     .	QQQQQQQQQ 	 	 	"i	    	 ''))	 	"l	   I1266<<>>		MD 	M 	M 	M 	M 	M 	M 	M 	M  .0 	On M MNNN 	H 	HC776??Dggi((G,,,,##Tg$F$FGGGV@@AAA $23E"F 	='<'<'<GO$ 	8-7G)* $$
 
  h55{DII=666666#O%%E+FO<<*,,BBBBBB11{
 

 & M6eo6o&& 	 6:5U5UV\^n5o5o %	U$U________=,,V_== -4X?Y[_-`-`*8N(VTV%#(L(/99,.)'+ "?h.??? ge-H#NNURUVVV  &
 %9$8&+&9).)?0$02C"'/";V")&+r"B"B"Hb% % %M (='< ( &,!1,<( ( ($  U U UJGTTTTTTTTU  	F!-.>.B.B.D.DEEK =$$V_55 	**6>DT*UUUUUUUUUU    U	)$}1===H((::: =) =) =) =) =) =) =)g"<< 666# (   <) <) <) <) <) <) <) <) {c))+/99;;%6%6%6%6%6%6
: KJtt4D  
 /hT[.g.gU_`dad`dUe.g.g(*)*%'	   <) <) <) <) <) <) <) <) <) <) <) <) <)=) =) =) =) =) =) =) =) =) =) =) =) =)Z   * ''))) ))!*;DDDDDDDDDDD,g.DE ) ) )&&((((())w  F'+|'<'<'>'> () () () () () () ()e1133 "KK m + 2s . 3!   35,.-.)+25g,,.86;$ $  7<) <) <) <) <) <) <) <) <) <) <) <) <)=) =) =) =) =) =) =) =) =) =) =) =) =)Z   * ''))) ))!*;DDDDDDDDDDD,g.DE ) ) )&&((((())U  %||GI|FF$ #fnn+1<<a+@+@LD&#'::<<D#' ) (#x88 )'+ABBx#'::<<8#;#;$)!)*.*T*:*:C.1ggi.D.DG'. %S07
w0K0K27))Ir2J2J+2 )S,9W,DM/? -S0@0I0I'0R0R0R'+'; !) !) !)$(D!)' #fnn) (?'>)<) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <) <)=) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =) =)~ % 	 	 	 	 	 	LL;YJJJ  &K&K&K "!"	         * ''))) ))!*;DDDDDDDDDDD,g.DE ) ) )&&((((())    	   * ''))) ))!*;DDDDDDDDDDD,g.DE ) ) )&&((((())   * ''))) ))!*;DDDDDDDDDDD,g.DE ) ) )&&((((()) 9;;'!!## 	KK]"s#!   #%"%g,,(&+   	L
(b#2#.#m:L:L	
 	
 	
 ,P/PG44$?? !'ll$"2$">"V4CVCV
 
 	
s   $$<C J 
K'KK"L> >
M
M$[= "[+&A)[[+![= Q))/RR[:Z4 A[[+[=  U/VVB[A<Z[Z+	([*Z+	+
[6[+
[	[+[	[+[= +
[55[= 8[59[= <`' =^8'^3=^8>`' ]88/^*)^*.`' 3^88`' _1 1/`#"`#'bab/bbbbr   _interrupt_depthr   c           #      @   	
}~K                                     r#                     	           d{V S ddlm} ddldt
          f fdt                      t          j                  ddl	m
} t           |                                        d          pi }|                    d	          pd                    d
i           }t          |t                    si }ddlm 	 ddlm}  dd          } ||rt'          |          nd           n# t(          $ r Y nw xY w d          }t+          j        d          }t          |t                    r|ni }|                    d          pi }|                              pi }|                    d          pi }d|v p1t          |t                    od|v pt          |t                    o|v }|r|s|n|p|pdddlm~ dk    oj        ~j        k    j        ~j        k    ot           dd                    r                                nddgdgdgdgdgdgj        ~j        k    r j                            ~j                  }t;          |dd          }t          |t                    rkt=          |d          r[|                                D ]F\  }}tA          |          tA          j!                  k    r|"                    |          r|d<    nGtG          j$                    ~ fdt           d                    r j                            j                  nd}|$tK          |          j&        tN          j&        u rdd}g dgdddt@          dt@          dt@          d t          f fd!j        ~j(        k    r
j)        p	}nj)        }|r%|j)        k    r *                    	          nd"|indj        ~j+        ~j,        fv rj)        r	r	nd fd#}dgdgdgdgtG          j$                     j-        d$t&          d%t\          ddffd& j                            j                  j!        j        ~j+        k    rj)        r	r|	d'n|r *                    	          nddt@          d(t@          ddffd)}~
	 f#d*}d}rtG          j/         |                      }d} fd+}!tG          j/         |!                      }  fd,}"tG          j/         |"                      }#tG          j0                     fd-}$tG          j/         |$                      }%tc          d.d/          }&|&dk    r|&ndt           d0d                    sdte          j2                     fd1}'tG          j/         |'                      }(	 tc          d2d3          })|)dk    r|)nd}*tc          d4d5          }+|+dk    r|+nd},d}-tG          j3         4                    |                    }.d}/d6}0|*$d}1	 tG          j5        |.h|07           d{V \  }2}3|2r|.6                                }1n艇7                                s҉rЉ j                            j                  }4d         }5|4r|5rt=          |4d8          r|48                              r|4j9                                      }6|6r|6j:        nd}7tv          <                    d9|%=                                rd:nd;           |5>                    |7           ?                                 n:d}1	 tG          j5        |.h|07           d{V \  }2}3|2r|.6                                }1nd         }8d<}9|8rLt=          |8d=          r<	 |8@                                }:|:                    d>d<          }9n# t(          $ r Y nw xY w|-s|,|9|,k    rd}- j                            j                  };|;rt'          |,d?z            pd@}<t'          |*|,z
  d?z            pd@}=	 |;A                    j!        dA|< dB|= dCD           d{V  n2# t(          $ r%}>tv          B                    dE|>           Y d}>~>nd}>~>ww xY w|9|*k    rd}/n艇7                                s҉rЉ j                            j                  }4d         }5|4r|5rt=          |4d8          r|48                              r|4j9                                      }6|6r|6j:        nd}7tv          <                    d9|%=                                rd:nd;           |5>                    |7           ?                                 7|/rd         }?i }@|?r6t=          |?d=          r&	 |?@                                }@n# t(          $ r Y nw xY w|@                    dFdG          }A|@                    d>d          }B|@                    dH          }C|@                    dId          }D|@                    dJd          }Etv          C                    dK|B|*|A|D|E|CpdL           |?r*t=          |?dM          r|?>                    t                     t'          |*d?z            pd@}FdN|F dOg}G|Cr$|GE                    dP|C dQ|BdRdS|D dT|E dU	           n#|GE                    dV|A dW|BdRdX|D dT|E dY	           |GE                    dZ           d[F                    |G          d         rd                             d\g           ng |Dd         pg ddd]}1d         }Hd         }I|Ir|I                    d^          nd}J|H[t=          |Hd_          rK|JsIt                      }K|HjH        |Kk    r0 I                    |HjH                  s J                               d         }L j                            j                  }Md}Nd}O|Lro|Mrlrit          |M          }N L                    |M|N          }N|L                    d`          r_|Ns]|L                    da          rH|L                    da          }Pt          |P          r tv          <                    dbpdc|P           n|P}On|Nr|Nj:        pdd}Qt;          |Nded          pg }Rt;          |Ndfd          pg }Sg }Tt          |R          D ]w\  }U}V|Ut          |S          k     r|S|U         ndd}W|WP                    dg          p(t;          |Ndhd          t          jR        t          jS        fv }X|Xr|TE                    |V           x|Tr	  T                    |Q|T           d{V \  }Y}Z|Ypd}O|Zrqj)        r	d"j)        ind}[|ZD ]\}\	 |MA                    j!        di|\ dj|[D           d{V  +# t(          $ r%}]tv          B                    dk|]           Y d}]~]Ud}]~]ww xY wnT# t(          $ r6}^tv          U                    dl|^           |Qpt          |N          }OY d}^~^nd}^~^ww xY w|Qpt          |N          }O|Or#tv          B                    dm|Oddn                    |Lr@|Os>|Ns<|L                    do          }_|_r%|_}Otv          B                    dp|Oddn                    |Or|OW                                P                    dT          r|OW                                X                    dd@          }`|`r"|`d         d@d         Y                                ndd}a|arB	 ddqlZm[}b  |b|a          rtv          <                    dr|a           d}Nd}On# t(          $ r Y nw xY w j\        r8|Ns|Or4tv          <                    dspdc ]                                           d}Nd}O|Ns|O
rtv          B                    dt|Oddn                    |Mr:t=          |Mdu          r*r(|Mj^        v r|Mj^                 _                                  j`        k    rtv          U                    dv            j                            j                  }M|Mr|Nrt          |Mj9        |N           n(|Mr&t=          |Mdw          r|Mb                    |O           d         p|1dx	 |r|c                                 |%c                                 |(c                                 | ro	d         du}c|cs3| c                                 	 |  d{V  n# tF          jd        $ r Y nvw xY w	 tG          je        | d67           d{V  nT# tF          jf        tF          jd        f$ r6 | c                                 	 |  d{V  n# tF          jd        $ r Y nw xY wY nw xY w|#c                                 r g                    y            j\        r h                    dz           ||%|#|(fD ]#}d|dr	 |d d{V  # tF          jd        $ r Y w xY w$S |L                    d`          }e|esd         }f|fr| r	 tG          je        | d67           d{V  n# tF          jf        tF          jd        f$ r6 | c                                 	 |  d{V  n# tF          jd        $ r Y nw xY wY n1t(          $ r%}gtv          B                    d{|g           Y d}g~gnd}g~gww xY wt          |L                    d|                    }ht          |frt;          |fd}d          p|hp|fot;          |fd~d                    }i|L                    ddd          }j|jrv|ist	 tv          <                    dpdc           |MA                    j!        |jD           d{V  nQ# t(          $ r%}gtv          U                    d|g           Y d}g~gn'd}g~gww xY w|jrtv          <                    dpdc           t;          tK          |M          dd          _|Mi                              }kt          |k          r8	  |k            }lt          jl        |l          r|l d{V  n# t(          $ r Y nyw xY wnt|Mrrt=          |Md          rb|Mjm        n                    d          }kt          |k          r8	  |k            }lt          jl        |l          r|l d{V  n# t(          $ r Y nw xY w|L                    d\          }m}n|O}od}pd}q|Nst;          |Ndd          p}n o                    |N          r p                              stv          <                    dpdc           |L|r|c                                 |%c                                 |(c                                 | ro	d         du}c|cs3| c                                 	 |  d{V  n# tF          jd        $ r Y nvw xY w	 tG          je        | d67           d{V  nT# tF          jf        tF          jd        f$ r6 | c                                 	 |  d{V  n# tF          jd        $ r Y nw xY wY nw xY w|#c                                 r g                    y            j\        r h                    dz           ||%|#|(fD ]#}d|dr	 |d d{V  # tF          jd        $ r Y w xY w$S  q                    |N|n|m           d{V }o|oh|L|r|c                                 |%c                                 |(c                                 | ro	d         du}c|cs3| c                                 	 |  d{V  n# tF          jd        $ r Y nvw xY w	 tG          je        | d67           d{V  nT# tF          jf        tF          jd        f$ r6 | c                                 	 |  d{V  n# tF          jd        $ r Y nw xY wY nw xY w|#c                                 r g                    y            j\        r h                    dz           ||%|#|(fD ]#}d|dr	 |d d{V  # tF          jd        $ r Y w xY w$S  r                    |N          }pt;          |Ndd          }q j                            j                  }r|rr4	 |rs                    j!        D           d{V  n# t(          $ r Y nw xY w t                    |o|m|nd@z   |p|q
  
         d{V }st          |L|s          |r|c                                 |%c                                 |(c                                 | ro	d         du}c|cs3| c                                 	 |  d{V  n# tF          jd        $ r Y nvw xY w	 tG          je        | d67           d{V  nT# tF          jf        tF          jd        f$ r6 | c                                 	 |  d{V  n# tF          jd        $ r Y nw xY wY nw xY w|#c                                 r g                    y            j\        r h                    dz           ||%|#|(fD ]#}d|dr	 |d d{V  # tF          jd        $ r Y w xY w$S 	 |r|c                                 |%c                                 |(c                                 | ro	d         du}c|cs3| c                                 	 |  d{V  n# tF          jd        $ r Y nvw xY w	 tG          je        | d67           d{V  nT# tF          jf        tF          jd        f$ r6 | c                                 	 |  d{V  n# tF          jd        $ r Y nw xY wY nw xY w|#c                                 r g                    y            j\        r h                    dz           ||%|#|(fD ]#}d|dr	 |d d{V  # tF          jd        $ r Y w xY w$nk# |r|c                                 |%c                                 |(c                                 | ro	d         du}c|cs3| c                                 	 |  d{V  n# tF          jd        $ r Y nvw xY w	 tG          je        | d67           d{V  nT# tF          jf        tF          jd        f$ r6 | c                                 	 |  d{V  n# tF          jd        $ r Y nw xY wY nw xY w|#c                                 r g                    y            j\        r h                    dz           ||%|#|(fD ]#}d|dr	 |d d{V  # tF          jd        $ r Y w xY w$w xY wd         }ft          |1t                    r|1                    d^          sl|1                    d          pdd}t|t p|tdk    }ut          |fot;          |fd}d                    }vt          |1                    d|                    }ht          |fot;          |fd~d                    }wt          |1                    d                    }x|us.|xs,|vs|hs|wr&tv          <                    dpdc|v|h|w           d|1d<   n|us|xr|f|fjv        }y|yr	 |fjw        x                    j!        |y|1d         d           d{V  d|1d<   tv          <                    d|ypdc           n5# t(          $ r(}ztv          U                    dpdc|z           Y d}z~znd}z~zww xY wr|rrt          |1t                    r|1                    d^          st=          |d          rt]                    j!        |tG          j$                    dfd}{	 |y                    |{           n2# t(          $ r%}|tv          B                    d||           Y d}|~|nd}|~|ww xY w|1S )a  
        Run the agent with the given message and context.
        
        Returns the full result dict from run_conversation, including:
          - "final_response": str (the text to send back)
          - "messages": list (full conversation including tool calls)
          - "api_calls": int
          - "completed": bool
        
        This is run in a thread pool to not block the event loop.
        Supports interruption via new messages.
        )rl   r  r   r  r  r  r  r  Nr   r  r   c                  <     sdS                                 S rm  r  r  s   rC   r  z4GatewayRunner._run_agent.<locals>._run_still_current3  r   rL   rU
  rx  rW
  r  r  )set_tool_preview_max_lentool_preview_lengthtool_progressHERMES_TOOL_PROGRESS_MODErg  tool_progress_overridesr  rl  r  interim_assistant_messagesTFr	  voice_mixer_activec                 ~   d         sd         dS              sdS dd<   
j                             j                  }|t          |d          sdS 	 t	          |                    d                   	t          d           dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)z?tool_start_callback: speak a one-time ack in the voice channel.r   NTplay_ack_in_voicezvoice ack scheduling errorr
  zvoice ack schedule failed: %s)	rQ  r'   r	  rr   r   rA  ru   rt   r;  )r%  r8  r$  r  _ack_errr  r  _voice_ack_fired_voice_ack_guild_voice_ack_loopr  s        rC   voice_ack_callbackz4GatewayRunner._run_agent.<locals>.voice_ack_callbackz3  s   " &6q&9&A%%'' "&Q}(()9::Hwx9L'M'MH(../?/BCC#! <	       H H H<hGGGGGGGGGHs   1B 
B<B77B<cleanup_progressr   r   r8  r   r$  c                    %r
             sdS | dk    r߉#d         s	 |                     d          pd}|k    r$dk    r~ddlm}m}m}m}	 t                      }
t          t          |
dd          d	
          }|rB ||
|          s6d#d<   %	                     |	                        |t          dz  |           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY wdS | dvrdS 	 rd         nd}|t          |dd	          rdS n# t          $ r Y nw xY w$dk    r|!d         k    rdS |!d<   ddlm}  ||d
          }d}d}	 'j                             (j                  }n# t          $ r d}Y nw xY wt          |dd	          r|dk    rt'          |t(                    rt'          |                     d          t*                    r|d                                         rddlm} |d                                         }"d         rdn| d| d}| d| d} |            }|dk    r|nd}|                                }|r|d         n|}t5          |          dk    }t5          |          |k    r|d|dz
           dz   }n|r|d z   }| d| d}$d!k    r|d"d<   %	                    |           dS d	"d<   |rddlm}  |            }t7          j        |d	t*          "          }|dk    r#t5          |          |k    r|d|dz
           dz   }| d| d#t;          |                                           d$| }n|r| d| d%| d&}n| d| d}%	                    |           dS ||}d"d<   n]|rNddlm}  |            }|dk    r|nd}t5          |          |k    r|d|dz
           dz   }| d| d%| d&}d	"d<   n| d| d}d	"d<   | d         k    r0&dxx         dz  cc<   %	                    d'|&d         f           dS | d<   d&d<   %	                    |           dS )(z3Callback invoked by agent on tool lifecycle events.Nztool.completedr   r)	  r  )TOOL_PROGRESS_FLAGr  r  tool_progress_hint_gatewayr  tool_progress_commandFr  Trr  z(tool-progress onboarding hint failed: %s>   tool.startedis_interruptedrb  )get_tool_emojiu   ⚙️supports_code_blocksr  r#  )get_tool_preview_max_lenrG   r  r   r  r  (   r   r  rf  z ...rp  )ensure_asciir   (z)
z: "r  	__dedup__)r'   r  rI  r  r  rJ  r  rj  r   putrY  rt   ru   r;  rH   agent.displayrN  rQ  r!   r   r6   r+   rI   rP  r  r  r   r,  r  r  rB  ))r   r8  r   r$  kwargsr)	  rI  r  r  rJ  r  gate_on	_hint_err_agent_for_interruptrN  emoji_code_block_full_code_block_short_progress_adapterrP  	_cmd_full_block_header_pl_cap_lines
_cmd_short
_multilineargs_strr   _LONG_TOOL_THRESHOLD_Sr  agent_holderlast_progress_msg	last_toollast_was_terminal_blocklong_tool_hint_firedprogress_modeprogress_queuerepeat_countr  r  s)                                rC   progress_callbackz3GatewayRunner._run_agent.<locals>.progress_callback3  s   ! ););)=)=  ---6J16M-X%zz*55:H#999mu>T>T             455"1#D)5LMM$)# # # # X7749K+L+L X6:03*../I/I/K/KLLL%Il]&BDVWWW  X X XLL!KYWWWWWWWWX !222:F'P|AD$'3(*:E9 93 F    %%)y|*C*C$IaL 544444"N9h???E  $ $)$(M$5$5fo$F$F!! ) ) )$(!!!) )+A5IIM++tT** ,txx	22C88 , O))++ ,
 CBBBBB O2244	
 2!4RBBU:R:RY:R:R:R  '4#J#J)#J#J#J ..00!Aggss2"--//*0?VAYYi
 [[1_
z??T))!+ITAXI!6!>JJ 5!+f!4J'4$L$L:$L$L$L! 	))#/15+A."&&'7888F-2'* 4FFFFFF2244C#z$UCPPPH Qww3x==3#6#6#+HS1WH#5#="QQYQQdiikk1B1BQQxQQCC 4"??Y??G???CC"33Y333C""3''' !,'-1'** 
3BBBBBB..00!Aggss2w<<$&&%itaxi058G;;;;;;;-2'**/////-2'*
 '***Q1$ ""Kl1o#FGGG#&a LOs#####s<   B!C 
C0C++C0<D 
D+*D+E? ?FFr  c                    K   &sd S )j                             *j                  sd S t                    j        t
          j        u rP&                                s:	 &                                 n# t          $ r Y nw xY w&                                :d S g d dd} d}t          t
                    rj
        nt          	 t          t          dd          pd          }n# t          $ r d}Y nw xY wt          d||dk    rdnd	z
            d
!rh	 t          j        j                  j        }d|v p*t%          d |                                D                       n# t(          t*          f$ r d
Y nw xY wdt,          dt,          f!*fddt.          dt,          fddt.          dt.          t.                   ffdd, fddt,          f!"*fddt0          ffd}	 	  #            sP&                                s:	 &                                 n# t          $ r Y nw xY w&                                :d S &                                }	 $r$d	         nd }|,t          |dd
          rt3          j        d	           d {V  n# t          $ r Y nw xY wt          |t6                    rBt          |          dk    r/|d	         dk    r#|\  }}}	r| d|	dz    dd<   rd         n|}
n[t          |t6                    r/t          |          dk    r|d	         dk    rd g d %d	<   d	(d	<   W|}
                    |
            |             d {V r[t;          j                    } t3          j        d           d {V   #            r"                    *j         !            d {V  t;          j                    }||| z
  z
  }|d	k    rt3          j        |           d {V   #            sd S r85d!!                              } |           d {V }|j"        st          |d"d#          pd##                                }t          |d$d
          r"tH          %                    d%j&                   d&|v sd'|v r4tH          '                    d(j&                   t;          j                    } nd
(                    *j         |
"!)           d {V } rIt          |d*d
          r8t          |dd           r'                    t-          |j)                             nr:d!!                              }(                    *j         |"!)           d {V }n$(                    *j         |
"!)           d {V }|j"        r7|j)        r0|j)         r'                    t-          |j)                             t;          j                    } t3          j        d           d {V   #            r"                    *j         !            d {V  n'# 'j*        $ r t3          j        d           d {V  Y n t2          j+        $ r &                                sC	 &                                }t          |t6                    rFt          |          dk    r3|d	         dk    r'|\  }}}	r| d|	dz    dd<    |             d {V  nt          |t6                    rst          |          dk    r`|d	         dk    rT |             d {V  r3r1r/           }	  |           d {V  n# t          $ r Y nw xY wd g d %d	<   d	(d	<   n%                    |            |             d {V  n# t          $ r Y nw xY w&                                Crrr |             d {V  r3r1r/           }	  |           d {V  n# t          $ r Y nw xY wY d S t          $ r?}tH          ,                    d+|           t3          j        d           d {V  Y d }~nd }~ww xY w~)-NTr  g      ?MAX_MESSAGE_LENGTHr
  r   r   @   r   Fr   c              3   F   K   | ]}|j         t          j        j        u V  d S r  )kindinspect	ParameterVAR_KEYWORD)r   params     rC   r  zKGatewayRunner._run_agent.<locals>.send_progress_messages.<locals>.<genexpr>4  sC         % "J'*;*GG     rL   r  r   c                 ~   K   j         | |d}t          dd          rd|d<   r|d<    j        di | d {V S )N)r   r  r   REQUIRES_EDIT_FINALIZEFTfinalizer   r   )r   rH   edit_message)r  r   rW  _edit_accepts_metadata_progress_metadatar   r  s      rC   _edit_progress_messagezXGatewayRunner._run_agent.<locals>.send_progress_messages.<locals>._edit_progress_message4  s|      %~",& 
 7$<eDD .)-F:&) <);F:&1W1;;F;;;;;;;;;rL   rl	  r   c                 @    d                     d | D                       S )Nr   c              3   4   K   | ]}t          |          V  d S r  r  )r   r  s     rC   r  zcGatewayRunner._run_agent.<locals>.send_progress_messages.<locals>._progress_text.<locals>.<genexpr>4  s(       = =tT = = = = = =rL   )r5   )rl	  s    rC   _progress_textzPGatewayRunner._run_agent.<locals>.send_progress_messages.<locals>._progress_text4  s#    yy = =u = = ====rL   c                     g }g }| D ]=}||gz   }|r1  |                    k    r|                     |           |g};|}>|r|                     |           |S )z>Partition progress lines into platform-sized editable bubbles.)r(   )rl	  groupsr   r  rJ  _PROGRESS_TEXT_LIMIT_progress_len_fnr  s        rC   _split_progress_groupszXGatewayRunner._run_agent.<locals>.send_progress_messages.<locals>._split_progress_groups4  s    %' "! , ,D '4& 0I ,#3#3NN94M4M#N#NQe#e#eg...#'&"+ +MM'***rL   c                     rKt          | dd          r<t          | dd           r-                    t          | j                             d S d S d S d S )Nr(  Fr  )rH   r(   r+   r  )r  _cleanup_msg_ids_cleanup_progresss    rC   _track_progress_resultzXGatewayRunner._run_agent.<locals>.send_progress_messages.<locals>._track_progress_result4  s    %D	599D  d;;D
 %++C0A,B,BCCCCCD D D D D DrL   r{   c                 j   K                        j        |            d {V } |           |S )Nr  )r   r   )r{   r  r  _progress_reply_tor  r   r  s     rC   _send_progress_textzUGatewayRunner._run_agent.<locals>.send_progress_messages.<locals>._send_progress_text4  sc      &||"N //	  ,           '&v...rL   c                    K   	rsdS  	          } t          |           dk    rdS  | d                   }
 
|           d{V }|j        sddS n& |           d{V }|j        r|j        r|j        
| dd         D ]1}  |                     d{V }|j        r|j        r|j        
2| d         	dS )zStart fresh editable progress bubbles before a bubble exceeds limit.

                Returns True when it delivered/split the current buffer and the
                caller should skip the normal send/edit path for this tick.
                Fr   r   Nr  T)r   r(  r  )r  
first_textr  r   r  r  r  r  can_editprogress_linesprogress_msg_ids       rC   !_roll_progress_overflow_if_neededzcGatewayRunner._run_agent.<locals>.send_progress_messages.<locals>._roll_progress_overflow_if_needed4  sI      & !X ! 5//??v;;!## 5+^F1I66
".#9#9/:#V#VVVVVVVF!> %#($u%
 $7#6z#B#BBBBBBBF~ <&*; <*0*;#ABBZ < <E#6#6~~e7L7L#M#MMMMMMMF~ <&*; <*0*;
 "(trL   rM  r  rT  u    (×r  r  	__reset__g333333?r   r   ru  rG   	retryableu5   [%s] Transient edit failure — keeping can_edit=Truefloodzretry afterz-[%s] Progress edit flood control, backing offr  r(  zProgress message error: %sr  )-rQ  r'   r!   r^   r}  r  r	  r	  rt   r   message_len_fnr   r   rH   r  rv  rQ  
parametersr  rb  r   r   r+   r  r   rz  r<  rS  r(   r   r  r  r   r5   r(  r,   ru   r;  rf   r:  r   r  Emptyrh  ru  )+_last_edit_ts_PROGRESS_EDIT_INTERVAL_raw_progress_limit_edit_paramsr  r   rZ  r  base_msgr   r   _now
_remaining	full_textr  rW	  _flood_result_pending_textr  r  r~  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  rh  ri  rn  r{  ro  r  r  s+                      @@@@@@@@@@@@rC   send_progress_messagesz8GatewayRunner._run_agent.<locals>.send_progress_messagesf4  s     ! m''88G 
 G}})-@-MMM(..00 &113333$    )..00 
 N"OHM&)# g':;;&& 
+&)'';OQU*V*V*^Z^&_&_## + + +&*###+ $'#-@3-F-FrrAN$ $  &+"! 3
3#*#4W5I#J#J#UL"l2   )5)<)<)>)>     +* ":. 3 3 3-2***3
< 
<s 
< 
< 
< 
< 
< 
< 
< 
< 
<>d >s > > > >d tDz        D D D D D D D          "T " " " " " " " " " " " "H|+{+--// "0"6"6"8"8 && . 9 9 ; ; ; ;#, & & & %& #1"6"6"8"8 &
 (3355C	BN/X|ATX,/;02BEA A; #*-"2"22222222$$    "#u-- 3#c((a--CFkDYDY-0*8U) O4<1N1N%!)1N1N1NN2.4BPnR00#C// 3CHHMMc!fP[F[F[ +/)+/3)!,*+Q !&--c222>>@@@@@@@@ !(,(8(8%mC000000000--// c")"5"5fnOa"5"b"bbbbbbbb   >++D!8D=<P!QJ!A~~ &mJ777777777 --//  <PO$?$(IIn$=$=	'='=oy'Y'Y!Y!Y!Y!Y!Y!Y%~ "W$+FGR$@$@$FB#M#M#O#OD  'v{EBB ) &$[$+L!" !" !" !)&$-42G2G !'$S$+L!" !" !" 150@0@+029,,(.(+););	 3? 3 3 - - - - - -M !2W$+M9e$L$LW %,M<$N$NW
 !1 7 7M<T8U8U V V V# (,		.(A(AI+2<<(.(1););	 ,8 , , & & & & & &FF ,3<<(.(+););	 ,8 , , & & & & & &F "> Pf.? P.4.?O0 P 0 7 7F<M8N8N O O O$(N$4$4M "-,,,,,,,,,))++ _%11&.K]1^^^^^^^^^{ - - -!-,,,,,,,,,,,- ' ' ',2244 """0";";"="=C)#u55 J#c((a--CPQFVaLaLa58 28U#1 !N<D9V9V%RS)9V9V9VN2$6*K*K*M*M$M$M$M$M$M$M$M!+C!7!7 JCHHMMcRSfXcNcNc 'H&G&I&I I I I I I I I#+ !- !-? !-4BN>4R4RM%-.D.D_Vc.d.d(d(d(d(d(d(d(d(d+4 %- %- %-(,%-26137; 1! 423Q . 5 5c : : :&G&G&I&I I I I I I I I( " " "!E"5 -2244 ":   BN B B??AAAAAAAAA !N ! !$2N>$B$B	!"8"8)"T"TTTTTTTTT( ! ! ! D!FF  + + +LL!=qAAA!-**************+u|+s8  ,B 
BB C6 6DD(AE6 6FFY %H: 9Y :
IY IY  Y 59J0 /Y 0
J=:Y <J==BY B Y ;Y 
Y &BY 3F%Y $a>$a>&C_+]>=_>
^_
^7_a>
_a>_?a>`#"a>#
`0-a>/`00a>6	a>?5a99a>r)  
prev_toolsc                                 sd S g }|pg D ]d}t          |t                    r+|                    |                    d          pd           B|                    t	          |                     et                              dj        rj        j        ndj	        | ||d          t          d           d S )Nrf   rG   z
agent:step)r!   r  r  r)  
tool_namesr  z agent:step hook scheduling errorr
  )r   r6   r(   r'   r+   r   r5  r!   rF   r  ru   )	r)  r  _names_t
_hooks_ref_loop_for_stepr  r  r  s	       rC   _step_callback_syncz5GatewayRunner._run_agent.<locals>._step_callback_sync5  s    %%''  !#F!'R + +b$'' +MM"&&.."6B7777MM#b''****$9? P 5 5b%~",!*"('/ /   >     rL   )r  rG  rl   c           
         	r
             sd S t          j        | |          }|Vt                              dj        rj        j        nd| t          t          |pd                    d d                    d S t          t          	
| |          t          d|  d          }|d S rd
fd	}|	                    |           d S d S )Nz(status_callback suppressed for %s/%s: %sr  rG      zstatus_callback (z) scheduling errorr
  r   c                     	 |                                  }n# t          $ r Y d S w xY wt          |dd           }t          |dd          r&|r&                    t	          |                     d S d S d S )Nr  r(  F)r  rt   rH   r(   r+   )r
  resmidr  s      rC   _track_status_idzQGatewayRunner._run_agent.<locals>._status_callback_sync.<locals>._track_status_id5  s    !jjll$   !#|T::CsIu55 :# :(//C99999: : : :s    
&&r  )
r   r!   ru   r;  rF   r   r+   r   r   r  )r   rl   prepared_message_futr  r  r  r  r  _status_adapter_status_chat_id_status_thread_metadatar  s        rC   _status_callback_syncz7GatewayRunner._run_agent.<locals>._status_callback_sync5  s;   " *<*<*>*> >   
  '>-3_KFO)))7GMr8J8JKKDSDQ	   ++O_jZj  mD  E  EN
NNN	  D |  	9: : : : : : &&'788888	9 	9rL   c                  #  #]^_`abc pdt           j        d<   t          t          j        dd                    } j        ej        k    rdnj        j        }rpd}qpd                                }|r|dz   |z                                   }~j        r|dz   ~j        z                                   }t                       	 ~
                              \  }}t                              d||                    d	          pd           n!# t          $ r}d
| g dg dcY d }~S d }~ww xY w~j        }~                              }|~_        ~                                ~_        d bd }	t)          t)          ~dd           dd           }
|
ddlm}  |            }
 {|d          }||
j        o
|
j        dk    nt3          |          }|}w}|}|s|r6	 ddlm}m} ~j                            j                  }|rt)          |dd          }|st=          d          |
j        }d}j        ej         k    rd}d}j        ej!        k    r tE          t)          |
dd          pd          nd} ||
j#        |
j$        ||||
j        pdt)          dd          pd          } ||j%        |mzzfdnd u          b|rdtL          dd fibfd }	bd<   n2# t          $ r%}t                              d!|           Y d }~nd }~ww xY wdd"dtL          d#t2          dd fhijlmbfd$}~'                    x||          }~(                    |d%         |d&         t|~)                              t)          d'd           t)          d(d           )          }d }t)          ~d*d           }t)          ~d+d           }|r||5  |                              } | r|| d,         |k    rp| d         }tU          |d-          r'	 |+                               n# tX          $ r Y nw xY w~-                    |g           t                              d.           d d d            n# 1 swxY w Y   | ddd%|d%         i|d&         i d/| d0dd1dd2td3sd4|pd d5~j.        pd d6|d7~j        d8|                    d8          d9|                    d:          d;|                    d<          d=|                    d>          d?|                    d@          dA|                    dBd          dC|                    dD          dEdF|d'j/        d(j0        dGj1        dHj%        dIj2        dj3        dJj4        dKdL~j5        dM~j6        }|r7|5|5  ||f|<   ~7                                 d d d            n# 1 swxY w Y   t                              dN|           rynd |_8        od         nd |_9        fj:        rnnd |_;        |	|_<        |r|nd |_=        k|_>        dhij~fdO}!|!|_?        d |_@        ||_A        ~j        |_B        |                    d8          pi |_C        t          jE                    `g ^t          jF                    _dPtL          dd fhijlmfdQad^_`afdR}"dPtL          dd f^_`aijfdS}#|#|_G        jrQrOt)          t          j          dTd           jI                    |"}U           nt)          jdVd           }$|$|"|$<   dWtL          dtL          fhjlmfdX}%|%|_J        |pd<   tU          |dY          r|jK        nd d<   t          vqZ          \  }&}'t                      }(|&D ]})|)                    d[          d\v r|)                    d]d          }*d^|*v rt          jO        d_t          jP                  }+|+Q                    |*          D ]S},|,R                    d,                                          S                    d`          }-|-r|(T                    |-           TddalUmV}.mW}/mX}0mY}1 dbt          dd f]hjlmfdc}2t)          ~ddi           }3r|3[                    d           nd }4|4r|4dz   xz   xt                      }5t          t          v          |5e          }6d }7r3	 ~j_        j`                                      }7n# t          $ r d }7Y nw xY wt3          |7d uot)          |7dfd          o|6          }8t3          |&o |&dg                             d[          dhk    o|6          }9|8r/t)          |7did           pdj}:|:djk    rdkn	|:dlk    rdmndn};do|; dpxz   xn|9rdqxz   xt)          ~drd           }3|3r&r$|3v r |3[                    d           }<|<r|<dz   xz   xpd] |0]          }= |.]|2           	 ~a                              }>|>r	 ddslbmc}?  |?x|>          \  }@}A|Ar)t          d                    dtt          |A          |A           t          du |@D                       r|@}Bnx}Bn6# t          $ r'}Ct          d                    dv|C           x}BY d }C~Cn
d }C~Cww xY wx}Bt          |B|'          }D|&dw}E|'rx|Edx<    |jh        |Dfi |E}F |1]           	 ddylimj}G  |G]           n# t          $ r Y nw xY w |/|=           n>#  |1]           	 ddylimj}G  |G]           n# t          $ r Y nw xY w |/|=           w xY w|F|d<   bbk                                 |F                    dz          }Hd}Id}Jd}Kd}Lpd         }M|Mr`tU          |Md{          rPt)          |Mjl        d|d          }It)          |Md}d          }Jt)          |Md~d          }Kt)          |Mjl        dd          pd}L|Mrt)          |Md%d           nd }N|Hs|F                    d          rd|Fd          nd}Oi dz|Od|F                    dg           d|F                    dd          d|F                    dd          d|F                    dd          d|F                    d          d|F                    dd          d|F                    d          d|F                    d          d|F                    dd          dYd         pg dt          |&          d||Id|Jd|Kd%|Nd|LS d^|Hvrt          |F                    dg           t          |&          |(          \  }P}Q|Prvt                      }Rg }S|PD ]0}T|T|Rvr*|RT                    |T           |Sn                    |T           1|Qr|So                    dd           |Hdz   dp                    |S          z   }Hpd         }d}U|rvrstU          |dE          rb|jq        k    rVd}Ut          r                    d|jq                   ~j_        j`                                      }V|Vr%|jq        |V_q        ~j_        s                                 t)          dFd           ej!        k    rt)          dd           dk    rt)          dJd           ~j5        	 ~j5        t                    |jq                  }W|WrV|W                    dJ          rAtM          |WdJ                   _4        t                              dj4        |jq                   n,# t          $ r t                              dd           Y nw xY w|rt)          |dE          nc|Urdnt          |&          }X|Hr~j5        r	 ddlumv}Y |d         r|d                             dg           ng }ZdtL          dt          dd fd}[|[|rSt)          |d%d           t)          |d	d           t)          |dd           t)          |dd           t)          |dd           dnd d}\~x                              r
c~fd|\d<    |Y~j5        cx|H|Zfi |\ n# t          $ r Y nw xY wi dz|Hd|F                    d          d|d         r|d                             dg           ng d|d         r|d                             dd          ndd|d         r|d                             d          nd d|d         r|d                             dd          ndd|d         r|d                             dd          ndd|d         r|d                             d          nd d|d         r|d                             d          nd dYd         pg d|Xd||Id|Jd|Kd%|Nd|LdEc|F                    dd          |F                    dd          dS )NrG   HERMES_SESSION_KEYrz  r  r  r
  r=  z3run_agent resolved: model=%s provider=%s session=%sr  u'   ⚠️ Provider authentication failed: r   r  r  r  r  r  r  r  r	  Tz(skip streaming for non-editable platformFr
  r  r  r  r  c                  .                          d          S )N)r  )rU  )rn  s   rC   r   z<GatewayRunner._run_agent.<locals>.run_sync.<locals>.<lambda>6  s    ););N)K)K rL   )r   r   r  r   on_new_messager  r{   r   c                 J                 r                     |            d S d S r  )r  )r{   r  r#  s    rC   _stream_delta_cbzDGatewayRunner._run_agent.<locals>.run_sync.<locals>._stream_delta_cb6  s;    #5#5#7#7 !D$4$=$=d$C$C$C$C$C!D !DrL   z$Could not set up stream consumer: %s)already_streamedr  c                .                sd S .|r                                  n                    |            d S |s%r#t          | pd                                          sd S t	                              |           t          d           d S )NrG   r   z+interim_assistant_callback scheduling errorr
  )on_segment_breakon_commentaryr+   rI   r   r   ru   )r{   r  r  r  r  r  r  r#  s     rC   _interim_assistant_cbzIGatewayRunner._run_agent.<locals>.run_sync.<locals>._interim_assistant_cb6  s    ))++ F#/' =(99;;;;(66t<<<F# ? #djb//BWBWBYBY F(#(('!8 )  
 #! M	 	 	 	 	 	rL   r  r+  r  rf
  )r  r  rf
  r~  r|  r   r  z#Reusing cached agent for session %sr	  r  rY
  r  rW
  ephemeral_system_promptprefill_messagesr  rU  rR  rZ
  r[
  r\
  r]
  r^
  r_
  r`
  ra
  rb
  rc
  rd
  re
  r  r!   r  r   rq  r  gateway_session_keyr  rg
  z)Created new agent for session %s (sig=%s)c                     r
             sd S 	 t          |           }n-# t          $ r  t                              dd           Y d S w xY w|sd S t	                              |          t          d           d S )Nzrender_notice_line failedTrp   z)notice_callback delivery scheduling errorr
  )r   rt   ru   r;  r   r8  )r   r  r  r  r  r  r  s     rC   _notice_callback_synczIGatewayRunner._run_agent.<locals>.run_sync.<locals>._notice_callback_sync7  s    & .@.@.B.B F-f55DD    LL!<tLLLLFF  F(11&$??"! K	     s   ! &A
Arl   c                     r
             sd S t                              |           t          d           d S )Nr   z+background_review_callback scheduling errorr
  )r   r   ru   )rl   r  r  r  r  r  s    rC   _deliver_bg_review_messagezNGatewayRunner._run_agent.<locals>.run_sync.<locals>._deliver_bg_review_message&7  sn    & .@.@.B.B F(#(('!8 )  
 #! M	 	 	 	 	 	rL   c                                                        5  t                    }                                  d d d            n# 1 swxY w Y   | D ]} |           d S r  )r)   r  r  )ri  queued_bg_review_pending_bg_review_pending_lock_bg_review_releaser  s     rC   _release_bg_review_messageszOGatewayRunner._run_agent.<locals>.run_sync.<locals>._release_bg_review_messages47  s    "&&(((, / /"#566G&,,.../ / / / / / / / / / / / / / / & 7 7F..v66667 7s   $AAAc                    r
             sd S                                  sR5                                   s#                    |            	 d d d            d S 	 d d d            n# 1 swxY w Y    |            d S r  )is_setr(   )rl   r  r  r  r  r  r  s    rC   _bg_review_sendzCGatewayRunner._run_agent.<locals>.run_sync.<locals>._bg_review_send=7  s   & .@.@.B.B F)0022 #0 # #188:: #.55g>>>"# # # # # # # ### # # # # # # # # # # # # # # +*733333s   +A++A/2A/r	  r  r  questionc           
      X   ddl m} dd l}sdS |                                j        d d         }|                    |pd| |rt          |          nd            	                                n# t          $ r Y nw xY wd}t          
                    | |rt          |          nd |pd          t          d	          }|d}nj	 |                    d
          }t          t          |dd                    }n4# t          $ r'}t                              d|           d}Y d }~nd }~ww xY w|s|                    pd           dS |                                }	|                    |t'          |	                    }
|
|
dk    rdt)          |	dz             dS |
S )Nr   rJ  rG   r  )r  r  r  r  F)r   r  r  r  r  r   zClarify send failed to scheduler
  r  rx  r(  zClarify send failed: %sz'[clarify prompt could not be delivered]z[user did not respond within rY  zm])r  rK  r
  r
  r
  r
  r  pause_typing_for_chatrt   r   send_clarifyru   r  r   rH   rv   r  get_clarify_timeoutwait_for_responser   r   )r  r  r  r#
  r  send_okr
  r  rM   r  r  r  r  r  r  r  s              rC   _clarify_callback_synczJGatewayRunner._run_agent.<locals>.run_sync.<locals>._clarify_callback_synca7  s4   AAAAAA$$$$& 2"[[]].ss3
%%) + 1r%-4>DMMM$	 &   #99/JJJJ    D  .#00 /!)18 BWd#-$/$52!8 1   #! A   ;#GG(!$B!7!7"&wvy%'H'H"I"I$ ( ( ('@#FFF"'(  E !..{/@bAAADD&::<<'99*eT[nn9]]#x2~~P3w|;L;LPPPPs*   A4 4
B B4D 
D6D11D6r  r   r   >   r   r&  r   r'  r   r)  )register_gateway_notifyreset_current_session_keyset_current_session_keyunregister_gateway_notifyapproval_datac                                                    |                     dd          }|                     dd          }t          t                    dd          	 t	                              |
|          t          d	          }|t          d
          |                    d          }|j	        rdS t          
                    d|j                   n2# t          $ r%}t          
                    d|           Y d}~nd}~ww xY wt          dd          }t          |          dk    r|dd         dz   n|}d| d| d| d| d| d| d}	 t	                              |          t          d	          }	|	|	                    d           dS dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)aY  Send the approval request to the user from the agent thread.

                If the adapter supports interactive button-based approvals
                (e.g. Discord's ``send_exec_approval``), use that for a richer
                UX.  Otherwise fall back to a plain text message with
                ``/approve`` instructions.
                r#  rG   rO  zdangerous commandsend_exec_approvalN)r   r#  r  rO  r   z#send_exec_approval scheduling errorr
  z$send_exec_approval: loop unavailabler  rx  zLButton-based approval failed (send returned error), falling back to text: %sz6Button-based approval failed, falling back to text: %sr#  r   r
  rf  u4   ⚠️ **Dangerous command requires approval:**
```
z
```
Reason: r$  zapprove` to execute, `z;approve session` to approve this pattern for the session, `z,approve always` to approve permanently, or `zdeny` to cancel.r   z#Approval text-send scheduling errorz#Failed to send approval request: %s)r  r'   rH   r^   r   r  ru   r*  r  r(  rv   ru  rt   r   r   )r  r  desc_approval_fut_approval_resultrD  r
  cmd_previewr   _approval_send_fut_approval_session_keyr  r  r  r  s             rC   _approval_notify_synczIGatewayRunner._run_agent.<locals>.run_sync.<locals>._approval_notify_sync7  s     55oFFF#''	266$((8KLL
 4002FMMY(@+>>(7(+,A,0)@ ?   +#)(M) ) ) )0"./U"V"VV+8+?+?+?+K+K(+3 #"Fj,2    %   TVX        _.DcJJ36s88c>>c$3$i%//sn'n n#n n !n n 9;n n *,	n n Z\	n n n L)A',,+%< -  
 '%$I	* 	* 	*& *5*11"1===== 65  L L LLL!FKKKKKKKKKLs8   "AC#  C# #
D-DDAF( (
G2GGr  )r   r  r  r   r  r  za gateway restartr  za gateway shutdownza gateway interruptionzD[System note: Your previous turn in this session was interrupted by z. The conversation history below is intact. If it contains unfinished tool result(s), process them first and summarize what was accomplished, then address the user's new message below.]

a)  [System note: Your previous turn was interrupted before you could process the last tool result(s). The conversation history contains tool outputs you haven't responded to yet. Please finish processing those results and summarize what was accomplished, then address the user's new message below.]

r  )build_native_content_partsz:Native image attachment: skipped %d unreadable path(s): %sc              3   H   K   | ]}|                     d           dk    V  dS )r^   rm
  Nr  r  s     rC   r  z=GatewayRunner._run_agent.<locals>.run_sync.<locals>.<genexpr>8  s1      LLquuV}};LLLLLLrL   z8Native image attachment failed, falling back to text: %s)conversation_historyr  persist_user_messager  r  r  r  session_prompt_tokenssession_completion_tokensre  ru  u   ⚠️ r!  r  r  r	  r  r  interrupt_messager  r"  input_tokensoutput_tokens)r"  r#  r*  r   u/   Session split detected: %s → %s (compression)r  r  uG   Restored source.thread_id=%s from binding after session split %s → %sz<Failed to restore thread_id from binding after session splitrp   )maybe_auto_titlern   rM   c                 >    t                               d| |           d S )Nz@Gateway auto-title failure suppressed (not user-visible): %s: %s)ru   r;  )rn   rM   s     rC   _title_failure_cbzEGatewayRunner._run_agent.<locals>.run_sync.<locals>._title_failure_cbO9  s*    ^ #    rL   r  r  r"  )r  r  r  r  r"  )failure_callbackmain_runtimec                 2                         |           S r  )r
  )rn  effective_session_idr  r  s    rC   r   z<GatewayRunner._run_agent.<locals>.run_sync.<locals>.<lambda>_9  s#    RVR|R|"0!S S rL   title_callbackr  r  response_transformed)r  r  r   r  )yr%   r&   r   r9  r!   r  rF   rI   rX  r  rM  ru   r;  r'   rt   rc  r  rZ  r[  r\  rH   rs  r  r  r  r   r  r  r  rQ  r*  r  r$  r  r   r  r  r   r+   rW  r  r  rr   r  rv  r  rV  r  rf
  r  rq  r  r  r  re  r  tool_progress_callbacktool_start_callbackr4  step_callbackstream_delta_callbackinterim_assistant_callbackstatus_callbacknotice_callbacknotice_clear_callbackr  rU  rR  r{  rk  r}  background_review_callbackr^   r	  clarify_callbackr  r  r)   r   compile
IGNORECASEr1  r   r  r-   r  r  r  r  r  r6   r@  r   r   r  rg  rJ  rh  rD  r  rv   r   r  r  rh
  tools.clarify_gatewayr  r  r  r;  r(   r0   r5   r  r:  rx  %get_telegram_topic_binding_by_sessionagent.title_generatorr  BaseExceptionr  )r	  r  combined_ephemeralevent_channel_promptr  rI  rM   rj
  r  r  r$  r  r%  r&  _want_stream_deltas_want_interim_messages_want_interim_consumerr  r  r  r'  r(  r)  r*  r+  r,  r  rk
  _sigrx  r  r  r  r  r  r  _pdcr  r   r   _history_media_paths_hm_hcr/  _matchr
  r  r  r  r  r  _pending_notes_msn_freshness_window_interruption_is_fresh_resume_entry_is_resume_pending_has_fresh_tool_tail_reason_reason_phrase_srn_approval_session_token_native_imgsr  r  _skipped_run_message_img_exc_api_run_message_conversation_kwargsr  _clear_clarify_sessionr  _last_prompt_toks_input_toks_output_toks_context_lengthr  _resolved_model	error_msgr6  r7  r:   unique_tagstag_session_was_splitr   _binding_effective_history_offsetr  all_msgsr  maybe_auto_title_kwargsr  r  r  r  r  r#  r  r  r  r  r6  r  r  r  r  r  r  r  rD  rh  r   r  rW
  r  r  r   "interim_assistant_messages_enabledrl   rp  rn  r  result_holderr  r  r  r  r  stream_consumer_holdertool_progress_enabledtools_holderr>  rF  s                                                                                                @@@@@@@rC   rl
  z*GatewayRunner._run_agent.<locals>.run_sync6  s    0;/@bBJ+, !+BD!I!IJJN %+Ox~$E$E556?K`L "0!52$2$8b#?#?#A#A # b&86&ADX&X%_%_%a%a", k&86&ADDa&a%h%h%j%j"
 <===(,(K(K! + + )L ) )%~
 I>--j99;;L"       &UPS&U&U "!"	        'B#EE'  F     &6D"!%!8!8!:!:D##GD(D99;MME}::::::'))
 65\; O #* :%/U":/** 
 #5%G"%;"" 9R&< 9R8Rcccccccc#}00AAH 3E 29C]_c1d1d.5 ["./Y"Z"ZZ,1L) (-!?ho==02-+/L  &(2CCC "'%1Lc"R"R"YVYZZZ!$ *
 )=(<*/*=-2-C#4(46G&+o&?&-fk2&F&F&L") ) ) ,A+@$,$*N#0%< $2#= "L!K!K!K!K%)0@, , ,( / DDs Dt D D D D D D D 5E.q1  R R RLL!GQQQQQQQQR NS   C d W[           , 88%XXJ
 //7#9% "==kJJ	488#FM4@@ 0  D E!$(;TBBKT>488F Yv1  Y Y#ZZ44F 
Y&)t"3"3 &q	 #6=99 %% & 2 2; ? ? ? ?#+ % % % $%88@PQQQ%JKXXXY Y Y Y Y Y Y Y Y Y Y Y Y Y Y }  $W- +   $2>  $t	
 %*E &6%5 '8&7 -?,F$ &*%;%Ct &6%5 "&!3!3 '1nn5H&I&I&I ')ffVnnn ')ffX&6&6&6 %'FF7OOO  #%&&...!" 137KU0S0S0S#$ .0VV4E-F-F-F%&  *z'( *\)* #NN+, !' 2 2-. %../0 #NN12 %..34 %..56 %..78 )49:  $//;< $(#7#7= @  86#5$ 8 8/4dm{+557778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 H+W[\\\ AV+_+<+<[_E( '7q&9&E""4 % :D9P"Z"5"5VZE*:E'H^/h/D/DdhE,$9E!         " %:E!*.E'%5E"!%!3E&0nn5H&I&I&ORE#!*!2!2,.&/n&6&6#C D          7 7 7 7 7 7 7 7 74 4 4 4 4 4 4 4 4 4 4 4 4 0?E,  
H; 
H4002SUYZZf#CC#3#1 D     #?4NPTUUD',G[)<  < # <  <  <  <  <  <  <  <  <  < | &<E" $LO-4UG-D-DNekk$LO  5Q-5 5 51M1 ), $ = =776??&:::'')R00C3)+1 M* * '5&=&=c&B&B = =F!'a!6!6!8!8!?!?!F!FB! = 4 8 8 < < <           NLT NLd NL NL NL NL NL NL NL NL NL NLb %T+A2FFN<GQ>%%k4888TD 2-'10 !@ A A%C*733-& & &"
 !M ))$($6$?$C$CK$P$PMM  ) ) )$(MMM)!%T) +M+;UCC+*" "
 $( +!"%))&11V;+*$ $  " !-$GG\K\ "333 (' "444 .-1 +(+ + +
   & 5
   %T+I4PPN 6+ 6+2O2O%))+t<< 6"Vmg5G$/$52!&=&=>S&T&T###$9;PQQQ5C
  $GGTT +/RRRRRR+E+E#(, ,( $ "NN \ #Hx   LLVLLLLL 306LL ,3L$ / / /V$   (// $+L#N *$ $ 
 -:)( ($ * KCJ()?@//0@YYDXYY))*?@@@]]]]]]**+@AAAA    D))*ABBBB *)*?@@@]]]]]]**+@AAAA    D))*ABBBB%M!  + ''))) $ZZ(899N !"KLO!!_F _'&*>?? _$+F,EG[]^$_$_!%f.EqII&v/JANN")&*CEUWX"Y"Y"^]^@FPgfgt<<<DO! ;A::g;N;NV7fWo777TV	$i

:r : :  K!;!; fjj599	
 vzz)U;;  K!8!8 "6::mU#C#C (4G)H)H VZZ00 ,VZZ8OQV-W-W \!_2 %c-&8&8 )*; #K $\  _!" %o# P ~--2QJJz2..#&}#5#5(<3 3 3/
/  	T55D"$K) 4 4d?? HHSMMM'..s333* D#**1.BCCC%3d%:TYY{=S=S%SN !OE!& ( ()E)E (%JZ^hJhJh%)"E 0   *377DD /','7E$&,,... FJ559JJJT::dBBT::B(4#'#3#Y#Y','7 $Z $ $ $ [(A(A /28K3H/I/IF,"LL i & 0 * % 0	   %   Z%) %      PU#d75,
#K#K#KZd  .@(WSEWEW%  '$"2 '&FFFFFFGTUVGW_}Q/33JCCC]_H - D     -> #)-%,UGT%B%B(/z4(H(H(/z4(H(H'.ui'F'F(/z4(H(H) ) ) )-	/ 	/+ 33F;; E E E E E E/0@A
 %$(,&   2    !   D . &**-=">"> MRSDT\M!,00R@@@Z\ ]STEU\]1-11+qAAA[\	
 -PQBR\]1-11+>>>X\ ][\M]h}Q/33M5IIIch ]STEU`=+//	5AAA[` -:JTq)--g666PT $R_`aRb%l]1%5%9%9:M%N%N%Nhl a.B !"; %&7      !/!" 2#$ '-jj1Eu&M&M(.

3I5(Q(Q'   s*  AD 
D1	D,&D1,D1$DK' '
L1LL<Q1P$#Q1$
P1.Q10P114Q11Q58Q52WW!W$d dd7l Ai. -l .
j8jl j/l k, ,
k98k9ml'&m'
l41m3l44m-A8|& &&}}CAA A
AAAAAc                     K   t          d          D ]G} d         #d                                          d{V   dS t          j        d           d{V  HdS )z8Wait for the stream consumer to be created, then run it.r
  r   Nr  )r  r  rz  r<  )r  r:  s    rC   _start_stream_consumerz8GatewayRunner._run_agent.<locals>._start_stream_consumer9  s      3ZZ * *)!,80377999999999FFmD))))))))))	* *rL   c                  4  K    d         "t          j        d           d {V   d         "sd S 6                              s t                              dpd           d S  d         j        <   j        r                    d           d S d S )Nr   r  uL   Skipping stale agent promotion for %s — generation %s is no longer currentrG   r5  )rz  r<  r		  ru   r:  rq  r'  r  )rh  r  r  r  s   rC   track_agentz-GatewayRunner._run_agent.<locals>.track_agent9  s      q/)mD))))))))) q/) 
 )$2N2N^3 3) b%2"  
 0<QD -~ 8++J777778 8rL   c                  j  K   sd S 	 t          j        d           d {V  	 j                            j                  } | s>t          | d          r|                               rd         }|r| j                                      }d }||j        pd}t          |dd           pg }t          |dd           pg }g }t          |          D ]w\  }}|t          |          k     r||         nd}	|	                    d          p(t          |d	d           t          j        t          j        fv }
|
r|                    |           x|r	                     ||           d {V \  }}|}|rqj        r	d
j        ind }|D ]\}	 |                     j        d| d|           d {V  +# t*          $ r%}t,                              d|           Y d }~Ud }~ww xY wnE# t*          $ r%}t,                              d|           Y d }~nd }~ww xY w|s|rt3          |          }t,                              d           |                    |                                            d S nA# t           j        $ r  t*          $ r%}t,                              d|           Y d }~nd }~ww xY w)NTg?has_pending_interruptr   rG   rB  rC  rG  rE  r  rM  r  r   z+Voice-interrupt echo failed (non-fatal): %sz(Voice-interrupt transcription failed: %sz3Interrupt detected from adapter, signaling agent...z,monitor_for_interrupt error (will retry): %s)rz  r<  rQ  r'   r!   rr   rB  rs  r{   rH   rH  r   rI  r  rj  ri  r(   rn  r  r   r   rt   ru   r;  rv   rP  r"  r)   rh  )r  rx  _peek_eventpending_text_media_urls_media_types_audio_paths_i_path_mtype	_is_audio	_enriched_transcriptsr  r  r  
_trans_exc_mon_err_interrupt_detectedrh  r  r  r  s                     rC   monitor_for_interruptz7GatewayRunner._run_agent.<locals>.monitor_for_interrupt9  s      M[mC(((((((((K[  $}00AAH# ! 
 x)@AA ="hFdFdepFqFq =" ,Q  ;" +3*D*H*H*U*UK+/L*6/:/?/E2 /6k<QU.V.V.\Z\/6{MSW/X/X/^\^/11:;1G1G !C !CIBACc,FWFWAWAW\"-=-=]_F(.(9(9((C(C )@+2;PT+U+UZeZkmxm~Y+ %. (1 %C(4(;(;E(B(B(B#/ !Y%*HLHoHo,8,I* I* C* C* C* C* C* C*(?	< 8A+7 )6\b\l9v+vGW9X9XrvJ7C -6 -6
16:B--8>8JC8J8J8JAK ;H ;6 ;6 56 56 56 56 56 56 56 56
 8A 16 16 164:LL8e8A56 56 56 56 56 56 56 5616
 ,5 %* %* %*(.,VXb)* )* )* )* )* )* )* )*%* *6 !Y+ !Y3KK3X3XL"LL)^___!OOL999/33555!E-     [ [ [LL!OQYZZZZZZZZ[YM[ss   !I2 DI2 9G' 'F43G' 4
G#>GG' G##G' &I2 '
H1HI2 HAI2 2J0J++J0r     long_running_notificationsc                    K   d S j                             j                  } | sd S d }	 t          j                   d {V  t          t          j                    z
  dz            }d         }d}t           dd                    }|rt          |d          r	 |	                                }g }|r'|
                    d|d          d	|d
                     |                    d          p|                    d          }|r"|
                    t          |                     |rdd                    |          z   }n# t          $ r Y nw xY wd| d| }		 d }
|rX	 |                     j        ||	           d {V }
n4# t          $ r'}t                               d|           d }
Y d }~nd }~ww xY w|
rt%          |
dd          sp|                     j        |	           d {V }
t%          |
dd          r<t%          |
dd           r+t          |
j                  }r
                    |           n2# t          $ r%}t                               d|           Y d }~nd }~ww xY wM)NTrY  r   rG   r  r  r  r  r   r	  r
  rZ  rD  r  u   ⏳ Working — z minzHeartbeat edit failed: %sr(  Fr   r  z#Long-running notification error: %s)rQ  r'   r!   rz  r<  r   r   r   rr   r  r(   r+   r5   rt   r}  r   ru   r;  rH   r   r  )_notify_adapter_heartbeat_msg_id_elapsed_mins
_agent_ref_status_detail_want_iteration_detail_ar  r  _heartbeat_text_notify_res_ee_ne_NOTIFY_INTERVALr  r  _notify_startr  rh  r  r  r  r  r>  s                rC   _notify_long_runningz6GatewayRunner._run_agent.<locals>._notify_long_running":  s     '"m//@@O"  04:Mm$4555555555 #TY[[=%@R$G H H
 *!_
!#)-++#$)	 * *&  '*6L"M"M '<<>>!#1 "MM ZR0@-A Z ZBGWDX Z Z   #%&&"8"8"XBFFCW<X<X" 8"MM#g,,777! I-4tyy7H7H-HN$   "X]"X"X"X"XM"&K( 	//0?0L0L & 1 /1 1 + + + + + +KK
  ) / / /"LL)DcJJJ*.KKKKKK/ ( KGKE,R,R K,;,@,@"N+%< -A - - ' ' ' ' ' '
 #;	5AA Kg'tG G K 14K4J0K0K-0 K 0 7 78I J J J  M M MLL!FLLLLLLLLMs:MsV   &B'E 
EE'I ,"F I 
G F;6I ;G  BI 
I6I11I6r  rX  r  i  r   rx  rB  zABackup interrupt detected for session %s (monitor task state: %s)rk  r  r  r  rY  rY  r   u   ⚠️ No activity for zB min. If the agent does not respond soon, it will be timed out in z- min. You can continue waiting or use /reset.r   z!Inactivity warning send error: %srZ  r  r
  r  r	  zaAgent idle for %.0fs (timeout %.0fs) in session %s | last_activity=%s | iteration=%s/%s | tool=%sr  r"  u   ⏱️ Agent inactive for u(    min — no tool calls or API responses.z!The agent appears stuck on tool `z` (r[  z!s since last activity, iteration r   z).zLast activity: r  zs ago, iteration z6). The agent may have been waiting on an API response.zTo increase the limit, set agent.gateway_timeout in config.yaml (value in seconds, 0 = no limit) and restart the gateway.
Try again, or use /reset to start fresh.r   r!  )r  r!  r  r  r"  r  r  r  r  r  z5Ignoring control interrupt message for session %s: %sr  rG   rB  rC  rG  rE  rM  r  z'Voice-drain echo failed (non-fatal): %sz$Voice-drain transcription failed: %sz9Processing queued message after agent completion: '%s...'rQ  pending_steerz0Delivering leftover /steer as next turn: '%s...'rD  uZ   Discarding command '/%s' from pending queue — commands must not be passed as agent inputz=Discarding pending follow-up for session %s during gateway %sz#Processing pending message: '%s...'r	  u^   Interrupt recursion depth %d reached for session %s — queueing message instead of recursing.queue_message)r  r!  r  r5  z5Stream consumer wait before queued message failed: %sr  final_response_sentfinal_content_deliveredr  zoQueued follow-up for session %s: final stream delivery not confirmed; sending first response before continuing.z7Failed to send first response before queued message: %sz_Queued follow-up for session %s: skipping resend because final streamed delivery was confirmed.r  r  r  r  uN   Discarding stale goal continuation for session %s — goal is no longer activer  r   )
rl   r  r   r  r  r  r  r6  r  r   r  r  zSuppressing normal final send for session %s: final delivery already confirmed (streamed=%s previewed=%s content_delivered=%s).r  )r   r  r   r|  zPEdited streamed message %s for session %s to include plugin-transformed content.z2Failed to edit streamed message for session %s: %sr	  c                  |    dfd} 	 t           |             t          d           d S # t          $ r Y d S w xY w)Nr   c                  p   K   D ]/} 	                      |            d {V   # t          $ r Y ,w xY wd S r  )delete_messagert   )_mid_adapter_snapshot_chat_id_snapshot_ids_snapshots    rC   _delete_allzLGatewayRunner._run_agent.<locals>._cleanup_temp_bubbles.<locals>._delete_all<  s       - ! !!"3"B"B 14# #          ) ! ! ! D!! !s   &
33z$Temp bubble cleanup scheduling errorr
  r  )r   ru   rt   )rn  rk  rl  rm  _loop_snapshots    rC   _cleanup_temp_bubblesz7GatewayRunner._run_agent.<locals>._cleanup_temp_bubbles<  s    ! ! ! ! ! ! ! !,#~%$J     
 !   DDs    - 
;;z-Post-delivery cleanup registration failed: %sNNNr  )zr  r5  r 	  r  r{  r   r  r  r!   rp
  rV
  r  r'   r   r6   r  r  rV  r9  r   rt   r%   r9  rs  r  r  Queuer	  rQ  rH   rr   r   r+   r   r?  rz  r6  r^   ri  r  r  r  r  r  
MATTERMOSTr  r  r  rk  r   r   ensure_futurerq
  r  r  r  rB  rs  r{   ru   r:  rk  r"  r)   r  r   r;  ru  _INTERRUPT_REASON_TIMEOUTr(   r5   r  r  r  r{  r  r  r  rH  r   rI  r  rj  ri  rn  rv   rP  rI   r  r,   r   rE  r'  ru  r	  r  _MAX_INTERRUPT_DEPTHr  rd  r  rh  r  r  r|  r  r  r   rv  isawaitabler  r@  r  r  r  r  r  r	  r  r  r   r}  r	  )r  rl   r  r   r  r  r  r  r6  r  r   rV
  agent_cfg_localdisplay_configr9  _tpl_resolved_tp_env_tp_display_cfg_platforms_cfg_platform_cfg_legacy_tp_overrides_tool_progress_configured_va_vtc_gid_tc_cleanup_adapter_progress_thread_idr  rl
  progress_taskr-  r>  r@  tracking_taskrQ  interrupt_monitor_NOTIFY_INTERVAL_RAWrb  _notify_task_agent_timeout_raw_agent_timeout_agent_warning_raw_agent_warning_warning_fired_executor_task_inactivity_timeout_POLL_INTERVALr  rk  r  _backup_adapter_backup_agent	_bp_event_bp_textrX  
_idle_secs_act_warn_adapter_elapsed_warn_remaining_mins	_warn_err_timed_out_agent	_activity
_last_desc	_secs_ago	_cur_tool_iter_n	_iter_max_timeout_mins_diag_linesr  _result_for_fb_run_failed
_cfg_modelr  r   r  ri  r  r  rE  rF  rG  rH  rI  rJ  rK  rL  rM  r  r  r  rN  _leftover_steer_pending_parts_pending_cmd_word_rc_pending_has_stream_consumerrn   was_interrupted_scr  
_previewed_already_streamedfirst_response_bg_cb
_bg_resultupdated_historynext_sourcenext_messagenext_message_idnext_channel_prompt_followup_adapterr  _final_is_empty_sentinel	_streamed_content_delivered_transformed
_sc_msg_id	_edit_errrp  _rper  r  rg  r`  rk  rl  r  r  r  rm  rP  r  ro  ra  r  r  r  r  r  r  r  r  rC  rD  rE  rh  rW
  r  r8  ri  rj  rk  rl  r  rp  rm  rn  r{  ro  r  r9  r:  r;  r<  r>  rF  s   ```````````                                                                                                                  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@rC   r	  zGatewayRunner._run_agent2  s91     4    
	22-%'-!1 3 	 	 	 	 	 	 	 	 	 	&%%%%%	MD 	M 	M 	M 	M 	M 	M 	M 	M
 +,,+FO<<??????!"5"5k<"P"PQQ%//'228b+//0CDDL$B77.$// 	 N
 	CBBBBB	>>>>>>**;F[]^__D$$$%=SYYYA>>>> 	 	 	D	 /.{L/ZZ)788)3ND)I)IQ~~r%))+66<"&**<88>B+//0IJJPb|+ =$// 5#}4 /66 9 $88 	" 484GG2'2U 	 	,+++++ - 6 ^6?hN^;^
 Ox// '' 0	   	+ +@ITF	!Fs $)' "715?h...-##H$455C 3 6==D$%% '#7K*L*L !%  ID#3xx3v~#6#6663;Q;QRV;W;W6.2(+!244	H 	H 	H 	H 	H 	H 	H 	H 	H 	H4 !##K?QRR
 
 BS\4=,,V_===X\'!""15H5WWW !&#&( !&w!%g	$ g	$# g	$# g	$s g	$ae g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$ g	$f ?hn,,"("2"F6F"("2
 !	+"f&666 ,,V5EFFF233&*	 	 8?H4G"HHHVM]HbrH  	I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+ I	+X
 vv"& !133Z
	3 	D 	T 	 	 	 	 	 	 	 	 	 	6 -++FO<< .?ho--&2B-GW- 1'7A A##
 ex  'Bd&F&FvO_&`&`&`  ~B#!	9c !	9C !	9D !	9 !	9 !	9 !	9 !	9 !	9 !	9 !	9 !	9 !	9 !	9 !	9 !	9F|	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	 |	~   	J#/0F0F0H0HIIM 	* 	* 	* 	* 	* )*@*@*B*BCC	8 	8 	8 	8 	8 	8 	8 	8.  +KKMM:: &mooQ	[ Q	[ Q	[ Q	[ Q	[ Q	[ Q	[ Q	[ Q	[f $/0E0E0G0GHH  **H#NN3G!3K3K//QU##,	 
 
 	$  $	F	M F	M F	M F	M F	M F	M F	M F	M F	M F	M F	M F	M F	M F	M F	MP *+?+?+A+ABB|	 ",,BD!I!I3E3I3I//tN!+,JC!P!P3E3I3I//tN"N$2228<< N #( N%  6$+L'(.% % %      GD!  #1#8#8#:#: /5577 6K 6*.-*;*;FO*L*L(4Q+ 6 6$+O=T$U$U6$3$I$I+$V$V6 )8(I(M(Mk(Z(ZI9B'Ly~~H"KK!; +*;*@*@*B*B Q		   *33H===/3355516 .  66$+L'(.% % %      GD!  #1#8#8#:#:!-aJ!$J! !gj:P&Q&Q !!#-#B#B#D#DD)-2JC)P)PJJ( ! ! ! D! + ]~/I *n < <)-(,(9(9&/(J(J( ],/"0D,E,E,JM.1>N3RWY2Y.Z.Z._^_O
]&3&8&8$*N%Om %O %O7F%O %O %O .E '9 '" '" !" !" !" !" !" !" !" !" $- ] ] ] &-PR[ \ \ \ \ \ \ \ \]!^33.2+.5577 6K 6*.-*;*;FO*L*L(4Q+ 6 6$+O=T$U$U6$3$I$I+$V$V6 )8(I(M(Mk(Z(ZI9B'Ly~~H"KK!; +*;*@*@*B*B Q		   *33H===/33555m66p # =#/? 	# 0@BX(Y(Y $4$I$I$K$K		$    ']]+?KK
%MM*BAFF	%MM.99	#--(8!<<%MM*:A>>	E~{'   $ J0@+(N(N J$../HIII #Nb$8 9 9 >Q) ) ) )  &&=I = =%B= =%,= =/8= = =     &&N* N N	N N N%,N N/8N N N  
 ""?   '+ii&<&<HUVWHX `a 0 4 4Z D D D^`!()!_2&'" " "!_F*1-N:HS.,,X666eK!gfg&>&>!{!355
<:--d6W6WXcekeq6r6r- ,,[999 #1%Fm''88G !MG Ap' Apk Ap 6w L L !% : :;Q^ _ _::m,, 8p] 8pvzzReGfGf 8p(.

3F(G(G%45FGG 4S'.3-    #4" .p %2$6$<"M")-t"L"L"RPRK#*=-#N#N#TRTL#%L%.{%;%; 7 7	E57#l:K:K5K5Kb!1!1QS"--h77 v&}ndKKP[PacnctOuu " % 7(//666# [_<@<c<c -|= = 7 7 7 7 7 73I| '0&74G+ *PVP`-jk6;K-L-Lfj
+7 
!* 
!*C	%*.5ll,2N,>,>,>,>5? /; /* /* )* )* )* )* )* )* )* )*
 ,5 %* %* %*(.,UW`)* )* )* )* )* )* )* )*%*  ) _ _ _"NN F
   '4&^7OP]7^7^GGGGGG	_ #0"Z3KM3Z3Z p%`bijmkmjmbnooo  cg cm c"(**_"="=" c-GLL!SU\]`^`]`Uabbb  7==??55c:: !(!6!6tQ!?!?ES$[N1$5abb$9$?$?$A$A$AY[!$ VVVVVV&;'899 +"KK!M 1  
 -1M&*G$    ~ = G S&3--//  
 !% OY OYBGCRCLQQQ
  Bww0BCC B BXcgngXX,[9??AAA $t'@@@NNA(+  
 #m//@@G D= D3G4M{\ijjjj  DWWo%F%F D--k7CCC(+`(X_/`/``x  '$$&&&$$&&&!!!  ! + >.q1= % , !&&((()))))))))"1   !%.{CHHHHHHHHHHH#0'2HI ! ! !#**,,,!"---------&5 ! ! ! D!	!   """  11 2    ~ 8++J777 '(9=,W   "







"1   U #)**]";";& A% 13C 
e{ 
e	e")"2;"L"L"LLLLLLLLL ' 4g6LM % % %'..000%&1 1 1 1 1 1 1 1 1#*#9 % % % $%( e e e"LL)`bcdddddddde!%fjj1E&F&F!G!GJ(,K.CU!K!K T%TRGC1JE$R$R) )%
 &,ZZ0@"%E%EN% .? i"KK !R + 2s   #*,, & .)@ #/ # #        
  ) i i i"NN+dfghhhhhhhhi' }'.3   tG}}.JDQQ]!(!C!C''5 "D " " $F++ %%-3VXX
#*#6z#B#B !5*4$4$4$4$4$4$4$4#, % % % $%% ! %WW6P%Q%Q %!(!A!E!EkSW!X!X#F++ %%-3VXX
#*#6z#B#B !5*4$4$4$4$4$4$4$4#, % % % $% #)**Z"A"A$&"&&*# ,")-4"H"H"RFK77FF &tOrOrs}O~O~ &l'.3    &N  '$$&&&$$&&&!!!  ! + >.q1= % , !&&((()))))))))"1   !%.{CHHHHHHHHHHH#0'2HI ! ! !#**,,,!"---------&5 ! ! ! D!	!   """  11 2    ~ 8++J777 '(9=,W   "







"1   m *.)K)K+* / *L * * $ $ $ $ $ $L
 $+%@  '$$&&&$$&&&!!!  ! + >.q1= % , !&&((()))))))))"1   !%.{CHHHHHHHHHHH#0'2HI ! ! !#**,,,!"---------&5 ! ! ! D!	!   """  11 2    ~ 8++J777 '(9=,W   "







"1   _ '+&B&B=&Q&QO*1-AQSW*X*X'
 %)M$5$5fo$F$F!$ /;;"N%< <           %    )-(#1+&) +#1%5%9%4#6 )8 ) ) # # # # # # @XX  '$$&&&$$&&&!!!  ! + >.q1= % , !&&((()))))))))"1   !%.{CHHHHHHHHHHH#0'2HI ! ! !#**,,,!"---------&5 ! ! ! D!	!   """  11 2    ~ 8++J777 '(9=,W   "







"1   EOYd  '$$&&&$$&&&!!!  ! + >.q1= % , !&&((()))))))))"1   !%.{CHHHHHHHHHHH#0'2HI ! ! !#**,,,!"---------&5 ! ! ! D!	!   """  11 2    ~ 8++J777 '(9=,W   "







"1   a  '$$&&&$$&&&!!!  ! + >.q1= % , !&&((()))))))))"1   !%.{CHHHHHHHHHHH#0'2HI ! ! !#**,,,!"---------&5 ! ! ! D!	!   """  11 2    ~ 8++J777 '(9=,W   "







"1   ( %Q'h%% .	hll8.D.D .	\\"2339rF%+!Bv/BB%:EBB I
 hll+?@@AAJ!%F%>FF" "  -C D DEEL% l 	 Z [m  V&3&   ,0((' L S_ !^
 !k66$*N'1$,-=$>%)	 7          480n&(:s    %   P'.3	        '	T ,  - - 8T** - LL**	 -
 (*KLL - !!122M & 0$577N        $T @@)- A    
  T T TLdSSSSSSSST s  /0E   
E-,E-GAq8 +*b Aq8 
b# Aq8 "b##AAq8 ?*d* )Aq8 *
e4eAq8 eDAq8 -j Aq8 
jAq8 jNAq8 ";z9 'zz9 
z5z0+z9 0z55z9 8Aq8 9
{9,{4/Aq8 4{99C4Aq8 .0A@ @Aq8 @
A@,@)Aq8 @+A@,@,D<Aq8 GAGGAG'G&AG'G+AHH/AIH8AII AIIAIIAIIAIIAIIAIJ0AJ9J9AKK
AKK$Aq8 K5AL LAq8 L/ANMAMM
ANMAMMANMAMMANM Aq8 M"	ANM+ANNAq8 NANNA4Aq8 PA AQ QAq8 Q
AQ5QAQ0Q+Aq8 Q0AQ5Q5A&Aq8 S&AT TAq8 T
ATTAq8 TATTA Aq8 U&AU8 U7Aq8 U8
AVVAq8 VAVVBAq8 Y)AY2Y2AZZAZZAZ%Z%/A[6[A[[A[6[A[0[-A[6[/A[0[0A[6[5A[6]A]]A](]'A](]-"Aq8 _2A_;_;A``A``A`.`./Aa?aAa'a&Aa?a'Aa9a6Aa?a8Aa9a9Aa?a>Aa?cAccAc1c0Ac1c6AAq8 d>"Ae! e Aq8 e!
Ae.e+Aq8 e-Ae.e.:Aq8 hAhhAh&h%Ah&h*Aii/Aji7Aj i?Ajj AjjAjjAjjAjjAjk/Ak8k8Al
l	Al
m3Am< m<AnnAnnAn/ n//Ap oAo(o'Ap o(Ao:o7Ap o9Ao:o:Ap o?Ap qAq q Aq2q1Aq2q8A#Aw sAs%s$Aw s%As7s4Aw s6As7s7Aw s;AttAw t/Au)uAuuAu)uAu#	u Au)u"Au#	u#Au)u&Aw u(Au)u)AAw w Aw	wAw w	Aw	wAw wAw	wAw {;AA} }
A~ }A};};A~ :B@ @
BA@B@=@=BAr  r  r  )r`  )r  )rK  r  r;  rq  )r`  r  r  )NNr   NN)r_   
__module____qualname____doc__r!  r   r+   r   __annotations__r#  r$  r  r%  r&  r	   r   r'  r   r(  r)  r*  r+  r,  r  r-  rz  Taskr.  r/  r
   r  r  r  rR  r  rY  r  r  r  r  r  r  r  r  r  r  r  r  propertyr  r   r  r  r  r  	frozensetr  r  r  r  r"  r$  r&  r(  r,  r/  r8  r<  r6   rS  rM  rW  r  rk  rn  rp  ru  ry  r}  r  r  r  staticmethodr  r  r  r  re  r  r  r   rU  rW  rY  r  r  r  r[  r]  r_  r`  ra  r  rb  r  rd  r  r  r  r  r  r  r  r  r1  r@  rB  r\  rf  rd  rv  rp  r)   rs  rz  r|  r  r  r  r  r  r_  rB  re  r>  r  rA  rh  r  r+  r8  r.  r  rh  r  rK  r  r  r  r	  r	  r  r  r	  r	  r	  r	  r  r	  r	  r	  r	  r	  r	  r	  r	  r	  rz
  r
  r
  r
  r
  r
  r
  r
  r
  r
  r
  r
  r
  r
  r
  r   r  r
  r
  r  rM  r  r  _APPROVAL_TIMEOUT_SECONDSr  r  r  r  r$  r  r  r  r  r   r  r"  r)  r+  r  _UPDATE_ALLOWED_PLATFORMSr9  r
  r8  r:  r;  r  r  r
	  rq
  rl  rm  rn  rb  rh  r	  r=  rv  r  r  r  classmethodr  r  r  r  rC  r  r|  r  r  r  r		  r	  r  r{  r  r  r  r  r  r5  r	  r   rL   rC   r   r     s#          ,.S%Z(---'c'''&OS&&&$IEIII $J$$$It$$$$"'4'''#t###!&$&&&7;Xm4;;;)-J&---:<d3S#X#67<<<>@ $sDcN':";@@@n, n,x6 n, n, n, n,b   >-
 -
 -
 -
f$     $&??-8 -c -c - - - -4S>    4@ @ @ @,s ,d ,W[ , , , ,+c +D +UY + + + +$* * * *X   <8% 8 8 8 86 6 6 6 6
 
 
 
 
 "T " " " X" '$ ' ' ' X' !Xc] ! ! ! X! 8C=    X
m 
 
 
 
 
 = T    . #,)RI"6"67M 7d 7 7 7 7	m 	 	 	 	 	 +/'= T    (
C 
 
 
 

# 
 
 
 

 
8C= 
 
 
 


 
	
 
 
 
"
  
   422 
#2 2 2 2h@@ 
@ @ @ @> +/%)&*k% k% k% 'k% c]	k%
 d^k% 
sDy	k% k% k% k%Z+s +3 +X\ +ae + + + +ZA9L AQU A A A AF## #$ # # # #
)c ) ) ) )Dc D D D DLs L L L LWT W W W W"5 5N 5UX 5]a 5 5 5 5 !! !  /	!
 
.	!! ! ! !F @D          U3 U4 U U U \US 3 SV    >	 	 	 	 	 	
 
HSM 
W_`cWd 
pt 
 
 
 
  )-$('+   !	
 SM  } 
   0 AC $
 $
 $
# $
t $
 $
 $
 $
L4    4 Dc3h$8    \B 
U3 
U 
U 
U \
U D4K    \ F Fc4i8H F F F \F8 +/%)	- - - '- c]	-
 
- - - -&TT #4.T 
	T T T T d
    \$ 
$ 
 
 
 \
 
3 
 
 
 \
 A# A A A \A.     \$     \6 D    \ $+    \(
$sCx. 
 
 
 
hsm    
 
# 
 
 
 
  
sHSM!	"	   : #3 #4 # # # \#V !%83 %8| %8X\ %8 %8 %8 %8Nw| wZ] wbf w w w wr#% #E$sCx.RVBV<W # # # #<Q Q Q Q Q Qk k k kZ1tCH~ 1$ 1 1 1 1'c 'd ' ' ' 'R 0S T    6/c / / / /b     *y y y yvKS KS KS KSZ 38U   4 d W[    , %9FFF P P# P P P PddT d d d dL0* 0*u 0*t 0* 0* 0* 0*df>$sCx. f>T f> f> f> f>Pp' p'c p' p' p' p'dc    t' t' t' t'r !& %B B B B 	B
 B 
B B B BH* * * *UU U 
%	&	U U U UvGc Gd G G G G>K:< K:HSM K: K: K: K:Z&w w 	w
 d38n%w 
#w w w wr?s ?tCy ? ? ? ?     *c    D9# D9_b D9 D9 D9 D9L$u c u  u  u  u t(D#(D47(D	#(D (D (D (D`%} %s %t % % % %T/8, /84 /8 /8 /8 /8r6| 6 6 6 6 6<!Q\ !Q !Q !Q !QLS    *W W W W0S 3 4    &)S )SV )[_ ) ) ) )VCG CG 	CG
 CG 
CG CG CG CGN \ hsm    \7Xl 7Xs 7X 7X 7X 7Xr%| % % % % %.MS MT M M M M's 'S 'VY '^b ' ' ' 'RB,B,&)B,7:B, B, B, B,R #4 44 4 	4
 4 
4 4 4 4lJ\ J J J J J JXwIwI wI
 
wI wI wI wI@ +/*.+/H HH  H 	H
 #3-H T#Y'H d3i(H 
H H H H`
] 
t 
 
 
 
8'U- 'UD 'U 'U 'U 'URU] Ut U U U U$	C 	C 	 	 	 	SdSd Sd 	Sd
 
Sd Sd Sd Sdj= T    .#6#6 #6 	#6
 
#6 #6 #6 #6J ,1(M d    &
3 
 
 
 
,"
M "
c "
 "
 "
 "
J, - , C ,  ,  ,  , \4< 4Y\ 4ad 4 4 4 4xh;| h; h; h; h; h;xV
 V
 	V

 V
 V
 
s$d*	+V
 V
 V
 V
pB B 	B
 B B 
#B B B BH4S>      .2
 
 &c]
 
$sCx.	!	
 
 
 
( $(-1!%  8$ # C=	 C= &c] # 
$sCx.	!   @  $(!%  8$# C=
 C= # 
   \> .| . . . . \. !$ !*	8>8+<h&7)@(/S[Sgiqiw  zB  zH	+ ! !X X X X  #!$	_? _?_? _? 	_?
 
_? _? _? _?Bp p p p pdF0(5c8TW=AX;Y2Z F0 F0 F0 F0V GKB B B s5c8C=)@#ABCB 
U3Xc]*+	,	B B B BH
 
4 
 
 
 
*# #$ # # # #
F F F#    .CC #YC 
	C C C CJd1d1 #Yd1 
sDI~		d1 d1 d1 d1L@ @
 
t@ @ @ @DE
t E
 E
 E
 E
N%F3 %FT %Fd %F %F %F %FNe>$ e>4 e> e> e> e>N 	) 	 	 	" PRU3d
?%;T#s(^%K LQQQE4S> E E E [E <T#s(^ < < < [<: $t $ $ $ $ [$L  #'""&A> A>A>A> A> 	A>
 4KA> tA> 4ZA> 
A> A> A> \A>F%%'*%<@%	% % % %*M M# MRV M M M M )-	- - -- !	-
 
- - - -^-# -$ - - - -^     $ UW 
 
 
c 
c 
[^ 
 
 
 
G3 GC GD G G G G  $J	
 
   . '+; ; ;; ;
 ; !;  $; 
; ; ; ;2=s =t = = = =~ "3 " " " " " \".) ) ) ) ) ):J J J JX/3 / / / /j    ,  (,*.Z
 Z
Z
 Z
 d38n%	Z

  Z
 Z
 Z
 !Z
 #3-Z
 
c3hZ
 Z
 Z
 Z
J	  (, !*.(,l' l'l' l' d38n%	l'
 l' l' l' !l' l' #3-l' !l' 
c3hl' l' l' l' l' l'rL   r         ?)r  
stop_eventr  c                   ddl m}m}  |            }|                                 s	 |                                rZt          |dd          sIt          |dd          r8 |            s|                     |           k|                    |d           dS n2# t          $ r%}t          
                    d|           Y d}~nd}~ww xY w|                     |           |                                 dS dS )u  Poll for the planned-stop marker and trigger graceful shutdown.

    On Windows, ``asyncio.add_signal_handler`` raises NotImplementedError
    for SIGTERM/SIGINT, so the standard signal-driven shutdown path
    never runs when ``hermes gateway stop`` signals the gateway. The
    consequence is that the drain loop is skipped — in-flight agent
    sessions are killed mid-turn and ``resume_pending`` is never set,
    so the next gateway boot has no idea those sessions need to be
    auto-resumed (issue #33778, v0.13.0 session-resume feature broken
    on native Windows).

    This watcher runs on every platform (cheap, defensive) and bridges
    the gap on Windows by translating a filesystem marker into the
    same shutdown-handler invocation a real SIGTERM would have produced
    on POSIX. The CLI's ``hermes_cli.gateway_windows.stop()`` writes
    the marker via ``write_planned_stop_marker(pid)`` and then waits
    for the gateway PID to exit; this watcher is what makes that
    exit happen cleanly.

    On POSIX this is a no-op safety net — the signal handler always
    races us to consuming the marker file because it fires synchronously
    from the kernel's signal delivery.

    Args:
        stop_event: cleared by start_gateway() during normal shutdown
            to tell the watcher to exit.
        runner: the GatewayRunner instance; we check ``_running`` and
            ``_draining`` to avoid triggering shutdown if the gateway
            is already in one of those states.
        loop: the asyncio event loop the shutdown handler must run on.
        shutdown_handler: same callable that's wired to SIGTERM —
            tolerates a ``None`` signal argument (planned stop case)
            and consumes the marker via
            ``consume_planned_stop_marker_for_self()``.
        poll_interval: seconds between marker checks. 0.5s gives a
            responsive shutdown without burning CPU.
    r   )_get_planned_stop_marker_path planned_stop_marker_targets_selfr'  Fri  Nz#Planned-stop watcher tick error: %s)r  r  r  r  r.   rH   r  call_soon_threadsafert   ru   r;  )	r  runnerrh   shutdown_handlerr  r  r  r	  rD  s	            rC   _run_planned_stop_watcherr  <  s^   Z        0/11K!! $'"	D""$$U;; FJ66, 8799 OOM222
 ))*:DAAA 	D 	D 	DLL>CCCCCCCC	D&&&I !! $' $' $' $' $'s   AB >B 
C!CCrY  ra  c                    ddl m} ddlm}m} ddlm} d}d}	d}
d}t                              d|           d}| 	                                s|	  |d||d	           n2# t          $ r%}t                              d
|           Y d}~nd}~ww xY w|dz  }||	z  dk    rw|ru	 ddlm} |9t           ||          |t          d          }||                    d           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w||z  dk    r	  |d          }|rt                              d|           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w	  |d          }|rt                              d|           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w||
z  dk    r_	  |            \  }}|rt                              d||           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w||z  dk    rU	 ddlm}  |t%          d          d            n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|                     |           | 	                                |t                              d            dS )!a  
    Background thread that ticks the cron scheduler at a regular interval.
    
    Runs inside the gateway process so cronjobs fire automatically without
    needing a separate `hermes cron daemon` or system cron entry.

    When ``adapters`` and ``loop`` are provided, passes them through to the
    cron delivery path so live adapters can be used for E2EE rooms.

    Also refreshes the channel directory every 5 minutes and prunes the
    image/audio/document cache + expired ``hermes debug share`` pastes
    once per hour.
    r   )tick)cleanup_image_cachecleanup_document_cache)_sweep_expired_pastesrY  r  z"Cron ticker started (interval=%ds)F)rp  rQ  rh   synczCron tick error: %sNr   r  z*Channel directory refresh scheduling errorr
  r_  rx  z#Channel directory refresh error: %srE  )max_age_hoursz-Image cache cleanup: removed %d stale file(s)zImage cache cleanup error: %sz0Document cache cleanup: removed %d stale file(s)z Document cache cleanup error: %sz4Paste sweep: deleted %d expired paste(s), %d pendingzPaste sweep error: %s)maybe_run_curatorr  c                 8    t                               d|           S )Nzcurator: %s)ru   r:  )r   s    rC   r   z$_start_cron_ticker.<locals>.<lambda>=  s    6;;}c+J+J rL   )idle_for_seconds
on_summaryzCurator tick error: %szCron ticker stopped)rP  r  ru  r  r  hermes_cli.debugr  ru   r:  r  rt   r;  r6  r  r   r  agent.curatorr  r   r  )r  rQ  rh   ra  	cron_tickr  r  r  IMAGE_CACHE_EVERYCHANNEL_DIR_EVERYPASTE_SWEEP_EVERYCURATOR_EVERY
tick_countr  r  r
  r  deleted	remainingr  s                       rC   _start_cron_tickerr  2=  sH    100000RRRRRRRR666666M
KK4h???J!! B*	3IehTNNNNN 	3 	3 	3LL.22222222	3 	a
))Q..8.GMMMMMM#
 3//994%$P  C
 

2
... G G GBAFFFFFFFFG ))Q..A--B??? ZKK OQXYYY A A A<a@@@@@@@@AD00rBBB ]KK RT[\\\ D D D?CCCCCCCCD ))Q..9%:%:%<%<" KKN    9 9 94a888888889 %**:;;;;;;!!%*5\\JJ      : : :5q99999999: 	)))E !! B*F KK%&&&&&s   A   
B*B

B#AC% %
D/DD!)E 
E:E55E:>)F( (
G2GG$+H 
H?H::H?!I. .
J8JJFr   	verbosityc                   )*K   ddl m}m}m}m}m}m}  |            }	|	|	t          j                    k    r|r ||	          }
t          
                    d|	           	 ddl m}  ||	           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w	  ||	d           n`# t          $ r Y nTt          t           f$ rA t                              d	|	           	 dd
l m}  |             n# t          $ r Y nw xY wY dS w xY wddl m} d}t)          d          D ]%} ||	          sd} nt+          j        d           &t                              d|	           	  ||	d           n$# t          $ r d}Y nt          t           f$ r Y nw xY w|s5t)          d          D ]%} ||	          sd} nt+          j        d           &|s?t                              d|	           	 dd
l m}  |             n# t          $ r Y nw xY wdS  |             	 t1                      dz                      d           n# t          $ r Y nw xY w	 dd
l m}  |             n# t          $ r Y nw xY w	 ddl m}  ||	|
          }|rt          
                    d|           n\# t          $ r Y nPw xY wt7          t1                                }t                              d|	|           t9          d|	 d           dS 	 ddlm}  |d           n# t          $ r Y nw xY wddlm }m!}  |tD          d           |ddl#m$} tJ          j&        tJ          j'        d (                    |tJ          j)                  }tK          j*         |                      }|+                    |           |,                     |d!                     tK          j-                    .                    |           |tK          j-                    j/        k     r&tK          j-                    +                    |           ta          |           *d)d:)*fd"	}*fd#}tc          j2                    }|3                    th                     tk          j6                    tk          j7                    u rtp          j9        tp          j:        fD ]*}	 |;                    |||           # tx          $ r Y 'w xY wt{          tp          d$          r2	 |;                    tp          j>        |           n+# tx          $ r Y nw xY wnt          
                    d%           tk          j?                    }tk          j@        t          |*||fdd&'          }|B                                 ddlC} dd(l mD}!m}m}  |            }"|"4|"t          j                    k    rt                              d)|"           dS  |            st                              d*           dS 	  |!             n5# t          $ r(  |             t                              d+           Y dS w xY w| F                    |           | F                    |           t                       	 dd,lHmI}# tc          j2                    }$|$J                    d|#           d{V  n2# t          $ r%}t                              d-|           Y d}~nd}~ww xY w*B                                 d{V }%|%sdS *jK        r)*jL        r t                              d.*jL                   dS tk          j?                    }&tk          j@        t          |&f*jN        tc          j2                    d/dd01          }'|'B                                 *O                                 d{V  *jP        r)*jL        r t                              d2*jL                   dS |&Q                                 |'R                    d34           |Q                                 |R                    d54           	 dd6lHmS}(  |(             n# t          $ r Y nw xY w*jT        t          *jT                  )r#*jV        st          
                    d7           dS *jW        r)t          
                    d8           t          d9          dS );a7  
    Start the gateway and run until interrupted.
    
    This is the main entry point for running the gateway.
    Returns True if the gateway ran successfully, False if it failed to start.
    A False return causes a non-zero exit code so systemd can auto-restart.
    
    Args:
        config: Optional gateway configuration override.
        replace: If True, kill any existing gateway instance before starting.
                 Useful for systemd services to avoid restart-loop deadlocks
                 when the previous process hasn't fully exited yet.
    r   )acquire_gateway_runtime_lockget_running_pidget_process_start_timer  r  terminate_pidNz<Replacing existing gateway instance (PID %d) with --replace.)write_takeover_markerz#Could not write takeover marker: %sFr  z1Permission denied killing PID %d. Cannot replace.)clear_takeover_marker)_pid_existsrH  Tr  zAOld gateway (PID %d) did not exit after SIGTERM, sending SIGKILL.g      ?zjOld gateway (PID %d) still appears alive after SIGKILL; aborting replacement to avoid a duplicate gateway.zgateway.pidr`  )release_all_scoped_locks)	owner_pidowner_start_timez2Released %d stale scoped lock(s) from old gateway.zAnother gateway instance is already running (PID %d, HERMES_HOME=%s). Use 'hermes gateway restart' to replace it, or 'hermes gateway stop' first.u"   
❌ Gateway already running (PID z).
   Use 'hermes gateway restart' to replace it,
   or 'hermes gateway stop' to kill it first.
   Or use 'hermes gateway run --replace' to auto-replace.
)sync_skills)quiet)setup_logging_safe_stderrr  )ro  r<  )RedactingFormatter)r   r   z#%(levelname)s %(name)s: %(message)sc                    d}	 ddl m}  |            }n2# t          $ r%}t                              d|           Y d }~nd }~ww xY wd}| t
          j        k    rd}nF|sD	 ddl m}  |            }n2# t          $ r%}t                              d|           Y d }~nd }~ww xY w	 ddlm	}m
}m}  ||           }	n4# t          $ r'}
d }	t                              d	|
           Y d }
~
nd }
~
ww xY w|r&t                              d
|	r|	d         nd           nV|r&t                              d|	r|	d         nd           n.dd_        t                              d|	r|	d         nd           |		 t                              d ||	                     n2# t          $ r%}
t                              d|
           Y d }
~
nd }
~
ww xY w	 t          dz  dz  } |||	d         d           n2# t          $ r%}
t                              d|
           Y d }
~
nd }
~
ww xY wt!          j                                                   d S )NFr   ) consume_takeover_marker_for_selfz Takeover marker check failed: %sT)$consume_planned_stop_marker_for_selfz$Planned stop marker check failed: %s)format_context_for_logsnapshot_shutdown_contextspawn_async_diagnosticz$snapshot_shutdown_context failed: %su?   Received %s as a planned --replace takeover — exiting cleanlysignalSIGTERMu9   Received %s as a planned gateway stop — exiting cleanlyzSIGTERM/SIGINTu#   Received %s — initiating shutdownzShutdown context: %sz!format_context_for_log failed: %slogszgateway-shutdown-diag.logr   )timeout_secondsz!spawn_async_diagnostic failed: %s)r  r  rt   ru   r;  r  SIGINTr  r"  r  r  r  r:  rp  rv   rY  rz  r  rh  )received_signalplanned_takeoverr  r  planned_stopr  r  r  r  _shutdown_ctxrD  	_diag_logrp  r  s               rC   shutdown_signal_handlerz.start_gateway.<locals>.shutdown_signal_handlerN>  s    !	@GGGGGG??AA 	@ 	@ 	@LL;Q????????	@ fm++LL! 	HHOOOOOOCCEE H H HCQGGGGGGGGH		E         
 65oFFMM 	E 	E 	E MLL?DDDDDDDD	E  	KKQ+8Gh''i     	KKK+8Nh''>N   
 *.& 15F-KK5+8Nh''>N   $F*,B,B=,Q,Q     F F F@"EEEEEEEEFF(614OO	&&}X6      F F F@"EEEEEEEEFFKKMM*****st    
AA  A A1 1
B ;BB $B: :
C+C&&C+/$F 
GF>>G!G) )
H3HHc                  6                          dd           d S )NFTr  )r  )r  s   rC   restart_signal_handlerz-start_gateway.<locals>.restart_signal_handler>  s"    4@@@@@rL   SIGUSR1z6Skipping signal handlers (not running in main thread).zplanned-stop-watcherr  )write_pid_filer  r  z^Another gateway instance (PID %d) started during our startup. Exiting to avoid double-running.zBGateway runtime lock is already held by another instance. Exiting.z8PID file race lost to another gateway instance. Exiting.)r
  zMCP tool discovery failed: %szGateway exiting cleanly: %s)rQ  rh   zcron-ticker)rY  r$  rW  r  rf   z Gateway exiting with failure: %sr  rx  r  )r
  z}Exiting with code 1 (signal-initiated shutdown without restart request) so systemd Restart=on-failure can revive the gateway.z_Exiting with code 75 (service-restart requested) so the service manager relaunches the gateway.K   r  )Xr  r  r  r  r  r  r  r%   r  ru   r:  r  rt   r;  ProcessLookupErrorPermissionErrorr*   ru  r  r  r  r   r<  rv   rf  rb  r  r+   printtools.skills_syncr  hermes_loggingr  r  rY  r  r  r>  WARNINGINFOr'   DEBUGStreamHandlersetLevelsetFormatterr?  
addHandlerlevelr   rz  r6  set_exception_handlerrz   r{  current_threadmain_threadr  r
  r  add_signal_handlerNotImplementedErrorrr   r  rk  r  r  r_  atexitr  FileExistsErrorr
  rD   r
  r
  r	  r  r  r  rQ  r  r   r)   r5   r
  r  
SystemExitr(  r+  )+r  r   r  r  r  r  r  r  r  existing_pidexisting_start_timer  r  r  r  old_gateway_exitedr  r  	_releasedro  r  r  r  r  _stderr_level_stderr_handlerr  r  rh   sig_planned_stop_watcher_stop_planned_stop_watcher_threadr(  r  _current_pidr
  _loopr(  	cron_stopcron_threadr
  rp  r  s+                                            @@rC   start_gatewayr8  =  s!     &                #?$$LLBIKK$?$? w	"8"8"F"FKKN  G@@@@@@%%l3333 G G GBAFFFFFFFFGl%88888%   #W-   G   DDDDDD))++++    Duu  322222!&2YY (! (!"{<00 )-&E
3 W   !M,d;;;;;) . . .)-&&&'1   D * )"2YY ) )*{<88 "15.!E
4(((() !LLM$  
HHHHHH--////$    5O ""]2::d:KKKK   @@@@@@%%''''   
	CCCCCC44*%8  	  aKK TV_```    o//00KLL^k  
 Ol O O O   5111111$    ;:::::::Ml;;;; 333333#O==AA)W][[!/??  ///$$%7%78]%^%^___&&7777,..444((7776""F "']+ ]+ ]+ ]+ ]+ ]+ ]+~A A A A A #%%D 	>???!!Y%:%<%<<<M6>2 	 	C''-DcJJJJ&   69%% 	''8NOOOO&   	 	LMMM "+!2!2#,#3((&$8OP#	$ $ $  !&&((( MMMOOOOOOOOOO"?$$LLBIKK$?$?/0<	
 	
 	
 u'')) P	
 	
 	
 u   $$&&&F	
 	
 	
 uu OOO$$$
OO0111(***9555555(**##D*<========== 9 9 94a888888889 LLNN""""""G u!  	LLL68JKKKt !!I"!\"OW5M5O5OPP  K  
"
"
$
$$$$$$$$&  	QLL;V=OPPPu MMOOOQ ""$$$ %%a%000777777    #)*** " &*C M	
 	
 	
 u " .	
 	
 	
 nn4s&  $A6 6
B% B  B%)B7 7
D+D/D ?D 
D
DDDD0E> >FFF7H 
HH%%I 
III- -
I:9I:>0J/ /
J<;J<L 
L,+L,
S""
S/.S/ T) )
T65T6
X .YY
5[   
[/
[**[/>a 
aac                  F   	 ddl m}   |              n# t          $ r Y nw xY wddl}|                    d          }|                    ddd	           |                    d
ddd           |                                }d}|j        r]ddl}t          |j        d          5 }|
                    |          pi }t          j        |          }ddd           n# 1 swxY w Y   t          j        t          |                    }|st!          j        d           dS dS )z CLI entry point for the gateway.r   )configure_windows_stdioNz)Hermes Gateway - Multi-platform messaging)rO  z--configr  zPath to gateway config file)rr  z	--verbosert  
store_truezVerbose output)rQ  rr  rs  rt  r   )hermes_cli.stdior:  rt   argparseArgumentParseradd_argument
parse_argsr  r|  r}  r~  r  r 
  rz  r  r8  r    exit)	r:  r=  parserr$  r  r|  r  r  r(  s	            rC   r  r  [?  s   <<<<<<!!!!    OOO$$1\$]]F

D/LMMM
T,EUVVVDF{ 3$+000 	3A>>!$$*D",T22F	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 k-//00G  s    
  %,CC!$C!__main__r  )r   Nr  )r   r  r   N)NNrY  )NFr   (  r  hermes_bootstrapModuleNotFoundErrorrz  r;  rv  r,  r>  r%   r   r  r1   r    r  r
  r{  r   sqlite3collectionsr   contextvarsr   r@
  r   r   typingr   r	   r
   r   r   agent.account_usager   r   agent.async_utilsr   
agent.i18nr   r  r   hermes_cli.fallback_configr   r  r  r  r  r  r   r	  DOTALLr   _GATEWAY_PROVIDER_ERROR_REr   r   r   r   rD   r+   rK   r  r   rg   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rS  r  r   r   r  r  r   r  r  r  r+  r.  r/  r   r)   r;  rK  rS  rV  rZ  r\  r^  rc  r&   r/   r0   r"   r$   r  rf  utilsrg  rh  ri  rj  rY  dotenvrk  hermes_cli.env_loaderrl  	_env_pathr#   r{  r  r  r  _config_pathr.   r|  r  r}  r>  r~  r  rw  r   _key_valr   r'   _terminal_cfgr6   _terminal_env_map_cfg_key_env_varr  r  r  _auxiliary_cfg_aux_bridged_keysrc  r  r  r-   rt   	_task_key	_task_cfgrI   _provru  rv  r  rQ  _upper
_agent_cfgr}  _tz_cfg_security_cfg_redactr,   _gateway_cfg_strict_allow_dirs_allow_dirs_strr4   r5   _trust_recent_trust_recent_seconds_bridge_errr  r^   r_   rw  r  dir_network_cfg_bootstrap_excr  r  _configured_cwdr9  r[  	_fallbackrs  r  r  r  r  r   r  rt  r  r  r  r  r  r  r  gateway.deliveryr	  gateway.authz_mixinr
  gateway.kanban_watchersr  gateway.slash_commandsr  ru  r  r  r  r  r  r  gateway.restartr  r  r  gateway.whatsapp_identityr  _canonical_whatsapp_identifierr  _expand_whatsapp_auth_aliasesr  _normalize_whatsapp_identifierr?  ru   objectr  r2  r)  rP  rV  rc  r  r  r  r  ru   _INTERRUPT_REASON_SSE_DISCONNECTr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  weakrefrS  r  rT  r  r  r  r  r   rk  AbstractEventLoopr  r  r8  r  r   rL   rC   <module>r~     s    "	 	 	 	
 	D	         				 				   



         # # # # # # $ $ $ $ $ $             3 3 3 3 3 3 3 3 3 3 3 3 3 3 P O O O O O O O 6 6 6 6 6 6       % % % % % % 9 9 9 9 9 9  # )- &+. ()rz*TUU &BJ	 MBI!  & (RZ		 M   )bj
	 M   $]M  
 $>M   BJ788BJ122BJ344BJ)**BJ/00BJ9:: 3 3 3 3lKc Kc K K K K
)] )t ) ) ) )X ,
% ,04S#X ,	 ,  ,  ,  ,Fc c         ( $.2:		 M$ $  ?S ?T ? ? ? ?.s # #    $c s S U]^aUb     :# : : : :
C 
C 
C< <s <s < < < <L )0 %S Xe_    D< < < < <&S 5 U    $  #'	) ) )) 
%) %	)
 
) ) ) )r- %S/   c C d38n cSVh    B ,M (!a  %O !_(3- _D _ _ _ _" %)7+ 7+ 7+$sCx.!7+ SM7+ 4S#X#./	7+ 7+ 7+ 7+t PXY\P] be    4d38n1E(F 3    :! ! !   N   M  )-N+ N+4S>"N+N+ "#N+ 49d?	N+ N+ N+ N+h1 1 1 1f3 3 3 3 3 3> > > > > >
<t < < < <
2D 2 2 2 29t 9 9 9 9
A A A A !$
       33ttH~~,344 5 5 5 - , , , , , ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^         4 4 4 4 4 46!	  |h9O9O9Q9Q9YZ[9\_e9e f f f fJ J J J< $$dee (1:'> $ m+ x
w
T,111 	-R"5?2&&,"D	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	766666%%**,, 	- 	-JD$z$c5$ 788 -T=S=S#&3t99
4  R00 ,	9ZZt<< ,	9!>!~! -! #$?	!
  7! %&C! $%A! 5!  !9! /! /! /! -!  !9! #$?!  !";!!" '(G#!$ #<31Y+M3](G5$?3! ! !6 '8&=&=&?&? 9 9"(},,(2D
  5((SSYY:N-N-N   5((ZZc-B-B(!w11$77!z$t55 9/9tz$/?/?
8,,/2s4yy
8, +r22 	Ijj>> 	I !F E EIIIIII88:: 9 9F%))&-88889    
 / I I	*..y"==	!z)T22 IMM*b99::@@BBY]]7B7788>>@@C	j" = =>>DDFF	3y}}Y;;<<BBDD"** GUf__AFBJ=F===> E>DBJ:F:::; KAJBJ=F===> I@HBJ<F<<<= XXgr**
 	**Z66 	j((69c*[:Q6R6R
23 J..58SDU9V5W5W
12(J66=@SLeAf=g=g
9:(J66=@SLeAf=g=g
9:&*44=@SLcAd=e=e
9:0J>>?Bs@A@ @
;< xx	2.. 	fJJ|T:: 	f L00?Bs<PaCb?c?c
;<<//>Ac,O_B`>a>a
:;!\11@CLQcDd@e@e
<=((:r** 	<zz'3// 	<,3MMOOBJ()R00:mT** 	K#''(899G"69c'll6H6H6J6J
23xx	2..:lD)) 	]"&&x00G""+CC 
9: '**+FGGK L:k3// )&1OOZdE];; )&(joo5W5Wk5W5W5W&W&WOO&(O" L<KBJ89(,,-ABBM((1CCc 
<= %1$4$45Q$R$R!$0BE#F[B\B\
>? 
 
 
 	;tK  ); ;-8; ;	
 	
 	
 	

 	L	
 	
 	
 	
 	
 	
 	
 	
 	

(_666666"cceeOODD88BGGLz,%% *,*:*:<*H*H *D)))) _ _ _	E
LN
L
LSVS]^^^^^^^^^_U777777 U U U	E
B.
B
BTTTTTTTTTUU>>>>>>  """" U U U	E
B.
B
BTTTTTTTTTU !
>  !$
  *..44 +/-AAA	/**>cc)$)++.>.>I!*BJ~                                 , + + + + + 9 9 9 9 9 9 > > > > > > < < < < < <                                  
	8	$	$ !&(( Ct C C C CL3t 3 3 3 3ls    ,s  C TW    6#e # # # # #'c 'hsm ' ' ' 'T4 49L 4 4 4 4 * 3 > #<  %< "$8 !'i$$&&%%''!''))(..00*0022)//11	 	 58C= 5T 5 5 5 5/4 /E#*cDj:P4Q / / / /d=3 =3: = = = =@C: C# C C C C
F F F F Fd    ::d : : : :( 4$; #     Xd3i0    6C M    4d |    <    $0L X\ 0 0 0 	+ + +++ 	+
 	+ + + +\$ 4    ( 
   @3
 3
 3
 3
l]T ]T ]T ]T ]T-/IKd ]T ]T ]TLi V' V' V'V' 
#V' V' 
V' V' V' V'r\' \'9? \'X[ \' \' \' \'~G G 7 G Gbjknbo Gx| G G G GT  @ zDFFFFF s   
 i1 Q5)i1 5Q99i1 <Q9=Fi1 .X5 4i1 5X=:i1 <X==P3i1 1j868j33j8<Al l8l33l8<m m1m,,m15n n*n%%n*