
    o;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
Z dZ!dZ"dZ#dZ$e%e&df         e'd<   dede(f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Z/dd Z0d!e&de&fd"Z1 e0             e	j2        3                    d e& ee4          j5        j5                             dd#l6m7Z7 dd$l8m9Z9m:Z:m;Z;m<Z<  e7            Z=dd%l>m?Z? dd&l@mAZA e=d'z  ZB eAe= ee4          C                                jD        d(         d'z  )            ejE        d*          ZFd+d,hZGe=d-z  ZHeHI                                r8	 ddlJZK eLeHd./          5 ZM eKjN        eM          pi ZOddd           n# 1 swxY w Y   dd0lmPZP  ePeO          ZOeOQ                                D ]1\  ZRZS eTeSe&eUe(e-f          reRejV        vr e&eS          ejV        eR<   2eOW                    d1i           ZXeXr eTeXeY          ri d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZZZeZQ                                D ]\  Z[Z\e[eXv reXe[         ZSe[d4k    r e&eS          d[v r%e[d4k    r& eTeSe&          rej2        ]                    eS          ZS eTeSe^          r ej_        eS          ejV        e\<   v e&eS          ejV        e\<   eOW                    d\i           Z`e`rs eTe`eY          rfd]d^d_d`dadbdcdddedadfdgdhdidadjZaeaQ                                D ];\  ZbZce`W                    ebi           Zd eTedeY          s) e&edW                    dkdl                    e                                Zf e&edW                    dmdl                    e                                Zg e&edW                    dndl                    e                                Zh e&edW                    dodl                    e                                Ziefrefdpk    refejV        ecdk         <   egregejV        ecdm         <   ehrehejV        ecdn         <   eireiejV        ecdo         <   =eOW                    dqi           Zjejr eTejeY          rdrejv r e&ejdr                   ejV        ds<   dtejv r e&ejdt                   ejV        du<   dvejv r e&ejdv                   ejV        dw<   dxejv r e&ejdx                   ejV        dy<   dzejv r e&ejdz                   ejV        d{<   d|ejv r e&ejd|                   ejV        d}<   eOW                    d~i           ZkekrF eTekeY          r:dekv r e&ekd                   ejV        d<   dekv r e&ekd                   ejV        d<   eOW                    ddl          Zlelr( eTele&          rele                                ejV        d<   eOW                    di           Zm eTemeY          r<emW                    d          Znen% e&en          o                                ejV        d<   nJ# ep$ rBZq erd eseq          jt         deq e	ju                    erde	ju                   Y dZq[qndZq[qww xY w	 ddl6mvZv d ew            v reOni W                    di           Zx eTexeY          r!exW                    d          r evd           n# ep$ r Y nw xY w	 ddlmyZy  ey             n# ep$ r Y nw xY w	 ddlmzZz  ez             n# ep$ r Y nw xY wdejV        d<   dejV        d<   ejV        W                    d5dl          Z{e{re{d[v r2 ej|        d          p e& ej}                              Z~e~ejV        d5<   ddlmZmZmZmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZ ddlmZmZmZ ddlmZmZmZ  ej        et          Z e            ZdeYfdZdeYdz  fdZde&fdZde&dedz  fdZdZdZdZdZdZdZ eeo                                eo                                eo                                eo                                eo                                eo                                h          Zdee&         de-fdZdede%e&dz  e&dz  f         fdZde&de&dz  fdZddde&fdZdeYfdZddeYdz  de&fdZdee^e&                  fdZde&ddfdZdeYddfdZddlZd aej        e'd<    G d d          Zddej        deUfdZddee         de-deeU         de-fdƄZdǄ Zetdk    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)cfg_get         @      >@  )zhermes_cli/config.pyzhermes_cli/__init__.pyzrun_agent.pyzgateway/run.pyzpyproject.toml._STALE_CODE_SENTINELS	repo_rootreturnc                     d}t           D ]D}	 | |z                                  }n# t          t          f$ r Y .w xY w|j        |k    r|j        }E|S )u;  Return the newest mtime across the stale-code sentinel files.

    Missing files are ignored (they may not exist on older checkouts).
    Returns 0.0 if no sentinel file is readable — treat that as "can't
    tell", which downstream callers interpret as "not stale" to avoid
    false-positive restart loops.
            )r   statOSErrorFileNotFoundErrorst_mtime)r   newestrelsts       0/home/ubuntu/.hermes/hermes-agent/gateway/run.py_compute_repo_mtimer"   ^   st     F$ ! !	c/''))BB*+ 	 	 	H	;[FMs   %99valuec                 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floatstrstrip
ValueErrorfromisoformatreplace)r#   textnumerics      r!   _coerce_gateway_timestampr2   q   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getr*   %_AUTO_CONTINUE_FRESHNESS_SECS_DEFAULT	TypeErrorr-   )raws    r!   _auto_continue_freshness_windowr<      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.
    Nr5   )r6   r7   r8   r*   r:   r-   )r=   r>   r;   s      r!   
_float_envr@      ss     *..

C
{cRiiW~~Szzz"   W~~s   A  A*)A*)nowwindow_secsrA   rB   c                    |t          |          nt          t                    }|dk    rdS t          |           }|dS |t          j                    n|}||z
  |k    S )a  Return True when an interruption marker is fresh enough to auto-continue.

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

    A non-positive ``window_secs`` disables the gate (always fresh), which
    restores the pre-fix behaviour for users who opt out via config.
    Nr   T)r*   r9   r2   time)r#   rA   rB   windowr'   currents         r!   _is_fresh_gateway_interruptionrG      sv    " " 	k899 
 {{t)%00It [dikkkcGY&((    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systemr'   )reversedr&   dictr8   )rI   msgrK   tss       r!   _last_transcript_timestamprR      s      t    #t$$ 	wwv 	t999WW[!!>III tt4rH   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)r6   r7   sslget_default_verify_pathscafileopenssl_cafilepathexistscertifiwhereImportError)rU   paths	candidater[   s       r!   _ensure_ssl_certsr`      s   "*$$JJJ ((**ElE$89  	 		22 	*3BJ'FF&-mmoo
?#   	  	 7>>)$$ 	*3BJ'FF	 s   .%B 
B"!B"platform_namec                     ddl m} |                    |                                 |                                  d          S )z9Return the configured home-target env var for a platform.r   )_HOME_TARGET_ENV_VARS_HOME_CHANNEL)cron.schedulerrc   r8   lowerupper)ra   rc   s     r!   _home_target_env_varrh     sS    444444 $$  ///  rH   )get_hermes_home)atomic_json_writeatomic_yaml_writebase_url_host_matchesis_truthy_value)load_dotenv)load_hermes_dotenvz.env   )hermes_homeproject_envz=^(?P<host>.+):(?P<container>/[^:]+?)(?::(?P<options>[^:]+))?$z/outputz/outputsconfig.yamlutf-8encoding)_expand_env_vars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   r5   r   r   r   r   agent	max_turnsHERMES_MAX_ITERATIONS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_freshnessr4   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force)print_config_warnings)warn_deprecated_cwd_env_vars1HERMES_QUIETHERMES_EXEC_ASKMESSAGING_CWD)Platform_BUILTIN_PLATFORM_VALUESGatewayConfig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   r6   getenvloggerwarning_try_resolve_fallback_providerRuntimeError	Exceptionr8   list)r   r   r   runtimeauth_exc	fb_configexcs          r!   _resolve_runtime_agent_kwargsr  )  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          |t                    s	  | |                    d	          |                    d
          |                    d                    }t                              d|                    d	                     |                    d          |                    d
          |                    d	          |                    d          |                    d          t          |                    d          pg           |                    d          dc S # t          $ r:}	t                              d|                    d	          |	           Y d}	~	nd}	~	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   Nrs   rt   ru   fallback_providersfallback_modelr   r   r   )r   explicit_base_urlexplicit_api_keyzFallback provider resolved: %sr   r   r   r  r  zFallback entry %s failed: %s)r  r   yaml_hermes_homerZ   open	safe_loadr8   r&   r  rO   r  infor
  debug)
r   _ycfg_path_fcfgfbfb_listentryr  fb_excs
             r!   r  r  P  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  
 <gkk*>U>UVVV&{{955 'J 7 7 'J 7 7 'J 7 7&{{955 V!4!4!:;;'.{{3D'E'E        ;UYYz=R=RTZ[[['	,    4sj   "H- H- A!H- !A%%H- (A%)/H- 5H- DG%"H- %
H)//H$H- $H))H- -
H:9H: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_typesr5   image/message_typez[User sent an image: ]audio/z[User sent audio: z[User sent a file: 
)getattr	enumeratelen
startswithr   PHOTOappendjoin)eventpartsr%  r&  iurlmtypes          r!   _build_media_placeholderr8  z  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rH   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)adapterr9  s     r!   _dequeue_pending_eventr=    s     &&{333rH   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 )r2  r+   r,   splitrf   _CONTROL_INTERRUPT_MESSAGES)r>  
normalizeds     r!   _is_control_interrupt_messagerD    sV     u#g,,,,..446677==??J444rH   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.
    rt   r/   )rv   errorsNNz---z
---   r   Nzname::rp      )"'r@  -_z
[^a-z0-9-]r5   z-{2,})	read_textr
  r/  find
splitlinesr,   rA  r.  rf   r/   resub)rE  contentenddeclared_nameliner;   slug_res           r!   _skill_slug_from_frontmatterr\    s   &$$gi$HH   zze$$ z
,,w
"
"C
Qwwz $M#))++  zz||??7## 	**S!$$Q'--//C3xx1}}Q3r7!2!2s1v7K7K!B$iIIKKME	  z  ((c22::3DDD77="d++D778S$''--c22D #]""s    
((command_namec                 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.
    rP  rO  r   )_get_disabled_skill_names)get_all_skills_dirszSKILL.mdc              3      K   | ]}|d v V  	dS )).gitz.githubz.hubz.archiveN .0parts     r!   	<genexpr>z+_check_unavailable_skill.<locals>.<genexpr>  s(      bb4tFFbbbbbbrH   The **zJ** skill is installed but disabled.
Enable it with: `hermes skills config`)get_optional_skills_dirzoptional-skillsz	official//zQ** skill is available but not installed.
Install it with: `hermes skills install `N)rf   r/   tools.skills_toolr_  agent.skill_utilsr`  rZ   rglobanyr4  r\  hermes_constantsri  r   __file__resolveparentrelative_tor  r2  r
  )r]  rC  r_  r`  disabled
skills_dirrE  rZ  rX  ri  r   optional_dir	_declaredr   r4  install_paths                   r!   _check_unavailable_skillrz    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.platformr   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  %  s    ..55HNBrH   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.
    rs   r   )get_config_pathread_raw_configNrrt   ru   z%Could not load gateway config from %s)r  hermes_cli.configr  r  r
  rZ   r  r  r  r  r  )config_pathr  r  r  fs        r!   _load_gateway_configr  *  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   r>   r5   )r  r8   r&   r+   rO   )r  r  	model_cfgs      r!   _resolve_gateway_modelr  G  s     &&&,@,B,BC$$I)S!! H	It	$	$ H}}Y''G9==+A+AGRG2rH   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  W  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.
    rJ     r   r   rp   mainrK  rI     )r{  	chat_typechat_id)dmthread	thread_idN)rA  r.  )r9  r4  results      r!   _parse_session_keyr  r  s     c""E
5zzQ58w..58v3E3EaqQx
 

 u::>>eAh*:::"'(F;4rH   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>  r5   r)  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r8   )r  evt_type_sid_cmd_pat_out_supr0   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4rH   c                      d S Nrc  rc  rH   r!   <lambda>r    s    D rH   _gateway_runner_refc                      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Zeed<   dZeed<   dZe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)            Z0e.dee         fd*            Z1e.dee         fd+            Z2d,e3defd-Z4dddd.d,ee3         d/ee         d0ee5         de6ee5f         fd1Z7d2ed3ed4e5de5fd5Z8d6e9ddfd7Z:d8eddfd9Z;defd:Z<defd;Z=defd<Z>defd=Z?d/ed>d?d6eddfd@Z@d/ed6edAed?         ded?         fdBZAddCd/ed6edefdDZBddEee         dFee         ddfdGZCddddHdedIee         dJee         dKee         ddf
dLZDeEdeFeeef                  fdM            ZGeEdefdN            ZHeEde5dz  fdO            ZIeEdPede6eef         fdQ            ZJdddRd,ee3         d/ee         de5dz  fdSZKd/edTee5         ddfdUZLeEdedz  fdV            ZMeEdefdW            ZNeEdefdX            ZOeEdefdY            ZPeEdefdZ            ZQeEde5fd[            ZReEdeSe5z  dz  fd\            ZTdeeef         fd]ZUd/ed^eVddfd_ZWd^eVd/edefd`ZXdaede6eeef         ef         fdbZYd8eddfdcZZdddZ[deeeef         ddfdfZ\dgeddfdhZ]diZ^djZ_dke`ddfdlZadefdmZbd/eddfdnZcddoZddddpdqedredefdsZedefdtZfdduZgdefdvZhddxefdyZiddxeddfd{Zjd|e5d}eddfd~Zkd|e5ddfdZlddZmddZndddddedededdfdZoddZpde$dedee9         fdZqd,e3defdZrdee$         defdZsdeddfdZtd^eVdee         fdZud^eVd,e3deFeeef                  dee         fdZvd/edeFe         fdZwdedefdZxdefdZyd^eVdezee{f         fdZ|d^eVdefdZ}d^eVdefdZ~d^eVdefdZd^eVdefdZd^eVdezee{f         fdZd^eVdezee{f         fdZd^eVdefdZd^eVdefdZd^eVdefdZd^eVdee         fdZd^eVdefdZd^eVdefdZd dZd^d?defdZded,ededdfdZd^eVdefdZd^eVdefdZeEd^eVdee         fd            Zd^eVdefdZd^eVdefdZd^eVdefdZdeddfdZdededefdZ	 d!d^eVdedeSdedef
dZd^eVdeddfdZded^eVddfdZd^eVdefdZd^eVdefdZded,ddeddfdZd^eVdefdZd^eVdefdZd^eVdezee{f         fdZd^eVdefdZd^eVdefdÄZd^eVdefdĄZd^eVdefdńZd^eVdefdƄZd^eVdefdǄZd^eVdefdȄZd^eVdefdɄZd^eVdee         fdʄZd^eVdefd˄Zd^eVdefd̄Zd^eVdedededee         f
dЄZdeeef         fdфZdeeeef                  fd҄ZdwZd^eVdee         fdӄZd^eVdefdԄZ ee$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          Zd^eVdefdՄZd^eVdefdքZÐddׄZ	 	 	 d"dededaeddfd݄ZdefdބZƐdd߄ZdedeSfdZdeSddfdZd ZdefdZdedeFe         defdZdedeFe         defdZde5fdZdede5ddfdZde5ddfdZdiZdZe6ed<   ed0e5dz  de5fd            ZeE	 dd3ede5deSdede5dz  defd            Zd/ed3ed4e5de6fdZd/ededefdZddd/edee         defdZd/eddfdZd/edefdZېd dd/ed8edefdZd/ededefdZd6ed/ededz  ddfdZސddd/ed,e3ded	ed
eddfdZd/eddfdZeEdgededdfd            ZdgeddfdZddZdefdZdee         fdZ	 	 	 d#dededeFeeef                  d,dded/edee         dee         deeef         fdZ	 	 	 	 	 d$dededeFeeef                  d,e3ded/edee         dedee         dee         deeef         fdZdS (%  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   _boot_wall_time_boot_repo_mtime_stale_code_restart_triggeredr  c           
         |pt                      | _        i | _        |                                  t	          j        |           a	 t          j                    | _        t          t                                                    j        j        | _        t          | j                  | _        n2# t           $ r% d| _        t          d          | _        d| _        Y nw xY wt#                      | _        d| _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                 | _        |                                  | _!        | "                                | _#        | $                                | _%        ddl&m' tQ          | j        j)        | j        fd          | _*        tW          | j                  | _,        d| _-        t]          j/                    | _0        d| _1        d| _2        d | _3        d | _4        d| _5        d| _6        d| _7        d| _8        d| _9        d | _:        i | _;        i | _<        i | _=        i | _>        i | _?        i | _@        i | _A        dd lB}t                      | _D        |E                                | _F        i | _G        i | _H        i | _I        i | _J        i | _K        dd lL}|M                    d          | _N        	 dd	lOmP}  |d
           n# t           $ r Y nw xY wd | _Q        	 ddlRmS}  |            | _Q        n2# t           $ r%}t          U                    d|           Y d }~nd }~ww xY w| jQ        	 ddlVmW}  |            X                    d          pi }|X                    dd          r| jQ        Y                    t          |X                    dd                    t          |X                    dd                    t          |X                    dd                    | j        j)                   n2# t           $ r%}	t          U                    d|	           Y d }	~	nd }	~	ww xY w	 ddlVmW}  |            X                    d          pi }
|
X                    dd          rwddl\m]}  |t          |
X                    dd                    t          |
X                    dd                    t          |
X                    dd                               n2# t           $ r%}	t          U                    d|	           Y d }	~	nd }	~	ww xY wddl^m_}  |            | _`        ddlamb}  |            | _c        | d                                | _e        t#                      | _f        d S ) Nr   r   Fr   process_registryc                 .                         |           S r  )has_active_for_session)keyr  s    r!   r  z(GatewayRunner.__init__.<locals>.<lambda>  s    0@0W0WX[0\0\ rH   )has_active_processes_fnrp   )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)r  r  r  z'checkpoint auto-maintenance skipped: %s)PairingStore)HookRegistry)gr   r  adapters'_warn_if_docker_media_delivery_is_risky_weakrefrefr  rD   r  r   rq  rr  rs  _repo_root_for_stalenessr"   r  r
  set_stale_code_notifiedr  _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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_generation	threadingr   _agent_cacheLock_agent_cache_lockr  r  _pending_approvals_failed_platforms_update_prompt_pending	itertoolscount_slash_confirm_countertools.tirith_securityr  _session_dbhermes_stater  r  r  r  r  r8   maybe_auto_prune_and_vacuumr)   r(   tools.checkpoint_managerr  gateway.pairingr  pairing_storegateway.hooksr  hooks_load_voice_modes_voice_mode_background_tasks)selfr  
