Skip to content

The agent that grows with you

fix(profiles): inherit canonical and legacy custom endpoints

Gille2d3e75aab02f
diff --git a/hermes_cli/profile_model_seed.py b/hermes_cli/profile_model_seed.py
new file mode 100644
index 00000000000..c026af9bef9
--- /dev/null
+++ b/hermes_cli/profile_model_seed.py
@@ -0,0 +1,34 @@
+"""Copy the model and its selected custom endpoint when creating an independent profile."""
+
+from hermes_cli.providers import custom_provider_aliases
+from hermes_cli.config import is_provider_enabled
+
+
+def launch_model_seed(source_cfg: dict) -> dict:
+ """Keep raw credential references and the original provider schema in the new profile."""
+ model_cfg = source_cfg.get("model")
+ if not model_cfg:
+ return {}
+ seed = {"model": model_cfg}
+ provider = model_cfg.get("provider") if isinstance(model_cfg, dict) else None
+ if not isinstance(provider, str):
+ return seed
+ requested = provider.strip().lower().replace(" ", "-")
+ providers = source_cfg.get("providers")
+ for key, entry in providers.items() if isinstance(providers, dict) else ():
+ if (
+ isinstance(entry, dict)
+ and is_provider_enabled(entry)
+ and (entry.get("api") or entry.get("url") or entry.get("base_url"))
+ and requested in custom_provider_aliases(entry.get("name") or key, key)
+ ):
+ seed["providers"] = {key: entry}
+ return seed
+ custom_providers = source_cfg.get("custom_providers")
+ for entry in custom_providers if isinstance(custom_providers, list) else ():
+ if isinstance(entry, dict) and requested in custom_provider_aliases(
+ entry.get("name", ""), entry.get("provider_key", "")
+ ):
+ seed["custom_providers"] = [entry]
+ return seed
+ return seed
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py
index 58c123f8c03..c986a44a49b 100644
--- a/hermes_cli/profiles.py
+++ b/hermes_cli/profiles.py
@@ -774,22 +774,6 @@ def _read_config_model(profile_dir: Path) -> tuple:
return _cached_profile_read(config_path, "config-model", _read)
-def launch_model_seed(source_cfg: dict) -> dict:
- """The config a fresh profile needs to run the launch profile's model: its ``model`` block plus,
- when that block points at a custom ``providers:`` gateway (self-hosted / local endpoint), that
- provider's definition — ``model.provider: my-gateway`` alone is "Unknown provider" on the first
- turn. ``{}`` when the launch profile has no model."""
- model_cfg = source_cfg.get("model")
- if not model_cfg:
- return {}
- seed = {"model": model_cfg}
- providers = source_cfg.get("providers")
- name = model_cfg.get("provider") if isinstance(model_cfg, dict) else None
- if isinstance(providers, dict) and name in providers:
- seed["providers"] = {name: providers[name]}
- return seed
-
-
def _seed_model_config(profile_dir: Path) -> None:
"""Copy (not link) the active profile's model block into a fresh profile so it is usable;
profiles stay independent islands afterwards."""
@@ -799,6 +783,7 @@ def _seed_model_config(profile_dir: Path) -> None:
with contextlib.suppress(Exception): # creation must not fail over this; `hermes model` sets it later
from hermes_constants import get_hermes_home
from hermes_cli.config import atomic_config_write, read_user_config_raw
+ from hermes_cli.profile_model_seed import launch_model_seed
source = get_hermes_home() / "config.yaml"
seed = launch_model_seed(read_user_config_raw(source)) if source.is_file() else {}
if seed:
diff --git a/tests/hermes_cli/test_profile_model_seed.py b/tests/hermes_cli/test_profile_model_seed.py
new file mode 100644
index 00000000000..3d4d4182b59
--- /dev/null
+++ b/tests/hermes_cli/test_profile_model_seed.py
@@ -0,0 +1,68 @@
+"""Fresh profiles retain the selected custom endpoint, without copying its neighbors."""
+
+from pathlib import Path
+
+import pytest
+
+from hermes_cli.config import (
+ DEFAULT_CONFIG,
+ atomic_config_write,
+ get_compatible_custom_providers,
+ read_user_config_raw,
+)
+from hermes_cli.profiles import create_profile
+from hermes_cli import runtime_provider
+
+
+ "schema,provider",
+ [
+ ("providers", "lab"),
+ ("providers", "custom:lab"),
+ ("providers", "custom:display-name"),
+ ("custom_providers", "custom:display-name"),
+ ],
+)
+def test_fresh_profile_retains_resolvable_custom_endpoint(
+ tmp_path, monkeypatch, schema, provider
+):
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setenv("LAB_TOKEN", "synthetic-secret")
+ endpoint = {
+ "name": "Display Name",
+ "base_url": "http://127.0.0.1:11434/v1",
+ "api_key": "${LAB_TOKEN}",
+ "api_mode": "chat_completions",
+ }
+ unrelated = {"name": "Unrelated", "base_url": "http://127.0.0.1:11435/v1"}
+ catalog = (
+ {"lab": endpoint, "other": unrelated}
+ if schema == "providers"
+ else [endpoint, unrelated]
+ )
+ source = {
+ "model": {"provider": provider, "default": "synthetic-model"},
+ schema: catalog,
+ "_config_version": DEFAULT_CONFIG["_config_version"],
+ }
+ atomic_config_write(home / "config.yaml", source)
+ monkeypatch.setattr(runtime_provider, "load_config", lambda: source)
+ assert runtime_provider._get_named_custom_provider(provider) is not None
+
+ profile = create_profile("sample-bot", no_alias=True, no_skills=True)
+ created = read_user_config_raw(profile / "config.yaml")
+
+ assert created["model"] == source["model"]
+ copied = get_compatible_custom_providers(created)
+ assert [entry["base_url"] for entry in copied] == [endpoint["base_url"]]
+ assert copied[0]["api_key"] == endpoint["api_key"]
+ monkeypatch.setattr(runtime_provider, "load_config", lambda: created)
+ resolved = runtime_provider._get_named_custom_provider(provider)
+ assert resolved is not None
+ assert resolved["base_url"] == endpoint["base_url"]
+ source["model"] = {"provider": "nous", "default": "other-model"}
+ atomic_config_write(home / "config.yaml", source)
+ assert read_user_config_raw(profile / "config.yaml") == created
diff --git a/tests/tui_gateway/test_profiles_inherit_launch_model.py b/tests/tui_gateway/test_profiles_inherit_launch_model.py
index ca3c4d11c37..6517bf3abfe 100644
--- a/tests/tui_gateway/test_profiles_inherit_launch_model.py
+++ b/tests/tui_gateway/test_profiles_inherit_launch_model.py
@@ -8,27 +8,103 @@ definition is already in the profile — writing it after the pin never ran (#10
from __future__ import annotations
+from pathlib import Path
+
import hermes_yaml as yaml
+import pytest
+from hermes_cli.config import DEFAULT_CONFIG, atomic_config_write, read_user_config_raw
import tui_gateway.server as srv
-def test_inherit_launch_model_carries_a_custom_provider_gateway(monkeypatch, tmp_path):
+ "schema,provider",
+ [
+ ("providers", "my-gateway"),
+ ("providers", "custom:my-gateway"),
+ ("custom_providers", "custom:my-gateway"),
+ ],
+)
+def test_inherit_launch_model_carries_a_custom_provider_gateway(
+ monkeypatch, tmp_path, schema, provider
+):
home = tmp_path / ".hermes"
home.mkdir()
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setenv("FAKE_GW_TOKEN", "gw-FAKE-111")
- (home / "config.yaml").write_text(
- "model:\n provider: my-gateway\n default: my-finetune\n"
- "providers:\n my-gateway:\n api: https://llm.internal.example.com/v1\n key_env: GW_KEY\n api_key: ${FAKE_GW_TOKEN}\n"
- " unrelated:\n api: https://other.example.com/v1\n")
+ endpoint = {
+ "name": "my-gateway",
+ "base_url": "http://127.0.0.1:11434/v1",
+ "api_key": "${FAKE_GW_TOKEN}",
+ }
+ unrelated = {"name": "unrelated", "base_url": "http://127.0.0.1:11435/v1"}
+ catalog = (
+ {"my-gateway": endpoint, "unrelated": unrelated}
+ if schema == "providers"
+ else [endpoint, unrelated]
+ )
+ atomic_config_write(
+ home / "config.yaml",
+ {
+ "model": {"provider": provider, "default": "my-finetune"},
+ schema: catalog,
+ "_config_version": DEFAULT_CONFIG["_config_version"],
+ },
+ )
profile = home / "profiles" / "scout"
profile.mkdir(parents=True)
+ retained = {
+ "name": "retained",
+ "base_url": "http://127.0.0.1:11436/v1",
+ "api_key": "${RETAINED_TOKEN}",
+ }
+ retained_catalog = {"retained": retained} if schema == "providers" else [retained]
+ atomic_config_write(
+ profile / "config.yaml",
+ {
+ "tts": {"provider": "edge"},
+ schema: retained_catalog,
+ "_config_version": DEFAULT_CONFIG["_config_version"],
+ },
+ )
- assert srv._inherit_launch_model(profile) is True
+ assert vars(srv)["_inherit_launch_model"](profile) is True
- cfg = yaml.safe_load((profile / "config.yaml").read_text())
- assert (cfg["model"]["provider"], cfg["model"]["default"]) == ("my-gateway", "my-finetune")
- assert set(cfg["providers"]) == {"my-gateway"}
+ cfg = yaml.safe_load((profile / "config.yaml").read_text(encoding="utf-8"))
+ assert (cfg["model"]["provider"], cfg["model"]["default"]) == (
+ provider,
+ "my-finetune",
+ )
+ assert cfg[schema] == (
+ {"my-gateway": endpoint, "retained": retained}
+ if schema == "providers"
+ else [endpoint, retained]
+ )
# The gateway travels as the launch file wrote it: the ref, never the launch secret.
- assert cfg["providers"]["my-gateway"]["api_key"] == "${FAKE_GW_TOKEN}"
+ copied = (
+ cfg["providers"]["my-gateway"]
+ if schema == "providers"
+ else cfg["custom_providers"][0]
+ )
+ assert copied["api_key"] == "${FAKE_GW_TOKEN}"
+
+
+def test_inherit_launch_model_preserves_explicit_profile_selection(
+ monkeypatch, tmp_path
+):
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ atomic_config_write(
+ home / "config.yaml", {"model": {"provider": "nous", "default": "launch-model"}}
+ )
+ profile = home / "profiles" / "scout"
+ profile.mkdir(parents=True)
+ selected = {"model": {"provider": "openrouter", "default": "profile-model"}}
+ atomic_config_write(profile / "config.yaml", selected)
+ before = read_user_config_raw(profile / "config.yaml")
+
+ assert vars(srv)["_inherit_launch_model"](profile) is False
+ assert read_user_config_raw(profile / "config.yaml") == before
diff --git a/tui_gateway/methods_profiles.py b/tui_gateway/methods_profiles.py
index 3ad67aaa0b2..1bcbc694adc 100644
--- a/tui_gateway/methods_profiles.py
+++ b/tui_gateway/methods_profiles.py
@@ -518,16 +518,27 @@ def _inherit_launch_model(path) -> bool:
model_cfg = launch_cfg.get("model") or {}
if not (model_cfg.get("provider") and model_cfg.get("default")):
return False
- # A custom `providers:` gateway travels with the model it backs (same seed as the CLI path). It is
+ # A custom endpoint travels with the model it backs (same seed as the CLI path). It is
# written BEFORE the pin: the pin validates the pick inside the new profile, and an empty profile
# rejects a provider it has not been told about ("Unknown provider").
# Seeded from the RAW launch file so a ${VAR} api_key travels as the ref, not its value.
- custom = _lazy("hermes_cli.profiles", "launch_model_seed")(read_user_config_raw()).get("providers")
- if custom:
- from hermes_cli.config import load_config, save_config
+ seed = _lazy("hermes_cli.profile_model_seed", "launch_model_seed")(read_user_config_raw())
+ if seed.get("providers") or seed.get("custom_providers"):
+ from hermes_cli.config import save_config
+ from hermes_cli.providers import custom_provider_aliases
with _hermes_home_scope(path):
- cfg = load_config()
- cfg["providers"] = {**(cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {}), **custom}
+ cfg = read_user_config_raw()
+ if seed.get("providers"):
+ existing_providers = cfg.get("providers")
+ cfg["providers"] = {**(existing_providers if isinstance(existing_providers, dict) else {}),
+ **seed["providers"]}
+ if custom := seed.get("custom_providers"):
+ aliases = custom_provider_aliases(custom[0].get("name", ""), custom[0].get("provider_key", ""))
+ existing = cfg.get("custom_providers")
+ cfg["custom_providers"] = custom + [
+ entry for entry in (existing if isinstance(existing, list) else [])
+ if not isinstance(entry, dict) or not aliases.intersection(
+ custom_provider_aliases(entry.get("name", ""), entry.get("provider_key", "")))]
save_config(cfg)
_pin_profile_model(path, str(model_cfg["provider"]), str(model_cfg["default"]))
return True