
    `gj                    p    U d Z ddlmZ ddlmZmZ dZdZdZda	de
d<   dd	Zdd
ZddZddZddZddZy)u  Configurable tool-output truncation limits.

Ported from anomalyco/opencode PR #23770 (``feat(truncate): allow
configuring tool output truncation limits``).

OpenCode hardcoded ``MAX_LINES = 2000`` and ``MAX_BYTES = 50 * 1024``
as tool-output truncation thresholds. Hermes-agent had the same
hardcoded constants in two places:

* ``tools/terminal_tool.py`` — ``MAX_OUTPUT_CHARS = 50000`` (terminal
  stdout/stderr cap)
* ``tools/file_operations.py`` — ``MAX_LINES = 2000`` /
  ``MAX_LINE_LENGTH = 2000`` (read_file pagination cap + per-line cap)

This module centralises those values behind a single config section
(``tool_output`` in ``config.yaml``) so power users can tune them
without patching the source. The existing hardcoded numbers remain as
defaults, so behaviour is unchanged when the config key is absent.

Example ``config.yaml``::

    tool_output:
      max_bytes: 100000        # terminal output cap (chars)
      max_lines: 5000          # read_file pagination + truncation cap
      max_line_length: 2000    # per-line length cap before '... [truncated]'

The limits reader is defensive: any error (missing config file, invalid
value type, etc.) falls back to the built-in defaults so tools never
fail because of a malformed config.
    )annotations)AnyDictiP  i  Nzdict | None_cached_limitsc                Z    	 t        |       }|dk  r|S |S # t        t        f$ r |cY S w xY w)z@Return ``value`` as a positive int, or ``default`` on any issue.r   )int	TypeError
ValueError)valuedefaultivs      K/root/.hermes/venv/lib/python3.12/site-packages/tools/tool_output_limits.py_coerce_positive_intr   0   s>    Z 
QwI	 z" s    **c                    t         t         S 	 ddlm}   |        xs i }t        |t              r|j                  d      nd}t        |t              si }t        |j                  d      t              t        |j                  d      t              t        |j                  d      t              da t         S # t        $ r i }Y pw xY w)	a  Return resolved tool-output limits, reading ``tool_output`` from config.

    Keys: ``max_bytes``, ``max_lines``, ``max_line_length``. Missing or
    invalid entries fall through to the ``DEFAULT_*`` constants. This
    function NEVER raises.

    Result is cached for the process lifetime to avoid repeated disk I/O
    on every tool call. Call ``_reset_tool_output_limits_cache()`` in
    tests that need a fresh read after config changes.
    Nr   )load_configtool_output	max_bytes	max_linesmax_line_length)r   r   r   )r   hermes_cli.configr   
isinstancedictget	Exceptionr   DEFAULT_MAX_BYTESDEFAULT_MAX_LINESDEFAULT_MAX_LINE_LENGTH)r   cfgsections      r   get_tool_output_limitsr    ;   s     !1m!r,6sD,A#''-(t'4(G
 *'++k*BDUV)'++k*BDUV/KK)*,C
N   s   AB7 7CCc                     da y)uA   Reset the cached limits — for tests or after config hot-reload.N)r        r   _reset_tool_output_limits_cacher$   \   s	     Nr#   c                     t               d   S )z?Shortcut for terminal-tool callers that only need the byte cap.r   r    r"   r#   r   get_max_bytesr'   b       !#K00r#   c                     t               d   S )z:Shortcut for file-ops callers that only need the line cap.r   r&   r"   r#   r   get_max_linesr*   g   r(   r#   c                     t               d   S )z>Shortcut for file-ops callers that only need the per-line cap.r   r&   r"   r#   r   get_max_line_lengthr,   l   s    !#$566r#   )r   r   r   r   returnr   )r-   zDict[str, int])r-   None)r-   r   )__doc__
__future__r   typingr   r   r   r   r   r   __annotations__r   r    r$   r'   r*   r,   r"   r#   r   <module>r3      sQ   > # 
     # "B1
1
7r#   