The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(config): custom endpoint save/delete detach the mirror for every key spelling
teknium166dfecca8995
diff --git a/hermes_cli/web_routers/config_env.py b/hermes_cli/web_routers/config_env.pyindex 70b89fb119a..1fc610be77d 100644--- a/hermes_cli/web_routers/config_env.py+++ b/hermes_cli/web_routers/config_env.py@@ -314,8 +314,15 @@ def _custom_endpoint_id(raw: str, fallback: str = "custom") -> str:return slug or fallback-def _resolve_custom_endpoint_entry(providers: Any, endpoint_id: str) -> Tuple[str, Optional[Dict[str, Any]]]:- """Resolve a custom endpoint id using the stored key first, then its legacy slug."""+def _resolve_custom_endpoint_entry(providers: Any, endpoint_id: str) -> Tuple[Any, Optional[Dict[str, Any]]]:+ """Resolve a custom endpoint id using the stored key first, then its legacy slug.++ The list route hands Desktop the literal ``providers.<key>`` (a v11→v12+ migration keeps dots/colons from the display name: ``local-127.0.0.1:8283``;+ hand-written keys keep their case), so that spelling must round-trip+ unchanged. Slugging is only the compatibility path for callers that still+ send an unslugged display name.+ """stored_key, entry = find_provider_entry(providers, endpoint_id)if entry is not None:return stored_key, entry@@ -425,21 +432,27 @@ def _custom_endpoint_response(cfg: Dict[str, Any]) -> Dict[str, Any]:}-def _detach_main_model_from_provider(cfg: Dict[str, Any], provider_key: str) -> None:+def _detach_main_model_from_provider(cfg: Dict[str, Any], provider_key: str, entry: Optional[Dict[str, Any]] = None) -> None:"""Drop the main-slot mirror of a provider that no longer exists.``activate_custom_endpoint`` copies the endpoint's ``base_url`` and``api_key`` onto ``model``; that mirror outranks the environment at clientconstruction, so deleting the endpoint without clearing it leaves the agentauthenticating to the deleted host with the deleted key (and the key in- config.yaml). Only touches ``model`` when it names the deleted provider.+ config.yaml). Only touches ``model`` when it names the deleted provider —+ ``switch_model`` spells that either as the stored key or as+ ``custom:<lowercased name>``, so both spellings count.See #62269."""model_cfg = cfg.get("model")if not isinstance(model_cfg, dict):return- if str(model_cfg.get("provider") or "").strip().lower() != provider_key:+ names = {coerce_provider_id(provider_key).lower()}+ if isinstance(entry, dict) and coerce_provider_id(entry.get("name")):+ names.add(coerce_provider_id(entry.get("name")).lower())+ current = str(model_cfg.get("provider") or "").strip().lower()+ if current.removeprefix("custom:") not in names:returnfor field in ("provider", "base_url", "api_key", "key_env"):model_cfg.pop(field, None)@@ -447,7 +460,6 @@ def _detach_main_model_from_provider(cfg: Dict[str, Any], provider_key: str) ->def _write_custom_endpoint(cfg: Dict[str, Any], body: CustomEndpointUpdate) -> Tuple[str, Dict[str, Any]]:- endpoint_id = _custom_endpoint_id(body.id or body.name)name = (body.name or "").strip()base_url = (body.base_url or "").strip().rstrip("/")model = (body.model or "").strip()@@ -468,7 +480,10 @@ def _write_custom_endpoint(cfg: Dict[str, Any], body: CustomEndpointUpdate) -> Tproviders = cfg.get("providers")if not isinstance(providers, dict):providers = {}- stored_key, existing = find_provider_entry(providers, endpoint_id)+ # An edit payload carries the stored key verbatim; slugging it first would+ # miss the entry and fork a slugged twin next to the original.+ stored_key, existing = _resolve_custom_endpoint_entry(providers, body.id or body.name)+ endpoint_id = coerce_provider_id(stored_key) if existing is not None else _custom_endpoint_id(body.id or body.name)if existing is None:existing = {}@@ -576,9 +591,10 @@ def activate_custom_endpoint(endpoint_id: str, profile: Optional[str] = None):):with _config_profile_scope(profile), _CONFIG_MUTATION_LOCK: # RMW spancfg = load_config()- provider_key, entry = _resolve_custom_endpoint_entry(cfg.get("providers"), endpoint_id)+ stored_key, entry = _resolve_custom_endpoint_entry(cfg.get("providers"), endpoint_id)if entry is None:raise HTTPException(status_code=404, detail="custom endpoint not found")+ provider_key = coerce_provider_id(stored_key)models = _models_from_custom_endpoint_entry(entry)model = str(entry.get("model") or (models[0] if models else "")).strip()@@ -620,10 +636,10 @@ def delete_custom_endpoint(endpoint_id: str, profile: Optional[str] = None):stored_key, entry = _resolve_custom_endpoint_entry(providers, endpoint_id)if entry is None or not isinstance(providers, dict):raise HTTPException(status_code=404, detail="custom endpoint not found")- provider_key = stored_key+ provider_key = coerce_provider_id(stored_key)providers.pop(stored_key, None)cfg["providers"] = providers- _detach_main_model_from_provider(cfg, provider_key)+ _detach_main_model_from_provider(cfg, provider_key, entry)remove_env_value(custom_endpoint_key_env(provider_key))save_config(cfg)response = _custom_endpoint_response(cfg)diff --git a/tests/hermes_cli/test_custom_endpoint_id_resolution.py b/tests/hermes_cli/test_custom_endpoint_id_resolution.pydeleted file mode 100644index ee8236d57da..00000000000--- a/tests/hermes_cli/test_custom_endpoint_id_resolution.py+++ /dev/null@@ -1,46 +0,0 @@-"""Regression tests for custom endpoint identifier resolution."""--from hermes_cli.web_routers.config_env import _resolve_custom_endpoint_entry---def test_custom_endpoint_resolution_prefers_stored_key_with_punctuation():- providers = {- "local-127.0.0.1:8283": {- "name": "Local",- "base_url": "http://127.0.0.1:8283/v1",- "model": "local-model",- }- }-- stored_key, entry = _resolve_custom_endpoint_entry(- providers, "local-127.0.0.1:8283"- )-- assert stored_key == "local-127.0.0.1:8283"- assert entry["name"] == "Local"---def test_custom_endpoint_resolution_keeps_slug_compatibility():- providers = {- "local-127-0-0-1-8283": {- "name": "Local",- "base_url": "http://127.0.0.1:8283/v1",- "model": "local-model",- }- }-- stored_key, entry = _resolve_custom_endpoint_entry(- providers, "local-127.0.0.1:8283"- )-- assert stored_key == "local-127-0-0-1-8283"- assert entry["name"] == "Local"---def test_custom_endpoint_resolution_returns_none_for_unknown_id():- stored_key, entry = _resolve_custom_endpoint_entry(- {"existing": {"name": "Existing"}}, "missing"- )-- assert stored_key is None- assert entry is Nonediff --git a/tests/hermes_cli/test_web_server.py b/tests/hermes_cli/test_web_server.pyindex 7aaca323e55..5eb6d9a7041 100644--- a/tests/hermes_cli/test_web_server.py+++ b/tests/hermes_cli/test_web_server.py@@ -1876,6 +1876,76 @@ class TestWebServerEndpoints:assert 2070 not in providersassert "2070" not in providers+ def test_punctuated_provider_key_round_trips_through_activate_edit_and_delete(self):+ """A stored ``providers.<key>`` with dots/colons or mixed case is what the+ list route returns as ``id``; the same spelling must reach the entry on+ activate, save (edit) and delete instead of being slugified into a+ non-existent twin (404 / duplicate row), and delete must still clear+ the model mirror ``switch_model`` wrote for it.+ """+ from urllib.parse import quote++ from hermes_cli.config import get_config_path, load_config++ get_config_path().write_text(+ "model:\n"+ " provider: openrouter\n"+ " default: some/model\n"+ "providers:\n"+ " local-127.0.0.1:8283:\n"+ " name: Local (127.0.0.1:8283)\n"+ " base_url: http://127.0.0.1:8283/v1\n"+ " model: Qwen.gguf\n"+ " EXllamav3:\n"+ " name: EXllamav3\n"+ " base_url: http://127.0.0.1:8290/v1\n"+ " model: Qwen3-27B\n",+ encoding="utf-8",+ )+ dotted = "local-127.0.0.1:8283"+ listed = [e["id"] for e in self.client.get("/api/providers/custom-endpoints").json()["endpoints"]]+ assert dotted in listed and "EXllamav3" in listed++ # Edit by the listed id updates the entry in place — no slugged twin.+ saved = self.client.post(+ "/api/providers/custom-endpoints",+ json={"id": dotted, "name": "Local (127.0.0.1:8283)",+ "base_url": "http://127.0.0.1:8283/v1", "model": "Qwen2.gguf"},+ )+ assert saved.status_code == 200, saved.text+ providers = load_config()["providers"]+ assert providers[dotted]["model"] == "Qwen2.gguf"+ assert "local-127-0-0-1-8283" not in providers++ for key in (dotted, "EXllamav3"):+ path = f"/api/providers/custom-endpoints/{quote(key, safe='')}"+ activate = self.client.post(f"{path}/activate", json={})+ assert activate.status_code == 200, activate.text+ assert load_config()["model"].get("base_url"), key+ deleted = self.client.request("DELETE", path)+ assert deleted.status_code == 200, deleted.text+ cfg = load_config()+ assert key not in (cfg.get("providers") or {})+ assert not cfg["model"].get("base_url"), f"{key}: deleted endpoint's host still routed to"+ assert not cfg["model"].get("provider"), key++ def test_unslugged_display_name_still_resolves_to_its_slug_key(self):+ """Compatibility fallback: a caller sending the display name reaches the+ dashboard-minted slug key; an unknown id is still a 404."""+ from hermes_cli.config import get_config_path, load_config++ get_config_path().write_text(+ "providers:\n"+ " local-8000:\n"+ " name: Local 8000\n"+ " base_url: http://127.0.0.1:8000/v1\n"+ " model: m\n",+ encoding="utf-8",+ )+ assert self.client.request("DELETE", "/api/providers/custom-endpoints/nope.nope").status_code == 404+ assert self.client.request("DELETE", "/api/providers/custom-endpoints/Local%208000").status_code == 200+ assert "local-8000" not in (load_config().get("providers") or {})+def test_custom_endpoint_save_scopes_to_the_requested_profile(self):"""``?profile=<name>`` must write into that profile's config.yaml.