Skip to content

The agent that grows with you

fix(gateway): surface launchctl bootstrap failures in refresh_launchd_plist_if_needed

ygd585be0c4921cfb
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index ed480d5095a..78604b34e55 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -4033,6 +4033,7 @@ def refresh_launchd_plist_if_needed() -> bool:
"launchd reload of %s failed — service not registered after %ds of retries; see %s",
target, int(_reload_budget), _launchd_reload_log_path(),
)
+ return False
print("↻ Updated gateway launchd service definition to match the current Hermes install")
return True
@@ -4043,8 +4044,17 @@ def launchd_install(force: bool = False):
if plist_path.exists() and not force:
if not launchd_plist_is_current():
print(f"↻ Repairing outdated launchd service at: {plist_path}")
- refresh_launchd_plist_if_needed()
- print("✓ Service definition updated")
+ if refresh_launchd_plist_if_needed():
+ print("✓ Service definition updated")
+ else:
+ # The plist was rewritten but launchd never registered it (or the write was refused):
+ # a success line here would hide an unloaded service with no KeepAlive.
+ from hermes_constants import display_hermes_home
+ print(
+ "⚠ Service definition could not be reloaded with launchd. "
+ "Run 'hermes gateway install --force' or check "
+ f"{display_hermes_home()}/logs/launchd-reload.log for details."
+ )
return
print(f"Service already installed at: {plist_path}")
print("Use --force to reinstall")
diff --git a/tests/hermes_cli/test_launchd_refresh_reporting.py b/tests/hermes_cli/test_launchd_refresh_reporting.py
new file mode 100644
index 00000000000..92a864008b2
--- /dev/null
+++ b/tests/hermes_cli/test_launchd_refresh_reporting.py
@@ -0,0 +1,46 @@
+"""launchd plist refresh must not report success when launchd never registered the service (#12866)."""
+import subprocess
+from unittest.mock import MagicMock
+
+from hermes_cli import gateway as gw
+
+
+def _stale_plist(tmp_path, monkeypatch, *, registered: bool):
+ plist_path = tmp_path / "com.hermes.plist"
+ plist_path.write_text("<old/>", encoding="utf-8")
+ monkeypatch.setattr(gw, "get_launchd_plist_path", lambda: plist_path)
+ monkeypatch.setattr(gw, "launchd_plist_is_current", lambda: False)
+ monkeypatch.setattr(gw, "generate_launchd_plist", lambda: "<new/>")
+ monkeypatch.setattr(gw, "_refuse_temp_home_service_write", lambda *a: False)
+ monkeypatch.setattr(gw, "get_launchd_label", lambda: "com.hermes.agent")
+ monkeypatch.setattr(gw, "_launchd_domain", lambda: "gui/501")
+ monkeypatch.setattr(gw, "_append_launchd_reload_log", lambda msg: None)
+ monkeypatch.setattr("gateway.status.get_running_pid", lambda: None)
+ monkeypatch.setattr(gw, "_retry_launchctl_bootstrap_until_registered", lambda *a, **k: registered)
+ monkeypatch.setattr(subprocess, "run", lambda *a, **k: MagicMock(returncode=0))
+
+
+def test_refresh_reports_registration_outcome(tmp_path, monkeypatch, capsys):
+ _stale_plist(tmp_path, monkeypatch, registered=False)
+ assert gw.refresh_launchd_plist_if_needed() is False
+ assert "Updated" not in capsys.readouterr().out
+
+ _stale_plist(tmp_path, monkeypatch, registered=True)
+ assert gw.refresh_launchd_plist_if_needed() is True
+ assert "Updated" in capsys.readouterr().out
+
+
+def test_install_repair_warns_instead_of_claiming_success(tmp_path, monkeypatch, capsys):
+ plist_path = tmp_path / "com.hermes.plist"
+ plist_path.write_text("<old/>", encoding="utf-8")
+ monkeypatch.setattr(gw, "get_launchd_plist_path", lambda: plist_path)
+ monkeypatch.setattr(gw, "launchd_plist_is_current", lambda: False)
+ monkeypatch.setattr(gw, "refresh_launchd_plist_if_needed", lambda: False)
+ monkeypatch.setattr("hermes_constants.display_hermes_home", lambda: "~/.hermes-work")
+
+ gw.launchd_install(force=False)
+
+ out = capsys.readouterr().out
+ assert "Service definition updated" not in out
+ assert "could not be reloaded" in out
+ assert "~/.hermes-work/logs/launchd-reload.log" in out