Skip to content

The agent that grows with you

fix(gateway): `--profile=ops` gateway is never matched as the default profile's

teknium1e03d3d00bf42
diff --git a/gateway/status.py b/gateway/status.py
index f90b89b22a5..5a410f696f6 100644
--- a/gateway/status.py
+++ b/gateway/status.py
@@ -402,9 +402,11 @@ def _command_line_belongs_to_profile(command: str, profile_home: Path) -> bool:
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
- # Default profile: accept unless argv names another profile or a conflicting explicit
- # HERMES_HOME= (its absence is not disqualifying -- HERMES_HOME usually arrives via the env).
- if "--profile " in command_lc or " -p " in 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)
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index c16e1f5603d..0c872421f56 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -592,9 +592,11 @@ def _scan_gateway_pids(
or f"hermes_home={current_home_lc}" in command_lc
)
- # Default profile: accept unless argv advertises another profile. HERMES_HOME may come via
- # env (invisible to wmic/CIM), so only a non-matching explicit HERMES_HOME= disqualifies.
- if "--profile " in command_lc or " -p " in 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
+ # fallback stop could SIGTERM the named gateway). HERMES_HOME may come via env (invisible to
+ # 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)
diff --git a/tests/gateway/test_multiplex_secondary_port_binding_env.py b/tests/gateway/test_multiplex_secondary_port_binding_env.py
index 80479702912..6fc983921b2 100644
--- a/tests/gateway/test_multiplex_secondary_port_binding_env.py
+++ b/tests/gateway/test_multiplex_secondary_port_binding_env.py
@@ -57,3 +57,23 @@ def test_profile_match_is_token_equality_not_substring(tmp_path, cmdline, expect
"""``-p ops`` must never claim (or let ``gateway stop`` SIGTERM) an ``-p ops-2`` gateway."""
from gateway.status import _command_line_belongs_to_profile
assert _command_line_belongs_to_profile(cmdline, tmp_path / "profiles" / "ops") is expected
+
+
[email protected]("cmdline", [
+ "/v/python -m hermes_cli.main --profile=ops gateway run",
+ "/v/python -m hermes_cli.main -p ops gateway run",
+ "/v/python -m hermes_cli.main --profile ops gateway run",
+])
+def test_named_gateway_is_never_the_default_profile_process(tmp_path, cmdline, monkeypatch):
+ """Every spelling of the profile flag the CLI pre-parser accepts marks a NAMED gateway, so neither
+ the default-home identity check nor the default profile's process-table fallback (what a
+ ``gateway stop`` with no pid file kills) may claim it -- ``--profile=ops`` used to pass both."""
+ import hermes_cli.gateway as gw
+ from gateway.status import _command_line_belongs_to_profile
+ assert _command_line_belongs_to_profile(cmdline, tmp_path) is False
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(gw, "_iter_proc_cmdlines", lambda exclude: iter([(424242, cmdline)]))
+ 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()) == []