§
    øžyj¿*  ã                  ój   — d Z ddlmZ ddlZddlZddgZdadd	„Zdd„Zdddœdd„Z	dd„Z
dd„Zdd„ZdS )u  Windows-safe stdio configuration.

On Windows, Python's ``sys.stdout``/``sys.stderr`` default to the console's
active code page (often ``cp1252``, sometimes ``cp437``, occasionally ``cp932``
on Japanese locales, etc.).  Hermes's banners, tool output feed, and slash
command listings all contain Unicode: box-drawing characters (``â”€â”Œâ”�â””â”˜â”œâ”¤``),
mathematical and geometric symbols (``â—† â—‡ â—Ž â–£ âš” âš– â†’``), and user-supplied
text in any language.  Printing those to a cp1252 console raises
``UnicodeEncodeError: 'charmap' codec can't encode characterâ€¦`` and kills the
whole CLI before the REPL even opens.

The fix is to force UTF-8 on the Python side and also flip the console's
code page to UTF-8 (65001).  Both matter: Python-level only helps when
Python's stdout is a real TTY; code-page flipping lets subprocesses and
child Python ``print()`` calls agree on encoding.

This module is a no-op on every non-Windows platform, and idempotent.
Entry points (``cli.py`` ``main``, ``hermes_cli/main.py`` CLI dispatch,
``gateway/run.py`` startup) call :func:`configure_windows_stdio` exactly
once early in startup.

Patterns cribbed from Claude Code (``src/utils/platform.ts``), OpenCode
(``packages/opencode/src/pty/index.ts`` env injection), and OpenAI Codex
(``codex-rs/core/src/unified_exec/process_manager.rs``).  None of those
actually flip the console code page â€” they rely on their runtime (Node or
Rust) writing UTF-16 to the Win32 console API and letting the terminal
sort it out.  Python doesn't get that luxury.
é    )ÚannotationsNÚconfigure_windows_stdioÚ
is_windowsFÚreturnÚboolc                 ó"   — t           j        dk    S )z4Return True iff running on native Windows (not WSL).Úwin32)ÚsysÚplatform© ó    ú6/home/ragecks/.hermes/hermes-agent/hermes_cli/stdio.pyr   r   )   s   € åŒ<˜7Ò"Ð"r   ÚNonec                 óž   — 	 ddl } | j        j        }|                     d¦  «         |                     d¦  «         dS # t
          $ r Y dS w xY w)us  Set the attached console's input and output code pages to UTF-8.

    Uses ``SetConsoleCP`` / ``SetConsoleOutputCP`` via ``ctypes``.  Failure
    is silent â€” if there's no attached console (e.g. Hermes is running
    behind a redirected stdout, under a service, or inside a PTY-less CI
    runner) these calls simply return 0 and we move on.

    CP_UTF8 is 65001.
    r   Niéý  )ÚctypesÚwindllÚkernel32ÚSetConsoleCPÚSetConsoleOutputCPÚ	Exception)r   r   s     r   Ú_flip_console_code_page_to_utf8r   .   sl   € ð
Øˆˆˆà”=Ô)ˆà×Ò˜eÑ$Ô$Ð$Ø×#Ò# EÑ*Ô*Ð*Ð*Ð*øÝð ð ð ð 	ˆˆðøøøs   ‚:> ¾
AÁAúutf-8Úreplace©ÚencodingÚerrorsr   Ústrr   c               ón   — 	 t          | dd¦  «        }|€dS  |||¬¦  «         dS # t          $ r Y dS w xY w)zöReconfigure a text stream to UTF-8 in place.

    Uses ``TextIOWrapper.reconfigure`` (Python 3.7+).  If the stream isn't
    a ``TextIOWrapper`` (e.g. it's been redirected to an ``io.StringIO``
    during tests), we skip rather than blow up.
    ÚreconfigureNr   )Úgetattrr   )Ústreamr   r   r   s       r   Ú_reconfigure_streamr"   E   s`   € ðÝ˜f m°TÑ:Ô:ˆØÐØˆFØˆ˜X¨fÐ5Ñ5Ô5Ð5Ð5Ð5øÝð ð ð Øˆˆðøøøs   ‚& —& ¦
