The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): let a browser tab answer the prompt a restart waits on
dokterdok90b36bf89e21
diff --git a/gateway/run_api.py b/gateway/run_api.pyindex 390723b404a..3742ee6a720 100644--- a/gateway/run_api.py+++ b/gateway/run_api.py@@ -337,6 +337,17 @@ def _restart_wait_read(runner, scope) -> bool:return scope['type'] == 'http' and restart_wait_read(runner, scope.get('method'), scope.get('path'))+def _restart_wait_socket_ticket(runner, scope) -> bool:+ """Allow authenticated gateway-ticket minting during restart wait."""+ from urllib.parse import parse_qs+ from gateway.drain_control import restart_wait_open+ from hermes_cli.web_session_sockets import GATEWAY_SOCKET+ return (scope['type'] == 'http' and scope.get('method') == 'POST'+ and scope.get('path') == '/api/webapp/socket-ticket'+ and parse_qs(scope.get('query_string', b'').decode('latin-1')).get('socket') == [GATEWAY_SOCKET]+ and restart_wait_open(runner))++class GatewayRuntimeAPI:"""Redeem private local tickets at the existing WS subprotocol boundary.@@ -381,7 +392,8 @@ class GatewayRuntimeAPI:and (self.runner._draining or descriptor['state'] == 'draining')and scope['type'] == 'websocket' and scope['path'] == '/api/ws')if ((descriptor['state'] != 'ready' or self.runner._draining) and not draining_socket- and not _restart_wait_read(self.runner, scope)):+ and not _restart_wait_read(self.runner, scope)+ and not _restart_wait_socket_ticket(self.runner, scope)):if scope['type'] == 'websocket':await send({'type': 'websocket.close', 'code': 1013})else:@@ -421,8 +433,14 @@ class GatewayRuntimeAPI:ticket, reason = _gateway_ws_ticket_from_subprotocol(ws)if reason == 'none' or ws.headers.get('origin'):if draining_socket:- await ws.close(code=1013)- return+ # Let a Webapp tab's ticket reach its normal upgrade gate during restart wait: only the+ # Webapp's gate redeems it there (elsewhere the loopback gate takes ``?token=``).+ admitted_in_wait = (reason == 'ok' and 'internal' not in ws.query_params+ and getattr(state, 'ui_surface', None) == 'webapp'+ and restart_wait_open(self.runner))+ if not admitted_in_wait:+ await ws.close(code=1013)+ returnreturn await self.app(scope, receive, send)if (reason != 'ok' or not _embedded_chat_enabled()or ws_host_origin_reason(ws, state) is not None or ws_client_reason(ws, state) is not Nonediff --git a/tests/gateway/test_restart_wait_prompt_reach.py b/tests/gateway/test_restart_wait_prompt_reach.pyindex 1dd45f0a53d..5315cbb2126 100644--- a/tests/gateway/test_restart_wait_prompt_reach.py+++ b/tests/gateway/test_restart_wait_prompt_reach.py@@ -304,3 +304,132 @@ async def test_restart_wait_cuts_sockets_opened_before_it(tmp_path, monkeypatch,assert received == [("text", frame), ("disconnect", 1000)], receivedelse:assert received == [("disconnect", 1013)], received++[email protected]("opened_in_wait", [False, True])+async def test_restart_wait_lets_a_webapp_tab_dial_back_in_to_answer_the_parked_prompt(+ tmp_path, monkeypatch, opened_in_wait):+ """Browser reconnects can answer during restart wait, through the real socket and session+ authority, which still refuses every verb outside the drain's allowlist; a socket opened before+ the wait, other sockets' tickets and stopping remain refused."""+ import json++ import httpx++ from gateway.run_api import GatewayRuntimeAPI+ from gateway.session_authority import LiveSession, SessionAuthority+ from tests.hermes_cli.test_webapp_socket_tickets import _SESSION, serve_webapp+ from tests.waiting import async_wait_until+ from tui_gateway import server as tui_server++ web = serve_webapp(tmp_path, monkeypatch).server+ runner = SimpleNamespace(_draining=False, _running=True, session_runtime_descriptor={"state": "ready"},+ session_authority=None)+ db = SessionDB(tmp_path / "state.db")+ db.create_session("s", source="test")+ authority = SessionAuthority(runner, profile_id=str(tmp_path / ".hermes"), instance_id="owner", db=db,+ epoch=begin_runtime_epoch(db, instance_id="owner"))+ authority.sessions["s"] = LiveSession(None, "route")+ monkeypatch.setattr(web.app.state, "session_authority", authority, raising=False)+ monkeypatch.setattr(tui_server, "_WS_ORPHAN_REAP_GRACE_S", 0)++ def open_restart_wait():+ runner._draining = True+ runner.session_runtime_descriptor.update(state="draining", drain_reason="restart_wait")++ if opened_in_wait:+ open_restart_wait()+ inbox, sent, replies = asyncio.Queue(), [], {}++ async def send(message):+ sent.append(message["type"])+ for line in (message.get("text") or "").splitlines():+ frame = json.loads(line)+ replies[frame.get("id")] = frame++ def ask(rid, method, **params):+ inbox.put_nowait({"type": "websocket.receive", "text": json.dumps(+ {"jsonrpc": "2.0", "id": rid, "method": method, "params": {"session_id": "s", **params}})})++ api = GatewayRuntimeAPI(web.app, runner, web.app)+ socket = None+ try:+ async with httpx.AsyncClient(transport=httpx.ASGITransport(app=api), base_url="http://localhost") as client:+ async def mint(socket):+ return await client.post("/api/webapp/socket-ticket", params={"socket": socket}, headers=_SESSION)++ if opened_in_wait:+ assert (await mint("host-terminal")).status_code == 503+ minted = await mint("gateway")+ assert minted.status_code == 200, minted.text+ protocols = ["hermes-gateway-v1", f"hermes-gateway-ticket.{minted.json()['ticket']}"]+ scope = {"type": "websocket", "path": "/api/ws", "scheme": "ws", "query_string": b"native_dial=browser",+ "client": ("127.0.0.1", 1234), "server": ("localhost", 80),+ "headers": [(b"host", b"localhost"), (b"origin", b"http://localhost"),+ (b"sec-websocket-protocol", ", ".join(protocols).encode())],+ "subprotocols": protocols}+ inbox.put_nowait({"type": "websocket.connect"})+ socket = asyncio.ensure_future(api(scope, inbox.get, send))+ await async_wait_until(lambda: "websocket.accept" in sent or socket.done(), what="the upgrade answered")+ assert sent[0] == "websocket.accept", sent+ if opened_in_wait:+ ask(1, "session.resume")+ await async_wait_until(lambda: 1 in replies, what="the resume")+ assert "result" in replies[1], replies[1]+ ask(2, "config.set", key="model", value="x")+ ask(3, "approval.respond", execution_generation=1, prompt_id="gone", choice="once")+ await async_wait_until(lambda: {2, 3} <= replies.keys(), what="both replies")+ assert replies[2]["error"]["message"] == "runtime_draining", replies[2]+ # Reached the authority: a stale prompt is its own verdict, never the drain's refusal.+ assert replies[3]["error"]["message"] == "stale_generation", replies[3]+ del runner.session_runtime_descriptor["drain_reason"] # stop() began+ else:+ open_restart_wait() # the wait opens while this socket is live+ # The next frame is cut (1013) before it is read: the socket ends unanswered.+ ask(4, "session.list")+ await asyncio.wait_for(socket, 10)+ assert 4 not in replies, replies[4]+ runner.session_runtime_descriptor.pop("drain_reason", None) # stop() began+ assert (await mint("gateway")).status_code == 503+ finally:+ if socket is not None and not socket.done():+ socket.cancel()+ await asyncio.gather(socket, return_exceptions=True)+ db.close()+++async def test_restart_wait_admits_a_browser_ticket_only_where_its_gate_redeems_it(tmp_path, monkeypatch):+ """Off the Webapp the loopback dashboard gate takes the legacy ``?token=`` and never redeems a+ ticket, so a ticket-shaped subprotocol beside it is still closed 1013 during the wait."""+ from gateway.run_api import GatewayRuntimeAPI+ from tests.hermes_cli.test_webapp_socket_tickets import _TOKEN, serve_webapp++ web = serve_webapp(tmp_path, monkeypatch).server+ monkeypatch.delattr(web.app.state, "ui_surface") # the gateway's own listener, as on main+ runner = SimpleNamespace(_draining=True, _running=True, session_authority=None,+ session_runtime_descriptor={"state": "draining", "drain_reason": "restart_wait"})+ reached = []++ async def handle(ws, **kwargs):+ reached.append(kwargs)++ monkeypatch.setattr("tui_gateway.ws.handle_ws", handle)+ sent = []++ async def receive():+ return {"type": "websocket.connect"}++ async def send(message):+ sent.append(message)++ protocols = ["hermes-gateway-v1", "hermes-gateway-ticket.not-a-ticket"]+ scope = {"type": "websocket", "path": "/api/ws", "scheme": "ws", "query_string": f"token={_TOKEN}".encode(),+ "client": ("127.0.0.1", 1234), "server": ("localhost", 80),+ "headers": [(b"host", b"localhost"), (b"origin", b"http://localhost"),+ (b"sec-websocket-protocol", ", ".join(protocols).encode())],+ "subprotocols": protocols}+ await GatewayRuntimeAPI(web.app, runner, web.app)(scope, receive, send)+ assert [(message["type"], message.get("code")) for message in sent] == [("websocket.close", 1013)], sent+ assert reached == []