The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): let Ink attach to a restart_wait owner on start and recover
Teknium6b083b51d424
diff --git a/hermes_cli/gateway_client.py b/hermes_cli/gateway_client.pyindex 4325dd4a26d..1b38e53e7aa 100644--- a/hermes_cli/gateway_client.py+++ b/hermes_cli/gateway_client.py@@ -187,7 +187,7 @@ async def connect_gateway(endpoint=None):(maintenance that must never start a gateway); default ensures one, starting it if needed."""from websockets.asyncio.client import connectfrom hermes_constants import get_hermes_home- from hermes_cli.gateway_runtime import ensure_gateway_runtime+ from hermes_cli.gateway_runtime import attachable, ensure_gateway_runtimefrom urllib.parse import urlsplitremote = "" if endpoint is not None else os.environ.get("HERMES_TUI_GATEWAY_URL", "").strip()@@ -205,9 +205,7 @@ async def connect_gateway(endpoint=None):# A client's own start: the daemon it may launch ends itself once idle (no client, adapter,# cron/kanban work or admission), and the next client starts a fresh one.result = await asyncio.to_thread(ensure_gateway_runtime, home, idle_exit=True)- # A restart's after-turn wait still owns its turns (#139041): attach to answer a parked prompt.- attachable = result.state == "ready" or (result.state == "draining" and result.reason_code == "restart_wait")- if not attachable or result.endpoint is None:+ if not attachable(result):detail = getattr(result, "detail", None)if result.reason_code == "runtime_exited" and detail:# A multi-line startup report (redacted traceback tail + where the full log is).diff --git a/hermes_cli/gateway_runtime.py b/hermes_cli/gateway_runtime.pyindex d78aacbd0eb..0e56427fecc 100644--- a/hermes_cli/gateway_runtime.py+++ b/hermes_cli/gateway_runtime.py@@ -49,6 +49,17 @@ class GatewayDiscovery:detail: str | None = None+def attachable(result: GatewayDiscovery) -> bool:+ """Whether a client may attach to this discovery/ensure verdict.++ ``ready`` owners, plus a restart's after-turn wait (``draining``/``restart_wait``): that owner+ still owns its turns and must stay reachable to answer a parked approval/clarify (#139041).+ Shared by every client bootstrap (``connect_gateway`` and Ink's ``gateway_bootstrap.py``)."""+ if result.endpoint is None:+ return False+ return result.state == "ready" or (result.state == "draining" and result.reason_code == "restart_wait")++def _canonical_home(home: str | Path) -> str:return os.path.normcase(str(Path(home).expanduser().resolve()))diff --git a/tests/tui_gateway/test_gateway_bootstrap_recover.py b/tests/tui_gateway/test_gateway_bootstrap_recover.pyindex 4eed73c23d6..35d5b7a228d 100644--- a/tests/tui_gateway/test_gateway_bootstrap_recover.py+++ b/tests/tui_gateway/test_gateway_bootstrap_recover.py@@ -121,3 +121,49 @@ def test_started_daemon_does_not_inherit_the_launch_time_kanban_board(monkeypatcwith pytest.raises(RuntimeError):module.bootstrap(True)assert seen == [None]++[email protected]("start,recover", [(True, False), (False, True)])+def test_restart_wait_owner_is_attachable_on_start_and_recover(monkeypatch, tmp_path, start, recover):+ """GW-6: an owner in a restart's after-turn wait (draining/restart_wait) still owns its turns;+ reopening the TUI to answer a parked prompt must attach, not refuse or re-ensure it."""+ import io+ import os+ module = _load_bootstrap()+ home = (tmp_path / "home").resolve()+ home.mkdir()+ endpoint = SimpleNamespace(profile_id=str(home), instance_id="draining-owner", api_origin="http://127.0.0.1:1",+ runtime_protocol=1, control_home=str(home))+ receipt = SimpleNamespace(state="draining", endpoint=endpoint, reason_code="restart_wait", detail=None)+ monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: home)+ monkeypatch.setattr(gateway_runtime, "discover_gateway_endpoint", lambda h, timeout=5: receipt)+ ensured = []+ monkeypatch.setattr(gateway_runtime, "ensure_gateway_runtime",+ lambda h, timeout=30, idle_exit=False: (ensured.append(h), receipt)[1])+ reply = json.dumps({"ok": True, "id": 1, "result": {+ "instance_id": endpoint.instance_id, "profile_id": endpoint.profile_id, "ticket": "grant"}}).encode()+ if os.name == "nt":+ import gateway.runtime_bootstrap_windows as windows_bootstrap+ monkeypatch.setattr(windows_bootstrap, "query_runtime_control", lambda h, req, timeout: reply)+ else:+ import hermes_cli.gateway_runtime_discovery as discovery++ class Peer:+ def __enter__(self):+ return self++ def __exit__(self, *args):+ return False++ def sendall(self, _request):+ pass++ def makefile(self, _mode):+ return io.BytesIO(reply)++ monkeypatch.setattr(discovery, "connect_private", lambda h, timeout: Peer())++ result = module.bootstrap(start, recover=recover)++ assert result["instance_id"] == "draining-owner"+ assert ensured == ([home] if start else [])diff --git a/ui-tui/scripts/gateway_bootstrap.py b/ui-tui/scripts/gateway_bootstrap.pyindex b7936c4eecc..6b04f4e6846 100644--- a/ui-tui/scripts/gateway_bootstrap.py+++ b/ui-tui/scripts/gateway_bootstrap.py@@ -48,7 +48,8 @@ def bootstrap(start: bool, recover: bool = False) -> dict:except (RuntimeError, OSError) as exc: # no committed generation / unreadable install stateraise ImportError(f"dependency environment unavailable: {exc}") from excfrom hermes_constants import get_hermes_home- from hermes_cli.gateway_runtime import control_home_for, discover_gateway_endpoint, ensure_gateway_runtime+ from hermes_cli.gateway_runtime import (attachable, control_home_for, discover_gateway_endpoint,+ ensure_gateway_runtime)from hermes_cli.gateway_runtime_discovery import connect_privatehome = get_hermes_home().resolve()@@ -63,9 +64,11 @@ def bootstrap(start: bool, recover: bool = False) -> dict:os.environ.pop(key, None)receipt = (ensure_gateway_runtime(home, timeout=30, idle_exit=True) if startelse discover_gateway_endpoint(home, timeout=5))- if not start and recover and receipt.state != "ready":+ # A restart's after-turn wait still owns its turns (#139041): attach to answer a parked prompt,+ # on a recovery too, rather than re-ensuring until the restart ends.+ if not start and recover and not attachable(receipt):receipt = ensure_gateway_runtime(home, timeout=30, idle_exit=True)- if receipt.state != "ready" or receipt.endpoint is None:+ if not attachable(receipt):raise RuntimeError(failure_message(receipt))endpoint = receipt.endpoint# A profile served by the default multiplexer has no socket of its own; the host mints its ticket.