§
    ãFjº  ã                  ó|   — d Z ddlmZ ddlZddlZddlmZ g d¢Zej        dk    Z	dd„Z
dZdZdZdd„Zdd„Zdd„ZdS )uæ  Windows subprocess compatibility helpers.

Hermes is developed on Linux / macOS and tested natively on Windows too.
Several common subprocess patterns break silently-or-loudly on Windows:

* ``["npm", "install", ...]`` â€” on Windows ``npm`` is ``npm.cmd``, a batch
  shim.  ``subprocess.Popen(["npm", ...])`` fails with WinError 193
  ("not a valid Win32 application") because CreateProcessW can't run a
  ``.cmd`` file without ``shell=True`` or PATHEXT resolution.

* ``start_new_session=True`` â€” on POSIX, this maps to ``os.setsid()`` and
  actually detaches the child.  On Windows it's silently ignored; the
  Windows equivalent is ``CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS``
  creationflags, which Python only applies when you pass them explicitly.

* Console-window flashes â€” every ``subprocess.Popen`` of a ``.exe`` on
  Windows spawns a cmd window briefly unless ``CREATE_NO_WINDOW`` is
  passed.  Cosmetic but jarring for background daemons.

This module centralizes the platform-branching logic so the rest of the
codebase doesn't sprinkle ``if sys.platform == "win32":`` everywhere.

**All helpers are no-ops on non-Windows** â€” calling them in Linux/macOS
code paths is safe by design.  That's the "do no damage on POSIX"
guarantee.
é    )ÚannotationsN)ÚSequence)Ú
IS_WINDOWSÚresolve_node_commandÚwindows_detach_flagsÚwindows_hide_flagsÚwindows_detach_popen_kwargsÚwin32ÚnameÚstrÚargvúSequence[str]Úreturnú	list[str]c                óB   — t          j        | ¦  «        }|r|g|¢S | g|¢S )uä  Resolve a Node-ecosystem command name to an absolute-path argv.

    On Windows, commands like ``npm``, ``npx``, ``yarn``, ``pnpm``,
    ``playwright``, ``prettier`` ship as ``.cmd`` files (batch shims).
    ``subprocess.Popen(["npm", "install"])`` fails with WinError 193
    because CreateProcessW doesn't execute batch files directly.

    ``shutil.which(name)`` *does* resolve ``.cmd`` via PATHEXT and returns
    the fully-qualified path â€” which CreateProcessW accepts because the
    extension tells Windows to route through ``cmd.exe /c``.

    On POSIX ``shutil.which`` also returns a fully-qualified path when
    found.  That's a small change from bare-name resolution (the OS does
    its own PATH search) but functionally identical and has the side
    benefit of making the argv reproducible in logs.

    Behavior when the command is not on PATH:
    - On Windows: return the bare name â€” caller can still try with
      ``shell=True`` as a last resort, OR the subsequent Popen will
      raise FileNotFoundError with a readable error we want to surface.
    - On POSIX: same.  Bare ``npm`` on a Linux box without npm installed
      fails the same way it did before this function existed.

    Args:
        name: The command name to resolve (``npm``, ``npx``, ``node`` â€¦).
        argv: The remaining arguments.  Must NOT include ``name`` itself â€”
            this function builds the full argv list.

    Returns:
        A list suitable for passing to subprocess.Popen/run/call.
    )ÚshutilÚwhich)r   r   Úresolveds      ú</usr/local/lib/hermes-agent/hermes_cli/_subprocess_compat.pyr   r   3   s6   € õ@ Œ|˜DÑ!Ô!€HØð !ØÐ ˜4Ð Ð Øˆ=4ˆ=Ðó    i   é   i   Úintc                 óB   — t           sdS t          t          z  t          z  S )uG  Return Win32 creationflags that detach a child from the parent
    console and process group.  0 on non-Windows.

    Pair with ``start_new_session=False`` (default) when calling
    subprocess.Popen â€” on POSIX use ``start_new_session=True`` instead,
    which maps to ``os.setsid()`` in the child.

    Rationale:
    - ``CREATE_NEW_PROCESS_GROUP`` â€” child has its own process group so
      Ctrl+C in the parent console doesn't propagate.
    - ``DETACHED_PROCESS`` â€” child has no console at all.  Necessary for
      background daemons (gateway watchers, update respawners) because
      without it, closing the console kills the child.
    - ``CREATE_NO_WINDOW`` â€” suppress the brief cmd flash that would
      otherwise appear when launching a console app.  Redundant with
      DETACHED_PROCESS but explicit for clarity.
    r   )r   Ú_CREATE_NEW_PROCESS_GROUPÚ_DETACHED_PROCESSÚ_CREATE_NO_WINDOW© r   r   r   r   f   s#   € õ$ ð ØˆqÝ$Õ'8Ñ8Õ;LÑLÐLr   c                 ó"   — t           sdS t          S )u;  Return Win32 creationflags that merely hide the child's console
    window without detaching the child.  0 on non-Windows.

    Use for short-lived console apps spawned as part of a larger
    operation (``taskkill``, ``where``, version probes) where we want no
    flash but also want to collect stdout/exit code synchronously.

    The key difference from :func:`windows_detach_flags`: NO
    ``DETACHED_PROCESS`` â€” the child still inherits stdio handles so
    ``capture_output=True`` works.  ``DETACHED_PROCESS`` would sever
    stdio and break stdout capture.
    r   )r   r   r   r   r   r   r   }   s   € õ ð ØˆqÝÐr   Údictc                 ó8   — t           rdt          ¦   «         iS ddiS )uü  Return a dict of Popen kwargs that detach a child on Windows and
    fall back to the POSIX equivalent (``start_new_session=True``) on
    Linux/macOS.

    Usage pattern:

    .. code-block:: python

        subprocess.Popen(
            argv,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            stdin=subprocess.DEVNULL,
            close_fds=True,
            **windows_detach_popen_kwargs(),
        )

    This replaces the unsafe-on-Windows pattern:

    .. code-block:: python

        subprocess.Popen(..., start_new_session=True)

    which silently fails to detach on Windows (the flag is accepted but
    has no effect â€” the child stays attached to the parent's console
    and dies when the console closes).
    ÚcreationflagsÚstart_new_sessionT)r   r   r   r   r   r	   r	      s)   € õ8 ð 9ØÕ!5Ñ!7Ô!7Ð8Ð8Ø Ð&Ð&r   )r   r   r   r   r   r   )r   r   )r   r   )Ú__doc__Ú
__future__r   r   ÚsysÚtypingr   Ú__all__Úplatformr   r   r   r   r   r   r   r	   r   r   r   ú<module>r)      sÙ   ððð ð6 #Ð "Ð "Ð "Ð "Ð "à €€€Ø 
€
€
€
Ø Ð Ð Ð Ð Ð ðð ð €ð Œ\˜WÒ$€
ð#ð #ð #ð #ð\ 'Ð ØÐ ØÐ ðMð Mð Mð Mð.ð ð ð ð$'ð 'ð 'ð 'ð 'ð 'r   