Skip to content

The agent that grows with you

fix(webapp-access): Web Access asks the Nous Portal again only when its callback changed

emozilla3d0d4b07bb57
diff --git a/hermes_cli/dashboard_register.py b/hermes_cli/dashboard_register.py
index a6d2d1e248f..a9f0fceece9 100644
--- a/hermes_cli/dashboard_register.py
+++ b/hermes_cli/dashboard_register.py
@@ -209,6 +209,10 @@ def cmd_dashboard_register(args) -> None:
print(f"✗ Failed to write HERMES_DASHBOARD_OAUTH_CLIENT_ID to .env: {exc}\n"
f" Set it manually: HERMES_DASHBOARD_OAUTH_CLIENT_ID={client_id}")
sys.exit(1)
+ # Web Access shares this client and never posts the same callback twice; this registration may
+ # have replaced it, so Web Access registers its callback again on its next start.
+ from hermes_cli.webapp_access import forget_registration
+ forget_registration()
# Explicit portal → always persist (the user asked); inferred → only if unset AND non-default.
existing_portal = _env_value("HERMES_DASHBOARD_PORTAL_URL")
should_write_portal = (
diff --git a/hermes_cli/webapp_access.py b/hermes_cli/webapp_access.py
index 0bfcf36df3a..eeb0284a57e 100644
--- a/hermes_cli/webapp_access.py
+++ b/hermes_cli/webapp_access.py
@@ -16,6 +16,7 @@ standalone dashboard, which may sit behind its own tunnel or proxy.
from __future__ import annotations
+import json
import os
import socket
from pathlib import Path
@@ -26,6 +27,7 @@ from gateway.webapp_listener import DEFAULT_PORT
_LOOPBACK_HOSTS = frozenset({"127.0.0.1", "::1", "localhost"})
_CLIENT_ID_ENV = "HERMES_DASHBOARD_OAUTH_CLIENT_ID"
+_REGISTRATION_PATH = Path("webapp") / "sign-in-registration.json"
class WebappAccessError(RuntimeError):
@@ -72,9 +74,34 @@ def nous_signed_in() -> bool:
return bool(status.get("logged_in") and not status.get("free_tier"))
+def _registration_path() -> Path:
+ """Where this home records the sign-in callback Web Access last registered with the Portal."""
+ from hermes_constants import get_hermes_home
+
+ return get_hermes_home() / _REGISTRATION_PATH
+
+
+def forget_registration() -> None:
+ """Drop the record of the callback Web Access registered, so its next start registers again.
+ ``hermes dashboard register`` calls this: it updates the same Portal client, and the Portal may
+ replace that client's redirect URI."""
+ _registration_path().unlink(missing_ok=True)
+
+
+def _registered(record: dict) -> bool:
+ try:
+ return json.loads(_registration_path().read_text(encoding="utf-8-sig")) == record
+ except (OSError, ValueError):
+ return False
+
+
def _register_nous_sign_in(callback_url: str) -> None:
"""Register (or update in place) this home's portal client with *callback_url*, as
- ``hermes dashboard register --redirect-uri`` does."""
+ ``hermes dashboard register --redirect-uri`` does.
+
+ The client is the dashboard's (``HERMES_DASHBOARD_OAUTH_CLIENT_ID``) and the Portal may replace
+ its redirect URI on every registration, so the same callback for the same client and Portal is
+ never posted twice: a start with nothing changed asks the Portal nothing."""
from hermes_cli.auth import resolve_nous_access_token
from hermes_cli.config import save_env_value
from hermes_cli.dashboard_register import (
@@ -83,17 +110,26 @@ def _register_nous_sign_in(callback_url: str) -> None:
_register_self_hosted_client,
_resolve_portal_base_url,
)
+ from utils import atomic_write_text
existing = (_env_value(_CLIENT_ID_ENV) or "").strip() or None
+ portal = _resolve_portal_base_url(os.environ.get("HERMES_DASHBOARD_PORTAL_URL"))
+ if existing and _registered({"portal": portal, "client_id": existing, "callback": callback_url}):
+ return
try:
result = _register_self_hosted_client(
access_token=resolve_nous_access_token(),
- portal_base_url=_resolve_portal_base_url(os.environ.get("HERMES_DASHBOARD_PORTAL_URL")),
+ portal_base_url=portal,
name=None if existing else _generate_dashboard_name(),
custom_redirect_uri=callback_url, existing_client_id=existing)
except Exception as exc:
raise WebappAccessError(f"Couldn't register Nous sign-in: {exc}") from exc
- save_env_value(_CLIENT_ID_ENV, str(result["client_id"]))
+ client_id = str(result["client_id"])
+ save_env_value(_CLIENT_ID_ENV, client_id)
+ path = _registration_path()
+ path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
+ record = {"portal": portal, "client_id": client_id, "callback": callback_url}
+ atomic_write_text(path, json.dumps(record) + "\n", mode=0o600)
def plan(mode: str, public_url: Optional[str] = None) -> tuple[str, str, str]:
@@ -129,10 +165,9 @@ def build_bundle() -> None:
def register(callback_url: str) -> None:
- """Register Nous sign-in for *callback_url*.
-
- Re-registered on every start, so the callback follows a changed LAN address or URL. Writes no
- config: it waits on the portal, so callers do it before taking the config lock."""
+ """Register Nous sign-in for *callback_url*, unless Web Access already registered it for this
+ client and Portal, so the callback follows a changed LAN address or URL. Writes no config: it
+ waits on the portal, so callers do it before taking the config lock."""
_register_nous_sign_in(callback_url)
diff --git a/tests/hermes_cli/test_webapp_access.py b/tests/hermes_cli/test_webapp_access.py
index bcafadd285b..373e084bdcb 100644
--- a/tests/hermes_cli/test_webapp_access.py
+++ b/tests/hermes_cli/test_webapp_access.py
@@ -282,3 +282,44 @@ def test_a_restart_refused_by_a_running_chat_is_reported_as_busy(client, nous, m
assert response.json()["restart_started"] is False
assert response.json()["restart_busy"] is True
assert response.json()["enabled"] is True
+
+
+def test_the_portal_is_asked_again_only_when_the_callback_or_client_changed(monkeypatch):
+ """Registering posts the callback under the dashboard's shared OAuth client id, and the Portal
+ may replace that client's redirect URI, so Web Access does not post the same callback for the
+ same client and Portal twice. A new LAN address, a new client id, or a registration made by
+ ``hermes dashboard register`` in between makes the next start register again."""
+ import argparse
+
+ from hermes_cli import auth, dashboard_register, webapp_access
+ from hermes_cli.config import save_env_value
+
+ posts: list[tuple] = []
+
+ def portal(*, access_token, portal_base_url, name, custom_redirect_uri, existing_client_id, timeout=15.0):
+ posts.append((custom_redirect_uri, existing_client_id))
+ return {"client_id": existing_client_id or "client-1", "name": name or "kept"}
+
+ monkeypatch.delenv("HERMES_DASHBOARD_PORTAL_URL", raising=False)
+ monkeypatch.setattr(dashboard_register, "_register_self_hosted_client", portal)
+ monkeypatch.setattr(dashboard_register, "_resolve_portal_base_url", lambda _override=None: "https://portal.example.test")
+ monkeypatch.setattr(auth, "resolve_nous_access_token", lambda: "token")
+ lan = "http://192.168.1.50:9120/auth/callback"
+
+ webapp_access.register(lan)
+ webapp_access.register(lan)
+ assert posts == [(lan, None)]
+
+ moved = "http://192.168.1.51:9120/auth/callback"
+ webapp_access.register(moved)
+ assert posts[-1] == (moved, "client-1")
+
+ save_env_value("HERMES_DASHBOARD_OAUTH_CLIENT_ID", "client-2")
+ webapp_access.register(moved)
+ assert posts[-1] == (moved, "client-2")
+
+ dashboard_register.cmd_dashboard_register(argparse.Namespace(
+ name=None, redirect_uri="https://dashboard.example.test/auth/callback", portal_url=None))
+ webapp_access.register(moved)
+ assert posts[-1] == (moved, "client-2")
+ assert len(posts) == 5