
    [j                         d Z ddlZ ej        dej                  Z ej        d          Z ej        d          Z ej        d          Zdedefd	Z	dedefd
Z
dS )uH  Strip ANSI escape sequences from subprocess output.

Used by terminal_tool, code_execution_tool, and process_registry to clean
command output before returning it to the model.  This prevents ANSI codes
from entering the model's context — which is the root cause of models
copying escape sequences into file writes.

Covers the full ECMA-48 spec: CSI (including private-mode ``?`` prefix,
colon-separated params, intermediate bytes), OSC (BEL and ST terminators),
DCS/SOS/PM/APC string sequences, nF multi-byte escapes, Fp/Fe/Fs
single-byte escapes, and 8-bit C1 control characters.
    Nz\x1b(?:\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]|\][\s\S]*?(?:\x07|\x1b\\)|[PX^_][\s\S]*?(?:\x1b\\)|[\x20-\x2f]+[\x30-\x7e]|[\x30-\x7e])|\x9b[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]|\x9d[\s\S]*?(?:\x07|\x9c)|[\x80-\x9f]z[\x1b\x80-\x9f]z [\x00-\x08\x0b\x0c\x0e-\x1f\x7f]z[\x00-\x08\x0b-\x1f\x7f-\x9f]textreturnc                 t    | rt                               |           s| S t                              d|           S )u   Remove ANSI escape sequences from text.

    Returns the input unchanged (fast path) when no ESC or C1 bytes are
    present.  Safe to call on any string — clean text passes through
    with negligible overhead.
     )_HAS_ESCAPEsearch_ANSI_ESCAPE_REsubr   s    2/home/ice/.hermes/hermes-agent/tools/ansi_strip.py
strip_ansir   .   s<      {))$// r4(((    c                     | rt                               |           s| S t          |           } d| v r*|                     dd                              dd          } t                              d|           S )uA  Sanitize stored/untrusted text before echoing it to a terminal.

    Removes ANSI/ECMA-48 escape sequences AND bare control characters,
    preserving only newlines and tabs (carriage returns are normalized
    to newlines so ``\r``-overwrite spoofing can't hide content).

    Use this when re-rendering conversation history or other persisted
    text in a terminal UI (e.g. the ``/resume`` recap): a message that
    arrived with embedded escapes — pasted content, gateway-origin
    text, or model output echoing injected tool results — must not be
    able to clear the screen, retitle the window, move the cursor, or
    restyle adjacent UI when replayed. Rich's ``Text()`` does NOT
    neutralize raw escape bytes, so sanitization has to happen before
    display. Mirrors openai/codex#31494 (``sanitize_user_text``).
    z

r   )_HAS_CONTROLr   r   replace_CONTROL_CHARS_REr
   r   s    r   sanitize_display_textr   :   su       |**400 dDt||||FD))11$==  T***r   )__doc__recompileDOTALLr	   r   r   r   strr   r    r   r   <module>r      s     
			"*
 I   bj+,, BJBCC  rz:;;	)S 	)S 	) 	) 	) 	)+ + + + + + + +r   