Skip to content

The agent that grows with you

fix(desktop): name the primary profile on scopable local REST calls to the attached host backend

teknium1c44b4ab9ad2d
diff --git a/apps/desktop/electron/connection-config.test.ts b/apps/desktop/electron/connection-config.test.ts
index 2cdae3d67fa..47f530509be 100644
--- a/apps/desktop/electron/connection-config.test.ts
+++ b/apps/desktop/electron/connection-config.test.ts
@@ -351,6 +351,21 @@ const ROUTES = [
opts: { primaryProfile: 'default' },
expected: { backend: 'primary', descriptorProfile: null, scopePath: false }
},
+ {
+ // #118431/#118432: the host backend this app attached to may have been
+ // launched under another profile's home, so the primary's own scopable
+ // REST calls must still say which profile they mean.
+ name: 'the primary profile names itself on a scopable local REST request',
+ profile: 'nash',
+ opts: { primaryProfile: 'nash', globalRemote: false, requestMethod: 'POST', requestPath: '/api/model/set' },
+ expected: { backend: 'primary', descriptorProfile: 'nash', scopePath: true }
+ },
+ {
+ name: 'the primary profile stays unscoped on a route the server cannot scope',
+ profile: 'nash',
+ opts: { primaryProfile: 'nash', globalRemote: false, requestMethod: 'POST', requestPath: '/api/files/upload' },
+ expected: { backend: 'primary', descriptorProfile: null, scopePath: false }
+ },
{
name: 'a renamed primary profile on a global remote is still scoped on the wire',
profile: ' coder ',
diff --git a/apps/desktop/electron/connection-config.ts b/apps/desktop/electron/connection-config.ts
index 702088c96f7..ff293957423 100644
--- a/apps/desktop/electron/connection-config.ts
+++ b/apps/desktop/electron/connection-config.ts
@@ -787,7 +787,19 @@ function resolveProfileBackendRoute(profile, opts: ProfileRouteOptions = {}): Pr
// launched for this Desktop label. Even its "primary" label must travel on
// the wire: the dashboard's process HERMES_HOME can belong to a different
// launch profile, so a bare request silently reads that profile instead.
- return opts.globalRemote
+ if (opts.globalRemote) {
+ return { backend: 'primary', descriptorProfile: scopedProfile, scopePath: true }
+ }
+
+ // The same holds for the LOCAL host backend: with one `hermes serve` per
+ // host the app attaches to whatever backend is running, and that process
+ // was launched under some OTHER profile's home whenever another app (or an
+ // earlier boot) registered it. A bare request the server can scope then
+ // resolves to that launch home, not this primary — the Settings → Models
+ // write that landed on the wrong profile's config.yaml (#118431/#118432).
+ // Naming the primary on a scopable route is a no-op for a backend that
+ // did launch as it (current-profile semantics server-side).
+ return localPrimaryRequestScope(opts) === true
? { backend: 'primary', descriptorProfile: scopedProfile, scopePath: true }
: { backend: 'primary', descriptorProfile: null, scopePath: false }
}
diff --git a/tests/hermes_cli/test_web_profiles_model_scope.py b/tests/hermes_cli/test_web_profiles_model_scope.py
index 78a28047c75..52ae0d7c177 100644
--- a/tests/hermes_cli/test_web_profiles_model_scope.py
+++ b/tests/hermes_cli/test_web_profiles_model_scope.py
@@ -140,3 +140,41 @@ def test_model_set_without_a_profile_pins_to_the_launch_home(client, homes):
assert (load_config().get("model") or {}).get("default") == "acme/mini"
with _hermes_home_scope(demo):
assert (load_config().get("model") or {}).get("default") is None
+
+
+def test_model_set_names_target_from_a_backend_launched_as_another_profile(homes, probe, monkeypatch):
+ """A→B→A under one host backend (#118431/#118432): a backend launched under profile A
+ serving ``POST /api/model/set?profile=B`` writes B's config.yaml and leaves A's (and the
+ root's) untouched; the follow-up for A lands on A only. This is the server half the
+ Desktop relies on once every Settings request names the profile its page displays."""
+ root, demo = homes
+ from hermes_cli import profiles as profiles_mod
+ from hermes_cli.config import invalidate_env_cache, load_config
+ from hermes_cli.web_server_profiles import _hermes_home_scope
+
+ other = profiles_mod.get_profile_dir("other")
+ other.mkdir(parents=True, exist_ok=True)
+ (other / "config.yaml").write_text(ACME_YAML, encoding="utf-8")
+ (other / ".env").write_text("ACME_RELAY_KEY=other-key\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(demo)) # launched as A = demo
+ invalidate_env_cache()
+ from hermes_cli import web_server
+
+ body = {"scope": "main", "provider": "acme", "model": "acme/mini", "confirm_expensive_model": True}
+
+ def model_of(home):
+ with _hermes_home_scope(home):
+ return (load_config().get("model") or {}).get("default")
+
+ with TestClient(web_server.app, raise_server_exceptions=False) as client:
+ client.headers["Authorization"] = f"Bearer {web_server._SESSION_TOKEN}"
+ resp = client.post("/api/model/set", params={"profile": "other"}, json=body)
+ assert resp.status_code == 200, resp.text
+ assert probe["api_key"] == "other-key" # validated under B's secret scope, not A's
+ assert model_of(other) == "acme/mini"
+ assert model_of(demo) is None and model_of(root) is None
+
+ resp = client.post("/api/model/set", params={"profile": "demo"}, json={**body, "model": "acme/mini"})
+ assert resp.status_code == 200, resp.text
+ assert probe["api_key"] == "profile-key"
+ assert model_of(demo) == "acme/mini" and model_of(root) is None