The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): inline the user-bus adoption gate at the run_gateway call site
kshitijk4poor16fe50b1a289
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 1e588e600b7..7f6bf765572 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -2064,26 +2064,6 @@ def _ensure_user_systemd_env() -> None:os.environ["DBUS_SESSION_BUS_ADDRESS"] = f"unix:path={bus_path}"-def _adopt_user_bus_when_started_by_systemd() -> None:- """Point a systemd-launched gateway at its own user bus before it boots (#104893).-- A *system*-level unit (``/etc/systemd/system``, ``User=<someone>``) is exec'd with- neither ``XDG_RUNTIME_DIR`` nor ``DBUS_SESSION_BUS_ADDRESS``, and a process environment- is fixed at exec time — so ``systemd-run --user`` fails for the whole lifetime of that- gateway even once ``/run/user/<uid>/bus`` is up. Every restart-safe worker crosses that- seam (``tools.process_registry.restart_safe_gateway_child_argv``) and it fails closed by- design, so cron and Kanban dispatch died at every fire on headless service installs.- ``_ensure_user_systemd_env`` derives both values from our own uid and adopts them only- when the runtime dir is really ours and the socket really exists, so a host with no user- manager keeps its honest "unavailable" verdict instead of a fabricated bus address.- Doing it here rather than at the dispatch seam matters: worker environments are snapshots- of ``os.environ`` taken at different points, so the adoption has to precede all of them.- """- if os.name != "posix" or not os.environ.get("INVOCATION_ID"):- return- _ensure_user_systemd_env()--def _wait_for_user_dbus_socket(timeout: float = 3.0) -> bool:"""Poll up to ``timeout`` s for a user systemd control socket ([email protected] takes a moment after enable-linger)."""deadline = time.monotonic() + timeout@@ -4550,7 +4530,10 @@ def run_gateway(verbose: int = 0, quiet: bool = False, replace: bool = False, foif _absorb:_absorb_windows_console_controls()- _adopt_user_bus_when_started_by_systemd()+ # A system-level unit execs us without XDG_RUNTIME_DIR/DBUS_SESSION_BUS_ADDRESS; adopt our own+ # user bus before any worker env snapshot so `systemd-run --user --scope` works (#104893).+ if is_linux() and os.environ.get("INVOCATION_ID"):+ _ensure_user_systemd_env()# Refresh the systemd unit on every boot so restart settings stay current even after an# exit-code-75 respawn (stale-code or /restart), which bypasses `hermes gateway restart`.diff --git a/tests/hermes_cli/test_gateway_user_bus_adoption.py b/tests/hermes_cli/test_gateway_user_bus_adoption.pyindex e502705f37a..a8737241bac 100644--- a/tests/hermes_cli/test_gateway_user_bus_adoption.py+++ b/tests/hermes_cli/test_gateway_user_bus_adoption.py@@ -1,11 +1,4 @@-"""#104893: a systemd-launched gateway must adopt its own user bus at boot.--A *system*-level unit (``/etc/systemd/system``, ``User=<someone>``) is exec'd with-neither ``XDG_RUNTIME_DIR`` nor ``DBUS_SESSION_BUS_ADDRESS``, and a process environment-is fixed at exec time. ``systemd-run --user`` is the seam every restart-safe cron and-Kanban worker crosses, and it fails closed by design — so on a headless service install-every agent-driven scheduled job died at dispatch, even once the user manager was up.-"""+"""#104893: a gateway started by a system-level systemd unit adopts its own user bus at boot."""import os@@ -77,7 +70,7 @@ def test_absent_user_bus_is_never_fabricated(monkeypatch):_fake_user_bus(monkeypatch, present=False)_service_manager_env(monkeypatch)- gw._adopt_user_bus_when_started_by_systemd()+ gw._ensure_user_systemd_env()assert "XDG_RUNTIME_DIR" not in os.environassert "DBUS_SESSION_BUS_ADDRESS" not in os.environdiff --git a/tools/process_registry.py b/tools/process_registry.pyindex b51b0404c8b..362afc3acd5 100644--- a/tools/process_registry.py+++ b/tools/process_registry.py@@ -235,15 +235,12 @@ def restart_safe_gateway_child_argv(if not _is_supervised_gateway_process() or not os.environ.get("INVOCATION_ID"):return commandif not _systemd_run_user_scope_available():- # This text is what the operator actually sees: it is stored as the cron- # execution's error and printed on the job row, so it names the remedy- # rather than only the symptom.+ # Stored as the cron execution's error and shown on the job row: name the remedy.raise RuntimeError("cannot create restart-safe systemd scope for gateway child: "- "systemd-run --user --scope is unavailable — this gateway has no reachable "- f"user D-Bus session (expected /run/user/{os.getuid()}/bus). On a system-level " # windows-footgun: ok — behind the _IS_LINUX return above- "service install, run `sudo loginctl enable-linger <gateway-user>` and restart "- "the gateway."+ "systemd-run --user --scope is unavailable (usually no reachable user D-Bus session at "+ f"/run/user/{os.getuid()}/bus). On a system-level service install, run " # windows-footgun: ok — behind the _IS_LINUX return above+ "`sudo loginctl enable-linger <gateway-user>` and restart the gateway.")scoped = _build_systemd_scope_argv(command, unit_suffix=unit_suffix)if scoped == command: