
    PL
jS                       d Z 	 ddlZn# e$ r Y nw xY wddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZ ej        e          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ZddlZddlmZmZmZm Z  ddl!m"Z"m#Z#m$Z$ ddl%m%Z% ddl&m'Z' ddl(m)Z) dd	l*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 dd
l2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8  e)            Z9 e'e:          j;        dz  Z< e5e9e<          Z=e=re=D ]Z>e?                    de>           ne?                    d           ddl@mAZAmBZBmCZCmDZD ddlEmFZFmGZGmHZH ddlEmIZJmKZLmMZMmNZN ddlOmPZPmQZQ ddlRmSZT ddlUmVZV ddlWmXZXmYZYmZZZ ddl[m\Z\ ddl]m^Z^ ddl_m`Z`maZa ddlbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZj ddlkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZu ddlvmwZw ddlxmyZy ddlzm{Z{ dd lbm|Z|m}Z}m~Z~mZmZmZmZmZ dd!lmZmZ dd"lmZmZmZmZ dd#lmZmZmZmZmZ dd$lmZmZmZmZmZ dd%lmZmZ dd&lmZmZmZ dd'lmZmZmZmZmZmZmZmZmZmZmZ dd(lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd)lmZmZmZmZmZ dd*lmZ d+Z ej                    Zd,Zd-efd.Zddd/d0edz  d1edz  d-efd2Zd-efd3Z G d4 d5eզ          Z G d6 d7          Z	 	 	 	 	 	 	 	 	 	 	 	 dId<ed=ed>ed1ed?ed@edAedBedCedDedEedFefdGZedHk    rddlZ ej        e٦           dS dS )Jao  
AI Agent Runner with Tool Calling

This module provides a clean, standalone agent that can execute AI models
with tool calling capabilities. It handles the conversation loop, tool execution,
and response management.

Features:
- Automatic tool calling loop until completion
- Configurable model parameters
- Error handling and recovery
- Message history management
- Support for multiple model providers

Usage:
    from run_agent import AIAgent
    
    agent = AIAgent(base_url="http://localhost:30000/v1", model="claude-opus-4-20250514")
    response = agent.run_conversation("Tell me about the latest Python updates")
    N)SimpleNamespace)ListDictAnyOptional)urlparseparse_qs
urlunparse)datetime)Path)get_hermes_home)OpenAI_OpenAIProxy_load_openai_cls_SafeWriter_install_safe_stdio_get_proxy_from_env_get_proxy_for_base_url)IterationBudget)load_hermes_dotenv)get_provider_request_timeoutget_provider_stale_timeoutz.env)hermes_homeproject_envz$Loaded environment variables from %sz7No .env file found. Using system environment variables.)get_tool_definitionsget_toolset_for_toolhandle_function_callcheck_toolset_requirements)
cleanup_vmget_active_envis_persistent_env)set_approval_callbackset_sudo_password_callback_get_approval_callback_get_sudo_password_callback)maybe_persist_tool_resultenforce_turn_budget)set_interrupt)cleanup_browser)StreamingContextScrubberbuild_memory_context_blocksanitize_context)StreamingThinkScrubber)jittered_backoff)classify_api_errorFailoverReason)DEFAULT_AGENT_IDENTITYPLATFORM_HINTSMEMORY_GUIDANCESESSION_SEARCH_GUIDANCESKILLS_GUIDANCEHERMES_AGENT_HELP_GUIDANCEKANBAN_GUIDANCEbuild_nous_subscription_prompt)
fetch_model_metadataestimate_tokens_roughestimate_messages_tokens_roughestimate_request_tokens_roughget_next_probe_tierparse_context_limit_from_error(parse_available_output_tokens_from_errorsave_context_lengthis_local_endpointquery_ollama_num_ctx)ContextCompressor)SubdirectoryHintTracker)apply_anthropic_cache_control)build_skills_system_promptbuild_context_files_promptbuild_environment_hintsload_soul_mdTOOL_USE_ENFORCEMENT_GUIDANCETOOL_USE_ENFORCEMENT_MODELS!GOOGLE_MODEL_OPERATIONAL_GUIDANCEOPENAI_MODEL_EXECUTION_GUIDANCE)estimate_usage_costnormalize_usage)"_derive_responses_function_call_id_deterministic_call_id_split_responses_tool_id_summarize_user_message_for_log)KawaiiSpinnerbuild_tool_previewget_cute_tool_message_detect_tool_failureget_tool_emoji)ToolCallGuardrailConfigToolCallGuardrailControllerToolGuardrailDecisionappend_toolguard_guidancetoolguard_synthetic_result)FILE_MUTATING_TOOL_NAMESfile_mutation_result_landed)convert_scratchpad_to_thinkhas_incomplete_scratchpadsave_trajectory)_SURROGATE_RE_sanitize_surrogates_sanitize_structure_surrogates_sanitize_messages_surrogates%_escape_invalid_chars_in_json_strings_repair_tool_call_arguments_strip_non_ascii_sanitize_messages_non_ascii_sanitize_tools_non_ascii_strip_images_from_messages_sanitize_structure_non_ascii)_NEVER_PARALLEL_TOOLS_PARALLEL_SAFE_TOOLS_PATH_SCOPED_TOOLS_DESTRUCTIVE_PATTERNS_REDIRECT_OVERWRITE_is_destructive_command_should_parallelize_tool_batch_extract_parallel_scope_path_paths_overlap_is_multimodal_tool_result_multimodal_text_summary!_append_subdir_hint_to_multimodal_extract_file_mutation_targets_extract_error_preview_trajectory_normalize_msg)atomic_json_writebase_url_host_matchesbase_url_hostnameenv_var_enablednormalize_proxy_url)cfg_get   z0.14.1returnc                      ddl m}  dd|  iS )zGReturn the User-Agent RouterMint needs to avoid Cloudflare 1010 blocks.r   )__version__
User-AgentzHermesAgent/)
hermes_clir   )_HERMES_VERSIONs    -/home/kuhnn/.hermes/hermes-agent/run_agent.py_routermint_headersr      s.    999999 	6_66     )providerbase_urlr   r   c                    | dS |                                  sdS |dk    s$t          |pd                              d          rdS t          |                                           dk    S )u?  Decide whether to wait for credential-pool rotation instead of falling back.

    The existing pool-rotation path requires the pool to (1) exist and (2) have
    at least one entry not currently in exhaustion cooldown.  But rotation is
    only meaningful when the pool has more than one entry.

    With a single-credential pool (common for Gemini OAuth, Vertex service
    accounts, and any "one personal key" configuration), the primary entry
    just 429'd and there is nothing to rotate to.  Waiting for the pool
    cooldown to expire means retrying against the same exhausted quota — the
    daily-quota 429 will recur immediately, and the retry budget is burned.

    Additionally, Google CloudCode / Gemini CLI rate limits are ACCOUNT-level
    throttles — even a multi-entry pool shares the same quota window, so
    rotation won't recover.  Skip straight to the fallback for those (#13636).

    In those cases we must fall back to the configured ``fallback_model``
    instead.  Returns True only when rotation has somewhere to go.

    See issues #11314 and #13636.
    NFgoogle-gemini-cli cloudcode-pa://   )has_availablestr
startswithlenentries)poolr   r   s      r   !_pool_may_recover_from_rate_limitr      su    0 |u u &&&#hn"*=*=*H*HIZ*[*[&ut||~~""r   c                      ddl } dt           d|                                                                  d|                                  d}|d|dd	S )
z8Return default HTTP headers required by Qwen Portal API.r   Nz	QwenCode/z (z; )enablez
qwen-oauth)r   zX-DashScope-CacheControlzX-DashScope-UserAgentzX-DashScope-AuthType)platform_QWEN_CODE_VERSIONsystemlowermachine)_plat_uas     r   _qwen_portal_headersr     sf    
X(
X
XELLNN,@,@,B,B
X
Xemmoo
X
X
XC$,!$ ,	  r   c                   f     e Zd ZdZdddddedee         dee         dee         ddf
 fd	Z xZS )
_StreamErrorEventa  Synthesized provider error surfaced from a Responses ``error`` SSE frame.

    Some Codex-style Responses backends (xAI for subscription/quota
    failures, custom relays under malformed-tool-call conditions) emit a
    standalone ``type=error`` frame instead of routing the failure
    through ``response.failed`` or returning an HTTP 4xx.  The fallback
    streaming path raises this exception so ``_summarize_api_error`` and
    ``_extract_api_error_context`` see a familiar ``.body`` /
    ``.status_code`` shape and the entitlement detector can match the
    underlying provider message ("do not have an active Grok
    subscription", etc.).
    N)codeparamstatus_codemessager   r   r   r   c                    t                                          |           || _        || _        || _        || _        d|||ddi| _        d S )Nerror)r   r   r   type)super__init__r   r   r   r   body)selfr   r   r   r   	__class__s        r   r   z_StreamErrorEvent.__init__-  sb     	!!!	
& "	 %
			r   )	__name__
__module____qualname____doc__r   r   intr   __classcell__)r   s   @r   r   r     s         " ##%)
 
 

 sm	

 }
 c]
 

 
 
 
 
 
 
 
 
 
r   r   c            }       |   e Zd ZdZdZedefd            Zej        deddfd            Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dmdededededede	e         dz  dede	e         dz  dede
dedee         dee         dededed ed!e
d"ed#ee         d$ee         d%ee         d&ed'ed(ed)ee         d*ed+ed,ed-ed.ed/ed0ed1ed2ed3ed4ed5ed6e
d7eeef         d8ed9eeef         d:eeeef                  d;ed<ed=ed>ed?ed@edAedBedCedDedEedFedGdHdIeeef         dJedKe
dLe
dMe
dNef|dOZdP ZdndQZdR ZdodSee
         ddfdTZdpdUZdV ZddWdXefdYZdefdZZdefd[Zd\eddfd]Zd\eddfd^Zd_d`lmZ  e!deeef         fda            Z"dbeeef         dceddfddZ#e!dee$defdf            Z%dee$defdgZ&ddhdiedee$dje
dke
dledbeeeef                  ddfdmZ'ddhdee$dje
dke
dledbeeeef                  ddfdnZ(doedpe$ddfdqZ)deeef         fdrZ*dndsZ+dndtZ,dodedefduZ-dodedefdvZ.dodedefdwZ/defdxZ0de1eef         fdyZ2dze	e3eef                  defd{Z4defd|Z5ddddd}dee         dee         dee         dee         de1eef         f
d~Z6e!dedefd            Z7e!dddedee         defd            Z8de
de3fdZ9dedefdZ:dedefdZ;e!dedefd            Z<defdZ=	 dodedzee	         defdZ>dededzeeeef                  defdZ?dee         fdZ@deddfdZAd_dlBmCZCmDZDmEZE e!dee         dee         dee         fd            ZF	 	 dqdee         dededdfdZGddddddee         dee         dee         dee         deeef         f
dZHdzee         ddfdZIdodzee         dee         fdZJdzee         ddfdZKdzee         de
fdZLdodzee         dee         fdZMdzee         dee         fdZNdefdZOdzeeeef                  dededeeeef                  fdZPdzeeeef                  dedefdZQe!deeeef                  dee
         defd            ZRe!deeSdefd            ZTdee         dee         fdZUdedefdZVe!deeSdeeef         fd            ZWdedeeeef                  fdZXdddeeef         dedeeeS         deeY         fdZZe!dedefd            Z[dodzeeeef                  fdZ\dod\eddfdZ]dndZ^dedefdZ_dee         fdZ`dedeeef         dededdf
dZadefdÄZbe!deeeeef         f         defdń            Zcdze	de
ddfdǄZddeddfdɄZedceddfdʄZfd˄ Zgdceddfd̄Zhde3fd̈́Zidodze	ddfd΄Zjdodze	ddfdτZkdedededdfdӄZldndԄZmdndՄZndeeeef                  ddfdׄZoedefd؄            Zpdodedeeef         fdڄZqdodedefdۄZre!defd܄            Zse!defd݄            Zt euh dޣ          Zve!dzeeeef                  deeeef                  fd߄            Zwe!deeef         defd            Zxe!dzeeeef                  deeeef                  fd            Zye!de	de	fd            Zze!de	de	fd            Z{dededz  fdZ|d Z}e!drdedede
defd            Z~e!dede1ee         ee         f         fd            Z	 dodedee         defdZdefdZdefdZdej        fdZe!dedefd            Ze!dsdedefd            Zde3dededefdZe!dede
fd            ZdedededdfdZdedefdZdedefdZdefdZe!de3defd            Zd ede3fdZdddedee3         defdZdededdfdZdtde3dedefdZdode3defdZddWdXedefd	ZddWdXedefd
ZdefdZdefdZdeddfdZdndZddddee
         dedee         deeeef                  de1eef         f
dZdefdZde3fdZdndZde3fdZdndZdeddfdZe!dedefd            ZdedefdZdeeef         ddfdZdeddfdZdeddfdZdeddfdZdefd Zdd!de3defd"Zdodd#defd$Zdefd%Zd&eSd'e
d(e
defd)Ze!dedefd*            Ze!d+ede1eeeY         f         fd,            Zd+ed-edefd.Zdefd/Zded-edefd0Zdodefd1Zd2e	de	fd3Zd2e	de	fd4Zdededefd5Zd2e	defd6Zdefd7Zdefd8Zd2e	de	fd9Zdze	ddfd:Zd2e	de3fd;Zdefd<Zde	e         fd=Zdee         fd>Zde3dz  fd?Zdede3fd@ZdefdAZdefdBZdefdCZdefdDZĐdEe3dFe3ddfdGZe!dFe3de3fdH            Ze!dddIdze	d*ede
fdJ            ZdefdKZddLddMdze	dedNe
dedOede1fdPZɐdQeddfdRZːdQedefdSZdedTe3dUededef
dVZ͐dQedefdWZΐdrdze	dXedYe
ddfdZZϐdTe3defd[Z	 	 dud\edTe3dXedee         dze	d]edefd^Ze!dvd`ededaedefdb            ZҐdrdze	dXedYe
ddfdcZӐdrdze	dXedYe
ddfddZdze	dYe
defdeZ	 	 	 	 	 dwdededeeeef                  dedfee         dgee         deeef         fdhZ֐dod\edfee         defdiZddjdededzeeeef                  dXedkedeeef         fdlZdS (x  AIAgentz
    AI Agent with tool calling capabilities.

    This class manages the conversation flow, tool execution, and response handling
    for AI models that support function calling.
    z[hermes-agent: tool call arguments were corrupted in this session and have been dropped to keep the conversation alive. See issue #15236.]r   c                     | j         S N)	_base_urlr   s    r   r   zAIAgent.base_urlS  s
    ~r   valueNc                 v    || _         |r|                                nd| _        t          |          | _        d S )Nr   )r   r   _base_url_lowerr   _base_url_hostnamer   r   s     r   r   zAIAgent.base_urlW  s8    05=u{{}}}2"3E":":r   r   Z         ?Fd        
   r   api_keyr   api_modeacp_commandacp_argscommandargsmodelmax_iterations
tool_delayenabled_toolsetsdisabled_toolsetssave_trajectoriesverbose_logging
quiet_modeephemeral_system_promptlog_prefix_chars
log_prefixproviders_allowedproviders_ignoredproviders_orderprovider_sortprovider_require_parametersprovider_data_collectionopenrouter_min_coding_score
session_idtool_progress_callbacktool_start_callbacktool_complete_callbackthinking_callbackreasoning_callbackclarify_callbackstep_callbackstream_delta_callbackinterim_assistant_callbacktool_gen_callbackstatus_callback
max_tokensreasoning_configservice_tierrequest_overridesprefill_messagesr   user_id	user_namechat_id	chat_name	chat_type	thread_idgateway_session_keyskip_context_filesload_soul_identityskip_memoryparent_session_iditeration_budgetr   fallback_modelcheckpoints_enabledcheckpoint_max_snapshotscheckpoint_max_total_size_mbcheckpoint_max_file_size_mbpass_session_idcA                    ddl m}A  |A| fi d|d|d|d|d|d|d	|d
|d|	d|
d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d |d!|d"| d#|!d$|"d%|#d&|$d'|%d(|&d)|'d*|(d+|)d,|*d-|+d.|,d/|-d0|.d1|/d2|0d3|1d4|2d5|3d6|4d7|5d8|6d9|7d:|8d;|9d<|:d=|;d>|<d?|=d@|>dA|?dB|@ dCS )Du2   Forwarder — see ``agent.agent_init.init_agent``.r   )
init_agentr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
session_dbr   r   r   credential_poolr  r  r  r  r  N)agent.agent_initr  )Br   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   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  sB                                                                     r   r   zAIAgent.__init__]  s   H 	0/////
B	
 B	
 B	
XB	
 GB	
 X	B	

 XB	
 $B	
 XB	
 GB	
 B	
 %B	
 *>B	
 "zB	
 .-B	
 0/B	
 0/B	
  ,O!B	
" "z#B	
$ %<$;%B	
& .-'B	
( "z)B	
* 0/+B	
, 0/-B	
. ,O/B	
0 (-1B	
2 )D(C3B	
4 &>%=5B	
6 )D(C7B	
8 "z9B	
: $:#9;B	
< !4 3=B	
> $:#9?B	
@ 0/AB	
B  21CB	
D .-EB	
F (-GB	
H #8"7IB	
J (B'AKB	
L 0/MB	
N ,OOB	
P "zQB	
R .-SB	
T &UB	
V 0/WB	
X .-YB	
Z X[B	
\ G]B	
^  i_B	
` GaB	
b  icB	
d  ieB	
f  igB	
h !4 3iB	
j  21kB	
l  21mB	
n $oB	
p "zqB	
r 0/sB	
t .-uB	
v *>wB	
x ,OyB	
z !4 3{B	
| &>%=}B	
~ *F)EB	
@ )D(CAB	
B ,OCB	
 B	
 B	
 B	
r   c                     | j         | j         S 	 ddlm}  |            | _         | j         S # t          $ r'}t                              dd           Y d}~dS d}~ww xY w)ai  Return a SessionDB for recall, lazily creating it if an entrypoint forgot.

        Most frontends pass ``session_db`` into ``AIAgent`` explicitly, but recall
        is important enough that a missing constructor argument should degrade by
        opening the default state DB instead of making the advertised
        ``session_search`` tool unusable.
        Nr   )	SessionDBz SessionDB unavailable for recallTexc_info)_session_dbhermes_stater  	Exceptionloggerdebug)r   r  excs      r   _get_session_db_for_recallz"AIAgent._get_session_db_for_recall  s     '##	......(y{{D## 	 	 	LL;dLKKK44444	s   , 
AAAc           	      Z   | j         s| j        sdS 	 | j                            | j        | j        pt
          j                            dd          | j        | j	        | j
        d| j                   d| _         dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)zDCreate session DB row on first use. Disables _session_db on failure.NHERMES_SESSION_SOURCEcli)r   sourcer   model_configsystem_promptr   r   Tz5Session DB creation failed (will retry next turn): %s)_session_db_createdr  create_sessionr   r   osenvirongetr   _session_init_model_config_cached_system_prompt_parent_session_idr  r  warning)r   es     r   _ensure_db_sessionzAIAgent._ensure_db_session  s    # 	4+; 	F	++?}V
7NPU(V(Vj!<"8"&"9 ,    (,D$$$ 	 	 	 NNG        	s   A&A: :
B*B%%B*c                 $   d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        t          | d          r"| j        r| j                                         dS dS dS )a  Reset all session-scoped token counters to 0 for a fresh session.
        
        This method encapsulates the reset logic for all session-level metrics
        including:
        - Token usage counters (input, output, total, prompt, completion)
        - Cache read/write tokens
        - API call count
        - Reasoning tokens
        - Estimated cost tracking
        - Context compressor internal counters
        
        The method safely handles optional attributes (e.g., context compressor)
        using ``hasattr`` checks.
        
        This keeps the counter reset logic DRY and maintainable in one place
        rather than scattering it across multiple methods.
        r   g        unknownnonecontext_compressorN)session_total_tokenssession_input_tokenssession_output_tokenssession_prompt_tokenssession_completion_tokenssession_cache_read_tokenssession_cache_write_tokenssession_reasoning_tokenssession_api_callssession_estimated_cost_usdsession_cost_statussession_cost_source_user_turn_counthasattrr*  on_session_resetr   s    r   reset_session_statezAIAgent.reset_session_state  s    & %&!$%!%&"%&")*&)*&*+'()%!"*-'#, #)  !" 4-.. 	743J 	7#4466666	7 	7 	7 	7r   config_context_lengthc           
      2   | j         pd                                                                dk    rdS 	 ddlm} ddlm} |t          | dd          }t          |pd|          } || j	        | j
        t          | dd          |          }|rSt          | d	d          }|B|                    | j	        || j
        t          | dd          | j         | j        
           dS dS dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)zT
        Preload the LM Studio model with at least Hermes' minimum context.
        r   lmstudioNr   )MINIMUM_CONTEXT_LENGTH)ensure_lmstudio_model_loaded_config_context_lengthr   r*  )r   context_lengthr   r   r   r   zLM Studio preload skipped: %s)r   stripr   agent.model_metadatar>  hermes_cli.modelsr?  getattrmaxr   r   update_modelr   r  r  r  )r   r;  r>  r?  
