The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): profile identity predicate must not accept a longer sibling HERMES_HOME
liuhao10245cca9b250c78
diff --git a/gateway/status.py b/gateway/status.pyindex b575e5982a1..4ac649a99ef 100644--- a/gateway/status.py+++ b/gateway/status.py@@ -558,6 +558,19 @@ def profile_flag_value(command: str) -> Optional[str]:return None+_HERMES_HOME_ASSIGNMENT_RE = re.compile(r"hermes_home=(?:\"([^\"]*)\"|'([^']*)'|(\S+))")+++def hermes_home_assignments(command: str) -> list[str]:+ """Values of every ``HERMES_HOME=<value>`` assignment in ``command`` (the caller lowercases+ and normalizes separators). Values are token-bounded, quotes stripped: the substring test+ this replaces let ``HERMES_HOME=/root/profiles/ops`` claim a ``/root/profiles/ops2`` gateway."""+ return [+ next(g for g in m.groups() if g is not None)+ for m in _HERMES_HOME_ASSIGNMENT_RE.finditer(command)+ ]++def _command_line_belongs_to_profile(command: str, profile_home: Path) -> bool:"""True when a gateway command line belongs to ``profile_home`` (mirrors``hermes_cli.gateway._matches_current_profile``): a stale state file can record a PID recycled@@ -567,14 +580,17 @@ def _command_line_belongs_to_profile(command: str, profile_home: Path) -> bool:profile_name = _profile_name_for_home(profile_home)home_lc = str(profile_home).lower().replace("\\", "/")if profile_name is not None and profile_name != "default":- return profile_flag_value(command_lc) == profile_name.lower() or f"hermes_home={home_lc}" in command_lc+ if profile_flag_value(command_lc) == profile_name.lower():+ return True+ return home_lc in hermes_home_assignments(command_lc)# Default profile: accept unless argv names another profile (any spelling the CLI pre-parser# accepts, ``--profile=ops`` included -- a substring test let that gateway pass as the default's)# or a conflicting explicit HERMES_HOME= (its absence is not disqualifying -- HERMES_HOME usually# arrives via the env).if profile_flag_value(command_lc) is not None:return False- return not ("hermes_home=" in command_lc and f"hermes_home={home_lc}" not in command_lc)+ assignments = hermes_home_assignments(command_lc)+ return not (assignments and home_lc not in assignments)def _record_matches_live_gateway_pid(diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 8a717119d62..87a256a3351 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -574,7 +574,10 @@ def _scan_gateway_pids(# Strict matcher shared with gateway.status: requires a real ``gateway run`` argv, so# ``gateway status``/``dashboard`` siblings and ``python -m tui_gateway`` don't match.from gateway.status import (- looks_like_gateway_command_line, looks_like_gateway_runtime_command_line, profile_flag_value,+ looks_like_gateway_command_line,+ looks_like_gateway_runtime_command_line,+ profile_flag_value,+ hermes_home_assignments,)current_home = str(get_hermes_home().resolve())# Forward slashes on both sides of the HERMES_HOME= match (mirrors gateway.status).@@ -587,10 +590,9 @@ def _scan_gateway_pids(command_lc = command.lower().replace("\\", "/")if current_profile_name:# Token equality, not substring: `-p ops` must not claim (or SIGTERM) an `-p ops-2` gateway.- return (- profile_flag_value(command_lc) == current_profile_name_lc- or f"hermes_home={current_home_lc}" in command_lc- )+ if profile_flag_value(command_lc) == current_profile_name_lc:+ return True+ return current_home_lc in hermes_home_assignments(command_lc)# Default profile: accept unless argv advertises another profile in any spelling the CLI# pre-parser accepts (``--profile=ops`` slipped past a substring test, so a default-profile@@ -598,7 +600,8 @@ def _scan_gateway_pids(# wmic/CIM), so only a non-matching explicit HERMES_HOME= disqualifies.if profile_flag_value(command_lc) is not None:return False- return not ("hermes_home=" in command_lc and f"hermes_home={current_home_lc}" not in command_lc)+ assignments = hermes_home_assignments(command_lc)+ return not (assignments and current_home_lc not in assignments)def _consider(pid: int, command: str) -> None:matches_runtime = looks_like_gateway_command_line(command) or (diff --git a/tests/gateway/test_status.py b/tests/gateway/test_status.pyindex 658d88c1424..1bdac6fca45 100644--- a/tests/gateway/test_status.py+++ b/tests/gateway/test_status.py@@ -384,6 +384,43 @@ class TestGatewayRuntimeStatus:cmdline = r"hermes_home=c:\opt\data\profiles\coder hermes gateway run --replace"assert status._command_line_belongs_to_profile(cmdline, home) is True+ def test_command_line_belongs_to_profile_rejects_longer_sibling_home(self):+ """A substring test let ``HERMES_HOME=/root/profiles/ops2`` satisfy the ``ops``+ profile's predicate, so a stale state record could borrow the sibling's live+ gateway identity (same shape as the ``-p ops`` vs ``-p ops-2`` token rule)."""+ home = Path("/fixture/profiles/ops")+ for cmdline in (+ "HERMES_HOME=/fixture/profiles/ops2 hermes gateway run",+ "HERMES_HOME=/fixture/profiles/ops-backup hermes gateway run",+ ):+ assert not status._command_line_belongs_to_profile(cmdline, home), cmdline++ def test_command_line_belongs_to_profile_default_rejects_sibling_home(self):+ """The default-home branch had the same substring hole: an explicit+ ``HERMES_HOME`` extending the default home's path is a different home."""+ home = Path("/opt/hermes-data")+ assert (+ status._command_line_belongs_to_profile(+ "HERMES_HOME=/opt/hermes-data2 hermes gateway run", home+ )+ is False+ )+ # A non-conflicting assignment still matches; no assignment still matches (env-borne).+ assert (+ status._command_line_belongs_to_profile(+ "HERMES_HOME=/opt/hermes-data hermes gateway run", home+ )+ is True+ )+ assert status._command_line_belongs_to_profile("hermes gateway run", home) is True++ def test_command_line_belongs_to_profile_matches_quoted_home(self):+ """``HERMES_HOME="/path with space"`` (ps/wmic re-quoting): the value is+ quote-delimited, so a token-bound comparison must strip the quotes."""+ home = Path("/opt/data/profiles/coder with space")+ cmdline = 'hermes_home="/opt/data/profiles/coder with space" hermes gateway run'+ assert status._command_line_belongs_to_profile(cmdline, home) is True+def test_write_runtime_status_explicit_none_clears_stale_fields(self, tmp_path, monkeypatch):monkeypatch.setenv("HERMES_HOME", str(tmp_path))