hermes-agent/tests/hermes_cli/test_dashboard_stop_spare_caller.py
teknium1 7bf1373cb8 fix(cli): select dashboard/serve backends by entrypoint + subcommand tokens, not argv substrings (#121156, salvage #121243)
`hermes update`'s post-update dashboard cleanup and `hermes dashboard --stop`
picked their kill list with `any(p in cmd for p in _DASHBOARD_PATTERNS)`,
i.e. "hermes serve" anywhere in the command line. "hermes serve" is a prefix
of "hermes server", so a terminal multiplexer started as
`herdr --session hermes server` was SIGTERMed by the updater, and (before
#124940) its unrelated systemd unit was restarted with every shell in it.
`_parse_dashboard_runtime` (launchd backend inventory, `--status`,
uninstall) used the same substrings.

Both now defer to the canonical token matcher
`update_cmd_windows._hermes_holder_subcommand`: a Hermes entry token
(`hermes`, `-m hermes_cli.main`, `.../hermes_cli/main.py`) followed, after
value flags, by exactly the `dashboard`/`serve` token. The matcher learns
the `hermes_cli/main.py` script entry (the one shape the substring table
covered that it did not) and strips single quotes, since launchd
`ProgramArguments` reach it `shlex.join`ed. `_DASHBOARD_PATTERNS` is
deleted; the spawn-ledger augmentation is unchanged.

The home-ownership fallback (`_hermes_home_for_pid` -> $HOME/.hermes) is
left as is: it is only unsafe when the selection has false positives, and
a process that passes the token matcher IS a Hermes backend whose default
home resolves that way. Point 3 of the issue (foreign unit restart) landed
in #124940.

Slim redo of #121243 by @MohamadKanso (same core hunks; that PR also
rewrites the entry detection positionally and reshapes the Windows holder
classifiers, beyond what this issue needs); #121781 by @salch-cred is a
later duplicate of the scan hunk mixed with an unrelated auth change.

Red on origin/main (2 failed) / green on head (2 passed):
tests/hermes_cli/test_process_identity_canonical_matchers.py -k "decoy or never_substrings"
2026-09-28 04:44:29 -07:00

42 lines
2.1 KiB
Python

"""Root selection for ``dashboard --stop`` / update cleanup must spare the caller.
The dashboard scan also selects the shell the ``--stop`` was typed into (``bash -c 'hermes
dashboard --stop'``: same entry token, same subcommand). Killing it takes down
the invoking terminal; the historical fix for the hosted-TUI case is descendant hygiene
(#113819), which is orthogonal to root selection.
"""
from __future__ import annotations
from unittest.mock import patch
from hermes_cli.dashboard_procs import _is_caller_wrapper_shell
from hermes_cli.main_dashboard import _find_stale_dashboard_pids
def test_scan_spares_only_the_callers_wrapper_shell(capsys):
"""The bash the ``--stop`` was typed into is dropped; the real backend and an unrelated
wrapper with the same argv shape (another user's ``bash -c 'hermes serve'``) stay targets."""
processes = [
(111, "/opt/hermes/bin/python hermes dashboard --port 9119"),
(222, "bash -c hermes dashboard --stop"),
(333, "bash -c hermes serve"),
]
with (
patch("hermes_cli.dashboard_procs._scan_dashboard_processes", return_value=processes),
patch("hermes_cli.dashboard_procs._caller_ancestor_pids", return_value={222}),
patch("hermes_cli.dashboard_procs._argv_head_command",
side_effect=lambda pid: "bash" if pid in (222, 333) else "python3.12"),
):
assert _find_stale_dashboard_pids() == [111, 333]
def test_python_headed_or_unreadable_ancestor_stays_killed():
"""A python-headed ancestor is a real backend (hosted TUI case) and must stay stoppable —
``--stop`` from inside the dashboard's own TUI — and an unreadable argv head is never spared."""
with patch("hermes_cli.dashboard_procs._argv_head_command", return_value="python3.12"):
assert _is_caller_wrapper_shell(4242, {4242}) is False
with patch("hermes_cli.dashboard_procs._argv_head_command", return_value=None):
assert _is_caller_wrapper_shell(4242, {4242}) is False
with patch("hermes_cli.dashboard_procs._argv_head_command", return_value="bash"):
assert _is_caller_wrapper_shell(4242, {4242}) is True