target_ctx
loaded_ctxccerrs           r   _ensure_lmstudio_runtime_loadedz'AIAgent._ensure_lmstudio_runtime_loaded6  s~    MR&&((..00J>>F	?CCCCCCFFFFFF$,(/6NPT(U(U%27a9OPPJ55
DM74B+G+G J  
 T#7>>>OO"j'1!% 'i < <!%!% $       ">  	? 	? 	?LL8#>>>>>>>>>	?s   B+C& &
D0DDc                 .    ddl m}  || |||||          S )u?   Forwarder — see ``agent.agent_runtime_helpers.switch_model``.r   )switch_model)agent.agent_runtime_helpersrN  )r   	new_modelnew_providerr   r   r   rN  s          r   rN  zAIAgent.switch_modelW  s.    <<<<<<|D)\7HhWWWr   c                 d    	 | j         pt          } ||i | dS # t          t          f$ r Y dS w xY w)a3  Print that silently handles broken pipes / closed stdout.

        In headless environments (systemd, Docker, nohup) stdout may become
        unavailable mid-session.  A raw ``print()`` raises ``OSError`` which
        can crash cron jobs and lose completed work.

        Internally routes through ``self._print_fn`` (default: builtin
        ``print``) so callers such as the CLI can inject a renderer that
        handles ANSI escape sequences properly (e.g. prompt_toolkit's
        ``print_formatted_text(ANSI(...))``) without touching this method.
        N)	_print_fnprintOSError
ValueError)r   r   kwargsfns       r   _safe_printzAIAgent._safe_print\  sU    	(5BB$ 	 	 	DD	s    //forcer[  c                    t          | dd          rdS |st          | dd          rdS |s|                                 r	| j        sdS  | j        |i | dS )ue  Verbose print — suppressed when actively streaming tokens.

        Pass ``force=True`` for error/warning messages that should always be
        shown even during streaming playback (TTS or display).

        During tool execution (``_executing_tools`` is True), printing is
        allowed even with stream consumers registered because no tokens
        are being streamed at that point.

        After the main response has been delivered and the remaining tool
        calls are post-response housekeeping (``_mute_post_response``),
        all non-forced output is suppressed.

        ``suppress_status_output`` is a stricter CLI automation mode used by
        parseable single-query flows such as ``hermes chat -q``. In that mode,
        all status/diagnostic prints routed through ``_vprint`` are suppressed
        so stdout stays machine-readable.
        suppress_status_outputFN_mute_post_response)rE  _has_stream_consumers_executing_toolsrY  )r   r[  r   rW  s       r   _vprintzAIAgent._vprintn  s    & 41599 	F 	'<eDD 	F 	3355 	d>S 	F$)&)))))r   c                     | j         dS t          t          dd          }|dS 	 t          |                                          S # t
          t          t          f$ r Y dS w xY w)a  Return True when quiet-mode spinner output has a safe sink.

        In headless/stdio-protocol environments, a raw spinner with no custom
        ``_print_fn`` falls back to ``sys.stdout`` and can corrupt protocol
        streams such as ACP JSON-RPC. Allow quiet spinners only when either:
        - output is explicitly rerouted via ``_print_fn``; or
        - stdout is a real TTY.
        NTstdoutF)rS  rE  sysboolisattyAttributeErrorrV  rU  )r   streams     r   _should_start_quiet_spinnerz#AIAgent._should_start_quiet_spinner  sp     >%4h-->5	(((
G4 	 	 	55	s    A A! A!c                 J    | j         o| j         ot          | dd          dk    S )aZ  Return True when quiet-mode tool summaries should print directly.

        Quiet mode is used by both the interactive CLI and embedded/library
        callers. The CLI may still want compact progress hints when no callback
        owns rendering. Embedded/library callers, on the other hand, expect
        quiet mode to be truly silent.
        r   r   r  )r   r   rE  r   s    r    _should_emit_quiet_tool_messagesz(AIAgent._should_emit_quiet_tool_messages  s5     O 7//7j"--6	
r   r   c                    	 |                      | j         | d           n# t          $ r Y nw xY w| j        rF	 |                     d|           dS # t          $ r  t                              dd           Y dS w xY wdS )u  Emit a lifecycle status message to both CLI and gateway channels.

        CLI users see the message via ``_vprint(force=True)`` so it is always
        visible regardless of verbose/quiet mode.  Gateway consumers receive
        it through ``status_callback("lifecycle", ...)``.

        This helper never raises — exceptions are swallowed so it cannot
        interrupt the retry/fallback logic.
        TrZ  	lifecyclez%status_callback error in _emit_statusr  Nra  r   r  r   r  r  r   r   s     r   _emit_statuszAIAgent._emit_status  s    	LLDO6W66dLCCCC 	 	 	D	 	UU$$['::::: U U UDtTTTTTTU	U 	U    # 
00A &A=<A=c                    	 |                      | j         | d           n# t          $ r Y nw xY w| j        rF	 |                     d|           dS # t          $ r  t                              dd           Y dS w xY wdS )a+  Emit a user-visible warning through the same status plumbing.

        Unlike debug logs, these warnings are meant for degraded side paths
        such as auxiliary compression or memory flushes where the main turn can
        continue but the user needs to know something important failed.
        TrZ  warnz&status_callback error in _emit_warningr  Nrn  ro  s     r   _emit_warningzAIAgent._emit_warning  s    	LLDO6W66dLCCCC 	 	 	D	 	VV$$VW55555 V V VEPTUUUUUUV	V 	Vrq  r   )STREAM_DIAG_HEADERSc                  "    ddl m}   |             S )u9   Forwarder — see ``agent.stream_diag.stream_diag_init``.r   stream_diag_init)agent.stream_diagrx  rw  s    r   _stream_diag_initzAIAgent._stream_diag_init  s%     	766666!!!r   diaghttp_responsec                 ,    ddl m}  || ||           dS )uE   Forwarder — see ``agent.stream_diag.stream_diag_capture_response``.r   )stream_diag_capture_responseN)ry  r~  )r   r{  r|  r~  s       r   _stream_diag_capture_responsez%AIAgent._stream_diag_capture_response  s2     	CBBBBB$$T4?????r   r   c                 $    ddl m}  ||           S )u@   Forwarder — see ``agent.stream_diag.flatten_exception_chain``.r   )flatten_exception_chain)ry  r  )r   r  s     r   _flatten_exception_chainz AIAgent._flatten_exception_chain  s'     	>=====&&u---r   c                    t          | dd          dk    rdS t          |t                    sdS t          |t          t          j        f          rdS t          |                                                                          }d|v S )a  Return True for malformed provider streaming data from SDK parsers.

        Some Anthropic-compatible streaming providers can send a malformed
        event-stream frame.  The Anthropic SDK surfaces that as a plain
        ``ValueError`` such as ``expected ident at line 1 column 149``.  That
        is provider wire-format trouble, not local request validation, so it
        should follow the same retry path as a truncated JSON body.
        r   Nanthropic_messagesFexpected ident at line)	rE  
isinstancerV  UnicodeEncodeErrorjsonJSONDecodeErrorr   rB  r   )r   r   r   s      r   _is_provider_stream_parse_errorz'AIAgent._is_provider_stream_parse_error  s     4T**.BBB5%,, 	5e0$2FGHH 	5e**""$$**,,'722r   )r{  kindattemptmax_attemptsmid_tool_callc          	      6    ddl m}  || ||||||           dS )u9   Forwarder — see ``agent.stream_diag.log_stream_retry``.r   )log_stream_retry)r  r   r  r  r  r{  N)ry  r  )r   r  r   r  r  r  r{  r  s           r   _log_stream_retryzAIAgent._log_stream_retry  sJ     	766666t5'%]	
 	
 	
 	
 	
 	
r   c                4    ddl m}  || |||||           dS )u9   Forwarder — see ``agent.stream_diag.emit_stream_drop``.r   )emit_stream_drop)r   r  r  r  r{  N)ry  r  )r   r   r  r  r  r{  r  s          r   _emit_stream_dropzAIAgent._emit_stream_drop	  sH     	766666w\'d	
 	
 	
 	
 	
 	
r   taskr  c                 L   	 |                      |          }n# t          $ r t          |          }Y nw xY w|p|j        j                                        }t          |          dk    r|dd                                         dz   }|                     d| d|            dS )z4Surface a compact warning for failed auxiliary work.   N   ...u   ⚠ Auxiliary z	 failed: )	_summarize_api_errorr  r   r   r   rB  r   rstriprt  )r   r  r  details       r   _emit_auxiliary_failurezAIAgent._emit_auxiliary_failure  s    	..s33FF 	 	 	XXFFF	2CM299;;v;;DSD\((**U2FCDCC6CCDDDDDs    44c           	          t          | dd          pdt          | dd          pdt          | dd          pdt          | dd          pdt          | dd          pddS )zBReturn the live main runtime for session-scoped auxiliary routing.r   r   r   r   r   r   )r   r   r   r   r   )rE  r   s    r   _current_main_runtimezAIAgent._current_main_runtime$  sw     T7B//52j"55;j"55;tY339rj"55;
 
 	
r   c                 (    ddl m}  ||            dS )uY   Forwarder — see ``agent.conversation_compression.check_compression_model_feasibility``.r   )#check_compression_model_feasibilityN)agent.conversation_compressionr  )r   r  s     r   $_check_compression_model_feasibilityz,AIAgent._check_compression_model_feasibility.  s+    VVVVVV++D11111r   c                 (    ddl m}  ||            dS )uP   Forwarder — see ``agent.conversation_compression.replay_compression_warning``.r   )replay_compression_warningN)r  r  )r   r  s     r   _replay_compression_warningz#AIAgent._replay_compression_warning3  s+    MMMMMM""4(((((r   c                     |t          |          }n/t          | dd          pt          t          | dd                    }|dk    S )z8Return True when a base URL targets OpenAI's native API.Nr   r   r   zapi.openai.comr   rE  r   r   hostnames      r   _is_direct_openai_urlzAIAgent._is_direct_openai_url8  s[    (22HHt%92>> BS/44C CH +++r   c                 x    |"t          |                                          }nt          | dd          pd}d|v S )u  Return True when a base URL targets Azure OpenAI.

        Azure OpenAI exposes an OpenAI-compatible endpoint at
        ``{resource}.openai.azure.com/openai/v1`` that accepts the
        standard ``openai`` Python client.  Unlike api.openai.com it
        does NOT support the Responses API — gpt-5.x models are served
        on the regular ``/chat/completions`` path — so routing decisions
        must treat Azure separately from direct OpenAI.
        Nr   r   zopenai.azure.com)r   r   rE  )r   r   urls      r   _is_azure_openai_urlzAIAgent._is_azure_openai_urlB  sE     h--%%''CC$ 1266<"C!S((r   c                     |t          |          }n/t          | dd          pt          t          | dd                    }|dk    S )zKReturn True when a base URL targets GitHub Copilot's OpenAI-compatible API.Nr   r   r   api.githubcopilot.comr  r  s      r   _is_github_copilot_urlzAIAgent._is_github_copilot_urlR  s[    (22HHt%92>> BS/44C CH 222r   c                     t          | j        | j                  }||S t          t	          j        dd                    S )a  Resolve the effective per-call request timeout in seconds.

        Priority:
          1. ``providers.<id>.models.<model>.timeout_seconds`` (per-model override)
          2. ``providers.<id>.request_timeout_seconds`` (provider-wide)
          3. ``HERMES_API_TIMEOUT`` env var (legacy escape hatch)
          4. 1800.0s default

        Used by OpenAI-wire chat completions (streaming and non-streaming) so
        the per-provider config knob wins over the 1800s default.  Without this
        helper, the hardcoded ``HERMES_API_TIMEOUT`` fallback would always be
        passed as a per-call ``timeout=`` kwarg, overriding the client-level
        timeout the AIAgent.__init__ path configured.
        NHERMES_API_TIMEOUTg      @)r   r   r   floatr  getenv)r   cfgs     r   _resolved_api_call_timeoutz"AIAgent._resolved_api_call_timeout\  s<     +4=$*EE?JRY3V<<===r   c                     t          | j        | j                  }||dfS t          j        d          }|t          |          dfS dS )a9  Resolve the base non-stream stale timeout and whether it is implicit.

        Priority:
          1. ``providers.<id>.models.<model>.stale_timeout_seconds``
          2. ``providers.<id>.stale_timeout_seconds``
          3. ``HERMES_API_CALL_STALE_TIMEOUT`` env var
          4. 300.0s default

        Returns ``(timeout_seconds, uses_implicit_default)`` so the caller can
        preserve legacy behaviors that only apply when the user has *not*
        explicitly configured a stale timeout, such as auto-disabling the
        detector for local endpoints.
        NFHERMES_API_CALL_STALE_TIMEOUT)g     r@T)r   r   r   r  r  r  )r   r  env_timeouts      r   %_resolved_api_call_stale_timeout_basez-AIAgent._resolved_api_call_stale_timeout_basep  sT     )
CC?:i ?@@"%%u,,{r   messagesc                 <   |                                  \  }}t          | dd          p| j        pd}|r |rt          |          rt	          d          S t          d |D                       dz  }|dk    rt          |d          S |d	k    rt          |d
          S |S )z@Compute the effective non-stream stale timeout for this request.r   Nr   infc              3   N   K   | ] }t          t          |                    V  !d S r   )r   r   ).0vs     r   	<genexpr>z<AIAgent._compute_non_stream_stale_timeout.<locals>.<genexpr>  s.      77SVV777777r      i g     @iP  g      |@)r  rE  r   rA   r  sumrF  )r   r  
stale_baseuses_implicit_defaultr   
est_tokenss         r   !_compute_non_stream_stale_timeoutz)AIAgent._compute_non_stream_stale_timeout  s    ,0,V,V,X,X)
)4d33Jt}J  	 X 	 2CH2M2M 	 <<77h777771<
z5)))z5)))r   c                 ,    t          | j        d          S )z1Return True when the base URL targets OpenRouter.openrouter.air~   r   r   s    r   _is_openrouter_urlzAIAgent._is_openrouter_url  s    $T%9?KKKr   r   r   r   r   c                .    ddl m}  || ||||          S )uP   Forwarder — see ``agent.agent_runtime_helpers.anthropic_prompt_cache_policy``.r   )anthropic_prompt_cache_policyr  )rO  r  )r   r   r   r   r   r  s         r   _anthropic_prompt_cache_policyz&AIAgent._anthropic_prompt_cache_policy  s5     	NMMMMM,,THxbjrwxxxxr   c                     |                                  }d|v r|                    dd          d         }|                    d          S )a>  Return True for models that require the Responses API path.

        GPT-5.x models are rejected on /v1/chat/completions by both
        OpenAI and OpenRouter (error: ``unsupported_api_for_model``).
        Detect these so the correct api_mode is set regardless of
        which provider is serving the model.
        /r   zgpt-5)r   rsplitr   )r   ms     r   _model_requires_responses_apiz%AIAgent._model_requires_responses_api  sD     KKMM!88a  $A||G$$$r   r   c                    |pd                                                                 }|dk    rdS |dk    r"	 ddlm}  ||           S # t          $ r Y nw xY wt
                              |           S )zCReturn True when this provider/model pair should use Responses API.r   nousFcopilotr   )!_should_use_copilot_responses_api)rB  r   rD  r  r  r   r  )r   r   normalized_providerr  s       r   &_provider_model_requires_responses_apiz.AIAgent._provider_model_requires_responses_api  s      (~24466<<>> &((5)++OOOOOO88???     44U;;;s   A	 	
AAc                     |                                  s(|                                 s|                                 rd|iS d|iS )av  Return the correct max tokens kwarg for the current provider.

        OpenAI's newer models (gpt-4o, o-series, gpt-5+) require
        'max_completion_tokens'. Azure OpenAI also requires
        'max_completion_tokens' for gpt-5.x models served via the
        OpenAI-compatible endpoint. OpenRouter, local models, and older
        OpenAI models use 'max_tokens'.
        max_completion_tokensr   )r  r  r  r   s     r   _max_tokens_paramzAIAgent._max_tokens_param  sT     %%'' 	44+D+D+F+F 	4$JeJeJgJg 	4+U33e$$r   contentc                 v    |sdS |                      |          }t          |                                          S )a  
        Check if content has actual text after any reasoning/thinking blocks.

        This detects cases where the model only outputs reasoning but no actual
        response, which indicates an incomplete generation that should be retried.
        Must stay in sync with _strip_think_blocks() tag variants.

        Args:
            content: The assistant message content to check

        Returns:
            True if there's meaningful content after think blocks, False otherwise
        F)_strip_think_blocksre  rB  )r   r  cleaneds      r   _has_content_after_think_blockz&AIAgent._has_content_after_think_block  s>      	5 **733 GMMOO$$$r   c                 &    ddl m}  || |          S )uE   Forwarder — see ``agent.agent_runtime_helpers.strip_think_blocks``.r   )strip_think_blocks)rO  r  )r   r  r  s      r   r  zAIAgent._strip_think_blocks  s'    BBBBBB!!$000r   c                 |    | sdS |                                  }|sdS |                    d          rdS |d         dv S )zCHeuristic: does visible assistant text look intentionally finished?Fz```Tr  u$   .!?:)"']}。！？：）】」』》)r  endswith)r  strippeds     r   _has_natural_response_endingz$AIAgent._has_natural_response_ending  sX      	5>>## 	5U## 	4|FFFr   c                     | j         pd                                }| j        pd                                }d|vr|dk    rdS d| j        v s	d| j        v rdS t	          | j        ot          | j                            S )zHDetect the narrow backend family affected by Ollama/GLM stop misreports.r   glmzaiFollamaz:11434T)r   r   r   r   re  r   rA   )r   model_lowerprovider_lowers      r   _is_ollama_glm_backendzAIAgent._is_ollama_glm_backend   s    z'R..00--24466##%(?(?5t+++x4;O/O/O4DMF&7&F&FGGGr   finish_reasonc                    |dk    s| j         dk    rdS |                                 sdS t          d |pg D                       sdS |t          |dd          rdS t          |dd          }t	          |t
                    sdS |                     |                                          }|sdS t          |          dk     st          j
        d	|          sdS |                     |           S )
zIDetect conservative stop->length misreports for Ollama-hosted GLM models.stopchat_completionsFc              3   r   K   | ]2}t          |t                    o|                    d           dk    V  3dS )roletoolN)r  dictr   )r  msgs     r   r  z:AIAgent._should_treat_stop_as_truncated.<locals>.<genexpr>  sT       
 
 sD!!?cggfoo&?
 
 
 
 
 