_threading
_itertoolsr  r  e_load_full_config	_sess_cfgr  	_ckpt_cfgr  r  r  r  s                 @r!   __init__zGatewayRunner.__init__  s}   5 3 5 5=?44666&l400
		(*.)++D 26x..2H2H2J2J2Q2XD)+>-, ,D!!  	( 	( 	(#&D ,0IID)$'D!!!	( /2ee!38* "&!<!<!>!>(,(J(J(L(L%!%!<!<!>!>!4466#88:: $ : : < <&*&F&F&H&H#!%!<!<!>!>#88:: 	<;;;;;)K$dk$\$\$\$\
 
 
  .dk::&}""'+/)-"'%*"!&$)!26 024613 >@LN3.079$ 	'&&&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   
  	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 '*eeso   A&B. .,CC+L> >
M
MM, ,
N6NN&CQ- -
R7RR B2U 
VU==V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`.
        rz   r5   dockerNc                 \    g | ])}|t           j        t           j        t           j        hv'|*S rc  )r   r~  
API_SERVERWEBHOOKre  ps     r!   
<listcomp>zIGatewayRunner._warn_if_docker_media_delivery_is_risky.<locals>.<listcomp>  s3    xxxQqQYQdfnfv@w7w7wq7w7w7wrH   r   c                 V    g | ]&}t          |t                    t          |          'S rc  )r&   r+   re  vs     r!   r8  zIGatewayRunner._warn_if_docker_media_delivery_is_risky.<locals>.<listcomp>  s-    LLL!As9K9KLs1vvLLLrH   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/...'.)r6   r  r,   rf   r  get_connected_platformsjsonloadsr&   r  r
  r  r  _DOCKER_VOLUME_SPEC_REmatchgroup$_DOCKER_MEDIA_OUTPUT_CONTAINER_PATHSr  )
r)  	connectedmessaging_platformsraw_volumesvolumesparsedhas_explicit_output_mountspecrC  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_toolrO  r
  )r)  rO  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.rJ  r#   )r)  r{  r  s      r!   
_voice_keyzGatewayRunner._voice_key  s    .,,7,,,rH   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_onlyrJ  z}Skipping legacy unprefixed voice mode key %r during migration. Re-enable voice mode on that chat to rebuild the prefixed key.)r@  rA  _VOICE_MODE_PATHrQ  r   JSONDecodeErrorr   r&   rO   itemsr+   r  r  )r)  datavalid_modesr  r  moder  s          r!   r&  zGatewayRunner._load_voice_modes  s    	:d3==??@@DD!4#7A 	 	 	III	 $%% 	I222!ZZ\\ 	 	MGT;&&g,,C#~~U  
 F3KKs   +. AAc                    	 | j         j                            dd           | j                             t	          j        | j        d                     d S # t          $ r&}t          	                    d|           Y d }~d S d }~ww xY w)NT)parentsexist_okrK  indentzFailed to save voice modes: %s)
rY  rs  mkdir
write_textr@  dumpsr'  r   r  r  )r)  r,  s     r!   _save_voice_modeszGatewayRunner._save_voice_modes  s    	@!(..td.KKK!,,
4+A666      	@ 	@ 	@NN;Q?????????	@s   AA 
B"BBru  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,  r&   r  adddiscard)r)  r<  r  ru  disabled_chatsenabled_chatss         r!   _set_adapter_auto_tts_disabledz,GatewayRunner._set_adapter_auto_tts_disabled  s     *DdKK.#.. 	F 	,w'''#G-FMMM--- /%%g...../ / ""7+++++rH   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.
        rj  Nri  rk  )r)  r<  r  rq  ro  rn  s         r!   _set_adapter_auto_tts_enabledz+GatewayRunner._set_adapter_auto_tts_enabled  s      )BDII--- 	F 	+g&&&$W.H$OON.#.. 0&&w/////0 0 !!'*****rH   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{  Nri  rj  r   r  voiceauto_ttsF_auto_tts_defaultrJ  c              3      K   | ]9\  }}|d k    |                                |t                    d         V  :dS )rW  Nr/  r.  re  r  r^  prefixs      r!   rg  zBGatewayRunner._sync_voice_mode_state_to_adapter.<locals>.<genexpr>,  sX       " "&/c45==S^^F%;%;= CKKLL! ===" "rH   c              3   ~   K   | ]7\  }}|d v 	|                               |t                    d         V  8dS ))rX  rV  Nry  rz  s      r!   rg  zBGatewayRunner._sync_voice_mode_state_to_adapter.<locals>.<genexpr>2  s_       ! !&/c4000S^^F5K5K0 CKKLL!0000! !rH   )r,  r&   r   r  r  r  r(   r8   r
  hasattrrw  r#   clearupdater'  r[  )	r)  r<  r{  rn  ro  r-  	_full_cfgrw  r{  s	           @r!   !_sync_voice_mode_state_to_adapterz/GatewayRunner._sync_voice_mode_state_to_adapter  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: %sr<  )
disconnectr
  r  r  r#   )r)  r<  r{  r,  s       r!   _safe_adapter_disconnectz&GatewayRunner._safe_adapter_disconnect7  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_TIMEOUTr5   r   z;Ignoring invalid HERMES_GATEWAY_PLATFORM_CONNECT_TIMEOUT=%r)	r6   r  r,   r*   maxr-   r  r  &_PLATFORM_CONNECT_TIMEOUT_SECS_DEFAULT)r)  r;   r}   s      r!   _platform_connect_timeout_secsz,GatewayRunner._platform_connect_timeout_secsK  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  connectr  wait_forTimeoutErrorr#   )r)  r<  r{  r}   r  s        r!   _connect_adapter_with_timeoutz+GatewayRunner._connect_adapter_with_timeoutZ  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_cleanlyf  s    !!rH   c                     | j         S r  )r  r  s    r!   should_exit_with_failurez&GatewayRunner.should_exit_with_failurej  s    &&rH   c                     | j         S r  )r  r  s    r!   exit_reasonzGatewayRunner.exit_reasonn  s      rH   c                     | j         S r  )r  r  s    r!   	exit_codezGatewayRunner.exit_coder  s
    rH   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.r  Nr  group_sessions_per_userTthread_sessions_per_userFr  r  )r}  r  _generate_session_keyr&   r+   r
  r,  r   )r)  r  r9  r  s       r!   _session_key_for_sourcez%GatewayRunner._session_key_for_sourcev  s    4)) 	d.@.L"0FFvNNk3// 'K '&&   x.. $+F4Mt$T$T%,V5OQV%W%W
 
 
 	
s   2A 
AAr  r9  user_configr9  r  c          	      8   |}|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                      }	|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=%sr5   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[]r   )get_default_model_for_provideru8   No model configured — defaulting to %s for provider %s)r  r
  r  r  r8   r  r  r  keysr  _apply_session_model_overridehermes_cli.modelsr  r  )r)  r  r9  r  resolved_session_keyr   overrideoverride_modeloverride_runtimeruntime_kwargsr  s              r!   _resolve_session_agent_runtimez,GatewayRunner._resolve_session_agent_runtime  s     +# 	,(:,'+'C'CF'K'K$$ , , ,'+$$$, '{33Nbl40445IJJJhl 	%\\'599N$LL44#<<	22$LL44$LL44	     ##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 	, 	$($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 
HH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  r8   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r<  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  unknown error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  errorr{  r#   fatal_error_codefatal_error_message_update_platform_runtime_statusfatal_error_retryabler  r8   r  popr  r  	platformsr  rD   	monotonicr  r  r  stopr  r.  )r)  r<  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-    !"  """""rH   c                 *    t          | j                  S r  )r.  r  r  s    r!   _running_agent_countz"GatewayRunner._running_agent_countD  s    4'(((rH   c                     | j         rdndS )Nrestartshutdownr  r  s    r!   _status_action_labelz"GatewayRunner._status_action_labelG  s     3CyyCrH   c                     | j         rdndS )N
restartingshutting downr  r  s    r!   _status_action_gerundz#GatewayRunner._status_action_gerundJ  s    #6K||OKrH   c                 "    | j         o| j        dv S )N)queuesteer)r  r  r  s    r!   _queue_during_drain_enabledz)GatewayRunner._queue_during_drain_enabledM  s     &V4+@DV+VVrH   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  r  )r,  r  
setdefaultr1  )r)  r9  r  r<  pending_slotqueued_eventss         r!   _enqueue_fifozGatewayRunner._enqueue_fifo^  s    ?Fw(;TBBF&6== M"/D,&&$$["55<<\JJJJJ(4L%%%rH   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.
        r  Nr   r  )r,  r8   r  r}  r  r  insert)r)  r9  r<  r  r  overflownext_queueds          r!   _promote_queued_eventz#GatewayRunner._promote_queued_eventn  s    "  &6== 	!   $$[11 	!  ll1oo 	1k4000 774G#H#H5@G%k22 $$["55<<QLLLrH   r<  c                    t          | dd          pi }t          |                    |g                     }||t          |di           v r|dz  }|S )u=   Total pending /queue items for a session — slot + overflow.r  Nr  rp   )r,  r.  r8   )r)  r9  r<  r  depths        r!   _queue_depthzGatewayRunner._queue_depth  sb    &6==CM%%k26677;''CVXZ2[2[#[#[QJErH   gateway_stater  c                     	 ddl m}  |||| j        |                                            d S # t          $ r Y d S w xY w)Nr   write_runtime_status)r  r  restart_requestedactive_agents)gateway.statusr  r  r  r
  )r)  r  r  r  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   r  )r{  r  r  r  )r  r  r
  )r)  r{  r  r  r  r  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_FILEr5   r   Nrs   rt   ru   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)r6   r  r  r  rZ   r  r  r8   r
  r   
expanduseris_absoluter  r  r@  loadr&   r  )		file_pathr  r  r  r  rY   r  r\  r,  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_PROMPTr5   r   Nrs   rt   ru   r   system_promptr>   )
r6   r  r  r  rZ   r  r  r   r,   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_effortr5   Nrs   rt   ru   r   reasoning_effortr  z5Unknown reasoning_effort '%s', using default (medium))rp  r  r  r  rZ   r  r  r+   r   r,   r
  r  r  )r  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   Nr5   u   —z--)r5   FFz--globalTr@  )	shlexr+   r,   r/   rA  r-   r1  r2  rf   )r  r  r0   tokenspersist_globalvalue_tokenstokens          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  r9  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  r9  r  r  s        r!   !_resolve_session_reasoning_configz/GatewayRunner._resolve_session_reasoning_config%  s      +# 	,(:,'+'C'CF'K'K$$ , , ,'+$$$, D"@"EEK	 	3$8I$E$E122**,,,s    --reasoning_configc                     |sdS t          | d          si | _        || j                            |d           dS t          |          | j        |<   dS )z3Set or clear the session-scoped reasoning override.Nr  )r}  r  r  rO   )r)  r9  r  s      r!   _set_session_reasoning_overridez-GatewayRunner._set_session_reasoning_override8  sm      	Ft;<< 	302D-#-11+tDDDDD=ABR=S=SD-k:::rH   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.
        r5   r   Nrs   rt   ru   r   r  r  >   rW  nonenormalr>   standard>   onfastpriorityr"  z#Unknown service_tier '%s', ignoring)r  r  rZ   r  r  r+   r   r,   r
  rf   r  r  r;   r  r  r  r  r#   s         r!   r  z GatewayRunner._load_service_tierG  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   Nrs   rt   ru   r   show_reasoningFr  )r  r  rZ   r  r  rm   r   r
  r  r  r  r  s       r!   r  z"GatewayRunner._load_show_reasoningb  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   r5   r   Nrs   rt   ru   r   r   r  r  r  r  )r6   r  r,   rf   r  r  rZ   r  r  r+   r   r
  )r^  r  r  r  r  s        r!   r  z#GatewayRunner._load_busy_input_modes  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   r5   r   Nrs   rt   ru   r   r   r  z7Invalid restart_drain_timeout '%s', using default %.0fs)r6   r  r,   r  r  rZ   r  r  r+   r   r
  r   r   r*   r:   r-   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_NOTIFICATIONSr5   r   Nrs   rt   ru   r    background_process_notificationsFrW  )Nr5   rV  >   rV  rW  r  r  zBUnknown background_process_notifications '%s', defaulting to 'all')r6   r  r  r  rZ   r  r  r   r+   r
  r,   rf   r  r  )r^  r  r  r  r  r;   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   Nrs   rt   ru   provider_routingr  r  rZ   r  r  r8   r
  r&  s       r!   r  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   Nrs   rt   ru   r  r  r0  )r  r  r  r  r   s        r!   r  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 rc  )_AGENT_PENDING_SENTINEL)re  r9  r   s      r!   
<dictcomp>z:GatewayRunner._snapshot_running_agents.<locals>.<dictcomp>  s4     
 
 
"U333 333rH   )r  r[  r  s    r!   _snapshot_running_agentsz&GatewayRunner._snapshot_running_agents  s2    
 
&*&:&@&@&B&B
 
 
 	
rH   r3  c                     | j                             |j        j                  }|sd S t	          |j        ||           d S r  )r  r8   r  r{  r   r  )r)  r9  r3  r<  s       r!   _queue_or_replace_pending_eventz-GatewayRunner._queue_or_replace_pending_event  sD    -##EL$9:: 	F#G$={ERRRRRrH   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  rV  reply_tometadataFr  r5   z'Gateway steer failed for session %s: %sr  r  r   truez"Busy ack suppressed for session %sr  r   api_call_countmax_iterationscurrent_tool<    min elapsed
iteration rj  	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

rs   z.Failed to apply busy-input onboarding hint: %szFailed to send busy-ack: %s)0_is_user_authorizedr  r  r  user_id	user_namer{  r#   r  r  r8   r  r  r8  r  _send_with_retryr  
message_idr  r  r0   r,   r4  r}  r(   r  r
  r   r  r  r6   r7   rf   r  rD   r  get_activity_summaryr  r)   r1  r2  agent.onboardingrK  rL  rM  rN  r  r  )"r)  r3  r9  r<  thread_metar>  running_agenteffective_modesteered
steer_text	can_steerr  is_queue_modeis_steer_moder   _BUSY_ACK_COOLDOWNrA   last_ackstatus_partssummary	iterationmax_iterrC  start_tselapsed_minstatus_detailrK  rL  rM  rN  	_user_cfg
_hint_mode_onb_errr,  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)r  get_running_looprD   r  r  )r   rA   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!!$ >[=ZrH   Tr   r   皙?F)r6  r  r(   r  r  rp  rD   sleep)r)  r}   snapshotrt  deadline	timed_outrr  rs  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((((""rH   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[  r4  r  r  r  r
  )r)  r  r9  r   r,  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   |                                  }|sdS | j        rdnd}| j        rdnd}d| d| }t                      }|D ]}d}	 t          | dd          M| j                                         | j        j                            |          }|rt          |d	d          nd}n3# t          $ r&}	t          
                    d
||	           Y d}	~	nd}	~	ww xY w||j        j        }
|j        }|j        }n7t          |          }|s|d         }
|d         }|                    d          }|
|f}||v r	 t!          |
          }| j                            |          }|s#|rd|ind}|                    |||           d{V  |                    |           t                              d|
|           |# t          $ r(}	t          
                    d|
||	           Y d}	~	d}	~	ww xY wdS )u&  Send a notification to every chat with an active agent.

        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.
        Nr  r  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     — r  originz>Failed to load session origin for shutdown notification %s: %sr{  r  r  r?  z#Sent shutdown notification to %s:%sz1Failed to send shutdown notification to %s:%s: %s)r6  r  r  r,  r  _ensure_loaded_entriesr8   r
  r  r  r{  r#   r  r  r  r   r  sendrl  r  )r)  activeactionhintrP   notifiedr9  r  r"  r,  platform_strr  r  _parsed	dedup_keyr{  r<  r?  s                     r!   #_notify_active_sessions_of_shutdownz1GatewayRunner._notify_active_sessions_of_shutdown  s      ..00 	F!%!8Mo &: X X :	 	 433T33! 6	 6	KF
4$77C&55777 .7;;KHHE?DNWUHd;;;$F   T        !%4 .",		 -[99 &z2!),#KK44	 &w/IH$$#L11-++H55  8AJK33dll7C(lCCCCCCCCCY'''9 '       G '1       e6	 6	s8   AB..
C8CC?+G,AG
G7G22G7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_agents 	  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,  r&   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rI  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   Nrp   rb  )	r@  r  _STUCK_LOOP_FILErZ   rA  rQ  r
  r8   rj   )r)  r  r@  rY   counts
new_countsr  s          r!   !_increment_restart_failure_countsz/GatewayRunner._increment_restart_failure_countsY	  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 rc  )_STUCK_LOOP_THRESHOLD)re  kr;  r)  s      r!   r8  z>GatewayRunner._suspend_stuck_loop_sessions.<locals>.<listcomp>	  s*    VVVDAqa4;U6U6Ua6U6U6UrH   Trp   u_   Auto-suspended stuck session %s (active across %d consecutive restarts — likely a stuck loop)
missing_ok)r@  r  r  rZ   rA  rQ  r
  r[  r  r  r8   	suspendedr  r  _saveunlink)r)  r@  rY   r  r  
stuck_keysr9  r"  s   `       r!   _suspend_stuck_loop_sessionsz*GatewayRunner._suspend_stuck_loop_sessionst	  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   Nrb  Tr  )	r@  r  r  rZ   rA  rQ  rj   r  r
  )r)  r9  r@  rY   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  r  quoterd  s     r!   rg  zAGatewayRunner._launch_detached_restart_command.<locals>.<genexpr>	  s,      @@Tu{4((@@@@@@rH   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  r6   getpidr2  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!)!)"&	       	*!)!)"&	      rH   detachedvia_servicer  r  c                     j         rdS d _         _         _        d _         d fd}t	          j         |                      } j                            |           |                     j        j	                   dS )NFTr   c                  |   K   t          j        d           d {V                      d            d {V  d S )N皙?Tr  detached_restartservice_restart)r  rw  r  )r  r)  r  s   r!   _run_restartz3GatewayRunner.request_restart.<locals>._run_restart	  sZ      -%%%%%%%%%))D8U`)aaaaaaaaaaarH   r   N)
r  r  r  r  r  create_taskr(  rl  add_done_callbackrm  )r)  r  r  r  tasks   ```  r!   request_restartzGatewayRunner.request_restart	  s    % 	5"&!)$/!%)"	b 	b 	b 	b 	b 	b 	b 	b "<<>>22""4(((t5=>>>trH   c                     | j         r| j        sdS 	 t          | j                  }n# t          $ r Y dS w xY w|dk    rdS || j        dz   k    S )u  Return True if source files on disk are newer than the running process.

        A gateway that survives ``hermes update`` (manual SIGTERM never
        escalated, systemd restart race, detached-process respawn failed,
        etc.) keeps pre-update modules cached in ``sys.modules``.  Later
        imports of names added post-update — e.g. ``cfg_get`` from PR
        #17304 — raise ImportError against the stale module object (see
        Issue #17648).  Detecting this at the source — "the code on disk
        is newer than me" — lets us auto-restart instead of serving
        broken responses until the user notices and runs
        ``hermes gateway restart`` manually.

        Returns False when the boot-time snapshot is unavailable or no
        sentinel file is readable, to avoid false-positive restart loops
        in unusual checkouts (sparse clones, read-only filesystems).
        Fr          @)r  r  r"   r  r
  )r)  rF   s     r!   _detect_stale_codez GatewayRunner._detect_stale_code	  sz    " # 	4+@ 	5	)$*GHHGG 	 	 	55	c>>5
 .444s   ' 
55c                 "   | j         rdS d| _         t                              d| j        t	          | j                             	 |                     dd           dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)u'  Idempotently kick off a graceful restart after stale-code detection.

        Runs at most once per process.  The restart request goes through
        the normal drain path so in-flight agent turns finish before the
        process exits; the service manager (systemd / launchd / detached
        profile watcher) then respawns with fresh code.  On manual
        ``hermes gateway run`` installs without a supervisor, the
        process exits and the user must restart by hand — but they get a
        user-visible message telling them so.
        NTu   Stale-code self-check: source files newer than gateway boot time (boot=%.0f, newest=%.0f) — requesting graceful restart. See Issue #17648.Fr  z%Stale-code restart request failed: %s)	r  r  r  r  r"   r  r  r
  r  )r)  r  s     r!   _trigger_stale_code_restartz)GatewayRunner._trigger_stale_code_restart	
  s     - 	F-1*  ! =>>	
 	
 	
	G  %T BBBBB 	G 	G 	GLL@#FFFFFFFFF	Gs   A 
B(B		Bc           
      b  K   t                               d           t                               d| j        j                   	 t	          t          j        dd                    }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t>          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                               d+|           Y d}~nd}~ww xY wd}d}g }g }| j        j%        &                                D ]\  }}|j'        s|d,z  }| (                    ||          }|sk|j)        }d- tT          j+        ,                                D             }||vrt                               d.|           nt                               d/|           |-                    | j.                   |/                    | j0                   |1                    | j"                   |2                    | j3                   t                               d0|j)                   | 4                    |j)        d1dd2           	 | 5                    ||           d{V }|rd|| j6        |<   | 7                    |           |d,z  }| 4                    |j)        d3dd2           t                               d4|j)                   n*t                               d5|j)                   | 8                    ||           d{V  |j9        r| 4                    |j)        |j:        rd6nd7|j;        |j<        2           |j:        r|n|}|=                    |j)         d8|j<                    |j:        r"|d,t}          j?                    d9z   d:| j@        |<   n]| 4                    |j)        d6dd;2           |=                    |j)         d<           |d,t}          j?                    d9z   d:| j@        |<   # t          $ r}t           A                    d=|j)        |           | 8                    ||           d{V  | 4                    |j)        d6dt          |          2           |=                    |j)         d8|            |d,t}          j?                    d9z   d:| j@        |<   Y d}~d}~ww xY w|dk    r|rld>C                    |          } t           A                    d?|            	 dd
l
m}  |d@|            n# t          $ r Y nw xY w| D                    |            dS |dk    rYd>C                    |          pdA} t           A                    dB|            	 dd
l
m}  |d@|            n# t          $ r Y nw xY wd S t                               dC           t                               dD           | j6        | jE        _6        d| _F        | G                    dE           t          | j        jI                  }!|!rt                               dF|!           | j        J                    dGdHdI | j6        K                                D             i           d{V  |dk    rt                               dJ|           	 ddKlLmM}"  |"| j6                   d{V }#t          dL |#O                    dHi           ,                                D                       }$t                               dM|$           n2# t          $ r%}t                               dN|           Y d}~nd}~ww xY w| P                                 d{V }%|%s?t!          dO t>          dPz  t>          dQz  fD                       r| Q                                 | R                                 d{V  	 dd#lm} |jS        rv|jS        T                    d          }&t          jV        | W                    |&                     t                               dR|&O                    dS                     |jS        vn2# t          $ r%}t           A                    dT|           Y d}~nd}~ww xY wt          jV        | X                                           t          jV        | Y                                           t          jV        | Z                                           | j@        rPt                               dUt          | j@                  dVC                    dW | j@        D                                  t          jV        | [                                           t                               dX           dS )Yz
        Start the gateway and all configured platform adapters.
        
        Returns True if at least one adapter connected successfully.
        zStarting Hermes Gateway...zSession storage: %sr   90zuAgent budget: max_iterations=%d (agent.max_turns from config.yaml, or HERMES_MAX_ITERATIONS from .env, or default 90)r   get_active_profile_namer>   zActive profile: %sr  startingN)r  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_USERSrc  platform_registryc              3   2   K   | ]}|j         	|j         V  d S r  )allowed_users_envre  r,  s     r!   rg  z&GatewayRunner.start.<locals>.<genexpr>l
  sB       ) )()&)#) ) ) ) ) )rH   c              3   2   K   | ]}|j         	|j         V  d S r  )allow_all_envr	  s     r!   rg  z&GatewayRunner.start.<locals>.<genexpr>p
  s@       + +$%?++ + + + + +rH   c              3   >   K   | ]}t          j        |          V  d S r  )r6   r  r:  s     r!   rg  z&GatewayRunner.start.<locals>.<genexpr>v
  s;       
 
BIaLL
 
 
 
 
 
rH   GATEWAY_ALLOW_ALL_USERSr5   r@  r   yesc              3   h   K   | ]-}t          j        |d                                           dv V  .dS )r5   r  N)r6   r  rf   r:  s     r!   rg  z&GatewayRunner.start.<locals>.<genexpr>y
  s\       e
 e
 Ia""$$(<<e
 e
 e
 e
 e
 e
rH   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 startupTr<  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: %srp   c                     h | ]	}|j         
S rc  rS  re  ms     r!   	<setcomp>z&GatewayRunner.start.<locals>.<setcomp>
  s    !Q!Q!Qa!'!Q!Q!QrH   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  rF  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 rc  rS  r6  s     r!   r8  z'GatewayRunner.start.<locals>.<listcomp>q  s    @@@a!'@@@rH   z#Gateway running with %s platform(s)build_channel_directoryc              3   4   K   | ]}t          |          V  d S r  r.  )re  chss     r!   rg  z&GatewayRunner.start.<locals>.<genexpr>{  s(      WW3s88WWWWWWrH   z%Channel directory built: %d target(s)z"Channel directory build failed: %sc              3   >   K   | ]}|                                 V  d S r  )rZ   )re  rY   s     r!   rg  z&GatewayRunner.start.<locals>.<genexpr>  s>        
  
 KKMM 
  
  
  
  
  
rH   .update_pending.json.update_pending.claimed.jsonz(Resumed watcher for recovered process %sr  z!Recovered watcher setup error: %sz;Starting reconnection watcher for %d failed platform(s): %srI  c              3   $   K   | ]}|j         V  d S r  rS  r6  s     r!   rg  z&GatewayRunner.start.<locals>.<genexpr>  s$      BBa!'BBBBBBrH   zPress Ctrl+C to stop)\r  r  r  r  r)   r6   r  r
  hermes_cli.profilesr  r  r  gateway.platform_registryr  r  plugin_entriesro  rf   r  r  r  r  r  r  agent.shell_hooksr  r%  discover_and_loadr  r  recover_from_checkpointr  rZ   r  r  suspend_recently_activer  r  r[  rq  _create_adapterr#   r   __members__r  set_message_handler_handle_messageset_fatal_error_handlerr  set_session_storeset_busy_session_handlerrk  r  r  r  r  r  has_fatal_errorr  r  r  r1  rD   r  r  r  r+   r2  r  r  r  r  r.  loaded_hooksemitr  gateway.channel_directoryr  sumr8   _send_update_notification#_schedule_update_notification_watch_send_restart_notificationpending_watchersr  r  r  _run_process_watcher_session_expiry_watcher_kanban_notifier_watcher_kanban_dispatcher_watcher_platform_reconnect_watcher)'r)  _effective_max_iterr  _profiler  _builtin_allowed_vars_builtin_allow_all_vars_plugin_allowed_vars_plugin_allow_all_varsr  _any_allowlist
_allow_allr  r  r  r  	recoveredr,  _clean_markerr  stuckconnected_countenabled_platform_countstartup_nonretryable_errorsstartup_retryable_errorsr{  r  r<  _pval_builtin_namessuccesstargetr  
hook_countr  	directorych_countr  watchers'                                          r!   startzGatewayRunner.start#
  s      	0111)4;+CDDD	"%bi0G&N&N"O"OKKE#   
  	 	 	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 --/////////	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<<>>???*+++ts7  =B 
BB3C 
CCC+ +
C87C8AE! !
E.-E.1H &H+*H+/!I &I:9I:7K 
K>K99K>:M 
MM 6N 
O!OO
1O< <
P+P&&P+F,\??
_=	B)_88_=:a 
aa.c 
ccA6i 
i7i22i70Bm4 4
n#>nn#,  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.
        rD  NrI  rJ  rK  r  r   rp   rI  c              3   *   K   | ]\  }}| d | V  dS )rJ  Nrc  )re  r7  cs      r!   rg  z8GatewayRunner._session_expiry_watcher.<locals>.<genexpr>  sA       . .'+q!1

q

. . . . . .rH   z,Session expiry: %d sessions to finalize (%s)r  r5   r  r  r  TzSession expiry finalized for %szkSession finalize gave up after %d attempts for %s: %s. Marking as finalized to prevent infinite retry loop.z*Session finalize failed (%d/%d) for %s: %sc              3   .   K   | ]\  }}|j         d V  dS )rp   N)expiry_finalized)re  rP  r,  s      r!   rg  z8GatewayRunner._session_expiry_watcher.<locals>.<genexpr>  sB          "a!:L            rH   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)+r  rw  r  r  r  r  r  r[  r`  _is_session_expiredr1  rA  r.  r8   r2  sortedr  r  r  r  r  r
  r,  r  r&   r  r  r4  r  _evict_cached_agent_lockr  r  r  r  r8  _sweep_idle_cached_agentsrD   r)   r  prune_old_entriesra  range)r)  r[  _finalize_failures_MAX_FINALIZE_RETRIES_expired_entriesr  r"  
_platforms_k_e_parts_plat_plat_summaryr  	_platform_cached_agent_cache_lock_cachedr,  failures_done_failed_idle_evicted_last_prune_ts_prune_interval_max_age_prunedrP  s                               r!   r>  z%GatewayRunner._session_expiry_watcher  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        rR	 #$fd
}t          j        |           d{V }|D ]}|d         }	|d         }
|	d         pd                                }	  ||          }n7# t          $ r* t          j        | j        |	|d                    d{V  Y ow xY w| j                            |          }||
r|
j        n|	d         dd         }|d         D ]u}|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.         ||/           d{V  |                    |d           # t          $ r}|                    |d          d0z   }|||<   t
                              d1|	d         ||||           ||k    rYt
                              d2|	d         ||           t          j        | j        |	           d{V  |                    |d           Y d}~ nd}~ww xY wt          j        | j        |	|d                    d{V  |d         r|d         d3         j        nd}|
o|
j        d4v } ||v }!| s|!r t          j        | j        |	           d{V  n2# t          $ r%}t
                              d5|           Y d}~nd}~ww xY wt?          t5          tA          d0|                              D ]&}"| j        s dS t          j
        d0           d{V  '| j        PdS dS )6a  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.
        r   r   	kanban_dbz<kanban notifier: kanban_db not importable; notifier disabledN)	completedblockedgave_upcrashedrz  rI  _kanban_sub_fail_countsr  c            
                                          } 	                                  n# t          $ r Y nw xY w	                     |           }g }|D ]}                    | |d         |d         |d         |                    d          pd          \  }}|sJ                    | |d                   }|                    ||||d           ||                                  S # |                                  w xY w)Ntask_idr{  r  r  r5   )r  r{  r  r  kinds)rU  cursoreventsr  )	r  init_dbr
  list_notify_subsunseen_events_for_subr8   get_taskr1  r  )	connsubs
deliveriesrU  r  r  r  TERMINAL_KINDS_kbs	          r!   _collectz8GatewayRunner._kanban_notifier_watcher.<locals>._collect  s;   ;;==D$   %"33D9913
#'  C-0-F-F $(+I),Z(+I*-''+*>*>*D"&4 .G . .NFF $* ) (#&<<c)n#E#ED&--'**0*0(,	/ /      *



