Skip to content

The agent that grows with you

fix(gateway): isolate PID check and credentials per profile (#74872)

webtecnicae0abbc4d1849
diff --git a/gateway/status.py b/gateway/status.py
index 9704d745946..cc611509ef8 100644
--- a/gateway/status.py
+++ b/gateway/status.py
@@ -698,6 +698,30 @@ def _get_code_identity_fields() -> dict[str, Any]:
return {}
+def _pid_record_belongs_to_current_profile(
+ record: Optional[dict[str, Any]],
+) -> bool:
+ """Return True when the PID record's ``hermes_home`` matches the current process.
+
+ PID records written by ``_build_pid_record()`` include the gateway's
+ ``hermes_home`` at write time. If a profile gateway was started (or recorded)
+ under a different HERMES_HOME, the record belongs to a different profile
+ and must be ignored — otherwise the default-profile gateway can mistakenly
+ assume the identity of another profile (issue #74872).
+
+ Records that predate the ``hermes_home`` field (pre-#74872 gateways) are
+ accepted conservatively (no field → assume current profile).
+ """
+ if not isinstance(record, dict):
+ return False
+ record_home = record.get("hermes_home")
+ if not record_home:
+ # Records without hermes_home belong to a pre-#74872 gateway;
+ # accept them conservatively.
+ return True
+ return _same_hermes_home(record_home, _get_process_hermes_home())
+
+
def _build_runtime_status_record() -> dict[str, Any]:
payload = _build_pid_record()
payload.update({
@@ -1546,6 +1570,13 @@ def get_runtime_status_running_pid(
):
return None
+ # When no explicit expected_home is given (active profile context),
+ # verify the persisted record's hermes_home matches the current process.
+ # This prevents the default-profile gateway from assuming another
+ # profile's identity via a stale runtime status record (#74872).
+ if expected_home is None and not _pid_record_belongs_to_current_profile(payload):
+ return None
+
if _record_matches_live_gateway_pid(payload, pid, expected_home=expected_home):
return pid
return None
@@ -2432,6 +2463,9 @@ def get_running_pid(
if recorded_start is not None and current_start is not None and current_start != recorded_start:
continue
+ if not _pid_record_belongs_to_current_profile(record):
+ continue
+
if _record_matches_live_gateway_pid(record, pid):
return pid
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 392c2e1e210..fc6819ab440 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -6363,6 +6363,24 @@ def _guard_existing_gateway_process_conflict(replace: bool = False) -> None:
logger.debug("Existing-gateway process probe failed", exc_info=True)
return
if pid is None:
+ # get_running_pid() now filters records by the current profile's
+ # HERMES_HOME (via _pid_record_belongs_to_current_profile). When no
+ # match was found, check whether a stale PID file from a different
+ # profile exists — the user may have switched profiles while the old
+ # gateway is still running.
+ try:
+ from gateway.status import _read_pid_record, _pid_record_belongs_to_current_profile
+
+ stale = _read_pid_record()
+ if stale is not None and not _pid_record_belongs_to_current_profile(stale):
+ stale_home = stale.get("hermes_home", "<unknown>")
+ logger.warning(
+ "PID file belongs to another profile (hermes_home=%s). "
+ "The old gateway may still be running under that profile.",
+ stale_home,
+ )
+ except Exception:
+ pass
return
print_error(