r   N
tool_callsr  r   z\s)r   r  anyrE  r  r   r  rB  r   researchr  )r   r  assistant_messager  r  visible_texts         r   _should_treat_stop_as_truncatedz'AIAgent._should_treat_stop_as_truncated
  s&    F""dm7I&I&I5**,, 	5 
 
 B
 
 
 
 
 	 5$0A<QU(V(V$5+Y=='3'' 	5//88>>@@ 	5|r!!5,)G)G!544\BBBBr   user_messageassistant_contentc                 *    ddl m}  || |||          S )uT   Forwarder — see ``agent.agent_runtime_helpers.looks_like_codex_intermediate_ack``.r   )!looks_like_codex_intermediate_ack)rO  r  )r   r  r  r  r  s        r   "_looks_like_codex_intermediate_ackz*AIAgent._looks_like_codex_intermediate_ack)  s0     	RQQQQQ00|EVX`aaar   c                 &    ddl m}  || |          S )uD   Forwarder — see ``agent.agent_runtime_helpers.extract_reasoning``.r   )extract_reasoning)rO  r	  )r   r   r	  s      r   _extract_reasoningzAIAgent._extract_reasoning3  s(    AAAAAA  '8999r   task_idc                 &    ddl m}  || |          S )uK   Forwarder — see ``agent.chat_completion_helpers.cleanup_task_resources``.r   )cleanup_task_resources)agent.chat_completion_helpersr  )r   r  r  s      r   _cleanup_task_resourceszAIAgent._cleanup_task_resources8  s'    HHHHHH%%dG444r   )_MEMORY_REVIEW_PROMPT_SKILL_REVIEW_PROMPT_COMBINED_REVIEW_PROMPTreview_messagesprior_snapshotc                 &    ddl m}  || |          S )uR   Forwarder — see ``agent.background_review.summarize_background_review_actions``.r   )#summarize_background_review_actions)agent.background_reviewr  )r  r  r  s      r   $_summarize_background_review_actionsz,AIAgent._summarize_background_review_actionsF  s*     	POOOOO22?NSSSr   messages_snapshotreview_memoryreview_skillsc                     ddl m}  || |||          \  }}t          j        |dd          }|                                 dS )u^  Spawn the background memory/skill review thread.

        Thin wrapper — the heavy lifting lives in
        ``agent.background_review.spawn_background_review_thread`` which
        returns the thread target.  ``threading.Thread`` is constructed
        here so existing tests that patch ``run_agent.threading.Thread``
        keep working.
        r   )spawn_background_review_thread)r  r  Tz	bg-review)targetdaemonnameN)r  r  	threadingThreadstart)r   r  r  r  r  r  _promptts           r   _spawn_background_reviewz AIAgent._spawn_background_reviewO  sh     	KJJJJJ88''	
 
 
 F4kJJJ						r   write_originexecution_contextr  tool_call_idr(  r)  r*  c                .    ddl m}  || ||||          S )uJ   Forwarder — see ``agent.background_review.build_memory_write_metadata``.r   )build_memory_write_metadatar'  )r  r,  )r   r(  r)  r  r*  r,  s         r   _build_memory_write_metadataz$AIAgent._build_memory_write_metadatag  s@     	HGGGGG**%/%
 
 
 	
r   c                    t          | dd          }t          | dd          }||dS d|cxk    rt          |          k     rAn dS ||         }t          |t                    r"|                    d          dk    r||d<   dS dS dS dS )a  Rewrite the current-turn user message before persistence/return.

        Some call paths need an API-only user-message variant without letting
        that synthetic text leak into persisted transcripts or resumed session
        history. When an override is configured for the active turn, mutate the
        in-memory messages list in place so both persistence and returned
        history stay clean.
        _persist_user_message_idxN_persist_user_message_overrider   r  userr  )rE  r   r  r  r   )r   r  idxoverrider  s        r   $_apply_persist_user_message_overridez,AIAgent._apply_persist_user_message_overridey  s     d7>>4!A4HHs{F####c(mm######3-C#t$$ *F)B)B!)I $#* *)B)Br   conversation_historyc                     |                      |           |                     |           || _        |                     |           |                     ||           dS )zSave session state to both JSON log and SQLite on any exit path.

        Ensures conversations are never lost, even on errors or early returns.
        N))_drop_trailing_empty_response_scaffoldingr4  _session_messages_save_session_log_flush_messages_to_session_db)r   r  r5  s      r   _persist_sessionzAIAgent._persist_session  se    
 	66x@@@11(;;;!)x(((**85IJJJJJr   c                    d}|rt          |d         t                    r|d                             d          s|d                             d          ri|                                 d}|rQt          |d         t                    r6|d                             d          N|d                             d          i|sdS |rt          |d         t                    ro|d                             d          dk    rP|                                 |r:t          |d         t                    r|d                             d          dk    P|rkt          |d         t                    rR|d                             d          d	k    r5|d                             d
          r|                                 dS dS dS dS dS )u  Remove private empty-response retry/failure scaffolding from transcript tails.

        Also rewinds past any trailing tool-result / assistant(tool_calls) pair
        that the failed iteration left hanging. Without this, the tail ends at
        a raw ``tool`` message and the next user turn lands as
        ``...tool, user, user`` — a protocol-invalid sequence that most
        providers silently reject (returns empty content), causing the
        empty-retry loop to fire forever. See #<TBD>.
        Fr  _empty_recovery_synthetic_empty_terminal_sentinelTNr  r  	assistantr  )r  r  r   pop)r   r  dropped_scaffoldings      r   r7  z1AIAgent._drop_trailing_empty_response_scaffolding  s    $		'8B<..		'   !<==			'
 B<##$>??		' LLNNN"& 		'8B<..		'   !<==			'
 B<##$>??		'" # 	F 	8B<..	   ((F22LLNNN	 	8B<..	   ((F22 	8B<..	   ((K77  .. 8 LLNNNNN	 	 	 	 8777r   c                 &    ddl m}  || |          S )uJ   Forwarder — see ``agent.agent_runtime_helpers.repair_message_sequence``.r   )repair_message_sequence)rO  rC  )r   r  rC  s      r   _repair_message_sequencez AIAgent._repair_message_sequence  s'    GGGGGG&&tX666r   c                    | j         sdS |                     |           	 | j        s|                                  |rt	          |          nd}t          || j                  }||d         D ]}|                    dd          }|                    d          }t          |          rt          |          }nt          |t                    rg }|D ]}	t          |	t                    rP|	                    d          dk    r7|                    t          |	                    dd                               gt          |	t                    r,|	                    d          d	v r|                    d
           |rd                    |          nd}d}
t!          |d          r3t          |j        t                    r|j        rd |j        D             }
n0t          |                    d          t                    r|d         }
| j                             | j        |||                    d          |
|                    d          |                    d          |dk    r|                    d          nd|dk    r|                    d          nd|dk    r|                    d          nd|dk    r|                    d          nd|dk    r|                    d          nd           t	          |          | _        dS # t(          $ r&}t*                              d|           Y d}~dS d}~ww xY w)u%  Persist any un-flushed messages to the SQLite session store.

        Uses _last_flushed_db_idx to track which messages have already been
        written, so repeated calls (from multiple exit paths) only write
        truly new messages — preventing the duplicate-write bug (#860).
        Nr   r  r(  r  r   textr   >   image	image_urlinput_imagez[screenshot]
r  c                 @    g | ]}|j         j        |j         j        d S ))r   	arguments)functionr   rL  r  tcs     r   
<listcomp>z9AIAgent._flush_messages_to_session_db.<locals>.<listcomp>  s:     ' ' ' "$!1@UVV' ' 'r   	tool_namer*  r  r?  	reasoningreasoning_contentreasoning_detailscodex_reasoning_itemscodex_message_items)r   r  r  rQ  r  r*  r  rR  rS  rT  rU  rV  z$Session DB append_message failed: %s)r  r4  r  r&  r   rF  _last_flushed_db_idxr   rw   rx   r  listr  appendr   joinr8  r  append_messager   r  r  r$  )r   r  r5  	start_idx
flush_fromr  r  r  _txtptool_calls_datar%  s               r   r:  z%AIAgent._flush_messages_to_session_db  s~     	F11(;;;/	F+ *'')))5IP0111qIY(ABBJ
, & &wwvy11''),, .g66 
@6w??GG.. @D$ 8 8%a.. 8155==F3J3J KKAEE&",=,=(>(>????'400 8QUU6]]Fk5k5k KK77715?diiooo4G"&3-- 8*S^T2R2R 8WZWe 8' '"%.' ' 'OO   5 5t<< 8&),&7O //##!ggk22.!$!8!8"%''/":":6:k6I6Icggk222tFJkFYFYcgg.A&B&B&B_cFJkFYFYcgg.A&B&B&B_cNRVaNaNa#''2I*J*J*JgkJNR]J]J]0E(F(F(Fcg 0     ),HD%%% 	F 	F 	FNNA1EEEEEEEEE	Fs   K+L 
L=L88L=c                     |sg S d}t          t          |          dz
  dd          D ]%}||                             d          dk    r|} n&||                                S |d|         S )a  
        Get messages up to (but not including) the last assistant turn.
        
        This is used when we need to "roll back" to the last successful point
        in the conversation, typically when the final assistant message is
        incomplete or malformed.
        
        Args:
            messages: Full message list
            
        Returns:
            Messages up to the last complete assistant turn (ending with user/tool message)
        Nr   r  r  r?  )ranger   r   copy)r   r  last_assistant_idxis       r   "_get_messages_up_to_last_assistantz*AIAgent._get_messages_up_to_last_assistant  s      	I "s8}}q("b11 	 	A{v&&+55%&" 6 %==??" +++,,r   c                 $    ddl m}  ||           S )uJ   Forwarder — see ``agent.system_prompt.format_tools_for_system_message``.r   )format_tools_for_system_message)agent.system_promptrh  )r   rh  s     r    _format_tools_for_system_messagez(AIAgent._format_tools_for_system_message*  s%    GGGGGG..t444r   
user_query	completedc                 *    ddl m}  || |||          S )uO   Forwarder — see ``agent.agent_runtime_helpers.convert_to_trajectory_format``.r   )convert_to_trajectory_format)rO  rn  )r   r  rk  rl  rn  s        r   _convert_to_trajectory_formatz%AIAgent._convert_to_trajectory_format/  s+    LLLLLL++D(J	RRRr   c                 r    | j         sdS |                     |||          }t          || j        |           dS )a  
        Save conversation trajectory to JSONL file.
        
        Args:
            messages (List[Dict]): Complete message history
            user_query (str): Original user query
            completed (bool): Whether the conversation completed successfully
        N)r   ro  _save_trajectory_to_filer   )r   r  rk  rl  
trajectorys        r   _save_trajectoryzAIAgent._save_trajectory4  sE     % 	F77*iXX
 TZCCCCCr   error_contextr   c                    |dvrdS t          | t                    sdS t          |                     d          pd                                          }t          |                     d          pd                                          }| d| }|                                sdS d|v rdS d	|v rd
|v rdS d|v rd
|v rdS dS )a.  Detect subscription/entitlement 403s that masquerade as auth failures.

        Returned True only when the body text matches a known entitlement
        shape AND the status is 401/403.  Refreshing an OAuth token cannot
        fix an unsubscribed account, so callers should surface the error
        instead of looping the credential pool.

        Current matches:
          * xAI OAuth: "do not have an active Grok subscription" /
            "out of available resources" / "does not have permission" + "grok"

        Extend here for new providers as we discover them (Anthropic's
        Claude Max OAuth entitlement errors look distinct enough today that
        the existing 1M-context-beta branch handles them; revisit if other
        subscription tiers start producing the same loop signature).
        >   N    Fr   r   reason z'do not have an active grok subscriptionTzout of available resourcesgrokzdoes not have permission)r  r  r   r   r   rB  )rt  r   r   rx  haystacks        r   _is_entitlement_failurezAIAgent._is_entitlement_failureC  s    * ...5-.. 	5m''	228b99??AA]&&x006B77==??((((~~ 	54@@4'833(8J8J4%11f6H6H4ur   c                 X   t          |           }t          | t                    r#d|                                v rd|dd          S d|v sd|v rt	          j        d|t          j                  }|r'|                    d                                          nd	}t	          j        d
|          }|r'|                    d                                          nd}t          | dd          }g }|r|
                    d|            |
                    |           |r|
                    d|            d                    |          S t          | dd          }t          |t                    rt          |                    d          t                    r)|                    di                               d          n|                    d          }	|	r)t          | dd          }|rd| dnd}
|
 |	dd          S t          | dd          }|rd| dnd}
|
 |dd          S )zExtract a human-readable one-liner from an API error.

        Handles Cloudflare HTML error pages (502, 503, etc.) by pulling the
        <title> tag instead of dumping raw HTML.  Falls back to a truncated
        str(error) for everything else.
        r  z'Malformed provider streaming response: Ni,  z	<!DOCTYPE<htmlz<title[^>]*>([^<]+)</title>r   z!HTML error page (title not found)z2Cloudflare Ray ID:\s*<strong[^>]*>([^<]+)</strong>r   zHTTP zRay u    — r   r   r   : r   r   )r   r  rV  r   r  r  
IGNORECASEgrouprB  rE  rY  rZ  r  r   )r   rawr  titlerayray_idr   partsr   r  prefixs              r   r  zAIAgent._summarize_api_errori  s^    %jj uj))	I(CIIKK77HS#YHHH #C	8#r}MMA*+TAGGAJJ$$&&&1TE)QSVWWC-0:SYYq\\'')))dF!%==KE 42[22333LL ._F__---<<&&& ufd++dD!! 	.:DTXXgEVEVX\:]:]v$((7B''++I666cgckcklucvcvC .%e]DAA4?G00000R -#dsd)--- e]D99,7?(((((R%#dsd)%%%r   keyc                 b    |sd S t          |          dk    rdS |d d          d|dd           S )N   z***r   r  )r   )r   r  s     r   _mask_api_key_for_logszAIAgent._mask_api_key_for_logs  sG     	4s88r>>5bqb'((c"##h(((r   	error_msgc                     |sdS |                                                     d          sd|v rdS d                    |                                          }t	          |          dk    r|dd         dz   }|S )	a  
        Clean up error messages for user display, removing HTML content and truncating.
        
        Args:
            error_msg: Raw error message from API or exception
            
        Returns:
            Clean, user-friendly error message
        zUnknown errorz<!DOCTYPE htmlr~  z:Service temporarily unavailable (HTML error page returned)ry     Nr  )rB  r   rZ  splitr   )r   r  r  s      r   _clean_error_messagezAIAgent._clean_error_message  s      	#"? ??''(899 	PW	=Q=QOO ((9??,,-- w<<#dsdme+Gr   c                 $    ddl m}  ||           S )uL   Forwarder — see ``agent.agent_runtime_helpers.extract_api_error_context``.r   )extract_api_error_context)rO  r  )r   r  s     r   _extract_api_error_contextz"AIAgent._extract_api_error_context  s(     	JIIIII((///r   responsec                     |dS t          |dd          }|sdS ddlm} t          || j        | j                  } ||          }|                    dd           |j        |d<   |j        |d<   |S )	zLToken buckets for ``post_api_request`` plugins (no raw ``response`` object).Nusager   )asdict)r   r   	raw_usageprompt_tokenstotal_tokens)	rE  dataclassesr  rO   r   r   r@  r  r  )r   r  r  r  cusummarys         r   #_usage_summary_for_api_request_hookz+AIAgent._usage_summary_for_api_request_hook  s    4Hgt44	 	4&&&&&&YWWW&**K&&&#%#3 "$/r   )r   
api_kwargsrx  c                ,    ddl m}  || |||          S )uI   Forwarder — see ``agent.agent_runtime_helpers.dump_api_request_debug``.r   )dump_api_request_debug)rx  r   )rO  r  )r   r  rx  r   r  s        r   _dump_api_request_debugzAIAgent._dump_api_request_debug  s1     	GFFFFF%%dJvUSSSSr   c                     | s| S t          |           } t          j        dd|           } t          j        dd|           } |                                 S )zCConvert REASONING_SCRATCHPAD to think tags and clean up whitespace.z\n+(<think>)z\n\1z(</think>)\n+z\1\n)r`   r  subrB  )r  s    r   _clean_session_contentzAIAgent._clean_session_content  sS      	N-g66&'7;;&)7G<<}}r   c                 <   |p| j         }|sdS 	 g }|D ]r}|                    d          dk    rB|                    d          r-t          |          }|                     |d                   |d<   |                    |           s| j                                        r	 t          j        | j        	                    d                    }|                    dt          |                    dg                               }|t          |          k    r%t          j        d	|t          |                     dS n# t          $ r Y nw xY w| j        | j        | j        | j        | j                                        t)          j                                                    | j        pd
| j        pg t          |          |d
}t1          | j        |dt2                     dS # t          $ r/}| j        rt          j        d|            Y d}~dS Y d}~dS d}~ww xY w)a  
        Save the full raw session to a JSON file.

        Stores every message exactly as the agent sees it: user messages,
        assistant messages (with reasoning, finish_reason, tool_calls),
        tool responses (with tool_call_id, tool_name), and injected system
        messages (compression summaries, todo snapshots, etc.).

        REASONING_SCRATCHPAD tags are converted to <think> blocks for consistency.
        Overwritten after each turn so it always reflects the latest state.
        Nr  r?  r  utf-8encodingmessage_countr  zHSkipping session log overwrite: existing has %d messages, current has %dr   )
r   r   r   r   session_startlast_updatedr  toolsr  r     )indentdefaultzFailed to save session log: )r8  r   r  r  rY  session_log_fileexistsr  loads	read_textr   loggingr  r  r   r   r   r   r  	isoformatr   nowr"  r  r}   r   r   r$  )r   r  r  r  existingexisting_countentryr%  s           r   r9  zAIAgent._save_session_log  s    5t5 	F0	DG $ $776??k11cggi6H6H1s))C%)%@%@Y%P%PC	Ns#### $++-- 
#z$*?*I*ISZ*I*[*[\\H%-\\/3x||T^`bGcGcCdCd%e%eN%G44f*CLL    5 !   D #o M M!%!3!=!=!?!? ( 8 8 : :!%!;!Ar)r!$W# E %	       	D 	D 	D# D Bq B BCCCCCCCCCD D D D D D	Ds>   BG"  BD= <G" =
E
G" 	E

BG" "
H,HHc                    d| _         || _        | j        t          d| j                   d| _        nd| _        t          | dd          }t          | dd          }|Q|O|5  t          |          }ddd           n# 1 swxY w Y   |D ]#}	 t          d|           # t          $ r Y  w xY w| j        5  t          | j	                  }ddd           n# 1 swxY w Y   |D ]J}	 |
                    |           # t          $ r%}t                              d|           Y d}~Cd}~ww xY w| j        s?t          d|r!t          |          dk    rd	|dd          d
n	|rd	| dndz              dS dS )a  
        Request the agent to interrupt its current tool-calling loop.
        
        Call this from another thread (e.g., input handler, message receiver)
        to gracefully stop the agent and process a new message.
        
        Also signals long-running tool executions (e.g. terminal commands)
        to terminate early, so the agent can respond immediately.
        
        Args:
            message: Optional new message that triggered the interrupt.
                     If provided, the agent will include this in its response context.
        
        Example (CLI):
            # In a separate input thread:
            if user_typed_something:
                agent.interrupt(user_input)
        
        Example (Messaging):
            # When new message arrives for active session:
            if session_has_running_agent:
                running_agent.interrupt(new_message.text)
        TNF_tool_worker_threads_tool_worker_threads_lockz0Failed to propagate interrupt to child agent: %su   
