The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): a providers.custom entry never lends its key to the endpoint the wizard submits
Teknium5c6592118a75
diff --git a/hermes_cli/web_server_config.py b/hermes_cli/web_server_config.pyindex 250354599bb..98c478b97a8 100644--- a/hermes_cli/web_server_config.py+++ b/hermes_cli/web_server_config.py@@ -476,14 +476,18 @@ def _normalize_main_model_assignment(provider: str, model: str) -> tuple[str, streturn prov_in, model_in+_BARE_CUSTOM_SELECTORS = frozenset({"custom", "local"})++def _is_explicit_custom_endpoint(provider: str, base_url: str) -> bool:"""A bare ``custom`` / ``local`` pick that carries its own endpoint URL defines a route; it isnever a reference to an already-configured provider."""- return provider.strip().lower() in {"custom", "local"} and bool((base_url or "").strip())+ return provider.strip().lower() in _BARE_CUSTOM_SELECTORS and bool((base_url or "").strip())def _validated_main_model_selection(- cfg: dict, provider: str, model: str, base_url: str = "", api_key: str = ""+ cfg: dict, provider: str, model: str, base_url: str = "", api_key: str = "",+ explicit_endpoint: Optional[bool] = None,) -> "ModelSwitchResult":"""Route a dashboard main-slot pick through ``switch_model`` (catalog/alias/credentialvalidation) seeded with the configured route, exactly like a ``/model <model> --provider@@ -494,9 +498,17 @@ def _validated_main_model_selection(model_cfg = cfg.get("model") if isinstance(cfg.get("model"), dict) else {}is_bare_custom = provider.strip().lower() in {"custom", "local"}- # An explicit endpoint must not resolve through the named custom providers: the legacy- # bare-``custom`` recovery in ``resolve_custom_provider`` would bind the FIRST named entry.- explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)+ # An explicit endpoint must not resolve through the configured providers: the legacy+ # bare-``custom`` recovery in ``resolve_custom_provider`` would bind the FIRST named entry, and+ # a ``providers.custom`` / ``providers.local`` block (same key as the bare selector) would win+ # the credential step and validate ITS host with ITS key. ``_prepare_main_assignment`` passes+ # whether the URL was SUBMITTED; a URL it filled from that block is a selection, not a route.+ if explicit_endpoint is None:+ explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)+ user_providers = cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {}+ if explicit_endpoint:+ user_providers = {k: v for k, v in user_providers.items()+ if str(k).strip().lower() not in _BARE_CUSTOM_SELECTORS}# The submitted endpoint is seeded as a current bare-``custom`` route so the credential step# keeps it: arriving from another provider it re-resolves ``custom`` from config and would# validate the saved ``model.base_url`` (and its key) instead of the endpoint being saved.@@ -506,7 +518,7 @@ def _validated_main_model_selection(current_model=str(model_cfg.get("default") or ""),current_base_url=base_url if is_bare_custom else str(model_cfg.get("base_url") or ""),current_api_key=api_key if is_bare_custom else "",- user_providers=cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {},+ user_providers=user_providers,custom_providers=[] if explicit_endpoint else get_compatible_custom_providers(cfg))if not result.success:raise HTTPException(status_code=400, detail=result.error_message or "model switch rejected")@@ -780,7 +792,8 @@ def _prepare_main_assignment(cfg: dict, provider: str, model: str, base_url: str``_CONFIG_MUTATION_LOCK``; it writes nothing."""if not provider or not model:raise HTTPException(status_code=400, detail="provider and model required for main")- if _is_explicit_custom_endpoint(provider, base_url):+ explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)+ if explicit_endpoint:# The Local / custom endpoint wizard: the submitted URL IS the route. Normalizing would# recover a bare ``custom`` onto the first named custom provider and keep ITS URL (#145466).provider, model = provider.strip(), model.strip()@@ -789,13 +802,16 @@ def _prepare_main_assignment(cfg: dict, provider: str, model: str, base_url: strprovider_entry = _provider_entry(cfg, provider)if not base_url and isinstance(provider_entry, dict) and provider_entry.get("base_url"):base_url = str(provider_entry.get("base_url") or "").strip()- return base_url, _validated_main_model_selection(cfg, provider, model, base_url, api_key)+ return base_url, _validated_main_model_selection(+ cfg, provider, model, base_url, api_key, explicit_endpoint=explicit_endpoint)def _apply_main_assignment_sync(cfg: dict, provider: str, model: str, base_url: str, api_key: str,prepared: "Optional[tuple[str, ModelSwitchResult]]" = None) -> dict:from hermes_cli.config import save_configfrom hermes_cli.free_tier_bootstrap import reconcile_record+ # Recorded from the SUBMITTED args, before preparation fills a URL from a named entry.+ explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)base_url, result = prepared or _prepare_main_assignment(cfg, provider, model, base_url, api_key)provider, model = result.target_provider, result.new_modelprovider_entry = _provider_entry(cfg, provider)@@ -807,7 +823,10 @@ def _apply_main_assignment_sync(cfg: dict, provider: str, model: str, base_url:endpoint_already_known = (provider_lc in {"custom", "local"} and bool(base_url) and _endpoint_known_to_config(cfg, base_url))model_cfg = _apply_main_model_assignment(cfg.get("model", {}), result, api_key)- _resolve_assignment_credentials(model_cfg, provider, provider_entry)+ if not explicit_endpoint:+ # A submitted endpoint carries its own key (or none); a ``providers.custom`` block shares+ # the selector's name, and hydrating from it would pair the new URL with the old host's key.+ _resolve_assignment_credentials(model_cfg, provider, provider_entry)cfg["model"] = model_cfgnew_provider = provider.strip().lower()diff --git a/tests/hermes_cli/test_model_set_explicit_custom_endpoint.py b/tests/hermes_cli/test_model_set_explicit_custom_endpoint.pyindex cff6e54a659..dfa0964ff6c 100644--- a/tests/hermes_cli/test_model_set_explicit_custom_endpoint.py+++ b/tests/hermes_cli/test_model_set_explicit_custom_endpoint.py@@ -104,3 +104,46 @@ def test_wizard_validation_probes_the_submitted_endpoint_not_the_saved_one(isolaassert all(url == NEW_URL for url, _ in probes), probesassert all((key or "") in {api_key, "no-key-required"} and key != "key-a" for _, key in probes), probesassert _read(isolated_home)["model"]["base_url"] == NEW_URL+++COLLIDING_URL = "http://127.0.0.1:9/v1"+SUBMITTED_URL = "http://127.0.0.2:9/v1"++[email protected]("api_key", ["key-b", ""])+def test_providers_custom_entry_never_lends_its_key_to_the_submitted_endpoint(isolated_home, monkeypatch, api_key):+ """A ``providers.custom`` entry shares its key with the bare selector the wizard submits. The+ submitted URL/key are the request's authority through validation AND persistence: the+ entry's host is never probed and its key is never written next to the new URL."""+ from hermes_cli import models_validate+ from hermes_cli.web_server_config import _apply_model_assignment_sync++ cfg = {"model": {"provider": "custom", "default": "model-a"},+ "providers": {"custom": {"base_url": COLLIDING_URL, "api_key": "key-a"}}}+ (isolated_home / "config.yaml").write_text(json.dumps(cfg), encoding="utf-8")+ probes = []++ def record(model, provider, *, api_key=None, base_url=None, **_kw):+ probes.append((base_url, api_key))+ return {"accepted": True, "persist": True, "recognized": True, "message": ""}++ monkeypatch.setattr(models_validate, "validate_requested_model", record)+ result = _apply_model_assignment_sync("main", "custom", "model-b", "", SUBMITTED_URL, api_key)++ assert result["base_url"] == SUBMITTED_URL+ assert all(url == SUBMITTED_URL and key != "key-a" for url, key in probes), probes+ model = _read(isolated_home)["model"]+ assert (model["base_url"], model.get("api_key", "")) == (SUBMITTED_URL, api_key)+++def test_bare_custom_without_url_still_selects_the_providers_custom_entry(isolated_home):+ """Control: with no submitted URL the ``providers.custom`` entry IS the selection, key included."""+ from hermes_cli.web_server_config import _apply_model_assignment_sync++ cfg = {"model": {"provider": "openrouter", "default": "model-a"},+ "providers": {"custom": {"base_url": COLLIDING_URL, "api_key": "key-a"}}}+ (isolated_home / "config.yaml").write_text(json.dumps(cfg), encoding="utf-8")+ _apply_model_assignment_sync("main", "custom", "model-b", "", "", "")++ model = _read(isolated_home)["model"]+ assert (model["base_url"], model.get("api_key", "")) == (COLLIDING_URL, "key-a")