
    9j                     4   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<    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_CHARST)frozenc                       e Zd ZU dZ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)default_factorytool_overrides	tool_namereturnc                     |t           v rt           |         S || j        v r| j        |         S ddlm} |                    || j                  }|t          d          k    r|S t          || j                  S )aT  Resolve the persistence threshold for a tool.

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

        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   tools.registryr   get_max_result_sizer   floatmin)selfr   r   registry_values       9/home/rurouni/.hermes/hermes-agent/tools/budget_config.pyresolve_thresholdzBudgetConfig.resolve_threshold%   s     )))$Y//+++&y11++++++!55iIa5bbU5\\))!!>4#;<<<    N)__name__
__module____qualname____doc__r	   r   int__annotations__r
   r   r   r   r   dictr   r   strr   r     r!   r   r   r      s            98880K0002L#222%*U4%@%@%@NDcN@@@=3 =3; = = = = = =r!   r      _CHARS_PER_TOKENg333333?_PER_RESULT_WINDOW_FRACTIONg333333?_PER_TURN_WINDOW_FRACTIONi@  _MIN_RESULT_SIZE_CHARSi>  _MIN_TURN_BUDGET_CHARScontext_lengthNr   c                 ^   | r| dk    r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   )DEFAULT_BUDGETr,   r&   r-   r.   maxr/   r   r	   r0   r
   r   r   )r1   window_chars
per_resultper_turns       r   budget_for_context_windowr8   T   s      ^q00!$44L\$??@@J<";;<<H +S=V-W-WXXJ)3x9R+S+STTH&/   r!   )r%   dataclassesr   r   typingr   r   r   r)   r'   r	   r&   r
   r   r   r3   r,   r-   r.   r/   r0   r8   r*   r!   r   <module>r;      s    
 ) ( ( ( ( ( ( (      
 u' 4U
#    ") 3 ( ( (!( 3 ( ( ("' C ' ' ' $"= "= "= "= "= "= "= "=L   #    &* U ) ) )#' 5 ' ' ' $  # # #$  $ $ $cDj \      r!   