⚡ Interrupt requested(   z: 'z...''r   )_interrupt_requested_interrupt_message_execution_thread_id_set_interrupt _interrupt_thread_signal_pendingrE  rX  r  _active_children_lock_active_children	interruptr  r  r   rT  r   )	r   r   _tracker_tracker_lock_worker_tids_wtidchildren_copychildr%  s	            r   r  zAIAgent.interrupt%  s   0 %)!") $04!:;;;49D11 59D1 4!7>>&A4HHM$= . .#H~~. . . . . . . . . . . . . . .%  "4////    D ' 	8 	8 !677M	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8" 	T 	TET(((( T T TOQRSSSSSSSST 	V-W  2TY\]dYeYehjYjYj1Iwss|1I1I1I1I  EL  qT  qAv}  qA  qA  qA  qA  RT  U  V  V  V  V  V	V 	VsN   "A>>BBB
B,+B,6CCC'C==
D,D''D,c                    d| _         d| _        d| _        | j        t	          d| j                   t          | dd          }t          | dd          }|Q|O|5  t          |          }ddd           n# 1 swxY w Y   |D ]#}	 t	          d|           # t          $ r Y  w xY wt          | dd          }|$|5  d| _        ddd           dS # 1 swxY w Y   dS dS )zMClear any pending interrupt request and the per-thread tool interrupt signal.FNr  r  _pending_steer_lock)	r  r  r  r  r  rE  rX  r  _pending_steer)r   r  r  r  r  _steer_locks         r   clear_interruptzAIAgent.clear_interrupti  s   $)!"&05-$05$";<<< 4!7>>&A4HHM$= . .#H~~. . . . . . . . . . . . . . .%  "5%0000    D d$94@@" + +&*#+ + + + + + + + + + + + + + + + + + #"s6   A66A:=A:B
B$#B$<CCCrF  c                 B   |r|                                 sdS |                                 }t          | dd          }|$t          | dd          }|r|dz   |z   n|| _        dS |5  | j        r| j        dz   |z   | _        n|| _        ddd           n# 1 swxY w Y   dS )a  
        Inject a user message into the next tool result without interrupting.

        Unlike interrupt(), this does NOT stop the current tool call. The
        text is stashed and the agent loop appends it to the LAST tool
        result's content once the current tool batch finishes. The model
        sees the steer as part of the tool output on its next iteration.

        Thread-safe: callable from gateway/CLI/TUI threads. Multiple calls
        before the drain point concatenate with newlines.

        Args:
            text: The user text to inject. Empty strings are ignored.

        Returns:
            True if the steer was accepted, False if the text was empty.
        Fr  Nr  rJ  T)rB  rE  r  )r   rF  r  _lockr  s        r   steerzAIAgent.steer  s   $  	4::<< 	5**,,3T::= t%5t<<HAI"V8d?W#<#<wD4 	. 	." .&*&9D&@7&J##&-#		. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.
 ts   &"BBBc                     t          | dd          }|t          | dd          }d| _        |S |5  | j        }d| _        ddd           n# 1 swxY w Y   |S )zReturn the pending steer text (if any) and clear the slot.

        Safe to call from the agent execution thread after appending tool
        results. Returns None when no steer is pending.
        r  Nr  )rE  r  )r   r  rF  s      r   _drain_pending_steerzAIAgent._drain_pending_steer  s     3T::=4!1488D"&DK 	' 	'&D"&D	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' s   AAArQ  resultis_errorc                    |t           vrdS t          | dd          }|dS t          ||          }|sdS t          ||          }|r$|s"t	          |          }|D ]}	|	|vr||d||	<   dS |D ]}	|                    |	d           dS )a  Record a ``write_file`` / ``patch`` outcome for the turn-end verifier.

        On failure, store ``{path: {error_preview, tool}}`` entries.  On
        success, remove any prior failure entries for the same paths (the
        model recovered within the turn).  Silently no-ops if the per-turn
        state dict hasn't been initialised yet (e.g. a tool dispatched
        outside ``run_conversation``).
        N_turn_failed_file_mutations)r  error_preview)_FILE_MUTATING_TOOLSrE  rz   r_   r{   r@  )
r   rQ  r   r  r  statetargetslandedpreviewpaths
             r   _record_file_mutation_resultz$AIAgent._record_file_mutation_result  s     000F;TBB=F0DAA 	F,Y?? 	&F 	&,V44G   u$$ ))0# #E$K    & &		$%%%%& &r   c                    	 ddl }|j                            d          }|(|                                                                dvS 	 ddlm}  |            pi }n# t          $ r i }Y nw xY wt          |t                    r|                    d          nd}t          |t                    r&d|v r"t          |                    d                    S n# t          $ r Y nw xY wdS )	ao  Check whether the per-turn file-mutation verifier footer is on.

        Config path: ``display.file_mutation_verifier`` (bool, default True).
        ``HERMES_FILE_MUTATION_VERIFIER`` env var overrides config.  Exposed
        as a method so tests can patch a single seam without reaching into
        the private ``_turn_failed_file_mutations`` state dict.
        r   NHERMES_FILE_MUTATION_VERIFIER>   0noofffalse)load_configdisplayfile_mutation_verifierT)r  r  r   rB  r   hermes_cli.configr  r  r  r  re  )r   _osenv_load_config_cfg_displays         r   _file_mutation_verifier_enabledz'AIAgent._file_mutation_verifier_enabled  s   	+//"ABBCyy{{((**2MMMIIIIII#|~~+   .8t.D.DNtxx	***$H(D)) D.F(.R.RHLL)ABBCCC 	 	 	D	ts7   AC A C A-*C ,A--A)C 
C%$C%failedc           	      
   | sdS dt          |            dg}d}|                                 D ]\  }}|dk    r n|                    d          pd                                }|                    d          pd}|r|                    d	| d
| d|            n|                    d	| d
| d           |dz  }t          |           |z
  }|dk    r|                    d| d           d                    |          S )a   Render the per-turn failed-mutation dict as a user-facing footer.

        Displays up to 10 paths with their first error preview, then a
        count of any additional failures.  Returns an empty string when
        the dict is empty so callers can concatenate unconditionally.
        r   u   ⚠️ File-mutation verifier: z file(s) were NOT modified this turn despite any wording above that may suggest otherwise. Run `git status` or `read_file` to confirm.r   r   r  r  patch     • u    — [z] z] failedr   u     • … and z morerJ  )r   itemsr   rB  rY  rZ  )r  linesshownr  infor  r  	remainings           r   $_format_file_mutation_failure_footerz,AIAgent._format_file_mutation_failure_footer   sN     	2&6{{& & &
  ,,.. 		 		JD${{xx006B==??G88F##.wD BCdCC$CC'CCDDDD@d@@$@@@AAAQJEEKK%'	q==LL:):::;;;yyr   num_tool_msgsc                 (    ddl m}  || ||          S )uV   Forwarder — see ``agent.agent_runtime_helpers.apply_pending_steer_to_tool_results``.r   )#apply_pending_steer_to_tool_results)rO  r  )r   r  r  r  s       r   $_apply_pending_steer_to_tool_resultsz,AIAgent._apply_pending_steer_to_tool_results   s)    SSSSSS224=QQQr   descc                 D    t          j                     | _        || _        dS )zAUpdate the last-activity timestamp and description (thread-safe).N)time_last_activity_ts_last_activity_desc)r   r  s     r   _touch_activityzAIAgent._touch_activity%  s    !%#'   r   c                     |dS t          |dd          }|sdS 	 ddlm}  ||| j                  }|	|| _        dS dS # t
          $ r Y dS w xY w)zParse x-ratelimit-* headers from an HTTP response and cache the state.

        Called after each streaming API call.  The httpx Response object is
        available on the OpenAI SDK Stream via ``stream.response``.
        Nheadersr   )parse_rate_limit_headersr  )rE  agent.rate_limit_trackerr  r   _rate_limit_stater  )r   r|  r
  r  r  s        r   _capture_rate_limitszAIAgent._capture_rate_limits*  s      F-D99 	F	IIIIII,,Wt}MMME ).&&& !  	 	 	DD	s   !A   
AAc                     | j         S )z1Return the last captured RateLimitState, or None.)r  r   s    r   get_rate_limit_statezAIAgent.get_rate_limit_state=  s    %%r   c                 |   |dS t          |dd          }|sdS 	 |                    d          }|sdS |                                dk    r2| xj        dz  c_        t                              d| j                   dS t                              d|                                           dS # t          $ r Y dS w xY w)zRead X-OpenRouter-Cache-Status from response headers and log it.

        Increments ``_or_cache_hits`` on HIT so callers can report savings.
        Nr
  zx-openrouter-cache-statusHITr   z)OpenRouter response cache HIT (total: %d)zOpenRouter response cache %s)rE  r   upper_or_cache_hitsr  r  r  r  )r   r|  r
  statuss       r   _check_openrouter_cache_statusz&AIAgent._check_openrouter_cache_statusA  s    
  F-D99 	F
	[[!<==F ||~~&&##q(##GI\]]]]];V\\^^LLLLL 	 	 	DD	s   B- AB- >-B- -
B;:B;c           	          t          j                     | j        z
  }| j        | j        t          |d          | j        | j        | j        | j        j        | j        j	        dS )zReturn a snapshot of the agent's current activity for diagnostics.

        Called by the gateway timeout handler to report what the agent was doing
        when it was killed, and by the periodic "still working" notifications.
        r   )last_activity_tslast_activity_descseconds_since_activitycurrent_toolapi_call_countr   budget_used
budget_max)
r  r  r  round_current_tool_api_call_countr   r   used	max_total)r   elapseds     r   get_activity_summaryzAIAgent.get_activity_summaryW  sc     )++ 66 $ 6"&":&+GQ&7&7 ."2"105/9	
 	
 		
r   c                 h   | j         rY	 | j                             |pg            n# t          $ r Y nw xY w	 | j                                          n# t          $ r Y nw xY wt	          | d          r?| j        r:	 | j                            | j        pd|pg            dS # t          $ r Y dS w xY wdS dS )uC  Shut down the memory provider and context engine — call at actual session boundaries.

        This calls on_session_end() then shutdown_all() on the memory
        manager, and on_session_end() on the context engine.
        NOT called per-turn — only at CLI exit, /reset, gateway
        session expiry, etc.
        r*  r   N)_memory_manageron_session_endr  shutdown_allr8  r*  r   r   r  s     r   shutdown_memory_providerz AIAgent.shutdown_memory_provideri  s!     	$33HNCCCC   $113333    4-.. 	43J 	'66O)rN        	 	 	 	s/   & 
33A 
AA9$B 
B-,B-c                    | j         r.	 | j                             |pg            n# t          $ r Y nw xY wt          | d          r?| j        r:	 | j                            | j        pd|pg            dS # t          $ r Y dS w xY wdS dS )u  Trigger end-of-session extraction without tearing providers down.
        Called when session_id rotates (e.g. /new, context compression);
        providers keep their state and continue running under the old
        session_id — they just flush pending extraction now.r*  r   N)r'  r(  r  r8  r*  r   r*  s     r   commit_memory_sessionzAIAgent.commit_memory_session  s    
  	$33HNCCCC    4-.. 	43J 	'66O)rN        	 	 	 	s   & 
33$A4 4
BBoriginal_user_messagefinal_responseinterruptedc                    |rdS | j         r|r|sdS 	 | j                             ||| j        pd           | j                             || j        pd           dS # t          $ r Y dS w xY w)us  Mirror a completed turn into external memory providers.

        Called at the end of ``run_conversation`` with the cleaned user
        message (``original_user_message``) and the finalised assistant
        response.  The external memory backend gets both ``sync_all`` (to
        persist the exchange) and ``queue_prefetch_all`` (to start
        warming context for the next turn) in one shot.

        Uses ``original_user_message`` rather than ``user_message``
        because the latter may carry injected skill content that bloats
        or breaks provider queries.

        Interrupted turns are skipped entirely (#15218).  A partial
        assistant output, an aborted tool chain, or a mid-stream reset
        is not durable conversational truth — mirroring it into an
        external memory backend pollutes future recall with state the
        user never saw completed.  The prefetch is gated on the same
        flag: the user's next message is almost certainly a retry of
        the same intent, and a prefetch keyed on the interrupted turn
        would fire against stale context.

        Normal completed turns still sync as before.  The whole body is
        wrapped in ``try/except Exception`` because external memory
        providers are strictly best-effort — a misconfigured or offline
        backend must not block the user from seeing their response.
        Nr   )r   )r'  sync_allr   queue_prefetch_allr  )r   r.  r/  r0  s       r   _sync_external_memory_for_turnz&AIAgent._sync_external_memory_for_turn  s    B  	F$ 	 	<Q 	F
	 ))%~?0b *     33%?0b 4       	 	 	DD	s   AA 
A*)A*c                    	 | j         5  t          | j                  }| j                                         ddd           n# 1 swxY w Y   |D ]M}	 |                                 # t
          $ r) 	 |                                 n# t
          $ r Y nw xY wY Jw xY wn# t
          $ r Y nw xY w	 t          | dd          }|!|                     |dd           d| _	        dS dS # t
          $ r Y dS w xY w)u  Release LLM client resources WITHOUT tearing down session tool state.

        Used by the gateway when evicting this agent from _agent_cache for
        memory-management reasons (LRU cap or idle TTL) — the session may
        resume at any time with a freshly-built AIAgent that reuses the
        same task_id / session_id, so we must NOT kill:
          - process_registry entries for task_id (user's bg shells)
          - terminal sandbox for task_id (cwd, env, shell state)
          - browser daemon for task_id (open tabs, cookies)
          - memory provider (has its own lifecycle; keeps running)

        We DO close:
          - OpenAI/httpx client pool (big chunk of held memory + sockets;
            the rebuilt agent gets a fresh client anyway)
          - Active child subagents (per-turn artefacts; safe to drop)

        Safe to call multiple times.  Distinct from close() — which is the
        hard teardown for actual session boundaries (/new, /reset, session
        expiry).
        Nclientcache_evictTrx  shared)
r  rX  r  clearrelease_clientsr  closerE  _close_openai_clientr6  )r   childrenr  r6  s       r   r;  zAIAgent.release_clients  s   ,	+ . . 566%++---. . . . . . . . . . . . . . . "  ))++++    $   	  	 	 	D		T8T22F!))&t)TTT" "!  	 	 	DD	s   B .AB AB 
AB A('B (
B3BB
BBBBB BB 
B,+B,02C& &
C43C4c                    t          | dd          pd}	 ddlm} |                    |           n# t          $ r Y nw xY w	 t          |           n# t          $ r Y nw xY w	 t          |           n# t          $ r Y nw xY w	 | j        5  t          | j	                  }| j	        
                                 ddd           n# 1 swxY w Y   |D ]'}	 |                                 # t          $ r Y $w xY wn# t          $ r Y nw xY w	 t          | dd          }|!|                     |dd	
           d| _        dS dS # t          $ r Y dS w xY w)a   Release all resources held by this agent instance.

        Cleans up subprocess resources that would otherwise become orphans:
        - Background processes tracked in ProcessRegistry
        - Terminal sandbox environments
        - Browser daemon sessions
        - Active child agents (subagent delegation)
        - OpenAI/httpx client connections

        Safe to call multiple times (idempotent).  Each cleanup step is
        independently guarded so a failure in one does not prevent the rest.
        r   Nr   r   )process_registry)r  r6  agent_closeTr8  )rE  tools.process_registryr@  kill_allr  r   r)   r  rX  r  r:  r<  r=  r6  )r   r  r@  r>  r  r6  s         r   r<  zAIAgent.close  s9    $d339r	??????%%g%6666 	 	 	D		w 	 	 	D		G$$$$ 	 	 	D	
	+ . . 566%++---. . . . . . . . . . . . . . . "  KKMMMM    D
  	 	 	D		T8T22F!))&t)TTT" "!  	 	 	DD	s   2 
??A 
A A $A4 4
B BC< .C:C< C

C< C
C< C+*C< +
C85C< 7C88C< <
D	D	2E 
EEhistoryc                    d}t          |          D ]}|                    d          dk    r|                    dd          }d|vr7	 t          j        |          }d|v r%t	          |d         t
                    r
|d         } nv# t          j        t          f$ r Y w xY w|rP| j        	                    |d	           | j
        s-|                     | j         d
t          |           d           t          d           dS )a  
        Recover todo state from conversation history.
        
        The gateway creates a fresh AIAgent per message, so the in-memory
        TodoStore is empty. We scan the history for the most recent todo
        tool response and replay it to reconstruct the state.
        Nr  r  r  r   z"todos"todosF)mergeu   📋 Restored z todo item(s) from history)reversedr   r  r  r  rX  r  	TypeError_todo_storewriter   ra  r   r   r  )r   rD  last_todo_responser  r  datas         r   _hydrate_todo_storezAIAgent._hydrate_todo_store5  s6    "G$$ 	 	Cwwv&((ggi,,G''z'**d??z$w-'F'F?)-g&E()4     	t""#5U"CCC? trrsCU?V?Vrrrsssus   
;BB! B!c                     | j         S )z)Check if an interrupt has been requested.)r  r   s    r   is_interruptedzAIAgent.is_interruptedU  s     ((r   system_messagec                 (    ddl m}  || |          S )uD   Forwarder — see ``agent.system_prompt.build_system_prompt_parts``.r   )build_system_prompt_partsrQ  )ri  rS  )r   rQ  rS  s      r   _build_system_prompt_partsz"AIAgent._build_system_prompt_partsc  s*    AAAAAA((nMMMMr   c                 (    ddl m}  || |          S )u>   Forwarder — see ``agent.system_prompt.build_system_prompt``.r   )build_system_promptrT  )ri  rW  )r   rQ  rW  s      r   _build_system_promptzAIAgent._build_system_prompth  s*    ;;;;;;""4GGGGr   c                     t          | t                    r.|                     dd          p|                     dd          pdS t          | dd          pt          | dd          pdS )z8Extract call ID from a tool_call entry (dict or object).call_idr   idr  r  r   rE  )rO  s    r   _get_tool_call_id_staticz AIAgent._get_tool_call_id_staticm  sk     b$ 	C66)R((BBFF4,<,<BBr9b))HWRr-B-BHbHr   c                     t          | t                    rD|                     d          }t          |t                    r|                    dd          pdS dS t          | dd          }t          |dd          pdS )ab  Extract function name from a tool_call entry (dict or object).

        Gemini's OpenAI-compatibility endpoint requires every `role: tool`
        message to carry the matching function name. OpenAI/Anthropic/ollama
        tolerate its absence, so the field is best-effort: callers fall back
        to "" and the message still works elsewhere.
        rM  r   r   Nr\  )rO  rX  s     r   _get_tool_call_name_staticz"AIAgent._get_tool_call_name_statict  s     b$ 	
##B"d## 0vvfb))/R/2RT**r62&&,",r   >   r  r1  r   rM  r?  	developerc                 $    ddl m}  ||           S )uH   Forwarder — see ``agent.agent_runtime_helpers.sanitize_api_messages``.r   )sanitize_api_messages)rO  rb  )r  rb  s     r   _sanitize_api_messageszAIAgent._sanitize_api_messages  s(     	FEEEEE$$X...r   r  c                 f   t          | t                    r|                     d          dk    rdS |                     d          rdS |                     d          }t          |t                    r|                                rdS nt          |t
                    r|D ]}t          |t                    s|r dS |                    d          }|dv r7|dk    rC|                    dd	          }t          |t                    r|                                r dS  dS n
