Skip to content

The agent that grows with you

fix(gateway): a supervised launchd job is not an unmanageable domain

Tyler Lyon1c1f8e060867
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index ab30fcc8819..e0cba0812af 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -3904,9 +3904,25 @@ def _launchd_fallback_to_detached(reason: str, *, exit_on_failure: bool = True)
def _launchd_degrade_or_raise(exc: subprocess.CalledProcessError, what: str) -> None:
- """Shared launchctl failure policy: domain unmanageable (5/125) → detached fallback; else re-raise."""
+ """Shared launchctl failure policy: domain unmanageable (5/125) → detached fallback; else re-raise.
+
+ A 5/125 exit is evidence about the *domain* only when launchd is not already supervising this job.
+ EIO (5) is ``launchctl bootstrap``'s answer for a label that is already loaded, so the ordinary
+ "reinstall/restart over the live gateway" case lands here with the service up and supervised.
+ Degrading there is not a graceful fallback: it writes the permanent launchd-unsupported marker and
+ starts a detached gateway *beside* the supervised one, and the marker makes
+ :func:`wait_for_launchd_gateway_supervision` answer True unconditionally — so no later
+ install/update can tell that nothing ties the gateway to launchd any more. A live supervised PID is
+ direct evidence this macOS does manage the job, so surface the failure instead of branding the host.
+ """
if not _launchctl_domain_unsupported(exc.returncode):
raise exc
+ label = get_launchd_label()
+ if _launchctl_label_supervising_process(label):
+ print(f"⚠ launchctl {what} failed (exit {exc.returncode}), but launchd still supervises {label}")
+ print(" Not switching to the detached fallback — this host manages the job.")
+ print(" Apply the definition with: hermes gateway stop && hermes gateway install --force")
+ raise exc
_launchd_fallback_to_detached(f"{what} exit {exc.returncode}")
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index 9f6564047db..5bc28e7e3e6 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -816,6 +816,58 @@ class TestLaunchdDomainDetection:
assert domain == "user/501"
+class TestLaunchdUnsupportedFallbackPolicy:
+ """A 5/125 launchctl exit must not brand a domain the host is demonstrably managing.
+
+ Regression for the recurrence where ``hermes gateway install --force`` over the LIVE job
+ returned EIO (5) — launchctl's answer for an already-loaded label — which
+ ``_launchd_degrade_or_raise`` read as "this macOS cannot manage launchd services". It wrote the
+ permanent launchd-unsupported marker and started a detached gateway beside the supervised one,
+ and the marker made ``wait_for_launchd_gateway_supervision()`` answer True unconditionally, so
+ no later install/update could see that nothing tied the gateway to launchd any more.
+ """
+
+ def _spy_fallback(self, monkeypatch):
+ """Record the two side effects of degrading; return the lists."""
+ marker_writes, spawned = [], []
+ monkeypatch.setattr(
+ gateway_cli, "_write_launchd_unsupported_marker", lambda: marker_writes.append("marker")
+ )
+ monkeypatch.setattr(
+ gateway_cli, "_spawn_detached_gateway", lambda: spawned.append("detached") or True
+ )
+ return marker_writes, spawned
+
+ def test_eio_on_a_supervised_label_does_not_degrade_to_detached(self, monkeypatch):
+ exc = subprocess.CalledProcessError(
+ 5, ["launchctl", "bootstrap"], stderr="Bootstrap failed: 5: Input/output error"
+ )
+ monkeypatch.setattr(gateway_cli, "get_launchd_label", lambda: "ai.hermes.gateway")
+ monkeypatch.setattr(
+ gateway_cli, "_launchctl_label_supervising_process", lambda label: True
+ )
+ marker_writes, spawned = self._spy_fallback(monkeypatch)
+
+ with pytest.raises(subprocess.CalledProcessError):
+ gateway_cli._launchd_degrade_or_raise(exc, "launchctl bootstrap")
+
+ assert marker_writes == [], "a supervised job's domain must not be branded unsupported"
+ assert spawned == [], "no detached gateway beside a supervised one"
+
+ def test_eio_without_a_supervised_process_still_falls_back(self, monkeypatch):
+ """The detached fallback for a domain that really cannot manage the job is unchanged."""
+ exc = subprocess.CalledProcessError(125, ["launchctl", "kickstart"])
+ monkeypatch.setattr(gateway_cli, "get_launchd_label", lambda: "ai.hermes.gateway")
+ monkeypatch.setattr(
+ gateway_cli, "_launchctl_label_supervising_process", lambda label: False
+ )
+ marker_writes, spawned = self._spy_fallback(monkeypatch)
+
+ gateway_cli._launchd_degrade_or_raise(exc, "launchctl kickstart")
+
+ assert marker_writes == ["marker"]
+ assert spawned == ["detached"]
+
class TestGatewayServiceDetection:
def test_supports_systemd_services_requires_systemctl_binary(self, monkeypatch):
monkeypatch.setattr(gateway_cli, "is_linux", lambda: True)