s   , 
99BC- -DrU  r  r{  r5   r  r  x   r  @r@  r  rb     r+     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 retryrz  limit_secondsu   ⏱ z timed out (max_runtime=zs); will retryr  r  r  rp   z=kanban notifier: send failed for %s on %s (attempt %d/%d): %szRkanban notifier: dropping subscription %s on %s after %d consecutive send failuresrL  )donearchivedzkanban notifier tick failed: %s)!gateway.configr   r  r  r
  r  r  r,  r  r  rw  r  	to_threadrf   r-   _kanban_advancer  r8   titlekindassigneepayloadr+   r,   rS  r  r)   r  r  _kanban_unsubstatusri  r  )%r)  r[  	_PlatformTERMINAL_EVENT_KINDSMAX_SEND_FAILURESsub_fail_countsr  r  drU  r  r  platr<  r  evr  whotaghandoffpayload_summaryhr  rP   r  errlimitr?  sub_keyr  fails	last_kindtask_terminalevent_terminalrP  r  r  s%                                      @@r!   r?  z&GatewayRunner._kanban_notifier_watcherV  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 a	'[G% % % % % %> $+#4X#>#>>>>>>>
# x xAE(CV9D$'
O$9r#@#@#B#BL!(y66% ! ! ! &/ 0#q{         !! #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8JC&P&P P P P P P P P / 3 3GT B B B!EEEEE%"* &/ 0#q{         =>hK$QAhKO$8$8T	(,(T@T1T)26J)J( N ")"3 $ 2C# #       mxr  G G G@#FFFFFFFFG 3s1h//0011 ' '} FFmA&&&&&&&&&&C m a	' a	' a	' a	' a	'sv    $;;?AU CU 1DU DKU :PU 
SB"S;U SA<U 
U2U--U2rU  r  c           	         ddl m} |                                }	 |                    ||d         |d         |d         |                    d          pd|           |                                 d	S # |                                 w xY w)
z@Sync helper: advance a subscription's cursor. Runs in to_thread.r   r  r  r{  r  r  r5   )r  r{  r  r  
new_cursorN)r  r  r  advance_notify_cursorr8   r  )r)  rU  r  r  r  s        r!   r  zGatewayRunner._kanban_advance#  s    //////{{}}
	%%IZI''+..4"! &    JJLLLLLDJJLLLLs   AA4 4B
c           	         ddl m} |                                }	 |                    ||d         |d         |d         |                    d          pd           |                                 d S # |                                 w xY w)	Nr   r  r  r{  r  r  r5   )r  r{  r  r  )r  r  r  remove_notify_subr8   r  )r)  rU  r  r  s       r!   r  zGatewayRunner._kanban_unsub3  s    //////{{}}		!!IZI''+..4" "    JJLLLLLDJJLLLLs   AA3 3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}t!          j        d           d{V  d}d}d}	d#fd}
dt$          ffd}t          
                    d|           | j        r!	 t!          j        |
           d{V }|t+          |dd          rt          
                    dt-          |j                  |j        t3          |j        d          rt-          |j                  ndt3          |j        d          rt-          |j                  nd|j        t3          |j        d          rt-          |j                  nd           t!          j        |           d{V }t%          |ot+          |dd                    }|r|s|dz  }nd}||k    rFt=          t?          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        tG          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_GATEWAYr5   )0falsenorW  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_secondsrD  rn  r     r   Optional[object]c                     d} 	                                  } 	                                  n# t          $ r Y nw xY w                    |           | &	 |                                  S # t          $ r Y S w xY wS # t          $ rH 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)ztRun one dispatch_once; return result or None on error.

            Runs in a worker thread via `asyncio.to_thread`.Nzkanban dispatcher: tick failed)r  r  r
  dispatch_oncer  r  	exception)r  r  s    r!   
_tick_oncez<GatewayRunner._kanban_dispatcher_watcher.<locals>._tick_once  sb    D{{}}KKMMMM    D((..
 #

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

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

$    $s   A< / A< 
<A< <A< A++
A87A8<$C C $B: :
CCCC C;C*)C;*
C74C;6C77C;c                     d} 	                                  } |                     d                                          }|du| &	 |                                  S # t          $ r Y S w xY wS # t          $ r. 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)zACheap probe: is there at least one ready+assigned+unclaimed task?NzfSELECT 1 FROM tasks WHERE status = 'ready' AND assignee IS NOT NULL     AND claim_lock IS NULL LIMIT 1F)r  executefetchoner  r
  )r  rowr  s     r!   _ready_nonemptyzAGatewayRunner._kanban_dispatcher_watcher.<locals>._ready_nonempty  s5   D{{}}ll9  (**	 
 $ #

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

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

$    $sk   >A, A
A('A(,
B$6B' :B 
BB#B$$B' 'C+C ?C 
C
CCCz7kanban dispatcher: embedded in gateway (interval=%.1fs)spawnedzckanban dispatcher: tick spawned=%d reclaimed=%d crashed=%d timed_out=%d promoted=%d auto_blocked=%d__len__rp   rZ  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   r  )$r  r  r
  r  r  r6   r7   r8   r,   rf   r  r&   rO   r  r  r*   r  rw  r(   r  r  r,  r.  r  	reclaimedr}  r  rz  promotedauto_blockedr)   rD   CancelledErrorr  r  min)r)  _load_configenv_overrider  r  
kanban_cfgr[  HEALTH_WINDOW	bad_tickslast_warn_atr  r  resready_pendingspawned_anyrA   sleptr  s                    @r!   r@  z(GatewayRunner._kanban_dispatcher_watcherA  s     ,	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
 mA
 		 	 	 	 	 	,	 	 	 	 	 	 	( 	Ex	
 	
 	
 m -	%P#-j99999999?wsIt'D'D? KKNCK((,3CK,K,KRCK(((QR.5cmY.O.OVCM***UV189I91U1U\C,---[\	 	 	 '.&7&H&H H H H H H H"3#H73	4+H+HII  " "NII !I--dikk**C\)S00C &   (+)   ;<<< P P P  !NOOOOOP
 E(""t}"mCX-=$>$>????????? (""t}"W m -	 -	 -	 -	 -	sJ    $44
B' '
C1CC:E $E)(E)0E6M' 'AN76N7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).
           rZ  
   Nr  rp   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 queuerF  r  u   ✓ %s reconnected successfullyr   r  r  zAReconnect %s: non-retryable error (%s), removing from retry queuer  zfailed to reconnectrK  z&Reconnect %s failed, next retry in %dsz)Reconnect %s error: %s, next retry in %ds)$r  rw  r  r  ri  rD   r  r  r  r  r  r#   r  r-  r/  r0  r1  r  r2  r  r3  rk  r  r  r  r  r  r7  r  r
  r4  r  r  r  r  r+   )r)  _MAX_ATTEMPTS_BACKOFF_CAPrP  rA   r{  r  r  attemptr<  rS  r  backoffr,  s                 r!   rA  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&r  r  r  r  c                    K   |rd _         | _        | _         j         j         d{V  dS d fd}t	          j         |                       _         j         d{V  dS )z-Stop the gateway and disconnect all adapters.TNr   c                  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)r  r  kill_allr  r  r
  r  tools.terminal_toolr  tools.browser_toolr  )r  r  _killedro  r  r  s         r!   _kill_tool_subprocesseszGGatewayRunner.stop.<locals>._stop_impl.<locals>._kill_tool_subprocesseso  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 restartr5   FTzYGateway drain timed out after %.1fs with %d active agent(s); interrupting remaining work.restart_timeoutshutdown_timeoutz%mark_resume_pending failed for %s: %sr  ro  ru  zpost-interruptz-Failed to launch detached gateway restart: %sr  r  r   u'   ✗ %s background-task cancel error: %su   ✓ %s disconnectedu   ✗ %s disconnect error: %sr  zfinal-cleanup)shutdown_cached_clientsz!shutdown_cached_clients error: %sr  _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  r  r  r  r  r  r  r{  r  r  r  r  r[  r4  r  mark_resume_pendingr
  r  r}  !_INTERRUPT_REASON_GATEWAY_RESTART"_INTERRUPT_REASON_GATEWAY_SHUTDOWNr  rp  rD   r  rw  r  r  r  r  r,  r  r~  r&   r  r  r  cancel_background_tasksr#   r  r(  r  cancelr  r  r  r}  r  r	  r  r  r  r  r  r  r  r  touchr  r  r  r   r  r  )r  r}   r  rz  _resume_reason_sk_agentro  interrupt_deadliner,  ru  _cache_idle_agents_entryr{  r<  _taskr  
_db_holderr  r  r  r)  s                         r!   
_stop_implz&GatewayRunner.stop.<locals>._stop_impln  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)r  )r  r  r  r  r  r  )r)  r  r  r  r  s   `    r!   r  zGatewayRunner.stop^  s        	8&*D#%5D"(7D%?&/!!!!!!!Fg	+ g	+ g	+ g	+ g	+ g	+R "-jjll;;orH   c                 H   K   | j                                          d{V  dS )zWait for shutdown signal.N)r	  waitr  s    r!   wait_for_shutdownzGatewayRunner.wait_for_shutdownZ  s3      "'')))))))))))rH   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}  r&   r  rO   r  r  r  r,  r'  r  is_registeredr#   create_adapterr  r  r
  r  r   TELEGRAM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/  r4  gateway.platforms.api_serverr0  r1  r5  gateway.platforms.webhookr2  r3  BLUEBUBBLESgateway.platforms.bluebubblesr4  r5  QQBOTgateway.platforms.qqbotr6  r7  YUANBAOgateway.platforms.yuanbaor8  r9  ).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/  r0  r1  r2  r3  r4  r5  r6  r7  r8  r9  s.                                                 r!   r-  zGatewayRunner._create_adapter^  s    67## 	
6<(F(F 	L##)3   L##*%?GG  	\CCCCCC ..x~>> +::8>6RR&"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
        TFr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  QQ_GROUP_ALLOWED_USERSr  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  DISCORD_ALLOW_BOTSFEISHU_ALLOW_BOTSr   r  r5   r  is_botr  )mentionsrV  DISCORD_ALLOWED_ROLES>   forumrD  r  r  c                 ^    h | ]*}|                                 |                                 +S rc  r,   )re  r  s     r!   r  z4GatewayRunner._is_user_authorized.<locals>.<setcomp>  sB     ! ! !$+RYR_R_RaRa!! ! !rH   ,*c                     h | ]=}|                                                     d           )|                                 >S )rO  )r,   r/  r:  s     r!   r  z4GatewayRunner._is_user_authorized.<locals>.<setcomp>  sL       7799'',,		  rH   #_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  rl  re  uids     r!   rg  z4GatewayRunner._is_user_authorized.<locals>.<genexpr>  s<      ccsWZW`W`WbWbcsyy{{ccccccrH   c              3   f   K   | ],}|                                 |                                 V  -d S r  rl  rr  s     r!   rg  z4GatewayRunner._is_user_authorized.<locals>.<genexpr>  s<      eesY\YbYbYdYdesyy{{eeeeeerH   c              3   f   K   | ],}|                                 |                                 V  -d S r  rl  rr  s     r!   rg  z4GatewayRunner._is_user_authorized.<locals>.<genexpr>  s<      aasUXU^U^U`U`asyy{{aaaaaarH   r  )2r{  r   rG  r5  rQ  r<  r>  rA  rC  rE  rI  rK  rW  rY  rM  rO  rS  rQ  rU  r]  r_  ra  r'  r  r8   r#   r  r  r
  r6   r  rf   r,  r,   r#  is_approvedr  r  rA  r  r  r2  rd  rp  r  r  rl  _expand_whatsapp_auth_aliases_normalize_whatsapp_identifierr(   )r)  r  rQ  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_varra   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!   rP  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  pairr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  )r  r  )rd  r5   ignorerc  r  )r,  r}  r  r8   r  r  r   r<  r>  rA  rC  rE  rI  rK  rW  rY  rM  rO  rS  rQ  rU  r]  r_  r6   r  r,   )r)  r{  r  platform_cfgry  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vrH   rV  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  privaterQ  r  rS  Fz7[%s] send_private_notice failed, falling back to publicr{  r  Tr<  )r  r8   r{  r,  r}  r  r  send_private_noticer  rQ  r
  r  r  r  )r)  r  rV  r<  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                 A  K   |j         }	 |                                 r|                                  	 dS n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w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           }|                    |          r|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|}|rtP          d$z  }	 |)                    d%          }|*                    |           |                    |           n7# tV          $ r*}t                              d&|           d'| cY d}~S d}~ww xY w|,                    |d           t[          |          d(k    r|n|dd(         d)z   }d*| d+S |rtP          d$z  }	 |)                    d%          }|*                    d           |                    |           t                              d,||           n2# tV          $ r%}t                              d-|           Y d}~nd}~ww xY w|,                    |d           dd.l.m/} |0                    |          }d}	 dd/l1m2}  ||          }n# t          $ r d}Y nw xY w|r|s|j"        pd#                                }|$                                }d} |d0v rd1} nX|d2v rd3} nQ|d4v rd5} nJ|3                                d6v rd1} n1|3                                d7v rd3} n|3                                d8v rd5} | 4|4                    ||                    d9          |            d{V }!|!pdS |5                    |           tm          d:d;          }"| j7                            |d          }#|| j8        v rz|#rwts          j9                    |#z
  }$| j8                            |          }%tu          d<          }&d}'|%rtw          |%d=          r	 |%<                                }(|(                    d>tu          d<                    }&d?|(                    d@d           dA|&dBdC|(                    dDd           dE|(                    dFd           }'n# t          $ r Y nw xY w|"dk    rt{          |"dGz  dH          ntu          d<          })|%t|          uo|"dk    r|&|"k    p|$|)k    }*|*rKt                              dI||$|&|"|'           | ?                    |dJK           | @                    |           || j8        v 
r2|$                                dLk    r| A                    |           d{V S ddMl%mB}+m&}, |$                                }-|-r |,|-          nd}.|.r&|.j'        dNk    r| C                    |           d{V S |.rY|.j'        dOk    rN| D                    ||t          dPQ           d{V  t                              dR|           t          dS          S |.rJ|.j'        dTk    r?| D                    ||t          dUQ           d{V  | H                    |           d{V S |$                                dVv r|I                                #                                }/|/sdWS | j                            |j                  }|rDt          |/t          jL        |j         |jM        |jN        X          }0| O                    ||0|           | P                    || j                            |j                  Y          }1|1dZk    rd[S d\|1 d]S |.r|.j'        d^k    r|I                                #                                }2|2sd_S | j8                            |          }3|3t|          u rZ| j                            |j                  }|r7t          |2t          jL        |j         |jM        |jN        X          }0|0|jQ        |<   d`S |3rtw          |3d^          r{	 |3R                    |2          }4n8# t          $ r+}5t                              da||5           db|5 cY d}5~5S d}5~5ww xY w|4r(|2ddc         t[          |2          dck    rddndz   }6de|6 dfS dgS | j                            |j                  }|r7t          |2t          jL        |j         |jM        |jN        X          }0|0|jQ        |<   dhS |.r|.j'        dik    rdjS |.rJ|.j'        dkv rA|.j'        dlk    r| S                    |           d{V S | T                    |           d{V S |.r&|.j'        dmk    r| U                    |           d{V S |.r&|.j'        dnk    r| V                    |           d{V S |.r&|.j'        dok    r| W                    |           d{V S |.rh|.j'        dpk    r]|I                                pd#                                3                                }7|7r|7dqv r| X                    |           d{V S drS |.r{|.j'        dsv rr|.j'        dtk    r| Y                    |           d{V S |.j'        duk    r| Z                    |           d{V S |.j'        dvk    r| [                    |           d{V S |.r|.j'        |+v r|.j'        dwk    r| \                    |           d{V S |.j'        dxk    r| ]                    |           d{V S |.j'        dyk    r| ^                    |           d{V S |.j'        dzk    r| _                    |           d{V S |.rd{|.j'         d|S |j`        t          ja        k    rTt                              d}|           | j                            |j                  }|rt          |jQ        ||           dS tu          t          jd        d~d                    }8| j7                            |d          }9|j        t          jf        k    r|j`        t          jL        k    r|8dk    r|9rts          j9                    |9z
  |8k    rkt                              dts          j9                    |9z
  |           | j                            |j                  }|rt          |jQ        ||d           dS | j8                            |          }3|3t|          u r|$                                dOk    r?| @                    |           t                              d|           t          d          S | j                            |j                  }|rt          |jQ        ||d           dS | jg        rn| h                                r| i                    ||           | h                                rd| j                                 dnd| j                                 dS | jk        dk    r3t                              d|           | i                    ||           dS | jk        d^k    r|j"        pd#                                }2d}:|2ritw          |3d^          rY	 t          |3R                    |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|           | i                    ||           dS t                              d|           |3l                    |j"                   || jQ        v r| jQ        |xx         d|j"        z   z  cc<   n|j"        | jQ        |<   dS |$                                };ddl%mm}<mn}=m&}> |;r |>|;          nd}|r|j'        n|;}?|;rg |=|?          r[|I                                #                                }@|j        r|j        j        nd|j        |?|;|@|@d}A	 | jo        p                    d|? |A           d{V }Bn5# t          $ r(}Ct                              d|?|C           g }BY d}C~Cnd}C~Cww xY w|BD ]}Dt          |Dt                    st'          |D                    dd                    #                                3                                }E|Er|Edk    ri|Edk    r8|D                    d          }Ft          |Ft&                    r|Fr|Fc S d|; dc S |Edk    r2|D                    d          }Ft          |Ft&                    r|Fr|Fndc S |Edk    rt'          |D                    dd                    #                                q                    dE          }G|Gs1t'          |D                    dd                    #                                }HdE|G d|H #                                |_"        |$                                };|;r |>|;          nd}|r|j'        n|;}? n|?dTk    r| H                    |           d{V S |?dwk    r| \                    |           d{V S |?dxk    r| ]                    |           d{V S |?dyk    r| ^                    |           d{V S |?dLk    r| A                    |           d{V S |?dmk    r| U                    |           d{V S |?dNk    r| C                    |           d{V S |?dOk    r| r                    |           d{V S |?dk    r| s                    |           d{V S |?dk    r| t                    |           d{V S |?duk    r| Z                    |           d{V S |?dvk    r| [                    |           d{V S |?dtk    r| Y                    |           d{V S |?dik    r| u                    |           d{V S |?dk    r| v                    |           d{V S |?dok    r| W                    |           d{V S |?dk    r| w                    |           d{V S |?dk    r| x                    |           d{V S |?dk    r| y                    |           d{V S |?dk    r| z                    |           d{V S |?dk    r| {                    |           d{V S |?dk    r| |                    |           d{V S |?dk    r| }                    |           d{V S |?dk    r| ~                    |           d{V S |?dlk    r| S                    |           d{V S |?dk    r| T                    |           d{V S |?dzk    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 |?dnk    r| V                    |           d{V S |?d^k    rC|I                                #                                }I|IsdS 	 |I|_"        n# t          $ r Y nw xY w|?dpk    r| X                    |           d{V S |?dk    r|                     |           d{V S | jg        rd| j                                 dS |;r?t          | j        t                    r| j                            di           pi }Jnt          | j        di           pi }Jt          |Jt                    si }J|;|Jv r|J|;         }K|K                    d          dk    r|K                    dd          }L|Lr	 t          j        |Lt          j        j        t          j        j                   d{V }Mt          j        |M                                d           d{V \  }N}O|Np|O                                #                                }P|Pr|PndS # t          j        $ r Y dS t          $ r}d| cY d}~S d}~ww xY wd|; dS |K                    d          dk    r|K                    dd          #                                }Q|Qrx|Q                    dE          r|QndE|Q }Q|Qq                    dE          }R|I                                #                                }S|Q d|S #                                |_"        |R};nd|; dS d|; dS |;r	 ddlm}T  |T|;                    dd                    }U|Ura|I                                #                                }S |U|S          }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 |I                                #                                }_ |X|[|_|ʦ          }`|`r|`|_"        nat/          |;          }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|          | j8        |<   ts          j9                    | j7        |<   |                     |          }b	 |                     ||||b           d{V }c	 d}dt          |ct                    r%t'          |c                    dЦ          pd          }dnt          |ct&                    r|c}d|d#                                rH	 | j
                            |          }en# t          $ r d}eY nw xY w|e|                     |e||dѦ           n2# t          $ r%}ft                              d|f           Y d}f~fnd}f~fww xY w|c| j8                            |          t|          u r| @                    |           S | j7        ,                    |d           tw          | dӦ          r| j        ,                    |d           S S # | j8                            |          t|          u r| @                    |           w | j7        ,                    |d           tw          | 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
        u   ⟳ Gateway code was updated in the background — restarting this gateway so your next message runs on the new code. Please retry in a moment.z Stale-code self-check failed: %sNinternalFr   r  pre_gateway_dispatch)r3  r  r  z*pre_gateway_dispatch invocation failed: %sr  skipz8pre_gateway_dispatch skip: reason=%s platform=%s chat=%sr  r  rewriter0   r0   allowz(Ignoring message with no user_id from %sz Unauthorized user: %s (%s) on %sr  r  r5   z;Hi~ I don't recognize you yet!

Here's your pairing code: `z5`

Ask the bot owner to run:
`hermes pairing approve r@  rk  z<Too many pairing requests right now~ Please try again later!r  )approver  y)denyr  nresolve_command.update_response.tmpz#Failed to write update response: %su/   ✗ Failed to send response to update process: r     …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  r  okconfirmonce)alwaysrememberr  )r  r  r  	nevermindr  )r  zapprove oncer  )r  zalways approve)r  r  r  
confirm_idr     infrU  seconds_since_activityz | last_activity=last_activity_descrH  .0fzs ago) | iteration=rA  rj  rB  r  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)r  qzUsage: /queue <prompt>r0   r(  r  rT  channel_promptr  rp   zQueued for the next turn.zQueued for the next turn. (z queued)r  zUsage: /steer <prompt>u9   Agent still starting — /steer queued for the next turn.zSteer failed for session %s: %su   ⚠️ Steer failed: rD  ...u8   ⏩ Steer queued — arrives after the next tool call: 'rN  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
backgroundr  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verboser  r  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 interruptT)
merge_textu7   HARD STOP (pending) for session %s — sentinel cleareduE   ⚡ Force-stopped. The agent was still starting — session unlocked.r:  r;  r<  r=  r  z'PRIORITY queue follow-up for session %sz(PRIORITY steer failed for session %s: %szPRIORITY steer for session %sz/PRIORITY steer-fallback-to-queue for session %sz!PRIORITY interrupt for session %sr+  )GATEWAY_KNOWN_COMMANDSis_gateway_known_commandr  )r{  rQ  r   raw_commandr   r  zcommand:z/command:%s hook dispatch failed (non-fatal): %sdecisionr  r>  z
Command `/z` was blocked by a hook.handledr]  r  	reasoningr!  personalityretryundosethomecompressusageinsights
reload-mcpzreload-skillsr  r  r  branchrollbackzJUsage: /steer <prompt>  (no agent is running; sending as a normal message)ru  z) and is not accepting new work right now.quick_commandsr  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.aliasrT  z' has no target defined.z4' has unsupported type (supported: 'exec', 'alias').)get_plugin_command_handlerrP  rO  z.Plugin command dispatch failed (non-fatal): %s)get_skill_commandsbuild_skill_invocation_messageresolve_skill_command_keyr=   )get_disabled_skill_namesr  rh  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)session_entryr  r  z!goal continuation hook failed: %sr  )r  r  r  r
  r  r  r(   r,  r  r  r  r  r&   rO   r8   r  r{  r#   r  r+   dataclassesr/   rQ  rP  rR  r  r  r#  _is_rate_limitedgenerate_coder  r  _record_rate_limitr  r0   r,   get_commandhermes_cli.commandsr  r=   r  with_suffixre  r   r  r.  toolsr  get_pendingtools.approvalr  rf   rr  clear_if_staler@   r  r  rD   r*   r}  rU  r  r4  "_invalidate_session_run_generation_release_running_agent_state_handle_status_commandr  _handle_restart_command_interrupt_and_clear_session_INTERRUPT_REASON_STOPr   _INTERRUPT_REASON_RESET_handle_reset_commandget_command_argsr   r   TEXTrT  r  r  r  r  r  _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_commandr(  r0  r   r6   r  r   r<  r  r  r8  r  r  r  r  r  r%  emit_collectlstrip_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_commandr  r  create_subprocess_shellr  PIPEr  communicatedecoder  r/  r  iscoroutineagent.skill_commandsr  r  r  rm  r  rz  _begin_session_run_generation_handle_message_with_agentget_or_create_session_post_turn_goal_continuationr  )gr)  r3  r  
_stale_excis_internalr  _hook_results	_hook_exc_result_action	_new_textra   coder<  
_quick_key_update_promptsr;   r  response_text_recognized_cmd_resolve_update_cmd_cmd_defresponse_pathtmpr,  label_slash_confirm_mod_pending_confirm_tool_approval_liver  
_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_textr  r  r[  rX  acceptedr  preview	_goal_arg_telegram_followup_grace_started_atrZ  r   r  r  _resolve_cmd	canonicalr  hook_ctxhook_results	_hook_errhook_resultr  r>  r  new_argssteer_payloadr  qcmdexec_cmdprocr  r  r  rT  target_command	user_argsr  plugin_handlerr  r  r  r  
skill_cmdscmd_key_skill_namerq  _get_plat_disableduser_instructionrP   _unavail_msg_run_generation_agent_result_final_textr  	_goal_excsg                                                                                                          r!   r0  zGatewayRunner._handle_messageG  s`+      	I&&(( 	00222
A 	  	I 	I 	ILL;ZHHHHHHHH	I
 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Q'33F;;CNN=111KK.... 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'33F;;CNN2&&&KK...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	  .	//	:: .	--//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 ) 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
"0  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 ,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99*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"  )7 
A&A!!A&B& &
C0CC(O/ /O>=O>P P*)P*?R 
R5R0*R50R5AU 
V)V		VW W&%W&B_ 
_$#_$m4 4
n)> n$n)$n)F"AF? F?
AG1G	AG,G,AG1M$AM' M'
ANM1ANNANgAg g
AggAgk)BAn nAn2n	An2n#An-n'An2n-An2rBAt t
At=tAt8t8At=uA2Ay v7AAy x	AAy y
Azy!AzzAz{	BA
 {(A'A~ }A}+ }*A~ }+A}:}7A~ }9A}:}:A~ ~BA
 ~
A~"A~=BA
 ABA
 A
B BC
rI   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        rl|j        t          j"        k    rVddl#}h d}t          |j                  D ]8\  }
}|
t          |j
                  k     r|j
        |
         nd}|dv rZtH          j%        &                    |          d         '                                }||v rd}n|(                    |          \  }}|r|}|                    d          stH          j%        )                    |          }|*                    dd          }t          |          dk    r|d         n|}tW          j,        dd|          }|                    d           r
d!| d"| d#}n	d$| d%| d&}| d') ):t          |d(d          r|j-        r|j.        dd)         }d*| d+) )d,)v r	 dd-l/m0} dd.l1m2} tH          j3                            d/tH          j%        4                    d0                    }tk                      } d}!	 tm                      }"|"                    d1i           }#to          |#tp                    r&|#                    d2          }$|$ts          |$          }!n# tB          $ r Y nw xY w || j:        | j;        p|                     d3          pd|                     d4          pd|!5          }% |)||%|6           d{V }&|&j<        r^| j                            |j                  }'|'r;|'                    |j         d7=                    |&j>                  pd8           d{V  dS |&j?        r|&j@        )n2# tB          $ r%}(t*          A                    d9|(           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.
        r5   r  Tr  Fr  [] r'  r*  nativer  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  rc  )re  markermessage_texts     r!   rg  z>GatewayRunner._prepare_inbound_message_text.<locals>.<genexpr>g  s(      NN&v-NNNNNNrH   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   >   .md.cfg.csv.ini.log.txt.xml.yml.json.toml.yaml)r5   zapplication/octet-streamrp   z
text/plain)zapplication/text/rP  rK  rI  z	[^\w.\- ]r  z![The user sent a text document: 'zC'. Its content has been included below. The file is also saved at: r)  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.]rO  reply_to_text  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_rootr+  zContext injection refused.z(@ context reference expansion failed: %s)Br0   r,  r  r  #_consume_pending_native_image_pathsr   rR  r%  r-  r.  r&  r/  r(  r   r0  r1  VOICEAUDIO_decide_image_input_moder  r  r  r  _enrich_message_with_vision"_enrich_message_with_transcriptionro  r  r8   r{  r  rQ  r  r  r
  DOCUMENT	mimetypesr6   rY   splitextrf   