||d	k    rdS |                     d          p|                     d          }t          |t                    r|                                rdS |                     d          }t          |t
                    r|rdS dS )um  Return True if ``msg`` is an assistant turn whose only payload is reasoning.

        "Thinking-only" means the model emitted reasoning (``reasoning`` or
        ``reasoning_content``) but no visible text and no tool_calls. When sent
        back to providers that convert reasoning into thinking blocks (native
        Anthropic, OpenRouter Anthropic, third-party Anthropic-compatible
        gateways), the resulting message has only thinking blocks — which
        Anthropic rejects with HTTP 400 "The final block in an assistant
        message cannot be `thinking`."

        Symmetric with Claude Code's ``filterOrphanedThinkingOnlyMessages``
        (src/utils/messages.ts). We drop the whole turn from the API copy
        rather than fabricating stub text — the message log (UI transcript)
        keeps the reasoning block; only the wire copy is cleaned.
        r  r?  Fr  r  r   >   thinkingredacted_thinkingrF  r   NrS  rR  TrT  )r  r  r   r   rB  rX  )r  r  blockbtyperF  rR  rds          r   _is_thinking_only_assistantz#AIAgent._is_thinking_only_assistant  s   " #t$$ 	;(F(F577<   	5'')$$gs## 	}} u&& 	   !%..  %$uu		&))===F?? 99VR00D!$,, % %$uuuu  W]]5GG/00HCGGK4H4H	i%% 	)//*;*; 	4WW())b$ 	B 	4ur   c                 $    ddl m}  ||           S )uU   Forwarder — see ``agent.agent_runtime_helpers.drop_thinking_only_and_merge_users``.r   )"drop_thinking_only_and_merge_users)rO  rl  )r  rl  s     r   #_drop_thinking_only_and_merge_usersz+AIAgent._drop_thinking_only_and_merge_users  s(    
 	SRRRRR11(;;;r   r  c                 D   ddl m}  |            }t          d | D                       }||k    r| S d}g }| D ]H}|j        j        dk    r!||k     r|                    |           |dz  }3|                    |           It                              d||z
  |           |S )aj  Truncate excess delegate_task calls to max_concurrent_children.

        The delegate_tool caps the task list inside a single call, but the
        model can emit multiple separate delegate_task tool_calls in one
        turn.  This truncates the excess, preserving all non-delegate calls.

        Returns the original list if no truncation was needed.
        r   )_get_max_concurrent_childrenc              3   :   K   | ]}|j         j        d k    dV  dS )delegate_taskr   N)rM  r   rN  s     r   r  z3AIAgent._cap_delegate_task_calls.<locals>.<genexpr>  s1      [[2r{7G?7Z7ZQ7Z7Z7Z7Z[[r   rq  r   zUTruncated %d excess delegate_task call(s) to enforce max_concurrent_children=%d limit)tools.delegate_toolro  r  rM  r   rY  r  r$  )r  ro  max_childrendelegate_countkept_delegates	truncatedrO  s          r   _cap_delegate_task_callsz AIAgent._cap_delegate_task_calls  s     	EDDDDD3355[[[[[[[\))	 	% 	%B{?22!L00$$R((("a'N  $$$$/\)<	
 	
 	

 r   c                 L   t                      }g }| D ]n}|j        j        |j        j        f}||vr+|                    |           |                    |           It                              d|j        j                   ot          |          t          |           k     r|n| S )zRemove duplicate (tool_name, arguments) pairs within a single turn.

        Only the first occurrence of each unique pair is kept.
        Returns the original list if no duplicates were found.
        zRemoved duplicate tool call: %s)	setrM  r   rL  addrY  r  r$  r   )r  seenuniquerO  r  s        r   _deduplicate_tool_callszAIAgent._deduplicate_tool_calls  s     EE 	T 	TB;#R[%:;C$b!!!!@"+BRSSSSVs:66vvJFr   c                 &    ddl m}  || |          S )uC   Forwarder — see ``agent.agent_runtime_helpers.repair_tool_call``.r   )repair_tool_call)rO  r  )r   rQ  r  s      r   _repair_tool_callzAIAgent._repair_tool_call  s'    @@@@@@i000r   c                 (    ddl m}  ||            dS )uC   Forwarder — see ``agent.system_prompt.invalidate_system_prompt``.r   )invalidate_system_promptN)ri  r  )r   r  s     r   _invalidate_system_promptz!AIAgent._invalidate_system_prompt	  s+    @@@@@@  &&&&&r   fn_namerL  indexc                 $    t          | ||          S )u  Generate a deterministic call_id from tool call content.

        Used as a fallback when the API doesn't provide a call_id.
        Deterministic IDs prevent cache invalidation — random UUIDs would
        make every API call's prefix unique, breaking OpenAI's prompt cache.
        )_codex_deterministic_call_id)r  rL  r  s      r   rQ   zAIAgent._deterministic_call_id	  s     ,GYFFFr   raw_idc                      t          |           S )z8Split a stored tool id into (call_id, response_item_id).)_codex_split_responses_tool_id)r  s    r   rR   z AIAgent._split_responses_tool_id	  s     .f555r   rZ  response_item_idc                 "    t          ||          S )zCBuild a valid Responses `function_call.id` (must start with `fc_`).)(_codex_derive_responses_function_call_id)r   rZ  r  s      r   rP   z*AIAgent._derive_responses_function_call_id	  s     8AQRRRr   c                 J    t          j                    }|j         d|j         S )N:)r!  current_threadr   ident)r   threads     r   _thread_identityzAIAgent._thread_identity	  s(    )+++.....r   c                     t          | dd          }t          | dd          }t          | dd          }d|                                  d| d| d| S )	Nr   r(  r   r   zthread=z
 provider=z
 base_url=z model=)rE  r  )r   r   r   r   s       r   _client_log_contextzAIAgent._client_log_context!	  s}    4Y774Y77gy111d++-- 1 1 1 1 1 1).1 1	
r   c                 `    t          | dd           }|t          j                    }|| _        |S )N_client_lock)rE  r!  RLockr  )r   locks     r   _openai_client_lockzAIAgent._openai_client_lock*	  s2    t^T22<?$$D $Dr   r6  c                    ddl m} t          | |          rdS t          | dd          }|-t	          |          r |            rdS nt          |          rdS t          | dd          }|t          t          |dd                    S dS )a  Check if an OpenAI client is closed.

        Handles both property and method forms of is_closed:
        - httpx.Client.is_closed is a bool property
        - openai.OpenAI.is_closed is a method returning bool

        Prior bug: getattr(client, "is_closed", False) returned the bound method,
        which is always truthy, causing unnecessary client recreation on every call.
        r   MockF	is_closedNT_client)unittest.mockr  r  rE  callablere  )r6  r  is_closed_attrhttp_clients       r   _is_openai_client_closedz AIAgent._is_openai_client_closed1	  s     	'&&&&&fd## 	5 d;;%'' !>##  4 n%% tfi66"[%@@AAAur   c                    	 dd l }dd l}|j        |j        dfg}t	          |d          rg|                    |j        |j        df           |                    |j        |j        df           |                    |j        |j	        df           n2t	          |d          r"|                    |j        |j
        df           t          |           }|                    |                    |          |	          S # t          $ r Y d S w xY w)
Nr   r   TCP_KEEPIDLE   r      TCP_KEEPALIVE)socket_options)	transportproxy)httpxsocket
SOL_SOCKETSO_KEEPALIVEr8  rY  IPPROTO_TCPr  TCP_KEEPINTVLTCP_KEEPCNTr  r   ClientHTTPTransportr  )r   _httpx_socket
_sock_opts_proxys        r   _build_keepalive_http_clientz$AIAgent._build_keepalive_http_clientO	  sF   	""""$$$$"-w/CQGHJw// T!!7#68Lb"QRRR!!7#68Mr"RSSS!!7#68KQ"OPPPP/22 T!!7#68Mr"RSSS
 -X66F== ..j.II !     	 	 	44	s   C:C= =
D
Dclient_kwargsr9  c                ,    ddl m}  || |||          S )uG   Forwarder — see ``agent.agent_runtime_helpers.create_openai_client``.r   )create_openai_clientr8  )rO  r  )r   r  rx  r9  r  s        r   _create_openai_clientzAIAgent._create_openai_clienth	  s.    DDDDDD##D-vVVVVr   c                 $    ddl m}  ||           S )uJ   Forwarder — see ``agent.agent_runtime_helpers.force_close_tcp_sockets``.r   )force_close_tcp_sockets)rO  r  )r6  r  s     r   _force_close_tcp_socketsz AIAgent._force_close_tcp_socketsm	  s(     	HGGGGG&&v...r   c                R   |d S |                      |          }	 |                                 t                              d||||                                            d S # t
          $ r;}t                              d|||                                 |           Y d }~d S d }~ww xY w)Nz<OpenAI client closed (%s, shared=%s, tcp_force_closed=%d) %sz6OpenAI client close failed (%s, shared=%s) %s error=%s)r  r<  r  r  r  r  r  )r   r6  rx  r9  force_closedr  s         r   r=  zAIAgent._close_openai_clients	  s    >F 44V<<	LLNNNKKN((**      	 	 	LLH((**        	s   AA! !
B&+0B!!B&c                   |                                  5  t          | dd           }	 |                     | j        |d          }nR# t          $ rE}t
                              d||                                 |           Y d }~d d d            dS d }~ww xY w|| _        d d d            n# 1 swxY w Y   | 	                    |d| d           dS )Nr6  Tr8  z7Failed to rebuild shared OpenAI client (%s) %s error=%sFzreplace:)
r  rE  r  _client_kwargsr  r  r$  r  r6  r=  )r   rx  
old_client
new_clientr  s        r   _replace_primary_openai_clientz&AIAgent._replace_primary_openai_client	  sU   %%'' 	% 	% x66J	!778KTZcg7hh

   M,,..	   uuu	% 	% 	% 	% 	% 	% 	% 	% %DK	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	!!*5H5H5HQU!VVVts:   B+AB+
B/B?B+B
B++B/2B/c                   |                                  5  t          | dd           }|#|                     |          s|cd d d            S d d d            n# 1 swxY w Y   t                              d||                                            |                     d|           st          d          |                                  5  | j        cd d d            S # 1 swxY w Y   d S )Nr6  zCDetected closed shared OpenAI client; recreating before use (%s) %szrecreate_closed:rx  z'Failed to recreate closed OpenAI client)	r  rE  r  r  r$  r  r  RuntimeErrorr6  )r   rx  r6  s      r   _ensure_primary_openai_clientz%AIAgent._ensure_primary_openai_client	  s   %%'' 	 	T8T22F!$*G*G*O*O!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	Q$$&&	
 	
 	

 22:UV:U:U2VV 	JHIII%%'' 	 	;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s#   *AAAC!!C%(C%c                 $    ddl m}  ||           S )uK   Forwarder — see ``agent.agent_runtime_helpers.cleanup_dead_connections``.r   )cleanup_dead_connections)rO  r  )r   r  s     r   _cleanup_dead_connectionsz!AIAgent._cleanup_dead_connections	  s%    HHHHHH''---r   c                    t          | t                    sdS g }|                     d          }t          |t                    r|                    |           |                     d          }t          |t                    r|                    |           dt
          dt          ffdt          fd|D                       S )zHReturn True when the outbound request still contains native image parts.Fr  inputr   r   c                 "   t          | t                    rH|                     d          }|dv rdS t          fd|                                 D                       S t          | t
                    rt          fd| D                       S dS )Nr   >   rH  rI  Tc              3   .   K   | ]} |          V  d S r    r  r  _contains_images     r   r  zPAIAgent._api_kwargs_have_image_parts.<locals>._contains_image.<locals>.<genexpr>	  s-      FF!??1--FFFFFFr   c              3   .   K   | ]} |          V  d S r   r  r  s     r   r  zPAIAgent._api_kwargs_have_image_parts.<locals>._contains_image.<locals>.<genexpr>	  s-      ==!??1--======r   F)r  r  r   r  valuesrX  )r   ptyper  s     r   r  z=AIAgent._api_kwargs_have_image_parts.<locals>._contains_image	  s    %&& G		&))8884FFFFu||~~FFFFFF%&& >====u======5r   c              3   .   K   | ]} |          V  d S r   r  )r  itemr  s     r   r  z7AIAgent._api_kwargs_have_image_parts.<locals>.<genexpr>	  s-      @@T??4((@@@@@@r   )r  r  r   rX  extendr   re  r  )r  
candidatesr  response_inputr  s       @r   _api_kwargs_have_image_partsz$AIAgent._api_kwargs_have_image_parts	  s     *d++ 	5
>>*--h%% 	(h''' $00nd++ 	.n---	3 	4 	 	 	 	 	 	 @@@@Z@@@@@@r   	is_visionc                (    ddl m}  |d|          S )Nr   )copilot_request_headersT)is_agent_turnr  )hermes_cli.copilot_authr  )r   r  r  s      r   _copilot_headers_for_requestz$AIAgent._copilot_headers_for_request	  s*    CCCCCC&&TYOOOOr   )r  c                   ddl m} |                     |          }t          ||          r|S |                                 5  t          | j                  }d d d            n# 1 swxY w Y   d|d<   t          t          |	                    dd                    d          r0| 
                    |pi           r|                     d	          |d
<   |                     ||d          S )Nr   r  r  max_retriesr   r   r  T)r  default_headersFr8  )r  r  r  r  r  r  r  r~   r   r   r  r  r  )r   rx  r  r  primary_clientrequest_kwargss         r   _create_request_openai_clientz%AIAgent._create_request_openai_client	  sH   &&&&&&;;6;JJnd++ 	"!!%%'' 	7 	7!$"566N	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 )*}%!#n&8&8R&H&H"I"IKbcc	b11*2BCC	b 150Q0Q\`0Q0a0aN,-)).PU)VVVs   A$$A(+A(c                6    |                      ||d           d S )NFr8  )r=  )r   r6  rx  s      r   _close_request_openai_clientz$AIAgent._close_request_openai_client	  s#    !!&!FFFFFr   on_first_deltac                 *    ddl m}  || |||          S )u;   Forwarder — see ``agent.codex_runtime.run_codex_stream``.r   )run_codex_stream)agent.codex_runtimer  )r   r  r6  r  r  s        r   _run_codex_streamzAIAgent._run_codex_stream	  s+    888888j&.IIIr   c                 (    ddl m}  || ||          S )uK   Forwarder — see ``agent.codex_runtime.run_codex_create_stream_fallback``.r   ) run_codex_create_stream_fallback)r  r  )r   r  r6  r  s       r   !_run_codex_create_stream_fallbackz)AIAgent._run_codex_create_stream_fallback	  s)    HHHHHH//j&IIIr   Tc                   | j         dk    s	| j        dvrdS 	 | j        dk    rddlm}  |d          }nddlm}  |d          }n9# t
          $ r,}t                              d	| j        |           Y d }~dS d }~ww xY wt          |	                    d
          pd          
                                }t          | j        pd          
                                }|r*|r(||k    r"t                              d| j                   dS 	 | j        dk    rddlm}  ||          }nddlm}  ||          }n9# t
          $ r,}t                              d| j        |           Y d }~dS d }~ww xY w|	                    d
          }	|	                    d          }
t          |	t                    r|	
                                sdS t          |
t                    r|

                                sdS |	
                                | _        |

                                                    d          | _        | j        | j        d
<   | j        | j        d<   |                     | j         d          sdS dS )Ncodex_responses>   	xai-oauthopenai-codexFr  r   )!resolve_codex_runtime_credentials)refresh_if_expiring)%resolve_xai_oauth_runtime_credentialsz%s singleton read failed: %sr   r   z%s singleton tokens differ from the active api_key; skipping singleton force-refresh to avoid silent account swap. Reactive credential rotation should go through the pool.)force_refreshz %s credential refresh failed: %sr   r  _credential_refreshr  T)r   r   hermes_cli.authr  r  r  r  r  r   r   rB  r   r  r  r   r  r  )r   r[  r  singleton_nowr  r  singleton_key
active_keycredsr   r   s              r   %_try_refresh_codex_client_credentialsz-AIAgent._try_refresh_codex_client_credentials	  s   =---Fc1c1c5	}..MMMMMM A A(-! ! ! RQQQQQ E E(-! ! !  	 	 	LL7LLL55555	 M--i88>B??EEGG+,,2244
 	Z 	MZ,G,GLLK 	   5	}..MMMMMM99NNNQQQQQQ==ERRR 	 	 	LL;T]CPPP55555	 ))I&&99Z(('3'' 	w}} 	5(C(( 	0@0@ 	5}} ((//44)-I&*.-J'22T]:_:_:_2`` 	5ts/   0A	 	
A?!A::A?0D> >
E4!E//E4c                   | j         dk    s| j        dk    rdS 	 ddlm}  |t	          dt          t          j        dd                              t          t          j        d	d
                    |          }n3# t          $ r&}t                              d|           Y d }~dS d }~ww xY w|                    d          }|                    d          }t          |t                    r|                                sdS t          |t                    r|                                sdS |                                | _        |                                                    d          | _        | j        | j        d<   | j        | j        d<   | j                            dd            |                     d          sdS dS )Nr  r  Fr   ) resolve_nous_runtime_credentials<   HERMES_NOUS_MIN_KEY_TTL_SECONDS1800HERMES_NOUS_TIMEOUT_SECONDS15)min_key_ttl_secondstimeout_seconds
force_mintz"Nous credential refresh failed: %sr   r   r  r  nous_credential_refreshr  T)r   r   r  r  rF  r   r  r  r  r  r  r  r   r  r   rB  r   r  r   r  r@  r  )r   r[  r  r  r  r   r   s          r   $_try_refresh_nous_client_credentialsz,AIAgent._try_refresh_nous_client_credentialsB
  s   =...$-62I2I5
	HHHHHH44$'C	:[]c0d0d,e,e$f$f %bi0Mt&T&T U U   EE
  	 	 	LL=sCCC55555	 ))I&&99Z(('3'' 	w}} 	5(C(( 	0@0@ 	5}} ((//44)-I&*.-J' 1488822:S2TT 	5ts   A"A= =
B-B((B-c                 0   | j         dk    rdS 	 ddlm}  |            \  }}n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY wt          |t                    r|                                sdS |                                }|| _	        | j	        | j
        d<   | j        | j
        d<   |                     t          | j        pd	                     |                     d
          sdS t                              d|           dS )a]  Refresh Copilot credentials and rebuild the shared OpenAI client.

        Copilot tokens may remain the same string across refreshes (`gh auth token`
        returns a stable OAuth token in many setups). We still rebuild the client
        on 401 so retries recover from stale auth/client state without requiring
        a session restart.
        r  Fr   )resolve_copilot_tokenz%Copilot credential refresh failed: %sNr   r   r   copilot_credential_refreshr  z%Copilot credentials refreshed from %sT)r   r  r  r  r  r  r  r   rB  r   r  r   "_apply_client_headers_for_base_urlr  r  )r   r  	new_tokentoken_sourcer  s        r   '_try_refresh_copilot_client_credentialsz/AIAgent._try_refresh_copilot_client_credentialse
  s=    =I%%5	EEEEEE&;&;&=&=#I|| 	 	 	LL@#FFF55555	 )S)) 	1B1B 	5OO%%	 )-I&*.-J'//DM4GR0H0HIII22:V2WW 	5;\JJJts   # 
AAAc                 :   | j         dk    st          | d          sdS | j        dk    rdS t          | dd          pd}d|v rdS 	 dd	lm}m}  |            }n3# t          $ r&}t          	                    d
|           Y d }~dS d }~ww xY wt          |t                    r|                                sdS |                                }|| j        k    rdS 	 | j                                         n# t          $ r Y nw xY w	  ||t          | dd           t!          | j        | j                            | _        n3# t          $ r&}t                              d|           Y d }~dS d }~ww xY w|| _        ddlm} | j        dk    r ||          nd| _        dS )Nr  _anthropic_api_keyF	anthropic_anthropic_base_urlr   z	azure.comr   )resolve_anthropic_tokenbuild_anthropic_clientz'Anthropic credential refresh failed: %stimeoutz?Failed to rebuild Anthropic client after credential refresh: %s)_is_oauth_tokenT)r   r8  r   rE  agent.anthropic_adapterr  r  r  r  r  r  r   rB  r  _anthropic_clientr<  r   r   r$  r   _is_anthropic_oauth)r   _baser  r  r  r  r   s          r   )_try_refresh_anthropic_client_credentialsz1AIAgent._try_refresh_anthropic_client_credentials
  s+   =000FZ8[8[05 =K''5 3R88>B%5	________//11II 	 	 	LLBCHHH55555	 )S)) 	1B1B 	5OO%%	///5	"((**** 	 	 	D		%;%;3T::4T]DJOO& & &D""
  	 	 	NN\^abbb55555	 #,
 	<;;;;;AER]A]A]??9#=#=#=ch tsB   A 
B"BBC2 2
C?>C?:D> >
E.E))E.c                    ddl m}m}m} t	          |d          r |            | j        d<   d S t	          |d          rt          |          | j        d<   d S t	          |d          r ||          | j        d<   d S t	          |d          rt                      | j        d<   d S t	          |d          rdd	lm	}  |            | j        d<   d S t	          |d
          rddi| j        d<   d S t	          |d          rt                      | j        d<   d S t	          |d          r4ddl m}  || j                            dd                    | j        d<   d S d }	 ddlm}  || j                  }	|	r|	j        rt          |	j                  }n# t"          $ r Y nw xY w|r|| j        d<   d S | j                            dd            d S )Nr   )_AI_GATEWAY_HEADERSbuild_nvidia_nim_headersbuild_or_headersr  r  ai-gateway.vercel.shzintegrate.api.nvidia.comzapi.routermint.comr  )copilot_default_headersapi.kimi.comr   zclaude-code/0.1.0portal.qwen.aizchatgpt.com)_codex_cloudflare_headersr   r   )get_provider_profile)agent.auxiliary_clientr'  r(  r)  r~   r  r  r   rD  r+  r   r.  r   	providersr/  r   r  r  r@  )
r   r   r'  r(  r)  r+  r.  _ph_headers_gpf2_ph2s
             r   r  z*AIAgent._apply_client_headers_for_base_url
  s   	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 !?;; "	A5E5E5G5GD 1222"8-CDD  	A59:M5N5ND 1222"8-GHH 	A5M5Mh5W5WD 1222"8-ABB 	A5H5J5JD 1222"8-DEE 	AAAAAAA5L5L5N5ND 1222"8^<< 	A6BDW5XD 1222"8-=>> 	A5I5K5KD 1222"8];; 	AHHHHHH5N5N#''	2666 6D 1222
 KCCCCCCuT]++ =D0 ="&t';"<"<K    A9D#$5666#''(94@@@@@s   3F 
FFc                    t          |dd           pt          |dd          }t          |dd           pt          |dd           p| j        }| j        dk    rddlm}m} 	 | j                                         n# t          $ r Y nw xY w|| _	        || _
         |||t          | j        | j                  	          | _        | j        d
k    r ||          nd| _        || _        || _        d S || _        t!          |t"                    r|                    d          n|| _        | j        | j        d<   | j        | j        d<   |                     | j                   |                     d           d S )Nruntime_api_keyaccess_tokenr   runtime_base_urlr   r  r   )r  r   r  r  Fr  r   credential_rotationr  )rE  r   r   r!  r  r   r"  r<  r  r  r  r   r   r   r#  r   r  r   r  r  r  r  )r   r  runtime_keyruntime_baser  r   s         r   _swap_credentialzAIAgent._swap_credential
  s   e%6==cP^`bAcAcu&8$??t75R\^bCcCctgkgt=000WWWWWWWW&,,....    '2D#'3D$%;%;\4T]DJOO& & &D" HL}XcGcGc{'C'C'CinD$&DL(DMF"4>|S4Q4Qc++C000Wc)-I&*.-J'//>>>++3H+IIIIIs    A: :
