mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 04:11:35 +00:00
`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"
97 lines
5.1 KiB
Python
97 lines
5.1 KiB
Python
"""Process-identity contract: the two kill/relaunch predicates and the profile liveness probe
|
|
defer to the canonical matchers instead of argv substrings (root AGENTS.md process-identity rule).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from hermes_cli.dashboard_procs import _is_desktop_local_serve_cmdline
|
|
from hermes_cli.update_cmd_windows import _hermes_holder_subcommand, _is_backend_argv
|
|
|
|
LOOPBACK = "--host 127.0.0.1 --port 0"
|
|
|
|
# (cmdline, holder subcommand, desktop-local reap?, Windows updater "Desktop backend"?). Substring
|
|
# scanners get every "trap" row wrong: "serve" appears inside --preserve-cache / observer.py / a flag
|
|
# value. The updater's kill set additionally requires the Desktop's `-m hermes_cli.main` spawn shape —
|
|
# a user-launched `hermes serve` / `hermes dashboard` is refused on, never tree-killed.
|
|
CMDLINES = [
|
|
("python -m hermes_cli.main serve " + LOOPBACK, "serve", True, True),
|
|
("python -m hermes_cli.main dashboard", "dashboard", False, True),
|
|
("/venv/bin/hermes serve --isolated --host=127.0.0.1 --port=0 --ssh-owner-nonce abc", "serve", True, False),
|
|
(r"C:\hermes\.venv\Scripts\hermes.exe serve --host 100.106.105.2 --port 9119", "serve", False, False),
|
|
("hermes.exe dashboard", "dashboard", False, False),
|
|
("hermes --profile ops serve " + LOOPBACK, "serve", True, False),
|
|
("hermes -m serve kanban --preserve-cache " + LOOPBACK, "kanban", False, False),
|
|
("python -m hermes_cli.main kanban --preserve-cache " + LOOPBACK, "kanban", False, False),
|
|
("hermes --reasoning high dashboard " + LOOPBACK, "dashboard", False, False),
|
|
("hermes gateway run --replace", "gateway", False, False),
|
|
("hermes chat --model serve", "chat", False, False),
|
|
("python observer.py serve " + LOOPBACK, None, False, False),
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("cmdline,subcommand,reapable,desktop_backend", CMDLINES)
|
|
def test_kill_and_relaunch_predicates_agree_with_the_canonical_holder_matcher(
|
|
cmdline, subcommand, reapable, desktop_backend):
|
|
assert _hermes_holder_subcommand(cmdline) == subcommand
|
|
# Desktop-local reap (a KILL path): serve + loopback + ephemeral port, decided by tokens.
|
|
assert _is_desktop_local_serve_cmdline(cmdline) is reapable
|
|
# Windows updater backend classifier (taskkill /T on orphans): canonical subcommand AND Desktop spawn shape.
|
|
assert _is_backend_argv(cmdline.lower()) is desktop_backend
|
|
|
|
|
|
def test_desktop_local_serve_spares_fixed_port_and_remote_hosts():
|
|
assert not _is_desktop_local_serve_cmdline("hermes serve --host 100.106.105.2 --port 9119 --skip-build")
|
|
assert not _is_desktop_local_serve_cmdline("hermes serve --host 127.0.0.1 --port 9119")
|
|
assert _is_desktop_local_serve_cmdline("hermes serve --host localhost --port 0")
|
|
|
|
|
|
def test_profile_liveness_is_the_shared_ladder(tmp_path, monkeypatch):
|
|
"""``_check_gateway_running`` is ``resolve_gateway_liveness`` scoped to the profile dir, with the
|
|
PID rung reading (never cleaning) THAT profile's ``gateway.pid``."""
|
|
import gateway.status as gw_status
|
|
from hermes_cli.profiles import _check_gateway_running
|
|
|
|
seen: dict = {}
|
|
|
|
def fake_resolve(**kwargs):
|
|
seen.update(kwargs)
|
|
return gw_status.GatewayLiveness(running=True, pid=1, source="pid")
|
|
|
|
monkeypatch.setattr(gw_status, "resolve_gateway_liveness", fake_resolve)
|
|
calls: list = []
|
|
monkeypatch.setattr(gw_status, "get_running_pid",
|
|
lambda path, cleanup_stale=True: calls.append((path, cleanup_stale)))
|
|
assert _check_gateway_running(tmp_path) is True
|
|
assert seen["profile_dir"] == tmp_path
|
|
seen["pid_probe"](tmp_path / "gateway.pid")
|
|
assert calls == [(tmp_path / "gateway.pid", False)]
|
|
|
|
|
|
# ``hermes serve`` is a substring of ``hermes server``: a terminal multiplexer started as
|
|
# ``herdr --session hermes server`` was SIGTERMed by ``hermes update`` and its unit restarted (#121156).
|
|
DECOY = "tool --name hermes server 30"
|
|
BACKENDS = [
|
|
"/opt/hermes/venv/bin/python -m hermes_cli.main serve --port 0",
|
|
"/usr/bin/python3 /opt/hermes/hermes_cli/main.py dashboard --no-open",
|
|
]
|
|
|
|
|
|
def test_dashboard_scan_selects_entrypoint_plus_subcommand_tokens_never_substrings(monkeypatch):
|
|
import hermes_cli.dashboard_procs as dashboard_procs
|
|
import hermes_cli.process_identity as process_identity
|
|
|
|
monkeypatch.setattr(dashboard_procs, "_iter_process_table",
|
|
lambda: [(4242, DECOY), *((5000 + i, cmd) for i, cmd in enumerate(BACKENDS))])
|
|
monkeypatch.setattr(process_identity, "ledger_entries", lambda: [])
|
|
assert dashboard_procs._scan_dashboard_processes() == [(5000, BACKENDS[0]), (5001, BACKENDS[1])]
|
|
|
|
|
|
def test_dashboard_runtime_parse_refuses_the_decoy_and_reads_a_real_backend():
|
|
from hermes_cli.main_dashboard import _parse_dashboard_runtime
|
|
|
|
assert _parse_dashboard_runtime(DECOY) is None
|
|
assert _parse_dashboard_runtime(BACKENDS[0]) == ("serve", "127.0.0.1", 0)
|
|
# launchd ProgramArguments arrive ``shlex.join``ed: a quoted path with spaces is still the entry.
|
|
assert _parse_dashboard_runtime("'/Users/a b/venv/bin/hermes' dashboard --port 9200") == ("dashboard", "127.0.0.1", 9200)
|