guess_typebasenamerA  rT  rU  reply_to_message_idr  agent.context_referencesr  agent.model_metadatar  r7   r  r  r  r&   rO   r)   _model	_base_urlr  r2  warningsexpandedr>  r  )*r)  r3  r  rI   _group_sessions_per_user_thread_sessions_per_userr9  _is_shared_multi_userimage_pathsaudio_pathsr5  rY   r7  	_img_modepending_native_stt_fail_markers_stt_adapter	_stt_meta_stt_msg
_mimetypes_TEXT_EXTENSIONS_extguessedrP  r  r4  display_name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_adapterr  r|  s*                                            @r!   _prepare_inbound_message_textz+GatewayRunner._prepare_inbound_message_text  sY     ( -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****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##G,, >L > >6:> > > !LM| M M15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T= <X =
U
X 	U

C X X 
Y
%YY
c                 v    t          | dd           }|sg S t          |                    |g           pg           S )Nr  )r,  r  r  )r)  r9  r  s      r!   r  z1GatewayRunner._consume_pending_native_image_paths  sE     'OQUVV 	IN&&{B77=2>>>rH   r9  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        }	t          |d	d
          r\| j                            |	d           |                     |	d           t          | d          r| j                            |	d           |j        |j        k    p!t          |d	d
          pt          |dd
          }
t          |dd
          rd
|_        |
rC| j                            d|j        r|j        j        nd|j
        |j        |	d           d{V  t5          || j        |          }|                     |          }d
}	 t;                      }t=          |                    d          pi                     dd
                    }n# t@          $ r Y nw xY wtC          ||          }t          |d	d
          rt          |dd          pd}|dk    rd}n|dk    rd}nd}|dz   |z   }	 | j        j        "                    |j        t          |dd                    }|j        r|j        j        nd}t          |dd
          }|dk    p|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'}	 | (                                }|r| d| }n# t@          $ r Y nw xY w|)                    |j        |t          |d(d          )           d{V  n2# t@          $ r%}t          *                    d*|           Y d}~nd}~ww xY wd
|_+        d|_,        t          |d+d          }|
r2|r/t[          |t                    r|gnt]          |          }	 d,d-l/m0} m1}! g }"g }#|D ]r}$ | |$|.          }%|%rF|%\  }&}'}(d/|( d0}) |!|&|'|)          }*|*r*|"2                    |*           |#2                    |$           Wt          3                    d1|$           s|"rP|"2                    |j                   d4                    |"          |_        t                              d2|#|	           n3# t@          $ r&}t          3                    d3||           Y d}~nd}~ww xY w| j        5                    |j                  }+|+rtm          |+          d4k    rd,d5l7m8},m9}- d6}.d7}/d8}0d9}1d}2d}3d}4d}5i }6	 t;                      }6|6r|6                    d:i           }7t[          |7t                    r|7}.nt[          |7tt                    r|7                    d;          p|7                    d:          p|.}.|7                    d<          }8|8(	 tw          |8          }2n# tx          tz          f$ r Y nw xY w|7                    d=          pd}3|7                    d>          pd}4|6                    d?i           }9t[          |9tt                    r~t	          |9                    d@d8                    >                                dAv }0|9                    dB          }:|:0	 tw          |:          };|;d,k    r|;}1n# tx          tz          f$ r Y nw xY w	 | ?                    ||	t[          |6tt                    r|6ndC          \  }.}<|<                    d=          p|3}3|<                    d>          p|4}4|<                    dD          p|5}5n# t@          $ r Y nw xY w|2I|4rF	 	 d,dEl@mA}=  |=|6          }>n<# t@          $ r/ |6                    dF          }>t[          |>t\                    sg }>Y nw xY w|>D ]}?t[          |?tt                    s|?                    d>          pdB                    dG          }@|@r|@|4B                    dG          k    r~|?                    dHi           }At[          |Att                    rQ|A                    |.i           }Bt[          |Btt                    r&|B                    d<          }C|Ctw          |C          }2 nn# tx          tz          f$ r Y nw xY wn# t@          $ r Y nw xY w|0r3 |-|.|4pd|5pd|2|3pdI          }Dtw          |D|/z            }Etw          |DdJz            }Ftm          |+          }G|jC        }H|Hd,k    r|HdK}In |,|+          dL}I|1}J|Ek    p|G|Jk    }K|Krt                              dM|GdN|Itw          |/dOz            |DdN|EdN           |jD        r	dP|jD        ind}L	 d,dQlEmF}M | ?                    ||	t[          |6tt                    r|6ndC          \  }.}<|<                    dD          rdR |+D             tm                    d4k    r |Mdi |<|.d4d8d8dSg|j        dT	 dU _G        t          jI                    }N|NJ                    dfdV           d{V \  }O}Pj        }Q|Q|j        k    r |Q|_        | j        K                                 | j        L                    |j        |O           d,|_C        |O}+tm          |O          }R |,|O          }St                              dW|G|RdN|SdN           |S|Fk    rt          3                    dX|SdN           t          dYd          }T|Tt          |TdZd
          rt          |Td[d,          }Ut          |Td\d          pd]}Vd^|V d_|U d`}W	 | j%                            |j                  }X|Xr*|j        r#|X)                    |j        |W|L)           d{V  n# t@          $ r%}Yt          3                    da|Y           Y d}Y~Ynd}Y~Yww xY w|Tt          |Tdbd          rt          |Tdbd          }Zt          |Tdcd          pd]}[dd|Z de|[ df}\	 | j%                            |j                  }X|Xr*|j        r#|X)                    |j        |\|L)           d{V  n2# t@          $ r%}Yt          3                    dg|Y           Y d}Y~Ynd}Y~Yww xY w| M                               n# | M                               w xY wn2# t@          $ r%}t          3                    dh|           Y d}~nd}~ww xY w|+s| j        N                                s|diz  }|+s|j        r|j        t          jP        k    r|j        t          jQ        k    r|j        j        }t          |          }]t          jT        |]          sP|j        t          jU        k    rdjndk}^dl|V                                 dm|^ dn}| W                    ||           d{V  |j        t          jX        k    rl| j%                            t          jX                  }| Y                    |          }_|_r1|r/t          |do          r|Z                    |_          }`|`r|d|` z  }| [                    |||+p           d{V }a|adS | \                    | j%                            |j                  |	|           	 |j        r|j        j        nd|j
        |j        |addq         dr}b| j                            ds|b           d{V  | ]                    |a||+||j        |	||j^        |j_        t	  	         d{V }c	 | j%                            |j                  }d|dr0t          |ddu          r |d`                    |j                   d{V  n# t@          $ r Y nw xY w| a                    ||          st                              dv|pdw|           | j%                            |j                  }et          t          |e          dxd          |ec                    ||y           n-|er+t          |edz          r|ejd                            |d           	 | e                    |           dS |c                    d{          pd}f|fd|k    rd}}f|c                    d~g           }gt          j                     |z
  }h|c                    dd,          }itm          |f          }jt                              d||j        pd|h|i|j           |	rd| f                    |	           	 | j        g                    |	           n3# t@          $ r&}kt          *                    d|	|k           Y d}k~knd}k~kww xY w|fs|c                    d          r|c                    dd]          }lt	          |l          >                                t          fddD                       pdv otm          |+          dk    }m|mrd}fndt	          |l          dd          d}f|c                    d          r|cd         |j        k    r|cd         |_        	 d,dlimj}n  |nt;                      t          |j                  dt          | dd
                    }on!# t@          $ r t          | dd
          }oY nw xY w|or|fr|c                    d          }p|pr|pl                                m                                }qtm          |q          dk    r7d4                    |qdd                   }r|rdtm          |q          dz
   dz  }rn|pl                                }rd|r d|f }fd}s	 d,dlnmo}t  |tt;                      t          |j                  |c                    d:          |c                    dd,          pd,|c                    d<          pdt          jp                            dd                    }sn4# t@          $ r'}ut          *                    d|u           d}sY d}u~und}u~uww xY w|sr|fr|c                    d          s|f d|s }f| j                            di |bd|fpdddq         i           d{V  	 d,dlqmr}v |vjs        rH|vjs                            d,          }wt          jt        | u                    |w                     |vjs        Hn2# t@          $ r%}t          v                    d|           Y d}~nd}~ww xY w	 d,dlqmr}x g }y|xjw        x                                sa|xjw        y                                }z|z                    dd          }{|{dv r|y2                    |z           |xjw        x                                a|yD ]c}zt          |z          }|||rP	 | {                    |||z           d{V  1# t@          $ r%}}t          v                    d|}           Y d}}~}[d}}~}ww xY wdn2# t@          $ r%}t          *                    d|           Y d}~nd}~ww xY wt=          |c                    d                    }~t	          |c                    dd                    >                                |~oSt=          |c                    d                    p1t          fddD                       pdv otm          |+          dk    }|r!t                              d|j                   n"|~r t                              d|j                   |c                    d          r|r|	rt                              d|j                   | j        |                    |	           | }                    |	           | j                            |	d           |                     |	d           t          | d          r| j                            |	d           |fpddz   }ft          j                                                    }|rn`|+s^|c                    dg           }| j                            |j        d|pg t                      |j        r|j        j        nd|d           |rn|~r%| j                            |j        d|a|d           n|c                    dtm          |+                    }tm          |g          |k    r
|g|d         ng }|sK| j                            |j        d|a|d           |fr$| j                            |j        d|f|d           nR| j        du}|D ]F}|                    d          dk    ri |d|i}| j                            |j        ||           G| j                            |j        |c                    dd,                     t=          |c                    d                    }|                     ||f|g|          r|                     ||f           d{V  |c                    d          r|c                    d          s|fr>| j%                            |j                  }|r|                     |f||           d{V  |srv	 | j%                            |j                  }|r!|)                    |j        |s           d{V  n2# t@          $ r%}kt          *                    d|k           Y d}k~knd}k~kww xY w	 | e                    |           dS |f| e                    |           S # t@          $ r}	 | j%                            |j                  }|r0t          |du          r |`                    |j                   d{V  n# t@          $ r Y nw xY wt                              d|	           t          |          j        }t	          |          rt	          |          dd         nd}ld}t          |dd          }dt                      v rtm          |+          nd,}|dk    rd}n|dk    rd}n|dk    rt          |dd          }i }	 |(|                                                    di           }n# t@          $ r Y nw xY w|                    d          dk    rC|                    dĦ          }|r)|d,k    r#d,dl}|                    |dz            }d| dǝ}n=d}n:d}n7|dk    rd}n.|dv r*|dk    r	 Y d}~| e                    |           dS |d9k    rd}d| d|l d| dНcY d}~| e                    |           S d}~ww xY w# | e                    |           w xY w)zAInner handler that runs under the _running_agents sentinel guard.r#   r5   NP   r+  r@  z3inbound message: platform=%s user=%s chat=%s msg=%rr  was_auto_resetF_pending_model_notesis_fresh_resetzsession:start)r{  rQ  r  r9  privacy
redact_pii)r  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.]rO  r  r  )r{  session_typereset_had_activityz+previous session was stopped or interruptedzdaily schedule at z:00rD  r  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_messager  z[IMPORTANT: The "zB" skill is auto-loaded. Follow its instructions for this session.]z#[Gateway] Auto-skill '%s' not foundz0[Gateway] Auto-loaded skill(s) %s for session %sz-[Gateway] Failed to auto-load skill(s) %s: %sr  )estimate_messages_tokens_roughr  zanthropic/claude-sonnet-4.6g333333?T  r   r>   r  r   r   compressionrq  r  hygiene_hard_message_limitr  r   get_compatible_custom_providerscustom_providersrj  models)r   r   r  r   gffffff?actual	estimateduf   Session hygiene: %s messages, ~%s tokens (%s) — auto-compressing (threshold: %s%% of %s = %s tokens)rm  d   r  AIAgentc                     g | ]Y}|                     d           dv |                     d          .|                     d           |                     d          dZS rK   user	assistantrV  rK   rV  r  r  s     r!   r8  z<GatewayRunner._handle_message_with_agent.<locals>.<listcomp>:  sk     ) ) )$%#$55==4I#I#I$%EE)$4$4 $J *+v155CSCS T T#I#I#IrH   memoryr   rB  
quiet_modeskip_memoryenabled_toolsetsr  c                      d S r  rc  akws     r!   r  z:GatewayRunner._handle_message_with_agent.<locals>.<lambda>L  s    D rH   c                  4                         d           S )Nr5   )approx_tokens_compress_context)_approx_tokens
_hyg_agent	_hyg_msgss   r!   r  z:GatewayRunner._handle_message_with_agent.<locals>.<lambda>Q  s%    
0L0L,5r:H 1M 1* 1* rH   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r3  r  rI   r  )r{  rQ  r  r>  zagent:start)	r>  context_promptrI   r  r  r9  r  event_message_idr  stop_typinguK   Discarding stale agent result for %s — generation %d is no longer currentr  pop_post_delivery_callback
generation_post_delivery_callbacksr  (empty)u   ⚠️ The model returned no response after processing tool results. This can happen with some models — try again or rephrase your question.messages	api_callszMresponse ready: platform=%s chat=%s time=%.1fs api_calls=%d response=%d charsz&clear_resume_pending failed for %s: %sfailedr  c              3       K   | ]}|v V  	d S r  rc  )re  r7  	error_strs     r!   rg  z;GatewayRunner._handle_message_with_agent.<locals>.<genexpr>]  s7       # #a1	> # # # # # #rH   )contextr  z	too largeztoo longexceedr  4002   u}   ⚠️ Session too large for the model's context window.
Use /compact to compress the conversation, or /reset to start fresh.zThe request failed: rZ  z2
Try again or use /reset to start a fresh session.r  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:endresponser  zProcess watcher setup error: %sr  r  )r  r  &Watch notification injection error: %szWatch queue drain error: %scompression_exhaustedc              3       K   | ]}|v V  	d S r  rc  )re  r7  _err_str_for_classifys     r!   rg  z;GatewayRunner._handle_message_with_agent.<locals>.<genexpr>  s9        aq11      rH   )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 longzjSkipping 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  rL   )rK   r  r   r{  r'   r  )rK   rV  r'   history_offsetr  rK   rM   r'   )skip_dbr  )r  ztrailing footer send failed: %szAgent error in session %szno details availablestatus_coderI   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  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.rc  )rD   r}  r{  r#   r+   r0   r/   r  r  rR  rQ  r  r  r/  r9  r,  r  r  r  r  
created_at
updated_atr  r%  r6  r  r   r  _set_session_envr  r(   r8   r
  r   get_reset_policynotifynotify_exclude_platformsr  at_houridle_minutes_format_session_infor  r  r  r  r&   r  r,  r  r  r1  r  r2  load_transcriptr.  r  r  r  rO   r)   r:   r-   rf   r  r  r  rstripr  r  	run_agentr  	_print_fnr  rp  run_in_executorr  rewrite_transcriptr  has_any_sessionsr   r~  r5  rh   r6   r  rC  r  r  r>  _get_guild_idr  r  _bind_adapter_run_generation
_run_agentrT  r  r  _is_session_run_currentr  r  r  _clear_session_envr  clear_resume_pendingro  gateway.display_configr  r  r,   rS  gateway.runtime_footerr  r7   r  r  r<  r  r=  r  completion_queueempty
get_nowaitr  _inject_watch_notificationreset_sessionre  r   rA   	isoformatappend_to_transcriptr  r  update_session_should_send_voice_reply_send_voice_reply_deliver_media_from_responser  __name__localsr@  mathceil)r)  r3  r  r9  r  _msg_start_time_platform_name_msg_previewr  r9  _is_new_sessionr  _session_env_tokens_redact_pii_pcfgr  reset_reasonr  policyra   had_activityshould_notifyr<  reason_texthoursminsdurationnoticesession_infor,  _auto_skill_namesr  r  _combined_parts_loaded_names_sname_loaded_loaded_skill
_skill_dir_display_name_note_partrI   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_metar  loop_compressedrP  _hyg_new_sid
_new_count_new_tokens_comp_dropped_err	_warn_msgr  _werr
_aux_model_aux_err_aux_msgr  sethome_cmdguild_id
vc_contextr|  r^  agent_result_typing_adapter_stale_adapterr  agent_messages_response_time
_api_calls	_resp_lenro  error_detail_is_ctx_fail_rds_show_reasoning_effectiver  linesdisplay_reasoning_footer_line_bfl_footer_errr  rX  _pr_watch_eventsr  r  
synth_texte2agent_failed_earlyis_context_overflow_failurerQ   	tool_defshistory_lennew_messagesagent_persistedrP   r"  _already_sent_media_adapter_foot_adapter_err_adapter
error_typestatus_hintr!  	_hist_len	_err_body	_err_json
_resets_inrI  _hoursr  r  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#/="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"  c	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  
 # MKK>"~$9$9=.455.22488   DJCS ]f.> ? ?Y]IA555555373V3V#)(35?	45P5P(Z		VZ 4W 4 40
L
 (++I66 tN) ))0) ) )I  #9~~22-4W ." ."&2."*434/3046>Z/</G." ." ." ."
b!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-. -. -. -. -. -. -. -.). %)$A$A*$M$M$M$MD$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..	
 	
 	
@	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f ##$788888c $''(899?RH 9$$. 
 *--j"==N!Y[[?:N%))+q99JHIKK_ ;)
I    11+>>>&;;KHHHH    LL@#R          0 0 : : +//II--3355	
  # # # # # < # # #       Y& *Gr)    
1 HLs</@/@#/F L L L  -- 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8s$  3AH: :
IIC&O% N" !O% "
N/,O% .N//5O% %
P/PP$CT8 8
U(U##U(<Bd( Y$ #d( $Y85d( 7Y88B*d( #\; :d( ;]d( ]d( A7_ 
d( 
_d( _	d( #_5 4d 56`.+d -`..C!d d( d$!d( #d$$d( (
d54d5=Bt< E
t! Ap t! 
q	$q?t! q		At! As t! 
t#t>t! tt! t< !t88t< <
u+u&&u+B Am A+ *Am +
85Am 788B9Am C	B-Am E7AF FAm F
AGFAF=F8Am F=AGGCAm JAAK KAm KAK9K6Am K8AK9K9B,Am N&BAP8 P7Am P8
AQ)QAQ$QAm Q$AQ)Q)AAm R>AAT TAm T
AUTAT>T9Am T>AUUAm UBAX0 WAW<W;AX0 W<
AX+XAX&X!AX0 X&AX+X+AX0 X/Am X0
AYX:AYYAm YAYYQAm j,AAk/ k.Am k/
Alk9AllAm lAllAm l9Am mAu2mAAn.n-Au-n.
An;n8Au-n:An;n;B:Au-q6*Ar!r Au-r!
Ar.r+Au-r-Ar.r.A6Au-t$Au5 t?Au-uAu2uAu5 u-Au2u2Au5 u5Avc                 6   ddl m}m} t                      }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	 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   r  r  r   r5   )r   r   r  r   r  r  u:   default — set model.context_length in config to overridedetectedi@B z.1fMi  Ku   ◆ Model: `rk  u   ◆ Provider: 
openrouteru   ◆ Context: z	 tokens (rJ  	localhostz	127.0.0.1z0.0.0.0u   ◆ Endpoint: r+  )r  r  r  r  r  r8   r&   rO   r)   r:   r-   r  r  r
  r  r+   r1  r2  )r)  r  r  r   r  r   r   r   custom_provsr\  r  raw_ctxr  r  r  
ctx_sourcectx_displayr  s                     r!   r,  z"GatewayRunner._format_session_info  s0    	\[[[[[[[&(( $	'))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		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sm   AD 5B D BD B1D C D C?<D >C??D 
DDAE& &
E32E3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d}n| j        !                    |d          }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	 ddl"m#} d |             }n# t          $ r d}Y nw xY w|rtI          | d| |           S tI          | |           S )zHandle /new or /reset command.session_resetr  r  Nr   r  )clear_env_passthrough)clear_credential_filesr  r  r  r5   r  zsession:end)r{  rQ  r9  zsession:resetu"   ✨ Session reset! Starting fresh.T)	force_newu   ✨ New session started!on_session_reset)get_random_tipu
   
✦ Tip: rO  )%r  r  r  r  r  r8   r,  r  r&   r  r  re  r  tools.env_passthroughr  r
  tools.credential_filesr  r@  r  r  r}  r  &_clear_session_boundary_security_stater  r  r  r{  r#   r%  r6  rQ  r,  r/  hermes_cli.tipsr  r   )r)  r3  r  r9  	old_entryru  rv  
_old_agent_qer  r  	new_entryr  _old_sidr[  header_new_sidr  	_tip_lines                      r!   r  z#GatewayRunner._handle_reset_command  sf      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	  	09FF *@@SW@XXI/F	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-2L   
L-,L-1M MM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_homer  u   👤 **Profile:** `rk  u   📂 **Home:** `r+  )rp  r  r&  r  r2  )r)  r3  r  r  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rH   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_slashr5   rj  r  rp   createu   ⚠ kanban error: zCreated\s+(t_[0-9a-f]+)\br{  r#   r  r  rQ  c                      ddl m}  |                                 }	 |                     |pd            |                                 d S # |                                 w xY w)Nr   r  )r  r{  r  r  rQ  )r  r  r  add_notify_subr  )r  r  r  r  r  r  rQ  s     r!   _subz2GatewayRunner._handle_kanban_command.<locals>._sub  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))r  rT  hermes_cli.kanbanr  r0   r,   r/  r  r.  rA  r  r
  searchrD  r  r,  r}  r#   r+   rf   r.  r  r  )r)  r3  r  rT  r  r0   	is_creater  r  r  r  r{  r  r  r  r  r  rQ  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 rc  rS  r6  s     r!   r8  z8GatewayRunner._handle_status_command.<locals>.<listcomp>  s    EEE1qwEEErH   Nr  r   input_tokensoutput_tokenscache_read_tokenscache_write_tokensreasoning_tokensu   📊 **Hermes Gateway Status**r5   z**Session ID:** `rk  z**Title:** z**Created:** z%Y-%m-%d %H:%Mz**Last Activity:** z**Tokens:** rm  z**Agent Running:** u   Yes ⚡Noz**Queued follow-ups:** z**Connected Platforms:** rI  r+  )r  r  r/  r  r  r9  r  r8   r{  r  r  get_session_titler  r
  get_sessionr1  extendr$  strftimer%  r2  )r)  r3  r  r  connected_platformsr9  
is_runningr<  queue_depthr  db_total_tokensr  r  s                r!   r  z$GatewayRunner._handle_status_command  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  r  r  r5   r  r   )r9  elapsedstater  r   c                     | d         S )Nr  rc  )r  s    r!   r  z6GatewayRunner._handle_agents_command.<locals>.<lambda>9  s
    I rH   T)r  reversec                 D    g | ]}|                     d           dk    |S )r  r  r  r6  s     r!   r8  z8GatewayRunner._handle_agents_command.<locals>.<listcomp>=  s7     ! ! !55??i// ///rH   c                 Z    g | ](}t          |d           |                                &|)S )r  )r}  r  )re  ts     r!   r8  z8GatewayRunner._handle_agents_command.<locals>.<listcomp>D  sH     
 
 
