The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): hermes webapp --sign-out reports what the gateway actually did
emozilla28bb6b71bf04
diff --git a/gateway/webapp_listener.py b/gateway/webapp_listener.pyindex 32e850424d0..ffb9cb6a100 100644--- a/gateway/webapp_listener.py+++ b/gateway/webapp_listener.py@@ -301,6 +301,11 @@ def register_control_verbs(runner, home) -> None:def webapp_sign_out(_params, _peer):from hermes_cli import web_server_file_tickets, web_session_sockets+ if runner.webapp_status["state"] != "serving":+ # No browser reaches this gateway's Webapp and it holds no browser credential: replacing+ # the stored one is the whole sign-out, and the next start serves the new one.+ browser_session_token(home, rotate=True)+ return {"signed_out": True, "serving": False, "sign_in_gate": False, "connections_closed": 0}_, web = serving()web._SESSION_TOKEN = browser_session_token(home, rotate=True)# Every tab loses access at once: the sockets and media links the old credential opened end@@ -309,7 +314,8 @@ def register_control_verbs(runner, home) -> None:# close too. Native clients never held it and stay connected.closed = web_session_sockets.close_all()web_server_file_tickets.revoke_all()- return {"signed_out": True, "sign_in_gate": bool(getattr(web.app.state, "auth_required", False)),+ return {"signed_out": True, "serving": True,+ "sign_in_gate": bool(getattr(web.app.state, "auth_required", False)),"connections_closed": closed}server.private_handlers.update({diff --git a/hermes_cli/webapp_launcher.py b/hermes_cli/webapp_launcher.pyindex 7bbd91ea66c..ee4696ad818 100644--- a/hermes_cli/webapp_launcher.py+++ b/hermes_cli/webapp_launcher.py@@ -120,17 +120,38 @@ def _status(target: Path) -> int:return 0+def _signed_out_summary(result: dict) -> str:+ """What a gateway's ``webapp-sign-out`` answer says it did, for the user."""+ if not result.get("serving", True):+ return ("The gateway is not serving the Webapp, so no browser is connected to it. Replaced its stored "+ "browser credential: every browser must sign in again once it serves the Webapp.")+ if "connections_closed" not in result:+ # A gateway started before this Hermes: it replaced the credential and closed the sockets that+ # credential opened, but logins behind the sign-in gate outlive it.+ return (f"Replaced the Webapp's browser credential. Restart the gateway ({RESTART_COMMAND}) to sign "+ "out browsers that signed in with a password or an account too.")+ closed = result["connections_closed"]+ connections = f"closed {closed} browser connection{'' if closed == 1 else 's'}"+ if result.get("sign_in_gate"):+ return f"Signed out every browser: {connections}. Browsers behind sign-in must sign in again."+ return f"Signed out of every Webapp tab: {connections}. Run hermes webapp to open it again."++def _sign_out(target: Path) -> int:from gateway.webapp_listener import browser_session_tokentry:response = _control(target, "webapp-sign-out")except GatewayUnreachable:- response = {"ok": False}- if not response.get("ok"):- # No gateway is serving the Webapp right now: replace the stored credential so no tab can- # use the old one when it does.+ # No gateway is running: replace the stored credential so no browser can use the old one+ # when one starts. Behind the sign-in gate, logins bound to it end too.browser_session_token(target, rotate=True)- print("Signed out of every Webapp tab.")+ print("No gateway is running for this profile. Replaced the stored Webapp credential: every browser "+ "must sign in again when the gateway next serves the Webapp.")+ return 0+ if not response.get("ok"):+ print(f"Nobody was signed out: {_refusal(response)}")+ return 1+ print(_signed_out_summary(response.get("result") or {}))return 0diff --git a/tests/gateway/test_webapp_browser_credential.py b/tests/gateway/test_webapp_browser_credential.pyindex e652a40e7a3..3208e912747 100644--- a/tests/gateway/test_webapp_browser_credential.py+++ b/tests/gateway/test_webapp_browser_credential.py@@ -4,7 +4,9 @@ The gateway keeps the browser credential in ``webapp/session-token`` inside itssurvive a restart. On POSIX the folder is user-only (0700) and the file owner-read/write (0600). Afolder found open to other users is closed again before the credential is used, and a credentialfile other users could read is treated as exposed: it is replaced, which signs the open tabs out,-rather than reused. Windows homes keep their own ACL, so this applies to POSIX only.+rather than reused. Windows homes keep their own ACL, so this applies to POSIX only. A gateway that+is not serving the Webapp holds no browser credential in memory: signing out of it replaces the+stored one, and its answer says that nothing else was closed."""import osimport stat@@ -58,3 +60,26 @@ def test_signing_out_closes_an_open_credential_folder_too(tmp_path):def test_a_private_credential_is_reused(tmp_path):first = webapp.browser_session_token(tmp_path)assert webapp.browser_session_token(tmp_path) == first++[email protected]("status", [+ {"state": "off", "url": None, "reason": None},+ {"state": "unavailable", "url": None, "reason": "could not listen on 127.0.0.1:9120 (in use)"},+])+def test_signing_out_of_a_gateway_not_serving_the_webapp_replaces_the_stored_credential(tmp_path, status):+ """No browser reaches that gateway's Webapp, so replacing the stored credential is the whole+ sign-out; it says so instead of claiming it closed anything."""+ import json+ from types import SimpleNamespace++ from gateway.control_socket import GatewayControlServer++ before = webapp.browser_session_token(tmp_path)+ runner = SimpleNamespace(session_control_server=GatewayControlServer(tmp_path), webapp_status=status,+ session_api=SimpleNamespace(app=SimpleNamespace(web=None)))+ webapp.register_control_verbs(runner, tmp_path)+ raw = json.dumps({"protocol": 1, "verb": "webapp-sign-out", "id": 1, "params": {}}).encode()+ answer = json.loads(runner.session_control_server.handle_request_line(raw, "same-user"))+ assert answer["ok"], answer+ assert answer["result"] == {"signed_out": True, "serving": False, "sign_in_gate": False, "connections_closed": 0}+ assert webapp.browser_session_token(tmp_path) != beforediff --git a/tests/hermes_cli/test_webapp_launcher.py b/tests/hermes_cli/test_webapp_launcher.pyindex 78f44cc1da4..5855cda7e4f 100644--- a/tests/hermes_cli/test_webapp_launcher.py+++ b/tests/hermes_cli/test_webapp_launcher.py@@ -180,10 +180,47 @@ def test_status_with_no_gateway_running_and_the_webapp_off_says_how_to_turn_it_odef test_sign_out_replaces_the_browser_credential_on_the_running_gateway(world, capsys):+ world["answers"]["webapp-sign-out"]["result"] = {+ "signed_out": True, "serving": True, "sign_in_gate": False, "connections_closed": 3}assert launcher.cmd_webapp(_args(sign_out=True)) == 0assert world["calls"]["control"] == [(world["root"], "webapp-sign-out")]assert world["calls"]["ensure"] == []- assert "Signed out" in capsys.readouterr().out+ out = capsys.readouterr().out+ assert "Signed out of every Webapp tab" in out and "closed 3 browser connections" in out+++def test_sign_out_behind_the_sign_in_gate_says_browsers_must_sign_in_again(world, capsys):+ world["answers"]["webapp-sign-out"]["result"] = {+ "signed_out": True, "serving": True, "sign_in_gate": True, "connections_closed": 1}+ assert launcher.cmd_webapp(_args(sign_out=True)) == 0+ out = capsys.readouterr().out+ assert "closed 1 browser connection." in out and "must sign in again" in out+++def test_sign_out_on_a_gateway_not_serving_the_webapp_says_no_browser_was_connected(world, capsys):+ world["answers"]["webapp-sign-out"]["result"] = {+ "signed_out": True, "serving": False, "sign_in_gate": False, "connections_closed": 0}+ assert launcher.cmd_webapp(_args(sign_out=True)) == 0+ out = capsys.readouterr().out+ assert "not serving the Webapp" in out and "Signed out of every" not in out+++def test_sign_out_on_a_gateway_that_does_not_report_what_it_closed_asks_for_a_restart(world, capsys):+ # A gateway started before this Hermes signs out loopback tabs only.+ world["answers"]["webapp-sign-out"]["result"] = {"signed_out": True}+ assert launcher.cmd_webapp(_args(sign_out=True)) == 0+ out = capsys.readouterr().out+ assert launcher.RESTART_COMMAND in out and "Signed out of every" not in out+++def test_a_sign_out_the_gateway_refuses_is_reported_as_not_done(world, capsys):+ from gateway.webapp_listener import TOKEN_PATH, browser_session_token+ before = browser_session_token(world["root"])+ world["answers"]["webapp-sign-out"] = {"ok": False, "error": "PermissionError: authenticated private request required"}+ assert launcher.cmd_webapp(_args(sign_out=True)) == 1+ out = capsys.readouterr().out+ assert "authenticated private request required" in out and "Signed out" not in out+ assert (world["root"] / TOKEN_PATH).read_text().strip() == beforedef test_a_gateway_that_will_not_start_is_reported(world, capsys):@@ -202,7 +239,8 @@ def test_signing_out_with_no_gateway_running_still_replaces_the_stored_credentiaworld["answers"]["webapp-sign-out"] = launcher.GatewayUnreachable("no gateway")assert launcher.cmd_webapp(_args(sign_out=True)) == 0assert (world["root"] / TOKEN_PATH).read_text().strip() != before- assert "Signed out" in capsys.readouterr().out+ out = capsys.readouterr().out+ assert "No gateway is running" in out and "sign in again" in outdef test_the_webapp_command_takes_one_action_at_a_time():diff --git a/tests/hermes_cli/test_webapp_sign_out_sockets.py b/tests/hermes_cli/test_webapp_sign_out_sockets.pyindex f911a98a69d..de64215f883 100644--- a/tests/hermes_cli/test_webapp_sign_out_sockets.py+++ b/tests/hermes_cli/test_webapp_sign_out_sockets.py@@ -73,7 +73,7 @@ def test_signing_out_closes_the_terminal_and_streams_the_session_token_opened(weclient.websocket_connect(f"ws://localhost/api/events?channel=kept&ticket={other}") as kept:metadata(shell)assert _close_code(shell, 0.2) is None and _close_code(events, 0.2) is None- assert sign_out() == {"signed_out": True, "sign_in_gate": False, "connections_closed": 2}+ assert sign_out() == {"signed_out": True, "serving": True, "sign_in_gate": False, "connections_closed": 2}assert _close_code(shell) == 4401assert _close_code(events) == 4401assert _close_code(kept, 0.5) is None@@ -83,7 +83,7 @@ def test_signing_out_closes_the_terminal_and_streams_the_session_token_opened(wedef test_signing_out_with_no_open_tabs_still_replaces_the_credential(webapp, sign_out):client = webapp.client- assert sign_out() == {"signed_out": True, "sign_in_gate": False, "connections_closed": 0}+ assert sign_out() == {"signed_out": True, "serving": True, "sign_in_gate": False, "connections_closed": 0}assert client.post("/api/webapp/gateway-ticket", headers={"X-Hermes-Session-Token": _TOKEN}).status_code == 401