Skip to content

The agent that grows with you

fix(update): never respawn backends from a foreign HERMES_HOME (#94030)

liuhao102457309c0cbbea
diff --git a/hermes_cli/dashboard_procs.py b/hermes_cli/dashboard_procs.py
index 2e8f2b16e89..3ca586c583d 100644
--- a/hermes_cli/dashboard_procs.py
+++ b/hermes_cli/dashboard_procs.py
@@ -274,27 +274,60 @@ def _profile_key_for_respawn(
return "profile:default"
+def _normalized_home_for_compare(home: str) -> str:
+ """Resolve *home* for install-identity comparison (#94030).
+
+ Same normalization ``_profile_key_for_respawn`` applies to ``home:``
+ keys, so symlinked / differently-spelled roots compare equal.
+ """
+ try:
+ return os.path.normcase(str(Path(home).resolve()))
+ except (OSError, RuntimeError, ValueError):
+ return os.path.normcase(home)
+
+
def _filter_dashboard_respawn_candidates(
candidates: list[tuple[int, list[str], str | None]],
+ *,
+ own_home: str | None = None,
) -> list[list[str]]:
"""Select which killed manual backends to respawn after ``hermes update``.
- Each candidate is ``(pid, argv, hermes_home)``.
+ Each candidate is ``(pid, argv, hermes_home)``. *own_home* is the
+ updating install's home; it defaults to this process's
+ ``get_hermes_home()`` and exists as a parameter so tests can pin it.
- Rules (#78821):
+ Rules (#78821, #94030):
1. Never resurrect Desktop ephemeral ``serve|dashboard --port 0``
backends — Desktop (``HERMES_DESKTOP_CHILD_PID``) owns their
lifecycle. These are also the PPID-1 orphans that previously
multiplied across updates because ``--port 0`` always binds a
fresh free port.
- 2. Dedupe by normalized cmdline (identical argv → one respawn).
- 3. Cap at most one managed backend per profile / ``HERMES_HOME``.
+ 2. Never replay a backend from a **foreign** ``HERMES_HOME``. The
+ respawn below is argv-only (no ``env=`` replay), so a foreign
+ backend would come back running on the *updating* install's home
+ and steal the foreign install's fixed port, leaving its own
+ supervisor (launchd/systemd/...) to crash-loop on ``EADDRINUSE``
+ (#94030). A foreign install's backend is owned by that install's
+ supervisor/user. An unreadable home (``None``) stays eligible —
+ keep the pre-#94030 behaviour when we cannot tell.
+ 3. Dedupe by normalized cmdline (identical argv → one respawn).
+ 4. Cap at most one managed backend per profile / ``HERMES_HOME``.
Intentionally does **not** blanket-skip every PPID-1 process: a prior
``hermes update`` respawn detaches with ``start_new_session=True``, so
fixed-port manual backends are reparented to init and must still be
eligible for the next update's #40449 restart.
"""
+ if own_home is None:
+ try:
+ from hermes_constants import get_hermes_home
+
+ own_home = str(get_hermes_home())
+ except Exception:
+ own_home = ""
+ own_key = _normalized_home_for_compare(own_home) if own_home else ""
+
selected: list[list[str]] = []
seen_cmdlines: set[tuple[str, ...]] = set()
seen_profiles: set[str] = set()
@@ -304,6 +337,8 @@ def _filter_dashboard_respawn_candidates(
continue
if _is_ephemeral_port_zero_backend(argv):
continue
+ if own_key and hermes_home and _normalized_home_for_compare(hermes_home) != own_key:
+ continue
norm = _normalize_dashboard_cmdline(argv)
if norm in seen_cmdlines:
continue
diff --git a/tests/hermes_cli/test_update_stale_dashboard.py b/tests/hermes_cli/test_update_stale_dashboard.py
index 0e9aca8e224..16b922e571e 100644
--- a/tests/hermes_cli/test_update_stale_dashboard.py
+++ b/tests/hermes_cli/test_update_stale_dashboard.py
@@ -633,10 +633,13 @@ class TestFilterDashboardRespawnCandidates:
home = "/tmp/hermes-home-a"
a = ["hermes", "dashboard", "--port", "8300"]
b = ["hermes", "dashboard", "--port", "8301"]
- out = _filter_dashboard_respawn_candidates([
- (1, a, home),
- (2, b, home),
- ])
+ out = _filter_dashboard_respawn_candidates(
+ [
+ (1, a, home),
+ (2, b, home),
+ ],
+ own_home=home,
+ )
assert out == [a]
def test_profile_flag_and_profiles_home_share_cap(self):
@@ -656,22 +659,108 @@ class TestFilterDashboardRespawnCandidates:
a = ["hermes", "--profile", "default", "dashboard", "--port", "8300"]
b = ["hermes", "dashboard", "--port", "8301"]
home = "/home/u/.hermes"
- out = _filter_dashboard_respawn_candidates([
- (1, a, home),
- (2, b, home),
- ])
+ out = _filter_dashboard_respawn_candidates(
+ [
+ (1, a, home),
+ (2, b, home),
+ ],
+ own_home=home,
+ )
assert out == [a]
- def test_distinct_dot_hermes_homes_do_not_share_cap(self):
+ def test_foreign_home_backend_is_not_replayed(self):
+ """A backend from another HERMES_HOME is never respawned (#94030).
+
+ Its supervisor/user owns its lifecycle; an argv-only replay would
+ run on the updating install's home and steal the foreign install's
+ fixed port. Supersedes the old "distinct homes don't share a cap"
+ pin — a foreign home is no longer replayed at all.
+ """
from hermes_cli.dashboard_procs import _filter_dashboard_respawn_candidates
a = ["hermes", "dashboard", "--port", "8300"]
b = ["hermes", "dashboard", "--port", "8301"]
- out = _filter_dashboard_respawn_candidates([
- (1, a, "/home/u/.hermes"),
- (2, b, "/work/project/.hermes"),
+ out = _filter_dashboard_respawn_candidates(
+ [
+ (1, a, "/home/u/.hermes"),
+ (2, b, "/work/project/.hermes"),
+ ],
+ own_home="/home/u/.hermes",
+ )
+ assert out == [a]
+
+ def test_skips_sidecar_fixed_port_serve_on_foreign_home(self):
+ """The reported case: launchd-supervised sidecar serve, fixed port."""
+ from hermes_cli.dashboard_procs import _filter_dashboard_respawn_candidates
+
+ argv = [
+ "/Users/u/.hermes-sidecar/hermes-agent/venv/bin/python",
+ "-m", "hermes_cli.main",
+ "serve", "--host", "127.0.0.1", "--port", "9118", "--skip-build",
+ ]
+ out = _filter_dashboard_respawn_candidates(
+ [(15364, argv, "/Users/u/.hermes-lifeos")],
+ own_home="/Users/u/.hermes",
+ )
+ assert out == []
+
+ def test_matching_hermes_home_is_kept(self):
+ from hermes_cli.dashboard_procs import _filter_dashboard_respawn_candidates
+
+ argv = ["hermes", "serve", "--host", "127.0.0.1", "--port", "9118"]
+ out = _filter_dashboard_respawn_candidates(
+ [(15364, argv, "/Users/u/.hermes")],
+ own_home="/Users/u/.hermes",
+ )
+ assert out == [argv]
+
+ def test_symlinked_hermes_home_compares_equal(self, tmp_path):
+ from hermes_cli.dashboard_procs import _filter_dashboard_respawn_candidates
+
+ real = tmp_path / "real-home"
+ real.mkdir()
+ link = tmp_path / "linked-home"
+ try:
+ link.symlink_to(real, target_is_directory=True)
+ except (OSError, NotImplementedError):
+ pytest.skip("symlinks unavailable on this platform")
+
+ argv = ["hermes", "serve", "--port", "9118"]
+ out = _filter_dashboard_respawn_candidates(
+ [(15364, argv, str(real))],
+ own_home=str(link),
+ )
+ assert out == [argv]
+
+ def test_unknown_home_stays_eligible(self):
+ """Unreadable HERMES_HOME (env probe failed) keeps pre-#94030 behaviour."""
+ from hermes_cli.dashboard_procs import _filter_dashboard_respawn_candidates
+
+ argv = ["hermes", "dashboard", "--port", "8300"]
+ out = _filter_dashboard_respawn_candidates(
+ [(1, argv, None)],
+ own_home="/home/u/.hermes",
+ )
+ assert out == [argv]
+
+ def test_own_home_defaults_to_get_hermes_home(self, monkeypatch):
+ from pathlib import Path
+
+ import hermes_constants
+ from hermes_cli.dashboard_procs import _filter_dashboard_respawn_candidates
+
+ monkeypatch.setattr(
+ hermes_constants, "get_hermes_home", lambda: Path("/home/u/.hermes")
+ )
+ argv = ["hermes", "serve", "--port", "9118"]
+ foreign = _filter_dashboard_respawn_candidates([
+ (1, argv, "/Users/u/.hermes-lifeos"),
+ ])
+ assert foreign == []
+ own = _filter_dashboard_respawn_candidates([
+ (1, argv, "/home/u/.hermes"),
])
- assert out == [a, b]
+ assert own == [argv]
def test_keeps_fixed_port_serve(self):
from hermes_cli.dashboard_procs import _filter_dashboard_respawn_candidates