BB)classified_reasonrt  has_retried_429r=  c                .    ddl m}  || ||||          S )uO   Forwarder — see ``agent.agent_runtime_helpers.recover_with_credential_pool``.r   )recover_with_credential_pool)r   r>  r=  rt  )rO  r@  )r   r   r>  r=  rt  r@  s         r   _recover_with_credential_poolz%AIAgent._recover_with_credential_pool  sJ     	MLLLLL++Dk[j  P  `m  n  n  n  	nr   c                     | j         }|dS | j        dk    s1t          t          | dd                                        d          rdS |                                S )zEWhether a rate-limit retry should wait for same-provider credentials.NFr   r   r   r   )_credential_poolr   r   rE  r   r   )r   r   s     r   '_credential_pool_may_recover_rate_limitz/AIAgent._credential_pool_may_recover_rate_limit  sf    $<5M00074R0011<<=NOO 1 5!!###r   c                 n    | j         dk    r|                                   | j        j        j        di |S )Nr  r  )r   r%  r"  r  create)r   r  s     r   _anthropic_messages_createz"AIAgent._anthropic_messages_create  sA    =000::<<<5t%.5CC
CCCr   c                 N   t          t          | dd                    }t          | dd          dk    r+ddlm} t          | dd	          pd	} ||          | _        dS dd
lm}  || j        t          | dd          t          | j        | j	                  |          | _        dS )u)  Rebuild the Anthropic client after an interrupt or stale call.

        Handles both direct Anthropic and Bedrock-hosted Anthropic models
        correctly — rebuilding with the Bedrock SDK when provider is bedrock,
        rather than always falling back to build_anthropic_client() which
        requires a direct Anthropic API key.

        Honors ``self._oauth_1m_beta_disabled`` (set by the reactive recovery
        path when an OAuth subscription rejects the 1M-context beta) so the
        rebuilt client carries the reduced beta set.
        _oauth_1m_beta_disabledFr   Nbedrockr   )build_anthropic_bedrock_client_bedrock_regionz	us-east-1)r  r  )r  drop_context_1m_beta)
re  rE  r!  rK  r"  r  r  r   r   r   )r   _drop_1mrK  regionr  s        r   _rebuild_anthropic_clientz!AIAgent._rebuild_anthropic_client$  s     &?GGHH4T**i77NNNNNNT#4kBBQkF%C%CF%K%KD"""FFFFFF%;%;'3T::4T]DJOO%-	& & &D"""r   c                 &    ddl m}  || |          S )uK   Forwarder — see ``agent.chat_completion_helpers.interruptible_api_call``.r   )interruptible_api_call)r  rR  )r   r  rR  s      r   _interruptible_api_callzAIAgent._interruptible_api_call>  s'    HHHHHH%%dJ777r   c                 D   t          | dd          }||                                }|rxt          | dd          }||                    |          }|rNd | j        | j        fD             }|D ]}	  ||           # t
          $ r Y w xY w|                     |           t          | dd          }|d|                                }|rNd | j        | j        fD             }|D ]}	  ||           # t
          $ r Y w xY w|                     |           d| _        dS )zDReset tracking for text delivered during the current model response._stream_think_scrubberN_stream_context_scrubberc                     g | ]}||S r   r  r  cbs     r   rP  z;AIAgent._reset_stream_delivery_tracking.<locals>.<listcomp>W  s     t t tegesesesesr   c                     g | ]}||S r   r  rX  s     r   rP  z;AIAgent._reset_stream_delivery_tracking.<locals>.<listcomp>e  s    pppBacaoRaoaoaor   r   )rE  flushfeedr   _stream_callbackr  _record_streamed_assistant_text _current_streamed_assistant_text)r   think_scrubber
think_tailctx_scrubber	callbacksrY  scrubbertails           r   _reset_stream_delivery_trackingz'AIAgent._reset_stream_delivery_trackingE  s    !'?FF%'--//J E  't-GNN+!-!2!2:!>!>J E t tt/I4K`.a t t tI' ! !!BzNNNN( ! ! ! D!88DDD 4!;TBB>>##D ;pp4+EtG\*]ppp	#  B4$   44T:::02---s$   1A==
B
	B
(C44
D Dc                 n    t          |t                    r|rt          | dd          |z   | _        dS dS dS )zCAccumulate visible assistant text emitted through stream callbacks.r_  r   N)r  r   rE  r_  )r   rF  s     r   r^  z'AIAgent._record_streamed_assistant_textn  sR    dC   	T 	@"EEL 111	 	 	 	r   c                     t          | t                    sdS t          j        dd|                                           S )Nr   z\s+ry  )r  r   r  r  rB  )rF  s    r   _normalize_interim_visible_textz'AIAgent._normalize_interim_visible_textu  s9    $$$ 	2vfc4((..000r   c           	          |                      |                     |pd                    }|sdS |                      |                     t          | dd          pd                    }t          |          o||k    S )Nr   Fr_  )ri  r  rE  re  )r   r  visible_contentstreameds       r   _interim_content_was_streamedz%AIAgent._interim_content_was_streamed{  s    >>$$W]33
 
  	577$$WT3UWY%Z%Z%`^`aa
 
 H~~=(o"==r   assistant_msgc                    t          | dd          }|t          |t                    sdS |                    d          }|                     |pd                                          }|r|dk    rdS |                     |          }	  |||           dS # t          $ r  t          	                    dd	           Y dS w xY w)
zESurface a real mid-turn assistant commentary message to the UI layer.r   Nr  r   z(empty))already_streamedz interim_assistant_callback errorTr  )
rE  r  r  r   r  rB  rm  r  r  r  )r   rn  rY  r  visiblerp  s         r   _emit_interim_assistant_messagez'AIAgent._emit_interim_assistant_message  s    T7>>:Zt<<:F##I..**7=b99??AA 	'Y..F==gFF	LBw)9:::::: 	L 	L 	LLL;dLKKKKKK	Ls   	B &CCc                    t          | dd          r%|r#|                                rd| _        d|z   }d}nd}t          |t                    rt          | dd          }||                    |pd          }n|                     |pd          }t          | dd          }||                    |          }nt          |          }|s&t          | d	d          s|                    d
          }|sdS d | j	        | j
        fD             }d}|D ] }	  ||           d}# t          $ r Y w xY w|r|                     |           dS dS )z;Fire all registered stream delta callbacks (display + TTS)._stream_needs_breakF

TrU  Nr   rV  r_  rJ  c                     g | ]}||S r   r  rX  s     r   rP  z.AIAgent._fire_stream_delta.<locals>.<listcomp>  s    hhhBY[YgRYgYgYgr   )rE  rB  rt  r  r   r\  r  r,   lstripr   r]  r  r^  )r   rF  prepended_breakr`  rd  rc  	deliveredrY  s           r   _fire_stream_deltazAIAgent._fire_stream_delta  s    4.66 	$4 	$DJJLL 	$',D$D=D"OO#OdC   	) %T+CTJJN)%**4:266 //
;; t%?FFH#}}T** (--" )78", , ) {{4(( 	Fhh4#=t?T"Uhhh		 	 	B4 		    	70066666	7 	7s   D!!
D.-D.c                 V    | j         }|	  ||           dS # t          $ r Y dS w xY wdS )z&Fire reasoning callback if registered.N)r   r  )r   rF  rY  s      r   _fire_reasoning_deltazAIAgent._fire_reasoning_delta  sN    $>4    >    
&&c                 V    | j         }|	  ||           dS # t          $ r Y dS w xY wdS )a  Notify display layer that the model is generating tool call arguments.

        Fires once per tool name when the streaming response begins producing
        tool_call / tool_use tokens.  Gives the TUI a chance to show a spinner
        or status line so the user isn't staring at a frozen screen while a
        large tool payload (e.g. a 45 KB write_file) is being generated.
        N)r   r  )r   rQ  rY  s      r   _fire_tool_gen_startedzAIAgent._fire_tool_gen_started  sP     #>9    >r}  c                 :    | j         dupt          | dd          duS )z4Return True if any streaming consumer is registered.Nr]  )r   rE  r   s    r   r_  zAIAgent._has_stream_consumers  s/     &d2 Ct/66dB	
r   r  c                *    ddl m}  || ||          S )uU   Forwarder — see ``agent.chat_completion_helpers.interruptible_streaming_api_call``.r   ) interruptible_streaming_api_callr  )r  r  )r   r  r  r  s       r   !_interruptible_streaming_api_callz)AIAgent._interruptible_streaming_api_call  s0     	SRRRRR//jQ_````r   zFailoverReason | Nonec                 &    ddl m}  || |          S )uJ   Forwarder — see ``agent.chat_completion_helpers.try_activate_fallback``.r   )try_activate_fallback)r  r  )r   rx  r  s      r   _try_activate_fallbackzAIAgent._try_activate_fallback  s'    GGGGGG$$T6222r   c                 $    ddl m}  ||           S )uJ   Forwarder — see ``agent.agent_runtime_helpers.restore_primary_runtime``.r   )restore_primary_runtime)rO  r  )r   r  s     r   _restore_primary_runtimez AIAgent._restore_primary_runtime  s%    GGGGGG&&t,,,r   	api_errorretry_countr  c                ,    ddl m}  || |||          S )uP   Forwarder — see ``agent.agent_runtime_helpers.try_recover_primary_transport``.r   )try_recover_primary_transport)r  r  )rO  r  )r   r  r  r  r  s        r   _try_recover_primary_transportz&AIAgent._try_recover_primary_transport  s2     	NMMMMM,,T9+cnoooor   c                     t          | t                    sdS | D ]1}t          |t                    r|                    d          dv r dS 2dS )NFr   >   rH  rI  T)r  rX  r  r   )r  parts     r   _content_has_image_partsz AIAgent._content_has_image_parts  s^    '4(( 	5 	 	D$%% $((6*:*:>Z*Z*Zttur   rH  c                    t          | pd                              d          \  }}}d}|                    d          rZ|t          d          d                              dd          d                                         }|                    d          r|}d	d
dddd                    |d          }t          j        d|d          }	 |5  |	                    t          j        |                     d d d            n# 1 swxY w Y   n:# t          $ r- 	 t          j        |j                   n# t           $ r Y nw xY w w xY wt#          |j                  }t          |          |fS )Nr   ,
image/jpegdata:;r   r   zimage/z.pngz.gifz.webpz.jpg)z	image/pngz	image/gifz
image/webpr  z	image/jpganthropic_image_F)r  suffixdelete)r   	partitionr   r   r  rB  r   tempfileNamedTemporaryFilerK  base64	b64decoder  r  unlinkr   rU  r   )	rH  header_rM  mime	mime_partr  tmpr  s	            r    _materialize_data_url_for_visionz(AIAgent._materialize_data_url_for_vision  s   io2..88==4W%% 	!s7||}}-33C;;A>DDFFI##H-- ! ! 
 
 #dF

 	 )1CF[`aaa
	 2 2		&*4001112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 	 	 		#(####   	 CH~~4yy$sT   D (D8D DD DD 
ED54E5
E?EEEr  c                 D   t          j        t          |pd                              d                                                    }| j                            |          }|r|S ddd                    |d          }d}t          |pd          }d }|                    d          r|                     |          \  }}d}		 d	d
l	m
}
 t          j         |
||                    }t          |t                    rt          j        |          ni }|                    d          pd                                }	n# t"          $ r}d| }	Y d }~nd }~ww xY w|r:|                                r&	 |                                 nS# t(          $ r Y nGw xY wnB# |r:|                                r'	 |                                 w # t(          $ r Y w w xY ww w xY w|	sd}	d| d|	 d}|r-t          |pd                              d          s	|d| dz  }|| j        |<   |S )Nr   r  r?  ztool result)r?  r  r1  zDescribe everything visible in this image in thorough detail. Include any text, code, UI, data, objects, people, layout, colors, and any other notable visual information.r  r   )vision_analyze_tool)rH  user_promptanalysiszImage analysis failed: zImage analysis failed.z[The z- attached an image. Here's what it contains:
]z@
[If you need a closer look, use vision_analyze with image_url: )hashlibsha256r   encode	hexdigest_anthropic_image_fallback_cacher   r   r  tools.vision_toolsr  asynciorunr  r  r  rB  r  r  r  rU  )r   rH  r  	cache_keycached
role_labelanalysis_promptvision_sourcecleanup_pathdescriptionr  result_jsonr  r%  notes                  r   &_describe_image_for_anthropic_fallbackz.AIAgent._describe_image_for_anthropic_fallback*  s   N3yB#7#7#>#>w#G#GHHRRTT	599)DD 	M %!
 
 #dF

 	
8 	 IO,,'+##G,, 	_*.*O*OP]*^*^'M<	>>>>>>!+##mYYY K 1;;0L0LTTZ,,,RTF!::j117R>>@@KK 	8 	8 	87A77KKKKKK	8   3 3 5 5  ''))))   D   3 3 5 5  ''))))   D   	32K_z__Q\___ 	Y_"!5!5!@!@!I!I 	dTadddD ;?,Y7sg   A9D? >F ?
E	EF EF 2F 
FFG0GG
GGGGc                    	 ddl m} t          | dd          pd                                }t          | dd          pd                                }|r|sdS  |||          }|dS t	          |j                  S # t          $ r Y dS w xY w)a  Return True if the active provider+model reports native vision.

        Used to decide whether to strip image content parts from API-bound
        messages (for non-vision models) or let the provider adapter handle
        them natively (for vision-capable models).
        r   )get_model_capabilitiesr   r   r   F)agent.models_devr  rE  rB  re  supports_visionr  )r   r  r   r   capss        r   _model_supports_visionzAIAgent._model_supports_vision]  s    	??????j"55;BBDDHT7B//52<<>>E 5 u))(E::D|u,--- 	 	 	55	s   AA< A< (A< <
B
	B
c                    |                      |          s|S g }g }|D ]}t          |t                    r<|                                r'|                    |                                           Tt          |t
                    sj|                    d          }|dv rOt          |                    dd          pd                                          }|r|                    |           |dv r|                    di           }t          |t
                    r|                    dd          nt          |pd          }	|	r*|                    |                     |	|                     n|                    d           kt          |                    dd          pd                                          }|r|                    |           d	                    d
 |D                                                       }
d                    d |D                                                       }|
r	|r|
 d	| S |
r|
S |r|S dS )Nr   >   rF  
input_textrF  r   >   rH  rI  rH  r  z:[An image was attached but no image source was available.]ru  c              3      K   | ]}||V  	d S r   r  )r  r  s     r   r  z8AIAgent._preprocess_anthropic_content.<locals>.<genexpr>  s'      BBdTBTBBBBBBr   rJ  c              3      K   | ]}||V  	d S r   r  )r  rF  s     r   r  z8AIAgent._preprocess_anthropic_content.<locals>.<genexpr>  s'      ??D$?4??????r   zI[A multimodal message was converted to text for Anthropic compatibility.])	r  r  r   rB  rY  r  r   r  rZ  )r   r  r  
text_partsimage_notesr  r  rF  
image_datarH  r  r  s               r   _preprocess_anthropic_contentz%AIAgent._preprocess_anthropic_contentq  sy   ,,W55 	N "
!# 	( 	(D$$$ ::<< 4%%djjll333dD)) HHV$$E...488FB//5266<<>> ,%%d+++444!XXk266
9CJPT9U9UpJNN5"555[^_i_omo[p[p	 e&&t'R'RS\^b'c'cdddd&&'cdddtxx++1r2288::D (!!$'''BBkBBBBBHHJJ??J?????EEGG 	+f 	+**&*** 	M 	MZZr   c                     |p| j         }t          | dd          }|	i }|| _        |                    |          }|ddlm}  ||          }|||<   |S )zReturn the cached transport for the given (or current) api_mode.

        Lazy-initializes on first call per api_mode. Returns None if no
        transport is registered for the mode.
        _transport_cacheNr   )get_transport)r   rE  r  r   agent.transportsr  )r   r   modecacher%  r  s         r   _get_transportzAIAgent._get_transport  sw     (4=0$77=E$)D!IIdOO9666666d##AE$Kr   api_messagesc           
      j    t           fd|D                       s|S                                  r|S t          j        |          }|D ]g}t	          |t
                    s                     |                    d          t          |                    dd          pd                    |d<   h|S )Nc              3      K   | ]A}t          |t                    o'                    |                    d                     V  BdS r  Nr  r  r  r   r  r  r   s     r   r  z>AIAgent._prepare_anthropic_messages_for_api.<locals>.<genexpr>  `       
 
 sD!!Wd&C&CCGGIDVDV&W&W
 
 
 
 
 
r   r  r  r1  	r  r  rc  deepcopyr  r  r  r   r   r   r  transformedr  s   `   r   #_prepare_anthropic_messages_for_apiz+AIAgent._prepare_anthropic_messages_for_api  s     
 
 
 
#
 
 
 
 
 	    &&(( 	  mL11 	 	Cc4(( !??	""CGGFF++5v66 C	NN r   c           
      j    t           fd|D                       s|S                                  r|S t          j        |          }|D ]g}t	          |t
                    s                     |                    d          t          |                    dd          pd                    |d<   h|S )a  Strip native image parts when the active model lacks vision.

        Runs on the chat.completions / codex_responses paths. Vision-capable
        models pass through unchanged (provider and any downstream translator
        handle the image parts natively). Non-vision models get each image
        replaced by a cached vision_analyze text description so the turn
        doesn't fail with "model does not support image input".
        c              3      K   | ]A}t          |t                    o'                    |                    d                     V  BdS r  r  r  s     r   r  zAAIAgent._prepare_messages_for_non_vision_model.<locals>.<genexpr>  r  r   r  r  r1  r  r  s   `   r   &_prepare_messages_for_non_vision_modelz.AIAgent._prepare_messages_for_non_vision_model  s      
 
 
 
#
 
 
 
 
 	   &&(( 	 mL11 
	 
	Cc4(( 
 "??	""CGGFF++5v66 C	NN r   c                 V   t          |          s|S |                    d          pg }|                     |          s|S |                                 r|S t	          |          }|dk    rt          j        d|d          S t                              d|| j	        | j
                   |S )a  Return the tool message content that is safe for the active model.

        Multimodal tool results normally unwrap to OpenAI-style content parts so
        vision-capable models can inspect screenshots.  Text-only providers must
        not receive those image parts, because a rejected tool result becomes
        part of the canonical history and can make the next user turn fail before
        the agent has a chance to recover.
        r  computer_usezcomputer_use returned screenshot/image content, but the active model/provider does not support image input. Switch to a vision-capable model for desktop computer use, or use browser tools for browser tasks.)r   text_summaryzWTool %s returned image content for non-vision model %s/%s; falling back to text summary)rw   r   r  r  rx   r  dumpsr  r$  r   r   )r   rQ  r  r  r  s        r   %_tool_result_content_for_active_modelz-AIAgent._tool_result_content_for_active_model  s     *&11 	M**Y''-2,,W55 	N&&(( 	N*622&&:/
 !(     	+MJ	
 	
 	
 r   c                 $    ddl m}  ||          S )uX   Forwarder — see ``agent.conversation_compression.try_shrink_image_parts_in_messages``.r   )"try_shrink_image_parts_in_messages)r  r  )r   r  r  s      r   #_try_shrink_image_parts_in_messagesz+AIAgent._try_shrink_image_parts_in_messages  s%    UUUUUU11,???r   c                     t          | dd          pd                                dv rdS t          | dd          pd                                }d|v pd|v pd|v pd	|v pd
