The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp-access): starting or stopping Web Access no longer rewrites the dashboard's public URL
emozillaa973c06285b4
diff --git a/hermes_cli/webapp_access.py b/hermes_cli/webapp_access.pyindex ff0a11c00a2..57e5353a979 100644--- a/hermes_cli/webapp_access.py+++ b/hermes_cli/webapp_access.py@@ -8,7 +8,10 @@ callback); a gated bind without it would keep the Webapp off. Two shapes:- ``lan``: ``webapp.host: 0.0.0.0``, opened at ``http://<this machine's LAN address>:<port>``; thewildcard bind engages the sign-in gate and the callback is rebuilt from each request.- ``public``: an HTTPS URL the user already runs that forwards to ``127.0.0.1:<port>``.- ``dashboard.public_url`` engages the gate on the loopback bind and fixes the callback.+ ``webapp.public_url`` engages the gate on the loopback bind and fixes the callback.++It writes only the ``webapp:`` block: ``dashboard.public_url`` and the ``.env`` values belong to the+standalone dashboard, which may sit behind its own tunnel or proxy."""from __future__ import annotations@@ -22,7 +25,6 @@ from urllib.parse import urlsplitfrom gateway.webapp_listener import DEFAULT_PORT_LOOPBACK_HOSTS = frozenset({"127.0.0.1", "::1", "localhost"})-_PUBLIC_URL_ENV = "HERMES_DASHBOARD_PUBLIC_URL"_CLIENT_ID_ENV = "HERMES_DASHBOARD_OAUTH_CLIENT_ID"@@ -126,21 +128,36 @@ def turn_off() -> None:def _write_config(*, enabled: bool, host: str = "", public_url: str = "") -> None:- """The ``webapp:`` block and ``dashboard.public_url``, which gates the gateway's listener and so goes- with the Webapp. A ``.env`` copy of the URL would shadow it (env wins), so that goes too."""- from hermes_cli.config import load_config, remove_env_value, save_config+ """The ``webapp:`` block, and nothing outside it. Starting pins ``webapp.public_url`` for the+ own-URL mode and drops it for LAN, where the callback follows each request; stopping changes+ only ``enabled``, so the next start offers the same address."""+ from hermes_cli.config import load_config, save_configcfg = load_config()webapp = cfg.get("webapp") if isinstance(cfg.get("webapp"), dict) else {}webapp.update(enabled=enabled, **({"host": host, "port": DEFAULT_PORT} if enabled else {}))+ if enabled and public_url:+ webapp["public_url"] = public_url+ elif enabled:+ webapp.pop("public_url", None)cfg["webapp"] = webapp- dashboard = cfg.setdefault("dashboard", {})- if public_url:- dashboard["public_url"] = public_url- else:- dashboard.pop("public_url", None)save_config(cfg)- remove_env_value(_PUBLIC_URL_ENV)+++def _configured_public_url(home: Path) -> str:+ """``webapp.public_url`` from *home*'s config, or ``""`` (``webapp_settings`` reports an+ unreadable config)."""+ from ruamel.yaml.error import YAMLError++ from gateway.webapp_listener import _config_document++ try:+ document = _config_document(home)+ except (OSError, ValueError, YAMLError):+ return ""+ block = document.get("webapp") if isinstance(document, dict) else None+ value = block.get("public_url") if isinstance(block, dict) else None+ return value.strip().rstrip("/") if isinstance(value, str) else ""def _gateway_state(home: Path) -> dict:@@ -160,11 +177,10 @@ def _gateway_state(home: Path) -> dict:def access_status() -> dict:"""Everything the Web Access pane shows, read for the gateway home in scope."""from gateway.webapp_listener import _is_wildcard, webapp_settings- from hermes_cli.dashboard_auth.prefix import resolve_public_urlhome = gateway_home()settings = webapp_settings(home)- public_url = resolve_public_url()+ public_url = _configured_public_url(home)return {"enabled": settings.enabled,"mode": ("lan" if _is_wildcard(settings.host) else "public") if settings.enabled else None,diff --git a/tests/hermes_cli/test_webapp_access.py b/tests/hermes_cli/test_webapp_access.pyindex a43841846eb..761da1645a3 100644--- a/tests/hermes_cli/test_webapp_access.py+++ b/tests/hermes_cli/test_webapp_access.py@@ -1,8 +1,8 @@"""Remote Webapp access (``hermes_cli.webapp_access``, ``/api/webapp-access/*``).-Contracts: a public URL gates the gateway's loopback bind until Web Access is stopped, LAN access-registers the LAN callback and unpins a public URL, and nothing is written or restarted without a-Nous account to sign in with.+Contracts: Web Access writes only the ``webapp:`` block (the own-URL mode's URL in+``webapp.public_url``), LAN access registers the LAN callback, and nothing is written or restarted+without a Nous account to sign in with."""from __future__ import annotations@@ -55,38 +55,42 @@ def _config() -> dict:return load_config()-def test_public_url_gates_the_loopback_bind_until_stopped(client, restarts, nous):- from hermes_cli.web_server_boundary import _dashboard_public_hosts, listener_auth_required+def _dashboard_setup() -> tuple[object, object]:+ """The standalone dashboard's own public URL, in config.yaml and in .env, as Web Access found it."""+ from hermes_cli.config import load_env, read_raw_config- response = client.post("/api/webapp-access/start", json={"mode": "public", "public_url": "https://hermes.example.test"})+ return read_raw_config().get("dashboard", {}).get("public_url"), load_env().get("HERMES_DASHBOARD_PUBLIC_URL")+++def test_web_access_keeps_its_url_in_the_webapp_block_and_leaves_the_dashboard_alone(client, restarts, nous):+ """Start and stop write only ``webapp.*``: the own-URL mode pins ``webapp.public_url``, LAN mode+ drops it (its callback follows each request), and the standalone dashboard's+ ``dashboard.public_url`` and ``.env`` pin, which a tunnelled dashboard needs after a restart,+ stay exactly as the user set them."""+ from hermes_cli.config import save_config, save_env_value+ save_config({**_config(), "dashboard": {"public_url": "https://dashboard.example.test"}})+ save_env_value("HERMES_DASHBOARD_PUBLIC_URL", "https://dashboard.example.test")+ dashboard = _dashboard_setup()++ response = client.post("/api/webapp-access/start", json={"mode": "public", "public_url": "https://hermes.example.test"})assert response.status_code == 200, response.text- assert _config()["webapp"] == {"enabled": True, "host": "127.0.0.1", "port": 9120}assert nous == ["https://hermes.example.test/auth/callback"]- # What the restarted gateway decides for its listener (gateway.run_api.start_gateway_api).- assert listener_auth_required("127.0.0.1", _dashboard_public_hosts())+ assert {key: _config()["webapp"].get(key) for key in ("enabled", "host", "public_url")} == {+ "enabled": True, "host": "127.0.0.1", "public_url": "https://hermes.example.test"}+ assert response.json()["public_url"] == "https://hermes.example.test"+ assert _dashboard_setup() == dashboard- # Stop takes the public URL with it: it would keep gating the gateway's private listener.assert client.post("/api/webapp-access/stop").status_code == 200assert _config()["webapp"]["enabled"] is False- assert not listener_auth_required("127.0.0.1", _dashboard_public_hosts())- assert restarts == ["turning on Web Access", "turning off Web Access"]---def test_lan_access_registers_the_lan_callback_and_unpins_a_public_url(client, restarts, nous):- from hermes_cli.config import load_env, save_config, save_env_value- from hermes_cli.dashboard_auth.prefix import resolve_public_url-- # A public URL from an earlier "own URL" setup, and an older .env pin that would shadow it.- save_config({**_config(), "dashboard": {"public_url": "https://old.example.test"}})- save_env_value("HERMES_DASHBOARD_PUBLIC_URL", "https://older.example.test")+ assert _dashboard_setup() == dashboardassert client.post("/api/webapp-access/start", json={"mode": "lan"}).status_code == 200-- assert nous == ["http://192.168.1.50:9120/auth/callback"]+ assert nous[-1] == "http://192.168.1.50:9120/auth/callback"assert _config()["webapp"]["host"] == "0.0.0.0"- assert resolve_public_url() == ""- assert "HERMES_DASHBOARD_PUBLIC_URL" not in load_env()+ assert not _config()["webapp"].get("public_url")+ assert _dashboard_setup() == dashboard+ assert restarts == ["turning on Web Access", "turning off Web Access", "turning on Web Access"]def test_start_without_a_nous_account_writes_and_restarts_nothing(client, restarts, nous, monkeypatch):