Skip to content

The agent that grows with you

fix(gateway): accept trailing-separator HERMES_HOME spellings, bound the assignment name

liuhao1024deda2c9dc3c1
diff --git a/gateway/status.py b/gateway/status.py
index 4ac649a99ef..71ca5be41cd 100644
--- a/gateway/status.py
+++ b/gateway/status.py
@@ -558,15 +558,19 @@ def profile_flag_value(command: str) -> Optional[str]:
return None
-_HERMES_HOME_ASSIGNMENT_RE = re.compile(r"hermes_home=(?:\"([^\"]*)\"|'([^']*)'|(\S+))")
+_HERMES_HOME_ASSIGNMENT_RE = re.compile(r"(?:^|\s)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."""
+ this replaces let ``HERMES_HOME=/root/profiles/ops`` claim a ``/root/profiles/ops2`` gateway.
+ The name is token-bounded too (``FOO=hermes_home=/x`` is not an assignment), and a trailing
+ separator on the value is stripped -- ``HERMES_HOME=/root/.hermes/`` (systemd ``Environment=``
+ or a shell wrapper spelling) is the same home as ``/root/.hermes``; callers strip the profile
+ home the same way."""
return [
- next(g for g in m.groups() if g is not None)
+ next(g for g in m.groups() if g is not None).rstrip("/")
for m in _HERMES_HOME_ASSIGNMENT_RE.finditer(command)
]
@@ -578,7 +582,7 @@ def _command_line_belongs_to_profile(command: str, profile_home: Path) -> bool:
``HERMES_HOME=`` on argv; the default gateway runs bare. Separators normalized."""
command_lc = command.lower().replace("\\", "/")
profile_name = _profile_name_for_home(profile_home)
- home_lc = str(profile_home).lower().replace("\\", "/")
+ home_lc = str(profile_home).lower().replace("\\", "/").rstrip("/")
if profile_name is not None and profile_name != "default":
if profile_flag_value(command_lc) == profile_name.lower():
return True
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 87a256a3351..7aefa83a7ca 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -580,8 +580,10 @@ def _scan_gateway_pids(
hermes_home_assignments,
)
current_home = str(get_hermes_home().resolve())
- # Forward slashes on both sides of the HERMES_HOME= match (mirrors gateway.status).
- current_home_lc = current_home.lower().replace("\\", "/")
+ # Forward slashes on both sides of the HERMES_HOME= match (mirrors gateway.status), and no
+ # trailing separator: the assignments parser strips one, so the systemd ``Environment=``
+ # spelling (``HERMES_HOME=/root/.hermes/``) compares equal to the resolved home.
+ current_home_lc = current_home.lower().replace("\\", "/").rstrip("/")
current_profile_arg = _profile_arg(current_home)
current_profile_name = current_profile_arg.split()[-1] if current_profile_arg else ""
current_profile_name_lc = current_profile_name.lower()
diff --git a/tests/gateway/test_multiplex_secondary_port_binding_env.py b/tests/gateway/test_multiplex_secondary_port_binding_env.py
index 6fc983921b2..0700c079061 100644
--- a/tests/gateway/test_multiplex_secondary_port_binding_env.py
+++ b/tests/gateway/test_multiplex_secondary_port_binding_env.py
@@ -77,3 +77,27 @@ def test_named_gateway_is_never_the_default_profile_process(tmp_path, cmdline, m
monkeypatch.setattr(gw, "is_windows", lambda: False)
monkeypatch.setattr(gw.os.path, "isdir", lambda p: p == "/proc")
assert gw._scan_gateway_pids(set()) == []
+
+
+ ("cmdline", "expected"),
+ [
+ ("HERMES_HOME={home}2 hermes gateway run", []), # longer sibling home
+ ("HERMES_HOME={home}/ hermes gateway run", [424242]), # trailing-separator spelling
+ ("HERMES_HOME={home} hermes gateway run", [424242]), # exact home
+ ],
+)
+def test_scan_gateway_pids_claims_own_home_spellings_not_the_sibling(
+ tmp_path, cmdline, expected, monkeypatch
+):
+ """``_scan_gateway_pids`` drives the mirrored HERMES_HOME predicate: the process-table
+ fallback must not sweep a longer sibling home's live gateway, while the supervisor
+ trailing-separator spelling (``HERMES_HOME=/root/.hermes/``) is still its own home."""
+ import hermes_cli.gateway as gw
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ rendered = cmdline.format(home=tmp_path)
+ monkeypatch.setattr(gw, "_iter_proc_cmdlines", lambda exclude: iter([(424242, rendered)]))
+ monkeypatch.setattr(gw, "_get_ancestor_pids", set)
+ monkeypatch.setattr(gw, "is_windows", lambda: False)
+ monkeypatch.setattr(gw.os.path, "isdir", lambda p: p == "/proc")
+ assert gw._scan_gateway_pids(set()) == expected
diff --git a/tests/gateway/test_status.py b/tests/gateway/test_status.py
index 1bdac6fca45..f82f75a761a 100644
--- a/tests/gateway/test_status.py
+++ b/tests/gateway/test_status.py
@@ -421,6 +421,38 @@ class TestGatewayRuntimeStatus:
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_command_line_belongs_to_profile_accepts_trailing_separator(self):
+ """A supervisor can export the home with a trailing separator (systemd ``Environment=``,
+ a ``sh -c 'HERMES_HOME=.../ gateway run'`` wrapper); that spelling is the same home, so
+ stripping one separator on both sides keeps the match without re-opening the sibling hole."""
+ home = Path("/fixture/profiles/ops")
+ assert (
+ status._command_line_belongs_to_profile(
+ "HERMES_HOME=/fixture/profiles/ops/ hermes gateway run", home
+ )
+ is True
+ )
+ # ... while a separator-plus-suffix is still a different (sibling) home.
+ assert not status._command_line_belongs_to_profile(
+ "HERMES_HOME=/fixture/profiles/ops/2 hermes gateway run", home
+ )
+ # Default-profile branch strips the same way.
+ home = Path("/opt/hermes-data")
+ assert (
+ status._command_line_belongs_to_profile(
+ "HERMES_HOME=/opt/hermes-data/ hermes gateway run", home
+ )
+ is True
+ )
+
+ def test_command_line_belongs_to_profile_ignores_embedded_assignment_name(self):
+ """``FOO=hermes_home=/x`` embeds the name inside another token: it is not a HERMES_HOME
+ assignment (the name must be token-bounded, like the value)."""
+ home = Path("/fixture/profiles/ops")
+ assert not status._command_line_belongs_to_profile(
+ "FOO=hermes_home=/fixture/profiles/ops hermes gateway run", home
+ )
+
def test_write_runtime_status_explicit_none_clears_stale_fields(self, tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path))