|v pd|v pd|v S )a  True when using an anthropic-compatible endpoint that preserves dots in model names.
        Alibaba/DashScope keeps dots (e.g. qwen3.5-plus).
        MiniMax keeps dots (e.g. MiniMax-M2.7).
        Xiaomi MiMo keeps dots (e.g. mimo-v2.5, mimo-v2.5-pro).
        OpenCode Go/Zen keeps dots for non-Claude models (e.g. minimax-m2.5-free).
        ZAI/Zhipu keeps dots (e.g. glm-4.7, glm-5.1).
        AWS Bedrock uses dotted inference-profile IDs
        (e.g. ``global.anthropic.claude-opus-4-7``,
        ``us.anthropic.claude-sonnet-4-5-20250929-v1:0``) and rejects
        the hyphenated form with
        ``HTTP 400 The provided model identifier is invalid``.
        Regression for #11976; mirrors the opencode-go fix for #5211
        (commit f77be22c), which extended this same allowlist.r   r   >   
minimax-cnopencode-goopencode-zenr  xiaomialibabarJ  minimaxTr   	dashscopealiyuncsr  zopencode.ai/zen/zbigmodel.cnxiaomimimo.comzbedrock-runtime.)rE  r   )r   bases     r   _anthropic_preserve_dotsz AIAgent._anthropic_preserve_dots  s     D*b))/R6688 =
 
 
 4j"--3::<<4 *T!*D * "T)* $	*
  4'* "T)
	
r   c                 ,    t          | j        d          S )z2Return True when the base URL targets Qwen Portal.r-  r  r   s    r   _is_qwen_portalzAIAgent._is_qwen_portal:  s    $T%9;KLLLr   c                    t          j        |          }|s|S |D ]}t          |t                    s|                    d          }t          |t
                    r
d|dg|d<   Lt          |t                    rfg }|D ]Z}t          |t
                    r|                    d|d           0t          |t                    r|                    |           [|r||d<   |D ]}t          |t                    ro|                    d          dk    rV|                    d          }t          |t                    r*|r(t          |d         t                    rddi|d         d	<    n|S )
Nr  rF  r   rF  r  r   r  r   	ephemeralcache_control)rc  r  r  r  r   r   rX  rY  )r   r  preparedr  r  normalized_partsr  s          r   _qwen_prepare_chat_messagesz#AIAgent._qwen_prepare_chat_messages>  s   =.. 	O 	6 	6Cc4(( ggi((G'3'' 6+17"C"C!DIGT** 
6 $& # 6 6D!$,, 6(//0N0NOOOO#D$// 6(//555# 6%5C	N  	 	C#t$$ H)D)D''),,gt,, I IZPRUY=Z=Z I4:K3HGBK0r   c                    |sdS |D ]}t          |t                    s|                    d          }t          |t                    r
d|dg|d<   Lt          |t                    rfg }|D ]Z}t          |t                    r|                    d|d           0t          |t                    r|                    |           [|r||d<   |D ]}t          |t                    rp|                    d          dk    rW|                    d          }t          |t                    r*|r(t          |d         t                    rdd	i|d         d
<    dS dS )u<   In-place variant — mutates an already-copied message list.Nr  rF  r  r  r   r  r   r  r  )r  r  r   r   rX  rY  )r   r  r  r  r  r  s         r   #_qwen_prepare_chat_messages_inplacez+AIAgent._qwen_prepare_chat_messages_inplace_  s    	F 	6 	6Cc4(( ggi((G'3'' 
6+17"C"C!DIGT** 6#% # 6 6D!$,, 6(//0N0NOOOO#D$// 6(//555# 6%5C	N 	 	C#t$$ H)D)D''),,gt,, I IZPRUY=Z=Z I4:K3HGBK0	 	r   c                 &    ddl m}  || |          S )uE   Forwarder — see ``agent.chat_completion_helpers.build_api_kwargs``.r   )build_api_kwargs)r  r  )r   r  r  s      r   _build_api_kwargszAIAgent._build_api_kwargs{  s'    BBBBBBl333r   c                 v   t          | j        d          rdS t          | j        d          rdS t          | j        d          st          | j        d          r5	 ddlm} t	           || j                            S # t          $ r Y dS w xY w| j        pd	                                	                                d
k    r-| 
                                }t          d |D                       S d| j        vrdS d| j        v rdS | j        pd		                                d}t          fd|D                       S )a3  Return True when reasoning extra_body is safe to send for this route/model.

        OpenRouter forwards unknown extra_body fields to upstream providers.
        Some providers/routes reject `reasoning` with 400s, so gate it to
        known reasoning-capable model families and direct Nous Portal.
        znousresearch.comTr*  zmodels.github.air  r   github_model_reasoning_effortsFr   r=  c              3   &   K   | ]}|o|d k    V  dS )r  Nr  )r  opts     r   r  z9AIAgent._supports_reasoning_extra_body.<locals>.<genexpr>  s+      <<s+se|<<<<<<r   
openrouterzapi.mistral.ai)	z	deepseek/z
anthropic/zopenai/zx-ai/zgoogle/gemini-2zgoogle/gemma-4z
qwen/qwen3ztencent/hy3-previewzxiaomi/c              3   B   K   | ]}                     |          V  d S r   )r   )r  r  r   s     r   r  z9AIAgent._supports_reasoning_extra_body.<locals>.<genexpr>  s1      SS5##F++SSSSSSr   )r~   r   rD  r  re  r   r  r   rB  r   "_lmstudio_reasoning_options_cachedr  )r   r  optsreasoning_model_prefixesr   s       @r   _supports_reasoning_extra_bodyz&AIAgent._supports_reasoning_extra_body  s    !!57IJJ 	4 !57MNN 	4!$"68JKK		$T%9;RSS		LLLLLL::4:FFGGG   uuMR&&((..00J>>::<<D<<t<<<<<<t3335t3335!r((**
$
  SSSS:RSSSSSSs   "A> >
BBc           	         ddl }t          | dd          }|	i x}| _        | j        | j        f}|                    |          }|$|\  }}|s|                                |z
  dk     r|S 	 ddlm}  || j        | j        t          | dd                    }n# t          $ r g }Y nw xY w||                                f||<   |S )a  Probe LM Studio's published reasoning ``allowed_options`` once per
        (model, base_url). The list (e.g. ``["off","on"]`` or
        ``["off","minimal","low"]``) is needed both for the supports-reasoning
        gate and for clamping the emitted ``reasoning_effort`` so toggle-style
        models don't 400 on ``high``. Cache is keyed on (model, base_url) so
        ``/model`` swaps and base-URL changes don't reuse a stale list.
        Non-empty results are cached permanently (model capabilities don't
        change). Empty results (transient probe failure OR genuinely
        non-reasoning model) are cached with a 60-second TTL to avoid an
        HTTP round-trip on every turn while still retrying reasonably soon.
        r   N_lm_reasoning_opts_cacher  ) lmstudio_model_reasoning_optionsr   r   )
r  rE  r  r   r   r   	monotonicrD  r  r  )r   _timer  r  r  r  tsr  s           r   r  z*AIAgent._lmstudio_reasoning_options_cached  s    	8$??=466ED1z4=)3HD" ))B."44	JJJJJJ33
DM74B+G+G DD  	 	 	DDD	EOO--.c
s   +,B B'&B'c                 T    ddl m}  || j        |                                           S )a  Resolve a safe top-level ``reasoning_effort`` for LM Studio.

        The iteration-limit summary path calls ``chat.completions.create()``
        directly, bypassing the transport. Share the helper so the two paths
        can't drift on effort resolution and clamping.
        r   )resolve_lmstudio_effort)agent.lmstudio_reasoningr  r   r  )r   r  s     r   *_resolve_lmstudio_summary_reasoning_effortz2AIAgent._resolve_lmstudio_summary_reasoning_effort  sA     	EDDDDD&&!3355
 
 	
r   c                    	 ddl m} n# t          $ r Y dS w xY w || j                  }|sdS | j        rt          | j        t                    rk| j                            d          du rdS t          | j                            dd                    	                                
                                }nd}|dk    rd	|v rd	}n ||vr|d
k    rd|v rd}nd|v rd}n|d         }d|iS )zDFormat reasoning payload for GitHub Models/OpenAI-compatible routes.r   r  NenabledFeffortmediumxhighhighminimallow)rD  r  r  r   r   r  r  r   r   rB  r   )r   r  supported_effortsrequested_efforts       r   #_github_models_reasoning_extra_bodyz+AIAgent._github_models_reasoning_extra_body  sZ   	HHHHHHH 	 	 	44	 ;:4:FF  	4  	(Z0Et%L%L 	($((33u<<t"%))(H==   eggeegg   (w&&65F+F+F%%6669,,:K1K1K#(  ...#+  #4Q#7 *++s   	 
c                 (    ddl m}  || ||          S )uL   Forwarder — see ``agent.chat_completion_helpers.build_assistant_message``.r   )build_assistant_message)r  r%  )r   r   r  r%  s       r   _build_assistant_messagez AIAgent._build_assistant_message  s*    IIIIII&&t->NNNr   c                 z    |                                  p'|                                 p|                                 S )a=  Return True when the active provider enforces reasoning_content echo-back.

        DeepSeek v4 thinking and Kimi / Moonshot thinking both reject replays
        of assistant tool-call messages that omit ``reasoning_content`` (refs
        #15250, #17400). Xiaomi MiMo thinking mode has the same requirement.
        )_needs_deepseek_tool_reasoning_needs_kimi_tool_reasoning_needs_mimo_tool_reasoningr   s    r   _needs_thinking_reasoning_padz%AIAgent._needs_thinking_reasoning_pad   s@     //11 1..001..00	
r   c                     | j         dv p>t          | j        d          p)t          | j        d          pt          | j        d          S )a  Return True when the current provider is Kimi / Moonshot thinking mode.

        Kimi ``/coding`` and Moonshot thinking mode both require
        ``reasoning_content`` on every assistant tool-call message; omitting
        it causes the next replay to fail with HTTP 400.

        Detection is host-driven, not model-name-driven: aggregators like
        OpenRouter that re-export Kimi/Moonshot models speak their own
        protocol and reject ``reasoning_content`` echoes. We only enable the
        kimi-reasoning replay when the request actually targets a
        kimi/moonshot endpoint or the dedicated kimi-coding provider.
        >   kimi-codingkimi-coding-cnr,  zmoonshot.aizmoonshot.cn)r   r~   r   r   s    r   r)  z"AIAgent._needs_kimi_tool_reasoning  sU     M>> C$T]NCCC$T]MBBC %T]MBB		
r   c                     | j         pd                                }| j        pd                                }|dk    pd|v pt          | j        d          S )a  Return True when the current provider is DeepSeek thinking mode.

        DeepSeek V4 thinking mode requires ``reasoning_content`` on every
        assistant tool-call turn; omitting it causes HTTP 400 when the
        message is replayed in a subsequent API request (#15250).
        r   deepseekzapi.deepseek.comr   r   r   r~   r   r   r   r   s      r   r(  z&AIAgent._needs_deepseek_tool_reasoning!  se     M'R..00!r((**
" HU"H$T]4FGG	
r   c                     | j         pd                                }| j        pd                                }|dk    p-d|v p)t          | j        d          pt          | j        d          S )aM  Return True when the current provider is Xiaomi MiMo thinking mode.

        MiMo thinking mode requires ``reasoning_content`` on every assistant
        tool-call message when replaying history; omitting it causes HTTP 400.
        Refs: https://platform.xiaomimimo.com/docs/zh-CN/usage-guide/passing-back-reasoning_content
        r   r  mimozapi.xiaomimimo.comr  r1  r2  s      r   r*  z"AIAgent._needs_mimo_tool_reasoning0  s|     M'R..00!r((**  FF$T]4HIIF %T]4DEE		
r   
source_msgapi_msgc                 (    ddl m}  || ||          S )uQ   Forwarder — see ``agent.agent_runtime_helpers.copy_reasoning_content_for_api``.r   )copy_reasoning_content_for_api)rO  r8  )r   r5  r6  r8  s       r   _copy_reasoning_content_for_apiz'AIAgent._copy_reasoning_content_for_api@  s)    NNNNNN--dJHHHr   c                     |                      d          }t          |t                    s| S ddhfd|D             | d<   | S )u  Strip Codex Responses API fields from tool_calls for strict providers.

        Providers like Mistral, Fireworks, and other strict OpenAI-compatible APIs
        validate the Chat Completions schema and reject unknown fields (call_id,
        response_item_id) with 400 or 422 errors. These fields are preserved in
        the internal message history — this method only modifies the outgoing
        API copy.

        Creates new tool_call dicts rather than mutating in-place, so the
        original messages list retains call_id/response_item_id for Codex
        Responses API compatibility (e.g. if the session falls back to a
        Codex provider later).

        Fields stripped: call_id, response_item_id
        r  rZ  r  c                 ~    g | ]9}t          |t                    r fd |                                D             n|:S )c                 $    i | ]\  }}|v	||S r  r  )r  kr  _STRIP_KEYSs      r   
<dictcomp>zJAIAgent._sanitize_tool_calls_for_strict_api.<locals>.<listcomp>.<dictcomp>[  s)    AAAdaA[,@,@Q,@,@,@r   )r  r  r  )r  rO  r>  s     r   rP  z?AIAgent._sanitize_tool_calls_for_strict_api.<locals>.<listcomp>Z  s_     !
 !
 !
  "d##,AAAAbhhjjAAAA)+!
 !
 !
r   )r   r  rX  )r6  r  r>  s     @r   #_sanitize_tool_calls_for_strict_apiz+AIAgent._sanitize_tool_calls_for_strict_apiE  sn    " [[..
*d++ 	N "45!
 !
 !
 !
 !!
 !
 !

 r   r  r   c                *    ddl m}  || ||          S )uO   Forwarder — see ``agent.agent_runtime_helpers.sanitize_tool_call_arguments``.r   )sanitize_tool_call_argumentsrA  )rO  rC  )r  r  r   rC  s       r   _sanitize_tool_call_argumentsz%AIAgent._sanitize_tool_call_argumentsa  s0     	MLLLLL++HVPZ[[[[r   c                     | j         dk    S )a  Determine if tool_calls need sanitization for strict APIs.

        Codex Responses API uses fields like call_id and response_item_id
        that are not part of the standard Chat Completions schema. These
        fields must be stripped when calling any other API to avoid
        validation errors (400 Bad Request).

        Returns:
            bool: True if sanitization is needed (non-Codex API), False otherwise.
        r  )r   r   s    r   _should_sanitize_tool_callsz#AIAgent._should_sanitize_tool_callsl  s     } 111r   r  approx_tokensr  focus_topicrH  rI  c                0    ddl m}  || |||||          S )uF   Forwarder — see ``agent.conversation_compression.compress_context``.r   )compress_contextrG  )r  rK  )r   r  rQ  rH  r  rI  rK  s          r   _compress_contextzAIAgent._compress_contexty  s<    CCCCCC(N'k
 
 
 	
r   decisionc                 8    |j         r| j        || _        dS dS dS )z?Record the first guardrail decision that should stop this turn.N)should_halt_tool_guardrail_halt_decisionr   rM  s     r   _set_tool_guardrail_haltz AIAgent._set_tool_guardrail_halt  s3     	:D$F$N19D...	: 	:$N$Nr   c                 @    |j         pd}d| d|j         d|j         dS )Nza toolzI stopped retrying z) because it hit the tool-call guardrail (z) after z repeated non-progressing attempts. The last tool result explains the blocker; the next step is to change strategy instead of repeating the same call.)rQ  r   count)r   rM  r  s      r   #_toolguard_controlled_halt_responsez+AIAgent._toolguard_controlled_halt_response  sP    !-XE$ E EE E'/~E E E	
r   function_argsfunction_resultc                    | j                             ||||          }|j        dv rt          ||          }|j        r|                     |           |S )N)r  >   haltrs  )_tool_guardrails
after_callactionr\   rO  rR  )r   rQ  rV  rW  r  rM  s         r   _append_guardrail_observationz%AIAgent._append_guardrail_observation  sq     (33	 4 
 
 ?...7RRO 	4))(333r   c                 J    |                      |           t          |          S r   )rR  r]   rQ  s     r   _guardrail_block_resultzAIAgent._guardrail_block_result  s$    %%h///)(333r   effective_task_idr  c                     |j         }d| _        	 t          |          s|                     ||||          d| _        S |                     ||||          d| _        S # d| _        w xY w)a=  Execute tool calls from the assistant message and append results to messages.

        Dispatches to concurrent execution only for batches that look
        independent: read-only tools may always share the parallel path, while
        file reads/writes may do so only when their target paths do not overlap.
        TF)r  r`  rt   _execute_tool_calls_sequential_execute_tool_calls_concurrent)r   r   r  r`  r  r  s         r   _execute_tool_callszAIAgent._execute_tool_calls  s     '1
 !%
	*1*== ::%x1BN  %*D!!	 66!8->  %*D!!ED!))))s   &A A 	A&c                 f   ddl m}  ||                    d          |                    d          |                    d          |                    d          |                    d          |                    d          |                    d	          |                    d
          | 	  	        S )zSingle call site for delegate_task dispatch.

        New DELEGATE_TASK_SCHEMA fields only need to be added here to reach all
        invocation paths (concurrent, sequential, inline).
        r   )rq  goalcontexttoolsetstasksr   r   r   r  )	rf  rg  rh  ri  r   r   r   r  parent_agent)rr  rq  r   )r   rV  _delegate_tasks      r   _dispatch_delegate_taskzAIAgent._dispatch_delegate_task  s     	HGGGGG~""6**!%%i00"&&z22##G,,(,,-=>>%))-88"&&z22""6**

 

 

 
	
r   function_namepre_tool_block_checkedc           	      0    ddl m}  || ||||||          S )u>   Forwarder — see ``agent.agent_runtime_helpers.invoke_tool``.r   )invoke_tool)rO  rp  )r   rm  rV  r`  r*  r  rn  rp  s           r   _invoke_toolzAIAgent._invoke_tool  sC     	<;;;;;{4?PR^`h  kA  B  B  	Br        labelr  c                    ddl }ddl}|                    d          j        }t	          d|t          |          z
            }g }|                    d          D ]\}t          |          |k    r|                    |           +|                    ||dd          }	|	                    |	p|g           ]d|z   
                    |          }
| |  |
 S )	aF  Word-wrap verbose tool output to fit the terminal width.

        Splits *text* on existing newlines and wraps each line individually,
        preserving intentional line breaks (e.g. pretty-printed JSON).
        Returns a ready-to-print string with *label* on the first line and
        continuation lines indented.
        r   N)x      r  rJ  TF)widthbreak_long_wordsbreak_on_hyphens)shutiltextwrapget_terminal_sizecolumnsrF  r   r  rY  wrapr  rZ  )rs  rF  r  _shutil_twcols