q&!!
*+&&((

 
 
rH   r(  u   🤖 **Active Agents & Tasks**z**Active agents:** N   rp   r9  u    · this chatu    · `rk  z. `u   ` · r  u    · r  z... and z morez"**Running background processes:** r@  r   r  W   r  z- `r  uptime_secondsz**Gateway async jobs:** z"No active agents or running tasks.r+  )r  r  r  rD   r  r  r,  r[  r*   r8   r  r)   r4  r1  r+   sortlist_sessionsr
  r  r.  r-  r  r2  rA  )r)  r3  r  r  rA   current_session_keyrunning_agentsrunning_started
agent_rowsr9  r   startedr  
is_pendingrunning_processesbackground_tasksr  idxr  rF   sidr   rf  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_pendingr  Nu2   STOP (pending) for session %s — sentinel cleareduL   ⚡ Stopped. The agent hadn't started yet — you can continue this session.stop_command_handlerr  zNo active task to stop.)r  r  r/  r9  r  r8   r4  r  r   r  r  r   )r)  r3  r  r  r9  r   s         r!   r  z"GatewayRunner._handle_stop_commandz  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,,rH   c                   K   |                      |          rGt                              d|j        r|j        j        r|j        j        j        nd|j                   dS | j        s| j        r+| 	                                }|rd| 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d| 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  r5   u   ⏳ Draining z" active agent(s) before restart...u*   ⏳ Gateway restart already in progress...N)r{  r  r  .restart_notify.jsonrb  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  r  r  r{  r#   platform_update_idr  r  r  r   r  r  rj   r  r
  r  rD   r(   r6   r7   r8   r  )r)  r3  r  notify_datar,  
dedup_datar  _under_services           r!   r  z%GatewayRunner._handle_restart_command  s      ,,U33 	KKD/4|^@U^%++[^(	   2" 	Pdn 	P--//E QPuPPPP!"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 	UT=TTTT  Z  [  [  	[s2   A!C; ;
D*D%%D*.AF 
F<F77F<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.
        NFtelegramr  r{  r  r  rZ  )r  r  r{  r#   r
  r  rZ   r@  rA  rQ  r8   r&   r)   r*   rD   )r)  r3  platform_valuemarker_pathr\  recorded_uidr  s          r!   r  z*GatewayRunner._is_stale_restart_redelivery  sy    =EL05#+5< (5	"\28NN 	 	 	55	Z''5	&)GGK%%'' u:k335566DD 	 	 	55	 88J>115xx,,,,, 	5 xx//lS%L11 	y{{\)C//u'<77s'   6 
AAB 0&B 
B%$B%c                   K   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d                    |          S )z/Handle /help command - list available commands.r   gateway_help_linesu   📖 **Hermes Commands**
r  u   
⚡ **Skill Commands** (z	 active):Nr  rk     ` — descriptionz	
... and z3 more. Use `/commands` for the full paginated list.r+  )	r  r"  r,  r  r1  r.  rd  r
  r2  )r)  r3  r"  r  r  rj  sorted_cmdsr  s           r!   r  z"GatewayRunner._handle_help_command#  sL     ::::::(
!!
	??????++--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	yy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           d                    |          S )zDHandle /commands [page] - paginated list of all commands and skills.r   r!  zUsage: `/commands [page]`rp   r#  r5   u   ⚡ **Skill Commands**:r%  zSkill commandrk  r$  zNo commands available.r  r  r  u   📚 **Commands** (z total, page rj  rJ  z`/commands u
   ` ← prevu   next → `/commands z | z_(Requested page z  was out of range, showing page z.)_r+  )r  r"  r  r,   r)   r-   r  r,  r  r1  rd  r8   r
  r  r   r  r{  r<  r  r.  r  r  r2  )r)  r3  r"  r  requested_pageentriesr  rj  r  descr   	page_sizetotal_pagespagerY  page_entriesr  	nav_partss                     r!   r  z&GatewayRunner._handle_commands_command9  s     ::::::))++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yys$   A 
AA-BD 
DDc                   ./012345678K   ddl }ddlm4m}m} ddlm} |                                                                } ||          \  }}}	d}
d}d6d5d8d7t          dz  }	 t                      }|r|                    di           }t          |t                    rB|                    d	d          }
|                    d
|          }|                    dd          6|                    d          8	 ddlm}  ||          7n%# t           $ r |                    d          7Y nw xY wn# t           $ r Y nw xY w|j        }|                     |          }| j                            |i           }|rX|                    d|
          }
|                    d
|          }|                    d6          6|                    d5          5|s|s| j                            |j                  }|duot-          t/          |          dd          du}|r	  ||6|
87d          }n# t           $ r g }Y nw xY w|rz| 2|3|
0|16/5.dt0          dt0          dt0          dt0          f./012345678fd}|j        r	d|j        ind}|                    |j        ||
||||           d{V }|j        rdS  ||          }d|
pd d| dg}	  ||6|
87d          }|D ]}|d         rdnd}|                    d |d!          d"|d#          d$| d%           |d&         rd'                    d( |d&         D                       }|d)         t?          |d&                   k    r"d*|d)         t?          |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  4|||
65|	|873	  	        }|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%}"tL          '                    d8|"           Y d}"~"nd}"~"ww xY wtQ          | 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 }|/                    di           }|j!        |d	<   |j"        |d
<   |j$        r
|j$        |d<   ddAlm0}$  |$|           n2# t           $ r%}%tL          '                    dB|%           Y d}%~%nd}%~%ww xY w|j*        p|j"        }dC|j!         d$g}|                    dD|            |j1        }&ddElm2}' d}(	 t                      })|)                    di           }*t          |*t                    r&|*                    dF          }+|+tg          |+          }(n# t           $ r Y nw xY w |'|j!        |j"        |j$        p6pd|j#        p5pd|&7|(G          },|,r|                    dH|,dIdJ           |&r|&j4        r|                    dK|&j4        dIdJ           |&5                                r*|                    dL|&6                                            |                    dM|&7                                            tq          |j$        pddN          rdO|j!        9                                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)	get_labelr5   r  rs   r   r>   r   r   	providersr  r  r   send_model_pickerr  )current_providercurrent_base_urlcurrent_modeluser_providersr  
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_inputr7  r9  r8  current_api_key	is_globalexplicit_providerr:  r  Error: Nr  r  r   	new_modelnew_providerr   r   r   z/Picker model switch failed for cached agent: %sr  $[Note: model was just switched from  to  via /. Adjust your self-identification accordingly.]r   r   r   r   r   Model switched to `rk  
Provider: resolve_display_context_lengthr   r  r5   r   r   
model_infor  r  	Context: rm   tokensMax output: Cost: Capabilities: u<   _(session only — use `/model <name> --global` to persist)_r+  )rS  r  r,  r8   r1  rG  target_providerr   r   r   r
  r  r  r}  r  provider_labelr  re  r1  rS  hermes_cli.model_switchrQ  r  r&   rO   r)   
max_outputhas_cost_dataformat_costformat_capabilitiesr2  )r<  r=  r>  r  cached_entryru  r  r  plabelr  mirQ  _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_modelrB  r8  r  
user_provss                    r!   _on_model_selectedz?GatewayRunner._handle_model_command.<locals>._on_model_selected  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  r5  r9  r7  r9  on_model_selectedr?  z
Current: `r  z` on r  
is_currentz
 (current)**r=   z** `--provider rZ  rk  rJ  r  rI  c              3   "   K   | ]
}d | d V  dS rk  Nrc  r  s     r!   rg  z6GatewayRunner._handle_model_command.<locals>.<genexpr>C  s*      .M.MAx1xxx.M.M.M.M.M.MrH   total_modelsz (+z more)z  api_urlz  `u    `/model <name>` — switch modelu5   `/model <name> --provider <slug>` — switch provideru$   `/model <name> --global` — persistr+  r@  rE  r  r  rF  z1In-place model switch failed for cached agent: %sr  rI  rJ  rK  rL  rM  rt   ru   )save_configz"Failed to persist model switch: %srN  rO  rP  r  rR  rT  rm  rU  rV  rW  rX  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[  r1  r2  r3  hermes_cli.providersr4  r  r,   r  r  r8   r&   rO   r  r  r
  r  r  r  r  r{  r,  r  r+   r  r6  r  rS  r1  r2  r.  r  rG  rY  r   r   r   r  r  r}  r  rZ  re  rZ   r  r  r  rx  rS  rQ  r)   r\  r]  r^  r_  rl   rf   warning_message)9r)  r3  r  r2  r3  r4  r  model_inputrD  r  r9  r7  r  r  r  r  r  r9  r  r<  
has_pickerr5  rp  r?  r  rZ  r  r7  r  
model_strsr  r`  ru  r  r  r  rx  r,  rb  rQ  _sw2_config_ctx_sw2_cfg_sw2_model_cfg_sw2_rawrg  cache_enabledrh  ri  rj  rk  rl  rm  rn  rB  r8  r  ro  s9                                                 @@@@@@@@@@@r!   r  z#GatewayRunner._handle_model_commandn  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 
  @$
# < <)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   .BD9 D D9 D52D9 4D55D9 9
EE&H8 8IID#P 
PP5SSS-9T' '
U1UU%Y: ;XY: X##Y: &X#'AY: :
Z)Z$$Z).A] 
]]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   r  rs   r   personalitiesr  z No personalities configured in `z/config.yaml`u!   🎭 **Available Personalities**
u'   • `none` — (no personality overlay)r%  r  r5   Nr  r  u   • `r$  z
Usage: `/personality <name>`r+  c                    t          | t                    r|                     dd          g}|                     d          r|                    d| d                     |                     d          r|                    d| d                     d                    d |D                       S t          |           S )	Nr  r5   tonezTone: stylezStyle: r+  c              3      K   | ]}||V  	d S r  rc  r6  s     r!   rg  zUGatewayRunner._handle_personality_command.<locals>._resolve_prompt.<locals>.<genexpr>  s'       7 7qQ 7 7 7 7 7 7 7rH   )r&   rO   r8   r1  r2  r+   )r#   r4  s     r!   _resolve_promptzBGatewayRunner._handle_personality_command.<locals>._resolve_prompt  s    %&& 8?B77899V$$ ;LL!9%-!9!9:::99W%% =LL!;5>!;!;<<<yy 7 7E 7 7 7777u::rH   )r  r>   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`, rI  c              3   "   K   | ]
}d | d V  dS ru  rc  )re  r  s     r!   rg  z<GatewayRunner._handle_personality_command.<locals>.<genexpr>  s*      *K*K8q888*K*K*K*K*K*KrH   zUnknown personality: `z`

Available: )rp  r  r  r,   rf   r  r  r   r
  r1  r[  r&   rO   r8   r.  r2  rk   r  )r)  r3  r  r   r  r  r  r  r=   r	  rX  r  r,  
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.Nrp   rL  rK   r  rV  r5   zNo previous message to retry.r   )r0   r(  r  raw_messager  )r  r  r/  r-  r  ri  r.  r8   r2  r  r   r   r  r  r  r0  )
r)  r3  r  r  rI   last_user_msglast_user_idxr5  	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6rH   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 	 t          | j
        t                    r| j
        pi                     d	i           nt          | j
        d	i           pi }t          |                    d
d          pd          }n# t          $ r d}Y nw xY w |||          |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   GoalManagerzgoal manager unavailable: %sNrH  z'goal manager: session lookup failed: %sr  r5   goalsr   r  r  default_max_turns)hermes_cli.goalsr  r
  r  r  r  r/  r  r,  r&   r  rO   r8   r)   )r)  r3  r  r  r  r  	goals_cfgr   s           r!   _get_goal_manager_for_eventz)GatewayRunner._get_goal_manager_for_eventA  s   	4444444 	 	 	LL7===:::::		 .DDU\RRMM 	 	 	LLBCHHH:::::	 m\488>B 	:	 dk400="''444T['266<" 
 IMM+r::@bAAII 	 	 	III	{cYGGGVVs:   	 
949A 
B'BB(A4D D,+D,c                 &  K   |                                 pd                                }|                                }|                     |          \  }}|dS |r|dk    r|                                S |dk    r$|                    d          }|dS d	|j         S |d
k    r#|                                }|dS d|j         dS |dv r.|                                }|	                                 |rdn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%}t0                              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.
        r5   Nz"Goals unavailable on this session.r  r  zuser-pausedr  zNo goal set.u   ⏸ Goal paused: r  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  u   ✓ Goal cleared.zNo active goal.zInvalid goal: r  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  r,   rf   r  status_liner  r  r  has_goalr~  r  r-   r  r  r8   r{  r  r   r   r  rT  r  r  r
  r  r  r   )r)  r3  r   rf   mgrr  r  hadr  r<  r9  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}%~3uz333HJJLLE}++fUZ f f f
 ---,,..CIIKKK*-D&&3DD	*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	
s7   =D 
D,D'!D,'D,A	G 
G=G88G=r  r  c                j   	 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 	 t          | j        t                    r| j        pi 	                    di           nt          | j        di           pi }t          |	                    dd	          pd	          }n# t          $ r d	}Y nw xY w |||
          }	|	                                sdS |	                    |pdd          }
|
	                    d          pd}|r)|&	 | j        	                    |j                  }|rt          |d          rddl}|                    ||          }|                    |          r	 |                                }|                                r|                    |           n|                    |           n=# t.          $ r0 	 |                    || j                   n# t          $ r Y nw xY wY nw xY wn2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|
	                    d          sdS |
	                    d          pd}|r|dS 	 | j        	                    |j                  }|                     |          }|r9|r9t7          |t8          j        |dd          }|                     |||           dS dS dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)a  Run the goal judge after a gateway turn and, if still active,
        enqueue a continuation prompt for the same session.

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

        We use the adapter's pending-message / FIFO machinery so any real
        user message that arrives simultaneously is handled by the same
        queue and takes priority naturally.
        r   r  z/goal continuation: goals module unavailable: %sNr  r5   r  r   r  r  T)user_initiatedr>  send_messagez)goal continuation: status send failed: %sshould_continuecontinuation_promptr  z%goal continuation: enqueue failed: %s)r  r  r
  r  r  r,  r&   r  rO   r8   r)   	is_activeevaluate_after_turnr  r{  r}  r  r  r+  get_event_loopr  r  run_until_completer	  run_coroutine_threadsafe_loopr  r   r   r  r  )r)  r  r  r  r  r  r  r  r   r  r  rP   r<  _asynciocoror  r	  r9  
cont_events                      r!   r0  z*GatewayRunner._post_turn_goal_continuation  s"   "	4444444 	 	 	LLJCPPPFFFFF	 m\488>B 	F	 dk400="''444T['266<" 
 IMM+r::@bAAII 	 	 	III	 kSIFFF}} 	F**>+?RPT*UUll9%%+  	O6%O-++FO<< %ww?? %...."//<<D++D11 %%#+#:#:#<#<D#00 > $ 0 0 6 6 6 6 $ 7 7 = = =+ % % %% ( A A$
 S S S S#, % % % $%	%  O O OH#NNNNNNNNO ||-.. 	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   	 
949A4C	 	CC5A H( AG* )H( *
H$5HH$
HH$HH$!H( #H$$H( (
I2IIA-L 
L2L--L2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.Nrp   rL  rK   r  zNothing to undo.rV  r5   r   (   r  u   ↩️ Undid z message(s).
Removed: "rM  )
r  r  r/  r-  r  ri  r.  r8   r2  r  )
r)  r3  r  r  rI   r  r5  removed_msgremoved_countrX  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RrH   c                   K   |j         }|j        r|j        j        nd}|j        }|j        p|}t          |          }	 ddlm}  ||t          |                     n# t          $ r}d| cY d}~S d}~ww xY wd| d| dS )	zOHandle /sethome command -- set the current chat as the platform's home channel.r  r   )save_env_valuezFailed to save home channel: Nu   ✅ Home channel set to **z** (ID: z@).
Cron jobs and cross-platform messages will be delivered here.)
r  r{  r#   r  	chat_namerh   r  r  r+   r
  )	r)  r3  r  ra   r  r  r  r  r,  s	            r!   r  z&GatewayRunner._handle_set_home_command  s      17O--i.$/	&}55	7888888N7CLL1111 	7 	7 	76166666666	7M M MG M M M	
s   A 
A8)A3-A83A8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  r)   r  id)r3  r;   s     r!   r4  zGatewayRunner._get_guild_id/  st     e]D11;43
## 	% 	%s|$$$3   	 SY 	 9<trH   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   enablerX  Trq  z}Voice mode enabled.
I'll reply with voice when you send voice messages.
Use /voice tts to get voice replies for all messages.)rW  disablerW  ru  z'Voice mode disabled. Text-only replies.ttsrV  z;Auto-TTS enabled.
All replies will include a voice message.)channelr2  Nleaver  zOff (text only)z"On (voice reply to voice messages)z!TTS (voice reply to all messages))rW  rX  rV  get_voice_channel_infozVoice mode: zVoice channel: #channel_namezParticipants: member_countmembersis_speakingz (speaking)r5   z  - r  r+  zVoice mode enabled.zVoice mode disabled.)r  r,   rf   r  r  r{  rT  r  r8   r'  rg  rs  rp  _handle_voice_channel_join_handle_voice_channel_leaver4  r}  r  r1  r2  )r)  r3  r   r  r{  	voice_keyr<  r^  labelsr  r  r  r  r  rF   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--rH   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_sourcesrV  Tr  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  r8   r  r{  r}  r4  get_user_voice_channelrQ  _handle_voice_channel_inputr  _handle_voice_timeout_cleanupr  r  r
  r  r  r+   rf   r  r  r)   r  _voice_text_channelsto_dictr  r'  rT  rg  rs  r=   )r)  r3  r<  r  voice_channelrS  r,  	err_lowers           r!   r  z(GatewayRunner._handle_voice_channel_join  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: %srW  Tr  r  zLeft voice channel.)r  r8   r  r{  r4  r}  r  r  r
  r  r  r'  rT  r  rg  rp  r  )r)  r3  r<  r  r,  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.
        rW  Tr  N)r'  rT  r   r>  rg  r  r8   rp  )r)  r  r<  s      r!   r  z+GatewayRunner._handle_voice_timeout_cleanup  so    
 HM)97CCD   -##H$455++GWt+LLLLLrH   r  rQ  
transcriptc           	        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 	 |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+          ||t,          j         |
|d                    }|                    |           d{V  dS )zHandle transcribed voice from a user in a voice channel.

        Creates a synthetic MessageEvent and processes it through the
        adapter's full message pipeline (session, typing, agent, TTS reply).
        Nr  r  )r{  r  rQ  rR  r  z/Unauthorized voice input from user %d, ignoringi  z	@everyoneu   @​everyonez@hereu   @​herez**[Voice]** <@z>: r   )SimpleNamespace)r  r  )r  r0   r(  r  )r  r8   r   r>  r  r,  r   	from_dictr+   rQ  rR  rP  r  r  _clientget_channelr/   r  r
  typesr  r   r   r  handle_message)r)  r  rQ  r  r<  
text_ch_idsource_datar  r  	safe_textr  r3  s               r!   r  z)GatewayRunner._handle_voice_channel_input  s0      -##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	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/F 
F! F!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:FrW  rV  rX  c              3      K   | ]K}|                     d           dk    o-t          d |                     d          pg D                       V  LdS )rK   r  c              3   p   K   | ]1}|                     d i                                d          dk    V  2dS )functionr=   text_to_speechNr  )re  tcs     r!   rg  zCGatewayRunner._should_send_voice_reply.<locals>.<genexpr>.<genexpr><   sX         z2&&**6226FF     rH   
tool_callsN)r8   ro  )re  rP   s     r!   rg  z9GatewayRunner._should_send_voice_reply.<locals>.<genexpr>:   s       
 
  GGFOO{*   77<006B    
 
 
 
 
 
rH   T)r/  r  r  r'  r8   rT  r{  r(  r   r  ro  )
r)  r3  r  r  r  r  
voice_modeis_voice_inputshouldhas_agent_ttss
             r!   rD  z&GatewayRunner._should_send_voice_reply   s    $  	8..x88 	5,&%))$//%,:OQX*Y*Y[`aa
,0AA 5  ?l*=~ 	  	5  
 
 &
 
 
 
 
  	5  	, 	5trH   r0   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)ra  )r0   output_pathr  rS  zAuto voice reply TTS failed: %sr  play_in_voice_channelr  
send_voice)r  
audio_pathr>  r  r?  zAuto voice reply failed: %sr<  rc  )#uuidtools.tts_toolr  r  r6   r  r   rY   r2  tempfile
gettempdiruuid4hexmakedirsdirnamer  r  r@  rA  r8   isfiler  r  r  r  r{  r4  r}  r  r  r  rT  r  r  r
  )r)  r3  r0   _uuidr  actual_pathr  r  tts_textr7  result_jsonr  r<  r  send_kwargsr,  s                   r!   rE  zGatewayRunner._send_voice_replyO   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 	 |                    |          \  }}|                    |          \  }}|                    |          \  }}|j        j        rd|j        j        ind}	ddl	m
}
 h d}h d}g }g }|D ]Z\  }} ||          j                                        }||v r|s|                    |           C|                    ||f           [g }|D ]Q} ||          j                                        |v r|                    |           <|                    |           R|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  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://r5   rc  )re  r7  _quotes     r!   r8  z>GatewayRunner._deliver_media_from_response.<locals>.<listcomp>   s/    OOOa444b9OOOrH   )r  imagesr?  z0[%s] Post-stream image batch delivery failed: %s)is_voice)r  r  r?  )r  
video_pathr?  r  r  r?  z*[%s] Post-stream media delivery failed: %sz)[%s] Post-stream file delivery failed: %sz'Post-stream media extraction failed: %s)pathlibr   urllib.parser  extract_mediaextract_imagesextract_local_filesr  r  gateway.platforms.baser	  suffixrf   r1  send_multiple_imagesr  r
  r  r  r=   r{  r  
send_videosend_document)r)  r  r3  r<  r   media_filesrP  cleanedlocal_files_thread_metar	  _VIDEO_EXTS_IMAGE_EXTSr  non_image_media
media_pathr	  extnon_image_localr  r	  r,  r	  s                         @r!   rF  z*GatewayRunner._deliver_media_from_response   s      	!     000000T	I$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+%%h%&&z2222#**J+ABBBB$&O( 6 6	4	??)//11[@@&&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   D+M =6E4 3M 4
F)>!F$M $F))
M 4B8I-,M -
J"7!JM J""M *A7L"!M "
M,!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_listNrs   rt   ru   r  rq  FzXCheckpoints are not enabled.
Enable in config.yaml:
```
checkpoints:
  enabled: true
```Tmax_snapshotsr  )rq  r8	  r|   zNo checkpoints found for rp   hashz!Invalid checkpoint number. Use 1-r   rS  u   ✅ Restored to checkpoint restored_tor   r  z1
A pre-rollback snapshot was saved automatically.u   ❌ r  )r!  r6	  r7	  r  r  rZ   r  r  r8   r&   r(   r
  r6   r  r+   r   homer  r,   list_checkpointsr)   r.  r-   restore)r)  r3  r6	  r7	  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%SrP  rI  NrD  r  r5   u   🔄 Background task started: "z"
Task ID: u9   
You can keep chatting — results will appear when done.)r  r,   r  r   rA   r  r6   urandomr	  r  r  _run_background_taskr(  rl  r  rm  r.  )r)  r3  r	  r  r  r
  rX  s          r!   r  z(GatewayRunner._handle_background_command)!  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rH   r	  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   r  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   r  )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  rB  r  Tverbose_loggingFr  rJ	  r  r  r  providers_allowedonlyproviders_ignoredr  providers_orderorderprovider_sortr  provider_require_parametersrequire_parametersprovider_data_collectiondata_collectionr  r{  rQ  rR  r  r  r  r  
session_dbr  )r  r  rc  )r  r8   rQ  rR  r  r  r  r  r  r  run_conversationr  )r   r  rJ	  r  rB  r  prr	  r  r)  r  r  
turn_routes    r!   run_syncz4GatewayRunner._run_background_task.<locals>.run_synco!  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r  r5   r  rE  rD  r  u&   ✅ Background task complete
Prompt: "z"

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

(No response generated)zBackground task %s failedz	 failed: )!r/  r  r  r8   r{  r  r  r  r  r  r  r  r  hermes_cli.tools_configrI	  rd  r   r)   r6   r  r  r  r  r  r  _run_in_executor_with_contextr#	  r$	  r.  
send_imager
  r*	  r  ) r)  r	  r  r  r<  _thread_metadatar  r   r  rI	  	agent_cfgr[	  r  r  r+	  r	  text_contentrX  r  r]	  alt_textr2	  	_is_voicer,  r  rJ	  r  rB  r  rY	  r  rZ	  s    ````                    @@@@@@@@r!   rF	  z"GatewayRunner._run_background_taskF!  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   Nrs   r  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.rt   ru   Nr   rL  Tz Failed to save config key %s: %sF
rZ   r  r  rA  r&   rO   rk   r
  r  r  
