Skip to content

The agent that grows with you

fix(dashboard): profile-less chat deep links inherit the launcher's preselected profile

liguoyua8cccc22f2c2
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index fad9a5cfbb3..2a57daa3d44 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -1418,6 +1418,9 @@ def start_server(
# host_header_middleware validates Host against this (DNS rebinding,
# GHSA-ppp5-vxwm-4cf7).
app.state.bound_host = host
+ # The SPA bootstrap reads this so profile-less deep links (/chat?resume=<id>) inherit the
+ # launcher's preselected profile instead of silently running in the launch scope (#73085).
+ app.state.initial_profile = str(initial_profile or "")
config, server = _build_uvicorn_server(host, port, ssh_isolated=bool(ssh_session_token))
diff --git a/hermes_cli/web_server_dashboard.py b/hermes_cli/web_server_dashboard.py
index 7e602279b0a..dddfd13317e 100644
--- a/hermes_cli/web_server_dashboard.py
+++ b/hermes_cli/web_server_dashboard.py
@@ -153,11 +153,15 @@ def mount_spa(application: FastAPI):
chat_js = "true" if _DASHBOARD_EMBEDDED_CHAT_ENABLED else "false"
gated = bool(getattr(app.state, "auth_required", False))
token_js = "" if gated else f'window.__HERMES_SESSION_TOKEN__="{_server()._SESSION_TOKEN}";'
+ # Launcher-preselected profile (``--open-profile``): the SPA's fallback scope when the URL
+ # omits ``?profile=`` (#73085). ``</`` escaped so a hostile name cannot close the script tag.
+ initial_profile_js = json.dumps(str(getattr(application.state, "initial_profile", "") or "")).replace("</", "<\\/")
bootstrap_script = (
f"<script>{token_js}"
f"window.__HERMES_DASHBOARD_EMBEDDED_CHAT__={chat_js};"
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"</script>"
)
if prefix:
diff --git a/tests/hermes_cli/test_dashboard_initial_profile_bootstrap.py b/tests/hermes_cli/test_dashboard_initial_profile_bootstrap.py
new file mode 100644
index 00000000000..711f182c81f
--- /dev/null
+++ b/tests/hermes_cli/test_dashboard_initial_profile_bootstrap.py
@@ -0,0 +1,47 @@
+from fastapi import FastAPI
+from starlette.testclient import TestClient
+
+from hermes_cli import web_server
+
+
+def test_spa_bootstrap_includes_dashboard_initial_profile(tmp_path, monkeypatch):
+ dist = tmp_path / "web_dist"
+ (dist / "assets").mkdir(parents=True)
+ (dist / "index.html").write_text(
+ "<html><head></head><body>Dashboard</body></html>",
+ encoding="utf-8",
+ )
+ monkeypatch.setattr(web_server, "WEB_DIST", dist)
+ monkeypatch.delenv("HERMES_SERVE_HEADLESS", raising=False)
+
+ app = FastAPI()
+ app.state.initial_profile = "worker_x"
+ web_server.mount_spa(app)
+
+ response = TestClient(app).get("/chat?resume=session-1")
+
+ assert response.status_code == 200
+ assert 'window.__HERMES_INITIAL_PROFILE__="worker_x";' in response.text
+
+
+def test_spa_bootstrap_escapes_initial_profile_for_script_context(
+ tmp_path, monkeypatch
+):
+ dist = tmp_path / "web_dist"
+ (dist / "assets").mkdir(parents=True)
+ (dist / "index.html").write_text(
+ "<html><head></head><body>Dashboard</body></html>",
+ encoding="utf-8",
+ )
+ monkeypatch.setattr(web_server, "WEB_DIST", dist)
+ monkeypatch.delenv("HERMES_SERVE_HEADLESS", raising=False)
+
+ app = FastAPI()
+ app.state.initial_profile = "bad</script><script>alert(1)</script>"
+ web_server.mount_spa(app)
+
+ response = TestClient(app).get("/chat")
+
+ assert response.status_code == 200
+ assert "bad<\\/script><script>alert(1)<\\/script>" in response.text
+ assert "bad</script><script>alert(1)</script>" not in response.text
diff --git a/web/src/contexts/ProfileProvider.tsx b/web/src/contexts/ProfileProvider.tsx
index df2f02106c8..8c1adaff2a5 100644
--- a/web/src/contexts/ProfileProvider.tsx
+++ b/web/src/contexts/ProfileProvider.tsx
@@ -8,6 +8,11 @@ import {
import { useLocation, useSearchParams } from "react-router";
import { api, setManagementProfile } from "@/lib/api";
import { ProfileContext } from "@/contexts/profile-context";
+import {
+ dashboardInitialProfile,
+ initialProfileScope,
+ shouldAdoptActiveProfile,
+} from "@/lib/profile-bootstrap";
/**
* Machine-level management-profile scope.
@@ -38,11 +43,13 @@ export function ProfileProvider({ children }: { children: ReactNode }) {
const { pathname } = useLocation();
const [profiles, setProfiles] = useState<string[]>([]);
const [currentProfile, setCurrentProfile] = useState("default");
+ const bootstrapProfile = dashboardInitialProfile();
- // Initial value comes from the URL (deep link / refresh / unified-launch
- // preselect); afterwards state leads and the URL follows.
+ // An explicit URL wins; profile-less deep links inherit the unified-launch
+ // preselection injected by the server. Afterwards state leads and the URL
+ // follows.
const [profile, setProfileState] = useState(
- () => searchParams.get("profile") ?? "",
+ () => initialProfileScope(searchParams, bootstrapProfile),
);
// Mirror into the api module synchronously on every render where it
@@ -92,11 +99,17 @@ export function ProfileProvider({ children }: { children: ReactNode }) {
const active = info.active || "default";
setCurrentProfile(current);
- // Deep links (?profile=) win. Otherwise align the switcher with the
- // sticky active profile so Chat and management pages match what the
- // Profiles page shows as "active" (machine dashboard runs as
- // `current`, usually default).
- if (urlProfile === null && active !== current) {
+ // Explicit URL and unified-launch bootstrap scopes win. Without
+ // either, align the switcher with the sticky active profile so Chat
+ // and management pages match what Profiles shows as "active".
+ if (
+ shouldAdoptActiveProfile(
+ urlProfile,
+ bootstrapProfile,
+ current,
+ active,
+ )
+ ) {
setManagementProfile(active);
setProfileState(active);
}
diff --git a/web/src/lib/profile-bootstrap.test.ts b/web/src/lib/profile-bootstrap.test.ts
new file mode 100644
index 00000000000..583af2e9f59
--- /dev/null
+++ b/web/src/lib/profile-bootstrap.test.ts
@@ -0,0 +1,40 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ initialProfileScope,
+ shouldAdoptActiveProfile,
+} from "./profile-bootstrap";
+
+describe("initialProfileScope", () => {
+ it("inherits the dashboard bootstrap profile when the URL omits profile", () => {
+ expect(initialProfileScope(new URLSearchParams("resume=session-1"), "worker_x"))
+ .toBe("worker_x");
+ });
+
+ it("keeps an explicit URL profile authoritative", () => {
+ expect(
+ initialProfileScope(
+ new URLSearchParams("resume=session-1&profile=review"),
+ "worker_x",
+ ),
+ ).toBe("review");
+ });
+
+ it("preserves an explicit empty profile", () => {
+ expect(
+ initialProfileScope(new URLSearchParams("profile="), "worker_x"),
+ ).toBe("");
+ });
+
+ it("does not replace a launch profile with the sticky active profile", () => {
+ expect(
+ shouldAdoptActiveProfile(null, "worker_x", "default", "review"),
+ ).toBe(false);
+ });
+
+ it("uses the sticky active profile without a URL or launch profile", () => {
+ expect(
+ shouldAdoptActiveProfile(null, "", "default", "review"),
+ ).toBe(true);
+ });
+});
diff --git a/web/src/lib/profile-bootstrap.ts b/web/src/lib/profile-bootstrap.ts
new file mode 100644
index 00000000000..8a0cb2204c0
--- /dev/null
+++ b/web/src/lib/profile-bootstrap.ts
@@ -0,0 +1,31 @@
+declare global {
+ interface Window {
+ __HERMES_INITIAL_PROFILE__?: string;
+ }
+}
+
+export function dashboardInitialProfile(): string {
+ if (typeof window === "undefined") return "";
+ return window.__HERMES_INITIAL_PROFILE__ ?? "";
+}
+
+export function initialProfileScope(
+ searchParams: URLSearchParams,
+ bootstrapProfile = dashboardInitialProfile(),
+): string {
+ const urlProfile = searchParams.get("profile");
+ return urlProfile === null ? bootstrapProfile : urlProfile;
+}
+
+export function shouldAdoptActiveProfile(
+ urlProfile: string | null,
+ bootstrapProfile: string,
+ currentProfile: string,
+ activeProfile: string,
+): boolean {
+ return (
+ urlProfile === null &&
+ !bootstrapProfile &&
+ activeProfile !== currentProfile
+ );
+}
diff --git a/web/vite.config.ts b/web/vite.config.ts
index b23b716d063..b45ee36b833 100644
--- a/web/vite.config.ts
+++ b/web/vite.config.ts
@@ -23,13 +23,15 @@ const BACKEND = process.env.HERMES_DASHBOARD_URL ?? "http://127.0.0.1:9119";
* token, every protected `/api/*` call 401s.
*
* This plugin fetches the running dashboard's `index.html` on each dev page
- * load, scrapes the `window.__HERMES_SESSION_TOKEN__` assignment, and
- * re-injects it into the dev HTML. No-op in production builds.
+ * load and forwards its runtime bootstrap values into the dev HTML. No-op in
+ * production builds.
*/
function hermesDevToken(): Plugin {
const TOKEN_RE = /window\.__HERMES_SESSION_TOKEN__\s*=\s*"([^"]+)"/;
const EMBEDDED_RE =
/window\.__HERMES_DASHBOARD_EMBEDDED_CHAT__\s*=\s*(true|false)/;
+ const INITIAL_PROFILE_RE =
+ /window\.__HERMES_INITIAL_PROFILE__\s*=\s*("(?:\\.|[^"\\])*")/;
return {
name: "hermes:dev-session-token",
@@ -48,13 +50,16 @@ function hermesDevToken(): Plugin {
}
const embeddedMatch = html.match(EMBEDDED_RE);
const embeddedJs = embeddedMatch ? embeddedMatch[1] : "true";
+ const initialProfileMatch = html.match(INITIAL_PROFILE_RE);
+ const initialProfileJs = initialProfileMatch?.[1] ?? '""';
return [
{
tag: "script",
injectTo: "head",
children:
`window.__HERMES_SESSION_TOKEN__="${match[1]}";` +
- `window.__HERMES_DASHBOARD_EMBEDDED_CHAT__=${embeddedJs};`,
+ `window.__HERMES_DASHBOARD_EMBEDDED_CHAT__=${embeddedJs};` +
+ `window.__HERMES_INITIAL_PROFILE__=${initialProfileJs};`,
},
];
} catch (err) {