wrap_width	out_linesraw_linewrappedr   s              r   _wrap_verbosezAIAgent._wrap_verbose  s    	!   ((33;TCKK/00
!	

4(( 	8 	8H8}}
**  ****((8:4849 # ; ;   !6XJ7777v##I..'%''''r   c                 ,    ddl m}  || ||||          S )uH   Forwarder — see ``agent.tool_executor.execute_tool_calls_concurrent``.r   )execute_tool_calls_concurrent)agent.tool_executorr  )r   r   r  r`  r  r  s         r   rc  z&AIAgent._execute_tool_calls_concurrent  0    EEEEEE,,T3DhPacqrrrr   c                 ,    ddl m}  || ||||          S )uH   Forwarder — see ``agent.tool_executor.execute_tool_calls_sequential``.r   )execute_tool_calls_sequential)r  r  )r   r   r  r`  r  r  s         r   rb  z&AIAgent._execute_tool_calls_sequential  r  r   c                 (    ddl m}  || ||          S )uJ   Forwarder — see ``agent.chat_completion_helpers.handle_max_iterations``.r   )handle_max_iterations)r  r  )r   r  r  r  s       r   _handle_max_iterationszAIAgent._handle_max_iterations  s)    GGGGGG$$T8^DDDr   stream_callbackpersist_user_messagec           	      0    ddl m}  || ||||||          S )u?   Forwarder — see ``agent.conversation_loop.run_conversation``.r   run_conversation)agent.conversation_loopr  )r   r  rQ  r5  r  r  r  r  s           r   r  zAIAgent.run_conversation   sD     	=<<<<<lNDXZacr  uI  J  J  	Jr   c                 @    |                      ||          }|d         S )a  
        Simple chat interface that returns just the final response.

        Args:
            message (str): User message
            stream_callback: Optional callback invoked with each text delta during streaming.

        Returns:
            str: Final assistant response
        )r  r/  r  )r   r   r  r  s       r   chatzAIAgent.chat  s(     &&w&PP&''r   )should_review_memoryr  c                0    ddl m}  || |||||          S )uD   Forwarder — see ``agent.codex_runtime.run_codex_app_server_turn``.r   )run_codex_app_server_turn)r  r.  r  r`  r  )r  r  )r   r  r.  r  r`  r  r  s          r   _run_codex_app_server_turnz"AIAgent._run_codex_app_server_turn  sQ     	BAAAAA((L`u  AI  ]n  EY  Z  Z  Z  	Zr   )@NNNNNNNNr   r   r   NNFFFNr   r   NNNNFNNNNNNNNNNNNNNNNNNNNNNNNNNNFFFNNNNNFr   r   r   F)r   Nr   )r   r   r   )FF)r   )r   )NN)NNF)rr  )NNNNN)r   r   r   r   &_TOOL_CALL_ARGUMENTS_CORRUPTION_MARKERpropertyr   r   setterrX  r   r  r   re  r   r  r   r   r   r  r&  r:  rL  rN  rY  ra  ri  rk  rp  rt  ry  ru  _STREAM_DIAG_HEADERSstaticmethodrz  r  BaseExceptionr  r  r  r  r  r  r  r  r  r  r  r  tupler  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r  r  r  r  r&  r-  r4  r;  r7  rD  r:  rf  rj  ro  rs  r|  r  r  r  r  r  r  r   r  r  r9  r  r  r  r  r  r  r  r  r  r  r  r  r%  r+  r-  r4  r;  r<  rN  rP  rU  rX  r]  r_  	frozenset_VALID_API_ROLESrc  rj  rm  rw  r}  r  r  rQ   rR   rP   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   rA  rD  rG  rP  rS  rf  r^  ri  rm  rr  rz  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*  r9  r@  rD  rF  rL  r[   rR  rU  r]  r_  rd  rl  rq  r  rc  rb  r  r  r  r  r  r   r   r   r   F  s%        	O +
 #    X _;c ;d ; ; ; _; %)!% &*'+"' % '+ #'+'+%)!,1(,7;+/(,+/&*'+%)"&*./3&*$(+/ ,015#'#(#(!!%.2)-$)(*,/+- %CG
 G
G
 G
 	G

 G
 G
 s)d"G
 G
 3i$G
 G
 G
 G
 s)G
  9G
  G
  !G
" #G
$ "%%G
& 'G
( )G
*  9+G
,  9-G
. c/G
0 1G
2 &*3G
4 #&5G
6 &.e_7G
8 9G
: !);G
< &=G
> !)?G
@ $AG
B %CG
D #EG
F  GG
H  (IG
J %-KG
L $MG
N "OG
P QG
R sCx.SG
T UG
V  S>WG
X tCH~.YG
Z [G
\ ]G
^ _G
` aG
b cG
d eG
f gG
h !iG
j !kG
l !mG
n oG
r sG
t ,uG
v S#XwG
z "{G
| #&}G
~ '*G
@ &)AG
B CG
 G
 G
 G
R  &   ,%7 %7 %7N? ?Xc] ?^b ? ? ? ?BX X X X
  $ ,1 * * *D * * * *6T    &
$ 
 
 
 
UC UD U U U U(VS VT V V V V& NMMMMM"tCH~ " " " \"
@cN@36@	@ @ @ @ . .# . . . \.
3] 3t 3 3 3 34 *.
 
 
 
 	

 
 
 
 tCH~&
 

 
 
 
0 *.
 
 
 
 	

 
 
 tCH~&
 

 
 
 
 	EC 	Em 	E 	E 	E 	E 	E
tCH~ 
 
 
 
2 2 2 2
) ) ) )
, ,c ,T , , , ,) )S )D ) ) ) ) 3 3s 3d 3 3 3 3>E > > > >(uUD[7I    0$tCH~:N SX    LD L L L L #'"&"&#
y 
y 
y 3-
y 3-	
y
 3-
y }
y 
tTz	
y 
y 
y 
y %S %T % % % \%  #'< < << 3-< 
	< < < \<*%s %t % % % %%c %d % % % %.13 13 1 1 1 1
 	Gc 	Gd 	G 	G 	G \	GH H H H H $(	C CC 4.	C
 
C C C C>bb b tCH~&	b
 
b b b b:x} : : : :
5s 5t 5 5 5 5          TdTT
T 
cT T T \T $#	 :  	
 
   6 '++/!%&*
 
 
 sm
 $C=	

 #
 sm
 
c3h
 
 
 
$*T$Z *D * * * *$	K 	Kd 	K4PT: 	K 	K 	K 	K3$t* 3QU 3 3 3 3j7d 7 7 7 7 7
9F 9Fd4j 9FX\]aXb 9F 9F 9F 9Fv-4: -$t* - - - ->5# 5 5 5 5
Sd4S>6J SX[ Shl Squvz{~  AD  |D  wE  rF S S S S
Dd38n)= D3 D[_ D D D D #S#X/#c]# 
# # # \#J +&I +&# +& +& +& \+&Z)(3- )HSM ) ) ) )c c    4 0) 0S#X 0 0 0 \0
C HTRUWZRZ^D\    * &*	T 	T 	TcN	T 		T
 	"	T 
$	T 	T 	T 	T      \@D @D$tCH~*> @D @D @D @DDBV BV BV BV BV BV BVH+ + + +B"# "$ " " " "Hhsm     %&%& 38n%& 	%&
 %& 
%& %& %& %&N    6  T#tCH~:M5N  SV       \ >RT RRU RZ^ R R R R
(C (D ( ( ( (
# $    && & &C D    ,
d 
 
 
 
$      6 d d    2/  #/ 	/
 / 
/ / / /b- - - -^6 6 6 6p4S#X+? D    @ ) ) ) ) X)N N NSRUX N N N N
H H3 H# H H H H
 I I I I \I -# - - - \-  !y!a!a!abb/d38n)= /$tCQTH~BV / / / \/
 3c3h 3D 3 3 3 \3j <tCH~&<	d38n	< < < \< T d    \< GD GT G G G \G"13 13: 1 1 1 1
' ' '
 G G G GC GPS G G G \G 6 6x}hsm7S1T 6 6 6 \6 +/S SS #3-S 
	S S S S/# / / / /
S 
 
 
 
Y_          \:  s C    \0W4 WC WQU WZ] W W W W
 / / / / / \/
3 3  QU    0     "s s     .4 . . . .
 A A$ A A A \A4P P$ P P P P
 Z^ W W Ws WQU Wbe W W W W4G3 G3 G4 G G G GJ JD J# JV^ J J J J
J JD J# J J J J
 FJ H H Hd Hd H H H HT EI ! ! !T !T ! ! ! !F! ! ! ! !F14 1 1 1 1f)A3 )A4 )A )A )A )AVJ J J JF 7;26
n 
n 
n c]
n 	
n
 $N3
n  S#X/
n 
tTz	
n 
n 
n 
n$ $ $ $ $DT D D D D
   48$ 8 8 8 8'3 '3 '3 '3RC D     1c 1c 1 1 1 \1
	>S 	>T 	> 	> 	> 	>LT#s(^ LPT L L L L37s 37t 37 37 37 37j# $         
t 
 
 
 
 ?Ca a aa3;a a a a3 3-D 3PT 3 3 3 3-$ - - - -
p"p47pFIp	p p p p # $    \ C E#xPT~BU<V    \:1 13 1SV 1 1 1 1f    (*[S *[ *[ *[ *[ *[ *[X s    $     84 D    @&s &C &TW & & & &P@ @ @ @ @ @
 
$  
  
  
  
DM M M M M     BD T    84d 4t 4 4 4 4
*T *T *T *T *TX DI        D
HSM 
 
 
 
,TD[ , , , ,@O OQU O O O O

t 
 
 
 

D 
 
 
 
(
 
 
 
 

D 
 
 
 
 I$ I IRV I I I I
 T d    \6  	\ \ \\ 	\
 
\ \ \ \\2T 2 2 2 2 ^br{  QU 
 
 
$ 
 
WZ 
lo 
  KN 
  Z_ 
 
 
 
:1F :4 : : : :

<Q 
VY 
 
 
 
  	  
   (40E 4# 4 4 4 4* *t *X[ *mp *y} * * * *.
T 
c 
 
 
 
( KO49B B# Bd BWZ B#+C=BCGB-1B>AB B B B ( (S ( (S (s ( ( ( \(0s s$ scf sx{ s  EI s s s s
s s$ scf sx{ s  EI s s s s
Et ES ES E E E E #59.2.2J JJ J #4S>2	J
 J "(+J 'smJ 
c3hJ J J J( (C ((82D (PS ( ( ( (* &+Z Z Z Z  #	Z
 tCH~&Z Z #Z 
c3hZ Z Z Z Z Zr   r   r   r   Fr   queryr   r   	max_turnsr   r   
list_toolsr   save_sampleverboser   c                    t          d           t          d           |rddlm}m} ddlm}m} t          d           t          d           t          d           t          d	            |            }g }g }g }|                                D ]_\  }} ||          }|rM||f}|d
v r|                    |           0|dv r|                    |           J|                    |           `t          d           |D ]X\  }}|d         rd	                    |d                   nd}t          d|dd|d                     t          d|            Yt          d           |D ]p\  }}|d         rd	                    |d                   nd}t          d|dd|d                     t          d|            t          d|d                     qt          d           |D ]9\  }}t          d|dd|d                     t          d|d                     :t          d            |            }|                                D ]b\  }}|d         rdnd}t          d | d!| d"|d                     |d         s+t          d#d	                    |d$                               c |            }t          d%t          |           d&           t          |          D ]'}t          |          }t          d'| d(| d)           (t          d*           t          d+           t          d,           t          d-           t          d.           t          d            t          d/           t          d0           t          d            t          d1           t          d2           t          d            t          d3           t          d4           d5S d5}d5}|r1d6 |                    d7          D             }t          d8|            |r1d9 |                    d7          D             }t          d:|            |r-t          d;           t          d<           t          d=           	 t          ||||||||
|>	  	        } n*# t          $ r}!t          d?|!            Y d5}!~!d5S d5}!~!ww xY w| d@}"n| }"t          dA|"            t          dB           |                     |"          }#t          dB           t          dC           t          d           t          dD|#dE                     t          dF|#dG                     t          dHt          |#dI                               |#dJ         r3t          dK           t          dL           t          |#dJ                    |	rt#          t%          j                              d5dM         }$dN|$ dO}%|                     |#dI         |"|#dE                   }&|&t+          j                                                    ||#dE         |"dP}	 t1          |%dQdRS          5 }'|'                    t5          j        |dTdUV                     d5d5d5           n# 1 swxY w Y   t          dW|%            n)# t8          $ r}!t          dX|!            Y d5}!~!nd5}!~!ww xY wt          dY           d5S )Za  
    Main function for running the agent directly.

    Args:
        query (str): Natural language query for the agent. Defaults to Python 3.13 example.
        model (str): Model name to use (OpenRouter format: provider/model). Defaults to anthropic/claude-sonnet-4.6.
        api_key (str): API key for authentication. Uses OPENROUTER_API_KEY env var if not provided.
        base_url (str): Base URL for the model API. Defaults to https://openrouter.ai/api/v1
        max_turns (int): Maximum number of API call iterations. Defaults to 10.
        enabled_toolsets (str): Comma-separated list of toolsets to enable. Supports predefined
                              toolsets (e.g., "research", "development", "safe").
                              Multiple toolsets can be combined: "web,vision"
        disabled_toolsets (str): Comma-separated list of toolsets to disable (e.g., "terminal")
        list_tools (bool): Just list available tools and exit
        save_trajectories (bool): Save conversation trajectories to JSONL files (appends to trajectory_samples.jsonl). Defaults to False.
        save_sample (bool): Save a single trajectory sample to a UUID-named JSONL file for inspection. Defaults to False.
        verbose (bool): Enable verbose logging for debugging. Defaults to False.
        log_prefix_chars (int): Number of characters to show in log previews for tool calls/responses. Defaults to 20.

    Toolset Examples:
        - "research": Web search, extract, crawl + vision tools
    u   🤖 AI Agent with Tool Callingz2==================================================r   )get_all_tool_namesget_available_toolsets)get_all_toolsetsget_toolset_infou    📋 Available Tools & Toolsets:z2--------------------------------------------------u'   
🎯 Predefined Toolsets (New System):z(---------------------------------------->   webvisioncreativeterminalrR  >   r  research
full_stackdevelopmentcontent_creationu   
📌 Basic Toolsets:resolved_toolsz, r)  r  r  z - r  z    Tools: u5   
📂 Composite Toolsets (built from other toolsets):includesz    Includes: z    Total tools: 
tool_countu!   
🎭 Scenario-Specific Toolsets:20u3   
📦 Legacy Toolsets (for backward compatibility):	availableu   ✅u   ❌z  ry  r  z    Requirements: requirementsu   
🔧 Individual Tools (z available):u     📌 z (from r   u   
💡 Usage Examples:z  # Use predefined toolsetszR  python run_agent.py --enabled_toolsets=research --query='search for Python news'zN  python run_agent.py --enabled_toolsets=development --query='debug this code'zP  python run_agent.py --enabled_toolsets=safe --query='analyze without terminal'z  # Combine multiple toolsetszM  python run_agent.py --enabled_toolsets=web,vision --query='analyze website'z  # Disable toolsetszQ  python run_agent.py --disabled_toolsets=terminal --query='no command execution'z&  # Run with trajectory saving enabledzF  python run_agent.py --save_trajectories --query='your question here'Nc                 6    g | ]}|                                 S r  rB  r  r%  s     r   rP  zmain.<locals>.<listcomp>  s      P P Pq P P Pr   r  u   🎯 Enabled toolsets: c                 6    g | ]}|                                 S r  r  r  s     r   rP  zmain.<locals>.<listcomp>  s     !R!R!R!''))!R!R!Rr   u   🚫 Disabled toolsets: u   💾 Trajectory saving: ENABLEDu:      - Successful conversations → trajectory_samples.jsonlu7      - Failed conversations → failed_trajectories.jsonl)	r   r   r   r   r   r   r   r   r   u    ❌ Failed to initialize agent: zTell me about the latest developments in Python 3.13 and what new features developers should know about. Please search for current information and try it out.u   
📝 User Query: z3
==================================================u   📋 CONVERSATION SUMMARYu   ✅ Completed: rl  u   📞 API Calls: 	api_callsu   💬 Messages: r  r/  u   
🎯 FINAL RESPONSE:z------------------------------r   sample_z.json)conversations	timestampr   rl  r  wr  r  Fr  )ensure_asciir  u"   
💾 Sample trajectory saved to: u   
⚠️ Failed to save sample: u    
👋 Agent execution completed!)rT  model_toolsr  r  rh  r  r  r  rY  rZ  r   sortedr   r  r   r  r  r   uuiduuid4ro  r   r  r  openrK  r  r  r  )(r  r   r   r   r  r   r   r  r   r  r  r   r  r  r  r  all_toolsetsbasic_toolsetscomposite_toolsetsscenario_toolsetsr   toolsetr  r  	tools_strincludes_strlegacy_toolsetsr  	all_toolsrQ  enabled_toolsets_listdisabled_toolsets_listagentr%  rk  r  	sample_idsample_filenamerr  fs(                                           r   mainr  (  s   H 

+,,,	(OOO  PJJJJJJJJ????????0111h 	8999h'')) )//11 		4 		4MD'##D))D 4tQQQ"))%0000fff&--e4444%,,U333 	&'''( 	- 	-JD$=ABR=S_		$'7"8999Y_I<4<<<tM':<<===+	++,,,, 	FGGG, 	< 	<JD$:>z:JV499T*%5666PVL<4<<<tM':<<===1<11222:d<&8::;;;; 	2333+ 	< 	<JD$<4<<<tM':<<===:d<&8::;;;; 	DEEE0022)//11 	N 	NJD$";/:UUUF=v====](;==>>>$ NL499T.5I+J+JLLMMM '&((	F#i..FFFGGG	** 	: 	:I*955G8I88g8889999&'''+,,,bccc^___`aaad-...]^^^d$%%%abbbd6777VWWW !! A P P4D4J4J34O4O P P P?(=??@@@ C!R!R5F5L5LS5Q5Q!R!R!RA)?AABBB I/000JKKKGHHH$24/#-

 

 

    444555
 }b 	

 
	
,

,
,---	/ ##J//F	/	
%&&&	(OOO	
1F;/
1
1222	
2VK0
2
2333	
5Cz 233
5
5666 (&'''hf%&'''  :
%%bqb)	4I444 88:;
 

 (!1133,
 
	:osW=== I
5uQGGGHHHI I I I I I I I I I I I I I I IIIJJJJ 	: 	: 	:8Q8899999999	: 

-.....sT   R! !
S+SSZ2 +ZZ2 ZZ2 ZZ2 2
[<[[__main__)Nr   Nr   r   NNFFFFr   )r   hermes_bootstrapModuleNotFoundErrorr  r  concurrent.futures
concurrentcontextvarsrc  r  r  r  	getLoggerr   r  r  randomr  sslrd  r  r  r!  typesr   urllib.requesturllibr  typingr   r   r   r   urllib.parser   r	   r
   r   pathlibr   hermes_constantsr   agent.process_bootstrapr   r   r   r   r   r   r   agent.iteration_budgetr   hermes_cli.env_loaderr   hermes_cli.timeoutsr   r   _hermes_home__file__parent_project_env_loaded_env_paths	_env_pathr  r  r   r   r   r   tools.terminal_toolr   r    r!   r"   _set_approval_callbackr#   _set_sudo_password_callbackr$   r%   tools.tool_result_storager&   r'   tools.interruptr(   r  tools.browser_toolr)   agent.memory_managerr*   r+   r,   agent.think_scrubberr-   agent.retry_utilsr.   agent.error_classifierr/   r0   agent.prompt_builderr1   r2   r3   r4   r5   r6   r7   r8   rC  r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   agent.context_compressorrC   agent.subdirectory_hintsrD   agent.prompt_cachingrE   rF   rG   rH   rI   rJ   rK   rL   rM   agent.usage_pricingrN   rO   agent.codex_responses_adapterrP   r  rQ   r  rR   r  rS   agent.displayrT   rU   _build_tool_previewrV   _get_cute_tool_message_implrW   rX   _get_tool_emojiagent.tool_guardrailsrY   rZ   r[   r\   r]    agent.tool_result_classificationr^   r  r_   agent.trajectoryr`   ra   rb   rq  agent.message_sanitizationrc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   agent.tool_dispatch_helpersrn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   utilsr}   r~   r   r   r   r  r   _MAX_TOOL_WORKERSEvent_openrouter_prewarm_doner   r  r   r   re  r   r   r  r   r   r   r  fireFirer  r   r   <module>r     s#
   .	 	 	 	
 	D	              		8	$	$ 				  				 



 



       ! ! ! ! ! !      , , , , , , , , , , , , 7 7 7 7 7 7 7 7 7 7             , , , , , ,                  3 2 2 2 2 2 5 4 4 4 4 4       
   tH~~$v-&&<\ZZZ  K& G G	:IFFFFG KKIJJJ            N M M M M M M M M M            U T T T T T T T ; ; ; ; ; ; . . . . . . h g g g g g g g g g 7 7 7 7 7 7 . . . . . . E E E E E E E E                                           7 6 6 6 6 6 < < < < < < > > > > > >                                        D D D D D D D D                                                                                                               " t s s s s s s s s s s s s s % % % % % %   +9?,,   T     %) #  #  #Tz #47$J #	 #  #  #  #F
d 
 
 
 
$
 $
 $
 $
 $
	 $
 $
 $
N`7Z `7Z `7Z `7Z `7Z `7Z `7Z `7ZFo  !#S/ S/S/S/ S/ 	S/
 S/ S/ S/ S/ S/ S/ S/ S/ S/ S/ S/l zKKKDIdOOOOO s   	 