Skip to content

The agent that grows with you

fix(dashboard): the dashboard serves its /index.html and page bootstrap as on main

emozilla68387ab2211f
diff --git a/hermes_cli/web_server_dashboard.py b/hermes_cli/web_server_dashboard.py
index eaec75be31f..90448c9e269 100644
--- a/hermes_cli/web_server_dashboard.py
+++ b/hermes_cli/web_server_dashboard.py
@@ -185,7 +185,9 @@ def mount_spa(application: FastAPI):
return JSONResponse({"error": stale}, status_code=503, headers=_NO_STORE)
chat_js = "true" if _DASHBOARD_EMBEDDED_CHAT_ENABLED else "false"
gated = bool(getattr(application.state, "auth_required", False))
- surface = getattr(application.state, "ui_surface", "dashboard")
+ # Only a host that records its surface (the gateway's Webapp) names it to the page.
+ surface = getattr(application.state, "ui_surface", None)
+ surface_js = "" if surface is None else f"window.__HERMES_UI_SURFACE__={json.dumps(surface)};"
# Webapp grants host files and shell.exec as well as PTYs: its existing
# session token must come from the operator, never anonymous HTML. The
# gateway-hosted listener (gateway/run_api.py) never publishes it either:
@@ -208,7 +210,7 @@ def mount_spa(application: FastAPI):
f'window.__HERMES_BASE_PATH__="{prefix}";'
f"window.__HERMES_AUTH_REQUIRED__={'true' if gated else 'false'};"
f"window.__HERMES_INITIAL_PROFILE__={initial_profile_js};"
- f"window.__HERMES_UI_SURFACE__={json.dumps(surface)};"
+ f"{surface_js}"
f"window.__HERMES_DASHBOARD_PROFILE__={serving_profile_js};"
f"</script>"
)
@@ -269,7 +271,8 @@ def mount_spa(application: FastAPI):
# real 404 JSON instead of index.html (which breaks JSON clients with a SyntaxError).
if full_path == "api" or full_path.startswith("api/"):
return JSONResponse({"detail": f"No such API endpoint: /{full_path}"}, status_code=404)
- if full_path == "index.html":
+ if full_path == "index.html" and not policy(application.state).html_token:
+ # Where HTML may not carry the session token, no entry serves the raw file.
return _serve_index(prefix)
dist = web_dist()
file_path = dist / full_path
diff --git a/tests/hermes_cli/test_spa_document_policy.py b/tests/hermes_cli/test_spa_document_policy.py
index 6ca95f486e0..3ed78bb20af 100644
--- a/tests/hermes_cli/test_spa_document_policy.py
+++ b/tests/hermes_cli/test_spa_document_policy.py
@@ -1,33 +1,62 @@
"""Every privileged SPA document uses the same bootstrap policy: uncached, prefixed, and tokenless
-whenever the gate or the surface withholds the session token."""
+whenever the gate or the surface withholds the session token. Where the surface's policy keeps the
+token out of HTML (the Webapp), ``/index.html`` gets that bootstrap too; the dashboard serves its
+``/index.html`` and its bootstrap as on main."""
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
+HTML = '<html><head><script src="/assets/app.js"></script></head><body>fixture</body></html>'
[email protected]("surface", ["webapp", "dashboard"])
[email protected]("prefix", ["", "/hermes"])
[email protected]("path", ["/", "/chat/session", "/index.html", "/help.html"])
-def test_spa_documents_share_the_bootstrap_policy(monkeypatch, tmp_path, surface, prefix, path):
+
+def _spa(monkeypatch, tmp_path, *, surface, gated) -> TestClient:
from hermes_cli import web_server as server
from hermes_cli.web_server_dashboard import mount_spa
- html = '<html><head><script src="/assets/app.js"></script></head><body>fixture</body></html>'
- (tmp_path / "index.html").write_text(html)
+ (tmp_path / "index.html").write_text(HTML)
(tmp_path / "help.html").write_text("<html><body>help</body></html>")
app = FastAPI()
- app.state.ui_surface = surface
- app.state.auth_required = True
+ if surface is not None:
+ app.state.ui_surface = surface
+ app.state.auth_required = gated
app.state.web_dist = tmp_path
monkeypatch.setattr(server, "app", app)
monkeypatch.delenv("HERMES_SERVE_HEADLESS", raising=False)
mount_spa(app)
- response = TestClient(app).get(path, headers={"X-Forwarded-Prefix": prefix})
+ return TestClient(app)
+
+
[email protected]("surface", ["webapp", None], ids=["webapp", "dashboard"])
[email protected]("prefix", ["", "/hermes"])
[email protected]("path", ["/", "/chat/session", "/index.html", "/help.html"])
+def test_spa_documents_share_the_bootstrap_policy(monkeypatch, tmp_path, surface, prefix, path):
+ from hermes_cli import web_server as server
+
+ response = _spa(monkeypatch, tmp_path, surface=surface, gated=True).get(
+ path, headers={"X-Forwarded-Prefix": prefix})
assert response.status_code == 200
- if path != "/help.html":
- # /index.html included: every entry to the app gets the bootstrap, never the raw file.
+ if path in ("/", "/chat/session") or (path == "/index.html" and surface == "webapp"):
+ # On the Webapp /index.html is included: every entry to the app gets the bootstrap.
assert "no-store" in response.headers.get("cache-control", "")
- assert f'window.__HERMES_UI_SURFACE__="{surface}"' in response.text
assert f'window.__HERMES_BASE_PATH__="{prefix}"' in response.text
assert f'src="{prefix}/assets/app.js"' in response.text
assert server._SESSION_TOKEN not in response.text
+
+
[email protected]("gated", [False, True])
+def test_the_dashboard_serves_index_html_and_its_bootstrap_as_on_main(monkeypatch, tmp_path, gated):
+ """With the Webapp off, ``/index.html`` is the built file itself and the bootstrap names no
+ surface: only the Webapp's page carries ``__HERMES_UI_SURFACE__``, which its renderer reads."""
+ client = _spa(monkeypatch, tmp_path, surface=None, gated=gated)
+
+ raw = client.get("/index.html")
+ assert raw.status_code == 200
+ assert raw.text == HTML
+ assert "__HERMES_UI_SURFACE__" not in client.get("/").text
+
+
+def test_the_webapp_page_names_its_surface(monkeypatch, tmp_path):
+ client = _spa(monkeypatch, tmp_path, surface="webapp", gated=False)
+
+ for path in ("/", "/index.html"):
+ assert 'window.__HERMES_UI_SURFACE__="webapp"' in client.get(path).text
diff --git a/tests/hermes_cli/test_webapp_private_bootstrap.py b/tests/hermes_cli/test_webapp_private_bootstrap.py
index a0864068a92..745f378ed84 100644
--- a/tests/hermes_cli/test_webapp_private_bootstrap.py
+++ b/tests/hermes_cli/test_webapp_private_bootstrap.py
@@ -30,10 +30,12 @@ def test_bootstrap_token_visibility(monkeypatch, tmp_path, surface, gated, headl
client = TestClient(application)
for path in ("/", "/chat", "/settings", "/index.html"):
response = client.get(path)
- should_inject = not gated and surface != "webapp" and (not headless or path == "/")
+ # The dashboard serves its built /index.html as on main; only the Webapp bootstraps it too.
+ raw = path == "/index.html" and surface != "webapp"
+ should_inject = not gated and surface != "webapp" and not raw and (not headless or path == "/")
assert (server._SESSION_TOKEN in response.text) is should_inject
assert server._SESSION_TOKEN not in str(response.headers)
- if not headless:
+ if not headless and not raw:
profile_js = json.dumps(serving_profile).replace("</", "<\\/")
assert f"window.__HERMES_DASHBOARD_PROFILE__={profile_js};" in response.text
if surface == "webapp" and not headless: