diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py index 01fbf9e745..8f020f17d5 100644 --- a/hermes_cli/gateway.py +++ b/hermes_cli/gateway.py @@ -131,9 +131,26 @@ def _get_service_pids() -> set: def _get_parent_pid(pid: int) -> int | None: - """Return the parent PID for ``pid``, or ``None`` when unavailable.""" + """Return the parent PID for ``pid``, or ``None`` when unavailable. + + Uses psutil (core dependency) which works on every platform. The + older implementation shelled out to ``ps -o ppid= -p ``, which + silently fails on Windows (no ``ps``) so the ancestor walk terminated + at self — the caller's dedup / exclude logic then couldn't distinguish + "hermes CLI that invoked this scan" from "real gateway process". + """ if pid <= 1: return None + try: + import psutil # type: ignore + return psutil.Process(pid).ppid() or None + except ImportError: + pass + except Exception: + return None + # Fallback: shell out to ps (POSIX only — bare ``ps`` doesn't exist on Windows). + if not shutil.which("ps"): + return None try: result = subprocess.run( ["ps", "-o", "ppid=", "-p", str(pid)], @@ -416,9 +433,53 @@ def _scan_gateway_pids(exclude_pids: set[int], all_profiles: bool = False) -> li except (OSError, subprocess.TimeoutExpired): return [] + # Windows-specific: collapse venv launcher stubs. A venv-built + # ``pythonw.exe`` in ``/Scripts/`` is a ~100 KB launcher exe + # that spawns the base Python (e.g. ``C:\Program Files\Python311\ + # pythonw.exe``) with the same command line, preserving the venv's + # ``pyvenv.cfg`` context. This is standard Windows CPython venv + # behaviour — BUT it means every gateway run produces two pythonw + # PIDs with identical command lines (one launcher stub, one actual + # interpreter) which is confusing in ``gateway status`` output. + # Filter the stub: if a PID in our result is the PARENT of another + # PID in our result, and both are pythonw.exe, the parent is the + # launcher stub — drop it, keep the child. + if is_windows() and len(pids) > 1: + pids = _filter_venv_launcher_stubs(pids) + return pids +def _filter_venv_launcher_stubs(pids: list[int]) -> list[int]: + """Drop venv-launcher ``pythonw.exe`` stubs that are parents of the real + interpreter process. See comment at the tail of ``_scan_gateway_pids``. + + Uses ``psutil`` (core dependency). Safe on any platform; only invoked + on Windows by the caller because the stub pattern is Windows-specific. + """ + try: + import psutil # type: ignore + except ImportError: + return pids + + pid_set = set(pids) + # Collect each PID's parent so we can flag "child of another matched PID". + parent_of: dict[int, int | None] = {} + for pid in pids: + try: + parent_of[pid] = psutil.Process(pid).ppid() + except (psutil.NoSuchProcess, psutil.AccessDenied): + parent_of[pid] = None + + # For each child whose parent is also in our set, drop the parent. + drop: set[int] = set() + for pid, ppid in parent_of.items(): + if ppid is not None and ppid in pid_set: + drop.add(ppid) + + return [p for p in pids if p not in drop] + + def find_gateway_pids(exclude_pids: set | None = None, all_profiles: bool = False) -> list: """Find PIDs of running gateway processes. diff --git a/scripts/install.ps1 b/scripts/install.ps1 index 2f24ea8970..e16d083f15 100644 --- a/scripts/install.ps1 +++ b/scripts/install.ps1 @@ -1161,6 +1161,115 @@ function Install-NodeDeps { } } +function Install-PlatformSdks { + # Ensure messaging-platform SDKs matching tokens the user added to + # ~/.hermes/.env are importable. Two problems this solves: + # + # 1. The tiered `uv pip install` cascade above can fall through to a + # lower tier when the first fails (common when RL git deps choke), + # which silently skips some messaging SDKs from [messaging]. + # 2. `uv` creates the venv without pip. If a messaging SDK ends up + # missing, the user can't `pip install python-telegram-bot` to + # recover — pip simply isn't in their venv. + # + # Strategy: bootstrap pip via `python -m ensurepip` (idempotent), then + # for each token set in .env, verify the matching SDK imports. If not, + # run one targeted `pip install` as last-chance recovery. Keeps fresh + # Windows installs from hitting silent "python-telegram-bot not installed" + # at runtime. + if ($NoVenv) { + Write-Info "Skipping platform-SDK verification (-NoVenv: no venv to bootstrap)" + return + } + + $pythonExe = "$InstallDir\venv\Scripts\python.exe" + if (-not (Test-Path $pythonExe)) { + Write-Warn "Skipping platform-SDK verification: $pythonExe not found" + return + } + + $envPath = "$HermesHome\.env" + if (-not (Test-Path $envPath)) { return } + $envLines = Get-Content $envPath -ErrorAction SilentlyContinue + + # Map: env var set in .env -> (import name, pip spec matching [messaging] extra). + # Specs mirror pyproject.toml to avoid version drift. + $sdkMap = @( + @{ Var = "TELEGRAM_BOT_TOKEN"; Import = "telegram"; Spec = "python-telegram-bot[webhooks]>=22.6,<23" }, + @{ Var = "DISCORD_BOT_TOKEN"; Import = "discord"; Spec = "discord.py[voice]>=2.7.1,<3" }, + @{ Var = "SLACK_BOT_TOKEN"; Import = "slack_sdk"; Spec = "slack-sdk>=3.27.0,<4" }, + @{ Var = "SLACK_APP_TOKEN"; Import = "slack_bolt";Spec = "slack-bolt>=1.18.0,<2" }, + @{ Var = "WHATSAPP_ENABLED"; Import = "qrcode"; Spec = "qrcode>=7.0,<8" } + ) + + # Which tokens are actually set (not placeholder)? + $needed = @() + foreach ($sdk in $sdkMap) { + $match = $envLines | Where-Object { + $_ -match ("^" + [regex]::Escape($sdk.Var) + "=.+") ` + -and $_ -notmatch "your-token-here" ` + -and $_ -notmatch "^\s*#" + } + if ($match) { $needed += $sdk } + } + if ($needed.Count -eq 0) { return } + + Write-Host "" + Write-Info "Verifying platform SDKs for tokens found in $envPath ..." + + # Verify each SDK's import without triggering side-effect imports. + # Quirk: PowerShell wraps non-zero-exit native stderr as a + # NativeCommandError that prints even with `2>$null` / `*> $null` + # unless we set $ErrorActionPreference to SilentlyContinue for the + # span. Save + restore rather than nuking globally. + $prevEAP = $ErrorActionPreference + $ErrorActionPreference = "SilentlyContinue" + try { + $missing = @() + foreach ($sdk in $needed) { + & $pythonExe -c "import $($sdk.Import)" 2>&1 | Out-Null + if ($LASTEXITCODE -ne 0) { + $missing += $sdk + Write-Warn " $($sdk.Import) NOT importable (needed for $($sdk.Var))" + } else { + Write-Success " $($sdk.Import) OK" + } + } + } finally { + $ErrorActionPreference = $prevEAP + } + if ($missing.Count -eq 0) { return } + + # Bootstrap pip into the venv if it isn't there. `uv` creates venvs + # without pip; ensurepip is the stdlib-blessed way to add it. + $prevEAP = $ErrorActionPreference + $ErrorActionPreference = "SilentlyContinue" + try { + & $pythonExe -m pip --version 2>&1 | Out-Null + if ($LASTEXITCODE -ne 0) { + Write-Info "Bootstrapping pip into venv (uv doesn't ship pip)..." + & $pythonExe -m ensurepip --upgrade 2>&1 | Out-Null + if ($LASTEXITCODE -ne 0) { + Write-Warn "ensurepip failed — can't auto-install missing SDKs." + Write-Info "Manual recovery: $UvCmd pip install `"$($missing[0].Spec)`"" + return + } + } + + foreach ($sdk in $missing) { + Write-Info " Installing $($sdk.Spec) ..." + & $pythonExe -m pip install $sdk.Spec 2>&1 | ForEach-Object { Write-Host " $_" } + if ($LASTEXITCODE -eq 0) { + Write-Success " Installed $($sdk.Import)" + } else { + Write-Warn " Failed to install $($sdk.Spec). Recover manually: $pythonExe -m pip install `"$($sdk.Spec)`"" + } + } + } finally { + $ErrorActionPreference = $prevEAP + } +} + function Invoke-SetupWizard { if ($SkipSetup) { Write-Info "Skipping setup wizard (-SkipSetup)" @@ -1343,6 +1452,7 @@ function Main { Set-PathVariable Copy-ConfigTemplates Invoke-SetupWizard + Install-PlatformSdks Start-GatewayIfConfigured Write-Completion