Repository navigation
Description
Summary
Since 5cd6c17 (2026-05-23, #30719), hermes gateway restart refuses to run when _HERMES_GATEWAY=1 is in the environment (hermes_cli/gateway.py, restart subcommand). The gateway sets that marker process-wide at import (gateway/run.py:835 — os.environ["_HERMES_GATEWAY"] = "1"), and _launch_detached_restart_command() (gateway/run.py ~3981) spawns its successor helper via subprocess.Popen without scrubbing the env. The helper therefore inherits the marker, its hermes gateway restart exits 1 with "Refusing to restart the gateway from inside the gateway process", and the refusal is invisible (stdout/stderr → DEVNULL).
Net effect: on any platform where /restart takes the detached path — i.e. anywhere INVOCATION_ID (systemd) is unset and no container is detected — the in-chat /restart tears the gateway down, exits 0, and nothing respawns it.
Observed live (macOS / launchd)
macOS 2026-06-08, launchd agent generated by an older Hermes (KeepAlive {SuccessfulExit: false}):
/restartfrom Telegram →gateway.exit_cleanin gateway-exit-diag.log → no successorgateway.startever appears → gateway fully dark (no poller, no cron) until a manuallaunchctl kickstart -k.- The helper's refusal never surfaces anywhere (DEVNULL).
Note: the #30719 defense exists to stop agent-initiated restart loops, but it also kills the gateway's own restart mechanism — the helper is guaranteed to run only after the old process has exited (its while kill -0 $PID wait loop), so it is not the in-process case the defense targets.
Suggested fixes
- Scrub
_HERMES_GATEWAYfrom the detached helper's child env in_launch_detached_restart_command()(all three spawn branches: setsid/bash/Windows watcher). The helper runs strictly post-exit, so the anti-loop defense shouldn't apply to it. - Optionally: detect launchd as a service manager in the /restart handler (the comment there already says "systemd/launchd" but only
INVOCATION_IDis checked).XPC_SERVICE_NAMEis set by launchd (verified:XPC_SERVICE_NAME=ai.hermes.gatewayin the agent's env) — routing macOS through thevia_servicepath would use the exit-75 branch added in 5987b24 and avoid the fragile detached helper entirely.
Mitigation that works today
Current generate_launchd_plist() emits unconditional KeepAlive: true, which revives the clean exit — installs that refresh their plist (hermes gateway install) recover from /restart fine. But users who recover with raw launchctl kickstart never trigger plist regeneration, so older SuccessfulExit:false plists persist indefinitely — and the detached helper is silently dead code for everyone not under systemd/container either way.
Environment
- Hermes
68a997fed(also present at current HEAD by code inspection) - macOS (darwin), Python 3.11.14, launchd LaunchAgent supervision
Activity
liuhao1024 commentedon Jun 11, 2026
This has already been fixed on
mainby commitcb2c13055(fix(gateway): scrub _HERMES_GATEWAY from POSIX detached restart watcher too). All three spawn paths (Windows watcher, setsid, bash) now create awatcher_envcopy with_HERMES_GATEWAYremoved before passing it tosubprocess.Popen.lifuyue commentedon Jun 11, 2026
I checked current
main, and this specific_HERMES_GATEWAYinheritance regression appears fixed by #43969.gateway/run.pynow scrubs_HERMES_GATEWAYfrom the detached restart watcher environment on both paths:setsid/bash -lcwatcher envI also verified the related regression coverage locally:
Result: 46 passed, 0 failed.
The remaining macOS launchd service-manager routing concern looks related but separate; it appears to be tracked by #29180 and open PRs such as #43888.
MuBeiGe commentedon Jun 13, 2026
Additional blocker on Windows:
is_installed()=False→prompt_yes_no→EOFErrorConfirmed the Layer 1 root cause (
_HERMES_GATEWAYenv inheritance) on Windows 0.16.0. After stripping_HERMES_GATEWAYfrom the watcher env, there's a second blocking layer specific to Windows installs that haven't runhermes gateway install:_launch_detached_restart_command()watcher runshermes gateway restartgateway_windows.start()callsis_installed()→ returnsFalse(Scheduled TaskHermes_Gatewaynever created)prompt_yes_no("Install it now?", True)(gateway_windows.py:1193)input()raisesEOFError→sys.exit(1)(setup.py:286)So even with the env scrub fix from #40141, Windows users without a prior
hermes gateway installare still blocked — the watcher helper hitsprompt_yes_nowith no TTY and dies.Alternative fix: route watcher through
gateway run --replaceinstead ofgateway restartThe
runsubcommand handler (hermes_cli/gateway.py:6201-6208) has no_HERMES_GATEWAYdefense check and bypassesgateway_windows.start()entirely — sidestepping both Layer 1 and Layer 2 in a single-line change per platform:gateway/run.py:4115(Windows watcher Popen target):restart→run --replacegateway/run.py:4174(POSIX watcher shell_cmd):restart→run --replace--replacehandles residual process cleanup. The watcher runs strictly post-exit (after thewhile kill -0 $PIDloop), so there's no risk of duplicate instances.Verified this approach on Windows 10 with Hermes 0.16.0 —
is_installed()=Falseconfirmed as the second failure point, and therun --replacepath avoids both the defense check and theprompt_yes_noblocking.teknium1 commentedon Jul 14, 2026
Thanks for the precise report and the follow-up discussion. This is an automated hermes-sweeper review; the reported detached-helper inheritance regression is already fixed on current
main.cb2c13055ed9c3f467be61dbef1f3a7a5dcdc5f6removes_HERMES_GATEWAYfrom the POSIX detached watcher environment ingateway/run.py:6293-6315.gateway/run.py:6251-6284, with regression coverage intests/gateway/test_restart_drain.py:317-353.abc3662bf6076045e4d4dc1e14a74cb35d69b86e:gateway/slash_commands.py:1289-1309detectsXPC_SERVICE_NAMEand uses the service restart path.Closing as implemented on main.