Skip to content

The agent that grows with you

fix(gateway): never report a copied profile dir as running

kuzi.0df9c8eff1c6
diff --git a/gateway/status.py b/gateway/status.py
index 96c84bb2f33..2c5306ce6d6 100644
--- a/gateway/status.py
+++ b/gateway/status.py
@@ -293,6 +293,35 @@ def scoped_lock_owner_label(record: Optional[dict[str, Any]]) -> Optional[str]:
return _profile_label_for_home(home) if isinstance(home, str) and home.strip() else None
+def _recorded_pid_home_conflict(profile_dir: Path) -> bool:
+ """``gateway.pid`` record names a DIFFERENT HERMES_HOME than ``profile_dir`` -> True.
+
+ A profile directory can be copied wholesale (sandbox injection, restore-from-backup,
+ cloning a profile). Its ``gateway.pid`` then belongs to a gateway of another home, and the
+ PID rung reports "gateway running" for a home that has none -- while ``hermes cron`` or
+ ``gateway list`` says the opposite on the same machine.
+ """
+ try:
+ with open(profile_dir / "gateway.pid", encoding="utf-8") as fh:
+ record = json.load(fh)
+ except Exception:
+ return False
+ return recorded_gateway_home_conflicts(record, expected_home=profile_dir)
+
+
+def _profile_dir_is_default_root_profile(profile_dir: Path) -> bool:
+ """True when ``profile_dir`` is ``<default root>/profiles/<name>`` (the pooled layout).
+
+ Rung 4 matches the multiplexer by profile *name*; a copied profile directory carries the
+ same name but no relation to that multiplexer, so the rung must not apply to it.
+ """
+ try:
+ from hermes_constants import get_default_hermes_root
+ return (Path(get_default_hermes_root()).resolve() / "profiles") == Path(profile_dir).resolve().parent
+ except Exception:
+ return False
+
+
def _get_pid_path() -> Path:
return _get_process_hermes_home() / "gateway.pid"
@@ -1366,6 +1395,10 @@ def resolve_gateway_liveness(
# Zero-arg call when unscoped: callers monkeypatch with zero-arg lambdas and
# /api/status's cache signature is keyed on the call shape.
pid = guarded(_pid_probe, profile_dir / "gateway.pid") if scoped else guarded(_pid_probe)
+ if pid is not None and scoped and _recorded_pid_home_conflict(profile_dir):
+ # The record belongs to another home (copied profile dir) -> not this home's gateway.
+ # Fall through to the remaining rungs instead of reporting a false green.
+ pid = None
if pid is not None:
return GatewayLiveness(running=True, pid=pid, source="pid")
health_body: Optional[dict[str, Any]] = None
@@ -1380,8 +1413,18 @@ def resolve_gateway_liveness(
if runtime is _UNSET:
reader_kwargs = {"path": profile_dir / "gateway_state.json"} if scoped else {}
runtime = guarded(_runtime_reader, **reader_kwargs)
+ # A scoped read that found no record must not degrade into "read the PROCESS home's file":
+ # ``get_runtime_status_running_pid(None, ...)`` re-reads the default path, and a copied profile
+ # directory keeps the profile NAME -- so the serving gateway's record would lend it a running PID
+ # it does not own. Hand rung 3 an empty record instead of None.
probe_kwargs = {"expected_home": profile_dir} if scoped else {}
- runtime_pid = guarded(_runtime_pid_probe, runtime, **probe_kwargs)
+ runtime_pid = guarded(
+ _runtime_pid_probe, {} if (scoped and runtime is None) else runtime, **probe_kwargs
+ )
+ if runtime_pid is not None and scoped and recorded_gateway_home_conflicts(
+ runtime if isinstance(runtime, dict) else None, expected_home=profile_dir):
+ # Same stale-green shape as rung 1, via a copied gateway_state.json.
+ runtime_pid = None
if runtime_pid is not None:
return GatewayLiveness(
running=True, pid=runtime_pid, source="runtime_status", health_body=health_body
@@ -1391,6 +1434,10 @@ def resolve_gateway_liveness(
# question is about the process's OWN home — which is a named profile inside a pooled
# `hermes --profile X serve` (the Desktop's per-profile backend answers its REST without
# `?profile=`), so it takes the same rung instead of reporting the served profile stopped.
+ if scoped and not _profile_dir_is_default_root_profile(profile_dir):
+ return GatewayLiveness(
+ running=False, pid=None, source="none", health_body=health_body, probe_error=probe_error
+ )
own_home = profile_dir if scoped else _get_process_hermes_home()
served = guarded(multiplexer_liveness_for_profile, own_home)
if served is not None: