
    sj                     \   U d Z ddlmZmZ ddlmZ d ed          iZeeef         e	d<   dZ
ee	d<   d	Zee	d
<   dZee	d<   dZee	d<   dZee	d<   defdZ ed           G d d                      Z e            ZdZee	d<   dZee	d<   dZee	d<   dZee	d<   dZee	d <   d!ed"z  defd#Zd"S )$zConfigurable budget constants for tool result persistence.

Per-tool resolution: pinned > config overrides > registry > default.
    )	dataclassfield)Dict	read_fileinfPINNED_THRESHOLDSi DEFAULT_RESULT_SIZE_CHARSi@ DEFAULT_TURN_BUDGET_CHARSi  DEFAULT_PREVIEW_SIZE_CHARSiP  DEFAULT_MCP_RESULT_SIZE_CHARSmcp_MCP_TOOL_PREFIXreturnc                  2   	 ddl m}   |             }t          |t                    r|                    d          nd}t          |t                    r.|                    d          }|t          |          }|dk    r|S n# t          $ r Y nw xY wt          S )u
  Read ``tool_budget.mcp_result_size_chars`` from the active config.

    Goes through :func:`hermes_cli.config.load_config_readonly` (the
    sanctioned read path — raw config.yaml parsing outside owner modules
    is guarded by tests/hermes_cli/test_config_read_guard.py). Fully
    guarded: any error, missing key, or non-positive value returns the
    built-in default. The ``tool_budget:`` block name is shared with the
    wider configurable-caps proposal (#80508) so the two can merge
    without a key rename.
    r   )load_config_readonlytool_budgetNmcp_result_size_chars)hermes_cli.configr   
isinstancedictgetint	Exceptionr   )r   datablockrawvalues        7/home/agent/.hermes/hermes-agent/tools/budget_config.py_configured_mcp_result_sizer   '   s    ::::::##%%+5dD+A+AK'''teT"" 	!))344CC199 L   ((s   A>B 
BBT)frozenc                       e Zd ZU dZeZeed<   eZ	eed<   e
Zeed<   eZeed<    ee          Zeeef         ed<   ded	eez  fd
ZdS )BudgetConfiga|  Immutable budget constants for the 3-layer tool result persistence system.

    Layer 2 (per-result): resolve_threshold(tool_name) -> threshold in chars.
    Layer 3 (per-turn):   turn_budget -> aggregate char budget across all tool
                          results in a single assistant turn.
    Preview:              preview_size -> inline snippet size after persistence.
    default_result_sizeturn_budgetpreview_sizemcp_result_size)default_factorytool_overrides	tool_namer   c                 Z   |t           v rt           |         S || j        v r| j        |         S |                    t                    rt	          | j        | j                  S ddlm} |	                    || j                  }|t          d          k    r|S t	          || j                  S )a  Resolve the persistence threshold for a tool.

        Priority: pinned -> tool_overrides -> mcp_ prefix -> registry
        per-tool -> default.

        MCP tools (``mcp_`` prefix) get a tighter default threshold
        (``mcp_result_size``, 50K chars) because MCP servers return
        un-paginated payloads with no per-tool registry entry to constrain
        them. The value is additionally capped at ``default_result_size``
        so a context-scaled budget for a small model still constrains MCP
        results the same way it constrains registry values.

        The registry per-tool value is capped at ``default_result_size`` so a
        context-scaled budget (small model) actually constrains tools that
        register a large fixed ``max_result_size_chars`` (web/terminal/x_search
        all register 100K). For the default budget this is a no-op because both
        equal 100K; for a scaled-down budget it prevents a per-tool registry
        value from re-inflating the cap past the model's window (#23767).
        r   )registry)defaultr   )r   r(   
startswithr   minr&   r#   tools.registryr+   get_max_result_sizefloat)selfr)   r+   registry_values       r   resolve_thresholdzBudgetConfig.resolve_thresholdR   s    ( )))$Y//+++&y1100 	Gt+T-EFFF++++++!55iIa5bbU5\\))!!>4#;<<<    N)__name__
__module____qualname____doc__r	   r#   r   __annotations__r
   r$   r   r%   r   r&   r   r   r(   r   strr1   r4    r5   r   r"   r"   B   s            98880K0002L#2228OS888%*U4%@%@%@NDcN@@@=3 =3; = = = = = =r5   r"      _CHARS_PER_TOKENg333333?_PER_RESULT_WINDOW_FRACTIONg333333?_PER_TURN_WINDOW_FRACTIONi@  _MIN_RESULT_SIZE_CHARSi>  _MIN_TURN_BUDGET_CHARScontext_lengthNc                    t                      }| r| dk    r"|t          k    rt          S t          |          S | t          z  }t          |t          z            }t          |t          z            }t          t          t          |t                              }t          t          t          |t                              }t          ||t          |          S )a  Return a BudgetConfig scaled to the active model's context window.

    The fixed defaults (100K result / 200K turn chars) are correct for large
    (200K+ token) models but blind to small ones: on a 65K-token model a single
    tool result persisted at the 100K-char threshold, or a 200K-char turn
    budget (~50K tokens), can by itself approach or exceed the whole window and
    force an oversized request (#23767).

    Scaling keeps large models byte-identical to today (the proportional value
    is clamped to the existing defaults as a CAP) while shrinking the budget for
    small models proportionally to their window, floored so a usable preview
    always survives.
    r   )r&   )r#   r$   r%   r&   )r   r   DEFAULT_BUDGETr"   r>   r   r?   r@   maxrA   r.   r	   rB   r
   r   )rC   r&   window_chars
per_resultper_turns        r   budget_for_context_windowrJ      s     233O =^q00;;;!!O<<<<!$44L\$??@@J<";;<<H +S=V-W-WXXJ)3x9R+S+STTH&/'	   r5   )r9   dataclassesr   r   typingr   r1   r   r;   r:   r	   r   r
   r   r   r   r   r"   rE   r>   r?   r@   rA   rB   rJ   r<   r5   r   <module>rM      s    
 ) ( ( ( ( ( ( (      
 u' 4U
#    ") 3 ( ( (!( 3 ( ( ("' C ' ' ' &, s + + +    )S ) ) ) )6 $-= -= -= -= -= -= -= -=b   #    &* U ) ) )#' 5 ' ' ' $  # # #$  $ $ $#cDj #\ # # # # # #r5   