Skip to content

The agent that grows with you

fix: gateway lifecycle guards gate on process ownership, not inherited env

nbxuhk0e038425db3d
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 64417c323f0..faea1430837 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -8045,7 +8045,13 @@ def _gateway_command_inner(args):
elif subcmd == "stop":
# Defense: refuse self-targeting gateway stop from inside the gateway.
# Prevents agent-initiated kill loops when combined with supervisor KeepAlive.
- if os.getenv("_HERMES_GATEWAY") == "1":
+ # The supervised probe also PASSES a plain foreground `hermes gateway run`
+ # (env set, PID owned, but no supervisor): that is intentional and
+ # harmless — with no supervisor there is no KeepAlive, so a self-stop is
+ # a one-shot exit rather than a respawn loop.
+ from tools.process_registry import _is_supervised_gateway_process
+
+ if _is_supervised_gateway_process():
print_error(
"Refusing to stop the gateway from inside the gateway process.\n"
"This command was blocked to prevent restart loops.\n"
@@ -8138,7 +8144,13 @@ def _gateway_command_inner(args):
elif subcmd == "restart":
# Defense: refuse self-targeting gateway restart from inside the gateway.
# Prevents agent-initiated kill loops when combined with supervisor KeepAlive.
- if os.getenv("_HERMES_GATEWAY") == "1":
+ # The supervised probe also PASSES a plain foreground `hermes gateway run`
+ # (env set, PID owned, but no supervisor): that is intentional and
+ # harmless — with no supervisor there is no KeepAlive, so a self-restart
+ # is a single relaunch rather than a respawn loop.
+ from tools.process_registry import _is_supervised_gateway_process
+
+ if _is_supervised_gateway_process():
print_error(
"Refusing to restart the gateway from inside the gateway process.\n"
"This command was blocked to prevent restart loops.\n"
diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py
index b0ec8ff39e6..32ae32259b1 100644
--- a/tools/terminal_tool.py
+++ b/tools/terminal_tool.py
@@ -2876,7 +2876,17 @@ def terminal_tool(
# never restart. This mirrors the `hermes gateway restart` guard in
# hermes_cli/gateway.py and the cron-path guard in hermes_cli/cron.py,
# but applies unconditionally (force=True cannot help here).
- if os.environ.get("_HERMES_GATEWAY") == "1":
+ # Gate on the SUPERVISED-gateway probe, not the raw _HERMES_GATEWAY
+ # marker: gateway.run sets it at import time, so it leaks into every
+ # process that merely imports gateway.run (hermes serve --isolated,
+ # CLI, web server) which are NOT the gateway and must be able to
+ # restart it. A plain foreground `hermes gateway run` (env set, PID
+ # owned, no supervisor) now also PASSES this guard: intentional and
+ # harmless, since without a supervisor there is no KeepAlive to turn a
+ # self-restart into a respawn loop.
+ from tools.process_registry import _is_supervised_gateway_process
+
+ if _is_supervised_gateway_process():
from cron.lifecycle_guard import (
_MAX_REFERENCED_SCRIPT_BYTES,
contains_gateway_lifecycle_command_or_referenced_script,