4³4c                 ó’  — t           rdS t          ¦   «         sda dS t          j                             d¦  «        dv rda dS t          j                             dd¦  «         t          j                             dd¦  «         t          ¦   «         } | rMt          j                             d	¦  «        s.t          j                             d
¦  «        s| t          j        d	<   t          ¦   «          t          ¦   «          t          t          j        ¦  «         t          t          j        ¦  «         t          t          j        ¦  «         da dS )uû  Force UTF-8 stdio on Windows.  No-op elsewhere.

    Idempotent â€” safe to call multiple times from different entry points.

    Returns ``True`` if anything was actually changed, ``False`` on
    non-Windows or on a repeat call.

    Set ``HERMES_DISABLE_WINDOWS_UTF8=1`` in the environment to opt out
    (for diagnosing encoding-related bugs by forcing the old cp1252 path).

    Also sets a sensible default ``EDITOR`` on Windows if none is already
    set â€” see :func:`_default_windows_editor`.
    FTÚHERMES_DISABLE_WINDOWS_UTF8>   Ú1ÚyesÚTrueÚtrueÚPYTHONIOENCODINGr   Ú
PYTHONUTF8r%   ÚEDITORÚVISUAL)Ú_CONFIGUREDr   ÚosÚenvironÚgetÚ
setdefaultÚ_default_windows_editorÚ_augment_path_with_known_toolsr   r"   r
   ÚstdoutÚstderrÚstdin)Ú_default_editors    r   r   r   U   s&  € õ  ð ØˆuÝ‰<Œ<ð àˆØˆuå	„z‡~‚~Ð3Ñ4Ô4Ð8TÐTÐTØˆØˆuõ
 „J×ÒÐ,¨gÑ6Ô6Ð6õ „J×Ò˜,¨Ñ,Ô,Ð,õ .Ñ/Ô/€OØð /�rœzŸ~š~¨hÑ7Ô7ð /ÅÄ
ÇÂÈxÑ@XÔ@Xð /Ø.�Œ
�8Ñõ #Ñ$Ô$Ð$õ $Ñ%Ô%Ð%õ �œ
Ñ#Ô#Ð#Ý�œ
Ñ#Ô#Ð#õ �œ	Ñ"Ô"Ð"à€KØˆ4r   c                 ó<   — ddl } |                      d¦  «        rdS dS )u³  Return a Windows-appropriate default for ``$EDITOR``.

    Priority order, first match wins:

    1. ``notepad`` â€” ships with every Windows install, no deps, works as a
       blocking editor (``subprocess.call(["notepad", file])`` blocks until
       the user closes the window).  This is the "always-works" default.

    The prompt_toolkit buffer's ``open_in_editor`` and Hermes's
    ``hermes config edit`` both honour ``$EDITOR``.  Users who prefer a
    different editor can override:

    - VSCode: ``$env:EDITOR = "code --wait"``  (``--wait`` is critical;
      without it the editor returns immediately and any input is lost)
    - Notepad++: ``$env:EDITOR = "'C:\Program Files\Notepad++\notepad++.exe' -multiInst -nosession"``
    - Neovim: ``$env:EDITOR = "nvim"``  (if installed)

    Set this before launching Hermes (User env var in Windows Settings, or
    export in a PowerShell profile) and Hermes picks it up automatically.
    r   NÚnotepadÚ )ÚshutilÚwhich)r;   s    r   r2   r2   ¡   s/   € ð* €M€M€Mð
 ‡|‚|�IÑÔð Øˆyð ˆ2r   c            
     ó\  — t          ¦   «         sdS t          j                             dd¦  «        } | sdS t          j                             | ddd¦  «        t          j                             | ddd¦  «        t          j                             | dddd¦  «        t          j                             | dd	d
