
    i*             	          U d 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Z"dZ#dZ$ ej%        d          Z&de'dede'fdZ(dZ)dedee*         fdZ+de*fdZ,de'de*de*fdZ-ddddedee*         dee*         de.fdZ/deeee'ef                           defdZ0dd Z1d!e'de'fd"Z2d!e'de'fd#Z3de.fd$Z4d%ej5        d&<    e1             e	j6        7                    d e' ee8          j9        j9                             dd'l:m;Z; dd(l<m=Z=m>Z>m?Z?m@Z@  e;            ZAdd)lBmCZC dd*lDmEZE eAd+z  ZF eEeA ee8          G                                jH        d,         d+z  -           dd.ZI ej%        d/          ZJd0d1hZKeAd2z  ZLeLM                                r8	 ddlNZO ePeLd34          5 ZQ eOjR        eQ          pi ZSddd           n# 1 swxY w Y   dd5l mTZT  eTeS          ZSeSU                                D ]1\  ZVZW eXeWe'eYe*e.f          reVej5        vr e'eW          ej5        eV<   2eSZ                    d6i           Z[e[r eXe[e\          ri d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_Z]e]U                                D ]\  Z^Z_e^e[v re[e^         ZWe^d9k    r e'eW          d`v r%e^d9k    r& eXeWe'          rej6        `                    eW          ZW eXeWea          r ejb        eW          ej5        e_<   v e'eW          ej5        e_<   eSZ                    dai           Zcecrs eXece\          rfdbdcdddedfdgdhdidjdfdkdldmdndfdoZdedU                                D ];\  ZeZfecZ                    eei           Zg eXege\          s) e'egZ                    dpdq                    h                                Zi e'egZ                    drdq                    h                                Zj e'egZ                    dsdq                    h                                Zk e'egZ                    dtdq                    h                                Zleireiduk    reiej5        efdp         <   ejrejej5        efdr         <   ekrekej5        efds         <   elrelej5        efdt         <   =eSZ                    dvi           Zmemr eXeme\          rdwemv r e'emdw                   ej5        dx<   dyemv r e'emdy                   ej5        dz<   d{emv r e'emd{                   ej5        d|<   d}emv r e'emd}                   ej5        d~<   demv r e'emd                   ej5        d<   demv r e'emd                   ej5        d<   eSZ                    di           ZnenrF eXene\          r:denv r e'end                   ej5        d<   denv r e'end                   ej5        d<   eSZ                    ddq          Zoeor( eXeoe'          reoh                                ej5        d<   eSZ                    di           Zp eXepe\          r<epZ                    d          Zqeq% e'eq          r                                ej5        d<   nJ# es$ rBZt eud evet          jw         det e	jx                    eude	jx                   Y dZt[tndZt[tww xY w	 ddl:myZy d ez            v reSni Z                    di           Z{ eXe{e\          r!e{Z                    d          r eyd           n'# es$ rZ| eude| e	jx                   Y dZ|[|ndZ|[|ww xY w	 ddl m}Z}  e}             n'# es$ rZ| eude| e	jx                   Y dZ|[|ndZ|[|ww xY w	 ddl m~Z~  e~             n'# es$ rZ| eude| e	jx                   Y dZ|[|ndZ|[|ww xY wd%ej5        d<   d%ej5        d<   ej5        Z                    d:dq          Zered`v r2 ej        d          p e' ej                              Zeej5        d:<   ddlmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZ ddlmZmZmZ ddlmZmZmZ  ej        ew          Z e            Zde\fdZde\dz  fdZde'fdZde'dedz  fdZdZdZdZdZdZdZ eer                                er                                er                                er                                er                                er                                h          Zdee'         de.fdZdedee'dz  e'dz  f         fdZde'de'dz  fdZddde'fdZde\fdZdde\dz  de'fdZdeeae'                  fdZde'ddfdZde\ddfdZddlZd aej        ed<   ddÜde\de'deYde'fdǄZde\de.fdȄZ G dɄ dʦ          Zddej        deYfd΄Zddee         de.deeY         de.fd҄Zdӄ Zewdk    r eʦ             dS dS )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)t)cfg_get         @      >@z'(?<![\w:/])/([A-Za-z0-9][A-Za-z0-9_-]*)textplatformreturnc                     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.
    valuetelegramr   )_sanitize_telegram_namematchr   c                 x     |                      d                    }|rd| n|                      d          S )N   /r   )group)r   	sanitizedr   s     0/home/piyush/.hermes/hermes-agent/gateway/run.py_replacez/_telegramize_command_mentions.<locals>._replaceE   s<    ++EKKNN;;	"+?9Q?    )getattrhermes_cli.commandsr   reMatchstr_TELEGRAM_COMMAND_MENTION_REsub)r   r   platform_valuer#   r   s       @r"   _telegramize_command_mentionsr-   8   s     Xw99N##;;;;;;@ @# @ @ @ @ @ @ (++Hd;;;r$     r   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   	timestampboolintfloatr)   strip
ValueErrorfromisoformatreplace)r   r   numerics      r"   _coerce_gateway_timestampr;   a   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_FRESHNESSN )osenvirongetr5   %_AUTO_CONTINUE_FRESHNESS_SECS_DEFAULT	TypeErrorr7   )raws    r"   _auto_continue_freshness_windowrE      sz     *..9
:
:C
{cRii:;;;<Szzz" < < <:;;;;;<s   A %A43A4name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.
    Nr>   )r?   r@   rA   r5   rC   r7   )rF   rG   rD   s      r"   
_float_envrI      ss     *..

C
{cRiiW~~Szzz"   W~~s   A  A*)A*)nowwindow_secsrJ   rK   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)r5   rB   r;   time)r   rJ   rK   windowr2   currents         r"   _is_fresh_gateway_interruptionrP      sv    " " 	k899 
 {{t)%00It [dikkkcGY&((r$   historyc                     | 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.
    Nrole)session_metasystemr2   )reversedr1   dictrA   )rQ   msgrS   tss       r"   _last_transcript_timestamprZ      s      t    #t$$ 	wwv 	t999WW[!!>III tt4r$   c                     dt           j        v rdS 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 )zBSet SSL_CERT_FILE if the system doesn't expose CA certs to Python.SSL_CERT_FILENr   )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@   sslget_default_verify_pathscafileopenssl_cafilepathexistscertifiwhereImportError)r]   paths	candidaterc   s       r"   _ensure_ssl_certsrh      s   "*$$JJJ ((**ElE$89  	 		22 	*3BJ'FF&-mmoo
?#   	  	 7>>)$$ 	*3BJ'FF	 s   .%B 
B"!B"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.schedulerrk   upper)ri   rk   resolveds      r"   _home_target_env_varrp     sN     544444$$]33H !!##2222r$   c                 &    t          |            dS )zDReturn the optional thread/topic env var for a platform home target.
_THREAD_ID)rp   )ri   s    r"   _home_thread_env_varrs     s    "=11====r$   c                  :    t           dz                                  S )zIReturn True when a /restart completion marker is waiting to be delivered..restart_notify.json)_hermes_homerb    r$   r"   _restart_notification_pendingrx     s    1199;;;r$   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   r   r   config.yamlNr   utf-8encoding_expand_env_varsagent	max_turnsHERMES_MAX_ITERATIONS)r   rv   r   __file__resolveparentsrb   yamlopen	safe_loadhermes_cli.configr   	ExceptionrA   r1   rW   r)   r?   r@   )config_path_yamlfcfgr   	agent_cfgs         r"   /_reload_runtime_env_preserving_config_authorityr   3  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/outputsr   r   r   r   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vercel_runtimeTERMINAL_VERCEL_RUNTIME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TERMINAL_CONTAINER_PERSISTENTTERMINAL_DOCKER_VOLUMES&TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE TERMINAL_DOCKER_RUN_AS_HOST_USERTERMINAL_SANDBOX_DIRTERMINAL_PERSISTENT_SHELL)container_persistentdocker_volumesdocker_mount_cwd_to_workspacedocker_run_as_host_usersandbox_dirpersistent_shell).autor   	auxiliaryAUXILIARY_VISION_PROVIDERAUXILIARY_VISION_MODELAUXILIARY_VISION_BASE_URLAUXILIARY_VISION_API_KEY)providermodelbase_urlapi_keyAUXILIARY_WEB_EXTRACT_PROVIDERAUXILIARY_WEB_EXTRACT_MODELAUXILIARY_WEB_EXTRACT_BASE_URLAUXILIARY_WEB_EXTRACT_API_KEYAUXILIARY_APPROVAL_PROVIDERAUXILIARY_APPROVAL_MODELAUXILIARY_APPROVAL_BASE_URLAUXILIARY_APPROVAL_API_KEY)visionweb_extractapprovalr   r>   r   r   r   r   r   r   r   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_ack_enabledHERMES_GATEWAY_BUSY_ACK_ENABLEDtimezoneHERMES_TIMEZONEsecurityredact_secretsHERMES_REDACT_SECRETSu.     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)BasePlatformAdapterEphemeralReplyMessageEventMessageType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} ddlm} 	  | t          j        d                    }n~# |$ rP}t                              d|           t                      }||cY d}~S t           ||                    |d}~wt          $ r}t           ||                    |d}~ww xY w|                    d          |                    d	          |                    d
          |                    d          |                    d          t          |                    d          pg           |                    d          dS )zResolve provider credentials for gateway-created AIAgent instances.

    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)	AuthErrorHERMES_INFERENCE_PROVIDER)	requestedu4   Primary provider auth failed: %s — trying fallbackNr   r   r   api_modecommandargscredential_poolr   r   r   r"  r#  r$  r%  )hermes_cli.runtime_providerr  r  hermes_cli.authr  r?   getenvloggerwarning_try_resolve_fallback_providerRuntimeErrorr   rA   list)r  r  r  runtimeauth_exc	fb_configexcs          r"   _resolve_runtime_agent_kwargsr3  L  s           *)))))H**i ;<<
 
 
  R R R 	MxXXX244	 88BBCCQ H H H88==>>CGH ;;y))KK
++KK
++KK
++;;y))W[[((.B//";;'899  s,   / B*,A? B*&A??B*B%%B*c                     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   |                    d          p|                    d          }|sdS t          |t                    r|n|g}|D ]}t          |t                    s	  | |                    d	          |                    d
          |                    d                    }t                              d|                    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  Nr   r   r   fallback_providersfallback_modelr   r   r   )r!  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   rv   rb   r   r   rA   r1   r.  rW   r*  infor   debug)
r  _ycfg_path_fr   fbfb_listentryr/  fb_excs
             r"   r,  r,  s  s   DDDDDD(-/   	4(W--- 	),,r""(bC	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)WW)**Gcgg6F.G.G 	4"2t,,6""2$ 	 	EeT** 22#ii
33&+ii
&;&;%*YYy%9%9  
 =KK
++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[[[1	6    4sj   "I I A!I !A%%I (A%)/I 5I D:H
I 
I/II II 
I"!I"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_typesr>   image/message_typez[User sent an image: ]audio/z[User sent audio: z[User sent a file: 
)r%   	enumeratelen
startswithr  PHOTOappendjoin)eventpartsrC  rD  iurlmtypes          r"   _build_media_placeholderrU    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r$   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)adapterrV  s     r"   _dequeue_pending_eventrZ    s     &&{333r$   zStop requestedzSession reset requestedz Execution timed out (inactivity)zSSE client disconnectedzGateway shutting downzGateway restartingmessagec                     | sdS d                     t          |                                                                                                                     }|t
          v S )z?Return True when an interrupt message is internal control flow.F )rO  r)   r6   splitlower_CONTROL_INTERRUPT_MESSAGES)r[  
normalizeds     r"   _is_control_interrupt_messagerb    sV     u#g,,,,..446677==??J444r$   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.
    r   r9   )r   errorsNNz---z
---   r   Nzname::r      )"'r]  -_z
[^a-z0-9-]r>   z-{2,})	read_textr   rL  find
splitlinesr6   r^  rK  r_  r9   r'   r+   )rc  contentenddeclared_namelinerD   slug_res           r"   _skill_slug_from_frontmatterrx    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                 f   |                                                      dd          }	 ddlm} ddlm}  |            } |            D ]y}|                                s|                    d          D ]L}t          d |j	        D                       r!t          |          \  }}|r|s8||k    r||v r
d|  d	c c S Mzdd
lm}	 t          t                                                    j        j        }
 |	|
dz            }|                                r|                    d          D ]n}t          |          \  }}|s||k    rQ|j                            |          }t%          |j	                  }dd                    |           }d|  d| dc S on# 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.
    rn  rm  r   )_get_disabled_skill_names)get_all_skills_dirszSKILL.mdc              3      K   | ]}|d v V  	dS )).gitz.githubz.hubz.archiveNrw   .0parts     r"   	<genexpr>z+_check_unavailable_skill.<locals>.<genexpr>)  s(      bb4tFFbbbbbbr$   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_  r9   tools.skills_toolr{  agent.skill_utilsr|  rb   rglobanyrQ  rx  hermes_constantsr  r   r   r   parentrelative_tor.  rO  r   )ry  ra  r{  r|  disabled
skills_dirrc  rv  rt  r  	repo_rootoptional_dir	_declaredrelrQ  install_paths                   r"   _check_unavailable_skillr    s    ##%%--c377J*??????999999,,.. .-// 	 	J$$&& &,,Z88  bbS[Sabbbbb &B8&L&L#m =  :%%-8*C*CB B B B     	=<<<<<NN**,,3:	..y;L/LMM   	(..z::  ">x"H"Hi :%%"/55lCCC OOE#@sxx#@#@LS S SCOS S S   &    4s   BF! C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  LOCALr   r   s    r"   _platform_config_keyr  M  s    ..55HNBr$   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.
    r   r   )get_config_pathread_raw_configNrr   r   z%Could not load gateway config from %s)rv   r   r  r  r   rb   r   r   r   r*  r:  )r   r  r  r   r   s        r"   _load_gateway_configr  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 configc                    | | 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   rG   r>   )r  rA   r1   r)   rW   )r  r   	model_cfgs      r"   _resolve_gateway_modelr  o  s     &&&,@,B,BC$$I)S!! H	It	$	$ H}}Y''G9==+A+AGRG2r$   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_specsys
executabler   )r  
hermes_bin	importlibs      r"   _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.
    rh     r   r   r   mainri  rg     )r   	chat_typechat_id)dmthread	thread_idN)r^  rK  )rV  rQ  results      r"   _parse_session_keyr    s     c""E
5zzQ58w..58v3E3EaqQx
 

 u::>>eAh*:::"'(F;4r$   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.type
completion
session_idunknownr#  watch_disabledz[IMPORTANT: r[  r>   rG  watch_matchpattern?output
suppressedr   [IMPORTANT: Background process z matched watch pattern "z".
Command: z
Matched output:
z
(z/ earlier matches were suppressed by rate limit)NrA   )r  evt_type_sid_cmd_pat_out_supr   s           r"   $_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4r$   c                      d S Nrw   rw   r$   r"   <lambda>r    s    D r$   _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.
    failederrorunknown errorc              3       K   | ]}|v V  	d S r  rw   )r  p	error_strs     r"   r  z2_normalize_empty_agent_response.<locals>.<genexpr>  s;       !
 !
 N!
 !
 !
 !
 !
 !
r$   )contexttokenz	too largeztoo longexceedpayload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.)rA   r)   r_  r  r4   )r  r  r  error_detailis_context_failurer  errr  s          @r"   _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	
 	

 Or$   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  r  	completedT)r1   rW   rA   )r  s    r"   '_should_clear_resume_pending_after_turnr    s     lD)) u&& u!! \%5%5i%@%@ LDTDTU\D]D] u$$--u4r$   c                      e Zd ZU dZi Zeeef         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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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*e+defd$            Z,e+defd%            Z-e+dee         fd&            Z.e+dee         fd'            Z/d(e0defd)Z1d(e0defd*Z2 e3d+d,h          Z4d(e0defd-Z5d(e0defd.Z6d/Z7d(e0defd0Z8defd1Z9defd2Z:d(e0dee         fd3Z;d(e0ddfd4Z<dddd5d(ee0         d6ee         d7ee=         de>ee=f         fd8Z?d9ed:ed;e=de=fd<Z@d=eAddfd>ZBd?eddfd@ZCdefdAZDdefdBZEdefdCZFdefdDZGd6edEdFd=eddfdGZHd6ed=edHedF         dedF         fdIZIddJd6ed=edefdKZJeKdLedefdM            ZLd6ed=edefdNZMdOedefdPZNd@dQee         dRee         ddfdSZOddddTdedUee         dVee         dWee         ddf
dXZPeKdeQeeef                  fdY            ZReKdefdZ            ZSeKde=dz  fd[            ZTeKd\ede>eef         fd]            ZUddd^d(ee0         d6ee         de=dz  fd_ZVd6ed`ee=         ddfdaZWeKdedz  fdb            ZXeKdefdc            ZYeKdefdd            ZZeKdefde            Z[eKdefdf            Z\eKde=fdg            Z]eKde^e=z  dz  fdh            Z_deeef         fdiZ`d6edjeaddfdkZbdjead6edefdlZcdmede>eeef         ef         fdnZdd?eddfdoZed?dpZfdqeeef         ddfdrZgdseddfdtZhduZidvZjdwekddfdxZldefdyZmd6eddfdzZnd?d{Zoddd|d}ed~edefdZp e3h d          ZqdefdZrdefdZsdAdefdZtdBdeddfdZu	 d>de=dedee         ddfdZvd>de=dee         ddfdZwd?dZxd?dZydddddedededdfdZzd?dZ{de!dedeeA         fdZ|d(e0defdZ}dee!         defdZ~deddfdZdjeadee         fdZdjead(e0deQeeef                  dee         fdZd6edeQe         fdZd6eddfdZd6efdZdedefdZdefdZdjeadeeef         fdZdjeadefdZdjeadefdZdjeadefdZdjeadefdZdjeadeeef         fdZdjeadeeef         fdZdjeadefdZdjeadefdZdjeadefdZdjeadee         fdZdjeadefdZdjeadefdZdefdZdCdZdjdFdefdZd(ededdfdZd(ededdfdZded(ededdfdZdjeadefdZdjeadefdZeKdjeadee         fd            ZdjeadefdZdjeadefdZdjeadefdZdeddfdZdedededefdĄZdededefdńZ	 dDdjeadede^dedef
dɄZdjeadeddfd˄Zdedjeaddfd̄Zdjeadefd̈́Zdjeadefd΄Zded(ddeddfd҄ZdjeadefdӄZdjeadefdԄZdjeadeeef         fdՄZdjeadefdքZdjeadefdׄZdjeadefd؄Zd(e0de=fdلZd(e0ddfdڄZd(e0ddfdۄZdedefd݄Zd(e0dOededdfdބZd(e0dOededdfd߄ZdZd(e0defdZdefdZd(e0defdZdEdjeadedefdZd(e0defdZdjeadedefdZdjeadefdZdjeadefdZdjeadefdZdjeadefdZdjeadefdZdjeadee         fdZdjeadefdZdjeadefdZdjeadedededee         f
dZdeeef         fdZdeeeef                  fdZdZdjeadee         fdZdjeadefdZ e3e!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        e!j        e!j        e!j        h          ZdjeadefdZdjeadefdZd?dZ	 	 	 dFdededmeddfdZdefd Zdee>eeee         f                  fdZdddeeke>eeee         f                           deke>eeee         f                  fdZdede^fdZde^ddfdZd	 Zdefd
ZdedeQe         defdZdedeQe         defdZde=fdZdede=ddfdZde=ddfdZduZdZe>ed<   ed7e=dz  de=fd            ZeK	 d>d:ede=de^dede=dz  defd            Zd6ed:ed;e=de>fdZd6ededefd Zdd!d6edee         defd"Zd6eddfd#Zd6edefd$Zd+d%d6ed?edefd&Zd6ed'edefd(Zd=ed6ed'edz  ddfd)Zd*d+d6ed(e0d,ed-ed.eddfd/Zd6eddfd0ZeKdsed1eddfd2            Zdseddfd3Z d?d4Zdefd5Zdee         fd6Z	 	 	 dGded7edeQeeef                  d(ddOed6edee         d8ee         deeef         fd9Z	 	 	 	 	 dHded7edeQeeef                  d(e0dOed6edee         d;ed8ee         d<ee         deeef         fd=ZdS (I  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_restart_drain_timeoutN
_exit_codeF	_draining_restart_requested_restart_task_started_restart_detached_restart_via_service
_stop_task_session_model_overrides_session_reasoning_overridesr  c                 
   |pt                      | _        i | _        |                                  t	          j        |           a|                                 | _        | 	                                | _
        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        ddlm t7          | j        j        | j        fd          | _        t=          | j                  | _        d| _         d | _!        tE          j#                    | _$        d| _%        d| _&        d | _'        d | _(        d| _)        d| _*        d| _+        d| _,        d| _-        d | _.        i | _/        i | _0        i | _1        i | _2        i | _3        i | _4        i | _5        tm                      | _7        d| _8        dd l9}tm                      | _:        |;                                | _<        i | _=        i | _>        i | _?        i | _@        i | _A        dd lB}|C                    d          | _D        	 ddlEmF}  |d	           n# t          $ r Y nw xY wd | _H        	 dd
lImJ}  |            | _H        n2# t          $ r%}t          L                    d|           Y d }~nd }~ww xY w| jH        	 ddlMmN}  |            O                    d          pi }|O                    dd          r| jH        P                    t          |O                    dd                    t          |O                    dd                    t          |O                    dd                    | j        j                   n2# t          $ r%}	t          L                    d|	           Y d }	~	nd }	~	ww xY w	 ddlMmN}  |            O                    d          pi }
|
O                    dd          rddlSmT}  |t          |
O                    dd                    t          |
O                    dd                    t          |
O                    dd                    t          |
O                    dd                               n2# t          $ r%}	t          L                    d|	           Y d }	~	nd }	~	ww xY wddlUmV}  |            | _W        dd lXmY}  |            | _Z        | [                                | _\        i | _]        t                      | __        d S )!Nr   process_registryc                 .                         |           S r  )has_active_for_session)keyr  s    r"   r  z(GatewayRunner.__init__.<locals>.<lambda>F  s    0@0W0WX[0\0\ r$   )has_active_processes_fnF   r   )ensure_installed)log_failures	SessionDBz&SQLite session store not available: %sload_configsessions
auto_pruneretention_daysZ   min_interval_hours   vacuum_after_pruneT)r  r  vacuumsessions_dirz%state.db auto-maintenance skipped: %scheckpoints)maybe_auto_prune_checkpoints   delete_orphansmax_total_size_mb  )r  r  r  r  z'checkpoint auto-maintenance skipped: %s)PairingStore)HookRegistry)`r  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_restart_drain_timeoutr  _load_provider_routing_provider_routing_load_fallback_model_fallback_modeltools.process_registryr  r	  r  session_storer  delivery_router_running_gateway_loopasyncioEvent_shutdown_event_exit_cleanly_exit_with_failure_exit_reasonr  r  r  r  r  r  r  _running_agentsr  _pending_messages_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   _pending_approvals_failed_platforms_update_prompt_pending	itertoolscount_slash_confirm_countertools.tirith_securityr	  r   _session_dbhermes_stater  r*  r:  r   r  rA   maybe_auto_prune_and_vacuumr4   r3   tools.checkpoint_managerr  gateway.pairingr  pairing_storegateway.hooksr  hooks_load_voice_modes_voice_mode_recent_voice_transcriptsset_background_tasks)selfr  
_threading
_itertoolsr	  r  e_load_full_config	_sess_cfgr2  	_ckpt_cfgr  r  r  r  s                 @r"   __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:: $ : : < <&*&F&F&H&H#!%!<!<!>!>#88:: 	<;;;;;)K$dk$\$\$\$\
 
 
  .dk::BF&}""'+/)-"'%*"!&$)!26 024613 >@LN3.079$ DO==$'! 	'&&&7B}}!+!2!2 DF% HJ) >@ BD 8:# 	'&&&&0&6&6q&9&9#	>>>>>>%00000 	 	 	D	  	F......(y{{D 	F 	F 	FLLA1EEEEEEEE	F '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s\   J+ +
J87J8K 
L#LLCO 
P	$PP	CS" "
T,TTr   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   r>   dockerNc                 \    g | ])}|t           j        t           j        t           j        hv'|*S rw   )r  r  
API_SERVERWEBHOOKr  r  s     r"   
<listcomp>zIGatewayRunner._warn_if_docker_media_delivery_is_risky.<locals>.<listcomp>  s3    xxxQqQYQdfnfv@w7w7wq7w7w7wr$   r   c                 V    g | ]&}t          |t                    t          |          'S rw   )r1   r)   r  vs     r"   rm  zIGatewayRunner._warn_if_docker_media_delivery_is_risky.<locals>.<listcomp>  s-    LLL!As9K9KLs1vvLLLr$   zACould not parse TERMINAL_DOCKER_VOLUMES for gateway media warningTexc_infoF	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?   r)  r6   r_  r  get_connected_platformsjsonloadsr1   r.  r   r*  r:  _DOCKER_VOLUME_SPEC_REr   r    $_DOCKER_MEDIA_OUTPUT_CONTAINER_PATHSr+  )
r_  	connectedmessaging_platformsraw_volumesvolumesparsedhas_explicit_output_mountspecr   container_paths
             r"   r!  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  r   )r_  r  s     r"   _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.rh  r   )r_  r   r  s      r"   
_voice_keyzGatewayRunner._voice_key%  s    .,,7,,,r$   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_onlyrh  z}Skipping legacy unprefixed voice mode key %r during migration. Re-enable voice mode on that chat to rebuild the prefixed key.)ru  rv  _VOICE_MODE_PATHro  FileNotFoundErrorJSONDecodeErrorOSErrorr1   rW   itemsr)   r*  r+  )r_  datavalid_modesr  r  moder  s          r"   rZ  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_okri  indentzFailed to save voice modes: %s)
r  r  mkdir
write_textru  dumpsr[  r  r*  r+  )r_  rb  s     r"   _save_voice_modeszGatewayRunner._save_voice_modesC  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r%   r1   r]  adddiscard)r_  rY  r  r  disabled_chatsenabled_chatss         r"   _set_adapter_auto_tts_disabledz,GatewayRunner._set_adapter_auto_tts_disabledL  s     *DdKK.#.. 	F 	,w'''#G-FMMM--- /%%g...../ / ""7+++++r$   enabledc                 ,   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_  rY  r  r  r  r  s         r"   _set_adapter_auto_tts_enabledz+GatewayRunner._set_adapter_auto_tts_enabledZ  s      )BDII--- 	F 	+g&&&$W.H$OON.#.. 0&&w/////0 0 !!'*****r$   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   r  voiceauto_ttsF_auto_tts_defaultrh  c              3      K   | ]9\  }}|d k    |                                |t                    d         V  :dS )r  NrL  rK  r  r  r  prefixs      r"   r  zBGatewayRunner._sync_voice_mode_state_to_adapter.<locals>.<genexpr>  sX       " "&/c45==S^^F%;%;= CKKLL! ===" "r$   c              3   ~   K   | ]7\  }}|d v 	|                               |t                    d         V  8dS ))r  r  Nr  r  s      r"   r  zBGatewayRunner._sync_voice_mode_state_to_adapter.<locals>.<genexpr>  s_       ! !&/c4000S^^F5K5K0 CKKLL!0000! !r$   )r%   r1   r  r]  r   r  r3   rA   r   hasattrr  r   clearupdater[  r  )	r_  rY  r   r  r  rc  	_full_cfgr  r  s	           @r"   !_sync_voice_mode_state_to_adapterz/GatewayRunner._sync_voice_mode_state_to_adapterl  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{V  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.
        Nz7Defensive %s disconnect after failed connect raised: %srY  )
disconnectr   r*  r:  r   )r_  rY  r   rb  s       r"   _safe_adapter_disconnectz&GatewayRunner._safe_adapter_disconnect  s      	$$&&&&&&&&&&& 	 	 	LLI"*"6I        	s     
A%AA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_TIMEOUTr>           z;Ignoring invalid HERMES_GATEWAY_PLATFORM_CONNECT_TIMEOUT=%r)	r?   r)  r6   r5   maxr7   r*  r+  &_PLATFORM_CONNECT_TIMEOUT_SECS_DEFAULT)r_  rD   r   s      r"   _platform_connect_timeout_secsz,GatewayRunner._platform_connect_timeout_secs  s    iA2FFLLNN 		))** 3(((    Q     65s   A
 
%A21A2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   Nr   z connect timed out after gs)r  connectr9  wait_forTimeoutErrorr   )r_  rY  r   r   r2  s        r"   _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  )r<  r_  s    r"   should_exit_cleanlyz!GatewayRunner.should_exit_cleanly  s    !!r$   c                     | j         S r  )r=  r  s    r"   should_exit_with_failurez&GatewayRunner.should_exit_with_failure  s    &&r$   c                     | j         S r  )r>  r  s    r"   exit_reasonzGatewayRunner.exit_reason  s      r$   c                     | j         S r  )r  r  s    r"   	exit_codezGatewayRunner.exit_code  s
    r$   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.r5  Nr  group_sessions_per_userTthread_sessions_per_userFr  r  )r  r5  _generate_session_keyr1   r)   r   r%   r  )r_  r  rV  r  s       r"   _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  FrR  Nr  user_idz(Failed to read Telegram topic mode stateTrq  )r   r  TELEGRAMr  r%   is_telegram_topic_mode_enabledr)   r  r  r   r*  r:  )r_  r  
session_dbrD   s       r"   _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r>   ry   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  Fr>   r   r  r  r  r  r)   r  _TELEGRAM_GENERAL_TOPIC_IDSr_  r  tids      r"   _is_telegram_topic_root_lobbyz+GatewayRunner._is_telegram_topic_root_lobby   sb    ?h///63Ct3K3K50088 	5&"(b))d666r$   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  Fr>   Tr  r  s      r"   _is_telegram_topic_lanez%GatewayRunner._is_telegram_topic_lane	  ss    ?h///63Ct3K3K50088 	5&"(b)) 	cT===5tr$   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_tsr>   Tr   Nr  F)r  r  r)   r  rM   	monotonicrA   #_TELEGRAM_LOBBY_REMINDER_COOLDOWN_Sr_  r  r  _timerJ   lasts         r"   $_should_send_telegram_lobby_reminderz2GatewayRunner._should_send_telegram_lobby_reminder  s     t:;; 	2/1D,fn*++ 	4oo/33GSAA:@@@547(1tr$   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.rw   r  s    r"   "_telegram_topic_root_lobby_messagez0GatewayRunner._telegram_topic_root_lobby_message*  s    *	
 	
r$   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.rw   r  s    r"    _telegram_topic_root_new_messagez.GatewayRunner._telegram_topic_root_new_message3  s    W	
 	
r$   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     r"   _telegram_topic_new_headerz(GatewayRunner._telegram_topic_new_header<  s,    ++F33 	49	
 	
r$   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.rR  Nr>   )r  r  r  rV  r  )r%   r  r  bind_telegram_topicr)   r  rV  r  )r_  r  session_entryr  s       r"   _record_telegram_topic_bindingz,GatewayRunner._record_telegram_topic_bindingF  s     T=$77
V^6;KF&&''&*++,"--%1$/ 	' 	
 	
 	
 	
 	
r$   r  rV  user_configrV  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          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||	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"  zZSession model override (fast): session=%s config_model=%s -> override_model=%s provider=%sr>   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 %s)r  r   r  r  rA   r*  r:  r.  keysr3  popr9  _apply_session_model_overridehermes_cli.modelsr  )r_  r  rV  r  resolved_session_keyr   overrideoverride_modeloverride_runtimeruntime_kwargsruntime_modelr  s               r"   _resolve_session_agent_runtimez,GatewayRunner._resolve_session_agent_runtimeW  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	     ##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       n$$s    --;H> >
I
Iuser_messager   r  c                 H   ddl m} |                    d          |                    d          |                    d          |                    d          |                    d          t          |                    d          pg           |                    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/  	signaturer+  Nrequest_overridesr   )r   r
  rA   r.  tupler%   r   )	r_  r  r   r  r
  r/  routeservice_tier	overridess	            r"   _resolve_turn_agent_configz(GatewayRunner._resolve_turn_agent_config  ss    	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   7D	 	DDrY  c                 R  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        s| j        r|j        rK|j        pd| _        d| _        t                               d           |                                  d{V  dS t                               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.z4All messaging platforms failed with retryable errorszuAll messaging platforms failed with retryable errors. Shutting down gateway for service restart (systemd will retry).zSNo connected messaging platforms remain, but %d platform(s) queued for reconnection)r*  r  r   r   fatal_error_codefatal_error_message_update_platform_runtime_statusfatal_error_retryabler   rA   r  r  r6  r  	platformsrL  rM   r  r9  r>  r=  stopr+  rK  )r_  rY  existingplatform_configs       r"   _handle_adapter_fatal_errorz)GatewayRunner._handle_adapter_fatal_error  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 	 , $+$?$yCy!*.'V   iikk!!!!!!!!!i.//    	 	 	 	s   C 3Dreasonc                 T    d| _         || _        | j                                         d S NT)r<  r>  r;  r]  )r_  r&  s     r"   _request_clean_exitz!GatewayRunner._request_clean_exit  s-    !"  """""r$   c                 *    t          | j                  S r  )rK  r?  r  s    r"   _running_agent_countz"GatewayRunner._running_agent_count  s    4'(((r$   c                     | j         rdndS )Nrestartshutdownr  r  s    r"   _status_action_labelz"GatewayRunner._status_action_label   s     3CyyCr$   c                     | j         rdndS )N
restartingshutting downr/  r  s    r"   _status_action_gerundz#GatewayRunner._status_action_gerund#  s    #6K||OKr$   c                 "    | j         o| j        dv S )N)queuesteer)r  r  r  s    r"   _queue_during_drain_enabledz)GatewayRunner._queue_during_drain_enabled&  s     &V4+@DV+VVr$   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.Nr@  rA  )r%   rA  
setdefaultrN  )r_  rV  r9  rY  pending_slotqueued_eventss         r"   _enqueue_fifozGatewayRunner._enqueue_fifo7  s    ?Fw(;TBBF&6== M"/D,&&$$["55<<\JJJJJ(4L%%%r$   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.
        rA  Nr   r@  )r%   rA   r  r  r@  r;  insert)r_  rV  rY  r?  r=  overflownext_queueds          r"   _promote_queued_eventz#GatewayRunner._promote_queued_eventG  s    "  &6== 	!   $$[11 	!  ll1oo 	1k4000 774G#H#H5@G%k22 $$["55<<QLLLr$   rY  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.rA  Nr@  r   )r%   rK  rA   )r_  rV  rY  r=  depths        r"   _queue_depthzGatewayRunner._queue_depthj  sb    &6==CM%%k26677;''CVXZ2[2[#[#[QJEr$   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   r>   z,[Continuing toward your standing goal]
Goal:)r%   r)   rL  )rI  r   s     r"   _is_goal_continuation_eventz)GatewayRunner._is_goal_continuation_eventr  s5     }fm<<B4yy##$STTTr$   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   Nr@  r   rA  )r%   r1   rW   rA   rK  r  rN  )
r_  rV  rY  removedr<  r?  r=  rB  keptr9  s
             r"   !_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r$   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.goalsrR  	is_activer   r*  r:  )r_  r  rR  r2  s       r"   _goal_still_active_for_sessionz,GatewayRunner._goal_still_active_for_session  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)rW  r  restart_requestedactive_agents)gateway.statusrZ  r  r+  r   )r_  rW  r  rZ  s       r"   _update_runtime_statusz$GatewayRunner._update_runtime_status  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   rY  )r   r  r  r  )r]  rZ  r   )r_  r   r  r  r  rZ  s         r"   r  z-GatewayRunner._update_platform_runtime_status  sm    		;;;;;;  !-%+	       	 	 	DD	s    
''c                  |   t          j        dd          } | s	 ddl}t          dz  }|                                rVt          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   |                    dd          } n# t          $ r Y nw xY w| 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 prefill_messages_file key in ~/.hermes/config.yaml.
        Relative paths are resolved from ~/.hermes/.
        HERMES_PREFILL_MESSAGES_FILEr>   r   Nr   r   r   prefill_messages_filez#Prefill messages file not found: %sr  z3Prefill messages file must contain a JSON array: %sz+Failed to load prefill messages from %s: %s)r?   r)  r   rv   rb   r   r   rA   r   r   
expanduseris_absoluter*  r+  ru  loadr1   r.  )		file_pathr;  r<  r=  r   ra   r   r  rb  s	            r"   r$  z$GatewayRunner._load_prefill_messages  sY    I<bAA	 		!!!!'-7??$$ Eh999 5R ll2..4"5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 #(? D DI    	II))++!! 	'$&D{{}} 	NN@$GGGI		dC'222 $ay||$ $ $ $ $ $ $ $ $ $ $ $ $ $ $dD)) TVZ[[[	K 	 	 	NNH$PQRRRIIIIII	s}   3B A0$B 0A44B 7A48B 
BBF	 )E
>F	 
EF	 E4F	 F	 	
F;F60F;6F;c                  n   t          j        dd          } | r| S 	 ddl}t          dz  }|                                rgt          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   t          |dd	d
          pd                                S n# t          $ r Y nw xY w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_PROMPTr>   r   Nr   r   r   r   system_promptrG   )
r?   r)  r   rv   rb   r   r   r   r6   r   )promptr;  r<  r=  r   s        r"   r&  z+GatewayRunner._load_ephemeral_system_prompt  s!    ;R@@ 	M	#m3H   Z(W555 1,,r**0bC1 1 1 1 1 1 1 1 1 1 1 1 1 1 1WorJJJPbWWYYYZ  	 	 	D	rs5   3B% A2&B% 2A66B% 9A6:)B% %
B21B2c                     ddl m}  d}	 ddl}t          dz  }|                                rtt          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   t          t          |dd	d
          pd          	                                }n# t          $ r Y nw xY w | |          }|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_effortr>   Nr   r   r   r   reasoning_effortrj  z5Unknown reasoning_effort '%s', using default (medium))r  rm  r   rv   rb   r   r   r)   r   r6   r   r*  r+  )rm  effortr;  r<  r=  r   r  s          r"   r(  z$GatewayRunner._load_reasoning_config  s^    	<;;;;;	#m3H   b(W555 1,,r**0bC1 1 1 1 1 1 1 1 1 1 1 1 1 1 1WS'3ErRRRXVXYY__aa 	 	 	D	''// 	\fllnn 	\NNRTZ[[[s4   3B! A!B! !A%%B! (A%)7B! !
B.-B.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   Nr>   u   —z--)r>   FFz--globalTr]  )	shlexr)   r6   r9   r^  r7   rN  rO  r_  )rp  rr  r   tokenspersist_globalvalue_tokensr  s          r"   _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  rV  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  r   r%   r(  )r_  r  rV  r  r  s        r"   !_resolve_session_reasoning_configz/GatewayRunner._resolve_session_reasoning_config3  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   )r  r   r  rW   )r_  rV  rz  s      r"   _set_session_reasoning_overridez-GatewayRunner._set_session_reasoning_overrideF  sm      	Ft;<< 	302D-#-11+tDDDDD=ABR=S=SD-k:::r$   c                     d} 	 ddl }t          dz  }|                                rtt          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   t          t          |ddd	          pd                                          } n# t          $ r Y nw xY w| 	                                }|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.
        r>   r   Nr   r   r   r   r  rj  >   r  nonenormalrG   standard>   onfastpriorityr  z#Unknown service_tier '%s', ignoring)r   rv   rb   r   r   r)   r   r6   r   r_  r*  r+  rD   r;  r<  r=  r   r   s         r"   r*  z GatewayRunner._load_service_tierU  sT    	#m3H   [(W555 1,,r**0bC1 1 1 1 1 1 1 1 1 1 1 1 1 1 1'#wKKKQrRRXXZZ 	 	 	D	 		 	!QQQ4...:<cBBBts4   3B AB AB "A#7B 
B('B(c                  .   	 ddl } t          dz  }|                                r`t          |d          5 }|                     |          pi }ddd           n# 1 swxY w Y   t          t          |dd          d	          S n# t          $ r Y nw xY wdS )
z<Load show_reasoning toggle from config.yaml display section.r   Nr   r   r   r   show_reasoningFrj  )r   rv   rb   r   r   r   r   r   r;  r<  r=  r   s       r"   r,  z"GatewayRunner._load_show_reasoningp  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&C,<==!     	 	 	D	us4   3B AB AB  A!"B 
BBc                     t          j        dd                                                                          } | s	 ddl}t
          dz  }|                                rt          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   t          t          |dd	d
          pd                                                                          } n# t          $ r Y nw xY w| dk    rdS | dk    rdS dS )z<Load gateway drain-time busy-input behavior from config/env.r   r>   r   Nr   r   r   r   r   rj  r6  r7  r  )r?   r)  r6   r_  r   rv   rb   r   r   r)   r   r   )r  r;  r<  r=  r   s        r"   r.  z#GatewayRunner._load_busy_input_mode  s_    y92>>DDFFLLNN 		!!!!'-7??$$ mh999 5R ll2..4"5 5 5 5 5 5 5 5 5 5 5 5 5 5 5wsI7HRTUUU[Y[\\bbddjjllD   7??77??7{s6   3C& 0BC& BC& BA	C& &
C32C3c                  r   t          j        dd                                          } | s	 ddl}t          dz  }|                                rtt          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   t          t          |dd	d
          pd                                          } n# t          $ r Y nw xY w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   r>   r   Nr   r   r   r   r   rj  z7Invalid restart_drain_timeout '%s', using default %.0fs)r?   r)  r6   r   rv   rb   r   r   r)   r   r   r  r  r5   rC   r7   r*  r+  r  s         r"   r/  z)GatewayRunner._load_restart_drain_timeout  s    i6;;AACC 		!!!!'-7??$$ hh999 5R ll2..4"5 5 5 5 5 5 5 5 5 5 5 5 5 5 5gc74KUWXXX^\^__eeggC   +C00 	5AAAc



z*   M9     sG   3C B6C BC 	B
7C 
CC/C? ?2D43D4c                     t          j        dd          } | s	 ddl}t          dz  }|                                rkt          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   t          |dd	          }|d
u rd} n|dvrt          |          } n# t          $ r Y nw xY w| 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_NOTIFICATIONSr>   r   Nr   r   r   r    background_process_notificationsFr  )Nr>   r  >   r  r  r  r  zBUnknown background_process_notifications '%s', defaulting to 'all')r?   r)  r   rv   rb   r   r   r   r)   r   r6   r_  r*  r+  )r  r;  r<  r=  r   rD   valids          r"   #_load_background_notifications_modez1GatewayRunner._load_background_notifications_mode  s~    y:B?? 	!!!!'-7??$$ (h999 5R ll2..4"5 5 5 5 5 5 5 5 5 5 5 5 5 5 5!#y2TUUCe||$J.."3xx   $$&&,,..111uNNT   5s5   3B' A0$B' 0A44B' 7A48.B' '
B43B4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   Nr   r   r   provider_routingr   rv   rb   r   r   rA   r   r  s       r"   r0  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                  N   	 ddl } t          dz  }|                                rpt          |d          5 }|                     |          pi }ddd           n# 1 swxY w Y   |                    d          p|                    d          pd}|r|S n# t          $ r Y nw xY wdS )a  Load fallback provider chain from config.yaml.

        Returns a list of provider dicts (``fallback_providers``), a single
        dict (legacy ``fallback_model``), or None if not configured.
        AIAgent.__init__ normalizes both formats into a chain.
        r   Nr   r   r   r5  r6  r  )r;  r<  r=  r   r>  s        r"   r2  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WW122Wcgg>N6O6OWSW I 	 	 	D	ts4   3B AB AB  A!2B 
B"!B"c                 H    d | j                                         D             S )Nc                 ,    i | ]\  }}|t           u||S rw   )_AGENT_PENDING_SENTINEL)r  rV  r   s      r"   
<dictcomp>z:GatewayRunner._snapshot_running_agents.<locals>.<dictcomp>  s4     
 
 
"U333 333r$   )r?  r  r  s    r"   _snapshot_running_agentsz&GatewayRunner._snapshot_running_agents  s2    
 
&*&:&@&@&B&B
 
 
 	
r$   rP  c                     | j                             |j        j                  }|sd S t	          |j        ||           d S r  )r   rA   r  r   r  r@  )r_  rV  rP  rY  s       r"   _queue_or_replace_pending_eventz-GatewayRunner._queue_or_replace_pending_event  sD    -##EL$9:: 	F#G$={ERRRRRr$   c                 
  K   |                      |j                  sQt                              d|j        j        |j        j        |j        j        r|j        j        j        nd|           dS | j        r| j	        
                    |j        j                  }|sdS |j        j        rd|j        j        ind }|                                 r/|                     ||           d|                                  d}nd|                                  d}|                    |j        j        ||j        |	           d {V  dS | j	        
                    |j        j                  }|sd
S | j        
                    |          }| j        }d
}|dk    r|j        pd                                }	|	o|d uo|t,          uot/          |d          }
|
rY	 t1          |                    |	                    }n5# t4          $ r(}t                              d||           d
}Y d }~nd }~ww xY w|sd}|st7          |j        ||           |dk    }|dk    }|dk    r7|r5|t,          ur,	 |                    |j                   n# t4          $ r Y nw xY wt<          j        
                    dd                                           dk    }|st          !                    d|           dS d}tE          j"                    }| j#        
                    |d          }||z
  |k     rdS || j#        |<   g }|r|t,          ur	 |$                                }|
                    dd          }|
                    dd          }|
                    d          }| j%        
                    |d          }|r3tM          ||z
  dz            }|dk    r|'                    | d           |r|'                    d| d|            |r|'                    d|            n# t4          $ r Y nw xY w|rdd(                    |           dnd}|rd | d!}n|rd"| d#}nd$| d%}	 dd&l)m*}m+}m,}m-} t]                      } |||          s0|rd}n|rd}nd}| d' ||           } |t^          d(z  |           n2# t4          $ r%} t          !                    d)|            Y d } ~ nd } ~ ww xY w|j        j        rd|j        j        ind }	 |                    |j        j        ||j        |	           d {V  n2# t4          $ r%}!t          !                    d*|!           Y d }!~!nd }!~!ww xY wdS )+Nz`Dropping message from unauthorized user in active session: user=%s (%s), platform=%s, session=%sr  Tr     ⏳ Gateway 2    — queued for the next turn after it comes back.   ⏳ Gateway is - and is not accepting another turn right now.r  rr  reply_tometadataFr7  r>   z'Gateway steer failed for session %s: %sr6  r  r   truez"Busy ack suppressed for session %sr  r   api_call_countmax_iterationscurrent_tool<    min elapsed
iteration r   	running:  (, )u   ⏩ Steered into current runz0. Your message arrives after the next tool call.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_seen

r   z.Failed to apply busy-input onboarding hint: %szFailed to send busy-ack: %s)0_is_user_authorizedr  r*  r+  r  	user_namer   r   r  r   rA   r  r8  r  r4  _send_with_retryr  
message_idr?  r  r   r6   r  r  r3   r7  r   r  r@  r  r?   r@   r_  r:  rM   rC  get_activity_summaryr  r4   rN  rO  agent.onboardingr  r  r  r  r  rv   )"r_  rP  rV  rY  thread_metar[  running_agenteffective_modesteered
steer_text	can_steerr2  is_queue_modeis_steer_moder   _BUSY_ACK_COOLDOWNrJ   last_ackstatus_partssummary	iterationmax_iterr  start_tselapsed_minstatus_detailr  r  r  r  	_user_cfg
_hint_mode_onb_errrb  s"                                     r"   #_handle_active_session_busy_messagez1GatewayRunner._handle_active_session_busy_message	  s}      ''55 		NN8$&/4|/DS%++)   4 > 	m''(=>>G tCH<CYc;(>??_cK//11 x44[%HHHy)C)C)E)EyyywD,F,F,H,Hwww**,)$	 +          4 -##EL$9:: 	5,00== .W$$**1133J 4!-4!)@@4 M733	   $$"=#6#6z#B#BCCGG  $ $ $NN#Lk[^___#GGGGGG$  )!(  	W'(A;PUVVV&'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),+&  	]2III'<<>>#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  		@} @ @ @ 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 @E|?U_{EL$:;;[_	;**,)$	 +            	; 	; 	;LL6::::::::	; tsn   "G5 5
H'?H""H'$I? ?
JJ7CP 
P! P!AR1 1
S ;SS  .T/ /
U9UU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)r9  get_running_looprM   r+  r^  )r   rJ   active_countlast_active_countlast_status_atr_  s      r"   _maybe_update_statusz@GatewayRunner._drain_active_agents.<locals>._maybe_update_status	  s}    *,,1133C4466L %(999cN>RWZ=Z=Z++J777$0!!$ >[=Zr$   Tr   r   皙?F)r  r+  r3   r?  r9  r  rM   sleep)r_  r   snapshotr  deadline	timed_outr  r  s   `     @@r"   _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((((""r$   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.  r?  r  r  r  r*  r:  r   )r_  r&  rV  r   rb  s        r"   _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                 	  K   |                                  }| j        rdnd}| j        rdnd}d| d| }t                      }|D ]n}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|
           |rd|ind}|                    |||           d{V }|At          |dd          du r.t          
                    d|
|t          |dd                     |                    |           t                              d|
|           ;# t          $ r(}	t          
                    d|
||	           Y d}	~	hd}	~	ww xY wt5          | 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        r	d|j        ind}|r1|                    t          |j                  ||           d{V }n.|                    t          |j                  |           d{V }|Kt          |dd          du r8t          
                    d|j        |j        t          |dd                     p|                    |           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.
        r2  r3  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     — Nr5  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=falser  successTFz1Failed to send shutdown notification to %s:%s: %sr  send returned success=Falsez/Sent shutdown notification to active chat %s:%sz\Shutdown notification suppressed for home channel: %s has gateway_restart_notification=falsez>Failed to send shutdown notification to home channel %s:%s: %sz0Sent shutdown notification to home channel %s:%s)r  r  r]  r%   r5  _ensure_loaded_entriesrA   r   r*  r:  _get_cached_session_sourcer   r   r)   r  r  r  r  r   r  r!  gateway_restart_notificationr9  sendr  r.  r  get_home_channel)r_  activeactionhintrX   notifiedrV  r  r@  rb  platform_strr  r  _parsed	dedup_keyr   rY  platform_cfgr  r  homes                        r"   #_notify_active_sessions_of_shutdownz1GatewayRunner._notify_active_sessions_of_shutdown	  s      ..00!%!8Mo &: X X :	 	 433T338;! K	 K	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$$%#L11-++H55 #{488BB+L4]+KKx$    8AJK33d&||GS8|LLLLLLLL%'&)T*J*Je*S*SLLK$1NOO	   Y'''E '       G '1        "&dm&9&9&;&;!<!< ,	 ,	Hg;//99D t| ;044X>>L'0Y'rN   !T\):):SWSa<kC<O<O<OgklIH$$<@NTK88PT 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	       M,	 ,	sd   AB**
C4CC2+J AJ $A'J 1J  
J2
J--J2B>R;R
S'R??Sr\  c           	          |                                 D ]L}	 ddlm}  |dt          |dd           d           n# t          $ r Y nw xY w|                     |           Md S )Nr   invoke_hookon_session_finalizer  gatewayr  r   )valueshermes_cli.pluginsr  r%   r   _cleanup_agent_resources)r_  r\  r   _invoke_hooks       r"   _finalize_shutdown_agentsz'GatewayRunner._finalize_shutdown_agentst
  s    "))++ 
	1 
	1EJJJJJJ)&ulDAA&    
    ))%0000
	1 
	1s   #<
A	A	r   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)	r  r%   r1   r.  r  r   r  agent.auxiliary_clientr  )r_  r   session_messagesr  s       r"   r  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rg  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  )	ru  rv   _STUCK_LOOP_FILErb   rv  ro  r   rA   r|   )r_  r  ru  ra   counts
new_countsr  s          r"   !_increment_restart_failure_countsz/GatewayRunner._increment_restart_failure_counts
  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 rw   )_STUCK_LOOP_THRESHOLD)r  krp  r_  s      r"   rm  z>GatewayRunner._suspend_stuck_loop_sessions.<locals>.<listcomp>
  s*    VVVDAqa4;U6U6Ua6U6U6Ur$   Tr   u_   Auto-suspended stuck session %s (active across %d consecutive restarts — likely a stuck loop)
missing_ok)ru  rv   r	  rb   rv  ro  r   r  r5  r  rA   	suspendedr*  r+  _saveunlink)r_  ru  ra   r
  r  
stuck_keysrV  r@  s   `       r"   _suspend_stuck_loop_sessionsz*GatewayRunner._suspend_stuck_loop_sessions
  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  )	ru  rv   r	  rb   rv  ro  r|   r  r   )r_  rV  ru  ra   r
  s        r"   _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                    }d                    d |D                       }d| 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]  c              3   >   K   | ]}t          j        |          V  d S r  rr  quoter  s     r"   r  zAGatewayRunner._launch_detached_restart_command.<locals>.<genexpr>  s,      @@Tu{4((@@@@@@r$   zwhile kill -0 z" 2>/dev/null; do sleep 0.2; done; z gateway restartsetsidbashz-lcTstdoutstderrstart_new_session)r  
subprocessr  r*  r  r?   getpidrO  r  PopenDEVNULL)r_  r  r$  
hermes_cmdcurrent_pidcmd	shell_cmd
setsid_bins           r"    _launch_detached_restart_commandz.GatewayRunner._launch_detached_restart_command  s8     (**
 	LLOPPPFikkhh@@Z@@@@@%[ % %% % % 	 \\(++
 	VUI6!)!)"&	       	*!)!)"&	      r$   detachedvia_servicer/  r0  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r-  detached_restartservice_restart)r9  r  r"  )r/  r_  r0  s   r"   _run_restartz3GatewayRunner.request_restart.<locals>._run_restart5  sZ      -%%%%%%%%%))D8U`)aaaaaaaaaaar$   r   N)
r  r  r  r  r9  create_taskr^  r  add_done_callbackr  )r_  r/  r0  r7  tasks   ```  r"   request_restartzGatewayRunner.request_restart-  s    % 	5"&!)$/!%)"	b 	b 	b 	b 	b 	b 	b 	b "<<>>22""4(((t5=>>>tr$   >   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                            |j                  }	|	;t                              d|j        t)          |j        d|j                             t+          dt,          j        |d	          }
t1          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 on the next gateway startup.
        c                 Z    g | ]'}|j         r|j        s|j        |j        j        v %|(S r  )resume_pendingr  r  resume_reason_AUTO_RESUME_REASONS)r  r@  r_  s     r"   rm  zCGatewayRunner._schedule_resume_pending_sessions.<locals>.<listcomp>Z  sX       #+ "O 0+t/HHH	  IHHr$   Nz/Failed to enumerate resume-pending sessions: %sr   z5Skipping auto-resume for %s: adapter not ready for %sr   r>   Tr   rF  r  internalr   z;Scheduled auto-resume for %d restart-interrupted session(s)) rE   r5  _lock_ensure_loaded_lockedr  r  r   r*  r+  r   rJ   last_resume_marked_at
updated_attotal_secondsr  r   rA   r   r:  rV  r%   r  r  TEXTr9  r9  handle_messager^  r  r:  r  r9  )r_  rN   
candidatesr2  rJ   	scheduledr@  markerr  rY  rP  r;  s   `           r"   !_schedule_resume_pending_sessionsz/GatewayRunner._schedule_resume_pending_sessionsG  sd    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\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;   A9 AA-!A9 -A11A9 4A15A9 9
B)B$$B)c           
        K   t                               d           	 t          j                    | _        n# t
          $ r
 d| _        Y nw xY wt                               d| j        j                   	 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}d}	d}
	 ddlm} t)          d |                                D                       }	t)          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d#lm} dd$lm}  | |            d%&           n,# t          $ r t                               d'd!"           Y nw xY w| j                                         	 dd(l m!} |"                                }|rt                               d)|           n2# t          $ r%}t                               d*|           Y d}~nd}~ww xY wtF          d+z  }|$                                r@t                               d,           	 |%                                 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                               d0|           Y d}~nd}~ww xY wd}d}g }g }| j        j)        *                                D ]\  }}|j+        s|d1z  }| ,                    ||          }|sk|j-        }d2 t\          j/        0                                D             }||vrt                               d3|           nt                               d4|           |1                    | j2                   |3                    | j4                   |5                    | j&                   |6                    | j7                   t                               d5|j-                   | 8                    |j-        d6dd7           	 | 9                    ||           d{V } | rd|| j:        |<   | ;                    |           |d1z  }| 8                    |j-        d8dd7           t                               d9|j-                   n*t                               d:|j-                   | <                    ||           d{V  |j=        r| 8                    |j-        |j>        rd;nd<|j?        |j@        7           |j>        r|n|}!|!A                    |j-         d=|j@                    |j>        r"|d1t          jC                    d>z   d?| jD        |<   n]| 8                    |j-        d;dd@7           |A                    |j-         dA           |d1t          jC                    d>z   d?| jD        |<   # t          $ r}t           E                    dB|j-        |           | <                    ||           d{V  | 8                    |j-        d;dt          |          7           |A                    |j-         d=|            |d1t          jC                    d>z   d?| jD        |<   Y d}~d}~ww xY w|dk    r|rldCG                    |          }"t           E                    dD|"           	 ddlm}  |dE|"           n# t          $ r Y nw xY w| H                    |"           d!S |dk    rYdCG                    |          pdF}"t           E                    dG|"           	 ddlm}  |dE|"           n# t          $ r Y nw xY wd%S t                               dH           t                               dI           | j:        | jI        _:        d!| _J        | K                    dJ           t          | j        jM                  }#|#rt                               dK|#           | j        N                    dLdMdN | j:        O                                D             i           d{V  |dk    rt                               dO|           	 ddPlPmQ}$  |$| j:                   d{V }%t          dQ |%S                    dMi           0                                D                       }&t                               dR|&           n2# t          $ r%}t                               dS|           Y d}~nd}~ww xY w| T                                 d{V }'|'s?t-          dT tF          dUz  tF          dVz  fD                       r| U                                 |dk    rt          jV        dW           d{V  t                      }(| X                                 d{V })|(s|)#|)r|)hnd}*| Y                    |*X           d{V  | Z                                 	 dd(l m!} |j[        rv|j[        \                    d          }+t          j]        | ^                    |+                     t                               dY|+S                    dZ                     |j[        vn2# t          $ r%}t           E                    d[|           Y d}~nd}~ww xY wt          j]        | _                                           t          j]        | `                                           t          j]        | a                                           | jD        rPt                               d\t          | jD                  d]G                    d^ | jD        D                                  t          j]        | b                                           t                               d_           d!S )`z
        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   90zuAgent budget: max_iterations=%d (agent.max_turns from config.yaml, or HERMES_MAX_ITERATIONS from .env, or default 90)r   r  )ry   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.r   get_active_profile_namerG   zActive profile: %srY  starting)rW  r  )TELEGRAM_ALLOWED_USERSDISCORD_ALLOWED_USERSWHATSAPP_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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_USERSrw   platform_registryc              3   2   K   | ]}|j         	|j         V  d S r  )allowed_users_envr  rb  s     r"   r  z&GatewayRunner.start.<locals>.<genexpr>  sB       ) )()&)#) ) ) ) ) )r$   c              3   2   K   | ]}|j         	|j         V  d S r  )allow_all_envr  s     r"   r  z&GatewayRunner.start.<locals>.<genexpr>  s@       + +$%?++ + + + + +r$   c              3   >   K   | ]}t          j        |          V  d S r  )r?   r)  ro  s     r"   r  z&GatewayRunner.start.<locals>.<genexpr>  s;       
 
BIaLL
 
 
 
 
 
r$   GATEWAY_ALLOW_ALL_USERSr>   r  ry   rT  c              3   h   K   | ]-}t          j        |d                                           dv V  .dS )r>   r  N)r?   r)  r_  ro  s     r"   r  z&GatewayRunner.start.<locals>.<genexpr>  s\       e
 e
 Ia""$$(<<e
 e
 e
 e
 e
 e
r$   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 startupTrq  r  )register_from_configF)accept_hooksz1shell-hook registration failed at gateway startupr  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 rw   r  r  ms     r"   	<setcomp>z&GatewayRunner.start.<locals>.<setcomp>g  s    !Q!Q!Qa!'!Q!Q!Qr$   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  ry  u   ✓ %s connectedu   ✗ %s failed to connectr  r  r   r  r  zfailed to connectz: failed to connectu   ✗ %s error: %sz; z0Gateway hit a non-retryable startup conflict: %sstartup_failedz4all configured messaging platforms failed to connectz?Gateway failed to connect any configured messaging platform: %szNo messaging platforms enabled.z5Gateway will continue running for cron job execution.runningz%s hook(s) loadedzgateway:startupr!  c                     g | ]	}|j         
S rw   r  rl  s     r"   rm  z'GatewayRunner.start.<locals>.<listcomp>  s    @@@a!'@@@r$   z#Gateway running with %s platform(s)build_channel_directoryc              3   4   K   | ]}t          |          V  d S r  rK  )r  chss     r"   r  z&GatewayRunner.start.<locals>.<genexpr>  s(      WW3s88WWWWWWr$   z%Channel directory built: %d target(s)z"Channel directory build failed: %sc              3   >   K   | ]}|                                 V  d S r  )rb   )r  ra   s     r"   r  z&GatewayRunner.start.<locals>.<genexpr>  s>        
  
 KKMM 
  
  
  
  
  
r$   .update_pending.json.update_pending.claimed.jsonr  skip_targetsz(Resumed watcher for recovered process %sr  z!Recovered watcher setup error: %sz;Starting reconnection watcher for %d failed platform(s): %sr  c              3   $   K   | ]}|j         V  d S r  r  rl  s     r"   r  z&GatewayRunner.start.<locals>.<genexpr>I  s$      BBa!'BBBBBBr$   zPress Ctrl+C to stop)cr*  r9  r9  r  r8  r-  r  r  r4   r?   r)  r   r_  r+  hermes_cli.profilesrV  r]  rZ  gateway.platform_registryr  r  plugin_entriesr  r  r  r:  r   r  agent.shell_hooksr  rY  discover_and_loadr4  r  recover_from_checkpointrv   rb   r  r5  suspend_recently_activer  r!  r  r  _create_adapterr   r  __members__r  set_message_handler_handle_messageset_fatal_error_handlerr%  set_session_storeset_busy_session_handlerr  r  r  r   r  r  has_fatal_errorr   r  r  rN  rM   r  rL  r  r)   rO  r)  r6  r7  r^  rK  loaded_hooksemitr  gateway.channel_directoryr  sumrA   _send_update_notification#_schedule_update_notification_watchr  rx   _send_restart_notification(_send_home_channel_startup_notificationsrQ  pending_watchersr  r9  _run_process_watcher_session_expiry_watcher_kanban_notifier_watcher_kanban_dispatcher_watcher_platform_reconnect_watcher),r_  _effective_max_iter_redact_raw
_redact_onrV  _profilerZ  _builtin_allowed_vars_builtin_allow_all_vars_plugin_allowed_vars_plugin_allow_all_varsr  _any_allowlist
_allow_allr  r  r  r  	recoveredrb  _clean_markerr  stuckconnected_countenabled_platform_countstartup_nonretryable_errorsstartup_retryable_errorsr   r$  rY  _pval_builtin_namesr  targetr&  
hook_countr  	directorych_countr  restart_notification_pendingdelivered_restart_targetskip_home_targetswatchers,                                               r"   startzGatewayRunner.start  s      	0111	&!(!9!;!;D 	& 	& 	&!%D	&)4;+CDDD	"%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	!
$#
" ')(*	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[  	;;;;;; 	 	 	LL<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 r	 r	%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 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%))#;<<v@v^`fgggCCCCCC((7GU[\\\\\    DuNN<===KKOPPP )-%##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-$$$$$$$$$ (E'F'F$)-)H)H)J)J#J#J#J#J#J#J  ( 	+C+O.FP)**D  ??. @          	..000	A??????"3 c*;??BB#D$=$=g$F$FGGGFT`HaHabbb #3 c  	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<<>>???*+++tsY  7 A
A4=B2 2
B?>B?A#D' '
D43D483E, ,
E98E9=F 
FF*AH 
HHJ( (&KK!K7 7&L L =7M5 5
N$?NN$ O5 5
PP6P= =
Q,Q''Q,01R" "
S,SS7F,_%%
b#/B)bb# c4 4
d de( (
e54e57A6k. .
l8ll?Br 
r2r--r2r  intervalc           	      6  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        |           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        j        5  d|_        | j                                         ddd           n# 1 swxY w Y   t                                d|j                   |!                    |j        d           # 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}~d}~ww xY w|retG          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}tK          j%                    |z
  |k    r	 tM          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tK          j%                    | _)        n2# t*          $ r%}t                                d |           Y d}~nd}~ww xY wtU          |          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.
        r  Nrg  rh  ri  r  r   r   r  c              3   *   K   | ]\  }}| d | V  dS )rh  Nrw   )r  r  cs      r"   r  z8GatewayRunner._session_expiry_watcher.<locals>.<genexpr>q  sA       . .'+q!1

q

. . . . . .r$   z,Session expiry: %d sessions to finalize (%s)r  r>   r  r  rJ  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  rn  rb  s      r"   r  z8GatewayRunner._session_expiry_watcher.<locals>.<genexpr>  sB          "a!:L            r$   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)+r9  r  r7  r5  r  r.  r  r  r  _is_session_expiredrN  r^  rK  rA   rO  sortedr*  r9  r  r  r  r   r%   rH  r1   r  r?  r  r  _evict_cached_agentrG  r  r:  r  r+  r  _sweep_idle_cached_agentsrM   r4   r  prune_old_entriesr  range)r_  r  _finalize_failures_MAX_FINALIZE_RETRIES_expired_entriesr  r@  
_platforms_k_e_parts_plat_plat_summaryr  	_platform_cached_agent_cache_lock_cachedrb  failures_done_failed_idle_evicted_last_prune_ts_prune_interval_max_age_prunedrn  s                               r"   r  z%GatewayRunner._session_expiry_watcherQ  sL      mB-/ !m T	'N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 < <JC;
!VVVVVV%(YYs^^F58[[1__q		"I(L 5+0+;)2    
  ) ! ! ! 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 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 T	' T	' T	' T	' T	's&  EV( ;AGL
GLGL.>H8,L8H<	<L?H<	 A L !KLK	LK	>LV( 
O=AO8;!N(O8(N,,O8/N,0AO82V( 8O==A*V( (1R V( 
S	$S?V( S		0V( :A"U V( 
V'VV( VV( (
W2WW      @c                   $%K   ddl m} 	 ddlm% n+# t          $ r t
                              d           Y dS w xY wd$$}d}t          | di           }|| _        t          j
        d	           d{V  | j        rk	 $%fd
}t          j        |           d{V }|D ]}|d         }	|d         }
|                    d          }|	d         pd                                }	  ||          }n8# t          $ r+ t          j        | j        |	|d         |           d{V  Y w xY w| j                            |          }||
r|
j        n|	d         dd         }|d         D ]v}|j        }|
r|
j        r|
j        nd}|rd| dnd}|dk    rd}d}|j        r4|j                            d          rt-          |j        d                   }|r:|                                                                d         dd         }d| }nG|
rE|
j        r>|
j                                                                        d         dd         }d| }d| d|	d          d| | }n.|dk    rZd}|j        r?|j                            d          r%d t-          |j        d                   dd          }d!| d|	d          d"| }n|d#k    rZd}|j        r?|j                            d$          r%dt-          |j        d$                   dd          }d%| d|	d          d&| }nn|d'k    rd%| d|	d          d(}nX|d)k    rPd}|j        r4|j                            d*          rt5          |j        d*                   }d+| d|	d          d,| d-}n1i }|	                    d.          r|	d.         |d.<   |	d         |	d         |	d/         |	                    d.          pdf}	 |                    |	d/         ||0           d{V  |                    |d           # t          $ r}|                    |d          d1z   }|||<   t
                              d2|	d         ||||           ||k    rZt
                              d3|	d         ||           t          j        | j        |	|           d{V  |                    |d           Y d}~ nd}~ww xY wt          j        | j        |	|d         |           d{V  |d         r|d         d4         j        nd} |
o|
j        d5v }!| |v }"|!s|"r!t          j        | j        |	|           d{V  n2# t          $ r%}t
                              d6|           Y d}~nd}~ww xY wt?          t5          tA          d1|                              D ]&}#| j        s dS t          j
        d1           d{V  '| j        idS dS )7u  Poll ``kanban_notify_subs`` and deliver terminal events to users.

        For each subscription row, fetches ``task_events`` newer than the
        stored cursor with kind in the terminal set (``completed``,
        ``blocked``, ``gave_up``, ``crashed``, ``timed_out``). Sends one
        message per new event to ``(platform, chat_id, thread_id)``,
        then advances the cursor. When a task reaches a terminal state
        (``completed`` / ``archived``), the subscription is removed.

        Runs in the gateway event loop; all SQLite work is pushed to a
        thread via ``asyncio.to_thread`` so the loop never blocks on the
        WAL lock. Failures in one tick don't stop subsequent ticks.

        **Multi-board:** iterates every board discovered on disk per
        tick. Subscriptions live inside each board's own DB and cannot
        cross boards, so delivery semantics are unchanged — this is
        purely a fan-out of the single-DB poll.
        r   r  	kanban_dbz<kanban notifier: kanban_db not importable; notifier disabledN)r  blockedgave_upcrashedr  rg  _kanban_sub_fail_countsr  c                     g } 	                      d          }n+# t          $ r                     j                  g}Y nw xY w|D ]7}|                    d          pj        }	                     |          }n# t          $ r Y Cw xY w	 	                     |           n# t          $ r Y nw xY w                    |          }|D ]}                    ||d         |d         |d         |                    d          pd	

          \  }}|sJ	                    ||d                   }	| 
                    ||||	|d           	 |                                  # |                                 w xY w| S )NFinclude_archivedrv  boardtask_idr   r  r  r>   )r
  r   r  r  kinds)r+   cursoreventsr;  r	  )list_boardsr   read_board_metadataDEFAULT_BOARDrA   r  init_dblist_notify_subsunseen_events_for_subget_taskrN  r  )
deliveriesboards
board_metarv  connsubsr+   r  r  r;  TERMINAL_KINDS_kbs             r"   _collectz8GatewayRunner._kanban_notifier_watcher.<locals>._collect$  s   -/JN!$%!H!H$ N N N"%"9"9#:K"L"L!MN&,  )  )
)~~f55J9J%#&;;T;#:#:DD( % % %$H%)% #$ 7 7 7 7#, % % % $%#&#7#7#=#=D'+ # #141J1J$(,/	N-0_,/	N.1ggk.B.B.Hb*8 2K 2" 2" (. !-$,'*||D#i.'I'I * 1 1+..4.4,0-13" 3" !# !# !# !##( !JJLLLLDJJLLLL%%sL    %AA)B  
BBB)(E))
B63E)5B66BE))E?r+   r;  r	  r   r>   r  r
  x   r  @r]  r  r  r  rI     u   ✔ zKanban u
    done — r  r&  r   u   ⏸ z blockedr  r  u   ✖ z& gave up after repeated spawn failuresr  z1 worker crashed (pid gone); dispatcher will retryr  limit_secondsu   ⏱ z timed out (max_runtime=zs); will retryr  r  r  r   z=kanban notifier: send failed for %s on %s (attempt %d/%d): %szRkanban notifier: dropping subscription %s on %s after %d consecutive send failuresrj  )donearchivedzkanban notifier tick failed: %s)!gateway.configr  r  r   r   r*  r+  r%   r  r9  r  r7  	to_threadrA   r_  r7   _kanban_advancer   titlekindassigneer  r)   r6   rq  r  r4   r  r  _kanban_unsubstatusr  r  )&r_  r  	_PlatformTERMINAL_EVENT_KINDSMAX_SEND_FAILURESsub_fail_countsr  r  dr+   r;  
board_slugr  platrY  r&  evr'  whotaghandoffpayload_summaryhr  rX   r&  r  limitr  sub_keyr2  fails	last_kindtask_terminalevent_terminalrn  r  r  s&                                       @@r"   r  z&GatewayRunner._kanban_notifier_watcher  s     & 	988888	3333333 	 	 	NNYZZZFF	 U  .
 ,3+R-
 -
 (7$ mAm o	'iG*& *& *& *& *& *&X $+#4X#>#>>>>>>>
# y yAE(CV9D!"wJ$'
O$9r#@#@#B#BL!(y66% ! ! ! &/ 0#q{J         !! #m//55G +/CTZZS^TcTJEk g g!w 15PPt}}D,/7j#jjjjR;.. ')G.2O!z MbjnnY.G.G M25bj6K2L2L. 3$3$9$9$;$;$F$F$H$H$KDSD$Q*2q((!% 3$+ 3$(K$5$5$7$7$B$B$D$DQ$G$M*2q((!9s !9 !93y> !9 !9(-!9/6!9 !9  C "Y..%'F!z PbjnnX.F.F P)Oc"*X2F.G.G.M)O)O"U"U"US^"U"UV"U"UCC!Y.."$C!z LbjnnW.E.E L&K3rz'/B+C+CDSD+I&K&K!Fs !F !F3y> !F !F@C!F !F  C "Y..!Es !E !E3y> !E !E !E  C "[00$%E!z Ibjnn_.M.M I(+BJ,G(H(H!Fs !F !F3y> !F !F05!F !F !F  C
 %3577;// E474DH[1	NC
O	NCGGK,@,@,FB#""),, #Ih #/ # #        ,//>>>>( " " "$3$7$7$C$Ca$GE7<OG4"NN!6 #Ie 13	    %(999 &%R$'	NL%!" !" !"
 '.&78JCQ[&\&\ \ \ \ \ \ \ \ / 3 3GT B B B!EEEEE%"* &/ 0#q{J         =>hK$QAhKO$8$8T	(,(T@T1T)26J)J( N ")"3 $ 2C# #       oyt  G G G@#FFFFFFFFG 3s1h//0011 ' '} FFmA&&&&&&&&&&_ m o	' o	' o	' o	' o	'sv    $;;?A&U &C21U 22D'$U &D''KU ):P%#U %
S/B#SU SA>U 
V&VVr+   r  r	  c           	         ddl m} |                    |          }	 |                    ||d         |d         |d         |                    d          pd|	           |                                 d
S # |                                 w xY w)zSync helper: advance a subscription's cursor. Runs in to_thread.

        ``board`` scopes the DB connection to the board that owns this
        subscription. Unsub cursors in one board can't touch another's.
        r   r  r  r
  r   r  r  r>   )r
  r   r  r  
new_cursorN)r  r   r  advance_notify_cursorrA   r  )r_  r+   r  r	  r  r  s         r"   r%  zGatewayRunner._kanban_advance  s     	0/////{{{''
	%%IZI''+..4"! &    JJLLLLLDJJLLLLs   AA6 6Bc           	         ddl m} |                    |          }	 |                    ||d         |d         |d         |                    d          pd	           |                                 d S # |                                 w xY w)
Nr   r  r  r
  r   r  r  r>   )r
  r   r  r  )r  r   r  remove_notify_subrA   r  )r_  r+   r	  r  r  s        r"   r)  zGatewayRunner._kanban_unsub  s    //////{{{''		!!IZI''+..4" "    JJLLLLLDJJLLLLs   AA5 5Bc                 	  K   	 ddl m} n+# t          $ r t                              d           Y dS w xY wt
          j                            dd                                          	                                }|dv rt          
                    d           dS 	  |            }n3# t          $ r&}t                              d	|           Y d}~dS d}~ww xY wt          |t                    r|                    d
i           ni }|                    dd          st          
                    d           dS 	 ddlm n+# t          $ r t                              d           Y dS w xY wt          |                    dd          pd          }|dk     rd}|                    dd          t          
                    d            |                    dj                  }	 t#          |          n?# t$          t&          f$ r+ t                              d|j                   j        Y nw xY wdk     r(t                              d|j                   j        t)          j        d           d{V  d}d}	d}
dt,          ddffdd,fd }dt.          ffd!}t          
                    d"|           | j        r	 t)          j        |           d{V }d#}|pg D ]\  }}|t5          |d$d          rd}t          
                    d%|t7          |j                  |j        t=          |j        d&          rt7          |j                  ndt=          |j         d&          rt7          |j                   nd|j!        t=          |j"        d&          rt7          |j"                  nd           t)          j        |           d{V }|r|s|	dz  }	nd}	|	|k    rFt#          tG          j#                              }||
z
  d'k    rt                              d(|	           |}
nS# t(          j$        $ r t          %                    d)            t          $ r t          &                    d*           Y nw xY wd+}||k     rD| j        r=t)          j        tO          d||z
                       d{V  |dz  }||k     r| j        =| j        dS dS )-u  Embedded kanban dispatcher — one tick every `dispatch_interval_seconds`.

        Gated by `kanban.dispatch_in_gateway` in config.yaml (default True).
        When true, the gateway hosts the single dispatcher for this profile:
        no separate `hermes kanban daemon` process needed. When false, the
        loop exits immediately and an external daemon is expected.

        Each tick calls :func:`kanban_db.dispatch_once` inside
        ``asyncio.to_thread`` so the SQLite WAL lock never blocks the
        event loop. Failures in one tick don't stop subsequent ticks —
        same pattern as `_kanban_notifier_watcher`.

        Shutdown: the loop checks ``self._running`` between ticks; gateway
        stop() flips it to False and cancels pending tasks, and the
        in-flight ``to_thread`` returns on its own after the current
        ``dispatch_once`` call finishes (typically <1ms on an idle board).
        r   r  z6kanban dispatcher: config loader unavailable; disabledN!HERMES_KANBAN_DISPATCH_IN_GATEWAYr>   )0falsenor  zEkanban dispatcher: disabled via HERMES_KANBAN_DISPATCH_IN_GATEWAY envz4kanban dispatcher: cannot load config (%s); disabledkanbandispatch_in_gatewayTzGkanban dispatcher: disabled via config kanban.dispatch_in_gateway=falser  z@kanban dispatcher: kanban_db not importable; dispatcher disableddispatch_interval_secondsr  r  	max_spawnzkanban dispatcher: max_spawn=failure_limitzDkanban dispatcher: invalid kanban.failure_limit=%r; using default %dr   zGkanban dispatcher: kanban.failure_limit=%r is below 1; using default %dr     rv  r   zOptional[object]c                    d}	                      |           }	                     |            n# t          $ r Y nw xY w                    ||           |&	 |                                 S # t          $ r Y S w xY wS # t          $ rI t
                              d|            Y |(	 |                                 dS # t          $ r Y dS w xY wdS w xY w# |&	 |                                 w # t          $ r Y w w xY ww xY w)a  Run one dispatch_once for a specific board.

            Runs in a worker thread via `asyncio.to_thread`. `board=slug`
            is passed through `dispatch_once` so `resolve_workspace` and
            `_default_spawn` see the right paths. The per-board DB is
            opened explicitly so concurrent boards never share a
            connection handle or accidentally claim across each other.
            Nr  )r	  rK  rL  z*kanban dispatcher: tick failed on board %s)r  r  r   dispatch_oncer  r*  	exception)rv  r  r  rL  rK  s     r"   _tick_once_for_boardzFGatewayRunner._kanban_dispatcher_watcher.<locals>._tick_once_for_boardP  s    D{{{..KKdK++++    D(('"/	 )   #

$    $	      !MtTTT#

$    $#	 #

$    $s   B 3 B 
A B A  B A33
B ?B %C)C -C 
CCCC DC32D3
D =D?D  D"list[tuple[str, Optional[object]]]c                     	                      d          } n+# t          $ r                     j                  g} Y nw xY wg }| D ]>}|                    d          pj        }|                    | |          f           ?|S )a  Run one dispatch_once per board. Returns (slug, result) pairs.

            Enumerating boards on every tick keeps the dispatcher honest
            when users create a new board mid-run: no restart required,
            the next tick picks it up automatically.
            Fr  rv  )r  r   r  r  rA   rN  )r  outbrv  r  rQ  s       r"   
_tick_oncez<GatewayRunner._kanban_dispatcher_watcher.<locals>._tick_oncep  s    F%@@ F F F11#2CDDEF8:C ? ?uuV}}9(9

D"6"6t"<"<=>>>>Js    %AAc                     	                      d          } n+# t          $ r                     j                  g} Y nw xY w| D ]}|                    d          pj        }d}	                     |          }                    |          r0	 |*	 |                                  dS # t          $ r Y  dS w xY w dS n8# t          $ r+ Y |&	 |                                 # t          $ r Y w xY ww xY w	 |&	 |                                 # t          $ r Y w xY w# |&	 |                                 w # t          $ r Y w w xY ww xY wdS )a~  Cheap probe: is there at least one ready+assigned+unclaimed
            task on ANY board whose assignee maps to a real Hermes profile
            (i.e. one the dispatcher would actually spawn for)?

            Tasks assigned to control-plane lanes (e.g. ``orion-cc``,
            ``orion-research``) are pulled by terminals via
            ``claim_task`` directly and never spawnable, so a queue full
            of those is "correctly idle", not "stuck". Filtering them out
            here keeps the stuck-warn fire only on real failures (broken
            PATH, missing venv, credential loss for a real Hermes profile).
            Fr  rv  Nr  T)r  r   r  r  rA   r  has_spawnable_readyr  )r  rU  rv  r  r  s       r"   _ready_nonemptyzAGatewayRunner._kanban_dispatcher_watcher.<locals>._ready_nonempty  s   F%@@ F F F11#2CDDEF ! !uuV}}9(9!;;T;22D..t44 $# '! JJLLLLLL( ! ! ! DDD! (''$    '! JJLLLL( ! ! ! D! ($
 '! JJLLLL( ! ! ! D! (t'! JJLLLL( ! ! ! D! (
 5s    %AA)+CB//
B>=B>D'
C:D'C((
C54C59C::D'D
D#"D#'E+E ?E 
E	
EE	Ez7kanban dispatcher: embedded in gateway (interval=%.1fs)Fspawnedzckanban dispatcher [%s]: spawned=%d reclaimed=%d crashed=%d timed_out=%d promoted=%d auto_blocked=%d__len__r  zkanban dispatcher stuck: ready queue non-empty for %d consecutive ticks but 0 workers spawned. Check profile health (venv, PATH, credentials) and `hermes kanban list --status ready`.zkanban dispatcher: cancelledz+kanban dispatcher: unexpected watcher errorr  )r   rR  )(r   r  r   r*  r+  r?   r@   rA   r6   r_  r9  r1   rW   r  r   r5   DEFAULT_FAILURE_LIMITr4   rC   r7   r9  r  r)   r3   r7  r$  r%   rK  rZ  	reclaimedr  r  r  promotedauto_blockedrM   CancelledErrorr:  rP  min)r_  _load_configenv_overrider   r2  
kanban_cfgr  raw_failure_limitHEALTH_WINDOW	bad_tickslast_warn_atrV  rY  resultsany_spawnedrv  resready_pendingrJ   sleptr  rQ  rL  rK  s                       @@@@r"   r  z(GatewayRunner._kanban_dispatcher_watcher  s7     ,	EEEEEEE 	 	 	NNSTTTFF	 z~~&I2NNTTVV\\^^666KK_```F	,..CC 	 	 	NNQSVWWWFFFFF	 /9d.C.CKSWWXr***
~~3T:: 	KKY   F	3333333 	 	 	NN]^^^FF	 (CRHHNBOOc>>H NN;55	 KKC	CCDDD&NN?C<UVV	6 122MM:& 	6 	6 	6NNV!)  
  5MMM	6 1NNY!)  
  5M
 mA
 		s 	/A 	 	 	 	 	 	 	 	@	 	 	 	 	 	 	"	 	 	 	 	 	 	B 	Ex	
 	
 	
 m 0	(P ' 1* = =======#")-R  ID#73	4+H+H&* R ,,M07Y0O0OVC,,,UV29#-2S2SZC...YZL5<S=My5Y5Y`C 0111_`
 
 
 '.&7&H&H H H H H H H  " "NII !I--dikk**C\)S00C &   (+)   ;<<< P P P  !NOOOOOP
 E(""t}"mCX-=$>$>????????? (""t}"] m 0	 0	 0	 0	 0	s\    $77
B* *
C4CC=E $E,+E,-G= =9H98H9E%P9 9AR	R	c           
        K   d}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         k     r'|d         |k    r0t          
                    d	|j        |d                    | j        |= c|d
         }|d         dz   }t                              d|j        ||           	 |                     ||          }	|	s)t          
                    d|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# t:          $ r Y nw xY w|	j        r^|	j        sW|                     |j        d|	j         |	j!                   t          
                    d|j        |	j!                   | j        |= n|                     |j        d|	j         |	j!        pd           tE          dd|dz
  z  z  |          }||d<   t          j                    |z   |d<   t                              d|j        |           # t:          $ r}|                     |j        ddtG          |                     tE          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 )u  Background task that periodically retries connecting failed platforms.

        Uses exponential backoff: 30s → 60s → 120s → 240s → 300s (cap).
        Stops retrying a platform after 20 failed attempts or if the error
        is non-retryable (e.g. bad auth token).
           r  
   Nr  r   r  r  z+Giving up reconnecting %s after %d attemptsr  z"Reconnecting %s (attempt %d/%d)...zGReconnect %s: adapter creation returned None, removing from retry queuery  r  u   ✓ %s reconnected successfullyr   r  r  zAReconnect %s: non-retryable error (%s), removing from retry queuer  zfailed to reconnectri  z&Reconnect %s failed, next retry in %dsz)Reconnect %s error: %s, next retry in %ds)$r9  r  r7  rL  r  rM   r  r.  r  r*  r+  r   r9  r  r  r  r  r%  r  r5  r  r  r  r   r  r6  r  r  r  r   r  r   r  r  ra  r)   )r_  _MAX_ATTEMPTS_BACKOFF_CAPrn  rJ   r   r9  r$  attemptrY  r  r  backoffrb  s                 r"   r  z)GatewayRunner._platform_reconnect_watcher  s      mBm r	') r + +A= !-**********.""C !7!<!<!>!>?? b b} FF-h7l+++
#}44NNE Z(8   .x8"&x.z*Q.8NG]  
L"228_MMG" !e$N   !28< //0DEEE33D4TUUU--d.@AAA44T5]^^^$($F$FwPX$Y$YYYYYYYG .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! #2 7;X  @@ (/6+2+C.5.I	 A    #NN c (0K   !% 6x @ @ @@ (/9+2+C.5.I.bMb	 A    '*"gk0B*C\&R&RG/6D,151A1AG1KD."KK H (   !   88 '1#'&)!ff	 9    ""gk(:";\JJG'.D$)-)9)9G)CD&NNC 7         2YY ' '} FFmA&&&&&&&&&&e m r	' r	' r	' r	' r	'sF   9A N:C<N7JN
J!N J!!C*N
P&BP!!P&r4  r-  r5  r6  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                  D  K   dt           dd fd} t                              dj        rdnd           d_        d_                                         d {V  j        }                    |           d {V \  }}|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t(          nt*                     t-          j                                                    dz   }j        rt-          j                                                    |k     r_                    d           t-          j        d           d {V  j        r)t-          j                                                    |k     _ | d           j        rUj        rN	                                  d {V  n2# t"          $ r%}	t                              d|	           Y d }	~	nd }	~	ww xY w                    |           t?          dd           }
t?          dd           }|
||
5  t          |                                           }|!                                 d d d            n# 1 swxY w Y   |D ]6}tE          |tF                    r|d         n|}$                    |           7t          j%                                                  D ]\  }}	 |&                                 d {V  n8# t"          $ r+}	t                              d|j'        |	           Y d }	~	nd }	~	ww xY w	 |(                                 d {V  t                              d|j'                   # t"          $ r+}	t                              d|j'        |	           Y d }	~	d }	~	ww xY wt          j)                  D ] }|j*        u r|+                                 !j)        !                                 j%        !                                 j        !                                 j,        !                                 j-        !                                 j.        !                                 t_          d          rj0        !                                 j1        2                                  | d           	 ddl3m4}  |             n2# t"          $ r%}t                              d|           Y d }~nd }~ww xY wt?          dd           fD ]q}|rt?          |dd           nd }|t_          |d          s*	 |5                                 @# t"          $ r%}t                              d|           Y d }~jd }~ww xY wdd l6m7}m8}  |              |             |s.	 tr          d!z  :                                 n*# t"          $ r Y nw xY wt                              d"           |r4;                    te          |<                                                     j        r!j=        rt|          _?        j@        pd#_@        d_                            d$j@                   t                              d%           d S )&N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   r  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)r4  r  kill_allr*  r9  r   r:  tools.terminal_toolry  tools.browser_toolrz  )rw  r  _killedr  ry  rz  s         r"   _kill_tool_subprocesseszGGatewayRunner.stop.<locals>._stop_impl.<locals>._kill_tool_subprocessesg  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 restartr>   FTzYGateway drain timed out after %.1fs with %d active agent(s); interrupting remaining work.r=  r>  z%mark_resume_pending failed for %s: %sr  r  r  zpost-interruptz-Failed to launch detached gateway restart: %srJ  rH  r   u'   ✗ %s background-task cancel error: %su   ✓ %s disconnectedu   ✗ %s disconnect error: %srC  zfinal-cleanup)shutdown_cached_clientsz!shutdown_cached_clients error: %sr5  _dbr  zSessionDB close error: %s)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.zGateway restart requestedstoppedzGateway stopped)Ar)   r*  r9  r  r7  r  r  r  r  r+  r+  r.  r?  r  r  r5  mark_resume_pendingr   r:  r  !_INTERRUPT_REASON_GATEWAY_RESTART"_INTERRUPT_REASON_GATEWAY_SHUTDOWNr9  r  rM   r^  r  r  r-  r  r  r%   r  r  r1   r  r  r   cancel_background_tasksr   r  r^  r  cancelr  r@  rK  r  rC  r;  r]  r  r  r  r]  r  r  rv   touchr  r  r  r  r  r>  )r  r   r\  r  _resume_reason_sk_agentr  interrupt_deadlinerb  r  _cache_idle_agents_entryr   rY  _taskr  
_db_holderr  r  r  r_  s                         r"   
_stop_implz&GatewayRunner.stop.<locals>._stop_implf  s      Ss  St  S  S  S  SD KK'"&"9Ar   "DM!DN ::<<<<<<<<<1G-1-F-Fw-O-O'O'O'O'O'O'O$M9 8: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& 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%)$-*=*=*?*?%@%@ 	S 	S!'_!99;;;;;;;;;;  _ _ _LL!JHN\]^^^^^^^^_S!,,.........KK 5x~FFFF  S S SLL!>PQRRRRRRRRS d455  DO++"((***M!!! &&(((#))+++"((***#))+++t^,, *!''))) $$&&& $#O444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 UTTTTTTTO((***  
!$55<<>>>>    D '    R66s=;M;M;O;O7P7PQQQ& U4+D U"C$($5$T9T!"DN''	43DEEEKK)*****s   4D
E D;;E <I 
J!JJ6LLL8N
O!OO:P
P<!P77P<U) )
V3VVW..
X8XX?Y 
Y)(Y)r8  )r  r  r  r  r9  r9  )r_  r-  r5  r6  r  s   `    r"   r"  zGatewayRunner.stopV  s        	8&*D#%5D"(7D%?&/!!!!!!!Fg	+ g	+ g	+ g	+ g	+ g	+R "-jjll;;or$   c                 H   K   | j                                          d{V  dS )zWait for shutdown signal.N)r;  waitr  s    r"   wait_for_shutdownzGatewayRunner.wait_for_shutdownR  s3      "'')))))))))))r$   c                    t          |d          rnt          |j        t                    rT|j                            d| j        j                   |j                            dt          | j        dd                     	 ddlm	} |
                    |j                  rA|                    |j        |          }||S t                              d|j                   dS n8# t          $ r+}t                              d	|j        |           Y d}~nd}~ww xY w|t"          j        k    r9dd
lm}m}  |            st                              d           dS  ||          S |t"          j        k    rBd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"          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                              d)           dS  |$|          S |t"          jT        k    rBdd*lUmV}&mW}'  |'            st                              d+           dS  |&|          }| |_        |S |t"          jX        k    r9dd,lYmZ}(m[})  |)            st                              d-           dS  |(|          S |t"          j\        k    r9dd.l]m^}*m_}+  |+            st                              d/           dS  |*|          S |t"          j`        k    r1dd0lamb},mc}- |-st                              d1           dS  |,|          S dS )2zCreate 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.
        extrar  r  Fr   r~  NzWPlatform '%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)DiscordAdaptercheck_discord_requirementsz!Discord: discord.py not installed)WhatsAppAdaptercheck_whatsapp_requirementsz8WhatsApp: Node.js not installed or bridge not configured)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)HomeAssistantAdaptercheck_ha_requirementsz:HomeAssistant: aiohttp not installed or HASS_TOKEN not set)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_requirementsz*WeComCallback: aiohttp/httpx 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)MattermostAdaptercheck_mattermost_requirementszJMattermost: MATTERMOST_TOKEN or MATTERMOST_URL not set, or aiohttp missing)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)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)dr  r1   r  rW   r;  r  r  r%   r  r  is_registeredr   create_adapterr*  r  r   r:  r  r  gateway.platforms.telegramr  r  r+  DISCORDgateway.platforms.discordr  r  gateway_runnerWHATSAPPgateway.platforms.whatsappr  r  SLACKgateway.platforms.slackr  r  SIGNALgateway.platforms.signalr  r  HOMEASSISTANTgateway.platforms.homeassistantr  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  
MATTERMOSTgateway.platforms.mattermostr  r  MATRIXgateway.platforms.matrixr  r  rj  gateway.platforms.api_serverr  r  rk  gateway.platforms.webhookr  r  BLUEBUBBLESgateway.platforms.bluebubblesr  r  QQBOTgateway.platforms.qqbotr  r  YUANBAOgateway.platforms.yuanbaor  r  ).r_  r   r  r  rY  rb  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  r  r  r  r  r  s.                                                 r"   r  zGatewayRunner._create_adapterV  s    67## 	
6<(F(F 	L##)3   L##*%?GG  	\CCCCCC ..x~>> +::8>6RR&"N 6N  
 t  	\ 	\ 	\LLGYZ[[[[[[[[	\ x(((________..00 LMMMt"?6***)))\\\\\\\\--// BCCCt$nV,,G%)G"N***________..00 YZZZt"?6***''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 =(((///cccccccc((** [\\\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 KLLLt''///''VVVVVVVV++-- \]]]t<'''((YYYYYYYY,,.. KLLLt =(((,,,eeeeeeee0022 klllt$$V,,,((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---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!>&)))ts    >C" ? C" "
D,!DDc                    |j         t          j        t          j        fv rdS |j        }|sdS i t          j        dt          j        dt          j        dt          j        dt          j	        dt          j
        dt          j        d	t          j        d
t          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        d}t          j        di}t          j        dt          j        di}i t          j        dt          j        dt          j        dt          j        dt          j	        dt          j
        dt          j        dt          j        dt          j        dt          j        d t          j        d!t          j        d"t          j        d#t          j        d$t          j        d%t          j        d&t          j        d'}t          j        d(t          j        d)i}|j         |vre	 d*d+lm} |                    |j         j                  }	|	r,|	j        r|	j        ||j         <   |	j        r|	j        ||j         <   n# t8          $ r Y nw xY w|                    |j         d,          }
|
r+t;          j        |
d,                                          d-v rdS tA          |d.d          rY|                    |j                   }|r=t;          j        |d/                                          !                                d0v rdS |j         t          j        k    r)t;          j        d1d,          !                                rdS |j         r|j         j        nd,}| j"        #                    ||          rdS t;          j        |                    |j         d,          d,          !                                }d,}d,}|j$        d2v rt;          j        |                    |j         d,          d,          !                                }t;          j        |                    |j         d,          d,          !                                }t;          j        d3d,          !                                }|s/|s-|s+|s)t;          j        d4d,                                          d-v S |r>|j$        d2v r5|j%        r.d5 |&                    d6          D             }d7|v s	|j%        |v rdS |j         t          j        k    r|r|j$        d2v r|j%        rd8 |&                    d6          D             }|r^tA          | d9d          sBtN          (                    d:d6)                    tU          |                               d| _+        |j%        |v rdS tY                      }|r2|-                    d; |&                    d6          D                        |r2|-                    d< |&                    d6          D                        |r2|-                    d= |&                    d6          D                        d7|v rdS |h}d>|v r.|.                    |&                    d>          d*                    |j         t          j        k    rtY                      }|D ]$}|-                    t_          |                     %|r|}|-                    t_          |                     ta          |          }|r|.                    |           tc          ||z            S )?ao  
        Check if a user is authorized to use the bot.
        
        Checks in order:
        1. Per-platform allow-all flag (e.g., DISCORD_ALLOW_ALL_USERS=true)
        2. Environment variable allowlists (TELEGRAM_ALLOWED_USERS, etc.)
        3. DM pairing approved list
        4. Global allow-all (GATEWAY_ALLOW_ALL_USERS=true)
        5. Default: deny
        TFrX  rY  rZ  r[  r\  r`  ra  rb  rc  rd  re  rf  rg  rh  ri  rj  rk  r^  r_  QQ_GROUP_ALLOWED_USERSrm  rn  ro  rp  rq  rr  rs  rt  ru  rv  rw  rx  ry  rz  r{  r|  r}  DISCORD_ALLOW_BOTSFEISHU_ALLOW_BOTSr   r~  r>   r  is_botr~  )mentionsr  DISCORD_ALLOWED_ROLES>   forumr    rl  r  c                 ^    h | ]*}|                                 |                                 +S rw   r6   )r  r  s     r"   r  z4GatewayRunner._is_user_authorized.<locals>.<setcomp>  sB     ! ! !$+RYR_R_RaRa!! ! !r$   ,*c                     h | ]=}|                                                     d           )|                                 >S )rm  )r6   rL  ro  s     r"   r  z4GatewayRunner._is_user_authorized.<locals>.<setcomp>  sL       7799'',,		  r$   #_warned_telegram_group_users_legacyu   TELEGRAM_GROUP_ALLOWED_USERS contains chat-ID-shaped values (%s). Treating them as chat IDs for backward compatibility. Move chat IDs to TELEGRAM_GROUP_ALLOWED_CHATS — the _USERS var is now for sender user IDs.c              3   f   K   | ],}|                                 |                                 V  -d S r  r  r  uids     r"   r  z4GatewayRunner._is_user_authorized.<locals>.<genexpr>  s<      ccsWZW`W`WbWbcsyy{{ccccccr$   c              3   f   K   | ],}|                                 |                                 V  -d S r  r  r  s     r"   r  z4GatewayRunner._is_user_authorized.<locals>.<genexpr>  s<      eesY\YbYbYdYdesyy{{eeeeeer$   c              3   f   K   | ],}|                                 |                                 V  -d S r  r  r  s     r"   r  z4GatewayRunner._is_user_authorized.<locals>.<genexpr>  s<      aasUXU^U^U`U`asyy{{aaaaaar$   r  )2r   r  r  rk  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rA   r   r  r  r   r?   r)  r_  r%   r6   rW  is_approvedr  r  r^  r*  r+  rO  r  r  r]  r  r  _expand_whatsapp_auth_aliases_normalize_whatsapp_identifierr3   )r_  r  r  platform_env_mapplatform_group_user_env_mapplatform_group_chat_env_mapplatform_allow_all_mapplatform_allow_bots_mapr  r@  platform_allow_all_varallow_bots_varri   platform_allowlistgroup_user_allowlistgroup_chat_allowlistglobal_allowlistallowed_group_idslegacy_chat_idsallowed_ids	check_idsnormalized_allowed_ids
allowed_idnormalized_user_ids                           r"   r  z!GatewayRunner._is_user_authorized  sf     ?x5x7GHHH4. 	5
7
5
 7
 N1	

 O3
 N1
 L-
 !;
 O3
 7
 O3
 N1
 #%C
 O3
  "=
  N.!
" 5#
( ='
# =N4'
#"
9"
7"
 9"
 N3	"

 O5"
 N3"
 L/"
 !="
 O5"
 9"
 O5"
 N3"
 #%E"
 O5"
  "?"
  N0!"
" 7#"
* 2O0#
 ?"222	GGGGGG)--fo.CDD V. T<A<S(9* VBGBU.v?    "8!;!;FOR!P!P! 	bi0F&K&K&Q&Q&S&SWk&k&k468U++ 	488IIN ")NF"C"C"I"I"K"K"Q"Q"S"SWj"j"jt Ox///	1266<<>> 0 4 28H--b))-AA 	4  Y'7';';FOR'P'PRTUU[[]]!!111#%9-H-L-LV_^`-a-ace#f#f#l#l#n#n #%9-H-L-LV_^`-a-ace#f#f#l#l#n#n 9%<bAAGGII! 	\*> 	\G[ 	\dt 	\96;;AACCG[[[
   	F$48J$J$Jv~$J! !/C/I/I#/N/N! ! ! '''6>=N+N+Nt Ox000$ 1 $666 7 -33C88  O
   t%JERR DNN6 !8!899   @DD<>_444 ee 	dcc6H6N6Ns6S6Scccccc 	fee6J6P6PQT6U6Ueeeeee 	baa6F6L6LS6Q6Qaaaaaa +4I	'>>MM'--,,Q/000 ?h///%(UU") Y Y
&--.KJ.W.WXXXX% 54:7CCDDD!?!H!H! 20111I+,,,s   7AJ 
JJc                 t   t          | dd          }|rht          |d          rX|rVt          |d          r|j                            |          nd}|r(dt          |di           v r|                    |          S |r"t          |d          r|j        dk    r|j        S |rli t          j        dt          j        d	t          j	        d
t          j
        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        dt          j        d}t          j        dt          j        di}t/          j        |                    |d          d                                          rdS |                    |d          D ],}t/          j        |d                                          r dS -t/          j        dd                                          rdS dS )u  Return how unauthorized DMs should be handled for a platform.

        Resolution order:
        1. Explicit per-platform ``unauthorized_dm_behavior`` in config — always wins.
        2. Explicit global ``unauthorized_dm_behavior`` in config — wins when no per-platform.
        3. When an allowlist (``PLATFORM_ALLOWED_USERS``,
           ``PLATFORM_GROUP_ALLOWED_USERS`` / ``PLATFORM_GROUP_ALLOWED_CHATS``,
           or ``GATEWAY_ALLOWED_USERS``) is configured, default to ``"ignore"`` —
           the allowlist signals that the owner has deliberately restricted
           access; spamming unknown contacts with pairing codes is both noisy
           and a potential info-leak. (#9337)
        4. No allowlist and no explicit config → ``"pair"`` (open-gateway default).
        r  Nget_unauthorized_dm_behaviorr!  unauthorized_dm_behaviorr  pairrX  rY  rZ  r[  r\  r`  ra  rb  rc  rd  re  rf  rg  rh  ri  rj  )r^  r_  )r  r>   ignorerw   rl  )r%   r  r!  rA   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r?   r)  r6   )r_  r   r  r  r   platform_group_env_mapenv_keys          r"   _get_unauthorized_dm_behaviorz+GatewayRunner._get_unauthorized_dm_behavior  s    x..  	Egf&DEE 	E( 	E=DV[=Y=Yc6+//999_cL E :glT[]_>`>` ` `::8DDD  	7gf&@AA 	7.&8866
  	$ !#;  #:  !#;  #8	 
 #9  #8  #6  #%?  #9  !#;  #9  #8  ')G  #9  $&A   #5! & ! $  ;&" y)--h;;R@@FFHH  x155hCC $ $9Wb))//11 $#88$ 9,b117799 	8vr$   rr  c           	      n  K   | j                             |j                  }|sdS t          | dd          }d}|r*t	          |d          r|                    |j                  }t          |dd          r	d|j        ind}|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_deliveryr  privater  r  r  Fz7[%s] send_private_notice failed, falling back to publicr   r  Trq  )r   rA   r   r%   r  r  r  send_private_noticer  r  r   r*  r:  r  )r_  r  rr  rY  r  notice_deliveryr  r  s           r"   _deliver_platform_noticez&GatewayRunner._deliver_platform_notice   s     -##FO44 	Fx.." 	Jgf&;<< 	J$88IIO6=fkSW6X6XbK!122^b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                 E  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        't                              d|j        j                   dS |                     |          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.} |/                    |          }d}	 dd0l0m1}  ||          }n# t          $ r d}Y nw xY w|r|s|j         pd!                                }|"                                }d} |d1v rd2} nX|d3v rd4} nQ|d5v rd6} nJ|2                                d7v rd2} n1|2                                d8v rd4} n|2                                d9v rd6} | 4|3                    ||                    d:          |            d{V }!|!pdS |4                    |           tk          d;d<          }"| j6                            |d          }#|| j7        v rz|#rwtq          j8                    |#z
  }$| j7                            |          }%ts          d=          }&d}'|%rtu          |%d>          r	 |%;                                }(|(                    d?ts          d=                    }&d@|(                    dAd           dB|&dCdD|(                    dEd           dF|(                    dGd           }'n# t          $ r Y nw xY w|"dk    rty          |"dHz  dI          nts          d=          })|%tz          uo|"dk    r|&|"k    p|$|)k    }*|*rKt                              dJ||$|&|"|'           | >                    |dKL           | ?                    |           || j7        v 
r2|"                                dMk    r| @                    |           d{V S ddNl#mA}+m$}, |"                                }-|-r |,|-          nd}.|.r&|.j%        dOk    r| B                    |           d{V S |.rY|.j%        dPk    rN| C                    ||t          dQR           d{V  t                              dS|           t          dT          S |.rJ|.j%        dUk    r?| C                    ||t          dVR           d{V  | G                    |           d{V S |"                                dWv r|H                                !                                }/|/sdXS | j                            |j                  }|rDt          |/t          jK        |j         |jL        |jM        Y          }0| N                    ||0|           | O                    || j                            |j                  Z          }1|1d[k    rd\S d]|1 d^S |.r|.j%        d_k    r|H                                !                                }2|2sd`S | j7                            |          }3|3tz          u rZ| j                            |j                  }|r7t          |2t          jK        |j         |jL        |jM        Y          }0|0|jP        |<   daS |3rtu          |3d_          r{	 |3Q                    |2          }4n8# t          $ r+}5t                              db||5           dc|5 cY d}5~5S d}5~5ww xY w|4r(|2ddd         tY          |2          ddk    rdendz   }6df|6 dgS dhS | j                            |j                  }|r7t          |2t          jK        |j         |jL        |jM        Y          }0|0|jP        |<   diS |.r|.j%        djk    rdkS |.rJ|.j%        dlv rA|.j%        dmk    r| R                    |           d{V S | S                    |           d{V S |.r&|.j%        dnk    r| T                    |           d{V S |.r&|.j%        dok    r| U                    |           d{V S |.r&|.j%        dpk    r| V                    |           d{V S |.rh|.j%        dqk    r]|H                                pd!                                2                                }7|7r|7drv r| W                    |           d{V S dsS |.r{|.j%        dtv rr|.j%        duk    r| X                    |           d{V S |.j%        dvk    r| Y                    |           d{V S |.j%        dwk    r| Z                    |           d{V S |.r|.j%        |+v r|.j%        dxk    r| [                    |           d{V S |.j%        dyk    r| \                    |           d{V S |.j%        dzk    r| ]                    |           d{V S |.j%        d{k    r| ^                    |           d{V S |.rd||.j%         d}S |j_        t          j`        k    rTt                              d~|           | j                            |j                  }|rt          |jP        ||           dS ts          t          jc        dd                    }8| j6                            |d          }9|j        t          je        k    r|j_        t          jK        k    r|8dk    r|9rtq          j8                    |9z
  |8k    rkt                              dtq          j8                    |9z
  |           | j                            |j                  }|rt          |jP        ||d%           dS | j7                            |          }3|3tz          u r|"                                dPk    r?| ?                    |           t                              d|           t          d          S | j                            |j                  }|rt          |jP        ||d%           dS | jf        rn| g                                r| h                    ||           | g                                rd| i                                 dnd| i                                 dS | jj        dk    r3t                              d|           | h                    ||           dS | jj        d_k    r|j         pd!                                }2d}:|2ritu          |3d_          rY	 t          |3Q                    |2                    }:n5# t          $ r(}5t                              d||5           d}:Y d}5~5nd}5~5ww xY w|:rt                              d|           dS t                              d|           | h                    ||           dS t                              d|           |3k                    |j                    || jP        v r| jP        |xx         d|j         z   z  cc<   n|j         | jP        |<   dS |"                                };ddl#ml}<mm}=m$}> |;r |>|;          nd}|r|j%        n|;}?|;rd|at          | jn        t                    r| jn                            di           pi }@nt          | jn        di           pi }@t          |@t                    r|;|@v r|@|;         }A|A                    d          dk    r|A                    dd          !                                }B|Br|Bo                    dF          r|BndF|B }B|Bp                    dF          }C|H                                !                                }D|B d|D !                                |_         |Cr|Cq                                d         n|C};|;r |>|;          nd}|r|j%        n|;}?|;rg |=|?          r[|H                                !                                }E|j        r|j        j        nd|j        |?|;|E|Ed}F	 | jr        s                    d|? |F           d{V }Gn5# t          $ r(}Ht                              d|?|H           g }GY d}H~Hnd}H~Hww xY w|GD ]}It          |It                    st!          |I                    dd                    !                                2                                }J|Jr|Jdk    ri|Jdk    r8|I                    d          }Kt          |Kt                     r|Kr|Kc S d|; dc S |Jdk    r2|I                    d          }Kt          |Kt                     r|Kr|Kndc S |Jdk    rt!          |I                    dd                    !                                p                    dF          }L|Ls1t!          |I                    dd                    !                                }MdF|L d|M !                                |_         |"                                };|;r |>|;          nd}|r|j%        n|;}? n|?dUk    rD| t                    |          r| u                                S | G                    |           d{V S |?dk    r| v                    |           d{V S |?dxk    r| [                    |           d{V S |?dyk    r| \                    |           d{V S |?dzk    r| ]                    |           d{V S |?dMk    r| @                    |           d{V S |?dnk    r| T                    |           d{V S |?dOk    r| B                    |           d{V S |?dPk    r| w                    |           d{V S |?dk    r| x                    |           d{V S |?dk    r| y                    |           d{V S |?dvk    r| Y                    |           d{V S |?dwk    r| Z                    |           d{V S |?duk    r| X                    |           d{V S |?djk    r| z                    |           d{V S |?dk    r| {                    |           d{V S |?dpk    r| V                    |           d{V S |?dk    r| |                    |           d{V S |?dk    r| }                    |           d{V S |?dk    r| ~                    |           d{V S |?dk    r|                     |           d{V S |?dk    r|                     |           d{V S |?dk    r|                     |           d{V S |?dk    r|                     |           d{V S |?dk    r|                     |           d{V S |?dmk    r| R                    |           d{V S |?dk    r| S                    |           d{V S |?d{k    r| ^                    |           d{V S |?dk    r|                     |           d{V S |?dk    r|                     |           d{V S |?dk    r|                     |           d{V S |?dk    r|                     |           d{V S |?dk    r|                     |           d{V S |?dok    r| U                    |           d{V S |?d_k    rC|H                                !                                }N|NsdS 	 |N|_         n# t          $ r Y nw xY w|?dqk    r| W                    |           d{V S |?dk    r|                     |           d{V S | jf        rd| i                                 dS |;r[t          | jn        t                    r| jn                            di           pi }@nt          | jn        di           pi }@t          |@t                    si }@|;|@v r|@|;         }A|A                    d          dk    r|A                    dd          }O|Or	 t          j        |Ot          j        j        t          j        j                   d{V }Pt          j        |P                                d           d{V \  }Q}R|Qp|R                                !                                }S|Sr|SndS # t          j        $ r Y dS t          $ r}d| cY d}~S d}~ww xY wd|; dS |A                    d          dk    r|A                    dd          !                                }B|Br|Bo                    dF          r|BndF|B }B|Bp                    dF          }C|H                                !                                }D|B d|D !                                |_         |Cr|Cq                                d         n|C};nd|; dS d|; dS |;r	 ddlm}T  |T|;                    dd¦                    }U|Ura|H                                !                                }D |U|D          }Vt          j        |V          r|V d{V }V|Vrt!          |V          ndS n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|;rF	 ddlm}Wm}Xm}Y  |W            }Z |Y|;          }[|[|Z|[                             dd          }\|j        r|j        j        nd}]|]r|\rddlm}^ |\ |^|]Ǧ          v r	d|\ d|] dʝS |H                                !                                }_ |X|[|_|˦          }`|`r|`|_         nat5          |;          }a|ar|aS |;                    dd¦          |<v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| t                    |          r+|                     |          r|                                 S dS tz          | j7        |<   tq          j8                    | j6        |<   |                     |          }b	 |                     ||||b           d{V }c	 d}dt          |ct                    r%t!          |c                    dѦ          pd          }dnt          |ct                     r|c}d|d!                                rN	 | j                            |          }en# t          $ r d}eY nw xY w|e|                     |e||dҦ           d{V  n2# t          $ r%}ft                              d|f           Y d}f~fnd}f~fww xY w|c| j7                            |          tz          u r| ?                    |           S | j6        +                    |d           tu          | dԦ          r| j        +                    |d           S S # | j7                            |          tz          u r| ?                    |           w | j6        +                    |d           tu          | dԦ          r| j        +                    |d           w 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
        rF  Fr   r  pre_gateway_dispatch)rP  r  r5  z*pre_gateway_dispatch invocation failed: %sNr  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  r  r>   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!rM  )approverT  y)denyrG  nresolve_command.update_response.update_prompt.json.tmpTr  z#Failed to write update response: %su/   ✗ Failed to send response to update process: ro     …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slash_confirm)has_blocking_approval)r'  rT  okconfirmonce)alwaysrememberr7  )r  rG  r)  	nevermindr  )r'  zapprove oncer6  )r7  zalways approve)r  r9  rG  
confirm_idr     infr  seconds_since_activityz | last_activity=last_activity_descr  .0fzs ago) | iteration=r  r   r  rp  i   zWEvicting stale _running_agents entry for %s (age: %.0fs, idle: %.0fs, timeout: %.0fs)%sstale_running_agent_evictionr&  r*  )ACTIVE_SESSION_BYPASS_COMMANDSr,  r-  r"  stop_commandinterrupt_reasoninvalidation_reasonu@   STOP for session %s — agent interrupted, session lock released+   ⚡ Stopped. You can continue this session.newnew_command)r6  qzUsage: /queue <prompt>r   rF  r  r  channel_promptrE  r   zQueued for the next turn.zQueued for the next turn. (z queued)r7  zUsage: /steer <prompt>u9   Agent still starting — /steer queued for the next turn.zSteer failed for session %s: %su   ⚠️ Steer failed: r  ...u8   ⏩ Steer queued — arrives after the next tool call: 'rl  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.)r'  r)  r'  agents
backgroundrH  goal)r*  pauseresumer  r"  r!  ub   Agent is running — use /goal status / pause / clear mid-run, or /stop before setting a new goal.)yoloverboserS  rT  footerhelpcommandsprofiler  u   ⏳ 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 interrupt)
merge_textu7   HARD STOP (pending) for session %s — sentinel cleareduE   ⚡ Force-stopped. The agent was still starting — session unlocked.r  r  r  r  r6  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 %sz!PRIORITY interrupt for session %srI  )GATEWAY_KNOWN_COMMANDSis_gateway_known_commandr,  quick_commandsr  aliasr  )r   r  r#  raw_commandr$  rp  zcommand:z/command:%s hook dispatch failed (non-fatal): %sdecisionr)  r[  z
Command `/z` was blocked by a hook.handledry  rp  topic	reasoningr  personalityretryundosethomecompressusageinsights
reload-mcpzreload-skillsr:  r&  rR  branchrollbackzJUsage: /steer <prompt>  (no agent is running; sending as a normal message)r  z) and is not accepting new work right now.execr#  )r!  r"  r  r  z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_handlerrn  rm  z.Plugin command dispatch failed (non-fatal): %s)get_skill_commandsbuild_skill_invocation_messageresolve_skill_command_keyrF   )get_disabled_skill_namesr  r  z** skill is disabled for z(.
Enable it with: `hermes skills config`r
  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): %sfinal_response)r  r  ru  z!goal continuation hook failed: %srC  )r  r3   r%   r  r  r5  r   r*  r+  r1   rW   rA   r9  r   r   r  r)   dataclassesr9   r  r:  r  r  r  r  rW  _is_rate_limitedgenerate_coder   r  _record_rate_limitr  r   r6   get_commandr&   r,  rF   rv   with_suffixr  r  r  r  rK  toolsr2  get_pendingtools.approvalr3  r_  r   clear_if_stalerI   r  r?  rM   r5   r  r  r  r  "_invalidate_session_run_generation_release_running_agent_state_handle_status_commandrB  _handle_restart_command_interrupt_and_clear_session_INTERRUPT_REASON_STOPr  _INTERRUPT_REASON_RESET_handle_reset_commandget_command_argsr  r  rL  r  rL  r>  rH  r@  r7  _handle_approve_command_handle_deny_command_handle_agents_command_handle_background_command_handle_kanban_command_handle_goal_command_handle_yolo_command_handle_verbose_command_handle_footer_command_handle_help_command_handle_commands_command_handle_profile_command_handle_update_commandrF  rM  r  r?   r)  r  r  r  r8  r  r4  r  r  r[  r\  r  rL  lstripr^  rY  emit_collectr  r  _handle_topic_command_handle_stop_command_handle_reasoning_command_handle_fast_command_handle_model_command_handle_personality_command_handle_retry_command_handle_undo_command_handle_set_home_command_handle_compress_command_handle_usage_command_handle_insights_command_handle_reload_mcp_command_handle_reload_skills_command_handle_debug_command_handle_title_command_handle_resume_command_handle_branch_command_handle_rollback_command_handle_voice_commandr9  create_subprocess_shellr$  PIPEr  communicatedecoder  ro  iscoroutineagent.skill_commandsrp  rq  rr  r  rs  r  r  r  _begin_session_run_generation_handle_message_with_agentget_or_create_session_post_turn_goal_continuationrC  )gr_  rP  r  is_internalr  _hook_results	_hook_exc_result_action	_new_textri   coderY  
_quick_key_update_promptsrD   r*  response_text_recognized_cmd_resolve_update_cmd_cmd_defresponse_pathprompt_pathtmprb  label_slash_confirm_mod_pending_confirm_tool_approval_liver3  
_raw_reply
_cmd_reply_confirm_choice	_resolved_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queued_textr9  rG  r  r  acceptedr2  preview	_goal_arg_telegram_followup_grace_started_atr  r#  r[  r\  _resolve_cmd	canonicalr]  qcmdr  target_command	user_argsrp  hook_ctxhook_results	_hook_errhook_resultr`  r[  rI  new_argssteer_payloadexec_cmdprocr!  r"  r  ro  plugin_handlerr  rp  rq  rr  
skill_cmdscmd_key_skill_namer  _get_plat_disableduser_instructionrX   _unavail_msg_run_generation_agent_result_final_textr  	_goal_excsg                                                                                                          r"   r  zGatewayRunner._handle_message?  ss-       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 &  *	^#
 LLCV_EZ[[[4))&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 	>=====-99*EE#	(<<<<<<"7"7
"C"C 	( 	( 	("'	( 	:$7 	:**1133J**,,J"O@@@"(555"*DDD"*!!##'JJJ"(!!##'CCC"*!!##'DDD"**"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 A."5"B"B!99%@@@@@@@@@  U."5"?"?77%;(6	 8          ^`jkkk%&STTT  
?."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  >."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{{  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  D."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  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<< /1"#'	    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LL<jIII##EJ///T333&z222dUZ6GG22225:Z&z2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	  .	//	:: .	--//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:::::::::33E:::::::::225999999999
""66u=========	!!55e<<<<<<<<<  44U;;;;;;;;;  44U;;;;;;;;;	!!55e<<<<<<<<<225999999999##77>>>>>>>>>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:::::::::%%99%@@@@@@@@@  44U;;;;;;;;;33E:::::::::225999999999	!!66u=========
""66u=========33E:::::::::
""66u=========$$88?????????'';;EBBBBBBBBB	!!55e<<<<<<<<<225999999999  44U;;;;;;;;;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33E:::::::::> 	mlT%?%?%A%Allll  '	l$+t,, R!%1A2!F!F!L"!(6F!K!K!Qrnd33 $!#.((%g.88F##v--#xx	266H U?)0)H ('.'9'>'.'9'>* * * $ $ $ $ $ $D
 4;3CDDTDTDVDV`b3c3c3c-c-c-c-c-c-cNFF&,&6%>%>%@%@%F%F%H%HF-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  	RR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:;  R R RMqQQQQQQQQR  :	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 ,C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  #''
337NNN11*==== '++J===400 <%))*d;;;;< #''
337NNN11*==== '++J===400 <%))*d;;;;<s  A 
A8A33A8	N NN%N< <O
O,AQ 
Q6Q1+Q61Q6A1U   
U/
U**U/&V8 8WW5B^8 8
__?m 
n
 n?n
n
E="AF  F 
AGF*AGGAGR
$AR/ R/
AS!R9ASSAS!mAm! m!
Am.m-Am.q;BAt tAut,	Aut5At?t9Aut?Aux3BAz< z<
A{+{A{&{&A{+{2A2B@ }%AB@ ~7AB@ @
B@4@B@/@/B@4B7BH> CA'BF D>BE EBF EBE(E%BF E'BE(E(#BF FBH> F
BF;FBF6F1BH> F6BF;F;BH> H>B BJ>rQ   c                  +K   |pg }|j         pd+t          | j        dd          }t          | j        dd          }|                     |          }|                     |           t          |||          }|r|j        rd|j         d+ +|j        rCg }g }	t          |j                  D ]\  }
}|
t          |j
                  k     r|j
        |
         nd}|                    d	          s|j        t          j        k    r|                    |           |                    d
          s|j        t          j        t          j        fv r|	                    |           |r|                                 }|dk    rWt          | dd          }|	i }|| _        t)          |          ||<   t*                              dt          |                     nEt*                              d|t          |                     |                     +|           d{V +|	r|                     +|	           d{V +d}t3          +fd|D                       r| j                            |j                  }|j        r	d|j        ind}|rP	 d}|                                 r|dz  }|                    |j         ||           d{V  n# tB          $ r Y nw xY w|j        r}|j        t          j"        k    rgddl#}ddl$m%} h d}t          |j                  D ]C\  }
}|
t          |j
                  k     r|j
        |
         nd}|dv rZtL          j'        (                    |          d         )                                }||v rd}n|*                    |          \  }}|r|}|                    d          stL          j'        +                    |          }|,                    dd          }t          |          dk    r|d         n|}t[          j.        d d|          } ||          }|                    d!          r
d"| d#| d$}n	d%| d&| d'}| d(+ +Et          |d)d          r|j/        r|j0        dd*         }d+| d,+ +d-+v r	 dd.l1m2} dd/l3m4}  tL          j5                            d0tL          j'        6                    d1                    }!to                      }"d}#	 tq                      }$|$                    d2i           }%ts          |%tt                    r&|%                    d3          }&|&tw          |&          }#n# tB          $ r Y nw xY w | | j<        | j=        p|"                    d4          pd|"                    d5          pd|#6          }' |+|!|'|!7           d{V }(|(j>        r^| j                            |j                  })|)r;|)                    |j         d8?                    |(j@                  pd9           d{V  dS |(jA        r|(jB        +n2# tB          $ r%}*t*          C                    d:|*           Y d}*~*nd}*~*ww xY w+S );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.
        r>   r  Tr  Fr  [] rE  rH  nativerB  NzSImage 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.)No STT providerzSTT is disabledzcan't listenVOICE_TOOLS_OPENAI_KEYc              3       K   | ]}|v V  	d S r  rw   )r  rP  message_texts     r"   r  z>GatewayRunner._prepare_inbound_message_text.<locals>.<genexpr>p  s(      NN&v-NNNNNNr$   r  u
  🎤 I received your voice message but can't transcribe it — no speech-to-text provider is configured.

To enable voice: install faster-whisper (`pip install faster-whisper` in the Hermes venv) and set `stt.enabled: true` in config.yaml, then /restart the gateway.z@

For full setup instructions, type: `/skill hermes-agent-setup`r  r   )to_agent_visible_cache_path>   .md.cfg.csv.ini.log.txt.xml.yml.json.toml.yaml)r>   zapplication/octet-streamr   z
text/plain)zapplication/text/rn  ri  rg  z	[^\w.\- ]r  z![The user sent a text document: 'zC'. Its content has been included below. The file is also saved at: rG  z[The user sent a document: 'z'. The file is saved at: z3. Ask the user what they'd like you to do with it.]r  reply_to_textr  z[Replying to: "z"]

r  )#preprocess_context_references_async)get_model_context_lengthr   ~r   context_lengthr   r   )r   r   config_context_length)r   r  allowed_rootrI  zContext injection refused.z(@ context reference expansion failed: %s)Dr   r%   r  r  #_consume_pending_native_image_pathsr  r  rC  rJ  rK  rD  rL  rF  r  rM  rN  VOICEAUDIO_decide_image_input_moderB  r.  r*  r9  _enrich_message_with_vision"_enrich_message_with_transcriptionr  r   rA   r   r  r  r  r  r   DOCUMENT	mimetypestools.credential_filesr  r?   ra   splitextr_  
guess_typebasenamer^  r'   r+   reply_to_message_idr  agent.context_referencesr  agent.model_metadatar  r@   rc  r3  r  r1   rW   r4   _model	_base_urlr  rO  warningsexpandedr[  r:  ),r_  rP  r  rQ   _group_sessions_per_user_thread_sessions_per_userrV  _is_shared_multi_userimage_pathsaudio_pathsrR  ra   rT  	_img_modepending_native_stt_fail_markers_stt_adapter	_stt_meta_stt_msg
_mimetypesr  _TEXT_EXTENSIONS_extguessedrn  r   rQ  display_name
agent_pathcontext_notereply_snippetr  r  _msg_cwd_msg_runtime_msg_config_ctx_msg_cfg_msg_model_cfg_msg_raw_ctx_msg_ctx_len_ctx_result_adapterr2  r   s,                                              @r"   _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 C	!KK$U%566 - -401C8I4J4J0J0J)!,,PR##H-- -1C{GX1X1X&&t,,,##H-- -1CHY[f[lGm1m1m&&t,,,  !99;;	((%,T3[]a%b%bN%-)+FTC26{2C2CN;/KKmK((   
 KKf!3{#3#3   *.)I)I$#* * $ $ $ $ $ $L
   !%)%L%L & &            %! NNNN<MNNNNN !#'=#4#4V_#E#ELCICS ]f.> ? ?Y]I# !!!= %  $4466 q (,p p"."3"3 & ()2 #4 # #        
  ) ! ! ! D!  (	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>>
##G,, DL D D6@D D D !LM| M M1;M M M !
 #/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 sW   >K
 

KKAX, .AU X, 
UX, UC X, X, ,
Y6YYc                 v    t          | dd           }|sg S t          |                    |g           pg           S )NrB  )r%   r.  r  )r_  rV  r.  s      r"   r  z1GatewayRunner._consume_pending_native_image_paths  sE     'OQUVV 	IN&&{B77=2>>>r$   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)	NrE  z*Failed to cache live session source for %sTrq  rF  r  F)r  )r%   r   rE  rv  r9   r   r*  r:  move_to_endrK  popitem)r_  rV  r  cached_sourcesmax_sizes        r"   _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 )NrE  )r%   rA   rG  r   )r_  rV  rI  r  s       r"   r  z(GatewayRunner._get_cached_session_source  s     	4 '94@@ 	4##K00**;7777   s   A 
AAr  run_generationc                 
=  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|           | 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|
rQt	          |
                    d          pd          }|r*||j        k    r| j                            |	|          }||}nD	 |                     ||           n,# t(          $ r t                              dd           Y nw xY wt5          |dd          r\| j                            |	d           |                     |	d           t          | d          r| j                            |	d           |j        |j         k    p!t5          |dd          pt5          |dd          }t5          |dd          rd|_!        |rC| j"        #                    d|j        r|j        j        nd|j
        |j        |	d           d{V  tI          || j%        |          }| &                    |          }d}	 tO                      }tQ          |                    d          pi                     dd                    }n# t(          $ r Y nw xY wtS          ||          }t5          |dd          rt5          |dd          pd}|dk    rd}n|dk    rd}nd}|dz   |z   }	 | j        j%        *                    |j        t5          |d d!          "          }|j        r|j        j        nd}t5          |d#d          }|dk    p|j+        o
|o||j,        v}|r| j-                            |j                  }|r|dk    rd$}nA|dk    rd%|j.         d&}n/|j/        d'z  }|j/        d'z  }|s| d(n|r| d)| d*n| d*}d+| }d,| d-}	 | 0                                }|r| d| }n# t(          $ r Y nw xY w|1                    |j        |t5          |d.d          /           d{V  n2# t(          $ r%} t                              d0|            Y d} ~ nd} ~ ww xY wd|_2        d|_3        t5          |d1d          }!|r2|!r/ti          |!t                    r|!gntk          |!          }"	 d2d3l6m7}#m8}$ g }%g }&|"D ]r}' |#|'|4          }(|(rF|(\  })}*}+d5|+ d6}, |$|)|*|,          }-|-r*|%9                    |-           |&9                    |'           Wt          :                    d7|'           s|%rP|%9                    |j                   d;                    |%          |_        t                              d8|&|	           n3# t(          $ r&} t          :                    d9|"|            Y d} ~ nd} ~ ww xY w| j        <                    |j                  }.|.rt{          |.          d:k    rd2d;l>m?}/m@}0 d<}1d=}2d}3d>}4d}5d}6d}7d}8i }9	 tO                      }9|9r|9                    d?i           }:ti          |:t                    r|:}1nti          |:t                    r|:                    d@          p|:                    d?          p|1}1|:                    dA          };|;(	 t          |;          }5n# t          t          f$ r Y nw xY w|:                    dB          pd}6|:                    dC          pd}7|9                    dDi           }<ti          |<t                    r~t	          |<                    dEd                    E                                dFv }3|<                    dG          }=|=0	 t          |=          }>|>d2k    r|>}4n# t          t          f$ r Y nw xY w	 | F                    ||	ti          |9t                    r|9ndH          \  }1}?|?                    dB          p|6}6|?                    dC          p|7}7|?                    dI          p|8}8n# t(          $ r Y nw xY w|5I|7rF	 	 d2dJlGmH}@  |@|9          }An<# t(          $ r/ |9                    dK          }Ati          |Atj                    sg }AY nw xY w|AD ]}Bti          |Bt                    s|B                    dC          pdI                    dL          }C|Cr|C|7I                    dL          k    r~|B                    dMi           }Dti          |Dt                    rQ|D                    |1i           }Eti          |Et                    r&|E                    dA          }F|Ft          |F          }5 nn# t          t          f$ r Y nw xY wn# t(          $ r Y nw xY w|3r] |0|1|7pd|8pd|5|6pdN          }Gt          |G|2z            }Ht          |GdOz            }It{          |.          }J|jJ        }K|Kd2k    r|KdP}Ln |/|.          dQ}L|4}M|Hk    p|J|Mk    }N|Nrt                              dR|JdS|Lt          |2dTz            |GdS|HdS           |j        r	dU|j        ind}O	 d2dVlKmL}P | F                    ||	ti          |9t                    r|9ndH          \  }1}?|?                    dI          rdW |.D             t{                    d:k    r |Pdi |?|1d:dddXg|j        dY	 dZ _M        t          jO                    }Q|QP                    dfd[           d{V \  }R}Sj        }T|T|j        k    r |T|_        | j        Q                                 | j        R                    |j        |R           d2|_J        |R}.t{          |R          }U |/|R          }Vt                              d\|J|UdS|VdS           |V|Ik    rt          :                    d]|VdS           t5          d^d          }W|Wt5          |Wd_d          rt5          |Wd`d2          }Xt5          |Wdad          pdb}Ydc|Y dd|X de}Z	 | j-                            |j                  }[|[r*|j        r#|[1                    |j        |Z|O/           d{V  n# t(          $ r%}\t          :                    df|\           Y d}\~\nd}\~\ww xY w|Wt5          |Wdgd          rt5          |Wdgd          }]t5          |Wdhd          pdb}^di|] dj|^ dk}_	 | j-                            |j                  }[|[r*|j        r#|[1                    |j        |_|O/           d{V  n2# t(          $ r%}\t          :                    dl|\           Y d}\~\nd}\~\ww xY w| S                    |	           | T                               n/# | S                    |	           | T                               w xY wn2# t(          $ r%} t          :                    dm|            Y d} ~ nd} ~ ww xY w|.s| j        U                                s|dnz  }|.s|j        r|j        t          jW        k    r|j        t          jX        k    r|j        j        }t          |          }`t          j[        |`          sP|j        t          j\        k    rdondp}adq|]                                 dr|a ds}| ^                    ||           d{V  |j        t          j_        k    rl| j-                            t          j_                  }| `                    |          }b|br1|r/t          |dt          r|a                    |b          }c|cr|d|c z  }| b                    |||.u           d{V }d|ddS | c                    | j-                            |j                  |	|           	 |j        r|j        j        nd|j
        |j        |dddv         dw}e| j"        #                    dx|e           d{V  | d                    |d||.||j        |	||je        |jf        y	  	         d{V }f	 | j-                            |j                  }g|gr0t          |gdz          r |gg                    |j                   d{V  n# t(          $ r Y nw xY w| h                    ||          st                              d{|pd||           | j-                            |j                  }ht5          t          |h          d}d          |hj                    ||~           n-|hr+t          |hd          r|hjk                            |d           	 | l                    |           dS |f                    d          pd}i|idk    rd}i|f                    dg           }jt          j                     |z
  }k|f                    dd2          }lt{          |i          }mt                              d||j        pd|k|l|m           |	rst          |f          rd| n                    |	           	 | j        o                    |	           n3# t(          $ r&}nt                              d|	|n           Y d}n~nnd}n~nww xY wt          |f|it{          |.                    }i|f                    d          r|fd         |j        k    r|fd         |_        	 d2dlqmr}o  |otO                      t          |j                  dt5          | dd                    }pn!# t(          $ r t5          | dd          }pY nw xY w|pr|ir|f                    d          }q|qr|qt                                u                                }rt{          |r          dk    r7d;                    |rdd                   }s|sdt{          |r          dz
   dz  }sn|qt                                }sd|s d|i }id}t	 d2dlvmw}u  |utO                      t          |j                  |f                    d?          |f                    dd2          pd2|f                    dA          pdt          jx                            dd                    }tn4# t(          $ r'}vt                              d|v           d}tY d}v~vnd}v~vww xY w|tr|ir|f                    d          s|i d|t }i| j"        #                    di |ed|ipdddv         i           d{V  	 d2dlymz}w |wj{        rH|wj{                            d2          }xt          j|        | }                    |x                     |wj{        Hn2# t(          $ r%} t          ~                    d|            Y d} ~ nd} ~ ww xY w	 d2dlymz}y g }z|yj                                        sa|yj                                        }{|{                    dd          }|||dv r|z9                    |{           |yj                                        a|zD ]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tQ          |f                    d                    }t	          |f                    dd                    E                                |oTtQ          |f                    d                    p2t	          fddD                       pdv ot{          |.          dk    }|r!t                              d|j                   n"|r t                              d|j                   |f                    d          r|r|	rt                              d|j                   | j                            |	           | S                    |	           | j                            |	d           |                     |	d           t          | d          r| j                            |	d           |ipddz   }it          j                                                    }|rn`|.s^|f                    dg           }| j                            |j        d|pg t                      |j        r|j        j        nd|d           |rn|r%| j                            |j        d|d|d           n|f                    dt{          |.                    }t{          |j          |k    r
|j|d         ng }|sK| j                            |j        d|d|d           |ir$| j                            |j        d|i|d           nR| j        du}|D ]F}|                    d          dk    ri |d|i}| j                            |j        ||           G| j                            |j        |f                    dd2                     tQ          |f                    d                    }|                     ||i|j|          r|                     ||i           d{V  |f                    d          r|f                    d          s|ir>| j-                            |j                  }|r|                     |i||           d{V  |trv	 | j-                            |j                  }|r!|1                    |j        |t           d{V  n2# t(          $ r%}nt                              d|n           Y d}n~nnd}n~nww xY w	 | l                    |           dS |i| l                    |           S # t(          $ r} 	 | j-                            |j                  }|r0t          |dz          r |g                    |j                   d{V  n# t(          $ r Y nw xY wt                              d|	           t          |           j        }t	          |           rt	          |           dd         nd}d}t5          | dd          }dt#                      v rt{          |.          nd2}|dk    rd}n|dk    rd}n|dk    rt5          | dd          }i }	 |(|                                                    di           }n# t(          $ r Y nw xY w|                    d          dk    rC|                    dĦ          }|r)|d2k    r#d2dl}|                    |dz            }d| dǝ}n=d}n:d}n7|dk    rd}n.|dv r*|dk    r	 Y d} ~ | l                    |           dS |d>k    rd}d| d| d| dѝcY d} ~ | l                    |           S d} ~ ww xY w# | l                    |           w xY w)zAInner handler that runs under the _running_agents sentinel guard.r   r>   NP   rI  r]  z3inbound message: platform=%s user=%s chat=%s msg=%rr  r  r  %Failed to read Telegram topic bindingTrq  r  z'Failed to record Telegram topic bindingwas_auto_resetF_pending_model_notesis_fresh_resetzsession:start)r   r  r  rV  privacy
redact_pii)rV  auto_reset_reasonidler  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:00r  r7  z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  r  z.Auto-reset notification failed (non-fatal): %s
auto_skillr   )_load_skill_payload_build_skill_messagert  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_roughr  zanthropic/claude-sonnet-4.6g333333?  r   rG   r  r   r   compressionr  r  hygiene_hard_message_limitr  r   get_compatible_custom_providerscustom_providersr   models)r   r   r  r   ffffff?actual	estimateduf   Session hygiene: %s messages, ~%s tokens (%s) — auto-compressing (threshold: %s%% of %s = %s tokens)r  d   r  AIAgentc                     g | ]Y}|                     d           dv |                     d          .|                     d           |                     d          dZS rS   user	assistantrr  rS   rr  r  r  s     r"   rm  z<GatewayRunner._handle_message_with_agent.<locals>.<listcomp>  sk     ) ) )$%#$55==4I#I#I$%EE)$4$4 $J *+v155CSCS T T#I#I#Ir$   memoryr   r  
quiet_modeskip_memoryenabled_toolsetsr  c                      d S r  rw   akws     r"   r  z:GatewayRunner._handle_message_with_agent.<locals>.<lambda>  s    D r$   c                  4                         d           S )Nr>   )approx_tokens_compress_context)_approx_tokens
_hyg_agent	_hyg_msgss   r"   r  z:GatewayRunner._handle_message_with_agent.<locals>.<lambda>  s%    
0L0L,5r:H 1M 1* 1* r$   u>   Session hygiene: compressed %s → %s msgs, ~%s → ~%s tokensz3Session hygiene: still ~%s tokens after compressioncontext_compressor_last_summary_fallback_used_last_summary_dropped_count_last_summary_errorr  u+   ⚠️ Context compression summary failed (). z historical message(s) were removed and replaced with a placeholder. Earlier context is no longer recoverable. Consider /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_error%   ℹ️ Configured compression model `
` failed (   ). 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.]z/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rP  r  rQ   r  )r   r  r  r[  zagent:start)	r[  context_promptrQ   r  r  rV  rM  event_message_idrL  stop_typinguK   Discarding stale agent result for %s — generation %d is no longer currentr  pop_post_delivery_callback
generation_post_delivery_callbacksru  (empty)u   ⚠️ The model returned no response after processing tool results. This can happen with some models — try again or rephrase your question.messagesr  zMresponse ready: platform=%s chat=%s time=%.1fs api_calls=%d response=%d charsz&clear_resume_pending failed for %s: %sr  resolve_display_settingr  r-  last_reasoning   z
_... (z more lines)_u   💭 **Reasoning:**
```
z
```

)build_footer_linelast_prompt_tokensr   )r  platform_keyr   context_tokensr  r   zruntime_footer build failed: %salready_sentz	agent:endr  r  zProcess watcher setup error: %sr  r  )r  r  &Watch notification injection error: %szWatch queue drain error: %sr  r  compression_exhaustedc              3       K   | ]}|v V  	d S r  rw   )r  r  _err_str_for_classifys     r"   r  z;GatewayRunner._handle_message_with_agent.<locals>.<genexpr>   s9        aq11      r$   )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|  rT   )rS   r|  r   r   r2   rp  )rS   rr  r2   history_offsetrq  rS   rU   r2   )skip_dbr  )r  ztrailing footer send failed: %szAgent error in session %sr  zno details availablestatus_coderQ   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`  r  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.rw   )rM   r  r   r   r)   r   r9   r*  r9  r  r  r  r5  r  rV  rK  r  rR  get_telegram_topic_bindingr  r   r:  rA   r  switch_sessionr  r%   r  r  r|  rS  
created_atrJ  rT  rY  r  r  r  _set_session_envr  r3   r  get_reset_policynotifynotify_exclude_platformsr   at_houridle_minutes_format_session_infor  rR  rW  r1   r.  r  r]  r^  rN  r+  rO  load_transcriptrK  r#  r_  r  rW   r4   rC   r7   r_  r  r   rd  rstripr  	run_agentrl  	_print_fnr9  r  run_in_executorr  rewrite_transcriptr  r  has_any_sessionsr  r  rk  rp   r?   r)  r  r&  r   r  _get_guild_idr  rD  _bind_adapter_run_generation
_run_agentr  rL  r  _is_session_run_currentr  r  r  _clear_session_envr  r  clear_resume_pendingr  gateway.display_configr  r  r6   rq  gateway.runtime_footerr  r@   r4  r  r  r9  r  r  completion_queueempty
get_nowaitr  _inject_watch_notificationr  reset_sessionr   rJ   	isoformatappend_to_transcriptr  update_session_should_send_voice_reply_send_voice_reply_deliver_media_from_responserP  __name__localsru  mathceil)r_  rP  r  r  rM  _msg_start_time_platform_name_msg_previewr  rV  bindingbound_session_idswitched_is_new_sessionr  _session_env_tokens_redact_pii_pcfgr  reset_reasonr9  policyri   had_activityshould_notifyrY  reason_texthoursminsdurationnoticesession_inforb  _auto_skill_namesr]  r^  _combined_parts_loaded_names_sname_loaded_loaded_skill
_skill_dir_display_name_note_partrQ   r_  r  
_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_limitr  _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_metarl  loop_compressedrn  _hyg_new_sid
_new_count_new_tokens_comp_dropped_err	_warn_msgrC  _werr
_aux_model_aux_err_aux_msgr  sethome_cmdguild_id
vc_contextr   r  r  _typing_adapter_stale_adapterr  agent_messages_response_time
_api_calls	_resp_lenr  _rds_show_reasoning_effectiver  linesdisplay_reasoning_footer_line_bfl_footer_errr  r  _pr_watch_eventsr  r  
synth_texte2agent_failed_earlyis_context_overflow_failurerY   	tool_defsr  new_messagesagent_persistedrX   r@  _already_sent_media_adapter_foot_adapter_err_adapter
error_typer  status_hintr  	_hist_len	_err_body	_err_json
_resets_inr  _hoursr  r  r  r  s                                                                                                                                                         @@@@r"   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	
 	
 	
 *@@HH#/"";777''// 	[ %0$*EE//!&"233 F    ,0     DtTTT  [#&w{{<'@'@'FB#G#G # 1(8M<T(T(T  $1@@N^__H+(0[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%,UJ%E%E +           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"  g	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       ( @&>&>*0b(.B*D*0b' ' '# -0'*<<- -) ),,?$,F(G(G% \\
 "/!A!A%%%3N$,MM%C%CG%L%LN$/M$ #6"&?? 5!_4  
 # QKK>"~$9$9=.455.22488   DJCS ]f.> ? ?Y]IE555555373V3V#)(35?	45P5P(Z		VZ 4W 4 40
L
 (++I66 xN) ))0) ) )I  #9~~22-4W ." ."&2."*434/3046>Z/</G." ." ." ."
f!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$($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','8WULikp=q=q'83:5B_ab3c3c/6u>SUY/Z/Z/m^m-^04-^ -^9A-^ -^ -^ )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N  	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..	
 	
 	
j	9 6<_LFO11"!>+6'-	 H *//-::::::::: "&$-(3'-!&!1$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z ##$788888w $''(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 -- F,|2LP]Ph2h2h+7+E(	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&7 L.?CCAFFG'(A(A'(J(JKKK '7 L  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%' 	 	 	 + )# '
 "77!,#2NN   
 +../?WNN?B>?R?RU`?`?`~kll;;fh $ &;;%0!'LrRR     *??)4%0XTVWW   '+&6d&BO+ 	 	776??h66$ 83 8R 8 8*??)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( S"/"4"4V^\"R"RRRRRRRR$ L L L%FKKKKKKKKL~ ##$788888{ z ##$78888w  8	 8	 8	#}00AA CGL-$H$H C&226>BBBBBBBBB   8+FFF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	    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8w8	v ##$78888sS  5A	D? ?(E*)E*G &H H.AM5 5
NNC&T  ?S T  
S*'T  )S**5T   
U*U

UCY3 3
Z#=ZZ#7Bi# ^ i# ^30i# 2^33B*i# a6 5i# 6b
i# 	b

i# A7d i# 
di# d	i# d0 /i 06e)&i (e))C!i 
i# ii# ii# #
i0/i08Bz! >E
y1 	Au y1 
vu?:y1 ?vAy1 Ax y1 
yx>9y1 >yy1 +z! 1,zz! !
{+{{A=B Aq C>AAE EAq E
AEEAq EAEEB9Aq H.B<Aq K+AL LAq L
AL6LAL1L,Aq L1AL6L6AAq NAAO OAq OAO,O)Aq O+AO,O,B,Aq RBAT+ T*Aq T+
AUT5AUUAq UAUUAAq V1AAX XAq X
AX6XAX1X,Aq X1AX6X6Aq X:BA\$ [A[0[/A\$ [0
A\[:A\\A\$ \A\\A\$ \#Aq \$
A]\.A]]	Aq ]A]]QAq n"AAo% o$Aq o%
Apo/App
Aq pAppAq p/Aq qAy(qAAr$r#Ay#r$
Ar1r.Ay#r0Ar1r1B:Ay#u,*AvvAy#v
Av$v!Ay#v#Av$v$A6Ay#xAy+ x5Ay#yAy(y	Ay+ y#Ay(y(Ay+ y+Az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   )r  DEFAULT_FALLBACK_CONTEXTNr   r  r   r   rc  re  r>   rf  r   )r   r   r  r   re  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: rI  )r#  r  rB  r  r  rA   r1   rW   r4   rC   r7   r   rd  r   r5   r3  r)   rN  rO  )r_  r  rB  r   r  r   r   r   custom_provsr  r  raw_ctxrd  re  cpcp_model	cp_models
raw_cp_ctxmodel_entry	model_ctxr/  r  
ctx_sourcectx_displayr(  s                            r"   r  z"GatewayRunner._format_session_info  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c                 	  K   |j         }|                     |          }|                     |d           | j        j                            |          }t          | dd          }|n|5  | j                            |          }t          |t                    r|d         n|r|nd}ddd           n# 1 swxY w Y   || 
                    |           |                     |           t          | dd          }||                    |d           	 ddlm}	  |	             n# t          $ r Y nw xY w	 ddlm}
  |
             n# t          $ r Y nw xY w| j                            |          }| j                            |d           |                     |d           t+          | d	          r| j                            |d           |                     |           	 dd
lm} |r|j        nd} |d||j        r|j        j        nd           n# t          $ r Y nw xY w| j                            d|j        r|j        j        nd|j        |d           d{V  | j                            d|j        r|j        j        nd|j        |d           d{V  	 |                                  }n# t          $ r d}Y nw xY w|r| !                    |          pd}n3| j        "                    |d          }| !                    |          pd}|#                                $                                }d}|r| j%        r|rddl&m'} 	 |(                    |          }n# tR          $ r}d}d| }Y d}~nd}~ww xY w|rO	 | j%        *                    |j        |           d| }n,# tR          $ r}d| d}Y d}~nd}~wt          $ r Y nw xY w|sd}||z   }| +                    |          rF|D	 | ,                    ||           n,# t          $ r tZ          .                    dd           Y nw xY w	 dd
lm} |r|j        nd} |d||j        r|j        j        nd           n# t          $ r Y nw xY w	 ddl/m0} d |             }n# t          $ r d}Y nw xY w|rtc          | d | |           S tc          | |           S )!zHandle /new or /reset command.session_resetrA  rJ  Nr   rA  )clear_env_passthrough)clear_credential_filesrS  r  r  r>   r  zsession:end)r   r  rV  zsession:resetu"   ✨ Session reset! Starting fresh.T)	force_newu   ✨ New session started!r  u   
⚠️ Title rejected: u   ✨ New session started: u   
⚠️ u    — session started untitled.uB   
⚠️ Title is empty after cleanup — session started untitled.z*Failed to rebind Telegram topic after /newrq  on_session_reset)get_random_tipu
   
✦ Tip: r  )2r  r  r  r5  r  rA   r%   rH  r1   r  r  r  r  tools.env_passthroughrT  r   r  rU  r  r  r|  r  rS  &_clear_session_boundary_security_stater  r  r  r   r   rY  r  r  r  r  r  r  r6   rR  rS  r  sanitize_titler7   set_session_titler  r  r*  r:  hermes_cli.tipsrX  r  )r_  rP  r  rV  	old_entryr  r  
_old_agent_qerT  rU  	new_entryr  _old_sidr  header
_title_arg_title_noter  r!   rb  _new_sidrX  	_tip_lines                           r"   r  z#GatewayRunner._handle_reset_commandp  sV      226:://O/TTT &/33K@@	
 d$7>>" h h+//<<+5gu+E+EgWQZZV]Kg77cg
h h h h h h h h h h h h h h h %--j999  ---
 d,d33?GGK&&&	CCCCCC!!#### 	 	 	D		EEEEEE""$$$$ 	 	 	D	 &44[AA	 	%))+t<<<,,[$???4/00 	=%))+t<<<
 	33K@@@	FFFFFF/8By++dHL.8;A?"R&/"7"7PRT T T T T 	 	 	D	 joom17H--b~&.
 .
   	 	 	 	 	 	 	 jooo17H--b~&0
 0
   	 	 	 	 	 	 		4466LL 	 	 	LLL	  	[44V<<d@dFF *@@SW@XXI44V<<Z@ZF ++--3355
 	d$* 	dy 	d......>%44Z@@		 > > > 	=!==>  
d$66y7KYWWWDDDFF! P P P"Oa"O"O"OKKKKKK    D  dc+% ''// 	ZI4IZ33FIFFFF Z Z ZITXYYYYYZ	FFFFFF/8By++dHL+;A?"R&/"7"7PRT T T T T 	 	 	D		6666668nn&6&688II 	 	 	III	  	L!V"J"J"Jy"J"JKKK444555s   *>B44B8;B8D& &
D32D37E 
EE$2H 
H$#H$"J7 7KKM& &
N0M<<N%N- -
O7OOO6P &P65P6:2Q- -
Q:9Q:>R R! R!c                    K   ddl m} ddlm}  |            } |            }d| dd| dg}d                    |          S )u@   Handle /profile — show active profile name and home directory.r   display_hermes_homerU  u   👤 **Profile:** `r  u   📂 **Home:** `rI  )r  rj  r  rV  rO  )r_  rP  rj  rV  r   profile_namer(  s          r"   r  z%GatewayRunner._handle_profile_command  s      888888??????%%''..00 2,111)w)))

 yyr$   c                   K   ddl }ddl}ddlm} |j        pd                                }|                    d          r|                    d          }|                    d          r)|t          d          d                                         }|	                    dd          dd         dgk    }	  |j
        ||           d{V }n# t          $ r}d	| cY d}~S d}~ww xY w|rR|rO |j        d
|          }	|	r;|	                    d          	 |j        }
t          |
dd          }t!          |d          r|j        nt%          |pd                                          t%          t          |
dd          pd          t%          t          |
dd          pd          t%          t          |
dd          pd          pdr<r:fd} |j
        |           d{V  |                                d dz   }n2# t          $ r%}t*                              d|           Y d}~nd}~ww xY wt          |          dk    r|dd         dz   }|pdS )u  Handle /kanban — delegate to the shared kanban CLI.

        Run the potentially-blocking DB work in a thread pool so the
        gateway event loop stays responsive.  Read operations (list,
        show, context, tail) are permitted while an agent is running;
        mutations are allowed too because the board is profile-agnostic
        and does not touch the running agent's state.

        For ``/kanban create`` invocations we also auto-subscribe the
        originating gateway source (platform + chat + thread) to the new
        task's terminal events, so the user hears back when the worker
        completes / blocks / auto-blocks / crashes without having to poll.
        r   N)	run_slashr>   r   rH  r   createu   ⚠ kanban error: zCreated\s+(t_[0-9a-f]+)\br   r   r  r  r  c                      ddl m}  |                                 }	 |                     |pd            |                                 d S # |                                 w xY w)Nr   r  )r
  r   r  r  r  )r  r   r  add_notify_subr  )r  r  r  r  r
  r  r  s     r"   _subz2GatewayRunner._handle_kanban_command.<locals>._subF  s~    CCCCCC#&;;==D- # 2 2$('-97.7.?4,3	 !3 !" !" !" !%



s   A A&u)   
(subscribed — you'll be notified when z completes or blocks)z'kanban create auto-subscribe failed: %si  uJ   
… (truncated; use `hermes kanban …` in your terminal for full output)z(no output))r9  r'   hermes_cli.kanbanrm  r   r6   rL  r  rK  r^  r$  r   searchr    r  r%   r  r   r)   r_  r  r*  r+  )r_  rP  r9  r'   rm  r   	is_creater  r2  r  r  r   rq  r  r  r
  r  r  s                @@@@@r"   r  z$GatewayRunner._handle_kanban_command  s(      				//////
 b''))??3 	$;;s##D??8$$ 	1H'..00DJJtQ''+z9		.,7,Y========FF 	. 	. 	.---------	.  !	S !	S	6??A S''!**S"\F&vz4@@H*1(G*D*D]#hnZ\J]J]egg ! "'&)R"@"@"FBGGG #GFK$D$D$J K KI!'&)R"@"@"FBGGO4G#  - - - - - - - - - 0g/555555555"MMOO67 6 6 66 
 ! S S SNN#LcRRRRRRRRS
 v;;ETE]%rrF&&s7   =C 
C.C)#C.)C.!C2H 
IH>>Ic                   K   |j         }| j                            |          }d | j                                        D             }|j        }|| j        v }|r| j                            |j                  nd}| 	                    ||          }d}	d}
| j
        r	 | j
                            |j                  }	n# t          $ r d}	Y nw xY w	 | j
                            |j                  }|rw|                    d          pd|                    d          pdz   |                    d          pdz   |                    d          pdz   |                    d	          pdz   }
n# t          $ r d}
Y nw xY wd
dd|j         dg}|	r|                    d|	            |                    d|j                            d           d|j                            d           d|
dd|rdnd g           |r|                    d|            |                    ddd                    |           g           d                    |          S )zHandle /status command.c                     g | ]	}|j         
S rw   r  rl  s     r"   rm  z8GatewayRunner._handle_status_command.<locals>.<listcomp>f  s    EEE1qwEEEr$   NrE  r   input_tokensoutput_tokenscache_read_tokenscache_write_tokensreasoning_tokensu   📊 **Hermes Gateway Status**r>   z**Session ID:** `r  z**Title:** z**Created:** z%Y-%m-%d %H:%Mz**Last Activity:** z**Tokens:** r  z**Agent Running:** u   Yes ⚡Noz**Queued follow-ups:** z**Connected Platforms:** r  rI  )r  r5  r  r   r  rV  r?  rA   r   rH  rR  get_session_titler  r   get_sessionrN  extendr  strftimerJ  rO  )r_  rP  r  r  connected_platformsrV  
is_runningrY  queue_depthr&  db_total_tokensrowr(  s                r"   r  z$GatewayRunner._handle_status_commanda  s     *@@HHEE0B0B0D0DEEE $/ D$88
 9?H$-##FO444D''W'EE  	$(::=;STT   $&22=3KLL 005A77?338q:77#677<1> 77#788=A? 77#566;!	= $  $ $ $"#$ -; 8;;;

  	0LL.u..///QM4==>NOOQQW-":"C"CDT"U"UWW.?...Ez"C))tEE	
 	 	 	  	BLL@;@@AAAH		2E(F(FHH
 	 	 	
 yys%   B= =CCBE) )E87E8c                 x  K   ddl m}m} t          j                    }|                     |j                  }t          | di           pi }t          | di           pi }g }|                                D ]\  }	}
t          |	                    |	|                    }t          dt          ||z
                      }|
t          u }|                    |	||rdnd|rdnt          t          |
dd          pd          |rdnt          t          |
d	d          pd          d
           |                    d d           g }	 d |                                D             }n# t"          $ r g }Y nw xY wd t          | dt%                                pt%                      D             }dddt'          |           g}|rt)          |dd         d          D ]\  }}|d         |k    rdnd}|d         rd|d          dnd}|d	         rd|d	          dnd}|                    | d|d          d|d          d ||d                    | | | 
           t'          |          dk    r)|                    dt'          |          dz
   d            |                    dd!t'          |           g           |r|dd         D ]}d"                    t          |	                    d#d                                                              }t'          |          d$k    r|dd%         d&z   }|                    d'|	                    dd(           d |t          |	                    d)d                               d| d           t'          |          dk    r)|                    dt'          |          dz
   d            |                    dd*t'          |           g           |s.|s,|s*|                    d           |                    d+           d,                    |          S )-z>Handle /agents command - list active agents and running tasks.r   )format_uptime_shortr  r?  r  rW  r  r>   r  r   )rV  elapsedstater  r   c                     | d         S )Nr  rw   )r  s    r"   r  z6GatewayRunner._handle_agents_command.<locals>.<lambda>  s
    I r$   T)r  reversec                 D    g | ]}|                     d           dk    |S )r*  r  r  rl  s     r"   rm  z8GatewayRunner._handle_agents_command.<locals>.<listcomp>  s7     ! ! !55??i// ///r$   c                 P    g | ]#}t          |d            |j                    !|$S )r!  )r  r!  )r  r   s     r"   rm  z8GatewayRunner._handle_agents_command.<locals>.<listcomp>  sH     
 
 
q&!!
*0!&((

 
 
r$   r^  u   🤖 **Active Agents & Tasks**z**Active agents:** N   r   rV  u    · this chatu    · `r  z. `u   ` · r  u    · r  z... and z morez"**Running background processes:** r]  r#  r  W   rM  z- `r  uptime_secondsz**Gateway async jobs:** z"No active agents or running tasks.rI  )r4  r  r  rM   r  r  r%   r  r5   rA   r  r4   r  rN  r)   sortlist_sessionsr   r]  rK  rJ  r  rO  r^  )r_  rP  r  r  rJ   current_session_keyrunning_agentsrunning_started
agent_rowsrV  r   startedr  
is_pendingrunning_processesbackground_tasksr(  idxr  rO   sidr   r  r*  s                           r"   r  z$GatewayRunner._handle_agents_command  sy     PPPPPPPPikk"::5<HH&t->CCIr '.BB G G M2!#
"0"6"6"8"8 	 	KO//SAABBG!Sw//00G"99J#.&+5DZZ9(2"c""GE<Y[<\<\<b`b8c8c#-YRR3wugr7R7R7XVX3Y3Y     	66EEE(*	#! !+99;;! ! !  	# 	# 	# "	#
 
&9355AAJSUU
 
 
 -3#j//33
  
	E%j"oq99  S-0-?CV-V-V//\^69,6GO2c,/2222R36w<G/G////R R Rs=1 R RG R R**3y>::R<?RAFRHOR R    :##CJ"(<CCCDDDMS9J5K5KMM	
 	
 	
  
	L)#2#.  hhs488Ir#:#:;;AACCDDs88b==crc(U*C\$((<55 \ \**3txx8H!/L/L+M+MNN\ \UX\ \ \    $%%**J,=(>(>(CJJJKKKB3/?+@+@BB	
 	
 	
  	?"3 	?<L 	?LLLL=>>>yys   E# #E21E2c                   K   |j         }| j                            |          }|j        }| j                            |          }|t          u rN|                     ||t          d           d{V  t          
                    d|           t          d          S |r3|                     ||t          d           d{V  t          d          S dS )	a  Handle /stop command - interrupt a running agent.

        When an agent is truly hung (blocked thread that never checks
        _interrupt_requested), the early intercept in _handle_message()
        handles /stop before this method is reached.  This handler fires
        only through normal command dispatch (no running agent) or as a
        fallback.  Force-clean the session lock in all cases for safety.

        The session is preserved so the user can continue the conversation.
        stop_command_pendingrD  Nu2   STOP (pending) for session %s — sentinel cleareduL   ⚡ Stopped. The agent hadn't started yet — you can continue this session.stop_command_handlerrG  zNo active task to stop.)r  r5  r  rV  r?  rA   r  r  r  r*  r9  r  )r_  rP  r  r  rV  r   s         r"   r  z"GatewayRunner._handle_stop_command  s$      *@@HH#/$((55+++33!7$:	 4          KKLkZZZ!"pqqq 	- 33!7$:	 4          ""OPPP,,r$   c                   K   |                      |          rGt                              d|j        r|j        j        r|j        j        j        nd|j                   dS | j        s| j        r6| 	                                }|rt          d|          S t          d          S 	 |j        j        r|j        j        j        nd|j        j        d}|j        j        r|j        j        |d	<   t          t          d
z  |d           n2# t           $ r%}t                              d|           Y d}~nd}~ww xY w	 |j        j        r|j        j        j        ndt%          j                    d}|j        
|j        |d<   t          t          dz  |d           n2# t           $ r%}t                              d|           Y d}~nd}~ww xY w| 	                                }t'          t(          j                            d                    }|r|                     dd           n|                     dd           |rt          d|          S t          d          S )zFHandle /restart command - drain active work, then restart the gateway.uo   Ignoring redelivered /restart (platform=%s, update_id=%s) — already processed by a previous gateway instance.r  r>   zgateway.draining)rO  u*   ⏳ Gateway restart already in progress...N)r   r  r  ru   r  z'Failed to write restart notify file: %s)r   requested_at	update_id.restart_last_processed.jsonz(Failed to write restart dedup marker: %sINVOCATION_IDFTr.  uy   ♻ Restarting gateway. If you aren't notified within 60 seconds, restart from the console with `hermes gateway restart`.)_is_stale_restart_redeliveryr*  r9  r  r   r   platform_update_idr  r  r+  r   r  r  r  r|   rv   r   r:  rM   r3   r?   r@   rA   r<  )r_  rP  rO  notify_datarb  
dedup_datar\  _under_services           r"   r  z%GatewayRunner._handle_restart_command!  s      ,,U33 	KKD/4|^@U^%++[^(	   2" 	Pdn 	P--//E :+59999!"NOOO	G;@<;PZEL177VZ </ K |% B+0<+AK(55    
  	G 	G 	GLLBAFFFFFFFF	G	H;@<;PZEL177VZ $	 J '3*/*B
;'==    
  	H 	H 	HLLCQGGGGGGGG	H 1133 bjnn_==>> 	C  %T BBBB  $E BBB 	>'}====  Z  [  [  	[s2   $A!D 
D5D00D59AF 
G"GG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   r  r   r  r  r  )r  r  r   r   r   rv   rb   ru  rv  ro  rA   r1   r4   r5   rM   )r_  rP  r,   marker_pathr  recorded_uidr  s          r"   r  z*GatewayRunner._is_stale_restart_redeliveryq  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   ddl m} dg |            }	 ddlm}  |            }|r|                    dt          |           d           t          |          }|dd         D ])}|                    d	| d
||         d                     *t          |          dk    r)|                    dt          |          dz
   d           n# t          $ r Y nw xY wt          d	                    |          t          t          |dd          dd                    S )z/Handle /help command - list available commands.r   gateway_help_linesu   📖 **Hermes Commands**
rp  u   
⚡ **Skill Commands** (z	 active):Nrp  r     ` — descriptionz	
... and z3 more. Use `/commands` for the full paginated list.rI  r  r   )r&   r  r  rp  rN  rK  r  r   r-   rO  r%   )r_  rP  r  r(  rp  r  sorted_cmdsr*  s           r"   r  z"GatewayRunner._handle_help_command  sv     ::::::(
!!
	??????++--J zT#j//TTTUUU$Z00&ss+ R RCLL!PS!P!P
30N!P!PQQQQ{##b((LL!xc+.>.>.C!x!x!xyyy 	 	 	D	,IIeGE8T22JEE
 
 	
s   B7C 
CCc           
        K   ddl m} |                                                                }|r"	 t	          |          }n# t
          $ r Y dS w xY wd}t           |                      }	 ddlm}  |            }|r|	                    d           |	                    d           t          |          D ]M}||                             dd                                          pd	}	|	                    d
| d|	            Nn# t          $ r Y nw xY w|sdS ddlm}
 |j        j        |
j        k    rdnd}t%          dt'          |          |z   dz
  |z            }t%          dt)          ||                    }|dz
  |z  }||||z            }dt'          |           d| d| ddg|}|dk    rpg }|dk    r|	                    d|dz
   d           ||k     r|	                    d|dz    d
           |                    dd                    |          g           ||k    r|	                    d| d| d           t/          d                    |          t1          t1          |dd          dd                    S )zDHandle /commands [page] - paginated list of all commands and skills.r   r  zUsage: `/commands [page]`r   r  r>   u   ⚡ **Skill Commands**:r  zSkill commandr  r  zNo commands available.r  r  ro  u   📚 **Commands** (z total, page r   r  z`/commands u
   ` ← prevu   next → `/commands z | z_(Requested page z  was out of range, showing page z.)_rI  r  Nr   )r&   r  r  r6   r4   r7   r.  r  rp  rN  r  rA   r   r#  r  r  r   r  r  rK  ra  r  rO  r-   r%   )r_  rP  r  rp  requested_pageentriesrp  r  r*  descr  	page_sizetotal_pagespager  page_entriesr(  	nav_partss                     r"   r  z&GatewayRunner._handle_commands_command  sA     ::::::))++1133 	3!$X 3 3 32223 N ))++,,
	??????++--J :r"""8999!*-- : :C%c?..}bAAGGII\_DNN#8s#8#8$#8#89999 	 	 	D	  	,++++++++,/83DDDBB"	!c'llY6:yHII1c.+6677Y&uUY%667 S#g,,RRTRRKRRR
 

 ??Iaxx  !Ctax!C!C!CDDDk!!  !Cq!C!C!CDDDLL"ejj334555>!!LLf^ff]afffggg,IIeGE8T22JEE
 
 	
s$   A 
AA-BD 
DDc                   /0123456789K   ddl }ddlm5m}m}m} ddlm} |                                	                                } ||          \  }}	}
d}d}d7d6d9d8t          dz  }	 t                      }|r|                    di           }t          |t                    rB|                    d	d          }|                    d
|          }|                    dd          7|                    d          9	 ddlm}  ||          8n%# t"          $ r |                    d          8Y nw xY wn# t"          $ r Y nw xY w|j        }|                     |          }| j                            |i           }|rX|                    d|          }|                    d
|          }|                    d7          7|                    d6          6|s|	s| j                            |j                  }|duot/          t1          |          dd          du}|r	  ||7|98d          }n# t"          $ r g }Y nw xY w|rz| 3|4|1|2706/dt2          dt2          dt2          dt2          f/0123456789fd}|j        r	d|j        ind}|                    |j        ||||||           d{V }|j        rdS  ||          }d|pd d| dg}	  ||7|98d          }|D ]}|d         rdnd}|                    d |d!          d"|d#          d$| d%           |d&         rd'                    d( |d&         D                       }|d)         tA          |d&                   k    r"d*|d)         tA          |d&                   z
   d+nd}|                    d,| |            n4|                    d-          r|                    d.|d-          d$           |                    d           n# t"          $ r Y nw xY w|                    d/           |                    d0           |                    d1           d2                    |          S  5|||76|
|	983	  	        }|j        s
d4|j!         S d} t/          | d5d          }!t/          | d6d          }"|!r1|"/|!5  |"                    |          } ddd           n# 1 swxY w Y   | ru| d         m	 | d                             |j"        |j#        |j$        |j%        |j&        7           n2# t"          $ r%}#tN          (                    d8|#           Y d}#~#nd}#~#ww xY wtS          | d9          si | _*        d:| d;|j"         d<|j+        p|j#         d=| j*        |<   |j"        |j#        |j$        |j%        |j&        d>| j        |<   | ,                    |           |
r	 |-                                rAt]          |d?@          5 }$|/                    |$          pi }ddd           n# 1 swxY w Y   ni }|0                    di           }|j"        |d	<   |j#        |d
<   |j%        r
|j%        |d<   ddAlm1}%  |%|           n2# t"          $ r%}&tN          (                    dB|&           Y d}&~&nd}&~&ww xY w|j+        p|j#        }dC|j"         d$g}|                    dD|            |j2        }'ddElm3}( d})	 t                      }*|*                    di           }+t          |+t                    r&|+                    dF          },|,ti          |,          })n# t"          $ r Y nw xY w |(|j"        |j#        |j%        p7pd|j$        p6pd|'8|)G          }-|-r|                    dH|-dIdJ           |'r|'j5        r|                    dK|'j5        dIdJ           |'6                                r*|                    dL|'7                                            |                    dM|'8                                            ts          |j%        pddN          rdO|j"        :                                v p
|j&        dPk    }.|.r|                    dQ           |j;        r|                    dR|j;                    |
r|                    dS           n|                    dT           d2                    |          S )Uu  Handle /model command — switch model for this session.

        Supports:
          /model                              — interactive picker (Telegram/Discord) or text list
          /model <name>                       — switch for this session only
          /model <name> --global              — switch and persist to config.yaml
          /model <name> --provider <provider> — switch provider + model
          /model --provider <provider>        — switch to provider, auto-detect model
        r   N)switch_modelparse_model_flagslist_authenticated_providerslist_picker_providers)	get_labelr>   rF  r   r   rG   r   r   	providersrc  re  r   send_model_pickerr  )current_providercurrent_base_urlcurrent_modeluser_providersre  
max_models_chat_idmodel_idprovider_slugr   c                 N  K    |d|	  	        }|j         s
d|j         S d}t          dd          }t          dd          }|r1|/|5  |                              }ddd           n# 1 swxY w Y   |ru|d         m	 |d                             |j        |j        |j        |j        |j	                   n2# t          $ r%}t                              d	|           Y d}~nd}~ww xY wt          d
          si _        d d|j         d|j        p|j         dj        <   |j        |j        |j        |j        |j	        dj        <                                  |j        p|j        }d|j         dg}	|	                    d|            |j        }
ddlm} d}	 t-                      }|                    di           }t/          |t0                    r&|                    d          }|t3          |          }n# t          $ r Y nw xY w ||j        |j        |j        ppd|j        ppd|
|          }|r|	                    d|dd           |
r|
j        r|	                    d|
j        dd           |
                                r*|	                    d|
                                            |	                    d|
                                            |	                    d           d                    |	          S ) z6Perform the model switch and return confirmation text.F		raw_inputr  r  r  current_api_key	is_globalexplicit_providerr  re  Error: NrJ  rH  r   	new_modelnew_providerr   r   r"  z/Picker model switch failed for cached agent: %srS  $[Note: model was just switched from  to  via /. Adjust your self-identification accordingly.]r   r   r   r   r"  Model switched to `r  
Provider: resolve_display_context_lengthr   r  r>   r   r   
model_infore  r  	Context: r   tokensMax output: Cost: Capabilities: u<   _(session only — use `/model <name> --global` to persist)_rI  )r  r  r%   rA   r  r  target_providerr   r   r"  r   r*  r+  r  rS  provider_labelr  r  rN  r  hermes_cli.model_switchr  r  r1   rW   r4   
max_outputhas_cost_dataformat_costformat_capabilitiesrO  )r  r  r  r  cached_entryr  r  r2  plabelr(  mir  _sw_config_ctx_sw_cfg_sw_model_cfg_sw_rawctx_cur_api_key_cur_base_url
_cur_model_cur_provider_self_session_key_switch_modelr  r  rH  
user_provss                    r"   _on_model_selectedz?GatewayRunner._handle_model_command.<locals>._on_model_selectedN  s{      "/&.-:*4-:,8&+.;+5-9
" 
" 
"  &~ D#CV-A#C#CC (,&-e5H$&O&O!(!E!E& H6+=!, H H/5zz,/G/GH H H H H H H H H H H H H H H' 
gLO,G	g ,Q < <.4.>171G,2N-3_-3_ != !" !" !" !" $- g g g &/`be f f f f f f f fg  'u.DEE <9;E6M: M MSYSc M M#)#8#RF<RM M M 2<@ &,%5(.(>'-~(.(.H H6|D 11,??? "(!6!P&:P!Jv7G!J!J!J K%:&%:%:;;;#.ZZZZZZ)-!&:&<&<G,3KK,D,DM)->> B*7*;*;<L*M*M#*#658\\N( ! ! ! D!<<","2%+_%N8H%NB$*N$Ko$K')-92@    E!LL)CS)C)C)C)CDDD V!} V %-TBM-T-T-T-T U U U!//11 J %-Hbnn6F6F-H-H I I I!LL)T":P:P:R:R)T)TUUU%cddd#yy///s=   A55A9<A99C 
C4C//C43AH 
H H r  )r  r  r  r  rV  on_model_selectedr  z
Current: `r  z` on r  
is_currentz
 (current)**rF   z** `--provider rv  r  rh  rf  r  c              3   "   K   | ]
}d | d V  dS r  Nrw   r  s     r"   r  z6GatewayRunner._handle_model_command.<locals>.<genexpr>  s*      .M.MAx1xxx.M.M.M.M.M.Mr$   total_modelsz (+z more)z  api_urlz  `u    `/model <name>` — switch modelu5   `/model <name> --provider <slug>` — switch provideru$   `/model <name> --global` — persistrI  r  r  rJ  rH  r  z1In-place model switch failed for cached agent: %srS  r  r  r  r  r  r   r   )save_configz"Failed to persist model switch: %sr  r  r  r  r  r  r  r  r  r  r  zopenrouter.aiclaudeanthropic_messageszPrompt caching: enabledz	Warning: z!Saved to config.yaml (`--global`)z-_(session only -- add `--global` to persist)_)<r   r  r  r  r  r  hermes_cli.providersr  r  r6   rv   r  rA   r1   rW   r   rd  r   r  r  r  r   r   r%   r  r)   r  r  r  r  rN  rO  rK  r  r  r  r   r   r"  r*  r+  r  rS  r  r  rb   r   r   r;  r	  r  r  r4   r  r  r  r  r~   r_  warning_message):r_  rP  r   r  r  r  r  rp  model_inputr  rt  r  r  r   r   r  rd  r  rV  r  rY  
has_pickerr  r  r  r  r  r(  r  r4  
model_strsr  r  r  r  r2  r   r	  rb  r  r  _sw2_config_ctx_sw2_cfg_sw2_model_cfg_sw2_rawr  cache_enabledr  r  r  r  r  r  r  r  r  rH  r  s:                                                  @@@@@@@@@@@r"   r  z#GatewayRunner._handle_model_command  s      		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	

 	322222))++1133 :K9J89T9T6& '
"]2	&((C ?GGGR00	i.. E$-MM)R$@$@M'0}}ZAQ'R'R$'0}}Z'D'D$ WW[11
?QQQQQQ#B#B3#G#GLL  ? ? ?#&77+=#>#>LLL? 	 	 	D	 226::044["EE 	G$LL-@@M'||J8HII'||J8HII&ll9oFFO  g	$#4 g	$m''88Gt# RDMM+>EETQ 
  @$
# 5 5)9)9&3'1)5#%! ! !II ! # # # "III#  s$ !E#.L!.J$4M$4M#2L\0"%\014\0EH\0\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0| CIBR\V-=>>X\H#*#<#< &"+&3)9$/*<!) $= $ $      F ~ $#t 'Y'788NS-"<9SS>SSUWXE88%5%5"/#-%1   	 # 	% 	%A*+L/A,,rCLL!Rai!R!R&	!R!RC!R!R!RSSS{ <%)YY.M.M8.M.M.M%M%M
VWXfVgjmnopxnyjzjzVzVz Ra&7#ak:J:J&J R R R R  AC%=*%=e%=%=>>>>y)) <%:1Y<%:%:%:;;;LL$$$$	%     LL;<<<LLPQQQLL?@@@99U### !-'-+$/%)

 

 

 ~ 	43V1333 d$7>>~t44 	76- 7 7%zz+667 7 7 7 7 7 7 7 7 7 7 7 7 7 7  
	YLO7	YQ,,$.!'!7"N#_#_ -      Y Y YRTWXXXXXXXXY
 t344 	+(*D%== = =fFV = =(BF,B= = = 	!+. %.~6
 6
%k2 	  ---  	HH%%'' kG<<< 6"nnQ//526 6 6 6 6 6 6 6 6 6 6 6 6 6 6 CNN7B77	'-'7	)$(.(>	*%? <,2OIj)999999C     H H HCQGGGGGGGGH  .H&2H:v'7:::;2.22333 JJJJJJ	+--H%\\'266N.$// 4)--.>??'&)(mmO 	 	 	D	,,"_>(8>BN;o;)"1
 
 
  	5LL3S3333444 	F} FDBMDDDDEEE!! :8bnn&6&688999LLD"*@*@*B*BDDEEE #6?#8b/JJsx[a[k[q[q[s[sOs 7"66 	  	4LL2333! 	?LL=V%;==>>> 	JLL<====LLHIIIyys   0BD; D D; D74D; 6D77D; ;
EE(H: :I	I	 D#P 
PP7SS S/9T) )
U3UU%Y< =X!Y< !X%%Y< (X%)AY< <
Z+Z&&Z+0A] 
]]c                 L  K   ddl m} |                                                                                                }t
          dz  }	 t                      }t          |ddi           }n# t          $ r i }i }Y nw xY w|sd |             dS |sd	g}|	                    d
           |
                                D ]\  }}	t          |	t                    r4|	                    d          p|	                    dd          dd         }
n"t          |	          dk    r|	dd         dz   n|	}
|	                    d| d|
            |	                    d           d                    |          S d }|dv rs	 d|vs(t          |                    d          t                    si |d<   d|d         d<   t!          ||           n# t          $ r}d| cY d}~S d}~ww xY wd| _        dS ||v r |||                   }	 d|vs(t          |                    d          t                    si |d<   ||d         d<   t!          ||           n# t          $ r}d| cY d}~S d}~ww xY w|| _        d| dS dd                    d |D                       z   }d| d| S ) z8Handle /personality command - list or set a personality.r   ri  r   r   personalitiesrj  z No personalities configured in `z/config.yaml`u!   🎭 **Available Personalities**
u'   • `none` — (no personality overlay)r  ri  r>   Nr  rM  u   • `r  z
Usage: `/personality <name>`rI  c                    t          | t                    r|                     dd          g}|                     d          r|                    d| d                     |                     d          r|                    d| d                     d                    d |D                       S t          |           S )	Nri  r>   tonezTone: stylezStyle: rI  c              3      K   | ]}||V  	d S r  rw   rl  s     r"   r  zUGatewayRunner._handle_personality_command.<locals>._resolve_prompt.<locals>.<genexpr>   s'       7 7qQ 7 7 7 7 7 7 7r$   )r1   rW   rA   rN  rO  r)   )r   rQ  s     r"   _resolve_promptzBGatewayRunner._handle_personality_command.<locals>._resolve_prompty   s    %&& 8?B77899V$$ ;LL!9%-!9!9:::99W%% =LL!;5>!;!;<<<yy 7 7E 7 7 7777u::r$   )r~  rG   neutralu*   ⚠️ Failed to save personality change: uX   🎭 Personality cleared — using base agent behavior.
_(takes effect on next message)_u   🎭 Personality set to **z#**
_(takes effect on next message)_z`none`, r  c              3   "   K   | ]
}d | d V  dS r	  rw   )r  r*  s     r"   r  z<GatewayRunner._handle_personality_command.<locals>.<genexpr>   s*      *K*K8q888*K*K*K*K*K*Kr$   zUnknown personality: `z`

Available: )r  rj  r  r6   r_  rv   r  r   r   rN  r  r1   rW   rA   rK  rO  r}   r'  )r_  rP  rj  r$  r   r  r	  r(  rF   rk  r  r	  rb  
new_prompt	availables                  r"   r  z)GatewayRunner._handle_personality_command\   s     888888%%''--//5577"]2	)++F#FG_bQQQMM 	 	 	FMMM	  	[Z6I6I6K6KZZZZ 
	$9:ELLBCCC - 3 3 5 5 < <ffd++ R$jj77_6::oWY;Z;Z[^\^[^;_GG58[[25E5EfSbSkE116G:T::::;;;;LL9:::99U###	 	 	 111H&((
6::g;N;NPT0U0U(&(F7O35w0!+v6666 H H HGAGGGGGGGGH,.D)nn]""(t)<==JH&((
6::g;N;NPT0U0U(&(F7O3=w0!+v6666 H H HGAGGGGGGGGH -7D)ZZZZZ*K*K]*K*K*K!K!KK	IIIiIIIsP   !A. .A?>A?>AG 
G$GG$G$AI 
I,I'!I,'I,c                 D  K   |j         }| j                            |          }| j                            |j                  }d}d}t          t          |          dz
  dd          D ]A}||                             d          dk    r ||                             dd          }|} nB|sdS |d|         }| j                            |j        |           d	|_	        t          |t          j        ||j        |j        
          }	|                     |	           d{V S )z6Handle /retry command - re-send the last user message.Nr   rj  rS   rp  rr  r>   zNo previous message to retry.r   )r   rF  r  raw_messagerL  )r  r5  r  r  r  r  rK  rA   r  r  r  r  rL  r	  rL  r  )
r_  rP  r  r  rQ   last_user_msglast_user_idxrR  	truncatedretry_events
             r"   r  z#GatewayRunner._handle_retry_command   sI     *@@HH$44]5MNN s7||a'R00 	 	Aqz~~f%%// '
y" = = ! 0
  	322 N]N+	--m.F	RRR+,( #$)) /
 
 
 ))+666666666r$   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   r  r   ro  )	r1   r  rW   rA   r%   r   r  r4   r   )r_  	goals_cfgr  s      r"   _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   rQ  zgoal manager unavailable: %sNrf  z'goal manager: session lookup failed: %sr  r>   r  default_max_turns)
rT  rR  r   r*  r:  r5  r  r  r%   r'	  )r_  rP  rR  r2  r  r  r   s          r"   _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c                   K   |                                 pd                                }|                                }|                     |          \  }}|dS |r|dk    r|                                S |dk    r|                    d          }|dS 	 |j        r$| j                            |j        j	                  nd}|j        r| 
                    |j                  nd}|r|r|                     ||           n2# t          $ r%}	t                              d	|	           Y d}	~	nd}	~	ww xY wd
|j         S |dk    r#|                                }|dS d|j         dS |dv r|                                }
|                                 	 |j        r$| j                            |j        j	                  nd}|j        r| 
                    |j                  nd}|r|r|                     ||           n2# t          $ r%}	t                              d|	           Y d}	~	nd}	~	ww xY w|
rt'          d          nt'          d          S 	 |                    |          }n# t*          $ r}	d|	 cY d}	~	S d}	~	ww xY w|j        r$| j                            |j        j	                  nd}|j        r| 
                    |j                  nd}|r|r}	 t-          |j        t.          j        |j        |j        |j                  }|                     |||           n2# t          $ r%}	t                              d|	           Y d}	~	nd}	~	ww xY wd|j         d|j         dS )u  Handle /goal for gateway platforms.

        Subcommands: ``/goal`` / ``/goal status`` / ``/goal pause`` /
        ``/goal resume`` / ``/goal clear``. Any other text becomes the
        new goal.

        Setting a new goal queues the goal text as the next turn so the
        agent starts working on it immediately — the post-turn
        continuation hook then takes over from there.
        r>   Nz"Goals unavailable on this session.r*  rQ  zuser-pausedrA  zNo goal set.z3goal pause: pending continuation cleanup failed: %su   ⏸ Goal paused: rR  zNo goal to resume.u   ▶ Goal resumed: uT   
Send any message to continue, or wait — I'll take the next step on the next turn.)r  r"  r!  z3goal clear: pending continuation cleanup failed: %szgateway.goal_clearedzgateway.no_active_goalzInvalid goal: rK  zgoal kickoff enqueue failed: %su   ⊙ Goal set (z-turn budget): u   
I'll keep working until the goal is done, you pause/clear it, or the budget is exhausted.
Controls: /goal status · /goal pause · /goal resume · /goal clear)r  r6   r_  r+	  status_linerQ  r  r   rA   r   r  rO  r   r*  r:  rP  rR  has_goalr  r   r]  r7   r  r  rL  r  rL  r>  r   )r_  rP  r$  r_  mgrr  r  rY  r  r2  hadkickoff_events               r"   r  z"GatewayRunner._handle_goal_command   s:      &&((.B5577

!==eDD];77 	%u((??$$$GII]I33E}%~YFKl\$-++EL,ABBBX\KP<aT99%,GGG]a
 Pz P:::wOOO Y Y YRTWXXXXXXXXY3uz333HJJLLE}++fUZ f f f
 ---,,..CIIKKKYFKl\$-++EL,ABBBX\KP<aT99%,GGG]a
 Pz P:::wOOO Y Y YRTWXXXXXXXXY03T1+,,,;S9T9TT	*GGDMMEE 	* 	* 	*)C))))))))	*
 ?DlT$-##EL$9:::PTCH<YT11%,???UY
 	Ez 	E
E ,!,!1 <$/#(#7! ! ! "":}gFFFF E E E>DDDDDDDDESU_ S SUZ S S S	
si   A*D 
D2D--D2A*H   
H/
H**H/I) )
J3I=7J=JA	L$ $
M.MMr[  c                   K   | j                             |j                  }|s,t                              dt          |dd                     dS 	 |                     |          }n,# t          $ r t          |dd          r	d|j        in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  r  T)goal continuation: status send failed: %sr  r  )r   rA   r   r*  r:  r%   _thread_metadata_for_sourcer   r  r  r  r+  )r_  r  r[  rY  r  r  s         r"   _send_goal_status_noticez&GatewayRunner._send_goal_status_noticeG!  s4     -##FO44 	LL?Q[]aAbAbcccF	g77??HH 	g 	g 	g:A&+W[:\:\fV%566bfHHH	g ||FNGh|OOOOOOOOgfi&F&FNN;99     s   A' '&BB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.
        r3	  r   Nr   c                     K   	                                 d {V  d S # t          $ r(} t                              d| d           Y d } ~ d S d } ~ ww xY w)Nr4	  Trq  )r6	  r   r*  r+  )r2  r[  r_  r  s    r"   _deliverzHGatewayRunner._defer_goal_status_notice_after_delivery.<locals>._deliveri!  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: %sr8  )
r   rA   r   r*  r:  r%   r  r   r  r:	  )	r_  r  r[  rY  r9	  rV  r  r  r2  s	   ```      r"   (_defer_goal_status_notice_after_deliveryz6GatewayRunner._defer_goal_status_notice_after_deliveryZ!  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  ru  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   rQ  z/goal continuation: goals module unavailable: %sNr  r>   r)	  T)user_initiatedr[  should_continuecontinuation_promptrK  z%goal continuation: enqueue failed: %s)rT  rR  r   r*  r:  r%   r'	  rU  evaluate_after_turnrA   r=	  r   r   r  r  r  rL  r>  )r_  r  r  ru  rR  r2  r  r   r/	  r`  rX   rk  rY  r  
cont_events                  r"   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                 0  K   |j         }| j                            |          }| j                            |j                  }d}t          t          |          dz
  dd          D ]%}||                             d          dk    r|} n&|dS ||                             dd          }t          |          |z
  }| j                            |j        |d|                    d	|_	        t          |          d
k    r|dd
         dz   n|}	d| d|	 dS )z?Handle /undo command - remove the last user/assistant exchange.Nr   rj  rS   rp  zNothing to undo.rr  r>   r   (   rM  u   ↩️ Undid z message(s).
Removed: "rk  )
r  r5  r  r  r  r  rK  rA   r  r  )
r_  rP  r  r  rQ   r!	  rR  removed_msgremoved_countr  s
             r"   r  z"GatewayRunner._handle_undo_command!  s>     *@@HH$44]5MNN s7||a'R00 	 	Aqz~~f%%// ! 0  %%m,00B??G}4--m.FP^Q^P^H_```+,(.1+.>.>.C.C+crc"U**R}RRwRRRRr$   c                 T  K   |j         }|j        r|j        j        nd}|j        }|j        p|}t          |          }t          |          }|j        }	 ddlm	}	  |	|t          |                      |	|t          |pd                     n# t          $ r}
d|
 cY d}
~
S d}
~
ww xY w|j        rn| j        j                            |j        t          d                    }t!          |j        t          |          ||rt          |          nd	          |_        d
| d| dS )zOHandle /sethome command -- set the current chat as the platform's home channel.r  r   )save_env_valuer>   zFailed to save home channel: NTr  )r   r  rF   r  u   ✅ Home channel set to **z** (ID: z@).
Cron jobs and cross-platform messages will be delivered here.)r  r   r   r  	chat_namerp   rs   r  r   rI	  r)   r   r  r!  r;  r  r  home_channel)r_  rP  r  ri   r  rK	  r  thread_env_keyr  rI	  rb  r$  s               r"   r  z&GatewayRunner._handle_set_home_command!  s     17O--i.$/	&}55-m<<$		7888888N7CLL111 N>3yB+?+?@@@@ 	7 	7 	76166666666	7
 ? 
	"k3>>t,,, O ,7G,5?#i...4	, , ,O(M M MG M M M	
s   :B 
B)B$B)$B)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.r	  Nr  guild)r%   r  r  r4   rO	  id)rP  rD   s     r"   r  zGatewayRunner._get_guild_id"  st     e]D11;43
## 	% 	%s|$$$3   	 SY 	 9<tr$   c                   K   |                                                                                                 }|j        j        }|j        j        }|                     ||          }| j                            |          }|dv r;d| j	        |<   | 
                                 |r|                     ||d           	 dS |dv r:d| j	        |<   | 
                                 |r|                     ||d           d	S |d
k    r;d| j	        |<   | 
                                 |r|                     ||d           	 dS |dv r|                     |           d{V S |dk    r|                     |           d{V S |dk    r#| j	                            |d          }dddd}| j                            |j        j                  }|                     |          }	|	rt!          |d          r|                    |	          }
|
rd|                    ||           d|
d          d|
d          g}|
d         D ];}|                    d          rdnd}|                    d|d           |            <d!                    |          S d|                    ||           S | j	                            |d          }|dk    r:d| j	        |<   | 
                                 |r|                     ||d           d"S d| j	        |<   | 
                                 |r|                     ||d           d#S )$z8Handle /voice [on|off|tts|channel|leave|status] command.)r  enabler  TrJ	  z}Voice mode enabled.
I'll reply with voice when you send voice messages.
Use /voice tts to get voice replies for all messages.)r  disabler  r  z'Voice mode disabled. Text-only replies.ttsr  z;Auto-TTS enabled.
All replies will include a voice message.)channelrO  Nleaver*  zOff (text only)z"On (voice reply to voice messages)z!TTS (voice reply to all messages))r  r  r  get_voice_channel_infozVoice mode: zVoice channel: #channel_namezParticipants: member_countmembersis_speakingz (speaking)r>   z  - r7  rI  zVoice mode enabled.zVoice mode disabled.)r  r6   r_  r  r  r   r  r   rA   r[  r  r  r  _handle_voice_channel_join_handle_voice_channel_leaver  r  rX	  rN  rO  )r_  rP  r$  r  r   	voice_keyrY  r  labelsr  r9  r(  r  r*  rO   s                  r"   r  z#GatewayRunner._handle_voice_command"  s     %%''--//5577,&<(OOHg66	-##H--###*6DY'""$$$ S227GT2RRRH 
 '''*/DY'""$$$ U33GWt3TTT<<U]]*/DY'""$$$ S227GT2RRR<  (((88?????????W__99%@@@@@@@@@X#''	599D(B: F m''(=>>G))%00H ,GG-EFF ,55h?? 	,?vzz$'='=??A4+?AA?n)=??E
 ")_ I I23%%2F2F!NB%GAn,=%Gv%G%GHHHH99U+++:&**T4"8"8::: &**9e<<G%.: +&&((( W66wQU6VVV,,.3 +&&((( Y77SW7XXX--r$   c                 B  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
        |_        	 |                    |           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_disconnectz 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  TrJ	  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).)r   rA   r  r   r  r  get_user_voice_channelr  _handle_voice_channel_inputrc	  _handle_voice_timeout_cleanuprd	  rb	  r   r*  r+  r)   r_  r  r  r4   r  _voice_text_channelsto_dictrh	  r[  r  r  r  rF   )r_  rP  rY  r  voice_channelr  rb  	err_lowers           r"   r]	  z(GatewayRunner._handle_voice_channel_joinf"  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(	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+   5C 
EAE	9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  TrT	  rc	  zLeft voice channel.)r   rA   r  r   r  r  rr	  rq	  r   r*  r+  r[  r  r  r  r  rc	  )r_  rP  rY  r  rb  s        r"   r^	  z)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  TrT	  N)r[  r  r  r  r  r   rA   r  )r_  r  rY  s      r"   rk	  z+GatewayRunner._handle_voice_timeout_cleanup"  so    
 HM)97CCD   -##H$455++GWt+LLLLLr$   r  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]r>   Fg      (@r\  Nc                 0    g | ]\  }}|z
  k    ||fS rw   rw   )r  rY   txtrJ   window_secondss      r"   rm  z@GatewayRunner._is_duplicate_voice_transcript.<locals>.<listcomp>"  s:     
 
 
CRx>)) I)))r$   T   rg  )difflibrv	  r'   r+   r6   r_  rM   r  r%   r1   rW   r\  rA   rK  ratiorN  )r_  r  r  rt	  rv	  ra  r  recent_storerecentrn  priorrJ   rz	  s              @@r"   _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ur$   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).
        Nrh	  rV	  )r   r  r  r  r  z/Unauthorized voice input from user %d, ignoringz?Suppressing duplicate voice transcript for guild=%s user=%s: %srj  i  z	@everyoneu   @​everyonez@hereu   @​herez**[Voice]** <@z>: r   )SimpleNamespace)r  rO	  )r  r   rF  r	  )r   rA   r  r  rl	  r%   r
  	from_dictr)   r  r  r  r*  r:  r	  r9  _clientget_channelr9   r  r   typesr	  r  r  r  rM  )r_  r  r  rt	  rY  
text_ch_idsource_datar  rV	  	safe_textr	  rP  s               r"   rj	  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  r"  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 )rS   rq  c              3   p   K   | ]1}|                     d i                                d          dk    V  2dS )functionrF   text_to_speechNr  )r  tcs     r"   r  zCGatewayRunner._should_send_voice_reply.<locals>.<genexpr>.<genexpr>J#  sX         z2&&**6226FF     r$   
tool_callsN)rA   r  )r  rX   s     r"   r  z9GatewayRunner._should_send_voice_reply.<locals>.<genexpr>H#  s       
 
  GGFOO{*   77<006B    
 
 
 
 
 
r$   T)rL  r  r  r[  rA   r  r   rF  r  r  r  )
r_  rP  r  r"  r  r  
voice_modeis_voice_inputshouldhas_agent_ttss
             r"   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tr$   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 t          j                            t          j
                    dd|                                j        dd          d          }t	          j        t          j                            |          d	
           t          j        |||           d{V }
t#          j        |
          }|                    d|          }|                    d          rt          j                            |          sat*                              d|                    d                     	 ||hdhz
  D ]'}		 t	          j        |	           # t          $ r Y $w xY wdS | j                            |j        j                  }|                     |          }|rRt7          |d          rBt7          |d          r2|                    |          r|                    ||           d{V  nW|rUt7          |d          rE|j        j        ||j        d}|j        j         rd|j        j         i|d<    |j!        di | d{V  n4# tD          $ r'}t*                              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)text_to_speech_tool_strip_markdown_for_ttsi  hermes_voice
tts_reply_r  z.mp3T)r  )r   output_pathrf  r  zAuto voice reply TTS failed: %sr  play_in_voice_channelrr	  
send_voice)r  
audio_pathr  r  r  zAuto voice reply failed: %srq  rw   )#uuidtools.tts_toolr	  r	  r?   r  r  ra   rO  tempfile
gettempdiruuid4hexmakedirsdirnamer9  r$  ru  rv  rA   isfiler*  r+  r   r  r   r  r  rr	  r	  r  r  r  r	  r   )r_  rP  r   _uuidr	  actual_pathr	  r	  tts_textr  result_jsonr  rY  r  send_kwargsrb  s                   r"   r  zGatewayRunner._send_voice_reply]#  sV     
3	SSSSSSSS..tETE{;;H T !+.$7  IaLLLL   D M #%%~9U[[]].ss3999 J K
33dCCCC ' 1#(
! ! !      K Z,,F !**[*==K::i(( {0K0K @&**WBUBUVVV. !+.$7  IaLLLL   D + 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$|3"- % 0/ /
 <) T/:EL<R.SK
+(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   J% A


AAD1J% F..
F;:F; C$J% $L %
K/KL KL $K99
LLL=L,+L=,
L9	6L=8L9	9L=c                 .  K   ddl m} ddlm 	 d|v }|                    |          \  }}|                    |          \  }}|                    |          \  }	}|j        j        rd|j        j        ind}
ddl	m
} 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  N)should_send_media_as_audio>   .3gp.avi.mkv.mov.mp4.webm>   .gif.jpg.png.jpeg.webpc                 0    g | ]}d  |           dfS )zfile://r>   rw   )r  r  _quotes     r"   rm  z>GatewayRunner._deliver_media_from_response.<locals>.<listcomp>#  s/    OOOa444b9OOOr$   )r  imagesr  z0[%s] Post-stream image batch delivery failed: %s)is_voice)r  r	  r  )r  
video_pathr  r  rf  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  extract_mediaextract_imagesextract_local_filesr  r  gateway.platforms.baser	  suffixr_  rN  send_multiple_imagesr  r   r*  r+  rF   r   r	  
send_videosend_document)r_  r  rP  rY  r   force_document_attachmentsmedia_filesrn  cleanedlocal_files_thread_metar	  _VIDEO_EXTS_IMAGE_EXTSr+  non_image_media
media_pathr	  extnon_image_localrf  r	  rb  r	  s                          @r"   r  z*GatewayRunner._deliver_media_from_response#  s      	!     000000_	I
 *;h)F&$228<<NK //99JAw$88AANKDILDZdK)?@@`dLIIIIII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   D3M$ 6E< ;M$ <
F1!F,'M$ ,F11
M$ <B8I54M$ 5
J*?!J% M$ %J**M$ 2A7L*)M$ *
M4!MM$ MM$ $
N.NNc                 N  K   ddl m}m} i }	 ddl}t          dz  }|                                rot          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   |                    di           }t          |t                    rd|i}n# t          $ r Y nw xY w|                    dd	          s	 d
S  |d|                    dd                    }	t          j        dt          t          j                                        }
|                                                                }|s!|	                    |
          } |||
          S |	                    |
          }|sd|
 S d}	 t)          |          dz
  }d|cxk    rt+          |          k     rn n||         d         }ndt+          |           dS n# t,          $ r |}Y nw xY w|	                    |
|          }|d         rd|d          d|d          dS d|d          S )uD   Handle /rollback command — list or restore filesystem checkpoints.r   )CheckpointManagerformat_checkpoint_listNr   r   r   r  r  FzXCheckpoints are not enabled.
Enable in config.yaml:
```
checkpoints:
  enabled: true
```Tmax_snapshotsr  )r  r	  r   zNo checkpoints found for r   hashz!Invalid checkpoint number. Use 1-r   r  u   ✅ Restored to checkpoint restored_tor   r&  z1
A pre-rollback snapshot was saved automatically.u   ❌ r  )rU  r	  r	  r   rv   rb   r   r   rA   r1   r3   r   r?   r)  r)   r   r  r  r6   list_checkpointsr4   rK  r7   restore)r_  rP  r	  r	  cp_cfgr;  	_cfg_pathr=  _datar/	  r   argr  target_hashr  r  s                   r"   r  z&GatewayRunner._handle_rollback_command$  s     VVVVVVVV 
	$}4I!! 1)g666 3"LL,,2E3 3 3 3 3 3 3 3 3 3 3 3 3 3 3="55fd++ 1'0F 	 	 	D	 zz)U++ 	R 
   **_b99
 
 

 iDIKK(8(899$$&&,,.. 	<..s33K))+s;;; **3// 	54s444	c((Q,CC****#k*******)#.v6N3{;K;KNNNN   	 	 	KKK	 S+..) 	Df].C D DvhGW D D D (fWo'''sH   3B  A%B  %A))B  ,A)-2B   
B-,B-AG G&%G&c                 $  K   |                                                                 }|s	 dS |j        }dt          j                                        d           dt          j        d                                           }t          j
        |                     |||                    }| j                            |           |                    | j        j                   |dd         t!          |          dk    rdnd	z   }d
| d| dS )u)  Handle /background <prompt> — run a prompt in a separate background session.

        Spawns a new AIAgent in a background thread with its own session.
        When it completes, sends the result back to the same chat without
        modifying the active session's conversation history.
        u   Usage: /background <prompt>
Example: /background Summarize the top HN stories today

Runs the prompt in a separate session. You can keep chatting — the result will appear here when done.bg_z%H%M%Srn  rg  Nr  rM  r>   u   🔄 Background task started: "z"
Task ID: u9   
You can keep chatting — results will appear when done.)r  r6   r  r   rJ   r  r?   urandomr	  r9  r9  _run_background_taskr^  r  r:  r  rK  )r_  rP  rk  r  r
  r  r  s          r"   r  z(GatewayRunner._handle_background_commandB$  s3      ''))//11 	S  Q//99QQBJqMM<M<M<O<OQQ #%%ffg>>
 
 	""5))) 6 >???"+#f++*:*:C J  J  Jg  J  J  J  	Jr$   rk  r
  r
  c                    K   ddl m  j                            j                  }|s#t
                              dj                   dS j        r	dj        ind}	 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                                                    _                                          _                             ||           fd}                     |           d{V }|r|                    dd          nd}|s"|r |                    d          rd|d          }|r*|                    |          \  }}|                    |          \  }}dd         t9                    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|pg D ]9\  }}	 |                    j        ||           d{V  *# t<          $ r Y 6w xY wdS dd         t9                    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   rk  z0No adapter for platform %s in background task %sNr  )r  r  r   u   ❌ Background task z, failed: no provider credentials configured.r  _get_platform_toolsr   disabled_toolsetsr   rS  )r  c            
          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	        } 	 | 
                    #          	                    |            S # 	                    |            w xY w)%Nr   r/  r  ru  Tverbose_loggingFrw  r	  rz  r  r  providers_allowedonlyproviders_ignoredr  providers_orderorderprovider_sortr  provider_require_parametersrequire_parametersprovider_data_collectiondata_collectionr  r   r  r  r  rK	  r  r  r  r6  )r  r
  rw   )r+  rA   r  r  r  rK	  r  r  rR  r3  run_conversationr  )r   rl  r	  rw  r  r  prrk  rz  r_  r  r
  
turn_routes    r"   run_syncz4GatewayRunner._run_background_task.<locals>.run_sync$  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'( %..)* #NN+, %..-. %../0 %..12  $//34 $(#7#75 89 11%+ ' 2  
 11%8888D11%8888s   E Eru  r>   r  r  r  rM  u&   ✅ Background task complete
Prompt: "z"

)r  rr  r  z(No response generated))r  	image_urlcaptionr  r	  z"

(No response generated)zBackground task %s failedz	 failed: )!r  rl  r   rA   r   r*  r+  r  r  r  r  r  r  hermes_cli.tools_configr	  r  r1  r4   r?   r)  ry  r)  r*  r+  r  _run_in_executor_with_contextr	  r	  rK  
send_imager   r	  rP  ) r_  rk  r  r
  rY  _thread_metadatar  r   r  r	  r   r	  r  r  r	  r	  text_contentr  rc  r	  alt_textr	  	_is_voicerb  rl  r	  rw  r  r  r	  rz  r	  s    ````                    @@@@@@@@r"   r	  z"GatewayRunner._run_background_task_$  sr      	&%%%%%-##FO44 	NNMv`ghhhF>D>NXK)9::TXC	.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#9 #9 #9 #9 #9 #9 #9 #9 #9 #9 #9 #9 #9 #9 #9 #9J  ==hGGGGGGGGF;AIvzz"2B777rH 7 76::g+>+> 76VG_66  0(/(=(=h(G(G%X'.'='=h'G'G$ "+#f++2B2BKQ7QQQ !,, & & 5!1 '          
    !,, & &)B B!1 '          -3Lb 	 	'Ix%00$*N&/$,%5	 1           %    /:.?R  )J	%33$*N&0%5 4          
 %     !"+#f++2B2BKll"Nkgkkk- #             		 		 		8'BBBll"NH7HHQHH- #          
          		s   ,A$N
 G7N
 
$K/.N
 /
K<9N
 ;K<<N
 	#L-,N
 -
L:7N
 9L::N
 ?A	N
 

O;O60)OO6
O'$O6&O''O66O;c                 r  K   ddl |                                                                }|                     |          \  }}t          dz  |                     |j                  }|                                 | _        | 	                    |j        |          | _
        dt          ffd}|sn| j
        }|d}n0|                    d          d	u rd
}n|                    dd          }| j        rdnd}	|t          | di           pi v }
|
rdnd}d| d| d|	 dS t          |j        j                  }|dv rd| _         |d| dd           d| dS |dv rd	| _         |d| dd	           d| dS |                                }|dk    rJ|rd S |                     |d           |                                 | _
        |                     |           d!S |d"k    rdd	i}n$|d#v rd|d$}nd%|p|                                 d&S || _
        |rn |d'|          r1|                     |d           |                     |           d(| d)S |                     ||           |                     |           d(| d*S |                     ||           |                     |           d(| d+S ),u]  Handle /reasoning command — manage reasoning effort and display toggle.

        Usage:
            /reasoning                       Show current effort level and display state
            /reasoning <level>               Set reasoning effort for this session only
            /reasoning <level> --global      Persist reasoning effort to config.yaml
            /reasoning reset                 Clear this session's reasoning override
            /reasoning show|on               Show model reasoning in responses
            /reasoning hide|off              Hide model reasoning from responses
        r   Nr   rw  key_pathc                    	 i }                                 r@t          d          5 }	                    |          pi }ddd           n# 1 swxY w Y   |                     d          }|}|dd         D ].}||vst	          ||         t
                    si ||<   ||         }/|||d         <   t          |           dS # t          $ r'}t          	                    d| |           Y d}~dS d}~ww xY w	z(Save a dot-separated key to config.yaml.r   r   Nr   rj  Tz Failed to save config key %s: %sF
rb   r   r   r^  r1   rW   r}   r   r*  r  
r
  r   r  r   r  rO   r  rb  r   r   s
           r"   _save_config_keyzAGatewayRunner._handle_reasoning_command.<locals>._save_config_key%  a    %%'' >kG<<< >&*nnQ&7&7&=2> > > > > > > > > > > > > > >~~c**%crc ) )A''z'!*d/K/K'%'
%ajGG$)R!!+{;;;t   ?1MMMuuuuu;   'C AC AC AA.C 
C7C22C7zmedium (default)r  Fznone (disabled)ro  mediumu   on ✓r  r   zsession overridezglobal configu*   🧠 **Reasoning Settings**

**Effort:** `z`
**Scope:** z
**Display:** zW

_Usage:_ `/reasoning <none|minimal|low|medium|high|xhigh|reset|show|hide> [--global]`)showr  Tzdisplay.platforms.z.show_reasoninguZ   🧠 ✓ Reasoning display: **ON**
Model thinking will be shown before each response on ****.)hider  u*   🧠 ✓ Reasoning display: **OFF** for **r	  resetut   ⚠️ `/reasoning reset --global` is not supported. Use `/reasoning <level> --global` to change the global default.uK   🧠 ✓ Session reasoning override cleared; falling back to global config.r~  )minimallowr
  highxhigh)r  ro     ⚠️ Unknown argument: `z`

**Valid levels:** none, minimal, low, medium, high, xhigh
**Display:** show, hide
**Persist:** add `--global` to save beyond this sessionzagent.reasoning_effortu"   🧠 ✓ Reasoning effort set to `z4` (saved to config)
_(takes effect on next message)_uH   ` (session only — config save failed)
_(takes effect on next message)_uO   ` (session only — add `--global` to persist)
_(takes effect on next message)_)r   r  r6   rv  rv   r  r  r,  r-  ry  r)  r)   rA   r%   r  r   r|  r(  r  r_  )r_  rP  rp  r$  rt  rV  r

  rcleveldisplay_statehas_session_overridescoper  ro  r}  r   r   s                  @@r"   r  z'GatewayRunner._handle_reasoning_command$  s      	))++1133#AA(KKn"]2225<@@#88::!%!G!G<# "H "
 "

	s 	 	 	 	 	 	 	(  	'Bz*	""e++)x22(,(<GHH%M#.74A_ac3d3d3jhj#k *>S&&OEh %h h#h h !.h h h ,EL,ABB>!!#'D O,OOOQUVVV\JV\ \ \
 ?""#(D O,OOOQVWWWPPPPP W N N  N00dCCC%)%@%@%B%BD"$$[111``V'FFDDD!%88FFJV-Gx~~7G7G J J J "( 	J 8&AA z44[$GGG((555yFyyyy00fEEE$$[111 J  J  J  J  J,,[&AAA  --- MF  M  M  M  	Mr$   c                   
K   ddl ddlm} |                                                                                                }t          dz  
|                                 | _        t                      }t          |          } ||          sdS dt          f
fd}|r|dk    r| j        d	k    rd
nd}d| dS |dv rd	| _        d
}d}	n|dv rd| _        d}d}	nd| dS  |d|          rd|	 dS d|	 dS )uL   Handle /fast — mirror the CLI Priority Processing toggle in gateway chats.r   N)model_supports_fast_moder   uO   ⚡ /fast is only available for OpenAI models that support Priority Processing.r
  c                    	 i }                                 r@t          d          5 }	                    |          pi }ddd           n# 1 swxY w Y   |                     d          }|}|dd         D ].}||vst	          ||         t
                    si ||<   ||         }/|||d         <   t          |           dS # t          $ r'}t          	                    d| |           Y d}~dS d}~ww xY wr
  r
  r	
  s
           r"   r

  z<GatewayRunner._handle_fast_command.<locals>._save_config_keyp%  r
  r
  r*  r  r  r  u(   ⚡ Priority Processing

Current mode: `z(`

_Usage:_ `/fast <normal|fast|status>`>   r  r  FAST>   r  r  NORMALr
  z*`

**Valid options:** normal, fast, statuszagent.service_tieru   ⚡ ✓ Priority Processing: **z5** (saved to config)
_(takes effect on next message)_z** (this session only))r   r   r
  r  r6   r_  rv   r*  r+  r  r  r)   )r_  rP  r
  r$  r  r   r

  r*  saved_valuer  r   r   s             @@r"   r  z"GatewayRunner._handle_fast_commandb%  s     >>>>>>%%''--//5577"]2!4466*,,&{33''.. 	edd	s 	 	 	 	 	 	 	(  	tx''#1Z??VVXF8"(8 8 8 >!!!+D KEE&&&!%D"KEE:T : : :
 0+>> 	srUrrrrNNNNNr$   c                    K   ddl m}m}m} |                     |j                  } ||          }|r ||           t          d          S  ||           t          d          S )uP   Handle /yolo — toggle dangerous command approval bypass for this session only.r   )disable_session_yoloenable_session_yolois_session_yolo_enableduW   ⚠️ YOLO mode **OFF** for this session — dangerous commands will require approval.uW   ⚡ YOLO mode **ON** for this session — all commands auto-approved. Use with caution.)r~  r#
  r$
  r%
  r  r  r  )r_  rP  r#
  r$
  r%
  rV  rO   s          r"   r  z"GatewayRunner._handle_yolo_command%  s      	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 225<@@))+66 	}  ---!"{|||,,,!"{|||r$   c                   K   t           dz  }t          |j        j                  }	 t	                      }t          t          |dd          d          }n# t          $ r d}Y nw xY w|s	 dS g d}dd	d
dd}ddlm	}  |||dd          }	|	|vrd}	|
                    |	          dz   t          |          z  }
||
         }	 d|vs(t          |                    d          t                    si |d<   |d         }d|vs(t          |                    d          t                    si |d<   ||d         vs.t          |d                             |          t                    si |d         |<   ||d         |         d<   t          ||           ||          d| dS # t          $ r3}t                               d|           ||          d| dcY d}~S d}~ww xY w)u  Handle /verbose command — cycle tool progress display mode.

        Gated by ``display.tool_progress_command`` in config.yaml (default off).
        When enabled, cycles the tool progress mode through off → new → all →
        verbose → off for the *current platform*.  The setting is saved to
        ``display.platforms.<platform>.tool_progress`` so each channel can
        have its own verbosity level independently.
        r   r   tool_progress_commandFrj  zThe `/verbose` command is not enabled for messaging platforms.

Enable it in `config.yaml`:
```yaml
display:
  tool_progress_command: true
```)r  rH  r  rT  u9   ⚙️ Tool progress: **OFF** — no tool activity shown.uv   ⚙️ Tool progress: **NEW** — shown when tool changes (preview length: `display.tool_preview_length`, default 40).ut   ⚙️ Tool progress: **ALL** — every tool call shown (preview length: `display.tool_preview_length`, default 40).uJ   ⚙️ Tool progress: **VERBOSE** — every tool call with full arguments.r   r  tool_progressr  r   r!  z
_(saved for **u%   ** — takes effect on next message)_z%Failed to save tool_progress mode: %sz
_(could not save to config: z)_N)rv   r  r  r   r  r   r   r   r  r  indexrK  r1   rA   rW   r}   r*  r+  )r_  rP  r   r  r  gate_enabledcycledescriptionsr  rO   r  new_moder   rb  s                 r"   r  z%GatewayRunner._handle_verbose_command%  s      #]2+EL,ABB	!.00K*Y0GHH  LL  	! 	! 	! LLL	!  	?  100N L Jc	
 
 	CBBBBB))+|_V[\\%G{{7##a'3u::5:	R++:kooi>X>XZ^3_3_+)+I&!),G'))GKK<T<TVZ1[1[)')$7;#777z'R]J^JbJbcoJpJprv?w?w757$\2BJGK .?k;777) U U!-U U U  	R 	R 	RNNBAFFF"8,QQAQQQQQQQQQ	Rs0   .A A%$A%CF 
G)(GGGc                 &  K   ddl m} t          dz  }t          |j        j                  }d}	 t          |dd          pd                                }|                    d          rU|	                    dd          }t          |          dk    r,|d                                                                         }n# t          $ r d}Y nw xY w	 t                      }n(# t          $ r}	t          d	|	
          cY d}	~	S d}	~	ww xY w |||          }
|dv rB|
d         rdnd}d                    |
                    d          pg           }d| d| d| dS |dv rd}n|dv rd}n|dk    r
|
d          }ndS 	 t#          |                    d          t$                    si |d<   |d         }t#          |                    d          t$                    si |d<   ||d         d<   t'          ||           nC# t          $ r6}	t(                              d|	           t          d|	
          cY d}	~	S d}	~	ww xY w|rdnd}d}|rCddl m}  |t/          |          pddd|
                    d          pg d           }|rd!| d}d| d"| d#S )$u8  Handle /footer command — toggle the runtime-metadata footer.

        Usage:
            /footer           → toggle on/off
            /footer on        → enable globally
            /footer off       → disable globally
            /footer status    → show current state + fields

        The footer is saved to ``display.runtime_footer.enabled`` (global).
        Per-platform overrides under ``display.platforms.<platform>.runtime_footer``
        are respected but not modified here — edit config.yaml directly for
        per-platform control.
        r   )resolve_footer_configr   r>   r[  Nr   r   zgateway.config_read_failed)r  )r*  r  r  ONOFFr  fieldsu   📎 Runtime footer: **z**
Fields: `z`
Platform: `r  )r  rR	  r  ry   T)r  rS	  rF  rE  Fz Usage: `/footer [on|off|status]`r   runtime_footerz)Failed to save runtime_footer.enabled: %szgateway.config_save_failed)format_runtime_footer)r   context_pctr   )r   r  r  r2
  z
Example: `r	  u4   
_(saved globally — takes effect on next message)_)r  r/
  rv   r  r  r   r%   r6   rL  r^  rK  r_  r   r  r   rO  rA   r1   rW   r}   r*  r+  r4
  r  )r_  rP  r/
  r   r  r	  r   rQ  r  rb  	effectiver  r2
  	new_stater   exampler4
  r  s                     r"   r  z$GatewayRunner._handle_footer_command%  s      	A@@@@@"]2+EL,ABB 	E9d339r@@BBDs## 3

4++u::>>(..**0022C 	 	 	CCC		< 4 6 6KK 	< 	< 	<1;;;;;;;;;	< *)+|DD	/!!%i0;DDeEYYy}}X66<"==F.% . .". .*. . . ///II444IIBYY%i00II55
	<kooi88$?? ,)+I&!),Ggkk*:;;TBB /,.()3<G$%i0k;7777 	< 	< 	<NNFJJJ1;;;;;;;;;	< ",u 
	4DDDDDD++,[99AT # }}X..Q2Q2Q2Q	  G  43333Ce C CC C C	
sO   BB? ?CCC! !
D+D;DD<A=G: :
H:+H5/H:5H:c                   K   |j         }| j                            |          }| j                            |j                  }|rt          |          dk     rdS |                                pd                                pd	 ddlm	} ddl
m} ddlm} |                     |          }|                     ||	          \  }	}
|
                    d
          sdS d |D              |d-i |
|	ddddg|j        d	 d _        t%          dd          pd}t%          dd          pd} |||          j        }|                              s-	 |                     |           |                                dS t/          j                    }|                    dfd           d{V \  }}j        }||j        k    r ||_        | j                                         | j                            ||           | j                            |j        d            ||||          } |||          }t=          t%          |dd                    }t?          t%          |dd          pd          }t%          |dd          }t%          |dd          }t%          |dd          }|                     |           |                                n/# |                     |           |                                w xY wd|d          g}r|                     d d            |                     |d!                    |d"         r|                     |d"                    |r|                     d#|pd$ d%| d&           n |r|                     d'| d(|pd$ d)           d*!                    |          S # tD          $ r*}tF          $                    d+|           d,| cY d}~S d}~ww xY w).a  Handle /compress command -- manually compress conversation context.

        Accepts an optional focus topic: ``/compress <focus>`` guides the
        summariser to preserve information related to *focus* while being
        more aggressive about discarding everything else.
        r  z?Not enough conversation to compress (need at least 4 messages).r>   Nr   rk  )summarize_manual_compression)estimate_request_tokens_roughrw  r   z*No provider configured -- cannot compress.c                     g | ]Y}|                     d           dv |                     d          .|                     d           |                     d          dZS rn  r  r  s     r"   rm  z:GatewayRunner._handle_compress_command.<locals>.<listcomp>g&  sg       55==$999aeeI>N>N9 v1553C3CDD999r$   Trs  rt  c                      d S r  rw   ry  s     r"   r  z8GatewayRunner._handle_compress_command.<locals>.<lambda>w&  s    t r$   _cached_system_promptr|  )ri  r|  zHNothing to compress yet (the transcript is still all protected context).c                  6                         d           S )Nr>   )r}  focus_topicr~  )r}  r@
  msgs	tmp_agents   r"   r  z8GatewayRunner._handle_compress_command.<locals>.<lambda>&  s     I77bP]kv7ww r$   r  r  Fr  r  r  r  u   🗜️ headlinezFocus: "rk  
token_linenoteu"   ⚠️ Summary generation failed (r  r  z historical message(s) were removed and replaced with a placeholder; earlier context is no longer recoverable. Consider checking your auxiliary.compression model configuration.r  r  r  rI  zManual compress failed: %szCompression failed: rw   )%r  r5  r  r  r  rK  r  r6   r  rl  !agent.manual_compression_feedbackr:
  r#  r;
  r  r  rA   r  r%   r  has_content_to_compressr  r  r9  r  r  r  r  r  rV  r3   r4   rN  rO  r   r*  r+  )r_  rP  r  r  rQ   rl  r:
  r;
  rV  r   r  _sys_prompt_tools
compressorr  
compressedrn  new_session_id
new_tokensr  _summary_failed_dropped_count_summary_err_aux_fail_model_aux_fail_errr(  rb  r}  r@
  rA
  rB
  s                              @@@@r"   r  z&GatewayRunner._handle_compress_commandI&  s      *@@HH$44]5MNN 	U#g,,**TT --//52<<>>F$q	.))))))VVVVVVJJJJJJ66v>>K$($G$G' %H % %!E> "%%i00 DCC    D       "*(3   I=9&;&;	# &i1H"MMSQS GT::Bd = =6! ! ! '9
!99$?? feZ ((555--i88888Y /11&*&:&:wwwwwww' ' ! ! ! ! ! !
A "+!5!]%===/=M,&,,..."55njQQQ"11!-! 2    ;:k  
 76!	  #'wz;XZ_'`'`"a"a!$WZ9VXY%Z%Z%_^_!`!`&z3H$OO #**6UW["\"\ '
4SUY Z Z ((555--i8888 ((555--i88885
 3556E :8888999LL.///v .WV_--- X9X X X%X X X    ! DO D D%8D D D   99U### 	. 	. 	.NN7;;;-!--------	.sL   	AO !#O AK( *O 
D3K( =+O (,LB=O 
PP;PP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.r   N_botget_mecheckedFz1Failed to fetch Telegram getMe topic capabilitiesTrq  rF   c                    t          |           rt          |           S t          dd           }t          |t                    r| |v r|                    |           S t          t                    r                    |           S d S )N
api_kwargs)r  r%   r1   rW   rA   )rF   rX
  mes     r"   _fieldz>GatewayRunner._get_telegram_topic_capabilities.<locals>._field&  s    r4   )r4((( \488J*d++ ,
0B0B!~~d+++"d## $vvd||#4r$   has_topics_enabledallows_users_to_create_topics)rV
  r[
  r\
  )
r%   r   rA   r   r  rU
  r   r*  r:  r)   )r_  r  rY  botrZ
  rY
  s        @r"    _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.r   N_create_dm_topicSystemz&Failed to create Telegram System topicTrq  z,System topic for Hermes commands and status.r  r  r  z*Failed to send Telegram System topic introrT
  pin_chat_message)r  r  disable_notificationz)Failed to pin Telegram System topic intro)r%   r   rA   r   r  callabler4   r   r*  r:  r  r)   r  rb
  )r_  r  rY  r  create_topicr  send_resultr]
  s           r"   _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.r   Nsend_image_fileassetsz'telegram-botfather-threads-settings.jpgu/   BotFather → Bot Settings → Threads Settingsr  )r  
image_pathr	  r  z)Failed to send Telegram topic setup imageTrq  )r%   r   rA   r   r  r  r   r   r   r  rb   ri
  r)   r  r   r*  r:  )r_  r  rY  rk
  s       r"    _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r&  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.rw	  r]  r>   zHermes Chatr  Nu   rM  )r'   r+   r)   r6   rK  r  )r_  r&  r	  s      r"   _sanitize_telegram_topic_titlez,GatewayRunner._sanitize_telegram_topic_title$'  sp    &c%+2&6&677==?? 	! = w<<#dsdm**,,u4Gr$   c                   K   |                      |          r|j        r|j        s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.Nr   _get_dm_topic_inforR  rP  r  r>   z5Failed to verify Telegram topic binding before renameTrq  rename_dm_topic)r  r  rF   rT
  edit_forum_topiceditForumTopic)r  message_thread_idrF   z8Failed to rename Telegram topic for auto-generated title)r  r  r  r%   r   rA   r   r  rd
  r)   r   r1   rW   r  r*  r:  ro
  r4   rC   r7   )r_  r  r  r&  rY  get_infooperator_topicr  r  
topic_namerename_topicr]
  rs
  s                r"   (_rename_telegram_topic_for_session_titlez6GatewayRunner._rename_telegram_topic_for_session_title/'  s      ++F33 	6> 	QWQa 	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\   
1B< <C
C9A2E. .&FF5AJ4 ?J4 8I> >/J1-J4 0J11J4 4&KKc                    |r|                      |          s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          j	        | 
                    |||          |          }dd}|                    |           dS )z>Schedule a topic rename from the auto-title background thread.Nr8  r   c                     	 |                                   d S # t          $ r  t                              dd           Y d S w xY w)Nz"Telegram topic title rename failedTrq  )r  r   r*  r:  )futs    r"   _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r8  )r  r9  r  r-  r%   	is_closedrv  r9   r   run_coroutine_threadsaferz
  r:  )r_  r  r  r&  r  copied_sourcefuturer~
  s           r"   %_schedule_telegram_topic_title_renamez3GatewayRunner._schedule_telegram_topic_title_rename}'  s     	D88@@ 	F	8+--DD 	8 	8 	84$77DDD	8<4>>++<F	#'/77MM 	# 	# 	#"MMM	#199-UZ[[
 
	R 	R 	R 	R 	  !455555s!   / AA)A> >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_tsr>   Tr   Nr  F)r  r
  r)   r  rM   r  rA   $_TELEGRAM_CAPABILITY_HINT_COOLDOWN_Sr  s         r"   %_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tr$   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.rw   r  s    r"   _telegram_topic_help_textz'GatewayRunner._telegram_topic_help_text'  s    M	
 	
r$   c                    | j         s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.Session database not available.r>   zCould not determine chat ID.r  Fz@Multi-session topic mode is not currently enabled for this chat.)r  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.)rR  r)   r  r  r  r   disable_telegram_topic_moder*  rP  r%   r1   rW   r  )r_  r  r  currently_enabledr2  attrstores          r"   %_disable_telegram_topic_mode_for_chatz3GatewayRunner._disable_telegram_topic_mode_for_chat'  sv    	544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	
 	
s/   0A A%$A%-B	 	
B<B71B<7B<r$  c                 V  K   |j         }|j        t          j        k    s|j        dk    rdS | j        sdS t          | dd          }t          |          r;	  ||          sdS n,# t          $ r t          
                    dd	           Y nw xY w|                                                                }|                                d
v r|                                 S |                                dv r|                     |          S |r&|j        s	 dS |                     ||           d{V S |                     |           d{V }|                    d          r|                    d          du r3|                     |          r|                     |           d{V  	 dS |                    d          du r3|                     |          r|                     |           d{V  	 dS 	 | j                            t/          |j                  t/          |j                  |                    d          |                    d                     n6# t          $ r)}t                              d           d| cY d}~S d}~ww xY w|j        s|                     |           d{V  |j        r	 | j                            t/          |j                  t/          |j                            }n.# t          $ r! t          
                    dd	           d}Y nw xY w|rat/          |                    d          pd          }d}		 | j                            |          }	n# t          $ r d}	Y nw xY w|	pd}
d|
 d| dS 	 dS |                     |          S )z:Handle /topic for Telegram DM user-managed topic sessions.r  z?The /topic command is only available in Telegram private chats.r
  r  Nz1You are not authorized to use /topic on this bot.zTopic auth check failedTrq  >   -h--helprV  r  >   r  r"  rS	  zTo restore a session, first create or open a Telegram topic, then send /topic <session-id> inside that topic. To create a new topic, open All Messages and send any message there.rV
  r[
  Fu  Telegram topics are not enabled for this bot yet.

How to enable them:
1. Open @BotFather.
2. Choose your bot.
3. Open Bot Settings → Threads Settings.
4. Turn on Threaded Mode and make sure users are allowed to create new threads.

Then send /topic again.r\
  u   Telegram topics are enabled, but users are not allowed to create topics.

Open @BotFather → choose your bot → Bot Settings → Threads Settings, then turn off 'Disallow users to create new threads'.

Then send /topic again.)r  r  r[
  r\
  z$Failed to enable Telegram topic modez&Failed to enable Telegram topic mode: rP  rQ  r  r>   Untitled sessionz"This topic is linked to:
Session: z
ID: z

Use /new to replace this topic with a fresh session.
For parallel work, open All Messages and send a message there to create another topic.zTelegram multi-session topics are enabled.

This topic will be used as an independent Hermes session. Use /new to replace this topic's current session. For parallel work, open All Messages and send a message there to create another topic.)r  r   r  r  r  rR  r%   rd
  r   r*  r:  r  r6   r_  r
  r
  r  _restore_telegram_topic_sessionr^
  rA   r
  rl
  enable_telegram_topic_moder)   r  r  rP  rg
  r  r}  #_telegram_topic_root_status_message)r_  rP  r$  r  auth_fncapabilitiesr2  r  r  r&  session_labels              r"   r  z#GatewayRunner._handle_topic_command'  s     ?h///63Ct3K3KTT 	544 $ 5t<<G 	GGwv ONNO G G G6FFFFFG %%''--// ::<<88811333 ::<<555==fEEE 	K# O 
 ==eTJJJJJJJJJ!BB6JJJJJJJJI&& 	 455>> ==fEE H??GGGGGGGGG.   ?@@EII==fEE H??GGGGGGGGG. 		B77FN++FN++#/#3#34H#I#I.:.>.>?^._._	 8      	B 	B 	BCDDDACAAAAAAAA	B  	=44V<<<<<<<<< 	*EE//!&"233 F      DtTTT   \!:!:!@bAA
! ,>>zJJEE  ! ! ! EEE! % ;);/ -/ /%/ / /\  77???sV   A$ $&BB8A(I! !
J+J	JJA L (L-,L-M4 4NN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.r>   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.r>   rp  )r  r  r8  z)Failed to list unlinked Telegram sessionsTrq  zPrevious unlinked sessions:rP	  r>   r&  r
  r  z- u    — `r  r  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.r>   z$To restore a previous session later:r
  r
  rI  )rR  (list_unlinked_telegram_sessions_for_userr)   r  r  r   r*  r:  rN  rA   r6   r  rO  )	r_  r  r(  r  sessionr  r&  r  ru  s	            r"   r
  z1GatewayRunner._telegram_topic_root_status_message[(  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  r>   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.rS  rP  r  z9That session is already linked to another Telegram topic.restored)r  r  r  rV  r  managed_modezalready linkedNrS   rq  rr  zSession restored: z

Last Hermes message:
)r  rR  resolve_session_idr6   r~  r)   rA   r  #is_telegram_session_linked_to_topicr  r  r  r  r  r7   r}  rV   get_messagesr   )r_  rP  r
  r  r  r
  linkedcurrent_bindingrV  r2  r&  last_assistantr[  r  s                 r"   r
  z-GatewayRunner._restore_telegram_topic_session(  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   |j         }| j                            |          }|j        }| j        sdS | j                            |          }|G	 | j                            ||j        r|j        j        nd|j	                   n# t          $ r Y nw xY w|                                                                }|r|	 | j                            |          }n# t          $ r}d| cY d}~S d}~ww xY w|sdS 	 | j                            ||          rd| dS d	S # t          $ r}d| cY d}~S d}~ww xY w| j                            |          }	|	r	d
| d|	 dS d
| dS )uB   Handle /title command — set or show the current session's title.r
  Nr  )r  r  r     ⚠️ uE   ⚠️ Title is empty after cleanup. Please use printable characters.u   ✏️ Session title set: **r	  zSession not found in database.u   📌 Session: `z`
Title: **z/`
No title set. Usage: `/title My Session Name`)r  r5  r  r  rR  r}  create_sessionr   r   r  r   r  r6   r[  r7   r\  )
r_  rP  r  r  r  existing_title	title_argr!   rb  r&  s
             r"   r  z#GatewayRunner._handle_title_command(  s     *@@HH"-
 	544 );;JGG! //)4:OR6?00"N 0    
     **,,2244	 	f% ,;;IFF		 % % %$}}$$$$$$% _^^%#55j)LL <G)GGGG;; % % %$}}$$$$$$% $66zBBE fJJJJJJJeeeeesN   5B 
BB C 
C4%C/)C4/C4< D 
D8)D3-D83D8c                 t  K   | j         sdS |j        }|                     |          }|                                                                }|s	 |j        r|j        j        nd}| j                             |d          }d |D             }|s	 dS dg}|dd         D ]M}	|	d         }
|	                    d	d
          dd         }|rd| dnd
}|	                    d|
 d|            N|	                    d           d
                    |          S # t          $ r*}t                              d|           d| cY d}~S d}~ww xY w| j                             |          }|sd| dS 	 | j                             |          }n3# t          $ r&}t                              d||           Y d}~nd}~ww xY w| j                            |          }|j        |k    rd| dS |                     |           | j                            ||          }|sdS |                     |           |                     |           | j                             |          p|}
| j                            |          }|rt3          d |D                       nd}|rd| d|dk    rdnd
 d nd
}d!|
 d| d"S )#u@   Handle /resume command — switch to a previously-named session.r
  Nrp  )r  r8  c                 <    g | ]}|                     d           |S )r&  r  )r  r  s     r"   rm  z8GatewayRunner._handle_resume_command.<locals>.<listcomp>(  s'    @@@w@!@@@r$   zNo named sessions found.
Use `/title My Session` to name your current session, then `/resume My Session` to return to it later.u   📋 **Named Sessions**
r&  r  r>   rE	  u    — _rn  u   • **r	  z 
Usage: `/resume <session name>`rI  z"Failed to list titled sessions: %szCould not list sessions: zNo session found matching '**z?**'.
Use `/resume` with no arguments to see available sessions.z0Failed to resolve resume continuation for %s: %su   📌 Already on session **r
  zFailed to switch session.c                 D    g | ]}|                     d           dk    |S rS   rp  r  r  s     r"   rm  z8GatewayRunner._handle_resume_command.<locals>.<listcomp>:)  ,    GGGqquuV}}/F/F/F/F/Fr$   r   r   messager   r  r  u   ↻ Resumed session **z. Conversation restored.)rR  r  r  r  r6   r   r   list_sessions_richrA   rN  rO  r   r*  r:  resolve_session_by_titleresolve_resume_session_idr5  r  r  r  r  rZ  r  r}  r  rK  )r_  rP  r  rV  rF   user_sourcer  titledr(  r  r&  r  preview_partrb  	target_idcurrent_entryra  rQ   	msg_countmsg_parts                       r"   r  z$GatewayRunner._handle_resume_command(  s      	544226::%%''--// 	777=Pfo33D+>>&b ?   A@X@@@ K 
 55 C CAgJEeeIr223B37G:A#I#6G#6#6#6#6rLLL!A%!A!A<!A!ABBBB@AAAyy''' 7 7 7A1EEE61666666667
 $==dCC	 	M M M M	[(BB9MMII 	[ 	[ 	[LLKYXYZZZZZZZZ	[ *@@HH#y0099999 	))+666 &55k9MM	 	/..33K@@@ 	  ---  229==E $44Y??LSZCGGGGGGHHHYZ	OX`K	KK)q..33bKKKK^`SSSSSSSs>   A D BD 
E"EEE2F 
F=F88F=c                 d  K   ddl }| j        sdS |j        }|                     |          }| j                            |          }| j                            |j                  }|sdS |                                	                                }ddl
m
} |                                }	|	                    d          }
|                                j        dd         }|
 d| }|r|}n=| j                            |j                  }|pd	}| j                            |          }|j        }	 | j                            ||j        r|j        j        nd
t)          | j        t,                    r0| j                            di           pi                     d          nd|           n7# t0          $ r*}t2                              d|           d| cY d}~S d}~ww xY w|D ]"}	 | j                            ||                    dd          |                    d          |                    d          p|                    d          |                    d          |                    d          |                    d          |                    d          |                    d          |                    d          |                    d          |                    d                     # t0          $ r Y  w xY w	 | j                            ||           n# t0          $ r Y nw xY w| j                            ||          }|sdS |                     |           |                     |           tA          d |D                       }d | d!| d"|d#k    rd$nd% d&| d'| d(S ))u  Handle /branch [name] — fork the current session into a new independent copy.

        Copies conversation history to a new session so the user can explore
        a different approach without losing the original.
        Inspired by Claude Code's /branch command.
        r   Nr
  u3   No conversation to branch — send a message first.r   z%Y%m%d_%H%M%SrM  rn  rl  r  r   rG   )r  r  r   parent_session_idz#Failed to create branch session: %szFailed to create branch: rS   rp  rr  	tool_namerF   r	  tool_call_idfinish_reasonrc  reasoning_contentreasoning_detailscodex_reasoning_itemscodex_message_items)r  rS   rr  r
  r	  r
  r
  rc  r
  r
  r
  r
  z*Branch created but failed to switch to it.c                 D    g | ]}|                     d           dk    |S r
  r  r  s     r"   rm  z8GatewayRunner._handle_branch_command.<locals>.<listcomp>)  r
  r$   u   ⑂ Branched to **z** (r
  r   r  r>   z copied)
Original: `z`
Branch: `z/`
Use `/resume` to switch back to the original.)!r	  rR  r  r  r5  r  r  r  r  r6   r   rJ   r  r	  r	  r}  get_next_title_in_lineager
  r   r   r1   r  rW   rA   r   r*  r  append_messager\  r  rZ  r  rK  )r_  rP  r	  r  rV  r
  rQ   branch_name_dtrJ   timestamp_str
short_uuidrL
  branch_titlecurrent_titlebaser
  rb  rX   ra  r
  s                        r"   r  z$GatewayRunner._handle_branch_command?)  sL      	 	544226:: *@@HH$44]5MNN 	IHH,,..4466 	-,,,,,ggii_55[[]]&rr*
)88J88  	L&LL ,>>}?WXXM ,HD+EEdKKL)4		3++)06Nv,,YMWX\XceiMjMjtt{w339r>>yIIIpt"3	 ,      	3 	3 	3LL>BBB2q22222222	3
  	 	C //-00GGI..!ggk22Ecggfoo"ww|44!$!8!8"%''/":":!ggk22&)gg.A&B&B&)gg.A&B&B*-''2I*J*J(+0E(F(F 0        	..~|LLLL 	 	 	D	 &55k>RR	 	@??33K@@@ 	  ---GGGGGGHH	= = == =+4>>CCr= =+= = '= = =	
sD   0A;F, ,
G 6GG G )DK88
LL
L& &
L32L3c           
        K   |j         }|                     |          }| j                            |          }|r	|t          u r_t          | dd          }t          | dd          }|r;|9|5  |                    |          }|r|d         }ddd           n# 1 swxY w Y   |r|t          urt          |dd          nd}|r|t          urt          |dd          nd}	|r|t          urt          |dd          nd}
|st          | dd          }	 | j                            |          }| j        	                    |j
                  pi }n# t          $ r i }Y nw xY w|p|                    d	          }|	p|                    d
          }	g }|rJ	 t          j        t          ||	|
           d{V }n# t          $ r d}Y nw xY w|rt          |d          }|r t!          |d          r|j        dk    rg }|                                }|rC|j        r<ddlm} |                    d ||                      |                    d           t          |dd          pd}t          |dd          pd}t          |dd          pd}t          |dd          pd}|                    d           |                    d|j         d           |                    d|d           |r|                    d|d           |r|                    d|d           |                    d|d           |                    d|j        d           |                    d|j                    	 dd lm}m}  ||j         |||||!          t          |dd          t          |dd          "          }|j        >|j        d#k    rd$nd}|                    d%| d&t=          |j                  d'           n |j        d(k    r|                    d)           n# t          $ r Y nw xY w|j        }|j         rU|j!        r tE          d*|j         |j!        z  d*z            nd}|                    d+|j         dd,|j!        dd-|d.d/           |j#        r|                    d0|j#                    |r*|                    d           |$                    |           d1%                    |          S | j                            |          }| j        &                    |j
                  }|rydd2l'm(} d3 |D             } ||          } d4d5tS          |           d6| dd7d8g}|r*|                    d           |$                    |           d1%                    |          S |rd1%                    |          S d9S ):a:  Handle /usage command -- show token usage for the current session.

        Checks both _running_agents (mid-turn) and _agent_cache (between turns)
        so that rate limits, cost estimates, and detailed token breakdowns are
        available whenever the user asks, not only while the agent is running.
        rJ  NrH  r   r   r   r   rR  billing_providerbilling_base_url)r   r   T)markdownsession_total_tokens)format_rate_limit_compactu   ⏱️ **Rate Limits:** r>   session_input_tokenssession_output_tokenssession_cache_read_tokenssession_cache_write_tokensu   📊 **Session Token Usage**zModel: `r  zInput tokens: r  zCache read tokens: zCache write tokens: zOutput tokens: zTotal: zAPI calls: )CanonicalUsageestimate_usage_cost)rw  rx  ry  rz  )r   r   ri  r  r  $z.4fincludedzCost: includedrj  r  z / r  r?  z%)zCompressions: rI  )r_  c                 j    g | ]0}|                     d           dv |                     d          .|1S )rS   ro  rr  r  r  s     r"   rm  z7GatewayRunner._handle_usage_command.<locals>.<listcomp>*  sB    fff!!%%--;P*P*PUVUZUZ[dUeUe*PA*P*P*Pr$   u   📊 **Session Info**z
Messages: zEstimated context: ~r  z;_(Detailed usage available after the first agent response)_z)No usage data available for this session.)*r  r  r?  rA   r  r%   r5  r  rR  r~  r  r   r9  r$  r   r   r  session_api_callsget_rate_limit_statehas_dataagent.rate_limit_trackerr
  rN  r   r
  agent.usage_pricingr
  r
  
amount_usdr*  r5   r  r  r  ra  compression_countr  rO  r  r#  r_  rK  )!r_  rP  r  rV  r   r  r  cachedr   r   r   _entry_for_billing	persistedaccount_linesaccount_snapshotr(  rl_stater
  rw  rx  
cache_readcache_writer
  r
  cost_resultr  r  pctr  rQ   r_  rA
  approxs!                                    r"   r  z#GatewayRunner._handle_usage_command)  s      226:: $((55 	*!888!$(;TBBKT>488F *v1  * *#ZZ44F * &q	* * * * * * * * * * * * * * * 8=oNeAeAe75*d333ko7<oNeAeAe75*d333ko5:muLc?c?c'%D111im 	EGD->>J%)%7%M%Mf%U%U" ,889K9VWW][]		   			D9==1C#D#DHD9==1C#D#DH $& 	\()0):'%#	* * * $ $ $ $ $ $    ( ( (#'   ( \ :;KVZ [ [ [ =	$WU$:;; =	$@WZ[@[@[E 1133H !H- !NNNNNN]8Q8QRZ8[8[]]^^^R    #5*@!DDIL#E+BAFFK!M (CQGGL1J!%)EqIINQKLL7888LL2EK222333LL:,:::;;; CA:AAABBB ECKCCCDDDLL<=<<<===LLA5#=AAABBBLL@u'>@@AAASSSSSSSS11K"N%1&3*4+6	   %UJ==$UJ==
 
 
 )5$/$6+$E$ESS2FLL!V&!V!V59O3P3P!V!V!VWWWW ':55LL!1222    *C% kUXUgnc#s58JJSPQQQmni)?iiicFXiii^aiiiijjj$ GEc.CEEFFF ,R   ]+++99U### *@@HH$44]5MNN 	$KKKKKKffwfffD33D99F'(SYY((8v8888M	E  ,R   ]+++99U### 	,99]+++::sI   , BBB;E EE#F6 6GG9B,P& &
P32P3c                   	
K   |                                                                 }t          j        dd|          }d
d|r	|                                }d}|t          |          k     r||         dk    rT|dz   t          |          k     r>	 t          ||dz                      
n# t          $ r d||dz             cY S w xY w|d	z  }nm||         d
k    r'|dz   t          |          k     r||dz            |d	z  }n:||                                         rt          ||                   
|dz  }n|dz  }|t          |          k     	 ddl	m
	 ddlm t          j                    }	
fd}|                    d|           d{V S # t           $ r,}t"                              d|d           d| cY d}~S d}~ww xY w)z>Handle /insights command -- show usage insights and analytics.z'[\u2012\u2013\u2014\u2015](days|source)z--\1r  Nr   z--daysr   zInvalid --days value: ri  z--sourcer  )InsightsEnginec                                   }  |           }|                               }|                    |          }|                                  |S )N)daysr  )generateformat_gatewayr  )dbenginereportr  r
  r  r
  r  s       r"   _run_insightsz=GatewayRunner._handle_insights_command.<locals>._run_insightsL*  sW    Y[['++d6BB..v66


r$   zInsights command error: %sTrq  zError generating insights: )r  r6   r'   r+   r^  rK  r4   r7   isdigitrS  r  agent.insightsr
  r9  r  r  r   r*  r  )r_  rP  r$  rQ  rR  r  r
  rb  r
  r  r
  r  s           @@@@r"   r  z&GatewayRunner._handle_insights_command(*  s]     %%''--// v@'4PP  	JJLLEAc%jj..8x''AECJJ,>,>G"5Q<00% G G GFa!eFFFFFGFAA1X++AE

0B0B"1q5\FFAA1X%%'' uQx==DFAAFA c%jj.. 	5......555555+--D        --dMBBBBBBBBB 	5 	5 	5LL5q4LHHH444444444	5s1   B/ /C
	C
AF 
G!!GGGc                    K   j         }                     |                                           }t          |t                    r|                    d          nd}d}t          |t                    r#t          |                    dd                    }|s                                d{V S dt          dt          t                   f fd}d} 
                    d	d
||           d{V S )u  Handle /reload-mcp — reconnect MCP servers and rebuild the cached agent.

        Reloading MCP tools invalidates the provider prompt cache for the
        active session (tool schemas are baked into the system prompt).  The
        next message re-sends full input tokens, which is expensive on
        long-context or high-reasoning models.

        To surface that cost, the command routes through the slash-confirm
        primitive: users get an Approve Once / Always Approve / Cancel
        prompt before the reload actually runs.  "Always Approve" persists
        ``approvals.mcp_reload_confirm: false`` so the prompt is silenced
        for subsequent reloads in any session.

        Users can also skip the confirm by flipping the config key directly.
        	approvalsNTmcp_reload_confirmchoicer   c                 6  K   | dk    r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
S |S )Nr  u0   🟡 /reload-mcp cancelled. MCP tools unchanged.r7  r   )save_config_valuezapprovals.mcp_reload_confirmFz7User opted out of /reload-mcp confirmation (session=%s)z.Failed to persist mcp_reload_confirm=false: %su   

ℹ️ Future `/reload-mcp` calls will run without confirmation. Re-enable via `approvals.mcp_reload_confirm: true` in config.yaml.)r  r  r*  r9  r   r+  _execute_mcp_reload)r  r  r2  r  rP  r_  rV  s       r"   _on_confirmz=GatewayRunner._handle_reload_mcp_command.<locals>._on_confirm{*  s     !!II!!Z555555%%&DeLLLKKQ#    ! Z Z ZNN#SUXYYYYYYYYZ  33E::::::::F!! Y Y Y
 Ms   -A 
A0A++A0u  ⚠️ **Confirm /reload-mcp**

Reloading MCP servers rebuilds the tool set for this session and **invalidates the provider prompt cache** — the next message will re-send full input tokens.  On long-context or high-reasoning models this can be expensive.

Choose:
• **Approve Once** — reload now
• **Always Approve** — reload now and silence this prompt permanently
• **Cancel** — leave MCP tools unchanged

_Text fallback: reply `/approve`, `/always`, or `/cancel`._rk  z/reload-mcp)rP  r#  r&  r[  handler)r  r  _read_user_configr1   rW   rA   r3   r  r)   r
   _request_slash_confirm)	r_  rP  r  r  r   confirm_requiredr  prompt_messagerV  s	   ``      @r"   r  z(GatewayRunner._handle_reload_mcp_commandY*  sS       226:: ,,..4>{D4Q4Q[KOOK000W[	i&& 	O#IMM2F$M$MNN 	911%888888888	c 	hsm 	 	 	 	 	 	 	 	2	J 	 00 " 1 
 
 
 
 
 
 
 
 	
r$   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  }dg}|r8|
                    dd                    t          |                                |
r8|
                    dd                    t          |
                                |r8|
                    dd                    t          |                                |	s|
                    d	           n6|
                    d
t          |           dt          |	           d           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&          $ r*}t(                              d|           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_serversrG  Nu   🔄 **MCP Servers Reloaded**
u   ♻️ Reconnected: r  u   ➕ Added: u   ➖ Removed: zNo MCP servers connected.u   
🔧 z tool(s) available from z
 server(s)zAdded servers: zRemoved servers: zReconnected servers: z MCP tool(s) now availablezNo MCP tools availablez. r>   rp  z,[IMPORTANT: MCP servers have been reloaded. zD. The tool list for this conversation has been updated accordingly.]rr  rI  zMCP reload failed: %su   ❌ MCP reload failed: )r9  r  tools.mcp_toolr  r  r  rG  r]  r  r  rN  rO  r  rK  r5  r  r  r  r  r   r*  r+  )r_  rP  r  r  r  r  rG  old_servers	new_toolsconnected_serversaddedrM  reconnectedr(  change_partstool_summarychange_detail
reload_msgr  rb  s                       r"   r  z!GatewayRunner._execute_mcp_reload*  s      '))>	1````````````  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67E VTDIIf[>Q>Q4R4RTTUUU GE499VE]]+C+CEEFFF KITYYvg-G-GIIJJJ$ s89999qs9~~qqsSdOeOeqqqrrr
 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### 	1 	1 	1NN2A6660Q00000000	1s   L2 "AL2 AL2 A=L2 "C:L2 C

L2 C
G>L2 ?L L2 
LL2 LL2 2
M&<M!M&!M&c           
         K   t          j                    }	 ddlm} |                    d|           d{V }|                    dg           }|                    dg           }|                    dd          }t          | j                                                  D ]}t          |dd          }	t          |	          s#	  |	            }
t          j        |
          r|
 d{V  K# t          $ r5}t                              dt          |d	|          |           Y d}~d}~ww xY wd
g}|sE|sC|                    d           |                    d| d           d                    |          S dt$          dt&          fd}|r8|                    d           |D ] }|                     ||                     !|r8|                    d           |D ] }|                     ||                     !|                    d| d           dg}|rM|                    d           |                    d           |D ] }|                     ||                     !|rM|                    d           |                    d           |D ] }|                     ||                     !|                    d           |                    d           d                    |          }|                     |j                  }t-          | d          si | _        |r
|| j        |<   d                    |          S # t          $ r*}t                              d|           d| cY d}~S d}~ww xY w)u  Handle /reload-skills — rescan skills dir, queue a note for next turn.

        Skills don't need to be in the system prompt for the model to use
        them (they're invoked via ``/skill-name``, ``skills_list``, or
        ``skill_view`` at runtime), so this does NOT clear the prompt cache
        — prefix caching stays intact.

        If any skills were added or removed, a one-shot note is queued on
        ``self._pending_skills_reload_notes[session_key]``. The gateway
        prepends it to the NEXT user message in this session (see the
        consumer at ~L11025 in ``_run_agent_turn``), then clears it. Nothing
        is written to the session transcript out-of-band, so message
        alternation is preserved.
        r   )reload_skillsNr  rM  totalrefresh_skill_groupz)Adapter %s refresh_skill_group raised: %srF   u   🔄 **Skills Reloaded**
zNo new skills detected.u   
📚 z skill(s) availablerI  itemr   c                 x    |                      dd          }|                      dd          }|rd| d| nd| S )NrF   r>   r  z    - r   r  )r  nmr  s      r"   	_fmt_linez>GatewayRunner._handle_reload_skills_command.<locals>._fmt_line#+  sO    XXfb))xxr2204G,,,d,,,-2--Gr$   u   ➕ **Added Skills:**u   ➖ **Removed Skills:**z[USER INITIATED SKILLS RELOAD:r>   zAdded Skills:zRemoved Skills:z,Use skills_list to see the updated catalog.]_pending_skills_reload_noteszSkills reload failed: %su   ❌ Skills reload failed: )r9  r  r  r  r  rA   r.  r   r  r%   rd
  inspectisawaitabler   r*  r+  rN  rO  rW   r)   r  r  r  r"  )r_  rP  r  r  r  r  rM  r  rY  refreshmayber2  r(  r!  r  sectionsrE
  rV  rb  s                      r"   r  z+GatewayRunner._handle_reload_skills_command*  s=      '))P	4:::::://mDDDDDDDDFJJw++EjjB//GJJw**E   4 4 6 677  !'+@$GG(( #GIIE*511 $#    NNC993        22E ( (6777AuAAABBByy'''H H H H H H
  24555! 2 2DLL41111 26777# 2 2DLL41111LL=5===>>> 99H 5###000! 5 5DOOIIdOO4444 5### 1222# 5 5DOOIIdOO4444OOBOOJKKK99X&&D66u|DDK4!?@@ 7461 FAE1+>99U### 	4 	4 	4NN5q999333333333	4sP   B-M	 &C,+M	 ,
D+6+D&!M	 &D++AM	 8GM	 	
M=M82M=8M=r#  c                  K   ddl m} |j        }|                     |          }t	          | j                   }	|                    ||	||           | 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   r1  FN)r  r&  r[  rV  r:  r  r  Tz*send_slash_confirm failed for %s on %s: %s)r|  r2  r  r  nextrP  registerr   rA   r   r5	  send_slash_confirmr  r%   r   r*  r:  )r_  rP  r#  r&  r[  r  r  r  rV  r:  rY  r  used_buttonsbutton_resultr2  s                  r"   r	  z$GatewayRunner._request_slash_confirma+  s{     * 	>=====226::T899;
 	##KWgNNN-##FO4433F;;&-&@&@"N# +)% 'A ' ' ! ! ! ! ! ! ! (W]Iu%M%M (#'L   @V_c         	4s   ;C 
C="C88C=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   r  )r   r  r1   rW   r   )r_  r  r   s      r"   r  zGatewayRunner._read_user_config+  sa    	555555+--C$S$//733R7 	 	 	III	s   (+ ::c                 4    t          |dd          }|dS d|iS )z@Build the metadata dict platforms need for thread-aware replies.r  N)r%   )r_  r  r  s      r"   r5	  z)GatewayRunner._thread_metadata_for_source+  s)    FK66	4Y''r$   c                   K   |j         }|                     |          }ddlm}m}  ||          s4|| j        v r)| j                            |           t          d          S dS |                                	                                
                                                                }d|v }d |D             }t          d |D                       rd}	d	}
n"t          d
 |D                       rd}	d}
nd}	d}
 |||	|          }|sdS | j                            |j                  }|r|                    |j                   |dk    rd| dnd}t$                              d||
           d|dk    rdnd d|
 | dS )u  Handle /approve command — unblock waiting agent thread(s).

        The agent thread(s) are blocked inside tools/approval.py waiting for
        the user to respond.  This handler signals the event so the agent
        resumes and the terminal_tool executes the command inline — the same
        flow as the CLI's synchronous input() approval.

        Supports multiple concurrent approvals (parallel subagents,
        execute_code).  ``/approve`` resolves the oldest pending command;
        ``/approve all`` resolves every pending command at once.

        Usage:
            /approve              — approve oldest pending command once
            /approve all          — approve ALL pending commands at once
            /approve session      — approve oldest + remember for session
            /approve all session  — approve all + remember for session
            /approve always       — approve oldest + remember permanently
            /approve all always   — approve all + remember permanently
        r   resolve_gateway_approvalr3  zgateway.approval_expiredzNo pending command to approve.r  c                     g | ]
}|d k    |S )r  rw   r  rz  s     r"   rm  z9GatewayRunner._handle_approve_command.<locals>.<listcomp>+  s    3331U

Q


r$   c              3      K   | ]}|d v V  	dS ))r7  	permanentpermanentlyNrw   r4  s     r"   r  z8GatewayRunner._handle_approve_command.<locals>.<genexpr>+  s(      NNqq::NNNNNNr$   r7  z (pattern approved permanently)c              3      K   | ]}|d v V  	dS ))r
  sesNrw   r4  s     r"   r  z8GatewayRunner._handle_approve_command.<locals>.<genexpr>+  s(      <<Q((<<<<<<r$   r
  z$ (pattern approved for this session)r6  r>   resolve_allr   r  
 commands)z4User approved %d dangerous command(s) via /approve%su   ✅ Commandr  z	 approvedz. The agent is resuming...)r  r  r~  r2  r3  rK  r  r   r  r6   r_  r^  r  r   rA   r   resume_typing_for_chatr  r*  r9  )r_  rP  r  rV  r2  r3  r$  r;  	remainingr  	scope_msgrO  rC  	count_msgs                 r"   r  z%GatewayRunner._handle_approve_command+  s     ( 226::	
 	
 	
 	
 	
 	
 	
 	
 %$[11 	4d555'++K888344433 %%''--//5577==??tm33333	NNINNNNN 	F9II<<)<<<<< 	F>IIFI((f+VVV 	433 =$$V_55 	<++FN;;;.3aii*****R	JES\]]]pEAIISS2pp	p9ppppr$   c                 8  K   |j         }|                     |          }ddlm}m}  ||          s'|| j        v r| j                            |           dS dS |                                                                	                                }d|v } ||d|          }|sdS | j
                            |j                  }	|	r|	                    |j                   |dk    rd	| d
nd}
t                              d|           d|dk    rdnd d|
 dS )u  Handle /deny command — reject pending dangerous command(s).

        Signals blocked agent thread(s) with a 'deny' result so they receive
        a definitive BLOCKED message, same as the CLI deny flow.

        ``/deny`` denies the oldest; ``/deny all`` denies everything.
        r   r1  u(   ❌ Command denied (approval was stale).zNo pending command to deny.r  r)  r:  r   r  r<  r>   z-User denied %d dangerous command(s) via /denyu   ❌ Commandr  z deniedr   )r  r  r~  r2  r3  rK  r  r  r6   r_  r   rA   r   r=  r  r*  r9  )r_  rP  r  rV  r2  r3  r$  r;  rO  rC  r@  s              r"   r  z"GatewayRunner._handle_deny_command+  sv      226::	
 	
 	
 	
 	
 	
 	
 	
 %$[11 	1d555'++K888AA00%%''--//5577tm((f+VVV 	100 =$$V_55 	<++FN;;;.3aii*****R	CUKKKJEAIISS2JJiJJJJr$   c                    	
K   ddl }ddlmm	m
mmm  |j                    }	
fd}|	                    d|           d{V S )u6  Handle /debug — upload debug report (summary only) and return paste URLs.

        Gateway uploads ONLY the summary report (system info + log tails),
        NOT full log files, to protect conversation privacy.  Users who need
        full log uploads should use ``hermes debug share`` from the CLI.
        r   N)_capture_dumpcollect_debug_reportupload_to_pastebin_schedule_auto_delete_GATEWAY_PRIVACY_NOTICE!_best_effort_sweep_expired_pastesc                  x    	              
            }  d|           }i }	  |          |d<   n# t           $ r}d| cY d }~S d }~ww xY w t          |                                                     dddg}t          d |D                       }|                                D ]$\  }}|                    d|d	| d
|            %|                    d           |                    d           |                    d           |                    d           d                    |          S )Nr  )	log_lines	dump_textReportu#   ✗ Failed to upload debug report: r>   z**Debug report uploaded:**c              3   4   K   | ]}t          |          V  d S r  r  )r  r  s     r"   r  zSGatewayRunner._handle_debug_command.<locals>._collect_and_upload.<locals>.<genexpr>B,  s(      33c!ff333333r$   r  <z`  u'   ⏱ Pastes will auto-delete in 6 hours.z<For full log uploads, use `hermes debug share` from the CLI.z3Share these links with the Hermes team for support.rI  )r   r.  r  r  r  rN  rO  )rK  r
  urlsr2  r(  label_widthr  rS  rG  rH  rC  rF  rD  rE  s           r"   _collect_and_uploadz@GatewayRunner._handle_debug_command.<locals>._collect_and_upload3,  s   --///%I))C9MMMFDC!3!3F!;!;X C C CBSBBBBBBBBC "!$t{{}}"5"5666,b2NPRSE33d33333K"jjll A A
s??????#??@@@@LLLLBCCCLLWXXXLLNOOO99U###s   5 
AA	A	A)
r9  hermes_cli.debugrC  rD  rE  rF  rG  rH  r  r  )r_  rP  r9  r  rQ  rG  rH  rC  rF  rD  rE  s        @@@@@@r"   r  z#GatewayRunner._handle_debug_command",  s       		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 (w'))	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$2 ))$0CDDDDDDDDDr$   c           
      *  K   ddl }ddl}ddl}ddlm} ddlm}m} |j        j        }| j	        }	||	vr>	 ddl
m}
 |
                    |j                  }|r|j        sdS n# t          $ r Y dS w xY w |            rd |d           S t!          t"                    j        j                                        }|d	z  }|                                sd
S t+                      }|s	 dS t,          dz  }t,          dz  }t,          dz  }|                     |j                  }|j        j        j        |j        j        |j        j        | |j                                                    d}|j        j        r|j        j        |d<   |                    d          }|                     |j        |                     |                     |           |!                    d           d"                    d |D                       }d| dtG          j$        tK          |                     dtG          j$        tK          |                     }	 |&                    d          }|r(|'                    |dd|g|j(        |j(        d           n&|'                    dd|g|j(        |j(        d           nH# t          $ r;}|!                    d           |!                    d           d| cY d}~S d}~ww xY w| )                                 dS )us  Handle /update command — update Hermes Agent to the latest version.

        Spawns ``hermes update`` in a detached session (via ``setsid``) so it
        survives the gateway restart that ``hermes update`` may trigger. Marker
        files are written so either the current gateway process or the next one
        can notify the user when the update finishes.
        r   Nr   )
is_managedformat_managed_messager~  u^   ✗ /update is only available from messaging platforms. Run `hermes update` from the terminal.u   ✗ zupdate Hermes Agentr~  u+   ✗ Not a git repository — cannot update.u   ✗ Could not locate the `hermes` command. Hermes is running, but the update command could not find the executable on PATH or via the current Python interpreter. Try running `hermes update` manually in your terminal.r  .update_output.txt.update_exit_code)r   r  r  rV  r2   r  r/  Tr  r]  c              3   >   K   | ]}t          j        |          V  d S r  r  r  s     r"   r  z7GatewayRunner._handle_update_command.<locals>.<genexpr>,  s,      !K!K%+d"3"3!K!K!K!K!K!Kr$   zPYTHONUNBUFFERED=1 z update --gateway > z* 2>&1; status=$?; printf '%s' "$status" > r  r  -cr   u   ✗ Failed to start update: u8   ⚕ Starting Hermes update… I'll stream progress here.)*ru  r  r$  r   r   rT  rU  r  r   _UPDATE_ALLOWED_PLATFORMSr  r  rA   r   allow_update_commandr   r   r   r  r   rb   r  rv   r  r  r  rJ   r  r  r{  r  r  r9   r  rO  rr  r  r)   r  r&  r'  r  )r_  rP  ru  r  r$  r   rT  rU  r   _allowedr  r@  project_rootgit_dirr(  pending_pathr	  exit_code_pathrV  pending_tmp_pendinghermes_cmd_str
update_cmdr,  rb  s                            r"   r  z$GatewayRunner._handle_update_commandN,  s      	%%%%%%HHHHHHHH <(18##xGGGGGG)--hn== |E$> |{{| x x xwwwx :<< 	JI001FGGIIIH~~,3;;=='~~ 	A@@(**
 	I  $&<<"%99%(;;225<@@-3|+|+&%1133
 
 <! 	:#(<#9GK #//77

7 3 3444\***... !K!K
!K!K!KKKW. W W+c+..//W W49KN@S@S4T4TW W 	
	6h//J   z:%-%-&*	 !       T:.%-%-&*	 !     	6 	6 	64000!!T!2225!55555555	6
 	00222IIs0   )A! !
A/.A/A%J7 7
K<0K71K<7K<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)	r%   r!  r9  r9  _watch_update_progressrf  r-  r*  r:  )r_  existing_tasks     r"   r  z1GatewayRunner._schedule_update_notification_watch,  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                 L   !"#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
          }|rd
|ind#|r4 r2t          |          }| j
                            |          |
s| d  }
 n# t          $ r Y w xY wr sQt                              d           |                                s|                                r"                                |	k     r|                                r|                                  d{V  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d, !"#fd}"                                |	k     rL|                                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                            |
          sx	 t          j        |                                          }|                    dd          }|                    dd          }|r |             d{V  d}t9          t;                    d d          V	                      |||
#!           d{V  d}n2# t          $ r%}t                              d"|           Y d}~nd}~ww xY w|s.|rd#| d$nd}                     d%| | d&#           d{V  d| j        |
<   t                              d'|
|dd(                    n># t
          j         t&          f$ r%}t                              d)|           Y d}~nd}~ww xY wt          j        |           d{V  "                                |	k     L|                                st                              d*|           |                    d            |             d{V  	                      d+#           d{V  n# t          $ r Y nw xY w|||||fD ]}|                    d           t           dz                      d           | j                            |
d           dS dS )-a  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  rV  rW  r.  Nr   r  rV  r  rh  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]r>   )r'   r+   r%  s    r"   _strip_ansiz9GatewayRunner._watch_update_progress.<locals>._strip_ansi,  s    62B===r$   r   r>   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.r>   N  c                 *    g | ]}||z            S rw   rw   )r  rR  clean	max_chunks     r"   rm  zOGatewayRunner._watch_update_progress.<locals>._flush_buffer.<locals>.<listcomp>-  s&    VVVeAa)mO,VVVr$   r   z```

```r  zUpdate stream send failed: %s)r6   rM   r  rK  r  r   r*  r:  )chunkschunkrb  ru  rv  rq  rY  bufferr  last_stream_timer  r  s      @@r"   _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ry   u   ✅ Hermes update finished.r  u(   ❌ Hermes update failed (exit code {}).z&Update finished (exit=%s), notified %sz$Update final notification failed: %sTr  r-  rk  rG   Fsend_update_prompt)r  rk  rG   rV  r  z%Button-based update prompt failed: %sz (default: r  u"   ⚕ **Update needs your input:**

zG

Reply `/approve` (yes) or `/deny` (no), or type your answer directly.z!Forwarded update prompt to %s: %srO  z Failed to read update prompt: %sz$Update watcher timed out after %.0fsu-   ❌ Hermes update timed out after 30 minutes.r8  )!rv   r9  r  rM   rb   ru  rv  ro  rA   r  r   r   r*  r+  r  r  r  r)   rK  r  r6   r4   r  formatr9  r  rM  r  r%   r  r}  r:  r  )$r_  rl  rm  r   r_  claimed_pathr	  r`  r  r  rV  ra   ra  r  r  r   
bytes_sentr|  rr  exit_code_rawr  rb  r  prompt_dataprompt_textrG   sent_buttonsbtn_errdefault_hintrq  rY  rz  r  r{  r  r  s$                                @@@@@@@r"   rg  z$GatewayRunner._watch_update_progress,  s
      $&<<#&DD"%99%(;;"%::'))99;;( !<0 	 	D{{}} "j)9)9::G#*;;z#:#:L%kk)44G")++m"<"<K 'K 8 8I;DNY77$H# F F#+L#9#9"&-"3"3H"="=* F-9*E*EG*E*EKE    D$  	g 	NNlmmm&&(( 3L,?,?,A,A 3tyy{{U]G]G]!((** 88:::::::::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"),, '!A#.!A0<!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 -.........K iikkH$$P $$&& 	?NNA7KKK%%e,,,-//!!!!!!!llC% #          
    "L+$k3 * *D))))..66$6GGG'++K>>>>>!	? 	?s   	B8E
EE#AM' '
M43M4B)P2 2
Q!<QQ!AT 
T%$T%B[  "Y [ 
Y2Y-([ -Y22A![ \*\

\^9 9
__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
          }
|                                st          
                    d           d}|}|                    |           	 |rZ|                    d           |                    d           |                    d           |                    d           dS dS |                                                                pd}t          |          }d}|                                r|                                }t          |          }| j                            |          }|r|	r|
rd
|
ind}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  rV  rW  FTr  r   r  r  z2Update notification deferred: update still runningry   r>   Nz\x1b\[[0-9;]*mrs  r0  iTr   u!   ✅ Hermes update finished.

```
rw  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)rv   rb   r9   r  r  ru  rv  ro  rA   r*  r9  r6   r4   r  r   r'   r+   rK  r  r   r+  )r_  r_  r  r	  r`  cleanupactive_pending_pathra  r  r  r  r  r  r  r   rY  r  rX   rb  s                      r"   r  z'GatewayRunner._send_update_notification-  s      $&<<#&DD"%99%(;;""$$ 	\-@-@-B-B 	5*@	7""$$ $ ((6666( $ $ $'..00 $#l  7#**d*;;;##t#444""d"333%%%66666	7 7o$ $$ "((** h  7#**d*;;;##t#444""d"333%%%66666	7 7e j!7!7!9!9::G";;z22Lkk),,GK00I!((** PQQQ&2#$$\222P  7#**d*;;;##t#444""d"333%%%66666	7 7M +4466<<>>E#MM**I F!!## 1$..00  --Hm''11G 7 7@JK33d 12v>>DDFF ~6{{T))!&!7 A~~RVRRRP6PPP A~~H}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 tsi   O /B O D#O O DO ;B,O D:O  Q 
O0O+&Q +O00Q AR+c           
      h  K   t           dz  }|                                sdS 	 t          j        |                                          }|                    d          }|                    d          }|                    d          }|r|s	 |                    d           dS t          |          }| j                            |          }|s4t          
                    d|           	 |                    d           dS | j        j                            |          }|;|j        s4t                              d	|           	 |                    d           dS |rd|ind}	|                    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.ru   Nr   r  r  Tr  z6Restart notification skipped: %s adapter not connectedzJRestart notification suppressed: %s has gateway_restart_notification=falseu;   ♻ Gateway restarted successfully. Your session continues.r  r  Fz3Restart notification to %s:%s was not delivered: %sr  r  z"Sent restart notification to %s:%szRestart notification failed: %s)rv   rb   ru  rv  ro  rA   r  r  r   r*  r:  r  r!  r  r9  r  r)   r%   r+  r   )r_  notify_pathr  r  r  r  r   rY  r  r  r  rb  s               r"   r  z(GatewayRunner._send_restart_notification-  s5     "%;;!!## 	47	0:k335566D88J//Lhhy))G--I w ` $/////]  --Hm''11G L    N $/////K  ;044X>>L'0Y'`    > $/////; 4=FY//$H"<<GM! (        F !gfi&F&F%&O&OI FG-JKK	    $///// KK4  
 |$$c'lli4YC	NNNUYY
 $////	  	 	 	NN<a@@@444$/////		 $////sF   A)I &AI AI !A4I .AI 
JJ5J JJ J1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        r	d|j        ind}
|
r1|                    t          |j                  ||
           d{V }n.|                    t          |j                  |           d{V }|Kt          |dd          du r8t                              d	|j        |j        t          |d
d                     t|                    |	           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: %sr  r  z/Sent home-channel startup notification to %s:%s)r]  r   r  r  r  r  r!  rA   r  r*  r9  r   r)   r  r  r%   r+  r  r   )r_  r  	deliveredskippedr[  r   rY  r  r  r  r  r  r2  s                r"   r  z6GatewayRunner._send_home_channel_startup_notifications+.  s      :=	'#%%G!%!4!4!6!6 ,	 ,	Hg;//99D t| ;044X>>L'0Y'mN   nc$,&7&7PTP^9hT^9L9L9LdhiF  Fi$7$7<@NTK88PT 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   0B>G-0;G--
H)7'H$$H)r  c           
      d   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                  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_varsr>   )r   r  rK	  r  r  r  rV  )gateway.session_contextr  r  r   r   r  rK	  r  r)   r  r  rV  )r_  r  r  s      r"   r  zGatewayRunner._set_session_envj.  s     	=<<<<<^,2N*n.4"7>~7OWc'.2333UW3:>3IQC.///r7>~7OWc'.2333UW+
 
 
 	
r$   rs  c                 (    ddl m}  ||           dS )z>Restore session context variables to their pre-handler values.r   )clear_session_varsN)r  r  )r_  rs  r  s      r"   r  z GatewayRunner._clear_session_env~.  s+    >>>>>>6"""""r$   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)r9  r  r   r  run)r_  funcr$  r  r  s        r"   r	  z+GatewayRunner._run_in_executor_with_context.  sP      '))nn)T)$EEEEEEEEEEEr$   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_providerr  u;   image_routing: decision failed, falling back to text — %sNr   )
agent.image_routingr  r  r  r  r   r  r   r*  r:  )	r_  r  r  r  r  r   r   r   r2  s	            r"   r  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)r	  user_promptNr  analysisr>   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: rG  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_toolsr  agent.memory_managerr  r*  r:  ru  rv  rA   rN  r   r  rO  )r_  r  r+  r  r  analysis_promptenriched_partsra   r	  r  r  rb  r  s                r"   r  z)GatewayRunner._enrich_message_with_vision.  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                 v  K   t          | j        dd          s+d}|                                 r|dz  }|dz  }|r| d| S |S ddlm} g }|D ])}	 t
                              d	|           t          j        ||           d
{V }|d         r"|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
}~#d
}~ww xY w|r>d                    |          }d}|r|                                |k    r|S |r| d| S |S |S )a  
        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:
            The enriched message string with transcriptions prepended.
        stt_enabledTzI[The user sent voice message(s), but transcription is disabled in config.z{ You have a skill called hermes-agent-setup that can help users configure Hermes features including voice, tools, and more.rG  r  r   )transcribe_audiozTranscribing user voice: %sNr  rt	  z8[The user sent a voice message~ Here's what they said: "z"]r  r  r  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.zC[The user sent a voice message but I had trouble transcribing it~ (z)]zTranscription error: %sze[The user sent a voice message but something went wrong when I tried to listen to it~ Let them know!]z.(The user sent a message with no text content))r%   r  r  tools.transcription_toolsr  r*  r:  r9  r$  rN  rA   rL  r   r  rO  r6   )r_  r  r,  disabled_noter  r  ra   r  rt	  r  _no_stt_noterb  r  _placeholders                 r"   r  z0GatewayRunner._enrich_message_with_transcription.  s       t{M488 
	!gM$$&& X S M 9'88Y888  >>>>>> (	 (	D':DAAA&01A4HHHHHHHH)$ !'!5J"))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         		[[00F LL Y__..,>> 2 11i111Ms   CD11
E6;0E11E6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
  rV  r>   r  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  )gateway.sessionr
  r)   rA   r6   r5  r  r  r%   r  r   r*  r:  r  r  r_  r  r   r  r  r  r  r7   r+  )r_  r  r
  rV  derived_platformderived_chat_typederived_chat_idr@  r2  cached_sourcer  ri   r  r  r   r  s                   r"   _build_process_event_sourcez)GatewayRunner._build_process_event_source:/  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r/  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||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  Nr   TrE  uA   Watch pattern notification — injecting for %s chat=%s thread=%sr  )r  r*  r+  rA   r  r   r   r)   r   r  r  r  rL  r9  r  r  rM  r   r  )
r_  r/  r  r  ri   rY  r  rz  synth_eventrb  s
             r"   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&(-	  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   5AD 
E EEr  c           
      \	  K   ddl m} |d         }|d         }|                    dd          }|                    dd          }|                    dd          }|                    d	d          }|                    d
d          }	|                    d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  |                    |          }|nt          |j
                  }||k    }|}|j        r7ddl m} |ra|                    |          sKddlm} |j
        r ||j
        dd                   nd}d| d|j         d|j         d| d	}|                     ||||||	|
d          }|st                              d|           nWd}| j                                        D ]\  }}||j        k    r|} n|r|j        r	 t-          |t.          j        |d          }t                              d|||j        |j                   |                    |           d{V  n2# t8          $ 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# t8          $ 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#| d*| d}d}| j                                        D ]\  }}|j        |k    r|} n|r\|rZ	 |rd	|ind}|                    |||&           d{V  n2# t8          $ r%}t                              d'|           Y d}~nd}~ww xY wIt                              d+|           dS ),u  
        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   r  r  check_intervalrV  r>   r   r  r  r  r  notify_on_completeFzCProcess watcher started: %s (every %ss, notify=%s, agent_notify=%s)r  TNz"Process watcher ended (silent): %s)
strip_ansii0r  z completed (exit code z).
Command: z	
Output:
rG  )r  rV  r   r  r  r  r  zHDropping completion notification with no routing metadata for process %srE  uU   Process %s finished — injecting agent notification for session %s chat=%s thread=%sz Agent notify injection error: %s)r  r  r  )r   Ni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) r4  r  rA   r  r*  r:  r9  r  exitedrK  output_bufferis_completion_consumedtools.ansi_stripr  r  r#  r  r+  r   r  r   r  r  r  rL  r9  r  rM  r   r  r   r  )r_  r  r  r  r  rV  ri   r  r  r  r  agent_notifynotify_moder
  last_output_lencurrent_output_lenhas_new_output	_pr_checkr  r  r/  r  rY  r  rz  r  rb  r  
new_outputr   	send_metas                                  r"   r  z"GatewayRunner._run_process_watcher/  s      	<;;;;;\*
+,kk-44J33++i,,KKR00	++i,,KKR00	{{#7??>>@@Z (K	G 	G 	G %mH---------*..z::?gn?	
 LL=zJJJFl	F-)))))))))&**:66G!$W%:!;!;//AN0O~ aF QPPPPP 0	(H(H(T(T 0;;;;;;HOH]e::g&;EFF&CDDDceD,* , ,&-&7, ,$+O, , %), , ,  "==&0'2$1#*%.#*%.? ?  F " f&   "G $ 3 3 5 5 " "1//&'G!E 0  P6> PP*6%/-8-='-)-	+ + +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Wl	F\ 	0*=====sI   4A K 
LK??L<&N# #
O-OO6&Q 
R'RR))r   r  )r   
max_tokens)ra  r  )ra  	threshold)ra  target_ratio)ra  protect_last_n)r   r	  _CACHE_BUSTING_CONFIG_KEYSc                 \   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|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_generation)r1   rW   r  rA   tools.registryr  r%   r   )clsr  rT  r   sectionr  section_valr  s           r"   _extract_cache_busting_configz+GatewayRunner._extract_cache_busting_configT0  s     !'T::Bkk: 	/ 	/LGS'''**K+t,, /*5//#*>*>w&&&&''*.w&&&&''	4///////6xPT/U/UC+,, 	4 	4 	4/3C+,,,	4
s   <B B)(B)r/  rw  ephemeral_prompt
cache_keysc           
         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|	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.
        r   Nr   r>   r   r   r"  T)	sort_keysrG   r{	  )
hashlibru  r)   rA   sha256encode	hexdigestr  r  r  )r   r/  rw  r  r  r  _j_api_key_api_key_fingerprint_cache_keys_sortedblobs              r"   _agent_config_signaturez%GatewayRunner._agent_config_signaturer0  sB   * 	#""""""" w{{9b117R88PX`w~~hoo.?.?@@JJLLL^`#Z%52$<$<$>$>??xx$J++J++J++,<D'(((" !&B"   
 
  ~~dkkmm,,6688"==r$   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  rA   )r_  rV  r   r  r  r  vals          r"   r  z+GatewayRunner._apply_session_model_override0  s{     044[AA 	).((We,,B 	* 	*C,,s##C&)s#n$$r$   agent_modelc                 p    | j                             |          }|duo|                    d          |k    S )zGReturn True if *agent_model* matches an active /model session override.Nr   r  )r_  rV  r  r  s       r"   _is_intentional_model_switchz*GatewayRunner._is_intentional_model_switch0  s9    044[AAt#LW(=(=(LLr$   rM  c                   |sdS ||                      ||          sdS | 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.
        FNrC  T)r  r?  r  r  r  rC  )r_  rV  rM  s      r"   r  z*GatewayRunner._release_running_agent_state0  s    :  	5%d.J.J/
 /
% 5  d333##K6664(( 	5!!+t444tr$   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 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.Nr"  rK  rM  r   )clear_sessionz:Failed to clear approval state for session boundary %s: %s)	r%   r1   rW   r  r~  r  r   r*  r:  )r_  rV  pending_skills_reload_notespending_approvalsupdate_prompt_pending_clear_approval_sessionrb  s          r"   rZ  z4GatewayRunner._clear_session_boundary_security_state0  s~    	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	OOOOOOO 	 	 	FF		##K00000 	 	 	LLL        	s*   :C 
CCC   
D*DD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   rD  Nr   )__dict__rA   rD  r4   )r_  rV  generationsnext_generations       r"   r  z+GatewayRunner._begin_session_run_generation1  sj      	1m''(ABBK+6D(kook1==>>B#2K r$   rA  c                n    |                      |          }|rt                              d|||           |S )z7Invalidate any in-flight run token for ``session_key``.u-   Invalidated run generation for %s → %d (%s))r  r*  r9  )r_  rV  r&  r  s       r"   r  z0GatewayRunner._invalidate_session_run_generation1  sG    77DD
 	KK?	   r$   r  c                     |sdS | j                             d          pi }t          |                    |d                    t          |          k    S )zEReturn True when ``generation`` is still current for ``session_key``.TrD  r   )r  rA   r4   )r_  rV  r  r  s       r"   r  z%GatewayRunner._is_session_run_current&1  sQ     	4m''(ABBHb;??;2233s:FFr$   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<	  )r%   rA   setattrr4   r   )r_  rY  rV  r  interrupt_events        r"   r  z*GatewayRunner._bind_adapter_run_generation-1  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_staterE  rF  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.NrA  interrupt_session_activityrX  )r?  rA   r  r  r  r   r   r  r  r  rX  r@  r  r  )r_  rV  r  rE  rF  r  r  rY  s           r"   r  z*GatewayRunner._interrupt_and_clear_session=1  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:::::	; 	;r$   c                     t          | dd          }|r8|5  | j                            |d           ddd           dS # 1 swxY w Y   dS dS )zBRemove a cached agent for a session (called on /new, /model, etc).rJ  N)r%   rH  r  )r_  rV  rG  s      r"   r  z!GatewayRunner._evict_cached_agentV1  s    1488 	9 9 9!%%k48889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9	9 	9s   ?AAinterrupt_depthc                 ^    |dk    rt          j                     | _        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)N)rM   _last_activity_ts_last_activity_desc_api_call_count)r   r  s     r"   _init_cached_agent_for_turnz)GatewayRunner._init_cached_agent_for_turn]1  s3     a&*ikkE#(DE% !r$   c                     |dS 	 t          |d          r|                                 dS |                     |           dS # t          $ r Y dS w xY w)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_clients)r  r  r  r   )r_  r   s     r"   _release_evicted_agent_softz)GatewayRunner._release_evicted_agent_softp1  s|     =F	u/00 5%%''''' --e44444 	 	 	DD	s   $A 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.
        rH  NrG  c                 B    h | ]}||t           ut          |          S r  r  rP	  r4  s     r"   r  z9GatewayRunner._enforce_agent_cache_cap.<locals>.<setcomp>1  8     
 
 
}*A!A!A qEE!A!A!Ar$   r?  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-r  r  r$  daemonrF   )r%   r  r  r  rK  _AGENT_CACHE_MAX_SIZEr.  r  rA   r1   r  rP	  rN  r  r*  r+  r9  rG  Threadr  r  )r_  r  running_idsexcess
evict_planordered_keysr  r@  r   rn  remaining_over_caps              r"   _enforce_agent_cache_capz&GatewayRunner._enforce_agent_cache_cap1  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	  
 %'''	 	r$   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.
        rH  NrJ  r   c                 B    h | ]}||t           ut          |          S r  r  r4  s     r"   r  z:GatewayRunner._sweep_idle_cached_agents.<locals>.<setcomp>1  r  r$   r?  r  z3Agent cache idle-TTL evict: session=%s (idle=%.0fs)Tzagent-cache-idle-r  r	  )r%   rM   r  r.  r  r1   r  rP	  _AGENT_CACHE_IDLE_TTL_SECSrN  r  r*  r9  rG  r  r  r  rK  )r_  r  rG  rJ   to_evictr  r  r@  r   last_activityrn  s              r"   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_URLr>   r   r  	proxy_urlN)r?   r)  r6   r  r  rA   )r_  rS  r   s      r"   _get_proxy_urlzGatewayRunner._get_proxy_url2  s     i+R006688 	#::c??""$$wwy!!'R,,["==CCEE 	#::c??"tr$   r  r  c	                 l   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          |          }|j        r
d|j        i}nd}|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!        |$|%          }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d/                    d0|+j+         d1|,dd2          g dg dcddd          d{V  cddd          d{V  |r|.                                 |&rS	 tK          j/        |&d3*           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                    d4pd5pd           dg dg tm          |          |d!d6c cddd          d{V  cddd          d{V  |r|.                                 |&rS	 tK          j/        |&d3*           d{V  S # tJ          j0        tJ          j1        f$ r |&2                                 Y S w xY wS |.7                    d7d89          }/|-|/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         
                    d@i           }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          >                    dA||5           |'szdB|5 g dg dcY d}5~5|r|.                                 |&rS	 tK          j/        |&d3*           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/        |&d3*           d{V  n# tJ          j0        tJ          j1        f$ r |&2                                 Y nuw xY wnp# |r|.                                 |&rS	 tK          j/        |&d3*           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                    dCpd5pd           dg dg tm          |          |d!d6S tF          5                    dD||pdddE         |6tm          |'                     |'pdFd|ddG|'dgd;g tm          |          ||duot          |'          d6S )HaT  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ru  r  r  r|  uH   ⚠️ Proxy URL not configured (GATEWAY_PROXY_URL or gateway.proxy_url)GATEWAY_PROXY_KEYr>   r   c                  <     sdS                                 S r(  r  rM  r_  rV  s   r"   _run_still_currentz>GatewayRunner._run_agent_via_proxy.<locals>._run_still_currentB2  )    %[%t//^LLLr$   rU   rr  rS   rr  ro  rp  zContent-Typezapplication/jsonzBearer AuthorizationzX-Hermes-Session-Idzhermes-agentT)r   r  streamNr  	streamingStreamingConfigr  r  r  GatewayStreamConsumerStreamConsumerConfigSUPPORTS_MESSAGE_EDITINGFfresh_final_after_secondsr  edit_intervalbuffer_thresholdr  buffer_onlyr/  )rY  r  r  r  z+Proxy: could not set up stream consumer: %sr  r;  )r  	sock_readr  z/v1/chat/completions)ru  headersr  zProxy error (%d) from %s: %sr  u   ⚠️ Proxy error (z): r  r  uK   Discarding stale proxy stream for %s — generation %d is no longer currentr  )ru  r  r  r|  r  r  response_previewedr   r9   )re  rI  r   zdata: rM  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 charsro  z(No response from remote agent)rq  )?aiohttpr  r  re   r  r?   r)  r6   r3   rN  rA   r%   r#  r*  r  r   r  r  r  r  	transportr  gateway.stream_consumerr,  r-  r   r  r  r  r  r5   r1  r2  r  r   r*  r:  r9  r9  r  send_typingrM   postr*  r   r+  finishr  r  r`  r  rr  iter_anyr9  rK  r  r^  rL  ru  rv  on_deltar  r  )7r_  r[  r  rQ   r  r  rV  rM  r  _AioClientSessionr  r  	proxy_keyr$  api_messagesrX   rS   rr  r5  body_stream_consumer_scfgr*  r  r  r  _plat_streaming_streaming_enabledr 
  r,  r-  rC  _adapter_supports_edit_effective_cursor_buffer_only_fresh_final_secs_consumer_cfg_sc_errstream_taskfull_response_start_timeoutr
  resp
error_textrz  ry  r   ru  r  objr7  r8  rb  _elapseds7   `     ``                                               r"   _run_agent_via_proxyz"GatewayRunner._run_agent_via_proxy2  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&& 	  	$:EvGW9X# "	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% % %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   $$8CI> >
J-J((J-<"L 
L,+L,[ $"[A)Z)0[[ -Q

/Q<;Q< Z)Z!AZ)$[6[ !T>>/U0/U04BZ)=A<Y:9Z):Z		Z)Z	
Z)[)
Z3	3[6Z3	7[:[ 
[[ [[ ` ^7'^^` <]/^
^` ^` 5_ /``a5"`?>a5?/a1.a50a11a5r   _interrupt_depthrL  c                  5   
fghijklmnopqrstuvwxyz{|}~K                                     r#                     |	           d{V S ddlmf ddldt
          f fdvt                      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g dk    oj        gj        k    j        gj        k    o#t5          |                    d          d          r                                nddgdgdgt           |d                    mmr j                            j                  nd}|$t;          |          j        t>          j        u rdmd}g ldgdhddt@          dt@          dt@          dt          fhv|f	dj        gj!        k    r
j"        p|	}nj"        }|rd |indtj        gj#        k    rj"        r|	r|	ndulmtuv| fd!}dg|dgdgdgtI          j%                    q j&        nd"t&          d#tN          ddfnqvfd${ j                            j                  wj(        yj        gj#        k    rj"        r|	r||	d%zn|rd |indzdt@          d&t@          ddflmqvwyzfd'xfgnqvwxyz{|
}~ f d(}d}rtI          j)         |                      }d}fd)}tI          j)         |                      }| fd*}tI          j)         |                      } tI          j*                    pp| fd+}!tI          j)         |!                      }"tW          d,d-          }#|#dk    r|#nditY          j,                    silmsz| fd.}$tI          j)         |$                      }%	 tW          d/d0          }&|&dk    r|&nd}'tW          d1d2          }(|(dk    r|(nd})d}*tI          j-         .                    |                    }+d},d3}-|'$d}.	 tI          j/        |+h|-4           d{V \  }/}0|/r|+0                                }.np1                                s҉rЉ j                            j                  }1|d         }2|1r|2rte          |1d5          r|13                              r|1j4                                      }3|3r|3j5        nd}4tl          7                    d6|"8                                rd7nd8           |29                    |4           p:                                 n:d}.	 tI          j/        |+h|-4           d{V \  }/}0|/r|+0                                }.n|d         }5d9}6|5rLte          |5d:          r<	 |5;                                }7|7                    d;d9          }6n# t(          $ r Y nw xY w|*s|)|6|)k    rd}* j                            j                  }8|8rt'          |)d<z            pd=}9t'          |'|)z
  d<z            pd=}:	 |8<                    j(        d>|9 d?|: d@zA           d{V  n2# t(          $ r%};tl          =                    dB|;           Y d};~;nd};~;ww xY w|6|'k    rd},np1                                s҉rЉ j                            j                  }1|d         }2|1r|2rte          |1d5          r|13                              r|1j4                                      }3|3r|3j5        nd}4tl          7                    d6|"8                                rd7nd8           |29                    |4           p:                                 7|,r|d         }<i }=|<r6te          |<d:          r&	 |<;                                }=n# t(          $ r Y nw xY w|=                    dCdD          }>|=                    d;d          }?|=                    dE          }@|=                    dFd          }A|=                    dGd          }Btl          >                    dH|?|'|>|A|B|@pdI           |<r*te          |<dJ          r|<9                    t~                     t'          |'d<z            pd=}CdK|C dLg}D|@r$|D@                    dM|@ dN|?dOdP|A dQ|B dR	           n#|D@                    dS|> dT|?dOdU|A dQ|B dV	           |D@                    dW           dXA                    |D          d         rd                             dYg           ng |Ad         pg dddZ}.|d         }Ed         }F|Fr|F                    d[          nd}G|E[te          |Ed\          rK|GsIt                      }H|EjC        |Hk    r0 D                    |EjC                  s E                               d         }I j                            j                  }Jd}Kd}L|Ir|Jr։rt          |J          }K G                    |J|K          }K|I                    d]          r]|Ks[|I                    d^          rF|I                    d^          }Mt          |M          rtl          7                    d_pd`|M           n>|M}Ln;|Kr9|Kj5        pt          |K          }Ltl          =                    da|Lddb                    |Ir@|Ls>|Ks<|I                    dc          }N|Nr%|N}Ltl          =                    dd|Lddb                    |Lr|LJ                                K                    dQ          r|LJ                                L                    dd=          }O|Or"|Od         d=d         M                                nde}P|PrB	 ddflNmO}Q  |Q|P          rtl          7                    dg|P           d}Kd}Ln# t(          $ r Y nw xY w jP        r8|Ks|Lr4tl          7                    dhpd` Q                                           d}Kd}L|Ks|L	rrtl          =                    di|Lddb                    |Jr:te          |Jdj          r*r(|JjR        v r|JjR                 S                                  jT        k    rtl          U                    dk            j                            j                  }J|Jr|Krt          |Jj4        |K           n(|Jr&te          |Jdl          r|JW                    |L           d         p|.dm	 |r|X                                 |"X                                 |%X                                 |rr	 tI          jY        |d34           d{V  nT# tH          jZ        tH          j[        f$ r6 |X                                 	 | d{V  n# tH          j[        $ r Y nw xY wY nw xY w| X                                 r \                    n            jP        r ]                    do           ||"| |%fD ]#}R|Rr	 |R d{V  # tH          j[        $ r Y w xY w$S |I                    d]          }S|Ssd         }T|Tr|r	 tI          jY        |d34           d{V  n# tH          jZ        tH          j[        f$ r6 |X                                 	 | d{V  n# tH          j[        $ r Y nw xY wY n1t(          $ r%}Utl          =                    dp|U           Y d}U~Und}U~Uww xY wt          |I                    dq                    }Vt          |Trt          |Tdrd          p|V          }W|I                    dsde          }X|Xrv|Wst	 tl          7                    dtpd`           |J<                    j(        |XzA           d{V  nQ# t(          $ r%}Utl          U                    du|U           Y d}U~Un'd}U~Uww xY w|Xrtl          7                    dvpd`           t          t;          |J          dwd          _|J_                    x          }Yt          |Y          r8	  |Y            }Zt          jb        |Z          r|Z d{V  n# t(          $ r Y nyw xY wnt|Jrrte          |Jdy          rb|Jjc        d                    d          }Yt          |Y          r8	  |Y            }Zt          jb        |Z          r|Z d{V  n# t(          $ r Y nw xY w|I                    dY          }[}\|L}]d}^d}_|Kt          |Kdzd          p}\ e                    |K          rZ f                              sDtl          7                    d{pd`           |I|r|X                                 |"X                                 |%X                                 |rr	 tI          jY        |d34           d{V  nT# tH          jZ        tH          j[        f$ r6 |X                                 	 | d{V  n# tH          j[        $ r Y nw xY wY nw xY w| X                                 r \                    n            jP        r ]                    do           ||"| |%fD ]#}R|Rr	 |R d{V  # tH          j[        $ r Y w xY w$S  g                    |K|\|[|           d{V }]|]'|I|r|X                                 |"X                                 |%X                                 |rr	 tI          jY        |d34           d{V  nT# tH          jZ        tH          j[        f$ r6 |X                                 	 | d{V  n# tH          j[        $ r Y nw xY wY nw xY w| X                                 r \                    n            jP        r ]                    do           ||"| |%fD ]#}R|Rr	 |R d{V  # tH          j[        $ r Y w xY w$S t          |Kd}d          }^t          |Kd~d          }_ j                            j                  }`|`r4	 |`h                    j(        zA           d{V  n# t(          $ r Y nw xY w i                    |]|[|\d=z   |^|_
  
         d{V 	 |r|X                                 |"X                                 |%X                                 |rr	 tI          jY        |d34           d{V  nT# tH          jZ        tH          j[        f$ r6 |X                                 	 | d{V  n# tH          j[        $ r Y nw xY wY nw xY w| X                                 r \                    n            jP        r ]                    do           ||"| |%fD ]#}R|Rr	 |R d{V  # tH          j[        $ r Y w xY w$S 	 |r|X                                 |"X                                 |%X                                 |rr	 tI          jY        |d34           d{V  nT# tH          jZ        tH          j[        f$ r6 |X                                 	 | d{V  n# tH          j[        $ r Y nw xY wY nw xY w| X                                 r \                    n            jP        r ]                    do           ||"| |%fD ]#}R|Rr	 |R d{V  # tH          j[        $ r Y w xY w$n*# |r|X                                 |"X                                 |%X                                 |rr	 tI          jY        |d34           d{V  nT# tH          jZ        tH          j[        f$ r6 |X                                 	 | d{V  n# tH          j[        $ r Y nw xY wY nw xY w| X                                 r \                    n            jP        r ]                    do           ||"| |%fD ]#}R|Rr	 |R d{V  # tH          j[        $ r Y w xY w$w xY wd         }Tt          |.t                    r|.                    d[          s|.                    ds          pde}a|a p|adk    }bt          |Tot          |Tdrd                    }ct          |.                    dq                    }V|bs(|cs|Vr$tl          7                    dpd`|c|V           d|.d<   mr|lrrt          |.t                    r|.                    d[          ste          |d          rtO          l          oj(        k|jtI          j%                    rdjkorfd}d	 |j                    |dx           n2# t(          $ r%}etl          =                    d|e           Y d}e~end}e~eww xY w|.S )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.
        )r[  r  rQ   r  r  rV  rM  r  Nr   rk  r   c                  <     sdS                                 S r(  r"  r#  s   r"   r$  z4GatewayRunner._run_agent.<locals>._run_still_current]3  r%  r$   r	  r   r	  r   r  )set_tool_preview_max_lentool_preview_lengthr(
  HERMES_TOOL_PROGRESS_MODEr!  tool_progress_overridesr  r  r  interim_assistant_messagesTrj  cleanup_progressFr   
event_typer
  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k    r|rddlm}  |            }t%          j        |d	t(                    }|dk    r#t+          |          |k    r|d|dz
           dz   }| d| dt-          |                                           d| }n|r| d| d| d}n| d| d}	                    |           dS |rIddlm}  |            }|dk    r|nd}t+          |          |k    r|d|dz
           dz   }| d| d| d}n| 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   r'
  Frj  Tr   z(tool-progress onboarding hint failed: %s)ztool.startedis_interruptedrH  )get_tool_emojiu   ⚙️rT  )get_tool_preview_max_len)ensure_asciirG   rg  rM  r]  (z)
z: "rk  rE	  r   	__dedup__)rA   r  rc  r  r  rd  r  r   r   putrv   r   r*  r:  r%   agent.displayrf  rg  ru  r  r)   rK  r.  r  )ra  r
  r  r$  kwargsr  rc  r  r  rd  r   gate_on	_hint_err_agent_for_interruptrf  emojirg  _plargs_strrX   _cap_LONG_TOOL_THRESHOLD_Sr$  agent_holderlast_progress_msg	last_toollong_tool_hint_firedprogress_modeprogress_queuerepeat_counts                        r"   progress_callbackz3GatewayRunner._run_agent.<locals>.progress_callback3  sj   ! ););)=)=  ---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 	)) 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'''
  0BBBBBB..00!Aggss2w<<$&&%itaxi058G;;;;;;;/////
 '***Q1$ ""Kl1o#FGGG#&a LOs#####s*   B!C 
C0C++C0<D 
D+*D+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}	 	              sP                                s:	                                  n# t          $ r Y nw xY w                                :d S                                 }	 rd         nd }|,t          |dd          rt          j        d           d {V  n# t          $ r Y nw xY wt          |t                    rBt          |          dk    r/|d         dk    r#|\  }}	}
|r|	 d	|
d
z    d|d<   |r|d         n|	}n[t          |t                    r/t          |          d
k    r|d         dk    rd }g }d d<   dd<   W|}|                    |           t!          j                    }|||z
  z
  }|dk    rt          j        |           d {V               sd S |r |d                    |          }|                     j        ||           d {V }|j        st          |dd          pd                                }d|v sd|v r t,                              d| j                   d}|                     j        |           d {V }rIt          |dd          r8t          |dd           r'                    t5          |j                             n|r:d                    |          }|                     j        |           d {V }n$|                     j        |           d {V }|j        r7|j        r0|j        }r'                    t5          |j                             t!          j                    }t          j        d           d {V               r"|                     j                   d {V  n# j        $ r t          j        d           d {V  Y nt          j        $ r                                 s.	                                 }t          |t                    r6t          |          dk    r#|d         dk    r|\  }}	}
|r|	 d	|
d
z    d|d<   nt          |t                    r~t          |          d
k    rk|d         dk    r_|rN|rL|rJd                    |          }	 |                     j        ||           d {V  n# t          $ r Y nw xY wd }g }d d<   dd<   n|                    |           n# t          $ r Y nw xY w                                .|rN|rL|rJd                    |          }	 |                     j        ||           d {V  n# t          $ r Y nw xY wY d S t          $ r?}t,                              d|           t          j        d
           d {V  Y d }~nd }~ww xY w)NTr  g      ?r   re  Frg  rj  u    (×r   r  rj  	__reset__rI  )r  r  rr  r  r>   floodzretry afterz1[%s] Progress edits disabled due to flood controlr  r  r  g333333?r  zProgress message error: %s) r   rA   r   r  edit_messager  r  r  r   r%   r9  r  r1   r  rK  rN  rM   r  rO  r  r  r_  r*  r9  rF   r  r)   r  r<  Emptyr`  r  ) rY  progress_linesprogress_msg_idcan_edit_last_edit_ts_PROGRESS_EDIT_INTERVALrD   rp  rn  base_msgrO  rX   _now
_remaining	full_textr  r  _flood_result_pending_textrb  _cleanup_msg_ids_cleanup_progress_progress_metadata_progress_reply_tor$  rv  rw  r{  r6  r|  r_  r  s                        r"   send_progress_messagesz8GatewayRunner._run_agent.<locals>.send_progress_messages@4  s&	     ! m''88G 
 G}})-@-MMM(..00 &113333$    )..00 
 N"OHM&)#p+o+--// "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  >++D!8D=<P!QJ!A~~ &mJ777777777 --//  4PO$?$(IIn$=$=	'.';';$*N'6$- (< ( ( " " " " " "
  &~ W$+FGR$@$@$FB#M#M#O#OD&$-42G2G !'$W$+L!" !" !" (-H29,,(.(+););	 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 ;#c((a--CPQFVaLaLa58 28U#1 !W<D9V9V%RS)9V9V9VN2$6!+C!7!7 ;CHHMMcRSfXcNcNc $, 	!- 	!-? 	!-48IIn4M4MM%-.5.B.B4:N7F4A /C /* /* )* )* )* )* )* )* )* )*
 ,5 %- %- %-(,%-26137; 1! 423Q . 5 5c : : :( " " "!E"7 -2244 "<   	!N 	! 	!$(IIn$=$=	!")"6"6(.+:(1 #7 # #        
  ) ! ! ! D!FF  + + +LL!=qAAA!-**************+]p+s   A5 5
BB'Q C Q 
C(%Q 'C((Q Q 9E Q 
EQ EBQ <AQ 
Q G:Q $Y,?$Y,$B.V,#U76V,7
VV,V'V,+Y,,
V96Y,8V993Y,-#XY,
XY,XY,$	Y,-5Y''Y,r  
prev_toolsc                                 sd S 	 g }|pg D ]d}t          |t                    r+|                    |                    d          pd           B|                    t	          |                     et          j                            d	j        r	j        j	        nd	j
        | ||d                     d S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)NrF   r>   z
agent:step)r   r  r  r  
tool_namesr|  zagent:step hook error: %s)r1   rW   rN  rA   r)   r9  r
  r  r   r   r  r   r*  r:  )
r  r  _names_tr  
_hooks_ref_loop_for_stepr$  r  r  s
        r"   _step_callback_syncz5GatewayRunner._run_agent.<locals>._step_callback_sync5  s9   %%'' > %'%+ / /B!"d++ /bffVnn&:;;;;c"gg....0OOL=C_$TFO$9$9RT#)>&0%.&,!+3 3   #
 
 
 
 
  > > >8"=========>s   B3C 
C4C//C4)r  r!  r[  c                 "   	r
             sd S 	 t          j        	                    
|                    }rdfd}|                    |           d S d S # t          $ r'}t
                              d| |           Y d }~d S d }~ww xY w)Nr  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  r   r%   rN  r)   )r}
  rk  midr  s      r"   _track_status_idzQGatewayRunner._run_agent.<locals>._status_callback_sync.<locals>._track_status_idL5  s    #"%**,,CC( # # #"FF#%c<>>"3	599 >c >,33CHH=====> > > >s    
&&zstatus_callback error (%s): %sr8  )r9  r
  r  r:  r   r*  r:  )ra  r[  _futr  r  r  r  r  r$  _status_adapter_status_chat_id_status_thread_metadatas        r"   _status_callback_syncz7GatewayRunner._run_agent.<locals>._status_callback_sync?5  s    " *<*<*>*> O7#(('!8 )  
 #  % 	=> > > > > > **+;<<<<<	= 	=  O O O=z2NNNNNNNNNOs   AA 
B'B		Bc            
      "   abcdefg pdt           j        d<   t          t          j        dd                    } j        ij        k    rdnj        j        }upd}tp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 fd }	t)          t)          dd           dd           }
|
ddlm}  |            }
 }|d          }||
j        o
|
j        dk    nt3          |          }|}y}|}|s|r	 ddlm}m} j                            j                  }|rt)          |dd          }|st=          d          |
j        }d}j        ij         k    rd}d}j        ij!        k    r tE          t)          |
dd          pd          nd} ||
j#        |
j$        |||          } ||j%        |q||fdnd           f|rdtL          dd fmffd}	fd<   n2# t          $ r%}t                              d|           Y d }~nd }~ww xY wdd dtL          d!t2          dd flmnpqffd"}'                    z||          }(                    |d#         |d$         w|)                              %          }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-                    |k           t                              d*           d d d            n# 1 swxY w Y   |z hdd#|d#         i|d$         i d+| d,dd-dd.wd/vd0|pd d1j.        pd d2|d3j        d4|                    d4          d5|                    d6          d7|                    d8          d9|                    d:          d;|                    d<          d=|                    d>d          d?|                    d@          dAdB|dCj/        dDj0        dEj%        dFj1        dGj2        dHj3        dIdJj4        dKj5        }|r7|5|5  ||f|<   6                                 d d d            n# 1 swxY w Y   t                              dL|           r{nd |_7        jj8        rrnd |_9        |	|_:        |r|nd |_;        o|_<        ||_=        j        |_>        |                    d4          pi |_?        t          jA                    dg bt          jB                    cdMtL          dd flmnpqfdNedbcdefdO}!dMtL          dd fbcdemnfdP}"|"|_C        nrQrOt)          t          n          dQd           nE                    |!R           nt)          ndSd           }#|#|!|#<   |sd<   tU          |dT          r|jF        nd d<   g }$xD ]}%|%                    dU          }&|&s|&dVv r|&dWk    r&dX|%v }'dY|%v }(|&dZk    })|'s|(s|)r4d[ |%G                                D             }*|$H                    |*           n|%                    d\          }+|+rt|%                    d]          r|%                    d^d_          },d`|, da|+ }+|&|+db}-|&dck    r!ddD ]}.|%                    |.          }/|/r|/|-|.<   |$H                    |-           t                      }0|$D ]}1|1                    dU          dev r|1                    d\d          }2df|2v rit          jK        dg|2          D ]S}3|3L                    d(                                          M                    dh          }4|4r|0N                    |4           TddilOmP}5mQ}6mR}7mS}8 djt          dd falnpqfdk}9t)          dli           }:r|:U                    d           nd };|;r|;dz   zz   zt                      }<t          t          x          |<m          }=d }>r3	 jY        jZ                                      }>n# t          $ r d }>Y nw xY wt3          |>d uot)          |>dnd          o|=          }?t3          |$o |$do                             dU          dZk    o|=          }@|?r/t)          |>dpd           pdq}A|Adqk    rdrn	|Adsk    rdtndu}Bdv|B dwzz   zn|@rdxzz   zt)          dyd           }:|:r&r$|:v r |:U                    d           }C|Cr|Cdz   zz   zpda |7a          }D |5a|9           	 [                              }E|Er	 ddzl\m]}F  |Fz|E          \  }G}H|Hr)t          ^                    d{t          |H          |H           t          d| |GD                       r|G}Inz}In6# t          $ r'}Jt          ^                    d}|J           z}IY d }J~Jn
d }J~Jww xY wz}I|a                    |I|$~          }K |8a            |6|D           n#  |8a            |6|D           w xY w|K~d<   ffb                                 |K                    d          }Ld}Md}Nd}Od}Psd         }Q|Qr`tU          |Qd          rPt)          |Qjc        dd          }Mt)          |Qdd          }Nt)          |Qdd          }Ot)          |Qjc        dd          pd}P|Qrt)          |Qd#d           nd }R|Ls|K                    d          rd|Kd          nd}Si d|Sd|K                    dg           d|K                    dd          d|K                    dd          d|K                    dd          d|K                    d          d|K                    dd          d|K                    d          d|K                    d          d|K                    dd          dTd         pg dt          |$          d|Md|Nd|Od#|Rd|PS df|Lvr<g }Td}U|K                    dg           D ]}%|%                    dU          dev r|%                    d\d          }+df|+v rvt          jK        dg|+          D ]Z}V|VL                    d(                                          M                    dh          }W|Wr|W|0vr|TH                    df|W            [d|+v rd}U|Trvt                      }Xg }Y|TD ]0}Z|Z|Xvr*|XN                    |Z           |YH                    |Z           1|Ur|Yd                    dd           |Ldz   de                    |Y          z   }Lsd         }d}[|rrtU          |dA          rt|jf        k    rid}[t          g                    d|jf                   jY        jZ                                      }-|-r%|jf        |-_f        jY        h                                 |rt)          |dA          ng|[rdnt          |$          }\|Lrቀj4        r	 ddlimj}] ~d         r~d                             dg           ng }^t)          |dd           }_|_|rSt)          |d#d           t)          |d	d           t)          |dd           t)          |dd           t)          |dd           dnd d}`k                              r
gfd|`d<    |]j4        gz|L|^fi |` n# t          $ r Y nw xY wi d|Ld|K                    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 dTd         pg d|\d|Md|Nd|Od#|Rd|PdAgd|K                    dd          iS )Nr>   HERMES_SESSION_KEYr   rS  r  r  r  z3run_agent resolved: model=%s provider=%s session=%sr   u'   ⚠️ Provider authentication failed: r   r  rw  r  r(  r)  r  r+  r.  Tz(skip streaming for non-editable platformFr/  r  r0  c                  .                          d          S )N)r  )rk  )r{  s   r"   r  z<GatewayRunner._run_agent.<locals>.run_sync.<locals>.<lambda>5  s    ););N)K)K r$   )rY  r  r  r  on_new_messager   r   c                 J                 r                     |            d S d S r  )r@  )r   r$  rE  s    r"   _stream_delta_cbzDGatewayRunner._run_agent.<locals>.run_sync.<locals>._stream_delta_cb5  s;    #5#5#7#7 !D$4$=$=d$C$C$C$C$C!D !Dr$   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	          j                            |                      d S # t          $ r&}t          	                    d|           Y d }~d S d }~ww xY w)Nr>   r  z$interim_assistant_callback error: %s)
on_segment_breakon_commentaryr)   r6   r9  r
  r  r   r*  r:  )	r   r  r  r  r$  r  r  r  rE  s	      r"   _interim_assistant_cbzIGatewayRunner._run_agent.<locals>.run_sync.<locals>._interim_assistant_cb5  s   ))++ F#/' =(99;;;;(66t<<<F# ? #djb//BWBWBYBY F
M4',,+ %< -  
 '     ! M M MLL!GLLLLLLLLLMs   (+B 
CC  Cr   r/  )r  rJ  rH  r   rG  z#Reusing cached agent for session %sr  ru  r	  rw  r	  ephemeral_system_promptprefill_messagesrz  r  r  r	  r	  r	  r  r	  r	  r	  r  r	  r	  r	  r	  r  r   r  r  r  rK	  r  r  gateway_session_keyr  r6  z)Created new agent for session %s (sig=%s)r[  c                     r
             sd S 	 t          j                            |                      d S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)Nr  z$background_review_callback error: %s)r9  r
  r  r   r*  r:  )r[  r  r  r$  r  r  r  s     r"   _deliver_bg_review_messagezNGatewayRunner._run_agent.<locals>.run_sync.<locals>._deliver_bg_review_messageM6  s    & .@.@.B.B F
M4',,+#%< -  
 '     ! M M MLL!GLLLLLLLLLMs   +> 
A.A))A.c                                                        5  t                    }                                  d d d            n# 1 swxY w Y   | D ]} |           d S r  )r]  r.  r  )ra  queued_bg_review_pending_bg_review_pending_lock_bg_review_releaser  s     r"   _release_bg_review_messageszOGatewayRunner._run_agent.<locals>.run_sync.<locals>._release_bg_review_messages\6  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_setrN  )r[  r  r  r  r  r$  r  s    r"   _bg_review_sendzCGatewayRunner._run_agent.<locals>.run_sync.<locals>._bg_review_sende6  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  r|  rS   )rT   rU   r	  r
  toolc                 &    i | ]\  }}|d k    ||S )r2   rw   )r  r  rp  s      r"   r  z>GatewayRunner._run_agent.<locals>.run_sync.<locals>.<dictcomp>6  s(     R R R$!QkAQAQAAQAQAQr$   rr  mirrormirror_sourcezanother sessionz[Delivered from r  rr  rq  )rc  r
  r
  )r  r	  zMEDIA:zMEDIA:(\S+)z",})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	          j        	                    
||                                        d	          }|j        rdS t          
                    d
|j                   n2# t          $ r%}t          
                    d|           Y d}~nd}~ww xY wt          |          dk    r|dd         dz   n|}d| d| d}	 t	          j        	                    
|                                        d	           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#  r>   r  zdangerous commandsend_exec_approvalN)r  r#  rV  r  r  r  r  zLButton-based approval failed (send returned error), falling back to text: %sz6Button-based approval failed, falling back to text: %sr  rM  u4   ⚠️ **Dangerous command requires approval:**
```
z
```
Reason: z

Reply `/approve` to execute, `/approve session` to approve this pattern for the session, `/approve always` to approve permanently, or `/deny` to cancel.r  z#Failed to send approval request: %s)pause_typing_for_chatrA   r%   r  r9  r
  r  r  r  r*  r+  r  r   rK  r  )r  r*  r  _approval_resultr  cmd_previewrX   _approval_session_keyr  r  r  r  s          r"   _approval_notify_synczIGatewayRunner._run_agent.<locals>.run_sync.<locals>._approval_notify_sync6  s:     55oFFF#''	266$((8KLL
 4002FMMY+2+K+>>(7(+,A,0)@ ?   +	, 	, !&&,, ) ,3 #"Fj,2    %   TVX        47s88c>>c$3$i%//sh'h h#h h h 
L4',,+%< -  
 '  fRf(((((  L L LLL!FKKKKKKKKKLs7   "AC , C 
C<C77C<+?E, ,
F6FFrS  )rK   rB  rj  rC  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  r	  Nr  rl  s     r"   r  z=GatewayRunner._run_agent.<locals>.run_sync.<locals>.<genexpr>7  s1      LLquuV}};LLLLLLr$   z8Native image attachment failed, falling back to text: %s)conversation_historyr
  ru  r  r  session_prompt_tokenssession_completion_tokensr  r  r
  r  r  r  r  r  r  interrupt_messager  r  rw  rx  z[[audio_as_voice]]rI  u/   Session split detected: %s → %s (compression))maybe_auto_title_emit_auxiliary_failurer   r   r"  )r   r   r   r   r"  )failure_callbackmain_runtimec                 2                         |           S r  )r
  )r&  effective_session_idr_  r  s    r"   r  z<GatewayRunner._run_agent.<locals>.run_sync.<locals>.<lambda>8  s#    RVR|R|"0!S S r$   title_callbackr  r6  rw   r8  )lr?   r@   r4   r)  r   r  r   r6   r'  r   r  r*  r:  rA   r   r1  ry  r)  r*  r+  r%   r#  r*  r  r:  r3   r;  r,  r-  r   r-  r  r  r  r5   r1  r2  r  r)   r  r  r  r  rG  KeyErrorr  r%  r  r  rK	  r  r  rR  r3  r  tool_progress_callbackr  step_callbackstream_delta_callbackinterim_assistant_callbackstatus_callbackrz  r  r  rG  r:  rI  background_review_callbackr  r:	  r|  r  rN  r]  r'   finditerr    r  r  r~  r  r  r  r  rW   r  rE   rP   rZ   r5  r  r  r  r  r+  rK  r  r	  r>  r  rA  rO  r  r9  r  agent.title_generatorr  r  )r  r  combined_ephemeralevent_channel_promptr   r  r2  r	  rz  r  rF  r*  rG  rH  _want_stream_deltas_want_interim_messages_want_interim_consumerr,  r-  rC  rI  rJ  rK  rL  rM  rN  r  r	  _sigr   r  r  r
  r  r  _pdcagent_historyrX   rS   has_tool_callshas_tool_call_idis_tool_message	clean_msgrr  
mirror_srcr@  _rkey_rval_history_media_paths_hm_hc_match_pr  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_excr  ru  _last_prompt_toks_input_toks_output_toks_context_lengthr  _resolved_model	error_msg
media_tagshas_voice_directiver   ra   seenunique_tagsr4  _session_was_split_effective_history_offsetr  all_msgs_title_failure_cbmaybe_auto_title_kwargsr  r  r  r  r  rE  r  rl  r  r  rX  r  r$  r  r  r  r  r  rv  rL  r  r	  rw  rQ   "interim_assistant_messages_enabledr[  r}  r{  r  result_holderrM  r_  r  rV  r  stream_consumer_holdertool_progress_enabledtools_holderr  s                                                                                                    @@@@@@@r"   r	  z*GatewayRunner._run_agent.<locals>.run_syncX5  sk    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"%;"" 6R&< 6R5Rcccccccc#}00AAH 0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) ) ) ,A+@$,$*N#0%< $2#= "L!K!K!K!K%)
, 
, 
,( / 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 M M MC Md MW[ M M M M M M M M M M M. 88%XXJ
 //7#9% "==kJJ 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+, %..-. #NN/0 %..12 %..34 %..56 )478  $//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(9C9P"Z"5"5VZE*:E'H^/h/D/DdhE,$9E!%5E"!%!3E&0nn5H&I&I&ORE#!*!2!2,.&/n&6&6#MC MD M M M M M M M M M M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[) $LO-4UG-D-DNekk$LO M *4 *4wwv  ,,, 8## ".!4#1S#8 "&&.! 4%5 4 4 R R#))++ R R RI!((3333 "ggi00G 4778,, Q),BS)T)TJ&P&P&Pw&P&PG)-' B B
  ;..*C 9 9(+#( !938E%L%,,U333
 ), $ = =776??&:::'')R00C3&(k.#&F&F = =F!'a!6!6!8!8!?!?!F!FB! = 4 8 8 < < <           ALT ALd AL AL AL AL AL AL AL AL AL ALH %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#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//S`jt/uu))*?@@@))*ABBBB *)*?@@@))*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# < ~--
&+#!::j"55 	; 	;Cwwv*>>>"%'')R"8"8#w..)+^W)M)M G G',{{1~~';';'='='D'DU'K'K#' !GD8L,L,L$.$5$5otoo$F$F$F3w>>6: 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$&,,...OT#d75,
#K#K#KZd  .@(WSEWEW%  $$"2 $#FFFFFFGTUVGW_}Q/33JCCC]_H
 )08$) )% -> #)-%,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#$ %fjj1Eu&M&M%  s   AD 
D1	D,&D1,D1$C'K 
K;K66K;<P6O)(P6)
O63P65O664P66P:=P:/VVV$e ee7l Aj. -l .
k8kl kl l+
C 
  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   Nr3  )r  r  r9  r  )rn  r  s    r"   _start_stream_consumerz8GatewayRunner._run_agent.<locals>._start_stream_consumerA8  s      3ZZ * *)!,80377999999999FFmD))))))))))	* *r$   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   r3  uL   Skipping stale agent promotion for %s — generation %s is no longer currentr>   r  )r9  r  r  r*  r9  r?  r  r^  )rv  rM  r_  rV  s   r"   track_agentz-GatewayRunner._run_agent.<locals>.track_agentM8  s      q/)mD))))))))) q/) 
 )$2N2N^3 3) b%2"  
 0<QD -~ 8++J777778 8r$   c                  F  K   sd S 	 t          j        d           d {V  	 j                            	j                  } | s>t          | d          r|                               rtd         }|rj| j                                      }|r|j        nd }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   z3Interrupt detected from adapter, signaling agent...z,monitor_for_interrupt error (will retry): %s)r9  r  r   rA   r   r  r  r@  r   r*  r:  r  r]  r`  r   )
rC  r   _peek_eventpending_text_mon_err_interrupt_detectedrv  r_  rV  r  s
        r"   monitor_for_interruptz7GatewayRunner._run_agent.<locals>.monitor_for_interruptn8  sp       [mC((((((((([  $}00AAH# ! 
 x)@AA "hFdFdepFqFq " ,Q  " +3*D*H*H*U*UK?J+T;+;+;PTL"LL)^___!OOL999/33555!E-     [ [ [LL!OQYZZZZZZZZ[? [s   !C  BC   D9DDr      c                    K   d S j                             j                  } | sd S 	 t          j                   d {V  t          t          j                    z
  dz            }d         }d}|rt          |d          r	 |                                }d|d          d|d	          g}|                    d
          r|	                    d|d
                     n)|	                    |                    dd                     dd
                    |          z   }n# t          $ r Y nw xY w	 |                     j        d| d| d           d {V }
rIt          |dd          r8t          |dd           r'		                    t          |j                             n2# t          $ r%}t"                              d|           Y d }~nd }~ww xY w)NTr  r   r>   r  r  r  r   r  r  r  r>  r  r  u   ⏳ Still working... (r  r  r  r  Fr  z#Long-running notification error: %s)r   rA   r   r9  r  r4   rM   r  r  rN  rO  r   r  r  r%   r)   r  r*  r:  )_notify_adapter_elapsed_mins
_agent_ref_status_detail_ar  _notify_res_ne_NOTIFY_INTERVALr  r  _notify_startr  rv  r_  r  s           r"   _notify_long_runningz6GatewayRunner._run_agent.<locals>._notify_long_running8  sq     '"m//@@O" Mm$4555555555 #TY[[=%@R$G H H)!_
!# 
'*6L"M"M 
	'<<>>"\r2B/C"\"\bIYFZ"\"\!]66.11 L"MM*Jb6H*J*JKKKK"MM"&&1Er*J*JKKK)0499V3D3D)D$   M(7(<(<]]]N]]]!8 )= ) ) # # # # # #K *M#KEBBM $KtDDM
 )//K4J0K0KLLL  M M MLL!FLLLLLLLLM;Ms,   	BD( (
D54D59A5F/ /
G9GGr   r;  r   i  r  r  r  zABackup interrupt detected for session %s (monitor task state: %s)r!  r  r  r  r=  r  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: %sr>  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.rI  r  )ru  r  r  r|  r  r  r  r   r  r  z5Ignoring control interrupt message for session %s: %sr  z9Processing queued message after agent completion: '%s...'rE	  pending_steerz0Delivering leftover /steer as next turn: '%s...'r>   r+  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)ru  r  r  r  z5Stream consumer wait before queued message failed: %sr6  final_response_sentru  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  rQ   r  r  rV  rM  rX  r  rL  r  zjSuppressing normal final send for session %s: final delivery already confirmed (streamed=%s previewed=%s).r  r:	  c                  v    dfd} 	 t          j         |                        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_messager   )_mid_adapter_snapshot_chat_id_snapshot_ids_snapshots    r"   _delete_allzLGatewayRunner._run_agent.<locals>._cleanup_temp_bubbles.<locals>._delete_all:  s       - ! !!"3"B"B 14# #          ) ! ! ! D!! !s   &
33r8  )r9  r
  r   )r;  r8  r9  r:  _loop_snapshots    r"   _cleanup_temp_bubblesz7GatewayRunner._run_agent.<locals>._cleanup_temp_bubbles:  sq    ! ! ! ! ! ! ! !4[[]]NSSSSS    DDs   * 
88z-Post-delivery cleanup registration failed: %sNNNr8  )kr  rW  r  rl  r6  r3   r  r  r   r	  r	  r  rA   r1   rW   r  r  rl  r[  r4   r   r?   r)  r#  r  rk  r   Queuer   r  r6  r  r)   r  r  r  r9  r  rY  r.  r  r9  r:  rI   rM   ensure_futurer	  r  r  r  r  r  r@  r   r*  r9  r!  r  r]  r  r  r:  r  _INTERRUPT_REASON_TIMEOUTrN  rO  r  r   r  r  rZ  rD  rb  rU  r6   rL  r^  r_  r&   r,  r  r0  r;	  r  _MAX_INTERRUPT_DEPTHr+  r  r2  r  r  r  r`  r  r^  r%   r  rd
  r#  r$  r  r  rK  rV  rD  r<  r  r:	  )r_  r[  r  rQ   r  r  rV  rM  rX  r  rL  r  r	  agent_cfg_localdisplay_configr[  _tpl_resolved_tp_env_tp_display_cfg_platforms_cfg_platform_cfg_legacy_tp_overrides_tool_progress_configured_cleanup_adapter_progress_thread_idr  r	  progress_taskrO  r  r  tracking_taskr$  interrupt_monitor_NOTIFY_INTERVAL_RAWr0  _notify_task_agent_timeout_raw_agent_timeout_agent_warning_raw_agent_warning_warning_fired_executor_task_inactivity_timeout_POLL_INTERVALr  r!  rn  _backup_adapter_backup_agent	_bp_event_bp_textr)  
_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  rY  r?  ra  r  _leftover_steer_pending_parts_pending_cmd_word_rc_pendingr;  was_interrupted_scrb  
_previewed_already_streamedfirst_response_bg_cb
_bg_resultupdated_historynext_sourcenext_messagenext_message_idnext_channel_prompt_followup_adapter_final_is_empty_sentinel	_streamedr=  _rperl  r  ru  r.  r8  r9  r  r  r  r:  r#  r  r<  r/  r  r  r$  r  r  r  r  r  rv  r	  rw  r  rw  rx  ry  r}  rz  r{  r6  r|  r  r  r  r  r  r  s   ````````` `                                                                                           @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@r"   r  zGatewayRunner._run_agent43  sY(     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// ""#?@@   	+ +@ITF	!Fs !##K?QRR
 
 BS\4=,,V_===X\'!""15H5WWW !&#&( !&w!%l	$ l	$# l	$# l	$s l	$ae l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$p ?hn,,"("2"F6F"("2CV`k+>??\` (/11f6F1K[1  	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 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>8 -++FO<< .?ho--&2B-GW- 1'7A A##
 M`&i{4G&H&Hei#	Oc 	OC 	OD 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O2^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	 ^	B   	J#/0F0F0H0HIIM 	* 	* 	* 	* 	* )*@*@*B*BCC	8 	8 	8 	8 	8 	8 	8 	8.  +KKMM:: &moo$	[ $	[ $	[ $	[ $	[ $	[ $	[ $	[ $	[L $/0E0E0G0GHH  **H#NN3G!3K3K//QU	$	M $	M $	M $	M $	M $	M $	M $	M $	M $	M $	M $	ML *+?+?+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 l' lk l 6w L L !% : :;Q^ _ _::m,, l] lvzzReGfGf l(.

3F(G(G%45FGG 4S'.3-    #4" l+0[4L]4[4[GLL!\^efigifi^jkkk  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--//  
 !% M M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_/`/``t  '$$&&&$$&&&!!!  !*;DDDDDDDDDDD,g.DE   &&((()))))))))"1   	   """  11 2    ~ 8++J777 '(9=,W   "







"1   o #)**]";";& @% 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 &%) )% &,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    &L  '$$&&&$$&&&!!!  !*;DDDDDDDDDDD,g.DE   &&((()))))))))"1   	   """  11 2    ~ 8++J777 '(9=,W   "







"1   I *.)K)K+* / *L * * $ $ $ $ $ $L
 $+%>  '$$&&&$$&&&!!!  !*;DDDDDDDDDDD,g.DE   &&((()))))))))"1   	   """  11 2    ~ 8++J777 '(9=,W   "







"1   { '.m\4&P&PO*1-AQSW*X*X'
 %)M$5$5fo$F$F!$ /;;"N%< <           %    "__(#1+&) +#1%5%9%4#6 -           '$$&&&$$&&&!!!  !*;DDDDDDDDDDD,g.DE   &&((()))))))))"1   	   """  11 2    ~ 8++J777 '(9=,W   "







"1   _M`  '$$&&&$$&&&!!!  !*;DDDDDDDDDDD,g.DE   &&((()))))))))"1   	   """  11 2    ~ 8++J777 '(9=,W   "







"1   ?  '$$&&&$$&&&!!!  !*;DDDDDDDDDDD,g.DE   &&((()))))))))"1   	   """  11 2    ~ 8++J777 '(9=,W   "







"1   ( %Q'h%% 	0hll8.D.D 	0\\"2339rF%+!Bv/BB%:EBB I
 hll+?@@AAJ% 09 0
 0 A&3	   ,0( #	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A`2 (*] A`2 
] A`2 ]  AA`2 <*_' &A`2 '
`1`A`2 `DA`2 *d? >A`2 ?
e	A`2 eOA`2 0u A`2 
uA`2 uD<A`2 {,,/|=|%$|=%|74|=6|77|=<|=~~/.~/4$A`2 6 5A`2 6/AA4@&A@/@.AA4@/AA@>AA4A AAAAA4AA`2 A	AA4AAA/A*A`2 A/AA4A4A!A`2 CA AD DA`2 D
AED!AED<A`2 EAEEA&A`2 F-&AG GA`2 G
AG!GA`2 G AG!G!A A`2 H"&AI	 IA`2 I	
AIIA`2 IAIIBA`2 LAL5L5/ANM%AM.M-ANM.AN M=ANM?AN N ANNANOAO&O&AO8O7AO8O="A`2 Q AQ=Q=/ASR-AR6R5ASR6ASSASSASSASSAST%AT.T.AU T?AU UAA`2 V	"AV, V+A`2 V,
AV9V6A`2 V8AV9V9*A`2 X%AYY/AZY2AY;Y:AZY;AZZ
AZZAZZAZZAZ[*A[3[3A\\A\]A]) ])/A^:^A^"^!A^:^"A^4^1A^:^3A^4^4A^:^9A^:`A``A`,`+A`,`2AAea4AbbAeb/Ac"cAc
c	Ac"c
Ac	cAc"cAc	cAc"cAec!Ac"c"AAed9AeeAeeAe	eAeeAe	eAejAj* j*
Akj4AkkAkr  r8  rf  )r  )r  )rP  r  r  )r>   )ri  rj  rk  r>  )NNr   NN(  r  
__module____qualname____doc__r  r	   r)   r5   __annotations__r  r  r  r  r
   r4   r  r3   r  r  r  r  r  r9  Taskr  r   r   r  rf  r!  r  rv   r  r  r  rZ  r  r  r  r  r  r  r  propertyr  r  r  r  r
  r  r  	frozensetr  r  r  r  r  r  r  r  r  rW   r  r  r  r  r%  r)  r+  r0  r4  r8  r>  rD  rH  staticmethodrK  rO  rV  r^  r  r   r$  r&  r(  rv  ry  r|  r*  r,  r.  r/  r  r0  r.  r2  r  r  r  r  r  r  r  r  r  r  r	  r]  r  r  r  r-  r<  rD  rQ  r  r  r  r%  r)  r  r  r"  r  r  r  r  r   r  rD  r  rK  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r'	  r+	  r  r6	  r=	  r  r  r  r  r  r]	  r^	  rk	  r	  rj	  r  r  r  r  r  r	  r  r  r  r  r  r  r^
  rg
  rl
  ro
  rz
  r
  r
  r
  r
  r
  r  r
  r
  r  r  r  r  r  r  r  r  r	  r  r5	  _APPROVAL_TIMEOUT_SECONDSr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rZ  r  r  r  rg  r  r  r  r  r  r  r	  r  r  r  r  r  r  rB  r  classmethodr  r  r  r  r  rZ  r  r  r  r  r  r  r  r  r  r  r  rW  r  rw   r$   r"   r  r    s{#          ,.S%Z(---'c'''$IEIII $J$$$It$$$$"'4'''#t###!&$&&&)-J&---:<d3S#X#67<<<>@ $sDcN':";@@@t, t,x6 t, t, t, t,n-
 -
 -
 -
f$     $&??-8 -c -c - - - -4S>    4@ @ @ @,s ,d ,W[ , , , ,+c +D +UY + + + +$* * * *X   (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= 
 
 
 


 
	
 
 
 
( +/%)&*P% P% P% 'P% c]	P%
 d^P% 
sDy	P% P% P% P%d*s *3 *X\ *ae * * * *X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  } 
   & #Dc3h$8 # # # \#J 3    \( D4K    \. F Fc4i8H F F F \F8 +/%)	- - - '- c]	-
 
- - - -&TT #4.T 
	T T T T d
    \4 $    \  3    \&     \2         \ D D    \ $+"4    \(
$sCx. 
 
 
 
S3 S| SX\ S S S Su| uZ] ubf u u u un#% #E$sCx.RVBV<W # # # #<Q Q Q Q Q QQ Q Q Qf1tCH~ 1$ 1 1 1 1'c 'd ' ' ' 'R 0S T    6/c / / / /b     *   > 38U   4 d W[    , %9FFF B3 B B B BHDT D D D DL`' `'c `' `' `' `'D^' ^'u ^'t ^' ^' ^' ^'B >B !$-5c]	   ,  hsm t    ^ ^ ^ ^@}' }' }' }'D !& %z z z z 	z
 z 
z z z zx* * * *zz z 
%	&	z z z zvI-- I-D I- I- I- I-VBhx6H BS B B B BHGc Gd G G G G>Y<< Y<HSM Y< Y< Y< Y<vK K 	K
 d38n%K 
#K K K KZ?s ?tCy ? ? ? ?     *c    g9# g9_b g9 g9 g9 g9Ru c u  u  u  u nR6 R6%^H[B\ R6 R6 R6 R6h <  C         K', K'3 K' K' K' K'Z= , = 3 =  =  =  = ~X , X 3 X  X  X  X t%- %-sNGZA[ %- %- %- %-NN[< N[E#~J]D^ N[ N[ N[ N[`/8, /84 /8 /8 /8 /8d
 
 
 
 
 
26
L 6
S 6
 6
 6
 6
pf  f (3- f  f  f  f PCJ| CJ CJ CJ CJ CJJ"7 "7# "7 "7 "7 "7NS    *W W W W,R
 R
3 R
 R
 R
 R
hS 3 4    &)S )SV )[_ ) ) ) )VCG CG 	CG
 CG 
CG CG CG CGJS S S S S S2&
L &
S &
 &
 &
 &
P \ hsm    \K. K.# K. K. K. K.Z1Xl 1Xs 1X 1X 1X 1Xf%| % % % % %.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8\ 8 8 8 8 8 8tnInI nI
 
nI nI nI nI`9(L 9(S 9( 9( 9( 9(vJl Js J J J J:PP#2P=@P	P P P PdoM\ oMc oM oM oM oMb:O :O :O :O :O :Ox} }sNGZA[ } } } }"@R< @RC @R @R @R @RDV
, V
3 V
 V
 V
 V
pB.L B.S B. B. B. B.H
] 
t 
 
 
 
8'U- 'UD 'U 'U 'U 'URU] Ut U U U U$	C 	C 	 	 	 	LdLd Ld 	Ld
 
Ld Ld Ld Ld\66 6 	6
 
6 6 6 6> ,1(M d    &
3 
 
 
 
,!
M !
c !
 !
 !
 !
Fo@ o@ o@S o@RU o@ o@ o@ o@b, - , C ,  ,  ,  , \4< 4Y\ 4ad 4 4 4 4l.f .f# .f .f .f .f`NT, NT3 NT NT NT NT`^
, ^
3 ^
 ^
 ^
 ^
@G; G;# G; G; G; G;R/5L /5S /5 /5 /5 /5bL
l L
xPS} L
 L
 L
 L
\F1| F1 F1 F1 F1 F1P`4 `4# `4 `4 `4 `4d9 9 	9
 9 9 
#9 9 9 9v4S>    (Xd38n5M ( ( ( ( !$;q< ;qHSM ;q ;q ;q ;qz#K #K #K #K #K #KN !*	8+X^X=N,hoh>O)@(/S[Sgiqiw  zB  zH	+ ! !*E *E# *E *E *E *EXeJ, eJ3 eJ eJ eJ eJNX X X X  #!$	P? P?P? P? 	P?
 
P? P? P? P?dV V V V Vp=0(5c8TW=AX;Y2Z =0 =0 =0 =0D GK= = = s5c8C=)@#ABC= 
U3Xc]*+	,	= = = =~
 
4 
 
 
 
(# #$ # # # #
F F F#    .CC #YC 
	C C C CJSS #YS 
	S S S SjE
t E
 E
 E
 E
N$F3 $FT $Fd $F $F $F $FLW>$ W>4 W> W> W> W>r )    t     [:  #'/> />/>/> /> 	/>
 4K/> 
/> /> /> \/>b%%'*%<@%	% % % %*M M# MRV M M M M )-	' ' '' !	'
 
' ' ' 'R# $    B     $ UW 
 
 
c 
c 
[^ 
 
 
 
G3 GC GD G G G G  $J	
 
   . '+; ; ;; ;
 ; !;  $; 
; ; ; ;29s 9t 9 9 9 9 "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	  (, !*.(,\ \\ \ d38n%	\
 \ \ \ !\ \ #3-\ !\ 
c3h\ \ \ \ \ \r$   r  r  
stop_eventr  c                    ddl m} ddlm}m} ddlm} d}d}	d}
d}t                              d|           d}| 	                                sv	  |d||	           n2# t          $ r%}t                              d
|           Y d}~nd}~ww xY w|dz  }||	z  dk    rr|rp	 ddlm} |4t          j         ||          |          }|                    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|                     |           | 	                                vt                              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_pastesr  r  z"Cron ticker started (interval=%ds)F)rT  r   r  zCron tick error: %sNr   r  r  r  z#Channel directory refresh error: %sr  )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)r*  r9  )rX   s    r"   r  z$_start_cron_ticker.<locals>.<lambda>&;  s    6;;}c+J+J r$   )idle_for_seconds
on_summaryzCurator tick error: %szCron ticker stopped)rm   r  r	  r  r  rR  r  r*  r9  r  r   r:  r  r  r9  r
  r  agent.curatorr  r5   r  )r  r   r  r  	cron_tickr  r  r  IMAGE_CACHE_EVERYCHANNEL_DIR_EVERYPASTE_SWEEP_EVERYCURATOR_EVERY
tick_countrb  r  r}
  rM  deletedr>  r  s                       r"   _start_cron_tickerr  :  s7    100000RRRRRRRR666666M
KK4h???J!! ?*	3IehTBBBBB 	3 	3 	3LL.22222222	3 	a
))Q..8.GMMMMMM#
 "://994 C JJrJ*** 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: 	))) !! ?*@ KK%&&&&&s   A 
B)B		B"<C 
D)D		D)E 
E4E//E48)F" "
G,GG+H
 

H9H44H9!I( (
J2JJFr9   	verbosityc                 P  $%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t'          d          D ]D}	 t          j        |	d           t+          j        d           -# t          t          f$ r Y  n_w xY wt                              d|	           	  ||	d           t+          j        d           n# t          t          t           f$ r Y nw xY w |             	 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 }  |tB          d           |ddl"m#} tH          j%        tH          j&        d'                    |tH          j(                  }tI          j)                    }|*                    |           |+                     |d                     tI          j,                    -                    |           |tI          j,                    j.        k     r&tI          j,                    *                    |           t_          |           %d$d2$%fd	}%fd }ta          j1                    }te          j3                    te          j4                    u rtj          j6        tj          j7        fD ]*}	 |8                    |||           # tr          $ r Y 'w xY wtu          tj          d!          r2	 |8                    tj          j;        |           n+# tr          $ r Y nw xY wnt          
                    d"           ddl<}dd#l m=}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|?                    |           |?                    |           	 dd'l@mA} ta          j1                    }|B                    d|           d{V  n2# t          $ r%}t                              d(|           Y d}~nd}~ww xY w%C                                 d{V } | sdS %jD        r)%jE        r t                              d)%jE                   dS te          jF                    }!te          jG        t          |!f%jI        ta          j1                    d*dd+,          }"|"C                                 %J                                 d{V  %jK        r)%jE        r t                              d-%jE                   dS |!L                                 |"M                    d./           	 dd0l@mN}#  |#             n# t          $ r Y nw xY w%jO        t          %jO                  $r#%jQ        st          
                    d1           dS dS )3a7  
    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_markerro  g      ?zAOld gateway (PID %d) did not exit after SIGTERM, sending SIGKILL.Tz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_loggingr  )r   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|rt                              d           n9|rt                              d	           nd	t                              d
           	 dd l	}|
                    ddgddd          }d |j                                        D             }|r/t                              dd                    |                     nt                              d           n# t          $ r Y n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: %suD   Received SIGTERM as a planned --replace takeover — exiting cleanlyuE   Received SIGTERM/SIGINT as a planned gateway stop — exiting cleanlyu/   Received SIGTERM/SIGINT — initiating shutdownpsauxrg  )capture_outputr   r   c                     g | ]l}d |                                 v sd|                                 v r>t          t          j                              |                                dd         vj|mS )r  r  r   ri  )r_  r)   r?   r%  r^  )r  ru  s     r"   rm  zBstart_gateway.<locals>.shutdown_signal_handler.<locals>.<listcomp><  sq       

,,	TZZ\\0I0I	$$DJJLL1,=== ===r$   u<   Shutdown diagnostic — other hermes processes running:
  %sz
  u7   Shutdown diagnostic — no other hermes processes found)r]  r  r   r*  r:  signalSIGINTr  r9  r$  r  r!  rq  r+  rO  r9  r9  r"  )received_signalplanned_takeoverr  rb  planned_stopr  _sp_ps_hermes_procs_signal_initiated_shutdownrunners            r"   shutdown_signal_handlerz.start_gateway.<locals>.shutdown_signal_handler;  sl    !	@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  
	KKKV     	KKKW    *.&KKIJJJ	$$$$''u#$   C !$!6!6!8!8  M
  WSKK..   
 UVVV 	 	 	D	FKKMM*****s?    
AA  A A1 1
B ;BB :BF 
FFc                  6                          dd           d S )NFTr.  )r<  )r  s   r"   restart_signal_handlerz-start_gateway.<locals>.restart_signal_handler<  s"    4@@@@@r$   SIGUSR1z6Skipping signal handlers (not running in main thread).)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)r   r  zcron-ticker)r  r$  rm  r
  rF   z Gateway exiting with failure: %sr  r  )r  z}Exiting with code 1 (signal-initiated shutdown without restart request) so systemd Restart=on-failure can revive the gateway.r  )Rr]  r  r  r  r  r  r  r?   r%  r*  r9  r  r   r:  ProcessLookupErrorPermissionErrorr  r  r  r  killrM   r  r+  r{   r  r  r)   printtools.skills_syncr  hermes_loggingr  rv   agent.redactr  loggingWARNINGINFOrA   DEBUGStreamHandlersetLevelsetFormatter	getLogger
addHandlerr
  r  r9  r  rG  current_threadmain_threadr  r  SIGTERMadd_signal_handlerNotImplementedErrorr  r  atexitr  FileExistsErrorr*  r  r  r  r  r  r  r:  r  r  r   r  r  r]  rO  r  r  
SystemExitr  )&r  r9   r  r  r  r  r  r  r  existing_pidexisting_start_timer  rb  r  rn  r  	_releasedr   r  r  r  _stderr_level_stderr_handlerr  r  r  sigr  r  _current_pidr  _loopr  	cron_stopcron_threadr  r  r  s&                                       @@r"   start_gatewayr  /;  sr
     &                #?$$LLBIKK$?$? [	"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 2YY  GL!,,,JsOOOO*O<   EE W   !M,d;;;;JsOOOO*OWE   D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][[!/11  ///$$%7%78]%^%^___&&7777,..444((7776""F "'@+ @+ @+ @+ @+ @+ @+D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 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777777    #)*** " &*C M	
 	
 	
 u4s   $A6 6
B% B  B%)B7 7
D+D/D ?D 
D
DDDD))EE('E(!F) )GG%G7 7
HHH 
H&%H&*0I 
I('I(8K 
KKQ))
Q65Q6 R0 0
R=<R=
U .VV;5W1 1
X ;XX ] 
]#"]#c                     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   Nz)Hermes Gateway - Multi-platform messaging)r  z--configrY  zPath to gateway config file)rV  z	--verbosez-v
store_truezVerbose output)r  rV  r   r   r   )argparseArgumentParseradd_argument
parse_argsr  r   r   r   r  r	  r9  r  r  r  exit)r  parserr$  r  r   r   r  r  s           r"   r  r  <  sN   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   ,B;;B?B?__main__r8  r  )NNr  )NFr   )r  r9  rv  r#  ru  r  r?   r'   rr  r  r  r	  rG  rM   collectionsr   contextvarsr   r	  r   r   typingr	   r
   r   r   r   agent.account_usager   r   
agent.i18nr   r   r   r  r  r  compiler*   r)   r-   rB   r5   r;   rE   rI   r3   rP   rZ   rh   rp   rs   rx   r@   ra   rA  r   r  r  r{   utilsr|   r}   r~   r   rv   dotenvr   hermes_cli.env_loaderr   	_env_pathr   r   r   rw  rx  _config_pathrb   r   r   r   r=  r   r   r   r  _key_valr1   r4   rA   _terminal_cfgrW   _terminal_env_map_cfg_key_env_varrc  r.  r  _auxiliary_cfg_aux_task_env	_task_key_env_map	_task_cfgr6   _provr$  r%  r  
_agent_cfgrH  _tz_cfg_security_cfg_redactr_  r   _bridge_errr  r  r  r"  r   dir_network_cfg_bootstrap_excr   r   _configured_cwdr)  r  	_fallbackr#  r  r  r  r  r  r  r  r	  r
  r  r  r  r  r  gateway.deliveryr  r	  r  r  r  r  r  gateway.restartr  r  r  gateway.whatsapp_identityr  _canonical_whatsapp_identifierr  r  r  r  r  r*  objectr  r3  r,  rU  rZ  r  r  rA   _INTERRUPT_REASON_SSE_DISCONNECTr  r  r  r`  rb  r  rx  r  r  r  r  r  r  r  weakrefr"  r  r#  r  r  r  r  r:  r  r  r  rw   r$   r"   <module>r     s             				 				  



        # # # # # # $ $ $ $ $ $             3 3 3 3 3 3 3 3 3 3 3 3 3 3 P O O O O O O O       % % % % % %  # )- &)rz*TUU < <s <s < < < <L )0 %S Xe_    D< < < < <&S 5 U    $  #'	) ) )) 
%) %	)
 
) ) ) ):d38n1E(F 3    :# # # #J3 3 3 3 3 3> > > > > >
<t < < < < !$
       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+ [
Z
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!  #$?!!" !";#!$ )H";1Y+M5$?/! ! !2 '8&=&=&?&? 9 9"(},,(2D
  5((SSYY:N-N-N   5((ZZc-B-B(!w11$77!z$-- 9/9tz$/?/?
8,,/2s4yy
8,
 +r22 $	?jj>> $	? !<5 ;9	  !A: @>	    !>7 =;	  M( (5':':'<'< ? ?#	8*..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 =Uf__7<BJx
34 ;4:BJx01 A7@BJx
34 ?6>BJx	23 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
;<!\11@CLQcDd@e@e
<=((:r** 	<zz'3// 	<,3MMOOBJ()R00:mT** 	K#''(899G"69c'll6H6H6J6J
23 
 
 
 	;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~                                 , + + + + +                                
	8	$	$ !&(( $t $ $ $ $N,t , , , ,^s    ,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
d    : 4$; #     Xd3i0    6C M    4d |    <    $0L X\ 0 0 0 	+ + +++ 	+
 	+ + + +\$ 4    (xZ xZ xZ xZ xZ xZ xZ xZvuY' Y'9? Y'X[ Y' Y' Y' Y'x[ [ 7 [ [bjknbo [x| [ [ [ [|
  0 zDFFFFF s   %[ 6H	[ H[ HR4[ \8\\A]5 5^:^^^. ._3___' '`,``