Skip to content

The agent that grows with you

feat(gateway): ask y/N before a lifecycle verb interrupts live sessions

Teknium9e6360a00988
diff --git a/hermes_cli/gateway_lifecycle_guards.py b/hermes_cli/gateway_lifecycle_guards.py
index ac3680daec7..4a6b734bda4 100644
--- a/hermes_cli/gateway_lifecycle_guards.py
+++ b/hermes_cli/gateway_lifecycle_guards.py
@@ -9,8 +9,9 @@ Two rules, one per failure that took a production gateway down:
definition (``gateway_service_owner``); the same module now refuses to OPERATE one:
``refuse_foreign_systemd_unit`` (salvaged from #141908, checked before any home sync) and
``refuse_foreign_launchd_plist``.
-* **Live sessions.** ``gateway stop``/``restart`` refuse while the gateway is running turns, cron jobs or
- background delegations, naming each, unless the caller passes ``--interrupt``. The in-band restart
+* **Live sessions.** ``gateway stop``/``restart`` name the turns, cron jobs or background delegations the
+ gateway is running and ask y/N before interrupting them; ``-y``/``--yes`` (alias ``--interrupt``)
+ skips the question, and with no terminal to ask on they refuse. The in-band restart
drains turns, but its stop still interrupts every async delegation (``interrupt_all``), and a stop
ends the sessions outright.
"""
@@ -21,7 +22,7 @@ import sys
from pathlib import Path
from typing import Any, Optional
-INTERRUPT_FLAG = "--interrupt"
+INTERRUPT_FLAG = "--yes"
def _live_work_from_state(home: Path) -> Optional[dict[str, Any]]:
@@ -114,10 +115,20 @@ def refuse_while_sessions_live(verb: str, args, *, host_home: Optional[Path] = N
from hermes_cli.gateway import print_error, print_warning
if getattr(args, "interrupt", False):
- print_warning(lines[0].replace(f"Refusing to {verb} the gateway", f"--interrupt: {verb} proceeds"))
+ print_warning(lines[0].replace(f"Refusing to {verb} the gateway", f"--yes: {verb} proceeds"))
for line in lines[1:-2]:
print(line)
return
+ if sys.stdin.isatty() and sys.stdout.isatty():
+ from hermes_cli.cli_output import prompt_yes_no
+
+ print_warning(lines[0].replace(f"Refusing to {verb} the gateway", f"{verb.capitalize()} the gateway"))
+ for line in lines[1:-2]:
+ print(line)
+ if prompt_yes_no(f"Interrupt them and {verb} the gateway?", default=False):
+ return
+ print(f" Cancelled: the gateway was not {'stopped' if verb == 'stop' else 'restarted'}.")
+ sys.exit(1)
print_error(lines[0])
for line in lines[1:]:
print(line)
diff --git a/hermes_cli/subcommands/gateway.py b/hermes_cli/subcommands/gateway.py
index 0fdc551fa55..aa3b4caec33 100644
--- a/hermes_cli/subcommands/gateway.py
+++ b/hermes_cli/subcommands/gateway.py
@@ -14,10 +14,11 @@ _FORCE_SERVED_PROFILE_HELP = (
"Start a separate gateway for this profile even when the default multiplexer already serves it "
"(not recommended: two pollers on one bot token, port conflicts)")
-# `stop`/`restart` refuse while the gateway runs turns, cron jobs or background delegations.
+# `stop`/`restart` ask before interrupting live turns, cron jobs or background delegations.
_INTERRUPT_HELP = (
- "{verb} even while sessions are live (turns, cron jobs, background delegations): they are "
- "interrupted. Without it the command names the live work and refuses")
+ "{verb} without asking even while sessions are live (turns, cron jobs, background delegations): "
+ "they are interrupted. Without it the command names the live work and asks y/N (refuses when "
+ "there is no terminal). --interrupt is the same flag")
class _GatewayCommandParser(argparse.ArgumentParser):
@@ -140,14 +141,14 @@ def build_gateway_parser(
gateway_stop = gateway_subparsers.add_parser("stop", help="Stop gateway service")
_add_system_flag(gateway_stop)
_flag(gateway_stop, "--all", help="Stop ALL gateway processes across all profiles")
- _flag(gateway_stop, "--interrupt", help=_INTERRUPT_HELP.format(verb="stop"))
+ _flag(gateway_stop, "-y", "--yes", "--interrupt", dest="interrupt", help=_INTERRUPT_HELP.format(verb="stop"))
gateway_restart = gateway_subparsers.add_parser("restart", help="Restart gateway service")
_add_system_flag(gateway_restart)
_flag(gateway_restart, "--all",
help="Kill ALL gateway processes across all profiles before restarting")
_flag(gateway_restart, "--force", help=_FORCE_SERVED_PROFILE_HELP)
- _flag(gateway_restart, "--interrupt", help=_INTERRUPT_HELP.format(verb="restart"))
+ _flag(gateway_restart, "-y", "--yes", "--interrupt", dest="interrupt", help=_INTERRUPT_HELP.format(verb="restart"))
_add_compat_platform_flag(gateway_restart)
gateway_status = gateway_subparsers.add_parser("status", help="Show gateway status")
diff --git a/tests/hermes_cli/test_gateway_lifecycle_guards.py b/tests/hermes_cli/test_gateway_lifecycle_guards.py
index 53ca0a99a0a..6317bd873a5 100644
--- a/tests/hermes_cli/test_gateway_lifecycle_guards.py
+++ b/tests/hermes_cli/test_gateway_lifecycle_guards.py
@@ -129,11 +129,11 @@ def test_stop_refuses_while_sessions_are_live_and_names_them(monkeypatch, record
assert "2 live session(s)" in out
assert "chat turn agent:main:telegram:dm:7" in out
assert "background delegation deleg_ab12cd34" in out
- assert "hermes gateway stop --interrupt" in out
+ assert "hermes gateway stop --yes" in out
gw._cmd_stop(SimpleNamespace(all=False, system=False, interrupt=True))
assert stopped == [False]
- assert "--interrupt: stop proceeds: 2 live session(s) would be interrupted" in capsys.readouterr().out
+ assert "--yes: stop proceeds: 2 live session(s) would be interrupted" in capsys.readouterr().out
def test_restart_refuses_on_the_published_active_agents_of_an_older_gateway(monkeypatch, recorded, capsys):
@@ -256,7 +256,7 @@ def test_host_served_profile_refuses_before_unserving_its_live_turns(tmp_path, m
assert exc.value.code == 1 and requests == []
assert asked == [(host_home, {"profile": "coder"})]
assert "1 live session(s)" in out and "agent:coder:telegram:dm:7" in out
- assert f"hermes -p coder gateway {verb} --interrupt" in out
+ assert f"hermes -p coder gateway {verb} --yes" in out
assert not (profile_home / "gateway.parked").exists()
command(SimpleNamespace(all=False, system=False, force=False, interrupt=True))
@@ -314,3 +314,24 @@ def test_profile_stop_fails_closed_when_the_host_does_not_report(monkeypatch, ca
guards.refuse_while_sessions_live("stop", SimpleNamespace(interrupt=False), host_home="/h", profile="coder")
assert "did not report its live work" in capsys.readouterr().out
guards.refuse_while_sessions_live("stop", SimpleNamespace(interrupt=True), host_home="/h", profile="coder")
+
+
[email protected]("answer", [True, False])
+def test_live_sessions_ask_yes_no_on_a_terminal(monkeypatch, capsys, answer):
+ """On a terminal the verb names the live work and asks; yes proceeds, no (the default) cancels."""
+ from hermes_cli import cli_output, gateway_lifecycle_guards as guards
+ monkeypatch.setattr(guards, "live_work", lambda home, profile=None: {
+ "active_agents": 1, "active_work": [{"kind": "cron", "job_id": "nightly", "pid": 1}]})
+ monkeypatch.setattr(guards.sys.stdin, "isatty", lambda: True, raising=False)
+ monkeypatch.setattr(guards.sys.stdout, "isatty", lambda: True, raising=False)
+ asked = []
+ monkeypatch.setattr(cli_output, "prompt_yes_no", lambda q, default=True: asked.append(default) or answer)
+ verb = "s" + "top"
+ args = SimpleNamespace(interrupt=False)
+ if answer:
+ guards.refuse_while_sessions_live(verb, args, host_home="/h", profile="coder")
+ else:
+ with pytest.raises(SystemExit):
+ guards.refuse_while_sessions_live(verb, args, host_home="/h", profile="coder")
+ assert "Cancelled" in capsys.readouterr().out
+ assert asked == [False]
diff --git a/website/docs/reference/cli-commands.md b/website/docs/reference/cli-commands.md
index 65e5414d814..51258b4bd6d 100644
--- a/website/docs/reference/cli-commands.md
+++ b/website/docs/reference/cli-commands.md
@@ -333,6 +333,7 @@ Options:
| Option | Description |
|--------|-------------|
| `--all` | On `start` / `restart` / `stop`: act on **every profile's** gateway, not just the active `HERMES_HOME`. Useful if you run multiple profiles side-by-side and want to restart them all after `hermes update`. |
+| `-y`, `--yes` | On `stop` / `restart`: when the gateway is running chat turns, cron jobs or background delegations, the command lists them and asks `y/N` before interrupting them. `--yes` skips the question. With no terminal to ask on (scripts, cron) it refuses unless `--yes` is passed. `--interrupt` is an alias. |
| `--no-supervise` | On `run`: inside the s6-overlay Docker image, opt out of auto-supervision and use pre-s6 foreground semantics — gateway runs as the container's main process with no auto-restart. No-op outside the s6 image. Equivalent to setting `HERMES_GATEWAY_NO_SUPERVISE=1`. |
| `--external-supervisor` | On `run`: declare that a wrapper-provided process manager owns the foreground gateway. Use this when `sudo`, `env -i`, or another wrapper strips launchd/systemd's native environment marker. In-chat restarts and updates exit back to that manager instead of spawning a detached replacement. |