Files
hermes-agent/tools/process_registry_wsl.py
Teknium 1cea505a54 refactor(tools): move WSL launcher detection to process_registry_wsl
_is_wsl_launcher_command and _WSL_LAUNCHER_NAMES move unchanged into
tools/process_registry_wsl.py; process_registry imports the function back.
Code-health FILE_LINES ratchet on PR #106742 (process_registry.py 2793 > 2788).

(cherry picked from commit f35413feae9748a630119fcc08c46d070b22e2cd)
2026-10-07 10:57:33 -07:00

35 lines
1.2 KiB
Python

"""Detect background spawns routed through a ``wsl[.exe]`` launcher chain (#120546)."""
from __future__ import annotations
import os
import shlex
_WSL_LAUNCHER_NAMES = frozenset({"wsl", "wsl.exe"})
def _is_wsl_launcher_command(command: str) -> bool:
"""True when *command* routes through a ``wsl[.exe]`` launcher chain (#120546).
The host PID recorded for such a spawn belongs to the short-lived launcher;
grandchildren inside the VM outlive it, so the entry must say so instead of
letting host-side hunting fail silently.
"""
if not isinstance(command, str) or not command.strip():
return False
candidates = []
try:
candidates.append((shlex.split(command, posix=True) or [""])[0])
except ValueError:
pass
# POSIX shlex eats Windows backslashes (``C:\...\wsl.exe``), so also try
# the naive first token where path separators survive.
words = command.strip().split()
if words:
candidates.append(words[0])
for first in candidates:
base = os.path.basename(first.replace("\\", "/")).strip("'\"").lower()
if base in _WSL_LAUNCHER_NAMES:
return True
return False