rh	  r#   r  r  r  rF   r  r,  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)rq  Fznone (disabled)r  mediumu   on ✓rW  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 ****.)hiderW  u*   🧠 ✓ Reasoning display: **OFF** for **rs  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lowrp	  highxhigh)rq  r     ⚠️ 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  r,   r  r  r  r  r  r  r  r  r+   r8   r,  r  r{  r  r  re  rf   )r)  r3  r  r   r  r9  rm	  rcleveldisplay_statehas_session_overridescoper  r  rJ  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rH   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_moders   uO   ⚡ /fast is only available for OpenAI models that support Priority Processing.rh	  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rj	  rk	  rl	  s
           r!   rm	  z<GatewayRunner._handle_fast_command.<locals>._save_config_keyW"  rn	  ro	  r  r"  r!  r  u(   ⚡ Priority Processing

Current mode: `z(`

_Usage:_ `/fast <normal|fast|status>`>   r   r!  FAST>   rW  r  NORMALry	  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  r,   rf   r  r  r  r  r  r+   )r)  r3  r	  r   r  r   rm	  r  saved_valuerA  r  r  s             @@r!   r  z"GatewayRunner._handle_fast_commandI"  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NrH   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)  r3  r	  r	  r	  r9  rF   s          r!   r  z"GatewayRunner._handle_yolo_command"  s      	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 225<@@))+66 	}  ---!"{|||,,,!"{|||rH   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.
        rs   r   tool_progress_commandFr  zThe `/verbose` command is not enabled for messaging platforms.

Enable it in `config.yaml`:
```yaml
display:
  tool_progress_command: true
```)rW  r  rV  r  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_progressrV  rp   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)r  r  r  r{  r  rm   r   r
  r:  r  indexr.  r&   r8   rO   rk   r  r  )r)  r3  r  r  r  gate_enabledcycledescriptionsr  rF   r  new_moder   r,  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}	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%          ||           n7# t          $ r*}	t&                              d|	           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_configrs   r5   r>  Nrj  rp   u#   ⚠️ Could not read config.yaml: )r  r  rq  ONOFFrI  fieldsu   📎 Runtime footer: **z**
Fields: `z`
Platform: `rk  )r   r  r@  r   T)rW  r  r  r  Fz Usage: `/footer [on|off|status]`r   runtime_footerz)Failed to save runtime_footer.enabled: %su   ⚠️ Could not save config: )format_runtime_footer)r   context_pctr{   )r   r  r  r	  z
Example: `rs  u4   
_(saved globally — takes effect on next message)_)r;  r	  r  r  r  r{  r,  r,   r/  rA  r.  rf   r
  r  r2  r8   r&   rO   rk   r  r  r	  r  )r)  r3  r	  r  r  rA	  r0   r4  r  r,  	effectiver  r	  	new_stater   exampler	  rX  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 	= 	= 	=<<<<<<<<<	= *)+|DD	/!!%i0;DDeEYYy}}X66<"==F.% . .". .*. . . ///II444IIBYY%i00II55
	8kooi88$?? ,)+I&!),Ggkk*:;;TBB /,.()3<G$%i0k;7777 	8 	8 	8NNFJJJ7A77777777	8 ",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! !
C:+C5/C:5C:0A=G. .
H"8HH"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 # tB          $ r*}tD          #                    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).r5   Nr   r  )summarize_manual_compression)estimate_request_tokens_roughr  r   z*No provider configured -- cannot compress.c                     g | ]Y}|                     d           dv |                     d          .|                     d           |                     d          dZS r  r  r  s     r!   r8  z:GatewayRunner._handle_compress_command.<locals>.<listcomp>N#  sg       55==$999aeeI>N>N9 v1553C3CDD999rH   Tr  r  c                      d S r  rc  r  s     r!   r  z8GatewayRunner._handle_compress_command.<locals>.<lambda>^#  s    t rH   _cached_system_promptr  )r  r  zHNothing to compress yet (the transcript is still all protected context).c                  6                         d           S )Nr5   )r  focus_topicr  )r  r	  msgs	tmp_agents   r!   r  z8GatewayRunner._handle_compress_command.<locals>.<lambda>q#  s     I77bP]kv7ww rH   r   r  Fr  r  r  r  u   🗜️ headlinezFocus: "rM  
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  r+  zManual compress failed: %szCompression failed: rc  )$r  r  r/  r-  r  r.  r  r,   r/  r  !agent.manual_compression_feedbackr	  r  r	  r  r  r8   r0  r,  r  has_content_to_compressr  r  rp  r1  r  r2  rC  r9  r(   r)   r1  r2  r
  r  r  )r)  r3  r  r  rI   r  r	  r	  r9  r   r  _sys_prompt_tools
compressorr  
compressedrP  new_session_id
new_tokensrb  _summary_failed_dropped_count_summary_err_aux_fail_model_aux_fail_errr  r,  r  r	  r	  r	  s                              @@@@r!   r  z&GatewayRunner._handle_compress_command0#  sX      *@@HH$44]5MNN 	U#g,,**TT --//52<<>>F$n	.))))))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eV --i88888S /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--i8888--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N !#N AJ> N 5D3J> (N >KB=N 
OO<OO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.Session database not available.Nr  )r  r  rQ     ⚠️ uE   ⚠️ Title is empty after cleanup. Please use printable characters.u   ✏️ Session title set: **rs  zSession not found in database.u   📌 Session: `z`
Title: **z/`
No title set. Usage: `/title My Session Name`)r  r  r/  r  r  r  create_sessionr{  r#   rQ  r
  r  r,   sanitize_titler-   set_session_title)
r)  r3  r  r  r  existing_title	title_arg	sanitizedr,  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	  Nr  )r  r  c                 <    g | ]}|                     d           |S )r  r  )re  r  s     r!   r8  z8GatewayRunner._handle_resume_command.<locals>.<listcomp>#  s'    @@@w@!@@@rH   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  rX  r5   r  u    — _rP  u   • **rs  z 
Usage: `/resume <session name>`r+  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 **rr	  zFailed to switch session.c                 D    g | ]}|                     d           dk    |S rK   r  r  r  s     r!   r8  z8GatewayRunner._handle_resume_command.<locals>.<listcomp>,$  ,    GGGqquuV}}/F/F/F/F/FrH   r   rH   messagerp   r  rJ  u   ↻ Resumed session **z. Conversation restored.)r  r  r  r  r,   r{  r#   list_sessions_richr8   r1  r2  r
  r  r  resolve_session_by_titleresolve_resume_session_idr  r/  r  r  switch_sessionr  re  r  r-  r.  )r)  r3  r  r9  r=   user_sourcer  titledr  r  r  rX  preview_partr,  	target_idcurrent_entryr  rI   	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%Sr  rP  r  r  r   r>   )r  r  r   parent_session_idz#Failed to create branch session: %szFailed to create branch: rK   r  rV  	tool_namer=   r  tool_call_idfinish_reasonr  reasoning_contentreasoning_detailscodex_reasoning_itemscodex_message_items)r  rK   rV  r	  r  r	  r	  r  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!   r8  z8GatewayRunner._handle_branch_command.<locals>.<listcomp>$  r	  rH   u   ⑂ Branched to **z** (r	  rp   r  r5   z copied)
Original: `z`
Branch: `z/`
Use `/resume` to switch back to the original.)!r 	  r  r  r  r  r/  r-  r  r  r,   r   rA   r  r	  r	  r  get_next_title_in_lineager	  r{  r#   r&   r  rO   r8   r
  r  r  append_messager	  r	  r  re  r.  )r)  r3  r		  r  r9  r	  rI   branch_name_dtrA   timestamp_str
short_uuidr	  branch_titlecurrent_titlebaser	  r,  rP   r  r	  s                        r!   r$  z$GatewayRunner._handle_branch_command1$  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.
        r  Nr  r   r   r   r   r  billing_providerbilling_base_url)r   r   T)markdownsession_total_tokens)format_rate_limit_compactu   ⏱️ **Rate Limits:** r5   session_input_tokenssession_output_tokenssession_cache_read_tokenssession_cache_write_tokensu   📊 **Session Token Usage**zModel: `rk  zInput tokens: rm  zCache read tokens: zCache write tokens: zOutput tokens: zTotal: zAPI calls: )CanonicalUsageestimate_usage_cost)r  r  r  r  )r   r   r  r  rW  $z.4fincludedzCost: includedr  rT  z / rH  r  z%)zCompressions: r+  )r  c                 j    g | ]0}|                     d           dv |                     d          .|1S )rK   r  rV  r  r  s     r!   r8  z7GatewayRunner._handle_usage_command.<locals>.<listcomp>
%  sB    fff!!%%--;P*P*PUVUZUZ[dUeUe*PA*P*P*PrH   u   📊 **Session Info**z
Messages: zEstimated context: ~rU  z;_(Detailed usage available after the first agent response)_z)No usage data available for this session.)*r  r  r  r8   r4  r,  r  r/  r  r  r  r
  r  r  r   r   r}  session_api_callsget_rate_limit_statehas_dataagent.rate_limit_trackerr	  r1  r   r	  agent.usage_pricingr	  r	  
amount_usdr  r*   r  r  r  r  compression_countr  r2  r-  r  r  r.  )!r)  r3  r  r9  r   ru  r  cachedr   r   r   _entry_for_billing	persistedaccount_linesaccount_snapshotr  rl_stater	  r  r  
cache_readcache_writer	  r	  cost_resultr{  rg  pctr  rI   r  r	  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--daysrp   zInvalid --days value: rK  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_insights>%  sW    Y[['++d6BB..v66


rH   zInsights command error: %sTr<  zError generating insights: )r  r,   rT  rU  rA  r.  r)   r-   isdigitr  r  agent.insightsr
  r  rp  r1  r
  r  r  )r)  r3  r   r4  r5  r  r
  r,  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.r  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  r  r
  r  _execute_mcp_reload)r
  r
  r  r  r3  r)  r9  s       r!   _on_confirmz=GatewayRunner._handle_reload_mcp_command.<locals>._on_confirmm%  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`._r  z/reload-mcp)r3  r   r  r>  handler)r  r  _read_user_configr&   rO   r8   r(   r
  r+   r
   _request_slash_confirm)	r)  r3  r  r  r
  confirm_requiredr
  prompt_messager9  s	   ``      @r!   r  z(GatewayRunner._handle_reload_mcp_commandK%  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 
 
 
 
 
 
 
 
 	
rH   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_serversrf  Nu   🔄 **MCP Servers Reloaded**
u   ♻️ Reconnected: rI  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. r5   r  z,[IMPORTANT: MCP servers have been reloaded. zD. The tool list for this conversation has been updated accordingly.]r  r+  zMCP reload failed: %su   ❌ MCP reload failed: )r  rp  tools.mcp_toolr
  r
  r 
  rf  r  r  r1  r1  r2  rd  r.  r  r/  r  rB  r  r
  r  r  )r)  r3  r  r
  r
  r 
  rf  old_servers	new_toolsconnected_serversaddedremovedreconnectedr  change_partstool_summarychange_detail
reload_msgr  r,  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%
  r&
  totalrefresh_skill_groupz)Adapter %s refresh_skill_group raised: %sr=   u   🔄 **Skills Reloaded**
zNo new skills detected.u   
📚 z skill(s) availabler+  itemr   c                 x    |                      dd          }|                      dd          }|rd| d| nd| S )Nr=   r5   r%  z    - r   r  )r0
  nmr*  s      r!   	_fmt_linez>GatewayRunner._handle_reload_skills_command.<locals>._fmt_line&  sO    XXfb))xxr2204G,,,d,,,-2--GrH   u   ➕ **Added Skills:**u   ➖ **Removed Skills:**z[USER INITIATED SKILLS RELOAD:r5   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: )r  rp  r,  r-
  r1  r8   r  r  r  r,  callableinspectisawaitabler
  r  r  r1  r2  rO   r+   r  r  r}  r4
  )r)  r3  r  r-
  r  r%
  r&
  r.
  r<  refreshmayber  r  r3
  r0
  sectionsr	  r9  r,  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   r  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   r  FN)r  r  r>  r9  r  r?  rS  Tz*send_slash_confirm failed for %s on %s: %s)r  r  r  r  nextr  registerr  r8   r{  _thread_metadata_for_sourcesend_slash_confirmr  r,  r
  r  r  )r)  r3  r   r  r>  r
  rB  r  r9  r  r<  r?  used_buttonsbutton_resultr  s                  r!   r
  z$GatewayRunner._request_slash_confirmS&  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  r&   rO   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!   r>
  z)GatewayRunner._thread_metadata_for_source&  s)    FK66	4Y''rH   c                   K   |j         }|                     |          }ddlm}m}  ||          s'|| j        v r| j                            |           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_approvalr  uQ   ⚠️ Approval expired (agent is no longer waiting). Ask the agent to try again.zNo pending command to approve.rV  c                     g | ]
}|d k    |S )rV  rc  re  r  s     r!   r8  z9GatewayRunner._handle_approve_command.<locals>.<listcomp>&  s    3331U

Q


rH   c              3      K   | ]}|d v V  	dS ))r  	permanentpermanentlyNrc  rH
  s     r!   rg  z8GatewayRunner._handle_approve_command.<locals>.<genexpr>&  s(      NNqq::NNNNNNrH   r  z (pattern approved permanently)c              3      K   | ]}|d v V  	dS ))sessionsesNrc  rH
  s     r!   rg  z8GatewayRunner._handle_approve_command.<locals>.<genexpr>&  s(      <<Q((<<<<<<rH   rM
  z$ (pattern approved for this session)r  r5   resolve_allrp   rH  
 commands)z4User approved %d dangerous command(s) via /approve%su   ✅ Commandr  z	 approvedz. The agent is resuming...)r  r  r  rF
  r  r  r  r  r,   rf   rA  ro  r  r8   r{  resume_typing_for_chatr  r  r  )r)  r3  r  r9  rF
  r  r   rP
  	remainingr
  	scope_msgr  r  	count_msgs                 r!   r  z%GatewayRunner._handle_approve_command&  s     ( 226::	
 	
 	
 	
 	
 	
 	
 	
 %$[11 	4d555'++K888jj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prH   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   rE
  u(   ❌ Command denied (approval was stale).zNo pending command to deny.rV  r  rO
  rp   rH  rQ
  r5   z-User denied %d dangerous command(s) via /denyu   ❌ Commandr  z deniedr   )r  r  r  rF
  r  r  r  r  r,   rf   r  r8   r{  rR
  r  r  r  )r)  r3  r  r9  rF
  r  r   rP
  r  r  rU
  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JrH   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: r5   z**Debug report uploaded:**c              3   4   K   | ]}t          |          V  d S r  r   )re  r  s     r!   rg  zSGatewayRunner._handle_debug_command.<locals>._collect_and_upload.<locals>.<genexpr>4'  s(      33c!ff333333rH   rk  <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.r+  )r
  r  r  r  r[  r1  r2  )r`
  r
  urlsr  r  label_widthrA  r6  r\
  r]
  rX
  r[
  rY
  rZ
  s           r!   _collect_and_uploadz@GatewayRunner._handle_debug_command.<locals>._collect_and_upload%'  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)
r  hermes_cli.debugrX
  rY
  rZ
  r[
  r\
  r]
  rp  r1  )r)  r3  r  r  rf
  r\
  r]
  rX
  r[
  rY
  rZ
  s        @@@@@@r!   r!  z#GatewayRunner._handle_debug_command'  s       		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 (w'))	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$2 ))$0CDDDDDDDDDrH   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 Agentrb  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  rQ  r9  r'   r  r  Tr  r@  c              3   >   K   | ]}t          j        |          V  d S r  r  rd  s     r!   rg  z7GatewayRunner._handle_update_command.<locals>.<genexpr>'  s,      !K!K%+d"3"3!K!K!K!K!K!KrH   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.)*r@  r  r  r   r  ri
  rj
  r  r{  _UPDATE_ALLOWED_PLATFORMSr'  r  r8   r#   allow_update_commandr
  r   rq  rs  rr  rZ   r  r  r  r  rQ  rA   rA  r  r  re  rf  r/   r  r2  r  r  r+   r  r  r  r:  )r)  r3  r@  r  r  r   ri
  rj
  r{  _allowedr  r"  project_rootgit_dirr  pending_pathr  exit_code_pathr9  pending_tmp_pendinghermes_cmd_str
update_cmdr  r,  s                            r!   r  z$GatewayRunner._handle_update_command@'  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  r  r  _watch_update_progressr{
  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 r        @      @poll_intervalstream_intervalc                 x   !"#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     rb|                                r|                                rU	 |                                }t%          |          |k    r||d         z  t%          |          }n# t&          $ r Y nw xY w |             d{V  	 |                                                                pd}t+          |          }|dk    r                     d#           d{V  n1                     d                    |          #           d{V  t                              d||
           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|||||fD ]}|                    d           t           dz                      d           | j                            |
d           dS |                                rU	 |                                }t%          |          |k    r||d         z  t%          |          }n# t&          $ r Y nw xY w                                r+"                                !z
  |k    r |             d{V  |                                r|
r| j                            |
          s	 t          j        |                                          }|                    dd          }|                    dd          }|r |             d{V  d}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        |
<   |                    d           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     b|                                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$  rk
  rl
  z.update_prompt.jsonNr{  r  r9  r  rJ  zOUpdate watcher: cannot resolve adapter/chat_id, falling back to completion-only124r0   r   c                 .    t          j        dd|           S )Nz\x1b\[[0-9;]*[A-Za-z]r5   )rT  rU  r  s    r!   _strip_ansiz9GatewayRunner._watch_update_progress.<locals>._strip_ansi'  s    62B===rH   r   r5   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.r5   N  c                 *    g | ]}||z            S rc  rc  )re  r5  clean	max_chunks     r!   r8  zOGatewayRunner._watch_update_progress.<locals>._flush_buffer.<locals>.<listcomp>(  s&    VVVeAa)mO,VVVrH   r   z```

```r  zUpdate stream send failed: %s)r,   rD   ri  r.  r  r
  r  r  )chunkschunkr,  r
  r
  r
  r<  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r   u   ✅ Hermes update finished.r  u(   ❌ Hermes update failed (exit code {}).z&Update finished (exit=%s), notified %sz$Update final notification failed: %sTr  r  r	  r>   Fsend_update_prompt)r  r	  r>   r9  r?  z%Button-based update prompt failed: %sz (default: rJ  u"   ⚕ **Update needs your input:**

zG

Reply `/approve` (yes) or `/deny` (no), or type your answer directly.z!Forwarded update prompt to %s: %sr  z Failed to read update prompt: %sz$Update watcher timed out after %.0fsu-   ❌ Hermes update timed out after 30 minutes.r  )!r  r  rp  rD   rZ   r@  rA  rQ  r8   r   r  r
  r  r  r9  rw  re  r+   r.  r   r,   r)   r  formatr  r  r  r  r,  r  r
  r  rZ  )$r)  r
  r
  r}   rt
  claimed_pathr  ru
  prompt_pathry  r9  rY   rv
  r  r  r{  
bytes_sentr
  rV  exit_code_rawr  r,  r7  prompt_dataprompt_textr>   sent_buttonsbtn_errdefault_hintr
  r<  r
  r  r
  r  r?  s$                                @@@@@@@r!   r|
  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@
 $**d*;;;$GVabecebeVfggg,g6 H H HLL!CQGGGGGGGGH -.........I iikkH$$N $$&& 	?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2A7[* *\% \  \%0_ 
__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$  rk
  rl
  FTr  r{  r  r  z2Update notification deferred: update still runningr   r5   Nz\x1b\[[0-9;]*mr
  r  iTr   u!   ✅ Hermes update finished.

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

```
u(   ✅ Hermes update finished successfully.u]   ❌ Hermes update failed. Check the gateway logs or run `hermes update` manually for details.r  z0Sent post-update notification to %s:%s (exit=%s)z#Post-update notification failed: %s)r  rZ   r/   r   r  r@  rA  rQ  r8   r  r  r,   r)   r   r  rT  rU  r.  r  r
  r  )r)  rt
  r
  r  ru
  cleanupactive_pending_pathrv
  r  r  r  r
  r  r  r{  r<  r?  rP   r,  s                      r!   r9  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           
        K   t           dz  }|                                sdS 	 t          j        |                                          }|                    d          }|                    d          }|                    d          }|r|s	 |                    d           dS t          |          }| j                            |          }|s4t          
                    d|           	 |                    d           dS |rd|ind}|                    |d	|
           d{V }	t          |	dd          rt                              d||           n,t                              d||t          |	dd                     n2# t          $ r%}
t                              d|
           Y d}
