Skip to content

The agent that grows with you

fix(tui): Ink reconnect re-ensures an owner that idle-exited, never an operator-stopped one

Tekniuma82d27c593d8
diff --git a/gateway/run_idle_exit.py b/gateway/run_idle_exit.py
index e07fc1d9f93..0bec7e04134 100644
--- a/gateway/run_idle_exit.py
+++ b/gateway/run_idle_exit.py
@@ -31,6 +31,9 @@ logger = logging.getLogger("gateway.run")
DEFAULT_IDLE_EXIT_SECONDS = 600.0
IDLE_EXIT_REASON = "idle_exit"
+# Head of the ``exit_reason`` persisted with ``stopped``: a reconnecting client's ``--recover``
+# (ui-tui/scripts/gateway_bootstrap.py) tells this self-ended exit from an operator stop by it.
+IDLE_EXIT_STOP_PREFIX = "auto-started gateway idle"
# Kanban statuses the embedded dispatcher acts on without a human (promotion, spawn, reclaim).
_KANBAN_DUE_STATUSES = ("scheduled", "ready", "running", "review")
@@ -176,7 +179,7 @@ def _begin_idle_exit(runner, window: float) -> None:
"""Withdraw admission synchronously (no await between the last idle check and here), then stop."""
runner._draining = True
runner.session_runtime_descriptor.update(state="draining", capabilities=[], drain_reason=IDLE_EXIT_REASON)
- runner._exit_reason = f"auto-started gateway idle for {window:.0f}s (gateway.unmanaged_idle_exit_seconds)"
+ runner._exit_reason = f"{IDLE_EXIT_STOP_PREFIX} for {window:.0f}s (gateway.unmanaged_idle_exit_seconds)"
logger.info("Exiting: %s; the next client starts a fresh gateway", runner._exit_reason)
runner._idle_exit_stop_task = asyncio.ensure_future(runner.stop())
diff --git a/tests/tui_gateway/test_gateway_bootstrap_recover.py b/tests/tui_gateway/test_gateway_bootstrap_recover.py
index cf64ee59105..3a7ff31f149 100644
--- a/tests/tui_gateway/test_gateway_bootstrap_recover.py
+++ b/tests/tui_gateway/test_gateway_bootstrap_recover.py
@@ -28,6 +28,11 @@ def _load_bootstrap():
({"gateway_state": "running", "pid": 999999}, True), # SIGKILL / OOM: dead process still claims live
({"gateway_state": "stopped", "pid": 999999}, False), # `hermes gateway stop`
({"gateway_state": "running", "desired_state": "stopped"}, False), # durable operator stop intent
+ # An auto-started owner ended itself (idle exit) while the TUI was suspended: the next client
+ # starts a fresh one. Its reason, not the bare `stopped`, tells it from an operator stop.
+ ({"gateway_state": "stopped", "exit_reason": "auto-started gateway idle for 600s "
+ "(gateway.unmanaged_idle_exit_seconds)"}, True),
+ ({"gateway_state": "stopped", "exit_reason": "Gateway restart requested"}, False),
])
def test_recover_reensures_only_a_crashed_owner(monkeypatch, tmp_path, record, ensured):
module = _load_bootstrap()
diff --git a/ui-tui/scripts/gateway_bootstrap.py b/ui-tui/scripts/gateway_bootstrap.py
index 2628e6e6cd7..1fb6a7ebaba 100644
--- a/ui-tui/scripts/gateway_bootstrap.py
+++ b/ui-tui/scripts/gateway_bootstrap.py
@@ -1,9 +1,11 @@
"""Ink's private pipe bootstrap; the child is a client, never an agent owner.
``--start`` ensures the owner (first launch). A reconnect discovers only, except ``--recover``
-re-ensures an owner that CRASHED: discovery finds none while its retained runtime record still
-claims it live and the operator recorded no stop intent. An explicitly stopped gateway (its
-record says ``stopped``) is never resurrected by a reconnect.
+re-ensures an owner that is gone without anyone asking: it CRASHED (its retained runtime record
+still claims it live) or it ended ITSELF by the auto-started idle exit (``gateway/run_idle_exit.py``,
+"the next client starts a fresh gateway" — this reconnecting client is that next client). An
+explicitly stopped gateway (``hermes gateway stop`` / a planned stop: ``stopped`` without the idle
+exit's reason, or a durable ``desired_state: stopped``) is never resurrected by a reconnect.
"""
import contextlib
import json
@@ -19,13 +21,20 @@ _LIVE_CLAIMS = frozenset({"running", "starting", "draining", "degraded"})
def owner_crashed(home: Path) -> bool:
- """After discovery answered ``absent``: did the owner serving *home* die without a clean stop?"""
+ """After discovery answered ``absent``: did the owner serving *home* end without anyone asking
+ (a crash, or its own idle exit while this client was away — suspended, asleep, disconnected)?"""
+ from gateway.run_idle_exit import IDLE_EXIT_STOP_PREFIX
from gateway.status import read_runtime_status
from hermes_cli.gateway_runtime_multiplex import implied_host_root, multiplexer_serves_home
owner = multiplexer_serves_home(home) or implied_host_root(home) or home
record = read_runtime_status(owner / "gateway_state.json")
- return (isinstance(record, dict) and record.get("desired_state") != "stopped"
- and record.get("gateway_state") in _LIVE_CLAIMS)
+ if not isinstance(record, dict) or record.get("desired_state") == "stopped":
+ return False
+ # The owner's own idle exit (gateway/run_idle_exit.py) persists ``stopped`` with this reason;
+ # no operator asked for it, and "the next client starts a fresh gateway" is this client.
+ idle_exited = (record.get("gateway_state") == "stopped"
+ and str(record.get("exit_reason") or "").startswith(IDLE_EXIT_STOP_PREFIX))
+ return idle_exited or record.get("gateway_state") in _LIVE_CLAIMS
def bootstrap(start: bool, recover: bool = False) -> dict: