The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(profiles): simplify the model seed and trim its tests
kshitijk4poord9dfb1243b20
diff --git a/hermes_cli/profile_model_seed.py b/hermes_cli/profile_model_seed.pyindex 74451746571..dae0221d8d4 100644--- a/hermes_cli/profile_model_seed.py+++ b/hermes_cli/profile_model_seed.py@@ -1,17 +1,20 @@"""Copy the model and its selected custom endpoint when creating an independent profile."""-from hermes_cli.config import (- find_provider_entry,- get_compatible_custom_providers,-)+from hermes_cli.config import find_provider_entry+from hermes_cli.config_providers import _normalize_custom_provider_entryfrom hermes_cli.runtime_provider_custom import (_get_named_custom_provider,+ _normalize_custom_provider_name,_shadowed_by_builtin,)def launch_model_seed(source_cfg: dict) -> dict:- """Keep raw credential references and the original provider schema in the new profile."""+ """The config a fresh profile needs to run the launch profile's model: its ``model`` block plus+ the endpoint definition it points at. ``model.provider: custom:lab`` alone is "Unknown provider"+ on the first turn. The selected ``providers:`` or ``custom_providers:`` entry is copied as+ written (``${VAR}`` refs stay refs), and an exact ``providers.<built-in>`` / ``providers.custom``+ key travels for its tuning even without a URL. ``{}`` when the launch profile has no model."""model_cfg = source_cfg.get("model")if not model_cfg:return {}@@ -19,14 +22,13 @@ def launch_model_seed(source_cfg: dict) -> dict:provider = model_cfg.get("provider") if isinstance(model_cfg, dict) else Noneif not isinstance(provider, str):return seed- requested = provider.strip().lower().replace(" ", "-")+ requested = _normalize_custom_provider_name(provider)providers = source_cfg.get("providers")providers = providers if isinstance(providers, dict) else {}selected = _get_named_custom_provider(provider, config=source_cfg, resolve_credentials=False)if selected is None:- # Exact built-in keys carry provider tuning even without an endpoint URL.exact = providers.get(provider)# Bare ``custom`` runs as provider ``custom`` and reads ``providers.custom`` tuning too.if (@@ -42,8 +44,8 @@ def launch_model_seed(source_cfg: dict) -> dict:return seedcustom_providers = source_cfg.get("custom_providers")for entry in custom_providers if isinstance(custom_providers, list) else ():- normalized = get_compatible_custom_providers({"custom_providers": [entry]})- if normalized and (normalized[0]["name"], normalized[0]["base_url"]) == (+ normalized = _normalize_custom_provider_entry(entry)+ if normalized and (normalized["name"], normalized["base_url"]) == (selected["name"],selected["base_url"],):diff --git a/tests/hermes_cli/test_profile_model_seed.py b/tests/hermes_cli/test_profile_model_seed.pyindex ad68038fc9e..5b3c8a152ee 100644--- a/tests/hermes_cli/test_profile_model_seed.py+++ b/tests/hermes_cli/test_profile_model_seed.py@@ -15,29 +15,40 @@ from hermes_cli.profile_model_seed import launch_model_seedfrom hermes_cli import runtime_provider+def create_from_launch(tmp_path, monkeypatch):+ """Write *source* as the launch config, create a fresh profile, return its raw config."""+ home = tmp_path / ".hermes"+ home.mkdir()+ monkeypatch.setattr(Path, "home", lambda: tmp_path)+ monkeypatch.setenv("HERMES_HOME", str(home))++ def _create(source: dict) -> dict:+ atomic_config_write(+ home / "config.yaml",+ {**source, "_config_version": DEFAULT_CONFIG["_config_version"]},+ )+ profile = create_profile("sample-bot", no_alias=True, no_skills=True)+ return read_user_config_raw(profile / "config.yaml")++ return _create++@pytest.mark.parametrize("schema,provider,url_key",[("providers", "lab", "base_url"),- ("providers", "custom:lab", "base_url"),- ("providers", "custom:display-name", "base_url"),- ("custom_providers", "custom:display-name", "base_url"),("providers", "custom:lab", "baseUrl"),+ ("custom_providers", "custom:display-name", "base_url"),],)def test_fresh_profile_retains_resolvable_custom_endpoint(- tmp_path, monkeypatch, schema, provider, url_key+ create_from_launch, monkeypatch, schema, provider, url_key):- 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",url_key: "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 = (@@ -45,71 +56,38 @@ def test_fresh_profile_retains_resolvable_custom_endpoint(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+ source = {"model": {"provider": provider, "default": "synthetic-model"}, schema: catalog}- profile = create_profile("sample-bot", no_alias=True, no_skills=True)- created = read_user_config_raw(profile / "config.yaml")+ created = create_from_launch(source)assert created["model"] == source["model"]copied = get_compatible_custom_providers(created)assert [entry["base_url"] for entry in copied] == [endpoint[url_key]]- assert copied[0]["api_key"] == endpoint["api_key"]+ assert copied[0]["api_key"] == "${LAB_TOKEN}"monkeypatch.setattr(runtime_provider, "load_config", lambda: created)resolved = runtime_provider._get_named_custom_provider(provider)assert resolved is not Noneassert resolved["base_url"] == endpoint[url_key]- source["model"] = {"provider": "nous", "default": "other-model"}- atomic_config_write(home / "config.yaml", source)- assert read_user_config_raw(profile / "config.yaml") == created[email protected]("schema", ["providers", "custom_providers"])[email protected]("provider", ["openrouter", "custom:openrouter"])+ "schema,provider,copied",+ [+ ("providers", "openrouter", False),+ ("custom_providers", "custom:openrouter", True),+ ],+)def test_builtin_name_collision_only_copies_explicit_custom_selection(- tmp_path, monkeypatch, schema, provider+ create_from_launch, schema, provider, copied):- home = tmp_path / ".hermes"- home.mkdir()- monkeypatch.setattr(Path, "home", lambda: tmp_path)- monkeypatch.setenv("HERMES_HOME", str(home))- endpoint = {- "name": "OpenRouter",- "base_url": "http://127.0.0.1:11434/v1",- "api_key": "synthetic-other-secret",- }+ endpoint = {"name": "OpenRouter", "base_url": "http://127.0.0.1:11434/v1"}catalog = {"other-endpoint": endpoint} if schema == "providers" else [endpoint]- 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)- is_custom = provider.startswith("custom:")- assert (- runtime_provider._get_named_custom_provider(provider) is not None- ) is is_custom- profile = create_profile("sample-bot", no_alias=True, no_skills=True)- created = read_user_config_raw(profile / "config.yaml")+ created = create_from_launch(+ {"model": {"provider": provider, "default": "synthetic-model"}, schema: catalog}+ )- assert created["model"] == source["model"]- if is_custom:- 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)- assert runtime_provider._get_named_custom_provider(provider) is not None- else:- assert "providers" not in created- assert "custom_providers" not in created+ assert bool(get_compatible_custom_providers(created)) is copied@pytest.mark.parametrize(@@ -123,27 +101,12 @@ def test_builtin_name_collision_only_copies_explicit_custom_selection(},],)-def test_fresh_profile_keeps_explicit_builtin_provider_settings(- tmp_path, monkeypatch, model-):- home = tmp_path / ".hermes"- home.mkdir()- monkeypatch.setattr(Path, "home", lambda: tmp_path)- monkeypatch.setenv("HERMES_HOME", str(home))- provider = model["provider"]- source = {- "model": model,- "providers": {provider: {"request_timeout_seconds": 150}},- "_config_version": DEFAULT_CONFIG["_config_version"],- }- atomic_config_write(home / "config.yaml", source)+def test_fresh_profile_keeps_exact_key_provider_tuning(create_from_launch, model):+ providers = {model["provider"]: {"request_timeout_seconds": 150}}- profile = create_profile("sample-bot", no_alias=True, no_skills=True)+ created = create_from_launch({"model": model, "providers": providers})- assert (- read_user_config_raw(profile / "config.yaml")["providers"]- == source["providers"]- )+ assert created["providers"] == providers@pytest.mark.parametrize(diff --git a/tests/tui_gateway/test_profiles_inherit_launch_model.py b/tests/tui_gateway/test_profiles_inherit_launch_model.pyindex 6517bf3abfe..0783cdae713 100644--- a/tests/tui_gateway/test_profiles_inherit_launch_model.py+++ b/tests/tui_gateway/test_profiles_inherit_launch_model.py@@ -20,7 +20,6 @@ import tui_gateway.server as srv@pytest.mark.parametrize("schema,provider",[- ("providers", "my-gateway"),("providers", "custom:my-gateway"),("custom_providers", "custom:my-gateway"),],@@ -69,7 +68,7 @@ def test_inherit_launch_model_carries_a_custom_provider_gateway(},)- assert vars(srv)["_inherit_launch_model"](profile) is True+ assert srv._inherit_launch_model(profile) is Truecfg = yaml.safe_load((profile / "config.yaml").read_text(encoding="utf-8"))assert (cfg["model"]["provider"], cfg["model"]["default"]) == (@@ -106,5 +105,5 @@ def test_inherit_launch_model_preserves_explicit_profile_selection(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 srv._inherit_launch_model(profile) is Falseassert read_user_config_raw(profile / "config.yaml") == before