d¦  «        t          j                             | ddd¦  «        g}t          j                             dd¦  «        }d„ |                     t          j        ¦  «        D ¦   «         }g }|D ]L}t          j                             |¦  «        r+| 	                    ¦   «         |vr| 
                    |¦  «         ŒM|r2t          j                             g |¢|‘¦  «        t          j        d<   dS dS )uS  Prepend well-known Hermes-managed tool directories to os.environ['PATH'].

    Fixes the "User PATH was just updated but my process can't see it" gap on
    Windows.  When install.ps1 runs, it adds entries like
    ``%LOCALAPPDATA%\hermes\git\bin`` to the User PATH via
    ``SetEnvironmentVariable(..., "User")``.  That write propagates to newly
    *spawned* processes only â€” already-running shells (including the one the
    user invokes ``hermes`` from right after install) retain their old PATH.

    Any subprocess Hermes spawns â€” bash, ``rg``, ``grep``, ``npm`` â€” inherits
    that stale PATH and reports commands as missing even though they're on
    disk.  Symptom: ``search_files`` reports "rg/find not available" when
    the user clearly just installed ripgrep.

    Patch-up strategy: add the known Hermes-managed tool directories to our
    PATH at startup so subprocess calls resolve correctly.  No-op on POSIX
    and when the directories don't exist.  The User PATH broadcast still
    happens in the background for future shells; this just smooths over
    the first-launch gap.
    NÚLOCALAPPDATAr:   ÚhermesÚgitÚcmdÚbinÚusrzhermes-agentÚvenvÚScriptsÚ	MicrosoftÚWinGetÚLinksÚPATHc                ó:   — h | ]}|¯|                      ¦   «         ’ŒS r   )Úlower)Ú.0Úps     r   ú	<setcomp>z1_augment_path_with_known_tools.<locals>.<setcomp>ô   s%   € ÐIÐIÐI AÀqÐI�a—g’g‘i”iÐIÐIÐIr   )r   r.   r/   r0   ÚpathÚjoinÚsplitÚpathsepÚisdirrK   Úappend)Úlocal_appdataÚcandidate_dirsÚexistingÚexisting_lowerÚprependÚds         r   r3   r3   Ã   s„  € õ* ‰<Œ<ð Øˆõ ”J—N’N >°2Ñ6Ô6€MØð Øˆõ 	Œ�Š�] H¨e°UÑ;Ô;Ý
Œ�Š�] H¨e°UÑ;Ô;Ý
Œ�Š�] H¨e°U¸EÑBÔBõ 	Œ�Š�] H¨n¸fÀiÑPÔPõ
 	Œ�Š�] K°¸7ÑCÔCð€Nõ Œz�~Š~˜f bÑ)Ô)€HØIÐI¨¯ª½¼
Ñ)CÔ)CÐIÑIÔI€NØ€GØð ð ˆÝŒ7�=Š=˜ÑÔð 	 §¢¡	¤	°Ð ?Ð ?Ø�NŠN˜1ÑÔÐøàð CÝœZŸ_š_Ð-A¨wÐ-A¸Ð-AÑBÔB�Œ
�6ÑÐÐðCð Cr   )r   r   )r   r   )r   r   r   r   r   r   )r   r   )Ú__doc__Ú
__future__r   r.   r
   Ú__all__r-   r   r   r"   r   r2   r3   r   r   r   ú<module>r^      så   ððð ð: #Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø 
€
€
€
à$ lÐ
3€ð €ð#ð #ð #ð #ð
ð ð ð ð. 4;È)ð ð ð ð ð ð ð Ið Ið Ið IðXð ð ð ðD8Cð 8Cð 8Cð 8Cð 8Cð 8Cr   