Skip to content

The agent that grows with you

fix(cli): keep key_env when saving a legacy custom endpoint

686f6c61bd0affe5e5f7
diff --git a/hermes_cli/web_routers/config_env.py b/hermes_cli/web_routers/config_env.py
index 94c1b2eb35a..a706952b110 100644
--- a/hermes_cli/web_routers/config_env.py
+++ b/hermes_cli/web_routers/config_env.py
@@ -595,9 +595,27 @@ def _write_custom_endpoint(cfg: Dict[str, Any], body: CustomEndpointUpdate) -> T
# 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:
+ # Settings saves the row the list rendered. A legacy custom_providers
+ # entry is not in providers, so resolving only there forked a keyless
+ # twin and left the list row (and its key_env) behind.
+ from hermes_cli.config_providers import _custom_provider_entry_to_provider_config
+
+ legacy = _pop_legacy_custom_provider(cfg, endpoint_id)
+ converted = (
+ _custom_provider_entry_to_provider_config(legacy, provider_key=endpoint_id)
+ if legacy is not None else None
+ )
+ if isinstance(converted, dict):
+ existing = converted
+ elif legacy is not None:
+ restored = cfg.get("custom_providers")
+ if isinstance(restored, list):
+ restored.append(legacy)
if existing is None:
existing = {}
+
# Merge onto the existing entry rather than replacing it: a providers.<name>
# block can carry hand-written keys the dashboard has no field for
# (``key_env``/``api_key_env``, ``extra_headers`` — possibly with
diff --git a/tests/hermes_cli/test_web_server.py b/tests/hermes_cli/test_web_server.py
index 50abdef183d..c568fa8f3b9 100644
--- a/tests/hermes_cli/test_web_server.py
+++ b/tests/hermes_cli/test_web_server.py
@@ -2407,6 +2407,46 @@ CONFIG_SCHEMA = ProviderConfigSchema(
model_cfg = load_config()["model"]
assert model_cfg["api_key"] == "sk-legacy"
+ def test_saving_legacy_custom_provider_keeps_key_env(self):
+ """Save on a legacy row must carry key_env onto providers and drop the list row.
+
+ The panel omits api_key (it only shows ${KEY_ENV}). Resolving only inside
+ providers forked a keyless entry and left the legacy row, so the next
+ request 401s (#126589).
+ """
+ from hermes_cli.config import load_config, save_config, save_env_value
+
+ save_env_value("HERMES_CUSTOM_127_0_0_1_8001_API_KEY", "secret-value")
+ cfg = load_config()
+ cfg["custom_providers"] = [{
+ "name": "Qwen Local",
+ "base_url": "http://127.0.0.1:8001/v1",
+ "key_env": "HERMES_CUSTOM_127_0_0_1_8001_API_KEY",
+ "model": "qwen",
+ "api_mode": "chat_completions",
+ }]
+ save_config(cfg)
+
+ listed = {e["id"]: e for e in self.client.get("/api/providers/custom-endpoints").json()["endpoints"]}
+ assert listed["qwen-local"]["source"] == "custom_providers"
+ assert listed["qwen-local"]["has_api_key"] is True
+
+ response = self.client.post("/api/providers/custom-endpoints", json={
+ "id": "qwen-local",
+ "name": "Qwen Local",
+ "base_url": "http://127.0.0.1:8001/v1",
+ "model": "qwen",
+ })
+ assert response.status_code == 200, response.text
+
+ cfg = load_config()
+ assert cfg.get("custom_providers") == []
+ assert cfg["providers"]["qwen-local"]["key_env"] == "HERMES_CUSTOM_127_0_0_1_8001_API_KEY"
+ rows = {e["id"]: e for e in self.client.get("/api/providers/custom-endpoints").json()["endpoints"]}
+ assert rows["qwen-local"]["source"] == "providers"
+ assert rows["qwen-local"]["has_api_key"] is True
+ assert rows["qwen-local"]["api_key_preview"] == "${HERMES_CUSTOM_127_0_0_1_8001_API_KEY}"
+
def test_legacy_custom_providers_entries_get_a_row_and_can_be_deleted(self):
"""A post-migration ``custom_providers:`` list entry is still routed by the
runtime (``get_compatible_custom_providers``), so Custom Endpoints must show