The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): tighten shebang-exec script-name match to known console scripts
Adam Durham70598f52e117
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.pyindex 0f5617854e8..bc8c8ea8d40 100644--- a/hermes_cli/profiles.py+++ b/hermes_cli/profiles.py@@ -1371,6 +1371,12 @@ def _profile_bound_backend_pids(canon: str, profile_dir: Path) -> list[int]:# the shim directly. In that shape the OS-reported argv[0] is the# interpreter, not "hermes", so the checks below would otherwise miss it._python_interpreter_re = re.compile(r"^python[\d.]*w?(\.exe)?$")+ # The actual console-script entry points this project ships (see+ # pyproject.toml [project.scripts]) -- used to validate argv[1] against+ # a known shim identity rather than a loose prefix match, since argv[1]+ # can be ANY user-invoked python script path when argv[0] is a bare+ # interpreter.+ _HERMES_CONSOLE_SCRIPT_NAMES = frozenset({"hermes", "hermes-agent", "hermes-acp"})pids: list[int] = []for proc in psutil.process_iter(["pid", "name", "username", "cmdline"]):@@ -1398,8 +1404,19 @@ def _profile_bound_backend_pids(canon: str, profile_dir: Path) -> list[int]:or exe_name.startswith("hermes"))if not is_hermes and len(argv) >= 2 and _python_interpreter_re.match(exe_name):+ # Match against the actual known console-script entry points+ # (pyproject.toml [project.scripts]: hermes, hermes-agent,+ # hermes-acp) rather than a bare `startswith("hermes")` --+ # that looser check is fine for a directly-resolved executable+ # name (argv[0] IS the interpreter there, so a false match is+ # rare), but here argv[1] can be ANY user-invoked python+ # script path, and a bare prefix match would misidentify an+ # unrelated script the user happens to name e.g.+ # "hermes-notes.py" or "hermes-unrelated-tool" as the shim,+ # making it killable by profile delete.script_name = os.path.basename(str(argv[1])).lower()- is_hermes = script_name == "hermes" or script_name.startswith("hermes")+ script_stem = script_name.rsplit(".", 1)[0] if "." in script_name else script_name+ is_hermes = script_stem in _HERMES_CONSOLE_SCRIPT_NAMESif not is_hermes:continuediff --git a/tests/hermes_cli/test_profiles.py b/tests/hermes_cli/test_profiles.pyindex cd1f0a4c77f..48e4dce81bc 100644--- a/tests/hermes_cli/test_profiles.py+++ b/tests/hermes_cli/test_profiles.py@@ -367,6 +367,95 @@ class TestDeleteProfile:pids = profiles._profile_bound_backend_pids("coder", profile_dir)assert pids == [201]+ def test_backend_scan_rejects_unrelated_hermes_prefixed_script(self, profile_env, monkeypatch):+ """A user's own script that happens to start with "hermes" (e.g.+ hermes-notes.py, hermes-unrelated-tool) must NOT be misidentified as+ the console-script shim just because argv[0] is a python interpreter+ and argv[1]'s basename starts with "hermes" -- only the actual known+ console-script entry points (hermes, hermes-agent, hermes-acp) count.+ """+ create_profile("coder", no_alias=True)+ profile_dir = get_profile_dir("coder")++ class FakeProc:+ def __init__(self, pid, cmdline, username="me"):+ self.pid = pid+ self.info = {"pid": pid, "name": "python3", "username": username, "cmdline": cmdline}++ def parent(self):+ return None++ def username(self):+ return "me"++ def environ(self):+ return {}++ self_pid = os.getpid()+ procs = [+ # Looks like the shim by prefix alone, but is the user's own+ # unrelated tool -- must be rejected, not killed by profile delete.+ FakeProc(301, ["/usr/bin/python3", "/Users/x/scripts/hermes-notes.py",+ "--profile", "coder", "serve"]),+ FakeProc(302, ["/usr/bin/python3", "/Users/x/scripts/hermes-unrelated-tool",+ "--profile", "coder", "serve"]),+ ]++ fake_psutil = types.SimpleNamespace(+ process_iter=lambda attrs=None: iter(procs),+ Process=lambda pid=None: FakeProc(self_pid, []),+ NoSuchProcess=Exception,+ AccessDenied=Exception,+ ZombieProcess=Exception,+ )+ monkeypatch.setitem(sys.modules, "psutil", fake_psutil)++ pids = profiles._profile_bound_backend_pids("coder", profile_dir)+ assert pids == []++ def test_backend_scan_matches_all_known_console_script_shims(self, profile_env, monkeypatch):+ """The other two real console-script entry points (hermes-agent,+ hermes-acp -- see pyproject.toml [project.scripts]) must also be+ recognized via the shebang-exec path, not just the primary "hermes"+ shim.+ """+ create_profile("coder", no_alias=True)+ profile_dir = get_profile_dir("coder")++ class FakeProc:+ def __init__(self, pid, cmdline, username="me"):+ self.pid = pid+ self.info = {"pid": pid, "name": "python3", "username": username, "cmdline": cmdline}++ def parent(self):+ return None++ def username(self):+ return "me"++ def environ(self):+ return {}++ self_pid = os.getpid()+ procs = [+ FakeProc(401, ["/usr/bin/python3", "/Users/x/.local/bin/hermes-agent",+ "--profile", "coder", "serve"]),+ FakeProc(402, ["/usr/bin/python3", "/Users/x/.local/bin/hermes-acp",+ "--profile", "coder", "serve"]),+ ]++ fake_psutil = types.SimpleNamespace(+ process_iter=lambda attrs=None: iter(procs),+ Process=lambda pid=None: FakeProc(self_pid, []),+ NoSuchProcess=Exception,+ AccessDenied=Exception,+ ZombieProcess=Exception,+ )+ monkeypatch.setitem(sys.modules, "psutil", fake_psutil)++ pids = profiles._profile_bound_backend_pids("coder", profile_dir)+ assert set(pids) == {401, 402}+# ===================================================================# TestListProfiles