~
nd}
~
ww xY w|                    d           dS # |                    d           w xY w)zANotify the chat that initiated /restart that the gateway is back.r  Nr{  r  r  Tr  z6Restart notification skipped: %s adapter not connectedu;   ♻ Gateway restarted successfully. Your session continues.r  rS  Fz"Sent restart notification to %s:%sz3Restart notification to %s:%s was not delivered: %sr  r  zRestart notification failed: %s)r  rZ   r@  rA  rQ  r8   r  r   r  r  r  r  r,  r  r  r
  )r)  notify_pathr\  r  r  r  r{  r<  r?  r  r,  s              r!   r;  z(GatewayRunner._send_restart_notification(  sa     "%;;!!## 	F,	0:k335566D88J//Lhhy))G--I w J $/////G  --Hm''11G L    8 $/////5 4=FY//$H"<<M! (        F vy%00 8     I FG_==	    	A 	A 	ANN<a@@@@@@@@	A $/////K$////s>   A)F &AF B F G 
F5F0+G 0F55G G(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_varsr5   )r{  r  r  r  rQ  rR  r9  )gateway.session_contextr
  r  r{  r#   r  r  r  r+   rQ  rR  r9  )r)  r  r
  s      r!   r&  zGatewayRunner._set_session_env)  s     	=<<<<<^,2N*n.4"7>~7OWc'.2333UW3:>3IQC.///r7>~7OWc'.2333UW+
 
 
 	
rH   r  c                 (    ddl m}  ||           dS )z>Restore session context variables to their pre-handler values.r   )clear_session_varsN)r
  r
  )r)  r  r
  s      r!   r8  z GatewayRunner._clear_session_env%)  s+    >>>>>>6"""""rH   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)r  rp  r   r1  run)r)  funcr   r  rg  s        r!   r`	  z+GatewayRunner._run_in_executor_with_context*)  sP      '))nn)T)$EEEEEEEEEEErH   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 — %sNr0   )
agent.image_routingr
  r  r
  r
  r  r  r
  r  r  )	r)  r
  r
  r
  r  r  r   r   r  s	            r!   r  z&GatewayRunner._decide_image_input_mode0)  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_promptNrS  analysisr5   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: r)  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: rO  )tools.vision_toolsr
  agent.memory_managerr
  r  r  r@  rA  r8   r1  r
  r  r2  )r)  r
  r  r
  r
  analysis_promptenriched_partsrY   r	  r  r%  r,  r{  s                r!   r  z)GatewayRunner._enrich_message_with_visionG)  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.r)  rO  r   )transcribe_audiozTranscribing user voice: %sNrS  r  z8[The user sent a voice message~ Here's what they said: "z"]r  r  rx  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  rQ  tools.transcription_toolsr
  r  r  r  r  r1  r8   r/  r
  r  r2  r,   )r)  r
  r  disabled_noter
  r
  rY   r  r  r  _no_stt_noter,  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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   r9  r5   r  Nz>Synthetic process-event session-store lookup failed for %s: %sr{  r  r  r  z9Synthetic process event has invalid platform metadata: %rr  rQ  rR  )r{  r  r  r  rQ  rR  )gateway.sessionr   r+   r8   r,   r  r  r  r,  r  r
  r  r  r  rf   r   r#   r   r'  r  r:  r-   r  )r)  r  r   r9  derived_platformderived_chat_typederived_chat_idr"  r  r  ra   r  r  r{  r  s                  r!   _build_process_event_sourcez)GatewayRunner._build_process_event_source)  sy    	211111#''-006B77==?? 	5
"11333*377DD (WUHd;; ( <'   T        )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	
H6 (/H H6 H22H6 6%II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#   Tr0   r(  r  r  uA   Watch pattern notification — injecting for %s chat=%s thread=%sr  )r
  r  r  r8   r}  r{  r#   r+   r  r[  r   r   r  r  r  r  r  r
  r  )
r)  r  r  r  ra   r<  r7  r  synth_eventr,  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rX  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_intervalr9  r5   r{  r  r  rQ  rR  notify_on_completeFzCProcess watcher started: %s (every %ss, notify=%s, agent_notify=%s)rW  TNz"Process watcher ended (silent): %s)
strip_ansii0r  z completed (exit code z).
Command: z	
Output:
r)  )r  r9  r{  r  r  rQ  rR  zHDropping completion notification with no routing metadata for process %sr
  uU   Process %s finished — injecting agent notification for session %s chat=%s thread=%sz Agent notify injection error: %s)rV  r  r  )r   Niz[Background process z finished with exit code z~ Here's the final output:
r  zWatcher delivery error: %srV  iz is still running~ New output:
zProcess watcher ended: %s) r  r  r8   r-  r  r  r  rw  exitedr.  output_bufferis_completion_consumedtools.ansi_stripr
  r  r   r
  r  r  r[  r{  r  r   r   r  r  r  r  r
  r  r#   r  )r)  rX  r  r  r[  r9  ra   r  r  rQ  rR  agent_notifynotify_moderM
  last_output_lencurrent_output_lenhas_new_output	_pr_checkr
  r  r  r  r<  r7  r  r
  r,  rU  
new_outputr|  	send_metas                                  r!   r=  z"GatewayRunner._run_process_watcherJ*  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  rq  )r  	threshold)r  target_ratio)r  protect_last_n)r   rJ	  _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)r&   rO   r
  r8   tools.registryr
  r,  r
  )clsr  outr  sectionr  section_valr
  s           r!   _extract_cache_busting_configz+GatewayRunner._extract_cache_busting_config*  s     !'T::Bkk: 	/ 	/LGS'''**K+t,, /*5//#*>*>w&&&&''*.w&&&&''	4///////6xPT/U/UC+,, 	4 	4 	4/3C+,,,	4
s   <B B)(B)r  r  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   r5   r   r   r   T)	sort_keysr>      )
hashlibr@  r+   r8   sha256encode	hexdigestrd  r[  rf  )r   r  r  r
  r
  r
  _j_api_key_api_key_fingerprint_cache_keys_sortedblobs              r!   _agent_config_signaturez%GatewayRunner._agent_config_signature+  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"==rH   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  r8   )r)  r9  r   r  r  r  vals          r!   r  z+GatewayRunner._apply_session_model_overrideF+  s{     044[AA 	).((We,,B 	* 	*C,,s##C&)s#n$$rH   agent_modelc                 p    | j                             |          }|duo|                    d          |k    S )zGReturn True if *agent_model* matches an active /model session override.Nr   r
  )r)  r9  r
  r  s       r!   _is_intentional_model_switchz*GatewayRunner._is_intentional_model_switch[+  s9    044[AAt#LW(=(=(LLrH   r  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.
        FNr  T)r7  r  r  r  r}  r  )r)  r9  r  s      r!   r  z*GatewayRunner._release_running_agent_state`+  s    :  	5%d.J.J/
 /
% 5  d333##K6664(( 	5!!+t444trH   c                    |sdS 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  r  r   )clear_sessionz:Failed to clear approval state for session boundary %s: %s)	r,  r&   rO   r  r  r
  r
  r  r  )r)  r9  pending_approvalsupdate_prompt_pending_clear_approval_sessionr,  s         r!   r  z4GatewayRunner._clear_session_boundary_security_state+  s;    	F#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*   >B 
BBB$ $
C.CC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   r  Nrp   )__dict__r8   r  r)   )r)  r9  generationsnext_generations       r!   r-  z+GatewayRunner._begin_session_run_generation+  sj      	1m''(ABBK+6D(kook1==>>B#2K rH   r5   r  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  r  )r)  r9  r  r  s       r!   r  z0GatewayRunner._invalidate_session_run_generation+  sG    77DD
 	KK?	   rH   r  c                     |sdS | j                             d          pi }t          |                    |d                    t          |          k    S )zEReturn True when ``generation`` is still current for ``session_key``.Tr  r   )r  r8   r)   )r)  r9  r  r  s       r!   r7  z%GatewayRunner._is_session_run_current+  sQ     	4m''(ABBHb;??;2233s:FFrH   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.N_active_sessions_hermes_run_generation)r,  r8   setattrr)   r
  )r)  r<  r9  r  interrupt_events        r!   r5  z*GatewayRunner._bind_adapter_run_generation+  s      	k 	Z-?F	%g/A2FFJJ;WWO*)A3z??SSSSS +* 	 	 	DD	s   AA 
A A T)release_running_stater  r  r  c                  K   |sdS | j                             |          }|r|t          ur|                    |           |                     ||           | j                            |j                  }|r1t          |d          r!|                    ||j	                   d{V  |r%t          |d          r|
                    |           | j                            |d           |r|                     |           dS dS )zFInterrupt the current run and clear queued session state consistently.Nr  interrupt_session_activityr;  )r  r8   r4  r  r  r  r{  r}  r  r  r;  r  r  r  )r)  r9  r  r  r  r  rX  r<  s           r!   r  z*GatewayRunner._interrupt_and_clear_session+  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:::::	; 	;rH   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).r  N)r,  r  r  )r)  r9  rf  s      r!   re  z!GatewayRunner._evict_cached_agent+  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)rD   _last_activity_ts_last_activity_desc_api_call_count)r   r  s     r!   _init_cached_agent_for_turnz)GatewayRunner._init_cached_agent_for_turn+  s3     a&*ikkE#(DE% !rH   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_soft,  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.
        r  Nmove_to_endc                 B    h | ]}||t           ut          |          S r  r4  r  rH
  s     r!   r  z9GatewayRunner._enforce_agent_cache_cap.<locals>.<setcomp><,  8     
 
 
}*A!A!A qEE!A!A!ArH   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  rT  r   daemonr=   )r,  r}  r  r  r.  _AGENT_CACHE_MAX_SIZEr  r  r8   r&   r  r  r1  r  r  r  r  r  Threadr  rY  )r)  r  running_idsexcess
evict_planordered_keysr  r"  r   rP  remaining_over_caps              r!   _enforce_agent_cache_capz&GatewayRunner._enforce_agent_cache_cap",  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	  
 %'''	 	rH   c                 t   t          | dd          }t          | dd          }||dS t          j                    }g }d t          | di                                           D             }|5  t          |                                          D ]t\  }}t          |t                    r
|r|d         nd}|)t          |          |v r;t          |dd          }	|	O||	z
  t          k    r|	                    ||f           u|D ]\  }}
|
                    |d           	 ddd           n# 1 swxY w Y   |D ]n\  }}t                              d||t          |d|          z
             t          j        | j        |fd	d
|dd                                                     ot#          |          S )u  Evict cached agents whose AIAgent has been idle > _AGENT_CACHE_IDLE_TTL_SECS.

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

        Agents currently in _running_agents are SKIPPED for the same reason
        as _enforce_agent_cache_cap: tearing down an active turn's clients
        mid-flight would crash the request.
        r  Nr  r   c                 B    h | ]}||t           ut          |          S r  r  rH
  s     r!   r  z:GatewayRunner._sweep_idle_cached_agents.<locals>.<setcomp>,  r  rH   r  r  z3Agent cache idle-TTL evict: session=%s (idle=%.0fs)Tzagent-cache-idle-r  r  )r,  rD   r  r  r[  r&   r  r  _AGENT_CACHE_IDLE_TTL_SECSr1  r  r  r  r  r!  r  rY  r.  )r)  r  rf  rA   to_evictr"  r  r"  r   last_activityrP  s              r!   rg  z'GatewayRunner._sweep_idle_cached_agentsn,  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_URLr5   rj  r  	proxy_urlN)r6   r  r,   r.  r  r8   )r)  r6  r  s      r!   _get_proxy_urlzGatewayRunner._get_proxy_url,  s     i+R006688 	#::c??""$$wwy!!'R,,["==CCEE 	#::c??"trH   r  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r  r  r  r  uH   ⚠️ Proxy URL not configured (GATEWAY_PROXY_URL or gateway.proxy_url)GATEWAY_PROXY_KEYr5   r   c                  <     sdS                                 S r  r7  r  r)  r9  s   r!   _run_still_currentz>GatewayRunner._run_agent_via_proxy.<locals>._run_still_current,  )    %[%t//^LLLrH   rM   r  rK   rV  r  r  zContent-Typezapplication/jsonzBearer AuthorizationzX-Hermes-Session-Idzhermes-agentT)r   r  streamNr  	streamingStreamingConfigr  rW  r  GatewayStreamConsumerStreamConsumerConfigSUPPORTS_MESSAGE_EDITINGFfresh_final_after_secondsr   edit_intervalbuffer_thresholdr  buffer_onlyrD  )r<  r  r  r?  z+Proxy: could not set up stream consumer: %sr  r  )r.
  	sock_readr  z/v1/chat/completions)r@  headersr  zProxy error (%d) from %s: %sr  u   ⚠️ Proxy error (z): rZ  r  uK   Discarding stale proxy stream for %s — generation %d is no longer currentr  )r  r  r  r  r  r  response_previewedrt   r/   )rG  r+  rp   zdata: r  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 charsr  z(No response from remote agent)r  )?aiohttpr2  r3  r]   r0  r6   r  r,   r(   r1  r8   r,  r  r?  r  r{  r  r:  r  rq  	transportr  gateway.stream_consumerrA  rB  r  r  r   rY  r<  r*   rF  rG  r  r
  r  r  r  r  r
  send_typingrD   postr  r0   r  finishr  r  r  r  rV  iter_anyr  r.  r*  rA  r/  r@  rA  on_deltarZ  r  )7r)  r>  r  rI   r  r  r9  r  r   _AioClientSessionr3  r/  	proxy_keyr9  api_messagesrP   rK   rV  rJ  body_stream_consumer_scfgr?  r  r  r  _plat_streaming_streaming_enabledrb	  rA  rB  r  _adapter_supports_edit_effective_cursor_buffer_only_fresh_final_secs_consumer_cfg_sc_errstream_taskfull_response_start_timeoutrM
  resp
error_textr
  r
  r0   rY  r\  objrL  rM  r,  _elapseds7   `     ``                                               r!   _run_agent_via_proxyz"GatewayRunner._run_agent_via_proxy,  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_depthr  c           !      /   
abcdefghijklmnopqrstuvwxyz{|}~K                                     r#                     |	           d{V S ddlma ddl{dt
          f fdkt                      t          j                  }ddl	m
} t           ||                    s                    d          pi }|                    d	          pdr                    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yddlmb ydk    oj        bj        k    j        bj        k    o#t5          |                    d          d          tr{                                ndzdgvdgudg|dgwdcddt8          dt8          dt8          dt          fckquvwyz|f	dxj        bj        k    r
j        p|	jnj        jjrdjindiikquz{| f	d }dgqdg~dgdgt?          j                     g j!        ed!t&          d"tD          ddfegkfd#p j#                            j                  lj$        njrdjindodt8          d$t8          ddfgklnofd%mabegjklmnopq
rstxz}~ f!d&}d}rt?          j%         |                      }d}fd'}t?          j%         |                      }q fd(}t?          j%         |                      }t?          j&                    ffq fd)}t?          j%         |                      } tO          d*d+          }!|!dk    r|!nddtQ          j(                    hdhoq fd,}"t?          j%         |"                      }#	 tO          d-d.          }$|$dk    r|$nd}%tO          d/d0          }&|&dk    r|&nd}'d}(t?          j)         *                    |                    })d}*d1}+|%$d},	 t?          j+        |)h|+2           d{V \  }-}.|-r|),                                },nf-                                s҉rЉ j#                            j                  }/qd         }0|/r|0rt]          |/d3          r|//                              r|/j0                                      }1|1r|1j1        nd}2td          3                    d4| 4                                rd5nd6           |05                    |2           f6                                 n:d},	 t?          j+        |)h|+2           d{V \  }-}.|-r|),                                },nqd         }3d7}4|3rLt]          |3d8          r<	 |37                                }5|5                    d9d7          }4n# t(          $ r Y nw xY w|(s|'|4|'k    rd}( j#                            j                  }6|6rt'          |'d:z            pd;}7t'          |%|'z
  d:z            pd;}8	 |68                    j$        d<|7 d=|8 d>o?           d{V  n2# t(          $ r%}9td          9                    d@|9           Y d}9~9nd}9~9ww xY w|4|%k    rd}*nf-                                s҉rЉ j#                            j                  }/qd         }0|/r|0rt]          |/d3          r|//                              r|/j0                                      }1|1r|1j1        nd}2td          3                    d4| 4                                rd5nd6           |05                    |2           f6                                 7|*rqd         }:i };|:r6t]          |:d8          r&	 |:7                                };n# t(          $ r Y nw xY w|;                    dAdB          }<|;                    d9d          }=|;                    dC          }>|;                    dDd          }?|;                    dEd          }@td          :                    dF|=|%|<|?|@|>pdG           |:r*t]          |:dH          r|:5                    tv                     t'          |%d:z            pd;}AdI|A dJg}B|>r$|B<                    dK|> dL|=dMdN|? dO|@ dP	           n#|B<                    dQ|< dR|=dMdS|? dO|@ dT	           |B<                    dU           dV=                    |B          ~d         r~d                             dWg           ng |?d         pg dddX},qd         }C~d         }D|Dr|D                    dY          nd}E|C[t]          |CdZ          rK|EsIt}                      }F|Cj?        |Fk    r0 @                    |Cj?                  s A                               ~d         }G j#                            j                  }Hd}Id}J|Gr|Hr։rt          |H          }I C                    |H|I          }I|G                    d[          r]|Is[|G                    d\          rF|G                    d\          }Kt          |K          rtd          3                    d]pd^|K           n>|K}Jn;|Ir9|Ij1        pt          |I          }Jtd          9                    d_|Jdd`                    |Gr@|Js>|Is<|G                    da          }L|Lr%|L}Jtd          9                    db|Jdd`                    |Jr|JF                                G                    dO          r|JF                                H                    dd;          }M|Mr"|Md         d;d         I                                ndc}N|NrB	 dddlJmK}O  |O|N          rtd          3                    de|N           d}Id}Jn# t(          $ r Y nw xY w jL        r8|Is|Jr4td          3                    dfpd^ M                                           d}Id}J|Is|Jrtd          9                    dg|Jdd`                    |Hr:t]          |Hdh          r*r(|HjN        v r|HjN                 O                                  jP        k    rtd          Q                    di            j#                            j                  }H|Hr|Irt          |Hj0        |I           n(|Hr&t]          |Hdj          r|HS                    |J           ~d         p|,dk	 |r|T                                 | T                                 |#T                                 |rr	 t?          jU        |d12           d{V  nT# t>          jV        t>          jW        f$ r6 |T                                 	 | d{V  n# t>          jW        $ r Y nw xY wY nw xY w|T                                 r X                    l            jL        r Y                    dm           || ||#fD ]#}P|Pr	 |P d{V  # t>          jW        $ r Y w xY w$S |G                    d[          }Q|QsUd         }R|Rr|r	 t?          jU        |d12           d{V  n# t>          jV        t>          jW        f$ r6 |T                                 	 | d{V  n# t>          jW        $ r Y nw xY wY n1t(          $ r%}Std          9                    dn|S           Y d}S~Snd}S~Sww xY wt          |G                    do                    }Tt          |Rrt          |Rdpd          p|T          }U|G                    dqdc          }V|Vrv|Ust	 td          3                    drpd^           |H8                    j$        |Vo?           d{V  nQ# t(          $ r%}Std          Q                    ds|S           Y d}S~Sn'd}S~Sww xY w|Vrtd          3                    dtpd^           t          t          |H          dud          C|H\                    v          }Wt          |W          r	  |W             ni# t(          $ r Y n]w xY wnX|HrVt]          |Hdw          rF|Hj^        _                    d          }Wt          |W          r	  |W             n# t(          $ r Y nw xY w|G                    dW          }X}Y|J}Zd}[d}\|I}t          |Idxd          p}Y `                    |I|Y|Xy           d{V }Z|Z'|G|r|T                                 | T                                 |#T                                 |rr	 t?          jU        |d12           d{V  nT# t>          jV        t>          jW        f$ r6 |T                                 	 | d{V  n# t>          jW        $ r Y nw xY wY nw xY w|T                                 r X                    l            jL        r Y                    dm           || ||#fD ]#}P|Pr	 |P d{V  # t>          jW        $ r Y w xY w$S t          |Idzd          }[t          |Id{d          }\ j#                            j                  }]|]r4	 |]a                    j$        o?           d{V  n# t(          $ r Y nw xY w b                    |Z|X|Yd;z   |[|\|
  
         d{V 	 |r|T                                 | T                                 |#T                                 |rr	 t?          jU        |d12           d{V  nT# t>          jV        t>          jW        f$ r6 |T                                 	 | d{V  n# t>          jW        $ r Y nw xY wY nw xY w|T                                 r X                    l            jL        r Y                    dm           || ||#fD ]#}P|Pr	 |P d{V  # t>          jW        $ r Y w xY w$S 	 |r|T                                 | T                                 |#T                                 |rr	 t?          jU        |d12           d{V  nT# t>          jV        t>          jW        f$ r6 |T                                 	 | d{V  n# t>          jW        $ r Y nw xY wY nw xY w|T                                 r X                    l            jL        r Y                    dm           || ||#fD ]#}P|Pr	 |P d{V  # t>          jW        $ r Y w xY w$n*# |r|T                                 | T                                 |#T                                 |rr	 t?          jU        |d12           d{V  nT# t>          jV        t>          jW        f$ r6 |T                                 	 | d{V  n# t>          jW        $ r Y nw xY wY nw xY w|T                                 r X                    l            jL        r Y                    dm           || ||#fD ]#}P|Pr	 |P d{V  # t>          jW        $ r Y w xY w$w xY wd         }Rt          |,t                    r|,                    dY          s|,                    dq          pdc}^|^ p|^d}k    }_t          |Rot          |Rdpd                    }`t          |,                    do                    }T|_s(|`s|Tr$td          3                    d~pd^|`|T           d|,d<   |,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  rI   r  r  r9  r  r   Nr   r  r   c                  <     sdS                                 S r  r7  r8  s   r!   r9  z4GatewayRunner._run_agent.<locals>._run_still_current-  r:  rH   rH	  r   rJ	  r   r  )set_tool_preview_max_lentool_preview_lengthr	  HERMES_TOOL_PROGRESS_MODEr  tool_progress_overridesrV  r  rW  interim_assistant_messagesTr  Fr   
event_typer	  rX  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   rY  rV  )TOOL_PROGRESS_FLAGrM  rN  tool_progress_hint_gatewayr   r	  Fr  Trs   z(tool-progress onboarding hint failed: %s)ztool.startedis_interruptedr  )get_tool_emojiu   ⚙️r  )get_tool_preview_max_len)ensure_asciir>   rI  r  r@  (z)
z: "rM  r  rp   	__dedup__)r8   rV  rw  rM  rN  rx  r  rm   r   putr  r
  r  r  r,  agent.displayrz  r{  r@  rf  r+   r.  r  r  )ru  r	  rX  r   kwargsrY  rw  rM  rN  rx  r   gate_on	_hint_err_agent_for_interruptrz  emojir{  _plargs_strrP   _cap_LONG_TOOL_THRESHOLD_Sr9  agent_holderlast_progress_msg	last_toollong_tool_hint_firedprogress_modeprogress_queuerepeat_counts                        r!   progress_callbackz3GatewayRunner._run_agent.<locals>.progress_callbackG.  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        srt          |dd          pd                                }d|v sd|v r t,                              d| j                   d}|                     j        |           d {V  ns|r9d                    |          }|                     j        |           d {V }n#|                     j        |           d {V }|j        r|j        r|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S)NTr   g      ?r   ry  FrI  r~  u    (×rp   rJ  rL  	__reset__r+  )r  rT  rV  r  r5   floodzretry afterz1[%s] Progress edits disabled due to flood controlr\	  g333333?r  zProgress message error: %s)r  r8   r{  r  edit_messager   r=  r>  r
  r,  r  rw  r&   r  r.  r1  rD   r  r2  r  rS  rf   r  r  r=   r  rT  rQ  Emptyr  r  )r<  progress_linesprogress_msg_idcan_edit_last_edit_ts_PROGRESS_EDIT_INTERVALr;   r  rP  base_msgr  rP   _now
_remaining	full_textr  r  _pending_textr,  _progress_metadatar9  r  r  r  r  r  r)  r  s                      r!   send_progress_messagesz8GatewayRunner._run_agent.<locals>.send_progress_messages.  s`     ! m''88G 
 G}})-@-MMM(..00 &113333$    )..00 
 N"OHM&)#Y+X+--// "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 --//  @O$?$(IIn$=$=	'.';';$*N'6$- (< ( ( " " " " " "
  &~ q$+FGR$@$@$FB#M#M#O#OD&$-42G2G !'$W$+L!" !" !" (-H"),,v~s]o,"p"pppppppp# z(,		.(A(AI+2<<Xal~<++%%%%%%FF ,3<<X[fx<+y+y%y%y%y%y%y%yF!> @f.? @.4.?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!-**************+oY+s   A5 5
BB'O  C O  
C(%O  'C((O  O  9E O  
EO  EBO  <AO  
O  FO   $W4$W4,B.T4#S?>T4?
T	T4T'T43W44
U>W4 U3W45#VW4
V&#W4%V&&W4,	W455W//W4rc  
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)Nr=   r5   z
agent:step)r{  rQ  r  rc  
tool_namesr  zagent:step hook error: %s)r&   rO   r1  r8   r+   r  r  r6  r{  r#   rQ  r
  r  r  )
rc  r  _names_tro  
_hooks_ref_loop_for_stepr9  r  r  s
        r!   _step_callback_syncz5GatewayRunner._run_agent.<locals>._step_callback_sync/  s9   %%'' > %'%+ / /B!"d++ /bffVnn&:;;;;c"gg....0OOL=C_$TFO$9$9RT#)>&0%.&,!+3 3   #
 
 
 
 
  > > >8"=========>s   B3C 
C4C//C4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status_callback error (%s): %sr  r  r  r
  r  r  )ru  r>  ro  r  r9  _status_adapter_status_chat_id_status_thread_metadatas      r!   _status_callback_syncz7GatewayRunner._run_agent.<locals>._status_callback_sync/  s    " *<*<*>*> 
O0#(('!8 )  
 #      O O O=z2NNNNNNNNNOs   +> 
A/A**A/c                  
   !abcdef pdt           j        d<   t          t          j        dd                    } j        hj        k    rdnj        j        }upd}tpd                                }|r|dz   |z                                   }j        r|dz   j        z                                   }	 t          t          dd	           n2# t          $ r t          t          dd
	           Y nt          $ r Y nw xY w	                               \  }}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t7          |          }|}y}|}|s|r!	 ddlm}m} j                            j                  }|rt-          |dd          }|stA          d          |
j!        }d}j        hj"        k    rd}d}j        hj#        k    r tI          t-          |
dd          pd          nd} ||
j%        |
j&        |||          } ||j'        |lrdlind ||fdnd           f|rd tP          d!d fmffd"}	fd<   n2# t          $ r%}t                              d#|           Y d }~nd }~ww xY wdd$d tP          d%t6          d!d fkmnpqffd&})                    z||          }*                    |d'         |d(         w|+                              )          }d }t-          d*d           }t-          d+d           }|r||5  |                              } | r|| d,         |k    rp| d         }tY          |d-          r'	 |-                               n# t\          $ r Y nw xY w/                    |j           t                              d.           d d d            n# 1 swxY w Y   |z gdd'|d'         i|d(         i d/| d0dd1dd2wd3vd4|pd d5j0        pd d6|d7j        d8|                    d8          d9|                    d:          d;|                    d<          d=|                    d>          d?|                    d@          dA|                    dBd          dC|                    dD          dEdF|dGj1        dHj2        dIj'        dJj3        dKj4        dj5        dLdMj6        dNj7        }|r7|5|5  ||f|<   8                                 d d d            n# 1 swxY w Y   t                              dO|           r{nd |_9        ij:        rrnd |_;        |	|_<        |r|nd |_=        o|_>        ||_?        j        |_@        |                    d8          pi |_A        t          jC                    dg bt          jD                    cdPtP          d!d fkmnpqfdQedbcdefdR}!dPtP          d!d fbcdemnfdS}"|"|_E        nrQrOt-          t          n          dTd           nG                    |!U           nt-          ndVd           }#|#|!|#<   |sd<   tY          |dW          r|jH        nd d<   g }$xD ]}%|%                    dX          }&|&s|&dYv r|&dZk    r&d[|%v }'d\|%v }(|&d]k    })|'s|(s|)r4d^ |%I                                D             }*|$J                    |*           n|%                    d_          }+|+rt|%                    d`          r|%                    dadb          },dc|, dd|+ }+|&|+de}-|&dfk    r!dgD ]}.|%                    |.          }/|/r|/|-|.<   |$J                    |-           t                      }0|$D ]}1|1                    dX          dhv r|1                    d_d          }2di|2v rit          jM        dj|2          D ]S}3|3N                    d,                                          O                    dk          }4|4r|0P                    |4           TddllQmR}5mS}6mT}7mU}8 dmt          d!d faknpqfdn}9t-          doi           }:r|:W                    d           nd };|;r|;dz   zz   zt                      }<t          t          x          |<p          }=d }>r3	 j[        j\                                      }>n# t          $ r d }>Y nw xY wt7          |>d uot-          |>dqd          o|=          }?t7          |$o |$dr                             dX          d]k    o|=          }@|?r/t-          |>dsd           pdt}A|Adtk    rdun	|Advk    rdwndx}Bdy|B dzzz   zn|@rd{zz   zt-          d|d           }:|:r&r$|:v r |:W                    d           }C|Cr|Cdz   zz   zpda |7a          }D |5a|9           	 ]                              }E|Er	 dd}l^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|c                    |I|$          }K |8a            |6|D           n#  |8a            |6|D           w xY w|K~d<   ffd                                 |K                    d          }Ld}Md}Nd}Od}Psd         }Q|Qr`tY          |Qd          rPt-          |Qje        dd          }Mt-          |Qdd          }Nt-          |Qdd          }Ot-          |Qje        dd          pd}P|Qrt-          |Qd'd           nd }R|Ls|K                    d          rd|Kd          nd}S|S|K                    dg           |K                    dd          |K                    dd          |K                    dd          d         pg t          |$          |M|N|O|R|PdS di|Lvr<g }Td}U|K                    dg           D ]}%|%                    dX          dhv r|%                    d_d          }+di|+v rvt          jM        dj|+          D ]Z}V|VN                    d,                                          O                    dk          }W|Wr|W|0vr|TJ                    di|W            [d|+v rd}U|Trvt                      }Xg }Y|TD ]0}Z|Z|Xvr*|XP                    |Z           |YJ                    |Z           1|Ur|Yf                    dd           |Ldz   dg                    |Y          z   }Lsd         }d}[|rrtY          |dE          rt|jh        k    rid}[t          i                    d|jh                   j[        j\                                      }-|-r%|jh        |-_h        j[        j                                 |rt-          |dE          n}\|[rdnt          |$          }]|Lrj6        r	 ddlkml}^ ~d         r~d                             dg           ng }_t-          |dd           }` |^j6        |\z|L|_|`|rSt-          |d'd           t-          |dd           t-          |dd           t-          |dd           t-          |dd           dnd            n# t          $ r Y nw xY w|L|K                    d          ~d         r~d                             dg           ng ~d         r~d                             dd          ndd         pg |]|M|N|O|R|P|\|K                    dd          dS )Nr5   HERMES_SESSION_KEYr   r  r}  rO  Trt   )r  rv   zlatin-1r  z3run_agent resolved: model=%s provider=%s session=%sr   u'   ⚠️ Provider authentication failed: r   r4  r  r  r=  r>  rW  r@  rC  z(skip streaming for non-editable platformFrD  r   rE  r  c                  .                          d          S )N)r  )r  )r  s   r!   r  z<GatewayRunner._run_agent.<locals>.run_sync.<locals>.<lambda>10  s    ););N)K)K rH   )r<  r  r  r?  on_new_messager0   r   c                 J                 r                     |            d S d S r  )rU  )r0   r9  rZ  s    r!   _stream_delta_cbzDGatewayRunner._run_agent.<locals>.run_sync.<locals>._stream_delta_cb70  s;    #5#5#7#7 !D$4$=$=d$C$C$C$C$C!D !DrH   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)Nr5   r  z$interim_assistant_callback error: %s)
on_segment_breakon_commentaryr+   r,   r  r  r  r
  r  r  )	r0   r  ro  r  r9  r  r  r  rZ  s	      r!   _interim_assistant_cbzIGatewayRunner._run_agent.<locals>.run_sync.<locals>._interim_assistant_cb>0  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
  r  r  rp   r  z#Reusing cached agent for session %srB  r  rL	  r  rJ	  ephemeral_system_promptprefill_messagesr  r  r  rM	  rN	  rO	  r  rP	  rQ	  rR	  r  rS	  rT	  rU	  rV	  r  r{  rQ  rR  r  r  r  gateway_session_keyrW	  r  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: %sr  )r>  ro  r  r9  r  r  r  s     r!   _deliver_bg_review_messagezNGatewayRunner._run_agent.<locals>.run_sync.<locals>._deliver_bg_review_message0  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~  )rv
  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_messages0  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_setr1  )r>  r  r  r  r  r9  r  s    r!   _bg_review_sendzCGatewayRunner._run_agent.<locals>.run_sync.<locals>._bg_review_send0  s   & .@.@.B.B F)0022 #0 # #188:: #.55g>>>"# # # # # # # ### # # # # # # # # # # # # # # +*733333s   +A++A/2A/register_post_delivery_callbackr  r  r  rK   )rL   rM   r  r	  toolc                 &    i | ]\  }}|d k    ||S )r'   rc  )re  r  r;  s      r!   r5  z>GatewayRunner._run_agent.<locals>.run_sync.<locals>.<dictcomp>0  s(     R R R$!QkAQAQAAQAQAQrH   rV  mirrormirror_sourcezanother sessionz[Delivered from rv  r  r  )r  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   r5   r%  zdangerous commandsend_exec_approvalN)r  r   r9  r%  r?  r  r  zLButton-based approval failed (send returned error), falling back to text: %sz6Button-based approval failed, falling back to text: %sr  r  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_chatr8   r,  r  r  r  r  r  rS  r  r  r  r
  r.  r  )r  r  r*  _approval_resultro  cmd_previewrP   _approval_session_keyr  r  r  r  s          r!   _approval_notify_synczIGatewayRunner._run_agent.<locals>.run_sync.<locals>._approval_notify_sync,1  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r  )rB   resume_pendingrL  resume_reasonr  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.]

r4
  )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  r6  s     r!   rg  z=GatewayRunner._run_agent.<locals>.run_sync.<locals>.<genexpr>1  s1      LLquuV}};LLLLLLrH   z8Native image attachment failed, falling back to text: %s)conversation_historyr  r  r  r  session_prompt_tokenssession_completion_tokensr  r  r	  r  r  r	  r  )r  r  r  r	  r  r  r  r  r  r  r   r  z[[audio_as_voice]]r+  u/   Session split detected: %s → %s (compression))maybe_auto_title_emit_auxiliary_failurer   r   r   )r   r   r   r   r   )failure_callbackmain_runtimer  rK  )r  r  r  r  r  r  r  r  r  r   r  r  rK  rc  r  )mr6   r7   r)   r  r{  r~  r#   r,   r  rn   	_env_pathUnicodeDecodeErrorr
  r  r  r  r8   r   r  r  r  r  r,  r  r?  rq  rO  r(   rP  rA  rB  r  r	  r  rY  r<  r*   rF  rG  r  r+   r  r
  r
  r}  r  KeyErrorr  r  rQ  rR  r  r  r  r  r  r'  tool_progress_callbackr5  step_callbackstream_delta_callbackinterim_assistant_callbackstatus_callbackr  r  r  r  r  r  background_review_callbackr  r  r  r[  r1  r  rT  finditerrD  r.  rl  r  r  r  r  r  rO   r  r<   rG   rR   r  r  r  r
  r  r  r.  ro  rX	  rS  r  r  r2  r  r  r  agent.title_generatorr  )rB  r  combined_ephemeralevent_channel_promptr   r  r  rY	  r  r  r[  r?  r\  r]  _want_stream_deltas_want_interim_messages_want_interim_consumerrA  rB  r  r^  r_  r`  ra  rb  rc  r  rZ	  _sigr   ru  r  r	  r  r  _pdcagent_historyrP   rK   has_tool_callshas_tool_call_idis_tool_message	clean_msgrV  
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  rp  _skipped_run_message_img_excr  r  _last_prompt_toks_input_toks_output_toks_context_lengthr  _resolved_model	error_msg
media_tagshas_voice_directiverC  rY   seenunique_tagsr  _session_was_spliteffective_session_id_effective_history_offsetr  all_msgs_title_failure_cbr  r  r  r  r  rZ  r  r   r  rm  r  _progress_thread_idr9  r  r  r  r  r  r  r  r  rJ	  r  rI   "interim_assistant_messages_enabledr>  r  r  r  result_holderr  r)  r  r9  r  stream_consumer_holdertool_progress_enabledtools_holderr  s                                                                                                    @@@@@@r!   r[	  z*GatewayRunner._run_agent.<locals>.run_sync/  s4    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"IwGGGGG% J J JIyIIIIII   (,(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#0K^%hk3F%G%Gdh $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	&/ &

:r : :!'K!;!;$jj599-3ZZ8OQV-W-W)!_2&)-&8&8*;$/%1,&5  2 ~--
&+#!::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& & & )-     !   D #1"(**-=">">DQRSDT\M!,00R@@@Z\ERSTEU\]1-11+qAAA[\%a.B";&7 +!-("12&,jj1Eu&M&M  s   6C !C=1	C=<C=AE 
E-	E("E-(E- C-L 
L=L88L=<Q8P+*Q8+
P85Q87P884Q88Q<?Q<1WW W&f ff9m Ak0 /m 0
l!:lm l!!m m-B)}. .
};:};c                     K   t          d          D ]G} d         #d                                          d{V   dS t          j        d           d{V  HdS )z8Wait for the stream consumer to be created, then run it.r  r   Nr  )ri  r
  r  rw  )rP  r&  s    r!   _start_stream_consumerz8GatewayRunner._run_agent.<locals>._start_stream_consumer2  s      3ZZ * *)!,80377999999999FFmD))))))))))	* *rH   c                  4  K    d         "t          j        d           d {V   d         "sd S 6                              s t                              dpd           d S  d         j        <   j        r                    d           d S d S )Nr   r  uL   Skipping stale agent promotion for %s — generation %s is no longer currentr5   ro  )r  rw  r7  r  r  r  r  r  )r  r  r)  r9  s   r!   track_agentz-GatewayRunner._run_agent.<locals>.track_agent2  s      q/)mD))))))))) q/) 
 )$2N2N^3 3) b%2"  
 0<QD -~ 8++J777778 8rH   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)r  rw  r  r8   r{  r}  r.  r  r0   r  r  r  r  r  r
  )
r  r   _peek_eventpending_text_mon_err_interrupt_detectedr  r)  r9  r  s
        r!   monitor_for_interruptz7GatewayRunner._run_agent.<locals>.monitor_for_interrupt2  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                  0  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  n2# t          $ r%}t                              d|           Y d }~nd }~ww xY wl)NTrD  r   r5   rU  rF  rA  rj  rB  rC  rG  r  r  rI  u   ⏳ Still working... (rE  rJ  r  z#Long-running notification error: %s)r  r8   r{  r  rw  r)   rD   r}  rU  r1  r2  r
  r  r  r  r  )_notify_adapter_elapsed_mins
_agent_ref_status_detail_arp  _ne_NOTIFY_INTERVAL_notify_startr  r  r)  r  s          r!   _notify_long_runningz6GatewayRunner._run_agent.<locals>._notify_long_running2  s"     '"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)..]]]N]]]!8 /          
 ! M M MLL!FLLLLLLLLM/Ms+   	BD( (
D54D59*E$ $
F.FFr   r  r   i  r  r  r.  zABackup interrupt detected for session %s (monitor task state: %s)r  r  r   rU  r  rD  rp   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  rC  rA  rB  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 rj  z).zLast activity: rH  zs ago, iteration z6). The agent may have been waiting on an API response.zTo increase the limit, set agent.gateway_timeout in config.yaml (value in seconds, 0 = no limit) and restart the gateway.
Try again, or use /reset to start fresh.r+  r  )r  r  r  r  r  r	  r	  r   interruptedinterrupt_messagez5Ignoring control interrupt message for session %s: %sr  z9Processing queued message after agent completion: '%s...'r  pending_steerz0Delivering leftover /steer as next turn: '%s...'r5   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)r  r  r
  ro  z5Stream consumer wait before queued message failed: %srK  final_response_sentr  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  r  rT  r  )
r>  r  rI   r  r  r9  r  rm  r   r  r  zjSuppressing normal final send for session %s: final delivery already confirmed (streamed=%s previewed=%s).r  NNN)cr0  rl  r/  r  r  r(   r  r  r{  r_	  rI	  rd  r8   r&   rO   r:  r  r  rp  r)   r
  r6   r  r  r   r5  rm   Queuer+   rC  r  r  rp  r%  r  r  r  r  r  r@   rD   ensure_futurer`	  r  r  r  r}  r.  r  r0   r  r  r  r  r  rU  r  r  r  _INTERRUPT_REASON_TIMEOUTr1  r2  r  r   r
  re  r=  r  rD  r8  r,   r/  rA  rf   r  r  r  r  r  r~  _MAX_INTERRUPT_DEPTHr  r   rB  r  r  r  r  r  r  r,  r  r  r5
  r  r  r  rQ  r6  )r)  r>  r  rI   r  r  r9  r  rm  r   r  r  rI	  agent_cfg_localdisplay_configrp  _tpl_resolved_tp_env_tp_display_cfg_platforms_cfg_platform_cfg_legacy_tp_overrides_tool_progress_configuredr  r[	  progress_taskrd  r*  r,  tracking_taskr3  interrupt_monitor_NOTIFY_INTERVAL_RAWr>  _notify_task_agent_timeout_raw_agent_timeout_agent_warning_raw_agent_warning_warning_fired_executor_task_inactivity_timeout_POLL_INTERVALr  r  rP  _backup_adapter_backup_agent	_bp_event_bp_textr8  
_idle_secs_act_warn_adapter_elapsed_warn_remaining_mins	_warn_err_timed_out_agent	_activity
_last_desc	_secs_ago	_cur_tool_iter_n	_iter_max_timeout_mins_diag_linesr  _result_for_fb_run_failed
_cfg_modelr  r<  r  rv
  r@  _leftover_steer_pending_parts_pending_cmd_word_rc_pendingr  was_interrupted_scr,  
_previewed_already_streamedfirst_response_bg_cbupdated_historynext_sourcenext_messagenext_message_idnext_channel_prompt_followup_adapter_final_is_empty_sentinel	_streamedr  r   r  r<  r  r2  r  r=  r  r#  r9  r  r  r  r  r  r  rJ	  r  r$  r  r  r  r  r  r  r  r  r  r%  r&  r'  r(  r  s   ````````` `                                                                                      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@r!   r6  zGatewayRunner._run_agent-  s#     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 !&w!%l	$ l	$# l	$# l	$s l	$ae l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$ l	$l ?hn,,"("2"F6F"("2CV`k+>??\`r	+ r	+ r	+ r	+ r	+ r	+ r	+ r	+ r	+ r	+ r	+ r	+ r	+j vv"& !133Z
	>3 	>D 	>T 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>8 -++FO<< .H["e;0C"D"Dae	Oc 	OC 	OD 	O 	O 	O 	O 	O 	O 	O 	O 	O 	OO	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	 O	d   	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@ *+?+?+A+ABBs	 ",,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--//  
 !% C C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_/`/```  '$$&&&$$&&&!!!  !*;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   [ #)**]";";& <% 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++ %% &#, % % % $%%
 ! %WW6P%Q%Q %!(!A!E!EkSW!X!X#F++ %% &#, % % % $% #)**Z"A"A$&"&&*# ,")-4"H"H"RFK)-)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   KC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   ?  '$$&&&$$&&&!!!  !*;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(s  "0E 
E E GAW ;*Z& %AW &
Z30AW 2Z33AAW *\: 9AW :
])]$AW $]))DAW =b AW 
bAW bOAW &0r AW 
r$!AW #r$$D<AW "x??/z/y87z8z
z	z

zz'{00||$AW ,}	 AW 	/9~~~AW 	"=AW A!AW @)A AA* A)AW A*
ABA4ABBAW BABBA&AW D 
AD D
AW D
ADDAW DADDA AW E
AE$ E#AW E$
AE1E.AW E0AE1E1AAW HAH(H(/AI9IAI!I AI9I!AI3I0AI9I2AI3I3AI9I8AI9KAKKAK+K*AK+K0AAW L4"AM MAW M
AM$M!AW M#AM$M$*AW OAO-O-/AP>PAP&P%AP>P&AP8P5AP>P7AP8P8AP>P=AP>RARRAR0R/AR0S7AT T/AU%UAUUAU%UAUUAU%UAUUAU%U$AU%V<AWWAWWAWWAA\XAX<X;A\X</AZY,AY5Y4AZY5AZ	ZAZZAZ	ZAZZ
A\ZAZZAA\[$A[-[,A\[-A[?	[<A\[>A[?	[?A\r  r  rH  )rZ  )r  )r3  r   rv  )r  r~
  r
  rD  )NNr   NN)rG  
__module____qualname____doc__r  r	   r+   r*   __annotations__r  r   r  r  r
   r)   r  r(   r  r  r  r  r  r  Taskr  r  r   r  r  r  r   r0  r  rQ  r  rY  r   rT  r&  rg  rp  rs  r  r  r  r  propertyr  r  r  r  r   r  rO   r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  staticmethodr   r  r  r  r  r  r  r  r  r  r  r-  r  r  r  r6  r   r8  rk  r{  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  rY  r>  r?  r  r  r@  rA  r  r  r-  rP  r  r  r0  r  r  r.  r,  r   r   r  r  r	  r  r  r  r  r  r  r  r  r  r  r  r
  r0  r  r  r4  r&  r  r  r  r  rD  rE  rF  r%  r  rF	  r  r  r  r  r  r  r"  r#  r$  r  r  r  r
  r   r
  r
  r>
  _APPROVAL_TIMEOUT_SECONDSr  r  	frozensetr<  r>  rC  rA  rE  rW  rY  rG  rI  rK  rM  rO  rS  rQ  rU  r]  r_  r~  ro
  r!  r  r:  r|
  r9  r;  r   r&  r8  r`	  r  r  r  r
  r?  r=  rH  r
  classmethodr
  r
  r  r
  r  r  r-  r  r7  r5  r  re  r  r  r'  rg  r0  rl  r6  rc  rH   r!   r  r    sR          ,.S%Z(---'c'''$IEIII $J$$$It$$$$"'4'''#t###!&$&&&)-J&---:<d3S#X#67<<<>@ $sDcN':";@@@ !OU   !e!!!*/!4///w, w,x6 w, w, w, w,t-
 -
 -
 -
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 
 
 
 
 
& +/%)&*H% H% H% 'H% c]	H%
 d^H% 
sDy	H% H% H% H%T*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         
 
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K K K KZ1tCH~ 1$ 1 1 1 1'c 'd ' ' ' 'R 0S T    6/c / / / /b     *   > 38U   4 d W[    "5D 5 5 5 5>G G G G4OT O O O Ob`' `'c `' `' `' `'DK' K'u K't K' K' K' K'Z4        $    \ \ \ \|}' }' }' }'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>H<< H<HSM H< H< H< H<TE E 	E
 d38n%E 
#E E E EN?s ?tCy ? ? ? ?_9# _9_b _9 _9 _9 _9BQ c Q  Q  Q  Q fp6 p6%^H[B\ p6 p6 p6 p6d <  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           ,3 L 3 S 3  3  3  3 je  e (3- e  e  e  e NCJ| CJ CJ CJ CJ CJJ"7 "7# "7 "7 "7 "7NW W W W<D
 D
3 D
 D
 D
 D
LZG ZG 	ZG
 ZG 
ZG ZG ZG ZGxS S S S S S2
L 
S 
 
 
 
* \ 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9,9,&)9,7:9, 9, 9, 9,@ #4 44 4 	4
 4 
4 4 4 4l8\ 8 8 8 8 8 8tcIcI cI
 
cI cI cI cIJ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.L .S . . . .B.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  #!$	O? O?O? O? 	O?
 
O? O? O? O?bV V V V Vp20 20 20 20h
 
4 
 
 
 
(# #$ # # # #
F F F#    .CC #YC 
	C C C CJSS #YS 
	S S S SjA
t A
 A
 A
 A
F$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# $    6     $ 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	  (, !*.(,O OO O d38n%	O
 O O O !O O #3-O !O 
c3hO O O O O OrH   r  rD  
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_pastesrD  r  z"Cron ticker started (interval=%ds)F)r  r  r  zCron tick error: %sNrp   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  r  )rP   s    r!   r  z$_start_cron_ticker.<locals>.<lambda>55  s    6;;}c+J+J rH   )idle_for_seconds
on_summaryzCurator tick error: %szCron ticker stopped)re   r  r&	  r  r  rg
  r  r  r  r  r
  r  r7  r  r  r  r  agent.curatorr  r*   r  )r  r  r  r[  	cron_tickr  r  r  IMAGE_CACHE_EVERYCHANNEL_DIR_EVERYPASTE_SWEEP_EVERYCURATOR_EVERY
tick_countr,  r  futr&
  deletedrS
  r  s                       r!   _start_cron_tickerr  4  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Fr/   	verbosityc                 L  $%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$$%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 )2a7  
    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_markerr  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  )rq   r^  )RedactingFormatter)r   rp   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| 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: %suD   Received SIGTERM as a planned --replace takeover — exiting cleanlyTu/   Received SIGTERM/SIGINT — initiating shutdownpsauxrI  )capture_outputr0   r}   c                     g | ]l}d |                                 v sd|                                 v r>t          t          j                              |                                dd         vj|mS )r  r  rp   rK  )rf   r+   r6   r  rA  )re  rY  s     r!   r8  zBstart_gateway.<locals>.shutdown_signal_handler.<locals>.<listcomp> 6  sq       

,,	TZZ\\0I0I	$$DJJLL1,=== ===rH   u<   Shutdown diagnostic — other hermes processes running:
  %sz
  u7   Shutdown diagnostic — no other hermes processes found)r  r  r
  r  r  r  r  r
  r  rS  r  r2  r  r  r  )planned_takeoverr  r,  _sp_ps_hermes_procs_signal_initiated_shutdownrunners         r!   shutdown_signal_handlerz.start_gateway.<locals>.shutdown_signal_handler5  s    !	@GGGGGG??AA 	@ 	@ 	@LL;Q????????	@  	KKKV    *.&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BD 
DDc                  6                          dd           d S )NFTr  )r  )r  s   r!   restart_signal_handlerz-start_gateway.<locals>.restart_signal_handler6  s"    4@@@@@rH   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)rT  r   r  r  r=   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.)Rr  r  r  r  r  r  r  r6   r  r  r  r  r
  r  ProcessLookupErrorPermissionErrorr   r  r  ri  killrD   rw  r  ri   r  r  r+   printtools.skills_syncr  hermes_loggingr  r  agent.redactr  loggingWARNINGINFOr8   DEBUGStreamHandlersetLevelsetFormatter	getLogger
addHandlerr{	  r  r  rp  r  current_threadmain_threadsignalSIGINTSIGTERMadd_signal_handlerNotImplementedErrorr}  r  atexitr  FileExistsErrorr=
  r!
  r
  r1  rY  r  r  r  r!  r  r  r  r  r  r2  r
  r  
SystemExitr  )&r  r/   r  r  r  r  r  r  r  existing_pidexisting_start_timer  r,  r  rP  r  	_releasedrq   r  r  r  _stderr_level_stderr_handlerr  r  r  sigr  r  _current_pidr
  r  rS  	cron_stopcron_threadr
  r  r  s&                                       @@r!   start_gatewayr  >5  sd
     &                #?$$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 "'.+ .+ .+ .+ .+ .+`A A A A A #%%D!!Y%:%<%<<<M6>2 	 	C''-DEEEE&   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''
Q43Q4 R. .
R;:R;
U .V
V95W/ /
X9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--configrn
  zPath to gateway config file)r  z	--verbosez-v
store_truezVerbose output)r  r  rt   ru   rp   )argparseArgumentParseradd_argument
parse_argsr  r  r  r  r   r  r  r
  r  r  exit)r  parserr   r  r  r  r\  rS  s           r!   r  r  6  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__r  r  )NNrD  )NFr   )r  r  r  r6
  r@  r  r6   rT  r  r  r  r	  r  rD   collectionsr   contextvarsr   r!	  r   r   typingr	   r
   r   r   r   agent.account_usager   r   r  r   r   r*  r  r9   r   r  r+   r  r*   r"   r2   r<   r@   r(   rG   rR   r`   rh   rY   r  rq  rs  rp  ri   utilsrj   rk   rl   rm   r  dotenvrn   hermes_cli.env_loaderro   r  rr  r`  compilerB  rE  _config_pathrZ   r  _yamlr  r  r  r   rw   r[  _key_valr&   r)   r7   r8   _terminal_cfgrO   _terminal_env_map_cfg_key_env_varr  r  rf  _auxiliary_cfg_aux_task_env	_task_key_env_map	_task_cfgr,   _provr  r  r
  
_agent_cfgrN  _tz_cfg_security_cfg_redactrf   r
  _bridge_errr  r  rG  r  r   dir_network_cfgr   r   _configured_cwdr  r;	  	_fallbackr  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   rw  r   rx  r  r  objectr4  r  r  r8  r=  r   r  rG   _INTERRUPT_REASON_SSE_DISCONNECTr  r  r  rB  rD  r\  rz  r  r  r  r  r  r  weakrefr  r  r  r  r  r  r  r  rc  rH   r!   <module>r     sq             				 				  



        # # # # # # $ $ $ $ $ $             3 3 3 3 3 3 3 3 3 3 3 3 3 3 P O O O O O O O % % % % % %  # )- && )0 %* uS#X   4 E    &S Xe_    D< < < < <&S 5 U    $  #'	) ) )) 
%) %	)
 
) ) ) ):d38n1E(F 3    :# # # #J           33ttH~~,344 5 5 5 - , , , , , ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^         4 4 4 4 4 46!	  |h9O9O9Q9Q9YZ[9\_e9e f f f f $$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)))) 	 	 	D		777777 	 	 	D		>>>>>>  """" 	 	 	D	 !
>  !$
  *..44 +/-AAA	/**>cc)$)++.>.>I!*BJ~                             , + + + + +                                
	8	$	$ !&(( $t $ $ $ $N't ' ' ' '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qD qD qD qD qD qD qD qDhIY' Y'9? Y'X[ Y' Y' Y' Y'xJ J 7 J Jbjknbo Jx| J J J JZ
  0 zDFFFFF st    Z- G0$Z- 0G44Z- 7G48R4Z- -[428[//[48A] ]]]- -]54]59^
 
^^